Dotclear

source: inc/public/lib.tpl.context.php @ 937:37a5159c95f8

Revision 937:37a5159c95f8, 10.4 KB checked in by franck <carnet.franck.paul@…>, 13 years ago (diff)

Add cat_only attribute (search in category description only) for {{tpl:EntryFirstImage}}. fixes #499

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     
102     # Static methods
103     public static function global_filter($str,
104     $encode_xml, $remove_html, $cut_string, $lower_case, $upper_case ,$tag='')
105     {
106          $args = func_get_args();
107          array_pop($args);
108          $args[0] =& $str;
109         
110          # --BEHAVIOR-- publicBeforeContentFilter
111          $res = $GLOBALS['core']->callBehavior('publicBeforeContentFilter',$GLOBALS['core'],$tag,$args);
112         
113          if ($remove_html) {
114               $str = self::remove_html($str);
115               $str = preg_replace('/\s+/',' ',$str);
116          }
117         
118          if ($encode_xml) {
119               $str = self::encode_xml($str);
120          }
121         
122          if ($cut_string) {
123               $str = self::cut_string($str,(integer) $cut_string);
124          }
125         
126          if ($lower_case) {
127               $str = self::lower_case($str);
128          } elseif ($upper_case) {
129               if ($upper_case == 2) {
130                    $str = self::capitalize($str);
131               } else {
132                    $str = self::upper_case($str);
133               }
134          }
135         
136          # --BEHAVIOR-- publicAfterContentFilter
137          $res = $GLOBALS['core']->callBehavior('publicAfterContentFilter',$GLOBALS['core'],$tag,$args);
138         
139          return $str;
140     }
141     
142     
143     public static function cut_string($str,$l)
144     {
145          return text::cutString($str,$l);
146     }
147     
148     public static function encode_xml($str)
149     {
150          return html::escapeHTML($str);
151     }
152     
153     public static function remove_html($str)
154     {
155          return html::decodeEntities(html::clean($str));
156     }
157     
158     public static function lower_case($str)
159     {
160          return mb_strtolower($str);
161     }
162     
163     public static function upper_case($str)
164     {
165          return mb_strtoupper($str);
166     }
167
168     public static function capitalize($str)
169     {
170          if ($str != '') {
171             $str[0] = mb_strtoupper($str[0]);
172          }
173          return $str;
174     }
175     
176     public static function categoryPostParam(&$p)
177     {
178          $not = substr($p['cat_url'],0,1) == '!';
179          if ($not) {
180               $p['cat_url'] = substr($p['cat_url'],1);
181          }
182         
183          $p['cat_url'] = preg_split('/\s*,\s*/',$p['cat_url'],-1,PREG_SPLIT_NO_EMPTY);
184         
185          foreach ($p['cat_url'] as &$v)
186          {
187               if ($not) {
188                    $v .= ' ?not';
189               }
190               if ($GLOBALS['_ctx']->exists('categories') && preg_match('/#self/',$v)) {
191                    $v = preg_replace('/#self/',$GLOBALS['_ctx']->categories->cat_url,$v);
192               } elseif ($GLOBALS['_ctx']->exists('posts') && preg_match('/#self/',$v)) {
193                    $v = preg_replace('/#self/',$GLOBALS['_ctx']->posts->cat_url,$v);
194               }
195          }
196     }
197     
198     # Static methods for pagination
199     public static function PaginationNbPages()
200     {
201          global $_ctx;
202         
203          if ($_ctx->pagination === null) {
204               return false;
205          }
206         
207          $nb_posts = $_ctx->pagination->f(0);
208          $nb_per_page = $_ctx->post_params['limit'][1];
209         
210          $nb_pages = ceil($nb_posts/$nb_per_page);
211         
212          return $nb_pages;
213     }
214     
215     public static function PaginationPosition($offset=0)
216     {
217          if (isset($GLOBALS['_page_number'])) {
218               $p = $GLOBALS['_page_number'];
219          } else {
220               $p = 1;
221          }
222         
223          $p = $p+$offset;
224         
225          $n = self::PaginationNbPages();
226          if (!$n) {
227               return $p;
228          }
229         
230          if ($p > $n || $p <= 0) {
231               return 1;
232          } else {
233               return $p;
234          }
235     }
236     
237     public static function PaginationStart()
238     {
239          if (isset($GLOBALS['_page_number'])) {
240               return self::PaginationPosition() == 1;
241          }
242         
243          return true;
244     }
245     
246     public static function PaginationEnd()
247     {
248          if (isset($GLOBALS['_page_number'])) {
249               return self::PaginationPosition() == self::PaginationNbPages();
250          }
251         
252          return false;
253     }
254     
255     public static function PaginationURL($offset=0)
256     {
257          $args = $_SERVER['URL_REQUEST_PART'];
258         
259          $n = self::PaginationPosition($offset);
260         
261          $args = preg_replace('#(^|/)page/([0-9]+)$#','',$args);
262         
263          $url = $GLOBALS['core']->blog->url.$args;
264         
265          if ($n > 1) {
266               $url = preg_replace('#/$#','',$url);
267               $url .= '/page/'.$n;
268          }
269         
270          # If search param
271          if (!empty($_GET['q'])) {
272               $s = strpos($url,'?') !== false ? '&amp;' : '?';
273               $url .= $s.'q='.rawurlencode($_GET['q']);
274          }
275          return $url;
276     }
277     
278     # Robots policy
279     public static function robotsPolicy($base,$over)
280     {
281          $pol = array('INDEX' => 'INDEX','FOLLOW' => 'FOLLOW', 'ARCHIVE' => 'ARCHIVE');
282          $base = array_flip(preg_split('/\s*,\s*/',$base));
283          $over = array_flip(preg_split('/\s*,\s*/',$over));
284         
285          foreach ($pol as $k => &$v)
286          {
287               if (isset($base[$k]) || isset($base['NO'.$k])) {
288                    $v = isset($base['NO'.$k]) ? 'NO'.$k : $k;
289               }
290               if (isset($over[$k]) || isset($over['NO'.$k])) {
291                    $v = isset($over['NO'.$k]) ? 'NO'.$k : $k;
292               }
293          }
294         
295          if ($pol['ARCHIVE'] == 'ARCHIVE') {
296               unset($pol['ARCHIVE']);
297          }
298         
299          return implode(', ',$pol);
300     }
301     
302     # Smilies static methods
303     public static function getSmilies($blog)
304     {
305          $path = array();
306          if (isset($GLOBALS['__theme'])) {
307               $path[] = $GLOBALS['__theme'];
308          }
309          $path[] = 'default';
310          $definition = $blog->themes_path.'/%s/smilies/smilies.txt';
311          $base_url = $blog->settings->system->themes_url.'/%s/smilies/';
312         
313          $res = array();
314         
315          foreach ($path as $t)
316          {
317               if (file_exists(sprintf($definition,$t))) {
318                    $base_url = sprintf($base_url,$t);
319                    return self::smiliesDefinition(sprintf($definition,$t),$base_url);
320               }
321          }
322          return false;
323     }
324     
325     public static function smiliesDefinition($f,$url)
326     {
327          $def = file($f);
328               
329          $res = array();
330          foreach($def as $v)
331          {
332               $v = trim($v);
333               if (preg_match('|^([^\t]*)[\t]+(.*)$|',$v,$matches))
334               {
335                    $r = '/(\A|[\s]+|>)('.preg_quote($matches[1],'/').')([\s]+|[<]|\Z)/ms';
336                    $s = '$1<img src="'.$url.$matches[2].'" '.
337                    'alt="$2" class="smiley" />$3';
338                    $res[$r] = $s;
339               }
340          }
341         
342          return $res;
343     }
344     
345     public static function addSmilies($str)
346     {
347          if (!isset($GLOBALS['__smilies']) || !is_array($GLOBALS['__smilies'])) {
348               return $str;
349          }
350         
351          return preg_replace(array_keys($GLOBALS['__smilies']),array_values($GLOBALS['__smilies']),$str);
352     }
353
354     # First post image helpers
355     public static function EntryFirstImageHelper($size,$with_category,$class="",$no_tag=false,$content_only=false,$cat_only=false)
356     {
357          global $core, $_ctx;
358         
359          $media = new dcMedia($core);
360          $sizes = implode('|',array_keys($media->thumb_sizes)).'|o';
361          if (!preg_match('/^'.$sizes.'$/',$size)) {
362               $size = 's';
363          }
364          $p_url = $core->blog->settings->system->public_url;
365          $p_site = preg_replace('#^(.+?//.+?)/(.*)$#','$1',$core->blog->url);
366          $p_root = $core->blog->public_path;
367         
368          $pattern = '(?:'.preg_quote($p_site,'/').')?'.preg_quote($p_url,'/');
369          $pattern = sprintf('/<img.+?src="%s(.*?\.(?:jpg|jpeg|gif|png))"[^>]+/msui',$pattern);
370         
371          $src = '';
372          $alt = '';
373         
374          # We first look in post content
375          if (!$cat_only && $_ctx->posts)
376          {
377               $subject = ($content_only ? '' : $_ctx->posts->post_excerpt_xhtml).$_ctx->posts->post_content_xhtml;
378               if (preg_match_all($pattern,$subject,$m) > 0)
379               {
380                    foreach ($m[1] as $i => $img) {
381                         if (($src = self::ContentFirstImageLookup($p_root,$img,$size)) !== false) {
382                              $dirname = str_replace('\\', '/', dirname($img)); 
383                              $src = $p_url.($dirname != '/' ? $dirname : '').'/'.$src;
384                              if (preg_match('/alt="([^"]+)"/',$m[0][$i],$malt)) {
385                                   $alt = $malt[1];
386                              }
387                              break;
388                         }
389                    }
390               }
391          }
392         
393          # No src, look in category description if available
394          if (!$src && $with_category && $_ctx->categories)
395          {
396               if (preg_match_all($pattern,$_ctx->categories->cat_desc,$m) > 0)
397               {
398                    foreach ($m[1] as $i => $img) {
399                         if (($src = self::ContentFirstImageLookup($p_root,$img,$size)) !== false) {
400                              $dirname = str_replace('\\', '/', dirname($img)); 
401                              $src = $p_url.($dirname != '/' ? $dirname : '').'/'.$src;
402                              if (preg_match('/alt="([^"]+)"/',$m[0][$i],$malt)) {
403                                   $alt = $malt[1];
404                              }
405                              break;
406                         }
407                    }
408               };
409          }
410         
411          if ($src) {
412               if ($no_tag) {
413                    return $src;
414               } else {
415                    return '<img alt="'.$alt.'" src="'.$src.'" class="'.$class.'" />';
416               }
417          }
418     }
419     
420     private static function ContentFirstImageLookup($root,$img,$size)
421     {
422          global $core;
423         
424          # Get base name and extension
425          $info = path::info($img);
426          $base = $info['base'];
427         
428          $media = new dcMedia($core);
429          $sizes = implode('|',array_keys($media->thumb_sizes));
430          if (preg_match('/^\.(.+)_('.$sizes.')$/',$base,$m)) {
431               $base = $m[1];
432          }
433         
434          $res = false;
435          if ($size != 'o' && file_exists($root.'/'.$info['dirname'].'/.'.$base.'_'.$size.'.jpg'))
436          {
437               $res = '.'.$base.'_'.$size.'.jpg';
438          }
439          else
440          {
441               $f = $root.'/'.$info['dirname'].'/'.$base;
442               if (file_exists($f.'.'.$info['extension'])) {
443                    $res = $base.'.'.$info['extension'];
444               } elseif (file_exists($f.'.jpg')) {
445                    $res = $base.'.jpg';
446               } elseif (file_exists($f.'.jpeg')) {
447                    $res = $base.'.jpeg';
448               } elseif (file_exists($f.'.png')) {
449                    $res = $base.'.png';
450               } elseif (file_exists($f.'.gif')) {
451                    $res = $base.'.gif';
452               } elseif (file_exists($f.'.JPG')) {
453                    $res = $base.'.JPG';
454               } elseif (file_exists($f.'.JPEG')) {
455                    $res = $base.'.JPEG';
456               } elseif (file_exists($f.'.PNG')) {
457                    $res = $base.'.PNG';
458               } elseif (file_exists($f.'.GIF')) {
459                    $res = $base.'.GIF';
460               }
461          }
462         
463          if ($res) {
464               return $res;
465          }
466          return false;
467     }
468}
469?>
Note: See TracBrowser for help on using the repository browser.

Sites map