HTMLPurifier 4.4.0
/home/ezyang/Dev/htmlpurifier/plugins/phorum/settings/migrate-sigs.php
Go to the documentation of this file.
00001 <?php
00002 
00003 function phorum_htmlpurifier_migrate_sigs_check() {
00004     global $PHORUM;
00005     $offset = 0;
00006     if (!empty($_POST['migrate-sigs'])) {
00007         if (!isset($_POST['confirmation']) || strtolower($_POST['confirmation']) !== 'yes') {
00008             echo 'Invalid confirmation code.';
00009             exit;
00010         }
00011         $PHORUM['mod_htmlpurifier']['migrate-sigs'] = true;
00012         phorum_db_update_settings(array("mod_htmlpurifier"=>$PHORUM["mod_htmlpurifier"]));
00013         $offset = 1;
00014     } elseif (!empty($_GET['migrate-sigs']) && $PHORUM['mod_htmlpurifier']['migrate-sigs']) {
00015         $offset = (int) $_GET['migrate-sigs'];
00016     }
00017     return $offset;
00018 }
00019 
00020 function phorum_htmlpurifier_migrate_sigs($offset) {
00021     global $PHORUM;
00022 
00023     if(!$offset) return; // bail out quick if $offset == 0
00024 
00025     // theoretically, we could get rid of this multi-request
00026     // doo-hickery if safe mode is off
00027     @set_time_limit(0); // attempt to let this run
00028     $increment = $PHORUM['mod_htmlpurifier']['migrate-sigs-increment'];
00029 
00030     require_once(dirname(__FILE__) . '/../migrate.php');
00031     // migrate signatures
00032     // do this in batches so we don't run out of time/space
00033     $end = $offset + $increment;
00034     $user_ids = array();
00035     for ($i = $offset; $i < $end; $i++) {
00036         $user_ids[] = $i;
00037     }
00038     $userinfos = phorum_db_user_get_fields($user_ids, 'signature');
00039     foreach ($userinfos as $i => $user) {
00040         if (empty($user['signature'])) continue;
00041         $sig = $user['signature'];
00042         // perform standard Phorum processing on the sig
00043         $sig = str_replace(array("&","<",">"), array("&amp;","&lt;","&gt;"), $sig);
00044         $sig = preg_replace("/<((http|https|ftp):\/\/[a-z0-9;\/\?:@=\&\$\-_\.\+!*'\(\),~%]+?)>/i", "$1", $sig);
00045         // prepare fake data to pass to migration function
00046         $fake_data = array(array("author"=>"", "email"=>"", "subject"=>"", 'body' => $sig));
00047         list($fake_message) = phorum_htmlpurifier_migrate($fake_data);
00048         $user['signature'] = $fake_message['body'];
00049         if (!phorum_api_user_save($user)) {
00050             exit('Error while saving user data');
00051         }
00052     }
00053     unset($userinfos); // free up memory
00054 
00055     // query for highest ID in database
00056     $type = $PHORUM['DBCONFIG']['type'];
00057     $sql = "select MAX(user_id) from {$PHORUM['user_table']}";
00058     $row = phorum_db_interact(DB_RETURN_ROW, $sql);
00059     $top_id = (int) $row[0];
00060 
00061     $offset += $increment;
00062     if ($offset > $top_id) { // test for end condition
00063         echo 'Migration finished';
00064         $PHORUM['mod_htmlpurifier']['migrate-sigs'] = false;
00065         phorum_htmlpurifier_commit_settings();
00066         return true;
00067     }
00068     $host  = $_SERVER['HTTP_HOST'];
00069     $uri   = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
00070     $extra = 'admin.php?module=modsettings&mod=htmlpurifier&migrate-sigs=' . $offset;
00071     // relies on output buffering to work
00072     header("Location: http://$host$uri/$extra");
00073     exit;
00074 
00075 }
00076 
00077 // vim: et sw=4 sts=4