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

/e107_plugins/forum/forum_update.php

https://github.com/CasperGemini/e107
PHP | 1743 lines | 1308 code | 245 blank | 190 comment | 98 complexity | e3b5e877338f33f0cccdc2b4e7c0fb23 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /*
  3. * e107 website system
  4. *
  5. * Copyright (C) 2008-2013 e107 Inc (e107.org)
  6. * Released under the terms and conditions of the
  7. * GNU General Public License (http://www.gnu.org/licenses/gpl.txt)
  8. *
  9. * Forum upgrade routines
  10. *
  11. */
  12. define('e_ADMIN_AREA', true);
  13. require_once ('../../class2.php');
  14. if (!getperms('P'))
  15. {
  16. header('location:' . e_BASE . 'index.php');
  17. exit ;
  18. }
  19. error_reporting(E_ALL);
  20. require_once (e_PLUGIN . 'forum/forum_class.php');
  21. require_once (e_ADMIN . 'auth.php');
  22. if (e_QUERY == "reset")
  23. {
  24. unset($_SESSION['forumUpgrade']);
  25. unset($_SESSION['forumupdate']);
  26. }
  27. //unset($_SESSION['forumupdate']['thread_last']);
  28. // unset($_SESSION['forumupdate']['thread_count']);
  29. $forum = new e107forum(true);
  30. $timestart = microtime();
  31. $f = new forumUpgrade;
  32. $sql = e107::getDb();
  33. if($_GET['reset'])
  34. {
  35. unset($_SESSION['forumUpgrade']);
  36. unset($_SESSION['forumupdate']);
  37. $f -> updateInfo['currentStep'] = intval($_GET['reset']);
  38. $f -> setUpdateInfo();
  39. }
  40. if (e_AJAX_REQUEST)
  41. {
  42. if (!vartrue($_GET['mode']))
  43. {
  44. echo "data-progress-mode not set!";
  45. exit ;
  46. }
  47. $func = 'step' . intval($_GET['mode']) . "_ajax";
  48. if (function_exists($func))
  49. {
  50. call_user_func($func);
  51. }
  52. else
  53. {
  54. echo $func . "() doesn't exist!";
  55. }
  56. exit ;
  57. }
  58. $upgradeNeeded = $f -> checkUpdateNeeded();
  59. $upgradeNeeded = true;
  60. if (!$upgradeNeeded)
  61. {
  62. $mes = e107::getMessage();
  63. $mes -> addInfo("The forum is already at the most recent version, no upgrade is required");
  64. $ns -> tablerender('Forum Upgrade', $mes -> render());
  65. require (e_ADMIN . 'footer.php');
  66. exit ;
  67. }
  68. if (isset($_POST) && count($_POST))
  69. {
  70. if (isset($_POST['skip_attach']))
  71. {
  72. $f -> updateInfo['skip_attach'] = 1;
  73. $f -> updateInfo['currentStep'] = 2;
  74. $f -> setUpdateInfo();
  75. }
  76. if (isset($_POST['nextStep']))
  77. {
  78. $tmp = array_keys($_POST['nextStep']);
  79. $f -> updateInfo['currentStep'] = $tmp[0];
  80. $f -> setUpdateInfo();
  81. }
  82. }
  83. $currentStep = (isset($f -> updateInfo['currentStep']) ? $f -> updateInfo['currentStep'] : 1);
  84. $stepParms = (isset($stepParms) ? $stepParms : '');
  85. //echo "currentStep = $currentStep <br />";
  86. if (function_exists('step' . $currentStep))
  87. {
  88. $result = call_user_func('step' . $currentStep, $stepParms);
  89. }
  90. require (e_ADMIN . 'footer.php');
  91. exit ;
  92. function step1()
  93. {
  94. global $f;
  95. $f -> updateInfo['currentStep'] = 1;
  96. $f -> setUpdateInfo();
  97. $mes = e107::getMessage();
  98. //Check attachment dir permissions
  99. if (!isset($f -> updateInfo['skip_attach']))
  100. {
  101. $f -> checkAttachmentDirs();
  102. if (isset($f -> error['attach']))
  103. {
  104. $text = "
  105. <h3>ERROR:</h3>
  106. The following errors have occured. These issues must be resolved if you ever want to enable attachment or image uploading in your forums. <br />If you do not ever plan on enabling this setting in your forum, you may click the 'skip' button <br /><br />
  107. ";
  108. foreach ($f->error['attach'] as $e)
  109. {
  110. $text .= '** ' . $e . '<br />';
  111. }
  112. $text .= "
  113. <br />
  114. <form method='post' action='" . e_SELF . "?step=2'>
  115. <input class='btn' type='submit' name='retest_attach' value='Retest Permissions' />
  116. &nbsp;&nbsp;&nbsp;
  117. <input class='btn btn-success' type='submit' name='skip_attach' value='Skip - I understand the risks' />
  118. </form>
  119. ";
  120. }
  121. else
  122. {
  123. $mes -> addSuccess("Attachment and attachment/thumb directories are writable");
  124. $text = "<form method='post' action='" . e_SELF . "?step=2'>
  125. <input class='btn btn-success' type='submit' name='nextStep[2]' value='Proceed to step 2' />
  126. </form>
  127. ";
  128. }
  129. e107::getRender() -> tablerender('Step 1: Attachment directory permissions', $mes -> render() . $text);
  130. }
  131. }
  132. function step2()
  133. {
  134. $mes = e107::getMessage();
  135. $ns = e107::getRender();
  136. if (!isset($_POST['create_tables']))
  137. {
  138. $text = "
  139. This step will create the new forum_thread, forum_post, and forum_attach tables. It will also create a forum_new table that will become the 'real' forum table once the data from the current table is migrated.
  140. <br /><br />
  141. <form method='post'>
  142. <input class='btn btn-success' data-loading-text='Please wait...' type='submit' name='create_tables' value='Proceed with table creation' />
  143. </form>
  144. ";
  145. $ns -> tablerender('Step 2: Forum table creation', $text);
  146. return;
  147. }
  148. // FIXME - use db_verify. ??
  149. require_once (e_HANDLER . 'db_table_admin_class.php');
  150. $db = new db_table_admin;
  151. $tabList = array(
  152. 'forum' => 'forum_new',
  153. 'forum_thread' => '',
  154. 'forum_post' => '',
  155. 'forum_track' => ''
  156. );
  157. //
  158. $ret = '';
  159. $failed = false;
  160. $text = '';
  161. foreach ($tabList as $name => $rename)
  162. {
  163. $message = 'Creating table ' . ($rename ? $rename : $name);
  164. $result = $db -> createTable(e_PLUGIN . 'forum/forum_sql.php', $name, true, $rename);
  165. if ($result === true)
  166. {
  167. $mes -> addSuccess($message);
  168. // $text .= 'Success <br />';
  169. }
  170. elseif ($result !== true)
  171. {
  172. // $text .= 'Failed <br />';
  173. $mes -> addError($message);
  174. $failed = true;
  175. }
  176. }
  177. if ($failed)
  178. {
  179. $mes -> addError("Creation of table(s) failed. You can not continue until these are created successfully!");
  180. }
  181. else
  182. {
  183. $text = "<form method='post' action='" . e_SELF . "?step=3'>
  184. <input class='btn' type='submit' name='nextStep[3]' value='Proceed to step 3' />
  185. </form>";
  186. }
  187. $ns -> tablerender('Step 2: Forum table creation', $mes -> render() . $text);
  188. }
  189. // FIXME - use e107::getPlugin()->manage_extended_field('add', $name, $attrib,
  190. // $source)
  191. function step3()
  192. {
  193. $ns = e107::getRender();
  194. $mes = e107::getMessage();
  195. $stepCaption = 'Step 3: Extended user field creation';
  196. if (!isset($_POST['create_extended']))
  197. {
  198. $text = "
  199. This step will create the new extended user fields required for the new forum code: <br />
  200. <ul>
  201. <li>user_plugin_forum_posts (to track number of posts for each user)</li>
  202. <li>user_plugin_forum_viewed (to track threads viewed by each user</li>
  203. </ul>
  204. <br /><br />
  205. <form method='post'>
  206. <input class='btn btn-success' data-loading-text='Please wait...' type='submit' name='create_extended' value='Proceed with field creation' />
  207. </form>
  208. ";
  209. $ns -> tablerender($stepCaption, $text);
  210. return;
  211. }
  212. require_once (e_HANDLER . 'user_extended_class.php');
  213. $ue = new e107_user_extended;
  214. $fieldList = array(
  215. 'plugin_forum_posts' => EUF_INTEGER,
  216. 'plugin_forum_viewed' => EUF_TEXTAREA
  217. );
  218. $failed = false;
  219. foreach ($fieldList as $fieldName => $fieldType)
  220. {
  221. $result = $ue -> user_extended_add_system($fieldName, $fieldType);
  222. if ($result === true)
  223. {
  224. $mes -> addSuccess('Creating extended user field user_' . $fieldName);
  225. }
  226. else
  227. {
  228. $mes -> addError('Creating extended user field user_' . $fieldName);
  229. $failed = true;
  230. }
  231. }
  232. if ($failed)
  233. {
  234. $mes -> addError("Creation of extended field(s) failed. You can not continue until these are create successfully!");
  235. }
  236. else
  237. {
  238. $text .= "
  239. <form method='post' action='" . e_SELF . "?step=4'>
  240. <input class='btn btn-success' type='submit' name='nextStep[4]' value='Proceed to step 4' />
  241. </form>
  242. ";
  243. }
  244. $ns -> tablerender($stepCaption, $mes -> render() . $text);
  245. }
  246. function step4()
  247. {
  248. global $pref;
  249. $mes = e107::getMessage();
  250. $ns = e107::getRender();
  251. $stepCaption = 'Step 4: Move user specific forum data and forum prefs';
  252. if (!isset($_POST['move_user_data']))
  253. {
  254. $text = "
  255. This step will move the main forum preferences into its own table row. It will also move all user_viewed data from user table into the user extended table.<br />
  256. The user_forum field data will not be moved, as it will be recalculated later.<br />
  257. <br />
  258. Depending on the size of your user table, this step could take a while.
  259. <br /><br />
  260. <form method='post'>
  261. <input class='btn btn-success' data-loading-text='Please wait...' type='submit' name='move_user_data' value='Proceed with user data move' />
  262. </form>
  263. ";
  264. $ns -> tablerender($stepCaption, $text);
  265. return;
  266. }
  267. /** Convert forum prefs to their own row **/
  268. $fconf = e107::getPlugConfig('forum', '', false);
  269. $coreConfig = e107::getConfig();
  270. $old_prefs = array();
  271. foreach ($pref as $k => $v)
  272. {
  273. if (substr($k, 0, 6) == 'forum_')
  274. {
  275. $nk = substr($k, 6);
  276. $mes -> addDebug("Converting $k to $nk");
  277. $old_prefs[$nk] = $v;
  278. $coreConfig -> remove($k);
  279. }
  280. }
  281. $forumPrefList = array(
  282. 'reported_post_email',
  283. 'email_notify',
  284. 'email_notify_on'
  285. );
  286. foreach ($forumPrefList as $_fp)
  287. {
  288. $mes -> addDebug("converting $_fp to $_fp");
  289. $old_prefs[$_fp] = $coreConfig -> get($_fp);
  290. $coreConfig -> remove($_fp);
  291. }
  292. $fconf -> setPref($old_prefs) -> save(false, true);
  293. $coreConfig -> save(false, true);
  294. $result = array(
  295. 'usercount' => 0,
  296. 'viewcount' => 0,
  297. 'trackcount' => 0
  298. );
  299. $db = new db;
  300. if ($db -> select('user', 'user_id, user_viewed, user_realm', "user_viewed != '' OR user_realm != ''"))
  301. {
  302. require_once (e_HANDLER . 'user_extended_class.php');
  303. $ue = new e107_user_extended;
  304. while ($row = $db -> fetch(MYSQL_ASSOC))
  305. {
  306. $result['usercount']++;
  307. $userId = (int)$row['user_id'];
  308. $viewed = $row['user_viewed'];
  309. $viewed = trim($viewed, '.');
  310. $tmp = preg_split('#\.+#', $viewed);
  311. $viewed = implode(',', $tmp);
  312. $realm = $row['user_realm'];
  313. $realm = str_replace('USERREALM', '', $realm);
  314. $realm = trim($realm, '-.');
  315. $trackList = preg_split('#\D+#', $realm);
  316. $debug = 'user_id = ' . $userId . '<br />';
  317. $debug .= 'viewed = ' . $viewed . '<br />';
  318. $debug .= 'realm = ' . $realm . '<br />';
  319. $debug .= 'tracking = ' . implode(',', $trackList) . '<br />';
  320. // $debug .= print_a($trackList, true);
  321. // $mes -> addDebug($debug);
  322. if ($viewed != '')
  323. {
  324. $ue -> user_extended_setvalue($userId, 'plugin_forum_viewed', mysql_real_escape_string($viewed));
  325. $result['viewcount']++;
  326. }
  327. if (is_array($trackList) && count($trackList))
  328. {
  329. foreach ($trackList as $threadId)
  330. {
  331. $result['trackcount']++;
  332. $threadId = (int)$threadId;
  333. if ($threadId > 0)
  334. {
  335. $tmp = array();
  336. $tmp['track_userid'] = $userId;
  337. $tmp['track_thread'] = $threadId;
  338. e107::getDb() -> insert('forum_track', $tmp);
  339. }
  340. }
  341. }
  342. }
  343. }
  344. $mes -> addSuccess("User data move results:
  345. <ul>
  346. <li>Number of users processed: {$result['usercount']} </li>
  347. <li>Number of viewed data processed: {$result['viewcount']} </li>
  348. <li>Number of tracked records added: {$result['trackcount']} </li>
  349. </ul>
  350. ");
  351. $text = "<form method='post' action='" . e_SELF . "?step=5'>
  352. <input class='btn btn-success' type='submit' name='nextStep[5]' value='Proceed to step 5' />
  353. </form>";
  354. $ns -> tablerender($stepCaption, $mes -> render() . $text);
  355. }
  356. function step5()
  357. {
  358. $sql = e107::getDb();
  359. $ns = e107::getRender();
  360. $mes = e107::getMessage();
  361. $stepCaption = 'Step 5: Migrate forum data';
  362. if (!isset($_POST['move_forum_data']))
  363. {
  364. $text = "This step will copy all of your forum configuration from the `forum` table into the `forum_new` table.<br />
  365. Once the information is successfully copied, the existing 1.0 forum table will be renamed `forum_old` and the newly created `forum_new` table will be renamed `forum`.<br />
  366. <br /><br />";
  367. $text .= "
  368. <form method='post'>
  369. <input class='btn btn-success' data-loading-text='Please wait...' type='submit' name='move_forum_data' value='Proceed with forum data move' />
  370. </form>
  371. ";
  372. $ns -> tablerender($stepCaption, $mes -> render() . $text);
  373. return;
  374. }
  375. $counts = array(
  376. 'parents' => 0,
  377. 'forums' => 0,
  378. 'subs' => 0
  379. );
  380. //XXX Typo on 'parents' ?
  381. if ($sql -> select('forum'))
  382. {
  383. $forumList = $sql -> db_getList();
  384. foreach ($forumList as $forum)
  385. {
  386. if ($forum['forum_parent'] == 0)
  387. {
  388. $counts['parents']++;
  389. }
  390. elseif ($forum['forum_sub'] != 0)
  391. {
  392. $counts['subs']++;
  393. }
  394. else
  395. {
  396. $counts['forums']++;
  397. }
  398. $tmp = $forum;
  399. $tmp['forum_threadclass'] = $tmp['forum_postclass'];
  400. $tmp['forum_options'] = '_NULL_';
  401. // $tmp['_FIELD_TYPES'] = $ftypes['_FIELD_TYPES'];
  402. if ($sql -> insert('forum_new', $tmp))
  403. {
  404. }
  405. else
  406. {
  407. $mes -> addError("Insert failed on " . print_a($tmp, true));
  408. }
  409. }
  410. $mes -> addSuccess("
  411. Forum data move results:
  412. <ul>
  413. <li>Number of forum parents processed: {$counts['parents']} </li>
  414. <li>Number of forums processed: {$counts['forums']} </li>
  415. <li>Number of sub forums processed: {$counts['subs']} </li>
  416. </ul>
  417. ");
  418. $result = $sql -> gen('RENAME TABLE `#forum` TO `#forum_old` ') ? e_MESSAGE_SUCCESS : E_MESSAGE_ERROR;
  419. $mes -> add("Renaming forum to forum_old", $result);
  420. $result = $sql -> gen('RENAME TABLE `#forum_new` TO `#forum` ') ? e_MESSAGE_SUCCESS : E_MESSAGE_ERROR;
  421. $mes -> add("Renaming forum_new to forum", $result);
  422. $text = "
  423. <form method='post' action='" . e_SELF . "?step=6'>
  424. <input class='btn btn-success' type='submit' name='nextStep[6]' value='Proceed to step 6' />
  425. </form>
  426. ";
  427. $ns -> tablerender($stepCaption, $mes -> render() . $text);
  428. }
  429. }
  430. function step6()
  431. {
  432. $sql = e107::getDb();
  433. $ns = e107::getRender();
  434. $mes = e107::getMessage();
  435. $stepCaption = 'Step 6: Thread and post data';
  436. $_SESSION['forumupdate']['thread_total'] = $sql -> count('forum_t', '(*)', "WHERE thread_parent = 0");
  437. $_SESSION['forumupdate']['thread_count'] = 0;
  438. $_SESSION['forumupdate']['thread_last'] = 0;
  439. $text = "This step will copy all of your existing forum threads and posts into the new `forum_thread` and `forum_post` tables.<br />
  440. Depending on your forum size and speed of server, this could take some time.<br /><br /> ";
  441. $text .= renderProgress("Begin thread data move", 6);
  442. $ns->tablerender($stepCaption, $mes -> render() . $text);
  443. }
  444. function renderProgress($caption, $step)
  445. {
  446. if(!$step)
  447. {
  448. return "No step entered in function";
  449. }
  450. $thisStep = 'step'.$step;
  451. $nextStep = 'step'.($step + 1);
  452. $text = '
  453. <div class="row-fluid">
  454. <div class="span9 well">
  455. <div class="progress progress-success progress-striped active" id="progressouter">
  456. <div class="bar" id="progress"></div>
  457. </div>
  458. <a id="'.$thisStep.'" data-loading-text="Please wait..." data-progress="' . e_SELF . '" data-progress-mode="'.$step.'" data-progress-show="'.$nextStep.'" data-progress-hide="'.$thisStep.'" class="btn btn-primary e-progress" >'.$caption.'</a>
  459. </div>
  460. </div>';
  461. $text .= "<form method='post' action='" . e_SELF . "?step=".($step+1)."'>
  462. <input id='".$nextStep."' style='display:none' class='btn btn-success' type='submit' name='nextStep[".($step+1)."]' value='Proceed to step ".($step+1)."' />
  463. </form>";
  464. return $text;
  465. }
  466. function step6_ajax()
  467. {
  468. global $f;
  469. $sql = e107::getDb();
  470. $lastThread = vartrue($_SESSION['forumupdate']['thread_last'], 0);
  471. $qry = "
  472. SELECT thread_id FROM `#forum_t`
  473. WHERE thread_parent = 0
  474. AND thread_id > {$lastThread}
  475. ORDER BY thread_id ASC
  476. LIMIT 0, 300
  477. ";
  478. if ($sql -> gen($qry))
  479. {
  480. $threadList = $sql -> db_getList();
  481. foreach ($threadList as $t)
  482. {
  483. $id = (int)$t['thread_id'];
  484. $result = $f -> migrateThread($id);
  485. if ($result === false)
  486. {
  487. echo "Error";
  488. }
  489. else
  490. {
  491. $_SESSION['forumupdate']['thread_last'] = $id;
  492. $_SESSION['forumupdate']['thread_count']++;
  493. }
  494. }
  495. }
  496. echo round(($_SESSION['forumupdate']['thread_count'] / $_SESSION['forumupdate']['thread_total']) * 100, 1);
  497. }
  498. function step7()
  499. {
  500. $ns = e107::getRender();
  501. $stepCaption = 'Step 7: Calculate user post counts';
  502. if (!isset($_POST['calculate_usercounts']))
  503. {
  504. $text = "
  505. This step will calculate post count information for all users, as well as recount all for thread and reply counts.
  506. <br /><br />
  507. <form method='post'>
  508. <input class='btn btn-success' data-loading-text='Please wait...' type='submit' name='calculate_usercounts' value='Proceed with post count calculation' />
  509. </form>
  510. ";
  511. $ns -> tablerender($stepCaption, $text);
  512. return;
  513. }
  514. global $forum;
  515. require_once (e_HANDLER . 'user_extended_class.php');
  516. $ue = new e107_user_extended;
  517. $counts = $forum -> getUserCounts();
  518. foreach ($counts as $uid => $count)
  519. {
  520. $ue -> user_extended_setvalue($uid, 'user_plugin_forum_posts', $count, 'int');
  521. }
  522. $forum -> forumUpdateCounts('all', true);
  523. // var_dump($counts);
  524. $text .= "
  525. Successfully recalculated forum posts for " . count($counts) . " users.
  526. <br /><br />
  527. <form method='post' action='" . e_SELF . "?step=8'>
  528. <input class='btn btn-success' type='submit' name='nextStep[8]' value='Proceed to step 8' />
  529. </form>
  530. ";
  531. $ns -> tablerender($stepCaption, $text);
  532. }
  533. function step8()
  534. {
  535. $sql = e107::getDb();
  536. $mes = e107::getMessage();
  537. $stepCaption = 'Step 8: Calculate last post information';
  538. $_SESSION['forumupdate']['lastpost_total'] = $sql -> count('forum', '(*)', "WHERE forum_parent != 0");
  539. $_SESSION['forumupdate']['lastpost_count'] = 0;
  540. $_SESSION['forumupdate']['lastpost_last'] = 0;
  541. $mes->addDebug("Total LastPost: ".$_SESSION['forumupdate']['lastpost_total']);
  542. $text = "
  543. This step will recalculate all thread and forum lastpost information";
  544. $text .= renderProgress('Proceed with lastpost calculation',8);
  545. e107::getRender() -> tablerender($stepCaption, $mes->render(). $text);
  546. return;
  547. }
  548. function step8_ajax()
  549. {
  550. $sql = e107::getDb();
  551. $lastThread = vartrue($_SESSION['forumupdate']['lastpost_last'], 0);
  552. global $forum;
  553. if ($sql->select('forum', 'forum_id', 'forum_parent != 0 AND forum_id > '.$lastThread.' ORDER BY forum_id LIMIT 2'))
  554. {
  555. while ($row = $sql->fetch(MYSQL_ASSOC))
  556. {
  557. $parentList[] = $row['forum_id'];
  558. }
  559. foreach($parentList as $id)
  560. {
  561. set_time_limit(60);
  562. $forum->forumUpdateLastpost('forum', $id, $updateThreads);
  563. $_SESSION['forumupdate']['lastpost_last'] = $id;
  564. $_SESSION['forumupdate']['lastpost_count']++;
  565. }
  566. }
  567. echo round(($_SESSION['forumupdate']['lastpost_count'] / $_SESSION['forumupdate']['lastpost_total']) * 100);
  568. }
  569. function step9()
  570. {
  571. $sql = e107::getDb();
  572. $stepCaption = 'Step 9: Migrate poll information';
  573. if (!isset($_POST['migrate_polls']))
  574. {
  575. $text = "
  576. This step will recalculate all poll information that has been entered in the forums.
  577. <br /><br />
  578. <form method='post'>
  579. <input class='btn btn-success' data-loading-text='Please wait...' type='submit' name='migrate_polls' value='Proceed with poll migration' />
  580. </form>
  581. ";
  582. e107::getRender() -> tablerender($stepCaption, $text);
  583. return;
  584. }
  585. $qry = "
  586. SELECT t.thread_id, p.poll_id FROM `#polls` AS p
  587. LEFT JOIN `#forum_thread` AS t ON t.thread_id = p.poll_datestamp
  588. WHERE t.thread_id IS NOT NULL
  589. ";
  590. if ($sql -> gen($qry))
  591. {
  592. while ($row = $sql -> fetch(MYSQL_ASSOC))
  593. {
  594. $threadList[] = $row['thread_id'];
  595. }
  596. foreach ($threadList as $threadId)
  597. {
  598. if ($sql -> select('forum_thread', 'thread_options', 'thread_id = ' . $threadId, 'default'))
  599. {
  600. $row = $sql -> fetch(MYSQL_ASSOC);
  601. if ($row['thread_options'])
  602. {
  603. $opts = unserialize($row['thread_options']);
  604. $opts['poll'] = 1;
  605. }
  606. else
  607. {
  608. $opts = array('poll' => 1);
  609. }
  610. $tmp = array();
  611. $tmp['thread_options'] = serialize($opts);
  612. $tmp['WHERE'] = 'thread_id = ' . $threadId;
  613. // $tmp['_FIELD_TYPES']['thread_options'] = 'escape';
  614. $sql -> update('forum_thread', $tmp);
  615. }
  616. }
  617. }
  618. else
  619. {
  620. $text = 'No threads found! <br />';
  621. }
  622. $text .= "
  623. Successfully migrated forum poll information for " . count($threadList) . " thread poll(s).
  624. <br /><br />
  625. <form method='post' action='" . e_SELF . "?step=10'>
  626. <input class='btn btn-success' type='submit' name='nextStep[10]' value='Proceed to step 10' />
  627. </form>
  628. ";
  629. e107::getRender() -> tablerender($stepCaption, $text);
  630. }
  631. function step10()
  632. {
  633. $sql = e107::getDb();
  634. $ns = e107::getRender();
  635. $mes = e107::getMessage();
  636. global $f;
  637. $stepCaption = 'Step 10: Migrate forum attachments';
  638. $_SESSION['forumupdate']['attachment_total'] = $sql -> count('forum_post', '(*)', "WHERE post_entry LIKE '%public/%' ");
  639. $_SESSION['forumupdate']['attachment_count'] = 0;
  640. $_SESSION['forumupdate']['attachment_last'] = 0;
  641. if ($_SESSION['forumupdate']['attachment_total'] == 0)
  642. {
  643. $text .= "
  644. No forum attachments found.
  645. <br /><br />
  646. <form method='post' action='" . e_SELF . "?step=11'>
  647. <input class='btn' type='submit' name='nextStep[11]' value='Proceed to step 11' />
  648. </form>
  649. ";
  650. $ns -> tablerender($stepCaption, $text);
  651. return;
  652. }
  653. $text = "
  654. This step will migrate the forum attachment information that was found in <b>" . $_SESSION['forumupdate']['attachment_total'] . "</b> posts.<br />
  655. All files will be moved from the e107_files/public directory into the <b>" . e_MEDIA . "plugins/forum/ </b> directory and related posts will be updated accordingly.
  656. <br /><br />
  657. ";
  658. $text .= renderProgress("Begin attachment migration",10);
  659. file_put_contents(e_LOG."forum_upgrade_attachments.log",''); // clear the log.
  660. $ns -> tablerender($stepCaption, $mes -> render() . $text);
  661. }
  662. /**
  663. * Attachments
  664. */
  665. function step10_ajax()//TODO
  666. {
  667. $sql = e107::getDb();
  668. global $f;
  669. $lastPost = vartrue($_SESSION['forumupdate']['attachment_last'], 0);
  670. /*
  671. $qry = "
  672. SELECT post_id, post_thread, post_entry, post_user FROM `#forum_post`
  673. WHERE post_entry REGEXP '_[[:digit:]]'
  674. AND post_id > {$lastPost} ORDER BY post_id LIMIT 50
  675. ";
  676. */
  677. $qry = "
  678. SELECT post_id, post_thread, post_entry, post_user FROM `#forum_post`
  679. WHERE post_id > {$lastPost} AND post_entry LIKE '%public/%'
  680. ORDER BY post_id LIMIT 50
  681. ";
  682. // file_put_contents(e_LOG."forum_update_step10.log",$qry."\n",FILE_APPEND);
  683. if ($sql->gen($qry))
  684. {
  685. while ($row = $sql->fetch(MYSQL_ASSOC))
  686. {
  687. $postList[] = $row;
  688. }
  689. $i = 0;
  690. $pcount = 0;
  691. $f -> log("Found " . count($postList) . " posts with attachments");
  692. foreach ($postList as $post)
  693. {
  694. // echo htmlentities($post['post_entry'])."<br />";
  695. $_SESSION['forumupdate']['attachment_last'] = $post['post_id'];
  696. $_SESSION['forumupdate']['attachment_count']++;
  697. $i++;
  698. // if($pcount++ > 10) { die('here 10'); }
  699. $attachments = array();
  700. $foundFiles = array();
  701. // echo $post['post_entry']."<br /><br />";
  702. //[link={e_FILE}public/1230091080_1_FT0_julia.jpg][img:width=60&height=45]{e_FILE}public/1230091080_1_FT0_julia_.jpg[/img][/link][br]
  703. //Check for images with thumbnails linking to full size
  704. // if (preg_match_all('#\[link=(.*?)\]\[img.*?\]({e_FILE}.*?)\[/img\]\[/link\]#ms', $post['post_entry'], $matches, PREG_SET_ORDER))
  705. if (preg_match_all('#\[link=([^\]]*?)\]\s*?\[img.*?\](({e_FILE}|e107_files|\.\./\.\./e107_files)[^\]]*)\[/img\]\s*?\[/link\]#ms', $post['post_entry'], $matches, PREG_SET_ORDER))
  706. {
  707. foreach ($matches as $match)
  708. {
  709. $att = array();
  710. $att['thread_id'] = $post['post_thread'];
  711. $att['type'] = 'img';
  712. $att['html'] = $match[0];
  713. $att['name'] = str_replace (array("\r\n", "\n", "\r"), '', $match[1]);
  714. $att['thumb'] = $match[2];
  715. $attachments[] = $att;
  716. $foundFiles[] = $match[1];
  717. $foundFiles[] = $match[2];
  718. logAttachment($att['thread_id'],'link', $att['name']);
  719. }
  720. }
  721. if (preg_match_all('#\[lightbox=([^\]]*?)\]\s*?\[img.*?\](({e_FILE}|e107_files|\.\./\.\./e107_files)[^\]]*)\[/img\]\s*?\[/lightbox\]#ms', $post['post_entry'], $matches, PREG_SET_ORDER))
  722. {
  723. foreach ($matches as $match)
  724. {
  725. $att = array();
  726. $att['thread_id'] = $post['post_thread'];
  727. $att['type'] = 'img';
  728. $att['html'] = $match[0];
  729. $att['name'] = str_replace (array("\r\n", "\n", "\r"), '', $match[1]);
  730. $att['thumb'] = $match[2];
  731. $attachments[] = $att;
  732. $foundFiles[] = $match[1];
  733. $foundFiles[] = $match[2];
  734. logAttachment($att['thread_id'],'lightbox', $att['name']);
  735. }
  736. }
  737. /*
  738. if (preg_match_all('#\[link=(.*?)\]\[img.*?\](\.\./\.\./e107_files/public/.*?)\[/img\]\[/link\]#ms', $post['post_entry'], $matches, PREG_SET_ORDER))
  739. {
  740. foreach ($matches as $match)
  741. {
  742. $att = array();
  743. $att['thread_id'] = $post['post_thread'];
  744. $att['type'] = 'img';
  745. $att['html'] = $match[0];
  746. $att['name'] = $match[1];
  747. $att['thumb'] = $match[2];
  748. $attachments[] = $att;
  749. $foundFiles[] = $match[1];
  750. $foundFiles[] = $match[2];
  751. logAttachment($att['thread_id'],'link2', $att['name']);
  752. }
  753. }
  754. */
  755. //<div
  756. // class=&#039;spacer&#039;>[img:width=604&height=453]{e_FILE}public/1229562306_1_FT0_julia.jpg[/img]</div>
  757. //Check for attached full-size images
  758. ;
  759. // if (preg_match_all('#\[img.*?\]({e_FILE}.*?_FT\d+_.*?)\[/img\]#ms', $post['post_entry'], $matches, PREG_SET_ORDER))
  760. if (preg_match_all('#\[img[^\]]*?\]\s*?(({e_FILE}|e107_files|\.\./\.\./e107_files)[^\[]*)\s*?\[/img\]#ms', $post['post_entry'], $matches, PREG_SET_ORDER))
  761. {
  762. foreach ($matches as $match)
  763. {
  764. //Ensure it hasn't already been handled above
  765. if (!in_array($match[1], $foundFiles))
  766. {
  767. $att = array();
  768. $att['thread_id'] = $post['post_thread'];
  769. $att['type'] = 'img';
  770. $att['html'] = $match[0];
  771. $att['name'] = str_replace (array("\r\n", "\n", "\r"), '', $match[1]);
  772. $att['thumb'] = '';
  773. $attachments[] = $att;
  774. logAttachment($att['thread_id'],'img', $att['name']);
  775. }
  776. }
  777. }
  778. /*
  779. if (preg_match_all('#\[img.*?\](\.\./\.\./e107_files/public/.*?_FT\d+_.*?)\[/img\]#ms', $post['post_entry'], $matches, PREG_SET_ORDER))
  780. {
  781. foreach ($matches as $match)
  782. {
  783. //Ensure it hasn't already been handled above
  784. if (!in_array($match[1], $foundFiles))
  785. {
  786. $att = array();
  787. $att['thread_id'] = $post['post_thread'];
  788. $att['type'] = 'img';
  789. $att['html'] = $match[0];
  790. $att['name'] = $match[1];
  791. $att['thumb'] = '';
  792. $attachments[] = $att;
  793. }
  794. }
  795. }
  796. */
  797. //[file={e_FILE}public/1230090820_1_FT0_julia.zip]julia.zip[/file]
  798. //Check for attached file (non-images)
  799. // if (preg_match_all('#\[file=({e_FILE}.*?)\](.*?)\[/file\]#ms', $post['post_entry'], $matches, PREG_SET_ORDER))
  800. if (preg_match_all('#\[file=(({e_FILE}|e107_files|\.\./\.\./e107_files)[^\]]*)#ms', $post['post_entry'], $matches, PREG_SET_ORDER))
  801. {
  802. foreach ($matches as $match)
  803. {
  804. $att = array();
  805. $att['thread_id'] = $post['post_thread'];
  806. $att['type'] = 'file';
  807. $att['html'] = $match[0];
  808. $att['name'] = $match[1];
  809. $att['thumb'] = '';
  810. $attachments[] = $att;
  811. logAttachment($att['thread_id'],'file', $att['name']);
  812. }
  813. }
  814. /*
  815. if (preg_match_all('#\[file=(\.\./\.\./e107_files/public/.*?)\](.*?)\[/file\]#ms', $post['post_entry'], $matches, PREG_SET_ORDER))
  816. {
  817. foreach ($matches as $match)
  818. {
  819. $att = array();
  820. $att['thread_id'] = $post['post_thread'];
  821. $att['type'] = 'file';
  822. $att['html'] = $match[0];
  823. $att['name'] = $match[1];
  824. $att['thumb'] = '';
  825. $attachments[] = $att;
  826. }
  827. }
  828. */
  829. if (count($attachments))
  830. {
  831. $f->log("found " . count($attachments) . " attachments");
  832. $newValues = array();
  833. $info = array();
  834. $info['post_entry'] = $post['post_entry'];
  835. foreach ($attachments as $attachment)
  836. {
  837. $error = '';
  838. $f->log($attachment['name']);
  839. if ($f->moveAttachment($attachment, $post, $error))
  840. {
  841. $type = $attachment['type'];
  842. $newValues[$type][] = basename($attachment['name']);
  843. $info['post_entry'] = str_replace($attachment['html'], '', $info['post_entry']);
  844. }
  845. else
  846. {
  847. $errorText .= "Failure processing post {$post['post_id']} - file {$attachment['name']} - {$error}<br />";
  848. $f -> log("Failure processing post {$post['post_id']} - file {$attachment['name']} - {$error}");
  849. }
  850. }
  851. // echo $errorText."<br />";
  852. // Did we make any changes at all?
  853. if (count($newValues))
  854. {
  855. $info['WHERE'] = 'post_id = ' . $post['post_id'];
  856. $info['post_attachments'] = e107::serialize($newValues);
  857. // $sql->update('forum_post', $info); // XXX FIXME TODO screwed up due to _FIELD_DEFS
  858. $sql->update('forum_post',"post_entry = \"".$info['post_entry']."\", post_attachments=\"".$info['post_attachments']."\" WHERE post_id = ".$post['post_id']."");
  859. }
  860. }
  861. }
  862. $totalOutput = round(($_SESSION['forumupdate']['attachment_count'] / $_SESSION['forumupdate']['attachment_total']) * 100, 1);
  863. echo $totalOutput;
  864. /*
  865. $debugRound = "
  866. forumupdate_attachment_count = ".$_SESSION['forumupdate']['attachment_count']."
  867. forumupdate_attachment_total = ".$_SESSION['forumupdate']['attachment_total']."
  868. calculated = ".$totalOutput."
  869. ";
  870. file_put_contents(e_LOG."forum_update_step10.log",$debugRound,FILE_APPEND);
  871. */
  872. }
  873. else
  874. {
  875. echo 100;
  876. }
  877. }
  878. function logAttachment($thread, $type, $attach)
  879. {
  880. $tab = ($type == 'img') ? "\t\t\t" : "\t\t";
  881. $text = $thread."\t\t".$type.$tab.$attach."\n";
  882. file_put_contents(e_LOG."forum_upgrade_attachments.log",$text, FILE_APPEND);
  883. }
  884. function step11()
  885. {
  886. $ns = e107::getRender();
  887. $stepCaption = 'Step 11: Delete old attachments';
  888. if (!isset($_POST['delete_orphans']))
  889. {
  890. $text = "
  891. The previous versions of the forum had difficulty deleting attachment files when posts or threads were deleted.
  892. <br />
  893. As a result of this, there is a potential for numerous files to exist that do not point to anything. In this step
  894. we will try to identify these files and delete them.
  895. <br /><br />
  896. <form method='post'>
  897. <input class='btn btn-success' data-loading-text='Please wait...' type='submit' name='delete_orphans' value='Proceed with attachment deletion' />
  898. </form>
  899. <form method='post' action='" . e_SELF . "?step=12'>
  900. <input class='btn btn-primary' type='submit' name='nextStep[12]' value='Skip this step' />
  901. </form>
  902. ";
  903. e107::getRender() -> tablerender($stepCaption, $text);
  904. return;
  905. }
  906. global $forum;
  907. require_once (e_HANDLER . 'file_class.php');
  908. $f = new e_file;
  909. $flist = $f -> get_files(e_FILE . 'public', '_\d+_FT\d+_');
  910. $numFiles = count($flist);
  911. if ($numFiles)
  912. {
  913. if ($_POST['delete_orphans'] == 'Delete files')
  914. {
  915. //Do the deletion
  916. $success = 0;
  917. $failText = '';
  918. foreach ($flist as $file)
  919. {
  920. $fileName = e_FILE . 'public/' . $file['fname'];
  921. $r = unlink($fileName);
  922. if ($r)
  923. {
  924. $success++;
  925. }
  926. else
  927. {
  928. $failText .= "Deletion failed: {$file['fname']}<br />";
  929. }
  930. }
  931. if ($failText)
  932. {
  933. $failText = "<br /><br />The following failures occured: <br />" . $failText;
  934. }
  935. $text .= "
  936. Successfully removed {$success} orphaned files <br />
  937. {$failText}
  938. <br /><br />
  939. <form method='post' action='" . e_SELF . "?step=12'>
  940. <input class='btn' type='submit' name='nextStep[12]' value='Proceed to step 12' />
  941. </form>
  942. ";
  943. $ns -> tablerender($stepCaption, $text);
  944. return;
  945. }
  946. $text = "There were {$numFiles} orphaned files found<br /><br />";
  947. if ($_POST['delete_orphans'] == 'Show files' || $numFiles < 31)
  948. {
  949. $i = 1;
  950. foreach ($flist as $file)
  951. {
  952. $text .= $i++ . ') ' . $file['fname'] . '<br />';
  953. }
  954. $extra = '';
  955. }
  956. else
  957. {
  958. $extra = "<input class='btn' type='submit' name='delete_orphans' value='Show files' />&nbsp; &nbsp; &nbsp; &nbsp;";
  959. }
  960. $text .= "
  961. <br /><br />
  962. <form method='post'>
  963. {$extra}
  964. <input class='btn' type='submit' name='delete_orphans' value='Delete files' />
  965. </form>
  966. ";
  967. $ns -> tablerender($stepCaption, $text);
  968. return;
  969. }
  970. else
  971. {
  972. $text .= "
  973. There were no orphaned files found <br />
  974. <br /><br />
  975. <form method='post' action='" . e_SELF . "?step=12'>
  976. <input class='btn' type='submit' name='nextStep[12]' value='Proceed to step 12' />
  977. </form>
  978. ";
  979. $ns -> tablerender($stepCaption, $text);
  980. return;
  981. }
  982. }
  983. function step12()
  984. {
  985. $sql = e107::getDb();
  986. $ns = e107::getRender();
  987. $mes = e107::getMessage();
  988. $f = new forumUpgrade;
  989. $stepCaption = 'Step 12: Delete old forum data';
  990. if (!isset($_POST['delete_old']) && !isset($_POST['skip_delete_old']))
  991. {
  992. $text = "
  993. The forum upgrade should now be complete.<br /> During the upgrade process the old forum tables were
  994. retained. You may choose to keep these tables as a backup or delete them. <br /><br />
  995. We will also be marking the forum upgrade as completed!
  996. <br /><br />
  997. <form method='post'>
  998. <input class='btn btn-danger' data-loading-text='Please wait...' type='submit' name='delete_old' value='Remove old forum tables' />
  999. <input class='btn btn-primary' type='submit' name='skip_delete_old' value='Keep old forum tables' />
  1000. </form>
  1001. ";
  1002. $ns -> tablerender($stepCaption, $text);
  1003. return;
  1004. }
  1005. if (vartrue($_POST['delete_old']))
  1006. {
  1007. $qryArray = array(
  1008. "DROP TABLE `#forum_old`",
  1009. "DROP TABLE `#forum_t`",
  1010. );
  1011. foreach ($qryArray as $qry)
  1012. {
  1013. $sql -> gen($qry);
  1014. }
  1015. }
  1016. unset($_SESSION['forumUpgrade']);
  1017. $ret = $f -> setNewVersion();
  1018. $mes -> addSuccess("Congratulations, the forum upgrade is now completed!<br /><br />{$ret}");
  1019. $text = "<a class='btn btn-primary' href='".e_ADMIN."e107_update.php'>Return to e107 Update</a>";
  1020. $ns -> tablerender($stepCaption, $mes -> render() . $text);
  1021. return;
  1022. }
  1023. class forumUpgrade
  1024. {
  1025. var $newVersion = '2.0';
  1026. var $error = array();
  1027. public $updateInfo;
  1028. private $attachmentData;
  1029. private $logf;
  1030. public function __construct()
  1031. {
  1032. $this -> updateInfo['lastThread'] = 0;
  1033. $this -> attachmentData = array();
  1034. $this -> logf = e_LOG . 'forum_upgrade.log';
  1035. $this -> getUpdateInfo();
  1036. }
  1037. public function log($msg, $append = true)
  1038. {
  1039. // echo "logf = ".$this->logf."<br />";
  1040. $txt = sprintf("%s - %s\n", date('m/d/Y H:i:s'), $msg);
  1041. // echo $txt."<br />";
  1042. $flag = ($append ? FILE_APPEND : '');
  1043. file_put_contents($this -> logf, $txt, $flag);
  1044. }
  1045. public function checkUpdateNeeded()
  1046. {
  1047. return true;
  1048. // include_once(e_PLUGIN.'forum/forum_update_check.php');
  1049. // $needed = update_forum_08('check');
  1050. // return !$needed;
  1051. }
  1052. function checkAttachmentDirs()
  1053. {
  1054. $dirs = array(
  1055. e_MEDIA . 'plugins/',
  1056. e_MEDIA . 'plugins/forum/',
  1057. e_MEDIA . 'plugins/forum/attachments/',
  1058. e_MEDIA . 'plugins/forum/attachments/thumb'
  1059. );
  1060. foreach ($dirs as $dir)
  1061. {
  1062. if (!file_exists($dir))
  1063. {
  1064. if (!mkdir($dir, 0755, true))
  1065. {
  1066. $this -> error['attach'][] = "Directory '{$dir}' does not exist and I was unable to create it";
  1067. }
  1068. }
  1069. else
  1070. {
  1071. if (!is_writable($dir))
  1072. {
  1073. $this -> error['attach'][] = "Directory '{$dir}' exits, but is not writeable";
  1074. }
  1075. }
  1076. }
  1077. }
  1078. function getUpdateInfo()
  1079. {
  1080. $sql = e107::getDb();
  1081. if ($_SESSION['forumUpgrade'])
  1082. {
  1083. $this -> updateInfo = $_SESSION['forumUpgrade'];
  1084. }
  1085. else
  1086. {
  1087. $this -> updateInfo = array();
  1088. }
  1089. return;
  1090. /*
  1091. if ($sql -> select('generic', '*', "gen_type = 'forumUpgrade'"))
  1092. {
  1093. $row = $sql -> fetch(MYSQL_ASSOC);
  1094. $this -> updateInfo = unserialize($row['gen_chardata']);
  1095. }
  1096. else
  1097. {
  1098. $qry = "INSERT INTO `#generic` (gen_type) VALUES ('forumUpgrade')";
  1099. $sql -> gen($qry);
  1100. $this -> updateInfo = array();
  1101. }
  1102. * */
  1103. }
  1104. function setUpdateInfo()
  1105. {
  1106. $_SESSION['forumUpgrade'] = $this -> updateInfo;
  1107. return;
  1108. }
  1109. function setNewVersion()
  1110. {
  1111. $pref = e107::getPref();
  1112. $sql = e107::getDb();
  1113. $sql -> update('plugin', "plugin_version = '{$this->newVersion}' WHERE plugin_name='Forum'");
  1114. $pref['plug_installed']['forum'] = $this -> newVersion;
  1115. save_prefs();
  1116. return "Forum Version updated to version: {$this->newVersion} <br />";
  1117. }
  1118. function migrateThread($threadId)
  1119. {
  1120. global $forum;
  1121. $threadId = (int)$threadId;
  1122. if (e107::getDb()->select('forum_t', '*', "thread_parent = {$threadId} OR thread_id = {$threadId}", 'default'))
  1123. {
  1124. $threadData = e107::getDb()->db_getList();
  1125. foreach ($threadData as $post)
  1126. {
  1127. if ($post['thread_parent'] == 0)
  1128. {
  1129. $result = $this -> addThread($post);
  1130. if ($result)
  1131. {
  1132. $result = $this -> addPost($post);
  1133. }
  1134. }
  1135. else
  1136. {
  1137. $result = $this -> addPost($post);
  1138. }
  1139. }
  1140. return ($result ? count($threadData) : false);
  1141. }
  1142. return false;
  1143. }
  1144. function addThread(&$post)
  1145. {
  1146. global $forum;
  1147. /*
  1148. * v1.x
  1149. * thread_id
  1150. * thread_name
  1151. * thread_thread
  1152. * thread_forum_id
  1153. * thread_datestamp
  1154. * thread_parent
  1155. * thread_user
  1156. * thread_views
  1157. * thread_active
  1158. * thread_lastpost
  1159. * thread_s
  1160. * thread_edit_datestamp
  1161. * thread_lastuser
  1162. * thread_total_replies
  1163. */
  1164. /*
  1165. * v2.x
  1166. * thread_id
  1167. * thread_name
  1168. * thread_forum_id
  1169. * thread_views
  1170. * thread_active
  1171. * thread_lastpost
  1172. * thread_sticky
  1173. * thread_datestamp
  1174. * thread_user
  1175. * thread_user_anon
  1176. * thread_lastuser
  1177. * thread_lastuser_anon
  1178. * thread_total_replies
  1179. * thread_options
  1180. */
  1181. $detected = mb_detect_encoding($post['thread_name']); // 'ISO-8859-1'
  1182. $threadName = iconv($detected,'UTF-8', $post['thread_name']);
  1183. $thread = array();
  1184. $thread['thread_id'] = $post['thread_id'];
  1185. $thread['thread_name'] = $threadName;
  1186. $thread['thread_forum_id'] = $post['thread_forum_id'];
  1187. $thread['thread_datestamp'] = $post['thread_datestamp'];
  1188. $thread['thread_lastpost'] = $post['thread_lastpost'];
  1189. $thread['thread_views'] = $post['thread_views'];
  1190. $thread['thread_active'] = $post['thread_active'];
  1191. $thread['thread_sticky'] = $post['thread_s'];
  1192. // $thread['thread_lastuser'] = $post['thread_lastuser'];
  1193. $thread['thread_total_replies'] = $post['thread_total_replies'];
  1194. $userInfo = $this -> getUserInfo($post['thread_user']);
  1195. $thread['thread_user'] = $userInfo['user_id'];
  1196. $thread['thread_user_anon'] = $userInfo['anon_name'];
  1197. // If thread marked as 'tracked by starter', we must convert to using
  1198. // forum_track table
  1199. if ($thread['thread_active'] == 99 && $thread['thread_user'] > 0)
  1200. {
  1201. $forum -> track('add', $thread['thread_user'], $thread['thread_id'], true);
  1202. $thread['thread_active'] = 1;
  1203. }
  1204. // $thread['_FIELD_TYPES'] = $forum->fieldTypes['forum_thread'];
  1205. // $thread['_FIELD_TYPES']['thread_name'] = 'escape'; //use escape to prevent
  1206. // double entities
  1207. $result = e107::getDb() -> insert('forum_thread', $thread);
  1208. return $result;
  1209. }
  1210. function addPost(&$post)
  1211. {
  1212. global $forum;
  1213. $detected = mb_detect_encoding($post['thread_thread']); // 'ISO-8859-1'
  1214. $postEntry = iconv($detected,'UTF-8', $post['thread_thread']);
  1215. $newPost = array();
  1216. $newPost['post_id'] = $post['thread_id'];
  1217. $newPost['post_thread'] = ($post['thread_parent'] == 0 ? $post['thread_id'] : $post['thread_parent']);
  1218. $newPost['post_entry'] = $postEntry;
  1219. $newPost['post_forum'] = $post['thread_forum_id'];
  1220. $newPost['post_datestamp'] = $post['thread_datestamp'];
  1221. $newPost['post_edit_datestamp'] = ($post['thread_edit_datestamp'] ? $post['thread_edit_datestamp'] : '_NULL_');
  1222. $userInfo = $this -> getUserInfo($post['thread_user']);
  1223. $newPost['post_user'] = $userInfo['user_id'];
  1224. $newPost['post_user_anon'] = $userInfo['anon_name'];
  1225. $newPost['post_ip'] = $userInfo['user_ip'];
  1226. // $newPost['_FIELD_TYPES'] = $forum->fieldTypes['forum_post'];
  1227. // $newPost['_FIELD_TYPES']['post_entry'] = 'escape'; //use escape to prevent
  1228. // double entities
  1229. // print_a($newPost);
  1230. // exit;
  1231. $result = e107::getDb() -> insert('forum_post', $newPost);
  1232. // exit;
  1233. return $result;
  1234. }
  1235. function getUserInfo(&$info)
  1236. {
  1237. $e107 = e107::getInstance();
  1238. $tmp = explode('.', $info);
  1239. $ret = array(
  1240. 'user_id' => 0,
  1241. 'user_ip' => '_NULL_',
  1242. 'anon_name' => '_NULL_'
  1243. );
  1244. if (count($tmp) == 2)
  1245. {
  1246. $id = (int)$tmp[0];
  1247. if ($id == 0)//Anonymous post
  1248. {
  1249. $_tmp = explode(chr(0), $tmp[1]);
  1250. if (count($_tmp) == 2)//Ip address exists
  1251. {
  1252. $ret['user_ip'] = $e107 -> ipEncode($_tmp[1]);
  1253. $ret['anon_name'] = $_tmp[0];
  1254. }
  1255. }
  1256. else
  1257. {
  1258. $ret['user_id'] = $id;
  1259. }
  1260. }
  1261. else
  1262. {
  1263. if (is_numeric($info) && $info > 0)
  1264. {
  1265. $ret['user_id'] = $info;
  1266. }
  1267. else
  1268. {
  1269. $ret['anon_name'] = 'Unknown';
  1270. }
  1271. }
  1272. return $ret;
  1273. }
  1274. function moveAttachment($attachment, $post, &$error)
  1275. {
  1276. global $forum;
  1277. set_time_limit(30);
  1278. $tp = e107::getParser();
  1279. $post_id = $post['post_id'];
  1280. $newPath = $forum->getAttachmentPath($post['post_user']);
  1281. if (!is_dir($newPath))
  1282. {
  1283. mkdir($newPath, 0755);
  1284. }
  1285. $attachment['name'] = str_replace(array(
  1286. ' ',
  1287. "\n",
  1288. "\r"
  1289. ), '', $attachment['name']);
  1290. $old = str_replace('{e_FILE}', e_FILE, $attachment['name']);
  1291. $fileInfo = pathinfo($attachment['name']);
  1292. $new = $newPath . "/" . $fileInfo['basename'];
  1293. $hash = md5($new);
  1294. if (!file_exists($old))
  1295. {
  1296. if (isset($this -> attachmentData[$hash]))
  1297. {
  1298. $error = "Post {$post_id} - Attachment already migrated with post: " . $this -> attachmentData[$hash];
  1299. }
  1300. else
  1301. {
  1302. $error = 'Original attachment not found (orphaned?)';
  1303. }
  1304. return false;
  1305. }
  1306. if (!file_exists($new))
  1307. {
  1308. $this -> log("Copying [{$old}] -> [{$new}]");
  1309. $r = rename($old, $new);
  1310. $this -> attachmentData[$hash] = $post_id;
  1311. // $r = true;
  1312. }
  1313. else
  1314. {
  1315. //File already exists, show some sort of error
  1316. if (isset($this -> attachmentData[$hash]))
  1317. {
  1318. $error = "Post {$post_id} - Attachment already migrated with post: " . $this -> attachmentData[$hash];
  1319. }
  1320. else
  1321. {
  1322. $error = 'Attachment file already exists';
  1323. }
  1324. return false;
  1325. }
  1326. if (!$r)
  1327. {
  1328. //File copy failed!
  1329. $error = 'Moving of attachments failed';
  1330. return false;
  1331. }
  1332. $oldThumb = '';
  1333. if ($attachment['thumb'])
  1334. {
  1335. $tmp = explode('/', $attachment['thumb']);
  1336. $fileInfo = pathinfo($attachment['thumb']);
  1337. $oldThumb = str_replace('{e_FILE}', e_FILE, $attachment['thumb']);
  1338. // $newThumb = e_PLUGIN.'forum/attachments/thumb/'.$tmp[1];
  1339. $newThumb = e_MEDIA . 'files/plugins/forum/attachments/thumb/' . $fileInfo['basename'];
  1340. $hash = md5($newThumb);
  1341. if (!file_exists($newThumb))
  1342. {
  1343. $r = rename($oldThumb, $newThumb);
  1344. // $r = true;
  1345. }
  1346. else
  1347. {
  1348. //File already exists, show some sort of error
  1349. if (isset($this -> attachmentData[$hash]))
  1350. {
  1351. $error = "Post {$post_id} - Thumb already migrated with post: " . $this -> attachmentData[$hash];
  1352. }
  1353. else
  1354. {
  1355. $error = 'Thumb file already exists';
  1356. }
  1357. return false;
  1358. }
  1359. if (!$r)
  1360. {
  1361. //File copy failed
  1362. $error = 'Moving of thumb failed';
  1363. return false;
  1364. }
  1365. }
  1366. //Copy was successful, let's delete the original files now.
  1367. // $r = true;
  1368. // $r = unlink($old);
  1369. if (!$r)
  1370. {
  1371. // $error = 'Was unable to delete old attachment: '.$old;
  1372. // return false;
  1373. }
  1374. if ($oldThumb)
  1375. {
  1376. // $r = true;
  1377. // $r = unlink($oldThumb);
  1378. if (!$r)
  1379. {
  1380. // $error = 'Was unable to delete old thumb: '.$oldThumb;
  1381. // return false;
  1382. }
  1383. }
  1384. return true;
  1385. }
  1386. }
  1387. function createThreadLimitDropdown($count)
  1388. {
  1389. $ret = "
  1390. <select class='tbox' name='threadLimit'>
  1391. ";
  1392. $last = min($count, 10000);
  1393. if ($count < 2000)
  1394. {
  1395. $ret .= "<option value='{$count}'>{$count}</option>";
  1396. }
  1397. else
  1398. {
  1399. for ($i = 2000; $i < $count; $i += 2000)
  1400. {
  1401. $ret .= "<option value='{$i}'>{$i}</option>";
  1402. }
  1403. if ($count < 10000)
  1404. {
  1405. $ret .= "<option value='{$count}'>{$count}</option>";
  1406. }
  1407. }
  1408. $ret .= '</select>';
  1409. return $ret;
  1410. }
  1411. function forum_update_adminmenu()
  1412. {
  1413. $action = 1;
  1414. $var[1]['text'] = '1 - Permissions';
  1415. $var[1]['link'] = e_SELF . "?step=1";
  1416. $var[2]['text'] = '2 - Create new tables';
  1417. $var[2]['link'] = '#';
  1418. $var[3]['text'] = '3 - Create extended fields';
  1419. $var[3]['link'] = '#';
  1420. $var[4]['text'] = '4 - Move user data';
  1421. $var[4]['link'] = '#';
  1422. $var[5]['text'] = '5 - Migrate forum config';
  1423. $var[5]['link'] = '#';
  1424. $var[6]['text'] = '6 - Migrate threads/replies';
  1425. $var[6]['link'] = '#';
  1426. $var[7]['text'] = '7 - Recalc all counts';
  1427. $var[7]['link'] = '#';
  1428. $var[8]['text'] = '8 - Calc lastpost data';
  1429. $var[8]['link'] = '#';
  1430. $var[9]['text'] = '9 - Migrate any poll data';
  1431. $var[9]['link'] = '#';
  1432. $var[10]['text'] = '10 - Migrate any attachments';
  1433. $var[10]['link'] = '#';
  1434. $var[11]['text'] = '11 - Delete old attachments';
  1435. $var[11]['link'] = '#';
  1436. $var[12]['text'] = '12 - Delete old forum data';
  1437. $var[12]['link'] = '#';
  1438. if(E107_DEBUG_LEVEL)
  1439. {
  1440. $var[13]['divider'] = true;
  1441. $var[14]['text'] = 'Reset';
  1442. $var[14]['link'] = e_SELF . "?reset";
  1443. $var[15]['text'] = 'Reset to 7';
  1444. $var[15]['link'] = e_SELF . "?step=7&reset=7";
  1445. $var[16]['text'] = 'Reset to 10';
  1446. $var[16]['link'] = e_SELF . "?step=10&reset=10";
  1447. }
  1448. if (isset($_GET['step']))
  1449. {
  1450. // $action = key($_POST['nextStep']);
  1451. $action = intval($_GET['step']);
  1452. }
  1453. show_admin_menu('Forum Upgrade', $action, $var);
  1454. }
  1455. ?>