index.php 53.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', 'configextensions/webservices');
16 17 18 19 20 21 22 23

$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'));
require_once('pieforms/pieform.php');

24 25 26
safe_require('auth', 'webservice');
$heading = get_string('webservices_title', 'auth.webservice');
$webservice_menu = PluginAuthWebservice::menu_items(MENUITEM);
27 28
$form = get_config_options_extended();

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
$inlinejs = <<<JS
    jQuery(function ($) {

        function save_protos_switch(name) {
            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());
        }

        $('#activate_webservices_enabled').change(function() {
            // open the protocols fieldset
            $('#activate_webservices_protos_pseudofieldset').closest('.pseudofieldset').removeClass('collapsed');
            if ($(this).is(':checked')) {
                // alert user to switch protocols on if none are active
                if ($('#activate_webservices_protos_pseudofieldset').closest('.pseudofieldset').find('input:checkbox:checked').length == 0) {
                    $('#activate_webservices_protos_pseudofieldset').closest('.pseudolegend').after('<div class="error">You need to enable at least one Protocol</div>');
                }
            }
            else {
                // turn all protocols off - not sure if we need this or should leave protocols on when master switch is off
                $('#activate_webservices_protos_pseudofieldset').closest('.pseudofieldset').find('input:checkbox').attr('checked', false);
                $('#activate_webservices').append('<input type="hidden" name="ajax" value="1">');
            }
            // 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');
        });
    });
JS;

72
$smarty = smarty(array(), array('<link rel="stylesheet" type="text/css" href="' . $THEME->get_url('style/webservice.css', false, 'auth/webservice') . '">'));
73
$smarty->assign('form', $form);
74
$smarty->assign('opened', param_alphanumext('open', ''));
75
$smarty->assign('TERTIARYMENU', $webservice_menu);
76
$smarty->assign('PAGEHEADING', $heading);
77
$smarty->assign('INLINEJAVASCRIPT', $inlinejs);
Naomi Guyer's avatar
Naomi Guyer committed
78 79
$smarty->assign('PAGEICON', 'fa fa-puzzle-piece');

80 81
$smarty->assign('pagedescription', get_string('webservicesconfigdesc', 'auth.webservice'));
$smarty->display('auth:webservice:configform.tpl');
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104

