[0] | 1 | <?php |
---|
| 2 | # -- BEGIN LICENSE BLOCK --------------------------------------- |
---|
| 3 | # This file is part of Dotclear 2. |
---|
| 4 | # |
---|
[1179] | 5 | # Copyright (c) 2003-2013 Olivier Meunier & Association Dotclear |
---|
[0] | 6 | # Licensed under the GPL version 2.0 license. |
---|
| 7 | # See LICENSE file or |
---|
| 8 | # http://www.gnu.org/licenses/old-licenses/gpl-2.0.html |
---|
| 9 | # |
---|
| 10 | # -- END LICENSE BLOCK ----------------------------------------- |
---|
| 11 | if (!defined('DC_RC_PATH')) { return; } |
---|
| 12 | |
---|
| 13 | /** |
---|
| 14 | @ingroup DC_CORE |
---|
| 15 | @brief Dotclear post record helpers. |
---|
| 16 | |
---|
| 17 | This class adds new methods to database post results. |
---|
| 18 | You can call them on every record comming from dcBlog::getPosts and similar |
---|
| 19 | methods. |
---|
| 20 | |
---|
| 21 | @warning You should not give the first argument (usualy $rs) of every described |
---|
| 22 | function. |
---|
| 23 | */ |
---|
| 24 | class rsExtPost |
---|
| 25 | { |
---|
| 26 | /** |
---|
| 27 | Returns whether post is editable. |
---|
[2566] | 28 | |
---|
[0] | 29 | @param rs Invisible parameter |
---|
| 30 | @return <b>boolean</b> |
---|
| 31 | */ |
---|
| 32 | public static function isEditable($rs) |
---|
| 33 | { |
---|
| 34 | # If user is admin or contentadmin, true |
---|
| 35 | if ($rs->core->auth->check('contentadmin',$rs->core->blog->id)) { |
---|
| 36 | return true; |
---|
| 37 | } |
---|
[2566] | 38 | |
---|
[0] | 39 | # No user id in result ? false |
---|
| 40 | if (!$rs->exists('user_id')) { |
---|
| 41 | return false; |
---|
| 42 | } |
---|
[2566] | 43 | |
---|
[0] | 44 | # If user is usage and owner of the entrie |
---|
| 45 | if ($rs->core->auth->check('usage',$rs->core->blog->id) |
---|
| 46 | && $rs->user_id == $rs->core->auth->userID()) { |
---|
| 47 | return true; |
---|
| 48 | } |
---|
[2566] | 49 | |
---|
[0] | 50 | return false; |
---|
| 51 | } |
---|
[2566] | 52 | |
---|
[0] | 53 | /** |
---|
| 54 | Returns whether post is deletable |
---|
[2566] | 55 | |
---|
[0] | 56 | @param rs Invisible parameter |
---|
| 57 | @return <b>boolean</b> |
---|
| 58 | */ |
---|
| 59 | public static function isDeletable($rs) |
---|
| 60 | { |
---|
| 61 | # If user is admin, or contentadmin, true |
---|
| 62 | if ($rs->core->auth->check('contentadmin',$rs->core->blog->id)) { |
---|
| 63 | return true; |
---|
| 64 | } |
---|
[2566] | 65 | |
---|
[0] | 66 | # No user id in result ? false |
---|
| 67 | if (!$rs->exists('user_id')) { |
---|
| 68 | return false; |
---|
| 69 | } |
---|
[2566] | 70 | |
---|
[0] | 71 | # If user has delete rights and is owner of the entrie |
---|
| 72 | if ($rs->core->auth->check('delete',$rs->core->blog->id) |
---|
| 73 | && $rs->user_id == $rs->core->auth->userID()) { |
---|
| 74 | return true; |
---|
| 75 | } |
---|
[2566] | 76 | |
---|
[0] | 77 | return false; |
---|
| 78 | } |
---|
[2566] | 79 | |
---|
[0] | 80 | /** |
---|
| 81 | Returns whether post is the first one of its day. |
---|
[2566] | 82 | |
---|
[0] | 83 | @param rs Invisible parameter |
---|
| 84 | @return <b>boolean</b> |
---|
| 85 | */ |
---|
| 86 | public static function firstPostOfDay($rs) |
---|
| 87 | { |
---|
| 88 | if ($rs->isStart()) { |
---|
| 89 | return true; |
---|
| 90 | } |
---|
[2566] | 91 | |
---|
[0] | 92 | $cdate = date('Ymd',strtotime($rs->post_dt)); |
---|
| 93 | $rs->movePrev(); |
---|
| 94 | $ndate = date('Ymd',strtotime($rs->post_dt)); |
---|
| 95 | $rs->moveNext(); |
---|
| 96 | return $ndate != $cdate; |
---|
| 97 | } |
---|
[2566] | 98 | |
---|
[0] | 99 | /** |
---|
| 100 | Returns whether post is the last one of its day. |
---|
[2566] | 101 | |
---|
[0] | 102 | @param rs Invisible parameter |
---|
| 103 | @return <b>boolean</b> |
---|
| 104 | */ |
---|
| 105 | public static function lastPostOfDay($rs) |
---|
| 106 | { |
---|
| 107 | if ($rs->isEnd()) { |
---|
| 108 | return true; |
---|
| 109 | } |
---|
[2566] | 110 | |
---|
[0] | 111 | $cdate = date('Ymd',strtotime($rs->post_dt)); |
---|
| 112 | $rs->moveNext(); |
---|
| 113 | $ndate = date('Ymd',strtotime($rs->post_dt)); |
---|
| 114 | $rs->movePrev(); |
---|
| 115 | return $ndate != $cdate; |
---|
| 116 | } |
---|
[2566] | 117 | |
---|
[0] | 118 | /** |
---|
| 119 | Returns whether comments are enabled on post. |
---|
[2566] | 120 | |
---|
[0] | 121 | @param rs Invisible parameter |
---|
| 122 | @return <b>boolean</b> |
---|
| 123 | */ |
---|
| 124 | public static function commentsActive($rs) |
---|
| 125 | { |
---|
| 126 | return |
---|
| 127 | $rs->core->blog->settings->system->allow_comments |
---|
| 128 | && $rs->post_open_comment |
---|
| 129 | && ($rs->core->blog->settings->system->comments_ttl == 0 || |
---|
| 130 | time()-($rs->core->blog->settings->system->comments_ttl*86400) < $rs->getTS()); |
---|
| 131 | } |
---|
[2566] | 132 | |
---|
[0] | 133 | /** |
---|
| 134 | Returns whether trackbacks are enabled on post. |
---|
[2566] | 135 | |
---|
[0] | 136 | @param rs Invisible parameter |
---|
| 137 | @return <b>boolean</b> |
---|
| 138 | */ |
---|
| 139 | public static function trackbacksActive($rs) |
---|
| 140 | { |
---|
| 141 | return |
---|
| 142 | $rs->core->blog->settings->system->allow_trackbacks |
---|
| 143 | && $rs->post_open_tb |
---|
| 144 | && ($rs->core->blog->settings->system->trackbacks_ttl == 0 || |
---|
| 145 | time()-($rs->core->blog->settings->system->trackbacks_ttl*86400) < $rs->getTS()); |
---|
| 146 | } |
---|
[2566] | 147 | |
---|
[0] | 148 | /** |
---|
| 149 | Returns whether post has at least one comment. |
---|
[2566] | 150 | |
---|
[0] | 151 | @param rs Invisible parameter |
---|
| 152 | @return <b>boolean</b> |
---|
| 153 | */ |
---|
| 154 | public static function hasComments($rs) |
---|
| 155 | { |
---|
| 156 | return $rs->nb_comment > 0; |
---|
| 157 | } |
---|
[2566] | 158 | |
---|
[0] | 159 | /** |
---|
| 160 | Returns whether post has at least one trackbacks. |
---|
[2566] | 161 | |
---|
[0] | 162 | @return <b>boolean</b> |
---|
| 163 | */ |
---|
| 164 | public static function hasTrackbacks($rs) |
---|
| 165 | { |
---|
| 166 | return $rs->nb_trackback > 0; |
---|
| 167 | } |
---|
[912] | 168 | |
---|
| 169 | /** |
---|
| 170 | Returns whether post has been updated since publication. |
---|
| 171 | |
---|
| 172 | @return <b>boolean</b> |
---|
| 173 | */ |
---|
| 174 | public static function isRepublished($rs) |
---|
| 175 | { |
---|
| 176 | return ($rs->getTS('upddt') + dt::getTimeOffset($rs->post_tz)) > $rs->getTS(); |
---|
| 177 | } |
---|
[2566] | 178 | |
---|
[0] | 179 | /** |
---|
| 180 | Returns full post URL. |
---|
[2566] | 181 | |
---|
[0] | 182 | @param rs Invisible parameter |
---|
| 183 | @return <b>string</b> |
---|
| 184 | */ |
---|
| 185 | public static function getURL($rs) |
---|
| 186 | { |
---|
| 187 | return $rs->core->blog->url.$rs->core->getPostPublicURL( |
---|
| 188 | $rs->post_type,html::sanitizeURL($rs->post_url) |
---|
| 189 | ); |
---|
| 190 | } |
---|
[2566] | 191 | |
---|
[0] | 192 | /** |
---|
| 193 | Returns full post category URL. |
---|
[2566] | 194 | |
---|
[0] | 195 | @param rs Invisible parameter |
---|
| 196 | @return <b>string</b> |
---|
| 197 | */ |
---|
| 198 | public static function getCategoryURL($rs) |
---|
| 199 | { |
---|
[776] | 200 | return $rs->core->blog->url.$rs->core->url->getURLFor('category',html::sanitizeURL($rs->cat_url)); |
---|
[0] | 201 | } |
---|
[2566] | 202 | |
---|
[0] | 203 | /** |
---|
| 204 | Returns whether post has an excerpt. |
---|
[2566] | 205 | |
---|
[0] | 206 | @param rs Invisible parameter |
---|
| 207 | @return <b>boolean</b> |
---|
| 208 | */ |
---|
| 209 | public static function isExtended($rs) |
---|
| 210 | { |
---|
| 211 | return $rs->post_excerpt_xhtml != ''; |
---|
| 212 | } |
---|
[2566] | 213 | |
---|
[0] | 214 | /** |
---|
| 215 | Returns post timestamp. |
---|
[2566] | 216 | |
---|
[0] | 217 | @param rs Invisible parameter |
---|
| 218 | @param type <b>string</b> (dt|upddt|creadt) defaults to post_dt |
---|
| 219 | @return <b>integer</b> |
---|
| 220 | */ |
---|
| 221 | public static function getTS($rs,$type='') |
---|
| 222 | { |
---|
| 223 | if ($type == 'upddt') { |
---|
| 224 | return strtotime($rs->post_upddt); |
---|
| 225 | } elseif ($type == 'creadt') { |
---|
| 226 | return strtotime($rs->post_creadt); |
---|
| 227 | } else { |
---|
| 228 | return strtotime($rs->post_dt); |
---|
| 229 | } |
---|
| 230 | } |
---|
[2566] | 231 | |
---|
[0] | 232 | /** |
---|
| 233 | Returns post date formating according to the ISO 8601 standard. |
---|
[2566] | 234 | |
---|
[0] | 235 | @param rs Invisible parameter |
---|
| 236 | @param type <b>string</b> (dt|upddt|creadt) defaults to post_dt |
---|
| 237 | @return <b>string</b> |
---|
| 238 | */ |
---|
| 239 | public static function getISO8601Date($rs,$type='') |
---|
| 240 | { |
---|
| 241 | if ($type == 'upddt' || $type == 'creadt') { |
---|
| 242 | return dt::iso8601($rs->getTS($type)+dt::getTimeOffset($rs->post_tz),$rs->post_tz); |
---|
| 243 | } else { |
---|
| 244 | return dt::iso8601($rs->getTS(),$rs->post_tz); |
---|
| 245 | } |
---|
| 246 | } |
---|
[2566] | 247 | |
---|
[0] | 248 | /** |
---|
| 249 | Returns post date formating according to RFC 822. |
---|
[2566] | 250 | |
---|
[0] | 251 | @param rs Invisible parameter |
---|
| 252 | @param type <b>string</b> (dt|upddt|creadt) defaults to post_dt |
---|
| 253 | @return <b>string</b> |
---|
| 254 | */ |
---|
| 255 | public static function getRFC822Date($rs,$type='') |
---|
| 256 | { |
---|
| 257 | if ($type == 'upddt' || $type == 'creadt') { |
---|
| 258 | return dt::rfc822($rs->getTS($type)+dt::getTimeOffset($rs->post_tz),$rs->post_tz); |
---|
| 259 | } else { |
---|
| 260 | return dt::rfc822($rs->getTS($type),$rs->post_tz); |
---|
| 261 | } |
---|
| 262 | } |
---|
[2566] | 263 | |
---|
[0] | 264 | /** |
---|
| 265 | Returns post date with <var>$format</var> as formatting pattern. If format |
---|
| 266 | is empty, uses <var>date_format</var> blog setting. |
---|
[2566] | 267 | |
---|
[0] | 268 | @param rs Invisible parameter |
---|
| 269 | @param format <b>string</b> Date format pattern |
---|
| 270 | @param type <b>string</b> (dt|upddt|creadt) defaults to post_dt |
---|
| 271 | @return <b>string</b> |
---|
| 272 | */ |
---|
| 273 | public static function getDate($rs,$format,$type='') |
---|
| 274 | { |
---|
| 275 | if (!$format) { |
---|
| 276 | $format = $rs->core->blog->settings->system->date_format; |
---|
| 277 | } |
---|
[2566] | 278 | |
---|
[0] | 279 | if ($type == 'upddt') { |
---|
| 280 | return dt::dt2str($format,$rs->post_upddt,$rs->post_tz); |
---|
| 281 | } elseif ($type == 'creadt') { |
---|
| 282 | return dt::dt2str($format,$rs->post_creadt,$rs->post_tz); |
---|
| 283 | } else { |
---|
| 284 | return dt::dt2str($format,$rs->post_dt); |
---|
| 285 | } |
---|
| 286 | } |
---|
[2566] | 287 | |
---|
[0] | 288 | /** |
---|
| 289 | Returns post time with <var>$format</var> as formatting pattern. If format |
---|
| 290 | is empty, uses <var>time_format</var> blog setting. |
---|
[2566] | 291 | |
---|
[0] | 292 | @param rs Invisible parameter |
---|
| 293 | @param format <b>string</b> Time format pattern |
---|
| 294 | @param type <b>string</b> (dt|upddt|creadt) defaults to post_dt |
---|
| 295 | @return <b>string</b> |
---|
| 296 | */ |
---|
| 297 | public static function getTime($rs,$format,$type='') |
---|
| 298 | { |
---|
| 299 | if (!$format) { |
---|
| 300 | $format = $rs->core->blog->settings->system->time_format; |
---|
| 301 | } |
---|
[2566] | 302 | |
---|
[0] | 303 | if ($type == 'upddt') { |
---|
| 304 | return dt::dt2str($format,$rs->post_upddt,$rs->post_tz); |
---|
| 305 | } elseif ($type == 'creadt') { |
---|
| 306 | return dt::dt2str($format,$rs->post_creadt,$rs->post_tz); |
---|
| 307 | } else { |
---|
| 308 | return dt::dt2str($format,$rs->post_dt); |
---|
| 309 | } |
---|
| 310 | } |
---|
[2566] | 311 | |
---|
[0] | 312 | /** |
---|
| 313 | Returns author common name using user_id, user_name, user_firstname and |
---|
| 314 | user_displayname fields. |
---|
[2566] | 315 | |
---|
[0] | 316 | @param rs Invisible parameter |
---|
| 317 | @return <b>string</b> |
---|
| 318 | */ |
---|
| 319 | public static function getAuthorCN($rs) |
---|
| 320 | { |
---|
| 321 | return dcUtils::getUserCN($rs->user_id, $rs->user_name, |
---|
| 322 | $rs->user_firstname, $rs->user_displayname); |
---|
| 323 | } |
---|
[2566] | 324 | |
---|
[0] | 325 | /** |
---|
| 326 | Returns author common name with a link if he specified one in its |
---|
| 327 | preferences. |
---|
[2566] | 328 | |
---|
[0] | 329 | @param rs Invisible parameter |
---|
| 330 | @return <b>string</b> |
---|
| 331 | */ |
---|
| 332 | public static function getAuthorLink($rs) |
---|
| 333 | { |
---|
| 334 | $res = '%1$s'; |
---|
| 335 | $url = $rs->user_url; |
---|
| 336 | if ($url) { |
---|
| 337 | $res = '<a href="%2$s">%1$s</a>'; |
---|
| 338 | } |
---|
[2566] | 339 | |
---|
[0] | 340 | return sprintf($res,html::escapeHTML($rs->getAuthorCN()),html::escapeHTML($url)); |
---|
| 341 | } |
---|
[2566] | 342 | |
---|
[0] | 343 | /** |
---|
| 344 | Returns author e-mail address. If <var>$encoded</var> is true, "@" sign is |
---|
| 345 | replaced by "%40" and "." by "%2e". |
---|
[2566] | 346 | |
---|
[0] | 347 | @param rs Invisible parameter |
---|
| 348 | @param encoded <b>boolean</b> Encode address. |
---|
| 349 | @return <b>string</b> |
---|
| 350 | */ |
---|
| 351 | public static function getAuthorEmail($rs,$encoded=true) |
---|
| 352 | { |
---|
| 353 | if ($encoded) { |
---|
| 354 | return strtr($rs->user_email,array('@'=>'%40','.'=>'%2e')); |
---|
| 355 | } |
---|
| 356 | return $rs->user_email; |
---|
| 357 | } |
---|
[2566] | 358 | |
---|
[0] | 359 | /** |
---|
| 360 | Returns post feed unique ID. |
---|
[2566] | 361 | |
---|
[0] | 362 | @param rs Invisible parameter |
---|
| 363 | @return <b>string</b> |
---|
| 364 | */ |
---|
| 365 | public static function getFeedID($rs) |
---|
| 366 | { |
---|
| 367 | return 'urn:md5:'.md5($rs->core->blog->uid.$rs->post_id); |
---|
[2566] | 368 | |
---|
[0] | 369 | $url = parse_url($rs->core->blog->url); |
---|
| 370 | $date_part = date('Y-m-d',strtotime($rs->post_creadt)); |
---|
[2566] | 371 | |
---|
[0] | 372 | return 'tag:'.$url['host'].','.$date_part.':'.$rs->post_id; |
---|
| 373 | } |
---|
[2566] | 374 | |
---|
[0] | 375 | /** |
---|
| 376 | Returns trackback RDF information block in HTML comment. |
---|
[2566] | 377 | |
---|
[0] | 378 | @param rs Invisible parameter |
---|
| 379 | @return <b>string</b> |
---|
| 380 | */ |
---|
| 381 | public static function getTrackbackData($rs) |
---|
| 382 | { |
---|
| 383 | return |
---|
| 384 | "<![CDATA[>\n". |
---|
| 385 | "<!--[\n". |
---|
| 386 | '<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"'."\n". |
---|
| 387 | ' xmlns:dc="http://purl.org/dc/elements/1.1/"'."\n". |
---|
| 388 | ' xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">'."\n". |
---|
| 389 | "<rdf:Description\n". |
---|
| 390 | ' rdf:about="'.$rs->getURL().'"'."\n". |
---|
| 391 | ' dc:identifier="'.$rs->getURL().'"'."\n". |
---|
| 392 | ' dc:title="'.htmlspecialchars($rs->post_title,ENT_COMPAT,'UTF-8').'"'."\n". |
---|
| 393 | ' trackback:ping="'.$rs->getTrackbackLink().'" />'."\n". |
---|
| 394 | "</rdf:RDF>\n". |
---|
| 395 | "<!]]><!---->\n"; |
---|
| 396 | } |
---|
[2566] | 397 | |
---|
[0] | 398 | /** |
---|
| 399 | Returns post trackback full URL. |
---|
[2566] | 400 | |
---|
[0] | 401 | @param rs Invisible parameter |
---|
| 402 | @return <b>string</b> |
---|
| 403 | */ |
---|
| 404 | public static function getTrackbackLink($rs) |
---|
| 405 | { |
---|
[776] | 406 | return $rs->core->blog->url.$rs->core->url->getURLFor('trackback',$rs->post_id); |
---|
[0] | 407 | } |
---|
[2566] | 408 | |
---|
[0] | 409 | /** |
---|
| 410 | Returns post content. If <var>$absolute_urls</var> is true, appends full |
---|
| 411 | blog URL to each relative post URLs. |
---|
[2566] | 412 | |
---|
[0] | 413 | @param rs Invisible parameter |
---|
| 414 | @param absolute_urls <b>boolean</b> With absolute URLs |
---|
| 415 | @return <b>string</b> |
---|
| 416 | */ |
---|
| 417 | public static function getContent($rs,$absolute_urls=false) |
---|
| 418 | { |
---|
| 419 | if ($absolute_urls) { |
---|
| 420 | return html::absoluteURLs($rs->post_content_xhtml,$rs->getURL()); |
---|
| 421 | } else { |
---|
| 422 | return $rs->post_content_xhtml; |
---|
| 423 | } |
---|
| 424 | } |
---|
[2566] | 425 | |
---|
[0] | 426 | /** |
---|
| 427 | Returns post excerpt. If <var>$absolute_urls</var> is true, appends full |
---|
| 428 | blog URL to each relative post URLs. |
---|
[2566] | 429 | |
---|
[0] | 430 | @param rs Invisible parameter |
---|
| 431 | @param absolute_urls <b>boolean</b> With absolute URLs |
---|
| 432 | @return <b>string</b> |
---|
| 433 | */ |
---|
| 434 | public static function getExcerpt($rs,$absolute_urls=false) |
---|
| 435 | { |
---|
| 436 | if ($absolute_urls) { |
---|
| 437 | return html::absoluteURLs($rs->post_excerpt_xhtml,$rs->getURL()); |
---|
| 438 | } else { |
---|
| 439 | return $rs->post_excerpt_xhtml; |
---|
| 440 | } |
---|
| 441 | } |
---|
[2566] | 442 | |
---|
[0] | 443 | /** |
---|
| 444 | Returns post media count using a subquery. |
---|
[2566] | 445 | |
---|
[0] | 446 | @param rs Invisible parameter |
---|
| 447 | @return <b>integer</b> |
---|
| 448 | */ |
---|
| 449 | public static function countMedia($rs) |
---|
| 450 | { |
---|
| 451 | if (isset($rs->_nb_media[$rs->index()])) |
---|
| 452 | { |
---|
| 453 | return $rs->_nb_media[$rs->index()]; |
---|
| 454 | } |
---|
| 455 | else |
---|
| 456 | { |
---|
| 457 | $strReq = |
---|
| 458 | 'SELECT count(media_id) '. |
---|
| 459 | 'FROM '.$rs->core->prefix.'post_media '. |
---|
| 460 | 'WHERE post_id = '.(integer) $rs->post_id.' '; |
---|
[2566] | 461 | |
---|
[0] | 462 | $res = (integer) $rs->core->con->select($strReq)->f(0); |
---|
| 463 | $rs->_nb_media[$rs->index()] = $res; |
---|
| 464 | return $res; |
---|
| 465 | } |
---|
| 466 | } |
---|
| 467 | } |
---|
| 468 | |
---|
| 469 | /** |
---|
| 470 | @ingroup DC_CORE |
---|
| 471 | @brief Dotclear comment record helpers. |
---|
| 472 | |
---|
| 473 | This class adds new methods to database comment results. |
---|
| 474 | You can call them on every record comming from dcBlog::getComments and similar |
---|
| 475 | methods. |
---|
| 476 | |
---|
| 477 | @warning You should not give the first argument (usualy $rs) of every described |
---|
| 478 | function. |
---|
| 479 | */ |
---|
| 480 | class rsExtComment |
---|
| 481 | { |
---|
| 482 | /** |
---|
| 483 | Returns comment date with <var>$format</var> as formatting pattern. If |
---|
| 484 | format is empty, uses <var>date_format</var> blog setting. |
---|
[2566] | 485 | |
---|
[0] | 486 | @param rs Invisible parameter |
---|
| 487 | @param format <b>string</b> Date format pattern |
---|
| 488 | @param type <b>string</b> (dt|upddt) defaults to comment_dt |
---|
| 489 | @return <b>string</b> |
---|
| 490 | */ |
---|
| 491 | public static function getDate($rs,$format,$type='') |
---|
| 492 | { |
---|
| 493 | if (!$format) { |
---|
| 494 | $format = $rs->core->blog->settings->system->date_format; |
---|
| 495 | } |
---|
[2566] | 496 | |
---|
[0] | 497 | if ($type == 'upddt') { |
---|
| 498 | return dt::dt2str($format,$rs->comment_upddt,$rs->comment_tz); |
---|
| 499 | } else { |
---|
| 500 | return dt::dt2str($format,$rs->comment_dt); |
---|
| 501 | } |
---|
| 502 | } |
---|
[2566] | 503 | |
---|
[0] | 504 | /** |
---|
| 505 | Returns comment time with <var>$format</var> as formatting pattern. If |
---|
| 506 | format is empty, uses <var>time_format</var> blog setting. |
---|
[2566] | 507 | |
---|
[0] | 508 | @param rs Invisible parameter |
---|
| 509 | @param format <b>string</b> Date format pattern |
---|
| 510 | @param type <b>string</b> (dt|upddt) defaults to comment_dt |
---|
| 511 | @return <b>string</b> |
---|
| 512 | */ |
---|
| 513 | public static function getTime($rs,$format,$type='') |
---|
| 514 | { |
---|
| 515 | if (!$format) { |
---|
| 516 | $format = $rs->core->blog->settings->system->time_format; |
---|
| 517 | } |
---|
[2566] | 518 | |
---|
[0] | 519 | if ($type == 'upddt') { |
---|
| 520 | return dt::dt2str($format,$rs->comment_updt,$rs->comment_tz); |
---|
| 521 | } else { |
---|
| 522 | return dt::dt2str($format,$rs->comment_dt); |
---|
| 523 | } |
---|
| 524 | } |
---|
[2566] | 525 | |
---|
[0] | 526 | /** |
---|
| 527 | Returns comment timestamp. |
---|
[2566] | 528 | |
---|
[0] | 529 | @param rs Invisible parameter |
---|
| 530 | @param type <b>string</b> (dt|upddt) defaults to comment_dt |
---|
| 531 | @return <b>integer</b> |
---|
| 532 | */ |
---|
| 533 | public static function getTS($rs,$type='') |
---|
| 534 | { |
---|
| 535 | if ($type == 'upddt') { |
---|
| 536 | return strtotime($rs->comment_upddt); |
---|
| 537 | } else { |
---|
| 538 | return strtotime($rs->comment_dt); |
---|
| 539 | } |
---|
| 540 | } |
---|
[2566] | 541 | |
---|
[0] | 542 | /** |
---|
| 543 | Returns comment date formating according to the ISO 8601 standard. |
---|
[2566] | 544 | |
---|
[0] | 545 | @param rs Invisible parameter |
---|
| 546 | @param type <b>string</b> (dt|upddt) defaults to comment_dt |
---|
| 547 | @return <b>string</b> |
---|
| 548 | */ |
---|
| 549 | public static function getISO8601Date($rs,$type='') |
---|
| 550 | { |
---|
| 551 | if ($type == 'upddt') { |
---|
| 552 | return dt::iso8601($rs->getTS($type)+dt::getTimeOffset($rs->comment_tz),$rs->comment_tz); |
---|
| 553 | } else { |
---|
| 554 | return dt::iso8601($rs->getTS(),$rs->comment_tz); |
---|
| 555 | } |
---|
| 556 | } |
---|
[2566] | 557 | |
---|
[0] | 558 | /** |
---|
| 559 | Returns comment date formating according to RFC 822. |
---|
[2566] | 560 | |
---|
[0] | 561 | @param rs Invisible parameter |
---|
| 562 | @param type <b>string</b> (dt|upddt) defaults to comment_dt |
---|
| 563 | @return <b>string</b> |
---|
| 564 | */ |
---|
| 565 | public static function getRFC822Date($rs,$type='') |
---|
| 566 | { |
---|
| 567 | if ($type == 'upddt') { |
---|
| 568 | return dt::rfc822($rs->getTS($type)+dt::getTimeOffset($rs->comment_tz),$rs->comment_tz); |
---|
| 569 | } else { |
---|
| 570 | return dt::rfc822($rs->getTS(),$rs->comment_tz); |
---|
| 571 | } |
---|
| 572 | } |
---|
[2566] | 573 | |
---|
[0] | 574 | /** |
---|
| 575 | Returns comment content. If <var>$absolute_urls</var> is true, appends full |
---|
| 576 | blog URL to each relative post URLs. |
---|
[2566] | 577 | |
---|
[0] | 578 | @param rs Invisible parameter |
---|
| 579 | @param absolute_urls <b>boolean</b> With absolute URLs |
---|
| 580 | @return <b>string</b> |
---|
| 581 | */ |
---|
| 582 | public static function getContent($rs,$absolute_urls=false) |
---|
| 583 | { |
---|
| 584 | $res = $rs->comment_content; |
---|
[2566] | 585 | |
---|
[0] | 586 | if ($rs->core->blog->settings->system->comments_nofollow) { |
---|
| 587 | $res = preg_replace_callback('#<a(.*?href=".*?".*?)>#ms',array('self','noFollowURL'),$res); |
---|
| 588 | } |
---|
[2566] | 589 | |
---|
[0] | 590 | if ($absolute_urls) { |
---|
| 591 | $res = html::absoluteURLs($res,$rs->getPostURL()); |
---|
| 592 | } |
---|
[2566] | 593 | |
---|
[0] | 594 | return $res; |
---|
| 595 | } |
---|
[2566] | 596 | |
---|
[0] | 597 | private static function noFollowURL($m) |
---|
| 598 | { |
---|
| 599 | if (preg_match('/rel="nofollow"/',$m[1])) { |
---|
| 600 | return $m[0]; |
---|
| 601 | } |
---|
[2566] | 602 | |
---|
[0] | 603 | return '<a'.$m[1].' rel="nofollow">'; |
---|
| 604 | } |
---|
[2566] | 605 | |
---|
[0] | 606 | /** |
---|
| 607 | Returns comment author link to his website if he specified one. |
---|
[2566] | 608 | |
---|
[0] | 609 | @param rs Invisible parameter |
---|
| 610 | @return <b>string</b> |
---|
| 611 | */ |
---|
| 612 | public static function getAuthorURL($rs) |
---|
| 613 | { |
---|
| 614 | if (trim($rs->comment_site)) { |
---|
| 615 | return trim($rs->comment_site); |
---|
| 616 | } |
---|
| 617 | } |
---|
[2566] | 618 | |
---|
[0] | 619 | /** |
---|
| 620 | Returns comment post full URL. |
---|
[2566] | 621 | |
---|
[0] | 622 | @param rs Invisible parameter |
---|
| 623 | @return <b>string</b> |
---|
| 624 | */ |
---|
| 625 | public static function getPostURL($rs) |
---|
| 626 | { |
---|
| 627 | return $rs->core->blog->url.$rs->core->getPostPublicURL( |
---|
| 628 | $rs->post_type,html::sanitizeURL($rs->post_url) |
---|
| 629 | ); |
---|
| 630 | } |
---|
[2566] | 631 | |
---|
[0] | 632 | /** |
---|
| 633 | Returns comment author name in a link to his website if he specified one. |
---|
[2566] | 634 | |
---|
[0] | 635 | @param rs Invisible parameter |
---|
| 636 | @return <b>string</b> |
---|
| 637 | */ |
---|
| 638 | public static function getAuthorLink($rs) |
---|
| 639 | { |
---|
| 640 | $res = '%1$s'; |
---|
| 641 | $url = $rs->getAuthorURL(); |
---|
| 642 | if ($url) { |
---|
| 643 | $res = '<a href="%2$s"%3$s>%1$s</a>'; |
---|
| 644 | } |
---|
[2566] | 645 | |
---|
[0] | 646 | $nofollow = ''; |
---|
| 647 | if ($rs->core->blog->settings->system->comments_nofollow) { |
---|
| 648 | $nofollow = ' rel="nofollow"'; |
---|
| 649 | } |
---|
[2566] | 650 | |
---|
[0] | 651 | return sprintf($res,html::escapeHTML($rs->comment_author),html::escapeHTML($url),$nofollow); |
---|
| 652 | } |
---|
[2566] | 653 | |
---|
[0] | 654 | /** |
---|
| 655 | Returns comment author e-mail address. If <var>$encoded</var> is true, |
---|
| 656 | "@" sign is replaced by "%40" and "." by "%2e". |
---|
[2566] | 657 | |
---|
[0] | 658 | @param rs Invisible parameter |
---|
| 659 | @param encoded <b>boolean</b> Encode address. |
---|
| 660 | @return <b>string</b> |
---|
| 661 | */ |
---|
| 662 | public static function getEmail($rs,$encoded=true) |
---|
| 663 | { |
---|
| 664 | if ($encoded) { |
---|
| 665 | return strtr($rs->comment_email,array('@'=>'%40','.'=>'%2e')); |
---|
| 666 | } |
---|
| 667 | return $rs->comment_email; |
---|
| 668 | } |
---|
[2566] | 669 | |
---|
[0] | 670 | /** |
---|
| 671 | Returns trackback site title if comment is a trackback. |
---|
[2566] | 672 | |
---|
[0] | 673 | @param rs Invisible parameter |
---|
| 674 | @return <b>string</b> |
---|
| 675 | */ |
---|
| 676 | public static function getTrackbackTitle($rs) |
---|
| 677 | { |
---|
| 678 | if ($rs->comment_trackback == 1 && |
---|
| 679 | preg_match('|<p><strong>(.*?)</strong></p>|msU',$rs->comment_content, |
---|
| 680 | $match)) { |
---|
| 681 | return html::decodeEntities($match[1]); |
---|
| 682 | } |
---|
| 683 | } |
---|
[2566] | 684 | |
---|
[0] | 685 | /** |
---|
| 686 | Returns trackback content if comment is a trackback. |
---|
[2566] | 687 | |
---|
[0] | 688 | @param rs Invisible parameter |
---|
| 689 | @return <b>string</b> |
---|
| 690 | */ |
---|
| 691 | public static function getTrackbackContent($rs) |
---|
| 692 | { |
---|
| 693 | if ($rs->comment_trackback == 1) { |
---|
| 694 | return preg_replace('|<p><strong>.*?</strong></p>|msU','', |
---|
| 695 | $rs->comment_content); |
---|
| 696 | } |
---|
| 697 | } |
---|
[2566] | 698 | |
---|
[0] | 699 | /** |
---|
| 700 | Returns comment feed unique ID. |
---|
[2566] | 701 | |
---|
[0] | 702 | @param rs Invisible parameter |
---|
| 703 | @return <b>string</b> |
---|
| 704 | */ |
---|
| 705 | public static function getFeedID($rs) |
---|
| 706 | { |
---|
| 707 | return 'urn:md5:'.md5($rs->core->blog->uid.$rs->comment_id); |
---|
[2566] | 708 | |
---|
[0] | 709 | $url = parse_url($rs->core->blog->url); |
---|
| 710 | $date_part = date('Y-m-d',strtotime($rs->comment_dt)); |
---|
[2566] | 711 | |
---|
[0] | 712 | return 'tag:'.$url['host'].','.$date_part.':'.$rs->comment_id; |
---|
| 713 | } |
---|
[2566] | 714 | |
---|
[0] | 715 | /** |
---|
| 716 | Returns whether comment is from the post author. |
---|
[2566] | 717 | |
---|
[0] | 718 | @param rs Invisible parameter |
---|
| 719 | @return <b>boolean</b> |
---|
| 720 | */ |
---|
| 721 | public static function isMe($rs) |
---|
| 722 | { |
---|
| 723 | return |
---|
| 724 | $rs->comment_email && $rs->comment_site && |
---|
| 725 | $rs->comment_email == $rs->user_email && |
---|
| 726 | $rs->comment_site == $rs->user_url; |
---|
| 727 | } |
---|
| 728 | } |
---|
| 729 | |
---|
| 730 | /** |
---|
| 731 | @ingroup DC_CORE |
---|
| 732 | @brief Dotclear dates record helpers. |
---|
| 733 | |
---|
| 734 | This class adds new methods to database dates results. |
---|
| 735 | You can call them on every record comming from dcBlog::getDates. |
---|
| 736 | |
---|
| 737 | @warning You should not give the first argument (usualy $rs) of every described |
---|
| 738 | function. |
---|
| 739 | */ |
---|
| 740 | class rsExtDates |
---|
| 741 | { |
---|
| 742 | /** |
---|
| 743 | @param rs Invisible parameter |
---|
| 744 | @return <b>integer</b> Date timestamp |
---|
| 745 | */ |
---|
| 746 | public static function ts($rs) |
---|
| 747 | { |
---|
| 748 | return strtotime($rs->dt); |
---|
| 749 | } |
---|
[2566] | 750 | |
---|
[0] | 751 | /** |
---|
| 752 | @param rs Invisible parameter |
---|
| 753 | @return <b>string</b> Date year |
---|
| 754 | */ |
---|
| 755 | public static function year($rs) |
---|
| 756 | { |
---|
| 757 | return date('Y',strtotime($rs->dt)); |
---|
| 758 | } |
---|
[2566] | 759 | |
---|
[0] | 760 | /** |
---|
| 761 | @param rs Invisible parameter |
---|
| 762 | @return <b>string</b> Date month |
---|
| 763 | */ |
---|
| 764 | public static function month($rs) |
---|
| 765 | { |
---|
| 766 | return date('m',strtotime($rs->dt)); |
---|
| 767 | } |
---|
[2566] | 768 | |
---|
[0] | 769 | /** |
---|
| 770 | @param rs Invisible parameter |
---|
| 771 | @return <b>integer</b> Date day |
---|
| 772 | */ |
---|
| 773 | public static function day($rs) |
---|
| 774 | { |
---|
| 775 | return date('d',strtotime($rs->dt)); |
---|
| 776 | } |
---|
[2566] | 777 | |
---|
[0] | 778 | /** |
---|
| 779 | Returns date month archive full URL. |
---|
[2566] | 780 | |
---|
[0] | 781 | @param rs Invisible parameter |
---|
| 782 | @param core <b>dcCore</b> dcCore instance |
---|
| 783 | @return <b>integer</b> |
---|
| 784 | */ |
---|
| 785 | public static function url($rs,$core) |
---|
| 786 | { |
---|
| 787 | $url = date('Y/m',strtotime($rs->dt)); |
---|
[2566] | 788 | |
---|
[776] | 789 | return $core->blog->url.$core->url->getURLFor('archive',$url); |
---|
[0] | 790 | } |
---|
[2566] | 791 | |
---|
[0] | 792 | /** |
---|
| 793 | Returns whether date is the first of year. |
---|
[2566] | 794 | |
---|
[0] | 795 | @param rs Invisible parameter |
---|
| 796 | @return <b>boolean</b> |
---|
| 797 | */ |
---|
| 798 | public static function yearHeader($rs) |
---|
| 799 | { |
---|
| 800 | if ($rs->isStart()) { |
---|
| 801 | return true; |
---|
| 802 | } |
---|
[2566] | 803 | |
---|
[0] | 804 | $y = $rs->year(); |
---|
| 805 | $rs->movePrev(); |
---|
| 806 | $py = $rs->year(); |
---|
| 807 | $rs->moveNext(); |
---|
[2566] | 808 | |
---|
[0] | 809 | return $y != $py; |
---|
| 810 | } |
---|
[2566] | 811 | |
---|
[0] | 812 | /** |
---|
| 813 | Returns whether date is the last of year. |
---|
[2566] | 814 | |
---|
[0] | 815 | @param rs Invisible parameter |
---|
| 816 | @return <b>boolean</b> |
---|
| 817 | */ |
---|
| 818 | public static function yearFooter($rs) |
---|
| 819 | { |
---|
| 820 | if ($rs->isEnd()) { |
---|
| 821 | return true; |
---|
| 822 | } |
---|
[2566] | 823 | |
---|
[0] | 824 | $y = $rs->year(); |
---|
| 825 | if ($rs->moveNext()) { |
---|
| 826 | $ny = $rs->year(); |
---|
| 827 | $rs->movePrev(); |
---|
| 828 | return $y != $ny; |
---|
| 829 | } |
---|
| 830 | return false; |
---|
[2566] | 831 | |
---|
[0] | 832 | } |
---|
| 833 | } |
---|
| 834 | |
---|
| 835 | /** |
---|
| 836 | @ingroup DC_CORE |
---|
| 837 | @brief Dotclear dates record helpers. |
---|
| 838 | |
---|
| 839 | This class adds new methods to database dates results. |
---|
| 840 | You can call them on every record comming from dcAuth::checkUser and |
---|
| 841 | dcCore::getUsers. |
---|
| 842 | |
---|
| 843 | @warning You should not give the first argument (usualy $rs) of every described |
---|
| 844 | function. |
---|
| 845 | */ |
---|
| 846 | class rsExtUser |
---|
| 847 | { |
---|
[3105] | 848 | private static $sortfield; |
---|
| 849 | private static $sortsign; |
---|
[0] | 850 | /** |
---|
| 851 | Returns a user option. |
---|
[2566] | 852 | |
---|
[0] | 853 | @param rs Invisible parameter |
---|
| 854 | @param name <b>string</b> Option name |
---|
| 855 | @return <b>string</b> |
---|
| 856 | */ |
---|
| 857 | public static function option($rs,$name) |
---|
| 858 | { |
---|
| 859 | $options = self::options($rs); |
---|
[2566] | 860 | |
---|
[0] | 861 | if (isset($options[$name])) { |
---|
| 862 | return $options[$name]; |
---|
| 863 | } |
---|
| 864 | return null; |
---|
| 865 | } |
---|
[2566] | 866 | |
---|
[0] | 867 | /** |
---|
| 868 | Returns all user options. |
---|
[2566] | 869 | |
---|
[0] | 870 | @param rs Invisible parameter |
---|
| 871 | @return <b>array</b> |
---|
| 872 | */ |
---|
| 873 | public static function options($rs) |
---|
| 874 | { |
---|
| 875 | $options = @unserialize($rs->user_options); |
---|
| 876 | if (is_array($options)) { |
---|
| 877 | return $options; |
---|
| 878 | } |
---|
| 879 | return array(); |
---|
| 880 | } |
---|
[3105] | 881 | |
---|
| 882 | /** |
---|
| 883 | Converts this record to a {@link extStaticRecord} instance. |
---|
| 884 | |
---|
| 885 | @param rs Invisible parameter |
---|
| 886 | */ |
---|
| 887 | public static function toExtStatic($rs) |
---|
| 888 | { |
---|
| 889 | if ($rs instanceof extStaticRecord) { |
---|
| 890 | return $rs; |
---|
| 891 | } |
---|
| 892 | return new extStaticRecord($rs); |
---|
| 893 | } |
---|
[0] | 894 | } |
---|
[3105] | 895 | |
---|
[3106] | 896 | class rsExtBlog |
---|
| 897 | { |
---|
| 898 | private static $sortfield; |
---|
| 899 | private static $sortsign; |
---|
| 900 | /** |
---|
| 901 | Converts this record to a {@link extStaticRecord} instance. |
---|
| 902 | |
---|
| 903 | @param rs Invisible parameter |
---|
| 904 | */ |
---|
| 905 | public static function toExtStatic($rs) |
---|
| 906 | { |
---|
| 907 | if ($rs instanceof extStaticRecord) { |
---|
| 908 | return $rs; |
---|
| 909 | } |
---|
| 910 | return new extStaticRecord($rs); |
---|
| 911 | } |
---|
| 912 | } |
---|
| 913 | |
---|
[3105] | 914 | class extStaticRecord extends staticRecord |
---|
| 915 | { |
---|
| 916 | private $sortfield; |
---|
| 917 | private $sortsign; |
---|
| 918 | |
---|
| 919 | public function __construct($rs) |
---|
| 920 | { |
---|
| 921 | parent::__construct($rs->__data,$rs->__info); |
---|
| 922 | } |
---|
| 923 | |
---|
| 924 | /** |
---|
| 925 | Lexically sort. |
---|
| 926 | |
---|
| 927 | @param field <b>string<b> sort field |
---|
| 928 | @param order <b>string<b> sort order |
---|
| 929 | */ |
---|
| 930 | public function lexicalSort($field,$order='asc') |
---|
| 931 | { |
---|
| 932 | $this->sortfield = $field; |
---|
| 933 | $this->sortsign = strtolower($order) == 'asc' ? 1 : -1; |
---|
| 934 | |
---|
| 935 | usort($this->__data,array($this,'lexicalSortCallback')); |
---|
| 936 | |
---|
| 937 | $this->sortfield = null; |
---|
| 938 | $this->sortsign = null; |
---|
| 939 | } |
---|
| 940 | private function lexicalSortCallback($a,$b) |
---|
| 941 | { |
---|
| 942 | $a = $a[$this->sortfield]; |
---|
| 943 | $b = $b[$this->sortfield]; |
---|
| 944 | |
---|
| 945 | # Integer values |
---|
| 946 | if ($a == (string) (integer) $a && $b == (string) (integer) $b) { |
---|
| 947 | $a = (integer) $a; |
---|
| 948 | $b = (integer) $b; |
---|
| 949 | return ($a - $b) * $this->sortsign; |
---|
| 950 | } |
---|
| 951 | |
---|
| 952 | return strcoll(strtolower(dcUtils::removeDiacritics($a)),strtolower(dcUtils::removeDiacritics($b))) * $this->sortsign; |
---|
| 953 | } |
---|
| 954 | } |
---|