📄 main_api.lib.php
字号:
return $url;}/*** Returns a difficult to guess password.* @param int $length, the length of the password* @return string the generated password*/function api_generate_password($length = 8){ $characters = 'abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789'; if ($length < 2) { $length = 2; } $password = ''; for ($i = 0; $i < $length; $i ++) { $password .= $characters[rand() % strlen($characters)]; } return $password;}/*** Checks a password to see wether it is OK to use.* @param string $password* @return true if the password is acceptable, false otherwise*/function api_check_password($password){ $lengthPass = strlen($password); if ($lengthPass < 5) { return false; } $passLower = strtolower($password); $cptLettres = $cptChiffres = 0; $consecutif = 0; $codeCharPrev = 0; for ($i = 0; $i < $lengthPass; $i ++) { $codeCharCur = ord($passLower[$i]); if ($i && abs($codeCharCur - $codeCharPrev) <= 1) { $consecutif ++; if ($consecutif == 3) { return false; } } else { $consecutif = 1; } if ($codeCharCur >= 97 && $codeCharCur <= 122) { $cptLettres ++; } elseif ($codeCharCur >= 48 && $codeCharCur <= 57) { $cptChiffres ++; } else { return false; } $codeCharPrev = $codeCharCur; } return ($cptLettres >= 3 && $cptChiffres >= 2) ? true : false;}/** * Clear the user ID from the session if it was the anonymous user. Generally * used on out-of-tools pages to remove a user ID that could otherwise be used * in the wrong context. * This function is to be used in conjunction with the api_set_anonymous() * function to simulate the user existence in case of an anonymous visit. * @param bool database check switch - passed to api_is_anonymous() * @return bool true if succesfully unregistered, false if not anonymous. */function api_clear_anonymous($db_check=false){ global $_user; if(api_is_anonymous($_user['user_id'],$db_check)) { unset($_user['user_id']); api_session_unregister('_uid'); return true; } else { return false; }}/** * truncates a string * * @author Brouckaert Olivier * @param string text - text to truncate * @param integer length - length of the truncated text * @param string endStr - suffix * @param boolean middle - if true, truncates on string middle */function api_trunc_str($text, $length = 30, $endStr = '...', $middle = false){ if (strlen($text) <= $length) { return $text; } if ($middle) { $text = rtrim(substr($text, 0, round($length / 2))).$endStr.ltrim(substr($text, -round($length / 2))); } else { $text = rtrim(substr($text, 0, $length)).$endStr; } return $text;}// deprecated, use api_trunc_str() insteadfunction shorten($input, $length = 15){ $length = intval($length); if (!$length) { $length = 15; } return api_trunc_str($input, $length);}/** * handling simple and double apostrofe in order that strings be stored properly in database * * @author Denes Nagy * @param string variable - the variable to be revised */function domesticate($input){ $input = stripslashes($input); $input = str_replace("'", "''", $input); $input = str_replace('"', "''", $input); return ($input);}/*============================================================================== FAILURE MANAGEMENT==============================================================================*//* * The Failure Management module is here to compensate * the absence of an 'exception' device in PHP 4. *//** * $api_failureList - array containing all the failure recorded * in order of arrival. */$api_failureList = array ();/** * Fills a global array called $api_failureList * This array collects all the failure occuring during the script runs * The main purpose is allowing to manage the display messages externaly * from the functions or objects. This strengthens encupsalation principle * * @author Hugues Peeters <peeters@ipm.ucl.ac.be> * @param string $failureType - the type of failure * @global array $api_failureList * @return bolean false to stay consistent with the main script */function api_set_failure($failureType){ global $api_failureList; $api_failureList[] = $failureType; return false;}/** * Sets the current user as anonymous if it hasn't been identified yet. This * function should be used inside a tool only. The function api_clear_anonymous() * acts in the opposite direction by clearing the anonymous user's data every * time we get on a course homepage or on a neutral page (index, admin, my space) * @return bool true if set user as anonymous, false if user was already logged in or anonymous id could not be found */function api_set_anonymous(){ global $_user; if(!empty($_user['user_id'])) { return false; } else { $user_id = api_get_anonymous_id(); if($user_id == 0) { return false; } else { api_session_unregister('_user'); $_user['user_id'] = $user_id; $_user['is_anonymous'] = true; api_session_register('_user'); $GLOBALS['_user'] = $_user; return true; } }}/** * get the last failure stored in $api_failureList; * * @author Hugues Peeters <hugues.peeters@claroline.net> * @param void * @return string - the last failure stored */function api_get_last_failure(){ global $api_failureList; return $api_failureList[count($api_failureList) - 1];}/** * collects and manage failures occuring during script execution * The main purpose is allowing to manage the display messages externaly * from functions or objects. This strengthens encupsalation principle * * @author Hugues Peeters <hugues.peeters@claroline.net> * @package dokeos.library */class api_failure{ /* * IMPLEMENTATION NOTE : For now the $api_failureList list is set to the * global scope, as PHP 4 is unable to manage static variable in class. But * this feature is awaited in PHP 5. The class is already written to minize * the change when static class variable will be possible. And the API won't * change. */ public $api_failureList = array (); /** * Pile the last failure in the failure list * * @author Hugues Peeters <peeters@ipm.ucl.ac.be> * @param string $failureType - the type of failure * @global array $api_failureList * @return bolean false to stay consistent with the main script */ function set_failure($failureType) { global $api_failureList; $api_failureList[] = $failureType; return false; } /** * get the last failure stored * * @author Hugues Peeters <hugues.peeters@claroline.net> * @param void * @return string - the last failure stored */ function get_last_failure() { global $api_failureList; return $api_failureList[count($api_failureList) - 1]; }}/*============================================================================== CONFIGURATION SETTINGS==============================================================================*//*** DEPRECATED, use api_get_setting instead*/function get_setting($variable, $key = NULL){ global $_setting; return api_get_setting($variable, $key);}/*** Returns the value of a setting from the web-adjustable admin config settings.** WARNING true/false are stored as string, so when comparing you need to check e.g.* if(api_get_setting("show_navigation_menu") == "true") //CORRECT* instead of* if(api_get_setting("show_navigation_menu") == true) //INCORRECT* @param string The variable name* @param string The subkey (sub-variable) if any. Defaults to NULL* @author Rene Haentjens* @author Bart Mollet*/function api_get_setting($variable, $key = NULL){ global $_setting; return is_null($key) ? (!empty($_setting[$variable])?$_setting[$variable]:null) : $_setting[$variable][$key];}/** * Returns an escaped version of $_SERVER['PHP_SELF'] to avoid XSS injection * @return string Escaped version of $_SERVER['PHP_SELF'] */function api_get_self(){ return htmlentities($_SERVER['PHP_SELF']);}/*============================================================================== LANGUAGE SUPPORT==============================================================================*//*** Whenever the server type in the Dokeos Config settings is* set to test/development server you will get an indication that a language variable* is not translated and a link to a suggestions form of DLTT.** @return language variable '$lang'.$variable or language variable $variable.** @author Roan Embrechts* @author Patrick Cool*/function get_lang($variable, $notrans = 'DLTT'){ $ot = '[='; //opening tag for missing vars $ct = '=]'; //closing tag for missing vars if(api_get_setting('hide_dltt_markup') == 'true') { $ot = ''; $ct = ''; } if (api_get_setting('server_type') != 'test') { $lvv = isset ($GLOBALS['lang'.$variable]) ? $GLOBALS['lang'.$variable] : (isset ($GLOBALS[$variable]) ? $GLOBALS[$variable] : $ot.$variable.$ct); if (!is_string($lvv)) return $lvv; return str_replace("\\'", "'", $lvv); } if (!is_string($variable)) return $ot.'get_lang(?)'.$ct; global $language_interface, $language_files; //language file specified in tool $langpath = api_get_path(SYS_CODE_PATH).'lang/'; if (isset ($language_files)) { if (!is_array($language_files)) { @include ($langpath.$language_interface.'/'.$language_files.'.inc.php'); } else { foreach ($language_files as $index => $language_file) { @include ($langpath.$language_interface.'/'.$language_file.'.inc.php'); } } } @ eval ('$langvar = $'.$variable.';'); // Note (RH): $$var doesn't work with arrays, see PHP doc if (isset ($langvar) && is_string($langvar) && strlen($langvar) > 0) { return str_replace("\\'", "'", $langvar); } @ eval ('$langvar = $lang'.$variable.';'); if (isset ($langvar) && is_string($langvar) && strlen($langvar) > 0) { return str_replace("\\'", "'", $langvar); } if ($notrans != 'DLTT') return $ot.$variable.$ct; if (!is_array($language_files)) { $language_file = $language_files; } else { $language_file = implode('.inc.php',$language_files); } return $ot.$variable.$ct."<a href=\"http://www.dokeos.com/DLTT/suggestion.php?file=".$language_file.".inc.php&variable=$".$variable."&language=".$language_interface."\" style=\"color:#FF0000\"><strong>#</strong></a>";}/** * Gets the current interface language * @return string The current language of the interface */function api_get_interface_language(){ global $language_interface; return $language_interface;}/*============================================================================== USER PERMISSIONS==============================================================================*//*** Check if current user is a platform administrator* @return boolean True if the user has platform admin rights,* false otherwise.*/function api_is_platform_admin($allow_sessions_admins = false){ if($_SESSION['is_platformAdmin']) return true; else { global $_user; if($allow_sessions_admins && $_user['status']==SESSIONADMIN) return true; } return false;}/** * Check if current user is allowed to create courses* @return boolean True if the user has course creation rights,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -