PageRenderTime 54ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/phpBB/includes/functions_install.php

https://github.com/naderman/phpbb-orchestra
PHP | 559 lines | 446 code | 52 blank | 61 comment | 53 complexity | 52605b2babe6b75d15a6676a74a236e4 MD5 | raw file
  1. <?php
  2. /**
  3. *
  4. * @package install
  5. * @version $Id$
  6. * @copyright (c) 2006 phpBB Group
  7. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  8. *
  9. */
  10. /**
  11. * @ignore
  12. */
  13. if (!defined('IN_PHPBB'))
  14. {
  15. exit;
  16. }
  17. /**
  18. * Determine if we are able to load a specified PHP module and do so if possible
  19. */
  20. function can_load_dll($dll)
  21. {
  22. // SQLite2 is a tricky thing, from 5.0.0 it requires PDO; if PDO is not loaded we must state that SQLite is unavailable
  23. // as the installer doesn't understand that the extension has a prerequisite.
  24. //
  25. // On top of this sometimes the SQLite extension is compiled for a different version of PDO
  26. // by some Linux distributions which causes phpBB to bomb out with a blank page.
  27. //
  28. // Net result we'll disable automatic inclusion of SQLite support
  29. //
  30. // See: r9618 and #56105
  31. if ($dll == 'sqlite')
  32. {
  33. return false;
  34. }
  35. return ((@ini_get('enable_dl') || strtolower(@ini_get('enable_dl')) == 'on') && (!@ini_get('safe_mode') || strtolower(@ini_get('safe_mode')) == 'off') && function_exists('dl') && @dl($dll . '.' . PHP_SHLIB_SUFFIX)) ? true : false;
  36. }
  37. /**
  38. * Returns an array of available DBMS with some data, if a DBMS is specified it will only
  39. * return data for that DBMS and will load its extension if necessary.
  40. */
  41. function get_available_dbms($dbms = false, $return_unavailable = false, $only_20x_options = false)
  42. {
  43. global $lang;
  44. $available_dbms = array(
  45. 'firebird' => array(
  46. 'LABEL' => 'FireBird',
  47. 'SCHEMA' => 'firebird',
  48. 'MODULE' => 'interbase',
  49. 'DELIM' => ';;',
  50. 'COMMENTS' => 'remove_remarks',
  51. 'DRIVER' => 'firebird',
  52. 'AVAILABLE' => true,
  53. '2.0.x' => false,
  54. ),
  55. 'mysqli' => array(
  56. 'LABEL' => 'MySQL with MySQLi Extension',
  57. 'SCHEMA' => 'mysql_41',
  58. 'MODULE' => 'mysqli',
  59. 'DELIM' => ';',
  60. 'COMMENTS' => 'remove_remarks',
  61. 'DRIVER' => 'mysqli',
  62. 'AVAILABLE' => true,
  63. '2.0.x' => true,
  64. ),
  65. 'mysql' => array(
  66. 'LABEL' => 'MySQL',
  67. 'SCHEMA' => 'mysql',
  68. 'MODULE' => 'mysql',
  69. 'DELIM' => ';',
  70. 'COMMENTS' => 'remove_remarks',
  71. 'DRIVER' => 'mysql',
  72. 'AVAILABLE' => true,
  73. '2.0.x' => true,
  74. ),
  75. 'mssql' => array(
  76. 'LABEL' => 'MS SQL Server 2000+',
  77. 'SCHEMA' => 'mssql',
  78. 'MODULE' => 'mssql',
  79. 'DELIM' => 'GO',
  80. 'COMMENTS' => 'remove_comments',
  81. 'DRIVER' => 'mssql',
  82. 'AVAILABLE' => true,
  83. '2.0.x' => true,
  84. ),
  85. 'mssql_odbc'=> array(
  86. 'LABEL' => 'MS SQL Server [ ODBC ]',
  87. 'SCHEMA' => 'mssql',
  88. 'MODULE' => 'odbc',
  89. 'DELIM' => 'GO',
  90. 'COMMENTS' => 'remove_comments',
  91. 'DRIVER' => 'mssql_odbc',
  92. 'AVAILABLE' => true,
  93. '2.0.x' => true,
  94. ),
  95. 'mssqlnative' => array(
  96. 'LABEL' => 'MS SQL Server 2005+ [ Native ]',
  97. 'SCHEMA' => 'mssql',
  98. 'MODULE' => 'sqlsrv',
  99. 'DELIM' => 'GO',
  100. 'COMMENTS' => 'remove_comments',
  101. 'DRIVER' => 'mssqlnative',
  102. 'AVAILABLE' => true,
  103. '2.0.x' => false,
  104. ),
  105. 'oracle' => array(
  106. 'LABEL' => 'Oracle',
  107. 'SCHEMA' => 'oracle',
  108. 'MODULE' => 'oci8',
  109. 'DELIM' => '/',
  110. 'COMMENTS' => 'remove_comments',
  111. 'DRIVER' => 'oracle',
  112. 'AVAILABLE' => true,
  113. '2.0.x' => false,
  114. ),
  115. 'postgres' => array(
  116. 'LABEL' => 'PostgreSQL 7.x/8.x',
  117. 'SCHEMA' => 'postgres',
  118. 'MODULE' => 'pgsql',
  119. 'DELIM' => ';',
  120. 'COMMENTS' => 'remove_comments',
  121. 'DRIVER' => 'postgres',
  122. 'AVAILABLE' => true,
  123. '2.0.x' => true,
  124. ),
  125. 'sqlite' => array(
  126. 'LABEL' => 'SQLite',
  127. 'SCHEMA' => 'sqlite',
  128. 'MODULE' => 'sqlite',
  129. 'DELIM' => ';',
  130. 'COMMENTS' => 'remove_remarks',
  131. 'DRIVER' => 'sqlite',
  132. 'AVAILABLE' => true,
  133. '2.0.x' => false,
  134. ),
  135. );
  136. if ($dbms)
  137. {
  138. if (isset($available_dbms[$dbms]))
  139. {
  140. $available_dbms = array($dbms => $available_dbms[$dbms]);
  141. }
  142. else
  143. {
  144. return array();
  145. }
  146. }
  147. // now perform some checks whether they are really available
  148. foreach ($available_dbms as $db_name => $db_ary)
  149. {
  150. if ($only_20x_options && !$db_ary['2.0.x'])
  151. {
  152. if ($return_unavailable)
  153. {
  154. $available_dbms[$db_name]['AVAILABLE'] = false;
  155. }
  156. else
  157. {
  158. unset($available_dbms[$db_name]);
  159. }
  160. continue;
  161. }
  162. $dll = $db_ary['MODULE'];
  163. if (!@extension_loaded($dll))
  164. {
  165. if (!can_load_dll($dll))
  166. {
  167. if ($return_unavailable)
  168. {
  169. $available_dbms[$db_name]['AVAILABLE'] = false;
  170. }
  171. else
  172. {
  173. unset($available_dbms[$db_name]);
  174. }
  175. continue;
  176. }
  177. }
  178. $any_db_support = true;
  179. }
  180. if ($return_unavailable)
  181. {
  182. $available_dbms['ANY_DB_SUPPORT'] = $any_db_support;
  183. }
  184. return $available_dbms;
  185. }
  186. /**
  187. * Generate the drop down of available database options
  188. */
  189. function dbms_select($default = '', $only_20x_options = false)
  190. {
  191. global $lang;
  192. $available_dbms = get_available_dbms(false, false, $only_20x_options);
  193. $dbms_options = '';
  194. foreach ($available_dbms as $dbms_name => $details)
  195. {
  196. $selected = ($dbms_name == $default) ? ' selected="selected"' : '';
  197. $dbms_options .= '<option value="' . $dbms_name . '"' . $selected .'>' . $lang['DLL_' . strtoupper($dbms_name)] . '</option>';
  198. }
  199. return $dbms_options;
  200. }
  201. /**
  202. * Get tables of a database
  203. */
  204. function get_tables($db)
  205. {
  206. switch ($db->sql_layer)
  207. {
  208. case 'mysql':
  209. case 'mysql4':
  210. case 'mysqli':
  211. $sql = 'SHOW TABLES';
  212. break;
  213. case 'sqlite':
  214. $sql = 'SELECT name
  215. FROM sqlite_master
  216. WHERE type = "table"';
  217. break;
  218. case 'mssql':
  219. case 'mssql_odbc':
  220. case 'mssqlnative':
  221. $sql = "SELECT name
  222. FROM sysobjects
  223. WHERE type='U'";
  224. break;
  225. case 'postgres':
  226. $sql = 'SELECT relname
  227. FROM pg_stat_user_tables';
  228. break;
  229. case 'firebird':
  230. $sql = 'SELECT rdb$relation_name
  231. FROM rdb$relations
  232. WHERE rdb$view_source is null
  233. AND rdb$system_flag = 0';
  234. break;
  235. case 'oracle':
  236. $sql = 'SELECT table_name
  237. FROM USER_TABLES';
  238. break;
  239. }
  240. $result = $db->sql_query($sql);
  241. $tables = array();
  242. while ($row = $db->sql_fetchrow($result))
  243. {
  244. $tables[] = current($row);
  245. }
  246. $db->sql_freeresult($result);
  247. return $tables;
  248. }
  249. /**
  250. * Used to test whether we are able to connect to the database the user has specified
  251. * and identify any problems (eg there are already tables with the names we want to use
  252. * @param array $dbms should be of the format of an element of the array returned by {@link get_available_dbms get_available_dbms()}
  253. * necessary extensions should be loaded already
  254. */
  255. function connect_check_db($error_connect, &$error, $dbms_details, $table_prefix, $dbhost, $dbuser, $dbpasswd, $dbname, $dbport, $prefix_may_exist = false, $load_dbal = true, $unicode_check = true)
  256. {
  257. global $phpbb_root_path, $phpEx, $config, $lang;
  258. $dbms = $dbms_details['DRIVER'];
  259. if ($load_dbal)
  260. {
  261. // Include the DB layer
  262. include($phpbb_root_path . 'includes/db/' . $dbms . '.' . $phpEx);
  263. }
  264. // Instantiate it and set return on error true
  265. $sql_db = 'dbal_' . $dbms;
  266. $db = new $sql_db();
  267. $db->sql_return_on_error(true);
  268. // Check that we actually have a database name before going any further.....
  269. if ($dbms_details['DRIVER'] != 'sqlite' && $dbms_details['DRIVER'] != 'oracle' && $dbname === '')
  270. {
  271. $error[] = $lang['INST_ERR_DB_NO_NAME'];
  272. return false;
  273. }
  274. // Make sure we don't have a daft user who thinks having the SQLite database in the forum directory is a good idea
  275. if ($dbms_details['DRIVER'] == 'sqlite' && stripos(phpbb_realpath($dbhost), phpbb_realpath('../')) === 0)
  276. {
  277. $error[] = $lang['INST_ERR_DB_FORUM_PATH'];
  278. return false;
  279. }
  280. // Check the prefix length to ensure that index names are not too long and does not contain invalid characters
  281. switch ($dbms_details['DRIVER'])
  282. {
  283. case 'mysql':
  284. case 'mysqli':
  285. if (strspn($table_prefix, '-./\\') !== 0)
  286. {
  287. $error[] = $lang['INST_ERR_PREFIX_INVALID'];
  288. return false;
  289. }
  290. // no break;
  291. case 'postgres':
  292. $prefix_length = 36;
  293. break;
  294. case 'mssql':
  295. case 'mssql_odbc':
  296. case 'mssqlnative':
  297. $prefix_length = 90;
  298. break;
  299. case 'sqlite':
  300. $prefix_length = 200;
  301. break;
  302. case 'firebird':
  303. case 'oracle':
  304. $prefix_length = 6;
  305. break;
  306. }
  307. if (strlen($table_prefix) > $prefix_length)
  308. {
  309. $error[] = sprintf($lang['INST_ERR_PREFIX_TOO_LONG'], $prefix_length);
  310. return false;
  311. }
  312. // Try and connect ...
  313. if (is_array($db->sql_connect($dbhost, $dbuser, $dbpasswd, $dbname, $dbport, false, true)))
  314. {
  315. $db_error = $db->sql_error();
  316. $error[] = $lang['INST_ERR_DB_CONNECT'] . '<br />' . (($db_error['message']) ? $db_error['message'] : $lang['INST_ERR_DB_NO_ERROR']);
  317. }
  318. else
  319. {
  320. // Likely matches for an existing phpBB installation
  321. if (!$prefix_may_exist)
  322. {
  323. $temp_prefix = strtolower($table_prefix);
  324. $table_ary = array($temp_prefix . 'attachments', $temp_prefix . 'config', $temp_prefix . 'sessions', $temp_prefix . 'topics', $temp_prefix . 'users');
  325. $tables = get_tables($db);
  326. $tables = array_map('strtolower', $tables);
  327. $table_intersect = array_intersect($tables, $table_ary);
  328. if (sizeof($table_intersect))
  329. {
  330. $error[] = $lang['INST_ERR_PREFIX'];
  331. }
  332. }
  333. // Make sure that the user has selected a sensible DBAL for the DBMS actually installed
  334. switch ($dbms_details['DRIVER'])
  335. {
  336. case 'mysqli':
  337. if (version_compare(mysqli_get_server_info($db->db_connect_id), '4.1.3', '<'))
  338. {
  339. $error[] = $lang['INST_ERR_DB_NO_MYSQLI'];
  340. }
  341. break;
  342. case 'sqlite':
  343. if (version_compare(sqlite_libversion(), '2.8.2', '<'))
  344. {
  345. $error[] = $lang['INST_ERR_DB_NO_SQLITE'];
  346. }
  347. break;
  348. case 'firebird':
  349. // check the version of FB, use some hackery if we can't get access to the server info
  350. if ($db->service_handle !== false && function_exists('ibase_server_info'))
  351. {
  352. $val = @ibase_server_info($db->service_handle, IBASE_SVC_SERVER_VERSION);
  353. preg_match('#V([\d.]+)#', $val, $match);
  354. if ($match[1] < 2)
  355. {
  356. $error[] = $lang['INST_ERR_DB_NO_FIREBIRD'];
  357. }
  358. $db_info = @ibase_db_info($db->service_handle, $dbname, IBASE_STS_HDR_PAGES);
  359. preg_match('/^\\s*Page size\\s*(\\d+)/m', $db_info, $regs);
  360. $page_size = intval($regs[1]);
  361. if ($page_size < 8192)
  362. {
  363. $error[] = $lang['INST_ERR_DB_NO_FIREBIRD_PS'];
  364. }
  365. }
  366. else
  367. {
  368. $sql = "SELECT *
  369. FROM RDB$FUNCTIONS
  370. WHERE RDB$SYSTEM_FLAG IS NULL
  371. AND RDB$FUNCTION_NAME = 'CHAR_LENGTH'";
  372. $result = $db->sql_query($sql);
  373. $row = $db->sql_fetchrow($result);
  374. $db->sql_freeresult($result);
  375. // if its a UDF, its too old
  376. if ($row)
  377. {
  378. $error[] = $lang['INST_ERR_DB_NO_FIREBIRD'];
  379. }
  380. else
  381. {
  382. $sql = 'SELECT 1 FROM RDB$DATABASE
  383. WHERE BIN_AND(10, 1) = 0';
  384. $result = $db->sql_query($sql);
  385. if (!$result) // This can only fail if BIN_AND is not defined
  386. {
  387. $error[] = $lang['INST_ERR_DB_NO_FIREBIRD'];
  388. }
  389. $db->sql_freeresult($result);
  390. }
  391. // Setup the stuff for our random table
  392. $char_array = array_merge(range('A', 'Z'), range('0', '9'));
  393. $char_len = mt_rand(7, 9);
  394. $char_array_len = sizeof($char_array) - 1;
  395. $final = '';
  396. for ($i = 0; $i < $char_len; $i++)
  397. {
  398. $final .= $char_array[mt_rand(0, $char_array_len)];
  399. }
  400. // Create some random table
  401. $sql = 'CREATE TABLE ' . $final . " (
  402. FIELD1 VARCHAR(255) CHARACTER SET UTF8 DEFAULT '' NOT NULL COLLATE UNICODE,
  403. FIELD2 INTEGER DEFAULT 0 NOT NULL);";
  404. $db->sql_query($sql);
  405. // Create an index that should fail if the page size is less than 8192
  406. $sql = 'CREATE INDEX ' . $final . ' ON ' . $final . '(FIELD1, FIELD2);';
  407. $db->sql_query($sql);
  408. if (ibase_errmsg() !== false)
  409. {
  410. $error[] = $lang['INST_ERR_DB_NO_FIREBIRD_PS'];
  411. }
  412. else
  413. {
  414. // Kill the old table
  415. $db->sql_query('DROP TABLE ' . $final . ';');
  416. }
  417. unset($final);
  418. }
  419. break;
  420. case 'oracle':
  421. if ($unicode_check)
  422. {
  423. $sql = "SELECT *
  424. FROM NLS_DATABASE_PARAMETERS
  425. WHERE PARAMETER = 'NLS_RDBMS_VERSION'
  426. OR PARAMETER = 'NLS_CHARACTERSET'";
  427. $result = $db->sql_query($sql);
  428. while ($row = $db->sql_fetchrow($result))
  429. {
  430. $stats[$row['parameter']] = $row['value'];
  431. }
  432. $db->sql_freeresult($result);
  433. if (version_compare($stats['NLS_RDBMS_VERSION'], '9.2', '<') && $stats['NLS_CHARACTERSET'] !== 'UTF8')
  434. {
  435. $error[] = $lang['INST_ERR_DB_NO_ORACLE'];
  436. }
  437. }
  438. break;
  439. case 'postgres':
  440. if ($unicode_check)
  441. {
  442. $sql = "SHOW server_encoding;";
  443. $result = $db->sql_query($sql);
  444. $row = $db->sql_fetchrow($result);
  445. $db->sql_freeresult($result);
  446. if ($row['server_encoding'] !== 'UNICODE' && $row['server_encoding'] !== 'UTF8')
  447. {
  448. $error[] = $lang['INST_ERR_DB_NO_POSTGRES'];
  449. }
  450. }
  451. break;
  452. }
  453. }
  454. if ($error_connect && (!isset($error) || !sizeof($error)))
  455. {
  456. return true;
  457. }
  458. return false;
  459. }
  460. /**
  461. * remove_remarks will strip the sql comment lines out of an uploaded sql file
  462. */
  463. function remove_remarks(&$sql)
  464. {
  465. $sql = preg_replace('/\n{2,}/', "\n", preg_replace('/^#.*$/m', "\n", $sql));
  466. }
  467. /**
  468. * split_sql_file will split an uploaded sql file into single sql statements.
  469. * Note: expects trim() to have already been run on $sql.
  470. */
  471. function split_sql_file($sql, $delimiter)
  472. {
  473. $sql = str_replace("\r" , '', $sql);
  474. $data = preg_split('/' . preg_quote($delimiter, '/') . '$/m', $sql);
  475. $data = array_map('trim', $data);
  476. // The empty case
  477. $end_data = end($data);
  478. if (empty($end_data))
  479. {
  480. unset($data[key($data)]);
  481. }
  482. return $data;
  483. }
  484. /**
  485. * For replacing {L_*} strings with preg_replace_callback
  486. */
  487. function adjust_language_keys_callback($matches)
  488. {
  489. if (!empty($matches[1]))
  490. {
  491. global $lang, $db;
  492. return (!empty($lang[$matches[1]])) ? $db->sql_escape($lang[$matches[1]]) : $db->sql_escape($matches[1]);
  493. }
  494. }
  495. ?>