From d234faf3a420826110773e4aedc63cce08a1753f Mon Sep 17 00:00:00 2001 From: Penny Leach Date: Sat, 1 May 2010 15:53:45 +0000 Subject: [PATCH] mnet MDL-22267 updated mnet_server_faultand mnet_server_exception string handling This is so that mnet_server_fault doesn't have to call get_string (in case of coding exception) --- auth/mnet/auth.php | 8 ++++---- enrol/mnet/enrol.php | 14 +++++++------- mnet/xmlrpc/server.php | 10 +++++----- mnet/xmlrpc/serverlib.php | 20 ++++++-------------- portfolio/mahara/lib.php | 12 ++++++------ repository/remotemoodle/repository.class.php | 8 ++++---- 6 files changed, 32 insertions(+), 40 deletions(-) diff --git a/auth/mnet/auth.php b/auth/mnet/auth.php index 1d056d18b03dc..e80e456381639 100644 --- a/auth/mnet/auth.php +++ b/auth/mnet/auth.php @@ -59,17 +59,17 @@ function user_authorise($token, $useragent) { $mnet_session = $DB->get_record('mnet_session', array('token'=>$token, 'useragent'=>$useragent)); if (empty($mnet_session)) { - throw new mnet_server_exception(1, get_string('authfail_nosessionexists', 'mnet')); + throw new mnet_server_exception(1, 'authfail_nosessionexists'); } // check session confirm timeout if ($mnet_session->confirm_timeout < time()) { - throw new mnet_server_exception(2, get_string('authfail_sessiontimedout', 'mnet')); + throw new mnet_server_exception(2, 'authfail_sessiontimedout'); } // session okay, try getting the user if (!$user = $DB->get_record('user', array('id'=>$mnet_session->userid))) { - throw new mnet_server_exception(3, get_string('authfail_usermismatch', 'mnet')); + throw new mnet_server_exception(3, 'authfail_usermismatch'); } $userdata = mnet_strip_user((array)$user, mnet_fields_to_send($remoteclient)); @@ -463,7 +463,7 @@ function update_enrolments($username, $courses) { // with that host... if (!$userid = $DB->get_field('mnet_session', 'userid', array('username'=>$username, 'mnethostid'=>$remoteclient->id))) { - throw new mnet_server_exception(1, get_string('authfail_nosessionexists', 'mnet')); + throw new mnet_server_exception(1, 'authfail_nosessionexists'); } if (empty($courses)) { // no courses? clear out quickly diff --git a/enrol/mnet/enrol.php b/enrol/mnet/enrol.php index 66f479f7bcfbf..e7cb0a9b91224 100644 --- a/enrol/mnet/enrol.php +++ b/enrol/mnet/enrol.php @@ -310,12 +310,12 @@ function enrol_user($user, $courseid) { if ($userrecord->id = $DB->insert_record('user', $userrecord)) { $userrecord = $DB->get_record('user', array('id'=>$userrecord->id)); } else { - throw new mnet_server_exception(5011, get_string('couldnotcreateuser', 'enrol_mnet')); + throw new mnet_server_exception(5011, 'couldnotcreateuser', 'enrol_mnet'); } } if (! $course = $DB->get_record('course', array('id'=>$courseid))) { - throw new mnet_server_exception(5012, get_string('coursenotfound', 'enrol_mnet')); + throw new mnet_server_exception(5012, 'coursenotfound', 'enrol_mnet'); } $courses = $this->available_courses(); @@ -324,9 +324,9 @@ function enrol_user($user, $courseid) { if (enrol_into_course($course, $userrecord, 'mnet')) { return true; } - throw new mnet_server_exception(5016, get_string('couldnotenrol', 'enrol_mnet')); + throw new mnet_server_exception(5016, 'couldnotenrol', 'enrol_mnet'); } - throw new mnet_server_exception(5013, get_string('courseunavailable', 'enrol_mnet')); + throw new mnet_server_exception(5013, 'courseunavailable', 'enrol_mnet'); } /** @@ -341,11 +341,11 @@ function unenrol_user($username, $courseid) { $remoteclient = get_mnet_remote_client(); if (!$userrecord = $DB->get_record('user', array('username'=>$username, 'mnethostid'=>$remoteclient->id))) { - throw new mnet_exception(5014, get_string('usernotfound', 'enrol_mnet')); + throw new mnet_exception(5014, 'usernotfound', 'enrol_mnet'); } if (! $course = $DB->get_record('course', array('id'=>$courseid))) { - throw new mnet_server_exception(5012, get_string('coursenotfound', 'enrol_mnet')); + throw new mnet_server_exception(5012, 'coursenotfound', 'enrol_mnet'); } $context = get_context_instance(CONTEXT_COURSE, $course->id); @@ -354,7 +354,7 @@ function unenrol_user($username, $courseid) { // require_capability('moodle/role:assign', $context, NULL, false); if (!role_unassign(0, $userrecord->id, 0, $context->id)) { - throw new mnet_exception(5015, get_string('couldnotunenrol', 'enrol_mnet')); + throw new mnet_exception(5015, 'couldnotunenrol', 'enrol_mnet'); } return true; diff --git a/mnet/xmlrpc/server.php b/mnet/xmlrpc/server.php index 44c996ba68dd0..32ad87581c15b 100644 --- a/mnet/xmlrpc/server.php +++ b/mnet/xmlrpc/server.php @@ -43,7 +43,7 @@ mnet_debug($HTTP_RAW_POST_DATA, 2); if (!isset($_SERVER)) { - exit(mnet_server_fault(712, "phperror")); + exit(mnet_server_fault(712, get_string('phperror', 'mnet'))); } @@ -95,19 +95,19 @@ // so detect a few common cases and send appropriate errors if (($remoteclient->request_was_encrypted == false) && ($remoteclient->plaintext_is_ok() == false)) { mnet_debug('non encrypted request'); - exit(mnet_server_fault(7021, 'forbidden-transport')); + exit(mnet_server_fault(7021, get_string('forbidden-transport', 'mnet'))); } if ($remoteclient->request_was_signed == false) { // Request was not signed mnet_debug('non signed request'); - exit(mnet_server_fault(711, 'verifysignature-error')); + exit(mnet_server_fault(711, get_string('verifysignature-error', 'mnet'))); } if ($remoteclient->signatureok == false) { // We were unable to verify the signature mnet_debug('non verified signature'); - exit(mnet_server_fault(710, 'verifysignature-invalid')); + exit(mnet_server_fault(710, get_string('verifysignature-invalid', 'mnet'))); } mnet_debug('unknown error'); -exit(mnet_server_fault(7000, 'unknownerror')); +exit(mnet_server_fault(7000, get_string('unknownerror', 'mnet'))); diff --git a/mnet/xmlrpc/serverlib.php b/mnet/xmlrpc/serverlib.php index d4e3f88c9a04f..245b568d93049 100644 --- a/mnet/xmlrpc/serverlib.php +++ b/mnet/xmlrpc/serverlib.php @@ -160,8 +160,7 @@ function mnet_server_strip_signature($plaintextmessage) { * Return the proper XML-RPC content to report an error in the local language. * * @param int $code The ID code of the error message - * @param string $text The array-key of the error message in the lang file - * or the full string (will be detected by the function + * @param string $text The full string of the error message (get_string will not be called) * @param string $param The $a param for the error message in the lang file * @return string $text The text of the error message */ @@ -170,13 +169,7 @@ function mnet_server_fault($code, $text, $param = null) { $code = 0; } $code = intval($code); - - $string = get_string($text, 'mnet', $param); - if (strpos($string, '[[') === 0) { - $string = $text; - } - - return mnet_server_fault_xml($code, $string); + return mnet_server_fault_xml($code, $text); } /** @@ -667,7 +660,6 @@ function mnet_server_dummy_method($methodname, $argsarray, $functionname) { } /** * mnet server exception. extends moodle_exception, but takes slightly different arguments. - * error strings are always in mnet, so $module is not necessary, * and unlike the rest of moodle, the actual int error code is used. * this exception should only be used during an xmlrpc server request, ie, not for client requests. */ @@ -675,13 +667,13 @@ class mnet_server_exception extends moodle_exception { /** * @param int $intcode the numerical error associated with this fault. this is not the string errorcode - * @param string $languagekey the key to the language string for the error message, or the text of the message (mnet_server_fault figures it out for you) if you're not using mnet error string file + * @param string $langkey the error message in full (get_string will not be used) + * @param string $module the language module, defaults to 'mnet' * @param mixed $a params for get_string */ - public function __construct($intcode, $languagekey, $a=null) { - parent::__construct($languagekey, 'mnet', '', $a); + public function __construct($intcode, $languagekey, $module='mnet', $a=null) { + parent::__construct($languagekey, $module, '', $a); $this->code = $intcode; - $this->message = $languagekey; // override this because mnet_server_fault (our handler) uses this directly } } diff --git a/portfolio/mahara/lib.php b/portfolio/mahara/lib.php index 8f70a211af756..9ca8a87d78cfc 100644 --- a/portfolio/mahara/lib.php +++ b/portfolio/mahara/lib.php @@ -334,30 +334,30 @@ public static function fetch_file($token) { $remoteclient = get_mnet_remote_client(); try { if (!$transferid = $DB->get_field('portfolio_mahara_queue', 'transferid', array('token' => $token))) { - throw new mnet_server_exception(8009, get_string('mnet_notoken', 'portfolio_mahara')); + throw new mnet_server_exception(8009, 'mnet_notoken', 'portfolio_mahara'); } $exporter = portfolio_exporter::rewaken_object($transferid); } catch (portfolio_exception $e) { - throw new mnet_server_exception(8010, get_string('mnet_noid', 'portfolio_mahara')); + throw new mnet_server_exception(8010, 'mnet_noid', 'portfolio_mahara'); } if ($exporter->get('instance')->get_config('mnethostid') != $remoteclient->id) { - throw new mnet_server_exception(8011, get_string('mnet_wronghost', 'portfolio_mahara')); + throw new mnet_server_exception(8011, 'mnet_wronghost', 'portfolio_mahara'); } global $CFG; try { $i = $exporter->get('instance'); $f = $i->get('file'); if (empty($f) || !($f instanceof stored_file)) { - throw new mnet_server_exception(8012, get_string('mnet_nofile', 'portfolio_mahara')); + throw new mnet_server_exception(8012, 'mnet_nofile', 'portfolio_mahara'); } try { $c = $f->get_content(); } catch (file_exception $e) { - throw new mnet_server_exception(8013, get_string('mnet_nofilecontents', 'portfolio_mahara', $e->getMessage())); + throw new mnet_server_exception(8013, 'mnet_nofilecontents', 'portfolio_mahara', $e->getMessage()); } $contents = base64_encode($c); } catch (Exception $e) { - throw new mnet_server_exception(8013, get_string('mnet_nofile', 'portfolio_mahara')); + throw new mnet_server_exception(8013, 'mnet_nofile', 'portfolio_mahara'); } $exporter->log_transfer(); $exporter->process_stage_cleanup(true); diff --git a/repository/remotemoodle/repository.class.php b/repository/remotemoodle/repository.class.php index 2615eb9bb557f..02dcfdf7edb85 100644 --- a/repository/remotemoodle/repository.class.php +++ b/repository/remotemoodle/repository.class.php @@ -66,7 +66,7 @@ public function retrieveFile($username, $source) { // also is the user allowed to roam? $USER = $DB->get_record('user',array('username' => $username, 'mnethostid' => $remoteclient->id)); if (empty($USER)) { - throw new mnet_server_exception(9012, get_string('usernotfound', 'repository_remotemoodle', $username)); + throw new mnet_server_exception(9012, 'usernotfound', 'repository_remotemoodle', $username); } $file = unserialize(base64_decode($source)); @@ -80,7 +80,7 @@ public function retrieveFile($username, $source) { $browser = get_file_browser(); $fileinfo = $browser->get_file_info(get_context_instance_by_id($contextid), $filearea, $itemid, $filepath, $filename); if (empty($fileinfo)) { - throw new mnet_server_exception(9013, get_string('usercannotaccess', 'repository_remotemoodle', $file)); + throw new mnet_server_exception(9013, 'usercannotaccess', 'repository_remotemoodle', $file); } ///retrieve the file with file API functions and return it encoded in base64 @@ -109,14 +109,14 @@ public function getFileList($username, $search) { // also is the user allowed to roam? $USER = $DB->get_record('user',array('username' => $username, 'mnethostid' => $remoteclient->id)); if (empty($USER)) { - throw new mnet_server_exception(9012, get_string('usernotfound', 'repository_remotemoodle', $username)); + throw new mnet_server_exception(9012, 'usernotfound', 'repository_remotemoodle', $username); } try { return repository::get_user_file_tree($search); } catch (Exception $e) { - throw new mnet_server_exception(9014, get_string('failtoretrievelist', 'repository_remotemoodle')); + throw new mnet_server_exception(9014, 'failtoretrievelist', 'repository_remotemoodle'); } }