XPressME Integration Kit

Trac

source: trunk/xpressme_integration_kit/include/xpress_block_render.php @ 794

Last change on this file since 794 was 757, checked in by toemon, 13 years ago

ConfigFromXoopsクラスを廃止し、modInfoクラスを使用するように変更
WP2.2以下で使用するテンプレートold_templateの廃止

File size: 11.5 KB
RevLine 
[44]1<?php
[172]2if(!defined('XPRESS_BLOCK_RENDER_FUNCTION_READ')){
3        define('XPRESS_BLOCK_RENDER_FUNCTION_READ',1);
[44]4        require_once dirname( __FILE__ ) .'/xml.php' ;
5        require_once dirname( __FILE__ ) .'/xpress_cache.php' ;
[757]6        global $modInfo;
[44]7       
[757]8        if (!is_object($modInfo)){ // is call other modules
9                require_once dirname(dirname( __FILE__ )) .'/class/modInfo_class.php' ;
10                $modInfo = new modInfoClass;
[98]11        }
12       
[44]13        function xpress_block_cache_write($mydirname,$block_name,$block)
14        {
15                        $xml = xpress_XML_serialize($block);
16                        $xml_name = $block_name . '.xml';
[364]17                        if (WPLANG == 'ja_EUC'){
18                                $xml = str_replace('<?xml version="1.0" ?>', '<?xml version="1.0" encoding="EUC-JP" ?>' , $xml);
19                        }
[44]20                        xpress_cache_write($mydirname,$xml_name,$xml);
21        }
22        function xpress_block_cache_read($mydirname,$block_name)
23        {
[502]24                $xml_name = $block_name . '.xml';
25                $xml_data = xpress_cache_read($mydirname,$xml_name);
26               
27                $GLOBALS['DO_LIBXML_PATCH'] = get_xpress_mod_config($mydirname,'libxml_patch');
28               
29                // The character-code not treatable exists when 'XML_unserialize' of PHP5 processes EUC-JP.
30                // And, the result is returned by character-code UTF-8.
31                // Measures
32                // After the character-code is converted into UTF-8, XML_unserialize will be processed.
33                if ( strstr($xml_data, '<?xml version="1.0" encoding="EUC-JP" ?>') !== false
34                         && version_compare(PHP_VERSION, '5.0.0', '>') )
35                {
36                        $xml_data = str_replace('<?xml version="1.0" encoding="EUC-JP" ?>', '<?xml version="1.0" encoding="UTF-8" ?>', $xml_data);
37                        $ans = mb_convert_variables('UTF-8' , 'EUC-JP', &$xml_data); //EUC-JP to UTF-8
[364]38                        $ret = @xpress_XML_unserialize($xml_data);
[502]39                        $ans = mb_convert_variables('EUC-JP' , 'UTF-8', &$ret); //UTF-8 to EUC-JP
40                } else {
41                        $ret = xpress_XML_unserialize($xml_data);
42                }
43                return $ret;
[44]44        }
45       
46        function get_block_id($mydirname,$func_file,$options)
47        {
[749]48                global $xoopsDB;
[44]49                $options_string = '';
[708]50                $mid = get_block_mid($mydirname);
[44]51                foreach ($options as $val){
52                        if (!empty($options_string)) $options_string .='|';
53                        $options_string .= $val;
54                }
55                        $block_tbl = $xoopsDB->prefix('newblocks');     
56                        $module_dir = XOOPS_ROOT_PATH . '/modules/' . $mydirname;
57
[708]58                        $sql = "SELECT bid FROM $block_tbl WHERE (mid = $mid) AND (func_file LIKE '$func_file') AND (options LIKE '$options_string')";
[44]59                        $result =  $xoopsDB->query($sql, 0, 0);
60                        if ($xoopsDB->getRowsNum($result)  > 0){
61                                $row = $xoopsDB->fetchArray($result);
62                                $block_id = $row['bid'];
63                        }
64                        return $block_id;
65        }
66
[463]67        function get_block_mid($mydirname)
68        {
[749]69                global $xoopsDB;
[757]70                $module_handler =& xoops_gethandler('module');
71                $module =& $module_handler->getByDirname($mydirname);
72                $module_id = $module->getVar('mid');
73                return $module_id;
[463]74        }
75
[44]76        function get_xpress_theme_name($mydirname)
77        {
[749]78                global $wpdb,$xoopsDB;
[46]79               
80                if (is_null($wpdb)){
[583]81                        $wp_prefix = preg_replace('/wordpress/','wp',$mydirname);
[44]82
[719]83                        $module_tbl = $xoopsDB->prefix($wp_prefix).'_options';
84                        $theme_name = '';
[44]85
[46]86                        $sql = "SELECT option_value FROM $module_tbl WHERE option_name LIKE 'template'";
87                        $result =  $xoopsDB->query($sql, 0, 0);
88                        if ($xoopsDB->getRowsNum($result)  > 0){
89                                $row = $xoopsDB->fetchArray($result);
90                                $theme_name = $row['option_value'];
91                        }
92                } else {
93                        $theme_name = get_option('template');
[44]94                }
95                return $theme_name;
96        }
97
[573]98        function xpress_block_header_set($mydirname = '')
[44]99        {
[573]100                require_once( dirname( __FILE__ ).'/xpress_block_header.php' );
101                $xml = xpress_block_header_cache_read($mydirname);
102                $block_header = $xml['block_header'];
103                if (!empty($block_header)){
104                        $tplVars =& $GLOBALS['xoopsTpl']->get_template_vars();
[44]105                        if(array_key_exists('xoops_block_header', $tplVars)) {
[573]106                                if (!strstr($tplVars['xoops_block_header'],$block_header)) {
107                                        $GLOBALS['xoopsTpl']->assign('xoops_block_header',$tplVars['xoops_block_header'].$block_header);
[44]108                                }
109                        } else {
[573]110                                $GLOBALS['xoopsTpl']->assign('xoops_block_header',$block_header);
[44]111                        }
[573]112                }
[44]113        }
114       
115       
[96]116    function xpress_block_cache_found($mydirname,$block_name)
117    {
[757]118        global $modInfo;
[657]119       
[757]120                $cache_dir = $modInfo->get_xoops_cache_path() . '/';
[96]121        $xml_name = $block_name . '.xml';
122
123        $filename = $cache_dir .$mydirname . '_' . $xml_name;
124                $cache_time = 0;
125//        if (file_exists($filename) && ((time() - filemtime($filename)) < $cache_time)) {
126        if (file_exists($filename)) {
127            return true;
128       } else {
129                        return false;
130                }
131    }
132       
[44]133        function xpress_block_render($mydirname,$block_function_name,$options)
134        {
[757]135                global $wpdb,$modInfo,$xoopsUserIsAdmin;
[44]136                $func_file = $block_function_name;
137                $call_theme_function_name = str_replace(".php", "", $block_function_name);
138                $inc_theme_file_name = $call_theme_function_name . '_theme.php';
139                $cache_title = str_replace(".php", "", $block_function_name);
[89]140                $blockID =get_block_id($mydirname,$func_file,$options);         
141
[483]142                $this_block_url = '/' . $mydirname . '/';
[96]143                $call_url = $_SERVER['REQUEST_URI'];
[719]144                $block['err_message'] = '';
145
[506]146                if (strstr($call_url , $this_block_url) !== false && strstr($call_url , $this_block_url . 'admin/') === false){
[44]147                        $block_theme_file = get_block_file_path($mydirname,$inc_theme_file_name);
[448]148                        require_once $block_theme_file['file_path'];
[44]149                        $block = $call_theme_function_name($options);           //The block name and the called function name should be assumed to be the same name.
[448]150                        if (!empty($block_theme_file['error']))
[719]151                                $block['err_message'] .= $block_theme_file['error'];
[44]152                } else {
[719]153                        if (xpress_block_cache_found($mydirname,$cache_title. $blockID)){
154                                $xml = xpress_block_cache_read($mydirname,$cache_title. $blockID);
155                                $block = $xml['block'];
[96]156                        } else {
[719]157                                $block['err_message'] .= sprintf(_MB_XP2_BLOCK_CACHE_ERR, '<a href="' . XOOPS_URL . '/modules/' . $mydirname . '">' . $mydirname .'</a>');
[96]158                        }
[44]159                }
[719]160
161                if(!cache_is_writable()){
162                        $block['err_message']  ='<span style="color:#ff0000">';
163                        $block['err_message'] .= _MB_XP2_CACHE_NOT_WRITABLE ;
164                        if($xoopsUserIsAdmin){
165                                $block['err_message'] .=  " ($cache_dir)";
166                                $block['err_message'] .= '</span>';
167                        }
168                }
[691]169                xpress_block_header_set($mydirname);
[656]170                $block['request_uri'] = $_SERVER['REQUEST_URI'];
[690]171                $temp_option = @explode(':' , $options[1]);
[455]172               
[480]173                if (isset($temp_option[1])) {
[497]174                        $templates_file = $options[1];
[463]175                } else {
[455]176                        $templates_file = 'db:'.$mydirname. '_' . str_replace(".php", ".html", $block_function_name);
[463]177                }
178               
[89]179                $tpl =& new XoopsTpl() ;
[757]180                $tpl->template_dir = $modInfo->get_module_templates_path();
[463]181                if (!$tpl->template_exists($templates_file)){
[757]182                        $src_file_path = $modInfo->get_module_templates_path() . '/' .$mydirname. '_' . str_replace(".php", ".html", $block_function_name);
[472]183                        $templates_file = add_xpress_tpl($mydirname,$templates_file,$src_file_path);
[463]184                }
[89]185                $tpl->assign( 'block' , $block ) ;
186                $ret['content'] = $tpl->fetch( $templates_file ) ;
[656]187                if(preg_match('/\S/',$ret['content'])){
188                        return $ret ;
189                }else {
190                        return null;
191                }
[44]192        }
193       
[463]194        function add_xpress_tpl($mydirname,$templates='',$src_file_path){
[757]195                global $wpdb,$modInfo , $xoops_db;
[463]196               
197                $mid = get_block_mid($mydirname);
198
199                $temp_parm = explode(':' , $templates);
200                if (empty($temp_parm[1])) {
201                        $filename=$temp_parm[0];
202                        $type = 'db';
203                } else  {
204                        $filename=$temp_parm[1];
205                        $type = $temp_parm[0];
206                }
[757]207                $temp_file_path = $modInfo->get_module_templates_path() . '/'. $filename;
[463]208                $pattern = '^' . $mydirname . '_';
209                if (preg_match('/' . $pattern . '/' , $filename, $match)){ // file prefix check
210                        if (!file_exists($temp_file_path)){             // Repetition check
211                                if (file_exists($src_file_path)){       // source file check
212                                        $rcd = copy($src_file_path, $temp_file_path);
213                                }
214                        }
[472]215                        return  'file:' . $filename;
[463]216                }
[472]217                return $templates;
[463]218        }
219       
[172]220        function xpress_block_cache_refresh($mydirname)
[135]221        {
222                global $xoops_db;
223                $mid = get_xpress_modid();
[394]224               
225                // It is a block that needs cache arranged outside the module.
226                // Only the block arranged outside the module is detected here.
227                $newblocks = get_xoops_prefix() . "newblocks";
228                $block_module_link = get_xoops_prefix(). "block_module_link";
229                $sql  = "SELECT * FROM $newblocks LEFT JOIN $block_module_link ON {$newblocks}.bid = {$block_module_link}.block_id ";
230                $sql .= "WHERE {$newblocks}.mid = $mid AND {$newblocks}.visible = 1 AND {$block_module_link}.module_id != $mid ";
231                $sql .= "GROUP BY {$newblocks}.bid";
232
[135]233                $blocks = $xoops_db->get_results($sql);
234                require_once get_xpress_dir_path() . '/include/xpress_block_render.php';
235
236                foreach($blocks as $block){
237                        $func_file = $block->func_file;
238                        $call_theme_function_name = str_replace(".php", "", $func_file);
239                        $inc_theme_file_name = str_replace(".php", "", $func_file) . '_theme.php';
240                        $cache_title = str_replace(".php", "", $func_file);
241                        $blockID = $block->bid;
242                        $options = explode("|", $block->options);
243
244                        $block_theme_file = get_block_file_path($mydirname,$inc_theme_file_name);
[448]245                        require_once $block_theme_file['file_path'];
[135]246                        $render = $call_theme_function_name($options);          //The block name and the called function name should be assumed to be the same name.                   
247                        $render_array['block'] = $render;
248                        $render_array['block']['options'] = $block->options;
[448]249                        if (!empty($block_theme_file['error']))
250                                $render_array['block']['err_message'] = $block_theme_file['error'];
[719]251                        if(cache_is_writable()){
252                                if (xpress_block_cache_found($mydirname,$cache_title. $blockID)){       
253                                        $render_serialize = xpress_XML_serialize($render_array);
254                                        $render_md5 = md5($render_serialize);
[135]255
[719]256                                        $cache_serialize = xpress_cache_read($mydirname,$cache_title. $blockID.'.xml');
257                                        $cache_md5 = md5($cache_serialize);
258                                       
259                                        if ($render_md5 != $cache_md5){
260                                                xpress_block_cache_write($mydirname,$cache_title. $blockID, $render_array);
261                                        }
262                                } else {
[135]263                                        xpress_block_cache_write($mydirname,$cache_title. $blockID, $render_array);
264                                }
265                        }
266                }
267        }
268       
[172]269        function xpress_unnecessary_block_cache_delete($mydirname)
[135]270        {
[757]271                global $xoops_db,$modInfo;
[463]272               
[135]273                $mid = get_xpress_modid();
274                $sql = "SELECT bid,options,func_file FROM " . get_xoops_prefix() . "newblocks WHERE mid = $mid AND visible = 1";
275                $blocks = $xoops_db->get_results($sql);
276                require_once get_xpress_dir_path() . '/include/xpress_block_render.php';
277
[588]278                $pattern =$mydirname . '_block_header';
[135]279                foreach($blocks as $block){
280                        $cache_file_name = $mydirname . '_'. str_replace(".php", "", $block->func_file) . $block->bid;
281                        if (!empty($pattern))  $pattern .= '|';
282                        $pattern .= $cache_file_name;
283                }
284                $pattern = '(' . $pattern . ')';
285               
[757]286                $cache_dir = $modInfo->get_xoops_cache_path() . '/';
[135]287                $cache_time = 0;
288        if ($dh = opendir($cache_dir)) {
289            while (($file = readdir($dh)) !== false) {
[482]290                if (preg_match('/^' . preg_quote($mydirname) . '_/', $file)) {
[135]291                        if(! preg_match('/' . $pattern . '/', $file)) {
292                        unlink($cache_dir.$file);
293                    }
294                }
295            }
296            closedir($dh);
297        }
298    }
[387]299   
300    function get_xpress_mod_config($mydirname,$conf_name=''){
[749]301        global $xoopsDB;
[387]302                $module_handler =& xoops_gethandler('module');
303                $xoopsModule =& $module_handler->getByDirname($mydirname);
304                $mid = $xoopsModule->getVar('mid');
305                $db_config = $xoopsDB->prefix('config');
306           
307                $wu_sql  =      "SELECT conf_value FROM  $db_config ";
308                $wu_sql .=      "WHERE (conf_modid = $mid ) AND (conf_name LIKE '$conf_name')";
309                $wu_res = $xoopsDB->queryF($wu_sql, 0, 0);
310                       
311                if ($wu_res === false){
312                        return 0;
313                } else {
314                        $xu_row = $xoopsDB->fetchArray($wu_res);
315                        return $xu_row['conf_value'];
316                }
[463]317        }       
[172]318}       
[44]319?>
Note: See TracBrowser for help on using the repository browser.