networking.php 6.42 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
require_once('searchlib.php');
define('TITLE', get_string('networking', 'admin'));

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

28

29 30
if (!$opensslext || !$curlext || !$xmlrpcext) {
    $smarty = smarty();
31 32
    setpageicon($smarty, 'icon-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
$openssl = OpenSslRepo::singleton();

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

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

122

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

126
    if ($form->get_submitvalue() === 'deletekey') {
127 128 129 130 131 132 133 134 135 136
        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'
        ));
    }

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

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

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

$smarty = smarty();
172
setpageicon($smarty, 'icon-exchange');
173 174
$smarty->assign('networkingform', $networkingform);
$smarty->display('admin/site/networking.tpl');