oauthv1sregister.php 29.1 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
 *
 */

define('INTERNAL', 1);
define('ADMIN', 1);
14
define('MENUITEM', 'webservices/oauthconfig');
15
16
define('SECTION_PAGE', 'oauth');
require(dirname(dirname(dirname(__FILE__))) . '/init.php');
Naomi Guyer's avatar
Naomi Guyer committed
17
18

define('TITLE', get_string('webservices_title', 'auth.webservice'));
19
define('SUBSECTIONHEADING', get_string('externalappsregister', 'auth.webservice'));
Naomi Guyer's avatar
Naomi Guyer committed
20

21
22
23
24
require_once(get_config('docroot') . 'webservice/libs/oauth-php/OAuthServer.php');
require_once(get_config('docroot') . 'webservice/libs/oauth-php/OAuthStore.php');
OAuthStore::instance('Mahara');

Matt Clarkson's avatar
Matt Clarkson committed
25
26
$server_id  = param_integer('edit', 0);
$config_server_id = param_integer('config', 0);
27
28
29
30
31
32
33
34
35
36
37
38
39
40
$dbserver = get_record('oauth_server_registry', 'id', $server_id);

$institutions = get_records_array('institution');
$iopts = array();
foreach ($institutions as $institution) {
    $iopts[trim($institution->name)] = $institution->displayname;
}

$services = get_records_array('external_services', 'restrictedusers', 0);
$sopts = array();
foreach ($services as $service) {
    $sopts[$service->id] = $service->name;
}

Matt Clarkson's avatar
Matt Clarkson committed
41
42
43
44
// we have a service config form
if ($config_server_id) {
    $form = webservice_server_config_form($config_server_id);
}
45
// we have an edit form
Matt Clarkson's avatar
Matt Clarkson committed
46
else if (!empty($dbserver)) {
47
48
49
50
51
52
53
54
    $disabled = array();
    list($moduletype, $module) = get_module_from_serverid($server_id);
    safe_require_plugin($moduletype, $module);
    $classname = generate_class_name($moduletype, $module);
    if (is_callable(array($classname, 'disable_webservice_fields'))) {
        $disabled = call_static_method($classname, 'disable_webservice_fields');
    }
    $form = webservice_server_edit_form($dbserver, $sopts, $iopts, $disabled);
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
}
// else we have just the standard list
else {
    $form = webservice_server_list_form($sopts, $iopts);
}

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

    $dbuser = get_record('usr', 'id', $USER->get('id'));
    if (empty($dbuser)) {
        $SESSION->add_error_msg(get_string('erroruser', 'auth.webservice'));
        redirect('/webservice/admin/oauthv1sregister.php');
    }
    $store = OAuthStore::instance();

    $new_app = array(
                'application_title' => $values['application'],
                'application_uri'   => 'http://example.com',
                'requester_name'    => $dbuser->firstname . ' ' . $dbuser->lastname,
                'requester_email'   => $dbuser->email,
                'callback_uri'      => 'http://example.com',
                'institution'       => $values['institution'],
                'externalserviceid' => $values['service'],
      );
    $key = $store->updateConsumer($new_app, $dbuser->id, true);
    $c = (object) $store->getConsumer($key, $dbuser->id, true);
    if (empty($c)) {
        $SESSION->add_error_msg(get_string('errorregister', 'auth.webservice'));
        redirect('/webservice/admin/oauthv1sregister.php');
    }
    else {
87
88
89
90
91
92
93
94
95
96
97
        // New application added - now check that institution has 'webservices' auth
        // Get auth priority we need for the ensure_record_exists function
        $priorities  = get_record_sql("SELECT MAX(priority) AS maxpriority, (
                                           SELECT priority FROM {auth_instance}
                                           WHERE institution = ? AND authname = 'webservice') AS webservicepriority
                                      FROM {auth_instance} WHERE institution = ?", array($values['institution'], $values['institution']));
        $priority = is_null($priorities->webservicepriority) ? $priorities->maxpriority + 1 : $priorities->webservicepriority;
        if (!ensure_record_exists('auth_instance', (object) array('institution' => $values['institution'], 'authname' => 'webservice'),
                                                   (object) array('institution' => $values['institution'], 'authname' => 'webservice', 'active' => 1, 'priority' => $priority, 'instancename' => 'webservice'))) {
            $SESSION->add_error_msg(get_string('setauthinstancefailed', 'auth.webservice', institution_display_name($values['institution'])));
        }
98
99
100
101
102
103
104
105
106
107
108
109
        redirect('/webservice/admin/oauthv1sregister.php?edit=' . $c->id);
    }

}

