[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 ----------------------------------------- |
---|
| 12 | if (!defined('DC_RC_PATH')) { return; } |
---|
| 13 | |
---|
| 14 | /** |
---|
| 15 | @ingroup DC_CORE |
---|
| 16 | @nosubgrouping |
---|
| 17 | @brief Dotclear blog class. |
---|
| 18 | |
---|
| 19 | Dotclear blog class instance is provided by dcCore $blog property. |
---|
| 20 | */ |
---|
| 21 | class dcBlog |
---|
| 22 | { |
---|
[337] | 23 | /** @var dcCore dcCore instance */ |
---|
| 24 | protected $core; |
---|
| 25 | /** @var connection Database connection object */ |
---|
| 26 | public $con; |
---|
| 27 | /** @var string Database table prefix */ |
---|
| 28 | public $prefix; |
---|
[2566] | 29 | |
---|
[337] | 30 | /** @var string Blog ID */ |
---|
| 31 | public $id; |
---|
| 32 | /** @var string Blog unique ID */ |
---|
| 33 | public $uid; |
---|
| 34 | /** @var string Blog name */ |
---|
| 35 | public $name; |
---|
| 36 | /** @var string Blog description */ |
---|
| 37 | public $desc; |
---|
| 38 | /** @var string Blog URL */ |
---|
| 39 | public $url; |
---|
| 40 | /** @var string Blog host */ |
---|
| 41 | public $host; |
---|
| 42 | /** @var string Blog creation date */ |
---|
| 43 | public $creadt; |
---|
| 44 | /** @var string Blog last update date */ |
---|
| 45 | public $upddt; |
---|
| 46 | /** @var string Blog status */ |
---|
| 47 | public $status; |
---|
[2566] | 48 | |
---|
[337] | 49 | /** @var dcSettings dcSettings object */ |
---|
| 50 | public $settings; |
---|
| 51 | /** @var string Blog theme path */ |
---|
| 52 | public $themes_path; |
---|
| 53 | /** @var string Blog public path */ |
---|
| 54 | public $public_path; |
---|
[2566] | 55 | |
---|
[0] | 56 | private $post_status = array(); |
---|
| 57 | private $comment_status = array(); |
---|
[2566] | 58 | |
---|
[0] | 59 | private $categories; |
---|
[2566] | 60 | |
---|
[337] | 61 | /** @var boolean Disallow entries password protection */ |
---|
| 62 | public $without_password = true; |
---|
[2566] | 63 | |
---|
[0] | 64 | /** |
---|
| 65 | Inits dcBlog object |
---|
[2566] | 66 | |
---|
[0] | 67 | @param core <b>dcCore</b> Dotclear core reference |
---|
| 68 | @param id <b>string</b> Blog ID |
---|
| 69 | */ |
---|
| 70 | public function __construct($core, $id) |
---|
| 71 | { |
---|
| 72 | $this->con =& $core->con; |
---|
| 73 | $this->prefix = $core->prefix; |
---|
| 74 | $this->core =& $core; |
---|
[2566] | 75 | |
---|
[0] | 76 | if (($b = $this->core->getBlog($id)) !== false) |
---|
| 77 | { |
---|
| 78 | $this->id = $id; |
---|
| 79 | $this->uid = $b->blog_uid; |
---|
| 80 | $this->name = $b->blog_name; |
---|
| 81 | $this->desc = $b->blog_desc; |
---|
| 82 | $this->url = $b->blog_url; |
---|
[1244] | 83 | $this->host = http::getHostFromURL($this->url); |
---|
[0] | 84 | $this->creadt = strtotime($b->blog_creadt); |
---|
| 85 | $this->upddt = strtotime($b->blog_upddt); |
---|
| 86 | $this->status = $b->blog_status; |
---|
[2566] | 87 | |
---|
[0] | 88 | $this->settings = new dcSettings($this->core,$this->id); |
---|
[2566] | 89 | |
---|
[0] | 90 | $this->themes_path = path::fullFromRoot($this->settings->system->themes_path,DC_ROOT); |
---|
| 91 | $this->public_path = path::fullFromRoot($this->settings->system->public_path,DC_ROOT); |
---|
[2566] | 92 | |
---|
[1353] | 93 | $this->post_status['-2'] = __('Pending'); |
---|
| 94 | $this->post_status['-1'] = __('Scheduled'); |
---|
| 95 | $this->post_status['0'] = __('Unpublished'); |
---|
| 96 | $this->post_status['1'] = __('Published'); |
---|
[2566] | 97 | |
---|
[1353] | 98 | $this->comment_status['-2'] = __('Junk'); |
---|
| 99 | $this->comment_status['-1'] = __('Pending'); |
---|
| 100 | $this->comment_status['0'] = __('Unpublished'); |
---|
| 101 | $this->comment_status['1'] = __('Published'); |
---|
[2566] | 102 | |
---|
[0] | 103 | # --BEHAVIOR-- coreBlogConstruct |
---|
| 104 | $this->core->callBehavior('coreBlogConstruct',$this); |
---|
| 105 | } |
---|
| 106 | } |
---|
[2566] | 107 | |
---|
[0] | 108 | /// @name Common public methods |
---|
| 109 | //@{ |
---|
| 110 | /** |
---|
| 111 | Returns blog URL ending with a question mark. |
---|
| 112 | */ |
---|
| 113 | public function getQmarkURL() |
---|
| 114 | { |
---|
| 115 | if (substr($this->url,-1) != '?') { |
---|
| 116 | return $this->url.'?'; |
---|
| 117 | } |
---|
[2566] | 118 | |
---|
[0] | 119 | return $this->url; |
---|
| 120 | } |
---|
[2566] | 121 | |
---|
[0] | 122 | /** |
---|
| 123 | Returns an entry status name given to a code. Status are translated, never |
---|
| 124 | use it for tests. If status code does not exist, returns <i>unpublished</i>. |
---|
[2566] | 125 | |
---|
[0] | 126 | @param s <b>integer</b> Status code |
---|
| 127 | @return <b>string</b> Blog status name |
---|
| 128 | */ |
---|
| 129 | public function getPostStatus($s) |
---|
| 130 | { |
---|
| 131 | if (isset($this->post_status[$s])) { |
---|
| 132 | return $this->post_status[$s]; |
---|
| 133 | } |
---|
| 134 | return $this->post_status['0']; |
---|
| 135 | } |
---|
[2566] | 136 | |
---|
[0] | 137 | /** |
---|
| 138 | Returns an array of available entry status codes and names. |
---|
[2566] | 139 | |
---|
[0] | 140 | @return <b>array</b> Simple array with codes in keys and names in value |
---|
| 141 | */ |
---|
| 142 | public function getAllPostStatus() |
---|
| 143 | { |
---|
| 144 | return $this->post_status; |
---|
| 145 | } |
---|
[2566] | 146 | |
---|
[0] | 147 | /** |
---|
| 148 | Returns an array of available comment status codes and names. |
---|
[2566] | 149 | |
---|
[0] | 150 | @return <b>array</b> Simple array with codes in keys and names in value |
---|
| 151 | */ |
---|
| 152 | public function getAllCommentStatus() |
---|
| 153 | { |
---|
| 154 | return $this->comment_status; |
---|
| 155 | } |
---|
[2566] | 156 | |
---|
[0] | 157 | /** |
---|
| 158 | Disallows entries password protection. You need to set it to |
---|
| 159 | <var>false</var> while serving a public blog. |
---|
[2566] | 160 | |
---|
[0] | 161 | @param v <b>boolean</b> |
---|
| 162 | */ |
---|
| 163 | public function withoutPassword($v) |
---|
| 164 | { |
---|
| 165 | $this->without_password = (boolean) $v; |
---|
| 166 | } |
---|
| 167 | //@} |
---|
[2566] | 168 | |
---|
[0] | 169 | /// @name Triggers methods |
---|
| 170 | //@{ |
---|
| 171 | /** |
---|
| 172 | Updates blog last update date. Should be called every time you change |
---|
| 173 | an element related to the blog. |
---|
| 174 | */ |
---|
| 175 | public function triggerBlog() |
---|
| 176 | { |
---|
| 177 | $cur = $this->con->openCursor($this->prefix.'blog'); |
---|
[2566] | 178 | |
---|
[0] | 179 | $cur->blog_upddt = date('Y-m-d H:i:s'); |
---|
[2566] | 180 | |
---|
[0] | 181 | $cur->update("WHERE blog_id = '".$this->con->escape($this->id)."' "); |
---|
[2566] | 182 | |
---|
[0] | 183 | # --BEHAVIOR-- coreBlogAfterTriggerBlog |
---|
| 184 | $this->core->callBehavior('coreBlogAfterTriggerBlog',$cur); |
---|
| 185 | } |
---|
[2566] | 186 | |
---|
[0] | 187 | /** |
---|
| 188 | Updates comment and trackback counters in post table. Should be called |
---|
| 189 | every time a comment or trackback is added, removed or changed its status. |
---|
[2566] | 190 | |
---|
[0] | 191 | @param id <b>integer</b> Comment ID |
---|
| 192 | @param del <b>boolean</b> If comment is delete, set this to true |
---|
| 193 | */ |
---|
| 194 | public function triggerComment($id,$del=false) |
---|
| 195 | { |
---|
[1042] | 196 | $this->triggerComments($id,$del); |
---|
| 197 | } |
---|
[2566] | 198 | |
---|
[1042] | 199 | /** |
---|
| 200 | Updates comments and trackbacks counters in post table. Should be called |
---|
[1689] | 201 | every time comments or trackbacks are added, removed or changed their status. |
---|
[2566] | 202 | |
---|
[1042] | 203 | @param ids <b>mixed</b> Comment(s) ID(s) |
---|
| 204 | @param del <b>boolean</b> If comment is delete, set this to true |
---|
[1262] | 205 | @param affected_posts <b>mixed</b> Posts(s) ID(s) |
---|
[1042] | 206 | */ |
---|
[1262] | 207 | public function triggerComments($ids, $del=false, $affected_posts=null) |
---|
[1042] | 208 | { |
---|
[1561] | 209 | $comments_ids = dcUtils::cleanIds($ids); |
---|
[2566] | 210 | |
---|
[1570] | 211 | # Get posts affected by comments edition |
---|
| 212 | if (empty($affected_posts)) { |
---|
[2566] | 213 | $strReq = |
---|
[1570] | 214 | 'SELECT post_id '. |
---|
[1262] | 215 | 'FROM '.$this->prefix.'comment '. |
---|
[1561] | 216 | 'WHERE comment_id'.$this->con->in($comments_ids). |
---|
[1570] | 217 | 'GROUP BY post_id'; |
---|
[2566] | 218 | |
---|
[1262] | 219 | $rs = $this->con->select($strReq); |
---|
[2566] | 220 | |
---|
[1570] | 221 | $affected_posts = array(); |
---|
[1262] | 222 | while ($rs->fetch()) { |
---|
[1570] | 223 | $affected_posts[] = (integer) $rs->post_id; |
---|
[1262] | 224 | } |
---|
[1042] | 225 | } |
---|
[2566] | 226 | |
---|
[1570] | 227 | if (!is_array($affected_posts) || empty($affected_posts)) { |
---|
| 228 | return; |
---|
| 229 | } |
---|
[2566] | 230 | |
---|
[1570] | 231 | # Count number of comments if exists for affected posts |
---|
[2566] | 232 | $strReq = |
---|
[1570] | 233 | 'SELECT post_id, COUNT(post_id) AS nb_comment, comment_trackback '. |
---|
[1042] | 234 | 'FROM '.$this->prefix.'comment '. |
---|
[1110] | 235 | 'WHERE comment_status = 1 '. |
---|
[1570] | 236 | 'AND post_id'.$this->con->in($affected_posts). |
---|
[1042] | 237 | 'GROUP BY post_id,comment_trackback'; |
---|
[2566] | 238 | |
---|
[0] | 239 | $rs = $this->con->select($strReq); |
---|
[2566] | 240 | |
---|
[1570] | 241 | $posts = array(); |
---|
[1042] | 242 | while ($rs->fetch()) { |
---|
[1570] | 243 | if ($rs->comment_trackback) { |
---|
| 244 | $posts[$rs->post_id]['trackback'] = $rs->nb_comment; |
---|
| 245 | } else { |
---|
| 246 | $posts[$rs->post_id]['comment'] = $rs->nb_comment; |
---|
| 247 | } |
---|
[1042] | 248 | } |
---|
[2566] | 249 | |
---|
[1570] | 250 | # Update number of comments on affected posts |
---|
[0] | 251 | $cur = $this->con->openCursor($this->prefix.'post'); |
---|
[1570] | 252 | foreach($affected_posts as $post_id) |
---|
[1042] | 253 | { |
---|
[1570] | 254 | $cur->clean(); |
---|
[2566] | 255 | |
---|
[1570] | 256 | if (!array_key_exists($post_id,$posts)) { |
---|
| 257 | $cur->nb_trackback = 0; |
---|
| 258 | $cur->nb_comment = 0; |
---|
| 259 | } else { |
---|
| 260 | $cur->nb_trackback = empty($posts[$post_id]['trackback']) ? 0 : $posts[$post_id]['trackback']; |
---|
| 261 | $cur->nb_comment = empty($posts[$post_id]['comment']) ? 0 : $posts[$post_id]['comment']; |
---|
[1042] | 262 | } |
---|
[2566] | 263 | |
---|
[1570] | 264 | $cur->update('WHERE post_id = '.$post_id); |
---|
[0] | 265 | } |
---|
| 266 | } |
---|
| 267 | //@} |
---|
[2566] | 268 | |
---|
[0] | 269 | /// @name Categories management methods |
---|
| 270 | //@{ |
---|
| 271 | public function categories() |
---|
| 272 | { |
---|
| 273 | if (!($this->categories instanceof dcCategories)) { |
---|
| 274 | $this->categories = new dcCategories($this->core); |
---|
| 275 | } |
---|
[2566] | 276 | |
---|
[0] | 277 | return $this->categories; |
---|
| 278 | } |
---|
[2566] | 279 | |
---|
[0] | 280 | /** |
---|
| 281 | Retrieves categories. <var>$params</var> is an associative array which can |
---|
| 282 | take the following parameters: |
---|
[2566] | 283 | |
---|
[0] | 284 | - post_type: Get only entries with given type (default "post") |
---|
| 285 | - cat_url: filter on cat_url field |
---|
| 286 | - cat_id: filter on cat_id field |
---|
| 287 | - start: start with a given category |
---|
| 288 | - level: categories level to retrieve |
---|
[2566] | 289 | |
---|
[0] | 290 | @param params <b>array</b> Parameters |
---|
| 291 | @return <b>record</b> |
---|
| 292 | */ |
---|
| 293 | public function getCategories($params=array()) |
---|
| 294 | { |
---|
| 295 | $c_params = array(); |
---|
| 296 | if (isset($params['post_type'])) { |
---|
| 297 | $c_params['post_type'] = $params['post_type']; |
---|
| 298 | unset($params['post_type']); |
---|
| 299 | } |
---|
| 300 | $counter = $this->getCategoriesCounter($c_params); |
---|
[2566] | 301 | |
---|
[1610] | 302 | if (isset($params['without_empty']) && ($params['without_empty'] == false)) { |
---|
| 303 | $without_empty = false; |
---|
| 304 | } else { |
---|
| 305 | $without_empty = $this->core->auth->userID() == false; # Get all categories if in admin display |
---|
| 306 | } |
---|
[2566] | 307 | |
---|
[0] | 308 | $start = isset($params['start']) ? (integer) $params['start'] : 0; |
---|
| 309 | $l = isset($params['level']) ? (integer) $params['level'] : 0; |
---|
[2566] | 310 | |
---|
[0] | 311 | $rs = $this->categories()->getChildren($start,null,'desc'); |
---|
[2566] | 312 | |
---|
[0] | 313 | # Get each categories total posts count |
---|
| 314 | $data = array(); |
---|
| 315 | $stack = array(); |
---|
| 316 | $level = 0; |
---|
| 317 | $cols = $rs->columns(); |
---|
| 318 | while ($rs->fetch()) |
---|
| 319 | { |
---|
| 320 | $nb_post = isset($counter[$rs->cat_id]) ? (integer) $counter[$rs->cat_id] : 0; |
---|
[2566] | 321 | |
---|
[0] | 322 | if ($rs->level > $level) { |
---|
| 323 | $nb_total = $nb_post; |
---|
| 324 | $stack[$rs->level] = (integer) $nb_post; |
---|
| 325 | } elseif ($rs->level == $level) { |
---|
| 326 | $nb_total = $nb_post; |
---|
| 327 | $stack[$rs->level] += $nb_post; |
---|
| 328 | } else { |
---|
| 329 | $nb_total = $stack[$rs->level+1] + $nb_post; |
---|
| 330 | if (isset($stack[$rs->level])) { |
---|
| 331 | $stack[$rs->level] += $nb_total; |
---|
| 332 | } else { |
---|
| 333 | $stack[$rs->level] = $nb_total; |
---|
| 334 | } |
---|
| 335 | unset($stack[$rs->level+1]); |
---|
| 336 | } |
---|
[2566] | 337 | |
---|
[0] | 338 | if ($nb_total == 0 && $without_empty) { |
---|
| 339 | continue; |
---|
| 340 | } |
---|
[2566] | 341 | |
---|
[0] | 342 | $level = $rs->level; |
---|
[2566] | 343 | |
---|
[0] | 344 | $t = array(); |
---|
| 345 | foreach ($cols as $c) { |
---|
| 346 | $t[$c] = $rs->f($c); |
---|
| 347 | } |
---|
| 348 | $t['nb_post'] = $nb_post; |
---|
| 349 | $t['nb_total'] = $nb_total; |
---|
[2566] | 350 | |
---|
[0] | 351 | if ($l == 0 || ($l > 0 && $l == $rs->level)) { |
---|
| 352 | array_unshift($data,$t); |
---|
| 353 | } |
---|
| 354 | } |
---|
[2566] | 355 | |
---|
[0] | 356 | # We need to apply filter after counting |
---|
[836] | 357 | if (isset($params['cat_id']) && $params['cat_id'] !== '') |
---|
[0] | 358 | { |
---|
| 359 | $found = false; |
---|
| 360 | foreach ($data as $v) { |
---|
| 361 | if ($v['cat_id'] == $params['cat_id']) { |
---|
| 362 | $found = true; |
---|
| 363 | $data = array($v); |
---|
| 364 | break; |
---|
| 365 | } |
---|
| 366 | } |
---|
| 367 | if (!$found) { |
---|
| 368 | $data = array(); |
---|
| 369 | } |
---|
| 370 | } |
---|
[2566] | 371 | |
---|
| 372 | if (isset($params['cat_url']) && ($params['cat_url'] !== '') |
---|
[836] | 373 | && !isset($params['cat_id'])) |
---|
[0] | 374 | { |
---|
| 375 | $found = false; |
---|
| 376 | foreach ($data as $v) { |
---|
| 377 | if ($v['cat_url'] == $params['cat_url']) { |
---|
| 378 | $found = true; |
---|
| 379 | $data = array($v); |
---|
| 380 | break; |
---|
| 381 | } |
---|
| 382 | } |
---|
| 383 | if (!$found) { |
---|
| 384 | $data = array(); |
---|
| 385 | } |
---|
| 386 | } |
---|
[2566] | 387 | |
---|
[0] | 388 | return staticRecord::newFromArray($data); |
---|
| 389 | } |
---|
[2566] | 390 | |
---|
[0] | 391 | /** |
---|
| 392 | Retrieves a category by its ID. |
---|
[2566] | 393 | |
---|
[0] | 394 | @param id <b>integer</b> Category ID |
---|
| 395 | @return <b>record</b> |
---|
| 396 | */ |
---|
| 397 | public function getCategory($id) |
---|
| 398 | { |
---|
| 399 | return $this->getCategories(array('cat_id' => $id)); |
---|
| 400 | } |
---|
[2566] | 401 | |
---|
[0] | 402 | /** |
---|
| 403 | Retrieves parents of a given category. |
---|
[2566] | 404 | |
---|
[0] | 405 | @param id <b>integer</b> Category ID |
---|
| 406 | @return <b>record</b> |
---|
| 407 | */ |
---|
| 408 | public function getCategoryParents($id) |
---|
| 409 | { |
---|
| 410 | return $this->categories()->getParents($id); |
---|
| 411 | } |
---|
[2566] | 412 | |
---|
[0] | 413 | /** |
---|
| 414 | Retrieves first parent of a given category. |
---|
[2566] | 415 | |
---|
[0] | 416 | @param id <b>integer</b> Category ID |
---|
| 417 | @return <b>record</b> |
---|
| 418 | */ |
---|
| 419 | public function getCategoryParent($id) |
---|
| 420 | { |
---|
| 421 | return $this->categories()->getParent($id); |
---|
| 422 | } |
---|
[2566] | 423 | |
---|
[0] | 424 | /** |
---|
| 425 | Retrieves all category's first children |
---|
[2566] | 426 | |
---|
[0] | 427 | @param id <b>integer</b> Category ID |
---|
| 428 | @return <b>record</b> |
---|
| 429 | */ |
---|
| 430 | public function getCategoryFirstChildren($id) |
---|
| 431 | { |
---|
| 432 | return $this->getCategories(array('start' => $id,'level' => $id == 0 ? 1 : 2)); |
---|
| 433 | } |
---|
[2566] | 434 | |
---|
[0] | 435 | private function getCategoriesCounter($params=array()) |
---|
| 436 | { |
---|
| 437 | $strReq = |
---|
| 438 | 'SELECT C.cat_id, COUNT(P.post_id) AS nb_post '. |
---|
| 439 | 'FROM '.$this->prefix.'category AS C '. |
---|
| 440 | 'JOIN '.$this->prefix."post P ON (C.cat_id = P.cat_id AND P.blog_id = '".$this->con->escape($this->id)."' ) ". |
---|
| 441 | "WHERE C.blog_id = '".$this->con->escape($this->id)."' "; |
---|
[2566] | 442 | |
---|
[0] | 443 | if (!$this->core->auth->userID()) { |
---|
| 444 | $strReq .= 'AND P.post_status = 1 '; |
---|
| 445 | } |
---|
[2566] | 446 | |
---|
[0] | 447 | if (!empty($params['post_type'])) { |
---|
[302] | 448 | $strReq .= 'AND P.post_type '.$this->con->in($params['post_type']); |
---|
[0] | 449 | } |
---|
[2566] | 450 | |
---|
[0] | 451 | $strReq .= 'GROUP BY C.cat_id '; |
---|
[2566] | 452 | |
---|
[0] | 453 | $rs = $this->con->select($strReq); |
---|
| 454 | $counters = array(); |
---|
| 455 | while ($rs->fetch()) { |
---|
| 456 | $counters[$rs->cat_id] = $rs->nb_post; |
---|
| 457 | } |
---|
[2566] | 458 | |
---|
[0] | 459 | return $counters; |
---|
| 460 | } |
---|
[2566] | 461 | |
---|
[0] | 462 | /** |
---|
| 463 | Creates a new category. Takes a cursor as input and returns the new category |
---|
| 464 | ID. |
---|
[2566] | 465 | |
---|
[0] | 466 | @param cur <b>cursor</b> Category cursor |
---|
| 467 | @return <b>integer</b> New category ID |
---|
| 468 | */ |
---|
| 469 | public function addCategory($cur,$parent=0) |
---|
| 470 | { |
---|
| 471 | if (!$this->core->auth->check('categories',$this->id)) { |
---|
| 472 | throw new Exception(__('You are not allowed to add categories')); |
---|
| 473 | } |
---|
[2566] | 474 | |
---|
[0] | 475 | $url = array(); |
---|
| 476 | if ($parent != 0) |
---|
| 477 | { |
---|
| 478 | $rs = $this->getCategory($parent); |
---|
| 479 | if ($rs->isEmpty()) { |
---|
| 480 | $url = array(); |
---|
| 481 | } else { |
---|
| 482 | $url[] = $rs->cat_url; |
---|
| 483 | } |
---|
| 484 | } |
---|
[2566] | 485 | |
---|
[0] | 486 | if ($cur->cat_url == '') { |
---|
| 487 | $url[] = text::tidyURL($cur->cat_title,false); |
---|
| 488 | } else { |
---|
| 489 | $url[] = $cur->cat_url; |
---|
| 490 | } |
---|
[2566] | 491 | |
---|
[0] | 492 | $cur->cat_url = implode('/',$url); |
---|
[2566] | 493 | |
---|
[0] | 494 | $this->getCategoryCursor($cur); |
---|
| 495 | $cur->blog_id = (string) $this->id; |
---|
[2566] | 496 | |
---|
[0] | 497 | # --BEHAVIOR-- coreBeforeCategoryCreate |
---|
| 498 | $this->core->callBehavior('coreBeforeCategoryCreate',$this,$cur); |
---|
[2566] | 499 | |
---|
[1261] | 500 | $id = $this->categories()->addNode($cur,$parent); |
---|
| 501 | # Update category's cursor |
---|
| 502 | $rs = $this->getCategory($id); |
---|
| 503 | if (!$rs->isEmpty()) { |
---|
| 504 | $cur->cat_lft = $rs->cat_lft; |
---|
| 505 | $cur->cat_rgt = $rs->cat_rgt; |
---|
| 506 | } |
---|
[2566] | 507 | |
---|
[0] | 508 | # --BEHAVIOR-- coreAfterCategoryCreate |
---|
| 509 | $this->core->callBehavior('coreAfterCategoryCreate',$this,$cur); |
---|
| 510 | $this->triggerBlog(); |
---|
[2566] | 511 | |
---|
[0] | 512 | return $cur->cat_id; |
---|
| 513 | } |
---|
[2566] | 514 | |
---|
[0] | 515 | /** |
---|
| 516 | Updates an existing category. |
---|
[2566] | 517 | |
---|
[0] | 518 | @param id <b>integer</b> Category ID |
---|
| 519 | @param cur <b>cursor</b> Category cursor |
---|
| 520 | */ |
---|
| 521 | public function updCategory($id,$cur) |
---|
| 522 | { |
---|
| 523 | if (!$this->core->auth->check('categories',$this->id)) { |
---|
| 524 | throw new Exception(__('You are not allowed to update categories')); |
---|
| 525 | } |
---|
[2566] | 526 | |
---|
[0] | 527 | if ($cur->cat_url == '') |
---|
| 528 | { |
---|
| 529 | $url = array(); |
---|
| 530 | $rs = $this->categories()->getParents($id); |
---|
| 531 | while ($rs->fetch()) { |
---|
| 532 | if ($rs->index() == $rs->count()-1) { |
---|
| 533 | $url[] = $rs->cat_url; |
---|
| 534 | } |
---|
| 535 | } |
---|
[2566] | 536 | |
---|
| 537 | |
---|
[0] | 538 | $url[] = text::tidyURL($cur->cat_title,false); |
---|
| 539 | $cur->cat_url = implode('/',$url); |
---|
| 540 | } |
---|
[2566] | 541 | |
---|
[0] | 542 | $this->getCategoryCursor($cur,$id); |
---|
[2566] | 543 | |
---|
[0] | 544 | # --BEHAVIOR-- coreBeforeCategoryUpdate |
---|
| 545 | $this->core->callBehavior('coreBeforeCategoryUpdate',$this,$cur); |
---|
[2566] | 546 | |
---|
[0] | 547 | $cur->update( |
---|
| 548 | 'WHERE cat_id = '.(integer) $id.' '. |
---|
| 549 | "AND blog_id = '".$this->con->escape($this->id)."' "); |
---|
[2566] | 550 | |
---|
[0] | 551 | # --BEHAVIOR-- coreAfterCategoryUpdate |
---|
| 552 | $this->core->callBehavior('coreAfterCategoryUpdate',$this,$cur); |
---|
[2566] | 553 | |
---|
[0] | 554 | $this->triggerBlog(); |
---|
| 555 | } |
---|
[1532] | 556 | |
---|
| 557 | /** |
---|
| 558 | Set category position |
---|
| 559 | |
---|
| 560 | @param id <b>integer</b> Category ID |
---|
| 561 | @param left <b>integer</b> Category ID before |
---|
| 562 | @param right <b>integer</b> Category ID after |
---|
| 563 | */ |
---|
| 564 | public function updCategoryPosition($id,$left,$right) |
---|
| 565 | { |
---|
| 566 | $this->categories()->updatePosition($id,$left,$right); |
---|
| 567 | $this->triggerBlog(); |
---|
| 568 | } |
---|
[2566] | 569 | |
---|
[0] | 570 | /** |
---|
| 571 | DEPRECATED METHOD. Use dcBlog::setCategoryParent and dcBlog::moveCategory |
---|
| 572 | instead. |
---|
[2566] | 573 | |
---|
[0] | 574 | @param id <b>integer</b> Category ID |
---|
| 575 | @param order <b>integer</b> Category position |
---|
| 576 | */ |
---|
| 577 | public function updCategoryOrder($id,$order) |
---|
| 578 | { |
---|
| 579 | return; |
---|
| 580 | } |
---|
[2566] | 581 | |
---|
[0] | 582 | /** |
---|
| 583 | Set a category parent |
---|
[2566] | 584 | |
---|
[0] | 585 | @param id <b>integer</b> Category ID |
---|
| 586 | @param parent <b>integer</b> Parent Category ID |
---|
| 587 | */ |
---|
| 588 | public function setCategoryParent($id,$parent) |
---|
| 589 | { |
---|
| 590 | $this->categories()->setNodeParent($id,$parent); |
---|
| 591 | $this->triggerBlog(); |
---|
| 592 | } |
---|
[2566] | 593 | |
---|
[0] | 594 | /** |
---|
| 595 | Set category position |
---|
[2566] | 596 | |
---|
[0] | 597 | @param id <b>integer</b> Category ID |
---|
| 598 | @param sibling <b>integer</b> Sibling Category ID |
---|
| 599 | @param move <b>integer</b> Order (before|after) |
---|
| 600 | */ |
---|
| 601 | public function setCategoryPosition($id,$sibling,$move) |
---|
| 602 | { |
---|
| 603 | $this->categories()->setNodePosition($id,$sibling,$move); |
---|
| 604 | $this->triggerBlog(); |
---|
| 605 | } |
---|
[2566] | 606 | |
---|
[0] | 607 | /** |
---|
| 608 | Deletes a category. |
---|
[2566] | 609 | |
---|
[0] | 610 | @param id <b>integer</b> Category ID |
---|
| 611 | */ |
---|
| 612 | public function delCategory($id) |
---|
| 613 | { |
---|
| 614 | if (!$this->core->auth->check('categories',$this->id)) { |
---|
| 615 | throw new Exception(__('You are not allowed to delete categories')); |
---|
| 616 | } |
---|
[2566] | 617 | |
---|
[0] | 618 | $strReq = 'SELECT COUNT(post_id) AS nb_post '. |
---|
| 619 | 'FROM '.$this->prefix.'post '. |
---|
| 620 | 'WHERE cat_id = '.(integer) $id.' '. |
---|
| 621 | "AND blog_id = '".$this->con->escape($this->id)."' "; |
---|
[2566] | 622 | |
---|
[0] | 623 | $rs = $this->con->select($strReq); |
---|
[2566] | 624 | |
---|
[0] | 625 | if ($rs->nb_post > 0) { |
---|
| 626 | throw new Exception(__('This category is not empty.')); |
---|
| 627 | } |
---|
[2566] | 628 | |
---|
[0] | 629 | $this->categories()->deleteNode($id,true); |
---|
| 630 | $this->triggerBlog(); |
---|
| 631 | } |
---|
[2566] | 632 | |
---|
[0] | 633 | /** |
---|
| 634 | Reset categories order and relocate them to first level |
---|
| 635 | */ |
---|
| 636 | public function resetCategoriesOrder() |
---|
| 637 | { |
---|
| 638 | if (!$this->core->auth->check('categories',$this->id)) { |
---|
[822] | 639 | throw new Exception(__('You are not allowed to reset categories order')); |
---|
[0] | 640 | } |
---|
[2566] | 641 | |
---|
[0] | 642 | $this->categories()->resetOrder(); |
---|
[734] | 643 | $this->triggerBlog(); |
---|
[0] | 644 | } |
---|
[2566] | 645 | |
---|
[0] | 646 | private function checkCategory($title,$url,$id=null) |
---|
| 647 | { |
---|
[1429] | 648 | # Let's check if URL is taken... |
---|
[2566] | 649 | $strReq = |
---|
[1429] | 650 | 'SELECT cat_url FROM '.$this->prefix.'category '. |
---|
| 651 | "WHERE cat_url = '".$this->con->escape($url)."' ". |
---|
| 652 | ($id ? 'AND cat_id <> '.(integer) $id. ' ' : ''). |
---|
| 653 | "AND blog_id = '".$this->con->escape($this->id)."' ". |
---|
| 654 | 'ORDER BY cat_url DESC'; |
---|
[2566] | 655 | |
---|
[0] | 656 | $rs = $this->con->select($strReq); |
---|
[2566] | 657 | |
---|
[1429] | 658 | if (!$rs->isEmpty()) |
---|
| 659 | { |
---|
[1743] | 660 | if ($this->con->driver() == 'mysql' || $this->con->driver() == 'mysqli') { |
---|
[1429] | 661 | $clause = "REGEXP '^".$this->con->escape($url)."[0-9]+$'"; |
---|
| 662 | } elseif ($this->con->driver() == 'pgsql') { |
---|
| 663 | $clause = "~ '^".$this->con->escape($url)."[0-9]+$'"; |
---|
| 664 | } else { |
---|
| 665 | $clause = "LIKE '".$this->con->escape($url)."%'"; |
---|
| 666 | } |
---|
[2566] | 667 | $strReq = |
---|
[1429] | 668 | 'SELECT cat_url FROM '.$this->prefix.'category '. |
---|
| 669 | "WHERE cat_url ".$clause.' '. |
---|
| 670 | ($id ? 'AND cat_id <> '.(integer) $id. ' ' : ''). |
---|
| 671 | "AND blog_id = '".$this->con->escape($this->id)."' ". |
---|
| 672 | 'ORDER BY cat_url DESC '; |
---|
[2566] | 673 | |
---|
[1429] | 674 | $rs = $this->con->select($strReq); |
---|
| 675 | $a = array(); |
---|
| 676 | while ($rs->fetch()) { |
---|
| 677 | $a[] = $rs->cat_url; |
---|
| 678 | } |
---|
[2566] | 679 | |
---|
[1429] | 680 | natsort($a); |
---|
| 681 | $t_url = end($a); |
---|
[2566] | 682 | |
---|
[1429] | 683 | if (preg_match('/(.*?)([0-9]+)$/',$t_url,$m)) { |
---|
| 684 | $i = (integer) $m[2]; |
---|
| 685 | $url = $m[1]; |
---|
| 686 | } else { |
---|
| 687 | $i = 1; |
---|
| 688 | } |
---|
[2566] | 689 | |
---|
[1429] | 690 | return $url.($i+1); |
---|
[0] | 691 | } |
---|
[2566] | 692 | |
---|
[1429] | 693 | # URL is empty? |
---|
| 694 | if ($url == '') { |
---|
| 695 | throw new Exception(__('Empty category URL')); |
---|
| 696 | } |
---|
[2566] | 697 | |
---|
[1429] | 698 | return $url; |
---|
[0] | 699 | } |
---|
[2566] | 700 | |
---|
[0] | 701 | private function getCategoryCursor($cur,$id=null) |
---|
| 702 | { |
---|
| 703 | if ($cur->cat_title == '') { |
---|
| 704 | throw new Exception(__('You must provide a category title')); |
---|
| 705 | } |
---|
[2566] | 706 | |
---|
[0] | 707 | # If we don't have any cat_url, let's do one |
---|
| 708 | if ($cur->cat_url == '') { |
---|
| 709 | $cur->cat_url = text::tidyURL($cur->cat_title,false); |
---|
| 710 | } |
---|
[2566] | 711 | |
---|
[0] | 712 | # Still empty ? |
---|
| 713 | if ($cur->cat_url == '') { |
---|
| 714 | throw new Exception(__('You must provide a category URL')); |
---|
| 715 | } else { |
---|
| 716 | $cur->cat_url = text::tidyURL($cur->cat_url,true); |
---|
| 717 | } |
---|
[2566] | 718 | |
---|
[0] | 719 | # Check if title or url are unique |
---|
[1429] | 720 | $cur->cat_url = $this->checkCategory($cur->cat_title,$cur->cat_url,$id); |
---|
[2566] | 721 | |
---|
[0] | 722 | if ($cur->cat_desc !== null) { |
---|
| 723 | $cur->cat_desc = $this->core->HTMLfilter($cur->cat_desc); |
---|
| 724 | } |
---|
| 725 | } |
---|
| 726 | //@} |
---|
[2566] | 727 | |
---|
[0] | 728 | /// @name Entries management methods |
---|
| 729 | //@{ |
---|
| 730 | /** |
---|
| 731 | Retrieves entries. <b>$params</b> is an array taking the following |
---|
| 732 | optionnal parameters: |
---|
[2566] | 733 | |
---|
[0] | 734 | - no_content: Don't retrieve entry content (excerpt and content) |
---|
| 735 | - post_type: Get only entries with given type (default "post", array for many types and '' for no type) |
---|
[1541] | 736 | - post_id: (integer or array) Get entry with given post_id |
---|
[0] | 737 | - post_url: Get entry with given post_url field |
---|
| 738 | - user_id: (integer) Get entries belonging to given user ID |
---|
| 739 | - cat_id: (string or array) Get entries belonging to given category ID |
---|
| 740 | - cat_id_not: deprecated (use cat_id with "id ?not" instead) |
---|
| 741 | - cat_url: (string or array) Get entries belonging to given category URL |
---|
| 742 | - cat_url_not: deprecated (use cat_url with "url ?not" instead) |
---|
| 743 | - post_status: (integer) Get entries with given post_status |
---|
| 744 | - post_selected: (boolean) Get select flaged entries |
---|
| 745 | - post_year: (integer) Get entries with given year |
---|
| 746 | - post_month: (integer) Get entries with given month |
---|
| 747 | - post_day: (integer) Get entries with given day |
---|
| 748 | - post_lang: Get entries with given language code |
---|
| 749 | - search: Get entries corresponding of the following search string |
---|
| 750 | - columns: (array) More columns to retrieve |
---|
| 751 | - sql: Append SQL string at the end of the query |
---|
| 752 | - from: Append SQL string after "FROM" statement in query |
---|
| 753 | - order: Order of results (default "ORDER BY post_dt DES") |
---|
| 754 | - limit: Limit parameter |
---|
[774] | 755 | - sql_only : return the sql request instead of results. Only ids are selected |
---|
[1541] | 756 | - exclude_post_id : (integer or array) Exclude entries with given post_id |
---|
[2566] | 757 | |
---|
[0] | 758 | Please note that on every cat_id or cat_url, you can add ?not to exclude |
---|
| 759 | the category and ?sub to get subcategories. |
---|
[2566] | 760 | |
---|
[0] | 761 | @param params <b>array</b> Parameters |
---|
| 762 | @param count_only <b>boolean</b> Only counts results |
---|
[191] | 763 | @return <b>record</b> A record with some more capabilities or the SQL request |
---|
[0] | 764 | */ |
---|
[774] | 765 | public function getPosts($params=array(),$count_only=false) |
---|
[0] | 766 | { |
---|
[817] | 767 | # --BEHAVIOR-- coreBlogBeforeGetPosts |
---|
[819] | 768 | $params = new ArrayObject($params); |
---|
[817] | 769 | $this->core->callBehavior('coreBlogBeforeGetPosts',$params); |
---|
| 770 | |
---|
[0] | 771 | if ($count_only) |
---|
| 772 | { |
---|
[2315] | 773 | $strReq = 'SELECT count(DISTINCT P.post_id) '; |
---|
[0] | 774 | } |
---|
[2566] | 775 | elseif (!empty($params['sql_only'])) |
---|
[774] | 776 | { |
---|
[2317] | 777 | $strReq = 'SELECT P.post_id '; |
---|
[774] | 778 | } |
---|
[0] | 779 | else |
---|
| 780 | { |
---|
| 781 | if (!empty($params['no_content'])) { |
---|
| 782 | $content_req = ''; |
---|
| 783 | } else { |
---|
| 784 | $content_req = |
---|
| 785 | 'post_excerpt, post_excerpt_xhtml, '. |
---|
| 786 | 'post_content, post_content_xhtml, post_notes, '; |
---|
| 787 | } |
---|
[2566] | 788 | |
---|
[0] | 789 | if (!empty($params['columns']) && is_array($params['columns'])) { |
---|
| 790 | $content_req .= implode(', ',$params['columns']).', '; |
---|
| 791 | } |
---|
[2566] | 792 | |
---|
[2318] | 793 | |
---|
| 794 | $strReq = |
---|
| 795 | 'SELECT P.post_id, P.blog_id, P.user_id, P.cat_id, post_dt, '. |
---|
[0] | 796 | 'post_tz, post_creadt, post_upddt, post_format, post_password, '. |
---|
| 797 | 'post_url, post_lang, post_title, '.$content_req. |
---|
| 798 | 'post_type, post_meta, post_status, post_selected, post_position, '. |
---|
| 799 | 'post_open_comment, post_open_tb, nb_comment, nb_trackback, '. |
---|
| 800 | 'U.user_name, U.user_firstname, U.user_displayname, U.user_email, '. |
---|
| 801 | 'U.user_url, '. |
---|
| 802 | 'C.cat_title, C.cat_url, C.cat_desc '; |
---|
| 803 | } |
---|
[2566] | 804 | |
---|
[0] | 805 | $strReq .= |
---|
| 806 | 'FROM '.$this->prefix.'post P '. |
---|
| 807 | 'INNER JOIN '.$this->prefix.'user U ON U.user_id = P.user_id '. |
---|
| 808 | 'LEFT OUTER JOIN '.$this->prefix.'category C ON P.cat_id = C.cat_id '; |
---|
[2309] | 809 | |
---|
[0] | 810 | if (!empty($params['from'])) { |
---|
| 811 | $strReq .= $params['from'].' '; |
---|
| 812 | } |
---|
[2566] | 813 | |
---|
[0] | 814 | $strReq .= |
---|
| 815 | "WHERE P.blog_id = '".$this->con->escape($this->id)."' "; |
---|
[2566] | 816 | |
---|
[0] | 817 | if (!$this->core->auth->check('contentadmin',$this->id)) { |
---|
| 818 | $strReq .= 'AND ((post_status = 1 '; |
---|
[2566] | 819 | |
---|
[0] | 820 | if ($this->without_password) { |
---|
| 821 | $strReq .= 'AND post_password IS NULL '; |
---|
| 822 | } |
---|
| 823 | $strReq .= ') '; |
---|
[2566] | 824 | |
---|
[0] | 825 | if ($this->core->auth->userID()) { |
---|
| 826 | $strReq .= "OR P.user_id = '".$this->con->escape($this->core->auth->userID())."')"; |
---|
| 827 | } else { |
---|
| 828 | $strReq .= ') '; |
---|
| 829 | } |
---|
| 830 | } |
---|
[2566] | 831 | |
---|
[0] | 832 | #Adding parameters |
---|
| 833 | if (isset($params['post_type'])) |
---|
| 834 | { |
---|
[407] | 835 | if (is_array($params['post_type']) || $params['post_type'] != '') { |
---|
| 836 | $strReq .= 'AND post_type '.$this->con->in($params['post_type']); |
---|
| 837 | } |
---|
[0] | 838 | } |
---|
| 839 | else |
---|
| 840 | { |
---|
| 841 | $strReq .= "AND post_type = 'post' "; |
---|
| 842 | } |
---|
[2566] | 843 | |
---|
[836] | 844 | if (isset($params['post_id']) && $params['post_id'] !== '') { |
---|
[0] | 845 | if (is_array($params['post_id'])) { |
---|
| 846 | array_walk($params['post_id'],create_function('&$v,$k','if($v!==null){$v=(integer)$v;}')); |
---|
| 847 | } else { |
---|
| 848 | $params['post_id'] = array((integer) $params['post_id']); |
---|
| 849 | } |
---|
| 850 | $strReq .= 'AND P.post_id '.$this->con->in($params['post_id']); |
---|
| 851 | } |
---|
[2566] | 852 | |
---|
[1541] | 853 | if (isset($params['exclude_post_id']) && $params['exclude_post_id'] !== '') { |
---|
| 854 | if (is_array($params['exclude_post_id'])) { |
---|
| 855 | array_walk($params['exclude_post_id'],create_function('&$v,$k','if($v!==null){$v=(integer)$v;}')); |
---|
| 856 | } else { |
---|
| 857 | $params['exclude_post_id'] = array((integer) $params['exclude_post_id']); |
---|
| 858 | } |
---|
[1544] | 859 | $strReq .= 'AND P.post_id NOT '.$this->con->in($params['exclude_post_id']); |
---|
[1540] | 860 | } |
---|
[2566] | 861 | |
---|
[836] | 862 | if (isset($params['post_url']) && $params['post_url'] !== '') { |
---|
[0] | 863 | $strReq .= "AND post_url = '".$this->con->escape($params['post_url'])."' "; |
---|
| 864 | } |
---|
[2566] | 865 | |
---|
[0] | 866 | if (!empty($params['user_id'])) { |
---|
| 867 | $strReq .= "AND U.user_id = '".$this->con->escape($params['user_id'])."' "; |
---|
| 868 | } |
---|
[2566] | 869 | |
---|
[836] | 870 | if (isset($params['cat_id']) && $params['cat_id'] !== '') |
---|
[0] | 871 | { |
---|
| 872 | if (!is_array($params['cat_id'])) { |
---|
| 873 | $params['cat_id'] = array($params['cat_id']); |
---|
| 874 | } |
---|
| 875 | if (!empty($params['cat_id_not'])) { |
---|
| 876 | array_walk($params['cat_id'],create_function('&$v,$k','$v=$v." ?not";')); |
---|
| 877 | } |
---|
| 878 | $strReq .= 'AND '.$this->getPostsCategoryFilter($params['cat_id'],'cat_id').' '; |
---|
| 879 | } |
---|
[836] | 880 | elseif (isset($params['cat_url']) && $params['cat_url'] !== '') |
---|
[0] | 881 | { |
---|
| 882 | if (!is_array($params['cat_url'])) { |
---|
| 883 | $params['cat_url'] = array($params['cat_url']); |
---|
| 884 | } |
---|
| 885 | if (!empty($params['cat_url_not'])) { |
---|
| 886 | array_walk($params['cat_url'],create_function('&$v,$k','$v=$v." ?not";')); |
---|
| 887 | } |
---|
| 888 | $strReq .= 'AND '.$this->getPostsCategoryFilter($params['cat_url'],'cat_url').' '; |
---|
| 889 | } |
---|
[2566] | 890 | |
---|
[0] | 891 | /* Other filters */ |
---|
| 892 | if (isset($params['post_status'])) { |
---|
| 893 | $strReq .= 'AND post_status = '.(integer) $params['post_status'].' '; |
---|
| 894 | } |
---|
[2566] | 895 | |
---|
[0] | 896 | if (isset($params['post_selected'])) { |
---|
| 897 | $strReq .= 'AND post_selected = '.(integer) $params['post_selected'].' '; |
---|
| 898 | } |
---|
[2566] | 899 | |
---|
[0] | 900 | if (!empty($params['post_year'])) { |
---|
| 901 | $strReq .= 'AND '.$this->con->dateFormat('post_dt','%Y').' = '. |
---|
| 902 | "'".sprintf('%04d',$params['post_year'])."' "; |
---|
| 903 | } |
---|
[2566] | 904 | |
---|
[0] | 905 | if (!empty($params['post_month'])) { |
---|
| 906 | $strReq .= 'AND '.$this->con->dateFormat('post_dt','%m').' = '. |
---|
| 907 | "'".sprintf('%02d',$params['post_month'])."' "; |
---|
| 908 | } |
---|
[2566] | 909 | |
---|
[0] | 910 | if (!empty($params['post_day'])) { |
---|
| 911 | $strReq .= 'AND '.$this->con->dateFormat('post_dt','%d').' = '. |
---|
| 912 | "'".sprintf('%02d',$params['post_day'])."' "; |
---|
| 913 | } |
---|
[2566] | 914 | |
---|
[0] | 915 | if (!empty($params['post_lang'])) { |
---|
| 916 | $strReq .= "AND P.post_lang = '".$this->con->escape($params['post_lang'])."' "; |
---|
| 917 | } |
---|
[2566] | 918 | |
---|
[0] | 919 | if (!empty($params['search'])) |
---|
| 920 | { |
---|
| 921 | $words = text::splitWords($params['search']); |
---|
[2566] | 922 | |
---|
[0] | 923 | if (!empty($words)) |
---|
| 924 | { |
---|
| 925 | # --BEHAVIOR-- corePostSearch |
---|
| 926 | if ($this->core->hasBehavior('corePostSearch')) { |
---|
| 927 | $this->core->callBehavior('corePostSearch',$this->core,array(&$words,&$strReq,&$params)); |
---|
| 928 | } |
---|
[2566] | 929 | |
---|
[0] | 930 | if ($words) |
---|
| 931 | { |
---|
| 932 | foreach ($words as $i => $w) { |
---|
| 933 | $words[$i] = "post_words LIKE '%".$this->con->escape($w)."%'"; |
---|
| 934 | } |
---|
| 935 | $strReq .= 'AND '.implode(' AND ',$words).' '; |
---|
| 936 | } |
---|
| 937 | } |
---|
| 938 | } |
---|
[2566] | 939 | |
---|
[2309] | 940 | if (isset($params['media'])) { |
---|
| 941 | if ($params['media'] == '0') { |
---|
[2318] | 942 | $strReq .= 'AND NOT '; |
---|
| 943 | } else { |
---|
[2566] | 944 | $strReq .= 'AND '; |
---|
[2309] | 945 | } |
---|
[2318] | 946 | $strReq .= 'EXISTS (SELECT M.post_id FROM '.$this->prefix.'post_media M '. |
---|
| 947 | 'WHERE M.post_id = P.post_id '; |
---|
[2317] | 948 | if (isset($params['link_type'])) { |
---|
[2318] | 949 | $strReq .= " AND M.link_type ".$this->con->in($params['link_type'])." "; |
---|
[2317] | 950 | } |
---|
| 951 | $strReq .= ")"; |
---|
[2309] | 952 | } |
---|
| 953 | |
---|
[0] | 954 | if (!empty($params['sql'])) { |
---|
| 955 | $strReq .= $params['sql'].' '; |
---|
| 956 | } |
---|
[2309] | 957 | |
---|
[0] | 958 | if (!$count_only) |
---|
| 959 | { |
---|
| 960 | if (!empty($params['order'])) { |
---|
| 961 | $strReq .= 'ORDER BY '.$this->con->escape($params['order']).' '; |
---|
| 962 | } else { |
---|
| 963 | $strReq .= 'ORDER BY post_dt DESC '; |
---|
| 964 | } |
---|
| 965 | } |
---|
[2566] | 966 | |
---|
[0] | 967 | if (!$count_only && !empty($params['limit'])) { |
---|
| 968 | $strReq .= $this->con->limit($params['limit']); |
---|
| 969 | } |
---|
[2566] | 970 | |
---|
[774] | 971 | if (!empty($params['sql_only'])) { |
---|
[191] | 972 | return $strReq; |
---|
| 973 | } |
---|
[2309] | 974 | |
---|
[0] | 975 | $rs = $this->con->select($strReq); |
---|
| 976 | $rs->core = $this->core; |
---|
| 977 | $rs->_nb_media = array(); |
---|
| 978 | $rs->extend('rsExtPost'); |
---|
[2566] | 979 | |
---|
[0] | 980 | # --BEHAVIOR-- coreBlogGetPosts |
---|
| 981 | $this->core->callBehavior('coreBlogGetPosts',$rs); |
---|
[2566] | 982 | |
---|
[0] | 983 | return $rs; |
---|
| 984 | } |
---|
[2566] | 985 | |
---|
[0] | 986 | /** |
---|
| 987 | Returns a record with post id, title and date for next or previous post |
---|
| 988 | according to the post ID. |
---|
| 989 | $dir could be 1 (next post) or -1 (previous post). |
---|
[2566] | 990 | |
---|
[0] | 991 | @param post_id <b>integer</b> Post ID |
---|
| 992 | @param dir <b>integer</b> Search direction |
---|
| 993 | @param restrict_to_category <b>boolean</b> Restrict to post with same category |
---|
| 994 | @param restrict_to_lang <b>boolean</b> Restrict to post with same lang |
---|
| 995 | @return record |
---|
| 996 | */ |
---|
| 997 | public function getNextPost($post,$dir,$restrict_to_category=false, $restrict_to_lang=false) |
---|
| 998 | { |
---|
| 999 | $dt = $post->post_dt; |
---|
| 1000 | $post_id = (integer) $post->post_id; |
---|
[2566] | 1001 | |
---|
[0] | 1002 | if($dir > 0) { |
---|
[336] | 1003 | $sign = '>'; |
---|
| 1004 | $order = 'ASC'; |
---|
| 1005 | } |
---|
| 1006 | else { |
---|
| 1007 | $sign = '<'; |
---|
| 1008 | $order = 'DESC'; |
---|
| 1009 | } |
---|
[2566] | 1010 | |
---|
[0] | 1011 | $params['post_type'] = $post->post_type; |
---|
| 1012 | $params['limit'] = 1; |
---|
| 1013 | $params['order'] = 'post_dt '.$order.', P.post_id '.$order; |
---|
| 1014 | $params['sql'] = |
---|
| 1015 | 'AND ( '. |
---|
| 1016 | " (post_dt = '".$this->con->escape($dt)."' AND P.post_id ".$sign." ".$post_id.") ". |
---|
| 1017 | " OR post_dt ".$sign." '".$this->con->escape($dt)."' ". |
---|
| 1018 | ') '; |
---|
[2566] | 1019 | |
---|
[0] | 1020 | if ($restrict_to_category) { |
---|
| 1021 | $params['sql'] .= $post->cat_id ? 'AND P.cat_id = '.(integer) $post->cat_id.' ' : 'AND P.cat_id IS NULL '; |
---|
| 1022 | } |
---|
[2566] | 1023 | |
---|
[0] | 1024 | if ($restrict_to_lang) { |
---|
| 1025 | $params['sql'] .= $post->post_lang ? 'AND P.post_lang = \''. $this->con->escape($post->post_lang) .'\' ': 'AND P.post_lang IS NULL '; |
---|
| 1026 | } |
---|
[2566] | 1027 | |
---|
[0] | 1028 | $rs = $this->getPosts($params); |
---|
[2566] | 1029 | |
---|
[0] | 1030 | if ($rs->isEmpty()) { |
---|
| 1031 | return null; |
---|
| 1032 | } |
---|
[2566] | 1033 | |
---|
[0] | 1034 | return $rs; |
---|
| 1035 | } |
---|
[2566] | 1036 | |
---|
[0] | 1037 | /** |
---|
| 1038 | Retrieves different languages and post count on blog, based on post_lang |
---|
| 1039 | field. <var>$params</var> is an array taking the following optionnal |
---|
| 1040 | parameters: |
---|
[2566] | 1041 | |
---|
[0] | 1042 | - post_type: Get only entries with given type (default "post", '' for no type) |
---|
| 1043 | - lang: retrieve post count for selected lang |
---|
| 1044 | - order: order statement (default post_lang DESC) |
---|
[2566] | 1045 | |
---|
[0] | 1046 | @param params <b>array</b> Parameters |
---|
| 1047 | @return record |
---|
| 1048 | */ |
---|
| 1049 | public function getLangs($params=array()) |
---|
| 1050 | { |
---|
| 1051 | $strReq = 'SELECT COUNT(post_id) as nb_post, post_lang '. |
---|
| 1052 | 'FROM '.$this->prefix.'post '. |
---|
| 1053 | "WHERE blog_id = '".$this->con->escape($this->id)."' ". |
---|
| 1054 | "AND post_lang <> '' ". |
---|
| 1055 | "AND post_lang IS NOT NULL "; |
---|
[2566] | 1056 | |
---|
[0] | 1057 | if (!$this->core->auth->check('contentadmin',$this->id)) { |
---|
| 1058 | $strReq .= 'AND ((post_status = 1 '; |
---|
[2566] | 1059 | |
---|
[0] | 1060 | if ($this->without_password) { |
---|
| 1061 | $strReq .= 'AND post_password IS NULL '; |
---|
| 1062 | } |
---|
| 1063 | $strReq .= ') '; |
---|
[2566] | 1064 | |
---|
[0] | 1065 | if ($this->core->auth->userID()) { |
---|
| 1066 | $strReq .= "OR user_id = '".$this->con->escape($this->core->auth->userID())."')"; |
---|
| 1067 | } else { |
---|
| 1068 | $strReq .= ') '; |
---|
| 1069 | } |
---|
| 1070 | } |
---|
[2566] | 1071 | |
---|
[0] | 1072 | if (isset($params['post_type'])) { |
---|
| 1073 | if ($params['post_type'] != '') { |
---|
| 1074 | $strReq .= "AND post_type = '".$this->con->escape($params['post_type'])."' "; |
---|
| 1075 | } |
---|
| 1076 | } else { |
---|
| 1077 | $strReq .= "AND post_type = 'post' "; |
---|
| 1078 | } |
---|
[2566] | 1079 | |
---|
[0] | 1080 | if (isset($params['lang'])) { |
---|
| 1081 | $strReq .= "AND post_lang = '".$this->con->escape($params['lang'])."' "; |
---|
| 1082 | } |
---|
[2566] | 1083 | |
---|
[0] | 1084 | $strReq .= 'GROUP BY post_lang '; |
---|
[2566] | 1085 | |
---|
[0] | 1086 | $order = 'desc'; |
---|
| 1087 | if (!empty($params['order']) && preg_match('/^(desc|asc)$/i',$params['order'])) { |
---|
| 1088 | $order = $params['order']; |
---|
| 1089 | } |
---|
| 1090 | $strReq .= 'ORDER BY post_lang '.$order.' '; |
---|
[2566] | 1091 | |
---|
[0] | 1092 | return $this->con->select($strReq); |
---|
| 1093 | } |
---|
[2566] | 1094 | |
---|
[0] | 1095 | /** |
---|
| 1096 | Returns a record with all distinct blog dates and post count. |
---|
| 1097 | <var>$params</var> is an array taking the following optionnal parameters: |
---|
[2566] | 1098 | |
---|
[0] | 1099 | - type: (day|month|year) Get days, months or years |
---|
| 1100 | - year: (integer) Get dates for given year |
---|
| 1101 | - month: (integer) Get dates for given month |
---|
| 1102 | - day: (integer) Get dates for given day |
---|
| 1103 | - cat_id: (integer) Category ID filter |
---|
| 1104 | - cat_url: Category URL filter |
---|
| 1105 | - post_lang: lang of the posts |
---|
| 1106 | - next: Get date following match |
---|
| 1107 | - previous: Get date before match |
---|
| 1108 | - order: Sort by date "ASC" or "DESC" |
---|
[2566] | 1109 | |
---|
[0] | 1110 | @param params <b>array</b> Parameters array |
---|
| 1111 | @return record |
---|
| 1112 | */ |
---|
| 1113 | public function getDates($params=array()) |
---|
| 1114 | { |
---|
| 1115 | $dt_f = '%Y-%m-%d'; |
---|
| 1116 | $dt_fc = '%Y%m%d'; |
---|
| 1117 | if (isset($params['type'])) { |
---|
| 1118 | if ($params['type'] == 'year') { |
---|
| 1119 | $dt_f = '%Y-01-01'; |
---|
| 1120 | $dt_fc = '%Y0101'; |
---|
| 1121 | } elseif ($params['type'] == 'month') { |
---|
| 1122 | $dt_f = '%Y-%m-01'; |
---|
| 1123 | $dt_fc = '%Y%m01'; |
---|
| 1124 | } |
---|
| 1125 | } |
---|
| 1126 | $dt_f .= ' 00:00:00'; |
---|
| 1127 | $dt_fc .= '000000'; |
---|
[2566] | 1128 | |
---|
[0] | 1129 | $cat_field = $catReq = $limit = ''; |
---|
[2566] | 1130 | |
---|
[836] | 1131 | if (isset($params['cat_id']) && $params['cat_id'] !== '') { |
---|
[0] | 1132 | $catReq = 'AND P.cat_id = '.(integer) $params['cat_id'].' '; |
---|
| 1133 | $cat_field = ', C.cat_url '; |
---|
[836] | 1134 | } elseif (isset($params['cat_url']) && $params['cat_url'] !== '') { |
---|
[0] | 1135 | $catReq = "AND C.cat_url = '".$this->con->escape($params['cat_url'])."' "; |
---|
| 1136 | $cat_field = ', C.cat_url '; |
---|
| 1137 | } |
---|
| 1138 | if (!empty($params['post_lang'])) { |
---|
| 1139 | $catReq = 'AND P.post_lang = \''. $params['post_lang'].'\' '; |
---|
| 1140 | } |
---|
[2566] | 1141 | |
---|
[0] | 1142 | $strReq = 'SELECT DISTINCT('.$this->con->dateFormat('post_dt',$dt_f).') AS dt '. |
---|
| 1143 | $cat_field. |
---|
| 1144 | ',COUNT(P.post_id) AS nb_post '. |
---|
| 1145 | 'FROM '.$this->prefix.'post P LEFT JOIN '.$this->prefix.'category C '. |
---|
| 1146 | 'ON P.cat_id = C.cat_id '. |
---|
| 1147 | "WHERE P.blog_id = '".$this->con->escape($this->id)."' ". |
---|
| 1148 | $catReq; |
---|
[2566] | 1149 | |
---|
[0] | 1150 | if (!$this->core->auth->check('contentadmin',$this->id)) { |
---|
| 1151 | $strReq .= 'AND ((post_status = 1 '; |
---|
[2566] | 1152 | |
---|
[0] | 1153 | if ($this->without_password) { |
---|
| 1154 | $strReq .= 'AND post_password IS NULL '; |
---|
| 1155 | } |
---|
| 1156 | $strReq .= ') '; |
---|
[2566] | 1157 | |
---|
[0] | 1158 | if ($this->core->auth->userID()) { |
---|
| 1159 | $strReq .= "OR P.user_id = '".$this->con->escape($this->core->auth->userID())."')"; |
---|
| 1160 | } else { |
---|
| 1161 | $strReq .= ') '; |
---|
| 1162 | } |
---|
| 1163 | } |
---|
[2566] | 1164 | |
---|
[0] | 1165 | if (!empty($params['post_type'])) { |
---|
| 1166 | $strReq .= "AND post_type ".$this->con->in($params['post_type'])." "; |
---|
| 1167 | } else { |
---|
| 1168 | $strReq .= "AND post_type = 'post' "; |
---|
| 1169 | } |
---|
[2566] | 1170 | |
---|
[0] | 1171 | if (!empty($params['year'])) { |
---|
| 1172 | $strReq .= 'AND '.$this->con->dateFormat('post_dt','%Y')." = '".sprintf('%04d',$params['year'])."' "; |
---|
| 1173 | } |
---|
[2566] | 1174 | |
---|
[0] | 1175 | if (!empty($params['month'])) { |
---|
| 1176 | $strReq .= 'AND '.$this->con->dateFormat('post_dt','%m')." = '".sprintf('%02d',$params['month'])."' "; |
---|
| 1177 | } |
---|
[2566] | 1178 | |
---|
[0] | 1179 | if (!empty($params['day'])) { |
---|
| 1180 | $strReq .= 'AND '.$this->con->dateFormat('post_dt','%d')." = '".sprintf('%02d',$params['day'])."' "; |
---|
| 1181 | } |
---|
[2566] | 1182 | |
---|
[0] | 1183 | # Get next or previous date |
---|
| 1184 | if (!empty($params['next']) || !empty($params['previous'])) |
---|
| 1185 | { |
---|
| 1186 | if (!empty($params['next'])) { |
---|
| 1187 | $pdir = ' > '; |
---|
| 1188 | $params['order'] = 'asc'; |
---|
| 1189 | $dt = $params['next']; |
---|
| 1190 | } else { |
---|
| 1191 | $pdir = ' < '; |
---|
| 1192 | $params['order'] = 'desc'; |
---|
| 1193 | $dt = $params['previous']; |
---|
| 1194 | } |
---|
[2566] | 1195 | |
---|
[0] | 1196 | $dt = date('YmdHis',strtotime($dt)); |
---|
[2566] | 1197 | |
---|
[0] | 1198 | $strReq .= 'AND '.$this->con->dateFormat('post_dt',$dt_fc).$pdir."'".$dt."' "; |
---|
| 1199 | $limit = $this->con->limit(1); |
---|
| 1200 | } |
---|
[2566] | 1201 | |
---|
[0] | 1202 | $strReq .= 'GROUP BY dt '.$cat_field; |
---|
[2566] | 1203 | |
---|
[0] | 1204 | $order = 'desc'; |
---|
| 1205 | if (!empty($params['order']) && preg_match('/^(desc|asc)$/i',$params['order'])) { |
---|
| 1206 | $order = $params['order']; |
---|
| 1207 | } |
---|
[2566] | 1208 | |
---|
[0] | 1209 | $strReq .= |
---|
| 1210 | 'ORDER BY dt '.$order.' '. |
---|
| 1211 | $limit; |
---|
[2566] | 1212 | |
---|
[0] | 1213 | $rs = $this->con->select($strReq); |
---|
| 1214 | $rs->extend('rsExtDates'); |
---|
| 1215 | return $rs; |
---|
| 1216 | } |
---|
[2566] | 1217 | |
---|
[0] | 1218 | /** |
---|
| 1219 | Creates a new entry. Takes a cursor as input and returns the new entry |
---|
| 1220 | ID. |
---|
[2566] | 1221 | |
---|
[0] | 1222 | @param cur <b>cursor</b> Post cursor |
---|
| 1223 | @return <b>integer</b> New post ID |
---|
| 1224 | */ |
---|
| 1225 | public function addPost($cur) |
---|
| 1226 | { |
---|
| 1227 | if (!$this->core->auth->check('usage,contentadmin',$this->id)) { |
---|
| 1228 | throw new Exception(__('You are not allowed to create an entry')); |
---|
| 1229 | } |
---|
[2566] | 1230 | |
---|
[0] | 1231 | $this->con->writeLock($this->prefix.'post'); |
---|
| 1232 | try |
---|
| 1233 | { |
---|
| 1234 | # Get ID |
---|
| 1235 | $rs = $this->con->select( |
---|
| 1236 | 'SELECT MAX(post_id) '. |
---|
[2566] | 1237 | 'FROM '.$this->prefix.'post ' |
---|
[0] | 1238 | ); |
---|
[2566] | 1239 | |
---|
[0] | 1240 | $cur->post_id = (integer) $rs->f(0) + 1; |
---|
| 1241 | $cur->blog_id = (string) $this->id; |
---|
| 1242 | $cur->post_creadt = date('Y-m-d H:i:s'); |
---|
| 1243 | $cur->post_upddt = date('Y-m-d H:i:s'); |
---|
| 1244 | $cur->post_tz = $this->core->auth->getInfo('user_tz'); |
---|
[2566] | 1245 | |
---|
[0] | 1246 | # Post excerpt and content |
---|
| 1247 | $this->getPostContent($cur,$cur->post_id); |
---|
[2566] | 1248 | |
---|
[0] | 1249 | $this->getPostCursor($cur); |
---|
[2566] | 1250 | |
---|
[0] | 1251 | $cur->post_url = $this->getPostURL($cur->post_url,$cur->post_dt,$cur->post_title,$cur->post_id); |
---|
[2566] | 1252 | |
---|
[0] | 1253 | if (!$this->core->auth->check('publish,contentadmin',$this->id)) { |
---|
| 1254 | $cur->post_status = -2; |
---|
| 1255 | } |
---|
[2566] | 1256 | |
---|
[0] | 1257 | # --BEHAVIOR-- coreBeforePostCreate |
---|
| 1258 | $this->core->callBehavior('coreBeforePostCreate',$this,$cur); |
---|
[2566] | 1259 | |
---|
[0] | 1260 | $cur->insert(); |
---|
| 1261 | $this->con->unlock(); |
---|
| 1262 | } |
---|
| 1263 | catch (Exception $e) |
---|
| 1264 | { |
---|
| 1265 | $this->con->unlock(); |
---|
| 1266 | throw $e; |
---|
| 1267 | } |
---|
[2566] | 1268 | |
---|
[0] | 1269 | # --BEHAVIOR-- coreAfterPostCreate |
---|
| 1270 | $this->core->callBehavior('coreAfterPostCreate',$this,$cur); |
---|
[2566] | 1271 | |
---|
[0] | 1272 | $this->triggerBlog(); |
---|
[2566] | 1273 | |
---|
[0] | 1274 | return $cur->post_id; |
---|
| 1275 | } |
---|
[2566] | 1276 | |
---|
[0] | 1277 | /** |
---|
| 1278 | Updates an existing post. |
---|
[2566] | 1279 | |
---|
[0] | 1280 | @param id <b>integer</b> Post ID |
---|
| 1281 | @param cur <b>cursor</b> Post cursor |
---|
| 1282 | */ |
---|
| 1283 | public function updPost($id,$cur) |
---|
| 1284 | { |
---|
| 1285 | if (!$this->core->auth->check('usage,contentadmin',$this->id)) { |
---|
| 1286 | throw new Exception(__('You are not allowed to update entries')); |
---|
| 1287 | } |
---|
[2566] | 1288 | |
---|
[0] | 1289 | $id = (integer) $id; |
---|
[2566] | 1290 | |
---|
[0] | 1291 | if (empty($id)) { |
---|
| 1292 | throw new Exception(__('No such entry ID')); |
---|
| 1293 | } |
---|
[2566] | 1294 | |
---|
[0] | 1295 | # Post excerpt and content |
---|
| 1296 | $this->getPostContent($cur,$id); |
---|
[2566] | 1297 | |
---|
[0] | 1298 | $this->getPostCursor($cur); |
---|
[2566] | 1299 | |
---|
[0] | 1300 | if ($cur->post_url !== null) { |
---|
| 1301 | $cur->post_url = $this->getPostURL($cur->post_url,$cur->post_dt,$cur->post_title,$id); |
---|
| 1302 | } |
---|
[2566] | 1303 | |
---|
[0] | 1304 | if (!$this->core->auth->check('publish,contentadmin',$this->id)) { |
---|
| 1305 | $cur->unsetField('post_status'); |
---|
| 1306 | } |
---|
[2566] | 1307 | |
---|
[0] | 1308 | $cur->post_upddt = date('Y-m-d H:i:s'); |
---|
[2566] | 1309 | |
---|
[335] | 1310 | #If user is only "usage", we need to check the post's owner |
---|
[0] | 1311 | if (!$this->core->auth->check('contentadmin',$this->id)) |
---|
| 1312 | { |
---|
| 1313 | $strReq = 'SELECT post_id '. |
---|
| 1314 | 'FROM '.$this->prefix.'post '. |
---|
| 1315 | 'WHERE post_id = '.$id.' '. |
---|
| 1316 | "AND user_id = '".$this->con->escape($this->core->auth->userID())."' "; |
---|
[2566] | 1317 | |
---|
[0] | 1318 | $rs = $this->con->select($strReq); |
---|
[2566] | 1319 | |
---|
[0] | 1320 | if ($rs->isEmpty()) { |
---|
| 1321 | throw new Exception(__('You are not allowed to edit this entry')); |
---|
| 1322 | } |
---|
| 1323 | } |
---|
[2566] | 1324 | |
---|
[0] | 1325 | # --BEHAVIOR-- coreBeforePostUpdate |
---|
| 1326 | $this->core->callBehavior('coreBeforePostUpdate',$this,$cur); |
---|
[2566] | 1327 | |
---|
[0] | 1328 | $cur->update('WHERE post_id = '.$id.' '); |
---|
[2566] | 1329 | |
---|
[0] | 1330 | # --BEHAVIOR-- coreAfterPostUpdate |
---|
| 1331 | $this->core->callBehavior('coreAfterPostUpdate',$this,$cur); |
---|
[2566] | 1332 | |
---|
[0] | 1333 | $this->triggerBlog(); |
---|
| 1334 | } |
---|
[2566] | 1335 | |
---|
[0] | 1336 | /** |
---|
| 1337 | Updates post status. |
---|
[2566] | 1338 | |
---|
[0] | 1339 | @param id <b>integer</b> Post ID |
---|
| 1340 | @param status <b>integer</b> Post status |
---|
| 1341 | */ |
---|
| 1342 | public function updPostStatus($id,$status) |
---|
| 1343 | { |
---|
[1030] | 1344 | $this->updPostsStatus($id,$status); |
---|
| 1345 | } |
---|
[2566] | 1346 | |
---|
[1030] | 1347 | /** |
---|
| 1348 | Updates posts status. |
---|
[2566] | 1349 | |
---|
[1030] | 1350 | @param ids <b>mixed</b> Post(s) ID(s) |
---|
| 1351 | @param status <b>integer</b> Post status |
---|
| 1352 | */ |
---|
| 1353 | public function updPostsStatus($ids,$status) |
---|
| 1354 | { |
---|
[0] | 1355 | if (!$this->core->auth->check('publish,contentadmin',$this->id)) { |
---|
| 1356 | throw new Exception(__('You are not allowed to change this entry status')); |
---|
| 1357 | } |
---|
[2566] | 1358 | |
---|
[1030] | 1359 | $posts_ids = dcUtils::cleanIds($ids); |
---|
[0] | 1360 | $status = (integer) $status; |
---|
[2566] | 1361 | |
---|
[1030] | 1362 | $strReq = "WHERE blog_id = '".$this->con->escape($this->id)."' ". |
---|
| 1363 | "AND post_id ".$this->con->in($posts_ids); |
---|
[2566] | 1364 | |
---|
[0] | 1365 | #If user can only publish, we need to check the post's owner |
---|
| 1366 | if (!$this->core->auth->check('contentadmin',$this->id)) |
---|
| 1367 | { |
---|
[1030] | 1368 | $strReq .= "AND user_id = '".$this->con->escape($this->core->auth->userID())."' "; |
---|
[0] | 1369 | } |
---|
[2566] | 1370 | |
---|
[0] | 1371 | $cur = $this->con->openCursor($this->prefix.'post'); |
---|
[2566] | 1372 | |
---|
[0] | 1373 | $cur->post_status = $status; |
---|
| 1374 | $cur->post_upddt = date('Y-m-d H:i:s'); |
---|
[2566] | 1375 | |
---|
[1030] | 1376 | $cur->update($strReq); |
---|
[0] | 1377 | $this->triggerBlog(); |
---|
| 1378 | } |
---|
[2566] | 1379 | |
---|
[1030] | 1380 | /** |
---|
| 1381 | Updates post selection. |
---|
[2566] | 1382 | |
---|
[1030] | 1383 | @param id <b>integer</b> Post ID |
---|
| 1384 | @param selected <b>integer</b> Is selected post |
---|
| 1385 | */ |
---|
[0] | 1386 | public function updPostSelected($id,$selected) |
---|
| 1387 | { |
---|
[1030] | 1388 | $this->updPostsSelected($id,$selected); |
---|
| 1389 | } |
---|
[2566] | 1390 | |
---|
[1030] | 1391 | /** |
---|
| 1392 | Updates posts selection. |
---|
[2566] | 1393 | |
---|
[1030] | 1394 | @param ids <b>mixed</b> Post(s) ID(s) |
---|
| 1395 | @param selected <b>integer</b> Is selected post(s) |
---|
| 1396 | */ |
---|
| 1397 | public function updPostsSelected($ids,$selected) |
---|
| 1398 | { |
---|
[0] | 1399 | if (!$this->core->auth->check('usage,contentadmin',$this->id)) { |
---|
| 1400 | throw new Exception(__('You are not allowed to change this entry category')); |
---|
| 1401 | } |
---|
[2566] | 1402 | |
---|
[1030] | 1403 | $posts_ids = dcUtils::cleanIds($ids); |
---|
[0] | 1404 | $selected = (boolean) $selected; |
---|
[2566] | 1405 | |
---|
[1030] | 1406 | $strReq = "WHERE blog_id = '".$this->con->escape($this->id)."' ". |
---|
| 1407 | "AND post_id ".$this->con->in($posts_ids); |
---|
[2566] | 1408 | |
---|
[0] | 1409 | # If user is only usage, we need to check the post's owner |
---|
| 1410 | if (!$this->core->auth->check('contentadmin',$this->id)) |
---|
| 1411 | { |
---|
[1030] | 1412 | $strReq .= "AND user_id = '".$this->con->escape($this->core->auth->userID())."' "; |
---|
[0] | 1413 | } |
---|
[2566] | 1414 | |
---|
[0] | 1415 | $cur = $this->con->openCursor($this->prefix.'post'); |
---|
[2566] | 1416 | |
---|
[0] | 1417 | $cur->post_selected = (integer) $selected; |
---|
| 1418 | $cur->post_upddt = date('Y-m-d H:i:s'); |
---|
[2566] | 1419 | |
---|
[1030] | 1420 | $cur->update($strReq); |
---|
[0] | 1421 | $this->triggerBlog(); |
---|
| 1422 | } |
---|
[2566] | 1423 | |
---|
[0] | 1424 | /** |
---|
| 1425 | Updates post category. <var>$cat_id</var> can be null. |
---|
[2566] | 1426 | |
---|
[0] | 1427 | @param id <b>integer</b> Post ID |
---|
| 1428 | @param cat_id <b>integer</b> Category ID |
---|
| 1429 | */ |
---|
| 1430 | public function updPostCategory($id,$cat_id) |
---|
| 1431 | { |
---|
[1030] | 1432 | $this->updPostsCategory($id,$cat_id); |
---|
| 1433 | } |
---|
[2566] | 1434 | |
---|
[1030] | 1435 | /** |
---|
| 1436 | Updates posts category. <var>$cat_id</var> can be null. |
---|
[2566] | 1437 | |
---|
[1030] | 1438 | @param ids <b>mixed</b> Post(s) ID(s) |
---|
| 1439 | @param cat_id <b>integer</b> Category ID |
---|
| 1440 | */ |
---|
| 1441 | public function updPostsCategory($ids,$cat_id) |
---|
| 1442 | { |
---|
[0] | 1443 | if (!$this->core->auth->check('usage,contentadmin',$this->id)) { |
---|
| 1444 | throw new Exception(__('You are not allowed to change this entry category')); |
---|
| 1445 | } |
---|
[2566] | 1446 | |
---|
[1030] | 1447 | $posts_ids = dcUtils::cleanIds($ids); |
---|
[0] | 1448 | $cat_id = (integer) $cat_id; |
---|
[2566] | 1449 | |
---|
[1030] | 1450 | $strReq = "WHERE blog_id = '".$this->con->escape($this->id)."' ". |
---|
| 1451 | "AND post_id ".$this->con->in($posts_ids); |
---|
[2566] | 1452 | |
---|
[0] | 1453 | # If user is only usage, we need to check the post's owner |
---|
| 1454 | if (!$this->core->auth->check('contentadmin',$this->id)) |
---|
| 1455 | { |
---|
[1030] | 1456 | $strReq .= "AND user_id = '".$this->con->escape($this->core->auth->userID())."' "; |
---|
[0] | 1457 | } |
---|
[2566] | 1458 | |
---|
[0] | 1459 | $cur = $this->con->openCursor($this->prefix.'post'); |
---|
[2566] | 1460 | |
---|
[0] | 1461 | $cur->cat_id = ($cat_id ? $cat_id : null); |
---|
| 1462 | $cur->post_upddt = date('Y-m-d H:i:s'); |
---|
[2566] | 1463 | |
---|
[1030] | 1464 | $cur->update($strReq); |
---|
[0] | 1465 | $this->triggerBlog(); |
---|
| 1466 | } |
---|
[2566] | 1467 | |
---|
[0] | 1468 | /** |
---|
[1015] | 1469 | Updates posts category. <var>$new_cat_id</var> can be null. |
---|
[2566] | 1470 | |
---|
[1015] | 1471 | @param old_cat_id <b>integer</b> Old category ID |
---|
| 1472 | @param new_cat_id <b>integer</b> New category ID |
---|
| 1473 | */ |
---|
[1030] | 1474 | public function changePostsCategory($old_cat_id,$new_cat_id) |
---|
[1015] | 1475 | { |
---|
| 1476 | if (!$this->core->auth->check('contentadmin,categories',$this->id)) { |
---|
| 1477 | throw new Exception(__('You are not allowed to change entries category')); |
---|
| 1478 | } |
---|
[2566] | 1479 | |
---|
[1015] | 1480 | $old_cat_id = (integer) $old_cat_id; |
---|
| 1481 | $new_cat_id = (integer) $new_cat_id; |
---|
[2566] | 1482 | |
---|
[1015] | 1483 | $cur = $this->con->openCursor($this->prefix.'post'); |
---|
[2566] | 1484 | |
---|
[1015] | 1485 | $cur->cat_id = ($new_cat_id ? $new_cat_id : null); |
---|
| 1486 | $cur->post_upddt = date('Y-m-d H:i:s'); |
---|
[2566] | 1487 | |
---|
[1015] | 1488 | $cur->update( |
---|
| 1489 | 'WHERE cat_id = '.$old_cat_id.' '. |
---|
| 1490 | "AND blog_id = '".$this->con->escape($this->id)."' " |
---|
| 1491 | ); |
---|
| 1492 | $this->triggerBlog(); |
---|
| 1493 | } |
---|
[2566] | 1494 | |
---|
[1015] | 1495 | /** |
---|
[0] | 1496 | Deletes a post. |
---|
[2566] | 1497 | |
---|
[0] | 1498 | @param id <b>integer</b> Post ID |
---|
| 1499 | */ |
---|
| 1500 | public function delPost($id) |
---|
| 1501 | { |
---|
[1030] | 1502 | $this->delPosts($id); |
---|
| 1503 | } |
---|
[2566] | 1504 | |
---|
[1030] | 1505 | /** |
---|
| 1506 | Deletes multiple posts. |
---|
[2566] | 1507 | |
---|
[1030] | 1508 | @param ids <b>mixed</b> Post(s) ID(s) |
---|
| 1509 | */ |
---|
| 1510 | public function delPosts($ids) |
---|
| 1511 | { |
---|
[0] | 1512 | if (!$this->core->auth->check('delete,contentadmin',$this->id)) { |
---|
| 1513 | throw new Exception(__('You are not allowed to delete entries')); |
---|
| 1514 | } |
---|
[2566] | 1515 | |
---|
[1030] | 1516 | $posts_ids = dcUtils::cleanIds($ids); |
---|
[2566] | 1517 | |
---|
[1030] | 1518 | if (empty($posts_ids)) { |
---|
[0] | 1519 | throw new Exception(__('No such entry ID')); |
---|
| 1520 | } |
---|
[2566] | 1521 | |
---|
[1030] | 1522 | $strReq = 'DELETE FROM '.$this->prefix.'post '. |
---|
| 1523 | "WHERE blog_id = '".$this->con->escape($this->id)."' ". |
---|
| 1524 | "AND post_id ".$this->con->in($posts_ids); |
---|
[2566] | 1525 | |
---|
[0] | 1526 | #If user can only delete, we need to check the post's owner |
---|
| 1527 | if (!$this->core->auth->check('contentadmin',$this->id)) |
---|
| 1528 | { |
---|
[1030] | 1529 | $strReq .= "AND user_id = '".$this->con->escape($this->core->auth->userID())."' "; |
---|
[0] | 1530 | } |
---|
[2566] | 1531 | |
---|
[0] | 1532 | $this->con->execute($strReq); |
---|
| 1533 | $this->triggerBlog(); |
---|
| 1534 | } |
---|
[2566] | 1535 | |
---|
[0] | 1536 | /** |
---|
| 1537 | Publishes all entries flaged as "scheduled". |
---|
| 1538 | */ |
---|
| 1539 | public function publishScheduledEntries() |
---|
| 1540 | { |
---|
| 1541 | $strReq = 'SELECT post_id, post_dt, post_tz '. |
---|
| 1542 | 'FROM '.$this->prefix.'post '. |
---|
| 1543 | 'WHERE post_status = -1 '. |
---|
| 1544 | "AND blog_id = '".$this->con->escape($this->id)."' "; |
---|
[2566] | 1545 | |
---|
[0] | 1546 | $rs = $this->con->select($strReq); |
---|
[2566] | 1547 | |
---|
[0] | 1548 | $now = dt::toUTC(time()); |
---|
[833] | 1549 | $to_change = new ArrayObject(); |
---|
| 1550 | |
---|
[0] | 1551 | if ($rs->isEmpty()) { |
---|
| 1552 | return; |
---|
| 1553 | } |
---|
[2566] | 1554 | |
---|
[0] | 1555 | while ($rs->fetch()) |
---|
| 1556 | { |
---|
| 1557 | # Now timestamp with post timezone |
---|
| 1558 | $now_tz = $now + dt::getTimeOffset($rs->post_tz,$now); |
---|
[2566] | 1559 | |
---|
[0] | 1560 | # Post timestamp |
---|
| 1561 | $post_ts = strtotime($rs->post_dt); |
---|
[2566] | 1562 | |
---|
[0] | 1563 | # If now_tz >= post_ts, we publish the entry |
---|
| 1564 | if ($now_tz >= $post_ts) { |
---|
| 1565 | $to_change[] = (integer) $rs->post_id; |
---|
| 1566 | } |
---|
| 1567 | } |
---|
[833] | 1568 | if (count($to_change)) |
---|
[0] | 1569 | { |
---|
[811] | 1570 | # --BEHAVIOR-- coreBeforeScheduledEntriesPublish |
---|
| 1571 | $this->core->callBehavior('coreBeforeScheduledEntriesPublish',$this,$to_change); |
---|
| 1572 | |
---|
[0] | 1573 | $strReq = |
---|
| 1574 | 'UPDATE '.$this->prefix.'post SET '. |
---|
| 1575 | 'post_status = 1 '. |
---|
| 1576 | "WHERE blog_id = '".$this->con->escape($this->id)."' ". |
---|
[833] | 1577 | 'AND post_id '.$this->con->in((array)$to_change).' '; |
---|
[0] | 1578 | $this->con->execute($strReq); |
---|
| 1579 | $this->triggerBlog(); |
---|
[811] | 1580 | |
---|
| 1581 | # --BEHAVIOR-- coreAfterScheduledEntriesPublish |
---|
| 1582 | $this->core->callBehavior('coreAfterScheduledEntriesPublish',$this,$to_change); |
---|
[0] | 1583 | } |
---|
[2566] | 1584 | |
---|
[0] | 1585 | } |
---|
[2566] | 1586 | |
---|
[0] | 1587 | /** |
---|
| 1588 | Retrieves all users having posts on current blog. |
---|
[2566] | 1589 | |
---|
[0] | 1590 | @param post_type <b>string</b> post_type filter (post) |
---|
| 1591 | @return record |
---|
| 1592 | */ |
---|
| 1593 | public function getPostsUsers($post_type='post') |
---|
| 1594 | { |
---|
| 1595 | $strReq = 'SELECT P.user_id, user_name, user_firstname, '. |
---|
| 1596 | 'user_displayname, user_email '. |
---|
| 1597 | 'FROM '.$this->prefix.'post P, '.$this->prefix.'user U '. |
---|
| 1598 | 'WHERE P.user_id = U.user_id '. |
---|
| 1599 | "AND blog_id = '".$this->con->escape($this->id)."' "; |
---|
[2566] | 1600 | |
---|
[0] | 1601 | if ($post_type) { |
---|
| 1602 | $strReq .= "AND post_type = '".$this->con->escape($post_type)."' "; |
---|
| 1603 | } |
---|
[2566] | 1604 | |
---|
[0] | 1605 | $strReq .= 'GROUP BY P.user_id, user_name, user_firstname, user_displayname, user_email '; |
---|
[2566] | 1606 | |
---|
[0] | 1607 | return $this->con->select($strReq); |
---|
| 1608 | } |
---|
[2566] | 1609 | |
---|
[0] | 1610 | private function getPostsCategoryFilter($arr,$field='cat_id') |
---|
| 1611 | { |
---|
| 1612 | $field = $field == 'cat_id' ? 'cat_id' : 'cat_url'; |
---|
[2566] | 1613 | |
---|
[0] | 1614 | $sub = array(); |
---|
| 1615 | $not = array(); |
---|
| 1616 | $queries = array(); |
---|
[2566] | 1617 | |
---|
[0] | 1618 | foreach ($arr as $v) |
---|
| 1619 | { |
---|
| 1620 | $v = trim($v); |
---|
| 1621 | $args = preg_split('/\s*[?]\s*/',$v,-1,PREG_SPLIT_NO_EMPTY); |
---|
| 1622 | $id = array_shift($args); |
---|
| 1623 | $args = array_flip($args); |
---|
[2566] | 1624 | |
---|
[0] | 1625 | if (isset($args['not'])) { $not[$id] = 1; } |
---|
| 1626 | if (isset($args['sub'])) { $sub[$id] = 1; } |
---|
| 1627 | if ($field == 'cat_id') { |
---|
| 1628 | if (preg_match('/^null$/i',$id)) { |
---|
| 1629 | $queries[$id] = 'P.cat_id IS NULL'; |
---|
| 1630 | } |
---|
| 1631 | else { |
---|
| 1632 | $queries[$id] = 'P.cat_id = '.(integer) $id; |
---|
| 1633 | } |
---|
| 1634 | } else { |
---|
| 1635 | $queries[$id] = "C.cat_url = '".$this->con->escape($id)."' "; |
---|
| 1636 | } |
---|
| 1637 | } |
---|
[2566] | 1638 | |
---|
[0] | 1639 | if (!empty($sub)) { |
---|
| 1640 | $rs = $this->con->select( |
---|
| 1641 | 'SELECT cat_id, cat_url, cat_lft, cat_rgt FROM '.$this->prefix.'category '. |
---|
| 1642 | "WHERE blog_id = '".$this->con->escape($this->id)."' ". |
---|
| 1643 | 'AND '.$field.' '.$this->con->in(array_keys($sub)) |
---|
| 1644 | ); |
---|
[2566] | 1645 | |
---|
[0] | 1646 | while ($rs->fetch()) { |
---|
| 1647 | $queries[$rs->f($field)] = '(C.cat_lft BETWEEN '.$rs->cat_lft.' AND '.$rs->cat_rgt.')'; |
---|
| 1648 | } |
---|
| 1649 | } |
---|
[2566] | 1650 | |
---|
[0] | 1651 | # Create queries |
---|
| 1652 | $sql = array( |
---|
| 1653 | 0 => array(), # wanted categories |
---|
| 1654 | 1 => array() # excluded categories |
---|
| 1655 | ); |
---|
[2566] | 1656 | |
---|
[0] | 1657 | foreach ($queries as $id => $q) { |
---|
| 1658 | $sql[(integer) isset($not[$id])][] = $q; |
---|
| 1659 | } |
---|
[2566] | 1660 | |
---|
[0] | 1661 | $sql[0] = implode(' OR ',$sql[0]); |
---|
| 1662 | $sql[1] = implode(' OR ',$sql[1]); |
---|
[2566] | 1663 | |
---|
[0] | 1664 | if ($sql[0]) { |
---|
| 1665 | $sql[0] = '('.$sql[0].')'; |
---|
| 1666 | } else { |
---|
| 1667 | unset($sql[0]); |
---|
| 1668 | } |
---|
[2566] | 1669 | |
---|
[0] | 1670 | if ($sql[1]) { |
---|
| 1671 | $sql[1] = '(P.cat_id IS NULL OR NOT('.$sql[1].'))'; |
---|
| 1672 | } else { |
---|
| 1673 | unset($sql[1]); |
---|
| 1674 | } |
---|
[2566] | 1675 | |
---|
[0] | 1676 | return implode(' AND ',$sql); |
---|
| 1677 | } |
---|
[2566] | 1678 | |
---|
[0] | 1679 | private function getPostCursor($cur,$post_id=null) |
---|
| 1680 | { |
---|
| 1681 | if ($cur->post_title == '') { |
---|
| 1682 | throw new Exception(__('No entry title')); |
---|
| 1683 | } |
---|
[2566] | 1684 | |
---|
[0] | 1685 | if ($cur->post_content == '') { |
---|
| 1686 | throw new Exception(__('No entry content')); |
---|
| 1687 | } |
---|
[2566] | 1688 | |
---|
[0] | 1689 | if ($cur->post_password === '') { |
---|
| 1690 | $cur->post_password = null; |
---|
| 1691 | } |
---|
[2566] | 1692 | |
---|
[0] | 1693 | if ($cur->post_dt == '') { |
---|
| 1694 | $offset = dt::getTimeOffset($this->core->auth->getInfo('user_tz')); |
---|
| 1695 | $now = time() + $offset; |
---|
| 1696 | $cur->post_dt = date('Y-m-d H:i:00',$now); |
---|
| 1697 | } |
---|
[2566] | 1698 | |
---|
[0] | 1699 | $post_id = is_int($post_id) ? $post_id : $cur->post_id; |
---|
[2566] | 1700 | |
---|
[0] | 1701 | if ($cur->post_content_xhtml == '') { |
---|
| 1702 | throw new Exception(__('No entry content')); |
---|
| 1703 | } |
---|
[2566] | 1704 | |
---|
[0] | 1705 | # Words list |
---|
| 1706 | if ($cur->post_title !== null && $cur->post_excerpt_xhtml !== null |
---|
| 1707 | && $cur->post_content_xhtml !== null) |
---|
| 1708 | { |
---|
| 1709 | $words = |
---|
| 1710 | $cur->post_title.' '. |
---|
| 1711 | $cur->post_excerpt_xhtml.' '. |
---|
| 1712 | $cur->post_content_xhtml; |
---|
[2566] | 1713 | |
---|
[0] | 1714 | $cur->post_words = implode(' ',text::splitWords($words)); |
---|
| 1715 | } |
---|
| 1716 | } |
---|
[2566] | 1717 | |
---|
[0] | 1718 | private function getPostContent($cur,$post_id) |
---|
| 1719 | { |
---|
| 1720 | $post_excerpt = $cur->post_excerpt; |
---|
| 1721 | $post_excerpt_xhtml = $cur->post_excerpt_xhtml; |
---|
| 1722 | $post_content = $cur->post_content; |
---|
| 1723 | $post_content_xhtml = $cur->post_content_xhtml; |
---|
[2566] | 1724 | |
---|
[0] | 1725 | $this->setPostContent( |
---|
| 1726 | $post_id,$cur->post_format,$cur->post_lang, |
---|
| 1727 | $post_excerpt,$post_excerpt_xhtml, |
---|
| 1728 | $post_content,$post_content_xhtml |
---|
| 1729 | ); |
---|
[2566] | 1730 | |
---|
[0] | 1731 | $cur->post_excerpt = $post_excerpt; |
---|
| 1732 | $cur->post_excerpt_xhtml = $post_excerpt_xhtml; |
---|
| 1733 | $cur->post_content = $post_content; |
---|
| 1734 | $cur->post_content_xhtml = $post_content_xhtml; |
---|
| 1735 | } |
---|
[2566] | 1736 | |
---|
[0] | 1737 | /** |
---|
| 1738 | Creates post HTML content, taking format and lang into account. |
---|
[2566] | 1739 | |
---|
[0] | 1740 | @param post_id <b>integer</b> Post ID |
---|
| 1741 | @param format <b>string</b> Post format |
---|
| 1742 | @param lang <b>string</b> Post lang |
---|
| 1743 | @param excerpt <b>string</b> Post excerpt |
---|
| 1744 | @param[out] excerpt_xhtml <b>string</b> Post excerpt HTML |
---|
| 1745 | @param content <b>string</b> Post content |
---|
| 1746 | @param[out] content_xhtml <b>string</b> Post content HTML |
---|
| 1747 | */ |
---|
| 1748 | public function setPostContent($post_id,$format,$lang,&$excerpt,&$excerpt_xhtml,&$content,&$content_xhtml) |
---|
| 1749 | { |
---|
| 1750 | if ($format == 'wiki') |
---|
| 1751 | { |
---|
| 1752 | $this->core->initWikiPost(); |
---|
| 1753 | $this->core->wiki2xhtml->setOpt('note_prefix','pnote-'.$post_id); |
---|
[970] | 1754 | switch ($this->settings->system->note_title_tag) { |
---|
| 1755 | case 1: |
---|
| 1756 | $tag = 'h3'; |
---|
| 1757 | break; |
---|
| 1758 | case 2: |
---|
| 1759 | $tag = 'p'; |
---|
| 1760 | break; |
---|
| 1761 | default: |
---|
| 1762 | $tag = 'h4'; |
---|
| 1763 | break; |
---|
| 1764 | } |
---|
| 1765 | $this->core->wiki2xhtml->setOpt('note_str','<div class="footnotes"><'.$tag.' class="footnotes-title">'. |
---|
| 1766 | __('Notes').'</'.$tag.'>%s</div>'); |
---|
| 1767 | $this->core->wiki2xhtml->setOpt('note_str_single','<div class="footnotes"><'.$tag.' class="footnotes-title">'. |
---|
| 1768 | __('Note').'</'.$tag.'>%s</div>'); |
---|
[0] | 1769 | if (strpos($lang,'fr') === 0) { |
---|
| 1770 | $this->core->wiki2xhtml->setOpt('active_fr_syntax',1); |
---|
| 1771 | } |
---|
| 1772 | } |
---|
[2566] | 1773 | |
---|
[0] | 1774 | if ($excerpt) { |
---|
| 1775 | $excerpt_xhtml = $this->core->callFormater($format,$excerpt); |
---|
| 1776 | $excerpt_xhtml = $this->core->HTMLfilter($excerpt_xhtml); |
---|
| 1777 | } else { |
---|
| 1778 | $excerpt_xhtml = ''; |
---|
| 1779 | } |
---|
[2566] | 1780 | |
---|
[0] | 1781 | if ($content) { |
---|
| 1782 | $content_xhtml = $this->core->callFormater($format,$content); |
---|
| 1783 | $content_xhtml = $this->core->HTMLfilter($content_xhtml); |
---|
| 1784 | } else { |
---|
| 1785 | $content_xhtml = ''; |
---|
| 1786 | } |
---|
[2566] | 1787 | |
---|
[0] | 1788 | # --BEHAVIOR-- coreAfterPostContentFormat |
---|
| 1789 | $this->core->callBehavior('coreAfterPostContentFormat',array( |
---|
| 1790 | 'excerpt' => &$excerpt, |
---|
| 1791 | 'content' => &$content, |
---|
| 1792 | 'excerpt_xhtml' => &$excerpt_xhtml, |
---|
| 1793 | 'content_xhtml' => &$content_xhtml |
---|
| 1794 | )); |
---|
| 1795 | } |
---|
[2566] | 1796 | |
---|
[0] | 1797 | /** |
---|
| 1798 | Returns URL for a post according to blog setting <var>post_url_format</var>. |
---|
| 1799 | It will try to guess URL and append some figures if needed. |
---|
[2566] | 1800 | |
---|
[0] | 1801 | @param url <b>string</b> Origin URL, could be empty |
---|
| 1802 | @param post_dt <b>string</b> Post date (in YYYY-MM-DD HH:mm:ss) |
---|
| 1803 | @param post_title <b>string</b> Post title |
---|
| 1804 | @param post_id <b>integer</b> Post ID |
---|
| 1805 | @return <b>string</b> result URL |
---|
| 1806 | */ |
---|
| 1807 | public function getPostURL($url,$post_dt,$post_title,$post_id) |
---|
| 1808 | { |
---|
| 1809 | $url = trim($url); |
---|
[2566] | 1810 | |
---|
[0] | 1811 | $url_patterns = array( |
---|
| 1812 | '{y}' => date('Y',strtotime($post_dt)), |
---|
| 1813 | '{m}' => date('m',strtotime($post_dt)), |
---|
| 1814 | '{d}' => date('d',strtotime($post_dt)), |
---|
| 1815 | '{t}' => text::tidyURL($post_title), |
---|
| 1816 | '{id}' => (integer) $post_id |
---|
| 1817 | ); |
---|
[2566] | 1818 | |
---|
[0] | 1819 | # If URL is empty, we create a new one |
---|
| 1820 | if ($url == '') |
---|
| 1821 | { |
---|
| 1822 | # Transform with format |
---|
| 1823 | $url = str_replace( |
---|
| 1824 | array_keys($url_patterns), |
---|
| 1825 | array_values($url_patterns), |
---|
| 1826 | $this->settings->system->post_url_format |
---|
| 1827 | ); |
---|
| 1828 | } |
---|
| 1829 | else |
---|
| 1830 | { |
---|
| 1831 | $url = text::tidyURL($url); |
---|
| 1832 | } |
---|
[2566] | 1833 | |
---|
[0] | 1834 | # Let's check if URL is taken... |
---|
| 1835 | $strReq = 'SELECT post_url FROM '.$this->prefix.'post '. |
---|
| 1836 | "WHERE post_url = '".$this->con->escape($url)."' ". |
---|
| 1837 | 'AND post_id <> '.(integer) $post_id. ' '. |
---|
| 1838 | "AND blog_id = '".$this->con->escape($this->id)."' ". |
---|
| 1839 | 'ORDER BY post_url DESC'; |
---|
[2566] | 1840 | |
---|
[0] | 1841 | $rs = $this->con->select($strReq); |
---|
[2566] | 1842 | |
---|
[0] | 1843 | if (!$rs->isEmpty()) |
---|
| 1844 | { |
---|
[1743] | 1845 | if ($this->con->driver() == 'mysql' || $this->con->driver() == 'mysqli') { |
---|
[0] | 1846 | $clause = "REGEXP '^".$this->con->escape($url)."[0-9]+$'"; |
---|
| 1847 | } elseif ($this->con->driver() == 'pgsql') { |
---|
| 1848 | $clause = "~ '^".$this->con->escape($url)."[0-9]+$'"; |
---|
| 1849 | } else { |
---|
| 1850 | $clause = "LIKE '".$this->con->escape($url)."%'"; |
---|
| 1851 | } |
---|
| 1852 | $strReq = 'SELECT post_url FROM '.$this->prefix.'post '. |
---|
| 1853 | "WHERE post_url ".$clause.' '. |
---|
| 1854 | 'AND post_id <> '.(integer) $post_id.' '. |
---|
| 1855 | "AND blog_id = '".$this->con->escape($this->id)."' ". |
---|
| 1856 | 'ORDER BY post_url DESC '; |
---|
[2566] | 1857 | |
---|
[0] | 1858 | $rs = $this->con->select($strReq); |
---|
| 1859 | $a = array(); |
---|
| 1860 | while ($rs->fetch()) { |
---|
| 1861 | $a[] = $rs->post_url; |
---|
| 1862 | } |
---|
[2566] | 1863 | |
---|
[0] | 1864 | natsort($a); |
---|
| 1865 | $t_url = end($a); |
---|
[2566] | 1866 | |
---|
[0] | 1867 | if (preg_match('/(.*?)([0-9]+)$/',$t_url,$m)) { |
---|
| 1868 | $i = (integer) $m[2]; |
---|
| 1869 | $url = $m[1]; |
---|
| 1870 | } else { |
---|
| 1871 | $i = 1; |
---|
| 1872 | } |
---|
[2566] | 1873 | |
---|
[0] | 1874 | return $url.($i+1); |
---|
| 1875 | } |
---|
[2566] | 1876 | |
---|
[0] | 1877 | # URL is empty? |
---|
| 1878 | if ($url == '') { |
---|
| 1879 | throw new Exception(__('Empty entry URL')); |
---|
| 1880 | } |
---|
[2566] | 1881 | |
---|
[0] | 1882 | return $url; |
---|
| 1883 | } |
---|
| 1884 | //@} |
---|
[2566] | 1885 | |
---|
[0] | 1886 | /// @name Comments management methods |
---|
| 1887 | //@{ |
---|
| 1888 | /** |
---|
| 1889 | Retrieves comments. <b>$params</b> is an array taking the following |
---|
| 1890 | optionnal parameters: |
---|
[2566] | 1891 | |
---|
[0] | 1892 | - no_content: Don't retrieve comment content |
---|
[2566] | 1893 | - post_type: Get only entries with given type (default no type, array for many types) |
---|
[0] | 1894 | - post_id: (integer) Get comments belonging to given post_id |
---|
| 1895 | - cat_id: (integer or array) Get comments belonging to entries of given category ID |
---|
[2521] | 1896 | - comment_id: (integer or array) Get comment with given ID (or IDs) |
---|
[1689] | 1897 | - comment_site: (string) Get comments with given comment_site |
---|
[0] | 1898 | - comment_status: (integer) Get comments with given comment_status |
---|
| 1899 | - comment_trackback: (integer) Get only comments (0) or trackbacks (1) |
---|
| 1900 | - comment_ip: (string) Get comments with given IP address |
---|
| 1901 | - post_url: Get entry with given post_url field |
---|
| 1902 | - user_id: (integer) Get entries belonging to given user ID |
---|
| 1903 | - q_author: Search comments by author |
---|
| 1904 | - sql: Append SQL string at the end of the query |
---|
| 1905 | - from: Append SQL string after "FROM" statement in query |
---|
| 1906 | - order: Order of results (default "ORDER BY comment_dt DES") |
---|
| 1907 | - limit: Limit parameter |
---|
[774] | 1908 | - sql_only : return the sql request instead of results. Only ids are selected |
---|
[2566] | 1909 | |
---|
[0] | 1910 | @param params <b>array</b> Parameters |
---|
| 1911 | @param count_only <b>boolean</b> Only counts results |
---|
| 1912 | @return <b>record</b> A record with some more capabilities |
---|
| 1913 | */ |
---|
| 1914 | public function getComments($params=array(),$count_only=false) |
---|
| 1915 | { |
---|
| 1916 | if ($count_only) |
---|
| 1917 | { |
---|
| 1918 | $strReq = 'SELECT count(comment_id) '; |
---|
| 1919 | } |
---|
[2566] | 1920 | elseif (!empty($params['sql_only'])) |
---|
[774] | 1921 | { |
---|
| 1922 | $strReq = 'SELECT P.post_id '; |
---|
| 1923 | } |
---|
[0] | 1924 | else |
---|
| 1925 | { |
---|
| 1926 | if (!empty($params['no_content'])) { |
---|
| 1927 | $content_req = ''; |
---|
| 1928 | } else { |
---|
| 1929 | $content_req = 'comment_content, '; |
---|
| 1930 | } |
---|
[2566] | 1931 | |
---|
[0] | 1932 | if (!empty($params['columns']) && is_array($params['columns'])) { |
---|
| 1933 | $content_req .= implode(', ',$params['columns']).', '; |
---|
| 1934 | } |
---|
[2566] | 1935 | |
---|
[0] | 1936 | $strReq = |
---|
| 1937 | 'SELECT C.comment_id, comment_dt, comment_tz, comment_upddt, '. |
---|
| 1938 | 'comment_author, comment_email, comment_site, '. |
---|
| 1939 | $content_req.' comment_trackback, comment_status, '. |
---|
| 1940 | 'comment_spam_status, comment_spam_filter, comment_ip, '. |
---|
| 1941 | 'P.post_title, P.post_url, P.post_id, P.post_password, P.post_type, '. |
---|
| 1942 | 'P.post_dt, P.user_id, U.user_email, U.user_url '; |
---|
| 1943 | } |
---|
[2566] | 1944 | |
---|
[0] | 1945 | $strReq .= |
---|
| 1946 | 'FROM '.$this->prefix.'comment C '. |
---|
| 1947 | 'INNER JOIN '.$this->prefix.'post P ON C.post_id = P.post_id '. |
---|
| 1948 | 'INNER JOIN '.$this->prefix.'user U ON P.user_id = U.user_id '; |
---|
[2566] | 1949 | |
---|
[0] | 1950 | if (!empty($params['from'])) { |
---|
| 1951 | $strReq .= $params['from'].' '; |
---|
| 1952 | } |
---|
[2566] | 1953 | |
---|
[0] | 1954 | $strReq .= |
---|
| 1955 | "WHERE P.blog_id = '".$this->con->escape($this->id)."' "; |
---|
[2566] | 1956 | |
---|
[0] | 1957 | if (!$this->core->auth->check('contentadmin',$this->id)) { |
---|
| 1958 | $strReq .= 'AND ((comment_status = 1 AND P.post_status = 1 '; |
---|
[2566] | 1959 | |
---|
[0] | 1960 | if ($this->without_password) { |
---|
| 1961 | $strReq .= 'AND post_password IS NULL '; |
---|
| 1962 | } |
---|
| 1963 | $strReq .= ') '; |
---|
[2566] | 1964 | |
---|
[0] | 1965 | if ($this->core->auth->userID()) { |
---|
| 1966 | $strReq .= "OR P.user_id = '".$this->con->escape($this->core->auth->userID())."')"; |
---|
| 1967 | } else { |
---|
| 1968 | $strReq .= ') '; |
---|
| 1969 | } |
---|
| 1970 | } |
---|
[2566] | 1971 | |
---|
[0] | 1972 | if (!empty($params['post_type'])) |
---|
| 1973 | { |
---|
[340] | 1974 | $strReq .= 'AND post_type '.$this->con->in($params['post_type']); |
---|
[0] | 1975 | } |
---|
[2566] | 1976 | |
---|
[836] | 1977 | if (isset($params['post_id']) && $params['post_id'] !== '') { |
---|
[0] | 1978 | $strReq .= 'AND P.post_id = '.(integer) $params['post_id'].' '; |
---|
| 1979 | } |
---|
[2566] | 1980 | |
---|
[836] | 1981 | if (isset($params['cat_id']) && $params['cat_id'] !== '') { |
---|
[0] | 1982 | $strReq .= 'AND P.cat_id = '.(integer) $params['cat_id'].' '; |
---|
| 1983 | } |
---|
[2566] | 1984 | |
---|
[836] | 1985 | if (isset($params['comment_id']) && $params['comment_id'] !== '') { |
---|
[2521] | 1986 | if (is_array($params['comment_id'])) { |
---|
| 1987 | array_walk($params['comment_id'],create_function('&$v,$k','if($v!==null){$v=(integer)$v;}')); |
---|
| 1988 | } else { |
---|
| 1989 | $params['comment_id'] = array((integer) $params['comment_id']); |
---|
| 1990 | } |
---|
| 1991 | $strReq .= 'AND comment_id '.$this->con->in($params['comment_id']); |
---|
[0] | 1992 | } |
---|
[2566] | 1993 | |
---|
[1689] | 1994 | if (isset($params['comment_site'])) { |
---|
| 1995 | $comment_site = $this->con->escape(str_replace('*','%',$params['comment_site'])); |
---|
| 1996 | $strReq .= "AND comment_site LIKE '".$comment_site."' "; |
---|
| 1997 | } |
---|
[2566] | 1998 | |
---|
[0] | 1999 | if (isset($params['comment_status'])) { |
---|
| 2000 | $strReq .= 'AND comment_status = '.(integer) $params['comment_status'].' '; |
---|
| 2001 | } |
---|
[2566] | 2002 | |
---|
[0] | 2003 | if (!empty($params['comment_status_not'])) |
---|
| 2004 | { |
---|
| 2005 | $strReq .= 'AND comment_status <> '.(integer) $params['comment_status_not'].' '; |
---|
| 2006 | } |
---|
[2566] | 2007 | |
---|
[0] | 2008 | if (isset($params['comment_trackback'])) { |
---|
| 2009 | $strReq .= 'AND comment_trackback = '.(integer) (boolean) $params['comment_trackback'].' '; |
---|
| 2010 | } |
---|
[2566] | 2011 | |
---|
[0] | 2012 | if (isset($params['comment_ip'])) { |
---|
[1018] | 2013 | $comment_ip = $this->con->escape(str_replace('*','%',$params['comment_ip'])); |
---|
| 2014 | $strReq .= "AND comment_ip LIKE '".$comment_ip."' "; |
---|
[0] | 2015 | } |
---|
[2566] | 2016 | |
---|
[0] | 2017 | if (isset($params['q_author'])) { |
---|
| 2018 | $q_author = $this->con->escape(str_replace('*','%',strtolower($params['q_author']))); |
---|
| 2019 | $strReq .= "AND LOWER(comment_author) LIKE '".$q_author."' "; |
---|
| 2020 | } |
---|
[2566] | 2021 | |
---|
[0] | 2022 | if (!empty($params['search'])) |
---|
| 2023 | { |
---|
| 2024 | $words = text::splitWords($params['search']); |
---|
[2566] | 2025 | |
---|
[0] | 2026 | if (!empty($words)) |
---|
| 2027 | { |
---|
| 2028 | # --BEHAVIOR coreCommentSearch |
---|
| 2029 | if ($this->core->hasBehavior('coreCommentSearch')) { |
---|
| 2030 | $this->core->callBehavior('coreCommentSearch',$this->core,array(&$words,&$strReq,&$params)); |
---|
| 2031 | } |
---|
[2566] | 2032 | |
---|
[0] | 2033 | if ($words) |
---|
| 2034 | { |
---|
| 2035 | foreach ($words as $i => $w) { |
---|
| 2036 | $words[$i] = "comment_words LIKE '%".$this->con->escape($w)."%'"; |
---|
| 2037 | } |
---|
| 2038 | $strReq .= 'AND '.implode(' AND ',$words).' '; |
---|
| 2039 | } |
---|
| 2040 | } |
---|
| 2041 | } |
---|
[2566] | 2042 | |
---|
[0] | 2043 | if (!empty($params['sql'])) { |
---|
| 2044 | $strReq .= $params['sql'].' '; |
---|
| 2045 | } |
---|
[2566] | 2046 | |
---|
[0] | 2047 | if (!$count_only) |
---|
| 2048 | { |
---|
| 2049 | if (!empty($params['order'])) { |
---|
| 2050 | $strReq .= 'ORDER BY '.$this->con->escape($params['order']).' '; |
---|
| 2051 | } else { |
---|
| 2052 | $strReq .= 'ORDER BY comment_dt DESC '; |
---|
| 2053 | } |
---|
| 2054 | } |
---|
[2566] | 2055 | |
---|
[0] | 2056 | if (!$count_only && !empty($params['limit'])) { |
---|
| 2057 | $strReq .= $this->con->limit($params['limit']); |
---|
| 2058 | } |
---|
[774] | 2059 | |
---|
| 2060 | if (!empty($params['sql_only'])) { |
---|
| 2061 | return $strReq; |
---|
| 2062 | } |
---|
[2566] | 2063 | |
---|
[0] | 2064 | $rs = $this->con->select($strReq); |
---|
| 2065 | $rs->core = $this->core; |
---|
| 2066 | $rs->extend('rsExtComment'); |
---|
[2566] | 2067 | |
---|
[0] | 2068 | # --BEHAVIOR-- coreBlogGetComments |
---|
| 2069 | $this->core->callBehavior('coreBlogGetComments',$rs); |
---|
[2566] | 2070 | |
---|
[0] | 2071 | return $rs; |
---|
| 2072 | } |
---|
[2566] | 2073 | |
---|
[0] | 2074 | /** |
---|
| 2075 | Creates a new comment. Takes a cursor as input and returns the new comment |
---|
| 2076 | ID. |
---|
[2566] | 2077 | |
---|
[0] | 2078 | @param cur <b>cursor</b> Comment cursor |
---|
| 2079 | @return <b>integer</b> New comment ID |
---|
| 2080 | */ |
---|
| 2081 | public function addComment($cur) |
---|
| 2082 | { |
---|
| 2083 | $this->con->writeLock($this->prefix.'comment'); |
---|
| 2084 | try |
---|
| 2085 | { |
---|
| 2086 | # Get ID |
---|
| 2087 | $rs = $this->con->select( |
---|
| 2088 | 'SELECT MAX(comment_id) '. |
---|
[2566] | 2089 | 'FROM '.$this->prefix.'comment ' |
---|
[0] | 2090 | ); |
---|
[2566] | 2091 | |
---|
[0] | 2092 | $cur->comment_id = (integer) $rs->f(0) + 1; |
---|
| 2093 | $cur->comment_upddt = date('Y-m-d H:i:s'); |
---|
[2566] | 2094 | |
---|
[0] | 2095 | $offset = dt::getTimeOffset($this->settings->system->blog_timezone); |
---|
| 2096 | $cur->comment_dt = date('Y-m-d H:i:s',time() + $offset); |
---|
| 2097 | $cur->comment_tz = $this->settings->system->blog_timezone; |
---|
[2566] | 2098 | |
---|
[0] | 2099 | $this->getCommentCursor($cur); |
---|
[2566] | 2100 | |
---|
[0] | 2101 | if ($cur->comment_ip === null) { |
---|
| 2102 | $cur->comment_ip = http::realIP(); |
---|
| 2103 | } |
---|
[2566] | 2104 | |
---|
[0] | 2105 | # --BEHAVIOR-- coreBeforeCommentCreate |
---|
| 2106 | $this->core->callBehavior('coreBeforeCommentCreate',$this,$cur); |
---|
[2566] | 2107 | |
---|
[0] | 2108 | $cur->insert(); |
---|
| 2109 | $this->con->unlock(); |
---|
| 2110 | } |
---|
| 2111 | catch (Exception $e) |
---|
| 2112 | { |
---|
| 2113 | $this->con->unlock(); |
---|
| 2114 | throw $e; |
---|
| 2115 | } |
---|
[2566] | 2116 | |
---|
[0] | 2117 | # --BEHAVIOR-- coreAfterCommentCreate |
---|
| 2118 | $this->core->callBehavior('coreAfterCommentCreate',$this,$cur); |
---|
[2566] | 2119 | |
---|
[0] | 2120 | $this->triggerComment($cur->comment_id); |
---|
| 2121 | if ($cur->comment_status != -2) { |
---|
| 2122 | $this->triggerBlog(); |
---|
[2566] | 2123 | } |
---|
[0] | 2124 | return $cur->comment_id; |
---|
| 2125 | } |
---|
[2566] | 2126 | |
---|
[0] | 2127 | /** |
---|
| 2128 | Updates an existing comment. |
---|
[2566] | 2129 | |
---|
[0] | 2130 | @param id <b>integer</b> Comment ID |
---|
| 2131 | @param cur <b>cursor</b> Comment cursor |
---|
| 2132 | */ |
---|
| 2133 | public function updComment($id,$cur) |
---|
| 2134 | { |
---|
| 2135 | if (!$this->core->auth->check('usage,contentadmin',$this->id)) { |
---|
| 2136 | throw new Exception(__('You are not allowed to update comments')); |
---|
| 2137 | } |
---|
[2566] | 2138 | |
---|
[0] | 2139 | $id = (integer) $id; |
---|
[2566] | 2140 | |
---|
[0] | 2141 | if (empty($id)) { |
---|
| 2142 | throw new Exception(__('No such comment ID')); |
---|
| 2143 | } |
---|
[2566] | 2144 | |
---|
[0] | 2145 | $rs = $this->getComments(array('comment_id' => $id)); |
---|
[2566] | 2146 | |
---|
[0] | 2147 | if ($rs->isEmpty()) { |
---|
| 2148 | throw new Exception(__('No such comment ID')); |
---|
| 2149 | } |
---|
[2566] | 2150 | |
---|
[0] | 2151 | #If user is only usage, we need to check the post's owner |
---|
| 2152 | if (!$this->core->auth->check('contentadmin',$this->id)) |
---|
| 2153 | { |
---|
| 2154 | if ($rs->user_id != $this->core->auth->userID()) { |
---|
| 2155 | throw new Exception(__('You are not allowed to update this comment')); |
---|
| 2156 | } |
---|
| 2157 | } |
---|
[2566] | 2158 | |
---|
[0] | 2159 | $this->getCommentCursor($cur); |
---|
[2566] | 2160 | |
---|
[0] | 2161 | $cur->comment_upddt = date('Y-m-d H:i:s'); |
---|
[2566] | 2162 | |
---|
[0] | 2163 | if (!$this->core->auth->check('publish,contentadmin',$this->id)) { |
---|
| 2164 | $cur->unsetField('comment_status'); |
---|
| 2165 | } |
---|
[2566] | 2166 | |
---|
[0] | 2167 | # --BEHAVIOR-- coreBeforeCommentUpdate |
---|
| 2168 | $this->core->callBehavior('coreBeforeCommentUpdate',$this,$cur,$rs); |
---|
[2566] | 2169 | |
---|
[0] | 2170 | $cur->update('WHERE comment_id = '.$id.' '); |
---|
[2566] | 2171 | |
---|
[0] | 2172 | # --BEHAVIOR-- coreAfterCommentUpdate |
---|
| 2173 | $this->core->callBehavior('coreAfterCommentUpdate',$this,$cur,$rs); |
---|
[2566] | 2174 | |
---|
[0] | 2175 | $this->triggerComment($id); |
---|
| 2176 | $this->triggerBlog(); |
---|
| 2177 | } |
---|
[2566] | 2178 | |
---|
[0] | 2179 | /** |
---|
| 2180 | Updates comment status. |
---|
[2566] | 2181 | |
---|
[0] | 2182 | @param id <b>integer</b> Comment ID |
---|
| 2183 | @param status <b>integer</b> Comment status |
---|
| 2184 | */ |
---|
| 2185 | public function updCommentStatus($id,$status) |
---|
| 2186 | { |
---|
[1041] | 2187 | $this->updCommentsStatus($id,$status); |
---|
| 2188 | } |
---|
[2566] | 2189 | |
---|
[1041] | 2190 | /** |
---|
| 2191 | Updates comments status. |
---|
[2566] | 2192 | |
---|
[1041] | 2193 | @param ids <b>mixed</b> Comment(s) ID(s) |
---|
| 2194 | @param status <b>integer</b> Comment status |
---|
| 2195 | */ |
---|
| 2196 | public function updCommentsStatus($ids,$status) |
---|
| 2197 | { |
---|
[0] | 2198 | if (!$this->core->auth->check('publish,contentadmin',$this->id)) { |
---|
| 2199 | throw new Exception(__("You are not allowed to change this comment's status")); |
---|
| 2200 | } |
---|
[2566] | 2201 | |
---|
[1041] | 2202 | $co_ids = dcUtils::cleanIds($ids); |
---|
| 2203 | $status = (integer) $status; |
---|
[2566] | 2204 | |
---|
| 2205 | $strReq = |
---|
[2626] | 2206 | 'UPDATE '.$this->prefix.'comment '. |
---|
| 2207 | 'SET comment_status = '.$status.' '; |
---|
[2566] | 2208 | $strReq .= |
---|
[2626] | 2209 | 'WHERE comment_id'.$this->con->in($co_ids). |
---|
| 2210 | 'AND post_id in (SELECT tp.post_id '. |
---|
| 2211 | 'FROM '.$this->prefix.'post tp '. |
---|
| 2212 | "WHERE tp.blog_id = '".$this->con->escape($this->id)."' "; |
---|
[1041] | 2213 | if (!$this->core->auth->check('contentadmin',$this->id)) |
---|
| 2214 | { |
---|
[2566] | 2215 | $strReq .= |
---|
[1041] | 2216 | "AND user_id = '".$this->con->escape($this->core->auth->userID())."' "; |
---|
| 2217 | } |
---|
[2626] | 2218 | $strReq .= ')'; |
---|
[1041] | 2219 | $this->con->execute($strReq); |
---|
[1042] | 2220 | $this->triggerComments($co_ids); |
---|
[1041] | 2221 | $this->triggerBlog(); |
---|
[0] | 2222 | } |
---|
[2566] | 2223 | |
---|
[0] | 2224 | /** |
---|
| 2225 | Delete a comment |
---|
[2566] | 2226 | |
---|
[0] | 2227 | @param id <b>integer</b> Comment ID |
---|
| 2228 | */ |
---|
| 2229 | public function delComment($id) |
---|
| 2230 | { |
---|
[1041] | 2231 | $this->delComments($id); |
---|
| 2232 | } |
---|
[2566] | 2233 | |
---|
[1041] | 2234 | /** |
---|
| 2235 | Delete comments |
---|
[2566] | 2236 | |
---|
[1041] | 2237 | @param ids <b>mixed</b> Comment(s) ID(s) |
---|
| 2238 | */ |
---|
| 2239 | public function delComments($ids) |
---|
| 2240 | { |
---|
[0] | 2241 | if (!$this->core->auth->check('delete,contentadmin',$this->id)) { |
---|
| 2242 | throw new Exception(__('You are not allowed to delete comments')); |
---|
| 2243 | } |
---|
[2566] | 2244 | |
---|
[1041] | 2245 | $co_ids = dcUtils::cleanIds($ids); |
---|
[2566] | 2246 | |
---|
[1262] | 2247 | if (empty($co_ids)) { |
---|
[0] | 2248 | throw new Exception(__('No such comment ID')); |
---|
| 2249 | } |
---|
[2566] | 2250 | |
---|
[1262] | 2251 | # Retrieve posts affected by comments edition |
---|
| 2252 | $affected_posts = array(); |
---|
| 2253 | $strReq = |
---|
[1570] | 2254 | 'SELECT post_id '. |
---|
[1262] | 2255 | 'FROM '.$this->prefix.'comment '. |
---|
[1570] | 2256 | 'WHERE comment_id'.$this->con->in($co_ids). |
---|
| 2257 | 'GROUP BY post_id'; |
---|
[2566] | 2258 | |
---|
[1262] | 2259 | $rs = $this->con->select($strReq); |
---|
[2566] | 2260 | |
---|
[1262] | 2261 | while ($rs->fetch()) { |
---|
| 2262 | $affected_posts[] = (integer) $rs->post_id; |
---|
| 2263 | } |
---|
[2566] | 2264 | |
---|
[2626] | 2265 | $strReq = |
---|
| 2266 | 'DELETE FROM '.$this->prefix.'comment '. |
---|
| 2267 | 'WHERE comment_id'.$this->con->in($co_ids).' '. |
---|
| 2268 | 'AND post_id in (SELECT tp.post_id '. |
---|
| 2269 | 'FROM '.$this->prefix.'post tp '. |
---|
| 2270 | "WHERE tp.blog_id = '".$this->con->escape($this->id)."' "; |
---|
[0] | 2271 | #If user can only delete, we need to check the post's owner |
---|
| 2272 | if (!$this->core->auth->check('contentadmin',$this->id)) |
---|
| 2273 | { |
---|
[2566] | 2274 | $strReq .= |
---|
[2626] | 2275 | "AND tp.user_id = '".$this->con->escape($this->core->auth->userID())."' "; |
---|
[0] | 2276 | } |
---|
[2626] | 2277 | $strReq .= ")"; |
---|
[1041] | 2278 | $this->con->execute($strReq); |
---|
[1262] | 2279 | $this->triggerComments($co_ids, true, $affected_posts); |
---|
[0] | 2280 | $this->triggerBlog(); |
---|
| 2281 | } |
---|
[1052] | 2282 | |
---|
| 2283 | public function delJunkComments() |
---|
| 2284 | { |
---|
| 2285 | if (!$this->core->auth->check('delete,contentadmin',$this->id)) { |
---|
| 2286 | throw new Exception(__('You are not allowed to delete comments')); |
---|
| 2287 | } |
---|
[2566] | 2288 | |
---|
[2626] | 2289 | $strReq = |
---|
| 2290 | 'DELETE FROM '.$this->prefix.'comment '. |
---|
| 2291 | 'WHERE comment_status = -2 '. |
---|
| 2292 | 'AND post_id in (SELECT tp.post_id '. |
---|
| 2293 | 'FROM '.$this->prefix.'post tp '. |
---|
| 2294 | "WHERE tp.blog_id = '".$this->con->escape($this->id)."' "; |
---|
[1052] | 2295 | #If user can only delete, we need to check the post's owner |
---|
| 2296 | if (!$this->core->auth->check('contentadmin',$this->id)) |
---|
| 2297 | { |
---|
[2566] | 2298 | $strReq .= |
---|
[2626] | 2299 | "AND tp.user_id = '".$this->con->escape($this->core->auth->userID())."' "; |
---|
[1052] | 2300 | } |
---|
[2626] | 2301 | $strReq .= ")"; |
---|
[1052] | 2302 | $this->con->execute($strReq); |
---|
| 2303 | $this->triggerBlog(); |
---|
| 2304 | } |
---|
[2566] | 2305 | |
---|
[0] | 2306 | private function getCommentCursor($cur) |
---|
| 2307 | { |
---|
| 2308 | if ($cur->comment_content !== null && $cur->comment_content == '') { |
---|
| 2309 | throw new Exception(__('You must provide a comment')); |
---|
| 2310 | } |
---|
[2566] | 2311 | |
---|
[0] | 2312 | if ($cur->comment_author !== null && $cur->comment_author == '') { |
---|
| 2313 | throw new Exception(__('You must provide an author name')); |
---|
| 2314 | } |
---|
[2566] | 2315 | |
---|
[0] | 2316 | if ($cur->comment_email != '' && !text::isEmail($cur->comment_email)) { |
---|
| 2317 | throw new Exception(__('Email address is not valid.')); |
---|
| 2318 | } |
---|
[2566] | 2319 | |
---|
[0] | 2320 | if ($cur->comment_site !== null && $cur->comment_site != '') { |
---|
[1227] | 2321 | if (!preg_match('|^http(s?)://|i',$cur->comment_site, $matches)) { |
---|
[0] | 2322 | $cur->comment_site = 'http://'.$cur->comment_site; |
---|
[1227] | 2323 | }else{ |
---|
| 2324 | $cur->comment_site = strtolower($matches[0]).substr($cur->comment_site, strlen($matches[0])); |
---|
[0] | 2325 | } |
---|
| 2326 | } |
---|
[2566] | 2327 | |
---|
[0] | 2328 | if ($cur->comment_status === null) { |
---|
| 2329 | $cur->comment_status = (integer) $this->settings->system->comments_pub; |
---|
| 2330 | } |
---|
[2566] | 2331 | |
---|
[0] | 2332 | # Words list |
---|
| 2333 | if ($cur->comment_content !== null) |
---|
| 2334 | { |
---|
| 2335 | $cur->comment_words = implode(' ',text::splitWords($cur->comment_content)); |
---|
| 2336 | } |
---|
| 2337 | } |
---|
| 2338 | //@} |
---|
| 2339 | } |
---|