/* 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();
    }
105 106 107 108 109 110 111
    if (!empty($_POST['ajax'])) {
        $protos = array('soap','xmlrpc','rest','oauth');
        foreach ($protos as $proto) {
            set_config('webservice_'.$proto.'_enabled', 0);
        }
        exit;
    }
112
    redirect('/webservice/admin/index.php?open=activate_webservices');
113 114 115 116 117 118
}

function activate_webservice_proto_submit(Pieform $form, $values) {
    $enabled = $values['enabled'] ? 0 : 1;
    $proto = $values['protocol'];
    set_config('webservice_'.$proto.'_enabled', $enabled);
119 120 121
    if (!empty($_POST['ajax'])) {
        exit;
    }
122
    redirect('/webservice/admin/index.php?open=activate_webservices_protos');
123 124 125 126 127 128 129 130 131 132 133 134
}

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 {
135
            $service = array('name' => $service, 'restrictedusers' => 0, 'enabled' => 0, 'tokenusers' => 0, 'component' => 'webservice', 'ctime' => db_format_timestamp(time()));
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
            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
159
    redirect('/webservice/admin/index.php?open=webservices_function_groups');
160 161 162 163 164 165
}

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

    if ($values['action'] == 'generate') {
166
        if (!empty($values['userid'][0])) {
167
            $dbuser = get_record('usr', 'id', $values['userid'][0]);
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
            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
206
    redirect('/webservice/admin/index.php?open=webservices_token');
207 208 209 210 211 212
}

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

    if ($values['action'] == 'add') {
213
        if (!empty($values['userid'][0])) {
214
            $dbuser = get_record('usr', 'id', $values['userid'][0]);
215 216 217 218 219 220 221 222 223 224 225 226 227 228
            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,
229
                                        'ctime' => db_format_timestamp(time()),
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 258 259 260 261 262 263 264
                                        '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
265
    redirect('/webservice/admin/index.php?open=webservices_user');
266 267 268 269 270 271 272 273 274 275 276
}

/**
 * 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);
    $elements =
277 278 279 280 281 282 283 284 285
        array(
            'webservicesmasterswitchform' =>
                array(
                    'type' => 'html',
                    'value' =>
                        pieform(
                            array(
                                'name' => 'activate_webservices',
                                'elementclasses' => false,
286
                                'class'           => 'form-switch',
287 288 289
                                'successcallback' => 'activate_webservices_submit',
                                'renderer' => 'div',
                                'jsform' => false,
290
                                'checkdirtychange' => false,
291 292 293 294 295 296 297 298
                                'elements' => array(
                                    'plugintype' => array('type' => 'hidden', 'value' => 'auth'),
                                    'type' => array('type' => 'hidden', 'value' => 'webservice'),
                                    'pluginname' => array('type' => 'hidden', 'value' => 'webservice'),
                                    'enabled' => array('type' => 'switchbox',
                                        'value' => $enabled,
                                        'labelhtml' => get_string('control_webservices', 'auth.webservice'),
                                    ),
299
                                ),
300 301 302 303
                            )
                        ),
                ),
        );
304 305 306 307 308 309 310 311 312 313 314 315

    return $elements;
}

/**
 * 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();
316
    $elements['label'] = array('title' => ' ', 'type' => 'html', 'value' => '<div class="title">' . get_string('protocol', 'auth.webservice') . '</div>');
317 318 319 320 321 322 323 324 325 326

    foreach (array('soap', 'xmlrpc', 'rest', 'oauth') as $proto) {
        $enabled = (get_config('webservice_' . $proto . '_enabled') || 0);
        $elements[$proto] =  array(
            'title' => ' ',
            'type' => 'html',
            'value' =>
        pieform(array(
            'name'            => 'activate_webservice_protos_' . $proto,
            'elementclasses'  => false,
327
            'class'           => 'form-switch',
328
            'renderer'        => 'div',
329 330
            'successcallback' => 'activate_webservice_proto_submit',
            'jsform'          => false,
331
            'checkdirtychange' => false,
332
            'elements' => array(
333

334 335 336 337
                'plugintype' => array('type' => 'hidden', 'value' => 'auth'),
                'type'       => array('type' => 'hidden', 'value' => 'webservice'),
                'pluginname' => array('type' => 'hidden', 'value' => 'webservice'),
                'protocol'   => array('type' => 'hidden', 'value' => $proto),
338 339 340 341
                'enabled'    => array('type' => 'switchbox',
                                      'value' => $enabled,
                                      'labelhtml' => get_string($proto, 'auth.webservice') . ': ',
                                      ),
342 343 344 345 346 347 348 349 350 351 352 353 354
            ),
        )));
    }

    return $elements;
}

/**
 * Service Function Groups edit form
 *
 * @return html
 */
