DIR : /home/kozerus/public_html/dgs/scripts/replace_emails_posts.php

/home/kozerus/public_html/dgs/scripts

<?php
/*
Dragon Go Server
Copyright (C) 2001-  Jens-Uwe Gaspar

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/


// Checks and replaces email-addresses in forum posts

chdir('..');
require_once 'include/std_functions.php';
require_once 'include/gui_functions.php';
require_once 'include/table_columns.php';
require_once 'include/form_functions.php';
require_once 'include/classlib_user.php';
require_once 'forum/forum_functions.php';

define('SEPLINE', "\n<p><hr>\n");
define('WRAPCOLS', 100);
define('RX_EMAIL', '([-+_\\.a-z0-9]+@[-\\.a-z0-9]+)');

const MODERATED_EMAIL = '[***moderated_email***]';


{
   global $base_path;

   $beginall = getmicrotime();
   disable_cache();
   connect2mysql();
   set_time_limit(0); // don't want script-break during "transaction" with multi-db-queries

   $logged_in = who_is_logged( $player_row, LOGIN_DEFAULT_OPTS_ADM_OPS );
   if ( !$logged_in )
      error('login_if_not_logged_in', 'scripts.replace_emails_posts');
   if ( $player_row['ID'] <= GUESTS_ID_MAX )
      error('not_allowed_for_guest', 'scripts.replace_emails_posts');
   if ( !(@$player_row['admin_level'] & ADMIN_DATABASE) )
      error('adminlevel_too_low', 'scripts.replace_emails_posts');

   $page = "replace_emails_posts.php";
   $pageUrl = $base_path."scripts/$page";

   $post_id = (int)get_request_arg( 'pid', 0 );
   $user_id = get_request_arg('user');
   $incl_keep_email = (bool)get_request_arg('incl_keep_email', '');
   $repl_email = @$_REQUEST['email'];

   if ( $toggle_pid = (int)@$_REQUEST['toggle_keep_email'] )
   {
      $success = toggle_keep_email($toggle_pid);
      print_page( $success, 'Toggled KEEP_EMAIL flag for post-id ' . $toggle_pid );
   }
   elseif ( ($replace_pid = (int)@$_REQUEST['repl_pid']) && preg_match( "/^(".RX_EMAIL.")$/i", $repl_email ) )
   {
      $success = replace_email( $replace_pid, $repl_email );
      print_page( $success, sprintf( 'Mail [%s] has been replaced for post-id %s', $repl_email, $replace_pid ) );
   }


   start_html( 'replace_emails_posts', true);

   $form = new Form( 'ReplaceEmailsPosts', $page, FORM_GET );

   $form->add_row( array(
         'DESCRIPTION', 'Post-ID',
         'TEXTINPUT',   'pid', 12, 12, $post_id,
         'TEXT',        '0 (=all) | forum-post-id', ));
   $form->add_row( array(
         'DESCRIPTION', 'Author',
         'TEXTINPUT',   'user', 12, 12, $user_id,
         'TEXT',        '0 (=all) | user-id | =user-handle', ));
   $form->add_row( array(
         'DESCRIPTION', 'Option',
         'CHECKBOX',    'incl_keep_email', 1, "Include posts with \"keeping email\"", $incl_keep_email, ));

   $form->add_empty_row();
   $form->add_row( array(
         'SUBMITBUTTON', 'check_it', 'Check Only', ));

   echo "<p><h3 class=center>Replace emails in forum posts:</h3>\n";
   $form->echo_string();

//-----------------

   echo "On ", date(DATE_FMT5, $NOW);

   if ( @$_REQUEST['check_it'] )
   {
      if ( is_numeric( $user_id ) && $user_id > 0 )
         $user = User::load_user( $user_id );
      elseif ( strpos( $user_id, 0, 1 ) === '=' )
         $user = User::load_user_by_handle( strpos( $user_id, 1 ) );
      else
         $user = null;

      if ( is_null( $user ) && $user_id !== '' )
         echo span( 'ErrorMsg', sprintf( "<p>NOTE: User [%s] not found!</p>\n", substr( $user_id, 1 ) ) );
      else
      {
         $find_uid = is_null( $user ) ? 0 : $user->ID;
         $todo = find_posts_with_emails( $post_id, $find_uid, $incl_keep_email );
         show_posts_with_emails( $todo );
      }
   }

   echo SEPLINE;
   echo "\n<br>Needed (all): " . sprintf("%1.3fs", (getmicrotime() - $beginall));
   end_html();
}//main


// \return arr( pid => [ Handle, Time/Forum_ID/Thread_ID/Flags, Text, Emails=[email1, ...] ], ... )
function find_posts_with_emails( $arg_post_id, $arg_uid, $include_kept_emails )
{
   $dbgmsg = "replace_emails_posts.find_posts_with_emails($arg_post_id,$arg_uid)";
   echo SEPLINE;

   // find posts with (potential) email-addresses
   $qsql = new QuerySQL(
      SQLP_FIELDS, 'P.*', 'UNIX_TIMESTAMP(P.Time) AS X_Time', 'PL.Handle',
      SQLP_FROM, 'Posts AS P', 'INNER JOIN Players AS PL ON PL.ID=P.User_ID',
      SQLP_WHERE, "P.Text LIKE '%@%'",
      SQLP_ORDER, 'P.Time ASC'
   );
   if ( $arg_post_id > 0 )
      $qsql->add_part( SQLP_WHERE, "P.ID=$arg_post_id" );
   if ( $arg_uid )
      $qsql->add_part( SQLP_WHERE, "P.User_ID=$arg_uid" );
   if ( !$include_kept_emails )
      $qsql->add_part( SQLP_WHERE, '(P.Flags & '.FPOST_FLAG_KEEP_EMAIL.')=0' );
   $result = db_query( "$dbgmsg.find_posts", $qsql->get_select() );

   // find posts with emails & replacement
   $todo = array();
   while ( $row = mysql_fetch_array($result) )
   {
      $pid = $row['ID'];
      $text = $row['Text'];

      $cnt = preg_match_all( "/".RX_EMAIL."/i", $text, $matches );
      if ($cnt !== false && $cnt > 0)
      {
         $todo[$pid] = array(
            'Handle' => $row['Handle'],
            'Time' => $row['X_Time'],
            'Forum_ID' => $row['Forum_ID'],
            'Thread_ID' => $row['Thread_ID'],
            'Flags' => $row['Flags'],
            'Text' => $text,
            'Emails' => $matches[0],
         );
      }

   }
   mysql_free_result($result);

   return $todo;
}//find_posts_with_emails

function show_posts_with_emails( array $todo )
{
   global $base_path, $page, $pageUrl;
   $trg = 'target="chg_email"';

   $table = new Table( 'ReplaceEmailsPosts', $page );
   $table->add_tablehead( 1, 'Post', 'Right' );
   $table->add_tablehead( 2, 'Author' );
   $table->add_tablehead( 3, 'Created & Flags', 'Center' );
   $table->add_tablehead( 4, 'Emails' );
   $table->add_tablehead( 5, 'Text' );

   $cnt_posts = 0;
   foreach ($todo as $pid => $arr) {
      $flags_str = ($arr['Flags'] & FPOST_FLAG_KEEP_EMAIL) ? "KEEP_EMAIL" : NO_VALUE;
      $arr_repl_emails = array();
      foreach ($arr['Emails'] as $email)
         $arr_repl_emails[] =
            anchor( $pageUrl."?repl_pid=$pid" . URI_AMP . "email=" . urlencode( $email ), 'Replace', '', $trg )
            . " $email";

      $row_str = array(
         1 => anchor( $base_path . 'forum/read.php?forum=' . $arr['Forum_ID'] . URI_AMP . 'thread=' . $arr['Thread_ID'] . URI_AMP
            . 'moderator=y#' . $pid, $pid, '', $trg ),
         2 => user_reference( REF_LINK, 1, '', '=' . $arr['Handle'], $arr['Handle'], '' ),
         3 => date( DATE_FMT, $arr['Time'] ) . "<br>\n" . span( 'blue', $flags_str )
            . "<br>\n"
            . anchor( $pageUrl . "?toggle_keep_email=$pid", 'Toggle KEEP_EMAIL', '', $trg  ),
         4 => join( "<br><br>\n", $arr_repl_emails ),
         5 => make_html_safe( wordwrap( $arr['Text'], WRAPCOLS ), true, RX_EMAIL ),
      );
      $table->add_row( $row_str );
      $cnt_posts++;
   }

   echo span( 'bold blue', sprintf( "Found %s posts with emails:<br>\n", $cnt_posts ) ),
      "<br>\n",
      "<span class=\"blue\">",
      "Action: Click on 'Toggle KEEP_EMAIL' to toggle respective flag (if flag is set, email should be untouched in post).<br>\n",
      "Action: Click on 'Replace email' to replace only the selected email in the respective post.<br>\n",
      "</span><br>\n",
      "<br>\n";

   $table->echo_table();
}//show_posts_with_emails

function toggle_keep_email( $pid )
{
   return db_query( "scripts.replace_emails_posts.toggle_keep_email($pid).upd_post",
      "UPDATE Posts SET Flags=Flags ^ " . FPOST_FLAG_KEEP_EMAIL . " WHERE ID=$pid LIMIT 1" );
}

function replace_email ( $pid, $email )
{
   $dbgmsg = "scripts.replace_emails_posts.replace_email($pid,$email)";
   $row = mysql_single_fetch( $dbgmsg . ".find_post",
      "SELECT Text FROM Posts WHERE ID=$pid LIMIT 1" );
   if ( !$row )
      return false;

   $newtext = str_replace( $email, MODERATED_EMAIL, $row['Text'] );
   $success = db_query( $dbgmsg . ".upd_post",
      sprintf( "UPDATE Posts SET Text = '%s' WHERE ID =%s LIMIT 1", mysql_addslashes($newtext), $pid ) );
   return $success;
}

function print_page( $success, $msg )
{
   start_html( 'replace_emails_posts', true);
   echo "<br><br>\n", span( 'larger bold ' . ($success ? 'blue' : 'darkred'), $msg );
   end_html();
   exit(0);
}

?>
//


koh5_pano



Your browser does not support the HTML5 canvas element.


Drag mouse to navigate.

Navigation





17.Aug.2010, Martin Wengenmayer