PageRenderTime 25ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/functions_install.php

https://code.google.com/p/phpbbex/
PHP | 448 lines | 341 code | 44 blank | 63 comment | 52 complexity | b2f5d1ce9628409de41456da255133f4 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. 'mysqli' => array(
  46. 'LABEL' => 'MySQL with MySQLi Extension',
  47. 'SCHEMA' => 'mysql_41',
  48. 'MODULE' => 'mysqli',
  49. 'DELIM' => ';',
  50. 'COMMENTS' => 'remove_remarks',
  51. 'DRIVER' => 'mysqli',
  52. 'AVAILABLE' => true,
  53. '2.0.x' => true,
  54. ),
  55. 'mysql' => array(
  56. 'LABEL' => 'MySQL',
  57. 'SCHEMA' => 'mysql',
  58. 'MODULE' => 'mysql',
  59. 'DELIM' => ';',
  60. 'COMMENTS' => 'remove_remarks',
  61. 'DRIVER' => 'mysql',
  62. 'AVAILABLE' => true,
  63. '2.0.x' => true,
  64. ),
  65. );
  66. if ($dbms)
  67. {
  68. if (isset($available_dbms[$dbms]))
  69. {
  70. $available_dbms = array($dbms => $available_dbms[$dbms]);
  71. }
  72. else
  73. {
  74. return array();
  75. }
  76. }
  77. // now perform some checks whether they are really available
  78. foreach ($available_dbms as $db_name => $db_ary)
  79. {
  80. if ($only_20x_options && !$db_ary['2.0.x'])
  81. {
  82. if ($return_unavailable)
  83. {
  84. $available_dbms[$db_name]['AVAILABLE'] = false;
  85. }
  86. else
  87. {
  88. unset($available_dbms[$db_name]);
  89. }
  90. continue;
  91. }
  92. $dll = $db_ary['MODULE'];
  93. if (!@extension_loaded($dll))
  94. {
  95. if (!can_load_dll($dll))
  96. {
  97. if ($return_unavailable)
  98. {
  99. $available_dbms[$db_name]['AVAILABLE'] = false;
  100. }
  101. else
  102. {
  103. unset($available_dbms[$db_name]);
  104. }
  105. continue;
  106. }
  107. }
  108. $any_db_support = true;
  109. }
  110. if ($return_unavailable)
  111. {
  112. $available_dbms['ANY_DB_SUPPORT'] = $any_db_support;
  113. }
  114. return $available_dbms;
  115. }
  116. /**
  117. * Generate the drop down of available database options
  118. */
  119. function dbms_select($default = '', $only_20x_options = false)
  120. {
  121. global $lang;
  122. $available_dbms = get_available_dbms(false, false, $only_20x_options);
  123. $dbms_options = '';
  124. foreach ($available_dbms as $dbms_name => $details)
  125. {
  126. $selected = ($dbms_name == $default) ? ' selected="selected"' : '';
  127. $dbms_options .= '<option value="' . $dbms_name . '"' . $selected .'>' . $lang['DLL_' . strtoupper($dbms_name)] . '</option>';
  128. }
  129. return $dbms_options;
  130. }
  131. /**
  132. * Get tables of a database
  133. *
  134. * @deprecated
  135. */
  136. function get_tables(&$db)
  137. {
  138. if (!class_exists('phpbb_db_tools'))
  139. {
  140. global $phpbb_root_path, $phpEx;
  141. require($phpbb_root_path . 'includes/db/db_tools.' . $phpEx);
  142. }
  143. $db_tools = new phpbb_db_tools($db);
  144. return $db_tools->sql_list_tables();
  145. }
  146. /**
  147. * Used to test whether we are able to connect to the database the user has specified
  148. * and identify any problems (eg there are already tables with the names we want to use
  149. * @param array $dbms should be of the format of an element of the array returned by {@link get_available_dbms get_available_dbms()}
  150. * necessary extensions should be loaded already
  151. */
  152. 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)
  153. {
  154. global $phpbb_root_path, $phpEx, $config, $lang;
  155. $dbms = $dbms_details['DRIVER'];
  156. if ($load_dbal)
  157. {
  158. // Include the DB layer
  159. include($phpbb_root_path . 'includes/db/' . $dbms . '.' . $phpEx);
  160. }
  161. // Instantiate it and set return on error true
  162. $sql_db = 'dbal_' . $dbms;
  163. $db = new $sql_db();
  164. $db->sql_return_on_error(true);
  165. // Check that we actually have a database name before going any further.....
  166. if ($dbms_details['DRIVER'] != 'sqlite' && $dbms_details['DRIVER'] != 'oracle' && $dbname === '')
  167. {
  168. $error[] = $lang['INST_ERR_DB_NO_NAME'];
  169. return false;
  170. }
  171. // Make sure we don't have a daft user who thinks having the SQLite database in the forum directory is a good idea
  172. if ($dbms_details['DRIVER'] == 'sqlite' && stripos(phpbb_realpath($dbhost), phpbb_realpath('../')) === 0)
  173. {
  174. $error[] = $lang['INST_ERR_DB_FORUM_PATH'];
  175. return false;
  176. }
  177. // Check the prefix length to ensure that index names are not too long and does not contain invalid characters
  178. switch ($dbms_details['DRIVER'])
  179. {
  180. case 'mysql':
  181. case 'mysqli':
  182. if (strspn($table_prefix, '-./\\') !== 0)
  183. {
  184. $error[] = $lang['INST_ERR_PREFIX_INVALID'];
  185. return false;
  186. }
  187. // no break;
  188. case 'postgres':
  189. $prefix_length = 36;
  190. break;
  191. case 'mssql':
  192. case 'mssql_odbc':
  193. case 'mssqlnative':
  194. $prefix_length = 90;
  195. break;
  196. case 'sqlite':
  197. $prefix_length = 200;
  198. break;
  199. case 'firebird':
  200. case 'oracle':
  201. $prefix_length = 6;
  202. break;
  203. }
  204. if (strlen($table_prefix) > $prefix_length)
  205. {
  206. $error[] = sprintf($lang['INST_ERR_PREFIX_TOO_LONG'], $prefix_length);
  207. return false;
  208. }
  209. // Try and connect ...
  210. if (is_array($db->sql_connect($dbhost, $dbuser, $dbpasswd, $dbname, $dbport, false, true)))
  211. {
  212. $db_error = $db->sql_error();
  213. $error[] = $lang['INST_ERR_DB_CONNECT'] . '<br />' . (($db_error['message']) ? $db_error['message'] : $lang['INST_ERR_DB_NO_ERROR']);
  214. }
  215. else
  216. {
  217. // Likely matches for an existing phpBB installation
  218. if (!$prefix_may_exist)
  219. {
  220. $temp_prefix = strtolower($table_prefix);
  221. $table_ary = array($temp_prefix . 'attachments', $temp_prefix . 'config', $temp_prefix . 'sessions', $temp_prefix . 'topics', $temp_prefix . 'users');
  222. $tables = get_tables($db);
  223. $tables = array_map('strtolower', $tables);
  224. $table_intersect = array_intersect($tables, $table_ary);
  225. if (sizeof($table_intersect))
  226. {
  227. $error[] = $lang['INST_ERR_PREFIX'];
  228. }
  229. }
  230. // Make sure that the user has selected a sensible DBAL for the DBMS actually installed
  231. switch ($dbms_details['DRIVER'])
  232. {
  233. case 'mysqli':
  234. if (version_compare(mysqli_get_server_info($db->db_connect_id), '4.1.3', '<'))
  235. {
  236. $error[] = $lang['INST_ERR_DB_NO_MYSQLI'];
  237. }
  238. break;
  239. case 'sqlite':
  240. if (version_compare(sqlite_libversion(), '2.8.2', '<'))
  241. {
  242. $error[] = $lang['INST_ERR_DB_NO_SQLITE'];
  243. }
  244. break;
  245. case 'firebird':
  246. // check the version of FB, use some hackery if we can't get access to the server info
  247. if ($db->service_handle !== false && function_exists('ibase_server_info'))
  248. {
  249. $val = @ibase_server_info($db->service_handle, IBASE_SVC_SERVER_VERSION);
  250. preg_match('#V([\d.]+)#', $val, $match);
  251. if ($match[1] < 2)
  252. {
  253. $error[] = $lang['INST_ERR_DB_NO_FIREBIRD'];
  254. }
  255. $db_info = @ibase_db_info($db->service_handle, $dbname, IBASE_STS_HDR_PAGES);
  256. preg_match('/^\\s*Page size\\s*(\\d+)/m', $db_info, $regs);
  257. $page_size = intval($regs[1]);
  258. if ($page_size < 8192)
  259. {
  260. $error[] = $lang['INST_ERR_DB_NO_FIREBIRD_PS'];
  261. }
  262. }
  263. else
  264. {
  265. $sql = "SELECT *
  266. FROM RDB$FUNCTIONS
  267. WHERE RDB$SYSTEM_FLAG IS NULL
  268. AND RDB$FUNCTION_NAME = 'CHAR_LENGTH'";
  269. $result = $db->sql_query($sql);
  270. $row = $db->sql_fetchrow($result);
  271. $db->sql_freeresult($result);
  272. // if its a UDF, its too old
  273. if ($row)
  274. {
  275. $error[] = $lang['INST_ERR_DB_NO_FIREBIRD'];
  276. }
  277. else
  278. {
  279. $sql = 'SELECT 1 FROM RDB$DATABASE
  280. WHERE BIN_AND(10, 1) = 0';
  281. $result = $db->sql_query($sql);
  282. if (!$result) // This can only fail if BIN_AND is not defined
  283. {
  284. $error[] = $lang['INST_ERR_DB_NO_FIREBIRD'];
  285. }
  286. $db->sql_freeresult($result);
  287. }
  288. // Setup the stuff for our random table
  289. $char_array = array_merge(range('A', 'Z'), range('0', '9'));
  290. $char_len = mt_rand(7, 9);
  291. $char_array_len = sizeof($char_array) - 1;
  292. $final = '';
  293. for ($i = 0; $i < $char_len; $i++)
  294. {
  295. $final .= $char_array[mt_rand(0, $char_array_len)];
  296. }
  297. // Create some random table
  298. $sql = 'CREATE TABLE ' . $final . " (
  299. FIELD1 VARCHAR(255) CHARACTER SET UTF8 DEFAULT '' NOT NULL COLLATE UNICODE,
  300. FIELD2 INTEGER DEFAULT 0 NOT NULL);";
  301. $db->sql_query($sql);
  302. // Create an index that should fail if the page size is less than 8192
  303. $sql = 'CREATE INDEX ' . $final . ' ON ' . $final . '(FIELD1, FIELD2);';
  304. $db->sql_query($sql);
  305. if (ibase_errmsg() !== false)
  306. {
  307. $error[] = $lang['INST_ERR_DB_NO_FIREBIRD_PS'];
  308. }
  309. else
  310. {
  311. // Kill the old table
  312. $db->sql_query('DROP TABLE ' . $final . ';');
  313. }
  314. unset($final);
  315. }
  316. break;
  317. case 'oracle':
  318. if ($unicode_check)
  319. {
  320. $sql = "SELECT *
  321. FROM NLS_DATABASE_PARAMETERS
  322. WHERE PARAMETER = 'NLS_RDBMS_VERSION'
  323. OR PARAMETER = 'NLS_CHARACTERSET'";
  324. $result = $db->sql_query($sql);
  325. while ($row = $db->sql_fetchrow($result))
  326. {
  327. $stats[$row['parameter']] = $row['value'];
  328. }
  329. $db->sql_freeresult($result);
  330. if (version_compare($stats['NLS_RDBMS_VERSION'], '9.2', '<') && $stats['NLS_CHARACTERSET'] !== 'UTF8')
  331. {
  332. $error[] = $lang['INST_ERR_DB_NO_ORACLE'];
  333. }
  334. }
  335. break;
  336. case 'postgres':
  337. if ($unicode_check)
  338. {
  339. $sql = "SHOW server_encoding;";
  340. $result = $db->sql_query($sql);
  341. $row = $db->sql_fetchrow($result);
  342. $db->sql_freeresult($result);
  343. if ($row['server_encoding'] !== 'UNICODE' && $row['server_encoding'] !== 'UTF8')
  344. {
  345. $error[] = $lang['INST_ERR_DB_NO_POSTGRES'];
  346. }
  347. }
  348. break;
  349. }
  350. }
  351. if ($error_connect && (!isset($error) || !sizeof($error)))
  352. {
  353. return true;
  354. }
  355. return false;
  356. }
  357. /**
  358. * remove_remarks will strip the sql comment lines out of an uploaded sql file
  359. */
  360. function remove_remarks(&$sql)
  361. {
  362. $sql = preg_replace('/\n{2,}/', "\n", preg_replace('/^#.*$/m', "\n", $sql));
  363. }
  364. /**
  365. * split_sql_file will split an uploaded sql file into single sql statements.
  366. * Note: expects trim() to have already been run on $sql.
  367. */
  368. function split_sql_file($sql, $delimiter)
  369. {
  370. $sql = str_replace("\r" , '', $sql);
  371. $data = preg_split('/' . preg_quote($delimiter, '/') . '$/m', $sql);
  372. $data = array_map('trim', $data);
  373. // The empty case
  374. $end_data = end($data);
  375. if (empty($end_data))
  376. {
  377. unset($data[key($data)]);
  378. }
  379. return $data;
  380. }
  381. /**
  382. * For replacing {L_*} strings with preg_replace_callback
  383. */
  384. function adjust_language_keys_callback($matches)
  385. {
  386. if (!empty($matches[1]))
  387. {
  388. global $lang, $db;
  389. return (!empty($lang[$matches[1]])) ? $db->sql_escape($lang[$matches[1]]) : $db->sql_escape($matches[1]);
  390. }
  391. }
  392. ?>