function service_fg_edit_form() {
355
    global $THEME;
356 357 358 359 360 361 362 363 364

    $form = array(
        'name'            => 'webservices_function_groups',
        'elementclasses'  => false,
        'successcallback' => 'webservices_function_groups_submit',
        'renderer'   => 'multicolumntable',
        'elements'   => array(
                        'servicegroup' => array(
                            'title' => ' ',
365
                            'datatable' => true,
366 367 368 369 370
                            'type'  => 'html',
                            'value' => get_string('service', 'auth.webservice'),
                        ),
                        'component' => array(
                            'title' => ' ',
371
                            'datatable' => true,
372 373 374 375 376
                            'type'  => 'html',
                            'value' => get_string('component', 'auth.webservice'),
                        ),
                        'enabled' => array(
                            'title' => ' ',
377
                            'datatable' => true,
378
                            'type'  => 'html',
379
                            'value' => get_string('enabled'),
380 381 382
                        ),
                        'restricted' => array(
                            'title' => ' ',
383
                            'datatable' => true,
384 385 386 387 388
                            'type'  => 'html',
                            'value' => get_string('restrictedusers', 'auth.webservice'),
                        ),
                        'tokenusers' => array(
                            'title' => ' ',
389
                            'datatable' => true,
390 391 392 393 394
                            'type'  => 'html',
                            'value' => get_string('fortokenusers', 'auth.webservice'),
                        ),
                        'functions' => array(
                            'title' => ' ',
395
                            'datatable' => true,
396 397 398
                            'type'  => 'html',
                            'value' => get_string('functions', 'auth.webservice'),
                        ),
399 400 401 402 403 404
                        'actions' => array(
                            'title' => ' ',
                            'datatable' => true,
                            'type'  => 'html',
                            'value' => '',
                        ),
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
                    ),
        );

    $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(
422 423 424
                'value'        => (($service->enabled == 1) ? display_icon('enabled') : display_icon('disabled')),
                'type'         => 'html',
                'class'        => 'center',
425 426 427
                'key'          => $service->name,
            );
            $form['elements']['id'. $service->id . '_restricted'] = array(
428 429 430
                'value' => (($service->restrictedusers == 1) ?  display_icon('enabled') : display_icon('disabled')),
                'type'         => 'html',
                'class'        => 'center',
431 432 433
                'key'          => $service->name,
            );
            $form['elements']['id'. $service->id . '_tokenusers'] = array(
434 435 436
                'value' => (($service->tokenusers == 1) ?  display_icon('enabled') : display_icon('disabled')),
                'type'         => 'html',
                'class'        => 'center',
437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
                '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(
455
                'value'        => pieform(array(
456 457 458 459 460 461 462 463 464 465
                                    'name'            => 'webservices_function_groups_edit_' . $service->id,
                                    'renderer'        => 'div',
                                    'elementclasses'  => false,
                                    '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(
466
                                                'type'  => 'image',
467
                                                'src'   => $THEME->get_image_url('btn_edit'),
468 469
                                                'alt'   => get_string('editspecific', 'mahara', $service->id),
                                                'elementtitle' => get_string('edit'),
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484
                                            ),
                                    ),
                                ))
                                .
                                pieform(array(
                                    'name'            => 'webservices_function_groups_delete_' . $service->id,
                                    'renderer'        => 'div',
                                    'elementclasses'  => false,
                                    '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(
485
                                                'type'  => 'image',
486
                                                'src'   => $THEME->get_image_url('btn_deleteremove'),
487 488
                                                'alt'   => get_string('deletespecific', 'mahara', $service->id),
                                                'elementtitle' => get_string('delete'),
489 490
                                            ),
                                    ),
491
                                ))
492 493 494
                                ,
                'type'         => 'html',
                'key'          => $service->name,
495
                'class'        => 'webserviceconfigcontrols btns2 right',
496 497 498
            );
        }
    }
499

500 501
    $pieform = new pieform($form);
    return $pieform->build(false) . '<div class="function_add">' .
502 503 504 505 506 507 508 509 510 511 512 513
                            pieform(array(
                                'name'            => 'webservices_function_groups_add',
                                'renderer'        => 'oneline',
                                'successcallback' => 'webservices_function_groups_submit',
                                'class'           => 'oneline inline',
                                'jsform'          => false,
                                'action'          => get_config('wwwroot') . 'webservice/admin/index.php',
                                'elements' => array(
                                    'service'    => array('type' => 'text'),
                                    'action'     => array('type' => 'hidden', 'value' => 'add'),
                                    'submit'     => array(
                                            'type'  => 'submit',
514
                                            'class' => 'submit',
515 516 517 518
                                            'value' => get_string('add')
                                        ),
                                ),
                            )) .
519
         '</div>';
