XOOPS 2.5.6  Final
 All Classes Namespaces Files Functions Variables Pages
main.php
Go to the documentation of this file.
1 <?php
2 // $Id: main.php 10982 2013-02-04 19:59:15Z trabis $
3 // ------------------------------------------------------------------------ //
4 // XOOPS - PHP Content Management System //
5 // Copyright (c) 2000 XOOPS.org //
6 // <http://www.xoops.org/> //
7 // ------------------------------------------------------------------------ //
8 // This program is free software; you can redistribute it and/or modify //
9 // it under the terms of the GNU General Public License as published by //
10 // the Free Software Foundation; either version 2 of the License, or //
11 // (at your option) any later version. //
12 // //
13 // You may not change or alter any portion of this comment or credits //
14 // of supporting developers from this source code or any supporting //
15 // source code which is considered copyrighted (c) material of the //
16 // original comment or credit authors. //
17 // //
18 // This program is distributed in the hope that it will be useful, //
19 // but WITHOUT ANY WARRANTY; without even the implied warranty of //
20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
21 // GNU General Public License for more details. //
22 // //
23 // You should have received a copy of the GNU General Public License //
24 // along with this program; if not, write to the Free Software //
25 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA //
26 // ------------------------------------------------------------------------ //
27 // Author: Kazumi Ono (AKA onokazu) //
28 // URL: http://www.myweb.ne.jp/, http://www.xoops.org/, http://jp.xoops.org/ //
29 // Project: The XOOPS Project //
30 // ------------------------------------------------------------------------- //
31 
32 // Check users rights
33 if ( !is_object($xoopsUser) || !is_object($xoopsModule) || !$xoopsUser->isAdmin($xoopsModule->mid()) ) exit( _NOPERM );
34 // Get Action type
35 $op = system_CleanVars ( $_REQUEST, 'op', 'list', 'string' );
36 
37 $filter = system_CleanVars ( $_GET, 'filter', 0, 'int' );
38 if ($filter) { $method = $_GET; } else { $method = $_REQUEST; }
39 
40 $sel = array(
41  'selmod' => -2,
42  'selgen' => -1,
43  'selgrp' => XOOPS_GROUP_USERS,
44  'selvis' => -1
45 );
46 foreach ($sel as $key => $value) {
47  $_{$key} = isset($_COOKIE[$key]) ? intval($_COOKIE[$key]) : $value;
48  ${$key} = system_CleanVars ($method, $key, $_{$key}, 'int');
49  setcookie($key, ${$key});
50 }
51 
52 $type = system_CleanVars ( $method, 'type', '', 'string' );
53 if ( $type == 'preview' ) $op = 'preview';
54 
55 if ( isset($_GET['op']) ) {
56  if ($_GET['op'] == "edit" || $_GET['op'] == "delete" || $_GET['op'] == "delete_ok" || $_GET['op'] == "clone") {
57  $op = $_GET['op'];
58  $bid = isset($_GET['bid']) ? intval($_GET['bid']) : 0;
59  }
60 }
61 
62 switch ($op) {
63 
64  case 'list':
65  // Define main template
66  $xoopsOption['template_main'] = 'system_blocks.html';
67  // Call Header
69  // Define Stylesheet
70  $xoTheme->addStylesheet( XOOPS_URL . '/modules/system/css/admin.css' );
71  // Define scripts
72  $xoTheme->addScript('browse.php?Frameworks/jquery/jquery.js');
73  $xoTheme->addScript('browse.php?Frameworks/jquery/plugins/jquery.ui.js');
74  $xoTheme->addScript('modules/system/js/admin.js');
75  $xoTheme->addScript('modules/system/js/blocks.js');
76  // Define Breadcrumb and tips
77  $xoBreadCrumb->addLink( _AM_SYSTEM_BLOCKS_ADMIN, system_adminVersion('blocksadmin', 'adminpath') );
78  $xoBreadCrumb->addHelp( system_adminVersion('blocksadmin', 'help') );
79  $xoBreadCrumb->addTips( sprintf(_AM_SYSTEM_BLOCKS_TIPS, system_AdminIcons('block.png'), system_AdminIcons('success.png'), system_AdminIcons('cancel.png')));
80  $xoBreadCrumb->render();
81 
82  // Initialize module handler
83  $module_handler =& xoops_gethandler('module');
84  $modules =& $module_handler->getObjects(null, true);
85  $criteria = new CriteriaCompo(new Criteria('hasmain', 1));
86 
87  $criteria->add(new Criteria('isactive', 1));
88  // Modules for blocks to be visible in
89  $display_list = $module_handler->getList($criteria);
90  unset($criteria);
91  // Initialize blocks handler
92  $block_handler =& xoops_getmodulehandler('block');
93  // Initialize module handler
94  $module_handler =& xoops_gethandler('module');
95  $modules =& $module_handler->getObjects(null, true);
96 
97  $filterform = new XoopsThemeForm('', 'filterform', 'admin.php', 'get');
98  $filterform->addElement( new XoopsFormHidden('fct', 'blocksadmin'));
99  $filterform->addElement( new XoopsFormHidden('op', 'list'));
100  $filterform->addElement( new XoopsFormHidden('filter', 1));
101  $sel_gen = new XoopsFormSelect( _AM_SYSTEM_BLOCKS_GENERATOR, 'selgen', $selgen);
102  $sel_gen->setExtra( "onchange='submit()'" );
103  $sel_gen->addOption( -1, _AM_SYSTEM_BLOCKS_TYPES );
104  $sel_gen->addOption( 0, _AM_SYSTEM_BLOCKS_CUSTOM );
105  foreach ($modules as $list) {
106  $sel_gen->addOption( $list->getVar('mid'), $list->getVar('name') );
107  }
108  $filterform->addElement( $sel_gen );
109 
110  $sel_mod = new XoopsFormSelect( _AM_SYSTEM_BLOCKS_SVISIBLEIN, 'selmod', $selmod );
111  $sel_mod->setExtra( "onchange='submit()'" );
112  ksort($display_list);
113  $display_list_spec[0] = _AM_SYSTEM_BLOCKS_ALLPAGES;
114  $display_list_spec[-1] = _AM_SYSTEM_BLOCKS_TOPPAGE;
115  $display_list_spec[-2] = _AM_SYSTEM_BLOCKS_TYPES;
116  $display_list = $display_list_spec + $display_list;
117  foreach ($display_list as $k => $v) {
118  $sel_mod->addOption( $k, $v );
119  }
120  $filterform->addElement( $sel_mod );
121 
122  // For selection of group access
123  $sel_grp = new XoopsFormSelect( _AM_SYSTEM_BLOCKS_GROUP, 'selgrp', $selgrp );
124  $sel_grp->setExtra( "onchange='submit()'" );
125  $member_handler =& xoops_gethandler('member');
126  $group_list = $member_handler->getGroupList();
127  $sel_grp->addOption( -1, _AM_SYSTEM_BLOCKS_TYPES );
128  $sel_grp->addOption( 0, _AM_SYSTEM_BLOCKS_UNASSIGNED );
129  foreach ($group_list as $k => $v) {
130  $sel_grp->addOption( $k, $v );
131  }
132  $filterform->addElement( $sel_grp );
133 
134  $sel_vis = new XoopsFormSelect( _AM_SYSTEM_BLOCKS_VISIBLE, 'selvis', $selvis );
135  $sel_vis->setExtra( "onchange='submit()'" );
136  $sel_vis->addOption( -1, _AM_SYSTEM_BLOCKS_TYPES );
137  $sel_vis->addOption( 0, _NO );
138  $sel_vis->addOption( 1, _YES );
139 
140  $filterform->addElement( $sel_vis );
141 
142  $filterform->assign( $xoopsTpl );
143 
144  /* Get blocks */
145  $selvis = ( $selvis == -1 ) ? null : $selvis;
146  $selmod = ( $selmod == -2 ) ? null : $selmod;
147  $order_block = ( isset($selvis) ? "" : "b.visible DESC, " ) . "b.side,b.weight,b.bid";
148 
149  if ($selgrp == 0) {
150  // get blocks that are not assigned to any groups
151  $blocks_arr = $block_handler->getNonGroupedBlocks($selmod, $toponlyblock = false, $selvis, $order_block);
152  } else {
153  $blocks_arr = $block_handler->getAllByGroupModule($selgrp, $selmod, $toponlyblock = false, $selvis, $order_block);
154  }
155 
156  if ($selgen >= 0) {
157  foreach (array_keys($blocks_arr) as $bid) {
158  if ($blocks_arr[$bid]->getVar("mid") != $selgen) {
159  unset($blocks_arr[$bid]);
160  }
161  }
162  }
163 
164  $arr = array();
165  foreach (array_keys($blocks_arr) as $i) {
166  $arr[$i] = $blocks_arr[$i]->toArray();
167  $xoopsTpl->append_by_ref( 'blocks', $arr[$i] );
168  }
169  $block = $block_handler->create();
170  $blockform = $block->getForm();
171  $xoopsTpl->assign('blockform', $blockform->render() );
172  // Call Footer
173  xoops_cp_footer();
174  break;
175 
176  case 'add':
177  // Define main template
178  $xoopsOption['template_main'] = 'system_blocks.html';
179  // Call Header
180  xoops_cp_header();
181  // Define Stylesheet
182  $xoTheme->addStylesheet( XOOPS_URL . '/modules/system/css/admin.css' );
183  $xoTheme->addStylesheet( XOOPS_URL . '/modules/system/css/ui/' . xoops_getModuleOption('jquery_theme', 'system') . '/ui.all.css');
184  // Define scripts
185  $xoTheme->addScript('browse.php?Frameworks/jquery/jquery.js');
186  $xoTheme->addScript('browse.php?Frameworks/jquery/plugins/jquery.ui.js');
187  $xoTheme->addScript('browse.php?Frameworks/jquery/plugins/jquery.form.js');
188  $xoTheme->addScript('modules/system/js/admin.js');
189  $xoTheme->addScript('modules/system/js/blocks.js');
190  // Define Breadcrumb and tips
191  $xoBreadCrumb->addLink( _AM_SYSTEM_BLOCKS_ADMIN, system_adminVersion('blocksadmin', 'adminpath') );
193  $xoBreadCrumb->render();
194  // Initialize blocks handler
195  $block_handler =& xoops_getmodulehandler('block');
196  $block = $block_handler->create();
197  $blockform = $block->getForm();
198  $xoopsTpl->assign('blockform', $blockform->render() );
199  // Call Footer
200  xoops_cp_footer();
201  break;
202 
203  case 'display':
204  // Initialize blocks handler
205  $block_handler =& xoops_getmodulehandler('block');
206  // Get variable
207  $block_id = system_CleanVars ( $_POST, 'bid', 0, 'int' );
208  $visible = system_CleanVars ( $_POST, 'visible', 0, 'int' );
209  if ( $block_id > 0 ) {
210  $block = $block_handler->get( $block_id );
211  $block->setVar('visible', $visible);
212  if (!$block_handler->insert( $block )) {
213  $error=true;
214  }
215  }
216  break;
217 
218  case 'drag':
219  // Initialize blocks handler
220  $block_handler =& xoops_getmodulehandler('block');
221  // Get variable
222  $block_id = system_CleanVars ( $_POST, 'bid', 0, 'int' );
223  $side = system_CleanVars ( $_POST, 'side', 0, 'int' );
224  if ( $block_id > 0 ) {
225  $block = $block_handler->get( $block_id );
226  $block->setVar('side', $side);
227  if (!$block_handler->insert( $block )) {
228  $error=true;
229  }
230  }
231  break;
232 
233  case 'order':
234  // Initialize blocks handler
235  $block_handler =& xoops_getmodulehandler('block');
236  if ( isset($_POST['blk'] ) ) {
237  $i=0;
238  foreach ($_POST['blk'] as $order) {
239  if( $order > 0 ) {
240  $block = $block_handler->get($order);
241  $block->setVar('weight', $i);
242  if (!$block_handler->insert($block)) {
243  $error=true;
244  }
245  $i++;
246  }
247  }
248  }
249  exit;
250  break;
251 
252  case 'preview':
253  if (!$GLOBALS['xoopsSecurity']->check()) {
254  redirect_header('admin.php?fct=blocksadmin', 3, implode('<br />', $GLOBALS['xoopsSecurity']->getErrors()));
255  exit();
256  }
257  // Initialize blocks handler
258  $block_handler =& xoops_getmodulehandler('block');
259  $block =& $block_handler->create();
260  $block->setVars($_POST);
261  $content = isset($_POST['content_block']) ? $_POST['content_block'] : '';
262  $block->setVar('content', $content);
264  echo '<div id="xo-preview-dialog" title="'.$block->getVar('title','s').'">'.$block->getContent( 's', $block->getVar('c_type') ).'</div>';
265  break;
266 
267  case 'save':
268  if (!$GLOBALS['xoopsSecurity']->check()) {
269  redirect_header('admin.php?fct=blocksadmin', 3, implode('<br />', $GLOBALS['xoopsSecurity']->getErrors()));
270  exit();
271  }
272  // Initialize blocks handler
273  $block_handler =& xoops_getmodulehandler('block');
274  // Get avatar id
275  $block_id = system_CleanVars ( $_POST, 'bid', 0, 'int' );
276  if ( $block_id > 0) {
277  $block =& $block_handler->get( $block_id );
278  } else {
279  $block =& $block_handler->create();
280  }
281  $block_type = system_CleanVars ($_POST, 'block_type', '', 'string');
282  $block->setVar('block_type', $block_type);
283 
284  if (!$block->isCustom()) {
285  $block->setVars( $_POST );
286  $type = $block->getVar('block_type');
287  $name = $block->getVar('name');
288  // Save block options
289  $options = $_POST['options'];
290  if (isset($options)) {
291  $options_count = count($options);
292  if ($options_count > 0) {
293  //Convert array values to comma-separated
294  for ( $i = 0; $i < $options_count; $i++ ) {
295  if (is_array($options[$i])) {
296  $options[$i] = implode(',', $options[$i]);
297  }
298  }
299  $options = implode('|', $options);
300  $block->setVar('options', $options);
301  }
302  }
303  } else {
304  $block->setVars( $_POST );
305  switch ($block->getVar('c_type')) {
306  case 'H':
308  break;
309  case 'P':
311  break;
312  case 'S':
314  break;
315  default:
317  break;
318  }
319  }
320  $block->setVar('name', $name);
321  $block->setVar('isactive', 1);
322 
323  $content = isset($_POST['content_block']) ? $_POST['content_block'] : '';
324  $block->setVar('content', $content);
325 
326  if (!$newid = $block_handler->insert( $block )) {
327  xoops_cp_header();
328  xoops_error( $block->getHtmlErrors() );
329  xoops_cp_footer();
330  exit();
331  }
332  if ( $newid != 0 ) {
333  $blocklinkmodule_handler =& xoops_getmodulehandler('blocklinkmodule');
334  // Delete old link
335  $criteria = new CriteriaCompo(new Criteria('block_id', $newid ));
336  $blocklinkmodule_handler->deleteAll($criteria);
337  // Assign link
338  $modules = $_POST['modules'];
339  foreach ($modules as $mid) {
340  $blocklinkmodule = $blocklinkmodule_handler->create();
341  $blocklinkmodule->setVar( 'block_id', $newid );
342  $blocklinkmodule->setVar( 'module_id', $mid );
343  if(!$blocklinkmodule_handler->insert($blocklinkmodule)){
344  xoops_cp_header();
345  xoops_error( $blocklinkmodule->getHtmlErrors() );
346  xoops_cp_footer();
347  exit();
348  }
349  }
350  }
351  $groupperm_handler =& xoops_gethandler('groupperm');
352  $groups = $_POST['groups'];
353  $groups_with_access =& $groupperm_handler->getGroupIds("block_read", $newid );
354  $removed_groups = array_diff($groups_with_access, $groups);
355  if (count($removed_groups) > 0) {
356  foreach ($removed_groups as $groupid) {
357  $criteria = new CriteriaCompo(new Criteria('gperm_name', 'block_read'));
358  $criteria->add(new Criteria('gperm_groupid', $groupid));
359  $criteria->add(new Criteria('gperm_itemid', $newid));
360  $criteria->add(new Criteria('gperm_modid', 1));
361  $perm = $groupperm_handler->getObjects($criteria);
362  if (isset($perm[0]) && is_object($perm[0])) {
363  $groupperm_handler->delete($perm[0]);
364  }
365  }
366  }
367  $new_groups = array_diff($groups, $groups_with_access);
368  if (count($new_groups) > 0) {
369  foreach ($new_groups as $groupid) {
370  $groupperm_handler->addRight("block_read", $newid, $groupid);
371  }
372  }
373  redirect_header('admin.php?fct=blocksadmin', 1, _AM_SYSTEM_BLOCKS_DBUPDATED);
374  break;
375 
376  case 'edit':
377  // Initialize blocks handler
378  $block_handler =& xoops_getmodulehandler('block');
379  // Get avatar id
380  $block_id = system_CleanVars ( $_REQUEST, 'bid', 0, 'int' );
381  if ( $block_id > 0 ) {
382  // Define main template
383  $xoopsOption['template_main'] = 'system_blocks.html';
384  // Call Header
385  xoops_cp_header();
386  // Define Stylesheet
387  $xoTheme->addStylesheet( XOOPS_URL . '/modules/system/css/admin.css' );
388  $xoTheme->addStylesheet( XOOPS_URL . '/modules/system/css/ui/' . xoops_getModuleOption('jquery_theme', 'system') . '/ui.all.css');
389  // Define scripts
390  $xoTheme->addScript('browse.php?Frameworks/jquery/jquery.js');
391  $xoTheme->addScript('browse.php?Frameworks/jquery/plugins/jquery.ui.js');
392  $xoTheme->addScript('browse.php?Frameworks/jquery/plugins/jquery.form.js');
393  $xoTheme->addScript('modules/system/js/admin.js');
394  // Define Breadcrumb and tips
395  $xoBreadCrumb->addLink( _AM_SYSTEM_BLOCKS_ADMIN, system_adminVersion('blocksadmin', 'adminpath') );
397  $xoBreadCrumb->render();
398 
399  $block = $block_handler->get($block_id);
400  $blockform = $block->getForm();
401  $xoopsTpl->assign('blockform', $blockform->render() );
402  // Call Footer
403  xoops_cp_footer();
404  } else {
405  redirect_header('admin.php?fct=blocksadmin', 1, _AM_SYSTEM_DBERROR);
406  }
407  break;
408 
409  case 'delete':
410  // Initialize blocks handler
411  $block_handler =& xoops_getmodulehandler('block');
412  // Get avatar id
413  $block_id = system_CleanVars ( $_REQUEST, 'bid', 0, 'int' );
414  if ( $block_id > 0 ) {
415  $block = $block_handler->get($block_id);
416  if ( $block->getVar('block_type') == 'S' ) {
417  redirect_header('admin.php?fct=blocksadmin', 4, _AM_SYSTEM_BLOCKS_SYSTEMCANT);
418  exit();
419  } elseif ($block->getVar('block_type') == 'M') {
420  // Fix for duplicated blocks created in 2.0.9 module update
421  // A module block can be deleted if there is more than 1 that
422  // has the same func_num/show_func which is mostly likely
423  // be the one that was duplicated in 2.0.9
424  if (1 >= $count = $block_handler->countSimilarBlocks($block->getVar('mid'), $block->getVar('func_num'), $block->getVar('show_func'))) {
425  redirect_header('admin.php?fct=blocksadmin', 4, _AM_SYSTEM_BLOCKS_MODULECANT);
426  exit();
427  }
428  }
429  // Define main template
430  $xoopsOption['template_main'] = 'system_header.html';
431  // Call Header
432  xoops_cp_header();
433  // Display Question
434  xoops_confirm(array('op' => 'delete_ok', 'fct' => 'blocksadmin', 'bid' => $block->getVar('bid')), 'admin.php', sprintf(_AM_SYSTEM_BLOCKS_RUSUREDEL, $block->getVar('title')) );
435  // Call Footer
436  xoops_cp_footer();
437  }
438  break;
439 
440  case 'delete_ok':
441  if (!$GLOBALS['xoopsSecurity']->check()) {
442  redirect_header('admin.php?fct=blocksadmin', 3, implode('<br />', $GLOBALS['xoopsSecurity']->getErrors()));
443  exit();
444  }
445  // Initialize blocks handler
446  $block_handler =& xoops_getmodulehandler('block');
447  // Get avatar id
448  $block_id = system_CleanVars ( $_POST, 'bid', 0, 'int' );
449  if ( $block_id > 0 ) {
450  $block = $block_handler->get( $block_id );
451  if ($block_handler->delete($block)) {
452  // Delete Group link
453  $blocklinkmodule_handler =& xoops_getmodulehandler('blocklinkmodule');
454  $blocklinkmodule = $blocklinkmodule_handler->getObjects(new CriteriaCompo(new Criteria('block_id', $block_id )));
455  foreach ($blocklinkmodule as $link) {
456  $blocklinkmodule_handler->delete($link, true);
457  }
458  // Delete Group permission
459  $groupperm_handler =& xoops_gethandler('groupperm');
460  $criteria = new CriteriaCompo(new Criteria('gperm_name', 'block_read'));
461  $criteria->add(new Criteria('gperm_itemid', $block_id));
462  $groupperm = $groupperm_handler->getObjects($criteria);
463  foreach ($groupperm as $perm) {
464  $groupperm_handler->delete($perm, true);
465  }
466  // Delete template
467  if ($block->getVar('template') != '') {
468  $tplfile_handler =& xoops_gethandler('tplfile');
469  $btemplate = $tplfile_handler->find($GLOBALS['xoopsConfig']['template_set'], 'block', $block_id);
470  if (count($btemplate) > 0) {
471  $tplfile_handler->delete($btemplate[0]);
472  }
473  }
474  redirect_header('admin.php?fct=blocksadmin', 1, _AM_SYSTEM_BLOCKS_DBUPDATED);
475  }
476  } else {
477  redirect_header('admin.php?fct=blocksadmin', 1, _AM_SYSTEM_DBERROR);
478  }
479  break;
480 
481  case 'clone':
482  // Initialize blocks handler
483  $block_handler =& xoops_getmodulehandler('block');
484  // Get avatar id
485  $block_id = system_CleanVars ( $_REQUEST, 'bid', 0, 'int' );
486  if ( $block_id > 0 ) {
487  // Define main template
488  $xoopsOption['template_main'] = 'system_blocks.html';
489  // Call Header
490  xoops_cp_header();
491  // Define Stylesheet
492  $xoTheme->addStylesheet( XOOPS_URL . '/modules/system/css/admin.css' );
493  // Define Breadcrumb and tips
494  $xoBreadCrumb->addLink( _AM_SYSTEM_BLOCKS_ADMIN, system_adminVersion('blocksadmin', 'adminpath') );
496  $xoBreadCrumb->render();
497 
498  $block = $block_handler->get($block_id);
499  $blockform = $block->getForm('clone');
500  $xoopsTpl->assign( 'blockform', $blockform->render() );
501  // Call Footer
502  xoops_cp_footer();
503  } else {
504  redirect_header('admin.php?fct=blocksadmin', 1, _AM_SYSTEM_DBERROR);
505  }
506  break;
507 
508 }
509 ?>