networking.php 6.64 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 51 52 53
$openssl = OpenSslRepo::singleton();

$yesno = array(true  => get_string('yes'),
               false => get_string('no'));

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

function networkingform_fail(Pieform $form) {
123 124 125 126
    $form->reply(PIEFORM_ERR, array(
        'message' => get_string('enablenetworkingfailed','admin'),
        'goto'    => '/admin/site/networking.php',
    ));
127 128
}

129

130
function networkingform_submit(Pieform $form, $values) {
131
    $reply = '';
Donal McMullan's avatar
WIP  
Donal McMullan committed
132

133
    if ($form->get_submitvalue() === 'deletekey') {
134 135 136 137 138 139 140 141 142 143
        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'
        ));
    }

144 145 146
    if (get_config('enablenetworking') != $values['enablenetworking']) {
        if (!set_config('enablenetworking', $values['enablenetworking'])) {
            networkingform_fail($form);
147 148
        }
        else {
149
            if (empty($values['enablenetworking'])) {
150
                $reply .= get_string('networkingdisabled','admin');
151 152
            }
            else {
153
                $reply .= get_string('networkingenabled','admin');
154 155
            }
        }
156 157
    }

158 159 160
    if (get_config('promiscuousmode') != $values['promiscuousmode']) {
        if (!set_config('promiscuousmode', $values['promiscuousmode'])) {
            networkingform_fail($form);
161 162
        }
        else {
163
            if (empty($values['promiscuousmode'])) {
164
                $reply .= get_string('promiscuousmodedisabled','admin');
165 166
            }
            else {
167
                $reply .= get_string('promiscuousmodeenabled','admin');
168 169
            }
        }
170
    }
171

172 173 174 175
    $form->reply(PIEFORM_OK, array(
        'message' => ($reply == '') ? get_string('networkingunchanged','admin') : $reply,
        'goto'    => '/admin/site/networking.php',
    ));
176 177 178
}

$smarty = smarty();
179
setpageicon($smarty, 'icon-exchange');
180
$smarty->assign('networkingform', $networkingform);
181
$smarty->assign('PAGEHEADING', TITLE);
182
$smarty->display('admin/site/networking.tpl');