iframesites.php 8.49 KB
Newer Older
1
2
3
4
5
6
<?php
/**
 *
 * @package    mahara
 * @subpackage admin
 * @author     Richard Mansfield
7
8
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL version 3 or later
 * @copyright  For copyright information on Mahara, please see the README file distributed with this software.
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
 *
 */

define('INTERNAL', 1);
define('ADMIN', 1);
define('MENUITEM', 'configextensions/iframesites');
define('SECTION_PLUGINTYPE', 'core');
define('SECTION_PLUGINNAME', 'admin');
define('SECTION_PAGE', 'iframesites');

require(dirname(dirname(dirname(__FILE__))) . '/init.php');
require_once('pieforms/pieform.php');
require_once('upgrade.php');
define('TITLE', get_string('allowediframesites', 'admin'));

$iframesources = get_records_menu('iframe_source', '', '', 'name,prefix');
$iframedomains = get_records_menu('iframe_source_icon');

$newform = pieform(array(
    'name'     => 'newurl',
    'elements' => array(
        'url' => array(
            'type'        => 'text',
            'title'       => get_string('Site'),
            'description' => get_string('iframeurldescription', 'admin'),
            'rules'       => array(
                'minlength' => 4,
                'maxlength' => 255,
                'regex'     => '/^[a-zA-Z0-9\/\._-]+$/',
            ),
        ),
        'name' => array(
            'type'        => 'text',
            'title'       => get_string('displayname'),
            'description' => get_string('iframedisplaynamedescription', 'admin'),
            'rules'       => array('minlength' => 1, 'maxlength' => 100),
        ),
        'submit' => array(
            'type'        => 'submit',
            'value'       => get_string('add'),
        ),
    ),
));

$editurls = array();
$i = 0;
foreach ($iframesources as $url => $name) {
    $elements = array(
        'url' => array(
            'type' => 'hidden',
            'value' => $url,
        ),
        'name' => array(
            'type' => 'text',
            'title' => get_string('displayname'),
            'description' => get_string('iframedisplaynamedescription', 'admin'),
            'defaultvalue' => $name,
        ),
        'icon' => array(
            'type' => 'text',
            'title' => get_string('iframeiconhost', 'admin'),
            'description' => get_string('iframeiconhostdescription', 'admin'),
            'defaultvalue' => $iframedomains[$name],
            'rules' => array(
                'minlength' => 4,
                'maxlength' => 253,
                'regex'     => '/^[a-zA-Z0-9-]{1,63}(\.[a-zA-Z0-9-]{1,63})+$/', // approximately
            ),
        ),
        'submit' => array(
            'type'  => 'submit',
            'value' => get_string('save'),
        ),
    );
    $editurls[$i] = array(
        'id'         => $i,
        'url'        => $url,
        'name'       => $name,
        'icon'       => favicon_display_url($iframedomains[$name]),
        'editform'   => pieform(array(
            'name'             => 'editurl_' . $i,
            'successcallback'  => 'editurl_submit',
            'elements'         => $elements,
        )),
        'deleteform' => pieform(array(
            'name'             => 'deleteurl_' . $i,
            'successcallback'  => 'deleteurl_submit',
            'renderer'         => 'oneline',
            'class'            => 'oneline inline',
            'elements'         => array(
                'url'  => array(
                    'type'         => 'hidden',
                    'value'        => $url,
                ),
                'submit' => array(
                    'type'         => 'image',
105
                    'src'          => $THEME->get_image_url('btn_deleteremove'),
106
                    'alt'          => get_string('deletespecific', 'mahara', $name),
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
                    'elementtitle' => get_string('delete'),
                    'confirm'      => get_string('confirmdeletemenuitem', 'admin'),
                ),
            ),
        )),
    );
    $i++;
}

