📄 main_api.lib.php
字号:
break; }}/*** This function returns the id of the user which is stored in the $_user array.** @example The function can be used to check if a user is logged in* if (api_get_user_id())* @return integer the id of the current user*/function api_get_user_id(){ if(empty($GLOBALS['_user']['user_id'])) { return 0; } return $GLOBALS['_user']['user_id'];}/** * @param $user_id (integer): the id of the user * @return $user_info (array): user_id, lastname, firstname, username, email, ... * @author Patrick Cool <patrick.cool@UGent.be> * @version 21 September 2004 * @desc find all the information about a user. If no paramater is passed you find all the information about the current user.*/function api_get_user_info($user_id = ''){ global $tbl_user; if ($user_id == '') { return $GLOBALS["_user"]; } else { $sql = "SELECT * FROM ".Database :: get_main_table(TABLE_MAIN_USER)." WHERE user_id='".mysql_real_escape_string($user_id)."'"; $result = api_sql_query($sql, __FILE__, __LINE__); if(mysql_num_rows($result) > 0) { $result_array = mysql_fetch_array($result); // this is done so that it returns the same array-index-names // ideally the names of the fields of the user table are renamed so that they match $_user (or vice versa) // $_user should also contain every field of the user table (except password maybe). This would make the // following lines obsolete (and the code cleaner and slimmer !!! $user_info['firstName'] = $result_array['firstname']; $user_info['lastName'] = $result_array['lastname']; $user_info['mail'] = $result_array['email']; $user_info['picture_uri'] = $result_array['picture_uri']; $user_info['user_id'] = $result_array['user_id']; $user_info['official_code'] = $result_array['official_code']; $user_info['status'] = $result_array['status']; $user_info['auth_source'] = $result_array['auth_source']; $user_info['username'] = $result_array['username']; $user_info['theme'] = $result_array['theme']; return $user_info; } return false; }}/** * Returns the current course id (integer)*/function api_get_course_id(){ return $GLOBALS["_cid"];}/** * Returns the current course directory * * This function relies on api_get_course_info() * @param string The course code - optional (takes it from session if not given) * @return string The directory where the course is located inside the Dokeos "courses" directory * @author Yannick Warnier <yannick.warnier@dokeos.com>*/function api_get_course_path($course_code=null){ if(!empty($course_code)) { $info = api_get_course_info($course_code); } else { $info = api_get_course_info(); } return $info['path'];}/** * Gets a course setting from the current course_setting table. Try always using integer values. * @param string The name of the setting we want from the table * @return mixed The value of that setting in that table. Return -1 if not found. */function api_get_course_setting($setting_name){ $table = Database::get_course_table(TABLE_COURSE_SETTING); $setting_name = mysql_real_escape_string($setting_name); $sql = "SELECT * FROM $table WHERE variable = '$setting_name'"; $res = api_sql_query($sql,__FILE__,__LINE__); if(Database::num_rows($res)>0){ $row = Database::fetch_array($res); return $row['value']; } return -1;}/** * Gets an anonymous user ID * * For some tools that need tracking, like the learnpath tool, it is necessary * to have a usable user-id to enable some kind of tracking, even if not * perfect. An anonymous ID is taken from the users table by looking for a * status of "6" (anonymous). * @return int User ID of the anonymous user, or O if no anonymous user found */function api_get_anonymous_id(){ $table = Database::get_main_table(TABLE_MAIN_USER); $sql = "SELECT user_id FROM $table WHERE status = 6"; $res = api_sql_query($sql,__FILE__,__LINE__); if(Database::num_rows($res)>0) { $row = Database::fetch_array($res); //error_log('api_get_anonymous_id() returns '.$row['user_id'],0); return $row['user_id']; } else //no anonymous user was found { return 0; }}/** * Returns the cidreq parameter name + current course id*/function api_get_cidreq(){ if (!empty ($GLOBALS["_cid"])) { return 'cidReq='.htmlspecialchars($GLOBALS["_cid"]); } return '';}/*** Returns the current course info array.* Note: this array is only defined if the user is inside a course.* Array elements:* ['name']* ['official_code']* ['sysCode']* ['path']* ['dbName']* ['dbNameGlu']* ['titular']* ['language']* ['extLink']['url' ]* ['extLink']['name']* ['categoryCode']* ['categoryName']* Now if the course_code is given, the returned array gives info about that* particular course, not specially the current one.* @todo same behaviour as api_get_user_info so that api_get_course_id becomes absolete too*/function api_get_course_info($course_code=null){ if(!empty($course_code)) { $course_code = Database::escape_string($course_code); $course_table = Database::get_main_table(TABLE_MAIN_COURSE); $course_cat_table = Database::get_main_table(TABLE_MAIN_CATEGORY); $sql = "SELECT `course`.*, `course_category`.`code` `faCode`, `course_category`.`name` `faName` FROM $course_table LEFT JOIN $course_cat_table ON `course`.`category_code` = `course_category`.`code` WHERE `course`.`code` = '$course_code'"; $result = api_sql_query($sql,__FILE__,__LINE__); $_course = array(); if (Database::num_rows($result)>0) { global $_configuration; $cData = Database::fetch_array($result); $_course['id' ] = $cData['code' ]; //auto-assigned integer $_course['name' ] = $cData['title' ]; $_course['official_code'] = $cData['visual_code' ]; // use in echo $_course['sysCode' ] = $cData['code' ]; // use as key in db $_course['path' ] = $cData['directory' ]; // use as key in path $_course['dbName' ] = $cData['db_name' ]; // use as key in db list $_course['dbNameGlu' ] = $_configuration['table_prefix'] . $cData['db_name'] . $_configuration['db_glue']; // use in all queries $_course['titular' ] = $cData['tutor_name' ]; $_course['language' ] = $cData['course_language' ]; $_course['extLink' ]['url' ] = $cData['department_url' ]; $_course['extLink' ]['name'] = $cData['department_name']; $_course['categoryCode'] = $cData['faCode' ]; $_course['categoryName'] = $cData['faName' ]; $_course['visibility' ] = $cData['visibility']; $_course['subscribe_allowed'] = $cData['subscribe']; $_course['unubscribe_allowed'] = $cData['unsubscribe']; } return $_course; } else { global $_course; return $_course; }}/*============================================================================== DATABASE QUERY MANAGEMENT==============================================================================*//** * Executes an SQL query * You have to use addslashes() on each value that you want to record into the database * * @author Olivier Brouckaert * @param string $query - SQL query * @param string $file - optional, the file path and name of the error (__FILE__) * @param string $line - optional, the line of the error (__LINE__) * @return resource - the return value of the query */function api_sql_query($query, $file = '', $line = 0){ $result = @mysql_query($query); if ($line && !$result) { $info = '<pre>'; $info .= '<b>MYSQL ERROR :</b><br/> '; $info .= mysql_error(); $info .= '<br/>'; $info .= '<b>QUERY :</b><br/> '; $info .= $query; $info .= '<br/>'; $info .= '<b>FILE :</b><br/> '; $info .= ($file == '' ? ' unknown ' : $file); $info .= '<br/>'; $info .= '<b>LINE :</b><br/> '; $info .= ($line == 0 ? ' unknown ' : $line); $info .= '</pre>'; //@ mysql_close(); //die($info); echo $info; } return $result;}/** * Store the result of a query into an array * * @author Olivier Brouckaert * @param resource $result - the return value of the query * @return array - the value returned by the query */function api_store_result($result){ $tab = array (); while ($row = mysql_fetch_array($result)) { $tab[] = $row; } return $tab;}/*============================================================================== SESSION MANAGEMENT==============================================================================*//** * Start the Dokeos session. * * The default lifetime for session is set here. It is not possible to have it * as a database setting as it is used before the database connection has been made. * It is taken from the configuration file, and if it doesn't exist there, it is set * to 360000 seconds * * @author Olivier Brouckaert * @param string variable - the variable name to save into the session */function api_session_start($already_installed = true){ global $storeSessionInDb; global $_configuration; /* causes too many problems and is not configurable dynamically * if($already_installed){ $session_lifetime = 360000; if(isset($_configuration['session_lifetime'])) { $session_lifetime = $_configuration['session_lifetime']; } session_set_cookie_params($session_lifetime,api_get_path(REL_PATH)); }*/ if (is_null($storeSessionInDb)) { $storeSessionInDb = false; } if ($storeSessionInDb && function_exists('session_set_save_handler')) { include_once (api_get_path(LIBRARY_PATH).'session_handler.class.php'); $session_handler = new session_handler(); @ session_set_save_handler(array (& $session_handler, 'open'), array (& $session_handler, 'close'), array (& $session_handler, 'read'), array (& $session_handler, 'write'), array (& $session_handler, 'destroy'), array (& $session_handler, 'garbage')); } session_name('dk_sid'); session_start(); if ($already_installed) { if (empty ($_SESSION['checkDokeosURL'])) { $_SESSION['checkDokeosURL'] = api_get_path(WEB_PATH); } elseif ($_SESSION['checkDokeosURL'] != api_get_path(WEB_PATH)) { api_session_clear(); } }}/** * save a variable into the session * * BUG: function works only with global variables * * @author Olivier Brouckaert * @param string variable - the variable name to save into the session */function api_session_register($variable){ global $$variable; session_register($variable); $_SESSION[$variable] = $$variable;}/** * Remove a variable from the session. * * @author Olivier Brouckaert * @param string variable - the variable name to remove from the session */function api_session_unregister($variable){ if(isset($GLOBALS[$variable])) { unset ($GLOBALS[$variable]); } if(isset($_SESSION[$variable])) { $_SESSION[$variable] = null; session_unregister($variable); } }/** * Clear the session * * @author Olivier Brouckaert */function api_session_clear(){ session_regenerate_id(); session_unset(); $_SESSION = array ();}/** * Destroy the session * * @author Olivier Brouckaert */function api_session_destroy(){ session_unset(); $_SESSION = array (); session_destroy();}/*============================================================================== STRING MANAGEMENT==============================================================================*/function api_add_url_param($url, $param){ if (empty ($param)) { return $url; } if (strstr($url, '?')) { if ($param[0] != '&') { $param = '&'.$param; } list (, $query_string) = explode('?', $url); $param_list1 = explode('&', $param); $param_list2 = explode('&', $query_string); $param_list1_keys = $param_list1_vals = array (); foreach ($param_list1 as $key => $enreg) { list ($param_list1_keys[$key], $param_list1_vals[$key]) = explode('=', $enreg); } $param_list1 = array ('keys' => $param_list1_keys, 'vals' => $param_list1_vals); foreach ($param_list2 as $enreg) { $enreg = explode('=', $enreg); $key = array_search($enreg[0], $param_list1['keys']); if (!is_null($key) && !is_bool($key)) { $url = str_replace($enreg[0].'='.$enreg[1], $enreg[0].'='.$param_list1['vals'][$key], $url); $param = str_replace('&'.$enreg[0].'='.$param_list1['vals'][$key], '', $param); } } $url .= $param; } else { $url = $url.'?'.$param; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -