[ Index ] [ Index ]     [ Classes ]     [ Functions ]     [ Variables ]     [ Constants ]

PHP Cross Reference of TXP stable 4.0.6

title

Body

[close]

/textpattern/include/ -> txp_discuss.php (source)

   1  <?php
   2  
   3  /*
   4      This is Textpattern
   5  
   6      Copyright 2005 by Dean Allen
   7      www.textpattern.com
   8      All rights reserved
   9  
  10      Use of this software indicates acceptance of the Textpattern license agreement 
  11  
  12  $HeadURL: http://svn.textpattern.com/releases/4.0.6/source/textpattern/include/txp_discuss.php $
  13  $LastChangedRevision: 2774 $
  14  
  15  */
  16  
  17      if (!defined('txpinterface')) die('txpinterface is undefined.');
  18  
  19      if ($event == 'discuss') {
  20          require_privs('discuss');
  21  
  22          if(!$step or !in_array($step, array('discuss_delete','discuss_save','discuss_list','discuss_edit','ipban_add','discuss_multi_edit','ipban_list','ipban_unban','discuss_change_pageby'))){
  23              discuss_list();
  24          } else $step();
  25      }
  26  
  27  //-------------------------------------------------------------
  28  	function discuss_save()
  29      {
  30          extract(doSlash(gpsa(array('email','name','web','message','ip'))));
  31          extract(array_map('assert_int',gpsa(array('discussid','visible','parentid'))));
  32          safe_update("txp_discuss",
  33              "email   = '$email',
  34               name    = '$name',
  35               web     = '$web',
  36               message = '$message',
  37               visible = $visible",
  38              "discussid = $discussid");
  39          update_comments_count($parentid);
  40          update_lastmod();
  41  
  42          $message = gTxt('comment_updated', array('{id}' => $discussid));
  43  
  44          discuss_list($message);
  45      }
  46  
  47  //-------------------------------------------------------------
  48  
  49  	function short_preview($message)
  50      {
  51          $message = strip_tags($message);
  52          $offset = min(150, strlen($message));
  53  
  54          if (strpos($message, ' ', $offset) !== false)
  55          {
  56              $maxpos = strpos($message,' ',$offset);
  57              $message = substr($message, 0, $maxpos).'&#8230;';
  58          }
  59  
  60          return $message;
  61      }
  62  
  63  //-------------------------------------------------------------
  64  
  65  	function discuss_list($message = '')
  66      {
  67          pagetop(gTxt('list_discussions'), $message);
  68  
  69          echo graf(
  70              '<a href="index.php?event=discuss'.a.'step=ipban_list">'.gTxt('list_banned_ips').'</a>'
  71          , ' style="text-align: center;"');
  72  
  73          extract(get_prefs());
  74  
  75          extract(gpsa(array('sort', 'dir', 'page', 'crit', 'search_method')));
  76  
  77          $dir = ($dir == 'asc') ? 'asc' : 'desc';
  78  
  79          switch ($sort)
  80          {
  81              case 'id':
  82                  $sort_sql = 'discussid '.$dir;
  83              break;
  84  
  85              case 'ip':
  86                  $sort_sql = 'ip '.$dir;
  87              break;
  88  
  89              case 'name':
  90                  $sort_sql = 'name '.$dir;
  91              break;
  92  
  93              case 'email':
  94                  $sort_sql = 'email '.$dir;
  95              break;
  96  
  97              case 'website':
  98                  $sort_sql = 'web '.$dir;
  99              break;
 100  
 101              case 'message':
 102                  $sort_sql = 'message '.$dir;
 103              break;
 104  
 105              case 'status':
 106                  $sort_sql = 'visible '.$dir;
 107              break;
 108  
 109              case 'parent':
 110                  $sort_sql = 'parentid '.$dir;
 111              break;
 112  
 113              default:
 114                  $sort = 'date';
 115                  $sort_sql = 'txp_discuss.posted '.$dir;
 116              break;
 117          }
 118  
 119          if ($sort != 'date') $sort_sql .= ', txp_discuss.posted asc';
 120  
 121          $switch_dir = ($dir == 'desc') ? 'asc' : 'desc';
 122  
 123          $criteria = 1;
 124  
 125          if ($search_method and $crit)
 126          {
 127              $crit_escaped = doSlash($crit);
 128  
 129              $critsql = array(
 130                  'id'      => "discussid = '$crit_escaped'",
 131                  'parent'  => "parentid = '$crit_escaped' OR title like '%$crit_escaped%'",
 132                  'name'    => "name like '%$crit_escaped%'",
 133                  'message' => "message like '%$crit_escaped%'",
 134                  'email'   => "email like '%$crit_escaped%'",
 135                  'website' => "web like '%$crit_escaped%'",
 136                  'ip'      => "ip like '%$crit_escaped%'",
 137              );
 138  
 139              if (array_key_exists($search_method, $critsql))
 140              {
 141                  $criteria = $critsql[$search_method];
 142                  $limit = 500;
 143              }
 144  
 145              else
 146              {
 147                  $search_method = '';
 148                  $crit = '';
 149              }
 150          }
 151  
 152          else
 153          {
 154              $search_method = '';
 155              $crit = '';
 156          }
 157  
 158          $spamq = cs('toggle_show_spam') ? '1=1' : 'visible != '.intval(SPAM);
 159  
 160          $total = getThing(
 161              'SELECT COUNT(*)'.
 162              ' FROM '.safe_pfx_j('txp_discuss').' LEFT JOIN '.safe_pfx_j('textpattern').' ON txp_discuss.parentid = textpattern.ID'. 
 163              ' WHERE '.$spamq.' AND '.$criteria
 164          );
 165  
 166          if ($total < 1)
 167          {
 168              if ($criteria != 1)
 169              {
 170                  echo n.discuss_search_form($crit, $search_method).
 171                      n.graf(gTxt('no_results_found'), ' style="text-align: center;"');
 172              }
 173  
 174              else
 175              {
 176                  echo graf(gTxt('no_comments_recorded'), ' style="text-align: center;"');
 177              }
 178  
 179              return;
 180          }
 181  
 182          $limit = max(@$comment_list_pageby, 15);
 183  
 184          list($page, $offset, $numPages) = pager($total, $limit, $page);
 185  
 186          echo discuss_search_form($crit, $search_method);
 187  
 188          $rs = safe_query(
 189              'SELECT txp_discuss.*, unix_timestamp(txp_discuss.posted) as uPosted, ID as thisid, Section as section, url_title, Title as title, Status, unix_timestamp(textpattern.Posted) as posted'.
 190              ' FROM '.safe_pfx_j('txp_discuss').' LEFT JOIN '.safe_pfx_j('textpattern').' ON txp_discuss.parentid = textpattern.ID'. 
 191              ' WHERE '.$spamq.' AND '.$criteria.
 192              ' ORDER BY '.$sort_sql.
 193              ' LIMIT '.$offset.', '.$limit
 194          );
 195  
 196          if ($rs)
 197          {
 198              echo n.n.'<form name="longform" method="post" action="index.php" onsubmit="return verify(\''.gTxt('are_you_sure').'\')">'.
 199  
 200                  n.startTable('list','','','','90%').
 201  
 202                  n.n.tr(
 203                      column_head('ID', 'id', 'discuss', true, $switch_dir, $crit, $search_method, ('id' == $sort) ? $dir : '').
 204                      column_head('date', 'date', 'discuss', true, $switch_dir, $crit, $search_method, ('date' == $sort) ? $dir : '').
 205                      column_head('name', 'name', 'discuss', true, $switch_dir, $crit, $search_method, ('name' == $sort) ? $dir : '').
 206                      column_head('message', 'message', 'discuss', true, $switch_dir, $crit, $search_method, ('message' == $sort) ? $dir : '').
 207                      column_head('email', 'email', 'discuss', true, $switch_dir, $crit, $search_method, (('email' == $sort) ? "$dir " : '').'discuss_detail').
 208                      column_head('website', 'website', 'discuss', true, $switch_dir, $crit, $search_method, (('website' == $sort) ? "$dir " : '').'discuss_detail').
 209                      column_head('IP', 'ip', 'discuss', true, $switch_dir, $crit, $search_method, (('ip' == $sort) ? "$dir " : '').'discuss_detail').
 210                      column_head('status', 'status', 'discuss', true, $switch_dir, $crit, $search_method, (('status' == $sort) ? "$dir " : '').'discuss_detail').
 211                      column_head('parent', 'parent', 'discuss', true, $switch_dir, $crit, $search_method, ('parent' == $sort) ? $dir : '').
 212                      hCell()
 213                  );
 214  
 215              include_once txpath.'/publish/taghandlers.php';
 216  
 217              while ($a = nextRow($rs))
 218              {
 219                  extract($a);
 220                  $parentid = assert_int($parentid);
 221  
 222                  $edit_url = '?event=discuss'.a.'step=discuss_edit'.a.'discussid='.$discussid.a.'sort='.$sort.
 223                      a.'dir='.$dir.a.'page='.$page.a.'search_method='.$search_method.a.'crit='.$crit;
 224  
 225                  $dmessage = ($visible == SPAM) ? short_preview($message) : $message;
 226  
 227                  switch ($visible)
 228                  {
 229                      case VISIBLE:
 230                          $comment_status = gTxt('visible');
 231                          $row_class = 'visible';
 232                      break;
 233  
 234                      case SPAM:
 235                          $comment_status = gTxt('spam');
 236                          $row_class = 'spam';
 237                      break;
 238  
 239                      case MODERATE:
 240                          $comment_status = gTxt('unmoderated');
 241                          $row_class = 'moderate';
 242                      break;
 243  
 244                      default:
 245                      break;
 246                  }
 247  
 248                  if (empty($thisid))
 249                  {
 250                      $parent = gTxt('article_deleted').' ('.$parentid.')';
 251                      $view = '';
 252                  }
 253  
 254                  else
 255                  {
 256                      $parent_title = empty($title) ? '<em>'.gTxt('untitled').'</em>' : escape_title($title);
 257  
 258                      $parent = href($parent_title, '?event=list'.a.'step=list'.a.'search_method=id'.a.'crit='.$parentid);
 259  
 260                      $view = '';
 261  
 262                      if ($visible == VISIBLE and in_array($Status, array(4,5)))
 263                      {
 264                          $view = n.t.'<li><a href="'.permlinkurl($a).'#c'.$discussid.'">'.gTxt('view').'</a></li>';
 265                      }
 266                  }
 267  
 268                  echo n.n.tr(
 269  
 270                      n.td('<a href="'.$edit_url.'">'.$discussid.'</a>'.
 271                          n.'<ul class="discuss_detail">'.
 272                          n.t.'<li><a href="'.$edit_url.'">'.gTxt('edit').'</a></li>'.
 273                          $view.
 274                          n.'</ul>'
 275                      , 50).
 276  
 277                      td(gTime($uPosted)).
 278                      td(htmlspecialchars(soft_wrap($name, 15))).
 279                      td(short_preview($dmessage)).
 280                      td(htmlspecialchars(soft_wrap($email, 15)), '', 'discuss_detail').
 281                      td(htmlspecialchars(soft_wrap($web, 15)), '', 'discuss_detail').
 282                      td($ip, '', 'discuss_detail').
 283                      td($comment_status, '', 'discuss_detail').
 284                      td($parent).
 285                      td(fInput('checkbox', 'selected[]', $discussid))
 286  
 287                  , ' class="'.$row_class.'"');
 288              }
 289  
 290              echo tr(
 291                  tda(
 292                      toggle_box('discuss_detail'),
 293                      ' colspan="2" style="text-align: left; border: none;"'
 294                  ).
 295                  tda(
 296                      select_buttons().
 297                      discuss_multiedit_form($page, $sort, $dir, $crit, $search_method)
 298                  , ' colspan="9" style="text-align: right; border: none;"')
 299              ).
 300  
 301              endTable().
 302              '</form>'.
 303  
 304              n.cookie_box('show_spam').
 305  
 306              nav_form('discuss', $page, $numPages, $sort, $dir, $crit, $search_method).
 307  
 308              pageby_form('discuss', $comment_list_pageby);
 309          }
 310      }
 311  
 312  //-------------------------------------------------------------
 313  
 314  	function discuss_search_form($crit, $method)
 315      {
 316          $methods =    array(
 317              'id'            => gTxt('ID'),
 318              'parent'  => gTxt('parent'),
 319              'name'        => gTxt('name'),
 320              'message' => gTxt('message'),
 321              'email'        => gTxt('email'),
 322              'website' => gTxt('website'),
 323              'ip'            => gTxt('IP')
 324          );
 325  
 326          return search_form('discuss', 'list', $crit, $methods, $method, 'message');
 327      }
 328  
 329  //-------------------------------------------------------------
 330  
 331  	function discuss_edit()
 332      {
 333          pagetop(gTxt('edit_comment'));
 334  
 335          extract(gpsa(array('discussid', 'sort', 'dir', 'page', 'crit', 'search_method')));
 336  
 337          $discussid = assert_int($discussid);
 338  
 339          $rs = safe_row('*, unix_timestamp(posted) as uPosted', 'txp_discuss', "discussid = $discussid");
 340  
 341          if ($rs)
 342          {
 343              extract($rs);
 344  
 345              $message = htmlspecialchars($message);
 346  
 347              if (fetch('ip', 'txp_discuss_ipban', 'ip', $ip))
 348              {
 349                  $ban_step = 'ipban_unban';
 350                  $ban_text = gTxt('unban');
 351              }
 352  
 353              else
 354              {
 355                  $ban_step = 'ipban_add';
 356                  $ban_text = gTxt('ban');
 357              }
 358  
 359              $ban_link = '[<a href="?event=discuss'.a.'step='.$ban_step.a.'ip='.$ip.
 360                  a.'name='.urlencode($name).a.'discussid='.$discussid.'">'.$ban_text.'</a>]';
 361  
 362              echo form(
 363                  startTable('edit').
 364                  stackRows(
 365  
 366                      fLabelCell('name').
 367                      fInputCell('name', $name),
 368  
 369                      fLabelCell('IP').
 370                      td("$ip $ban_link"),
 371  
 372                      fLabelCell('email').
 373                      fInputCell('email', $email),
 374  
 375                      fLabelCell('website').
 376                      fInputCell('web', $web),
 377  
 378                      fLabelCell('date').
 379                      td(
 380                          safe_strftime('%d %b %Y %X', $uPosted)
 381                      ),
 382  
 383                      tda(gTxt('message')).
 384                      td(
 385                          '<textarea name="message" cols="60" rows="15">'.$message.'</textarea>'
 386                      ),
 387  
 388                      fLabelCell('status').
 389                      td(
 390                          selectInput('visible', array(
 391                              VISIBLE     => gTxt('visible'),
 392                              SPAM         => gTxt('spam'),
 393                              MODERATE => gTxt('unmoderated')
 394                          ), $visible, false)
 395                      ),
 396  
 397                      td().td(fInput('submit', 'step', gTxt('save'), 'publish')),
 398  
 399                      hInput('sort', $sort).
 400                      hInput('dir', $dir).
 401                      hInput('page', $page).
 402                      hInput('crit', $crit).
 403                      hInput('search_method', $search_method).
 404  
 405                      hInput('discussid', $discussid).
 406                      hInput('parentid', $parentid).
 407                      hInput('ip', $ip).
 408  
 409                      eInput('discuss').
 410                      sInput('discuss_save')
 411                  ).
 412  
 413                  endTable()
 414              );
 415          }
 416  
 417          else
 418          {
 419              echo graf(gTxt('comment_not_found'),' style="text-align: center;"');
 420          }
 421      }
 422  
 423  // -------------------------------------------------------------
 424  
 425  	function ipban_add() 
 426      {
 427          extract(gpsa(array('ip', 'name', 'discussid')));
 428          $discussid = assert_int($discussid);
 429  
 430          if (!$ip)
 431          {
 432              return ipban_list(gTxt('cant_ban_blank_ip'));
 433          }
 434  
 435          $ban_exists = fetch('ip', 'txp_discuss_ipban', 'ip', $ip);
 436  
 437          if ($ban_exists)
 438          {
 439              $message = gTxt('ip_already_banned', array('{ip}' => $ip));
 440  
 441              return ipban_list($message);
 442          }
 443  
 444          $rs = safe_insert('txp_discuss_ipban', "
 445              ip = '".doSlash($ip)."', 
 446              name_used = '".doSlash($name)."', 
 447              banned_on_message = $discussid, 
 448              date_banned = now()
 449          ");
 450  
 451          // hide all messages from that IP also
 452          if ($rs)
 453          {
 454              safe_update('txp_discuss', "visible = ".SPAM, "ip = '".doSlash($ip)."'");
 455  
 456              $message = gTxt('ip_banned', array('{ip}' => $ip));
 457  
 458              return ipban_list($message);
 459          }
 460  
 461          ipban_list();
 462      }
 463  
 464  // -------------------------------------------------------------
 465  
 466  	function ipban_unban()
 467      {
 468          $ip = doSlash(gps('ip'));
 469  
 470          $rs = safe_delete('txp_discuss_ipban', "ip = '$ip'");
 471  
 472          if ($rs)
 473          {
 474              $message = gTxt('ip_ban_removed', array('{ip}' => $ip));
 475  
 476              ipban_list($message);
 477          }
 478      }
 479  
 480  // -------------------------------------------------------------
 481  
 482  	function ipban_list($message = '')
 483      {
 484          pageTop(gTxt('list_banned_ips'), $message);
 485  
 486          $rs = safe_rows_start('*, unix_timestamp(date_banned) as uBanned', 'txp_discuss_ipban', 
 487              "1 = 1 order by date_banned desc");
 488  
 489          if ($rs and numRows($rs) > 0)
 490          {
 491              echo startTable('list').
 492                  tr(
 493                      hCell(gTxt('date_banned')).
 494                      hCell(gTxt('IP')).
 495                      hCell(gTxt('name_used')).
 496                      hCell(gTxt('banned_for')).
 497                      hCell()
 498                  );
 499  
 500              while ($a = nextRow($rs))
 501              {
 502                  extract($a);
 503  
 504                  echo tr(
 505                      td(
 506                          safe_strftime('%d %b %Y %I:%M %p', $uBanned)
 507                      , 100).
 508  
 509                      td(
 510                          $ip
 511                      , 100).
 512  
 513                      td(
 514                          $name_used
 515                      , 100).
 516  
 517                      td(
 518                          '<a href="?event=discuss'.a.'step=discuss_edit'.a.'discussid='.$banned_on_message.'">'.
 519                              $banned_on_message.'</a>'
 520                      , 100).
 521  
 522                      td(
 523                          '<a href="?event=discuss'.a.'step=ipban_unban'.a.'ip='.$ip.'">'.gTxt('unban').'</a>'
 524                      )
 525                  );
 526              }
 527  
 528              echo endTable();
 529          }
 530  
 531          else
 532          {
 533              echo graf(gTxt('no_ips_banned'),' style="text-align: center;"');
 534          }
 535      }
 536  
 537  // -------------------------------------------------------------
 538  	function discuss_change_pageby() 
 539      {
 540          event_change_pageby('comment');
 541          discuss_list();
 542      }
 543  
 544  // -------------------------------------------------------------
 545  
 546  	function discuss_multiedit_form($page, $sort, $dir, $crit, $search_method) 
 547      {
 548          $methods = array(
 549              'visible'     => gTxt('show'),
 550              'unmoderated' => gTxt('hide_unmoderated'),
 551              'spam'        => gTxt('hide_spam'),
 552              'ban'         => gTxt('ban_author'),
 553              'delete'      => gTxt('delete'),
 554          );
 555  
 556          return event_multiedit_form('discuss', $methods, $page, $sort, $dir, $crit, $search_method);
 557      }
 558  
 559  // -------------------------------------------------------------
 560  	function discuss_multi_edit() 
 561      {
 562          //FIXME, this method needs some refactoring
 563          
 564          $selected = ps('selected');
 565          $method = ps('edit_method');
 566          $done = array();
 567          if ($selected) {
 568              // Get all articles for which we have to update the count
 569              foreach($selected as $id)
 570                  $ids[] = assert_int($id);
 571              $parentids = safe_column("DISTINCT parentid","txp_discuss","discussid IN (".implode(',',$ids).")");
 572  
 573              $rs = safe_rows_start('*', 'txp_discuss', "discussid IN (".implode(',',$ids).")");
 574              while ($row = nextRow($rs)) {
 575                  extract($row);
 576                  $id = assert_int($discussid);
 577                  $parentids[] = $parentid;
 578  
 579                  if ($method == 'delete') {
 580                      // Delete and if succesful update commnet count 
 581                      if (safe_delete('txp_discuss', "discussid = $id"))
 582                          $done[] = $id;
 583                  }
 584                  elseif ($method == 'ban') {
 585                      // Ban the IP and hide all messages by that IP
 586                      if (!safe_field('ip', 'txp_discuss_ipban', "ip='".doSlash($ip)."'")) {
 587                          safe_insert("txp_discuss_ipban",
 588                              "ip = '".doSlash($ip)."',
 589                              name_used = '".doSlash($name)."',
 590                              banned_on_message = $id,
 591                              date_banned = now()
 592                          ");
 593                          safe_update('txp_discuss',
 594                              "visible = ".SPAM,
 595                              "ip='".doSlash($ip)."'"
 596                          );
 597                      }
 598                      $done[] = $id;
 599                  }
 600                  elseif ($method == 'spam') {
 601                          if (safe_update('txp_discuss',
 602                              "visible = ".SPAM,
 603                              "discussid = $id"
 604                          ))
 605                              $done[] = $id;
 606                  }
 607                  elseif ($method == 'unmoderated') {
 608                          if (safe_update('txp_discuss',
 609                              "visible = ".MODERATE,
 610                              "discussid = $id"
 611                          ))
 612                              $done[] = $id;
 613                  }
 614                  elseif ($method == 'visible') {
 615                          if (safe_update('txp_discuss',
 616                              "visible = ".VISIBLE,
 617                              "discussid = $id"
 618                          ))
 619                              $done[] = $id;
 620                  }
 621                  
 622              }
 623  
 624              $done = join(', ', $done);
 625  
 626              if ($done)
 627              {
 628                  // might as well clean up all comment counts while we're here.
 629                  clean_comment_counts($parentids);
 630  
 631                  $messages = array(
 632                      'delete'            => gTxt('comments_deleted', array('{list}' => $done)),
 633                      'ban'                    => gTxt('ips_banned', array('{list}' => $done)),
 634                      'spam'                => gTxt('comments_marked_spam', array('{list}' => $done)),
 635                      'unmoderated' => gTxt('comments_marked_unmoderated', array('{list}' => $done)),
 636                      'visible'            => gTxt('comments_marked_visible', array('{list}' => $done))
 637                  );
 638  
 639                  update_lastmod();
 640  
 641                  return discuss_list($messages[$method]);
 642              }
 643          }
 644  
 645          return discuss_list();
 646      }
 647  
 648  ?>


Generated: Mon Feb 18 03:42:45 2008 Cross-referenced by PHPXref 0.7