| 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 | # Static methods for pagination |
|---|
| 165 | public static function PaginationNbPages() |
|---|
| 166 | { |
|---|
| 167 | global $_ctx; |
|---|
| 168 | |
|---|
| 169 | if ($_ctx->pagination === null) { |
|---|
| 170 | return false; |
|---|
| 171 | } |
|---|
| 172 | |
|---|
| 173 | $nb_posts = $_ctx->pagination->f(0); |
|---|
| 174 | $nb_per_page = $_ctx->post_params['limit'][1]; |
|---|
| 175 | |
|---|
| 176 | $nb_pages = ceil($nb_posts/$nb_per_page); |
|---|
| 177 | |
|---|
| 178 | return $nb_pages; |
|---|
| 179 | } |
|---|
| 180 | |
|---|
| 181 | public static function PaginationPosition($offset=0) |
|---|
| 182 | { |
|---|
| 183 | if (isset($GLOBALS['_page_number'])) { |
|---|
| 184 | $p = $GLOBALS['_page_number']; |
|---|
| 185 | } else { |
|---|
| 186 | $p = 1; |
|---|
| 187 | } |
|---|
| 188 | |
|---|
| 189 | $p = $p+$offset; |
|---|
| 190 | |
|---|
| 191 | $n = self::PaginationNbPages(); |
|---|
| 192 | if (!$n) { |
|---|
| 193 | return $p; |
|---|
| 194 | } |
|---|
| 195 | |
|---|
| 196 | if ($p > $n || $p <= 0) { |
|---|
| 197 | return 1; |
|---|
| 198 | } else { |
|---|
| 199 | return $p; |
|---|
| 200 | } |
|---|
| 201 | } |
|---|
| 202 | |
|---|
| 203 | public static function PaginationStart() |
|---|
| 204 | { |
|---|
| 205 | if (isset($GLOBALS['_page_number'])) { |
|---|
| 206 | return self::PaginationPosition() == 1; |
|---|
| 207 | } |
|---|
| 208 | |
|---|
| 209 | return true; |
|---|
| 210 | } |
|---|
| 211 | |
|---|
| 212 | public static function PaginationEnd() |
|---|
| 213 | { |
|---|
| 214 | if (isset($GLOBALS['_page_number'])) { |
|---|
| 215 | return self::PaginationPosition() == self::PaginationNbPages(); |
|---|
| 216 | } |
|---|
| 217 | |
|---|
| 218 | return false; |
|---|
| 219 | } |
|---|
| 220 | |
|---|
| 221 | public static function PaginationURL($offset=0) |
|---|
| 222 | { |
|---|
| 223 | $args = $_SERVER['URL_REQUEST_PART']; |
|---|
| 224 | |
|---|
| 225 | $n = self::PaginationPosition($offset); |
|---|
| 226 | |
|---|
| 227 | $args = preg_replace('#(^|/)page/([0-9]+)$#','',$args); |
|---|
| 228 | |
|---|
| 229 | $url = $GLOBALS['core']->blog->url.$args; |
|---|
| 230 | |
|---|
| 231 | if ($n > 1) { |
|---|
| 232 | $url = preg_replace('#/$#','',$url); |
|---|
| 233 | $url .= '/page/'.$n; |
|---|
| 234 | } |
|---|
| 235 | |
|---|
| 236 | # If search param |
|---|
| 237 | if (!empty($_GET['q'])) { |
|---|
| 238 | $s = strpos($url,'?') !== false ? '&' : '?'; |
|---|
| 239 | $url .= $s.'q='.rawurlencode($_GET['q']); |
|---|
| 240 | } |
|---|
| 241 | return $url; |
|---|
| 242 | } |
|---|
| 243 | |
|---|
| 244 | # Robots policy |
|---|
| 245 | public static function robotsPolicy($base,$over) |
|---|
| 246 | { |
|---|
| 247 | $pol = array('INDEX' => 'INDEX','FOLLOW' => 'FOLLOW', 'ARCHIVE' => 'ARCHIVE'); |
|---|
| 248 | $base = array_flip(preg_split('/\s*,\s*/',$base)); |
|---|
| 249 | $over = array_flip(preg_split('/\s*,\s*/',$over)); |
|---|
| 250 | |
|---|
| 251 | foreach ($pol as $k => &$v) |
|---|
| 252 | { |
|---|
| 253 | if (isset($base[$k]) || isset($base['NO'.$k])) { |
|---|
| 254 | $v = isset($base['NO'.$k]) ? 'NO'.$k : $k; |
|---|
| 255 | } |
|---|
| 256 | if (isset($over[$k]) || isset($over['NO'.$k])) { |
|---|
| 257 | $v = isset($over['NO'.$k]) ? 'NO'.$k : $k; |
|---|
| 258 | } |
|---|
| 259 | } |
|---|
| 260 | |
|---|
| 261 | if ($pol['ARCHIVE'] == 'ARCHIVE') { |
|---|
| 262 | unset($pol['ARCHIVE']); |
|---|
| 263 | } |
|---|
| 264 | |
|---|
| 265 | return implode(', ',$pol); |
|---|
| 266 | } |
|---|
| 267 | |
|---|
| 268 | # Smilies static methods |
|---|
| 269 | public static function getSmilies($blog) |
|---|
| 270 | { |
|---|
| 271 | $path = array(); |
|---|
| 272 | if (isset($GLOBALS['__theme'])) { |
|---|
| 273 | $path[] = $GLOBALS['__theme']; |
|---|
| 274 | } |
|---|
| 275 | $path[] = 'default'; |
|---|
| 276 | $definition = $blog->themes_path.'/%s/smilies/smilies.txt'; |
|---|
| 277 | $base_url = $blog->settings->system->themes_url.'/%s/smilies/'; |
|---|
| 278 | |
|---|
| 279 | $res = array(); |
|---|
| 280 | |
|---|
| 281 | foreach ($path as $t) |
|---|
| 282 | { |
|---|
| 283 | if (file_exists(sprintf($definition,$t))) { |
|---|
| 284 | $base_url = sprintf($base_url,$t); |
|---|
| 285 | return self::smiliesDefinition(sprintf($definition,$t),$base_url); |
|---|
| 286 | } |
|---|
| 287 | } |
|---|
| 288 | return false; |
|---|
| 289 | } |
|---|
| 290 | |
|---|
| 291 | public static function smiliesDefinition($f,$url) |
|---|
| 292 | { |
|---|
| 293 | $def = file($f); |
|---|
| 294 | |
|---|
| 295 | $res = array(); |
|---|
| 296 | foreach($def as $v) |
|---|
| 297 | { |
|---|
| 298 | $v = trim($v); |
|---|
| 299 | if (preg_match('|^([^\t]*)[\t]+(.*)$|',$v,$matches)) |
|---|
| 300 | { |
|---|
| 301 | $r = '/(\A|[\s]+|>)('.preg_quote($matches[1],'/').')([\s]+|[<]|\Z)/ms'; |
|---|
| 302 | $s = '$1<img src="'.$url.$matches[2].'" '. |
|---|
| 303 | 'alt="$2" class="smiley" />$3'; |
|---|
| 304 | $res[$r] = $s; |
|---|
| 305 | } |
|---|
| 306 | } |
|---|
| 307 | |
|---|
| 308 | return $res; |
|---|
| 309 | } |
|---|
| 310 | |
|---|
| 311 | public static function addSmilies($str) |
|---|
| 312 | { |
|---|
| 313 | if (!isset($GLOBALS['__smilies']) || !is_array($GLOBALS['__smilies'])) { |
|---|
| 314 | return $str; |
|---|
| 315 | } |
|---|
| 316 | |
|---|
| 317 | return preg_replace(array_keys($GLOBALS['__smilies']),array_values($GLOBALS['__smilies']),$str); |
|---|
| 318 | } |
|---|
| 319 | |
|---|
| 320 | } |
|---|
| 321 | ?> |
|---|