index.php 56.5 KB
Newer Older
1 2 3 4
<?php
/**
 *
 * @package    mahara
5
 * @subpackage auth-webservice
6
 * @author     Catalyst IT Ltd
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
 *
 */

define('INTERNAL', 1);
define('ADMIN', 1);
require(dirname(dirname(dirname(__FILE__))) . '/init.php');
15
define('MENUITEM', 'webservices/config');
16 17 18 19 20 21 22

$path = get_config('docroot') . 'lib/zend';
set_include_path($path . PATH_SEPARATOR . get_include_path());
require_once(get_config('docroot').'webservice/lib.php');
require_once(get_config('docroot') . 'api/xmlrpc/lib.php');
define('TITLE', get_string('webservices_title', 'auth.webservice'));

23 24
safe_require('auth', 'webservice');
$heading = get_string('webservices_title', 'auth.webservice');
25
$form = get_config_options_extended();
26 27
$serviceenabled = get_string('webservicesenabled', 'auth.webservice');
$servicenotenabled = get_string('webservicesnotenabled', 'auth.webservice');
28

29 30
$inlinejs = <<<JS
    jQuery(function ($) {
31 32
        var webservicesenabled = '$serviceenabled';
        var webservicesnotenabled = '$servicenotenabled';
33
        function save_protos_switch(name) {
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
            // Check if we have master switch and at least one protocol active
            var master = $('#activate_webservices').find('input:checkbox');
            var target = $('#activate_webservices').siblings('.form-group').find('input:checkbox');

            if ($(master).filter(':checked').length) {
                // master switch is on
                if (target.filter(':checked').length === 0) {
                    $('#needprotocols').removeClass('text-success').addClass('text-danger').text(webservicesnotenabled);
                }
                else {
                    $('#needprotocols').removeClass('text-danger').addClass('text-success').text(webservicesenabled);
                }
            }
            else {
                $('#needprotocols').removeClass('text-danger').removeClass('text-success').text('');
            }

            // Save new state
52 53 54 55 56 57
            if (!$('#ajax_' + name).length) {
                $('#activate_webservice_protos_' + name).append('<input id="ajax_' + name + '" type="hidden" name="ajax" value="1">');
            }
            $.post('index.php', $('#activate_webservice_protos_' + name).serialize());
        }

58
        if (!$('#needprotocols').length) {
59
            $('#activate_webservices_enabled_container').append('<span id="needprotocols" class="form-message-inline"></span>');
60 61 62 63
        }

        $('#activate_webservices_enabled').on('change', function() {
            var target = $(this).closest('form').siblings('.form-group').find('input:checkbox');
64 65
            if ($(this).is(':checked')) {
                // alert user to switch protocols on if none are active
66 67 68 69 70
                if (target.filter(':checked').length === 0) {
                    $('#needprotocols').removeClass('text-success').addClass('text-danger').text(webservicesnotenabled);
                }
                else {
                    $('#needprotocols').removeClass('text-danger').addClass('text-success').text(webservicesenabled);
71 72 73
                }
            }
            else {
74 75
                // turn all protocols off
                target.prop('checked', false);
76
                $('#activate_webservices').append('<input type="hidden" name="ajax" value="1">');
77
                $('#activate_webservices_pseudofieldset .form-message-inline').text('');
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
            }
            // save master switch form
            $.post('index.php', $('#activate_webservices').serialize());
        });
        // saving the form when switching the protocols
        $('#activate_webservice_protos_soap_enabled').change(function() {
            save_protos_switch('soap');
        });
        $('#activate_webservice_protos_xmlrpc_enabled').change(function() {
            save_protos_switch('xmlrpc');
        });
        $('#activate_webservice_protos_rest_enabled').change(function() {
            save_protos_switch('rest');
        });
        $('#activate_webservice_protos_oauth_enabled').change(function() {
            save_protos_switch('oauth');
        });
95 96

        $('#activate_connections_enabled').on('change', function() {
Robert Lyon's avatar
Robert Lyon committed
97
            // save master connection switch form
98 99
            $.post('index.php', $('#activate_connections').serialize());
        });
100 101 102
    });
JS;

103 104 105
$smarty = smarty();
setpageicon($smarty, 'icon-puzzle-piece');

106
$smarty->assign('form', $form);
107
$smarty->assign('opened', param_alphanumext('open', ''));
108
$smarty->assign('PAGEHEADING', $heading);
109
$smarty->assign('INLINEJAVASCRIPT', $inlinejs);
110 111
$smarty->assign('pagedescription', get_string('webservicesconfigdesc', 'auth.webservice'));
$smarty->display('auth:webservice:configform.tpl');
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134

/* pieforms callback for activate_webservices for
 */
function activate_webservices_submit(Pieform $form, $values) {

    $enabled = $values['enabled'] ? 0 : 1;
    set_config('webservice_enabled', $enabled);

    // reload/upgrade the web services configuration
    if ($enabled) {
        // ensure that we have a webservice auth_instance
        $authinstance = get_record('auth_instance', 'institution', 'mahara', 'authname', 'webservice');
        if (empty($authinstance)) {
            $authinstance = (object)array(
                   'instancename' => 'webservice',
                    'priority'     => 2,
                    'institution'  => 'mahara',
                    'authname'     => 'webservice',
            );
            insert_record('auth_instance', $authinstance);
        }
        external_reload_webservices();
    }
135 136 137 138 139 140 141
    if (!empty($_POST['ajax'])) {
        $protos = array('soap','xmlrpc','rest','oauth');
        foreach ($protos as $proto) {
            set_config('webservice_'.$proto.'_enabled', 0);
        }
        exit;
    }
Robert Lyon's avatar
Robert Lyon committed
142
    redirect('/webservice/admin/index.php');
143 144 145 146 147 148
}

