source: extensions/AMenuManager/amm_ajax.php @ 9752

Last change on this file since 9752 was 9112, checked in by grum, 13 years ago

release 3.0.3
fix bug:2166,bug:2182

  • Property svn:executable set to *
File size: 22.0 KB
Line 
1<?php
2/*
3 * -----------------------------------------------------------------------------
4 * Plugin Name: Advanced Menu Manager
5 * -----------------------------------------------------------------------------
6 * Author     : Grum
7 *   email    : grum@piwigo.org
8 *   website  : http://photos.grum.fr
9 *   PWG user : http://forum.piwigo.org/profile.php?id=3706
10 *
11 *   << May the Little SpaceFrog be with you ! >>
12 *
13 * -----------------------------------------------------------------------------
14 *
15 * See main.inc.php for release information
16 *
17 * manage all the ajax requests
18 * -----------------------------------------------------------------------------
19 */
20
21
22  define('PHPWG_ROOT_PATH',dirname(dirname(dirname(__FILE__))).'/');
23
24  /*
25   * set ajax module in admin mode if request is used for admin interface
26   */
27  if(!isset($_REQUEST['ajaxfct'])) $_REQUEST['ajaxfct']='';
28  if(preg_match('/^admin\./i', $_REQUEST['ajaxfct']))
29  {
30    define('IN_ADMIN', true);
31  }
32
33  // the common.inc.php file loads all the main.inc.php plugins files
34  include_once(PHPWG_ROOT_PATH.'include/common.inc.php' );
35  include_once(PHPWG_PLUGINS_PATH.'GrumPluginClasses/classes/GPCAjax.class.inc.php');
36  include_once('amm_root.class.inc.php');
37
38  load_language('plugin.lang', AMM_PATH);
39
40
41  class AMM_Ajax extends AMM_root
42  {
43    /**
44     * constructor
45     */
46    public function __construct($prefixeTable, $filelocation)
47    {
48      parent::__construct($prefixeTable, $filelocation);
49      $this->loadConfig();
50      $this->checkRequest();
51      $this->returnAjaxContent();
52    }
53
54    /**
55     * check the $_REQUEST values and set default values
56     *
57     */
58    protected function checkRequest()
59    {
60      global $user;
61
62      if(!isset($_REQUEST['ajaxfct'])) $_REQUEST['ajaxfct']='';
63      if(!isset($_REQUEST['errcode'])) $_REQUEST['errcode']='';
64      if(!isset($_REQUEST['token'])) $_REQUEST['token']='';
65
66      // check if asked function is valid
67      if(!($_REQUEST['ajaxfct']=='admin.links.get' or
68           $_REQUEST['ajaxfct']=='admin.links.set' or
69           $_REQUEST['ajaxfct']=='admin.links.list' or
70           $_REQUEST['ajaxfct']=='admin.links.delete' or
71           $_REQUEST['ajaxfct']=='admin.links.order' or
72           $_REQUEST['ajaxfct']=='admin.links.setConfig' or
73           $_REQUEST['ajaxfct']=='admin.randomPict.setConfig' or
74           $_REQUEST['ajaxfct']=='admin.blocks.get' or
75           $_REQUEST['ajaxfct']=='admin.blocks.set' or
76           $_REQUEST['ajaxfct']=='admin.blocks.delete' or
77           $_REQUEST['ajaxfct']=='admin.blocks.list' or
78           $_REQUEST['ajaxfct']=='admin.album.setConfig' or
79           $_REQUEST['ajaxfct']=='admin.coreBlocks.setConfig'
80           )) $_REQUEST['ajaxfct']='';
81
82      if(preg_match('/^admin\./i', $_REQUEST['ajaxfct']) and !is_admin()) $_REQUEST['ajaxfct']='';
83
84
85      if($_REQUEST['ajaxfct']!='')
86      {
87        /*
88         * no check for admin.links.list request
89         */
90
91        /*
92         * check admin.links.get request
93         * check admin.blocks.get request
94         */
95        if($_REQUEST['ajaxfct']=='admin.links.get' or
96           $_REQUEST['ajaxfct']=='admin.blocks.get request')
97        {
98          if(!isset($_REQUEST['id'])) $_REQUEST['id']='';
99
100          if($_REQUEST['id']=='') $_REQUEST['ajaxfct']='';
101        }
102
103        /*
104         * check admin.links.set request
105         */
106        if($_REQUEST['ajaxfct']=='admin.links.set')
107        {
108          if(!isset($_REQUEST['id'])) $_REQUEST['id']='';
109          if(!isset($_REQUEST['datas']['label'])) $_REQUEST['datas']['label']='';
110          if(!isset($_REQUEST['datas']['url'])) $_REQUEST['datas']['url']='';
111          if(!isset($_REQUEST['datas']['icon'])) $_REQUEST['datas']['icon']='';
112          if(!isset($_REQUEST['datas']['mode'])) $_REQUEST['datas']['mode']='0';
113          if(!isset($_REQUEST['datas']['visible'])) $_REQUEST['datas']['visible']='n';
114          if(!isset($_REQUEST['datas']['accessUsers'])) $_REQUEST['datas']['accessUsers']=array();
115          if(!isset($_REQUEST['datas']['accessGroups'])) $_REQUEST['datas']['accessGroups']=array();
116
117          if($_REQUEST['token']!=get_pwg_token() or
118             $_REQUEST['datas']['label']=='' or
119             $_REQUEST['datas']['url']=='' or
120             $_REQUEST['datas']['icon']=='' or
121             !($_REQUEST['datas']['mode']=='0' or $_REQUEST['datas']['mode']=='1') or
122             !($_REQUEST['datas']['visible']=='y' or $_REQUEST['datas']['visible']=='n')
123            ) $_REQUEST['ajaxfct']='';
124        }
125
126        /*
127         * check admin.links.delete request
128         * check admin.blocks.delete request
129         *
130         */
131        if($_REQUEST['ajaxfct']=='admin.links.delete' or
132           $_REQUEST['ajaxfct']=='admin.blocks.delete')
133        {
134          if(!isset($_REQUEST['id'])) $_REQUEST['id']='';
135
136          if($_REQUEST['id']=='' or
137             $_REQUEST['token']!=get_pwg_token()
138            ) $_REQUEST['ajaxfct']='';
139        }
140
141        /*
142         * check admin.links.order request
143         */
144        if($_REQUEST['ajaxfct']=='admin.links.order')
145        {
146          if(!isset($_REQUEST['datas']['links'])) $_REQUEST['datas']['links']=array();
147
148          if(count($_REQUEST['datas']['links'])<=1 or
149             $_REQUEST['token']!=get_pwg_token()
150            ) $_REQUEST['ajaxfct']='';
151        }
152
153
154        /*
155         * check admin.links.setConfig request
156         */
157        if($_REQUEST['ajaxfct']=='admin.links.setConfig')
158        {
159          if(!isset($_REQUEST['datas']['showIcons'])) $_REQUEST['datas']['showIcons']='';
160          if(!isset($_REQUEST['datas']['title'])) $_REQUEST['datas']['title']=array();
161
162          if($_REQUEST['token']!=get_pwg_token() or
163             $_REQUEST['datas']['showIcons']=='' or
164             count($_REQUEST['datas']['title'])==0
165            ) $_REQUEST['ajaxfct']='';
166        }
167
168
169
170        /*
171         * check admin.randomPict.setConfig request
172         */
173        if($_REQUEST['ajaxfct']=='admin.randomPict.setConfig')
174        {
175          if(!isset($_REQUEST['datas']['blockHeight'])) $_REQUEST['datas']['blockHeight']='';
176          if(!isset($_REQUEST['datas']['blockTitles'])) $_REQUEST['datas']['blockTitles']=array();
177          if(!isset($_REQUEST['datas']['infosName'])) $_REQUEST['datas']['infosName']='';
178          if(!isset($_REQUEST['datas']['infosComment'])) $_REQUEST['datas']['infosComment']='';
179          if(!isset($_REQUEST['datas']['freqDelay'])) $_REQUEST['datas']['freqDelay']='';
180          if(!isset($_REQUEST['datas']['selectMode'])) $_REQUEST['datas']['selectMode']='';
181          if(!isset($_REQUEST['datas']['selectCat'])) $_REQUEST['datas']['selectCat']=array();
182
183          if($_REQUEST['token']!=get_pwg_token() or
184             !is_numeric($_REQUEST['datas']['blockHeight']) or
185             count($_REQUEST['datas']['blockTitles'])==0 or
186             !($_REQUEST['datas']['infosName']=='n' or
187               $_REQUEST['datas']['infosName']=='o' or
188               $_REQUEST['datas']['infosName']=='u') or
189             !($_REQUEST['datas']['infosComment']=='n' or
190               $_REQUEST['datas']['infosComment']=='o' or
191               $_REQUEST['datas']['infosComment']=='u') or
192             !is_numeric($_REQUEST['datas']['freqDelay']) or
193             !($_REQUEST['datas']['selectMode']=='a' or
194               $_REQUEST['datas']['selectMode']=='f' or
195               $_REQUEST['datas']['selectMode']=='c') or
196             ($_REQUEST['datas']['selectMode']=='c' and
197              count($_REQUEST['datas']['selectCat'])==0)
198            ) $_REQUEST['ajaxfct']='';
199        }
200
201
202        /*
203         * check admin.blocks.set request
204         */
205        if($_REQUEST['ajaxfct']=='admin.blocks.set')
206        {
207          if(!isset($_REQUEST['id'])) $_REQUEST['id']='';
208          if(!isset($_REQUEST['datas']['nfo'])) $_REQUEST['datas']['nfo']='';
209          if(!isset($_REQUEST['datas']['visible'])) $_REQUEST['datas']['visible']='';
210          if(!isset($_REQUEST['datas']['langs'])) $_REQUEST['datas']['langs']=array();
211
212          if($_REQUEST['token']!=get_pwg_token() or
213             $_REQUEST['datas']['nfo']=='' or
214             !($_REQUEST['datas']['visible']=='y' or $_REQUEST['datas']['visible']=='n') or
215             count($_REQUEST['datas']['langs'])==0
216            ) $_REQUEST['ajaxfct']='';
217        }
218
219
220
221
222        /*
223         * check admin.coreBlocks.setConfig request
224         */
225        if($_REQUEST['ajaxfct']=='admin.coreBlocks.setConfig')
226        {
227          if(!isset($_REQUEST['datas']['menuItems'])) $_REQUEST['datas']['menuItems']=array();
228          if(!isset($_REQUEST['datas']['blocks'])) $_REQUEST['datas']['blocks']=array();
229
230          if($_REQUEST['token']!=get_pwg_token() or
231             count($_REQUEST['datas']['menuItems'])!=count($this->defaultMenus)
232            ) $_REQUEST['ajaxfct']='';
233        }
234
235
236        /*
237         * check admin.album.setConfig request
238         */
239        if($_REQUEST['ajaxfct']=='admin.album.setConfig')
240        {
241          if(!isset($_REQUEST['datas']['selectCat'])) $_REQUEST['datas']['selectCat']=array();
242
243          if($_REQUEST['token']!=get_pwg_token()) $_REQUEST['ajaxfct']='';
244        }
245
246
247      }
248
249    } //checkRequest
250
251
252    /**
253     * return ajax content
254     */
255    protected function returnAjaxContent()
256    {
257      $result="KO!".l10n('g002_error_invalid_ajax_call');
258      switch($_REQUEST['ajaxfct'])
259      {
260        case 'admin.links.get':
261          $result=$this->ajax_amm_admin_linksGet($_REQUEST['id']);
262          break;
263        case 'admin.links.set':
264          $result=$this->ajax_amm_admin_linksSet($_REQUEST['id'],$_REQUEST['datas']['label'],$_REQUEST['datas']['url'],$_REQUEST['datas']['mode'],$_REQUEST['datas']['icon'],$_REQUEST['datas']['visible'],$_REQUEST['datas']['accessUsers'],$_REQUEST['datas']['accessGroups']);
265          break;
266        case 'admin.links.list':
267          $result=$this->ajax_amm_admin_linksList();
268          break;
269        case 'admin.links.order':
270          $result=$this->ajax_amm_admin_linksOrder($_REQUEST['datas']['links']);
271          break;
272        case 'admin.links.delete':
273          $result=$this->ajax_amm_admin_linksDelete($_REQUEST['id']);
274          break;
275        case 'admin.links.setConfig':
276          $result=$this->ajax_amm_admin_linksSetConfig($_REQUEST['datas']);
277          break;
278
279        case 'admin.randomPict.setConfig':
280          $result=$this->ajax_amm_admin_randomPictSetConfig($_REQUEST['datas']);
281          break;
282
283        case 'admin.blocks.get':
284          $result=$this->ajax_amm_admin_blocksGet($_REQUEST['id']);
285          break;
286        case 'admin.blocks.set':
287          $result=$this->ajax_amm_admin_blocksSet($_REQUEST['id'],$_REQUEST['datas']['visible'],$_REQUEST['datas']['nfo'],$_REQUEST['datas']['langs']);
288          break;
289        case 'admin.blocks.list':
290          $result=$this->ajax_amm_admin_blocksList();
291          break;
292        case 'admin.blocks.delete':
293          $result=$this->ajax_amm_admin_blocksDelete($_REQUEST['id']);
294          break;
295
296        case 'admin.coreBlocks.setConfig':
297          $result=$this->ajax_amm_admin_coreBlocksSetConfig($_REQUEST['datas']['menuItems'], $_REQUEST['datas']['blocks']);
298          break;
299
300        case 'admin.album.setConfig':
301          $result=$this->ajax_amm_admin_albumSetConfig($_REQUEST['datas']);
302          break;
303      }
304      GPCAjax::returnResult($result);
305    }
306
307
308    /*
309     * -------------------------------------------------------------------------
310     *
311     * ADMIN FUNCTIONS
312     *
313     * -------------------------------------------------------------------------
314     */
315
316
317    /*
318     * -------------------------------------------------------------------------
319     * Links
320     * -------------------------------------------------------------------------
321     */
322
323    /**
324     * return a html formatted list of urls
325     */
326    private function ajax_amm_admin_linksList()
327    {
328      global $template, $user;
329      $local_tpl = new Template(AMM_PATH."admin/", "");
330      $local_tpl->set_filename('body_page',
331                    dirname($this->getFileLocation()).'/admin/amm_linkslinks_detail.tpl');
332
333      $datas['links']=array();
334
335      $links=$this->getLinks();
336      foreach($links as $link)
337      {
338        $datas['links'][]=array(
339          'id' => $link['id'],
340          'label' => $link['label'],
341          'url' => $link['url'],
342          'mode' => l10n("g002_mode_".$this->urlsModes[$link['mode']]),
343          'icon' => "plugins/".AMM_DIR."/links_pictures/".$link['icon'],
344          'visible' => l10n('g002_yesno_'.$link['visible'])
345        );
346      }
347
348      $local_tpl->assign('themeconf', $template->get_template_vars('themeconf'));
349      $local_tpl->assign('datas', $datas);
350      $local_tpl->assign('plugin', array('PATH' => AMM_PATH));
351
352      return($local_tpl->parse('body_page', true));
353    }
354
355
356    /**
357     * update links order
358     *
359     * @param Array $links
360     * @return String : OK or KO
361     */
362    private function ajax_amm_admin_linksOrder($links)
363    {
364      return($this->setLinksOrder($links)?'OK':'KO');
365    }
366
367
368    /**
369     * delete a link
370     *
371     * @param Integer $id : link id
372     * @return String : OK or KO
373     */
374    private function ajax_amm_admin_linksDelete($id)
375    {
376      return($this->deleteLink($id)?'OK':'KO');
377    }
378
379    /**
380     * return link content as a json string
381     *
382     * @param Integer $id : link id
383     * @return String : json string
384     */
385    private function ajax_amm_admin_linksGet($id)
386    {
387      $link=$this->getLink($id);
388      $link['accessUsers']=explode(',', $link['accessUsers']);
389      $link['accessGroups']=explode(',', $link['accessGroups']);
390      return(json_encode($link));
391    }
392
393    /**
394     * set link values
395     * if id is empty, create a new link
396     *
397     * @param String $id : link id
398     * @param String $label : link label
399     * @param String $url : link url
400     * @param String $mode : link mode (open a new window or not)
401     * @param String $icon : displayed icon
402     * @param String $visible : link visibility
403     * @return String : $id if OK, otherwise -1
404     */
405    private function ajax_amm_admin_linksSet($id, $label, $url, $mode, $icon, $visible, $accessUsers, $accessGroups)
406    {
407      return($this->setLink($id, $label, $url, $mode, $icon, $visible, implode(',', $accessUsers), implode(',', $accessGroups)));
408    }
409
410    /**
411     * set the links config
412     *
413     * $config is an array with keys :
414     *  String 'showIcons' : values 'y' or 'n'
415     *  Array  'titles'    : each array occurs is an array('id' => '', 'value' => '')
416     *                            id = lang id ('fr_FR', 'en_UK', ...)
417     *
418     * @param Array $config
419     * @return String : OK or KO
420     */
421    private function ajax_amm_admin_linksSetConfig($config)
422    {
423      $this->config['amm_links_show_icons']=$config['showIcons'];
424
425      $this->config['amm_links_title']=array();
426      foreach($config['title'] as $title)
427      {
428        $this->config['amm_links_title'][$title['id']]=base64_encode($title['value']);
429      }
430
431      $this->saveConfig();
432
433      return('OK!'.l10n('g002_config_saved'));
434    }
435
436
437    /*
438     * -------------------------------------------------------------------------
439     * random picture
440     * -------------------------------------------------------------------------
441     */
442
443
444    /**
445     * set the random picture config
446     *
447     * $config is an array with keys :
448     *  Integer 'blockHeight' : if value=0, the browser assume an automatic size
449     *  Array  'blockTitles'  : each array occurs is an array('id' => '', 'value' => '')
450     *                            id = lang id ('fr_FR', 'en_UK', ...)
451     *  String 'infosName'      : allow to display picture's name ;
452     *                              can take 'n' (no), 'o' (over), or 'u' (under)
453     *  String 'infosComment'   : allow to display picture's comment ;
454     *                              can take 'n' (no), 'o' (over), or 'u' (under)
455     *  Integer 'freqDelay'     : allow to choose change frequency (delay is in
456     *                            milliseconds)
457     *                            if value=0, there is no change
458     *  String 'selectMode'     : allows to choose picture to be randomly selected
459     *                              can take 'a' (all), 'f' (webmaster's favorites), 'c' (from categories)
460     *  Array  'selectCat'      : list of selected categories (choosing a cat implies to choose all sub cat)
461     *
462     * @param Array $config
463     * @return String : OK or KO
464     */
465    private function ajax_amm_admin_randomPictSetConfig($config)
466    {
467      $this->config['amm_randompicture_selectMode']=$config['selectMode'];
468      $this->config['amm_randompicture_selectCat']=$config['selectCat'];
469      $this->config['amm_randompicture_showname']=$config['infosName'];
470      $this->config['amm_randompicture_showcomment']=$config['infosComment'];
471      $this->config['amm_randompicture_periodicchange']=$config['freqDelay'];
472      $this->config['amm_randompicture_height']=($config['blockHeight']==99)?0:$config['blockHeight'];
473      $this->config['amm_randompicture_title']=array();
474      foreach($config['blockTitles'] as $title)
475      {
476        $this->config['amm_randompicture_title'][$title['id']]=base64_encode($title['value']);
477      }
478
479      $this->saveConfig();
480      return('OK!'.l10n('g002_config_saved'));
481    }
482
483
484
485    /*
486     * -------------------------------------------------------------------------
487     * personalised blocks
488     * -------------------------------------------------------------------------
489     */
490
491    /**
492     * return a html formatted list of blocks
493     */
494    private function ajax_amm_admin_blocksList()
495    {
496      global $template, $user;
497
498      $local_tpl = new Template(AMM_PATH."admin/", "");
499      $local_tpl->set_filename('body_page',
500                    dirname($this->getFileLocation()).'/admin/amm_personalised_detail.tpl');
501
502
503
504      $datas=array(
505        'blocks'=>array()
506      );
507
508      $blocks=$this->getPersonalisedBlocks(false, '', true);
509      foreach($blocks as $block)
510      {
511        $datas['blocks'][]=array(
512          'id' => $block['id'],
513          'nfo' => $block['nfo'],
514          'title' => $block['title'],
515          'visible' => l10n('g002_yesno_'.$block['visible'])
516        );
517      }
518
519      $local_tpl->assign('themeconf', $template->get_template_vars('themeconf'));
520      $local_tpl->assign('datas', $datas);
521      $local_tpl->assign('plugin', array('PATH' => AMM_PATH));
522
523      return($local_tpl->parse('body_page', true));
524    }
525
526    /**
527     * delete a block
528     *
529     * @param Integer $id : block id
530     * @return String : OK or KO
531     */
532    private function ajax_amm_admin_blocksDelete($id)
533    {
534      return($this->deletePersonalisedBlock($id)?'OK':'KO');
535    }
536
537    /**
538     * return block content as a json string
539     *
540     * @param Integer $id : block id
541     * @return String : json string
542     */
543    private function ajax_amm_admin_blocksGet($id)
544    {
545      return(json_encode($this->getPersonalisedBlock($id)));
546    }
547
548    /**
549     * set block values
550     * if id is empty, create a new block
551     *
552     * @param String $id      : block id
553     * @param String $visible : block visibility ('y' or 'n')
554     * @param String $nfo     : block description
555     * @param Array  $langs   : block langs, each record is an array
556     *                                  array('title' => '', 'content' => '')
557     * @return String : $id if OK, otherwise -1
558     */
559    private function ajax_amm_admin_blocksSet($id, $visible, $nfo, $lang)
560    {
561      return($this->setPersonalisedBlock($id, $visible, $nfo, $lang));
562    }
563
564    /*
565     * -------------------------------------------------------------------------
566     * core blocks
567     * -------------------------------------------------------------------------
568     */
569
570    /**
571     * set the menu config
572     *  - core blocks content
573     *  - blocks order&visibility
574     *
575     * @param Array $subMenus : for core blocks, sub menu items
576     *                          array(
577     *                            'subMenuId' => array(
578     *                                          'visibility' => (String),
579     *                                          'order' => (Integer),
580     *                                          'container' => (String),
581     *                                        )
582     *                          )
583     * @param Array $menus    : menu blocks order&visibility
584     *                          array(
585     *                            'block1' => array(
586     *                                'id'    => (String),
587     *                                'order' => (Integer),
588     *                                'users' => array(),
589     *                                'groups' => array()
590     *                            )
591     *                          )
592     * @return String : OK or KO
593     */
594    private function ajax_amm_admin_coreBlocksSetConfig($subMenu, $menus)
595    {
596      foreach($subMenu as $key=>$val)
597      {
598        if(!isset($subMenu[$key]['visibilityUser'])) $subMenu[$key]['visibilityUser']=array();
599        if(!isset($subMenu[$key]['visibilityGroup'])) $subMenu[$key]['visibilityGroup']=array();
600
601        $subMenu[$key]['visibility']=implode(',', $subMenu[$key]['visibilityUser']).'/'.implode(',', $subMenu[$key]['visibilityGroup']);
602        unset($subMenu[$key]['visibilityUser']);
603        unset($subMenu[$key]['visibilityGroup']);
604      }
605      $this->config['amm_blocks_items']=$subMenu;
606      $this->saveConfig();
607
608
609      foreach($menus as $key=>$val)
610      {
611        if(!isset($menus[$key]['users'])) $menus[$key]['users']=array();
612        if(!isset($menus[$key]['groups'])) $menus[$key]['groups']=array();
613      }
614      $this->setRegisteredBlocks($menus);
615
616      return('OK!'.l10n('g002_config_saved'));
617    }
618
619
620
621    /*
622     * -------------------------------------------------------------------------
623     * album to menu
624     * -------------------------------------------------------------------------
625     */
626
627
628    /**
629     * set the album to menu config
630     *
631     * $config is an array with keys :
632     *  Array  'selectCat'      : list of selected categories
633     *
634     * @param Array $config
635     * @return String : OK or KO
636     */
637    private function ajax_amm_admin_albumSetConfig($config)
638    {
639      $this->config['amm_albums_to_menu']=$config['selectCat'];
640
641      $this->saveConfig();
642      return('OK!'.l10n('g002_config_saved'));
643    }
644
645
646  } //class
647
648
649  $returned=new AMM_Ajax($prefixeTable, __FILE__);
650?>
651
Note: See TracBrowser for help on using the repository browser.