520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537
}

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

    $form = array(
        'name'            => 'webservices_tokens',
        'elementclasses'  => false,
        'successcallback' => 'webservices_tokens_submit',
        'renderer'   => 'multicolumntable',
        'elements'   => array(
                        'token' => array(
                            'title' => ' ',
538
                            'datatable'  => true,
539 540 541 542 543
                            'type'  => 'html',
                            'value' => get_string('token', 'auth.webservice'),
                        ),
                        'institution' => array(
                            'title' => ' ',
544
                            'datatable'  => true,
545 546 547 548 549
                            'type'  => 'html',
                            'value' => get_string('institution'),
                        ),
                        'username' => array(
                            'title' => ' ',
550
                            'datatable'  => true,
551 552 553 554 555
                            'type'  => 'html',
                            'value' => get_string('username', 'auth.webservice'),
                        ),
                        'servicename' => array(
                            'title' => ' ',
556
                            'datatable'  => true,
557 558 559 560 561
                            'type'  => 'html',
                            'value' => get_string('servicename', 'auth.webservice'),
                        ),
                        'enabled' => array(
                            'title' => ' ',
562
                            'datatable'  => true,
563
                            'type'  => 'html',
564
                            'value' => get_string('enabled'),
565 566 567
                        ),
                        'wssigenc' => array(
                            'title' => ' ',
568
                            'datatable'  => true,
569 570 571 572 573
                            'type'  => 'html',
                            'value' => get_string('titlewssigenc', 'auth.webservice'),
                        ),
                        'functions' => array(
                            'title' => ' ',
574
                            'datatable'  => true,
575 576 577
                            'type'  => 'html',
                            'value' => get_string('functions', 'auth.webservice'),
                        ),
578 579 580 581 582 583
                        'actions' => array(
                            'title' => ' ',
                            'datatable'  => true,
                            'type'  => 'html',
                            'value' => '',
                        ),
584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617
                    ),
        );

    $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(
618 619 620
                'value' => (($token->enabled == 1) ? display_icon('enabled') : display_icon('disabled')),
                'type'         => 'html',
                'class'        => 'center',
621 622 623
                'key'          => $token->token,
            );
            $form['elements']['id'. $token->tokenid . '_wssigenc'] = array(
624 625 626
                'value' => (($token->wssigenc == 1) ? display_icon('enabled') : display_icon('disabled')),
                'type'         => 'html',
                'class'        => 'center',
627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652
                '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(
                'value'        => '<span class="actions inline">' .
                                pieform(array(
                                    'name'            => 'webservices_token_edit_' . $token->tokenid,
                                    'renderer'        => 'div',
                                    'elementclasses'  => false,
                                    'successcallback' => 'webservices_token_submit',
                                    'class'           => 'oneline inline',
653
                                    'jsform'          => false,
654 655 656 657
                                    'elements' => array(
                                        'token'      => array('type' => 'hidden', 'value' => $token->tokenid),
                                        'action'     => array('type' => 'hidden', 'value' => 'edit'),
                                        'submit'     => array(
658
                                                'type'  => 'image',
659
                                                'src'   => $THEME->get_image_url('btn_edit'),
660 661
                                                'alt'   => get_string('editspecific', 'mahara', $token->tokenid),
                                                'elementtitle' => get_string('edit'),
662 663 664 665 666 667 668 669 670 671
                                            ),
                                    ),
                                ))
                                .
                                pieform(array(
                                    'name'            => 'webservices_token_delete_' . $token->tokenid,
                                    'renderer'        => 'div',
                                    'elementclasses'  => false,
                                    'successcallback' => 'webservices_token_submit',
                                    'class'           => 'oneline inline',
672
                                    'jsform'          => false,
673 674 675 676
                                    'elements' => array(
                                        'token'      => array('type' => 'hidden', 'value' => $token->tokenid),
                                        'action'     => array('type' => 'hidden', 'value' => 'delete'),
                                        'submit'     => array(
677
                                                'type'  => 'image',
678
                                                'src'   => $THEME->get_image_url('btn_deleteremove'),
679 680
                                                'alt'   => get_string('deletespecific', 'mahara', $token->tokenid),
                                                'elementtitle' => get_string('delete'),
681 682 683 684 685 686
                                            ),
                                    ),
                                )) . '</span>'
                                ,
                'type'         => 'html',
                'key'          => $token->token,
687
                'class'        => 'webserviceconfigcontrols btns2 right',
688 689 690
            );
        }
    }
691 692 693 694
    else {
        // no results so hide headings
        $form['elements'] = array();
    }
695 696 697 698 699 700 701 702 703 704 705

    $username = '';
    if ($user  = param_integer('user', 0)) {
        $dbuser = get_record('usr', 'id', $user);
        if (!empty($dbuser)) {
            $username = $dbuser->username;
        }
    }
    else {
        $username = param_alphanum('username', '');
    }
706 707
    $pieform = new pieform($form);
    return $pieform->build(false) . '<div class="function_add">' .
