1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
|
<?php
// +-----------------------------------------------------------------------+
// | Piwigo - a PHP based photo gallery |
// +-----------------------------------------------------------------------+
// | Copyright(C) 2008-2011 Piwigo Team http://piwigo.org |
// | Copyright(C) 2003-2008 PhpWebGallery Team http://phpwebgallery.net |
// | Copyright(C) 2002-2003 Pierrick LE GALL http://le-gall.net/pierrick |
// +-----------------------------------------------------------------------+
// | This program is free software; you can redistribute it and/or modify |
// | it under the terms of the GNU General Public License as published by |
// | the Free Software Foundation |
// | |
// | This program is distributed in the hope that it will be useful, but |
// | WITHOUT ANY WARRANTY; without even the implied warranty of |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
// | General Public License for more details. |
// | |
// | You should have received a copy of the GNU General Public License |
// | along with this program; if not, write to the Free Software |
// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
// | USA. |
// +-----------------------------------------------------------------------+
/**
* Provides functions to handle categories.
*
*
*/
/**
* Is the category accessible to the connected user ?
*
* Note : if the user is not authorized to see this category, page creation
* ends (exit command in this function)
*
* @param int category id to verify
* @return void
*/
function check_restrictions($category_id)
{
global $user;
// $filter['visible_categories'] and $filter['visible_images']
// are not used because it's not necessary (filter <> restriction)
if (in_array($category_id, explode(',', $user['forbidden_categories'])))
{
access_denied();
}
}
function get_categories_menu()
{
global $page, $user, $filter, $conf;
$query = '
SELECT ';
// From CATEGORIES_TABLE
$query.= '
id, name, permalink, nb_images, global_rank,';
// From USER_CACHE_CATEGORIES_TABLE
$query.= '
date_last, max_date_last, count_images, count_categories';
// $user['forbidden_categories'] including with USER_CACHE_CATEGORIES_TABLE
$query.= '
FROM '.CATEGORIES_TABLE.' INNER JOIN '.USER_CACHE_CATEGORIES_TABLE.'
ON id = cat_id and user_id = '.$user['id'];
// Always expand when filter is activated
if (!$user['expand'] and !$filter['enabled'])
{
$where = '
(id_uppercat is NULL';
if (isset($page['category']))
{
$where .= ' OR id_uppercat IN ('.$page['category']['uppercats'].')';
}
$where .= ')';
}
else
{
$where = '
'.get_sql_condition_FandF
(
array
(
'visible_categories' => 'id',
),
null,
true
);
}
$where = trigger_event('get_categories_menu_sql_where',
$where, $user['expand'], $filter['enabled'] );
$query.= '
WHERE '.$where.'
;';
$result = pwg_query($query);
$cats = array();
$selected_category = isset($page['category']) ? $page['category'] : null;
while ($row = pwg_db_fetch_assoc($result))
{
$child_date_last = @$row['max_date_last']> @$row['date_last'];
$row = array_merge($row,
array(
'NAME' => trigger_event(
'render_category_name',
$row['name'],
'get_categories_menu'
),
'TITLE' => get_display_images_count(
$row['nb_images'],
$row['count_images'],
$row['count_categories'],
false,
' / '
),
'URL' => make_index_url(array('category' => $row)),
'LEVEL' => substr_count($row['global_rank'], '.') + 1,
'SELECTED' => $selected_category['id'] == $row['id'] ? true : false,
'IS_UPPERCAT' => $selected_category['id_uppercat'] == $row['id'] ? true : false,
)
);
if ($conf['index_new_icon'])
{
$row['icon_ts'] = get_icon($row['max_date_last'], $child_date_last);
}
array_push($cats, $row);
if ($row['id']==@$page['category']['id']) //save the number of subcats for later optim
$page['category']['count_categories'] = $row['count_categories'];
}
usort($cats, 'global_rank_compare');
// Update filtered data
if (function_exists('update_cats_with_filtered_data'))
{
update_cats_with_filtered_data($cats);
}
return $cats;
}
/**
* Retrieve informations about a category in the database
*
* Returns an array with following keys :
*
* - comment
* - dir : directory, might be empty for virtual categories
* - name : an array with indexes from 0 (lowest cat name) to n (most
* uppercat name findable)
* - nb_images
* - id_uppercat
* - site_id
* -
*
* @param int category id
* @return array
*/
function get_cat_info( $id )
{
$query = '
SELECT *
FROM '.CATEGORIES_TABLE.'
WHERE id = '.$id.'
;';
$cat = pwg_db_fetch_assoc(pwg_query($query));
if (empty($cat))
return null;
foreach ($cat as $k => $v)
{
// If the field is true or false, the variable is transformed into a
// boolean value.
if ($cat[$k] == 'true' or $cat[$k] == 'false')
{
$cat[$k] = get_boolean( $cat[$k] );
}
}
$upper_ids = explode(',', $cat['uppercats']);
if ( count($upper_ids)==1 )
{// no need to make a query for level 1
$cat['upper_names'] = array(
array(
'id' => $cat['id'],
'name' => $cat['name'],
'permalink' => $cat['permalink'],
)
);
}
else
{
$names = array();
$query = '
SELECT id, name, permalink
FROM '.CATEGORIES_TABLE.'
WHERE id IN ('.$cat['uppercats'].')
;';
$names = hash_from_query($query, 'id');
// category names must be in the same order than uppercats list
$cat['upper_names'] = array();
foreach ($upper_ids as $cat_id)
{
array_push( $cat['upper_names'], $names[$cat_id]);
}
}
return $cat;
}
// get_complete_dir returns the concatenation of get_site_url and
// get_local_dir
// Example : "pets > rex > 1_year_old" is on the the same site as the
// Piwigo files and this category has 22 for identifier
// get_complete_dir(22) returns "./galleries/pets/rex/1_year_old/"
function get_complete_dir( $category_id )
{
return get_site_url($category_id).get_local_dir($category_id);
}
// get_local_dir returns an array with complete path without the site url
// Example : "pets > rex > 1_year_old" is on the the same site as the
// Piwigo files and this category has 22 for identifier
// get_local_dir(22) returns "pets/rex/1_year_old/"
function get_local_dir( $category_id )
{
global $page;
$uppercats = '';
$local_dir = '';
if ( isset( $page['plain_structure'][$category_id]['uppercats'] ) )
{
$uppercats = $page['plain_structure'][$category_id]['uppercats'];
}
else
{
$query = 'SELECT uppercats';
$query.= ' FROM '.CATEGORIES_TABLE.' WHERE id = '.$category_id;
$query.= ';';
$row = pwg_db_fetch_assoc( pwg_query( $query ) );
$uppercats = $row['uppercats'];
}
$upper_array = explode( ',', $uppercats );
$database_dirs = array();
$query = 'SELECT id,dir';
$query.= ' FROM '.CATEGORIES_TABLE.' WHERE id IN ('.$uppercats.')';
$query.= ';';
$result = pwg_query( $query );
while( $row = pwg_db_fetch_assoc( $result ) )
{
$database_dirs[$row['id']] = $row['dir'];
}
foreach ($upper_array as $id)
{
$local_dir.= $database_dirs[$id].'/';
}
return $local_dir;
}
// retrieving the site url : "http://domain.com/gallery/" or
// simply "./galleries/"
function get_site_url($category_id)
{
global $page;
$query = '
SELECT galleries_url
FROM '.SITES_TABLE.' AS s,'.CATEGORIES_TABLE.' AS c
WHERE s.id = c.site_id
AND c.id = '.$category_id.'
;';
$row = pwg_db_fetch_assoc(pwg_query($query));
return $row['galleries_url'];
}
// returns an array of image orders available for users/visitors
function get_category_preferred_image_orders()
{
global $conf, $page;
return trigger_event('get_category_preferred_image_orders',
array(
array(l10n('Default'), '', true),
array(l10n('Rating score'), 'average_rate DESC', $conf['rate']),
array(l10n('Most visited'), 'hit DESC', true),
array(l10n('Creation date'), 'date_creation DESC', true),
array(l10n('Post date'), 'date_available DESC', true),
array(l10n('File name'), 'file ASC', true),
array(
l10n('Rank'),
'rank ASC',
('categories' == @$page['section'] and !isset($page['flat']) and !isset($page['chronology_field']) )
),
array( l10n('Permissions'), 'level DESC', is_admin() )
));
}
function display_select_categories($categories,
$selecteds,
$blockname,
$fullname = true)
{
global $template;
$tpl_cats = array();
foreach ($categories as $category)
{
if (!empty($category['permalink']))
{
$category['name'] .= ' √';
}
if ($fullname)
{
$option = strip_tags(
get_cat_display_name_cache(
$category['uppercats'],
null,
false
)
);
}
else
{
$option = str_repeat(' ',
(3 * substr_count($category['global_rank'], '.')));
$option.= '- ';
$option.= strip_tags(
trigger_event(
'render_category_name',
$category['name'],
'display_select_categories'
)
);
}
$tpl_cats[ $category['id'] ] = $option;
}
$template->assign( $blockname, $tpl_cats);
$template->assign( $blockname.'_selected', $selecteds);
}
function display_select_cat_wrapper($query, $selecteds, $blockname,
$fullname = true)
{
$result = pwg_query($query);
$categories = array();
if (!empty($result))
{
while ($row = pwg_db_fetch_assoc($result))
{
array_push($categories, $row);
}
}
usort($categories, 'global_rank_compare');
display_select_categories($categories, $selecteds, $blockname, $fullname);
}
/**
* returns all subcategory identifiers of given category ids
*
* @param array ids
* @return array
*/
function get_subcat_ids($ids)
{
$query = '
SELECT DISTINCT(id)
FROM '.CATEGORIES_TABLE.'
WHERE ';
foreach ($ids as $num => $category_id)
{
is_numeric($category_id)
or trigger_error(
'get_subcat_ids expecting numeric, not '.gettype($category_id),
E_USER_WARNING
);
if ($num > 0)
{
$query.= '
OR ';
}
$query.= 'uppercats '.DB_REGEX_OPERATOR.' \'(^|,)'.$category_id.'(,|$)\'';
}
$query.= '
;';
$result = pwg_query($query);
$subcats = array();
while ($row = pwg_db_fetch_assoc($result))
{
array_push($subcats, $row['id']);
}
return $subcats;
}
/** finds a matching category id from a potential list of permalinks
* @param array permalinks example: holiday holiday/france holiday/france/paris
* @param int idx - output of the index in $permalinks that matches
* return category id or null if no match
*/
function get_cat_id_from_permalinks( $permalinks, &$idx )
{
$in = '';
foreach($permalinks as $permalink)
{
if ( !empty($in) ) $in.=', ';
$in .= '\''.$permalink.'\'';
}
$query ='
SELECT cat_id AS id, permalink, 1 AS is_old
FROM '.OLD_PERMALINKS_TABLE.'
WHERE permalink IN ('.$in.')
UNION
SELECT id, permalink, 0 AS is_old
FROM '.CATEGORIES_TABLE.'
WHERE permalink IN ('.$in.')
;';
$perma_hash = hash_from_query($query, 'permalink');
if ( empty($perma_hash) )
return null;
for ($i=count($permalinks)-1; $i>=0; $i--)
{
if ( isset( $perma_hash[ $permalinks[$i] ] ) )
{
$idx = $i;
$cat_id = $perma_hash[ $permalinks[$i] ]['id'];
if ($perma_hash[ $permalinks[$i] ]['is_old'])
{
$query='
UPDATE '.OLD_PERMALINKS_TABLE.' SET last_hit=NOW(), hit=hit+1
WHERE permalink=\''.$permalinks[$i].'\' AND cat_id='.$cat_id.'
LIMIT 1';
pwg_query($query);
}
return $cat_id;
}
}
return null;
}
function global_rank_compare($a, $b)
{
return strnatcasecmp($a['global_rank'], $b['global_rank']);
}
function rank_compare($a, $b)
{
if ($a['rank'] == $b['rank'])
{
return 0;
}
return ($a['rank'] < $b['rank']) ? -1 : 1;
}
/**
* returns display text for information images of category
*
* @param array categories
* @return string
*/
function get_display_images_count($cat_nb_images, $cat_count_images, $cat_count_categories, $short_message = true, $Separator = '\n')
{
$display_text = '';
if ($cat_count_images > 0)
{
if ($cat_nb_images > 0 and $cat_nb_images < $cat_count_images)
{
$display_text.= get_display_images_count($cat_nb_images, $cat_nb_images, 0, $short_message, $Separator).$Separator;
$cat_count_images-= $cat_nb_images;
$cat_nb_images = 0;
}
//at least one image direct or indirect
$display_text.= l10n_dec('%d photo', '%d photos', $cat_count_images);
if ($cat_count_categories == 0 or $cat_nb_images == $cat_count_images)
{
//no descendant categories or descendants do not contain images
if (! $short_message)
{
$display_text.= ' '.l10n('in this album');
}
}
else
{
$display_text.= ' '.l10n_dec('in %d sub-album', 'in %d sub-albums', $cat_count_categories);
}
}
return $display_text;
}
/**
* Find a random photo among all photos below a given album in the tree (not
* only photo directly associated to the album but also to sub-albums)
*
* we need $category['uppercats'], $category['id'], $category['count_images']
*/
function get_random_image_in_category($category, $recursive=true)
{
$image_id = null;
if ($category['count_images']>0)
{
$query = '
SELECT image_id
FROM '.CATEGORIES_TABLE.' AS c
INNER JOIN '.IMAGE_CATEGORY_TABLE.' AS ic ON ic.category_id = c.id
WHERE ';
if ($recursive)
{
$query.= '
(c.id='.$category['id'].' OR uppercats LIKE \''.$category['uppercats'].',%\')';
}
else
{
$query.= '
c.id='.$category['id'];
}
$query.= '
'.get_sql_condition_FandF
(
array
(
'forbidden_categories' => 'c.id',
'visible_categories' => 'c.id',
'visible_images' => 'image_id',
),
"\n AND"
).'
ORDER BY '.DB_RANDOM_FUNCTION.'()
LIMIT 1
;';
$result = pwg_query($query);
if (pwg_db_num_rows($result) > 0)
{
list($image_id) = pwg_db_fetch_row($result);
}
}
return $image_id;
}
/**
* create a tree from a flat list of categories, no recursivity for high speed
*/
function categories_flatlist_to_tree($categories)
{
$tree = array();
$key_of_cat = array();
foreach ($categories as $key => &$node)
{
$key_of_cat[$node['id']] = $key;
if (!isset($node['id_uppercat']))
{
$tree[$key] = &$node;
}
else
{
if (!isset($categories[ $key_of_cat[ $node['id_uppercat'] ] ]['sub_categories']))
{
$categories[ $key_of_cat[ $node['id_uppercat'] ] ]['sub_categories'] = array();
}
$categories[ $key_of_cat[ $node['id_uppercat'] ] ]['sub_categories'][$key] = &$node;
}
}
return $tree;
}
?>
|