function activate_webservice_proto_submit(Pieform $form, $values) {
    $enabled = $values['enabled'] ? 0 : 1;
    $proto = $values['protocol'];
    set_config('webservice_'.$proto.'_enabled', $enabled);
149 150 151
    if (!empty($_POST['ajax'])) {
        exit;
    }
152
    redirect('/webservice/admin/index.php');
153 154 155 156 157 158 159 160 161 162 163 164
}

function webservices_function_groups_submit(Pieform $form, $values) {
    global $SESSION;

    if ($values['action'] == 'add') {
        $service = preg_replace('/[^a-zA-Z0-9_ ]+/', '', $values['service']);
        $service = trim($service);
        if (empty($service) || record_exists('external_services', 'name', $service)) {
            $SESSION->add_error_msg(get_string('invalidinput', 'auth.webservice'));
        }
        else {
165
            $service = array('name' => $service, 'restrictedusers' => 0, 'enabled' => 0, 'tokenusers' => 0, 'component' => 'webservice', 'ctime' => db_format_timestamp(time()));
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
            insert_record('external_services', $service);
            $SESSION->add_ok_msg(get_string('configsaved', 'auth.webservice'));
        }
    }
    else {
        $service = get_record('external_services', 'id', $values['service']);
        if (!empty($service)) {
            if ($values['action'] == 'edit') {
                redirect('/webservice/admin/serviceconfig.php?service=' . $values['service']);
            }
            else if ($values['action'] == 'delete') {
                // remove everything associated with a service
                $params = array($values['service']);
                delete_records_select('external_tokens', "externalserviceid  = ?", $params);
                delete_records_select('external_services_users', "externalserviceid  = ?", $params);
                delete_records_select('external_services_functions', "externalserviceid  = ?", $params);
                delete_records('external_services', 'id', $values['service']);
                $SESSION->add_ok_msg(get_string('configsaved', 'auth.webservice'));
            }
        }
    }

    // default back to where we came from
189
    redirect('/webservice/admin/index.php?open=webservices_function_groups');
190 191 192 193 194 195
}

function webservices_token_submit(Pieform $form, $values) {
    global $SESSION, $USER;

    if ($values['action'] == 'generate') {
196
        if (!empty($values['userid'][0])) {
197
            $dbuser = get_record('usr', 'id', $values['userid'][0]);
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
            if (!empty($dbuser)) {
                $services = get_records_array('external_services', 'restrictedusers', 0);
                if (empty($services)) {
                    $SESSION->add_error_msg(get_string('noservices', 'auth.webservice'));
                }
                else {
                    // just pass the first one for the moment
                    $service = array_shift($services);
                    $token = webservice_generate_token(EXTERNAL_TOKEN_PERMANENT, $service, $dbuser->id);
                    $dbtoken = get_record('external_tokens', 'token', $token);
                    redirect('/webservice/admin/tokenconfig.php?token=' . $dbtoken->id);
                }
            }
            else {
                $SESSION->add_error_msg(get_string('invaliduserselected', 'auth.webservice'));
            }
        }
        else {
            $SESSION->add_error_msg(get_string('nouser', 'auth.webservice'));
        }

    }
    else {
        $token = get_record('external_tokens', 'id', $values['token']);
        if (!empty($token)) {
            if ($values['action'] == 'edit') {
                redirect('/webservice/admin/tokenconfig.php?token=' . $values['token']);
            }
            else if ($values['action'] == 'delete') {
                // remove everything associated with a service
                $params = array($values['token']);
                delete_records_select('external_tokens', "id = ?", $params);
                $SESSION->add_ok_msg(get_string('configsaved', 'auth.webservice'));
            }
        }
    }

    // default back to where we came from
236
    redirect('/webservice/admin/index.php?open=webservices_token');
237 238 239 240 241 242
}

function webservices_user_submit(Pieform $form, $values) {
    global $SESSION, $USER;

    if ($values['action'] == 'add') {
243
        if (!empty($values['userid'][0])) {
244
            $dbuser = get_record('usr', 'id', $values['userid'][0]);
245 246 247 248 249 250 251 252 253 254 255 256 257 258
            if ($auth_instance = webservice_validate_user($dbuser)) {
                // make sure that this account is not already in use
                $existing = get_record('external_services_users', 'userid', $dbuser->id);
                if (empty($existing)) {
                    $services = get_records_array('external_services', 'restrictedusers', 1);
                    if (empty($services)) {
                      $SESSION->add_error_msg(get_string('noservices', 'auth.webservice'));
                    }
                    else {
                        // just pass the first one for the moment
                        $service = array_shift($services);
                        $dbserviceuser = (object) array('externalserviceid' => $service->id,
                                        'userid' => $dbuser->id,
                                        'institution' => $auth_instance->institution,
259
                                        'ctime' => db_format_timestamp(time()),
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
                                        'publickeyexpires' => time(),
                                        'wssigenc' => 0,
                                        'publickey' => '');
                        $dbserviceuser->id = insert_record('external_services_users', $dbserviceuser, 'id', true);
                        redirect('/webservice/admin/userconfig.php?suid=' . $dbserviceuser->id);
                    }
                }
                else {
                    $SESSION->add_error_msg(get_string('duplicateuser', 'auth.webservice'));
                }
            }
            else {
                $SESSION->add_error_msg(get_string('invaliduserselected', 'auth.webservice'));
            }
        }
        else {
            $SESSION->add_error_msg(get_string('nouser', 'auth.webservice'));
        }
    }
    else {
        $dbserviceuser = get_record('external_services_users', 'id', $values['suid']);
        if (!empty($dbserviceuser)) {
            if ($values['action'] == 'edit') {
                redirect('/webservice/admin/userconfig.php?suid=' . $values['suid']);
            }
            else if ($values['action'] == 'delete') {
                // remove everything associated with a service
                $params = array($values['suid']);
                delete_records_select('external_services_users', "id = ?", $params);
                $SESSION->add_ok_msg(get_string('configsaved', 'auth.webservice'));
            }
        }
    }

    // default back to where we came from
295
    redirect('/webservice/admin/index.php?open=webservices_user');
296 297 298 299 300 301 302 303 304 305
}

