~ubuntu-branches/ubuntu/jaunty/moodle/jaunty

« back to all changes in this revision

Viewing changes to auth/mnet/auth.php

  • Committer: Bazaar Package Importer
  • Author(s): Jordan Mantha, Matt Oquist
  • Date: 2009-02-25 15:16:22 UTC
  • mfrom: (1.1.11 upstream)
  • Revision ID: james.westby@ubuntu.com-20090225151622-0ekt1liwhv2obfza
Tags: 1.9.4.dfsg-0ubuntu1
* Merge with Debian git (Closes LP: #322961, #239481, #334611):
  - use Ubuntu's smarty lib directory for linking
  - use internal yui library 
  - add update-notifier support back in

[Matt Oquist]
  * renamed prerm script
  * significantly rewrote postinst and other maintainer scripts to improve
    user experience and package maintainability
    (Closes LP: #225662, #325450, #327843, #303078, #234609)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<?php
 
1
<?php // $Id: auth.php,v 1.27.2.13 2008/10/30 01:42:17 peterbulmer Exp $
2
2
 
3
3
/**
4
4
 * @author Martin Dougiamas
89
89
        }
90
90
 
91
91
        // session okay, try getting the user
92
 
        if (!$user = get_complete_user_data('id', $mnet_session->userid)) {
 
92
        if (!$user = get_record('user', 'id', $mnet_session->userid)) {
93
93
            echo mnet_server_fault(3, get_string('authfail_usermismatch', 'mnet'));
94
94
            exit;
95
95
        }
115
115
        $userdata['session.gc_maxlifetime']  = ini_get('session.gc_maxlifetime');
116
116
        $userdata['picture']                 = $user->picture;
117
117
        if (!empty($user->picture)) {
118
 
            $imagefile = "{$CFG->dataroot}/users/{$user->id}/f1.jpg";
 
118
            $imagefile = make_user_directory($user->id, true) . "/f1.jpg";
119
119
            if (file_exists($imagefile)) {
120
120
                $userdata['imagehash'] = sha1(file_get_contents($imagefile));
121
121
            }
171
171
        require_once $CFG->dirroot . '/mnet/xmlrpc/client.php';
172
172
 
173
173
        // check remote login permissions
174
 
        if (! has_capability('moodle/site:mnetlogintoremote', get_context_instance(CONTEXT_SYSTEM, SITEID))
 
174
        if (! has_capability('moodle/site:mnetlogintoremote', get_context_instance(CONTEXT_SYSTEM))
175
175
                or is_mnet_remote_user($USER)
176
176
                or $USER->username == 'guest'
177
177
                or empty($USER->id)) {
178
 
            error(get_string('notpermittedtojump', 'mnet'));
 
178
            print_error('notpermittedtojump', 'mnet');
179
179
        }
180
180
 
181
181
        // check for SSO publish permission first
182
182
        if ($this->has_service($mnethostid, 'sso_sp') == false) {
183
 
            error(get_string('hostnotconfiguredforsso', 'mnet'));
 
183
            print_error('hostnotconfiguredforsso', 'mnet');
184
184
        }
185
185
 
186
186
        // set RPC timeout to 30 seconds if not configured
209
209
            $mnet_session->expires = time() + (integer)ini_get('session.gc_maxlifetime');
210
210
            $mnet_session->session_id = session_id();
211
211
            if (! $mnet_session->id = insert_record('mnet_session', addslashes_object($mnet_session))) {
212
 
                error(get_string('databaseerror', 'mnet'));
 
212
                print_error('databaseerror', 'mnet');
213
213
            }
214
214
        } else {
215
215
            $mnet_session->useragent = sha1($_SERVER['HTTP_USER_AGENT']);
218
218
            $mnet_session->expires = time() + (integer)ini_get('session.gc_maxlifetime');
219
219
            $mnet_session->session_id = session_id();
220
220
            if (false == update_record('mnet_session', addslashes_object($mnet_session))) {
221
 
                error(get_string('databaseerror', 'mnet'));
 
221
                print_error('databaseerror', 'mnet');
222
222
            }
223
223
        }
224
224
 
225
225
        // construct the redirection URL
226
226
        //$transport = mnet_get_protocol($mnet_peer->transport);
227
227
        $wantsurl = urlencode($wantsurl);
228
 
        $url = "{$mnet_peer->wwwroot}/auth/mnet/land.php?token={$mnet_session->token}&idp={$MNET->wwwroot}&wantsurl={$wantsurl}";
 
228
        $url = "{$mnet_peer->wwwroot}{$mnet_peer->application->sso_land_url}?token={$mnet_session->token}&idp={$MNET->wwwroot}&wantsurl={$wantsurl}";
229
229
 
230
230
        return $url;
231
231
    }
244
244
        require_once $CFG->dirroot . '/mnet/xmlrpc/client.php';
245
245
 
246
246
        // verify the remote host is configured locally before attempting RPC call
247
 
        if (! $remotehost = get_record('mnet_host', 'wwwroot', $remotewwwroot)) {
248
 
            error(get_string('notpermittedtoland', 'mnet'));
 
247
        if (! $remotehost = get_record('mnet_host', 'wwwroot', $remotewwwroot, 'deleted', 0)) {
 
248
            print_error('notpermittedtoland', 'mnet');
249
249
        }
250
250
 
251
251
        // get the originating (ID provider) host info
268
268
                list($code, $message) = array_map('trim',explode(':', $errormessage, 2));
269
269
                if($code == 702) {
270
270
                    $site = get_site();
271
 
                    print_error('mnet_session_prohibited','mnet', $remotewwwroot, format_string($site->fullname));
 
271
                    print_error('mnet_session_prohibited', 'mnet', $remotewwwroot, format_string($site->fullname));
272
272
                    exit;
273
273
                }
274
274
                $message .= "ERROR $code:<br/>$errormessage<br/>";
285
285
        $firsttime = false;
286
286
 
287
287
        // get the local record for the remote user
288
 
        $localuser = get_record('user', 'username', $remoteuser->username, 'mnethostid', $remotehost->id);
 
288
        $localuser = get_record('user', 'username', addslashes($remoteuser->username), 'mnethostid', $remotehost->id);
289
289
 
290
290
        // add the remote user to the database if necessary, and if allowed
291
291
        // TODO: refactor into a separate function
292
 
        if (! $localuser->id) {
 
292
        if (empty($localuser) || ! $localuser->id) {
293
293
            if (empty($this->config->auto_add_remote_users)) {
294
 
                error(get_string('nolocaluser', 'mnet'));
 
294
                print_error('nolocaluser', 'mnet');
295
295
            }
296
296
            $remoteuser->mnethostid = $remotehost->id;
297
297
            if (! insert_record('user', addslashes_object($remoteuser))) {
298
 
                error(get_string('databaseerror', 'mnet'));
 
298
                print_error('databaseerror', 'mnet');
299
299
            }
300
300
            $firsttime = true;
301
301
            if (! $localuser = get_record('user', 'username', addslashes($remoteuser->username), 'mnethostid', $remotehost->id)) {
302
 
                error(get_string('nolocaluser', 'mnet'));
 
302
                print_error('nolocaluser', 'mnet');
303
303
            }
304
304
        }
305
305
 
319
319
 
320
320
            // TODO: fetch image if it has changed
321
321
            if ($key == 'imagehash') {
322
 
                $dirname = "{$CFG->dataroot}/users/{$localuser->id}";
 
322
                $dirname = make_user_directory($localuser->id, true);
323
323
                $filename = "$dirname/f1.jpg";
324
324
 
325
325
                $localhash = '';
338
338
                        if (strlen($fetchrequest->response['f1']) > 0) {
339
339
                            $imagecontents = base64_decode($fetchrequest->response['f1']);
340
340
                            file_put_contents($filename, $imagecontents);
 
341
                            $localuser->picture = 1;
341
342
                        }
342
343
                        if (strlen($fetchrequest->response['f2']) > 0) {
343
344
                            $imagecontents = base64_decode($fetchrequest->response['f2']);
391
392
            $mnet_session->expires = time() + (integer)$session_gc_maxlifetime;
392
393
            $mnet_session->session_id = session_id();
393
394
            if (! $mnet_session->id = insert_record('mnet_session', addslashes_object($mnet_session))) {
394
 
                error(get_string('databaseerror', 'mnet'));
 
395
                print_error('databaseerror', 'mnet');
395
396
            }
396
397
        } else {
397
398
            $mnet_session->expires = time() + (integer)$session_gc_maxlifetime;
771
772
                          join("\n", $mnet_request->error));
772
773
                break;
773
774
            }
774
 
 
775
 
            $query = "SELECT
776
 
                          l.id as remoteid,
777
 
                          l.time,
778
 
                          l.userid,
779
 
                          l.ip,
780
 
                          l.course,
781
 
                          l.module,
782
 
                          l.cmid,
783
 
                          l.action,
784
 
                          l.url,
785
 
                          l.info,
786
 
                          c.fullname as coursename,
787
 
                          c.modinfo as modinfo,
788
 
                          u.username
789
 
                      FROM
790
 
                          {$CFG->prefix}user u,
791
 
                          {$CFG->prefix}log l,
792
 
                          {$CFG->prefix}course c
793
 
                      WHERE
794
 
                          l.userid = u.id AND
795
 
                          u.mnethostid = '$mnethostid' AND
796
 
                          l.id > '".$mnet_request->response['last log id']."' AND
797
 
                          c.id = l.course
798
 
                      ORDER BY
799
 
                          remoteid ASC";
800
 
 
801
 
            $results = get_records_sql($query);
802
 
 
803
 
            if (false == $results) continue;
804
 
 
805
 
            $param = array();
806
 
 
807
 
            foreach($results as $result) {
808
 
                if (!empty($result->modinfo) && !empty($result->cmid)) {
809
 
                    $modinfo = unserialize($result->modinfo);
810
 
                    unset($result->modinfo);
 
775
            $mnethostlogssql = '
 
776
            SELECT
 
777
                mhostlogs.remoteid, mhostlogs.time, mhostlogs.userid, mhostlogs.ip,
 
778
                mhostlogs.course, mhostlogs.module, mhostlogs.cmid, mhostlogs.action,
 
779
                mhostlogs.url, mhostlogs.info, mhostlogs.username, c.fullname as coursename,
 
780
                c.modinfo
 
781
            FROM
 
782
                (
 
783
                    SELECT
 
784
                        l.id as remoteid, l.time, l.userid, l.ip, l.course, l.module, l.cmid,
 
785
                        l.action, l.url, l.info, u.username
 
786
                    FROM
 
787
                        ' . $CFG->prefix . 'user u
 
788
                        INNER JOIN ' . $CFG->prefix . 'log l on l.userid = u.id
 
789
                    WHERE
 
790
                        u.mnethostid = ' . $mnethostid . '
 
791
                        AND l.id > ' . $mnet_request->response['last log id'] . '
 
792
                    ORDER BY remoteid ASC
 
793
                    LIMIT 500
 
794
                ) mhostlogs
 
795
                INNER JOIN ' . $CFG->prefix . 'course c on c.id = mhostlogs.course
 
796
            ORDER by mhostlogs.remoteid ASC';
 
797
 
 
798
            $mnethostlogs = get_records_sql($mnethostlogssql);
 
799
 
 
800
            if ($mnethostlogs == false) {
 
801
                continue;
 
802
            }
 
803
 
 
804
            $processedlogs = array();
 
805
 
 
806
            foreach($mnethostlogs as $hostlog) {
 
807
                // Extract the name of the relevant module instance from the
 
808
                // course modinfo if possible.
 
809
                if (!empty($hostlog->modinfo) && !empty($hostlog->cmid)) {
 
810
                    $modinfo = unserialize($hostlog->modinfo);
 
811
                    unset($hostlog->modinfo);
811
812
                    $modulearray = array();
812
813
                    foreach($modinfo as $module) {
813
814
                        $modulearray[$module->cm] = urldecode($module->name);
814
815
                    }
815
 
                    $result->resource_name = $modulearray[$result->cmid];
 
816
                    $hostlog->resource_name = $modulearray[$hostlog->cmid];
816
817
                } else {
817
 
                    $result->resource_name = '';
 
818
                    $hostlog->resource_name = '';
818
819
                }
819
820
 
820
 
                $param[] = array (
821
 
                                    'remoteid'      => $result->remoteid,
822
 
                                    'time'          => $result->time,
823
 
                                    'userid'        => $result->userid,
824
 
                                    'ip'            => $result->ip,
825
 
                                    'course'        => $result->course,
826
 
                                    'coursename'    => $result->coursename,
827
 
                                    'module'        => $result->module,
828
 
                                    'cmid'          => $result->cmid,
829
 
                                    'action'        => $result->action,
830
 
                                    'url'           => $result->url,
831
 
                                    'info'          => $result->info,
832
 
                                    'resource_name' => $result->resource_name,
833
 
                                    'username'      => $result->username
 
821
                $processedlogs[] = array (
 
822
                                    'remoteid'      => $hostlog->remoteid,
 
823
                                    'time'          => $hostlog->time,
 
824
                                    'userid'        => $hostlog->userid,
 
825
                                    'ip'            => $hostlog->ip,
 
826
                                    'course'        => $hostlog->course,
 
827
                                    'coursename'    => $hostlog->coursename,
 
828
                                    'module'        => $hostlog->module,
 
829
                                    'cmid'          => $hostlog->cmid,
 
830
                                    'action'        => $hostlog->action,
 
831
                                    'url'           => $hostlog->url,
 
832
                                    'info'          => $hostlog->info,
 
833
                                    'resource_name' => $hostlog->resource_name,
 
834
                                    'username'      => $hostlog->username
834
835
                                 );
835
836
            }
836
837
 
837
 
            unset($result);
 
838
            unset($hostlog);
838
839
 
839
840
            $mnet_request = new mnet_xmlrpc_client();
840
841
            $mnet_request->set_method('auth/mnet/auth.php/refresh_log');
841
842
 
842
843
            // set $token and $useragent parameters
843
 
            $mnet_request->add_param($param);
 
844
            $mnet_request->add_param($processedlogs);
844
845
 
845
846
            if ($mnet_request->send($mnet_peer) === true) {
846
847
                if ($mnet_request->response['code'] > 0) {
885
886
 
886
887
            unset($logEntryObj->username);
887
888
 
 
889
            $logEntryObj = $this->trim_logline($logEntryObj);
888
890
            $insertok = insert_record('mnet_log', addslashes_object($logEntryObj), false);
889
891
 
890
892
            if ($insertok) {
891
893
                $MNET_REMOTE_CLIENT->last_log_id = $logEntryObj->remoteid;
 
894
                $MNET_REMOTE_CLIENT->updateparams->last_log_id = $logEntryObj->remoteid;
892
895
            } else {
893
896
                $returnString .= 'Record with id '.$logEntryObj->remoteid." failed to insert.\n";
894
897
            }
1123
1126
            $mnet_request->add_param($username);
1124
1127
            $mnet_request->add_param($useragent);
1125
1128
            if ($mnet_request->send($mnet_peer) === false) {
1126
 
                debugging("Server side error has occured on host $mnethostid: " .
 
1129
                debugging("Server side error has occured on host $mnetsession->mnethostid: " .
1127
1130
                          join("\n", $mnet_request->error));
1128
1131
            }
1129
1132
        }
1262
1265
        global $CFG;
1263
1266
 
1264
1267
        if ($user = get_record('user', 'username', addslashes($username), 'mnethostid', $CFG->mnet_localhost_id)) {
1265
 
            $filename1 = "{$CFG->dataroot}/users/{$user->id}/f1.jpg";
1266
 
            $filename2 = "{$CFG->dataroot}/users/{$user->id}/f2.jpg";
 
1268
            $filename1 = make_user_directory($user->id, true) . "/f1.jpg";
 
1269
            $filename2 = make_user_directory($user->id, true) . "/f2.jpg";
1267
1270
            $return = array();
1268
1271
            if (file_exists($filename1)) {
1269
1272
                $return['f1'] = base64_encode(file_get_contents($filename1));
1311
1314
                svc.apiversion,
1312
1315
                h2s.id as h2s_id
1313
1316
            FROM
 
1317
                {$CFG->prefix}mnet_host h,
1314
1318
                {$CFG->prefix}mnet_service svc,
1315
1319
                {$CFG->prefix}mnet_host2service h2s
1316
1320
            WHERE
 
1321
                h.deleted = '0' AND
 
1322
                h.id = h2s.hostid AND
1317
1323
                h2s.hostid = '$mnethostid' AND
1318
1324
                h2s.serviceid = svc.id AND
1319
1325
                svc.name = '$servicename' AND
1348
1354
        }
1349
1355
    }
1350
1356
 
 
1357
    /**
 
1358
     * Trims a log line from mnet peer to limit each part to a length which can be stored in our DB
 
1359
     *
 
1360
     * @param object $logline The log information to be trimmed
 
1361
     * @return object The passed logline object trimmed to not exceed storable limits
 
1362
     */
 
1363
    function trim_logline ($logline) {
 
1364
        $limits = array('ip' => 15, 'coursename' => 40, 'module' => 20, 'action' => 40,
 
1365
                        'url' => 255);
 
1366
        foreach ($limits as $property => $limit) {
 
1367
            if (isset($logline->$property)) {
 
1368
                $logline->$property = substr($logline->$property, 0, $limit);
 
1369
            }
 
1370
        }
 
1371
 
 
1372
        return $logline;
 
1373
    }
 
1374
 
 
1375
 
1351
1376
}
1352
1377
 
1353
1378
?>