708 709 710 711 712 713 714
                            pieform(array(
                                'name'            => 'webservices_token_generate',
                                'renderer'        => 'oneline',
                                'successcallback' => 'webservices_token_submit',
                                'jsform'          => false,
                                'action'          => get_config('wwwroot') . 'webservice/admin/index.php',
                                'elements' => array(
715 716 717 718
                                    'userid' => array(
                                        'type' => 'autocomplete',
                                        'title' => get_string('username') . ': ',
                                        'ajaxurl' => get_config('wwwroot') . 'webservice/admin/users.json.php',
719
                                        'multiple' => true,
720 721
                                        'allowclear' => true,
                                        'ajaxextraparams' => array(),
722 723 724 725
                                        'extraparams' => array(
                                            'maximumSelectionSize' => 1
                                        ),
                                        'width' => '280px',
726
                                    ),
727 728 729
                                    'action'     => array('type' => 'hidden', 'value' => 'generate'),
                                    'submit'     => array(
                                            'type'  => 'submit',
730
                                            'class' => 'submit',
731 732 733 734
                                            'value' => get_string('generate', 'auth.webservice')
                                        ),
                                ),
                            )).
735
         '</div>';
736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754

}

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

    $form = array(
        'name'            => 'webservices_users',
        'elementclasses'  => false,
        'successcallback' => 'webservices_users_submit',
        'renderer'   => 'multicolumntable',
        'elements'   => array(
                        'username' => array(
                            'title' => ' ',
755
                            'datatable'  => true,
756 757 758 759 760
                            'type'  => 'html',
                            'value' => get_string('username', 'auth.webservice'),
                        ),
                        'institution' => array(
                            'title' => ' ',
761
                            'datatable'  => true,
762 763 764 765 766
                            'type'  => 'html',
                            'value' => get_string('institution'),
                        ),
                        'servicename' => array(
                            'title' => ' ',
767
                            'datatable'  => true,
768 769 770 771 772
                            'type'  => 'html',
                            'value' => get_string('servicename', 'auth.webservice'),
                        ),
                        'enabled' => array(
                            'title' => ' ',
773
                            'datatable'  => true,
774
                            'type'  => 'html',
775
                            'value' => get_string('enabled'),
776 777 778
                        ),
                        'wssigenc' => array(
                            'title' => ' ',
779
                            'datatable'  => true,
780 781 782 783 784
                            'type'  => 'html',
                            'value' => get_string('titlewssigenc', 'auth.webservice'),
                        ),
                        'functions' => array(
                            'title' => ' ',
785
                            'datatable'  => true,
786 787 788
                            'type'  => 'html',
                            'value' => get_string('functions', 'auth.webservice'),
                        ),
789 790 791 792 793 794
                        'actions' => array(
                            'title' => ' ',
                            'datatable'  => true,
                            'type'  => 'html',
                            'value' => '',
                        ),
795 796 797
                    ),
        );

798
    $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());
799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823
    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(
824 825 826
                'value' => (($user->enabled == 1) ?  display_icon('enabled') : display_icon('disabled')),
                'type'         => 'html',
                'class'        => 'center',
827 828 829
                'key'          => $user->id,
            );
            $form['elements']['id'. $user->id . '_wssigenc'] = array(
830 831 832
                'value' => (($user->wssigenc == 1) ?  display_icon('enabled') : display_icon('disabled')),
                'type'         => 'html',
                'class'        => 'center',
833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858
                '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(
                'value'        => '<span class="actions inline">' .
                                pieform(array(
                                    'name'            => 'webservices_user_edit_' . $user->id,
                                    'renderer'        => 'div',
                                    'elementclasses'  => false,
                                    'successcallback' => 'webservices_user_submit',
                                    'class'           => 'oneline inline',
859
                                    'jsform'          => false,
860 861 862 863
                                    'elements' => array(
                                        'suid'       => array('type' => 'hidden', 'value' => $user->id),
                                        'action'     => array('type' => 'hidden', 'value' => 'edit'),
                                        'submit'     => array(
864
                                                'type'  => 'image',
865
                                                'src'   => $THEME->get_image_url('btn_edit'),
866 867
                                                'alt'   => get_string('editspecific', 'mahara', $user->username),
                                                'elementtitle' => get_string('edit'),
868 869 870 871 872 873 874 875 876 877
                                            ),
                                    ),
                                ))
                                .
                                pieform(array(
                                    'name'            => 'webservices_user_delete_' . $user->id,
                                    'renderer'        => 'div',
                                    'elementclasses'  => false,
                                    'successcallback' => 'webservices_user_submit',
                                    'class'           => 'oneline inline',
878
                                    'jsform'          => false,
879 880 881 882
                                    'elements' => array(
                                        'suid'       => array('type' => 'hidden', 'value' => $user->id),
                                        'action'     => array('type' => 'hidden', 'value' => 'delete'),
                                        'submit'     => array(
883
                                                'type'  => 'image',
884
                                                'src'   => $THEME->get_image_url('btn_deleteremove'),
885 886
                                                'alt'   => get_string('deletespecific', 'mahara', $user->username),
                                                'elementtitle' => get_string('delete'),
887 888 889 890 891 892 893 894 895 896
                                            ),
                                    ),
                                )) . '</span>'
                                ,
                'type'         => 'html',
                'key'          => $user->id,
                'class'        => 'actions',
            );
        }
    }
