PageRenderTime 26ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/Portal_XL50_Premod-3.0.8/phpBB3/blog/upgrade/upgrade.php

https://github.com/Lucky65/phpBB-Portal-XL-5.0-italian
PHP | 526 lines | 401 code | 69 blank | 56 comment | 55 complexity | 3b5fbeb0b44cc2b3004e9e51fc87ac1a MD5 | raw file
  1. <?php
  2. /**
  3. *
  4. * @package phpBB3 User Blog
  5. * @version $Id: upgrade.php 485 2008-08-15 23:33:57Z exreaction@gmail.com $
  6. * @copyright (c) 2008 EXreaction, Lithium Studios
  7. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  8. *
  9. */
  10. if (!defined('IN_PHPBB'))
  11. {
  12. exit;
  13. }
  14. /**
  15. * Blog Upgrade Class
  16. */
  17. class blog_upgrade
  18. {
  19. var $available_upgrades = array();
  20. var $selected_options = array();
  21. /**
  22. * Load the available upgrade options
  23. */
  24. function blog_upgrade()
  25. {
  26. global $cache, $config, $phpbb_root_path, $phpEx, $user;
  27. if (!isset($config['user_blog_version']))
  28. {
  29. trigger_error(sprintf($user->lang['CLICK_INSTALL_BLOG'], '<a href="' . append_sid("{$phpbb_root_path}blog.$phpEx", 'page=install') . '">', '</a>'));
  30. }
  31. $this->selected_options = request_var('config', array('' => ''), true);
  32. if (!sizeof($this->selected_options))
  33. {
  34. $cache_data = $cache->get('_blog_upgrade');
  35. if ($cache_data !== false)
  36. {
  37. $this->selected_options = $cache_data;
  38. }
  39. }
  40. $dh = @opendir($phpbb_root_path . 'blog/upgrade/');
  41. if ($dh)
  42. {
  43. while (($file = readdir($dh)) !== false)
  44. {
  45. if ($file != "upgrade.$phpEx" && $file != "functions.$phpEx" && substr($file, -(strlen($phpEx) + 1)) === '.' . $phpEx)
  46. {
  47. $name = substr($file, 0, -(strlen($phpEx) + 1));
  48. $this->available_upgrades[$name] = array();
  49. include($phpbb_root_path . 'blog/upgrade/' . $file);
  50. }
  51. }
  52. closedir($dh);
  53. }
  54. return true;
  55. }
  56. /**
  57. * Cleans the blog tables
  58. */
  59. function clean_tables()
  60. {
  61. global $db;
  62. if ($this->selected_options['truncate'])
  63. {
  64. $sql_array[] = 'TRUNCATE TABLE ' . BLOGS_TABLE;
  65. $sql_array[] = 'TRUNCATE TABLE ' . BLOGS_ATTACHMENT_TABLE;
  66. $sql_array[] = 'TRUNCATE TABLE ' . BLOGS_IN_CATEGORIES_TABLE;
  67. $sql_array[] = 'TRUNCATE TABLE ' . BLOGS_POLL_OPTIONS_TABLE;
  68. $sql_array[] = 'TRUNCATE TABLE ' . BLOGS_POLL_VOTES_TABLE;
  69. $sql_array[] = 'TRUNCATE TABLE ' . BLOGS_RATINGS_TABLE;
  70. $sql_array[] = 'TRUNCATE TABLE ' . BLOGS_REPLY_TABLE;
  71. $sql_array[] = 'TRUNCATE TABLE ' . BLOGS_SUBSCRIPTION_TABLE;
  72. $sql_array[] = 'TRUNCATE TABLE ' . BLOG_SEARCH_WORDLIST_TABLE;
  73. $sql_array[] = 'TRUNCATE TABLE ' . BLOG_SEARCH_WORDMATCH_TABLE;
  74. //$sql_array[] = 'TRUNCATE TABLE ' . BLOG_SEARCH_RESULTS_TABLE;
  75. foreach ($sql_array as $sql)
  76. {
  77. $db->sql_query($sql);
  78. }
  79. unset($sql_array);
  80. }
  81. }
  82. /**
  83. * Outputs the list of available upgrade options
  84. */
  85. function output_available_list()
  86. {
  87. global $template, $phpbb_root_path, $phpEx, $user;
  88. foreach($this->available_upgrades as $name => $data)
  89. {
  90. $template->assign_block_vars('convertors', array(
  91. 'SOFTWARE' => $data['upgrade_title'],
  92. 'VERSION' => $data['upgrade_version'],
  93. 'AUTHOR' => $data['upgrade_copyright'],
  94. 'U_CONVERT' => append_sid("{$phpbb_root_path}blog.$phpEx", 'page=upgrade&amp;stage=1&amp;mode=' . $name),
  95. )
  96. );
  97. }
  98. }
  99. /**
  100. * Get Options
  101. */
  102. function get_options($name)
  103. {
  104. global $dbhost, $dbport, $dbname, $table_prefix;
  105. $options = array(
  106. 'legend0' => 'DB_CONFIG',
  107. 'db_host' => array('lang' => 'DB_HOST', 'type' => 'text:25:100', 'explain' => true, 'default' => $dbhost),
  108. 'db_port' => array('lang' => 'DB_PORT', 'type' => 'text:25:100', 'explain' => true, 'default' => $dbport),
  109. 'db_name' => array('lang' => 'DB_NAME', 'type' => 'text:25:100', 'explain' => false, 'default' => $dbname),
  110. 'db_user' => array('lang' => 'DB_USERNAME', 'type' => 'text:25:100', 'explain' => false, 'default' => ''),
  111. 'db_password' => array('lang' => 'DB_PASSWORD', 'type' => 'password:25:100', 'explain' => false, 'default' => ''),
  112. 'db_prefix' => array('lang' => 'TABLE_PREFIX', 'type' => 'text:25:100', 'explain' => false, 'default' => $table_prefix),
  113. 'limit' => array('lang' => 'LIMIT', 'type' => 'text:25:100', 'explain' => true, 'default' => 250),
  114. 'legend1' => 'STAGE_ADVANCED',
  115. 'truncate' => array('lang' => 'TRUNCATE_TABLES', 'type' => 'radio:yes_no', 'explain' => true, 'default' => true),
  116. 'blogs' => array('lang' => 'UPGRADE_BLOGS', 'type' => 'radio:yes_no', 'explain' => false, 'default' => true),
  117. );
  118. if (isset($this->available_upgrades[$name]['custom_options']))
  119. {
  120. $options = array_merge($options, $this->available_upgrades[$name]['custom_options']);
  121. }
  122. return $options;
  123. }
  124. /**
  125. * Output Upgrade Options
  126. *
  127. * @param string $name - The name of the upgrade script we want to show the custom upgrade options for
  128. */
  129. function output_upgrade_options($name)
  130. {
  131. global $user, $template;
  132. if (!isset($this->available_upgrades[$name]))
  133. {
  134. trigger_error('NO_MODE');
  135. }
  136. $options = $this->get_options($name);
  137. // this code is mostly from acp_board.php
  138. $new_config = array();
  139. foreach ($options as $config_key => $vars)
  140. {
  141. if (!is_array($vars) && strpos($config_key, 'legend') === false)
  142. {
  143. continue;
  144. }
  145. if (strpos($config_key, 'legend') !== false)
  146. {
  147. $template->assign_block_vars('options', array(
  148. 'S_LEGEND' => true,
  149. 'LEGEND' => (isset($user->lang[$vars])) ? $user->lang[$vars] : $vars)
  150. );
  151. continue;
  152. }
  153. $type = explode(':', $vars['type']);
  154. $l_explain = '';
  155. if ($vars['explain'] && isset($vars['lang_explain']))
  156. {
  157. $l_explain = (isset($user->lang[$vars['lang_explain']])) ? $user->lang[$vars['lang_explain']] : $vars['lang_explain'];
  158. }
  159. else if ($vars['explain'])
  160. {
  161. $l_explain = (isset($user->lang[$vars['lang'] . '_EXPLAIN'])) ? $user->lang[$vars['lang'] . '_EXPLAIN'] : '';
  162. }
  163. $new_config[$config_key] = (isset($vars['default'])) ? $vars['default'] : '';
  164. $template->assign_block_vars('options', array(
  165. 'KEY' => $config_key,
  166. 'TITLE' => (isset($user->lang[$vars['lang']])) ? $user->lang[$vars['lang']] : $vars['lang'],
  167. 'S_EXPLAIN' => $vars['explain'],
  168. 'TITLE_EXPLAIN' => $l_explain,
  169. 'CONTENT' => build_cfg_template($type, $config_key, $new_config, $config_key, $vars),
  170. )
  171. );
  172. }
  173. }
  174. /**
  175. * Connect to the old database
  176. */
  177. function old_db_connect()
  178. {
  179. global $old_db, $sql_db, $user;
  180. $old_db = new $sql_db();
  181. $old_db->sql_connect($this->selected_options['db_host'], $this->selected_options['db_user'], $this->selected_options['db_password'], $this->selected_options['db_name'], $this->selected_options['db_port']);
  182. }
  183. /**
  184. * Confirm Upgrade Options
  185. */
  186. function confirm_upgrade_options($name, &$error)
  187. {
  188. global $cache, $sql_db, $user, $old_db;
  189. if (!isset($this->available_upgrades[$name]))
  190. {
  191. trigger_error('NO_MODE');
  192. }
  193. $options = $this->get_options($name);
  194. $default_options = array();
  195. foreach ($options as $config_key => $vars)
  196. {
  197. $default_options[$config_key] = (isset($vars['default'])) ? $vars['default'] : '';
  198. }
  199. $this->selected_options = array_merge($default_options, $this->selected_options);
  200. $this->selected_options['limit'] = intval($this->selected_options['limit']);
  201. if ($this->selected_options['limit'] < 1)
  202. {
  203. $error[] = $user->lang['LIMIT_INCORRECT'];
  204. }
  205. connect_check_db(true, $error, array('DRIVER' => substr($sql_db, 5)), $this->selected_options['db_prefix'], $this->selected_options['db_host'],$this->selected_options['db_user'], $this->selected_options['db_password'], $this->selected_options['db_name'], $this->selected_options['db_port'], false, false);
  206. if (sizeof($error) == 1 && $error[0] == '')
  207. {
  208. $error = array();
  209. }
  210. if (!sizeof($error))
  211. {
  212. $this->old_db_connect();
  213. $old_db->sql_return_on_error(true);
  214. foreach ($this->available_upgrades[$name]['requred_tables'] as $table)
  215. {
  216. $sql = 'SELECT * FROM ' . $this->selected_options['db_prefix'] . $table . ' LIMIT 1';
  217. if (!$old_db->sql_query($sql))
  218. {
  219. $error[] = sprintf($user->lang['DB_TABLE_NOT_EXIST'], $this->selected_options['db_prefix'] . $table);
  220. }
  221. }
  222. }
  223. if (!sizeof($error))
  224. {
  225. // put the options in the cache for the upgrade
  226. $cache->put('_blog_upgrade', $this->selected_options);
  227. return true;
  228. }
  229. return false;
  230. }
  231. /**
  232. * Run the requested blog upgrade script
  233. *
  234. * @param string $name - The name of the upgrade script we want to show the custom upgrade options for
  235. */
  236. function run_upgrade($name)
  237. {
  238. global $phpbb_root_path, $phpEx, $old_db, $db, $config, $user, $auth, $cache;
  239. global $part, $part_cnt, $section, $section_cnt;
  240. if (!isset($this->available_upgrades[$name]))
  241. {
  242. trigger_error('NO_MODE');
  243. }
  244. $run_upgrade = true; // checked in the file
  245. include($phpbb_root_path . 'blog/upgrade/' . $name . '.' . $phpEx);
  246. }
  247. /**
  248. * Reindex the blogs/replies
  249. */
  250. function reindex($mode = '')
  251. {
  252. global $db, $user, $phpbb_root_path, $phpEx;
  253. global $part, $part_cnt, $section, $section_cnt;
  254. $section_cnt = 1;
  255. $blog_search = setup_blog_search();
  256. if ($mode == 'delete')
  257. {
  258. $blog_search->delete_index();
  259. }
  260. else
  261. {
  262. if ($section == 0)
  263. {
  264. $sql = 'SELECT * FROM ' . BLOGS_TABLE . '
  265. WHERE blog_deleted = 0
  266. AND blog_approved = 1
  267. ORDER BY blog_id DESC';
  268. $result = $db->sql_query_limit($sql, $this->selected_options['limit'], ($part * $this->selected_options['limit']));
  269. while ($row = $db->sql_fetchrow($result))
  270. {
  271. $blog_search->index('add', $row['blog_id'], 0, $row['blog_text'], $row['blog_subject'], $row['user_id']);
  272. }
  273. $sql = 'SELECT count(blog_id) AS cnt FROM ' . BLOGS_TABLE . '
  274. WHERE blog_deleted = 0
  275. AND blog_approved = 1';;
  276. $result = $db->sql_query($sql);
  277. $cnt = $db->sql_fetchrow($result);
  278. if ($cnt['cnt'] >= (($part + 1) * $this->selected_options['limit']))
  279. {
  280. $part++;
  281. $part_cnt = ceil($cnt['cnt'] / $this->selected_options['limit']);
  282. }
  283. else
  284. {
  285. $part = 0;
  286. $section++;
  287. }
  288. }
  289. else
  290. {
  291. $sql = 'SELECT * FROM ' . BLOGS_REPLY_TABLE . '
  292. WHERE reply_deleted = 0
  293. AND reply_approved = 1
  294. ORDER BY reply_id DESC';
  295. $result = $db->sql_query_limit($sql, $this->selected_options['limit'], ($part * $this->selected_options['limit']));
  296. while ($row = $db->sql_fetchrow($result))
  297. {
  298. $blog_search->index('add', $row['blog_id'], $row['reply_id'], $row['reply_text'], $row['reply_subject'], $row['user_id']);
  299. }
  300. $sql = 'SELECT count(reply_id) AS cnt FROM ' . BLOGS_REPLY_TABLE . '
  301. WHERE reply_deleted = 0
  302. AND reply_approved = 1';
  303. $result = $db->sql_query($sql);
  304. $cnt = $db->sql_fetchrow($result);
  305. if ($cnt['cnt'] >= (($part + 1) * $this->selected_options['limit']))
  306. {
  307. $part++;
  308. $part_cnt = ceil($cnt['cnt'] / $this->selected_options['limit']);
  309. }
  310. else
  311. {
  312. $part = 0;
  313. $section++;
  314. }
  315. }
  316. }
  317. }
  318. /**
  319. * Resync the data
  320. */
  321. function resync()
  322. {
  323. global $config, $db, $user;
  324. global $part, $part_cnt, $section, $section_cnt;
  325. $blog_data = array();
  326. $start = ($part * $this->selected_options['limit']);
  327. $limit = $this->selected_options['limit'];
  328. $section_cnt = 3;
  329. // Start by selecting all blog data that we will use
  330. $sql = 'SELECT blog_id, blog_reply_count, blog_real_reply_count FROM ' . BLOGS_TABLE . ' ORDER BY blog_id ASC';
  331. $result = $db->sql_query($sql);
  332. while($row = $db->sql_fetchrow($result))
  333. {
  334. $blog_data[$row['blog_id']] = $row;
  335. }
  336. $db->sql_freeresult($result);
  337. $blog_count = sizeof($blog_data);
  338. $i=0;
  339. switch ($section)
  340. {
  341. case 0 :
  342. foreach($blog_data as $row)
  343. {
  344. if ($i < $start)
  345. {
  346. continue;
  347. }
  348. if ($i > ($start + $limit))
  349. {
  350. break;
  351. }
  352. // count all the replies (an SQL query seems the easiest way to do it)
  353. $sql = 'SELECT count(reply_id) AS total
  354. FROM ' . BLOGS_REPLY_TABLE . '
  355. WHERE blog_id = ' . $row['blog_id'] . '
  356. AND reply_deleted = 0
  357. AND reply_approved = 1';
  358. $result = $db->sql_query($sql);
  359. $total = $db->sql_fetchrow($result);
  360. $db->sql_freeresult($result);
  361. if ($total['total'] != $row['blog_reply_count'])
  362. {
  363. // Update the reply count
  364. $sql = 'UPDATE ' . BLOGS_TABLE . ' SET blog_reply_count = ' . $total['total'] . ' WHERE blog_id = ' . $row['blog_id'];
  365. $db->sql_query($sql);
  366. }
  367. }
  368. break;
  369. case 1 :
  370. foreach($blog_data as $row)
  371. {
  372. if ($i < $start)
  373. {
  374. continue;
  375. }
  376. if ($i > ($start + $limit))
  377. {
  378. break;
  379. }
  380. // count all the replies (an SQL query seems the easiest way to do it)
  381. $sql = 'SELECT count(reply_id) AS total
  382. FROM ' . BLOGS_REPLY_TABLE . '
  383. WHERE blog_id = ' . $row['blog_id'];
  384. $result = $db->sql_query($sql);
  385. $total = $db->sql_fetchrow($result);
  386. $db->sql_freeresult($result);
  387. if ($total['total'] != $row['blog_real_reply_count'])
  388. {
  389. // Update the reply count
  390. $sql = 'UPDATE ' . BLOGS_TABLE . ' SET blog_real_reply_count = ' . $total['total'] . ' WHERE blog_id = ' . $row['blog_id'];
  391. $db->sql_query($sql);
  392. }
  393. }
  394. break;
  395. case 2 :
  396. // select the users data we will need
  397. $sql = 'SELECT user_id, blog_count FROM ' . USERS_TABLE . ' ORDER BY user_id DESC';
  398. $result = $db->sql_query($sql);
  399. while($row = $db->sql_fetchrow($result))
  400. {
  401. if ($i < $start)
  402. {
  403. continue;
  404. }
  405. if ($i > ($start + $limit))
  406. {
  407. break;
  408. }
  409. // count all the replies (an SQL query seems the easiest way to do it)
  410. $sql2 = 'SELECT count(blog_id) AS total
  411. FROM ' . BLOGS_TABLE . '
  412. WHERE user_id = ' . $row['user_id'] . '
  413. AND blog_deleted = 0
  414. AND blog_approved = 1';
  415. $result2 = $db->sql_query($sql2);
  416. $total = $db->sql_fetchrow($result2);
  417. $db->sql_freeresult($result2);
  418. if ($total['total'] != $row['blog_count'])
  419. {
  420. // Update the reply count
  421. $sql = 'UPDATE ' . USERS_TABLE . ' SET blog_count = ' . $total['total'] . ' WHERE user_id = ' . $row['user_id'];
  422. $db->sql_query($sql);
  423. }
  424. }
  425. $db->sql_freeresult($result);
  426. break;
  427. }
  428. if ($blog_count >= (($part + 1) * $this->selected_options['limit']))
  429. {
  430. $part++;
  431. $part_cnt = ceil($blog_count / $this->selected_options['limit']);
  432. }
  433. else
  434. {
  435. $sql = 'SELECT blog_id FROM ' . BLOGS_TABLE . ' WHERE blog_deleted = 0 AND blog_approved = 1';
  436. $result = $db->sql_query($sql);
  437. while ($row = $db->sql_fetchrow($result))
  438. {
  439. $ids[] = $row['blog_id'];
  440. }
  441. set_config('num_blogs', sizeof($ids), true);
  442. if (sizeof($ids))
  443. {
  444. $sql = 'SELECT count(reply_id) AS reply_count FROM ' . BLOGS_REPLY_TABLE . '
  445. WHERE reply_deleted = 0
  446. AND reply_approved = 1
  447. AND ' . $db->sql_in_set('blog_id', $ids); // Make sure we only count the # of replies from non-deleted blogs.
  448. $result = $db->sql_query($sql);
  449. $row = $db->sql_fetchrow($result);
  450. set_config('num_blog_replies', $row['reply_count'], true);
  451. }
  452. $part = 0;
  453. $section++;
  454. }
  455. }
  456. }
  457. ?>