PageRenderTime 45ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/class_qi.php

http://github.com/phpbb/quickinstall
PHP | 406 lines | 333 code | 33 blank | 40 comment | 26 complexity | e99065f2a77cec96daef52664bed9028 MD5 | raw file
  1. <?php
  2. /**
  3. *
  4. * @package quickinstall
  5. * @copyright (c) 2007 phpBB Limited
  6. * @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
  7. *
  8. */
  9. /**
  10. * @ignore
  11. */
  12. if (!defined('IN_QUICKINSTALL'))
  13. {
  14. exit;
  15. }
  16. /**
  17. * Class with functions usefull for qi. Some stuff is from the install functions, this class is to be used statically.
  18. */
  19. class qi
  20. {
  21. /**
  22. * Output the standard page header
  23. */
  24. public static function page_header($page_title = '')
  25. {
  26. if (defined('HEADER_INC'))
  27. {
  28. return;
  29. }
  30. define('HEADER_INC', true);
  31. global $template, $user, $phpbb_root_path, $quickinstall_path, $settings, $page, $mode;
  32. $update = self::get_update();
  33. $template->assign_vars(array(
  34. 'PAGE_TITLE' => $page_title,
  35. 'T_THEME_PATH' => 'style',
  36. 'U_DOCS' => self::url('docs'),
  37. 'U_MANAGE' => self::url('manage'),
  38. 'U_MAIN' => self::url('main'),
  39. 'U_PHPINFO' => self::url('phpinfo'),
  40. 'U_SETTINGS' => self::url('settings'),
  41. 'S_CONTENT_DIRECTION' => $user->lang['DIRECTION'],
  42. 'S_CONTENT_ENCODING' => 'UTF-8',
  43. 'S_USER_LANG' => $user->lang['USER_LANG'],
  44. 'S_SHOW_CONFIRM' => $settings->get_config('show_confirm', 1),
  45. 'TRANSLATION_INFO' => $user->lang['TRANSLATION_INFO'],
  46. 'QI_VERSION' => QI_VERSION,
  47. 'VERSION_CHECK_TITLE' => !empty($update) ? sprintf($user->lang['VERSION_CHECK_TITLE'], $update['current'], QI_VERSION) : '',
  48. 'U_VERSION_CHECK_URL' => !empty($update) ? $update['download'] : '',
  49. ));
  50. header('Content-type: text/html; charset=UTF-8');
  51. header('Cache-Control: private, no-cache="set-cookie"');
  52. header('Expires: 0');
  53. header('Pragma: no-cache');
  54. return;
  55. }
  56. /**
  57. * Output the standard page footer
  58. */
  59. public static function page_footer()
  60. {
  61. global $db, $template;
  62. $template->display('body');
  63. // Close our DB connection.
  64. if (!empty($db) && is_object($db))
  65. {
  66. $db->sql_close();
  67. }
  68. exit;
  69. }
  70. /**
  71. * Generate an HTTP/1.1 header to redirect the user to another page
  72. * This is used during the installation when we do not have a database available to call the normal redirect function
  73. * @param string $page The page to redirect to relative to the qi root path
  74. */
  75. public static function redirect($page)
  76. {
  77. if (strpos($page, 'http://') == 0 || strpos($page, 'https://') == 0)
  78. {
  79. // Assume we have a fully qualified URL. And we are done.
  80. header('Location: ' . $page);
  81. exit;
  82. }
  83. $server_name = (!empty($_SERVER['SERVER_NAME'])) ? $_SERVER['SERVER_NAME'] : getenv('SERVER_NAME');
  84. $server_port = (!empty($_SERVER['SERVER_PORT'])) ? (int) $_SERVER['SERVER_PORT'] : (int) getenv('SERVER_PORT');
  85. $secure = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ? 1 : 0;
  86. $script_name = (!empty($_SERVER['PHP_SELF'])) ? $_SERVER['PHP_SELF'] : getenv('PHP_SELF');
  87. if (!$script_name)
  88. {
  89. $script_name = (!empty($_SERVER['REQUEST_URI'])) ? $_SERVER['REQUEST_URI'] : getenv('REQUEST_URI');
  90. }
  91. // Replace backslashes and doubled slashes (could happen on some proxy setups)
  92. $script_name = str_replace(array('\\', '//'), '/', $script_name);
  93. $script_path = trim(dirname($script_name));
  94. $url = (($secure) ? 'https://' : 'http://') . $server_name;
  95. if ($server_port && (($secure && $server_port <> 443) || (!$secure && $server_port <> 80)))
  96. {
  97. $url .= ':' . $server_port;
  98. }
  99. // Make sure script path ends with a slash.
  100. $script_path .= (substr($script_path, -1) != '/') ? '/' : '';
  101. // Since $script_path ends with a slash we don't want $page to start with one.
  102. $page = ($page[0] == '/') ? substr($page, 1) : $page;
  103. $url .= $script_path . $page;
  104. header('Location: ' . $url);
  105. exit;
  106. }
  107. /**
  108. * Add Language Items
  109. *
  110. * @param mixed $lang_set specifies the language entries to include
  111. */
  112. public static function add_lang($lang_set, $lang_path = false)
  113. {
  114. global $user;
  115. $user->lang = (!empty($user->lang)) ? $user->lang : 'en';
  116. if (is_array($lang_set))
  117. {
  118. foreach ($lang_set as $key => $lang_file)
  119. {
  120. // Please do not delete this line.
  121. // We have to force the type here, else [array] language inclusion will not work
  122. $key = (string) $key;
  123. if (!is_array($lang_file))
  124. {
  125. self::set_lang($user->lang, $lang_file, $lang_path);
  126. }
  127. else
  128. {
  129. self::add_lang($lang_file, $lang_path);
  130. }
  131. }
  132. unset($lang_set);
  133. }
  134. else if ($lang_set)
  135. {
  136. self::set_lang($user->lang, $lang_set, $lang_path);
  137. }
  138. }
  139. /**
  140. * Set language entry (called by add_lang)
  141. * @access private
  142. */
  143. protected static function set_lang(&$lang, $lang_file, $lang_path = false)
  144. {
  145. global $phpEx, $settings, $quickinstall_path;
  146. if (empty($lang_path))
  147. {
  148. $lang = $settings->get_config('qi_lang', 'en');
  149. $lang_path = "{$quickinstall_path}language/$lang/";
  150. }
  151. if (!file_exists($lang_path) || !is_dir($lang_path))
  152. {
  153. trigger_error("Could not find language $lang_path", E_USER_ERROR);
  154. }
  155. $language_filename = "{$lang_path}$lang_file.$phpEx";
  156. if ((@include($language_filename)) === false)
  157. {
  158. trigger_error("Language file $language_filename couldn't be opened.", E_USER_ERROR);
  159. }
  160. }
  161. /**
  162. * Format user date
  163. */
  164. public static function format_date($gmepoch, $format = false, $forcedate = false)
  165. {
  166. global $user, $settings;
  167. static $midnight;
  168. $tz = new DateTimeZone($settings->get_config('qi_tz', ''));
  169. $tz_ary = $tz->getTransitions(time());
  170. $offset = (float) $tz_ary[0]['offset'];
  171. unset($tz_ary, $tz);
  172. $lang_dates = $user->lang['datetime'];
  173. $format = (!$format) ? $user->lang['default_dateformat'] : $format;
  174. // Short representation of month in format
  175. if ((strpos($format, '\M') === false && strpos($format, 'M') !== false) || (strpos($format, '\r') === false && strpos($format, 'r') !== false))
  176. {
  177. $lang_dates['May'] = $lang_dates['May_short'];
  178. }
  179. unset($lang_dates['May_short']);
  180. if (!$midnight)
  181. {
  182. list($d, $m, $y) = explode(' ', gmdate('j n Y', time() + $offset));
  183. $midnight = gmmktime(0, 0, 0, $m, $d, $y) - $offset;
  184. }
  185. if (strpos($format, '|') === false || ($gmepoch < $midnight - 86400 && !$forcedate) || ($gmepoch > $midnight + 172800 && !$forcedate))
  186. {
  187. return strtr(@gmdate(str_replace('|', '', $format), $gmepoch + $offset), $lang_dates);
  188. }
  189. if ($gmepoch > $midnight + 86400 && !$forcedate)
  190. {
  191. $format = substr($format, 0, strpos($format, '|')) . '||' . substr(strrchr($format, '|'), 1);
  192. return str_replace('||', $user->lang['datetime']['TOMORROW'], strtr(@gmdate($format, $gmepoch + $offset), $lang_dates));
  193. }
  194. else if ($gmepoch > $midnight && !$forcedate)
  195. {
  196. $format = substr($format, 0, strpos($format, '|')) . '||' . substr(strrchr($format, '|'), 1);
  197. return str_replace('||', $user->lang['datetime']['TODAY'], strtr(@gmdate($format, $gmepoch + $offset), $lang_dates));
  198. }
  199. else if ($gmepoch > $midnight - 86400 && !$forcedate)
  200. {
  201. $format = substr($format, 0, strpos($format, '|')) . '||' . substr(strrchr($format, '|'), 1);
  202. return str_replace('||', $user->lang['datetime']['YESTERDAY'], strtr(@gmdate($format, $gmepoch + $offset), $lang_dates));
  203. }
  204. return strtr(@gmdate(str_replace('|', '', $format), $gmepoch + $offset), $lang_dates);
  205. }
  206. public static function url($page, $params = array())
  207. {
  208. global $quickinstall_path, $phpEx;
  209. if (!empty($params))
  210. {
  211. array_walk($params, function (&$value, $name) {
  212. $value = urlencode($name) . '=' . urlencode($value);
  213. });
  214. }
  215. return $quickinstall_path . 'index.' . $phpEx . '?page=' . $page . (!empty($params) ? ('&amp;' . implode('&amp;', $params)) : '');
  216. }
  217. /**
  218. * Error and message handler, call with trigger_error if reqd.
  219. * Mostly borrowed from phpBB includes/functions.php.
  220. */
  221. public static function msg_handler($errno, $msg_text, $errfile, $errline)
  222. {
  223. global $phpEx, $phpbb_root_path, $msg_title, $msg_long_text, $quickinstall_path;
  224. global $user;
  225. // Do not display notices if we suppress them via @
  226. if (error_reporting() == 0 && $errno != E_USER_ERROR && $errno != E_USER_WARNING && $errno != E_USER_NOTICE)
  227. {
  228. return;
  229. }
  230. // Message handler is stripping text. In case we need it, we are possible to define long text...
  231. if (isset($msg_long_text) && $msg_long_text && !$msg_text)
  232. {
  233. $msg_text = $msg_long_text;
  234. }
  235. if (!defined('E_DEPRECATED'))
  236. {
  237. define('E_DEPRECATED', 8192);
  238. }
  239. switch ($errno)
  240. {
  241. case E_NOTICE:
  242. case E_WARNING:
  243. // Check the error reporting level and return if the error level does not match
  244. // If DEBUG is defined the default level is E_ALL
  245. if (($errno & ((defined('DEBUG')) ? E_ALL : error_reporting())) == 0)
  246. {
  247. return;
  248. }
  249. // remove complete path to installation, with the risk of changing backslashes meant to be there
  250. $errfile = str_replace(array(phpbb_functions::phpbb_realpath($phpbb_root_path), '\\'), array('', '/'), $errfile);
  251. $msg_text = str_replace(array(phpbb_functions::phpbb_realpath($phpbb_root_path), '\\'), array('', '/'), $msg_text);
  252. $error_name = ($errno === E_WARNING) ? 'PHP Warning' : 'PHP Notice';
  253. echo '<b>[QI Debug] ' . $error_name . '</b>: in file <b>' . $errfile . '</b> on line <b>' . $errline . '</b>: <b>' . $msg_text . '</b><br />' . "\n";
  254. return;
  255. break;
  256. case E_USER_ERROR:
  257. case E_USER_WARNING:
  258. case E_USER_NOTICE:
  259. if (!empty($user) && !empty($user->lang))
  260. {
  261. $msg_text = (!empty($user->lang[$msg_text])) ? $user->lang[$msg_text] : $msg_text;
  262. $msg_title = (!isset($msg_title)) ? $user->lang['GENERAL_ERROR'] : ((!empty($user->lang[$msg_title])) ? $user->lang[$msg_title] : $msg_title);
  263. $l_return_index = sprintf($user->lang['GO_QI_MAIN'], '<a href="' . qi::url('main') . '">', '</a> &bull; ');
  264. $l_return_index .= sprintf($user->lang['GO_QI_SETTINGS'], '<a href="' . qi::url('settings') . '">', '</a>');
  265. }
  266. else
  267. {
  268. $msg_title = 'General Error';
  269. $l_return_index = '<a href="' . qi::url('main') . '">Go to QuickInstall main page</a> &bull; ';
  270. $l_return_index .= '<a href="' . qi::url('settings') . '">Go to settings</a> &bull; ';
  271. }
  272. $backtrace = phpbb_functions::get_backtrace();
  273. if ($backtrace)
  274. {
  275. $msg_text .= '<br /><br />BACKTRACE<br />' . $backtrace;
  276. }
  277. phpbb_functions::send_status_line(503, 'Service Unavailable');
  278. $error_out = file_get_contents($quickinstall_path . 'style/error.html');
  279. $error_out = str_replace(
  280. array('{L_QUICKINSTALL}', '{L_PHPBB_QI_TEXT}', '{QI_PATH}', '{MSG_TITLE}', '{MSG_EXPLAIN}', '{MSG_TEXT}', '{SETTINGS_FORM}', '{RETURN_LINKS}', '{QI_VERSION}', '{L_FOR_PHPBB_VERSIONS}', '{L_POWERED_BY_PHPBB}'),
  281. array($user->lang['QUICKINSTALL'], $user->lang['PHPBB_QI_TEXT'], $quickinstall_path, $msg_title, '', $msg_text, '', $l_return_index, QI_VERSION, $user->lang['FOR_PHPBB_VERSIONS'], $user->lang['POWERED_BY_PHPBB']),
  282. $error_out
  283. );
  284. if (self::is_ajax())
  285. {
  286. echo json_encode(array('errorOut' => $error_out));
  287. }
  288. else
  289. {
  290. echo $error_out;
  291. }
  292. // As a pre-caution... some setups display a blank page if the flush() is not there.
  293. (ob_get_level() > 0) ? @ob_flush() : @flush();
  294. // On a fatal error (and E_USER_ERROR *is* fatal) we never want other scripts to continue and force an exit here.
  295. exit;
  296. break;
  297. }
  298. // If we notice an error not handled here we pass this back to PHP by returning false
  299. // This may not work for all php versions
  300. return(false);
  301. }
  302. /**
  303. * Is an AJAX request active
  304. *
  305. * @return bool
  306. */
  307. public static function is_ajax()
  308. {
  309. return !empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest';
  310. }
  311. /**
  312. * Send an ajax response and exit
  313. *
  314. * @param array $response
  315. */
  316. public static function ajax_response($response)
  317. {
  318. echo json_encode($response);
  319. exit;
  320. }
  321. /**
  322. * Get version update data
  323. *
  324. * @return array
  325. */
  326. public static function get_update()
  327. {
  328. global $quickinstall_path, $phpEx;
  329. if (!class_exists('qi_version_helper'))
  330. {
  331. include "{$quickinstall_path}includes/qi_version_helper.$phpEx";
  332. }
  333. $version_helper = new qi_version_helper();
  334. return $version_helper
  335. ->set_current_version(QI_VERSION)
  336. ->force_stability('stable')
  337. ->set_file_location('www.phpbb.com', '/customise/db/official_tool/phpbb3_quickinstall', 'version_check')
  338. ->get_update();
  339. }
  340. }