File: fastIce.0.5.3.php - Tab length: 1 2 4 8 - Lines: on off - No wrap: on off

001: <?php
002: /* *** ** * fastIce Framework.
003: ** *
004: *
005:     fastIce alpha 0.5.3 � 2010~2011 noferi Micka�l/m2m - noferov@gmail.com - Some Rights Reserved.
006: 
007:     Except where otherwise noted, this work is licensed under a Creative Commons Attribution 3.0 License, CC-by-nc-sa
008:     terms of licence CC-by-nc-sa are readable at : http://creativecommons.org/licenses/by-nc-sa/3.0/
009: *
010: ** *
011: * ** *** */
012: 
013: // global config .......................................
014: 
015: define ('template','template');
016: 
017: define('redisServer','127.0.0.1');
018: define('redisPrefix','fastice');
019: 
020: define('site_url','/');
021: define('site_full_url','http:yoursite.com/');
022: define('mail_domain','yoursite.com');
023: 
024: define('defaultTitle','the default title');
025: define('defaultKeywords','my, keywords');
026: define('defaultDescription','a nice description');
027: define('defaultMeta','');
028: 
029: define('defaultLangage','en');
030: setlocale (LC_ALL, 'fr_FR.utf8','fra');
031: 
032: ini_set('display_errors', 1);
033: 
034: /* ********************* **/
035: 
036: define ('noFollowKeyWord','[$$]');
037: 
038: global $nofollow,$nodesigncache,$norendercache,$renderInclude,$redis,$global_current_file,$fnc,$designPath,$commonDesignPath,$currentDesign,$currentLangage,$urlpath;
039: $redis = new Redis(); if(!$redis->connect(redisServer)) die('cannot rape the database ...');
040: 
041: if(isset($_GET['deleteCache']))
042: {   $cache = $redis->keys(redisPrefix.':designCache:*');
043:     foreach($cache as $c) $redis->delete($c);
044: }
045: 
046: function setInfo($langage,$upath)
047: {   global $currentLangage,$canonicalurl,$urlpath;$currentLangage=$langage;$urlpath=$upath;
048:     if($langage!=defaultLangage)
049:     {   $canonicalurl=site_full_url.$langage.'/';
050:         if($upath != '') $canonicalurl.=$upath.'/';
051:     }
052:     else
053:     { $canonicalurl=site_full_url;
054:       if($upath != '') $canonicalurl.=$upath.'/';
055:     }
056: }
057: 
058: function getlang(){global $currentLangage;return $currentLangage;}
059: function getUrlPath(){global $urlpath;return $urlpath;}
060: 
061: define ('site_full_path',dirname(__FILE__));
062: define ('site_path',site_full_path.'/files');
063: define ('common_path','common');
064: define ('module_path','plugins');
065: define ('design_path','constants.ini');
066: 
067: $nofollow=0;$nodesigncache=0;$norendercache=0;$renderInclude=array();$global_current_file='';$fnc=array();$designPath='';$commonDesignPath='';
068: /* additionnal keywords for render */
069: function addToRender($word,$txt) { global $renderInclude,$currentDesign; $renderInclude[$word] .= $txt; setDesignCache($currentDesign.'/'.$word,$txt); }
070: function setRender($word,$txt) { global $renderInclude,$currentDesign; $renderInclude[$word] = $txt; setDesignCache($currentDesign.'/'.$word,$txt); }
071: function extendRenderWord($word) { if(!isset($renderWords[$word])) array_push($renderWords,$word); }
072: global $renderWords; $renderWords = array('head','js','jquery','title','meta','style','keywords','description');
073: 
074: function addToHead($t){addToRender('head',$t);}function setHead($t){setRender('head',$t);}$renderInclude['head']='';function addToJs($t){addToRender('js',$t);}function setJs($t){setRender('js',$t);}$renderInclude['js']='';function addToJquery($t){addToRender('jquery',$t);}function setJquery($t){setRender('jquery',$t);}$renderInclude['jquery']='';function addToTitle($t){addToRender('title',$t);}function setTitle($t){setRender('title',$t);}$renderInclude['title']='';function addToMeta($t){addToRender('meta',$t);}function setMeta($t){setRender('meta',$t);}$renderInclude['meta']='';function addToStyle($t){addToRender('style',$t);}function setStyle($t){setRender('style',$t);}$renderInclude['style']='';function addToKeywords($t){addToRender('keywords',$t);}function setKeywords($t){setRender('keywords',$t);}$renderInclude['keywords']='';function addToDescription($t){addToRender('description',$t);}function setDescription($t){setRender('description',$t);}$renderInclude['description']='';
075: //foreach($renderWords as $word){ print('function addTo'.ucfirst($word).'($t){addToRender(\''.$word.'\',$t);}function set'.ucfirst($word).'($t){setRender(\''.$word.'\',$t);}$renderInclude[\''.$word.'\']=\'\';'); } die();
076: 
077: function renderPage($url,$page)
078: {   global $renderInclude, $design_cache, $canonicalurl,$currentLangage;
079:     if(empty($renderInclude['title'])) $renderInclude['title'] = $design_cache['ini:title'];
080:     if(empty($renderInclude['keywords'])) $renderInclude['keywords'] = $design_cache['ini:keywords'];
081:     if(empty($renderInclude['description'])) $renderInclude['description'] = $design_cache['ini:description'];
082:     if(empty($renderInclude['meta'])) $renderInclude['meta'] = $design_cache['ini:meta'];
083:     $renderInclude['meta'] .= '<link rel="canonical" href="'.$canonicalurl.$url.'" /><meta name="generator" content="fastIce" />';
084:     return str_replace(array('[head]','[js]','[jquery]','[meta]','[style]','[keywords]','[description]','[title]','[url]','[lang]'),array($renderInclude['head'],$renderInclude['js'],$renderInclude['jquery'],$renderInclude['meta'],$renderInclude['style'],$renderInclude['keywords'],$renderInclude['description'],$renderInclude['title'],site_url,$currentLangage),$page);
085: }
086: 
087: function get_include_contents($filename)
088: {   if(is_file($filename))
089:     {   ob_start(); include ($filename); $contents = ob_get_contents(); ob_end_clean(); return $contents;
090:     } return false;
091: }
092: 
093: function showPage($key,$seed=1)
094: {   global $fnc,$global_current_file,$need_fix_name,$designPath,$commonDesignPath,$redis;
095: 
096:     if($seed){ $designPath=$key; $commonDesignPath=''; }
097: 
098:     $dpath = $designPath; $cdpath = $commonDesignPath;
099: 
100:     $out = '';
101:     if($seed)
102:     {   global $seedPath,$seedKey,$design_cache,$currentLangage,$urlpath; $seedPath=$urlpath.'/'.$key; $seedKey=$key;
103: 
104:         $design_cache = $redis->hgetall(redisPrefix.':designCache:'.$currentLangage.':'.$seedPath);
105: 
106:         if(!isset($design_cache['ini:loaded'])) // redis page info not set
107:         {   //print '<br>loading ini';
108:             $page_opt = array('loaded'=>1);
109:             $path = template.'/'.$key.'/'.$key.'.ini';
110:             if(is_file($path)) $page_opt = array_merge(parse_ini_file($path),$page_opt);
111:             $outarray = array(); foreach($page_opt as $name=>$value) $outarray['ini:'.$name] = $value;
112:             $design_cache = array_merge($design_cache,$outarray);
113:             $redis->hMset(redisPrefix.':designCache:'.$currentLangage.':'.$seedPath,$outarray);
114:         }
115: 
116:         if(!isset($design_cache['ini:skeleton'])) $design_cache['ini:skeleton'] = 'normal';
117:         if(!isset($design_cache['ini:title'])) $design_cache['ini:title'] = defaultTitle;
118:         if(!isset($design_cache['ini:keywords'])) $design_cache['ini:keywords'] = defaultKeywords;
119:         if(!isset($design_cache['ini:description'])) $design_cache['ini:description'] = defaultDescription;
120:         if(!isset($design_cache['ini:meta'])) $design_cache['ini:meta'] = defaultMeta;
121: 
122:         if(!isset($design_cache['ini:sk']))
123:         {   //print '<br>loading skeleton';
124:             $path = template.'/'.common_path.'/skeleton/'.$design_cache['ini:skeleton'].'.html';
125:             if(is_file($path))
126:             {   $out = file_get_contents($path); $redis->hset(redisPrefix.':designCache:'.$currentLangage.':'.$seedKey,'ini:sk',$out);
127:             } else $out = '<h3>page skeleton not found !</h3><br><b>'.$path.'</b>';
128:         } else $out = $design_cache['ini:sk'];
129: 
130:     } else {
131:         $global_current_file = $key;
132:         $out = getDesign($key);
133:         $designPath .= '/'.$key; $commonDesignPath .= '/'.$key;
134:     }
135: 
136:     global $renderInclude;
137:     // global $renderWords,$renderInclude; foreach($renderWords as $w) print '$d=getDesignCache(\''.$w.'\');if($d!==false){if(!isset($renderInclude[\''.$w.'\']))$renderInclude[\''.$w.'\']=$d;else $renderInclude[\''.$w.'\'].=$d;}';die();
138:     $d=getDesignCache('head');if($d!==false){if(!isset($renderInclude['head']))$renderInclude['head']=$d;else $renderInclude['head'].=$d;}$d=getDesignCache('js');if($d!==false){if(!isset($renderInclude['js']))$renderInclude['js']=$d;else $renderInclude['js'].=$d;}$d=getDesignCache('jquery');if($d!==false){if(!isset($renderInclude['jquery']))$renderInclude['jquery']=$d;else $renderInclude['jquery'].=$d;}$d=getDesignCache('title');if($d!==false){if(!isset($renderInclude['title']))$renderInclude['title']=$d;else $renderInclude['title'].=$d;}$d=getDesignCache('meta');if($d!==false){if(!isset($renderInclude['meta']))$renderInclude['meta']=$d;else $renderInclude['meta'].=$d;}$d=getDesignCache('style');if($d!==false){if(!isset($renderInclude['style']))$renderInclude['style']=$d;else $renderInclude['style'].=$d;}$d=getDesignCache('keywords');if($d!==false){if(!isset($renderInclude['keywords']))$renderInclude['keywords']=$d;else $renderInclude['keywords'].=$d;}$d=getDesignCache('description');if($d!==false){if(!isset($renderInclude['description']))$renderInclude['description']=$d;else $renderInclude['description'].=$d;}
139: 
140:     //if($out && $out!='') $out = preg_replace_callback(includePattern,'showPage',$out); else $out = '';
141:     if($out && $out!='')
142:     {   $offset=0;
143:         for(;;)
144:         {   $start = strpos($out,'�',$offset);
145:             if($start === false) break;
146:             $off = $start+2;
147:             $end = strpos($out,'�',$off);
148:             if($end === false) break;
149:             $size = $end-$off;
150:             $word = substr($out,$off,$size);
151:             $out = substr_replace($out,showPage($word,0),$start,$size+4);
152:             $offset = $start;
153:         }
154:     } else $out = '';
155: 
156:     if($seed && $need_fix_name) $out = str_replace(noFollowKeyWord,'�', $out);
157:     $designPath = $dpath; $commonDesignPath = $cdpath;
158: 
159:     return $out;
160: }
161: 
162: function getArgs($string,$word,$separator)
163: {   $wordSize = strlen($word);
164:     if(substr($string,0,$wordSize) == $word)
165:     {   $args = substr($string,$wordSize);
166:         $args = explode($separator,$args);
167:         array_unshift($args,count($args));
168:         return $args;
169:     }   return array(0);
170: }
171: 
172: global $design_cache;
173: function noDesignCache(){global $nodesigncache;$nodesigncache=1;}
174: function setDesignCache($design,$content)
175: {   global $nodesigncache,$seedPath,$designPath,$currentLangage,$redis;
176:     if(!$nodesigncache)
177:         $redis->hset(redisPrefix.':designCache:'.$currentLangage.':'.$seedPath,$designPath.'/'.$design,$content);
178: }
179: 
180: function getDesignCache($design)
181: {   global $design_cache,$designPath,$redis;
182:     $k = $designPath.'/'.$design; if(isset($design_cache[$k])) return $design_cache[$k];
183:     return false;
184: }
185: 
186: function getDesign($design)
187: {   if($design == '') return ''; $d = getDesignCache($design); if($d !== false) return $d;
188:     global $nofollow,$last_design,$need_fix_name,$redis,$nodesigncache,$currentDesign; $nodesigncache=0; $currentDesign=$design;
189:     if($design == 'nofolow'){ $nofollow=1; $need_fix_name=1; return ''; }
190:     if($design == 'folow'){   $nofollow=0; return ''; }
191:     if($nofollow) { return noFollowKeyWord.$design.noFollowKeyWord; }
192:     global $designPath,$commonDesignPath,$currentLangage;
193: 
194:     if(false === strstr($design,'|'))
195:     {
196:         // search design in the template folder, absolute path with lang prefix
197:         $path = template.'/'.$designPath.'/'.$currentLangage.'.'.$design.'.php'; // template folder
198:         if(file_exists($path)) { $d=get_include_contents($path);if($d!==false){setDesignCache($design,$d);return $d;}}
199: 
200:         // search design in the template folder, absolute path
201:         $path = template.'/'.$designPath.'/'.$design.'.php'; // template folder
202:         if(file_exists($path)) { $d=get_include_contents($path);if($d!==false){setDesignCache($design,$d);return $d;}}
203: 
204:         // search design in the common template folder, absolute path with lang prefix
205:         $path = template.'/'.common_path.$commonDesignPath.'/'.$currentLangage.'.'.$design.'.php';
206:         if(file_exists($path)) { $d=get_include_contents($path);if($d!==false){setDesignCache($design,$d);return $d;}}
207: 
208:         // search design in the common template folder, absolute path
209:         $path = template.'/'.common_path.$commonDesignPath.'/'.$design.'.php';
210:         if(file_exists($path)) { $d=get_include_contents($path);if($d!==false){setDesignCache($design,$d);return $d;}}
211: 
212:         // search design in the common folder, absolute path with lang prefix
213:         $path = common_path.$commonDesignPath.'/'.$currentLangage.'.'.$design.'.php';
214:         if(file_exists($path)) { $d=get_include_contents($path);if($d!==false){setDesignCache($design,$d);return $d;}}
215: 
216:         // search design in the common folder, absolute path
217:         $path = common_path.$commonDesignPath.'/'.$design.'.php';
218:         if(file_exists($path)) { $d=get_include_contents($path);if($d!==false){setDesignCache($design,$d);return $d;}}
219: 
220:         // search design in the common template folder, just file, no path, lang prefix
221:         $path = template.'/'.common_path.'/'.$currentLangage.'.'.$design.'.php';
222:         if(file_exists($path)) { $d=get_include_contents($path);if($d!==false){setDesignCache($design,$d);return $d;}}
223: 
224:         // search design in the common template folder, just file, no path
225:         $path = template.'/'.common_path.'/'.$design.'.php';
226:         if(file_exists($path)) { $d=get_include_contents($path);if($d!==false){setDesignCache($design,$d);return $d;}}
227: 
228:         // search design in the common folder, just file, no path, lang prefix
229:         $path = common_path.'/'.$currentLangage.'.'.$design.'.php';
230:         if(file_exists($path)) { $d=get_include_contents($path);if($d!==false){setDesignCache($design,$d);return $d;}}
231: 
232:         // search design in the common folder, just file, no path
233:         $path = common_path.'/'.$design.'.php';
234:         if(file_exists($path)) { $d=get_include_contents($path);if($d!==false){setDesignCache($design,$d);return $d;}}
235: 
236:         // search design in constant files
237:         global $global_constants,$seedPath,$seedKey;
238:         if(!isset($global_constants))
239:         {   //$global_constants = $redis->hgetall(redisPrefix.':designCache:'.$currentLangage.':'.$seedPath.':constant');
240:             //if(empty($global_constants))
241:             {   $global_constants = array();
242:                 //print '<br>loading constants';
243:                 // common constants
244:                 $path = template.'/'.common_path.'/'.design_path;
245:                 if(is_file($path)) $constants = parse_ini_file($path,true);
246: 
247:                 //print '<pre>';print_r($constants);print '</pre>';
248: 
249:                 // page specific constants
250:                 $path = template.'/'.$seedKey.'/'.design_path;
251:                 if(is_file($path)) $constants = array_merge_recursive($constants,parse_ini_file($path,true));
252:                 if(isset($constants))
253:                 {   //print '<pre>';print_r($constants);print '</pre>';
254:                     foreach($constants as $name=>$sub)
255:                     {   if($name == $currentLangage || $name == 'common')
256:                             foreach($sub as $cnt=>$val) $global_constants[$cnt] = $val;
257:                     }
258:                 }
259:                 //$redis->hmset(redisPrefix.':designCache:'.$currentLangage.':'.$seedPath.':constant',$global_constants);
260:                 //print '<pre>';print_r($global_constants);print '</pre>';
261:             }
262:         }
263:         if(isset($global_constants[$design])){$d=$global_constants[$design];setDesignCache($design,$d);return $d;}
264:     }
265:     else
266:     {   $mdl = explode('|',$design);
267:         if(isset($mdl[0]))
268:         {   $mdl = $mdl[0];
269:             $mdlargs = getArgs($design,$mdl.'|','|');
270:             if($mdlargs[0] > 0)
271:             {   $fn = 'fn_'.$mdl;
272:                 ob_start();
273:                 if(function_exists($fn)) $fn($mdlargs);
274:                 else    {   $path = module_path.'/'.$mdl.'/'.$mdl.'.php';
275:                         if(is_file($path))
276:                         {   //print '<b> load module '.$mdl.' </b>';
277:                             include($path); $fn($mdlargs);
278:                         }// else { print '<br>module not found or invalid : <b>'.$mdl.'</b><br>args : '; print_r($mdlargs); }
279:                     }
280:                 $out = ob_get_contents(); ob_end_clean();
281:                 setDesignCache($design,$out); return $out;
282:             }
283:         }
284:     }
285: 
286:     return '<span style="color:red;">'.$design.' not found!</span>';
287: }
288: 
289: function fillDesign($data, $design, $callback=false)
290: {   $design = getDesign($design);
291:     $keywords = array();
292: 
293:     $offset=0; // search all keywords in the design
294:     for(;;)
295:     {   $start = strpos($design,'$',$offset);
296:         if($start === false) break;
297:         $off = $start+1;
298:         $end = strpos($design,'$',$off);
299:         if($end === false) break;
300:         $size = $end-$off;
301:         $word = substr($design,$off,$size);
302:         $keywords[] = $word;
303:         $offset = $end+1;
304:     }//print '<pre>';print_r($keywords);print '</pre>';
305: 
306:     $out = '';
307:     if($callback !== false)
308:     {   $n=0;
309:         if(is_array($callback))
310:         {   foreach($data as $dta)
311:             {   $d = $design;
312:                 foreach($keywords as $word)
313:                 {   if(isset($callback[$word])) // specific word callback exist !
314:                     { if(isset($dta[$word])) $d = str_replace('$'.$word.'$', $callback[$word]($dta[$word],$n), $d); // data contain this word, callback receive the word
315:                       else $d = str_replace('$'.$word.'$', $callback[$word]($dta,$n), $d); // data not get the word, callback receive all the data
316:                     } else $d = str_replace('$'.$word.'$','', $d); // else, fill blank
317:                 }
318:                 $out .= $d; $n++;
319:             }
320:         }
321:           else
322:         {   // callback is a generic function, who get word and complete data
323:             foreach($data as $dta)
324:             {   $d = $design;
325:                 foreach($keywords as $word) $d = str_replace('$'.$word.'$', $callback($word,$dta,$n++), $d);
326:                 $out .= $d;
327:             }
328:         }
329:     }
330:       else
331:     {   foreach($data as $dta)
332:         {   $d = $design;
333:             foreach($keywords as $word => $size)
334:             {   // no callback, just replace specific word with data, or fill blank if word data not set
335:                 if(isset($dta[$word])) $d = str_replace('$'.$word.'$', $dta[$word]); else $d = str_replace('$'.$word.'$','', $d);
336:             }
337:             $out .= $d;
338:         }
339:     }
340:     return $out;
341: }
342: 
343: function lredisHashFill($listkey, $design, $callback=false, $kprefix='', $ksuffix='', $start=0, $end=-1, &$size=0)
344: {   global $redis;
345:     list($keys,$size) = $redis->multi(Redis::PIPELINE)->lRange($listkey,$start,$end)->lSize($listkey)->exec();
346:     $redis->multi(Redis::PIPELINE); foreach($keys as $k) $redis->hgetall($kprefix.$k.$ksuffix); $data = $redis->exec();
347:     //print '<pre>';print_r($data);print '</pre>';
348:     return fillDesign($data,$design,$callback);
349: }
350: 
351: function sredisHashFill($setkey, $design, $callback=false, $kprefix='', $ksuffix='')
352: {   global $redis; $keys = $redis->smembers($setkey); $redis->multi(Redis::PIPELINE); foreach($keys as $k) $redis->hgetall($kprefix.$k.$ksuffix); $data = $redis->exec();
353:     //print '<pre>';print_r($data);print '</pre>';
354:     return fillDesign($data,$design,$callback);
355: }
356: 
357: function redisHashFill($hashkey, $design, $callback=false)
358: {   global $redis;
359:     return fillDesign(array($redis->hgetall($hashkey)), $design, $callback);
360: }
361: 
362: function isUserPrivilege($prv)
363: { return ( isset($_SESSION['user']) && (( isset($_SESSION['user']['right:all']) && !isset($_SESSION['user']['right:'.$prv]) ) || ( isset($_SESSION['user']['right:'.$prv]) &&  $_SESSION['user']['right:'.$prv] )));
364: }
365: 
366: ?>