networking.php 6.54 KB
Newer Older
1 2 3 4 5
<?php
/**
 *
 * @package    mahara
 * @subpackage admin
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', 'configsite/networking');
15 16 17
define('SECTION_PLUGINTYPE', 'core');
define('SECTION_PLUGINNAME', 'admin');
define('SECTION_PAGE', 'networking');
18 19

require(dirname(dirname(dirname(__FILE__))) . '/init.php');
20
require_once(get_config('docroot') . 'api/xmlrpc/lib.php');
21 22 23 24
require_once('pieforms/pieform.php');
require_once('searchlib.php');
define('TITLE', get_string('networking', 'admin'));

25 26 27 28
$opensslext = extension_loaded('openssl');
$curlext    = extension_loaded('curl');
$xmlrpcext  = extension_loaded('xmlrpc');

29

30 31
if (!$opensslext || !$curlext || !$xmlrpcext) {
    $smarty = smarty();
32 33
    setpageicon($smarty, 'icon-exchange');

Naomi Guyer's avatar
Naomi Guyer committed
34
    $smarty->assign('PAGEHEADING', TITLE);
35 36 37 38 39
    $missingextensions = array();
    !$opensslext && $missingextensions[] = 'openssl';
    !$curlext    && $missingextensions[] = 'curl';
    !$xmlrpcext  && $missingextensions[] = 'xmlrpc';
    $smarty->assign('missingextensions', $missingextensions);
40
    $smarty->display('admin/site/networking.tpl');
41 42 43
    exit;
}

44 45 46 47 48 49 50
$openssl = OpenSslRepo::singleton();

$networkingform = pieform(
    array(
        'name'     => 'networkingform',
        'jsform'   => true,
        'elements' => array(
51 52 53
            'wwwroot' => array(
                'type'         => 'html',
                'title'        => get_string('wwwroot','admin'),
54
                'description'  => get_string('wwwrootdescription', 'admin'),
55 56
                'value'        => get_config('wwwroot')
            ),
57 58 59
            'pubkey' => array(
                'type'         => 'html',
                'title'        => get_string('publickey','admin'),
60
                'description'  => get_string('publickeydescription2', 'admin', 365),
61
                'value'        => '<pre style="font-size: 0.7em; white-space: pre;">'.$openssl->certificate.'</pre>'
62
            ),
63 64 65 66 67 68 69 70 71 72
            'sha1fingerprint' => array(
                'type'         => 'html',
                'title'        => 'SHA1 Fingerprint',
                'value'        => $openssl->sha1_fingerprint
            ),
            'md5fingerprint' => array(
                'type'         => 'html',
                'title'        => 'MD5 Fingerprint',
                'value'        => $openssl->md5_fingerprint
            ),
73 74 75
            'expires' => array(
                'type'         => 'html',
                'title'        => get_string('publickeyexpires','admin'),
76
                'value'        => format_date($openssl->expires)
77 78
            ),
            'enablenetworking' => array(
79
                'type'         => 'switchbox',
80 81 82 83
                'title'        => get_string('enablenetworking','admin'),
                'description'  => get_string('enablenetworkingdescription','admin'),
                'defaultvalue' => get_config('enablenetworking'),
            ),
84
            'promiscuousmode' => array(
85
                'type'         => 'switchbox',
86
                'class'        => 'last',
87 88 89 90
                'title'        => get_string('promiscuousmode','admin'),
                'description'  => get_string('promiscuousmodedescription','admin'),
                'defaultvalue' => get_config('promiscuousmode'),
            ),
91 92
            'submitbuttons' => array(
                'type' => 'fieldset',
93
                'class' => 'btn-group last',
94 95
                'elements' => array (
                    'submit' => array(
96
                        'class' => 'btn-primary text-inline',
97
                        'name' => 'submit',
98 99 100 101 102 103
                        'type'  => 'button',
                        'usebuttontag' => true,
                        'content' => get_string('savechanges','admin'),
                        'value' => 'submit'
                    ),
                    'deletesubmit' => array(
104
                        'class' => 'btn-default text-inline',
105 106 107
                        'name' => 'submit', // must be called submit so we can access it's value
                        'type'  => 'button',
                        'usebuttontag' => true,
108
                        'content' => '<span class="icon icon-refresh icon-lg left text-danger" role="presentation" aria-hidden="true"></span> '. get_string('deletekey', 'admin'),
109 110 111
                        'value' => 'deletekey'
                    )
                )
112 113 114 115 116 117
            )
        )
    )
);

function networkingform_fail(Pieform $form) {
118 119 120 121
    $form->reply(PIEFORM_ERR, array(
        'message' => get_string('enablenetworkingfailed','admin'),
        'goto'    => '/admin/site/networking.php',
    ));
122 123
}

124

125
function networkingform_submit(Pieform $form, $values) {
126
    $reply = '';
Donal McMullan's avatar
Donal McMullan committed
127

128
    if ($form->get_submitvalue() === 'deletekey') {
129 130 131 132 133 134 135 136 137 138
        global $SESSION;
        $openssl = OpenSslRepo::singleton();
        $openssl->get_keypair(true);
        $SESSION->add_info_msg(get_string('keydeleted', 'admin'));
        // Using cancel here as a hack to get it to redirect so it shows the new keys
        $form->reply(PIEFORM_CANCEL, array(
            'location'    => get_config('wwwroot') . 'admin/site/networking.php'
        ));
    }

139 140 141
    if (get_config('enablenetworking') != $values['enablenetworking']) {
        if (!set_config('enablenetworking', $values['enablenetworking'])) {
            networkingform_fail($form);
142 143
        }
        else {
144
            if (empty($values['enablenetworking'])) {
145
                $reply .= get_string('networkingdisabled','admin');
146 147
            }
            else {
148
                $reply .= get_string('networkingenabled','admin');
149 150
            }
        }
151 152
    }

153 154 155
    if (get_config('promiscuousmode') != $values['promiscuousmode']) {
        if (!set_config('promiscuousmode', $values['promiscuousmode'])) {
            networkingform_fail($form);
156 157
        }
        else {
158
            if (empty($values['promiscuousmode'])) {
159
                $reply .= get_string('promiscuousmodedisabled','admin');
160 161
            }
            else {
162
                $reply .= get_string('promiscuousmodeenabled','admin');
163 164
            }
        }
165
    }
166

167 168 169 170
    $form->reply(PIEFORM_OK, array(
        'message' => ($reply == '') ? get_string('networkingunchanged','admin') : $reply,
        'goto'    => '/admin/site/networking.php',
    ));
171 172 173
}

$smarty = smarty();
174
setpageicon($smarty, 'icon-exchange');
175
$smarty->assign('networkingform', $networkingform);
176
$smarty->assign('PAGEHEADING', TITLE);
177
$smarty->display('admin/site/networking.tpl');