source: branches/1.7/include/functions_rate.inc.php @ 24883

Last change on this file since 24883 was 2470, checked in by rvelices, 16 years ago
  • bug fix: registered users could rate a picture several times from diffrent ip adresses
  • Property svn:eol-style set to LF
  • Property svn:keywords set to Author Date Id Revision
File size: 4.4 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | PhpWebGallery - a PHP based picture gallery                           |
4// | Copyright (C) 2002-2003 Pierrick LE GALL - pierrick@phpwebgallery.net |
5// | Copyright (C) 2003-2007 PhpWebGallery Team - http://phpwebgallery.net |
6// +-----------------------------------------------------------------------+
7// | branch        : BSF (Best So Far)
8// | file          : $RCSfile$
9// | last update   : $Date: 2008-08-11 11:24:59 +0000 (Mon, 11 Aug 2008) $
10// | last modifier : $Author: rvelices $
11// | revision      : $Revision: 2470 $
12// +-----------------------------------------------------------------------+
13// | This program is free software; you can redistribute it and/or modify  |
14// | it under the terms of the GNU General Public License as published by  |
15// | the Free Software Foundation                                          |
16// |                                                                       |
17// | This program is distributed in the hope that it will be useful, but   |
18// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
19// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
20// | General Public License for more details.                              |
21// |                                                                       |
22// | You should have received a copy of the GNU General Public License     |
23// | along with this program; if not, write to the Free Software           |
24// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
25// | USA.                                                                  |
26// +-----------------------------------------------------------------------+
27
28/**
29 * rate a picture by a user
30 *
31 * @param int image identifier
32 * @param int rate
33 * @return void
34 */
35function rate_picture($image_id, $rate)
36{
37  global $conf, $user;
38
39  if (!isset($rate)
40      or !$conf['rate']
41      or !in_array($rate, $conf['rate_items']))
42  {
43    return;
44  }
45
46  $user_anonymous = is_autorize_status(ACCESS_CLASSIC) ? false : true;
47
48  if ($user_anonymous and !$conf['rate_anonymous'])
49  {
50    return;
51  }
52
53  $ip_components = explode('.', $_SERVER["REMOTE_ADDR"]);
54  if (count($ip_components) > 3)
55  {
56    array_pop($ip_components);
57  }
58  $anonymous_id = implode ('.', $ip_components);
59
60  if ($user_anonymous)
61  {
62    if (isset($_COOKIE['pwg_anonymous_rater']))
63    {
64      if ($anonymous_id != $_COOKIE['pwg_anonymous_rater'])
65      { // client has changed his IP adress or he's trying to fool us
66        $query = '
67SELECT element_id
68  FROM '.RATE_TABLE.'
69  WHERE user_id = '.$user['id'].'
70    AND anonymous_id = \''.$anonymous_id.'\'
71;';
72        $already_there = array_from_query($query, 'element_id');
73
74        if (count($already_there) > 0)
75        {
76          $query = '
77DELETE
78  FROM '.RATE_TABLE.'
79  WHERE user_id = '.$user['id'].'
80    AND anonymous_id = \''.$_COOKIE['pwg_anonymous_rater'].'\'
81    AND element_id IN ('.implode(',', $already_there).')
82;';
83           pwg_query($query);
84         }
85
86         $query = '
87UPDATE
88  '.RATE_TABLE.'
89  SET anonymous_id = \'' .$anonymous_id.'\'
90  WHERE user_id = '.$user['id'].'
91    AND anonymous_id = \'' . $_COOKIE['pwg_anonymous_rater'].'\'
92;';
93         pwg_query($query);
94
95         setcookie(
96            'pwg_anonymous_rater',
97            $anonymous_id,
98            strtotime('+10 years'),
99            cookie_path()
100           );
101      } // end client changed ip
102    } // end client has cookie
103    else
104    {
105      setcookie(
106          'pwg_anonymous_rater',
107          $anonymous_id,
108          strtotime('+10 years'),
109          cookie_path()
110          );
111    }
112  } // end anonymous user
113  $query = '
114DELETE
115  FROM '.RATE_TABLE.'
116  WHERE element_id = '.$image_id.'
117  AND user_id = '.$user['id'].'
118';
119  if ($user_anonymous)
120  {
121    $query.= ' AND anonymous_id = \''.$anonymous_id.'\'';
122  }
123  pwg_query($query);
124  $query = '
125INSERT
126  INTO '.RATE_TABLE.'
127  (user_id,anonymous_id,element_id,rate,date)
128  VALUES
129  ('
130    .$user['id'].','
131    .'\''.$anonymous_id.'\','
132    .$image_id.','
133    .$rate
134    .',NOW())
135;';
136  pwg_query($query);
137
138  // update of images.average_rate field
139  $query = '
140SELECT ROUND(AVG(rate),2) AS average_rate
141  FROM '.RATE_TABLE.'
142  WHERE element_id = '.$image_id.'
143;';
144  $row = mysql_fetch_array(pwg_query($query));
145  $query = '
146UPDATE '.IMAGES_TABLE.'
147  SET average_rate = '.$row['average_rate'].'
148  WHERE id = '.$image_id.'
149;';
150  pwg_query($query);
151}
152
153?>
Note: See TracBrowser for help on using the repository browser.