migrate-sigs.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. function phorum_htmlpurifier_migrate_sigs_check() {
  3. global $PHORUM;
  4. $offset = 0;
  5. if (!empty($_POST['migrate-sigs'])) {
  6. if (!isset($_POST['confirmation']) || strtolower($_POST['confirmation']) !== 'yes') {
  7. echo 'Invalid confirmation code.';
  8. exit;
  9. }
  10. $PHORUM['mod_htmlpurifier']['migrate-sigs'] = true;
  11. phorum_db_update_settings(array("mod_htmlpurifier"=>$PHORUM["mod_htmlpurifier"]));
  12. $offset = 1;
  13. } elseif (!empty($_GET['migrate-sigs']) && $PHORUM['mod_htmlpurifier']['migrate-sigs']) {
  14. $offset = (int) $_GET['migrate-sigs'];
  15. }
  16. return $offset;
  17. }
  18. function phorum_htmlpurifier_migrate_sigs($offset) {
  19. global $PHORUM;
  20. if(!$offset) return; // bail out quick if $offset == 0
  21. // theoretically, we could get rid of this multi-request
  22. // doo-hickery if safe mode is off
  23. @set_time_limit(0); // attempt to let this run
  24. $increment = $PHORUM['mod_htmlpurifier']['migrate-sigs-increment'];
  25. require_once(dirname(__FILE__) . '/../migrate.php');
  26. // migrate signatures
  27. // do this in batches so we don't run out of time/space
  28. $end = $offset + $increment;
  29. $user_ids = array();
  30. for ($i = $offset; $i < $end; $i++) {
  31. $user_ids[] = $i;
  32. }
  33. $userinfos = phorum_db_user_get_fields($user_ids, 'signature');
  34. foreach ($userinfos as $i => $user) {
  35. if (empty($user['signature'])) continue;
  36. $sig = $user['signature'];
  37. // perform standard Phorum processing on the sig
  38. $sig = str_replace(array("&","<",">"), array("&amp;","&lt;","&gt;"), $sig);
  39. $sig = preg_replace("/<((http|https|ftp):\/\/[a-z0-9;\/\?:@=\&\$\-_\.\+!*'\(\),~%]+?)>/i", "$1", $sig);
  40. // prepare fake data to pass to migration function
  41. $fake_data = array(array("author"=>"", "email"=>"", "subject"=>"", 'body' => $sig));
  42. list($fake_message) = phorum_htmlpurifier_migrate($fake_data);
  43. $user['signature'] = $fake_message['body'];
  44. if (!phorum_api_user_save($user)) {
  45. exit('Error while saving user data');
  46. }
  47. }
  48. unset($userinfos); // free up memory
  49. // query for highest ID in database
  50. $type = $PHORUM['DBCONFIG']['type'];
  51. $sql = "select MAX(user_id) from {$PHORUM['user_table']}";
  52. $row = phorum_db_interact(DB_RETURN_ROW, $sql);
  53. $top_id = (int) $row[0];
  54. $offset += $increment;
  55. if ($offset > $top_id) { // test for end condition
  56. echo 'Migration finished';
  57. $PHORUM['mod_htmlpurifier']['migrate-sigs'] = false;
  58. phorum_htmlpurifier_commit_settings();
  59. return true;
  60. }
  61. $host = $_SERVER['HTTP_HOST'];
  62. $uri = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
  63. $extra = 'admin.php?module=modsettings&mod=htmlpurifier&migrate-sigs=' . $offset;
  64. // relies on output buffering to work
  65. header("Location: http://$host$uri/$extra");
  66. exit;
  67. }
  68. // vim: et sw=4 sts=4