/**
 * Form layout for webservices master switch fieldset
 *
 * @return pieforms $element array
 */
function webservices_master_switch_form() {
    // enable/disable webservices completely
    $enabled = (get_config('webservice_enabled') || 0);
306 307 308 309
    $element = array(
        'type'  => 'html',
        'value' =>
            pieform(
310
                array(
311 312 313 314 315 316 317 318 319 320 321 322
                    'name' => 'activate_webservices',
                    'elementclasses' => false,
                    'class'           => 'form-switch',
                    'successcallback' => 'activate_webservices_submit',
                    'renderer' => 'div',
                    'jsform' => false,
                    'checkdirtychange' => false,
                    'elements' => array(
                        'plugintype' => array('type' => 'hidden', 'value' => 'auth'),
                        'type' => array('type' => 'hidden', 'value' => 'webservice'),
                        'pluginname' => array('type' => 'hidden', 'value' => 'webservice'),
                        'enabled' => array('type' => 'switchbox',
Robert Lyon's avatar
Robert Lyon committed
323
                            'class' => 'switchbox last',
324
                            'value' => $enabled,
325
                            'labelhtml' => '<span class="pseudolabel">'. get_string('control_webservices1', 'auth.webservice') .'</span>',
326
                        ),
327 328 329 330
                    ),
                )
            ),
    );
331

332
    return $element;
333 334 335 336 337 338 339 340 341 342
}

/**
 * Form layout for webservices protocol switch fieldset
 *
 * @return pieforms $element array
 */
function webservices_protocol_switch_form() {
    // enable/disable separate protocols of SOAP/XML-RPC/REST
    $elements = array();
343 344 345 346
    $elements['label'] = array(
        'title' => ' ',
        'type' => 'html',
        'class' => 'fake-form',
347
        'value' => '<h4 class="title">' . get_string('protocol', 'auth.webservice') . '</h4>'
348
    );
349 350 351 352 353 354

    foreach (array('soap', 'xmlrpc', 'rest', 'oauth') as $proto) {
        $enabled = (get_config('webservice_' . $proto . '_enabled') || 0);
        $elements[$proto] =  array(
            'title' => ' ',
            'type' => 'html',
Naomi Guyer's avatar
Naomi Guyer committed
355
            'class'            => 'fake-form',
356 357 358 359
            'value' =>
        pieform(array(
            'name'            => 'activate_webservice_protos_' . $proto,
            'elementclasses'  => false,
360
            'class'           => 'form-switch',
361
            'renderer'        => 'div',
362 363
            'successcallback' => 'activate_webservice_proto_submit',
            'jsform'          => false,
364
            'checkdirtychange' => false,
365
            'elements' => array(
366

367 368 369 370
                'plugintype' => array('type' => 'hidden', 'value' => 'auth'),
                'type'       => array('type' => 'hidden', 'value' => 'webservice'),
                'pluginname' => array('type' => 'hidden', 'value' => 'webservice'),
                'protocol'   => array('type' => 'hidden', 'value' => $proto),
371
                'enabled'    => array('type' => 'switchbox',
Robert Lyon's avatar
Robert Lyon committed
372
                                      'class' => 'switchbox',
373
                                      'value' => $enabled,
Naomi Guyer's avatar
Naomi Guyer committed
374
                                      'labelhtml' => '<span class="pseudolabel">' .get_string($proto, 'auth.webservice') . ': </span>',
375
                                      ),
376 377 378 379 380 381 382 383 384 385 386 387 388
            ),
        )));
    }

    return $elements;
}

/**
 * Service Function Groups edit form
 *
 * @return html
 */
