PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/stk/includes/database_cleaner/functions_database_cleaner.php

https://code.google.com/p/phpbbex/
PHP | 462 lines | 336 code | 60 blank | 66 comment | 29 complexity | 6e375e4da2c793e6c01ae5ed768243f2 MD5 | raw file
Possible License(s): AGPL-1.0
  1. <?php
  2. /**
  3. *
  4. * @package Support Toolkit - Database Cleaner
  5. * @version $Id$
  6. * @copyright (c) 2009 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. * Collect all configiration data.
  19. */
  20. function get_config_rows(&$phpbb_config, &$config_rows, &$existing_config)
  21. {
  22. global $db;
  23. $existing_config = array();
  24. $sql_ary = array(
  25. 'SELECT' => 'c.config_name',
  26. 'FROM' => array(
  27. CONFIG_TABLE => 'c',
  28. ),
  29. 'ORDER_BY' => 'config_name ASC',
  30. );
  31. $sql = $db->sql_build_query('SELECT', $sql_ary);
  32. $result = $db->sql_query($sql);
  33. while ($row = $db->sql_fetchrow($result))
  34. {
  35. $existing_config[] = $row['config_name'];
  36. }
  37. $db->sql_freeresult($result);
  38. $config_rows = array_unique(array_merge(array_keys($phpbb_config), $existing_config));
  39. sort($config_rows);
  40. }
  41. /**
  42. * Collect all extension groups
  43. */
  44. function get_extension_groups_rows(&$extension_groups_data, &$extension_groups_rows, &$existing_extension_groups)
  45. {
  46. global $db;
  47. $existing_extension_groups = array();
  48. $sql_ary = array(
  49. 'SELECT' => 'eg.group_name',
  50. 'FROM' => array(
  51. EXTENSION_GROUPS_TABLE => 'eg',
  52. ),
  53. );
  54. $sql = $db->sql_build_query('SELECT', $sql_ary);
  55. $result = $db->sql_query($sql);
  56. while ($row = $db->sql_fetchrow($result))
  57. {
  58. $existing_extension_groups[] = $row['group_name'];
  59. }
  60. $db->sql_freeresult($result);
  61. $extension_groups_rows = array_unique(array_merge(array_keys($extension_groups_data), $existing_extension_groups));
  62. sort($extension_groups_rows);
  63. }
  64. /**
  65. * Collect the extensions for a given group
  66. */
  67. function get_extensions($group, &$group_id)
  68. {
  69. global $db;
  70. $sql_ary = array(
  71. 'SELECT' => 'e.extension, eg.group_id',
  72. 'FROM' => array(
  73. EXTENSIONS_TABLE => 'e',
  74. EXTENSION_GROUPS_TABLE => 'eg',
  75. ),
  76. 'WHERE' => "e.group_id = eg.group_id
  77. AND eg.group_name = '" . $db->sql_escape($group) . "'",
  78. );
  79. $sql = $db->sql_build_query('SELECT', $sql_ary);
  80. $result = $db->sql_query($sql);
  81. $set = array();
  82. while ($row = $db->sql_fetchrow($result))
  83. {
  84. $set[] = $row['extension'];
  85. if (empty($group_id))
  86. {
  87. $group_id = $row['group_id'];
  88. }
  89. }
  90. $db->sql_freeresult($result);
  91. return $set;
  92. }
  93. function get_permission_rows(&$permission_data, &$permission_rows, &$existing_permissions)
  94. {
  95. global $db;
  96. $existing_permissions = array();
  97. $sql_ary = array(
  98. 'SELECT' => 'ao.auth_option',
  99. 'FROM' => array(
  100. ACL_OPTIONS_TABLE => 'ao',
  101. ),
  102. );
  103. $sql = $db->sql_build_query('SELECT', $sql_ary);
  104. $result = $db->sql_query($sql);
  105. while ($row = $db->sql_fetchrow($result))
  106. {
  107. $existing_permissions[] = $row['auth_option'];
  108. }
  109. $db->sql_freeresult($result);
  110. $permission_rows = array_unique(array_merge(array_keys($permission_data), $existing_permissions));
  111. sort($permission_rows);
  112. }
  113. function get_role_rows(&$roles_data, &$role_rows, &$existing_roles)
  114. {
  115. global $db;
  116. $existing_roles = array();
  117. $sql_ary = array(
  118. 'SELECT' => 'ar.role_name',
  119. 'FROM' => array(
  120. ACL_ROLES_TABLE => 'ar',
  121. ),
  122. );
  123. $sql = $db->sql_build_query('SELECT', $sql_ary);
  124. $result = $db->sql_query($sql);
  125. while ($row = $db->sql_fetchrow($result))
  126. {
  127. $existing_roles[] = $row['role_name'];
  128. }
  129. $db->sql_freeresult($result);
  130. $role_rows = array_unique(array_merge(array_keys($roles_data), $existing_roles));
  131. sort($role_rows);
  132. }
  133. /**
  134. * Get all the phpBB system groups
  135. */
  136. function get_group_rows(&$group_data, &$group_rows, &$existing_groups)
  137. {
  138. global $db;
  139. $existing_groups = array();
  140. $sql_ary = array(
  141. 'SELECT' => 'g.group_name',
  142. 'FROM' => array(
  143. GROUPS_TABLE => 'g',
  144. ),
  145. 'WHERE' => 'group_type = 3',
  146. );
  147. $sql = $db->sql_build_query('SELECT', $sql_ary);
  148. $result = $db->sql_query($sql);
  149. while ($row = $db->sql_fetchrow($result))
  150. {
  151. $existing_groups[] = $row['group_name'];
  152. }
  153. $db->sql_freeresult($result);
  154. $group_rows = array_unique(array_merge(array_keys($group_data), $existing_groups));
  155. sort($group_rows);
  156. }
  157. /**
  158. * Get the columns of a given database table
  159. * @param String $table The name of the table
  160. */
  161. function get_columns($table)
  162. {
  163. global $db;
  164. static $db_tools = null;
  165. if ($db_tools == null)
  166. {
  167. if (!class_exists('phpbb_db_tools'))
  168. {
  169. include(PHPBB_ROOT_PATH . 'includes/db/db_tools.' . PHP_EXT);
  170. }
  171. $db_tools = new phpbb_db_tools($db);
  172. }
  173. // Set the query and column for each dbms
  174. static $sql = '';
  175. static $column_name = '';
  176. if (empty($sql))
  177. {
  178. switch ($db_tools->sql_layer)
  179. {
  180. // MySQL
  181. case 'mysql_40' :
  182. case 'mysql_41' :
  183. $sql = "SHOW COLUMNS FROM %s";
  184. $column_name = 'Field';
  185. break;
  186. // PostgreSQL
  187. case 'postgres' :
  188. $sql = "SELECT a.attname
  189. FROM pg_class c, pg_attribute a
  190. WHERE c.relname = '%s'
  191. AND a.attnum > 0
  192. AND a.attrelid = c.oid";
  193. $column_name = 'attname';
  194. break;
  195. // MsSQL
  196. case 'mssql' :
  197. case 'mssqlnative' :
  198. $sql = "SELECT c.name
  199. FROM syscolumns c
  200. LEFT JOIN sysobjects o ON c.id = o.id
  201. WHERE o.name = '%s'";
  202. $column_name = 'name';
  203. break;
  204. // Oracle
  205. case 'oracle' :
  206. $sql = "SELECT column_name
  207. FROM user_tab_columns
  208. WHERE table_name = '%s'";
  209. $column_name = 'column_name';
  210. break;
  211. // Firebird
  212. case 'firebird' :
  213. $sql = "SELECT RDB\$FIELD_NAME as FNAME
  214. FROM RDB\$RELATION_FIELDS
  215. WHERE RDB\$RELATION_NAME = '%s'";
  216. $column_name = 'fname';
  217. break;
  218. // SQLite
  219. case 'sqlite' :
  220. $sql = "SELECT sql
  221. FROM sqlite_master
  222. WHERE type = 'table'
  223. AND name = '%s'";
  224. $column_name = 'sql';
  225. break;
  226. }
  227. }
  228. // Run the query
  229. $result = $db->sql_query(sprintf($sql, $table));
  230. // Get the columns
  231. $columns = array();
  232. if ($db_tools->sql_layer != 'sqlite')
  233. {
  234. while ($row = $db->sql_fetchrow($result))
  235. {
  236. array_push($columns, $row[$column_name]);
  237. }
  238. }
  239. else
  240. {
  241. // Unfortunately SQLite doen't play as nice as the others
  242. $col_ary = $entities = $matches = array();
  243. $cols = $declaration = '';
  244. while ($row = $db->sql_fetchrow($result))
  245. {
  246. preg_match('#\((.*)\)#s', $row[$column_name], $matches);
  247. $cols = trim($matches[1]);
  248. $col_ary = preg_split('/,(?![\s\w]+\))/m', $cols);
  249. foreach ($col_ary as $declaration)
  250. {
  251. $entities = preg_split('#\s+#', trim($declaration));
  252. if ($entities[0] == 'PRIMARY')
  253. {
  254. continue;
  255. }
  256. array_push($columns, $entities[0]);
  257. }
  258. }
  259. }
  260. $db->sql_freeresult($result);
  261. return $columns;
  262. }
  263. /**
  264. * Get all tables used by phpBB
  265. */
  266. function get_phpbb_tables()
  267. {
  268. global $db, $table_prefix;
  269. static $_tables = array();
  270. if (!empty($_tables))
  271. {
  272. return $_tables;
  273. }
  274. if (!function_exists('get_tables'))
  275. {
  276. include PHPBB_ROOT_PATH . 'includes/functions_install.' . PHP_EXT;
  277. }
  278. // Function returns all tables in the database
  279. $all_tables = get_tables($db);
  280. // @TODO: tprefix, uppercase voor firebird/oracle!
  281. // Only get tables using the phpBB prefix
  282. if (!empty($table_prefix))
  283. {
  284. foreach ($all_tables as $table)
  285. {
  286. // Use `stripos` for Oracle and Firebird support. (#62821)
  287. if (stripos($table, $table_prefix) === 0)
  288. {
  289. $_tables[] = $table;
  290. }
  291. }
  292. }
  293. else
  294. {
  295. // Use is using an empty table prefix (Bug #62537)
  296. // no way to determine the phpBB tables, in this case
  297. // we'll show everything with a warning that the tool
  298. // most likely want to trash a lot of tables '-,-
  299. global $template;
  300. $template->assign_vars(array(
  301. 'ERROR_MESSAGE' => user_lang('EMPTY_PREFIX_EXPLAIN'),
  302. 'ERROR_TITLE' => user_lang('EMPTY_PREFIX'),
  303. ));
  304. $_tables = $all_tables;
  305. }
  306. sort($_tables);
  307. return $_tables;
  308. }
  309. /**
  310. * Compile the cleaner data
  311. * @param database_cleaner_data The database cleaner data object
  312. * @param String The version
  313. */
  314. function fetch_cleaner_data(&$data, $phpbb_version)
  315. {
  316. global $config;
  317. // Fetch all the files
  318. if (!function_exists('filelist'))
  319. {
  320. include PHPBB_ROOT_PATH . 'includes/functions_admin.' . PHP_EXT;
  321. }
  322. $filelist = array_shift(filelist(STK_ROOT_PATH . 'includes/database_cleaner/', 'data/', PHP_EXT));
  323. usort($filelist, 'version_compare');
  324. // Add the data
  325. foreach ($filelist as $file)
  326. {
  327. $version = pathinfo_filename($file);
  328. $class = 'datafile_' . $version;
  329. if (!class_exists($class))
  330. {
  331. include STK_ROOT_PATH . "includes/database_cleaner/data/{$version}." . PHP_EXT;
  332. }
  333. $_datafile = new $class();
  334. // Set the data
  335. $data->bots = array_merge($data->bots, $_datafile->bots);
  336. $data->config = array_merge($data->config, $_datafile->config);
  337. $data->acl_options = array_merge($data->acl_options, $_datafile->acl_options);
  338. $data->acl_roles = array_merge($data->acl_roles, $_datafile->acl_roles);
  339. $data->acl_role_data = array_merge_recursive($data->acl_role_data, $_datafile->acl_role_data);
  340. $data->extension_groups = array_merge($data->extension_groups, $_datafile->extension_groups);
  341. $data->extensions = array_merge($data->extensions, $_datafile->extensions);
  342. $data->module_categories = array_merge($data->module_categories, $_datafile->module_categories);
  343. $data->module_extras = array_merge($data->module_extras, $_datafile->module_extras);
  344. $data->groups = array_merge($data->groups, $_datafile->groups);
  345. $data->removed_config = array_merge($data->removed_config, $_datafile->removed_config);
  346. $data->report_reasons = array_merge($data->report_reasons, $_datafile->report_reasons);
  347. $_datafile->get_schema_struct($data->schema_data);
  348. // Just make sure that nothing sticks
  349. unset($_datafile);
  350. // Break after our version
  351. if (version_compare($version, $phpbb_version, 'eq'))
  352. {
  353. break;
  354. }
  355. }
  356. // Perform some actions that only have to be done on given versions or on all
  357. switch($phpbb_version)
  358. {
  359. case '3_0_10' :
  360. case '3_0_9' :
  361. // The extension group names have been changed, remove the old ones
  362. foreach ($data->extension_groups as $key => $null)
  363. {
  364. if (strpos($key, 'EXT_') === 0)
  365. {
  366. unset($data->extension_groups[$key]);
  367. }
  368. }
  369. // Same for the extensions
  370. foreach ($data->extensions as $key => $null)
  371. {
  372. if (strpos($key, 'EXT_') === 0)
  373. {
  374. unset($data->extensions[$key]);
  375. }
  376. }
  377. // No Break;
  378. case '3_0_8' :
  379. case '3_0_7_pl1' :
  380. case '3_0_7' :
  381. case '3_0_6' :
  382. // If $config['questionnaire_unique_id] exists add it to the config data array
  383. if (isset($config['questionnaire_unique_id']))
  384. {
  385. $data->config['questionnaire_unique_id'] = array('config_value' => $config['questionnaire_unique_id'], 'is_dynamic' => '0');
  386. }
  387. // Need to force do some ordering on $module_extras
  388. $extra_add = array('ACP_FORUM_PERMISSIONS_COPY');
  389. array_splice($data->module_extras['acp']['ACP_FORUM_BASED_PERMISSIONS'], 1, 0, $extra_add);
  390. // No Break;
  391. case '3_0_5' :
  392. case '3_0_4' :
  393. case '3_0_3' :
  394. case '3_0_2' :
  395. case '3_0_1' :
  396. case '3_0_0' :
  397. $data->config['version'] = $phpbb_version; // We always need to set the version afterwards
  398. break;
  399. }
  400. // Call init
  401. $data->init();
  402. }