Commit 8745ed40 authored by Donal McMullan's avatar Donal McMullan

Tighten up code: remove $CFG calls, fix attributions, remove some debug

code
parent 67cdde56
......@@ -59,7 +59,7 @@ $limit = param_integer('limit', 10);
$offset = param_integer('offset', 0);
$userid = $USER->get('id');
$prefix = get_config('prefix');
$prefix = get_config('dbprefix');
if ($type == 'all') {
$count = count_records('notification_internal_activity', 'usr', $userid);
......
......@@ -159,7 +159,7 @@ function statusChange() {
JAVASCRIPT;
$prefix = get_config('prefix');
$prefix = get_config('dbprefix');
$sql = 'SELECT DISTINCT u.*
FROM ' . $prefix . 'usr u
JOIN ' . $prefix . 'view v ON v.owner = u.id
......
......@@ -18,7 +18,7 @@
*
* @package mahara
* @subpackage admin
* @author Richard Mansfield <richard.mansfield@catalyst.net.nz>
* @author Donal McMullan <richard.mansfield@catalyst.net.nz>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL
* @copyright (C) 2006,2007 Catalyst IT Ltd http://catalyst.net.nz
*
......@@ -30,7 +30,7 @@ define('MENUITEM', 'configsite');
define('SUBMENUITEM', 'networking');
require(dirname(dirname(dirname(__FILE__))) . '/init.php');
require($CFG->docroot .'/api/xmlrpc/lib.php');
require_once(get_config('docroot') . 'api/xmlrpc/lib.php');
require_once('pieforms/pieform.php');
require_once('searchlib.php');
define('TITLE', get_string('networking', 'admin'));
......
......@@ -74,7 +74,7 @@ function adminusers_submit(Pieform $form, $values) {
execute_sql('UPDATE ' . $table . '
SET admin = 1
WHERE id IN (' . join(',', $values['users']) . ')');
execute_sql('DELETE FROM ' . get_config('prefix') . 'usr_activity_preference
execute_sql('DELETE FROM ' . get_config('dbprefix') . 'usr_activity_preference
WHERE activity IN (SELECT name FROM ' . get_config('dbprefix') . 'activity_type
WHERE admin = 1 )
AND usr NOT IN (' . join(',', $values['users']) . ')');
......
......@@ -75,14 +75,14 @@ $ALLOWEDKEYS = array(
* @return array Array of auth instance records
*/
function auth_get_auth_instances() {
global $CFG;
static $cache = array();
if(count($cache) > 0) {
return $cache;
}
// TODO: work out why this won't accept a placeholder - had to use db_quote
$dbprefix = get_config('dbprefix');
$sql ='
SELECT DISTINCT
i.id,
......@@ -90,8 +90,8 @@ function auth_get_auth_instances() {
institution.displayname,
i.instancename
FROM
'.$CFG->dbprefix.'institution,
'.$CFG->dbprefix.'auth_instance i
'.$dbprefix.'institution,
'.$dbprefix.'auth_instance i
WHERE
i.institution = institution.name
ORDER BY
......
......@@ -25,7 +25,7 @@
*/
defined('INTERNAL') || die();
require_once($CFG->docroot .'/api/xmlrpc/lib.php');
require(get_config('docroot') . 'api/xmlrpc/lib.php');
class Client {
......
......@@ -52,7 +52,6 @@ class Dispatcher {
);
function __construct($payload) {
global $CFG;
$this->payload = $payload;
// xmlrpc_decode_request is defined such that the '$method' string is
......@@ -111,8 +110,7 @@ class Dispatcher {
}
function keyswap($function, $params) {
global $CFG;
require_once($CFG->docroot .'/include/eLearning/peer.php');
require_once(get_config('docroot') . 'include/eLearning/peer.php');
//TODO: Verify params
(empty($params[0])) ? $wwwroot = null : $wwwroot = $params[0];
......
......@@ -56,7 +56,6 @@ function generate_token() {
}
function start_jump_session($peer, $instanceid, $wantsurl="") {
global $CFG;
global $USER;
$rpc_negotiation_timeout = 15;
......@@ -106,7 +105,7 @@ function start_jump_session($peer, $instanceid, $wantsurl="") {
}
}
$wwwroot = dropslash($CFG->wwwroot);
$wwwroot = dropslash(get_config('wwwroot'));
// construct the redirection URL
$url = "{$peer->wwwroot}{$peer->application->ssolandurl}?token={$sso_session->token}&idp={$wwwroot}&wantsurl={$wantsurl}";
......@@ -119,29 +118,26 @@ function api_dummy_method($methodname, $argsarray, $functionname) {
}
function fetch_user_image($username) {
global $REMOTEWWWROOT, $CFG;
global $REMOTEWWWROOT;
$institution = get_field('host', 'institution', 'wwwroot', $REMOTEWWWROOT);
$f = fopen('/tmp/fetchimage.txt','w');
fwrite($f, "Starting\n");
if (false == $institution) {
fwrite($f, "No ins\n");
// This should never happen, because if we don't know the host we'll
// already have exited
throw new XmlrpcServerException('Unknown error');
}
$authinstances = auth_get_auth_instances_for_institution($institution);
$candidates = array();
$dbprefix = get_config('dbprefix');
$authinstances = auth_get_auth_instances_for_institution($institution);
$candidates = array();
$sql = 'SElECT
ai.*
FROM
'.$CFG->dbprefix.'auth_instance ai,
'.$CFG->dbprefix.'auth_instance ai2,
'.$CFG->dbprefix.'auth_instance_config aic
'.$dbprefix.'auth_instance ai,
'.$dbprefix.'auth_instance ai2,
'.$dbprefix.'auth_instance_config aic
WHERE
ai.id = ? AND
ai.institution = ? AND
......@@ -152,7 +148,6 @@ function fetch_user_image($username) {
ai2.authname = \'xmlrpc\'';
foreach ($authinstances as $authinstance) {
fwrite($f, "{$authinstance->authname}\n");
if ($authinstance->authname != 'xmlrpc') {
$records = get_records_sql_array($sql, array($authinstance->id, $institution));
if (false == $records) {
......@@ -161,7 +156,6 @@ function fetch_user_image($username) {
}
try {
$user = new User;
fwrite($f, "{$authinstance->id}, $username\n");
$user->find_by_instanceid_username($authinstance->id, $username);
$candidates[$user->id] = $user;
} catch (Exception $e) {
......@@ -171,17 +165,14 @@ function fetch_user_image($username) {
}
if (count($candidates) != 1) {
fwrite($f, "".count($candidates)."\n");
return false;
}
$user = array_pop($candidates);
fwrite($f, "IC: {$user->profileicon}\n");
$ic = $user->profileicon;
if (!empty($ic)) {
$filename = get_config('dataroot') . 'artefact/internal/profileicons/' . ($user->profileicon % 256) . '/'.$user->profileicon;
fwrite($f, "FN: $filename\n");
$return = array();
try {
$fi = file_get_contents($filename);
......@@ -193,17 +184,16 @@ function fetch_user_image($username) {
require_once('file.php');
$im = get_dataroot_image_path('artefact/internal/profileicons' , $user->profileicon, '100x100');
fwrite($f, "IM: $im\n");
$fi = file_get_contents($im);
$return['f2'] = base64_encode($fi);
return $return;
} else {
fwrite($f, "EMPTY\n");
// no icon
}
}
function user_authorise($token, $useragent) {
global $CFG, $USER;
global $USER;
$sso_session = get_record('sso_session', 'token', $token, 'useragent', $useragent);
if (empty($sso_session)) {
......@@ -226,8 +216,8 @@ function user_authorise($token, $useragent) {
throw new Exception('Unable to get information for the specified user');
}
require_once($CFG->docroot .'/artefact/lib.php');
require_once($CFG->docroot .'/artefact/internal/lib.php');
require(get_config('docroot') . 'artefact/lib.php');
require(get_config('docroot') . 'artefact/internal/lib.php');
$element_list = call_static_method('ArtefactTypeProfile', 'get_all_fields');
$element_required = call_static_method('ArtefactTypeProfile', 'get_mandatory_fields');
......@@ -264,7 +254,7 @@ function user_authorise($token, $useragent) {
// Todo: push application name to list of hosts... update Moodle block to display more info, maybe in 'Other' list
$userdata['myhosts'] = array();
$userdata['myhosts'][] = array('name'=> $SITE->shortname, 'url' => $CFG->wwwroot, 'count' => 0);
$userdata['myhosts'][] = array('name'=> $SITE->shortname, 'url' => get_config('wwwroot'), 'count' => 0);
return $userdata;
}
......@@ -274,7 +264,6 @@ function user_authorise($token, $useragent) {
* instances of its canonical auth instance
*/
function get_service_providers($instance) {
global $CFG;
static $cache = array();
if (defined('INSTALLER')) {
......@@ -285,6 +274,8 @@ function get_service_providers($instance) {
return $cache[$instance];
}
$dbprefix = get_config('dbprefix');
$query = '
SELECT
h.name,
......@@ -292,11 +283,11 @@ function get_service_providers($instance) {
h.wwwroot,
aic.instance
FROM
'.$CFG->prefix.'auth_instance_config aic,
'.$CFG->prefix.'auth_instance_config aic2,
'.$CFG->prefix.'auth_instance_config aic3,
'.$CFG->prefix.'host h,
'.$CFG->prefix.'application a
'.$dbprefix.'auth_instance_config aic,
'.$dbprefix.'auth_instance_config aic2,
'.$dbprefix.'auth_instance_config aic3,
'.$dbprefix.'host h,
'.$dbprefix.'application a
WHERE
aic.value = ? AND
aic.field = \'parent\' AND
......@@ -330,7 +321,6 @@ function get_service_providers($instance) {
}
function get_public_key($uri, $application=null) {
global $CFG;
static $keyarray = array();
if (isset($keyarray[$uri])) {
......@@ -347,7 +337,7 @@ function get_public_key($uri, $application=null) {
if (empty($xmlrpcserverurl)) {
throw new XmlrpcClientException('Unknown application');
}
$wwwroot = dropslash($CFG->wwwroot);
$wwwroot = dropslash(get_config('wwwroot'));
$rq = xmlrpc_encode_request('system/keyswap', array($wwwroot, $openssl->certificate), array("encoding" => "utf-8"));
$ch = curl_init($uri . $xmlrpcserverurl);
......@@ -446,13 +436,12 @@ function parse_payload($payload) {
}
function get_peer($wwwroot) {
global $CFG;
$wwwroot = (string)$wwwroot;
static $peers = array();
if(isset($peers[$wwwroot])) return $peers[$wwwroot];
require_once($CFG->docroot .'/include/eLearning/peer.php');
require_once(get_config('docroot') . 'include/eLearning/peer.php');
$peer = new Peer();
if(!$peer->findByWwwroot($wwwroot)) {
......@@ -464,7 +453,6 @@ function get_peer($wwwroot) {
}
function xmldsig_envelope_strip(&$xml) {
global $CFG;
$signature = base64_decode($xml->Signature->SignatureValue);
$payload = base64_decode($xml->object);
......@@ -517,7 +505,6 @@ function xmldsig_envelope_strip(&$xml) {
* @return string An XML-ENC document
*/
function xmlenc_envelope($message, $remote_certificate) {
global $CFG;
// Generate a key resource from the remote_certificate text string
$publickey = openssl_get_publickey($remote_certificate);
......@@ -528,7 +515,7 @@ function xmlenc_envelope($message, $remote_certificate) {
}
// Initialize vars
$wwwroot = dropslash($CFG->wwwroot);
$wwwroot = dropslash(get_config('wwwroot'));
$encryptedstring = '';
$symmetric_keys = array();
......@@ -595,9 +582,9 @@ EOF;
* @return string An XML-DSig document
*/
function xmldsig_envelope($message) {
global $CFG;
$openssl = OpenSslRepo::singleton();
$wwwroot = dropslash($CFG->wwwroot);
$wwwroot = dropslash(get_config('wwwroot'));
$digest = sha1($message);
$sig = base64_encode($openssl->sign_message($message));
......@@ -844,8 +831,7 @@ class OpenSslRepo {
* @return string The signature over that text
*/
private function generate_keypair() {
global $CFG;
$host = get_hostname_from_uri($CFG->wwwroot);
$host = get_hostname_from_uri(get_config('wwwroot'));
$organization = get_config('sitename');
$email = get_config('noreplyaddress');
......
......@@ -46,7 +46,7 @@ require(dirname(__FILE__).'/lib.php');
// Catch anything that goes wrong in init.php
ob_start();
require(dirname(dirname(dirname(__FILE__))).'/init.php');
require_once($CFG->docroot .'/api/xmlrpc/dispatcher.php');
require_once(get_config('docroot') . 'api/xmlrpc/dispatcher.php');
$errors = trim(ob_get_contents());
ob_end_clean();
......
......@@ -25,8 +25,7 @@
*/
defined('INTERNAL') || die();
global $CFG;
require_once($CFG->docroot .'/auth/lib.php');
require_once(get_config('docroot') . 'auth/lib.php');
/**
* The internal authentication method, which authenticates users against the
......@@ -75,7 +74,6 @@ class AuthImap extends Auth {
*/
public function authenticate_user_account($user, $password) {
$this->must_be_ready();
global $CFG;
if (! function_exists('imap_open')) {
throw new ConfigException('IMAP is not available in your PHP environment. Check that it is properly installed');
......
......@@ -25,8 +25,7 @@
*/
defined('INTERNAL') || die();
global $CFG;
require_once($CFG->docroot .'/auth/lib.php');
require_once(get_config('docroot') . 'auth/lib.php');
/**
* The internal authentication method, which authenticates users against the
......
......@@ -388,13 +388,14 @@ function auth_setup () {
* @return array Array of auth instance records
*/
function auth_get_auth_instances_for_institution($institution=null) {
global $CFG;
static $cache = array();
if(null == $institution) {
return array();
}
$dbprefix = get_config('dbprefix');
if (!isset($cache[$institution])) {
// Get auth instances in order of priority
// DO NOT CHANGE THE SORT ORDER OF THIS RESULT SET
......@@ -410,8 +411,8 @@ function auth_get_auth_instances_for_institution($institution=null) {
a.requires_config,
a.requires_parent
FROM
'.$CFG->dbprefix.'auth_instance i,
'.$CFG->dbprefix.'auth_installed a
'.$dbprefix.'auth_instance i,
'.$dbprefix.'auth_installed a
WHERE
a.name = i.authname AND
i.institution = '. db_quote($institution).'
......@@ -429,16 +430,26 @@ function auth_get_auth_instances_for_institution($institution=null) {
return $cache[$institution];
}
/**
* Given a wwwroot, find any auth instances that can come from that host
*
* @param string wwwroot of the host that is connecting to us
* @return array array of record objects
*/
function auth_get_auth_instances_for_wwwroot($wwwroot) {
global $CFG;
$dbprefix = get_config('dbprefix');
// TODO: we just need ai.id and ai.authname... rewrite query, or
// just drop this function
$query = " SELECT
ai.*,
aic.*,
i.*
FROM
{$CFG->dbprefix}auth_instance ai,
{$CFG->dbprefix}auth_instance_config aic,
{$CFG->dbprefix}institution i
{$dbprefix}auth_instance ai,
{$dbprefix}auth_instance_config aic,
{$dbprefix}institution i
WHERE
aic.field = 'wwwroot' AND
aic.value = ? AND
......@@ -456,7 +467,6 @@ function auth_get_auth_instances_for_wwwroot($wwwroot) {
* @return array Array of auth instance records
*/
function auth_get_auth_instances_for_username($institution, $username) {
global $CFG;
static $cache = array();
if (!isset($cache[$institution][$username])) {
......@@ -464,6 +474,8 @@ function auth_get_auth_instances_for_username($institution, $username) {
// DO NOT CHANGE THE SORT ORDER OF THIS RESULT SET
// YEAH EINSTEIN - THAT MEANS YOU!!!
$dbprefix = get_config('dbprefix');
// TODO: work out why this won't accept a placeholder - had to use db_quote
$sql ='
SELECT DISTINCT
......@@ -474,9 +486,9 @@ function auth_get_auth_instances_for_username($institution, $username) {
a.requires_config,
a.requires_parent
FROM
'.$CFG->dbprefix.'auth_instance i,
'.$CFG->dbprefix.'auth_installed a,
'.$CFG->dbprefix.'usr u
'.$dbprefix.'auth_instance i,
'.$dbprefix.'auth_installed a,
'.$dbprefix.'usr u
WHERE
a.name = i.authname AND
i.institution = ? AND
......@@ -504,20 +516,22 @@ function auth_get_auth_instances_for_username($institution, $username) {
* @return array Array of auth instance records
*/
function auth_get_available_auth_types($institution=null) {
global $CFG;
if (!is_null($institution) && (!is_string($institution) || strlen($institution) > 255)) {
return array();
}
$dbprefix = get_config('dbprefix');
// TODO: work out why this won't accept a placeholder - had to use db_quote
$sql ='
SELECT DISTINCT
a.name,
a.requires_config
FROM
'.$CFG->dbprefix.'auth_installed a
'.$dbprefix.'auth_installed a
LEFT JOIN
'.$CFG->dbprefix.'auth_instance i
'.$dbprefix.'auth_instance i
ON
a.name = i.authname AND
i.institution = '. db_quote($institution).'
......
......@@ -94,19 +94,20 @@ class User {
*
*/
public function find_by_id($id) {
global $CFG;
if (!is_numeric($id) || $id < 0) {
throw new InvalidArgumentException('parameter must be a positive integer to create a User object');
}
$dbprefix = get_config('dbprefix');
$sql = 'SELECT
*,
' . db_format_tsfield('expiry') . ',
' . db_format_tsfield('lastlogin') . ',
' . db_format_tsfield('suspendedctime') . '
FROM
'.$CFG->dbprefix.'usr
'.$dbprefix.'usr
WHERE
id = ?';
......@@ -124,7 +125,6 @@ class User {
*
*/
public function find_by_instanceid_username($instanceid, $username) {
global $CFG;
if (!is_numeric($instanceid) || $instanceid < 0) {
throw new InvalidArgumentException('parameter must be a positive integer to create a User object');
......@@ -134,13 +134,15 @@ class User {
$instanceid = $parentid;
}
$dbprefix = get_config('dbprefix');
$sql = 'SELECT
*,
' . db_format_tsfield('expiry') . ',
' . db_format_tsfield('lastlogin') . ',
' . db_format_tsfield('suspendedctime') . '
FROM
'.$CFG->dbprefix.'usr
'.$dbprefix.'usr
WHERE
LOWER(username) = ? AND
authinstance = ?';
......
......@@ -14,9 +14,9 @@
define('INTERNAL', 1);
require(dirname(dirname(dirname(__FILE__))).'/init.php');
require_once($CFG->docroot .'/api/xmlrpc/client.php');
require_once($CFG->docroot .'/auth/xmlrpc/lib.php');
require_once($CFG->docroot .'/include/eLearning/institution.php');
require_once(get_config('docroot') .'api/xmlrpc/client.php');
require_once(get_config('docroot') .'auth/xmlrpc/lib.php');
require_once(get_config('docroot') .'include/eLearning/institution.php');
$remotewwwroot = param_variable('wr');
$instanceid = param_variable('ins');
......
......@@ -19,9 +19,9 @@ define('XMLRPC', 1);
require(dirname(dirname(dirname(__FILE__))).'/init.php');
require_once($CFG->docroot .'/api/xmlrpc/client.php');
require_once($CFG->docroot .'/auth/xmlrpc/lib.php');
require_once($CFG->docroot .'/include/eLearning/institution.php');
require_once(get_config('docroot') .'api/xmlrpc/client.php');
require_once(get_config('docroot') .'auth/xmlrpc/lib.php');
require_once(get_config('docroot') .'include/eLearning/institution.php');
$token = param_variable('token');
$remotewwwroot = param_variable('idp');
......@@ -48,6 +48,6 @@ foreach($instances as $instance) {
}
// confirm the MNET session
// redirect
redirect($CFG->wwwroot . $wantsurl);
redirect(get_config('wwwroot') . $wantsurl);
?>
......@@ -25,11 +25,10 @@
*/
defined('INTERNAL') || die();
global $CFG;
require_once($CFG->docroot .'/auth/lib.php');
require_once($CFG->docroot .'/include/eLearning/peer.php');
require_once($CFG->docroot .'/include/eLearning/applicationset.php');
require_once($CFG->docroot .'/api/xmlrpc/lib.php');
require_once(get_config('docroot') . 'auth/lib.php');
require_once(get_config('docroot') . 'include/eLearning/peer.php');
require_once(get_config('docroot') . 'include/eLearning/applicationset.php');
require_once(get_config('docroot') . 'api/xmlrpc/lib.php');
/**
* The XMLRPC authentication method, which authenticates users against the
......@@ -100,7 +99,7 @@ class AuthXmlrpc extends Auth {
* Grab a delegate object for auth stuff
*/
public function request_user_authorise($token, $remotewwwroot) {
global $CFG, $USER;
global $USER;
$this->must_be_ready();
$peer = get_peer($remotewwwroot);
......@@ -258,8 +257,8 @@ class AuthXmlrpc extends Auth {
$error = get_string('profileiconuploadexceedsquota', 'artefact.internal', get_config('wwwroot'));
}
require_once($CFG->docroot .'/artefact/lib.php');
require_once($CFG->docroot .'/artefact/internal/lib.php');
require_once(get_config('docroot') .'/artefact/lib.php');
require_once(get_config('docroot') .'/artefact/internal/lib.php');
// Entry in artefact table
$artefact = new ArtefactTypeProfileIcon();
......
......@@ -48,7 +48,7 @@ if (!$membership = user_can_access_community($id)) {
}
$community = get_record('community', 'id', $id);
$prefix = get_config('prefix');
$prefix = get_config('dbprefix');
$dbnow = db_format_timestamp(time());
switch ($type) {
......
......@@ -25,8 +25,7 @@
*/
defined('INTERNAL') || die();
global $CFG;
require_once($CFG->docroot .'/include/eLearning/application.php');
require_once(get_config('docroot') . 'include/eLearning/application.php');
class ApplicationSet implements Iterator {
......
......@@ -27,8 +27,7 @@
// TODO : lib
defined('INTERNAL') || die();
global $CFG;
require_once($CFG->docroot .'/include/eLearning/peer.php');
require_once(get_config('docroot') .'include/eLearning/peer.php');
class HostSet implements Iterator {
......@@ -44,12 +43,13 @@ class HostSet implements Iterator {
}
function findByInstitution($institution) {
global $CFG;
$dbprefix = get_config('dbprefix');
$sql = 'SELECT
h.*
FROM
'.$CFG->dbprefix.'host h