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