XPressME Integration Kit

Trac

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

Last change on this file since 448 was 448, checked in by toemon, 14 years ago

ブロックのバージョン管理実装 Fixes #246

File size: 10.7 KB
Line 
1<?php
2if(!defined('XPRESS_BLOCK_RENDER_FUNCTION_READ')){
3        define('XPRESS_BLOCK_RENDER_FUNCTION_READ',1);
4        require_once dirname( __FILE__ ) .'/xml.php' ;
5        require_once dirname( __FILE__ ) .'/xpress_cache.php' ;
6        global $xoops_config;
7       
8        if (!is_object($xoops_config)){ // is call other modules
9                require_once dirname(dirname( __FILE__ )) .'/class/config_from_xoops.class.php' ;
10                $xoops_config = new ConfigFromXoops;
11        }
12       
13        function xpress_block_cache_write($mydirname,$block_name,$block)
14        {
15                        $xml = xpress_XML_serialize($block);
16                        $xml_name = $block_name . '.xml';
17                        if (WPLANG == 'ja_EUC'){
18                                $xml = str_replace('<?xml version="1.0" ?>', '<?xml version="1.0" encoding="EUC-JP" ?>' , $xml);
19                        }
20                        xpress_cache_write($mydirname,$xml_name,$xml);
21        }
22        function xpress_block_cache_read($mydirname,$block_name)
23        {
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                        $ret = @xpress_XML_unserialize($xml_data);
30                        if (strstr($xml_data, '<?xml version="1.0" encoding="EUC-JP" ?>') !== false){
31                                $ans = mb_convert_variables('EUC-JP' , 'UTF-8', &$ret);
32                        }
33                        return $ret;
34        }
35       
36        function get_block_id($mydirname,$func_file,$options)
37        {
38                $options_string = '';
39                foreach ($options as $val){
40                        if (!empty($options_string)) $options_string .='|';
41                        $options_string .= $val;
42                }
43                        $xoopsDB =& Database::getInstance();
44                        $block_tbl = $xoopsDB->prefix('newblocks');     
45                        $module_dir = XOOPS_ROOT_PATH . '/modules/' . $mydirname;
46
47                        $sql = "SELECT bid FROM $block_tbl WHERE (func_file LIKE '$func_file') AND (options LIKE '$options_string')";
48                        $result =  $xoopsDB->query($sql, 0, 0);
49                        if ($xoopsDB->getRowsNum($result)  > 0){
50                                $row = $xoopsDB->fetchArray($result);
51                                $block_id = $row['bid'];
52                        }
53                        return $block_id;
54        }
55
56        function get_xpress_theme_name($mydirname)
57        {
58                global $wpdb;
59               
60                if (is_null($wpdb)){
61                        $xoopsDB =& Database::getInstance();
62                        $wp_prefix = $mydirname;
63                        if ($wp_prefix == 'wordpress') $wp_prefix = 'wp';
64
65                        $module_tbl = $xoopsDB->prefix($wp_prefix).'_options'; 
66                        $theme_name = '';       
67
68                        $sql = "SELECT option_value FROM $module_tbl WHERE option_name LIKE 'template'";
69                        $result =  $xoopsDB->query($sql, 0, 0);
70                        if ($xoopsDB->getRowsNum($result)  > 0){
71                                $row = $xoopsDB->fetchArray($result);
72                                $theme_name = $row['option_value'];
73                        }
74                } else {
75                        $theme_name = get_option('template');
76                }
77                return $theme_name;
78        }
79
80        function get_block_stylesheet_url($mydirname)
81        {
82                global $xoops_config;
83                $mydirpath = $xoops_config->xoops_root_path . '/modules/' . $mydirname;
84                $select_theme = get_xpress_theme_name($mydirname);
85                $style_file = $mydirpath . '/wp-content/themes/' . $select_theme . '/blocks/block_style.css';
86                if (file_exists($style_file))
87                        return $xoops_config->xoops_url . '/modules/' .$mydirname . '/wp-content/themes/' . $select_theme . '/blocks/block_style.css';
88                else   
89                        return $xoops_config->xoops_url . '/modules/' .$mydirname . '/wp-content/themes/xpress_default/blocks/block_style.css';
90        }
91       
92        function get_stylesheet_url($mydirname)
93        {
94                global $xoops_config;
95                $mydirpath = $xoops_config->xoops_root_path . '/modules/' . $mydirname;
96                $select_theme = get_xpress_theme_name($mydirname);
97                $style_file = $mydirpath . '/wp-content/themes/' . $select_theme . '/style.css';
98                if (file_exists($style_file))
99                        return $xoops_config->xoops_url . '/modules/' .$mydirname . '/wp-content/themes/' . $select_theme . '/style.css';
100                else   
101                        return $xoops_config->xoops_url . '/modules/' .$mydirname . '/wp-content/themes/xpress_default/style.css';
102        }
103       
104        function xpress_get_plugin_css($mydirname = '')
105        {
106                $this_url = '/modules/'. $mydirname;
107                $call_url = $_SERVER['REQUEST_URI'];
108                if (!strstr($call_url,$this_url)){                     
109                        global $xoops_config;
110                       
111                        // Module Main CSS
112                        $output = "\n".'<style type="text/css">@import url('.get_stylesheet_url($mydirname).');</style>';
113                       
114                        // hotDate
115                        $output .= "\n".'<style type="text/css">@import url('.$xoops_config->module_url.'/wp-content/plugins/hotDates/hotDates.css);</style>';
116                        return $output;
117                }
118                return '';
119        }
120
121        function xpress_block_css_set($mydirname = '')
122        {
123                $style_url =  get_block_stylesheet_url($mydirname);
124               
125                $tplVars =& $GLOBALS['xoopsTpl']->get_template_vars();
126                $csslink = "\n".'<link rel="stylesheet" type="text/css" media="screen" href="'. $style_url .'" />';
127                // Module Main CSS Plugin Css
128                $csslink .= xpress_get_plugin_css($mydirname);
129
130                        if(array_key_exists('xoops_block_header', $tplVars)) {
131                                if (!strstr($tplVars['xoops_block_header'],$csslink)) {
132                                        $GLOBALS['xoopsTpl']->assign('xoops_block_header',$tplVars['xoops_block_header'].$csslink);
133                                }
134                        } else {
135                                $GLOBALS['xoopsTpl']->assign('xoops_block_header',$csslink);
136                        }
137        }
138       
139       
140    function xpress_block_cache_found($mydirname,$block_name)
141    {
142        global $xoops_config;
143        if(defined('XOOPS_ROOT_PATH')){
144                $cache_dir = XOOPS_ROOT_PATH . '/cache/';
145        } else {
146                $cache_dir = $xoops_config->xoops_root_path . '/cache/';
147        }
148        $xml_name = $block_name . '.xml';
149
150        $filename = $cache_dir .$mydirname . '_' . $xml_name;
151                $cache_time = 0;
152//        if (file_exists($filename) && ((time() - filemtime($filename)) < $cache_time)) {
153        if (file_exists($filename)) {
154            return true;
155       } else {
156                        return false;
157                }
158    }
159       
160        function xpress_block_render($mydirname,$block_function_name,$options)
161        {
162                global $wpdb;
163                $func_file = $block_function_name;
164                $call_theme_function_name = str_replace(".php", "", $block_function_name);
165                $inc_theme_file_name = $call_theme_function_name . '_theme.php';
166                $cache_title = str_replace(".php", "", $block_function_name);
167                $blockID =get_block_id($mydirname,$func_file,$options);         
168
169                $this_url = '/modules/'. $mydirname;
170                $call_url = $_SERVER['REQUEST_URI'];
171               
172                xpress_block_css_set($mydirname);
173                if (strstr($call_url,$this_url)){                       
174                        $block_theme_file = get_block_file_path($mydirname,$inc_theme_file_name);
175                        require_once $block_theme_file['file_path'];
176                        $block = $call_theme_function_name($options);           //The block name and the called function name should be assumed to be the same name.
177                        if (!empty($block_theme_file['error']))
178                                $block['err_message'] = $block_theme_file['error'];
179                } else {
180                        if (xpress_block_cache_found($mydirname,$cache_title. $blockID)){
181                                $xml = xpress_block_cache_read($mydirname,$cache_title. $blockID);
182                                $block = $xml['block'];
183                        } else {
184                                $block['err_message'] = sprintf(_MB_XP2_BLOCK_CACHE_ERR, '<a href="' . XOOPS_URL . '/modules/' . $mydirname . '">' . $mydirname .'</a>');
185                        }
186                }
187
188                $templates_file = 'db:'.$mydirname. '_' . str_replace(".php", ".html", $block_function_name);
189                $tpl =& new XoopsTpl() ;
190                $tpl->assign( 'block' , $block ) ;
191                $ret['content'] = $tpl->fetch( $templates_file ) ;
192                return $ret ;
193        }
194       
195        function xpress_block_cache_refresh($mydirname)
196        {
197                global $xoops_db;
198                $mid = get_xpress_modid();
199               
200                // It is a block that needs cache arranged outside the module.
201                // Only the block arranged outside the module is detected here.
202                $newblocks = get_xoops_prefix() . "newblocks";
203                $block_module_link = get_xoops_prefix(). "block_module_link";
204                $sql  = "SELECT * FROM $newblocks LEFT JOIN $block_module_link ON {$newblocks}.bid = {$block_module_link}.block_id ";
205                $sql .= "WHERE {$newblocks}.mid = $mid AND {$newblocks}.visible = 1 AND {$block_module_link}.module_id != $mid ";
206                $sql .= "GROUP BY {$newblocks}.bid";
207
208                $blocks = $xoops_db->get_results($sql);
209                require_once get_xpress_dir_path() . '/include/xpress_block_render.php';
210
211                foreach($blocks as $block){
212                        $func_file = $block->func_file;
213                        $call_theme_function_name = str_replace(".php", "", $func_file);
214                        $inc_theme_file_name = str_replace(".php", "", $func_file) . '_theme.php';
215                        $cache_title = str_replace(".php", "", $func_file);
216                        $blockID = $block->bid;
217                        $options = explode("|", $block->options);
218
219                        $block_theme_file = get_block_file_path($mydirname,$inc_theme_file_name);
220                        require_once $block_theme_file['file_path'];
221                        $render = $call_theme_function_name($options);          //The block name and the called function name should be assumed to be the same name.                   
222                        $render_array['block'] = $render;
223                        $render_array['block']['options'] = $block->options;
224                        if (!empty($block_theme_file['error']))
225                                $render_array['block']['err_message'] = $block_theme_file['error'];
226                        if (xpress_block_cache_found($mydirname,$cache_title. $blockID)){       
227                                $render_serialize = xpress_XML_serialize($render_array);
228                                $render_md5 = md5($render_serialize);
229
230                                $cache_serialize = xpress_cache_read($mydirname,$cache_title. $blockID.'.xml');
231                                $cache_md5 = md5($cache_serialize);
232                               
233                                if ($render_md5 != $cache_md5){
234                                        xpress_block_cache_write($mydirname,$cache_title. $blockID, $render_array);
235                                }
236                        } else {
237                                xpress_block_cache_write($mydirname,$cache_title. $blockID, $render_array);
238                        }
239                }
240        }
241       
242        function xpress_unnecessary_block_cache_delete($mydirname)
243        {
244                global $xoops_db,$xoops_config;
245                $mid = get_xpress_modid();
246                $sql = "SELECT bid,options,func_file FROM " . get_xoops_prefix() . "newblocks WHERE mid = $mid AND visible = 1";
247                $blocks = $xoops_db->get_results($sql);
248                require_once get_xpress_dir_path() . '/include/xpress_block_render.php';
249
250                $pattern ='';
251                foreach($blocks as $block){
252                        $cache_file_name = $mydirname . '_'. str_replace(".php", "", $block->func_file) . $block->bid;
253                        if (!empty($pattern))  $pattern .= '|';
254                        $pattern .= $cache_file_name;
255                }
256                $pattern = '(' . $pattern . ')';
257               
258                $cache_dir = $xoops_config->xoops_root_path . '/cache/';
259                $cache_time = 0;
260        if ($dh = opendir($cache_dir)) {
261            while (($file = readdir($dh)) !== false) {
262                if (preg_match('/^' . preg_quote($mydirname) . '/', $file)) {
263                        if(! preg_match('/' . $pattern . '/', $file)) {
264                        unlink($cache_dir.$file);
265                    }
266                }
267            }
268            closedir($dh);
269        }
270    }
271   
272    function get_xpress_mod_config($mydirname,$conf_name=''){
273                $module_handler =& xoops_gethandler('module');
274                $xoopsModule =& $module_handler->getByDirname($mydirname);
275                $mid = $xoopsModule->getVar('mid');
276                $xoopsDB =& Database::getInstance();
277                $db_config = $xoopsDB->prefix('config');
278           
279                $wu_sql  =      "SELECT conf_value FROM  $db_config ";
280                $wu_sql .=      "WHERE (conf_modid = $mid ) AND (conf_name LIKE '$conf_name')";
281                $wu_res = $xoopsDB->queryF($wu_sql, 0, 0);
282                       
283                if ($wu_res === false){
284                        return 0;
285                } else {
286                        $xu_row = $xoopsDB->fetchArray($wu_res);
287                        return $xu_row['conf_value'];
288                }
289        }
290       
291}       
292?>
Note: See TracBrowser for help on using the repository browser.