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