Dotclear

source: inc/public/lib.tpl.context.php @ 270:48858be15bda

Revision 270:48858be15bda, 9.5 KB checked in by Franck <carnet.franck.paul@…>, 14 years ago (diff)

Changement d'année

Line 
1<?php
2# -- BEGIN LICENSE BLOCK ---------------------------------------
3#
4# This file is part of Dotclear 2.
5#
6# Copyright (c) 2003-2011 Olivier Meunier & Association Dotclear
7# Licensed under the GPL version 2.0 license.
8# See LICENSE file or
9# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
10#
11# -- END LICENSE BLOCK -----------------------------------------
12if (!defined('DC_RC_PATH')) { return; }
13
14class context
15{
16     public $stack = array();
17     
18     public function __set($name,$var)
19     {
20          if ($var === null) {
21               $this->pop($name);
22          } else {
23               $this->stack[$name][] =& $var;
24               if ($var instanceof record) {
25                    $this->stack['cur_loop'][] =& $var;
26               }
27          }
28     }
29     
30     public function __get($name)
31     {
32          if (!isset($this->stack[$name])) {
33               return null;
34          }
35         
36          $n = count($this->stack[$name]);
37          if ($n > 0) {
38               return $this->stack[$name][($n-1)];
39          }
40         
41          return null;
42     }
43     
44     public function exists($name)
45     {
46          return isset($this->stack[$name][0]);
47     }
48     
49     public function pop($name)
50     {
51          if (isset($this->stack[$name])) {
52               $v = array_pop($this->stack[$name]);
53               if ($v instanceof record) {
54                    array_pop($this->stack['cur_loop']);
55               }
56               unset($v);
57          }
58     }
59     
60     # Loop position tests
61     public function loopPosition($start,$length=null,$even=null)
62     {
63          if (!$this->cur_loop) {
64               return false;
65          }
66         
67          $index = $this->cur_loop->index();
68          $size = $this->cur_loop->count();
69         
70          $test = false;
71          if ($start >= 0)
72          {
73               $test = $index >= $start;
74               if ($length !== null) {
75                    if ($length >= 0) {
76                         $test = $test && $index < $start + $length;
77                    } else {
78                         $test = $test && $index < $size + $length;
79                    }
80               }
81          }
82          else
83          {
84               $test = $index >= $size + $start;
85               if ($length !== null) {
86                    if ($length >= 0) {
87                         $test = $test && $index < $size + $start + $length;
88                    } else {
89                         $test = $test && $index < $size + $length;
90                    }
91               }
92          }
93         
94          if ($even !== null) {
95               $test = $test && $index%2 == $even;
96          }
97         
98          return $test;
99     }
100     
101     # Static methods
102     public static function global_filter($str,
103     $encode_xml, $remove_html, $cut_string, $lower_case, $upper_case ,$tag='')
104     {
105          $args = func_get_args();
106          array_pop($args);
107          $args[0] =& $str;
108         
109          # --BEHAVIOR-- publicBeforeContentFilter
110          $res = $GLOBALS['core']->callBehavior('publicBeforeContentFilter',$GLOBALS['core'],$tag,$args);
111         
112          if ($remove_html) {
113               $str = self::remove_html($str);
114               $str = preg_replace('/\s+/',' ',$str);
115          }
116         
117          if ($encode_xml) {
118               $str = self::encode_xml($str);
119          }
120         
121          if ($cut_string) {
122               $str = self::cut_string($str,(integer) $cut_string);
123          }
124         
125          if ($lower_case) {
126               $str = self::lower_case($str);
127          } elseif ($upper_case) {
128               $str = self::upper_case($str);
129          }
130         
131          # --BEHAVIOR-- publicAfterContentFilter
132          $res = $GLOBALS['core']->callBehavior('publicAfterContentFilter',$GLOBALS['core'],$tag,$args);
133         
134          return $str;
135     }
136     
137     
138     public static function cut_string($str,$l)
139     {
140          return text::cutString($str,$l);
141     }
142     
143     public static function encode_xml($str)
144     {
145          return html::escapeHTML($str);
146     }
147     
148     public static function remove_html($str)
149     {
150          return html::decodeEntities(html::clean($str));
151     }
152     
153     public static function lower_case($str)
154     {
155          return mb_strtolower($str);
156     }
157     
158     public static function upper_case($str)
159     {
160          return mb_strtoupper($str);
161     }
162     
163     public static function categoryPostParam(&$p)
164     {
165          $not = substr($p['cat_url'],0,1) == '!';
166          if ($not) {
167               $p['cat_url'] = substr($p['cat_url'],1);
168          }
169         
170          $p['cat_url'] = preg_split('/\s*,\s*/',$p['cat_url'],-1,PREG_SPLIT_NO_EMPTY);
171         
172          foreach ($p['cat_url'] as &$v)
173          {
174               if ($not) {
175                    $v .= ' ?not';
176               }
177               if ($GLOBALS['_ctx']->exists('categories') && preg_match('/#self/',$v)) {
178                    $v = preg_replace('/#self/',$GLOBALS['_ctx']->categories->cat_url,$v);
179               } elseif ($GLOBALS['_ctx']->exists('posts') && preg_match('/#self/',$v)) {
180                    $v = preg_replace('/#self/',$GLOBALS['_ctx']->posts->cat_url,$v);
181               }
182          }
183     }
184     
185     # Static methods for pagination
186     public static function PaginationNbPages()
187     {
188          global $_ctx;
189         
190          if ($_ctx->pagination === null) {
191               return false;
192          }
193         
194          $nb_posts = $_ctx->pagination->f(0);
195          $nb_per_page = $_ctx->post_params['limit'][1];
196         
197          $nb_pages = ceil($nb_posts/$nb_per_page);
198         
199          return $nb_pages;
200     }
201     
202     public static function PaginationPosition($offset=0)
203     {
204          if (isset($GLOBALS['_page_number'])) {
205               $p = $GLOBALS['_page_number'];
206          } else {
207               $p = 1;
208          }
209         
210          $p = $p+$offset;
211         
212          $n = self::PaginationNbPages();
213          if (!$n) {
214               return $p;
215          }
216         
217          if ($p > $n || $p <= 0) {
218               return 1;
219          } else {
220               return $p;
221          }
222     }
223     
224     public static function PaginationStart()
225     {
226          if (isset($GLOBALS['_page_number'])) {
227               return self::PaginationPosition() == 1;
228          }
229         
230          return true;
231     }
232     
233     public static function PaginationEnd()
234     {
235          if (isset($GLOBALS['_page_number'])) {
236               return self::PaginationPosition() == self::PaginationNbPages();
237          }
238         
239          return false;
240     }
241     
242     public static function PaginationURL($offset=0)
243     {
244          $args = $_SERVER['URL_REQUEST_PART'];
245         
246          $n = self::PaginationPosition($offset);
247         
248          $args = preg_replace('#(^|/)page/([0-9]+)$#','',$args);
249         
250          $url = $GLOBALS['core']->blog->url.$args;
251         
252          if ($n > 1) {
253               $url = preg_replace('#/$#','',$url);
254               $url .= '/page/'.$n;
255          }
256         
257          # If search param
258          if (!empty($_GET['q'])) {
259               $s = strpos($url,'?') !== false ? '&amp;' : '?';
260               $url .= $s.'q='.rawurlencode($_GET['q']);
261          }
262          return $url;
263     }
264     
265     # Robots policy
266     public static function robotsPolicy($base,$over)
267     {
268          $pol = array('INDEX' => 'INDEX','FOLLOW' => 'FOLLOW', 'ARCHIVE' => 'ARCHIVE');
269          $base = array_flip(preg_split('/\s*,\s*/',$base));
270          $over = array_flip(preg_split('/\s*,\s*/',$over));
271         
272          foreach ($pol as $k => &$v)
273          {
274               if (isset($base[$k]) || isset($base['NO'.$k])) {
275                    $v = isset($base['NO'.$k]) ? 'NO'.$k : $k;
276               }
277               if (isset($over[$k]) || isset($over['NO'.$k])) {
278                    $v = isset($over['NO'.$k]) ? 'NO'.$k : $k;
279               }
280          }
281         
282          if ($pol['ARCHIVE'] == 'ARCHIVE') {
283               unset($pol['ARCHIVE']);
284          }
285         
286          return implode(', ',$pol);
287     }
288     
289     # Smilies static methods
290     public static function getSmilies($blog)
291     {
292          $path = array();
293          if (isset($GLOBALS['__theme'])) {
294               $path[] = $GLOBALS['__theme'];
295          }
296          $path[] = 'default';
297          $definition = $blog->themes_path.'/%s/smilies/smilies.txt';
298          $base_url = $blog->settings->system->themes_url.'/%s/smilies/';
299         
300          $res = array();
301         
302          foreach ($path as $t)
303          {
304               if (file_exists(sprintf($definition,$t))) {
305                    $base_url = sprintf($base_url,$t);
306                    return self::smiliesDefinition(sprintf($definition,$t),$base_url);
307               }
308          }
309          return false;
310     }
311     
312     public static function smiliesDefinition($f,$url)
313     {
314          $def = file($f);
315               
316          $res = array();
317          foreach($def as $v)
318          {
319               $v = trim($v);
320               if (preg_match('|^([^\t]*)[\t]+(.*)$|',$v,$matches))
321               {
322                    $r = '/(\A|[\s]+|>)('.preg_quote($matches[1],'/').')([\s]+|[<]|\Z)/ms';
323                    $s = '$1<img src="'.$url.$matches[2].'" '.
324                    'alt="$2" class="smiley" />$3';
325                    $res[$r] = $s;
326               }
327          }
328         
329          return $res;
330     }
331     
332     public static function addSmilies($str)
333     {
334          if (!isset($GLOBALS['__smilies']) || !is_array($GLOBALS['__smilies'])) {
335               return $str;
336          }
337         
338          return preg_replace(array_keys($GLOBALS['__smilies']),array_values($GLOBALS['__smilies']),$str);
339     }
340     
341     # First post image helpers
342     public static function EntryFirstImageHelper($size,$with_category,$class="")
343     {
344          if (!preg_match('/^sq|t|s|m|o$/',$size)) {
345               $size = 's';
346          }
347         
348          global $core, $_ctx;
349         
350          $p_url = $core->blog->settings->system->public_url;
351          $p_site = preg_replace('#^(.+?//.+?)/(.*)$#','$1',$core->blog->url);
352          $p_root = $core->blog->public_path;
353         
354          $pattern = '(?:'.preg_quote($p_site,'/').')?'.preg_quote($p_url,'/');
355          $pattern = sprintf('/<img.+?src="%s(.*?\.(?:jpg|gif|png))"[^>]+/msu',$pattern);
356         
357          $src = '';
358          $alt = '';
359         
360          # We first look in post content
361          if ($_ctx->posts)
362          {
363               $subject = $_ctx->posts->post_excerpt_xhtml.$_ctx->posts->post_content_xhtml.$_ctx->posts->cat_desc;
364               if (preg_match_all($pattern,$subject,$m) > 0)
365               {
366                    foreach ($m[1] as $i => $img) {
367                         if (($src = self::ContentFirstImageLookup($p_root,$img,$size)) !== false) {
368                              $src = $p_url.(dirname($img) != '/' ? dirname($img) : '').'/'.$src;
369                              if (preg_match('/alt="([^"]+)"/',$m[0][$i],$malt)) {
370                                   $alt = $malt[1];
371                              }
372                              break;
373                         }
374                    }
375               }
376          }
377         
378          # No src, look in category description if available
379          if (!$src && $with_category && $_ctx->categories)
380          {
381               if (preg_match_all($pattern,$_ctx->categories->cat_desc,$m) > 0)
382               {
383                    foreach ($m[1] as $i => $img) {
384                         if (($src = self::ContentFirstImageLookup($p_root,$img,$size)) !== false) {
385                              $src = $p_url.(dirname($img) != '/' ? dirname($img) : '').'/'.$src;
386                              if (preg_match('/alt="([^"]+)"/',$m[0][$i],$malt)) {
387                                   $alt = $malt[1];
388                              }
389                              break;
390                         }
391                    }
392               };
393          }
394         
395          if ($src) {
396               return '<img alt="'.$alt.'" src="'.$src.'" class="'.$class.'" />';
397          }
398     }
399     
400     private static function ContentFirstImageLookup($root,$img,$size)
401     {
402          # Get base name and extension
403          $info = path::info($img);
404          $base = $info['base'];
405         
406          if (preg_match('/^\.(.+)_(sq|t|s|m)$/',$base,$m)) {
407               $base = $m[1];
408          }
409         
410          $res = false;
411          if ($size != 'o' && file_exists($root.'/'.$info['dirname'].'/.'.$base.'_'.$size.'.jpg'))
412          {
413               $res = '.'.$base.'_'.$size.'.jpg';
414          }
415          else
416          {
417               $f = $root.'/'.$info['dirname'].'/'.$base;
418               if (file_exists($f.'.'.$info['extension'])) {
419                    $res = $base.'.'.$info['extension'];
420               } elseif (file_exists($f.'.jpg')) {
421                    $res = $base.'.jpg';
422               } elseif (file_exists($f.'.png')) {
423                    $res = $base.'.png';
424               } elseif (file_exists($f.'.gif')) {
425                    $res = $base.'.gif';
426               }
427          }
428         
429          if ($res) {
430               return $res;
431          }
432          return false;
433     }
434}
435?>
Note: See TracBrowser for help on using the repository browser.

Sites map