function service_fg_edit_form() {
389
    global $THEME;
390

391 392
    $editicon = 'icon icon-pencil left';
    $deleteicon = 'icon icon-trash left text-danger';
393

394 395 396 397 398 399 400 401
    $form = array(
        'name'            => 'webservices_function_groups',
        'elementclasses'  => false,
        'successcallback' => 'webservices_function_groups_submit',
        'renderer'   => 'multicolumntable',
        'elements'   => array(
                        'servicegroup' => array(
                            'title' => ' ',
402
                            'datatable' => true,
403 404 405 406 407
                            'type'  => 'html',
                            'value' => get_string('service', 'auth.webservice'),
                        ),
                        'component' => array(
                            'title' => ' ',
408
                            'datatable' => true,
409 410 411 412 413
                            'type'  => 'html',
                            'value' => get_string('component', 'auth.webservice'),
                        ),
                        'enabled' => array(
                            'title' => ' ',
414
                            'datatable' => true,
415
                            'type'  => 'html',
416
                            'value' => get_string('enabled'),
417 418 419
                        ),
                        'restricted' => array(
                            'title' => ' ',
420
                            'datatable' => true,
421 422 423 424 425
                            'type'  => 'html',
                            'value' => get_string('restrictedusers', 'auth.webservice'),
                        ),
                        'tokenusers' => array(
                            'title' => ' ',
426
                            'datatable' => true,
427 428 429 430 431
                            'type'  => 'html',
                            'value' => get_string('fortokenusers', 'auth.webservice'),
                        ),
                        'functions' => array(
                            'title' => ' ',
432
                            'datatable' => true,
433 434 435
                            'type'  => 'html',
                            'value' => get_string('functions', 'auth.webservice'),
                        ),
436 437 438 439 440 441
                        'actions' => array(
                            'title' => ' ',
                            'datatable' => true,
                            'type'  => 'html',
                            'value' => '',
                        ),
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
                    ),
        );

    $dbservices = get_records_array('external_services', null, null, 'name');
    if ($dbservices) {
        foreach ($dbservices as $service) {
            $form['elements']['id'. $service->id . '_service'] = array(
                'value'        =>  $service->name,
                'type'         => 'html',
                'key'          => $service->name,
            );
            $form['elements']['id'. $service->id . '_component'] = array(
                'value'        =>  $service->component,
                'type'         => 'html',
                'key'          => $service->name,
            );
            $form['elements']['id'. $service->id . '_enabled'] = array(
459 460 461
                'value'        => (($service->enabled == 1) ? display_icon('enabled') : display_icon('disabled')),
                'type'         => 'html',
                'class'        => 'center',
462 463 464
                'key'          => $service->name,
            );
            $form['elements']['id'. $service->id . '_restricted'] = array(
465 466 467
                'value' => (($service->restrictedusers == 1) ?  display_icon('enabled') : display_icon('disabled')),
                'type'         => 'html',
                'class'        => 'center',
468 469 470
                'key'          => $service->name,
            );
            $form['elements']['id'. $service->id . '_tokenusers'] = array(
471 472 473
                'value' => (($service->tokenusers == 1) ?  display_icon('enabled') : display_icon('disabled')),
                'type'         => 'html',
                'class'        => 'center',
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491
                'key'          => $service->name,
            );
            $functions = get_records_array('external_services_functions', 'externalserviceid', $service->id);
            $function_list = array();
            if ($functions) {
                foreach ($functions as $function) {
                    $dbfunction = get_record('external_functions', 'name', $function->functionname);
                    $function_list[]= '<a href="' . get_config('wwwroot') . 'webservice/wsdoc.php?id=' . $dbfunction->id . '">' . $function->functionname . '</a>';
                }
            }
            $form['elements']['id'. $service->id . '_functions'] = array(
                'value'        =>  implode(', ', $function_list),
                'type'         => 'html',
                'key'          => $service->name,
            );

            // edit and delete buttons
            $form['elements']['id'. $service->id . '_actions'] = array(
492
                'value'        => pieform(array(
493 494
                                    'name'            => 'webservices_function_groups_edit_' . $service->id,
                                    'renderer'        => 'div',
Naomi Guyer's avatar
Naomi Guyer committed
495
                                    'class'  => 'form-as-button pull-left',
496 497 498 499 500 501 502
                                    'successcallback' => 'webservices_function_groups_submit',
                                    'jsform'          => false,
                                    'action'          => get_config('wwwroot') . 'webservice/admin/index.php',
                                    'elements' => array(
                                        'service'    => array('type' => 'hidden', 'value' => $service->id),
                                        'action'     => array('type' => 'hidden', 'value' => 'edit'),
                                        'submit'     => array(
503
                                                'type'  => 'button',
504
                                                'class' => 'btn-default btn-sm',
505
                                                'usebuttontag' => true,
Naomi Guyer's avatar
Naomi Guyer committed
506
                                                'value'   => '<span class="'.$editicon.'"></span>' . get_string('edit', 'mahara'),
507
                                                'elementtitle' => get_string('edit'),
508 509 510 511 512 513 514
                                            ),
                                    ),
                                ))
                                .
                                pieform(array(
                                    'name'            => 'webservices_function_groups_delete_' . $service->id,
                                    'renderer'        => 'div',
Naomi Guyer's avatar
Naomi Guyer committed
515
                                    'class'  => 'form-as-button pull-left',
516 517 518 519 520 521 522
                                    'successcallback' => 'webservices_function_groups_submit',
                                    'jsform'          => false,
                                    'action'          => get_config('wwwroot') . 'webservice/admin/index.php',
                                    'elements' => array(
                                        'service'    => array('type' => 'hidden', 'value' => $service->id),
                                        'action'     => array('type' => 'hidden', 'value' => 'delete'),
                                        'submit'     => array(
523 524
                                                'type'  => 'button',
                                                'usebuttontag' => true,
525
                                                'class' => 'btn-default btn-sm',
Naomi Guyer's avatar
Naomi Guyer committed
526
                                                'value'   => '<span class="'.$deleteicon.'"></span>' . get_string('delete', 'mahara'),
527
                                                'elementtitle' => get_string('delete'),
528 529
                                            ),
                                    ),
530
                                ))
531 532 533
                                ,
                'type'         => 'html',
                'key'          => $service->name,
534
                'class'        => 'webserviceconfigcontrols btn-group',
535 536 537
            );
        }
    }
538

539
    $pieform = pieform_instance($form);
540
    return $pieform->build(false) . '<div class="function_add">' .
Naomi Guyer's avatar
Naomi Guyer committed
541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
        pieform(array(
            'name'            => 'webservices_function_groups_add',
            'renderer'        => 'div',
            'successcallback' => 'webservices_function_groups_submit',
            'class'           => 'form-inline',
            'jsform'          => false,
            'action'          => get_config('wwwroot') . 'webservice/admin/index.php',
            'elements' => array(
                'service'    => array(
                    'type' => 'text',
                    'class' => 'last'
                ),
                'action'     => array('type' => 'hidden', 'value' => 'add'),
                'submit'     => array(
                        'type'  => 'submit',
556
                        'class' => 'submit btn-primary last',
Naomi Guyer's avatar
Naomi Guyer committed
557 558 559 560 561
                        'value' => get_string('add')
                    ),
            ),
        )) .
        '</div>';
562 563 564 565 566 567 568 569 570 571
}

