networking.php 6 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();
Naomi Guyer's avatar
Naomi Guyer committed
31 32
    $smarty->assign('PAGEHEADING', TITLE);
    $smarty->assign('PAGEICON', 'fa fa-exchange');
33 34 35 36 37
    $missingextensions = array();
    !$opensslext && $missingextensions[] = 'openssl';
    !$curlext    && $missingextensions[] = 'curl';
    !$xmlrpcext  && $missingextensions[] = 'xmlrpc';
    $smarty->assign('missingextensions', $missingextensions);
38
    $smarty->display('admin/site/networking.tpl');
39 40 41
    exit;
}

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

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

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

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

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

118 119 120 121 122 123 124 125 126 127 128
    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'
        ));
    }

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

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

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

$smarty = smarty();
164
$smarty->assign('networkingform', $networkingform);
Naomi Guyer's avatar
Naomi Guyer committed
165
$smarty->assign('PAGEICON', 'fa fa-exchange');
166
$smarty->assign('PAGEHEADING', TITLE);
167
$smarty->display('admin/site/networking.tpl');