function editurl_submit(Pieform $form, $values) {
    global $iframesources, $iframedomains, $SESSION;

    if (isset($iframesources[$values['url']])) {
        $oldname = $iframesources[$values['url']];
        $newname = strip_tags($values['name']);
        $iframesources[$values['url']] = $newname;

        db_begin();

        // Update the icon list if necessary
        if (!isset($iframedomains[$newname])) {
            insert_record('iframe_source_icon', (object) array('name' => $newname, 'domain' => $values['icon']));
        }
        else if ($iframedomains[$newname] != $values['icon']) {
            set_field('iframe_source_icon', 'domain', $values['icon'], 'name', $newname);
        }

        set_field('iframe_source', 'name', $newname, 'prefix', $values['url']);
        if (!in_array($oldname, $iframesources)) {
            delete_records('iframe_source_icon', 'name', $oldname);
        }

        update_safe_iframe_regex();
        db_commit();

        $SESSION->add_ok_msg(get_string('itemupdated'));
    }
    else {
        $SESSION->add_error_msg(get_string('updatefailed'));
    }

    redirect('/admin/extensions/iframesites.php');
}

function deleteurl_submit(Pieform $form, $values) {
    global $iframesources, $iframedomains, $SESSION;

    if (isset($iframesources[$values['url']])) {
        $oldname = $iframesources[$values['url']];

        db_begin();
        delete_records('iframe_source', 'prefix', $values['url']);

        // If this was the last site using this name, remove it from
        // the domain list too.
        unset($iframesources[$values['url']]);
        if (!in_array($oldname, $iframesources)) {
            delete_records('iframe_source_icon', 'name', $oldname);
        }

        update_safe_iframe_regex();
        db_commit();

        $SESSION->add_ok_msg(get_string('itemdeleted'));
    }
    else {
        $SESSION->add_error_msg(get_string('deletefailed', 'admin'));
    }

    redirect('/admin/extensions/iframesites.php');
}

function newurl_validate(Pieform $form, $values) {
    global $iframesources;

    if (!$urldata = process_allowed_iframe_url($values['url'])) {
        $form->set_error('url', get_string('iframeinvalidsite', 'admin'));
    }
    if (isset($iframesources[$urldata['key']])) {
186
        $form->set_error('url', get_string('urlalreadyexists', 'admin'));
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
    }
}

function newurl_submit(Pieform $form, $values) {
    global $iframesources, $iframedomains;

    $urldata = process_allowed_iframe_url($values['url']);

    db_begin();
    if (!isset($iframedomains[$values['name']])) {
        insert_record('iframe_source_icon', (object) array('name' => $values['name'], 'domain' => strtolower($urldata['domain'])));
    }
    insert_record('iframe_source', (object) array('prefix' => $urldata['key'], 'name' => strip_tags($values['name'])));
    update_safe_iframe_regex();
    db_commit();
    redirect('/admin/extensions/iframesites.php');
}

/**
 * To generate the htmlpurifier URI.SafeIframeRegexp for allowed iframe
 * sites, we'll only use the host and path parts of the given url, and
 * just strip out the rest.
 *
 * @param string $url A URL prefix to be used in the htmlpurifier regex
 *
 * @return array with the following elements
 *               key: key for the iframesources array and to use in the regex,
 *               domain: domain to use when fetching a favicon for the site.
 */
function process_allowed_iframe_url($url) {
    $parts = parse_url($url);

    if (isset($parts['scheme'])) {
        return false;
    }

    // Add 'http://' here just to run it through the url validator.
    $tovalidate = 'http://' . $url;

    if (!filter_var($tovalidate, FILTER_VALIDATE_URL)) {
        return false;
    }

    $parts = parse_url($tovalidate);

    if (isset($parts['host'])) {
        $domain = $parts['host'];
        $key = $parts['host'] . (isset($parts['path']) ? $parts['path'] : '');
    }
    else {
        $domain = $key = $parts['path'];
    }

    // Add a trailing slash if there's no path part in the given site,
    // to stop the user entering something obviously silly.
    if (strpos($key, '/') === false) {
        $key .= '/';
    }

    return array('domain' => $domain, 'key' => $key);
}

$js = <<<EOF
\$j(function() {
    \$j('.url-open-editform').click(function(e) {
        e.preventDefault();
        \$j('#' + this.id + '-form').toggleClass('js-hidden');
    });
});
EOF;

258
$smarty = smarty();
259
$smarty->assign('PAGEHEADING', TITLE);
Naomi Guyer's avatar
Naomi Guyer committed
260
$smarty->assign('PAGEICON', 'fa fa-puzzle-piece');
261
262
263
264
$smarty->assign('INLINEJAVASCRIPT', $js);
$smarty->assign('editurls', $editurls);
$smarty->assign('newform', $newform);
$smarty->display('admin/extensions/iframesites.tpl');