XPressME Integration Kit

Trac

source: branches/Ver2.4/xpressme_integration_kit/class/check_blocks_class.php @ 834

Last change on this file since 834 was 811, checked in by toemon, 12 years ago

XCL2.2.1 xoops_gethandler('SingleToken?')エラー対策 fixed #414

File size: 12.8 KB
Line 
1<?php
2
3//================================================================
4// check block table
5// 2007-10-10 K.OHWADA
6//================================================================
7
8//---------------------------------------------------------
9// this program works in XOOPS 2.0.16aJP, XOOPS Cube 2.1.2, XOOPS 2.0.17
10//
11// NOTE
12// xoops 2.0.16 JP  : class/xoopsblock.php and kernel/block.php are different
13// xoops cube 2.1.2 : class/xoopsblock.php and kernel/block.php are the same
14// xoops 2.0.17     : no token class
15//---------------------------------------------------------
16
17include_once dirname( __FILE__ ).'/../../../mainfile.php';
18include_once XOOPS_ROOT_PATH.'/class/xoopsblock.php';
19
20//=========================================================
21// class xoops_block_check
22//=========================================================
23class xoops_block_check
24{
25        var $_TOKEN_NAME = 'block';
26        var $module_id        = 0;
27        var $_msg_array  = array();
28        var $_error_flag = false;
29        var $_xoops_version = '2.0';
30        var $_use_token     = false;
31        var $_blocks_check_array = array();
32        var $_all_ok_flag = true;
33        var $_module_dir = '';
34        var $module_name = '';
35        var $module_dirname = '';
36        var $remove_submit_form;
37        var $update_link;
38//---------------------------------------------------------
39// constructor
40//---------------------------------------------------------
41function xoops_block_check()
42{
43        $this->_detect_xoops_version();
44}
45
46function &getInstance()
47{
48        static $instance;
49        if (!isset($instance))
50        {
51                $instance = new xoops_block_check();
52        }
53        return $instance;
54}
55
56//--------------------------------------------------------
57// public
58//--------------------------------------------------------
59function get_message()
60{
61        return $this->_get_msg();
62}
63
64function get_op()
65{
66        $op  = '';
67        $mid = 0;
68        foreach ( $_POST as $k => $v )
69        {
70                if ( preg_match( "/^amid:/", $k ) )
71                {
72                        $op  = 'remove_all_block';
73                        $mid = intval( str_replace("amid:", "", $k) );
74                        break;
75                }
76                if ( preg_match( "/^mid:/", $k ) )
77                {
78                        $op  = 'remove_block';
79                        $mid = intval( str_replace("mid:", "", $k) );
80                        break;
81                }
82        }
83        $this->module_id = $mid;
84        return $op;
85}
86
87function check_blocks($module_dir)
88{
89        $this->_all_ok_flag = true;
90        $this->_module_dir = $module_dir;
91        $this->module_dirname = $module_dir;
92
93        if ( $this->_use_token )
94        {
95                $text = $this->_create_token();
96        }
97
98        $objs =& $this->_get_module_objects();
99        $obj = $objs[0];
100        if (!empty($obj)){
101                $this->module_id = $obj->getVar('mid', 'n');
102                $url = $this->_build_url_module_update( $this->module_dirname );
103                $this->update_link = $this->_build_url_module_update( $this->module_dirname );
104                $this->remove_submit_form .= '<input type="submit" name="mid:'.$this->module_id.'" value="Remove Block: '.$this->module_name.'" />'."\n";
105
106                return $this->_check_block_by_module( $obj );
107
108        } else {
109                $this->_err( 'Modules Not Found' );
110                return false;
111        }
112}
113
114function remove_all_block()
115{
116        $text = "<h1>Remove xoops block table</h1>\n";
117
118
119/*
120        if( $this->_use_token && !$this->_validate_token() )
121        {
122                $text .= '<h3 style="color:#ff0000">Token Error</h3>'."\n";
123                $text .= '<a href="block_check.php">Check Bloks</a>';
124                return $text;
125        }
126*/
127        $module_obj =& $this->_get_module_object_bymodule_id( $this->module_id );
128        if( !is_object($module_obj) )
129        {
130                $text .= '<h3 style="color:#ff0000">"No Module: mid='. $this->module_id ."</h3>\n";
131                $text .= "<br />\n";
132                $text .= '<a href="check_blocks.php">Check Bloks</a>';
133                return $text;
134        }
135
136        $ret = $this->_remove_block_bymodule_id( $this->module_id );
137        if ( $ret ) {
138                $text .= '<h4 style="color:#0000ff">Success</h4>'."\n";
139        } else {
140                $text .= '<h4 style="color:#ff0000">Failed</h4>'."\n";
141        }
142
143        $url = $this->_build_url_module_update( $module_obj->getVar('dirname', 'n') );
144        $text .= '<a href="'.$url.'">GO to Module Update: '.$module_obj->getVar('name', 's')."</a><br /><br />\n";
145//      $text .= '<a href="check_blocks.php">Check xoops block table</a><br />'."\n";
146
147        return $text;
148}
149function remove_block()
150{
151        $text = "<h1>Remove xoops block table</h1>\n";
152
153        $error = false;
154        $block_objs =& $this->_get_block_object_orber_num_bymodule_id( $this->module_id);
155        $module_obj =& $this->_get_module_object_bymodule_id( $this->module_id );
156        $infos    =& $module_obj->getInfo('blocks');
157
158        foreach ( $infos as $num => $info )
159        {
160                $block_err = false;
161                if ( !isset( $block_objs[ $num ] ) ){
162                        $block_err = true;
163                } else {
164                        $block_obj = $block_objs[ $num ];
165                        if ( isset($info['file']) && ( $info['file'] != $block_obj->getVar('func_file', 'n') ) ) $block_err = true;
166                        if ( isset($info['show_func']) && ( $info['show_func'] != $block_obj->getVar('show_func', 'n') ) ) $block_err = true;
167                        if ( isset($info['edit_func']) && ( $info['edit_func'] != $block_obj->getVar('edit_func', 'n') ) ) $block_err = true;
168                        if ( isset($info['template']) && ( $info['template'] != $block_obj->getVar('template', 'n') ) ) $block_err = true;
169                        if ( isset($info['options']) ){
170                                $option_arr_1 = explode( '|', $info['options'] );
171                                $option_arr_2 = explode( '|', $block_obj->getVar('options', 'n') );
172                       
173                                $excludes_block = array();
174                       
175                                if (in_array($info['file'],$excludes_block)){
176                                        if ( count($option_arr_1) > count($option_arr_2) ) $block_err = true;
177                                } else {
178                                        if ( count($option_arr_1) != count($option_arr_2) ) $block_err = true;
179                                }
180                        }
181                }
182                if ($block_err){
183                        $ret = $this->_delete_block( $block_obj );
184                        if ( !$ret ) $error = true;
185                }
186        }
187
188        if ( !$error ) {
189                $text .= '<h4 style="color:#0000ff">Success</h4>'."\n";
190        } else {
191                $text .= '<h4 style="color:#ff0000">Failed</h4>'."\n";
192        }
193
194        $url = $this->_build_url_module_update( $module_obj->getVar('dirname', 'n') );
195        $text .= '<a href="'.$url.'">GO to Module Update: '.$module_obj->getVar('name', 's')."</a><br /><br />\n";
196//      $text .= '<a href="check_blocks.php">Check xoops block table</a><br />'."\n";
197
198        return $text;
199}
200
201//--------------------------------------------------------
202// private
203//--------------------------------------------------------
204function _check_block_by_module( &$module_obj )
205{
206        $this->_msg_array = array();
207
208        $mid      =  $module_obj->getVar('mid', 'n');
209        $mod_name =  $module_obj->getVar('name', 's');
210        $dirname  =  $module_obj->getVar('dirname', 'n');
211        $infos    =& $module_obj->getInfo('blocks');
212
213        $this->module_name = $mod_name ;
214        $this->module_dirname = $dirname ;
215       
216
217        if ( !is_array($infos) || !count($infos) )
218        {
219                $this->_msg( 'No block' );
220                return $this->_get_msg();
221        }
222
223
224        $block_objs =& $this->_get_block_object_orber_num_bymodule_id( $mid );
225
226        foreach ( $infos as $num => $info )
227        {
228                if ( !isset( $block_objs[ $num ] ) )
229                {
230                        $this->_err( htmlspecialchars( $info['name'] ).': not exist in block table' );
231                        $this->_all_ok_flag = false;
232
233                        continue;
234                }
235
236                $this->_check_block_by_obj( $info, $block_objs[ $num ] );
237        }
238
239        if ($this->_all_ok_flag){
240        } else {               
241        }
242
243        return $this->_all_ok_flag;
244}
245
246function _check_block_by_obj( &$info, &$block_obj )
247{
248        $this->_error_flag = false;
249       
250        $bid = $block_obj->getVar('bid', 'n');
251        $edit_url = $this->_build_url_block_edit( $bid );
252        $name = '<a href="' . $edit_url . '">'. htmlspecialchars( $info['name'] ). '</a>';
253//      $name = htmlspecialchars( $info['name'] );
254
255        if ( isset($info['file']) && ( $info['file'] != $block_obj->getVar('func_file', 'n') ) )
256        {
257                $this->_err( $name.': file unmatch' );
258        }
259
260        if ( isset($info['show_func']) && ( $info['show_func'] != $block_obj->getVar('show_func', 'n') ) )
261        {
262                $this->_err( $name.': show_func unmatch' );
263        }
264
265        if ( isset($info['edit_func']) && ( $info['edit_func'] != $block_obj->getVar('edit_func', 'n') ) )
266        {
267                $this->_err( $name.': edit_func unmatch' );
268        }
269
270        if ( isset($info['template']) && ( $info['template'] != $block_obj->getVar('template', 'n') ) )
271        {
272                $this->_err( $name.': template unmatch' );
273        }
274
275        if ( isset($info['options']) )
276        {
277                $option_arr_1 = explode( '|', $info['options'] );
278                $option_arr_2 = explode( '|', $block_obj->getVar('options', 'n') );
279               
280                $excludes_block = array();
281               
282                if (in_array($info['file'],$excludes_block)){
283                        if ( count($option_arr_1) > count($option_arr_2) )
284                        {
285                                $this->_err( $name.': options count unmatch' );
286                        }
287
288                } else {
289                        if ( count($option_arr_1) != count($option_arr_2) )
290                        {
291                                $this->_err( $name.': options count unmatch' );
292                        }
293                }
294        }
295
296        if ( !$this->_error_flag )
297        {
298                $this->_msg( $name.': OK' );
299        } else {
300                $this->_all_ok_flag = false;
301        }
302
303}
304
305function _remove_block_bymodule_id( $mid )
306{
307        $error = false;
308        $objs =& $this->_get_block_object_bymodule_id( $mid );
309        foreach ( $objs as $obj )
310        {
311                $ret = $this->_delete_block( $obj );
312                if ( !$ret )
313                {       $error = true;  }
314        }
315        if ( $error )
316        {       return false;   }
317        return true;
318}
319
320function _msg( $msg )
321{
322        $this->_msg_array[] = '&emsp;' .$msg;
323}
324
325function _err( $msg )
326{
327        $this->_msg_array[] = '&emsp;' . $this->_highlight( $msg );
328        $this->_error_flag  = true;
329}
330
331function _get_msg()
332{
333        $msg = implode( "<br />\n", $this->_msg_array );
334        return $msg;
335}
336
337
338function _highlight( $msg )
339{
340        $text = null;
341        if ( $msg )
342        {
343                $text = '<span style="color: #ff0000;">'.$msg.'</span>';
344        }
345        return $text;
346}
347
348function _build_url_module_update( $dirname )
349{
350        if ( $this->_xoops_version == '2.1' ) {
351                $url = XOOPS_URL.'/modules/legacy/admin/index.php?action=ModuleUpdate&dirname='.$dirname;
352        } else {
353                $url = XOOPS_URL.'/modules/system/admin.php?fct=modulesadmin&op=update&module='.$dirname;
354        }
355        return $url;
356}
357function _build_url_block_edit( $bid )
358{
359        $dir_name = $this->module_dirname;
360        if (file_exists(XOOPS_ROOT_PATH . '/modules/altsys/admin/index.php')){
361//              $url = XOOPS_URL.'/modules/altsys/admin/index.php?mode=admin&lib=altsys&page=myblocksadmin&dirname=xp_trunk&op=edit&bid='.$bid;
362                $url = XOOPS_URL.'/modules/'.$dir_name.'/admin/index.php?mode=admin&lib=altsys&page=myblocksadmin&dirname='.$dir_name.'xp_trunk&op=edit&bid='.$bid;
363        } else if ( $this->_xoops_version == '2.1' ) {
364                $url = XOOPS_URL.'/modules/legacy/admin/index.php?action=BlockEdit&bid='.$bid;
365        } else {
366                $url = XOOPS_URL.'/modules/system/admin.php?fct=blocksadmin&op=mod&bid='.$bid;
367        }
368        return $url;
369}
370//--------------------------------------------------------
371// user handler
372//--------------------------------------------------------
373function is_admin()
374{
375        global $xoopsUser;
376        if ( is_object($xoopsUser) && $xoopsUser->isAdmin() )
377        {       return true;    }
378        return false;
379}
380
381//--------------------------------------------------------
382// module handler
383//--------------------------------------------------------
384function &_get_module_objects()
385{
386        $criteria = new CriteriaCompo();
387        $criteria->add( new Criteria('isactive', '1', '=') );
388        $criteria->add( new Criteria('dirname', $this->_module_dir, '=') );
389
390        $module_handler =& xoops_gethandler('module');
391        $objs           =& $module_handler->getObjects( $criteria );
392        return $objs;
393}
394
395function &_get_module_object_bymodule_id( $mid )
396{
397        $module_handler =& xoops_gethandler('module');
398        $obj            =& $module_handler->get( $mid );
399        return $obj;
400}
401
402//--------------------------------------------------------
403// block handler
404//--------------------------------------------------------
405function &_get_block_object_orber_num_bymodule_id( $mid )
406{
407        $arr  = array();
408        $objs =& $this->_get_block_object_bymodule_id( $mid );
409        foreach ( $objs as $obj )
410        {
411                $arr[ $obj->getVar('func_num', 'n') ] = $obj;
412        }
413        return $arr;
414}
415
416function &_get_block_object_bymodule_id( $mid, $asobject=true )
417{
418        if ( defined('ICMS_VERSION_BUILD') && ICMS_VERSION_BUILD > 27  ) { /* ImpressCMS 1.2+ */
419                $block_handler =& xoops_gethandler ('block');
420                $objs =& $block_handler->getByModule( $mid, $asobject );
421        } else { /* legacy support */
422                $objs =& XoopsBlock::getByModule( $mid, $asobject ) ; /* from class/xoopsblock.php */
423        }
424        return $objs;
425}
426
427function _delete_block( &$obj )
428{
429// NOT use xoops_gethandler in xoops 2.0.16jp
430        return $obj->delete();
431}
432
433//--------------------------------------------------------
434// token handler
435//--------------------------------------------------------
436function _create_token()
437{
438        $obj =& XoopsSingleTokenHandler::quickCreate($this->_TOKEN_NAME);
439        return $obj->getHtml();
440}
441
442function _validate_token()
443{
444        return XoopsSingleTokenHandler::quickValidate( $this->_TOKEN_NAME );
445}
446
447//--------------------------------------------------------
448// xoops version
449//--------------------------------------------------------
450function get_xoops_version()
451{
452        return $this->_xoops_version;
453}
454
455function _detect_xoops_version()
456{
457// very easy way
458        if ( preg_match("/XOOPS[\s+]Cube.*[\s+]2\.1/i", XOOPS_VERSION) )
459        {
460                $this->_xoops_version = '2.1';
461        }
462
463// xoops 2.0.17 has no token class
464        if ( file_exists( XOOPS_ROOT_PATH.'/class/token.php' ) )
465        {
466                $this->_use_token = true;
467        }
468}
469
470// --- class end ---
471}
472
473?>
Note: See TracBrowser for help on using the repository browser.