/**
 * Service Tokens Groups edit form
 *
 * @return html
 */
function service_tokens_edit_form() {
    global $THEME, $USER;

572 573
    $editicon = 'icon icon-pencil left';
    $deleteicon = 'icon icon-trash left text-danger';
574

575 576 577 578
    $form = array(
        'name'            => 'webservices_tokens',
        'elementclasses'  => false,
        'successcallback' => 'webservices_tokens_submit',
579
        'renderer'   => 'multicolumntable',
580 581 582
        'elements'   => array(
                        'token' => array(
                            'title' => ' ',
583
                            'datatable'  => true,
584 585 586 587 588
                            'type'  => 'html',
                            'value' => get_string('token', 'auth.webservice'),
                        ),
                        'institution' => array(
                            'title' => ' ',
589
                            'datatable'  => true,
590 591 592 593 594
                            'type'  => 'html',
                            'value' => get_string('institution'),
                        ),
                        'username' => array(
                            'title' => ' ',
595
                            'datatable'  => true,
596 597 598 599 600
                            'type'  => 'html',
                            'value' => get_string('username', 'auth.webservice'),
                        ),
                        'servicename' => array(
                            'title' => ' ',
601
                            'datatable'  => true,
602 603 604 605 606
                            'type'  => 'html',
                            'value' => get_string('servicename', 'auth.webservice'),
                        ),
                        'enabled' => array(
                            'title' => ' ',
607
                            'datatable'  => true,
608
                            'type'  => 'html',
609
                            'value' => get_string('enabled'),
610 611 612
                        ),
                        'wssigenc' => array(
                            'title' => ' ',
613
                            'datatable'  => true,
614 615 616 617 618
                            'type'  => 'html',
                            'value' => get_string('titlewssigenc', 'auth.webservice'),
                        ),
                        'functions' => array(
                            'title' => ' ',
619
                            'datatable'  => true,
620 621 622
                            'type'  => 'html',
                            'value' => get_string('functions', 'auth.webservice'),
                        ),
623 624 625 626 627 628
                        'actions' => array(
                            'title' => ' ',
                            'datatable'  => true,
                            'type'  => 'html',
                            'value' => '',
                        ),
629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662
                    ),
        );

    $dbtokens = get_records_sql_array('SELECT et.id as tokenid, et.wssigenc AS wssigenc, et.externalserviceid as externalserviceid, et.institution as institution, u.id as userid, u.username as username, et.token as token, es.name as name, es.enabled as enabled FROM {external_tokens} AS et LEFT JOIN {usr} AS u ON et.userid = u.id LEFT JOIN {external_services} AS es ON et.externalserviceid = es.id WHERE et.tokentype = ? ORDER BY u.username', array(EXTERNAL_TOKEN_PERMANENT));
    if (!empty($dbtokens)) {
        foreach ($dbtokens as $token) {
            $form['elements']['id'. $token->tokenid . '_token'] = array(
                'value'        =>  $token->token,
                'type'         => 'html',
                'key'          => $token->token,
            );
            $dbinstitution = get_record('institution', 'name', $token->institution);
            $form['elements']['id'. $token->tokenid . '_institution'] = array(
                'value'        =>  $dbinstitution->displayname,
                'type'         => 'html',
                'key'          => $token->token,
            );
            if ($USER->is_admin_for_user($token->userid)) {
                $user_url = get_config('wwwroot') . 'admin/users/edit.php?id=' . $token->userid;
            }
            else {
                $user_url = get_config('wwwroot') . 'user/view.php?id=' . $token->userid;
            }
            $form['elements']['id'. $token->tokenid . '_username'] = array(
                'value'        =>  '<a href="' . $user_url . '">' . $token->username . '</a>',
                'type'         => 'html',
                'key'          => $token->token,
            );
            $form['elements']['id'. $token->tokenid . '_servicename'] = array(
                'value'        =>  $token->name,
                'type'         => 'html',
                'key'          => $token->token,
            );
            $form['elements']['id'. $token->tokenid . '_enabled'] = array(
663 664 665
                'value' => (($token->enabled == 1) ? display_icon('enabled') : display_icon('disabled')),
                'type'         => 'html',
                'class'        => 'center',
666 667 668
                'key'          => $token->token,
            );
            $form['elements']['id'. $token->tokenid . '_wssigenc'] = array(
669 670 671
                'value' => (($token->wssigenc == 1) ? display_icon('enabled') : display_icon('disabled')),
                'type'         => 'html',
                'class'        => 'center',
672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690
                'key'          => $token->token,
            );

            $functions = get_records_array('external_services_functions', 'externalserviceid', $token->externalserviceid);
            $function_list = array();
            if ($functions) {
                foreach ($functions as $function) {
                    $dbfunction = get_record('external_functions', 'name', $function->functionname);
                    $function_list[]= '<a href="' . get_config('wwwroot') . 'webservice/wsdoc.php?id=' . $dbfunction->id . '">' . $function->functionname . '</a>';
                }
            }
            $form['elements']['id'. $token->tokenid . '_functions'] = array(
                'value'        =>  implode(', ', $function_list),
                'type'         => 'html',
                'key'          => $token->token,
            );

            // edit and delete buttons
            $form['elements']['id'. $token->tokenid . '_actions'] = array(
691
                'value'        => pieform(array(
692 693 694 695
                                    'name'            => 'webservices_token_edit_' . $token->tokenid,
                                    'renderer'        => 'div',
                                    'elementclasses'  => false,
                                    'successcallback' => 'webservices_token_submit',
696
                                    'class'           => 'form-as-button pull-left',
697
                                    'jsform'          => false,
698 699 700 701
                                    'elements' => array(
                                        'token'      => array('type' => 'hidden', 'value' => $token->tokenid),
                                        'action'     => array('type' => 'hidden', 'value' => 'edit'),
                                        'submit'     => array(
702 703
                                                'type'  => 'button',
                                                'usebuttontag' => true,
704
                                                'class' => 'btn-default btn-sm',
705 706
                                                'value'   => '<span class="'.$editicon.'"></span>' . get_string('edit'),
                                                'elementtitle' => get_string('editspecific', 'mahara', $token->tokenid),
707 708 709 710 711 712 713 714 715
                                            ),
                                    ),
                                ))
                                .
                                pieform(array(
                                    'name'            => 'webservices_token_delete_' . $token->tokenid,
                                    'renderer'        => 'div',
                                    'elementclasses'  => false,
                                    'successcallback' => 'webservices_token_submit',
716
                                    'class'           => 'form-as-button pull-left',
717
                                    'jsform'          => false,
718 719 720 721
                                    'elements' => array(
                                        'token'      => array('type' => 'hidden', 'value' => $token->tokenid),
                                        'action'     => array('type' => 'hidden', 'value' => 'delete'),
                                        'submit'     => array(
722 723
                                                'type'  => 'button',
                                                'usebuttontag' => true,
724
                                                'class' => 'btn-default btn-sm',
725 726
                                                'value'   => '<span class="'.$deleteicon.'"></span>' . get_string('delete'),
                                                'elementtitle' => get_string('deletespecific', 'mahara', $token->tokenid),
727 728
                                            ),
                                    ),
729
                                )),
730 731
                'type'         => 'html',
                'key'          => $token->token,
732
                'class'        => 'webserviceconfigcontrols btn-group',
733 734 735
            );
        }
    }
736 737 738 739
    else {
        // no results so hide headings
        $form['elements'] = array();
    }
740 741 742 743 744 745 746 747 748 749 750

    $username = '';
    if ($user  = param_integer('user', 0)) {
        $dbuser = get_record('usr', 'id', $user);
        if (!empty($dbuser)) {
            $username = $dbuser->username;
        }
    }
    else {
        $username = param_alphanum('username', '');
    }
751
    $pieform = pieform_instance($form);
752
    return $pieform->build(false) . '<div class="function_add">' .
753 754
                            pieform(array(
                                'name'            => 'webservices_token_generate',
Naomi Guyer's avatar
Naomi Guyer committed
755
                                'renderer'        => 'div',
756 757 758 759
                                'successcallback' => 'webservices_token_submit',
                                'jsform'          => false,
                                'action'          => get_config('wwwroot') . 'webservice/admin/index.php',
                                'elements' => array(
760 761 762 763
                                    'userid' => array(
                                        'type' => 'autocomplete',
                                        'title' => get_string('username') . ': ',
                                        'ajaxurl' => get_config('wwwroot') . 'webservice/admin/users.json.php',
764
                                        'multiple' => true,
765 766
                                        'allowclear' => true,
                                        'ajaxextraparams' => array(),
767
                                        'extraparams' => array(
768
                                            'maximumSelectionLength' => 1
769 770
                                        ),
                                        'width' => '280px',
771
                                    ),
772 773 774
                                    'action'     => array('type' => 'hidden', 'value' => 'generate'),
                                    'submit'     => array(
                                            'type'  => 'submit',
775
                                            'class' => 'submit btn-primary last',
776 777 778 779
                                            'value' => get_string('generate', 'auth.webservice')
                                        ),
                                ),
                            )).
780
         '</div>';
781 782 783 784 785 786 787 788 789 790 791

}