897 898 899 900
    else {
        // no results so hide headings
        $form['elements'] = array();
    }
901 902 903 904 905 906 907 908 909 910 911

    $username = '';
    if ($user  = param_integer('user', 0)) {
        $dbuser = get_record('usr', 'id', $user);
        if (!empty($dbuser)) {
            $username = $dbuser->username;
        }
    }
    else {
        $username = param_alphanum('username', '');
    }
912
    $searchicon = $THEME->get_image_url('btn-search', 'auth/webservice');
913 914
    $pieform = new pieform($form);
    return $pieform->build(false) . '<div id="user_add">' .
915 916 917 918 919 920 921
                            pieform(array(
                                'name'            => 'webservices_user_generate',
                                'renderer'        => 'oneline',
                                'successcallback' => 'webservices_user_submit',
                                'jsform'          => false,
                                'action'          => get_config('wwwroot') . 'webservice/admin/index.php',
                                'elements' => array(
922 923 924 925
                                    'userid' => array(
                                        'type' => 'autocomplete',
                                        'title' => get_string('username') . ': ',
                                        'ajaxurl' => get_config('wwwroot') . 'webservice/admin/users.json.php',
926
                                        'multiple' => true,
927 928
                                        'allowclear' => true,
                                        'ajaxextraparams' => array(),
929 930 931 932
                                        'extraparams' => array(
                                            'maximumSelectionSize' => 1
                                        ),
                                        'width' => '280px',
933
                                    ),
934 935 936
                                    'action'     => array('type' => 'hidden', 'value' => 'add'),
                                    'submit'     => array(
                                            'type'  => 'submit',
937
                                            'class' => 'submit',
938 939 940 941
                                            'value' => get_string('add')
                                        ),
                                ),
                            )).
942
         '</div>';
943 944 945 946 947 948 949 950 951 952 953 954 955
}

