[0] | 1 | <?php |
---|
| 2 | # -- BEGIN LICENSE BLOCK --------------------------------------- |
---|
| 3 | # |
---|
| 4 | # This file is part of Dotclear 2. |
---|
| 5 | # |
---|
[1179] | 6 | # Copyright (c) 2003-2013 Olivier Meunier & Association Dotclear |
---|
[0] | 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 ----------------------------------------- |
---|
[3721] | 12 | if (!defined('DC_RC_PATH')) {return;} |
---|
[0] | 13 | |
---|
| 14 | class context |
---|
| 15 | { |
---|
[3721] | 16 | public $stack = array(); |
---|
[2566] | 17 | |
---|
[3721] | 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 | } |
---|
[2566] | 29 | |
---|
[3721] | 30 | public function __get($name) |
---|
| 31 | { |
---|
| 32 | if (!isset($this->stack[$name])) { |
---|
| 33 | return; |
---|
| 34 | } |
---|
[2566] | 35 | |
---|
[3721] | 36 | $n = count($this->stack[$name]); |
---|
| 37 | if ($n > 0) { |
---|
| 38 | return $this->stack[$name][($n - 1)]; |
---|
| 39 | } |
---|
[2566] | 40 | |
---|
[3721] | 41 | return; |
---|
| 42 | } |
---|
[2566] | 43 | |
---|
[3721] | 44 | public function exists($name) |
---|
| 45 | { |
---|
| 46 | return isset($this->stack[$name][0]); |
---|
| 47 | } |
---|
[2566] | 48 | |
---|
[3721] | 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 | } |
---|
[2566] | 59 | |
---|
[3721] | 60 | # Loop position tests |
---|
| 61 | public function loopPosition($start, $length = null, $even = null, $modulo = null) |
---|
| 62 | { |
---|
| 63 | if (!$this->cur_loop) { |
---|
| 64 | return false; |
---|
| 65 | } |
---|
[2566] | 66 | |
---|
[3721] | 67 | $index = $this->cur_loop->index(); |
---|
| 68 | $size = $this->cur_loop->count(); |
---|
[2566] | 69 | |
---|
[3721] | 70 | $test = false; |
---|
| 71 | if ($start >= 0) { |
---|
| 72 | $test = $index >= $start; |
---|
| 73 | if ($length !== null) { |
---|
| 74 | if ($length >= 0) { |
---|
| 75 | $test = $test && $index < $start + $length; |
---|
| 76 | } else { |
---|
| 77 | $test = $test && $index < $size + $length; |
---|
| 78 | } |
---|
| 79 | } |
---|
| 80 | } else { |
---|
| 81 | $test = $index >= $size + $start; |
---|
| 82 | if ($length !== null) { |
---|
| 83 | if ($length >= 0) { |
---|
| 84 | $test = $test && $index < $size + $start + $length; |
---|
| 85 | } else { |
---|
| 86 | $test = $test && $index < $size + $length; |
---|
| 87 | } |
---|
| 88 | } |
---|
| 89 | } |
---|
[2566] | 90 | |
---|
[3721] | 91 | if ($even !== null) { |
---|
| 92 | $test = $test && $index % 2 == $even; |
---|
| 93 | } |
---|
[2566] | 94 | |
---|
[3721] | 95 | if ($modulo !== null) { |
---|
| 96 | $test = $test && ($index % $modulo == 0); |
---|
| 97 | } |
---|
[2774] | 98 | |
---|
[3721] | 99 | return $test; |
---|
| 100 | } |
---|
[2566] | 101 | |
---|
[3721] | 102 | /** |
---|
| 103 | @deprecated since version 2.11 , use tpl_context::global_filters instead |
---|
| 104 | */ |
---|
| 105 | public static function global_filter($str, |
---|
| 106 | $encode_xml, $remove_html, $cut_string, $lower_case, $upper_case, $encode_url, $tag = '') { |
---|
| 107 | return self::global_filters( |
---|
| 108 | $str, |
---|
| 109 | array(0 => null, |
---|
| 110 | 'encode_xml' => $encode_xml, |
---|
| 111 | 'remove_html' => $remove_html, |
---|
| 112 | 'cut_string' => $cut_string, |
---|
| 113 | 'lower_case' => $lower_case, |
---|
| 114 | 'upper_case' => ($upper_case == 1 ? 1 : 0), |
---|
| 115 | 'capitalize' => ($upper_case == 2 ? 1 : 0), |
---|
| 116 | 'encode_url' => $encode_url), |
---|
| 117 | $tag); |
---|
| 118 | } |
---|
[3426] | 119 | |
---|
[3721] | 120 | public static function global_filters($str, $args, $tag = '') |
---|
| 121 | { |
---|
| 122 | $args[0] = &$str; |
---|
| 123 | $params = new ArrayObject($args); |
---|
[2566] | 124 | |
---|
[3721] | 125 | # --BEHAVIOR-- publicBeforeContentFilter |
---|
| 126 | $res = $GLOBALS['core']->callBehavior('publicBeforeContentFilter', $GLOBALS['core'], $tag, $params); |
---|
| 127 | $str = $params[0]; |
---|
[2566] | 128 | |
---|
[3721] | 129 | if ($params['strip_tags']) { |
---|
| 130 | $str = self::strip_tags($str); |
---|
| 131 | } |
---|
| 132 | if ($params['remove_html']) { |
---|
| 133 | $str = self::remove_html($str); |
---|
| 134 | $str = preg_replace('/\s+/', ' ', $str); |
---|
| 135 | } |
---|
| 136 | if ($params['encode_xml'] || $params['encode_html']) { |
---|
| 137 | $str = self::encode_xml($str); |
---|
| 138 | } |
---|
[2566] | 139 | |
---|
[3721] | 140 | if ($params['cut_string'] > 0) { |
---|
| 141 | $str = self::cut_string($str, (integer) $params['cut_string']); |
---|
| 142 | } |
---|
[2566] | 143 | |
---|
[3721] | 144 | if ($params['lower_case']) { |
---|
| 145 | $str = self::lower_case($str); |
---|
| 146 | } elseif ($params['capitalize']) { |
---|
| 147 | $str = self::capitalize($str); |
---|
| 148 | } elseif ($params['upper_case']) { |
---|
| 149 | $str = self::upper_case($str); |
---|
| 150 | } |
---|
[3426] | 151 | |
---|
[3721] | 152 | if ($params['encode_url']) { |
---|
| 153 | $str = self::encode_url($str); |
---|
| 154 | } |
---|
[2566] | 155 | |
---|
[3721] | 156 | # --BEHAVIOR-- publicAfterContentFilter |
---|
| 157 | $res = $GLOBALS['core']->callBehavior('publicAfterContentFilter', $GLOBALS['core'], $tag, $params); |
---|
| 158 | $str = $params[0]; |
---|
[2566] | 159 | |
---|
[3721] | 160 | return $str; |
---|
| 161 | } |
---|
[2566] | 162 | |
---|
[3721] | 163 | public static function encode_url($str) |
---|
| 164 | { |
---|
| 165 | return urlencode($str); |
---|
| 166 | } |
---|
[2566] | 167 | |
---|
[3721] | 168 | public static function cut_string($str, $l) |
---|
| 169 | { |
---|
| 170 | return text::cutString($str, $l); |
---|
| 171 | } |
---|
[2566] | 172 | |
---|
[3721] | 173 | public static function encode_xml($str) |
---|
| 174 | { |
---|
| 175 | return html::escapeHTML($str); |
---|
| 176 | } |
---|
[2566] | 177 | |
---|
[3721] | 178 | public static function remove_html($str) |
---|
| 179 | { |
---|
| 180 | return html::decodeEntities(html::clean($str)); |
---|
| 181 | } |
---|
[2566] | 182 | |
---|
[3721] | 183 | public static function strip_tags($str) |
---|
| 184 | { |
---|
| 185 | return trim(preg_replace('/ {2,}/', ' ', str_replace(array("\r", "\n", "\t"), ' ', html::clean($str)))); |
---|
| 186 | } |
---|
[3426] | 187 | |
---|
[3721] | 188 | public static function lower_case($str) |
---|
| 189 | { |
---|
| 190 | return mb_strtolower($str); |
---|
| 191 | } |
---|
[2566] | 192 | |
---|
[3721] | 193 | public static function upper_case($str) |
---|
| 194 | { |
---|
| 195 | return mb_strtoupper($str); |
---|
| 196 | } |
---|
[930] | 197 | |
---|
[3721] | 198 | public static function capitalize($str) |
---|
| 199 | { |
---|
| 200 | if ($str != '') { |
---|
| 201 | $str[0] = mb_strtoupper($str[0]); |
---|
| 202 | } |
---|
| 203 | return $str; |
---|
| 204 | } |
---|
[2566] | 205 | |
---|
[3721] | 206 | public static function categoryPostParam(&$p) |
---|
| 207 | { |
---|
| 208 | $not = substr($p['cat_url'], 0, 1) == '!'; |
---|
| 209 | if ($not) { |
---|
| 210 | $p['cat_url'] = substr($p['cat_url'], 1); |
---|
| 211 | } |
---|
[2566] | 212 | |
---|
[3721] | 213 | $p['cat_url'] = preg_split('/\s*,\s*/', $p['cat_url'], -1, PREG_SPLIT_NO_EMPTY); |
---|
[2566] | 214 | |
---|
[3721] | 215 | foreach ($p['cat_url'] as &$v) { |
---|
| 216 | if ($not) { |
---|
| 217 | $v .= ' ?not'; |
---|
| 218 | } |
---|
| 219 | if ($GLOBALS['_ctx']->exists('categories') && preg_match('/#self/', $v)) { |
---|
| 220 | $v = preg_replace('/#self/', $GLOBALS['_ctx']->categories->cat_url, $v); |
---|
| 221 | } elseif ($GLOBALS['_ctx']->exists('posts') && preg_match('/#self/', $v)) { |
---|
| 222 | $v = preg_replace('/#self/', $GLOBALS['_ctx']->posts->cat_url, $v); |
---|
| 223 | } |
---|
| 224 | } |
---|
| 225 | } |
---|
[2566] | 226 | |
---|
[3721] | 227 | # Static methods for pagination |
---|
| 228 | public static function PaginationNbPages() |
---|
| 229 | { |
---|
| 230 | global $_ctx; |
---|
[2566] | 231 | |
---|
[3721] | 232 | if ($_ctx->pagination === null) { |
---|
| 233 | return false; |
---|
| 234 | } |
---|
[2566] | 235 | |
---|
[3721] | 236 | $nb_posts = $_ctx->pagination->f(0); |
---|
| 237 | if (($GLOBALS['core']->url->type == 'default') || ($GLOBALS['core']->url->type == 'default-page')) { |
---|
| 238 | $nb_pages = ceil(($nb_posts - $_ctx->nb_entry_first_page) / $_ctx->nb_entry_per_page + 1); |
---|
| 239 | } else { |
---|
| 240 | $nb_pages = ceil($nb_posts / $_ctx->nb_entry_per_page); |
---|
| 241 | } |
---|
[2566] | 242 | |
---|
[3721] | 243 | return $nb_pages; |
---|
| 244 | } |
---|
[2566] | 245 | |
---|
[3721] | 246 | public static function PaginationPosition($offset = 0) |
---|
| 247 | { |
---|
| 248 | if (isset($GLOBALS['_page_number'])) { |
---|
| 249 | $p = $GLOBALS['_page_number']; |
---|
| 250 | } else { |
---|
| 251 | $p = 1; |
---|
| 252 | } |
---|
[2566] | 253 | |
---|
[3721] | 254 | $p = $p + $offset; |
---|
[2566] | 255 | |
---|
[3721] | 256 | $n = self::PaginationNbPages(); |
---|
| 257 | if (!$n) { |
---|
| 258 | return $p; |
---|
| 259 | } |
---|
[2566] | 260 | |
---|
[3721] | 261 | if ($p > $n || $p <= 0) { |
---|
| 262 | return 1; |
---|
| 263 | } else { |
---|
| 264 | return $p; |
---|
| 265 | } |
---|
| 266 | } |
---|
[2566] | 267 | |
---|
[3721] | 268 | public static function PaginationStart() |
---|
| 269 | { |
---|
| 270 | if (isset($GLOBALS['_page_number'])) { |
---|
| 271 | return self::PaginationPosition() == 1; |
---|
| 272 | } |
---|
[2566] | 273 | |
---|
[3721] | 274 | return true; |
---|
| 275 | } |
---|
[2566] | 276 | |
---|
[3721] | 277 | public static function PaginationEnd() |
---|
| 278 | { |
---|
| 279 | if (isset($GLOBALS['_page_number'])) { |
---|
| 280 | return self::PaginationPosition() == self::PaginationNbPages(); |
---|
| 281 | } |
---|
[2566] | 282 | |
---|
[3721] | 283 | return false; |
---|
| 284 | } |
---|
[2566] | 285 | |
---|
[3721] | 286 | public static function PaginationURL($offset = 0) |
---|
| 287 | { |
---|
| 288 | $args = $_SERVER['URL_REQUEST_PART']; |
---|
[2566] | 289 | |
---|
[3721] | 290 | $n = self::PaginationPosition($offset); |
---|
[2566] | 291 | |
---|
[3721] | 292 | $args = preg_replace('#(^|/)page/([0-9]+)$#', '', $args); |
---|
[2566] | 293 | |
---|
[3721] | 294 | $url = $GLOBALS['core']->blog->url . $args; |
---|
[2566] | 295 | |
---|
[3721] | 296 | if ($n > 1) { |
---|
| 297 | $url = preg_replace('#/$#', '', $url); |
---|
| 298 | $url .= '/page/' . $n; |
---|
| 299 | } |
---|
[2566] | 300 | |
---|
[3721] | 301 | # If search param |
---|
| 302 | if (!empty($_GET['q'])) { |
---|
| 303 | $s = strpos($url, '?') !== false ? '&' : '?'; |
---|
| 304 | $url .= $s . 'q=' . rawurlencode($_GET['q']); |
---|
| 305 | } |
---|
| 306 | return $url; |
---|
| 307 | } |
---|
[2566] | 308 | |
---|
[3721] | 309 | # Robots policy |
---|
| 310 | public static function robotsPolicy($base, $over) |
---|
| 311 | { |
---|
| 312 | $pol = array('INDEX' => 'INDEX', 'FOLLOW' => 'FOLLOW', 'ARCHIVE' => 'ARCHIVE'); |
---|
| 313 | $base = array_flip(preg_split('/\s*,\s*/', $base)); |
---|
| 314 | $over = array_flip(preg_split('/\s*,\s*/', $over)); |
---|
[2566] | 315 | |
---|
[3721] | 316 | foreach ($pol as $k => &$v) { |
---|
| 317 | if (isset($base[$k]) || isset($base['NO' . $k])) { |
---|
| 318 | $v = isset($base['NO' . $k]) ? 'NO' . $k : $k; |
---|
| 319 | } |
---|
| 320 | if (isset($over[$k]) || isset($over['NO' . $k])) { |
---|
| 321 | $v = isset($over['NO' . $k]) ? 'NO' . $k : $k; |
---|
| 322 | } |
---|
| 323 | } |
---|
[2566] | 324 | |
---|
[3721] | 325 | if ($pol['ARCHIVE'] == 'ARCHIVE') { |
---|
| 326 | unset($pol['ARCHIVE']); |
---|
| 327 | } |
---|
[2566] | 328 | |
---|
[3721] | 329 | return implode(', ', $pol); |
---|
| 330 | } |
---|
[2566] | 331 | |
---|
[3721] | 332 | # Smilies static methods |
---|
| 333 | public static function getSmilies($blog) |
---|
| 334 | { |
---|
| 335 | $path = array(); |
---|
| 336 | if (isset($GLOBALS['__theme'])) { |
---|
| 337 | $path[] = $GLOBALS['__theme']; |
---|
| 338 | if (isset($GLOBALS['__parent_theme'])) { |
---|
| 339 | $path[] = $GLOBALS['__parent_theme']; |
---|
| 340 | } |
---|
| 341 | } |
---|
| 342 | $path[] = 'default'; |
---|
| 343 | $definition = $blog->themes_path . '/%s/smilies/smilies.txt'; |
---|
| 344 | $base_url = $blog->settings->system->themes_url . '/%s/smilies/'; |
---|
[2566] | 345 | |
---|
[3721] | 346 | $res = array(); |
---|
[2566] | 347 | |
---|
[3721] | 348 | foreach ($path as $t) { |
---|
| 349 | if (file_exists(sprintf($definition, $t))) { |
---|
| 350 | $base_url = sprintf($base_url, $t); |
---|
| 351 | return self::smiliesDefinition(sprintf($definition, $t), $base_url); |
---|
| 352 | } |
---|
| 353 | } |
---|
| 354 | return false; |
---|
| 355 | } |
---|
[2566] | 356 | |
---|
[3721] | 357 | public static function smiliesDefinition($f, $url) |
---|
| 358 | { |
---|
| 359 | $def = file($f); |
---|
[2566] | 360 | |
---|
[3721] | 361 | $res = array(); |
---|
| 362 | foreach ($def as $v) { |
---|
| 363 | $v = trim($v); |
---|
| 364 | if (preg_match('|^([^\t]*)[\t]+(.*)$|', $v, $matches)) { |
---|
| 365 | $r = '/(\G|[\s]+|>)(' . preg_quote($matches[1], '/') . ')([\s]+|[<]|\Z)/ms'; |
---|
| 366 | $s = '$1<img src="' . $url . $matches[2] . '" ' . |
---|
| 367 | 'alt="$2" class="smiley" />$3'; |
---|
| 368 | $res[$r] = $s; |
---|
| 369 | } |
---|
| 370 | } |
---|
[2566] | 371 | |
---|
[3721] | 372 | return $res; |
---|
| 373 | } |
---|
[2566] | 374 | |
---|
[3721] | 375 | public static function addSmilies($str) |
---|
| 376 | { |
---|
| 377 | if (!isset($GLOBALS['__smilies']) || !is_array($GLOBALS['__smilies'])) { |
---|
| 378 | return $str; |
---|
| 379 | } |
---|
[975] | 380 | |
---|
[3721] | 381 | # Process part adapted from SmartyPants engine (J. Gruber et al.) : |
---|
[975] | 382 | |
---|
[3721] | 383 | $tokens = self::tokenizeHTML($str); |
---|
| 384 | $result = ''; |
---|
| 385 | $in_pre = 0; # Keep track of when we're inside <pre> or <code> tags. |
---|
[975] | 386 | |
---|
[3721] | 387 | foreach ($tokens as $cur_token) { |
---|
| 388 | if ($cur_token[0] == "tag") { |
---|
| 389 | # Don't mess with quotes inside tags. |
---|
| 390 | $result .= $cur_token[1]; |
---|
| 391 | if (preg_match('@<(/?)(?:pre|code|kbd|script|math)[\s>]@', $cur_token[1], $matches)) { |
---|
| 392 | $in_pre = isset($matches[1]) && $matches[1] == '/' ? 0 : 1; |
---|
| 393 | } |
---|
| 394 | } else { |
---|
| 395 | $t = $cur_token[1]; |
---|
| 396 | if (!$in_pre) { |
---|
| 397 | $t = preg_replace(array_keys($GLOBALS['__smilies']), array_values($GLOBALS['__smilies']), $t); |
---|
| 398 | } |
---|
| 399 | $result .= $t; |
---|
| 400 | } |
---|
| 401 | } |
---|
[975] | 402 | |
---|
[3721] | 403 | return $result; |
---|
| 404 | } |
---|
[937] | 405 | |
---|
[3721] | 406 | private static function tokenizeHTML($str) |
---|
| 407 | { |
---|
| 408 | # Function from SmartyPants engine (J. Gruber et al.) |
---|
| 409 | # |
---|
| 410 | # Parameter: String containing HTML markup. |
---|
| 411 | # Returns: An array of the tokens comprising the input |
---|
| 412 | # string. Each token is either a tag (possibly with nested, |
---|
| 413 | # tags contained therein, such as <a href="<MTFoo>">, or a |
---|
| 414 | # run of text between tags. Each element of the array is a |
---|
| 415 | # two-element array; the first is either 'tag' or 'text'; |
---|
| 416 | # the second is the actual value. |
---|
| 417 | # |
---|
| 418 | # |
---|
| 419 | # Regular expression derived from the _tokenize() subroutine in |
---|
| 420 | # Brad Choate's MTRegex plugin. |
---|
| 421 | # <http://www.bradchoate.com/past/mtregex.php> |
---|
| 422 | # |
---|
| 423 | $index = 0; |
---|
| 424 | $tokens = array(); |
---|
[975] | 425 | |
---|
[3721] | 426 | $match = '(?s:<!(?:--.*?--\s*)+>)|' . # comment |
---|
| 427 | '(?s:<\?.*?\?>)|' . # processing instruction |
---|
| 428 | # regular tags |
---|
| 429 | '(?:<[/!$]?[-a-zA-Z0-9:]+\b(?>[^"\'>]+|"[^"]*"|\'[^\']*\')*>)'; |
---|
[975] | 430 | |
---|
[3721] | 431 | $parts = preg_split("{($match)}", $str, -1, PREG_SPLIT_DELIM_CAPTURE); |
---|
[975] | 432 | |
---|
[3721] | 433 | foreach ($parts as $part) { |
---|
| 434 | if (++$index % 2 && $part != '') { |
---|
| 435 | $tokens[] = array('text', $part); |
---|
| 436 | } else { |
---|
| 437 | $tokens[] = array('tag', $part); |
---|
| 438 | } |
---|
[975] | 439 | |
---|
[3721] | 440 | } |
---|
| 441 | return $tokens; |
---|
| 442 | } |
---|
[975] | 443 | |
---|
[3721] | 444 | # First post image helpers |
---|
| 445 | public static function EntryFirstImageHelper($size, $with_category, $class = "", $no_tag = false, $content_only = false, $cat_only = false) |
---|
| 446 | { |
---|
| 447 | global $core, $_ctx; |
---|
[2566] | 448 | |
---|
[3721] | 449 | try { |
---|
| 450 | $media = new dcMedia($core); |
---|
| 451 | $sizes = implode('|', array_keys($media->thumb_sizes)) . '|o'; |
---|
| 452 | if (!preg_match('/^' . $sizes . '$/', $size)) { |
---|
| 453 | $size = 's'; |
---|
| 454 | } |
---|
| 455 | $p_url = $core->blog->settings->system->public_url; |
---|
| 456 | $p_site = preg_replace('#^(.+?//.+?)/(.*)$#', '$1', $core->blog->url); |
---|
| 457 | $p_root = $core->blog->public_path; |
---|
[2566] | 458 | |
---|
[3721] | 459 | $pattern = '(?:' . preg_quote($p_site, '/') . ')?' . preg_quote($p_url, '/'); |
---|
| 460 | $pattern = sprintf('/<img.+?src="%s(.*?\.(?:jpg|jpeg|gif|png))"[^>]+/msui', $pattern); |
---|
[2566] | 461 | |
---|
[3721] | 462 | $src = ''; |
---|
| 463 | $alt = ''; |
---|
[2566] | 464 | |
---|
[3721] | 465 | # We first look in post content |
---|
| 466 | if (!$cat_only && $_ctx->posts) { |
---|
| 467 | $subject = ($content_only ? '' : $_ctx->posts->post_excerpt_xhtml) . $_ctx->posts->post_content_xhtml; |
---|
| 468 | if (preg_match_all($pattern, $subject, $m) > 0) { |
---|
| 469 | foreach ($m[1] as $i => $img) { |
---|
| 470 | if (($src = self::ContentFirstImageLookup($p_root, $img, $size)) !== false) { |
---|
| 471 | $dirname = str_replace('\\', '/', dirname($img)); |
---|
| 472 | $src = $p_url . ($dirname != '/' ? $dirname : '') . '/' . $src; |
---|
| 473 | if (preg_match('/alt="([^"]+)"/', $m[0][$i], $malt)) { |
---|
| 474 | $alt = $malt[1]; |
---|
| 475 | } |
---|
| 476 | break; |
---|
| 477 | } |
---|
| 478 | } |
---|
| 479 | } |
---|
| 480 | } |
---|
[2566] | 481 | |
---|
[3721] | 482 | # No src, look in category description if available |
---|
| 483 | if (!$src && $with_category && $_ctx->posts->cat_desc) { |
---|
| 484 | if (preg_match_all($pattern, $_ctx->posts->cat_desc, $m) > 0) { |
---|
| 485 | foreach ($m[1] as $i => $img) { |
---|
| 486 | if (($src = self::ContentFirstImageLookup($p_root, $img, $size)) !== false) { |
---|
| 487 | $dirname = str_replace('\\', '/', dirname($img)); |
---|
| 488 | $src = $p_url . ($dirname != '/' ? $dirname : '') . '/' . $src; |
---|
| 489 | if (preg_match('/alt="([^"]+)"/', $m[0][$i], $malt)) { |
---|
| 490 | $alt = $malt[1]; |
---|
| 491 | } |
---|
| 492 | break; |
---|
| 493 | } |
---|
| 494 | } |
---|
| 495 | }; |
---|
| 496 | } |
---|
[2566] | 497 | |
---|
[3721] | 498 | if ($src) { |
---|
| 499 | if ($no_tag) { |
---|
| 500 | return $src; |
---|
| 501 | } else { |
---|
| 502 | return '<img alt="' . $alt . '" src="' . $src . '" class="' . $class . '" />'; |
---|
| 503 | } |
---|
| 504 | } |
---|
[2566] | 505 | |
---|
[3721] | 506 | } catch (Exception $e) { |
---|
| 507 | $core->error->add($e->getMessage()); |
---|
| 508 | } |
---|
| 509 | } |
---|
[2566] | 510 | |
---|
[3721] | 511 | private static function ContentFirstImageLookup($root, $img, $size) |
---|
| 512 | { |
---|
| 513 | global $core; |
---|
[2566] | 514 | |
---|
[3721] | 515 | # Get base name and extension |
---|
| 516 | $info = path::info($img); |
---|
| 517 | $base = $info['base']; |
---|
[2566] | 518 | |
---|
[3721] | 519 | try { |
---|
| 520 | $media = new dcMedia($core); |
---|
| 521 | $sizes = implode('|', array_keys($media->thumb_sizes)); |
---|
| 522 | if (preg_match('/^\.(.+)_(' . $sizes . ')$/', $base, $m)) { |
---|
| 523 | $base = $m[1]; |
---|
| 524 | } |
---|
[2566] | 525 | |
---|
[3721] | 526 | $res = false; |
---|
| 527 | if ($size != 'o' && file_exists($root . '/' . $info['dirname'] . '/.' . $base . '_' . $size . '.jpg')) { |
---|
| 528 | $res = '.' . $base . '_' . $size . '.jpg'; |
---|
| 529 | } elseif ($size != 'o' && file_exists($root . '/' . $info['dirname'] . '/.' . $base . '_' . $size . '.png')) { |
---|
| 530 | $res = '.' . $base . '_' . $size . '.png'; |
---|
| 531 | } else { |
---|
| 532 | $f = $root . '/' . $info['dirname'] . '/' . $base; |
---|
| 533 | if (file_exists($f . '.' . $info['extension'])) { |
---|
| 534 | $res = $base . '.' . $info['extension']; |
---|
| 535 | } elseif (file_exists($f . '.jpg')) { |
---|
| 536 | $res = $base . '.jpg'; |
---|
| 537 | } elseif (file_exists($f . '.jpeg')) { |
---|
| 538 | $res = $base . '.jpeg'; |
---|
| 539 | } elseif (file_exists($f . '.png')) { |
---|
| 540 | $res = $base . '.png'; |
---|
| 541 | } elseif (file_exists($f . '.gif')) { |
---|
| 542 | $res = $base . '.gif'; |
---|
| 543 | } elseif (file_exists($f . '.JPG')) { |
---|
| 544 | $res = $base . '.JPG'; |
---|
| 545 | } elseif (file_exists($f . '.JPEG')) { |
---|
| 546 | $res = $base . '.JPEG'; |
---|
| 547 | } elseif (file_exists($f . '.PNG')) { |
---|
| 548 | $res = $base . '.PNG'; |
---|
| 549 | } elseif (file_exists($f . '.GIF')) { |
---|
| 550 | $res = $base . '.GIF'; |
---|
| 551 | } |
---|
| 552 | } |
---|
| 553 | } catch (Exception $e) { |
---|
| 554 | $core->error->add($e->getMessage()); |
---|
| 555 | } |
---|
[2566] | 556 | |
---|
[3721] | 557 | if ($res) { |
---|
| 558 | return $res; |
---|
| 559 | } |
---|
| 560 | return false; |
---|
| 561 | } |
---|
[0] | 562 | } |
---|