/**
 * Service Users edit form
 *
 * @return html
 */
function service_users_edit_form() {
    global $THEME, $USER;

792 793
    $editicon = 'icon icon-pencil left';
    $deleteicon = 'icon icon-trash left text-danger';
794

795 796 797 798 799 800 801 802
    $form = array(
        'name'            => 'webservices_users',
        'elementclasses'  => false,
        'successcallback' => 'webservices_users_submit',
        'renderer'   => 'multicolumntable',
        'elements'   => array(
                        'username' => array(
                            'title' => ' ',
803
                            'datatable'  => true,
804 805 806 807 808
                            'type'  => 'html',
                            'value' => get_string('username', 'auth.webservice'),
                        ),
                        'institution' => array(
                            'title' => ' ',
809
                            'datatable'  => true,
810 811 812 813 814
                            'type'  => 'html',
                            'value' => get_string('institution'),
                        ),
                        'servicename' => array(
                            'title' => ' ',
815
                            'datatable'  => true,
816 817 818 819 820
                            'type'  => 'html',
                            'value' => get_string('servicename', 'auth.webservice'),
                        ),
                        'enabled' => array(
                            'title' => ' ',
821
                            'datatable'  => true,
822
                            'type'  => 'html',
823
                            'value' => get_string('enabled'),
824 825 826
                        ),
                        'wssigenc' => array(
                            'title' => ' ',
827
                            'datatable'  => true,
828 829 830 831 832
                            'type'  => 'html',
                            'value' => get_string('titlewssigenc', 'auth.webservice'),
                        ),
                        'functions' => array(
                            'title' => ' ',
833
                            'datatable'  => true,
834 835 836
                            'type'  => 'html',
                            'value' => get_string('functions', 'auth.webservice'),
                        ),
837 838 839 840 841 842
                        'actions' => array(
                            'title' => ' ',
                            'datatable'  => true,
                            'type'  => 'html',
                            'value' => '',
                        ),
843 844 845
                    ),
        );

846
    $dbusers = get_records_sql_array('SELECT eu.id as id, eu.userid as userid, eu.wssigenc AS wssigenc, eu.externalserviceid as externalserviceid, eu.institution as institution, u.username as username, es.name as name, es.enabled as enabled FROM {external_services_users} AS eu LEFT JOIN {usr} AS u ON eu.userid = u.id LEFT JOIN {external_services} AS es ON eu.externalserviceid = es.id ORDER BY eu.id', array());
847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871
    if (!empty($dbusers)) {
        foreach ($dbusers as $user) {
            $dbinstitution = get_record('institution', 'name', $user->institution);
            if ($USER->is_admin_for_user($user->id)) {
                $user_url = get_config('wwwroot') . 'admin/users/edit.php?id=' . $user->userid;
            }
            else {
                $user_url = get_config('wwwroot') . 'user/view.php?id=' . $user->userid;
            }
            $form['elements']['id'. $user->id . '_username'] = array(
                'value'        =>  '<a href="' . $user_url . '">' . $user->username . '</a>',
                'type'         => 'html',
                'key'          => $user->id,
            );
            $form['elements']['id'. $user->id . '_institution'] = array(
                'value'        =>  $dbinstitution->displayname,
                'type'         => 'html',
                'key'          => $user->id,
            );
            $form['elements']['id'. $user->id . '_servicename'] = array(
                'value'        =>  $user->name,
                'type'         => 'html',
                'key'          => $user->id,
            );
            $form['elements']['id'. $user->id . '_enabled'] = array(
872 873 874
                'value' => (($user->enabled == 1) ?  display_icon('enabled') : display_icon('disabled')),
                'type'         => 'html',
                'class'        => 'center',
875 876 877
                'key'          => $user->id,
            );
            $form['elements']['id'. $user->id . '_wssigenc'] = array(
878 879 880
                'value' => (($user->wssigenc == 1) ?  display_icon('enabled') : display_icon('disabled')),
                'type'         => 'html',
                'class'        => 'center',
881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899
                'key'          => $user->id,
            );

            $functions = get_records_array('external_services_functions', 'externalserviceid', $user->externalserviceid);
            $function_list = array();
            if ($functions) {
                foreach ($functions as $function) {
                    $dbfunction = get_record('external_functions', 'name', $function->functionname);
                    $function_list[]= '<a href="' . get_config('wwwroot') . 'webservice/wsdoc.php?id=' . $dbfunction->id . '">' . $function->functionname . '</a>';
                }
            }
            $form['elements']['id'. $user->id . '_functions'] = array(
                'value'        =>  implode(', ', $function_list),
                'type'         => 'html',
                'key'          => $user->id,
            );

            // edit and delete buttons
            $form['elements']['id'. $user->id . '_actions'] = array(
900
                'value'        => pieform(array(
901 902 903 904
                                    'name'            => 'webservices_user_edit_' . $user->id,
                                    'renderer'        => 'div',
                                    'elementclasses'  => false,
                                    'successcallback' => 'webservices_user_submit',
905
                                    'class'           => 'form-as-button pull-left',
906
                                    'jsform'          => false,
907 908 909 910
                                    'elements' => array(
                                        'suid'       => array('type' => 'hidden', 'value' => $user->id),
                                        'action'     => array('type' => 'hidden', 'value' => 'edit'),
                                        'submit'     => array(
911 912
                                                'type'  => 'button',
                                                'usebuttontag' => true,
913
                                                'class' => 'btn-default btn-sm',
914
                                                'value'   => '<span class="'. $editicon.'" role="presentation" aria-hidden="true"></span>' . get_string('edit'),
915
                                                'elementtitle' => get_string('editspecific', 'mahara', $user->username),
916 917 918 919 920 921 922 923 924
                                            ),
                                    ),
                                ))
                                .
                                pieform(array(
                                    'name'            => 'webservices_user_delete_' . $user->id,
                                    'renderer'        => 'div',
                                    'elementclasses'  => false,
                                    'successcallback' => 'webservices_user_submit',
925
                                    'class'           => 'form-as-button pull-left',
926
                                    'jsform'          => false,
927 928 929 930
                                    'elements' => array(
                                        'suid'       => array('type' => 'hidden', 'value' => $user->id),
                                        'action'     => array('type' => 'hidden', 'value' => 'delete'),
                                        'submit'     => array(
931 932
                                                'type'  => 'button',
                                                'usebuttontag' => true,
933
                                                'class' => 'btn-default btn-sm',
934
                                                'value'   => '<span class="'.$deleteicon.'" role="presentation" aria-hidden="true"></span>' . get_string('delete'),
935
                                                'elementtitle' => get_string('deletespecific', 'mahara', $user->username),
936 937
                                            ),
                                    ),
938
                                )),
939 940
                'type'         => 'html',
                'key'          => $user->id,
941
                'class'        => 'webserviceconfigcontrols btn-group',
942 943 944
            );
        }
    }
945 946 947 948
    else {
        // no results so hide headings
        $form['elements'] = array();
    }
949 950 951 952 953 954 955 956 957 958 959

    $username = '';
    if ($user  = param_integer('user', 0)) {
        $dbuser = get_record('usr', 'id', $user);
        if (!empty($dbuser)) {
            $username = $dbuser->username;
        }
    }
    else {
        $username = param_alphanum('username', '');
    }
960

961
    $pieform = pieform_instance($form);
962
    return $pieform->build(false) . '<div id="user_add">' .
963 964
                            pieform(array(
                                'name'            => 'webservices_user_generate',
Naomi Guyer's avatar
Naomi Guyer committed
965
                                'renderer'        => 'div',
966 967 968 969
                                'successcallback' => 'webservices_user_submit',
                                'jsform'          => false,
                                'action'          => get_config('wwwroot') . 'webservice/admin/index.php',
                                'elements' => array(
970 971 972 973
                                    'userid' => array(
                                        'type' => 'autocomplete',
                                        'title' => get_string('username') . ': ',
                                        'ajaxurl' => get_config('wwwroot') . 'webservice/admin/users.json.php',
974
                                        'multiple' => true,
975 976
                                        'allowclear' => true,
                                        'ajaxextraparams' => array(),
977
                                        'extraparams' => array(
978
                                            'maximumSelectionLength' => 1
979 980
                                        ),
                                        'width' => '280px',
981
                                    ),
982 983 984
                                    'action'     => array('type' => 'hidden', 'value' => 'add'),
                                    'submit'     => array(
                                            'type'  => 'submit',
985
                                            'class' => 'submit btn-primary last',
986 987 988 989
                                            'value' => get_string('add')
                                        ),
                                ),
                            )).
990
         '</div>';
991 992
}

