PageRenderTime 46ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/include/vitals.inc.php

https://github.com/atutor/AChecker
PHP | 319 lines | 182 code | 54 blank | 83 comment | 47 complexity | ef90aa4ea58c76d2bac3359fff131296 MD5 | raw file
  1. <?php
  2. /************************************************************************/
  3. /* AChecker */
  4. /************************************************************************/
  5. /* Copyright (c) 2008 - 2011 */
  6. /* Inclusive Design Institute */
  7. /* */
  8. /* This program is free software. You can redistribute it and/or */
  9. /* modify it under the terms of the GNU General Public License */
  10. /* as published by the Free Software Foundation. */
  11. /************************************************************************/
  12. // $Id$
  13. if (!defined('AC_INCLUDE_PATH')) { exit; }
  14. define('AC_DEVEL', 1);
  15. define('AC_ERROR_REPORTING', E_ALL ^ E_NOTICE); // default is E_ALL ^ E_NOTICE, use E_ALL or E_ALL + E_STRICT for developing
  16. // Emulate register_globals off. src: http://php.net/manual/en/faq.misc.php#faq.misc.registerglobals
  17. function unregister_GLOBALS() {
  18. if (!ini_get('register_globals')) { return; }
  19. // Might want to change this perhaps to a nicer error
  20. if (isset($_REQUEST['GLOBALS'])) { die('GLOBALS overwrite attempt detected'); }
  21. // Variables that shouldn't be unset
  22. $noUnset = array('GLOBALS','_GET','_POST','_COOKIE','_REQUEST','_SERVER','_ENV', '_FILES');
  23. $input = array_merge($_GET,$_POST,$_COOKIE,$_SERVER,$_ENV,$_FILES,isset($_SESSION) && is_array($_SESSION) ? $_SESSION : array());
  24. foreach ($input as $k => $v) {
  25. if (!in_array($k, $noUnset) && isset($GLOBALS[$k])) { unset($GLOBALS[$k]); }
  26. }
  27. }
  28. /*
  29. * structure of this document (in order):
  30. *
  31. * 0. load config.inc.php
  32. * 1. initilize db connection
  33. * 2. load constants
  34. * 3. initilize session
  35. * 4. load $_config from table 'config'
  36. * 5. start language block
  37. * 6. load common libraries
  38. * 7. initialize theme and template management
  39. * 8. initialize a user instance without user id.
  40. * if $_SESSION['user_id'] is set, it's assigned to instance in include/header.inc.php
  41. * 9. register pages based on current user's priviledge
  42. ***/
  43. /**** 0. start system configuration options block ****/
  44. error_reporting(0);
  45. include_once(AC_INCLUDE_PATH.'config.inc.php');
  46. error_reporting(AC_ERROR_REPORTING);
  47. if (!defined('AC_INSTALL') || !AC_INSTALL) {
  48. header('Cache-Control: no-store, no-cache, must-revalidate');
  49. header('Pragma: no-cache');
  50. $relative_path = substr(AC_INCLUDE_PATH, 0, -strlen('include/'));
  51. header('Location: ' . $relative_path . 'install/not_installed.php');
  52. exit;
  53. }
  54. /*** end system config block ****/
  55. /***** 1. database connection *****/
  56. //if (!defined('AC_REDIRECT_LOADED')){
  57. // require_once(AC_INCLUDE_PATH.'lib/mysql_connect.inc.php');
  58. //}
  59. /***** end database connection ****/
  60. /*** 2. constants ***/
  61. require_once(AC_INCLUDE_PATH.'constants.inc.php');
  62. /*** 3. initilize session ***/
  63. @set_time_limit(0);
  64. @ini_set('session.gc_maxlifetime', '36000'); /* 10 hours */
  65. @session_cache_limiter('private, must-revalidate');
  66. session_name('CheckerID');
  67. error_reporting(AC_ERROR_REPORTING);
  68. ob_start();
  69. session_set_cookie_params(0, $_base_path);
  70. session_start();
  71. $str = ob_get_contents();
  72. ob_end_clean();
  73. unregister_GLOBALS();
  74. /***** end session initilization block ****/
  75. function my_add_null_slashes( $string ) {
  76. return mysql_real_escape_string(stripslashes($string));
  77. }
  78. function my_null_slashes($string) {
  79. return $string;
  80. }
  81. if ( get_magic_quotes_gpc() == 1 ) {
  82. $addslashes = 'my_add_null_slashes';
  83. $stripslashes = 'stripslashes';
  84. } else {
  85. $addslashes = 'mysql_real_escape_string';
  86. $stripslashes = 'my_null_slashes';
  87. }
  88. require(AC_INCLUDE_PATH.'phpCache/phpCache.inc.php'); // cache library
  89. require(AC_INCLUDE_PATH.'classes/DAO/ThemesDAO.class.php');
  90. require(AC_INCLUDE_PATH.'classes/DAO/ConfigDAO.class.php');
  91. /***** 4. load $_config from table 'config' *****/
  92. $configDAO = new ConfigDAO();
  93. $rows = $configDAO->getAll();
  94. foreach ($rows as $id => $row)
  95. {
  96. $_config[$row['name']] = $row['value'];
  97. }
  98. // define as constants. more constants are defined in include/constants.inc.php
  99. define('EMAIL', $_config['contact_email']);
  100. define('SITE_NAME', $_config['site_name']);
  101. /***** end loading $_config *****/
  102. /***** 5. start language block *****/
  103. // set current language
  104. require(AC_INCLUDE_PATH . 'classes/Language/LanguageManager.class.php');
  105. $languageManager = new LanguageManager();
  106. $myLang = $languageManager->getMyLanguage();
  107. if ($myLang === FALSE) {
  108. echo 'There are no languages installed!';
  109. exit;
  110. }
  111. $myLang->saveToSession();
  112. /* set right-to-left language */
  113. $rtl = '';
  114. if ($myLang->isRTL()) {
  115. $rtl = 'rtl_'; /* basically the prefix to a rtl variant directory/filename. eg. rtl_tree */
  116. }
  117. /***** end language block ****/
  118. /***** 6. load common libraries *****/
  119. require(AC_INCLUDE_PATH.'lib/output.inc.php'); /* output functions */
  120. /***** end load common libraries ****/
  121. /***** 7. initialize theme and template management *****/
  122. require(AC_INCLUDE_PATH.'classes/Savant2/Savant2.php');
  123. // set default template paths:
  124. $savant = new Savant2();
  125. if (isset($_SESSION['prefs']['PREF_THEME']) && file_exists(AC_INCLUDE_PATH . '../themes/' . $_SESSION['prefs']['PREF_THEME']) && isset($_SESSION['valid_user']) && $_SESSION['valid_user'])
  126. {
  127. if (!is_dir(AC_INCLUDE_PATH . '../themes/' . $_SESSION['prefs']['PREF_THEME']))
  128. {
  129. $_SESSION['prefs']['PREF_THEME'] = 'default';
  130. }
  131. else
  132. {
  133. //check if enabled
  134. $themesDAO = new ThemesDAO();
  135. $row = $themesDAO->getByID($_SESSION['prefs']['PREF_THEME']);
  136. if ($row['status'] == 0)
  137. {
  138. // get default
  139. $_SESSION['prefs']['PREF_THEME'] = get_default_theme();
  140. }
  141. }
  142. } else
  143. {
  144. $_SESSION['prefs']['PREF_THEME'] = get_default_theme();
  145. }
  146. $savant->addPath('template', AC_INCLUDE_PATH . '../themes/' . $_SESSION['prefs']['PREF_THEME'] . '/');
  147. require(AC_INCLUDE_PATH . '../themes/' . $_SESSION['prefs']['PREF_THEME'] . '/theme.cfg.php');
  148. require(AC_INCLUDE_PATH.'classes/Message/Message.class.php');
  149. $msg = new Message($savant);
  150. /***** end of initialize theme and template management *****/
  151. /***** 8. initialize user instance *****/
  152. // used as global var
  153. if (isset($_SESSION['user_id']) && $_SESSION['user_id'] > 0)
  154. {
  155. // check if $_SESSION['user_id'] is valid
  156. include_once(AC_INCLUDE_PATH.'classes/DAO/UsersDAO.class.php');
  157. $usersDAO = new UsersDAO();
  158. $user = $usersDAO->getUserByID($_SESSION['user_id']);
  159. if (!$user) // invalid user
  160. unset($_SESSION['user_id']);
  161. else
  162. {
  163. include_once(AC_INCLUDE_PATH.'classes/User.class.php');
  164. $_current_user = new User($_SESSION['user_id']);
  165. }
  166. }
  167. /***** end of initialize user instance *****/
  168. /*** 9. register pages based on user's priviledge ***/
  169. require_once(AC_INCLUDE_PATH.'page_constants.inc.php');
  170. // used in AC_print @ include/lib/output.inc.php
  171. function query_bit( $bitfield, $bit ) {
  172. if (!is_int($bitfield)) {
  173. $bitfield = intval($bitfield);
  174. }
  175. if (!is_int($bit)) {
  176. $bit = intval($bit);
  177. }
  178. return ( $bitfield & $bit ) ? true : false;
  179. }
  180. /**
  181. * This function is used for printing variables for debugging.
  182. * @access public
  183. * @param mixed $var The variable to output
  184. * @param string $title The name of the variable, or some mark-up identifier.
  185. * @author Joel Kronenberg
  186. */
  187. function debug($var, $title='') {
  188. if (!defined('AC_DEVEL') || !AC_DEVEL) {
  189. return;
  190. }
  191. echo '<pre style="border: 1px black solid; padding: 0px; margin: 10px;" title="debugging box">';
  192. if ($title) {
  193. echo '<h4>'.$title.'</h4>';
  194. }
  195. ob_start();
  196. print_r($var);
  197. $str = ob_get_contents();
  198. ob_end_clean();
  199. $str = str_replace('<', '&lt;', $str);
  200. $str = str_replace('[', '<span style="color: red; font-weight: bold;">[', $str);
  201. $str = str_replace(']', ']</span>', $str);
  202. $str = str_replace('=>', '<span style="color: blue; font-weight: bold;">=></span>', $str);
  203. $str = str_replace('Array', '<span style="color: purple; font-weight: bold;">Array</span>', $str);
  204. echo $str;
  205. echo '</pre>';
  206. }
  207. /**
  208. * This function is used for printing variables into a log file for debugging.
  209. * if the the log path/name is not provided, use default log @ temp/achecker.log
  210. * @access public
  211. * @param mixed $var The variable to output
  212. * @param string $log The location of the log file. If not provided, use the default one.
  213. * @author Cindy Qi Li
  214. */
  215. function debug_to_log($var, $log='') {
  216. if (!defined('AC_DEVEL') || !AC_DEVEL) {
  217. return;
  218. }
  219. if ($log == '') $log = AC_TEMP_DIR. 'achecker.log';
  220. $handle = fopen($log, 'a');
  221. fwrite($handle, "\n\n");
  222. fwrite($handle, date("F j, Y, g:i a"));
  223. fwrite($handle, "\n");
  224. fwrite($handle, var_export($var,1));
  225. fclose($handle);
  226. }
  227. /****************************************************/
  228. /* compute the $_my_uri variable */
  229. $bits = explode(SEP, getenv('QUERY_STRING'));
  230. $num_bits = count($bits);
  231. $_my_uri = '';
  232. for ($i=0; $i<$num_bits; $i++) {
  233. // if ( (strpos($bits[$i], 'enable=') === 0)
  234. // || (strpos($bits[$i], 'disable=') === 0)
  235. // || (strpos($bits[$i], 'expand=') === 0)
  236. // || (strpos($bits[$i], 'collapse=') === 0)
  237. // || (strpos($bits[$i], 'lang=') === 0)
  238. // ) {
  239. if ( (strpos($bits[$i], 'lang=') === 0)
  240. ) {
  241. /* we don't want this variable added to $_my_uri */
  242. continue;
  243. }
  244. if (($_my_uri == '') && ($bits[$i] != '')) {
  245. $_my_uri .= '?';
  246. } else if ($bits[$i] != ''){
  247. $_my_uri .= SEP;
  248. }
  249. $_my_uri .= $bits[$i];
  250. }
  251. if ($_my_uri == '') {
  252. $_my_uri .= '?';
  253. } else {
  254. $_my_uri .= SEP;
  255. }
  256. $_my_uri = $_SERVER['PHP_SELF'].$_my_uri;
  257. function get_default_theme() {
  258. $themesDAO = new ThemesDAO();
  259. $rows = $themesDAO->getDefaultTheme();
  260. if (!is_dir(AC_INCLUDE_PATH . '../themes/' . $rows[0]['dir_name']))
  261. return 'default';
  262. else
  263. return $rows[0]['dir_name'];
  264. }
  265. ?>