root/include/class.tags.php

Revision 329, 10.3 kB (checked in by Nafania, 1 year ago)

фикс http://svn.tbdevsz.ru/ticket/41

Line 
1 <?php
2
3 class tags {</span>
4 <span class="code-keyword">
5     static $fast_mode = false;
6     static $auth_check = false;
7     static $tag_cat_id = 0;
8     static $tag_owner = 0;
9
10     //fast mode is for first add tags, in uploading for example
11     function set_fast_add_mode () {
12         if ( !self::$tag_cat_id || !self::$tag_owner ) {
13             throw new Exception('set cat_id and owner first');
14         }
15         self::$fast_mode = true;
16     }
17
18     function tag_cloud_for_cat ( $cat_id ) {
19         global $db, $userdata, $config;
20
21         $tags = array();
22
23         $cats_sql = '';
24
25         if ( is_array($cat_id) && sizeof($cat_id) ) {
26             $cats_sql .=  ' AND tt.tag_cat_id IN (' . implode(', ', $cat_id) . ')';
27         }
28         elseif ( $cat_id ) {
29             $cats_sql .=  ' AND tt.tag_cat_id = ' . $cat_id;
30         }
31
32         $sql = 'SELECT tt.tag_name, SUM(tt.tag_count) AS tag_count
33                 FROM ' . TAGS_TABLE . ' tt, ' . CATEGORIES_TABLE . ' c
34                 WHERE tt.tag_type = ' . TYPE_TORRENT . '
35                 AND tt.tag_cat_id = c.id ' . ( $userdata['session_logged_in'] ? '' : ' AND c.anonymous_view = 1 ' ) . $cats_sql . '
36                 GROUP BY tt.tag_name
37                 ORDER BY tag_count DESC';
38         $result = $db->sql_query_limit($sql, 30, 0, 3600);
39
40         if ( $row = $db->sql_fetchrow($result) ) {
41             $max_qty = $min_qty = 0;
42             do {
43                 $tags[] = $row;
44                 $max_qty = max($max_qty, $row['tag_count']);
45                 $min_qty = min($min_qty, $row['tag_count']);
46             }
47             while ( $row = $db->sql_fetchrow($result) );
48
49             $spread = $max_qty - $min_qty;
50             $spread = ( $spread == 0 ? 1 : $spread );
51
52             $step = ( $config['max_tag_size'] - $config['min_tag_size'] ) / $spread;
53
54             foreach ( $tags AS $_null => &$tag_ary ) {
55                 $tag_ary['enc_tag_name'] = tags::encode_tag_name($tag_ary['tag_name']);
56                 $tag_ary['size'] = tags::get_tag_size($tag_ary['tag_count'], $min_qty, $step);
57             }
58
59             shuffle($tags);
60         }
61
62         return $tags;
63     }
64
65     function encode_tag_name ( $tag_name ) {
66         $enc_tag_name = undo_htmlspecialchars($tag_name);
67         $enc_tag_name = rawurlencode($enc_tag_name);
68
69         return $enc_tag_name;
70     }
71
72     function get_tag_size ( $tag_count, $min_qty, $step ) {
73         global $config;
74
75         return round($config['min_tag_size'] + (($tag_count - $min_qty) * $step));
76     }
77
78     function tags_for_single ( $type, $id ) {
79         global $db, $config;
80
81         $sql = 'SELECT tag_name, tag_count
82                 FROM ' . TAGS_TABLE . '
83                 WHERE tag_type = ' . $type . '
84                 AND tag_for_id = ' . $id;
85         $result = $db->sql_query($sql);
86
87         $tags = array();
88
89         while ( $row = $db->sql_fetchrow($result) ) {
90             $tags[$row['tag_name']] = $row['tag_count'];
91         }
92
93         return tags::build_tag_cloud_by_ary($tags);
94     }
95
96     function build_tag_cloud_by_ary ( $tags = array() ) {
97         global $config;
98
99         $new_tags = array();
100
101         if ( !sizeof($tags) ) {
102             return $new_tags;
103         }
104
105         //simple tag cloud find here http://www.bytemycode.com/snippets/snippet/415/
106         $max_qty = max(array_values($tags));
107         $min_qty = min(array_values($tags));
108
109         $spread = $max_qty - $min_qty;
110         $spread = ( $spread == 0 ? 1 : $spread );
111
112         $step = ( $config['max_tag_size'] - $config['min_tag_size'] ) / $spread;
113
114         $i = 0;
115
116         foreach ( $tags AS $tag_name => $tag_count ) {
117             $size = tags::get_tag_size($tag_count, $min_qty, $step);
118             ++$i;
119
120             $new_tags[] = array(
121                 'tag_name' => $tag_name,
122                 'js_tag_name' => str_replace("'", "\'", $tag_name),
123                 'enc_tag_name' => tags::encode_tag_name($tag_name),
124                 'tag_size' => $size,
125                 'tag_num' => $i,
126             );
127         }
128
129         return $new_tags;
130     }
131
132     function simple_get_tag_for_single ( $type, $id ) {
133         global $db;
134
135         $tags = array();
136
137         $sql = 'SELECT tag_count, tag_name
138                     FROM ' . TAGS_TABLE . '
139                     WHERE tag_type = ' . $type . '
140                     AND tag_for_id = ' . $id;
141         $result = $db->sql_query($sql);
142         while( $row = $db->sql_fetchrow($result) ){
143             $tags[$row['tag_name']] = $row['tag_count'];
144         }
145
146         return $tags;
147     }
148
149     function set_tag_cat_id( $cat_id = 0 ){
150         self::$tag_cat_id = $cat_id;
151     }
152
153     function set_tag_owner( $owner = 0 ){
154         self::$tag_owner = $owner;
155     }
156
157     function add_tag ( $type, $id, $tag_name, $vote_type ) {
158         global $config, $lang;
159
160         $tag_name = urldecode($tag_name);
161         $ret_message = array();
162         $prev_tags = array();
163
164         if ( strpos($tag_name, ',') !== false ) {
165             $tag_name = explode(',', $tag_name);
166         }
167         else {
168             $tag_name = array($tag_name);
169         }
170         tags::clean_tag($tag_name);
171
172         if ( !self::$fast_mode ) {
173             if ( !sizeof($tag_name) ) {
174                 return $lang['access_denied'];
175             }
176
177             if ( !$error = add_tags_for::getHandle($type, $id) ) {
178                 return $lang['access_denied'];
179             }
180
181             if ( !self::$auth_check && !tags::check_allow_add($config['tags_who_allowed']) ) {
182                 return $lang['access_denied'];
183             }
184
185             if ( !tags::check_max_user_tags($config['tags_max_allow'], $id, $type, $tag_name) ) {
186                 return sprintf($lang['no_more_tags_allowed'], $config['tags_max_allow']);
187             }
188         }
189
190         $prev_tags = tags::simple_get_tag_for_single($type, $id);
191
192         foreach ( $tag_name AS $_null => $name ) {
193             if ( !tags::check_tag_length($config['tags_min_length'], $config['tags_max_length'], $name) ) {
194                 $ret_message[] = sprintf($lang['tag_is_too_short_or_long'], $name, $config['tags_min_length'], $config['tags_max_length']);
195                 continue;
196             }
197
198             if ( !self::$fast_mode && tags::check_dupe_tag($id, $type, $name) ) {
199                 $ret_message[] = $lang['dupe_vote'] . ' ' . $name;
200                 continue;
201             }
202
203             $prev_tags_count = ( isset($prev_tags[$name]) ? (int) $prev_tags[$name] : 0 ); // 2 is for tag vote down
204
205             $func = 'tag_vote_' . $vote_type;
206             tags::$func($type, $id, $name, $prev_tags_count);
207
208             tags::insert_tag_user($type, $id, $name);
209         }
210
211         return implode("\n", $ret_message);
212     }
213
214     function clean_tag ( &$tag_name ) {
215         if ( is_array($tag_name) ) {
216             array_walk($tag_name, 'tags::clean_tag');
217             array_filter($tag_name);
218             return;
219         }
220         $tag_name = trim($tag_name);
221         $tag_name = preg_replace('/[\.,:\^]/', '', $tag_name);
222         $tag_name = utf_strtolower($tag_name);
223     }
224
225     function insert_tag_user ($type, $id, $name ) {
226         global $db, $userdata;
227
228         $sql = 'INSERT INTO ' . TAGS_USERS_TABLE . ' ' . $db->sql_build_array('INSERT', array(
229             'tag_users_uid' => $userdata['uid'],
230             'tag_users_for_id' => $id,
231             'tag_users_type' => $type,
232             'tag_users_name' => $name,
233         ));
234         $db->sql_query($sql);
235     }
236
237     function tag_vote_down ( $type, $id, $tag_name, &$tags_count ) {
238         global $db;
239
240         if ( !$tags_count ) {
241             return;
242         }
243
244         if ( $tags_count <= 1 ) {
245             $sql = 'DELETE FROM ' . TAGS_TABLE . '
246                     WHERE tag_name = \'' . $db->sql_escape($tag_name) . '\'
247                     AND tag_type = ' . $type . '
248                     AND tag_for_id = ' . $id;
249         }
250         else {
251             $sql = 'UPDATE ' . TAGS_TABLE . '
252                     SET tag_count = tag_count - 1
253                     WHERE tag_name = \'' . $db->sql_escape($tag_name) . '\'
254                     AND tag_type = ' . $type . '
255                     AND tag_for_id = ' . $id;
256         }
257         $db->sql_query($sql);
258
259         --$tags_count;
260
261         return;
262     }
263
264     function tag_vote_up ( $type, $id, $tag_name, &$tags_count ) {
265         global $db;
266
267         if ( $tags_count ) {
268             $sql = 'UPDATE ' . TAGS_TABLE . '
269                 SET tag_count = tag_count + 1
270                 WHERE tag_name = \'' . $db->sql_escape($tag_name) . '\'
271                 AND tag_type = ' . $type . '
272                 AND tag_for_id = ' . $id;
273             $db->sql_query($sql);
274         }
275         else {
276             $sql = 'INSERT INTO ' . TAGS_TABLE . ' ' . $db->sql_build_array('INSERT', array(
277                 'tag_name' => $tag_name,
278                 'tag_type' => $type,
279                 'tag_for_id' => $id,
280                 'tag_count' => 1,
281                 'tag_cat_id' => self::$tag_cat_id,
282             ));
283             $db->sql_query($sql);
284         }
285
286         ++$tags_count;
287
288         return;
289     }
290
291     function check_dupe_tag ( $id, $tag_type, $name) {
292         global $db, $userdata;
293
294         $sql = 'SELECT tag_users_for_id
295                 FROM ' . TAGS_USERS_TABLE . '
296                 WHERE tag_users_uid = ' . $userdata['uid'] . '
297                 AND tag_users_for_id = ' . $id . '
298                 AND tag_users_type = ' . $tag_type . '
299                 AND tag_users_name = \'' . $db->sql_escape($name) . '\'';
300         $result = $db->sql_query($sql);
301         return $db->sql_fetchrow($result);
302     }
303
304     function check_tag_length ( $min, $max, $tag_name ){
305         $tag_length = utf_strlen($tag_name);
306
307         if ( $max && $tag_length > $max ) {
308             return false;
309         }
310         if ( $min && $tag_length < $min ) {
311             return false;
312         }
313
314         return true;
315     }
316
317     function check_allow_add( $tags_who_allowed ) {
318         global $userdata;
319
320         if ( self::$auth_check ) {
321             return true;
322         }
323
324         switch ( $tags_who_allowed ) {
325             case 0:
326                 $allow_add_tags = true;
327             break;
328
329             case 1:
330                 $allow_add_tags = $userdata['uid'] == self::$tag_owner || $userdata['class'] >= UC_MODERATOR;
331             break;
332
333             case 2:
334                 $allow_add_tags = $userdata['class'] >= UC_MODERATOR;
335             break;
336         }
337
338         self::$auth_check = true;
339
340         return $allow_add_tags;
341
342     }
343
344     function return_allow_add_tag ( $type, $id, $owner = false ) {
345         global $config, $userdata;
346
347         self::$auth_check = true;
348
349         self::$tag_owner = $owner;
350
351         if ( !self::$tag_owner ) {
352             add_tags_for::getHandle($type, $id);
353         }
354
355         if ( !self::$tag_owner ) {
356             return false;
357         }
358
359         switch ( $config['tags_who_allowed'] ) {
360             case 0:
361                 $allow_add_tags = true;
362             break;
363
364             case 1:
365                 $allow_add_tags = $userdata['uid'] == self::$tag_owner || $userdata['class'] >= UC_MODERATOR;
366             break;
367
368             case 2:
369                 $allow_add_tags = $userdata['class'] >= UC_MODERATOR;
370             break;
371         }
372
373         return $allow_add_tags;
374     }
375
376     function check_max_user_tags ( $tags_max_allow, $id, $tag_type, $tag_name ) {
377         global $userdata, $db;
378
379         if ( !$tags_max_allow ) {
380             return;
381         }
382
383         $sql = 'SELECT COUNT(*) AS count FROM ' . TAGS_USERS_TABLE . ' WHERE tag_users_uid = ' . $userdata['uid'] . ' AND tag_users_for_id = ' . $id . ' AND tag_users_type = ' . $tag_type;
384         $result = $db->sql_query($sql);
385         $count = ( $row = $db->sql_fetchrow($result) ) ? $row['count'] : 0;
386
387         if ( $count >= $tags_max_allow || ( ( sizeof($tag_name) + $count ) >= $tags_max_allow ) ) {
388             return false;
389         }
390         return true;
391     }
392 }
393
394 class add_tags_for extends tags {
395     function __construct() {
396     }
397
398     function getHandle($type, $id) {
399         $func = 'add_for_' . $type;
400         return add_tags_for::$func($id);
401     }
402
403     //torrent
404     function add_for_1($id) {
405         global $db;
406
407         $sql = 'SELECT owner, category FROM ' . TORRENTS_TABLE . ' WHERE fid = ' . $id;
408         $result = $db->sql_query($sql);
409         if ( $row = $db->sql_fetchrow($result) ) {
410             self::$tag_owner = $row['owner'];
411             self::$tag_cat_id = $row['category'];
412             return true;
413         }
414         return false;
415     }
416 }
417 ?>
Note: See TracBrowser for help on using the browser.