Changeset 402 for trunk/picture.php


Ignore:
Timestamp:
Mar 30, 2004, 12:40:21 AM (20 years ago)
Author:
z0rglub
Message:

code refactoring

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/picture.php

    r394 r402  
    7070}
    7171
    72 //---------------------------------------- prev, current & next picture management
    73 $picture=array();
    74 $picture['prev']['name']='';
    75 $picture['next']['name']='';
    76 $picture['prev']['thumbnail']='';
    77 $picture['next']['thumbnail']='';
    78 $picture['prev']['url']='';
    79 $picture['next']['url']='';
     72//------------------------------------- prev, current & next picture management
     73$picture = array();
     74$picture['prev']['name'] = '';
     75$picture['next']['name'] = '';
     76$picture['prev']['thumbnail'] = '';
     77$picture['next']['thumbnail'] = '';
     78$picture['prev']['url'] = '';
     79$picture['next']['url'] = '';
    8080
    8181$next = $page['num'] + 1;
    8282$prev = $page['num'] - 1;
    8383
    84 if ( $page['num'] == $page['cat_nb_images']-1)
     84if ( $page['num'] == $page['cat_nb_images'] - 1 )
    8585{
    8686  $next = 0;
    8787}
    8888
    89 $query = 'SELECT * FROM '.IMAGES_TABLE;
     89$query = 'SELECT *';
     90$query.= ' FROM '.IMAGES_TABLE;
    9091$query.= ' INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic';
    9192$query.= ' ON id=ic.image_id';
     
    9394$query.= $conf['order_by'];
    9495
    95 if ($prev <0)
     96if ( $prev < 0 )
     97{
    9698  $query.= ' LIMIT 0,2';
     99}
    97100else
     101{
    98102  $query.= ' LIMIT '.$prev.',3';
    99  
     103}
    100104$query.= ';';
    101105
    102106$result = mysql_query( $query );
    103 $nb_row = mysql_num_rows($result);
     107$nb_row = mysql_num_rows( $result );
    104108$index = array('prev','current','next');
    105 for ($i=0; $i<$nb_row;$i++)
     109
     110for ( $i = 0; $i < $nb_row; $i++ )
    106111{
    107112  $j=($prev<0)?$index[$i+1]:$index[$i];
     
    116121  $cat_directory = $array_cat_directories[$row['storage_category_id']];
    117122  $file = substr ( $row['file'], 0, strrpos ( $row['file'], ".") );
     123
    118124  $picture[$j]['src'] = $cat_directory.$row['file'];
     125
    119126  $picture[$j]['thumbnail'] = $cat_directory.'thumbnail/';
    120   $picture[$j]['thumbnail'].= $conf['prefix_thumbnail'].$file.".".$row['tn_ext'];
    121  
    122   if (!empty($row['name']))
     127  $picture[$j]['thumbnail'].= $conf['prefix_thumbnail'].$file;
     128  $picture[$j]['thumbnail'].= '.'.$row['tn_ext'];
     129 
     130  if ( !empty( $row['name'] ) )
    123131  {
    124132    $picture[$j]['name'] = $row['name'];
     
    126134  else
    127135  {
    128     $picture[$j]['name'] = str_replace( "_", " ",$file);
     136    $picture[$j]['name'] = str_replace( '_', ' ', $file );
    129137  }
    130138
     
    133141  if ( $page['cat'] == 'search' )
    134142  {
    135     $picture[$j]['url'].= "&amp;search=".$_GET['search'].'&amp;mode='.$_GET['mode'];
     143    $picture[$j]['url'].= '&amp;search='.$_GET['search'];
     144    $picture[$j]['url'].= '&amp;mode='.$_GET['mode'];
    136145  }
    137146}
     
    144153}
    145154
    146 $url_admin = PHPWG_ROOT_PATH.'admin.php?page=picture_modify&amp;cat_id='.$page['cat'];
     155$url_admin = PHPWG_ROOT_PATH.'admin.php?page=picture_modify';
     156$url_admin.= '&amp;cat_id='.$page['cat'];
    147157$url_admin.= '&amp;image_id='.$_GET['image_id'];
    148158 
     
    150160if ( isset( $_GET['add_fav'] ) )
    151161{
    152   $query = 'DELETE FROM '.FAVORITES_TABLE.' WHERE user_id = '.$user['id'];
    153   $query.= ' AND image_id = '.$picture['current']['id'].';';
     162  $query = 'DELETE FROM '.FAVORITES_TABLE;
     163  $query.= ' WHERE user_id = '.$user['id'];
     164  $query.= ' AND image_id = '.$picture['current']['id'];
     165  $query.= ';';
    154166  $result = mysql_query( $query );
    155167 
    156168  if ( $_GET['add_fav'] == 1 )
    157169  {
    158     $query = 'INSERT INTO '.FAVORITES_TABLE.' (image_id,user_id) VALUES';
    159     $query.= ' ('.$picture['current']['id'].','.$user['id'].');';
    160         $result = mysql_query( $query );
    161   }
    162   if ( !$_GET['add_fav'] && $page['cat']=='fav')
    163   {
    164     if ( $prev < 0 && $nb_row==1 )
    165     {
    166       // there is no favorite picture anymore
    167       // we redirect the user to the category page
     170    $query = 'INSERT INTO '.FAVORITES_TABLE;
     171    $query.= ' (image_id,user_id) VALUES';
     172    $query.= ' ('.$picture['current']['id'].','.$user['id'].')';
     173    $query.= ';';
     174    $result = mysql_query( $query );
     175  }
     176  if ( !$_GET['add_fav'] and $page['cat'] == 'fav' )
     177  {
     178    if ( $prev < 0 and $nb_row == 1 )
     179    {
     180      // there is no favorite picture anymore we redirect the user to the
     181      // category page
    168182      $url = add_session_id( $url_home );
    169183      header( 'Request-URI: '.$url );
     
    172186      exit();
    173187    }
    174         else if ( $prev < 0 )
    175         {
    176           $url = add_session_id( str_replace('&amp;','&',$picture['next']['url']), true);
    177         }
    178         else
    179         {
    180           $url = add_session_id( str_replace('&amp;','&',$picture['prev']['url']), true);
    181         }
    182         header( 'Request-URI: '.$url );
    183         header( 'Content-Location: '.$url ); 
    184         header( 'Location: '.$url );
    185         exit();
    186   }
    187 }
    188 
    189 //---------------------------------------------------------  comment registeration
    190   if ( isset( $_POST['content'] ) && !empty($_POST['content']) )
    191   {
    192     $register_comment = true;
    193         $author = !empty($_POST['author'])?$_POST['author']:$lang['guest'];
    194     // if a guest try to use the name of an already existing user, he must
    195     // be rejected
    196     if ( $author != $user['username'] )
    197     {
    198       $query = 'SELECT COUNT(*) AS user_exists';
    199       $query.= ' FROM '.USERS_TABLE;
    200       $query.= " WHERE username = '".$author."'";
    201       $query.= ';';
    202       $row = mysql_fetch_array( mysql_query( $query ) );
    203       if ( $row['user_exists'] == 1 )
    204       {
    205             $template->assign_block_vars('information', array('INFORMATION'=>$lang['comment_user_exists']));
    206         $register_comment = false;
    207       }
    208     }
    209 
    210     if ( $register_comment )
    211     {
    212       // anti-flood system
    213       $reference_date = time() - $conf['anti-flood_time'];
    214       $query = 'SELECT id FROM '.COMMENTS_TABLE;
    215       $query.= ' WHERE date > '.$reference_date;
    216       $query.= " AND author = '".$author."'";
    217       $query.= ';';
    218       if ( mysql_num_rows( mysql_query( $query ) ) == 0
    219            || $conf['anti-flood_time'] == 0 )
    220       {
    221         $query = 'INSERT INTO '.COMMENTS_TABLE;
    222         $query.= ' (author,date,image_id,content,validated) VALUES (';
    223                 $query.= "'".$author."'";
    224         $query.= ','.time().','.$_GET['image_id'];
    225         $query.= ",'".htmlspecialchars( $_POST['content'], ENT_QUOTES)."'";
    226         if ( !$conf['comments_validation'] || $user['status'] == 'admin' )
    227           $query.= ",'true'";
    228         else
    229           $query.= ",'false'";
    230         $query.= ');';
    231         mysql_query( $query );
    232         // information message
    233         $message = $lang['comment_added'];
    234         if ( $conf['comments_validation'] and $user['status'] != 'admin' )
    235         {
    236           $message.= '<br />'.$lang['comment_to_validate'];
    237         }
    238         $template->assign_block_vars('information', array('INFORMATION'=>$message));
    239         // notification to the administrators
    240         if ( $conf['mail_notification'] )
    241         {
    242           $cat_name = get_cat_display_name( $page['cat_name'], ' > ', '' );
    243           $cat_name = strip_tags( $cat_name );
    244           notify( 'comment', $cat_name.' > '.$picture['current']['name']);
    245         }
     188    else if ( $prev < 0 )
     189    {
     190      $url = str_replace( '&amp;', '&', $picture['next']['url'] );
     191      $url = add_session_id( $url, true);
     192    }
     193    else
     194    {
     195      $url = str_replace('&amp;', '&', $picture['prev']['url'] );
     196      $url = add_session_id( $url, true);
     197    }
     198    header( 'Request-URI: '.$url );
     199    header( 'Content-Location: '.$url ); 
     200    header( 'Location: '.$url );
     201    exit();
     202  }
     203}
     204
     205//------------------------------------------------------  comment registeration
     206if ( isset( $_POST['content'] ) && !empty($_POST['content']) )
     207{
     208  $register_comment = true;
     209  $author = !empty($_POST['author'])?$_POST['author']:$lang['guest'];
     210  // if a guest try to use the name of an already existing user, he must be
     211  // rejected
     212  if ( $author != $user['username'] )
     213  {
     214    $query = 'SELECT COUNT(*) AS user_exists';
     215    $query.= ' FROM '.USERS_TABLE;
     216    $query.= " WHERE username = '".$author."'";
     217    $query.= ';';
     218    $row = mysql_fetch_array( mysql_query( $query ) );
     219    if ( $row['user_exists'] == 1 )
     220    {
     221      $template->assign_block_vars(
     222        'information',
     223        array('INFORMATION'=>$lang['comment_user_exists']));
     224      $register_comment = false;
     225    }
     226  }
     227 
     228  if ( $register_comment )
     229  {
     230    // anti-flood system
     231    $reference_date = time() - $conf['anti-flood_time'];
     232    $query = 'SELECT id FROM '.COMMENTS_TABLE;
     233    $query.= ' WHERE date > '.$reference_date;
     234    $query.= " AND author = '".$author."'";
     235    $query.= ';';
     236    if ( mysql_num_rows( mysql_query( $query ) ) == 0
     237         or $conf['anti-flood_time'] == 0 )
     238    {
     239      $query = 'INSERT INTO '.COMMENTS_TABLE;
     240      $query.= ' (author,date,image_id,content,validated) VALUES (';
     241      $query.= "'".$author."'";
     242      $query.= ','.time().','.$_GET['image_id'];
     243      $query.= ",'".htmlspecialchars( $_POST['content'], ENT_QUOTES)."'";
     244      if ( !$conf['comments_validation'] or $user['status'] == 'admin' )
     245      {       
     246        $query.= ",'true'";
    246247      }
    247248      else
    248249      {
    249         // information message
    250         $template->assign_block_vars('information', array('INFORMATION'=>$lang['comment_anti-flood']));
     250        $query.= ",'false'";
    251251      }
    252     }
    253   }
    254   // comment deletion
    255   if ( isset( $_GET['del'] )
    256        && is_numeric( $_GET['del'] )
    257        && $user['status'] == 'admin' )
    258   {
    259     $query = 'DELETE FROM '.COMMENTS_TABLE.' WHERE id = '.$_GET['del'].';';
    260     mysql_query( $query );
    261   }
     252      $query.= ');';
     253      mysql_query( $query );
     254      // information message
     255      $message = $lang['comment_added'];
     256      if ( $conf['comments_validation'] and $user['status'] != 'admin' )
     257      {
     258        $message.= '<br />'.$lang['comment_to_validate'];
     259      }
     260      $template->assign_block_vars('information',
     261                                   array('INFORMATION'=>$message));
     262      // notification to the administrators
     263      if ( $conf['mail_notification'] )
     264      {
     265        $cat_name = get_cat_display_name( $page['cat_name'], ' > ', '' );
     266        $cat_name = strip_tags( $cat_name );
     267        notify( 'comment', $cat_name.' > '.$picture['current']['name']);
     268      }
     269    }
     270    else
     271    {
     272      // information message
     273      $template->assign_block_vars(
     274        'information',
     275        array('INFORMATION'=>$lang['comment_anti-flood']));
     276    }
     277  }
     278}
     279// comment deletion
     280if ( isset( $_GET['del'] )
     281     and is_numeric( $_GET['del'] )
     282     and $user['status'] == 'admin' )
     283{
     284  $query = 'DELETE FROM '.COMMENTS_TABLE;
     285  $query.= ' WHERE id = '.$_GET['del'];
     286  $query.= ';';
     287  mysql_query( $query );
     288}
    262289
    263290//
     
    267294$title =  $picture['current']['name'];
    268295$refresh = 0;
    269 if ( isset( $_GET['slideshow'] ) && $next)
    270 {
    271         $refresh= $_GET['slideshow'];
    272         $url_link = $picture['next']['url'];
     296if ( isset( $_GET['slideshow'] ) and $next )
     297{
     298  $refresh= $_GET['slideshow'];
     299  $url_link = $picture['next']['url'];
    273300}
    274301
Note: See TracChangeset for help on using the changeset viewer.