// Turn of magic_quotes_runtime. Anyone with this on deserves a slap in the faceset_magic_quotes_runtime(0);}if(ini_get_bool('magic_quotes_sybase')){// See above comment re. magic_quotes_runtime@ini_set('magic_quotes_sybase',0);

if('0'===ini_get('apc.stat')or'off'===ini_get('apc.stat')){// We don't run with apc.stat=0 (see https://bugs.launchpad.net/mahara/+bug/548333)thrownewConfigSanityException(get_string('apcstatoff','error'));}

// Since sessionpath can now exist outside of the the dataroot, check it separately.// NOTE: If we implement separate session handlers, we may want to remove or alter this checkif(!check_dir_exists(get_config('sessionpath'))||!is_writable(get_config('sessionpath'))){thrownewConfigSanityException(get_string('sessionpathnotwritable','error',get_config('sessionpath')));}

functionensure_upgrade_sanity(){// Check column collation is equal to the defaultif(is_mysql()){require_once('ddl.php');if(table_exists(newXMLDBTable('event_type'))){if(!column_collation_is_default('event_type','name')){thrownewConfigSanityException(get_string('dbcollationmismatch','admin'));}}

continue;}if($install){log_info(get_string('installingplugin','admin',$name));}else{log_info(get_string('upgradingplugin','admin',$name));}if($name=='firstcoredata'||$name=='lastcoredata'){$funname='core_install_'.$name.'_defaults';$funname();continue;}elseif($install&&$name=='localpreinst'){$name(array('localdata'=>true));}elseif($install&&$name=='localpostinst'){// Update local version

// Because we don't need to perform parameter replacement to test if string exists// we can use a simple pass-through for the $replacefunc argument.$string=get_string_location($identifier,$section,array(),'_return_string_unchanged');return$string!=='[['.$identifier.'/'.$section.']]';}

/** * Find out the number of expected arguments if $identifier for get_string() is * passed in by a variable. Eg in webservices error handling. */functioncount_string_args($identifier,$section='mahara'){$string=get_string_location($identifier,$section,array(),'_return_string_unchanged');preg_match_all('/[^\%]\%[^\%]/',$string,$matches);returncount($matches[0]);}

functionget_string_from_language($lang,$identifier,$section='mahara'){$variables=func_get_args();if(count($variables)>3){// we have some stuff we need to sprintfarray_shift($variables);array_shift($variables);array_shift($variables);//shift off the first three.}else{$variables=array();}

if($plugintype=='blocktype'){// these are a bit of a special case$bits=explode('/',$pluginname);if(count($bits)==2){$location='artefact/'.$bits[0].'/blocktype/'.$bits[1].'/lang/';}else{try{if($artefactplugin=blocktype_artefactplugin($pluginname)){$location='artefact/'.$artefactplugin.'/blocktype/'.$pluginname.'/lang/';}else{$location='blocktype/'.$pluginname.'/lang/';}}catch(SQLException$e){if(get_config('installed')){throw$e;}}}}elseif($plugintype!='core'){

// get a string without sprintfing it.functionget_raw_string($identifier,$section='mahara'){// For a raw string we don't want to format any arguments using// sprintf, so the replace function passed to get_string_location// should just return the first argument and ignore the second.

$extras=plugin_types();$extras[]='theme';// Allow themes to have lang files the same as pluginsforeach($extrasas$tocheck){if(strpos($section,$tocheck.'.')===0){$pluginname=substr($section,strlen($tocheck)+1);if($tocheck=='blocktype'&&strpos($pluginname,'/')!==false){// it belongs to an artefact plugin$bits=explode('/',$pluginname);$langdirectory='artefact/'.$bits[0].'/blocktype/'.$bits[1].'/lang/';$section='blocktype.'.$bits[1];}else{$langdirectory=$tocheck.'/'.$pluginname.'/lang/';

// First check the theme/plugin locations$result=get_string_local($docroot.$langdirectory,$lang.'/'.$section.'.php',$identifier);if($result!==false){return$replacefunc($result,$variables,$lang);}// Then check the default location for the string in the current language

// First check the theme/plugin locations$result=get_string_local($docroot.$langdirectory,$parentlang.'/'.$section.'.php',$identifier);if($result!==false){return$replacefunc($result,$variables,$parentlang);}// Then check the default location for the string in the current language$result=get_string_local(get_language_root($parentlang).$langdirectory,$parentlang.'/'.$section.'.php',$identifier);

/** * Get string from a file, checking the appropriate local customisation directory first * */functionget_string_local($langpath,$langfile,$identifier){foreach(array(get_config('docroot').'local/lang/',$langpath)as$dir){$file=$dir.$langfile;if(is_readable($file)){

/** * Returns a list of directories in which to search for language packs. * * This is influenced by the configuration variable 'langpacksearchpaths' */functionlanguage_get_searchpaths(){static$searchpaths=array();if(!$searchpaths){

$datarootbase=get_config('dataroot').'langpacks/*';$datarootpaths=glob($datarootbase,GLOB_MARK|GLOB_ONLYDIR);if($datarootpaths===false){log_warn("Problem searching for langfiles at this path: ".$datarootbase);$datarootpaths=array();}

// langpacksearchpaths configuration variable - for experts :)$lpsearchpaths=(array)get_config('langpacksearchpaths');$langpacksearchpaths=array();foreach($lpsearchpathsas$path){if(is_dir($path)){$langpacksearchpaths[]=(substr($path,-1)=='/')?$path:"$path/";}else{log_warn("Path in langpacksearchpaths is not a directory: $path");

* language the user is using * * Returns null if the language can't be found * * @param string $language The language to look for */functionget_language_root($language=null){static$language_root_cache=array();if(!isset($language_root_cache[$language])){if($language==null){$language=current_language();}foreach(language_get_searchpaths()as$path){if(is_dir("$path/lang/$language")){return$language_root_cache[$language]=$path;}}// Oh noes, can't be found$language_root_cache[$language]=null;}return$language_root_cache[$language];}

foreach($themelistAS$subdir=>$theme){$themes[$subdir]=isset($theme->displayname)?$theme->displayname:$subdir;}}return$themes;}/** * Return a list of themes available to this user * If the user is a member of any institutions, only themes available to * those institutions are returned; or * If a user is not a member of any institution, all themes not marked as * institution specific are returned. * @return array subdir => name */

return$themes;}/** * Return a list of themes available to the specified institution * @param string institution the name of the institution to load themes for * @return array subdir => name * @throws SystemException if unable to read the theme directory */functionget_institution_themes($institution){static$institutionthemes=array();if(!isset($institutionthemes[$institution])){$themes=get_all_theme_objects();$r=array();foreach($themesAS$subdir=>$theme){

return$institutionthemes[$institution];}/** * Return a list of all themes available on the system * @return array An array of theme objects * @throws SystemException if unable to read the theme directory */functionget_all_theme_objects(){static$themes=null;

// is the theme directory name valid?if(!Theme::name_is_valid($subdir)){log_warn(get_string('themenameinvalid','error',$subdir));}else{$config_path=$themebase.$subdir.'/themeconfig.php';if(is_readable($config_path)){require($config_path);if(empty($theme->disabled)||!$theme->disabled){

/** * This function loads up the basic $CFG * from the database table * note that it doesn't load plugin config * as not every page needs them * @return boolean false if the assignment fails (generally if the databse is not installed) */functionload_config(){

return$CFG->plugin->{$plugintype}->{$pluginname}->{$key};}// If we have already fetched this plugin's data from the database, then return NULL.// (Note that some values may come from config.php before we hit the database.)

// Now, return it if we found it, otherwise null.// (This could be done by a recursive call to get_config_plugin(), but it's// less error-prone to do it this way and it doesn't cause that much duplication)

$dbvalue=get_field($table,'value','plugin',$pluginname,'field',$key);if(false!==$dbvalue){if(// No need to update the DB if the value already matches($dbvalue===$value)||set_field($table,'value',$value,'plugin',$pluginname,'field',$key)){$success=true;}

$table=$plugintype.'_instance_config';$dbvalue=get_field($table,'value','instance',$instanceid,'field',$key);if($dbvalue!==false){if(// No need to update the DB if the value already matches($dbvalue===$value)||set_field($table,'value',$value,'instance',$instanceid,'field',$key)){

/** * Set or update an institution config value. * * @param string $institutionname The institution name * @param string $key The config name * @param string $value The config's new value * @return boolean Whether or not the config was updated successfully */functionset_config_institution($institutionname,$key,$value){global$CFG;if(isset($CFG->fetchedinst->{$institutionname})){$inst=$CFG->fetchedinst->{$institutionname};}else{// No cache hit, so instatiate a new Institution objecttry{$inst=newInstitution($institutionname);}catch(ParamOutOfRangeException$e){returnnull;}}if(isset($inst)){$inst->{$key}=$value;$inst->commit();returntrue;}returnfalse;}

* @param int $userid (Optional) If not supplied, fetch for the current user's institutions * @return array The results for the all the users' institutions, in the order * supplied by load_user_institutions(). Array key is institution name.

// Check for the user and key in the cache (The cache is stored in $CFG so it can be cleared/updated// if we ever write a set_config_institution() method)$userobj="user{$userid}";if(isset($CFG->userinstconf->{$userobj}->{$key})){return$CFG->userinstconf->{$userobj}->{$key};