PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/pre-check.php

https://bitbucket.org/blackriver/openx
PHP | 394 lines | 270 code | 20 blank | 104 comment | 40 complexity | 5c1cf1e80e72186370bfe4d5863d641d MD5 | raw file
  1. <?php
  2. /*
  3. +---------------------------------------------------------------------------+
  4. | OpenX v2.8 |
  5. | ========== |
  6. | |
  7. | Copyright (c) 2003-2009 OpenX Limited |
  8. | For contact details, see: http://www.openx.org/ |
  9. | |
  10. | This program is free software; you can redistribute it and/or modify |
  11. | it under the terms of the GNU General Public License as published by |
  12. | the Free Software Foundation; either version 2 of the License, or |
  13. | (at your option) any later version. |
  14. | |
  15. | This program is distributed in the hope that it will be useful, |
  16. | but WITHOUT ANY WARRANTY; without even the implied warranty of |
  17. | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
  18. | GNU General Public License for more details. |
  19. | |
  20. | You should have received a copy of the GNU General Public License |
  21. | along with this program; if not, write to the Free Software |
  22. | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
  23. +---------------------------------------------------------------------------+
  24. $Id: pre-check.php 81772 2012-09-11 00:07:29Z chris.nutting $
  25. */
  26. /**
  27. * @package OpenX
  28. * @author Ɓukasz Wikierski <lukasz.wikierski@openx.org>
  29. *
  30. * A pre-initialisation file to check if system settings allow OpenX
  31. * to be run or not.
  32. */
  33. require_once 'memory.php';
  34. /**
  35. * A function to check system settings and display detected problems
  36. */
  37. function OX_initialSystemCheck(){
  38. $errorUrl = 'http://www.openx.org/help/2.8/pre-init-error/';
  39. $installed = OX_checkSystemInstalled();
  40. $aErrors = array();
  41. $erorCode = OX_checkSystemInitialRequirements($aErrors);
  42. if ($erorCode !== true) {
  43. $imageRelativePath = "./www/admin/precheck/";
  44. // Do functions strpos & parse_url exist? If so, try to
  45. // guess the proper relative path...
  46. if ($erorCode != -2) {
  47. // Checking if URL include www or admin in path
  48. if (strpos($_SERVER['REQUEST_URI'], '/www/admin/') !== false) {
  49. $imageRelativePath = "./precheck/";
  50. } else if (strpos($_SERVER['REQUEST_URI'], '/www/') !== false) {
  51. $imageRelativePath = "./admin/precheck/";
  52. }
  53. }
  54. // We always trying show images in CSS
  55. $bodyBackground = "url('{$imageRelativePath}body_piksel.gif') repeat-x";
  56. $liBackground = "background: url('{$imageRelativePath}list_element.gif') no-repeat;";
  57. $openXLogo = "background: url('{$imageRelativePath}openx_logo.gif') no-repeat;";
  58. $message = '
  59. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  60. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  61. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  62. <head>
  63. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  64. <meta name="Keywords" content="" />
  65. <meta name="Description" content="" />
  66. <title>php Error page</title>
  67. <style type="text/css">
  68. body {
  69. margin: 0;
  70. background: #fff '. $bodyBackground .';
  71. font-family: Arial, Helvetica, sans-serif;
  72. font: 12px Arial;
  73. color: #747474;
  74. }
  75. h1 {
  76. width:80%;
  77. font: 26px Arial;
  78. color:#000;
  79. }
  80. h2 {
  81. width:80%;
  82. font:12px Arial;
  83. color:#747474;
  84. margin-top:20px;
  85. }
  86. .error_container {
  87. margin-top: 80px;
  88. margin-left: 93px;
  89. }
  90. .error_list {
  91. width: 80%;
  92. padding-left: 33px;
  93. padding-top: 15px;
  94. padding-bottom:15px;
  95. line-height:18px;
  96. border-top: 1px solid #eee;
  97. border-bottom: 1px solid #eee;
  98. }
  99. ul {
  100. list-style-type: none;
  101. list-style-position: outside;
  102. padding:0px;
  103. padding-top:10px;
  104. margin:0px;
  105. margin-left:15px;
  106. }
  107. li {
  108. '.$liBackground.'
  109. padding-left:10px;
  110. }
  111. .help_link:active, .help_link:link, .help_link:visited {
  112. color: #0767a8;
  113. text-decoration:none;
  114. }
  115. .help_link:hover {
  116. color: #0767a8;
  117. text-decoration:underline;
  118. }
  119. .logo_image {
  120. '.$openXLogo.'
  121. width: 173px;
  122. height: 60px;
  123. }
  124. </style>
  125. </head>
  126. <body>
  127. <div class="logo_image">&nbsp;</div>
  128. <div class="error_container">
  129. ';
  130. if ($installed) {
  131. $message .= "
  132. <h1>Sorry, but OpenX cannot currently run on your machine</h1>
  133. ";
  134. } else {
  135. $message .= "
  136. <h1>Sorry, but the OpenX installer system cannot currently be started</h1>
  137. ";
  138. }
  139. $message .= '
  140. <div class="error_list">
  141. Detected problem';
  142. if (count($aErrors) > 1) {
  143. $message .= "s";
  144. }
  145. $message .= ":
  146. <ul>
  147. ";
  148. echo $message;
  149. foreach ($aErrors as $errorMessage) {
  150. echo "<li>{$errorMessage}</li>";
  151. }
  152. $message = '
  153. </ul>
  154. </div>
  155. <h2>Please see our <a href="'.$errorUrl.'" class="help_link" >documentation</a> for help
  156. with the above error';
  157. if (count($aErrors) > 1) {
  158. $message .= "s";
  159. }
  160. $message .= ".
  161. </h2>
  162. </div>
  163. </body>
  164. </html>
  165. ";
  166. echo $message;
  167. // Terminate execution
  168. exit;
  169. }
  170. }
  171. /**
  172. * Check (roughly) to see if the system is installed without
  173. * any of the usual code to check this, as running in pre-init
  174. * mode.
  175. *
  176. * @return bool True if the system seems to be installed, false
  177. * otherwise.
  178. */
  179. function OX_checkSystemInstalled()
  180. {
  181. $path = @dirname(__FILE__);
  182. if (!@empty($path)) {
  183. if (@file_exists($path . '/var/UPGRADE')) {
  184. return false;
  185. } else {
  186. return true;
  187. }
  188. }
  189. return false;
  190. }
  191. /**
  192. * Check for situation when installing or using software is eithe
  193. * imposible, or results in unformated error output, due to system
  194. * configuration.
  195. *
  196. * @param &$aErrors Array of error mesages. All errors that it is possible to
  197. * detect will be set, regardless of the function return value.
  198. * @return bool|int True on system check OK, negative int value on detected problems.
  199. * -1 => The "function_exists" built-in function doesn't exist
  200. * -2 => At least one of the "strpos" or "parse_url" built-in
  201. * functions don't exist
  202. * -3 => One of the other required built-in functions was
  203. * detected as being disabled
  204. * -4 => The amount of memory required was too low
  205. *
  206. */
  207. function OX_checkSystemInitialRequirements(&$aErrors){
  208. // Variables for tracking if the test has passed or not,
  209. // and if not, what value to return
  210. $isSystemOK = true;
  211. $return = true;
  212. // The general list of built in PHP functions that are required to
  213. // run OpenX, apart from the functions:
  214. //
  215. // - "function_exists"
  216. // - "array_intersect"
  217. // - "explode"
  218. // - "ini_get"
  219. // - "trim"
  220. // - "parse_url"
  221. // - "strpos"
  222. //
  223. // These other functions are tested separately, as they are
  224. // required to test for the existence of the functions in the
  225. // array below!
  226. $aRequiredFunctions = array(
  227. 'dirname',
  228. 'empty',
  229. 'file_exists',
  230. 'ini_set',
  231. 'parse_ini_file',
  232. 'version_compare'
  233. );
  234. // Prepare error strings, in the simplest possible way
  235. $errorString1 = 'The built in PHP function "';
  236. $errorString2 = '" is in the "disable_functions" list in your "php.ini" file.';
  237. // Need "function_exists" to be able to test for functions required
  238. // for testing what is in the "disabled_functions" list
  239. if (!function_exists('function_exists')) {
  240. $aErrors[] = $errorString1 . 'function_exists' . $errorString2;
  241. // Cannot detect any more errors, as function_exists is
  242. // needed to detect the required functions!
  243. return -1;
  244. }
  245. // Test for existence of "parse_url" and "strpos", which are
  246. // special cases required for the display of the error message
  247. // in the event of anything failing in this test!
  248. if (!function_exists('parse_url')) {
  249. $aErrors[] = $errorString1 . 'parse_url' . $errorString2;
  250. $isSystemOK = false;
  251. if ($return === true) {
  252. $return = -2;
  253. }
  254. }
  255. if (!function_exists('strpos')) {
  256. $aErrors[] = $errorString1 . 'strpos' . $errorString2;
  257. $isSystemOK = false;
  258. if ($return === true) {
  259. $return = -2;
  260. }
  261. }
  262. // Test for existence of "array_intersect", "explode", "ini_get"
  263. // and "trim", which are all required as part of the code to test
  264. // which functions are in the "disabled_functions" list below...
  265. if (!function_exists('array_intersect')) {
  266. $aErrors[] = $errorString1 . 'array_intersect' . $errorString2;
  267. $isSystemOK = false;
  268. if ($return === true) {
  269. $return = -3;
  270. }
  271. }
  272. if (!function_exists('explode')) {
  273. $aErrors[] = $errorString1 . 'explode' . $errorString2;
  274. $isSystemOK = false;
  275. if ($return === true) {
  276. $return = -3;
  277. }
  278. }
  279. if (!function_exists('ini_get')) {
  280. $aErrors[] = $errorString1 . 'ini_get' . $errorString2;
  281. $isSystemOK = false;
  282. if ($return === true) {
  283. $return = -3;
  284. }
  285. }
  286. if (!function_exists('trim')) {
  287. $aErrors[] = $errorString1 . 'trim' . $errorString2;
  288. $isSystemOK = false;
  289. if ($return === true) {
  290. $return = -3;
  291. }
  292. }
  293. // Test the disabled functons list with required functions list
  294. // defined above in $aRequiredFunctions
  295. $aDisabledFunctions = explode(',', ini_get('disable_functions'));
  296. foreach ($aDisabledFunctions as $key => $value) {
  297. $aDisabledFunctions[$key] = trim($value);
  298. }
  299. $aNeededFunctions = array_intersect($aDisabledFunctions, $aRequiredFunctions);
  300. if (count($aNeededFunctions) > 0) {
  301. $isSystemOK = false;
  302. foreach ($aNeededFunctions as $functionName) {
  303. $aErrors[] = $errorString1 . $functionName . $errorString2;
  304. }
  305. }
  306. // Check PHP version, as use of PHP < 5.1.4 will result in parse errors
  307. $errorMessage = "PHP version 5.1.4, or greater, was not detected.";
  308. if (function_exists('version_compare')) {
  309. $result = version_compare(phpversion(), '5.1.4', '<');
  310. if ($result) {
  311. $aErrors[] = $errorMessage;
  312. $isSystemOK = false;
  313. if ($return === true) {
  314. $return = -3;
  315. }
  316. }
  317. }
  318. // Check minimum memory requirements are okay (24MB)
  319. $minimumRequiredMemory = OX_getMinimumRequiredMemory();
  320. $phpMemoryLimit = OX_getMemoryLimitSizeInBytes();
  321. if ($phpMemoryLimit > 0 && $phpMemoryLimit < $minimumRequiredMemory) {
  322. // The memory limit is too low, but can it be increased?
  323. $memoryCanBeSet = OX_checkMemoryCanBeSet();
  324. if (!$memoryCanBeSet) {
  325. $minimumRequiredMemoryInMB = $minimumRequiredMemory / 1048576;
  326. $errorMessage = 'The PHP "memory_limit" value is set to less than the required minimum of ' .
  327. $minimumRequiredMemoryInMB . 'MB, but because the built in PHP function "ini_set" ' .
  328. 'has been disabled, the memory limit cannot be automatically increased.';
  329. $aErrors[] = $errorMessage;
  330. $isSystemOK = false;
  331. if ($return === true) {
  332. $return = -4;
  333. }
  334. }
  335. }
  336. // Check magic_quotes_runtime and try to unset it
  337. $GLOBALS['original_get_magic_quotes_runtime'] = OX_getMagicQuotesRuntime();
  338. if ($GLOBALS['original_get_magic_quotes_runtime']) {
  339. ini_set('magic_quotes_runtime', 0);
  340. if (OX_getMagicQuotesRuntime()) {
  341. // try deprecated set_magic_quotes_runtime
  342. if (function_exists('set_magic_quotes_runtime')) {
  343. @set_magic_quotes_runtime(0);
  344. }
  345. }
  346. // check magic_quotes_runtime again, stop if still is set
  347. if (OX_getMagicQuotesRuntime()) {
  348. $aErrors[] = 'The PHP magic_quotes_runtime option is ON, and cannot be automatically turned off.';
  349. $isSystemOK = false;
  350. if ($return === true) {
  351. $return = -5;
  352. }
  353. }
  354. }
  355. if (!$isSystemOK) {
  356. return $return;
  357. }
  358. return true;
  359. }
  360. /**
  361. * Get magic_quotes_runtime settings using get_magic_quotes_runtime or if function
  362. * doesn't exist try to read from php.ini
  363. *
  364. * @return int Returns 0 if magic quotes gpc are off, 1 otherwise
  365. */
  366. function OX_getMagicQuotesRuntime()
  367. {
  368. return (function_exists('get_magic_quotes_runtime'))
  369. ? get_magic_quotes_runtime() : ini_get('magic_quotes_runtime');
  370. }
  371. OX_initialSystemCheck();
  372. ?>