[ Index ]

PHP Cross Reference of Textpattern 4.0.8

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: https://textpattern.googlecode.com/svn/releases/4.0.8/source/textpattern/include/txp_discuss.php $
  13  $LastChangedRevision: 3071 $
  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          global $comment_list_pageby;
  68  
  69          pagetop(gTxt('list_discussions'), $message);
  70  
  71          echo graf(
  72              '<a href="index.php?event=discuss'.a.'step=ipban_list">'.gTxt('list_banned_ips').'</a>'
  73          , ' style="text-align: center;"');
  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'".(intval($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          $counts = getRows(
 159              'SELECT visible, COUNT(*) AS c'.
 160              ' FROM '.safe_pfx_j('txp_discuss').' LEFT JOIN '.safe_pfx_j('textpattern').' ON txp_discuss.parentid = textpattern.ID'.
 161              ' WHERE '. $criteria.' GROUP BY visible'
 162          );
 163  
 164          $count[SPAM] = $count[MODERATE] = $count[VISIBLE] = 0;
 165  
 166          if ($counts) foreach($counts as $c)
 167          {
 168              $count[$c['visible']] = $c['c'];
 169          }
 170          
 171          // grand total comment count
 172          $total = $count[SPAM] + $count[MODERATE] + $count[VISIBLE];
 173  
 174          if ($total < 1)
 175          {
 176              if ($criteria != 1)
 177              {
 178                  echo n.discuss_search_form($crit, $search_method).
 179                      n.graf(gTxt('no_results_found'), ' class="indicator"');
 180              }
 181  
 182              else
 183              {
 184                  echo graf(gTxt('no_comments_recorded'), ' class="indicator"');
 185              }
 186  
 187              return;
 188          }
 189  
 190          // paging through displayed comments
 191          $total = ((cs('toggle_show_spam')) ? $count[SPAM] : 0) + $count[MODERATE] + $count[VISIBLE];        
 192          $limit = max($comment_list_pageby, 15);
 193          list($page, $offset, $numPages) = pager($total, $limit, $page);
 194  
 195          echo discuss_search_form($crit, $search_method);
 196  
 197          $spamq = cs('toggle_show_spam') ? '1=1' : 'visible != '.intval(SPAM);
 198  
 199          $rs = safe_query(
 200              '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'.
 201              ' FROM '.safe_pfx_j('txp_discuss').' LEFT JOIN '.safe_pfx_j('textpattern').' ON txp_discuss.parentid = textpattern.ID'.
 202              ' WHERE '.$spamq.' AND '.$criteria.
 203              ' ORDER BY '.$sort_sql.
 204              ' LIMIT '.$offset.', '.$limit
 205          );
 206  
 207          if ($rs)
 208          {
 209              echo n.n.'<form name="longform" method="post" action="index.php" onsubmit="return verify(\''.gTxt('are_you_sure').'\')">'.
 210  
 211                  n.startTable('list','','','','90%').
 212  
 213                  n.n.tr(
 214                      column_head('ID', 'id', 'discuss', true, $switch_dir, $crit, $search_method, ('id' == $sort) ? $dir : '').
 215                      column_head('date', 'date', 'discuss', true, $switch_dir, $crit, $search_method, ('date' == $sort) ? $dir : '').
 216                      column_head('name', 'name', 'discuss', true, $switch_dir, $crit, $search_method, ('name' == $sort) ? $dir : '').
 217                      column_head('message', 'message', 'discuss', true, $switch_dir, $crit, $search_method, ('message' == $sort) ? $dir : '').
 218                      column_head('email', 'email', 'discuss', true, $switch_dir, $crit, $search_method, (('email' == $sort) ? "$dir " : '').'discuss_detail').
 219                      column_head('website', 'website', 'discuss', true, $switch_dir, $crit, $search_method, (('website' == $sort) ? "$dir " : '').'discuss_detail').
 220                      column_head('IP', 'ip', 'discuss', true, $switch_dir, $crit, $search_method, (('ip' == $sort) ? "$dir " : '').'discuss_detail').
 221                      column_head('status', 'status', 'discuss', true, $switch_dir, $crit, $search_method, (('status' == $sort) ? "$dir " : '').'discuss_detail').
 222                      column_head('parent', 'parent', 'discuss', true, $switch_dir, $crit, $search_method, ('parent' == $sort) ? $dir : '').
 223                      hCell()
 224                  );
 225  
 226              include_once txpath.'/publish/taghandlers.php';
 227  
 228              while ($a = nextRow($rs))
 229              {
 230                  extract($a);
 231                  $parentid = assert_int($parentid);
 232  
 233                  $edit_url = '?event=discuss'.a.'step=discuss_edit'.a.'discussid='.$discussid.a.'sort='.$sort.
 234                      a.'dir='.$dir.a.'page='.$page.a.'search_method='.$search_method.a.'crit='.$crit;
 235  
 236                  $dmessage = ($visible == SPAM) ? short_preview($message) : $message;
 237  
 238                  switch ($visible)
 239                  {
 240                      case VISIBLE:
 241                          $comment_status = gTxt('visible');
 242                          $row_class = 'visible';
 243                      break;
 244  
 245                      case SPAM:
 246                          $comment_status = gTxt('spam');
 247                          $row_class = 'spam';
 248                      break;
 249  
 250                      case MODERATE:
 251                          $comment_status = gTxt('unmoderated');
 252                          $row_class = 'moderate';
 253                      break;
 254  
 255                      default:
 256                      break;
 257                  }
 258  
 259                  if (empty($thisid))
 260                  {
 261                      $parent = gTxt('article_deleted').' ('.$parentid.')';
 262                      $view = '';
 263                  }
 264  
 265                  else
 266                  {
 267                      $parent_title = empty($title) ? '<em>'.gTxt('untitled').'</em>' : escape_title($title);
 268  
 269                      $parent = href($parent_title, '?event=article'.a.'step=edit'.a.'ID='.$parentid);
 270  
 271                      $view = '';
 272  
 273                      if ($visible == VISIBLE and in_array($Status, array(4,5)))
 274                      {
 275                          $view = n.t.'<li><a href="'.permlinkurl($a).'#c'.$discussid.'">'.gTxt('view').'</a></li>';
 276                      }
 277                  }
 278  
 279                  echo n.n.tr(
 280  
 281                      n.td('<a href="'.$edit_url.'">'.$discussid.'</a>'.
 282                          n.'<ul class="discuss_detail">'.
 283                          n.t.'<li><a href="'.$edit_url.'">'.gTxt('edit').'</a></li>'.
 284                          $view.
 285                          n.'</ul>'
 286                      , 50).
 287  
 288                      td(gTime($uPosted)).
 289                      td(htmlspecialchars(soft_wrap($name, 15))).
 290                      td(short_preview($dmessage)).
 291                      td(htmlspecialchars(soft_wrap($email, 15)), '', 'discuss_detail').
 292                      td(htmlspecialchars(soft_wrap($web, 15)), '', 'discuss_detail').
 293                      td($ip, '', 'discuss_detail').
 294                      td($comment_status, '', 'discuss_detail').
 295                      td($parent).
 296                      td(fInput('checkbox', 'selected[]', $discussid))
 297  
 298                  , ' class="'.$row_class.'"');
 299              }
 300              
 301              if (empty($message))
 302                  echo tr(tda(gTxt('just_spam_results_found'),' colspan="9" style="text-align: left; border: none;"'));
 303              
 304              echo tr(
 305                  tda(
 306                      toggle_box('discuss_detail'),
 307                      ' colspan="2" style="text-align: left; border: none;"'
 308                  ).
 309                  tda(
 310                      select_buttons().
 311                      discuss_multiedit_form($page, $sort, $dir, $crit, $search_method)
 312                  , ' colspan="9" style="text-align: right; border: none;"')
 313              ).
 314  
 315              endTable().
 316              '</form>'.
 317  
 318              n.cookie_box('show_spam').
 319  
 320              nav_form('discuss', $page, $numPages, $sort, $dir, $crit, $search_method).
 321  
 322              pageby_form('discuss', $comment_list_pageby);
 323          }
 324      }
 325  
 326  //-------------------------------------------------------------
 327  
 328  	function discuss_search_form($crit, $method)
 329      {
 330          $methods =    array(
 331              'id'            => gTxt('ID'),
 332              'parent'  => gTxt('parent'),
 333              'name'        => gTxt('name'),
 334              'message' => gTxt('message'),
 335              'email'        => gTxt('email'),
 336              'website' => gTxt('website'),
 337              'ip'            => gTxt('IP')
 338          );
 339  
 340          return search_form('discuss', 'list', $crit, $methods, $method, 'message');
 341      }
 342  
 343  //-------------------------------------------------------------
 344  
 345  	function discuss_edit()
 346      {
 347          pagetop(gTxt('edit_comment'));
 348  
 349          extract(gpsa(array('discussid', 'sort', 'dir', 'page', 'crit', 'search_method')));
 350  
 351          $discussid = assert_int($discussid);
 352  
 353          $rs = safe_row('*, unix_timestamp(posted) as uPosted', 'txp_discuss', "discussid = $discussid");
 354  
 355          if ($rs)
 356          {
 357              extract($rs);
 358  
 359              $message = htmlspecialchars($message);
 360  
 361              if (fetch('ip', 'txp_discuss_ipban', 'ip', $ip))
 362              {
 363                  $ban_step = 'ipban_unban';
 364                  $ban_text = gTxt('unban');
 365              }
 366  
 367              else
 368              {
 369                  $ban_step = 'ipban_add';
 370                  $ban_text = gTxt('ban');
 371              }
 372  
 373              $ban_link = '[<a href="?event=discuss'.a.'step='.$ban_step.a.'ip='.$ip.
 374                  a.'name='.urlencode($name).a.'discussid='.$discussid.'">'.$ban_text.'</a>]';
 375  
 376              echo form(
 377                  startTable('edit').
 378                  stackRows(
 379  
 380                      fLabelCell('name').
 381                      fInputCell('name', $name),
 382  
 383                      fLabelCell('IP').
 384                      td("$ip $ban_link"),
 385  
 386                      fLabelCell('email').
 387                      fInputCell('email', $email),
 388  
 389                      fLabelCell('website').
 390                      fInputCell('web', $web),
 391  
 392                      fLabelCell('date').
 393                      td(
 394                          safe_strftime('%d %b %Y %X', $uPosted)
 395                      ),
 396  
 397                      tda(gTxt('message')).
 398                      td(
 399                          '<textarea name="message" cols="60" rows="15">'.$message.'</textarea>'
 400                      ),
 401  
 402                      fLabelCell('status').
 403                      td(
 404                          selectInput('visible', array(
 405                              VISIBLE     => gTxt('visible'),
 406                              SPAM         => gTxt('spam'),
 407                              MODERATE => gTxt('unmoderated')
 408                          ), $visible, false)
 409                      ),
 410  
 411                      td().td(fInput('submit', 'step', gTxt('save'), 'publish')),
 412  
 413                      hInput('sort', $sort).
 414                      hInput('dir', $dir).
 415                      hInput('page', $page).
 416                      hInput('crit', $crit).
 417                      hInput('search_method', $search_method).
 418  
 419                      hInput('discussid', $discussid).
 420                      hInput('parentid', $parentid).
 421                      hInput('ip', $ip).
 422  
 423                      eInput('discuss').
 424                      sInput('discuss_save')
 425                  ).
 426  
 427                  endTable()
 428              );
 429          }
 430  
 431          else
 432          {
 433              echo graf(gTxt('comment_not_found'),' class="indicator"');
 434          }
 435      }
 436  
 437  // -------------------------------------------------------------
 438  
 439  	function ipban_add()
 440      {
 441          extract(gpsa(array('ip', 'name', 'discussid')));
 442          $discussid = assert_int($discussid);
 443  
 444          if (!$ip)
 445          {
 446              return ipban_list(gTxt('cant_ban_blank_ip'));
 447          }
 448  
 449          $ban_exists = fetch('ip', 'txp_discuss_ipban', 'ip', $ip);
 450  
 451          if ($ban_exists)
 452          {
 453              $message = gTxt('ip_already_banned', array('{ip}' => $ip));
 454  
 455              return ipban_list($message);
 456          }
 457  
 458          $rs = safe_insert('txp_discuss_ipban', "
 459              ip = '".doSlash($ip)."',
 460              name_used = '".doSlash($name)."',
 461              banned_on_message = $discussid,
 462              date_banned = now()
 463          ");
 464  
 465          // hide all messages from that IP also
 466          if ($rs)
 467          {
 468              safe_update('txp_discuss', "visible = ".SPAM, "ip = '".doSlash($ip)."'");
 469  
 470              $message = gTxt('ip_banned', array('{ip}' => $ip));
 471  
 472              return ipban_list($message);
 473          }
 474  
 475          ipban_list();
 476      }
 477  
 478  // -------------------------------------------------------------
 479  
 480  	function ipban_unban()
 481      {
 482          $ip = doSlash(gps('ip'));
 483  
 484          $rs = safe_delete('txp_discuss_ipban', "ip = '$ip'");
 485  
 486          if ($rs)
 487          {
 488              $message = gTxt('ip_ban_removed', array('{ip}' => $ip));
 489  
 490              ipban_list($message);
 491          }
 492      }
 493  
 494  // -------------------------------------------------------------
 495  
 496  	function ipban_list($message = '')
 497      {
 498          pageTop(gTxt('list_banned_ips'), $message);
 499  
 500          $rs = safe_rows_start('*, unix_timestamp(date_banned) as uBanned', 'txp_discuss_ipban',
 501              "1 = 1 order by date_banned desc");
 502  
 503          if ($rs and numRows($rs) > 0)
 504          {
 505              echo startTable('list').
 506                  tr(
 507                      hCell(gTxt('date_banned')).
 508                      hCell(gTxt('IP')).
 509                      hCell(gTxt('name_used')).
 510                      hCell(gTxt('banned_for')).
 511                      hCell()
 512                  );
 513  
 514              while ($a = nextRow($rs))
 515              {
 516                  extract($a);
 517  
 518                  echo tr(
 519                      td(
 520                          safe_strftime('%d %b %Y %I:%M %p', $uBanned)
 521                      , 100).
 522  
 523                      td(
 524                          $ip
 525                      , 100).
 526  
 527                      td(
 528                          $name_used
 529                      , 100).
 530  
 531                      td(
 532                          '<a href="?event=discuss'.a.'step=discuss_edit'.a.'discussid='.$banned_on_message.'">'.
 533                              $banned_on_message.'</a>'
 534                      , 100).
 535  
 536                      td(
 537                          '<a href="?event=discuss'.a.'step=ipban_unban'.a.'ip='.$ip.'">'.gTxt('unban').'</a>'
 538                      )
 539                  );
 540              }
 541  
 542              echo endTable();
 543          }
 544  
 545          else
 546          {
 547              echo graf(gTxt('no_ips_banned'),' class="indicator"');
 548          }
 549      }
 550  
 551  // -------------------------------------------------------------
 552  	function discuss_change_pageby()
 553      {
 554          event_change_pageby('comment');
 555          discuss_list();
 556      }
 557  
 558  // -------------------------------------------------------------
 559  
 560  	function discuss_multiedit_form($page, $sort, $dir, $crit, $search_method)
 561      {
 562          $methods = array(
 563              'visible'     => gTxt('show'),
 564              'unmoderated' => gTxt('hide_unmoderated'),
 565              'spam'        => gTxt('hide_spam'),
 566              'ban'         => gTxt('ban_author'),
 567              'delete'      => gTxt('delete'),
 568          );
 569  
 570          return event_multiedit_form('discuss', $methods, $page, $sort, $dir, $crit, $search_method);
 571      }
 572  
 573  // -------------------------------------------------------------
 574  	function discuss_multi_edit()
 575      {
 576          //FIXME, this method needs some refactoring
 577  
 578          $selected = ps('selected');
 579          $method = ps('edit_method');
 580          $done = array();
 581  
 582          if ($selected and is_array($selected))
 583          {
 584              // Get all articles for which we have to update the count
 585              foreach($selected as $id)
 586                  $ids[] = assert_int($id);
 587              $parentids = safe_column("DISTINCT parentid","txp_discuss","discussid IN (".implode(',',$ids).")");
 588  
 589              $rs = safe_rows_start('*', 'txp_discuss', "discussid IN (".implode(',',$ids).")");
 590              while ($row = nextRow($rs)) {
 591                  extract($row);
 592                  $id = assert_int($discussid);
 593                  $parentids[] = $parentid;
 594  
 595                  if ($method == 'delete') {
 596                      // Delete and if succesful update commnet count
 597                      if (safe_delete('txp_discuss', "discussid = $id"))
 598                          $done[] = $id;
 599                  }
 600                  elseif ($method == 'ban') {
 601                      // Ban the IP and hide all messages by that IP
 602                      if (!safe_field('ip', 'txp_discuss_ipban', "ip='".doSlash($ip)."'")) {
 603                          safe_insert("txp_discuss_ipban",
 604                              "ip = '".doSlash($ip)."',
 605                              name_used = '".doSlash($name)."',
 606                              banned_on_message = $id,
 607                              date_banned = now()
 608                          ");
 609                          safe_update('txp_discuss',
 610                              "visible = ".SPAM,
 611                              "ip='".doSlash($ip)."'"
 612                          );
 613                      }
 614                      $done[] = $id;
 615                  }
 616                  elseif ($method == 'spam') {
 617                          if (safe_update('txp_discuss',
 618                              "visible = ".SPAM,
 619                              "discussid = $id"
 620                          ))
 621                              $done[] = $id;
 622                  }
 623                  elseif ($method == 'unmoderated') {
 624                          if (safe_update('txp_discuss',
 625                              "visible = ".MODERATE,
 626                              "discussid = $id"
 627                          ))
 628                              $done[] = $id;
 629                  }
 630                  elseif ($method == 'visible') {
 631                          if (safe_update('txp_discuss',
 632                              "visible = ".VISIBLE,
 633                              "discussid = $id"
 634                          ))
 635                              $done[] = $id;
 636                  }
 637  
 638              }
 639  
 640              $done = join(', ', $done);
 641  
 642              if ($done)
 643              {
 644                  // might as well clean up all comment counts while we're here.
 645                  clean_comment_counts($parentids);
 646  
 647                  $messages = array(
 648                      'delete'            => gTxt('comments_deleted', array('{list}' => $done)),
 649                      'ban'                    => gTxt('ips_banned', array('{list}' => $done)),
 650                      'spam'                => gTxt('comments_marked_spam', array('{list}' => $done)),
 651                      'unmoderated' => gTxt('comments_marked_unmoderated', array('{list}' => $done)),
 652                      'visible'            => gTxt('comments_marked_visible', array('{list}' => $done))
 653                  );
 654  
 655                  update_lastmod();
 656  
 657                  return discuss_list($messages[$method]);
 658              }
 659          }
 660  
 661          return discuss_list();
 662      }
 663  
 664  ?>


Generated: Thu May 21 23:03:01 2009 Cross-referenced by PHPXref 0.7