function webservices_server_submit(Pieform $form, $values) {
    global $USER, $SESSION;
    $store = OAuthStore::instance();
    $is_admin = ($USER->get('admin') ||defined('ADMIN') || defined('INSTITUTIONALADMIN') || $USER->is_institutional_admin() ? true : false);
    $dbserver = get_record('oauth_server_registry', 'id', $values['token']);
    if ($dbserver) {
        if ($values['action'] == 'delete') {
Matt Clarkson's avatar
Matt Clarkson committed
110
111
112
113
114
115

            delete_records_sql('
                                DELETE FROM {oauth_server_config}
                                WHERE oauthserverregistryid = ?
                                ', array($dbserver->id));

116
117
118
119
            delete_records_sql('
                                DELETE FROM {oauth_server_token}
                                WHERE osr_id_ref = ?
                                ', array($dbserver->id));
120
121
122
123
            delete_records_sql('
                                DELETE FROM {lti_assessment}
                                WHERE oauthserver = ?
                                ', array($dbserver->id));
124
125
126
127
128
129
            $store->deleteServer($dbserver->consumer_key, $dbserver->userid, $is_admin);
            $SESSION->add_ok_msg(get_string('oauthserverdeleted', 'auth.webservice'));
        }
        else if ($values['action'] == 'edit') {
            redirect('/webservice/admin/oauthv1sregister.php?edit=' . $values['token']);
        }
Matt Clarkson's avatar
Matt Clarkson committed
130
131
132
        else if ($values['action'] == 'config') {
            redirect('/webservice/admin/oauthv1sregister.php?config=' . $values['token']);
        }
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
    }
    redirect('/webservice/admin/oauthv1sregister.php');
}

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

    $store = OAuthStore::instance();
    $dbserver = get_record('oauth_server_registry', 'id', $values['id']);
    if ($dbserver) {

       $app = array(
                    'application_title' => $values['application_title'],
                    'application_uri'   => $values['application_uri'],
                    'requester_name'    => $dbserver->requester_name,
                    'requester_email'   => $dbserver->requester_email,
                    'callback_uri'      => $values['callback_uri'],
                    'institution'       => $values['institution'],
                    'externalserviceid' => $values['service'],
                    'consumer_key'      => $dbserver->consumer_key,
                    'consumer_secret'   => $dbserver->consumer_secret,
                    'id'                => $values['id'],
       );
        $key = $store->updateConsumer($app, $USER->get('id'), true);
        $c = (object) $store->getConsumer($key, $USER->get('id'), true);
        if (empty($c)) {
            $SESSION->add_error_msg(get_string('errorregister', 'auth.webservice'));
            redirect('/webservice/admin/oauthv1sregister.php');
        }
        else {
            redirect('/webservice/admin/oauthv1sregister.php?edit=' . $c->id);
        }
    }

    $SESSION->add_error_msg(get_string('errorupdate', 'auth.webservice'));
    redirect('/webservice/admin/oauthv1sregister.php');
}

171
$pieform = pieform_instance($form);
172
$form = $pieform->build(false);
173

Naomi Guyer's avatar
Naomi Guyer committed
174
$smarty = smarty();
175
setpageicon($smarty, 'icon-puzzle-piece');
176
177
178
179
180
181
182
183
safe_require('auth', 'webservice');
PluginAuthWebservice::menu_items($smarty, 'webservice/oauthconfig');
$smarty->assign('form', $form);
$smarty->display('form.tpl');

function webservice_main_submit(Pieform $form, $values) {
}

184
function webservice_server_edit_form($dbserver, $sopts, $iopts, $disabled = array()) {
185
186
187
188
189

    $server_details =
        array(
            'name'             => 'webservice_oauth_server',
            'successcallback'  => 'webservice_oauth_server_submit',
190
            'jsform'           => false,
191
            'elements'   => array(
192
193
194
                    'id' => array(
                        'type'  => 'hidden',
                        'value' => $dbserver->id,
195
                    ),
196
197
198
199
200
201
202
203
204
                    'userid' => array(
                        'type'  => 'hidden',
                        'value' => $dbserver->userid,
                    ),
                    'consumer_key' => array(
                        'type'  => 'hidden',
                        'value' => $dbserver->consumer_key,
                    ),
                ),
205
            );
206
207
208
209
210
    $server_details['elements']['consumer_key_html'] = array(
        'title'        => get_string('consumer_key', 'auth.webservice'),
        'type'         => 'html',
        'value'        => $dbserver->consumer_key,
    );
211
212
    $server_details['elements']['consumer_secret'] = array(
        'title'        => get_string('consumer_secret', 'auth.webservice'),
213
214
        'type'         => 'html',
        'value'        => $dbserver->consumer_secret,
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
    );

    $server_details['elements']['application_title'] = array(
        'title'        => get_string('application_title', 'auth.webservice'),
        'defaultvalue' =>  $dbserver->application_title,
        'type'         => 'text',
    );

    $server_details['elements']['user'] = array(
        'title'        => get_string('serviceuser', 'auth.webservice'),
        'value'        =>  get_field('usr', 'username', 'id', $dbserver->userid),
        'type'         => 'html',
    );

    $server_details['elements']['application_uri'] = array(
        'title'        => get_string('application_uri', 'auth.webservice'),
        'defaultvalue' =>  $dbserver->application_uri,
        'type'         => 'text',
233
        'disabled'     => (isset($disabled['application_uri']) ? true : false),
234
235
236
237
238
239
    );

    $server_details['elements']['callback_uri'] = array(
        'title'        => get_string('callback', 'auth.webservice'),
        'defaultvalue' =>  $dbserver->callback_uri,
        'type'         => 'text',
240
        'disabled'     => (isset($disabled['callback_uri']) ? true : false),
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
    );

    $server_details['elements']['institution'] = array(
        'type'         => 'select',
        'title'        => get_string('institution'),
        'options'      => $iopts,
        'defaultvalue' => trim($dbserver->institution),
    );

    $server_details['elements']['service'] = array(
        'type'         => 'select',
        'title'        => get_string('servicename', 'auth.webservice'),
        'options'      => $sopts,
        'defaultvalue' => $dbserver->externalserviceid,
    );

    $server_details['elements']['enabled'] = array(
258
        'title'        => get_string('enabled'),
259
        'defaultvalue' => (($dbserver->enabled == 1) ? 'checked' : ''),
260
        'type'         => 'switchbox',
261
262
263
264
265
266
267
268
269
270
271
272
273
        'disabled'     => true,
    );

    $functions = get_records_array('external_services_functions', 'externalserviceid', $dbserver->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>';
        }
    }
    $server_details['elements']['functions'] = array(
        'title'        => get_string('functions', 'auth.webservice'),
274
        'value'        => '<div class="align-with-input">' . implode(', ', $function_list) . '</div>',
275
276
277
278
279
280
281
        'type'         => 'html',
    );

    $server_details['elements']['submit'] = array(
        'type'  => 'submitcancel',
        'value' => array(get_string('save'), get_string('back')),
        'goto'  => get_config('wwwroot') . 'webservice/admin/oauthv1sregister.php',
282
        'class'        => 'btn-primary',
283
284
285
286
287
    );

    $elements = array(
            // fieldset for managing service function list
            'token_details' => array(
288
289
290
291
292
293
294
295
296
                    'type' => 'fieldset',
                    'class' => 'with-padding',
                    'elements' => array(
                        'sflist' => array(
                            'value' =>     pieform($server_details),
                        )
                    ),
                    'collapsible' => false,
                ),
297
298
299
        );

    $form = array(
Naomi Guyer's avatar
Naomi Guyer committed
300
        'renderer' => 'div',
301
302
        'id' => 'maintable',
        'name' => 'tokenconfig',
303
        'jsform' => false,
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
        'successcallback' => 'webservice_server_edit_submit',
        'elements' => $elements,
    );

    return $form;
}

function webservice_server_list_form($sopts, $iopts) {
    global $USER, $THEME;

    $dbconsumers = get_records_sql_assoc('
            SELECT  osr.id              as id,
                    userid              as userid,
                    institution         as institution,
                    externalserviceid   as externalserviceid,
                    u.username          as username,
                    u.email             as email,
                    consumer_key        as consumer_key,
                    consumer_secret     as consumer_secret,
Matt Clarkson's avatar
Matt Clarkson committed
323
                    osr.enabled         as enabled,
324
                    status              as status,
325
                    osr.ctime           as issue_date,
326
327
328
329
330
                    application_uri     as application_uri,
                    application_title   as application_title,
                    application_descr   as application_descr,
                    requester_name      as requester_name,
                    requester_email     as requester_email,
Matt Clarkson's avatar
Matt Clarkson committed
331
332
                    callback_uri        as callback_uri,
                    es.component        as component
333
334
            FROM {oauth_server_registry} osr
            JOIN {usr} u
Matt Clarkson's avatar
Matt Clarkson committed
335
336
337
                ON osr.userid = u.id
            JOIN {external_services} es
                ON es.id = osr.externalserviceid
338
339
340
341
342
343
344
345
346
347
348
349
            ORDER BY application_title, username
            ', array());
    $form = '';
    if (!empty($dbconsumers)) {
        $form = array(
            'name'            => 'webservices_tokens',
            'elementclasses'  => false,
            'successcallback' => 'webservices_tokens_submit',
            'renderer'   => 'multicolumntable',
            'elements'   => array(
                            'application' => array(
                                'title' => ' ',
350
                                'datatable' => true,
351
352
353
354
355
                                'type'  => 'html',
                                'value' => get_string('application', 'auth.webservice'),
                            ),
                            'username' => array(
                                'title' => ' ',
356
                                'datatable' => true,
357
358
359
360
361
                                'type'  => 'html',
                                'value' => get_string('owner', 'auth.webservice'),
                            ),
                            'consumer_key' => array(
                                'title' => ' ',
362
                                'datatable' => true,
363
364
365
366
367
                                'type'  => 'html',
                                'value' => get_string('consumer_key', 'auth.webservice'),
                            ),
                            'consumer_secret' => array(
                                'title' => ' ',
368
                                'datatable' => true,
369
370
371
372
373
                                'type'  => 'html',
                                'value' => get_string('consumer_secret', 'auth.webservice'),
                            ),
                            'enabled' => array(
                                'title' => ' ',
374
                                'datatable' => true,
375
                                'type'  => 'html',
376
                                'value' => get_string('enabled'),
377
                            ),
Matt Clarkson's avatar
Matt Clarkson committed
378
                            'institution' => array(
379
                                'title' => ' ',
380
                                'datatable' => true,
381
                                'type'  => 'html',
Matt Clarkson's avatar
Matt Clarkson committed
382
                                'value' => get_string('institution'),
383
                            ),
384
385
386
387
388
389
                            'actions' => array(
                                'title' => ' ',
                                'datatable' => true,
                                'type' => 'html',
                                'value' => '',
                            ),
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
                        ),
            );
        foreach ($dbconsumers as $consumer) {
            $form['elements']['id' . $consumer->id . '_application'] = array(
                'value'        =>  $consumer->application_title,
                'type'         => 'html',
                'key'        => $consumer->consumer_key,
            );

            if ($USER->is_admin_for_user($consumer->userid)) {
                $user_url = get_config('wwwroot') . 'admin/users/edit.php?id=' . $consumer->userid;
            }
            else {
                $user_url = get_config('wwwroot') . 'user/view.php?id=' . $consumer->userid;
            }
            $form['elements']['id' . $consumer->id . '_username'] = array(
                'value'        =>  '<a href="' . $user_url . '">' . $consumer->username . '</a>',
                'type'         => 'html',
                'key'        => $consumer->consumer_key,
            );
            $form['elements']['id' . $consumer->id . '_consumer_key'] = array(
                'value'        =>  $consumer->consumer_key,
                'type'         => 'html',
                'key'        => $consumer->consumer_key,
            );
            $form['elements']['id' . $consumer->id . '_consumer_secret'] = array(
                'value'        =>  $consumer->consumer_secret,
                'type'         => 'html',
                'key'        => $consumer->consumer_key,
            );
            $form['elements']['id' . $consumer->id . '_enabled'] = array(
421
422
423
424
425
                'value' => (
                            ($consumer->enabled == 1) ?
                             display_icon('enabledspecific', false, get_string('enabledspecific', 'mahara', $consumer->application_title)) :
                             display_icon('disabledspecific', false, get_string('disabledspecific', 'mahara', $consumer->application_title))
                           ),
426
                'type'         => 'html',
427
                'class'        => 'text-center',
428
429
                'key'        => $consumer->consumer_key,
            );
Matt Clarkson's avatar
Matt Clarkson committed
430
431
            $form['elements']['id' . $consumer->id . '_institution'] = array(
                'value'        => institution_display_name($consumer->institution),
432
433
434
435
436
437
                'type'         => 'html',
                'key'        => $consumer->consumer_key,
            );

            // edit and delete buttons
            $form['elements']['id' . $consumer->id . '_actions'] = array(
438
                'value' => pieform(array(
439
440
                        'name' => 'webservices_server_edit_' . $consumer->id,
                        'renderer' => 'div',
441
                        'class' => 'form-as-button pull-left',
442
443
444
445
446
447
448
                        'elementclasses' => false,
                        'successcallback' => 'webservices_server_submit',
                        'jsform' => false,
                        'elements' => array(
                            'token' => array('type' => 'hidden', 'value' => $consumer->id),
                            'action' => array('type' => 'hidden', 'value' => 'edit'),
                            'submit' => array(
449
450
                                'type' => 'button',
                                'usebuttontag' => true,
451
                                'class' => 'btn-default btn-xs',
452
453
                                'value' => '<span class="icon icon-pencil icon-lg" role="presentation" aria-hidden="true"></span><span class="sr-only">' . get_string('editspecific', 'mahara', $consumer->application_title) . '</span>',
                                'elementtitle' => get_string('editspecific', 'mahara', $consumer->application_title),
454
455
456
457
458
459
460
                            ),
                        ),
                    ))
                    .
                    pieform(array(
                        'name' => 'webservices_server_delete_' . $consumer->id,
                        'renderer' => 'div',
461
                        'class' => 'form-as-button pull-left',
462
463
464
465
466
467
468
                        'elementclasses' => false,
                        'successcallback' => 'webservices_server_submit',
                        'jsform' => false,
                        'elements' => array(
                            'token' => array('type' => 'hidden', 'value' => $consumer->id),
                            'action' => array('type' => 'hidden', 'value' => 'delete'),
                            'submit' => array(
469
470
                                'type' => 'button',
                                'usebuttontag' => true,
471
                                'class' => 'btn-default btn-xs',
472
473
                                'value' => '<span class="icon icon-trash icon-lg text-danger" role="presentation" aria-hidden="true"></span><span class="sr-only">'.get_string('deletespecific', 'mahara', $consumer->application_title).'</span>',
                                'elementtitle' => get_string('deletespecific', 'mahara', $consumer->application_title),
474
475
                            ),
                        ),
476
                    )),
477
478
                'type' => 'html',
                'key' => $consumer->consumer_key,
479
                'class' => 'webserviceconfigcontrols btn-group icon-cell',
480
            );
Matt Clarkson's avatar
Matt Clarkson committed
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510

            // Check if service has extra settings
            if ($consumer->component) {
                list($moduletype, $module) = explode("/", $consumer->component);

                $hasconfig = false;

                if (safe_require_plugin($moduletype, $module)) {
                    $classname = generate_class_name($moduletype, $module);
                    if (is_callable(array($classname, 'has_oauth_service_config'))) {
                        $hasconfig = call_static_method($classname, 'has_oauth_service_config');
                    }
                }

                if ($hasconfig) {
                    $form['elements']['id' . $consumer->id . '_actions']['value'] .=
                        pieform(array(
                            'name' => 'webservices_server_config_' . $consumer->id,
                            'renderer' => 'div',
                            'class' => 'form-as-button pull-left',
                            'elementclasses' => false,
                            'successcallback' => 'webservices_server_submit',
                            'jsform' => false,
                            'elements' => array(
                                'token' => array('type' => 'hidden', 'value' => $consumer->id),
                                'action' => array('type' => 'hidden', 'value' => 'config'),
                                'submit' => array(
                                    'type' => 'button',
                                    'usebuttontag' => true,
                                    'class' => 'btn-default btn-xs',
511
512
                                    'value' => '<span class="icon icon-cog icon-lg " role="presentation" aria-hidden="true"></span><span class="sr-only">'.get_string('managespecific', 'mahara', $consumer->application_title).'</span>',
                                    'elementtitle' => get_string('managespecific', 'mahara', $consumer->application_title),
Matt Clarkson's avatar
Matt Clarkson committed
513
514
515
516
517
518
                                ),
                            ),
                        ));
                }
            }

519
        }
Matt Clarkson's avatar
Matt Clarkson committed
520

521
        $pieform = pieform_instance($form);
522
        $form = $pieform->build(false);
523
524
    }

525
526
    $form = '<div class="table-responsive">' . $form . '</div><div>' .
        pieform(array(
527
528
529
530
            'name' => 'webservices_token_generate',
            'renderer' => 'div',
            'validatecallback' => 'webservices_add_application_validate',
            'successcallback' => 'webservices_add_application_submit',
531
            'class' => 'form-inline form-inline-align-bottom',
532
533
534
535
536
537
538
539
540
            'jsform' => false,
            'action' => get_config('wwwroot') . 'webservice/admin/oauthv1sregister.php',
            'elements' => array(
                'application' => array(
                    'type' => 'text',
                    'title' => get_string('application', 'auth.webservice') . ': ',
                ),
                'institution' => array(
                    'type' => 'select',
541
542
                    'title' => get_string('institution'),
                    'class' => 'institution input-small hide-label',
543
544
545
546
                    'options' => $iopts,
                ),
                'service' => array(
                    'type' => 'select',
547
548
                    'title' => get_string('service', 'auth.webservice'),
                    'class' => 'hide-label',
549
550
551
552
553
                    'options' => $sopts,
                ),
                'action' => array('type' => 'hidden', 'value' => 'add'),
                'submit' => array(
                    'type' => 'submit',
554
                    'class' => 'btn-primary',
555
556
557
                    'value' => get_string('add', 'auth.webservice'),
                ),
            ),
558
        )) . '</div>';
559
560

    $elements = array(
561
562
        // fieldset for managing service function list
        'register_server' => array(
563
564
565
            'type' => 'container',
            'title' => get_string('userapplications1', 'auth.webservice'),
            'isformgroup' => false,
566
567
568
569
570
571
572
573
            'elements' => array(
                'sflist' => array(
                    'type' => 'html',
                    'value' => $form,
                )
            ),
        ),
    );
574
575

    $form = array(
Naomi Guyer's avatar
Naomi Guyer committed
576
        'renderer' => 'div',
577
578
579
580
581
582
583
584
585
586
        'type' => 'div',
        'id' => 'maintable',
        'name' => 'maincontainer',
        'dieaftersubmit' => false,
        'successcallback' => 'webservice_main_submit',
        'elements' => $elements,
    );

    return $form;
}
Matt Clarkson's avatar
Matt Clarkson committed
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

function webservice_server_config_form($serverid) {
    global $USER, $THEME;

    list($moduletype, $module) = get_module_from_serverid($serverid);

    if (safe_require_plugin($moduletype, $module)) {

        $elements = call_static_method(generate_class_name($moduletype, $module), 'get_oauth_service_config_options', $serverid);

        $elements['submit'] = array(
            'type'  => 'submitcancel',
            'value' => array(get_string('save'), get_string('back')),
            'goto'  => get_config('wwwroot') . 'webservice/admin/oauthv1sregister.php',
            'class' => 'btn-primary',
        );

        $elements['id'] = array(
            'type'  => 'hidden',
            'value' => $serverid,
        );

        $fieldset = array(
            // fieldset for managing service function list
            'token_details' => array(
                    'type' => 'fieldset',
                    'class' => 'with-padding',
                    'elements' => array(
                        'sflist' => array(
                            'value' =>  pieform(array(
                                'name' => 'oauthconfigoptions',
618
619
                                'plugintype' => $moduletype,
                                'pluginname' => $module,
Matt Clarkson's avatar
Matt Clarkson committed
620
621
622
623
624
625
626
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
653
654
655
656
657
658
659
660
661
662
663
664
                                'successcallback'  => 'webservice_server_config_submit',
                                'elements' => $elements)),
                        )
                    ),
                    'collapsible' => false,
                ),
        );

        $form = array(
            'name' => 'maincontainer',
            'jsform' => false,
            'elements' => $fieldset,
        );

        return $form;
    }
}

function webservice_server_config_submit(Pieform $form, $values) {

    $serverid = $values['id'];

    list($moduletype, $module) = get_module_from_serverid($serverid);

    if (safe_require_plugin($moduletype, $module)) {

        call_static_method(generate_class_name($moduletype, $module), 'save_oauth_service_config_options', $serverid, $values);
        redirect(get_config('wwwroot') . 'webservice/admin/oauthv1sregister.php');
    }

    return false;
}


function update_oauth_server_config($serverid, $key, $value) {

    $dbvalue = get_field('oauth_server_config', 'value', 'oauthserverregistryid', $serverid, 'field', $key);
    if (false !== $dbvalue) {
        if (($dbvalue == $value)
            || set_field('oauth_server_config', 'value', $value, 'oauthserverregistryid', $serverid, 'field', $key)
        ) {
            return true;
        }
    }
    else {
665
        $config = new stdClass();
Matt Clarkson's avatar
Matt Clarkson committed
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
        $config->oauthserverregistryid = $serverid;
        $config->field = $key;
        $config->value = $value;
        $status = insert_record('oauth_server_config', $config);

        return true;
    }

    return false;
}

function get_module_from_serverid($serverid) {

    $consumer = get_record_sql('
            SELECT es.id, es.component
            FROM {oauth_server_registry} osr
            JOIN {external_services} es
                ON es.id = osr.externalserviceid
            WHERE osr.id = ? ', array($serverid));
685
686
687
688
    if (substr_count($consumer->component, '/') > 0) {
        return explode("/", $consumer->component);
    }
    return array('auth', 'webservice');
Matt Clarkson's avatar
Matt Clarkson committed
689
}