993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007
/**
 * Form layout for webservices master switch fieldset
 *
 * @return pieforms $element array
 */
function webservices_connections_master_switch_form() {
    // enable/disable webservices completely
    $enabled = (get_config('webservice_connections_enabled') || 0);
    $element = array(
        'type'  => 'html',
        'value' =>
            pieform(
                array(
                    'name' => 'activate_connections',
                    'elementclasses' => false,
Robert Lyon's avatar
Robert Lyon committed
1008
                    'class'           => 'form-switch form-group',
1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036
                    'successcallback' => 'activate_connections_submit',
                    'renderer' => 'div',
                    'jsform' => false,
                    'checkdirtychange' => false,
                    'elements' => array(
                        'plugintype' => array('type' => 'hidden', 'value' => 'auth'),
                        'type' => array('type' => 'hidden', 'value' => 'webservice'),
                        'pluginname' => array('type' => 'hidden', 'value' => 'webservice'),
                        'enabled' => array('type' => 'switchbox',
                            'class' => 'switchbox last',
                            'value' => $enabled,
                            'labelhtml' => '<span class="pseudolabel">'. get_string('control_webservices_connections', 'auth.webservice') .'</span>',
                        ),
                    ),
                )
            ),
    );

    return $element;
}

/* pieforms callback for activate_connections for
 */
