networking.php 5.99 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 29 30
$opensslext = extension_loaded('openssl');
$curlext    = extension_loaded('curl');
$xmlrpcext  = extension_loaded('xmlrpc');

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

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

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

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

$networkingform = pieform(
    array(
        'name'     => 'networkingform',
        'jsform'   => true,
        'elements' => array(
53 54 55
            'wwwroot' => array(
                'type'         => 'html',
                'title'        => get_string('wwwroot','admin'),
56
                'description'  => get_string('wwwrootdescription', 'admin'),
57 58
                'value'        => get_config('wwwroot')
            ),
59 60 61
            'pubkey' => array(
                'type'         => 'html',
                'title'        => get_string('publickey','admin'),
62
                'description'  => get_string('publickeydescription2', 'admin', 365),
63
                'value'        => '<pre style="font-size: 0.7em; white-space: pre;">'.$openssl->certificate.'</pre>'
64
            ),
65 66 67 68 69 70 71 72 73 74
            'sha1fingerprint' => array(
                'type'         => 'html',
                'title'        => 'SHA1 Fingerprint',
                'value'        => $openssl->sha1_fingerprint
            ),
            'md5fingerprint' => array(
                'type'         => 'html',
                'title'        => 'MD5 Fingerprint',
                'value'        => $openssl->md5_fingerprint
            ),
75 76 77
            'expires' => array(
                'type'         => 'html',
                'title'        => get_string('publickeyexpires','admin'),
78
                'value'        => format_date($openssl->expires)
79 80
            ),
            'enablenetworking' => array(
81
                'type'         => 'switchbox',
82 83 84 85 86
                'title'        => get_string('enablenetworking','admin'),
                'description'  => get_string('enablenetworkingdescription','admin'),
                'defaultvalue' => get_config('enablenetworking'),
                'options'      => $yesno,
            ),
87
            'promiscuousmode' => array(
88
                'type'         => 'switchbox',
89 90 91 92 93
                'title'        => get_string('promiscuousmode','admin'),
                'description'  => get_string('promiscuousmodedescription','admin'),
                'defaultvalue' => get_config('promiscuousmode'),
                'options'      => $yesno,
            ),
94
            'submit' => array(
Naomi Guyer's avatar
Naomi Guyer committed
95
                'class' => 'btn btn-success',
96 97
                'type'  => 'submit',
                'value' => get_string('savechanges','admin')
98 99
            ),
            'deletesubmit' => array(
Naomi Guyer's avatar
Naomi Guyer committed
100
                'class' => 'btn btn-danger',
101 102 103
                'type'  => 'submit',
                'title' => get_string('deletekey', 'admin'),
                'value' => get_string('delete')
104 105 106 107 108 109
            )
        )
    )
);

function networkingform_fail(Pieform $form) {
110 111 112 113
    $form->reply(PIEFORM_ERR, array(
        'message' => get_string('enablenetworkingfailed','admin'),
        'goto'    => '/admin/site/networking.php',
    ));
114 115 116
}

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

119 120 121 122 123 124 125 126 127 128 129
    if (isset($values['deletesubmit'])) {
        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'
        ));
    }

130 131 132
    if (get_config('enablenetworking') != $values['enablenetworking']) {
        if (!set_config('enablenetworking', $values['enablenetworking'])) {
            networkingform_fail($form);
133 134
        }
        else {
135
            if (empty($values['enablenetworking'])) {
136
                $reply .= get_string('networkingdisabled','admin');
137 138
            }
            else {
139
                $reply .= get_string('networkingenabled','admin');
140 141
            }
        }
142 143
    }

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

158 159 160 161
    $form->reply(PIEFORM_OK, array(
        'message' => ($reply == '') ? get_string('networkingunchanged','admin') : $reply,
        'goto'    => '/admin/site/networking.php',
    ));
162 163 164
}

$smarty = smarty();
165
setpageicon($smarty, 'icon-exchange');
166
$smarty->assign('networkingform', $networkingform);
167
$smarty->assign('PAGEHEADING', TITLE);
168
$smarty->display('admin/site/networking.tpl');