wiclear-2007-07-19/admin/tools/trackback/index.php

<?php
# ***** BEGIN LICENSE BLOCK *****
# This file is part of WiClear.
# Copyright (c) 2004-2007 David Jobet. All rights
# reserved.
#
# WiClear is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# WiClear 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with DotClear; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301, USA
#
# ***** END LICENSE BLOCK *****

require wc_inc_path.'inc/classes/check_list.class.php';

function createTrackbackTable()
{
  global $wiki;

  $trackback_sql_file = wc_inc_path.'admin/tools/trackback/trackbacks.sql';

  $db_charset = $wiki->con->htmlCharsetToMySqlCharset(wc_charset);
  if ($wiki->con->createTableFromFile($trackback_sql_file, $wiki->prefix, $db_charset) === false)
  {
    echo '<h1>'.tr('Error while creating table').' trackbacks : '.$wiki->con->error.'</h1>';
  }

  $ping_sql_file = wc_inc_path.'admin/tools/trackback/pings.sql';

  if ($wiki->con->createTableFromFile($ping_sql_file, $wiki->prefix, $db_charset) === false)
  {
    echo '<h1>'.tr('Error while creating table').' pings : '.$wiki->con->error.'</h1>';
  }
}

function getAllTrackbacks($offset, $limit)
{
  global $wiki;

  $query = <<<SQL
    SELECT id, url, blog_name, title, excerpt, timestamp, ip
    FROM {prefix}trackbacks
    ORDER BY timestamp DESC
    LIMIT  $limit
    OFFSET $offset
SQL;

  $query = str_replace('{prefix}', $wiki->prefix, $query);
  $result = $wiki->con->select($query);

  return $result;
}

function deleteTrackbacks($idsToDelete)
{
  if (sizeof($idsToDelete) == 0)
  {
    return;
  }

  global $wiki;

  $query = <<<SQL
    DELETE FROM {prefix}trackbacks
    WHERE id in (
SQL;

   $first = true;
   foreach ($idsToDelete as $id)
   {
     if (!$first)
     {
       $query .= ',';
     }
     $query .= $id;
     $first = false;
   }
   $query .= ')';

  $query = str_replace('{prefix}', $wiki->prefix, $query);
  $result = $wiki->con->execute($query);

  return $result;
}

function displayTrackbackAdmin()
{
  $form = new form(createToolURL('trackback'), 'post');
  $form->addFieldSet();
  $form->addLegend(tr('Trackback admin'));
  $form->addHtml('<h1>'.tr('Plugin trackback is installed').'</h1>');

  $trackbacks = getAllTrackbacks(0, 50);

  if (empty($trackbacks))
  {
    $form->addHtml('<h1>'.tr('No trackbacks yet').'</h1>');
  }
  else
  {
    $form->addHtml('<h1>'.tr('Displaying all trackbacks').'</h1>');

    $form->addHtml('<table>');
    $form->addHtml('<tr><th>'.tr('Delete').'</th><th>'.tr('Ban').'</th><th>'.tr('Trackback').'</th></tr>');

    foreach ($trackbacks as $trackback)
    {
      $form->addHtml('<tr>');

      $form->addHtml('<td>');
      $form->addCheckbox('delete_'.$trackback[0], false);
      $form->addHtml('</td>');

      $form->addHtml('<td>');
      $form->addCheckbox('ban_'.$trackback[6], false);
      $form->addHtml('</td>');

      $trackbackDesc = formatDate($trackback[5]).':'.formatHtml($trackback[2]).':<a href="'.htmlentities($trackback[1], ENT_QUOTES).'">'.formatHtml($trackback[3]).'</a>'.formatHtml($trackback[4]);
      $form->addHtml('<td>');
      $form->addHtml($trackbackDesc);
      $form->addHtml('</td>');

      $form->addHtml('</tr>');
    }
    $form->addHtml('</table>');

    $form->addHidden('check', '1');
    $form->addButton('submit', 'submit');
  }

  echo $form->toHtml();
}

function onTrackbackAdmin()
{
  $toBan = array();
  $toDelete = array();
  foreach ($_POST as $key=>$value)
  {
    if (strstr($key, 'ban_') !== false)
    {
      $ip = substr($key, 4, strlen($key) - 4);
      $toBan[] = str_replace('_', '.', $ip);
    }
    if (strstr($key, 'delete_') !== false)
    {
      $toDelete[] = substr($key, 7, strlen($key) - 7);
    }
  }

  deleteTrackbacks($toDelete);
  global $wiki;
  $wiki->addbans($toBan);
}

global $wiki;

// we have some task to perform
if (isset($_POST['check']))
{
  // ok we have to install the table
  $mode = varPost('mode');
  if ($mode == 'create')
  {
    createTrackbackTable();
  }
}

// verify if table "trackbacks" already exist
if ($wiki->con->tableExists($wiki->prefix.'trackbacks') === false)
{
  // table trackbacks does not exist yet
  // we have to create it
  $form = new form(createToolURL('trackback'), 'post');
  $form->addFieldset();
  $form->addLegend(tr('Trackback installer'));
  $form->addHtml('<h1>'.tr('No table [trackbacks]').'</h1>');
  $form->addHtml('<p>'.tr('You appear not to have an SQL table named trackbacks').'.</p>');
  $form->addHtml('<p>'.tr('Should I create it for you ?').'</p>');
  $form->addHidden('check', '1');
  $form->addHidden('mode', 'create');
  $form->addButton('submit', tr('Go'), '');
  echo $form->toHtml();
}
else
{
  if (isset($_POST['check']))
  {
    onTrackbackAdmin();
  }

  displayTrackbackAdmin();
}

?>