function activate_connections_submit(Pieform $form, $values) {

    $enabled = $values['enabled'] ? 0 : 1;
    set_config('webservice_connections_enabled', $enabled);

Robert Lyon's avatar
Robert Lyon committed
1037
    redirect('/webservice/admin/index.php');
1038 1039 1040
}


1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051
/**
 *  Custom webservices config page
 *  - activate/deactivate webservices comletely
 *  - activate/deactivat protocols - SOAP/XML-RPC/REST
 *  - manage service clusters
 *  - manage users and access tokens
 *
 *  @return pieforms $element array
 */
function get_config_options_extended() {

1052
    $protosform = array(
1053 1054
            'name'            => 'activate_webservice_protos',
            'elements'        => webservices_protocol_switch_form(),
1055
    );
1056
    $protos = pieform_instance($protosform);
1057 1058 1059 1060 1061 1062 1063

    // certificate values from MNet
    $openssl = OpenSslRepo::singleton();

    $elements = array(
            // fieldset of master switch
            'webservicesmaster' => array(
Naomi Guyer's avatar
Naomi Guyer committed
1064 1065 1066 1067
                'type' => 'fieldset',
                'legend' => get_string('protocolswitches', 'auth.webservice'),
                'elements' =>  array(
                    'protos_help' =>  array(
1068 1069
                        'type' => 'html',
                        'value' => '<div><p>' . get_string('manage_protocols', 'auth.webservice') . '</p></div>',
Naomi Guyer's avatar
Naomi Guyer committed
1070
                    ),
1071 1072
                    'masterswitchlabel' =>  array(
                        'type' => 'html',
1073
                        'value' => '<h4>' . get_string('masterswitch', 'auth.webservice') . '</h4>',
Naomi Guyer's avatar
Naomi Guyer committed
1074
                    ),
1075
                    'webservicesmasterswitchform' => webservices_master_switch_form(),
Robert Lyon's avatar
Robert Lyon committed
1076 1077 1078 1079 1080
                    'masterswitchlabel2' =>  array(
                        'type' => 'html',
                        'value' => '<h4>' . get_string('connectionsmasterswitch', 'auth.webservice') . '</h4>',
                    ),
                    'webservicesconnectionsswitchform' => webservices_connections_master_switch_form(),
1081 1082 1083 1084
                    'enablewebserviceprotos' =>  array(
                        'type' => 'html',
                        'value' => $protos->build(false),
                    )
Naomi Guyer's avatar
Naomi Guyer committed
1085 1086 1087
                ),
                'collapsible' => true,
                'collapsed'   => true,