forked from Piwigo/AMenuManager
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathamm_root.class.inc.php
More file actions
executable file
·724 lines (632 loc) · 20.1 KB
/
amm_root.class.inc.php
File metadata and controls
executable file
·724 lines (632 loc) · 20.1 KB
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
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
<?php
/* -----------------------------------------------------------------------------
Plugin : Advanced Menu Manager
Author : Grum
email : grum@grum.fr
website : http://photos.grum.fr
PWG user : http://forum.piwigo.org/profile.php?id=3706
<< May the Little SpaceFrog be with you ! >>
------------------------------------------------------------------------------
See main.inc.php for release information
AMM_root : root class for plugin
--------------------------------------------------------------------------- */
if (!defined('PHPWG_ROOT_PATH')) { die('Hacking attempt!'); }
include_once(PHPWG_ROOT_PATH.'include/block.class.php');
include_once(PHPWG_PLUGINS_PATH.'GrumPluginClasses/classes/CommonPlugin.class.inc.php');
include_once(PHPWG_PLUGINS_PATH.'GrumPluginClasses/classes/GPCUsersGroups.class.inc.php');
class AMM_root extends CommonPlugin
{
protected $defaultMenus = array(
/* about visibility & accessibility system :
* - by default, everything is visible (users & groups)
* - items not visibles are listed (in release < 3.3.4, this rule wa applied to groups only)
*
* on the user interface, checked items are visibles => not checked items are stored
*/
'favorites' => array('container' => 'special', 'visibility' => '/', 'order' => 0, 'translation' => 'My favorites'),
'most_visited' => array('container' => 'special', 'visibility' => '/', 'order' => 1, 'translation' => 'Most visited'),
'best_rated' => array('container' => 'special', 'visibility' => '/', 'order' => 2, 'translation' => 'Best rated'),
'random' => array('container' => 'special', 'visibility' => '/', 'order' => 3, 'translation' => 'Random pictures'),
'recent_pics' => array('container' => 'special', 'visibility' => '/', 'order' => 4, 'translation' => 'Recent pictures'),
'recent_cats' => array('container' => 'special', 'visibility' => '/', 'order' => 5, 'translation' => 'Recent categories'),
'calendar' => array('container' => 'special', 'visibility' => '/', 'order' => 6, 'translation' => 'Calendar'),
'qsearch' => array('container' => 'menu', 'visibility' => '/', 'order' => 0, 'translation' => 'Quick search'),
'tags' => array('container' => 'menu', 'visibility' => '/', 'order' => 1, 'translation' => 'Tags'),
'search' => array('container' => 'menu', 'visibility' => '/', 'order' => 2, 'translation' => 'Search'),
'comments' => array('container' => 'menu', 'visibility' => '/', 'order' => 3, 'translation' => 'Comments'),
'about' => array('container' => 'menu', 'visibility' => '/', 'order' => 4, 'translation' => 'About'),
'rss' => array('container' => 'menu', 'visibility' => '/', 'order' => 5, 'translation' => 'Notification')
);
protected $urlsModes=array(0 => 'new_window', 1 => 'current_window');
public function __construct($prefixeTable, $filelocation)
{
$this->setPluginName("Advanced Menu Manager");
$this->setPluginNameFiles("amm");
parent::__construct($prefixeTable, $filelocation);
$list=array('urls', 'personalised', 'personalised_langs', 'blocks');
$this->setTablesList($list);
}
public function __destruct()
{
unset($this->defaultMenus);
parent::__destruct();
}
/*
* ---------------------------------------------------------------------------
* common AIP & PIP functions
* ---------------------------------------------------------------------------
*/
/**
* this function initialize config var with default values
*/
public function initConfig()
{
$this->config=array(
'amm_links_show_icons' => 'y',
'amm_links_title' => array(),
'amm_randompicture_preload' => 25, //number preloaded random pictures
'amm_randompicture_showname' => 'n', //n:no, o:over, u:under
'amm_randompicture_showcomment' => 'n', //n:no, o:over, u:under
'amm_randompicture_periodicchange' => 0, //0: no periodic change ; periodic change in milliseconds
'amm_randompicture_height' => 0, //0: automatic, otherwise it's the fixed height in pixels
'amm_randompicture_title' => array(),
'amm_randompicture_selectMode' => 'a', // a:all, f:webmaster's favorites, c:categories
'amm_randompicture_selectCat' => array(), // categories id list
'amm_blocks_items' => $this->defaultMenus,
'amm_albums_to_menu' => array(),
'amm_old_blk_menubar' => '', // keep a copy of piwigo's menubar config
'newInstall' => 'n'
);
$languages=get_languages();
foreach($languages as $key => $val)
{
if($key=='fr_FR')
{
$this->config['amm_links_title'][$key]=base64_encode('Liens');
$this->config['amm_randompicture_title'][$key]=base64_encode('Une image au hasard');
}
else
{
$this->config['amm_links_title'][$key]=base64_encode('Links');
$this->config['amm_randompicture_title'][$key]=base64_encode('A random picture');
}
}
}
public function registerBlocks( $menu_ref_arr )
{
$menu = & $menu_ref_arr[0];
if ($menu->get_id() != 'menubar') return;
$menu->register_block( new RegisteredBlock( 'mbAMM_randompict', 'Random pictures', 'AMM'));
$menu->register_block( new RegisteredBlock( 'mbAMM_links', 'Links', 'AMM'));
$blocks=$this->getPersonalisedBlocks();
if(count($blocks))
{
$idDone=array();
foreach($blocks as $key => $val)
{
if(!isset($idDone[$val['id']]))
{
$menu->register_block( new RegisteredBlock( 'mbAMM_personalised'.$val['id'], $val['title'], 'AMM'));
$idDone[$val['id']]="";
}
}
}
$this->loadConfig();
if(count($this->config['amm_albums_to_menu'])>0)
{
$sql="SELECT id, name
FROM ".CATEGORIES_TABLE."
WHERE id IN(".implode(',', $this->config['amm_albums_to_menu']).");";
$result=pwg_query($sql);
if($result)
{
while($row=pwg_db_fetch_assoc($result))
{
$row['name']=trigger_change('render_category_name', $row['name'], 'amm_album_to_menu');
$menu->register_block( new RegisteredBlock( 'mbAMM_album'.$row['id'], $row['name'].' ('.l10n('g002_album2menu').') ', 'AMM'));
}
}
}
}
/*
* ---------------------------------------------------------------------------
*
* Links functions
*
* ---------------------------------------------------------------------------
*/
/**
* return an array of links (each url is an array)
*
* @param Bool onlyVisible : if true, only visible links are returned
* @return Array
*/
protected function getLinks($onlyVisible=false)
{
$returned=array();
$sql="SELECT id, label, url, mode, icon, position, visible, accessUsers, accessGroups
FROM ".$this->tables['urls'];
if($onlyVisible)
{
$sql.=" WHERE visible = 'y' ";
}
$sql.=" ORDER BY position ASC, id ASC";
$result=pwg_query($sql);
if($result)
{
while($row=pwg_db_fetch_assoc($result))
{
$returned[]=$row;
}
}
return($returned);
}
/**
* return values for a given link
*
* @param String $id : link id
* @return Array
*/
protected function getLink($id)
{
$returned=array();
$sql="SELECT id, label, url, mode, icon, position, visible, accessUsers, accessGroups
FROM ".$this->tables['urls']."
WHERE id = '$id';";
$result=pwg_query($sql);
if($result)
{
while($row=pwg_db_fetch_assoc($result))
{
$returned=$row;
}
}
return($returned);
}
/**
* set values for a link
* if link id is empty : create a new link
* if not, update link
*
* @param String $id : link id
* @return Integer : -1 if fails
* otherwise link id
*/
protected function setLink($id, $label, $url, $mode, $icon, $visible, $accessUsers, $accessGroups)
{
if($id=='')
{
$sql="INSERT INTO ".$this->tables['urls']." (label,url,mode,icon,position,visible,accessUsers,accessGroups) VALUES
(
'".$label."',
'".$url."',
'$mode',
'$icon',
0,
'$visible',
'$accessUsers',
'$accessGroups'
);";
}
else
{
$sql="UPDATE ".$this->tables['urls']."
SET label='".$label."',
url='".$url."',
mode='$mode',
icon='$icon',
visible='$visible',
accessUsers='".$accessUsers."',
accessGroups='".$accessGroups."'
WHERE id='$id';";
}
$result=pwg_query($sql);
if($result)
{
if($id=='')
{
return(pwg_db_insert_id());
}
else
{
return($id);
}
}
return(-1);
}
/**
* delete a given link
*
* @param String $id : link id
* @return Bool : true if deleted, otherwise false
*/
protected function deleteLink($id)
{
$sql="DELETE FROM ".$this->tables['urls']."
WHERE id = '$id';";
$result=pwg_query($sql);
if($result) return(true);
return(false);
}
/**
* return number of links
*
* @param Bool onlyVisible : if true, only visible links are counted
* @return Array
*/
protected function getLinksCount($onlyVisible=false)
{
$returned=0;
$sql="SELECT count(id) FROM ".$this->tables['urls'];
if($onlyVisible)
{
$sql.=" WHERE visible = 'y' ";
}
$result=pwg_query($sql);
if($result)
{
$tmp=pwg_db_fetch_row($result);
$returned=$tmp[0];
}
return($returned);
}
/**
* set order from given links
*
* @param Array $links : array
* each item is an array ('id' => '', 'order' =>'')
* @return Bool :
*/
protected function setLinksOrder($links)
{
$returned=true;
foreach($links as $link)
{
$sql="UPDATE ".$this->tables['urls']."
SET position='".$link['order']."'
WHERE id='".$link['id']."';";
$result=pwg_query($sql);
if(!$result) $returned=false;
}
return($returned);
}
/*
* ---------------------------------------------------------------------------
*
* Personalised Blocks functions
*
* ---------------------------------------------------------------------------
*/
/**
* return an array of personalised blocks (each block is an array)
*
* @param Bool onlyVisible : if true, only visibles blocks are returned
* @return Array
*/
protected function getPersonalisedBlocks($onlyVisible=false, $lang='', $emptyContent=false)
{
global $user;
if($lang=="") $lang=$user['language'];
$returned=array();
$sql="SELECT pt.id, pt.visible, pt.nfo, ptl.lang, ptl.title, ptl.content
FROM ".$this->tables['personalised']." pt
LEFT JOIN ".$this->tables['personalised_langs']." ptl
ON pt.id=ptl.id
WHERE (ptl.lang = '*' OR ptl.lang = '".pwg_db_real_escape_string($lang)."') ";
if($onlyVisible) $sql.=" AND pt.visible = 'y' ";
if($emptyContent==false) $sql.=" AND ptl.content != '' ";
$sql.=" ORDER BY pt.id, ptl.lang ASC ";
$result=pwg_query($sql);
if($result)
{
while($row=pwg_db_fetch_assoc($result))
{
$row['content'] = trigger_change('get_extended_desc', $row['content']);
$returned[]=$row;
}
}
return($returned);
}
/**
* return values for a given personalised block
*
* @param String $id : link id
* @return Array
*/
protected function getPersonalisedBlock($id)
{
$returned=array(
'visible' => false,
'nfo' => '',
'langs' => array()
);
$sql="SELECT visible, nfo
FROM ".$this->tables['personalised']."
WHERE id='$id';";
$result=pwg_query($sql);
if($result)
{
while($row=pwg_db_fetch_assoc($result))
{
$returned['visible']=$row['visible'];
$returned['nfo']=$row['nfo'];
}
$sql="SELECT lang, title, content
FROM ".$this->tables['personalised_langs']."
WHERE id='$id'
ORDER BY lang ASC;";
$result=pwg_query($sql);
if($result)
{
while($row=pwg_db_fetch_assoc($result))
{
$returned['langs'][$row['lang']]=$row;
}
}
}
return($returned);
}
/**
* set values for a personalised block
* if block id is empty : create a new block
* if not, update block
*
* @param String $id : block id
* @return Integer : -1 if fails
* otherwise block id
*/
protected function setPersonalisedBlock($id, $visible, $nfo, $langs)
{
$ok=false;
if($id=='')
{
$sql="INSERT INTO ".$this->tables['personalised']." (visible,nfo) VALUES
(
'$visible',
'".$nfo."'
);";
$result=pwg_query($sql);
if($result) $ok=true;
$id=pwg_db_insert_id();
}
else
{
$sql="UPDATE ".$this->tables['personalised']."
SET visible='$visible',
nfo='".pwg_db_real_escape_string(stripslashes($nfo))."'
WHERE id='$id';";
$result=pwg_query($sql);
if($result)
{
$sql="DELETE FROM ".$this->tables['personalised_langs']."
WHERE id='$id';";
$result=pwg_query($sql);
if($result) $ok=true;
}
}
if($ok)
{
$values=array();
foreach($langs as $key => $lang)
{
$values[]="('$id',
'".substr($lang['lang'],0,5)."',
'".pwg_db_real_escape_string(stripslashes($lang['title']))."',
'".pwg_db_real_escape_string(stripslashes($lang['content']))."')";
}
$sql="INSERT INTO ".$this->tables['personalised_langs']." VALUES ".implode(',', $values);
$result=pwg_query($sql);
if($result) $ok=true;
}
if($ok) return($id);
return(-1);
}
/**
* delete a given personalised block
*
* @param String $id : block id
* @return Bool : true if deleted, otherwise false
*/
protected function deletePersonalisedBlock($id)
{
$sql="DELETE FROM ".$this->tables['personalised']."
WHERE id = '$id';";
$result=pwg_query($sql);
if($result)
{
$sql="DELETE FROM ".$this->tables['personalised_langs']."
WHERE id = '$id';";
$result=pwg_query($sql);
if($result) return(true);
}
return(false);
}
/**
* return an array of all registered blocks
* each array item is an array :
* String 'id' => ''
* Integer 'order' => 0
* Array 'users' => array()
* Array 'groups' => array()
* String 'name' => ''
* String 'owner' => ''
*
* @param Bool $userFiltered : true if returned blocks are filtered to current
* user
* @return Array
*/
protected function getRegisteredBlocks($userFiltered=false)
{
global $conf, $user;
$returned=array();
$order=0;
$users=new GPCUsers();
$groups=new GPCGroups();
$menu = new BlockManager('menubar');
$menu->load_registered_blocks();
$registeredBlocks = $menu->get_registered_blocks();
$nbExistingGroups=0;
$sql="SELECT COUNT(id) AS nbGroup
FROM `".GROUPS_TABLE."`;";
$result=pwg_query($sql);
if($result)
{
while($row=pwg_db_fetch_assoc($result))
{
$nbExistingGroups=$row['nbGroup'];
}
}
$userGroups=array();
$sql="SELECT group_id
FROM ".USER_GROUP_TABLE."
WHERE user_id = '".$user['id']."';";
$result=pwg_query($sql);
if($result)
{
while($row=pwg_db_fetch_assoc($result))
{
$userGroups[$row['group_id']]='';
}
}
$sql="SELECT id, `order`, users, `groups`
FROM ".$this->tables['blocks']."
ORDER BY `order`;";
$result=pwg_query($sql);
if($result)
{
while($row=pwg_db_fetch_assoc($result))
{
$row['users']=(trim($row['users'])=='')?array():explode(',', $row['users']);
$row['groups']=(trim($row['groups'])=='')?array():explode(',', $row['groups']);
if(isset($registeredBlocks[$row['id']]))
{
$ok=true;
if($userFiltered)
{
// filter access enabled
$users->setAlloweds($row['users'], false);
if($users->isAllowed($user['status']))
{
// user is authorized, check group
if(count($userGroups))
{
// users is attached to one group or more, check access for each group
// at least, one group must have right access
$nbOk=count($userGroups);
$groups->setAlloweds($row['groups'], false);
foreach($row['groups'] as $val)
{
if(isset($userGroups[$val]) and !$groups->isAllowed($val)) $nbOk--;
}
// no group is authorized to access the menu
if($nbOk==0) $ok=false;
}
elseif($nbExistingGroups>0 and count($row['groups'])>0)
{
// if user is not attached to any group and if at least there 1
// existing group, no authorization to access to the menu
$ok=false;
}
}
else
{
// user not authorized
$ok=false;
}
}
if($ok)
{
$returned[$row['id']]=array(
'id' => $row['id'],
'order' => $row['order'],
'users' => $row['users'],
'groups'=> $row['groups'],
'name' => $registeredBlocks[$row['id']]->get_name(),
'owner' => $registeredBlocks[$row['id']]->get_owner()
);
$order=$row['order'];
}
unset($registeredBlocks[$row['id']]);
}
}
}
/*
* add new blocks, unknown from plugin
* by default, users & groups are visibles
*/
foreach($registeredBlocks as $key=>$val)
{
$order+=10;
$returned[$key]=array(
'id' => $key,
'order' => $order,
'users' => array(),
'groups'=> array(),
'name' => $val->get_name(),
'owner' => $val->get_owner()
);
}
return($returned);
}
/**
* set order for registered blocks
*
* note : Piwigo's order is maintened
*
* @param Array $block : array of block ; each items is an array
* String 'id' => ''
* Integer 'order' => ''
* Array 'users' => array()
* Array 'groups' => array()
* @return Bool : true, false is something is wrong
*/
protected function setRegisteredBlocks($blocks)
{
$returned=true;
$sql="DELETE FROM ".$this->tables['blocks'];
pwg_query($sql);
foreach($blocks as $block)
{
$sql="INSERT INTO ".$this->tables['blocks']." VALUES (
'".$block['id']."',
'".$block['order']."',
'".implode(',', $block['users'])."',
'".implode(',', $block['groups'])."'
);";
$result=pwg_query($sql);
if(!$result) $returned=false;
}
return($returned);
}
static public function checkPluginRelease()
{
global $template, $prefixeTable, $conf;
$config=array();
GPCCore::loadConfig('amm', $config);
if($config['installed']!=AMM_VERSION2)
{
/* the plugin was updated without being deactivated
* deactivate + activate the plugin to process the database upgrade
*/
include(AMM_PATH."amm_install.class.inc.php");
$amm=new AMM_Install($prefixeTable, dirname(__FILE__));
$amm->deactivate();
$amm->activate();
if(is_object($template)) $template->delete_compiled_templates();
}
}
protected function sortCoreBlocksItemsCompare($a, $b)
{
if($a['container']==$b['container'])
{
if($a['order']==$b['order']) return(0);
return(($a['order']<$b['order'])?-1:1);
}
else return(($a['container']<$b['container'])?-1:1);
}
protected function sortCoreBlocksItems()
{
uasort($this->config['amm_blocks_items'], array($this, "sortCoreBlocksItemsCompare"));
}
} // amm_root class
?>