/**
 *  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() {

956
    $protosform = array(
957 958
            'name'            => 'activate_webservice_protos',
            'elements'        => webservices_protocol_switch_form(),
959
    );
960
    $protos = new Pieform($protosform);
961 962 963 964 965 966 967 968 969 970 971 972 973 974

    // certificate values from MNet
    $openssl = OpenSslRepo::singleton();
    $yesno = array(true  => get_string('yes'),
                   false => get_string('no'));

    $elements = array(
            // fieldset of master switch
            'webservicesmaster' => array(
                'type' => 'fieldset',
                'legend' => get_string('masterswitch', 'auth.webservice'),
                'elements' =>  webservices_master_switch_form(),
                'collapsible' => true,
                'collapsed'   => true,
975
                'name' => 'activate_webservices',
976 977 978 979 980 981 982 983 984
            ),

            // fieldset of protocol switches
            'protocolswitches' => array(
                                'type' => 'fieldset',
                                'legend' => get_string('protocolswitches', 'auth.webservice'),
                                'elements' =>  array(
                                                'protos_help' =>  array(
                                                'type' => 'html',
985
                                                'value' => '<div><p>' . get_string('manage_protocols', 'auth.webservice') . '</p></div>',
986 987 988
                                                ),
                                                'enablewebserviceprotos' =>  array(
                                                'type' => 'html',
989
                                                'value' => $protos->build(false),
990 991 992 993
                                                ),
                                            ),
                                'collapsible' => true,
                                'collapsed'   => true,
994
                                'name' => 'activate_webservices_protos',
995 996 997 998 999 1000 1001 1002 1003
                            ),

            // System Certificates
            'certificates' => array(
                                'type' => 'fieldset',
                                'legend' => get_string('certificates', 'auth.webservice'),
                                'elements' =>  array(
                                                'protos_help' =>  array(
                                                'type' => 'html',
1004
                                                'value' => '<div><p>' . get_string('manage_certificates', 'auth.webservice', get_config('wwwroot') . 'admin/site/networking.php') . '</p></div>',
1005 1006 1007 1008
                                                ),

                                                'pubkey' => array(
                                                    'type'         => 'html',
1009 1010
                                                    'value'        => '<div class="title">' . get_string('publickey','admin') . '</div>' .
                                                                      '<div class="detail">' . get_string('publickeydescription2', 'admin', 365) . '</div>' .
1011
                                                                      '<pre style="font-size: 0.7em; white-space: pre;">' . $openssl->certificate . '</pre>'
1012 1013 1014
                                                ),
                                                'sha1fingerprint' => array(
                                                    'type'         => 'html',
1015
                                                    'value'        => '<div><p>' . get_string('sha1fingerprint', 'auth.webservice', $openssl->sha1_fingerprint) . '</p></div>',
1016 1017 1018
                                                ),
                                                'md5fingerprint' => array(
                                                    'type'         => 'html',
1019
                                                    'value'        => '<div><p>' . get_string('md5fingerprint', 'auth.webservice', $openssl->md5_fingerprint) . '</p></div>',
1020 1021 1022
                                                ),
                                                'expires' => array(
                                                    'type'         => 'html',
1023
                                                    'value'        => '<div><p>' . get_string('publickeyexpireson','auth.webservice', format_date($openssl->expires)) . '</p></div>'
1024 1025 1026 1027
                                                ),
                                            ),
                                'collapsible' => true,
                                'collapsed'   => true,
1028
                                'name' => 'activate_webservices_networking',
1029 1030 1031 1032 1033 1034 1035 1036
                            ),

            // fieldset for managing service function groups
            'servicefunctiongroups' => array(
                                'type' => 'fieldset',
                                'legend' => get_string('servicefunctiongroups', 'auth.webservice'),
                                'elements' => array(
                                    'sfgdescription' => array(
1037
                                        'value' => '<div><p>' . get_string('sfgdescription', 'auth.webservice') . '</p></div>'
1038 1039 1040 1041 1042 1043 1044 1045
                                    ),
                                    'webservicesservicecontainer' => array(
                                        'type'         => 'html',
                                        'value' => service_fg_edit_form(),
                                    )
                                ),
                                'collapsible' => true,
                                'collapsed'   => true,
1046
                                'name' => 'webservices_function_groups',
1047 1048 1049 1050 1051 1052 1053 1054 1055
                            ),


            // fieldset for managing service tokens
            'servicetokens' => array(
                                'type' => 'fieldset',
                                'legend' => get_string('servicetokens', 'auth.webservice'),
                                'elements' => array(
                                    'stdescription' => array(
1056
                                        'value' => '<div><p>' . get_string('stdescription', 'auth.webservice') . '</p></div>'
1057 1058 1059 1060 1061 1062 1063 1064
                                    ),
                                    'webservicestokenscontainer' => array(
                                        'type'         => 'html',
                                        'value' => service_tokens_edit_form(),
                                    )
                                ),
                                'collapsible' => true,
                                'collapsed'   => false,
1065
                                'name' => 'webservices_token',
1066 1067 1068 1069 1070 1071 1072 1073
                            ),

            // fieldset for managing service tokens
            'serviceusers' => array(
                                'type' => 'fieldset',
                                'legend' => get_string('manageserviceusers', 'auth.webservice'),
                                'elements' => array(
                                    'sudescription' => array(
1074
                                        'value' => '<div><p>' . get_string('sudescription', 'auth.webservice') . '</p></div>'
1075 1076 1077 1078 1079 1080 1081 1082
                                    ),
                                    'webservicesuserscontainer' => array(
                                        'type'         => 'html',
                                        'value' => service_users_edit_form(),
                                    )
                                ),
                                'collapsible' => true,
                                'collapsed'   => false,
1083
                                'name' => 'webservices_user',
1084 1085 1086
                            ),
);
    $form = array(
1087
        'renderer' => 'div',
1088 1089 1090 1091 1092 1093
        'type' => 'div',
        'elements' => $elements,
    );

    return $form;
}