PageRenderTime 55ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/functions_install.php

https://bitbucket.org/jablonski/yebood
PHP | 597 lines | 459 code | 52 blank | 86 comment | 53 complexity | b815144bfdb44e07e2a9614878e689b4 MD5 | raw file
Possible License(s): AGPL-1.0
  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. * @deprecated
  205. */
  206. function get_tables(&$db)
  207. {
  208. if (!class_exists('phpbb_db_tools'))
  209. {
  210. global $phpbb_root_path, $phpEx;
  211. require($phpbb_root_path . 'includes/db/db_tools.' . $phpEx);
  212. }
  213. $db_tools = new phpbb_db_tools($db);
  214. return $db_tools->sql_list_tables();
  215. }
  216. /**
  217. * Used to test whether we are able to connect to the database the user has specified
  218. * and identify any problems (eg there are already tables with the names we want to use
  219. * @param array $dbms should be of the format of an element of the array returned by {@link get_available_dbms get_available_dbms()}
  220. * necessary extensions should be loaded already
  221. */
  222. 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)
  223. {
  224. global $phpbb_root_path, $phpEx, $config, $lang;
  225. $dbms = $dbms_details['DRIVER'];
  226. if ($load_dbal)
  227. {
  228. // Include the DB layer
  229. include($phpbb_root_path . 'includes/db/' . $dbms . '.' . $phpEx);
  230. }
  231. // Instantiate it and set return on error true
  232. $sql_db = 'dbal_' . $dbms;
  233. $db = new $sql_db();
  234. $db->sql_return_on_error(true);
  235. // Check that we actually have a database name before going any further.....
  236. if ($dbms_details['DRIVER'] != 'sqlite' && $dbms_details['DRIVER'] != 'oracle' && $dbname === '')
  237. {
  238. $error[] = $lang['INST_ERR_DB_NO_NAME'];
  239. return false;
  240. }
  241. // Make sure we don't have a daft user who thinks having the SQLite database in the forum directory is a good idea
  242. if ($dbms_details['DRIVER'] == 'sqlite' && stripos(phpbb_realpath($dbhost), phpbb_realpath('../')) === 0)
  243. {
  244. $error[] = $lang['INST_ERR_DB_FORUM_PATH'];
  245. return false;
  246. }
  247. // Check the prefix length to ensure that index names are not too long and does not contain invalid characters
  248. switch ($dbms_details['DRIVER'])
  249. {
  250. case 'mysql':
  251. case 'mysqli':
  252. if (strspn($table_prefix, '-./\\') !== 0)
  253. {
  254. $error[] = $lang['INST_ERR_PREFIX_INVALID'];
  255. return false;
  256. }
  257. // no break;
  258. case 'postgres':
  259. $prefix_length = 36;
  260. break;
  261. case 'mssql':
  262. case 'mssql_odbc':
  263. case 'mssqlnative':
  264. $prefix_length = 90;
  265. break;
  266. case 'sqlite':
  267. $prefix_length = 200;
  268. break;
  269. case 'firebird':
  270. case 'oracle':
  271. $prefix_length = 6;
  272. break;
  273. }
  274. if (strlen($table_prefix) > $prefix_length)
  275. {
  276. $error[] = sprintf($lang['INST_ERR_PREFIX_TOO_LONG'], $prefix_length);
  277. return false;
  278. }
  279. // Try and connect ...
  280. if (is_array($db->sql_connect($dbhost, $dbuser, $dbpasswd, $dbname, $dbport, false, true)))
  281. {
  282. $db_error = $db->sql_error();
  283. $error[] = $lang['INST_ERR_DB_CONNECT'] . '<br />' . (($db_error['message']) ? $db_error['message'] : $lang['INST_ERR_DB_NO_ERROR']);
  284. }
  285. else
  286. {
  287. // Likely matches for an existing phpBB installation
  288. if (!$prefix_may_exist)
  289. {
  290. $temp_prefix = strtolower($table_prefix);
  291. $table_ary = array($temp_prefix . 'attachments', $temp_prefix . 'config', $temp_prefix . 'sessions', $temp_prefix . 'topics', $temp_prefix . 'users');
  292. $tables = get_tables($db);
  293. $tables = array_map('strtolower', $tables);
  294. $table_intersect = array_intersect($tables, $table_ary);
  295. if (sizeof($table_intersect))
  296. {
  297. $error[] = $lang['INST_ERR_PREFIX'];
  298. }
  299. }
  300. // Make sure that the user has selected a sensible DBAL for the DBMS actually installed
  301. switch ($dbms_details['DRIVER'])
  302. {
  303. case 'mysqli':
  304. if (version_compare(mysqli_get_server_info($db->db_connect_id), '4.1.3', '<'))
  305. {
  306. $error[] = $lang['INST_ERR_DB_NO_MYSQLI'];
  307. }
  308. break;
  309. case 'sqlite':
  310. if (version_compare(sqlite_libversion(), '2.8.2', '<'))
  311. {
  312. $error[] = $lang['INST_ERR_DB_NO_SQLITE'];
  313. }
  314. break;
  315. case 'firebird':
  316. // check the version of FB, use some hackery if we can't get access to the server info
  317. if ($db->service_handle !== false && function_exists('ibase_server_info'))
  318. {
  319. $val = @ibase_server_info($db->service_handle, IBASE_SVC_SERVER_VERSION);
  320. preg_match('#V([\d.]+)#', $val, $match);
  321. if ($match[1] < 2)
  322. {
  323. $error[] = $lang['INST_ERR_DB_NO_FIREBIRD'];
  324. }
  325. $db_info = @ibase_db_info($db->service_handle, $dbname, IBASE_STS_HDR_PAGES);
  326. preg_match('/^\\s*Page size\\s*(\\d+)/m', $db_info, $regs);
  327. $page_size = intval($regs[1]);
  328. if ($page_size < 8192)
  329. {
  330. $error[] = $lang['INST_ERR_DB_NO_FIREBIRD_PS'];
  331. }
  332. }
  333. else
  334. {
  335. $sql = "SELECT *
  336. FROM RDB$FUNCTIONS
  337. WHERE RDB$SYSTEM_FLAG IS NULL
  338. AND RDB$FUNCTION_NAME = 'CHAR_LENGTH'";
  339. $result = $db->sql_query($sql);
  340. $row = $db->sql_fetchrow($result);
  341. $db->sql_freeresult($result);
  342. // if its a UDF, its too old
  343. if ($row)
  344. {
  345. $error[] = $lang['INST_ERR_DB_NO_FIREBIRD'];
  346. }
  347. else
  348. {
  349. $sql = 'SELECT 1 FROM RDB$DATABASE
  350. WHERE BIN_AND(10, 1) = 0';
  351. $result = $db->sql_query($sql);
  352. if (!$result) // This can only fail if BIN_AND is not defined
  353. {
  354. $error[] = $lang['INST_ERR_DB_NO_FIREBIRD'];
  355. }
  356. $db->sql_freeresult($result);
  357. }
  358. // Setup the stuff for our random table
  359. $char_array = array_merge(range('A', 'Z'), range('0', '9'));
  360. $char_len = mt_rand(7, 9);
  361. $char_array_len = sizeof($char_array) - 1;
  362. $final = '';
  363. for ($i = 0; $i < $char_len; $i++)
  364. {
  365. $final .= $char_array[mt_rand(0, $char_array_len)];
  366. }
  367. // Create some random table
  368. $sql = 'CREATE TABLE ' . $final . " (
  369. FIELD1 VARCHAR(255) CHARACTER SET UTF8 DEFAULT '' NOT NULL COLLATE UNICODE,
  370. FIELD2 INTEGER DEFAULT 0 NOT NULL);";
  371. $db->sql_query($sql);
  372. // Create an index that should fail if the page size is less than 8192
  373. $sql = 'CREATE INDEX ' . $final . ' ON ' . $final . '(FIELD1, FIELD2);';
  374. $db->sql_query($sql);
  375. if (ibase_errmsg() !== false)
  376. {
  377. $error[] = $lang['INST_ERR_DB_NO_FIREBIRD_PS'];
  378. }
  379. else
  380. {
  381. // Kill the old table
  382. $db->sql_query('DROP TABLE ' . $final . ';');
  383. }
  384. unset($final);
  385. }
  386. break;
  387. case 'oracle':
  388. if ($unicode_check)
  389. {
  390. $sql = "SELECT *
  391. FROM NLS_DATABASE_PARAMETERS
  392. WHERE PARAMETER = 'NLS_RDBMS_VERSION'
  393. OR PARAMETER = 'NLS_CHARACTERSET'";
  394. $result = $db->sql_query($sql);
  395. while ($row = $db->sql_fetchrow($result))
  396. {
  397. $stats[$row['parameter']] = $row['value'];
  398. }
  399. $db->sql_freeresult($result);
  400. if (version_compare($stats['NLS_RDBMS_VERSION'], '9.2', '<') && $stats['NLS_CHARACTERSET'] !== 'UTF8')
  401. {
  402. $error[] = $lang['INST_ERR_DB_NO_ORACLE'];
  403. }
  404. }
  405. break;
  406. case 'postgres':
  407. if ($unicode_check)
  408. {
  409. $sql = "SHOW server_encoding;";
  410. $result = $db->sql_query($sql);
  411. $row = $db->sql_fetchrow($result);
  412. $db->sql_freeresult($result);
  413. if ($row['server_encoding'] !== 'UNICODE' && $row['server_encoding'] !== 'UTF8')
  414. {
  415. $error[] = $lang['INST_ERR_DB_NO_POSTGRES'];
  416. }
  417. }
  418. break;
  419. }
  420. }
  421. if ($error_connect && (!isset($error) || !sizeof($error)))
  422. {
  423. return true;
  424. }
  425. return false;
  426. }
  427. /**
  428. * Removes comments from schema files
  429. *
  430. * @deprecated Use phpbb_remove_comments() instead.
  431. */
  432. function remove_remarks(&$sql)
  433. {
  434. // Remove # style comments
  435. $sql = preg_replace('/\n{2,}/', "\n", preg_replace('/^#.*$/m', "\n", $sql));
  436. // Return by reference
  437. }
  438. /**
  439. * Removes "/* style" as well as "# style" comments from $input.
  440. *
  441. * @param string $input Input string
  442. *
  443. * @return string Input string with comments removed
  444. */
  445. function phpbb_remove_comments($input)
  446. {
  447. if (!function_exists('remove_comments'))
  448. {
  449. global $phpbb_root_path, $phpEx;
  450. require($phpbb_root_path . 'includes/functions_admin.' . $phpEx);
  451. }
  452. // Remove /* */ comments
  453. remove_comments($input);
  454. // Remove # style comments
  455. remove_remarks($input);
  456. return $input;
  457. }
  458. /**
  459. * split_sql_file will split an uploaded sql file into single sql statements.
  460. * Note: expects trim() to have already been run on $sql.
  461. */
  462. function split_sql_file($sql, $delimiter)
  463. {
  464. $sql = str_replace("\r" , '', $sql);
  465. $data = preg_split('/' . preg_quote($delimiter, '/') . '$/m', $sql);
  466. $data = array_map('trim', $data);
  467. // The empty case
  468. $end_data = end($data);
  469. if (empty($end_data))
  470. {
  471. unset($data[key($data)]);
  472. }
  473. return $data;
  474. }
  475. /**
  476. * For replacing {L_*} strings with preg_replace_callback
  477. */
  478. function adjust_language_keys_callback($matches)
  479. {
  480. if (!empty($matches[1]))
  481. {
  482. global $lang, $db;
  483. return (!empty($lang[$matches[1]])) ? $db->sql_escape($lang[$matches[1]]) : $db->sql_escape($matches[1]);
  484. }
  485. }
  486. /**
  487. * Creates the output to be stored in a phpBB config.php file
  488. *
  489. * @param array $data Array containing the database connection information
  490. * @param string $dbms The name of the DBAL class to use
  491. * @param array $load_extensions Array of additional extensions that should be loaded
  492. * @param bool $debug If the debug constants should be enabled by default or not
  493. *
  494. * @return string The output to write to the file
  495. */
  496. function phpbb_create_config_file_data($data, $dbms, $load_extensions, $debug = false)
  497. {
  498. $load_extensions = implode(',', $load_extensions);
  499. $config_data = "<?php\n";
  500. $config_data .= "// phpBB 3.0.x auto-generated configuration file\n// Do not change anything in this file!\n";
  501. $config_data_array = array(
  502. 'dbms' => $dbms,
  503. 'dbhost' => $data['dbhost'],
  504. 'dbport' => $data['dbport'],
  505. 'dbname' => $data['dbname'],
  506. 'dbuser' => $data['dbuser'],
  507. 'dbpasswd' => htmlspecialchars_decode($data['dbpasswd']),
  508. 'table_prefix' => $data['table_prefix'],
  509. 'acm_type' => 'file',
  510. 'load_extensions' => $load_extensions,
  511. );
  512. foreach ($config_data_array as $key => $value)
  513. {
  514. $config_data .= "\${$key} = '" . str_replace("'", "\\'", str_replace('\\', '\\\\', $value)) . "';\n";
  515. }
  516. $config_data .= "\n@define('PHPBB_INSTALLED', true);\n";
  517. if ($debug)
  518. {
  519. $config_data .= "@define('DEBUG', true);\n";
  520. $config_data .= "@define('DEBUG_EXTRA', true);\n";
  521. }
  522. else
  523. {
  524. $config_data .= "// @define('DEBUG', true);\n";
  525. $config_data .= "// @define('DEBUG_EXTRA', true);\n";
  526. }
  527. return $config_data;
  528. }
  529. ?>