Ignore:
Timestamp:
Feb 22, 2007, 2:12:32 AM (17 years ago)
Author:
rvelices
Message:
  • user comments are not saved in the database with htmlspecialchars anymore
  • web service: added the possibility to enter a user comment using the service...
  • new comment functions from picture_comment.inc.php
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/picture_comment.inc.php

    r1819 r1849  
    55// | Copyright (C) 2003-2007 PhpWebGallery Team - http://phpwebgallery.net |
    66// +-----------------------------------------------------------------------+
    7 // | branch        : BSF (Best So Far)
    87// | file          : $Id$
    98// | last update   : $Date$
     
    3130 */
    3231
    33 //returns string action to perform on a new comment: validate, moderate, reject
    34 function user_comment_check($action, $comment, $picture)
    35 {
    36   global $conf,$user;
    37 
    38   if ($action=='reject')
    39     return $action;
    40 
    41   $my_action = $conf['comment_spam_reject'] ? 'reject':'moderate';
    42   if ($action==$my_action)
    43     return $action;
    44 
    45   // we do here only BASIC spam check (plugins can do more)
    46   if ( !$user['is_the_guest'] )
    47     return $action;
    48 
    49   $link_count = preg_match_all( '/https?:\/\//',
    50     $comment['content'], $matches);
    51 
    52   if ( $link_count>$conf['comment_spam_max_links'] )
    53     return $my_action;
    54 
    55   if ( isset($comment['ip']) and $conf['comment_spam_check_ip'] )
    56   {
    57     $rev_ip = implode( '.', array_reverse( explode('.',$comment['ip']) ) );
    58     $lookup = $rev_ip . '.sbl-xbl.spamhaus.org.';
    59     $res = gethostbyname( $lookup );
    60     if ( $lookup != $res )
    61       return $my_action;
    62   }
    63 
    64   return $action;
    65 }
    66 
    67 
    68 
    69 add_event_handler('user_comment_check', 'user_comment_check',
    70   EVENT_HANDLER_PRIORITY_NEUTRAL, 3);
    71 
    72 
    7332// the picture is commentable if it belongs at least to one category which
    7433// is commentable
     
    8948    die ('Session expired');
    9049  }
    91   if (!$conf['comments_validation'] or is_admin())
     50
     51  $comm = array(
     52    'author' => trim( stripslashes(@$_POST['author']) ),
     53    'content' => trim( stripslashes($_POST['content']) ),
     54    'image_id' => $page['image_id'],
     55   );
     56
     57  include_once(PHPWG_ROOT_PATH.'include/functions_comment.inc.php');
     58 
     59  $comment_action = insert_user_comment(
     60      $comm, @$_POST['key'], $page['image_id'], $infos
     61    );
     62
     63  switch ($comment_action)
    9264  {
    93     $comment_action='validate'; //one of validate, moderate, reject
    94   }
    95   else
    96   {
    97     $comment_action='moderate'; //one of validate, moderate, reject
     65    case 'moderate':
     66      array_push( $infos, $lang['comment_to_validate'] );
     67    case 'validate':
     68      array_push( $infos, $lang['comment_added']);
     69      break;
     70    case 'reject':
     71      set_status_header(403);
     72      array_push($infos, l10n('comment_not_added') );
     73      break;
     74    default:
     75      trigger_error('Invalid comment action '.$comment_action, E_USER_WARNING);
    9876  }
    9977
    100   $_POST['content'] = trim( stripslashes($_POST['content']) );
    101 
    102   if ( $user['is_the_guest'] )
     78  foreach ($infos as $info)
    10379  {
    104     $author = empty($_POST['author'])?'guest':$_POST['author'];
    105     // if a guest try to use the name of an already existing user, he must be
    106     // rejected
    107     if ( $author != 'guest' )
    108     {
    109       $query = 'SELECT COUNT(*) AS user_exists';
    110       $query.= ' FROM '.USERS_TABLE;
    111       $query.= ' WHERE '.$conf['user_fields']['username']." = '".$author."'";
    112       $query.= ';';
    113       $row = mysql_fetch_assoc( pwg_query( $query ) );
    114       if ( $row['user_exists'] == 1 )
    115       {
    116         $template->assign_block_vars(
    117           'information',
    118           array('INFORMATION'=>$lang['comment_user_exists']));
    119         $comment_action='reject';
    120       }
    121     }
    122   }
    123   else
    124   {
    125     $author = $user['username'];
    126   }
    127 
    128   $comm = array(
    129     'author' => $author,
    130     'content' => $_POST['content'],
    131     'image_id' => $page['image_id'],
    132     'ip' => $_SERVER['REMOTE_ADDR'],
    133     'agent' => $_SERVER['HTTP_USER_AGENT']
    134    );
    135 
    136   if ($comment_action!='reject' and empty($comm['content']) )
    137   { // empty comment content
    138     $comment_action='reject';
    139   }
    140 
    141   $key = explode(':', @$_POST['key']);
    142   if ( count($key)!=2
    143         or $key[0]>time()-2 // page must have been retrieved more than 2 sec ago
    144         or $key[0]<time()-3600 // 60 minutes expiration
    145         or hash_hmac('md5', $key[0], $conf['secret_key'])!=$key[1]
    146       )
    147   {
    148     $comment_action='reject';
    149   }
    150  
    151   if ($comment_action!='reject' and $conf['anti-flood_time']>0 )
    152   { // anti-flood system
    153     $reference_date = time() - $conf['anti-flood_time'];
    154     $query = 'SELECT id FROM '.COMMENTS_TABLE;
    155     $query.= ' WHERE date > FROM_UNIXTIME('.$reference_date.')';
    156     $query.= " AND author = '".$comm['author']."'";
    157     $query.= ';';
    158     if ( mysql_num_rows( pwg_query( $query ) ) > 0 )
    159     {
    160       $template->assign_block_vars(
     80    $template->assign_block_vars(
    16181        'information',
    162         array('INFORMATION'=>$lang['comment_anti-flood']));
    163       $comment_action='reject';
    164     }
    165   }
    166 
    167   // perform more spam check
    168   $comment_action = trigger_event('user_comment_check',
    169       $comment_action, $comm, $picture['current']
    170     );
    171 
    172   if ( $comment_action!='reject' )
    173   {
    174     list($dbnow) = mysql_fetch_row(pwg_query('SELECT NOW();'));
    175 
    176     $data = $comm;
    177     $data['date'] = $dbnow;
    178     $data['content'] = addslashes(
    179         // this htmlpsecialchars is not good here
    180         htmlspecialchars($comm['content'],ENT_QUOTES)
     82        array( 'INFORMATION'=>$info )
    18183      );
    182 
    183     if ($comment_action=='validate')
    184     {
    185       $data['validated'] = 'true';
    186       $data['validation_date'] = $dbnow;
    187     }
    188     else
    189     {
    190       $data['validated'] = 'false';
    191     }
    192 
    193     include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
    194     $fields = array('author', 'date', 'image_id', 'content', 'validated',
    195                     'validation_date');
    196     mass_inserts(COMMENTS_TABLE, $fields, array($data));
    197     $comm['id'] = mysql_insert_id();
    198 
    199     // information message
    200     $message = $lang['comment_added'];
    201     if ($comment_action!='validate')
    202     {
    203       $message.= '<br />'.$lang['comment_to_validate'];
    204     }
    205     $template->assign_block_vars('information',
    206                                  array('INFORMATION'=>$message));
    207     if ( ($comment_action=='validate' and $conf['email_admin_on_comment'])
    208       or $conf['email_admin_on_comment_validation'] )
    209     {
    210       include_once(PHPWG_ROOT_PATH.'include/functions_mail.inc.php');
    211 
    212       $del_url = get_absolute_root_url().'comments.php?delete='.$comm['id'];
    213 
    214       $content =
    215         'Author: '.$comm['author']."\n"
    216         .'Comment: '.$comm['content']."\n"
    217         .'IP: '.$comm['ip']."\n"
    218         .'Browser: '.$comm['agent']."\n\n"
    219         .'Delete: '.$del_url."\n";
    220 
    221       if ($comment_action!='validate')
    222       {
    223         $content .=
    224           'Validate: '.get_absolute_root_url()
    225           .'comments.php?validate='.$comm['id'];
    226       }
    227 
    228       pwg_mail
    229       (
    230         format_email('administrators', get_webmaster_mail_address()),
    231         array
    232         (
    233           'subject' => 'PWG comment by '.$comm['author'],
    234           'content' => $content,
    235           'Bcc' => get_administrators_email()
    236         )
    237       );
    238     }
    239   }
    240   else
    241   {
    242     set_status_header(403);
    243     $template->assign_block_vars('information',
    244           array('INFORMATION'=>l10n('comment_not_added') )
    245         );
    24684  }
    24785
     
    336174      or ($user['is_the_guest'] and $conf['comments_forall']))
    337175  {
    338     $key = time();
    339     $key .= ':'.hash_hmac('md5', $key, $conf['secret_key']);
     176    include_once(PHPWG_ROOT_PATH.'include/functions_comment.inc.php');
     177    $key = get_comment_post_key($page['image_id']);
    340178    $content = '';
    341179    if ('reject'===@$comment_action)
Note: See TracChangeset for help on using the changeset viewer.