/post_history/Subs-Install.php

https://github.com/nikop/PostHistory · PHP · 258 lines · 197 code · 46 blank · 15 comment · 57 complexity · 26bc944d0916b26e9e58fa25969b247f MD5 · raw file

  1. <?php
  2. if (!defined('SMF'))
  3. die('Hacking attempt...');
  4. function doTables($tables, $columnRename = array())
  5. {
  6. global $smcFunc, $db_prefix, $db_type, $db_show_debug;
  7. $log = array();
  8. $existingTables = $smcFunc['db_list_tables']();
  9. foreach ($tables as $table)
  10. {
  11. $table_name = $table['name'];
  12. $tableExists = in_array($db_prefix . $table_name, $existingTables);
  13. // Create table
  14. if (!$tableExists && empty($table['smf']))
  15. $smcFunc['db_create_table']('{db_prefix}' . $table_name, $table['columns'], $table['indexes']);
  16. // Update table
  17. else
  18. {
  19. $currentTable = $smcFunc['db_table_structure']('{db_prefix}' . $table_name);
  20. // Renames in this table?
  21. if (!empty($table['rename']))
  22. {
  23. foreach ($currentTable['columns'] as $column)
  24. {
  25. if (isset($table['rename'][$column['name']]))
  26. {
  27. $old_name = $column['name'];
  28. $column['name'] = $table['rename'][$column['name']];
  29. $smcFunc['db_change_column']('{db_prefix}' . $table_name, $old_name, $column);
  30. }
  31. }
  32. }
  33. // Global renames? (should be avoided)
  34. if (!empty($columnRename))
  35. {
  36. foreach ($currentTable['columns'] as $column)
  37. {
  38. if (isset($columnRename[$column['name']]))
  39. {
  40. $old_name = $column['name'];
  41. $column['name'] = $columnRename[$column['name']];
  42. $smcFunc['db_change_column']('{db_prefix}' . $table_name, $old_name, $column);
  43. }
  44. }
  45. }
  46. // Check that all columns are in
  47. foreach ($table['columns'] as $id => $col)
  48. {
  49. $exists = false;
  50. // TODO: Check that definition is correct
  51. foreach ($currentTable['columns'] as $col2)
  52. {
  53. if ($col['name'] === $col2['name'])
  54. {
  55. $exists = true;
  56. break;
  57. }
  58. }
  59. // Add missing columns
  60. if (!$exists)
  61. $smcFunc['db_add_column']('{db_prefix}' . $table_name, $col);
  62. }
  63. // Remove any unnecassary columns
  64. foreach ($currentTable['columns'] as $col)
  65. {
  66. $exists = false;
  67. foreach ($table['columns'] as $col2)
  68. {
  69. if ($col['name'] === $col2['name'])
  70. {
  71. $exists = true;
  72. break;
  73. }
  74. }
  75. if (!$exists && isset($table['upgrade']['columns'][$col['name']]))
  76. {
  77. if ($table['upgrade']['columns'][$col['name']] == 'drop')
  78. $smcFunc['db_remove_column']('{db_prefix}' . $table_name, $col['name']);
  79. }
  80. elseif (!$exists && !empty($db_show_debug) && empty($table['smf']))
  81. $log[] = sprintf('Table %s has non-required column %s', $table_name, $col['name']);
  82. }
  83. // Check that all indexes are in and correct
  84. foreach ($table['indexes'] as $id => $index)
  85. {
  86. $exists = false;
  87. foreach ($currentTable['indexes'] as $index2)
  88. {
  89. // Primary is special case
  90. if ($index['type'] == 'primary' && $index2['type'] == 'primary')
  91. {
  92. $exists = true;
  93. if ($index['columns'] !== $index2['columns'])
  94. {
  95. $smcFunc['db_remove_index']('{db_prefix}' . $table_name, 'primary');
  96. $smcFunc['db_add_index']('{db_prefix}' . $table_name, $index);
  97. }
  98. break;
  99. }
  100. // Make sure index is correct
  101. elseif (isset($index['name']) && isset($index2['name']) && $index['name'] == $index2['name'])
  102. {
  103. $exists = true;
  104. // Need to be changed?
  105. if ($index['type'] != $index2['type'] || $index['columns'] !== $index2['columns'])
  106. {
  107. $smcFunc['db_remove_index']('{db_prefix}' . $table_name, $index['name']);
  108. $smcFunc['db_add_index']('{db_prefix}' . $table_name, $index);
  109. }
  110. break;
  111. }
  112. }
  113. if (!$exists)
  114. $smcFunc['db_add_index']('{db_prefix}' . $table_name, $index);
  115. }
  116. // Remove unnecassary indexes
  117. foreach ($currentTable['indexes'] as $index)
  118. {
  119. $exists = false;
  120. foreach ($table['indexes'] as $index2)
  121. {
  122. // Primary is special case
  123. if ($index['type'] == 'primary' && $index2['type'] == 'primary')
  124. $exists = true;
  125. // Make sure index is correct
  126. elseif (isset($index['name']) && isset($index2['name']) && $index['name'] == $index2['name'])
  127. $exists = true;
  128. }
  129. if (!$exists)
  130. {
  131. if (isset($table['upgrade']['indexes']))
  132. {
  133. foreach ($table['upgrade']['indexes'] as $index2)
  134. {
  135. if ($index['type'] == 'primary' && $index2['type'] == 'primary' && $index['columns'] === $index2['columns'])
  136. $smcFunc['db_remove_index']('{db_prefix}' . $table_name, 'primary');
  137. elseif (isset($index['name']) && isset($index2['name']) && $index['name'] == $index2['name'] && $index['type'] == $index2['type'] && $index['columns'] === $index2['columns'])
  138. $smcFunc['db_remove_index']('{db_prefix}' . $table_name, $index['name']);
  139. elseif (!empty($db_show_debug))
  140. $log[] = $table_name . ' has Unneeded index ' . var_dump($index);
  141. }
  142. }
  143. elseif (!empty($db_show_debug))
  144. $log[] = $table_name . ' has Unneeded index ' . var_dump($index);
  145. }
  146. }
  147. }
  148. }
  149. if (!empty($log))
  150. log_error(implode('<br />', $log));
  151. return $log;
  152. }
  153. function doSettings($addSettings)
  154. {
  155. global $smcFunc, $modSettings;
  156. $update = array();
  157. foreach ($addSettings as $variable => $value)
  158. {
  159. list ($value, $overwrite) = $value;
  160. if ($overwrite || !isset($modSettings[$variable]))
  161. $update[$variable] = $value;
  162. }
  163. if (!empty($update))
  164. updateSettings($update);
  165. }
  166. function doPermission($permissions)
  167. {
  168. global $smcFunc;
  169. $perm = array();
  170. foreach ($permissions as $permission => $default)
  171. {
  172. $result = $smcFunc['db_query']('', '
  173. SELECT COUNT(*)
  174. FROM {db_prefix}permissions
  175. WHERE permission = {string:permission}',
  176. array(
  177. 'permission' => $permission
  178. )
  179. );
  180. list ($num) = $smcFunc['db_fetch_row']($result);
  181. if ($num == 0)
  182. {
  183. foreach ($default as $grp)
  184. $perm[] = array($grp, $permission);
  185. }
  186. }
  187. if (empty($perm))
  188. return;
  189. $smcFunc['db_insert']('insert',
  190. '{db_prefix}permissions',
  191. array(
  192. 'id_group' => 'int',
  193. 'permission' => 'string'
  194. ),
  195. $perm,
  196. array()
  197. );
  198. }
  199. function updateAdminFeatures($item, $enabled = false)
  200. {
  201. global $modSettings;
  202. $admin_features = isset($modSettings['admin_features']) ? explode(',', $modSettings['admin_features']) : array('cd,cp,k,w,rg,ml,pm');
  203. if (!is_array($item))
  204. $item = array($item);
  205. if ($enabled)
  206. $admin_features = array_merge($admin_features, $item);
  207. else
  208. $admin_features = array_diff($admin_features, $item);
  209. updateSettings(array('admin_features' => implode(',', $admin_features)));
  210. return true;
  211. }
  212. ?>