[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 | @defgroup DC_CORE Dotclear Core Classes |
---|
| 16 | */ |
---|
| 17 | |
---|
| 18 | /** |
---|
| 19 | @ingroup DC_CORE |
---|
| 20 | @nosubgrouping |
---|
| 21 | @brief Dotclear core class |
---|
| 22 | |
---|
| 23 | True to its name dcCore is the core of Dotclear. It handles everything related |
---|
| 24 | to blogs, database connection, plugins... |
---|
| 25 | */ |
---|
| 26 | class dcCore |
---|
| 27 | { |
---|
| 28 | public $con; ///< <b>connection</b> Database connection object |
---|
[407] | 29 | public $prefix; ///< <b>string</b> Database tables prefix |
---|
[0] | 30 | public $blog; ///< <b>dcBlog</b> dcBlog object |
---|
| 31 | public $error; ///< <b>dcError</b> dcError object |
---|
| 32 | public $auth; ///< <b>dcAuth</b> dcAuth object |
---|
| 33 | public $session; ///< <b>sessionDB</b> sessionDB object |
---|
| 34 | public $url; ///< <b>urlHandler</b> urlHandler object |
---|
| 35 | public $wiki2xhtml; ///< <b>wiki2xhtml</b> wiki2xhtml object |
---|
| 36 | public $plugins; ///< <b>dcModules</b> dcModules object |
---|
| 37 | public $media; ///< <b>dcMedia</b> dcMedia object |
---|
[407] | 38 | public $postmedia; ///< <b>dcPostMedia</b> dcPostMedia object |
---|
| 39 | public $rest; ///< <b>dcRestServer</b> dcRestServer object |
---|
[0] | 40 | public $log; ///< <b>dcLog</b> dcLog object |
---|
[2595] | 41 | public $stime; ///< <b>float</b> starting time |
---|
[2566] | 42 | |
---|
[0] | 43 | private $versions = null; |
---|
| 44 | private $formaters = array(); |
---|
| 45 | private $behaviors = array(); |
---|
| 46 | private $post_types = array(); |
---|
[2566] | 47 | |
---|
[0] | 48 | /** |
---|
| 49 | dcCore constructor inits everything related to Dotclear. It takes arguments |
---|
| 50 | to init database connection. |
---|
[2566] | 51 | |
---|
[0] | 52 | @param driver <b>string</b> Database driver name |
---|
| 53 | @param host <b>string</b> Database hostname |
---|
| 54 | @param db <b>string</b> Database name |
---|
| 55 | @param user <b>string</b> Database username |
---|
| 56 | @param password <b>string</b> Database password |
---|
| 57 | @param prefix <b>string</b> DotClear tables prefix |
---|
| 58 | @param persist <b>boolean</b> Persistent database connection |
---|
| 59 | */ |
---|
| 60 | public function __construct($driver, $host, $db, $user, $password, $prefix, $persist) |
---|
| 61 | { |
---|
[2595] | 62 | if (defined('DC_START_TIME')) { |
---|
| 63 | $this->stime=DC_START_TIME; |
---|
| 64 | } else { |
---|
| 65 | $this->stime = microtime(true); |
---|
| 66 | } |
---|
| 67 | |
---|
[0] | 68 | $this->con = dbLayer::init($driver,$host,$db,$user,$password,$persist); |
---|
[2566] | 69 | |
---|
[0] | 70 | # define weak_locks for mysql |
---|
| 71 | if ($this->con instanceof mysqlConnection) { |
---|
| 72 | mysqlConnection::$weak_locks = true; |
---|
[1743] | 73 | } elseif ($this->con instanceof mysqliConnection) { |
---|
| 74 | mysqliConnection::$weak_locks = true; |
---|
[0] | 75 | } |
---|
[2566] | 76 | |
---|
[331] | 77 | # define searchpath for postgresql |
---|
| 78 | if ($this->con instanceof pgsqlConnection) |
---|
| 79 | { |
---|
| 80 | $searchpath = explode ('.',$prefix,2); |
---|
| 81 | if (count($searchpath) > 1) |
---|
| 82 | { |
---|
| 83 | $prefix = $searchpath[1]; |
---|
| 84 | $sql = 'SET search_path TO '.$searchpath[0].',public;'; |
---|
| 85 | $this->con->execute($sql); |
---|
| 86 | } |
---|
| 87 | } |
---|
[2566] | 88 | |
---|
[0] | 89 | $this->prefix = $prefix; |
---|
[2566] | 90 | |
---|
[0] | 91 | $this->error = new dcError(); |
---|
| 92 | $this->auth = $this->authInstance(); |
---|
| 93 | $this->session = new sessionDB($this->con,$this->prefix.'session',DC_SESSION_NAME,'',null,DC_ADMIN_SSL); |
---|
| 94 | $this->url = new dcUrlHandlers(); |
---|
[2566] | 95 | |
---|
[2492] | 96 | $this->plugins = new dcPlugins($this); |
---|
[2566] | 97 | |
---|
[0] | 98 | $this->rest = new dcRestServer($this); |
---|
[2566] | 99 | |
---|
[0] | 100 | $this->meta = new dcMeta($this); |
---|
[2566] | 101 | |
---|
[0] | 102 | $this->log = new dcLog($this); |
---|
| 103 | } |
---|
[2566] | 104 | |
---|
[0] | 105 | private function authInstance() |
---|
| 106 | { |
---|
| 107 | # You can set DC_AUTH_CLASS to whatever you want. |
---|
| 108 | # Your new class *should* inherits dcAuth. |
---|
| 109 | if (!defined('DC_AUTH_CLASS')) { |
---|
| 110 | $c = 'dcAuth'; |
---|
| 111 | } else { |
---|
| 112 | $c = DC_AUTH_CLASS; |
---|
| 113 | } |
---|
[2566] | 114 | |
---|
[0] | 115 | if (!class_exists($c)) { |
---|
| 116 | throw new Exception('Authentication class '.$c.' does not exist.'); |
---|
| 117 | } |
---|
[2566] | 118 | |
---|
[0] | 119 | if ($c != 'dcAuth' && !is_subclass_of($c,'dcAuth')) { |
---|
| 120 | throw new Exception('Authentication class '.$c.' does not inherit dcAuth.'); |
---|
| 121 | } |
---|
[2566] | 122 | |
---|
[0] | 123 | return new $c($this); |
---|
| 124 | } |
---|
[2566] | 125 | |
---|
| 126 | |
---|
[0] | 127 | /// @name Blog init methods |
---|
| 128 | //@{ |
---|
| 129 | /** |
---|
| 130 | Sets a blog to use in <var>blog</var> property. |
---|
[2566] | 131 | |
---|
[0] | 132 | @param id <b>string</b> Blog ID |
---|
| 133 | */ |
---|
| 134 | public function setBlog($id) |
---|
| 135 | { |
---|
| 136 | $this->blog = new dcBlog($this, $id); |
---|
| 137 | } |
---|
[2566] | 138 | |
---|
[0] | 139 | /** |
---|
| 140 | Unsets <var>blog</var> property. |
---|
| 141 | */ |
---|
| 142 | public function unsetBlog() |
---|
| 143 | { |
---|
| 144 | $this->blog = null; |
---|
| 145 | } |
---|
| 146 | //@} |
---|
[2566] | 147 | |
---|
| 148 | |
---|
[0] | 149 | /// @name Blog status methods |
---|
| 150 | //@{ |
---|
| 151 | /** |
---|
| 152 | Returns an array of available blog status codes and names. |
---|
[2566] | 153 | |
---|
[0] | 154 | @return <b>array</b> Simple array with codes in keys and names in value |
---|
| 155 | */ |
---|
| 156 | public function getAllBlogStatus() |
---|
| 157 | { |
---|
| 158 | return array( |
---|
| 159 | 1 => __('online'), |
---|
| 160 | 0 => __('offline'), |
---|
| 161 | -1 => __('removed') |
---|
| 162 | ); |
---|
| 163 | } |
---|
[2566] | 164 | |
---|
[0] | 165 | /** |
---|
| 166 | Returns a blog status name given to a code. This is intended to be |
---|
| 167 | human-readable and will be translated, so never use it for tests. |
---|
| 168 | If status code does not exist, returns <i>offline</i>. |
---|
[2566] | 169 | |
---|
[0] | 170 | @param s <b>integer</b> Status code |
---|
| 171 | @return <b>string</b> Blog status name |
---|
| 172 | */ |
---|
| 173 | public function getBlogStatus($s) |
---|
| 174 | { |
---|
| 175 | $r = $this->getAllBlogStatus(); |
---|
| 176 | if (isset($r[$s])) { |
---|
| 177 | return $r[$s]; |
---|
| 178 | } |
---|
| 179 | return $r[0]; |
---|
| 180 | } |
---|
| 181 | //@} |
---|
[2566] | 182 | |
---|
[0] | 183 | /// @name Admin nonce secret methods |
---|
| 184 | //@{ |
---|
[2566] | 185 | |
---|
[0] | 186 | public function getNonce() |
---|
| 187 | { |
---|
| 188 | return crypt::hmac(DC_MASTER_KEY,session_id()); |
---|
| 189 | } |
---|
[2566] | 190 | |
---|
[0] | 191 | public function checkNonce($secret) |
---|
| 192 | { |
---|
| 193 | if (!preg_match('/^([0-9a-f]{40,})$/i',$secret)) { |
---|
| 194 | return false; |
---|
| 195 | } |
---|
[2566] | 196 | |
---|
[0] | 197 | return $secret == crypt::hmac(DC_MASTER_KEY,session_id()); |
---|
| 198 | } |
---|
[2566] | 199 | |
---|
[0] | 200 | public function formNonce() |
---|
| 201 | { |
---|
| 202 | if (!session_id()) { |
---|
| 203 | return; |
---|
| 204 | } |
---|
[2566] | 205 | |
---|
[0] | 206 | return form::hidden(array('xd_check'),$this->getNonce()); |
---|
| 207 | } |
---|
| 208 | //@} |
---|
[2566] | 209 | |
---|
[2679] | 210 | /// @name Text Formatters methods |
---|
| 211 | //@{ |
---|
| 212 | /** |
---|
| 213 | Adds a new text formater which will call the function <var>$func</var> to |
---|
| 214 | transform text. The function must be a valid callback and takes one |
---|
| 215 | argument: the string to transform. It returns the transformed string. |
---|
| 216 | |
---|
| 217 | @param editor_id <b>string</b> Editor id (dcLegacyEditor, dcCKEditor, ...) |
---|
| 218 | @param name <b>string</b> Formater name |
---|
| 219 | @param func <b>callback</b> Function to use, must be a valid and callable callback |
---|
| 220 | */ |
---|
| 221 | public function addEditorFormater($editor_id,$name,$func) |
---|
| 222 | { |
---|
| 223 | if (is_callable($func)) { |
---|
| 224 | $this->formaters[$editor_id][$name] = $func; |
---|
| 225 | } |
---|
| 226 | } |
---|
[2566] | 227 | |
---|
[0] | 228 | /// @name Text Formatters methods |
---|
| 229 | //@{ |
---|
| 230 | /** |
---|
| 231 | Adds a new text formater which will call the function <var>$func</var> to |
---|
| 232 | transform text. The function must be a valid callback and takes one |
---|
| 233 | argument: the string to transform. It returns the transformed string. |
---|
[2566] | 234 | |
---|
[0] | 235 | @param name <b>string</b> Formater name |
---|
| 236 | @param func <b>callback</b> Function to use, must be a valid and callable callback |
---|
| 237 | */ |
---|
| 238 | public function addFormater($name,$func) |
---|
| 239 | { |
---|
[2679] | 240 | $this->addEditorFormater('dcLegacyEditor',$name,$func); |
---|
[0] | 241 | } |
---|
[2566] | 242 | |
---|
[0] | 243 | /** |
---|
[2679] | 244 | Returns editors list |
---|
[2566] | 245 | |
---|
[2679] | 246 | @return <b>array</b> An array of editors values. |
---|
| 247 | */ |
---|
| 248 | public function getEditors() |
---|
| 249 | { |
---|
| 250 | $editors = array(); |
---|
| 251 | |
---|
| 252 | foreach (array_keys($this->formaters) as $editor_id) { |
---|
| 253 | $editors[$editor_id] = $this->plugins->moduleInfo($editor_id,'name'); |
---|
| 254 | } |
---|
| 255 | |
---|
| 256 | return $editors; |
---|
| 257 | } |
---|
| 258 | |
---|
| 259 | /** |
---|
| 260 | Returns formaters list by editor |
---|
| 261 | |
---|
| 262 | @param editor_id <b>string</b> Editor id (dcLegacyEditor, dcCKEditor, ...) |
---|
[0] | 263 | @return <b>array</b> An array of formaters names in values. |
---|
| 264 | */ |
---|
[2679] | 265 | public function getFormaters($editor_id='') |
---|
[0] | 266 | { |
---|
[2679] | 267 | $formaters_list = array(); |
---|
| 268 | |
---|
| 269 | if (!empty($editor_id) && isset($this->formaters[$editor_id])) { |
---|
| 270 | $formaters_list = array_keys($this->formaters[$editor_id]); |
---|
| 271 | } else { |
---|
| 272 | foreach ($this->formaters as $editor => $formaters) { |
---|
| 273 | $formaters_list[$editor] = array_keys($formaters); |
---|
| 274 | } |
---|
| 275 | } |
---|
| 276 | |
---|
| 277 | return $formaters_list; |
---|
[0] | 278 | } |
---|
[2566] | 279 | |
---|
[0] | 280 | /** |
---|
| 281 | If <var>$name</var> is a valid formater, it returns <var>$str</var> |
---|
| 282 | transformed using that formater. |
---|
[2566] | 283 | |
---|
[2679] | 284 | @param editor_id <b>string</b> Editor id (dcLegacyEditor, dcCKEditor, ...) |
---|
| 285 | @param name <b>string</b> Formater name |
---|
| 286 | @param str <b>string</b> String to transform |
---|
| 287 | @return <b>string</b> String transformed |
---|
| 288 | */ |
---|
| 289 | public function callEditorFormater($editor_id,$name,$str) |
---|
| 290 | { |
---|
| 291 | if (isset($this->formaters[$editor_id]) && isset($this->formaters[$editor_id][$name])) { |
---|
| 292 | return call_user_func($this->formaters[$editor_id][$name],$str); |
---|
| 293 | } |
---|
| 294 | |
---|
| 295 | return $str; |
---|
| 296 | } |
---|
| 297 | //@} |
---|
| 298 | |
---|
| 299 | /** |
---|
| 300 | If <var>$name</var> is a valid formater, it returns <var>$str</var> |
---|
| 301 | transformed using that formater. |
---|
| 302 | |
---|
[0] | 303 | @param name <b>string</b> Formater name |
---|
| 304 | @param str <b>string</b> String to transform |
---|
| 305 | @return <b>string</b> String transformed |
---|
| 306 | */ |
---|
| 307 | public function callFormater($name,$str) |
---|
| 308 | { |
---|
[2679] | 309 | return $this->callEditorFormater('dcLegacyEditor',$name,$str); |
---|
[0] | 310 | } |
---|
| 311 | //@} |
---|
[2566] | 312 | |
---|
| 313 | |
---|
[0] | 314 | /// @name Behaviors methods |
---|
| 315 | //@{ |
---|
| 316 | /** |
---|
| 317 | Adds a new behavior to behaviors stack. <var>$func</var> must be a valid |
---|
| 318 | and callable callback. |
---|
[2566] | 319 | |
---|
[0] | 320 | @param behavior <b>string</b> Behavior name |
---|
| 321 | @param func <b>callback</b> Function to call |
---|
| 322 | */ |
---|
| 323 | public function addBehavior($behavior,$func) |
---|
| 324 | { |
---|
| 325 | if (is_callable($func)) { |
---|
| 326 | $this->behaviors[$behavior][] = $func; |
---|
| 327 | } |
---|
| 328 | } |
---|
[2566] | 329 | |
---|
[0] | 330 | /** |
---|
| 331 | Tests if a particular behavior exists in behaviors stack. |
---|
[2566] | 332 | |
---|
[0] | 333 | @param behavior <b>string</b> Behavior name |
---|
| 334 | @return <b>boolean</b> |
---|
| 335 | */ |
---|
| 336 | public function hasBehavior($behavior) |
---|
| 337 | { |
---|
| 338 | return isset($this->behaviors[$behavior]); |
---|
| 339 | } |
---|
[2566] | 340 | |
---|
[0] | 341 | /** |
---|
| 342 | Get behaviors stack (or part of). |
---|
[2566] | 343 | |
---|
[0] | 344 | @param behavior <b>string</b> Behavior name |
---|
| 345 | @return <b>array</b> |
---|
| 346 | */ |
---|
| 347 | public function getBehaviors($behavior='') |
---|
| 348 | { |
---|
| 349 | if (empty($this->behaviors)) return null; |
---|
[2566] | 350 | |
---|
[0] | 351 | if ($behavior == '') { |
---|
| 352 | return $this->behaviors; |
---|
| 353 | } elseif (isset($this->behaviors[$behavior])) { |
---|
| 354 | return $this->behaviors[$behavior]; |
---|
| 355 | } |
---|
[2566] | 356 | |
---|
[0] | 357 | return array(); |
---|
| 358 | } |
---|
[2566] | 359 | |
---|
[0] | 360 | /** |
---|
| 361 | Calls every function in behaviors stack for a given behavior and returns |
---|
| 362 | concatened result of each function. |
---|
[2566] | 363 | |
---|
[0] | 364 | Every parameters added after <var>$behavior</var> will be pass to |
---|
| 365 | behavior calls. |
---|
[2566] | 366 | |
---|
[0] | 367 | @param behavior <b>string</b> Behavior name |
---|
| 368 | @return <b>string</b> Behavior concatened result |
---|
| 369 | */ |
---|
| 370 | public function callBehavior($behavior) |
---|
| 371 | { |
---|
| 372 | if (isset($this->behaviors[$behavior])) |
---|
| 373 | { |
---|
| 374 | $args = func_get_args(); |
---|
| 375 | array_shift($args); |
---|
[2566] | 376 | |
---|
[0] | 377 | $res = ''; |
---|
[2566] | 378 | |
---|
[0] | 379 | foreach ($this->behaviors[$behavior] as $f) { |
---|
| 380 | $res .= call_user_func_array($f,$args); |
---|
| 381 | } |
---|
[2566] | 382 | |
---|
[0] | 383 | return $res; |
---|
| 384 | } |
---|
| 385 | } |
---|
| 386 | //@} |
---|
[2566] | 387 | |
---|
[0] | 388 | /// @name Post types URLs management |
---|
| 389 | //@{ |
---|
| 390 | public function getPostAdminURL($type,$post_id,$escaped=true) |
---|
| 391 | { |
---|
| 392 | if (!isset($this->post_types[$type])) { |
---|
| 393 | $type = 'post'; |
---|
| 394 | } |
---|
[2566] | 395 | |
---|
[0] | 396 | $url = sprintf($this->post_types[$type]['admin_url'],$post_id); |
---|
| 397 | return $escaped ? html::escapeURL($url) : $url; |
---|
| 398 | } |
---|
[2566] | 399 | |
---|
[0] | 400 | public function getPostPublicURL($type,$post_url,$escaped=true) |
---|
| 401 | { |
---|
| 402 | if (!isset($this->post_types[$type])) { |
---|
| 403 | $type = 'post'; |
---|
| 404 | } |
---|
[2566] | 405 | |
---|
[0] | 406 | $url = sprintf($this->post_types[$type]['public_url'],$post_url); |
---|
| 407 | return $escaped ? html::escapeURL($url) : $url; |
---|
| 408 | } |
---|
[2566] | 409 | |
---|
[1694] | 410 | public function setPostType($type,$admin_url,$public_url,$label='') |
---|
[0] | 411 | { |
---|
| 412 | $this->post_types[$type] = array( |
---|
| 413 | 'admin_url' => $admin_url, |
---|
[1694] | 414 | 'public_url' => $public_url, |
---|
| 415 | 'label' => ($label != '' ? $label : $type) |
---|
[0] | 416 | ); |
---|
| 417 | } |
---|
[2566] | 418 | |
---|
[0] | 419 | public function getPostTypes() |
---|
| 420 | { |
---|
| 421 | return $this->post_types; |
---|
| 422 | } |
---|
| 423 | //@} |
---|
[2566] | 424 | |
---|
[0] | 425 | /// @name Versions management methods |
---|
| 426 | //@{ |
---|
| 427 | /** |
---|
| 428 | Returns a given $module version. |
---|
[2566] | 429 | |
---|
[0] | 430 | @param module <b>string</b> Module name |
---|
| 431 | @return <b>string</b> Module version |
---|
| 432 | */ |
---|
| 433 | public function getVersion($module='core') |
---|
| 434 | { |
---|
| 435 | # Fetch versions if needed |
---|
| 436 | if (!is_array($this->versions)) |
---|
| 437 | { |
---|
| 438 | $strReq = 'SELECT module, version FROM '.$this->prefix.'version'; |
---|
| 439 | $rs = $this->con->select($strReq); |
---|
[2566] | 440 | |
---|
[0] | 441 | while ($rs->fetch()) { |
---|
| 442 | $this->versions[$rs->module] = $rs->version; |
---|
| 443 | } |
---|
| 444 | } |
---|
[2566] | 445 | |
---|
[0] | 446 | if (isset($this->versions[$module])) { |
---|
| 447 | return $this->versions[$module]; |
---|
| 448 | } else { |
---|
| 449 | return null; |
---|
| 450 | } |
---|
| 451 | } |
---|
[2566] | 452 | |
---|
[0] | 453 | /** |
---|
| 454 | Sets $version to given $module. |
---|
[2566] | 455 | |
---|
[0] | 456 | @param module <b>string</b> Module name |
---|
| 457 | @param version <b>string</b> Module version |
---|
| 458 | */ |
---|
| 459 | public function setVersion($module,$version) |
---|
| 460 | { |
---|
| 461 | $cur_version = $this->getVersion($module); |
---|
[2566] | 462 | |
---|
[0] | 463 | $cur = $this->con->openCursor($this->prefix.'version'); |
---|
| 464 | $cur->module = (string) $module; |
---|
| 465 | $cur->version = (string) $version; |
---|
[2566] | 466 | |
---|
[0] | 467 | if ($cur_version === null) { |
---|
| 468 | $cur->insert(); |
---|
| 469 | } else { |
---|
| 470 | $cur->update("WHERE module='".$this->con->escape($module)."'"); |
---|
| 471 | } |
---|
[2566] | 472 | |
---|
[0] | 473 | $this->versions[$module] = $version; |
---|
| 474 | } |
---|
[2566] | 475 | |
---|
[0] | 476 | /** |
---|
| 477 | Removes given $module version entry. |
---|
[2566] | 478 | |
---|
[0] | 479 | @param module <b>string</b> Module name |
---|
| 480 | */ |
---|
| 481 | public function delVersion($module) |
---|
| 482 | { |
---|
| 483 | $strReq = |
---|
| 484 | 'DELETE FROM '.$this->prefix.'version '. |
---|
| 485 | "WHERE module = '".$this->con->escape($module)."' "; |
---|
[2566] | 486 | |
---|
[0] | 487 | $this->con->execute($strReq); |
---|
[2566] | 488 | |
---|
[0] | 489 | if (is_array($this->versions)) { |
---|
| 490 | unset($this->versions[$module]); |
---|
| 491 | } |
---|
| 492 | } |
---|
[2566] | 493 | |
---|
[0] | 494 | //@} |
---|
[2566] | 495 | |
---|
[0] | 496 | /// @name Users management methods |
---|
| 497 | //@{ |
---|
| 498 | /** |
---|
| 499 | Returns a user by its ID. |
---|
[2566] | 500 | |
---|
[0] | 501 | @param id <b>string</b> User ID |
---|
| 502 | @return <b>record</b> |
---|
| 503 | */ |
---|
| 504 | public function getUser($id) |
---|
| 505 | { |
---|
| 506 | $params['user_id'] = $id; |
---|
[2566] | 507 | |
---|
[0] | 508 | return $this->getUsers($params); |
---|
| 509 | } |
---|
[2566] | 510 | |
---|
[0] | 511 | /** |
---|
| 512 | Returns a users list. <b>$params</b> is an array with the following |
---|
| 513 | optionnal parameters: |
---|
[2566] | 514 | |
---|
[0] | 515 | - <var>q</var>: search string (on user_id, user_name, user_firstname) |
---|
| 516 | - <var>user_id</var>: user ID |
---|
| 517 | - <var>order</var>: ORDER BY clause (default: user_id ASC) |
---|
| 518 | - <var>limit</var>: LIMIT clause (should be an array ![limit,offset]) |
---|
[2566] | 519 | |
---|
[0] | 520 | @param params <b>array</b> Parameters |
---|
| 521 | @param count_only <b>boolean</b> Only counts results |
---|
| 522 | @return <b>record</b> |
---|
| 523 | */ |
---|
| 524 | public function getUsers($params=array(),$count_only=false) |
---|
| 525 | { |
---|
| 526 | if ($count_only) |
---|
| 527 | { |
---|
| 528 | $strReq = |
---|
| 529 | 'SELECT count(U.user_id) '. |
---|
| 530 | 'FROM '.$this->prefix.'user U '. |
---|
| 531 | 'WHERE NULL IS NULL '; |
---|
| 532 | } |
---|
| 533 | else |
---|
| 534 | { |
---|
| 535 | $strReq = |
---|
| 536 | 'SELECT U.user_id,user_super,user_status,user_pwd,user_change_pwd,'. |
---|
| 537 | 'user_name,user_firstname,user_displayname,user_email,user_url,'. |
---|
| 538 | 'user_desc, user_lang,user_tz, user_post_status,user_options, '. |
---|
| 539 | 'count(P.post_id) AS nb_post '. |
---|
| 540 | 'FROM '.$this->prefix.'user U '. |
---|
| 541 | 'LEFT JOIN '.$this->prefix.'post P ON U.user_id = P.user_id '. |
---|
| 542 | 'WHERE NULL IS NULL '; |
---|
| 543 | } |
---|
[2566] | 544 | |
---|
[0] | 545 | if (!empty($params['q'])) { |
---|
| 546 | $q = $this->con->escape(str_replace('*','%',strtolower($params['q']))); |
---|
| 547 | $strReq .= 'AND ('. |
---|
| 548 | "LOWER(U.user_id) LIKE '".$q."' ". |
---|
| 549 | "OR LOWER(user_name) LIKE '".$q."' ". |
---|
| 550 | "OR LOWER(user_firstname) LIKE '".$q."' ". |
---|
| 551 | ') '; |
---|
| 552 | } |
---|
[2566] | 553 | |
---|
[0] | 554 | if (!empty($params['user_id'])) { |
---|
| 555 | $strReq .= "AND U.user_id = '".$this->con->escape($params['user_id'])."' "; |
---|
| 556 | } |
---|
[2566] | 557 | |
---|
[0] | 558 | if (!$count_only) { |
---|
| 559 | $strReq .= 'GROUP BY U.user_id,user_super,user_status,user_pwd,user_change_pwd,'. |
---|
| 560 | 'user_name,user_firstname,user_displayname,user_email,user_url,'. |
---|
| 561 | 'user_desc, user_lang,user_tz,user_post_status,user_options '; |
---|
[2566] | 562 | |
---|
[0] | 563 | if (!empty($params['order']) && !$count_only) { |
---|
| 564 | $strReq .= 'ORDER BY '.$this->con->escape($params['order']).' '; |
---|
| 565 | } else { |
---|
| 566 | $strReq .= 'ORDER BY U.user_id ASC '; |
---|
| 567 | } |
---|
| 568 | } |
---|
[2566] | 569 | |
---|
[0] | 570 | if (!$count_only && !empty($params['limit'])) { |
---|
| 571 | $strReq .= $this->con->limit($params['limit']); |
---|
| 572 | } |
---|
[2566] | 573 | |
---|
[0] | 574 | $rs = $this->con->select($strReq); |
---|
| 575 | $rs->extend('rsExtUser'); |
---|
| 576 | return $rs; |
---|
| 577 | } |
---|
[2566] | 578 | |
---|
[0] | 579 | /** |
---|
| 580 | Create a new user. Takes a cursor as input and returns the new user ID. |
---|
[2566] | 581 | |
---|
[0] | 582 | @param cur <b>cursor</b> User cursor |
---|
| 583 | @return <b>string</b> |
---|
| 584 | */ |
---|
| 585 | public function addUser($cur) |
---|
| 586 | { |
---|
| 587 | if (!$this->auth->isSuperAdmin()) { |
---|
| 588 | throw new Exception(__('You are not an administrator')); |
---|
| 589 | } |
---|
[2566] | 590 | |
---|
[0] | 591 | if ($cur->user_id == '') { |
---|
| 592 | throw new Exception(__('No user ID given')); |
---|
| 593 | } |
---|
[2566] | 594 | |
---|
[0] | 595 | if ($cur->user_pwd == '') { |
---|
| 596 | throw new Exception(__('No password given')); |
---|
| 597 | } |
---|
[2566] | 598 | |
---|
[0] | 599 | $this->getUserCursor($cur); |
---|
[2566] | 600 | |
---|
[0] | 601 | if ($cur->user_creadt === null) { |
---|
| 602 | $cur->user_creadt = date('Y-m-d H:i:s'); |
---|
| 603 | } |
---|
[2566] | 604 | |
---|
[0] | 605 | $cur->insert(); |
---|
[2566] | 606 | |
---|
[0] | 607 | $this->auth->afterAddUser($cur); |
---|
[2566] | 608 | |
---|
[0] | 609 | return $cur->user_id; |
---|
| 610 | } |
---|
[2566] | 611 | |
---|
[0] | 612 | /** |
---|
| 613 | Updates an existing user. Returns the user ID. |
---|
[2566] | 614 | |
---|
[0] | 615 | @param id <b>string</b> User ID |
---|
| 616 | @param cur <b>cursor</b> User cursor |
---|
| 617 | @return <b>string</b> |
---|
| 618 | */ |
---|
| 619 | public function updUser($id,$cur) |
---|
| 620 | { |
---|
| 621 | $this->getUserCursor($cur); |
---|
[2566] | 622 | |
---|
[0] | 623 | if (($cur->user_id !== null || $id != $this->auth->userID()) && |
---|
| 624 | !$this->auth->isSuperAdmin()) { |
---|
| 625 | throw new Exception(__('You are not an administrator')); |
---|
| 626 | } |
---|
[2566] | 627 | |
---|
[0] | 628 | $cur->update("WHERE user_id = '".$this->con->escape($id)."' "); |
---|
[2566] | 629 | |
---|
[0] | 630 | $this->auth->afterUpdUser($id,$cur); |
---|
[2566] | 631 | |
---|
[0] | 632 | if ($cur->user_id !== null) { |
---|
| 633 | $id = $cur->user_id; |
---|
| 634 | } |
---|
[2566] | 635 | |
---|
[0] | 636 | # Updating all user's blogs |
---|
| 637 | $rs = $this->con->select( |
---|
| 638 | 'SELECT DISTINCT(blog_id) FROM '.$this->prefix.'post '. |
---|
| 639 | "WHERE user_id = '".$this->con->escape($id)."' " |
---|
| 640 | ); |
---|
[2566] | 641 | |
---|
[0] | 642 | while ($rs->fetch()) { |
---|
| 643 | $b = new dcBlog($this,$rs->blog_id); |
---|
| 644 | $b->triggerBlog(); |
---|
| 645 | unset($b); |
---|
| 646 | } |
---|
[2566] | 647 | |
---|
[0] | 648 | return $id; |
---|
| 649 | } |
---|
[2566] | 650 | |
---|
[0] | 651 | /** |
---|
| 652 | Deletes a user. |
---|
[2566] | 653 | |
---|
[0] | 654 | @param id <b>string</b> User ID |
---|
| 655 | */ |
---|
| 656 | public function delUser($id) |
---|
| 657 | { |
---|
| 658 | if (!$this->auth->isSuperAdmin()) { |
---|
| 659 | throw new Exception(__('You are not an administrator')); |
---|
| 660 | } |
---|
[2566] | 661 | |
---|
[0] | 662 | if ($id == $this->auth->userID()) { |
---|
| 663 | return; |
---|
| 664 | } |
---|
[2566] | 665 | |
---|
[0] | 666 | $rs = $this->getUser($id); |
---|
[2566] | 667 | |
---|
[0] | 668 | if ($rs->nb_post > 0) { |
---|
| 669 | return; |
---|
| 670 | } |
---|
[2566] | 671 | |
---|
[0] | 672 | $strReq = 'DELETE FROM '.$this->prefix.'user '. |
---|
| 673 | "WHERE user_id = '".$this->con->escape($id)."' "; |
---|
[2566] | 674 | |
---|
[0] | 675 | $this->con->execute($strReq); |
---|
[2566] | 676 | |
---|
[0] | 677 | $this->auth->afterDelUser($id); |
---|
| 678 | } |
---|
[2566] | 679 | |
---|
[0] | 680 | /** |
---|
| 681 | Checks whether a user exists. |
---|
[2566] | 682 | |
---|
[0] | 683 | @param id <b>string</b> User ID |
---|
| 684 | @return <b>boolean</b> |
---|
| 685 | */ |
---|
| 686 | public function userExists($id) |
---|
| 687 | { |
---|
| 688 | $strReq = 'SELECT user_id '. |
---|
| 689 | 'FROM '.$this->prefix.'user '. |
---|
| 690 | "WHERE user_id = '".$this->con->escape($id)."' "; |
---|
[2566] | 691 | |
---|
[0] | 692 | $rs = $this->con->select($strReq); |
---|
[2566] | 693 | |
---|
[0] | 694 | return !$rs->isEmpty(); |
---|
| 695 | } |
---|
[2566] | 696 | |
---|
[0] | 697 | /** |
---|
| 698 | Returns all user permissions as an array which looks like: |
---|
[2566] | 699 | |
---|
[0] | 700 | - [blog_id] |
---|
| 701 | - [name] => Blog name |
---|
| 702 | - [url] => Blog URL |
---|
| 703 | - [p] |
---|
[2679] | 704 | - [permission] => true |
---|
[0] | 705 | - ... |
---|
[2566] | 706 | |
---|
[0] | 707 | @param id <b>string</b> User ID |
---|
| 708 | @return <b>array</b> |
---|
| 709 | */ |
---|
| 710 | public function getUserPermissions($id) |
---|
| 711 | { |
---|
| 712 | $strReq = 'SELECT B.blog_id, blog_name, blog_url, permissions '. |
---|
| 713 | 'FROM '.$this->prefix.'permissions P '. |
---|
| 714 | 'INNER JOIN '.$this->prefix.'blog B ON P.blog_id = B.blog_id '. |
---|
| 715 | "WHERE user_id = '".$this->con->escape($id)."' "; |
---|
[2566] | 716 | |
---|
[0] | 717 | $rs = $this->con->select($strReq); |
---|
[2566] | 718 | |
---|
[0] | 719 | $res = array(); |
---|
[2566] | 720 | |
---|
[0] | 721 | while ($rs->fetch()) |
---|
| 722 | { |
---|
| 723 | $res[$rs->blog_id] = array( |
---|
| 724 | 'name' => $rs->blog_name, |
---|
| 725 | 'url' => $rs->blog_url, |
---|
| 726 | 'p' => $this->auth->parsePermissions($rs->permissions) |
---|
| 727 | ); |
---|
| 728 | } |
---|
[2566] | 729 | |
---|
[0] | 730 | return $res; |
---|
| 731 | } |
---|
[2566] | 732 | |
---|
[0] | 733 | /** |
---|
| 734 | Sets user permissions. The <var>$perms</var> array looks like: |
---|
[2566] | 735 | |
---|
[0] | 736 | - [blog_id] => '|perm1|perm2|' |
---|
| 737 | - ... |
---|
[2566] | 738 | |
---|
[0] | 739 | @param id <b>string</b> User ID |
---|
| 740 | @param perms <b>array</b> Permissions array |
---|
| 741 | */ |
---|
| 742 | public function setUserPermissions($id,$perms) |
---|
| 743 | { |
---|
| 744 | if (!$this->auth->isSuperAdmin()) { |
---|
| 745 | throw new Exception(__('You are not an administrator')); |
---|
| 746 | } |
---|
[2566] | 747 | |
---|
[0] | 748 | $strReq = 'DELETE FROM '.$this->prefix.'permissions '. |
---|
| 749 | "WHERE user_id = '".$this->con->escape($id)."' "; |
---|
[2566] | 750 | |
---|
[0] | 751 | $this->con->execute($strReq); |
---|
[2566] | 752 | |
---|
[0] | 753 | foreach ($perms as $blog_id => $p) { |
---|
| 754 | $this->setUserBlogPermissions($id, $blog_id, $p, false); |
---|
| 755 | } |
---|
| 756 | } |
---|
[2566] | 757 | |
---|
[0] | 758 | /** |
---|
| 759 | Sets user permissions for a given blog. <var>$perms</var> is an array with |
---|
| 760 | permissions in values |
---|
[2566] | 761 | |
---|
[0] | 762 | @param id <b>string</b> User ID |
---|
| 763 | @param blog_id <b>string</b> Blog ID |
---|
| 764 | @param perms <b>array</b> Permissions |
---|
| 765 | @param delete_first <b>boolean</b> Delete permissions before |
---|
| 766 | */ |
---|
| 767 | public function setUserBlogPermissions($id, $blog_id, $perms, $delete_first=true) |
---|
| 768 | { |
---|
| 769 | if (!$this->auth->isSuperAdmin()) { |
---|
| 770 | throw new Exception(__('You are not an administrator')); |
---|
| 771 | } |
---|
[2566] | 772 | |
---|
[0] | 773 | $no_perm = empty($perms); |
---|
[2566] | 774 | |
---|
[0] | 775 | $perms = '|'.implode('|',array_keys($perms)).'|'; |
---|
[2566] | 776 | |
---|
[0] | 777 | $cur = $this->con->openCursor($this->prefix.'permissions'); |
---|
[2566] | 778 | |
---|
[0] | 779 | $cur->user_id = (string) $id; |
---|
| 780 | $cur->blog_id = (string) $blog_id; |
---|
| 781 | $cur->permissions = $perms; |
---|
[2566] | 782 | |
---|
[0] | 783 | if ($delete_first || $no_perm) |
---|
| 784 | { |
---|
| 785 | $strReq = 'DELETE FROM '.$this->prefix.'permissions '. |
---|
| 786 | "WHERE blog_id = '".$this->con->escape($blog_id)."' ". |
---|
| 787 | "AND user_id = '".$this->con->escape($id)."' "; |
---|
[2566] | 788 | |
---|
[0] | 789 | $this->con->execute($strReq); |
---|
| 790 | } |
---|
[2566] | 791 | |
---|
[0] | 792 | if (!$no_perm) { |
---|
| 793 | $cur->insert(); |
---|
| 794 | } |
---|
| 795 | } |
---|
[2566] | 796 | |
---|
[0] | 797 | /** |
---|
| 798 | Sets a user default blog. This blog will be selected when user log in. |
---|
[2566] | 799 | |
---|
[0] | 800 | @param id <b>string</b> User ID |
---|
| 801 | @param blog_id <b>string</b> Blog ID |
---|
| 802 | */ |
---|
| 803 | public function setUserDefaultBlog($id, $blog_id) |
---|
| 804 | { |
---|
| 805 | $cur = $this->con->openCursor($this->prefix.'user'); |
---|
[2566] | 806 | |
---|
[0] | 807 | $cur->user_default_blog = (string) $blog_id; |
---|
[2566] | 808 | |
---|
[0] | 809 | $cur->update("WHERE user_id = '".$this->con->escape($id)."'"); |
---|
| 810 | } |
---|
[2566] | 811 | |
---|
[0] | 812 | private function getUserCursor($cur) |
---|
| 813 | { |
---|
| 814 | if ($cur->isField('user_id') |
---|
| 815 | && !preg_match('/^[A-Za-z0-9@._-]{2,}$/',$cur->user_id)) { |
---|
| 816 | throw new Exception(__('User ID must contain at least 2 characters using letters, numbers or symbols.')); |
---|
| 817 | } |
---|
[2566] | 818 | |
---|
[0] | 819 | if ($cur->user_url !== null && $cur->user_url != '') { |
---|
| 820 | if (!preg_match('|^http(s?)://|',$cur->user_url)) { |
---|
| 821 | $cur->user_url = 'http://'.$cur->user_url; |
---|
| 822 | } |
---|
| 823 | } |
---|
[2566] | 824 | |
---|
[0] | 825 | if ($cur->isField('user_pwd')) { |
---|
| 826 | if (strlen($cur->user_pwd) < 6) { |
---|
| 827 | throw new Exception(__('Password must contain at least 6 characters.')); |
---|
| 828 | } |
---|
| 829 | $cur->user_pwd = crypt::hmac(DC_MASTER_KEY,$cur->user_pwd); |
---|
| 830 | } |
---|
[2566] | 831 | |
---|
[0] | 832 | if ($cur->user_lang !== null && !preg_match('/^[a-z]{2}(-[a-z]{2})?$/',$cur->user_lang)) { |
---|
| 833 | throw new Exception(__('Invalid user language code')); |
---|
| 834 | } |
---|
[2566] | 835 | |
---|
[0] | 836 | if ($cur->user_upddt === null) { |
---|
| 837 | $cur->user_upddt = date('Y-m-d H:i:s'); |
---|
| 838 | } |
---|
[2566] | 839 | |
---|
[0] | 840 | if ($cur->user_options !== null) { |
---|
| 841 | $cur->user_options = serialize((array) $cur->user_options); |
---|
| 842 | } |
---|
| 843 | } |
---|
[2566] | 844 | |
---|
[0] | 845 | /** |
---|
| 846 | Returns user default settings in an associative array with setting names in |
---|
| 847 | keys. |
---|
[2566] | 848 | |
---|
[0] | 849 | @return <b>array</b> |
---|
| 850 | */ |
---|
| 851 | public function userDefaults() |
---|
| 852 | { |
---|
| 853 | return array( |
---|
| 854 | 'edit_size' => 24, |
---|
| 855 | 'enable_wysiwyg' => true, |
---|
[696] | 856 | 'post_format' => 'wiki' |
---|
[0] | 857 | ); |
---|
| 858 | } |
---|
| 859 | //@} |
---|
[2566] | 860 | |
---|
[0] | 861 | /// @name Blog management methods |
---|
| 862 | //@{ |
---|
| 863 | /** |
---|
| 864 | Returns all blog permissions (users) as an array which looks like: |
---|
[2566] | 865 | |
---|
[0] | 866 | - [user_id] |
---|
| 867 | - [name] => User name |
---|
| 868 | - [firstname] => User firstname |
---|
| 869 | - [displayname] => User displayname |
---|
| 870 | - [super] => (true|false) super admin |
---|
| 871 | - [p] |
---|
[2679] | 872 | - [permission] => true |
---|
[0] | 873 | - ... |
---|
[2566] | 874 | |
---|
[0] | 875 | @param id <b>string</b> Blog ID |
---|
| 876 | @param with_super <b>boolean</b> Includes super admins in result |
---|
| 877 | @return <b>array</b> |
---|
| 878 | */ |
---|
| 879 | public function getBlogPermissions($id,$with_super=true) |
---|
| 880 | { |
---|
| 881 | $strReq = |
---|
| 882 | 'SELECT U.user_id AS user_id, user_super, user_name, user_firstname, '. |
---|
[1698] | 883 | 'user_displayname, user_email, permissions '. |
---|
[0] | 884 | 'FROM '.$this->prefix.'user U '. |
---|
| 885 | 'JOIN '.$this->prefix.'permissions P ON U.user_id = P.user_id '. |
---|
| 886 | "WHERE blog_id = '".$this->con->escape($id)."' "; |
---|
[2566] | 887 | |
---|
[0] | 888 | if ($with_super) { |
---|
| 889 | $strReq .= |
---|
| 890 | 'UNION '. |
---|
| 891 | 'SELECT U.user_id AS user_id, user_super, user_name, user_firstname, '. |
---|
[1698] | 892 | "user_displayname, user_email, NULL AS permissions ". |
---|
[0] | 893 | 'FROM '.$this->prefix.'user U '. |
---|
| 894 | 'WHERE user_super = 1 '; |
---|
| 895 | } |
---|
[2566] | 896 | |
---|
[0] | 897 | $rs = $this->con->select($strReq); |
---|
[2566] | 898 | |
---|
[0] | 899 | $res = array(); |
---|
[2566] | 900 | |
---|
[0] | 901 | while ($rs->fetch()) |
---|
| 902 | { |
---|
| 903 | $res[$rs->user_id] = array( |
---|
| 904 | 'name' => $rs->user_name, |
---|
| 905 | 'firstname' => $rs->user_firstname, |
---|
| 906 | 'displayname' => $rs->user_displayname, |
---|
[1698] | 907 | 'email' => $rs->user_email, |
---|
[0] | 908 | 'super' => (boolean) $rs->user_super, |
---|
| 909 | 'p' => $this->auth->parsePermissions($rs->permissions) |
---|
| 910 | ); |
---|
| 911 | } |
---|
[2566] | 912 | |
---|
[0] | 913 | return $res; |
---|
| 914 | } |
---|
[2566] | 915 | |
---|
[0] | 916 | /** |
---|
| 917 | Returns a blog of given ID. |
---|
[2566] | 918 | |
---|
[0] | 919 | @param id <b>string</b> Blog ID |
---|
| 920 | @return <b>record</b> |
---|
| 921 | */ |
---|
| 922 | public function getBlog($id) |
---|
| 923 | { |
---|
| 924 | $blog = $this->getBlogs(array('blog_id'=>$id)); |
---|
[2566] | 925 | |
---|
[0] | 926 | if ($blog->isEmpty()) { |
---|
| 927 | return false; |
---|
| 928 | } |
---|
[2566] | 929 | |
---|
[0] | 930 | return $blog; |
---|
| 931 | } |
---|
[2566] | 932 | |
---|
[0] | 933 | /** |
---|
| 934 | Returns a record of blogs. <b>$params</b> is an array with the following |
---|
| 935 | optionnal parameters: |
---|
[2566] | 936 | |
---|
[0] | 937 | - <var>blog_id</var>: Blog ID |
---|
| 938 | - <var>q</var>: Search string on blog_id, blog_name and blog_url |
---|
| 939 | - <var>limit</var>: limit results |
---|
[2566] | 940 | |
---|
[0] | 941 | @param params <b>array</b> Parameters |
---|
| 942 | @param count_only <b>boolean</b> Count only results |
---|
| 943 | @return <b>record</b> |
---|
| 944 | */ |
---|
| 945 | public function getBlogs($params=array(),$count_only=false) |
---|
| 946 | { |
---|
| 947 | $join = ''; // %1$s |
---|
| 948 | $where = ''; // %2$s |
---|
[2566] | 949 | |
---|
[0] | 950 | if ($count_only) |
---|
| 951 | { |
---|
| 952 | $strReq = 'SELECT count(B.blog_id) '. |
---|
| 953 | 'FROM '.$this->prefix.'blog B '. |
---|
| 954 | '%1$s '. |
---|
| 955 | 'WHERE NULL IS NULL '. |
---|
| 956 | '%2$s '; |
---|
| 957 | } |
---|
| 958 | else |
---|
| 959 | { |
---|
| 960 | $strReq = |
---|
| 961 | 'SELECT B.blog_id, blog_uid, blog_url, blog_name, blog_desc, blog_creadt, '. |
---|
| 962 | 'blog_upddt, blog_status '. |
---|
| 963 | 'FROM '.$this->prefix.'blog B '. |
---|
| 964 | '%1$s '. |
---|
| 965 | 'WHERE NULL IS NULL '. |
---|
| 966 | '%2$s '; |
---|
[2566] | 967 | |
---|
[0] | 968 | if (!empty($params['order'])) { |
---|
| 969 | $strReq .= 'ORDER BY '.$this->con->escape($params['order']).' '; |
---|
| 970 | } else { |
---|
| 971 | $strReq .= 'ORDER BY B.blog_id ASC '; |
---|
| 972 | } |
---|
[2566] | 973 | |
---|
[0] | 974 | if (!empty($params['limit'])) { |
---|
| 975 | $strReq .= $this->con->limit($params['limit']); |
---|
| 976 | } |
---|
| 977 | } |
---|
[2566] | 978 | |
---|
[0] | 979 | if ($this->auth->userID() && !$this->auth->isSuperAdmin()) |
---|
| 980 | { |
---|
| 981 | $join = 'INNER JOIN '.$this->prefix.'permissions PE ON B.blog_id = PE.blog_id '; |
---|
| 982 | $where = |
---|
| 983 | "AND PE.user_id = '".$this->con->escape($this->auth->userID())."' ". |
---|
| 984 | "AND (permissions LIKE '%|usage|%' OR permissions LIKE '%|admin|%' OR permissions LIKE '%|contentadmin|%') ". |
---|
| 985 | "AND blog_status IN (1,0) "; |
---|
| 986 | } elseif (!$this->auth->userID()) { |
---|
| 987 | $where = 'AND blog_status IN (1,0) '; |
---|
| 988 | } |
---|
[2566] | 989 | |
---|
[0] | 990 | if (!empty($params['blog_id'])) { |
---|
| 991 | $where .= "AND B.blog_id = '".$this->con->escape($params['blog_id'])."' "; |
---|
| 992 | } |
---|
[2566] | 993 | |
---|
[0] | 994 | if (!empty($params['q'])) { |
---|
[1593] | 995 | $params['q'] = strtolower(str_replace('*','%',$params['q'])); |
---|
[0] | 996 | $where .= |
---|
| 997 | 'AND ('. |
---|
| 998 | "LOWER(B.blog_id) LIKE '".$this->con->escape($params['q'])."' ". |
---|
| 999 | "OR LOWER(B.blog_name) LIKE '".$this->con->escape($params['q'])."' ". |
---|
| 1000 | "OR LOWER(B.blog_url) LIKE '".$this->con->escape($params['q'])."' ". |
---|
| 1001 | ') '; |
---|
| 1002 | } |
---|
[2566] | 1003 | |
---|
[0] | 1004 | $strReq = sprintf($strReq,$join,$where); |
---|
| 1005 | return $this->con->select($strReq); |
---|
| 1006 | } |
---|
[2566] | 1007 | |
---|
[0] | 1008 | /** |
---|
| 1009 | Creates a new blog. |
---|
[2566] | 1010 | |
---|
[0] | 1011 | @param cur <b>cursor</b> Blog cursor |
---|
| 1012 | */ |
---|
| 1013 | public function addBlog($cur) |
---|
| 1014 | { |
---|
| 1015 | if (!$this->auth->isSuperAdmin()) { |
---|
| 1016 | throw new Exception(__('You are not an administrator')); |
---|
| 1017 | } |
---|
[2566] | 1018 | |
---|
[0] | 1019 | $this->getBlogCursor($cur); |
---|
[2566] | 1020 | |
---|
[0] | 1021 | $cur->blog_creadt = date('Y-m-d H:i:s'); |
---|
| 1022 | $cur->blog_upddt = date('Y-m-d H:i:s'); |
---|
| 1023 | $cur->blog_uid = md5(uniqid()); |
---|
[2566] | 1024 | |
---|
[0] | 1025 | $cur->insert(); |
---|
| 1026 | } |
---|
[2566] | 1027 | |
---|
[0] | 1028 | /** |
---|
| 1029 | Updates a given blog. |
---|
[2566] | 1030 | |
---|
[0] | 1031 | @param id <b>string</b> Blog ID |
---|
| 1032 | @param cur <b>cursor</b> Blog cursor |
---|
| 1033 | */ |
---|
| 1034 | public function updBlog($id,$cur) |
---|
| 1035 | { |
---|
| 1036 | $this->getBlogCursor($cur); |
---|
[2566] | 1037 | |
---|
[0] | 1038 | $cur->blog_upddt = date('Y-m-d H:i:s'); |
---|
[2566] | 1039 | |
---|
[0] | 1040 | $cur->update("WHERE blog_id = '".$this->con->escape($id)."'"); |
---|
| 1041 | } |
---|
[2566] | 1042 | |
---|
[0] | 1043 | private function getBlogCursor($cur) |
---|
| 1044 | { |
---|
[2596] | 1045 | if (($cur->blog_id !== null |
---|
| 1046 | && !preg_match('/^[A-Za-z0-9._-]{2,}$/',$cur->blog_id)) || |
---|
| 1047 | (!$cur->blog_id)) { |
---|
[2566] | 1048 | throw new Exception(__('Blog ID must contain at least 2 characters using letters, numbers or symbols.')); |
---|
[0] | 1049 | } |
---|
[2566] | 1050 | |
---|
[2596] | 1051 | if (($cur->blog_name !== null && $cur->blog_name == '') || |
---|
| 1052 | (!$cur->blog_name)) { |
---|
[0] | 1053 | throw new Exception(__('No blog name')); |
---|
| 1054 | } |
---|
[2566] | 1055 | |
---|
[2596] | 1056 | if (($cur->blog_url !== null && $cur->blog_url == '') || |
---|
| 1057 | (!$cur->blog_url)) { |
---|
[0] | 1058 | throw new Exception(__('No blog URL')); |
---|
| 1059 | } |
---|
[2566] | 1060 | |
---|
[0] | 1061 | if ($cur->blog_desc !== null) { |
---|
| 1062 | $cur->blog_desc = html::clean($cur->blog_desc); |
---|
| 1063 | } |
---|
| 1064 | } |
---|
[2566] | 1065 | |
---|
[0] | 1066 | /** |
---|
| 1067 | Removes a given blog. |
---|
| 1068 | @warning This will remove everything related to the blog (posts, |
---|
| 1069 | categories, comments, links...) |
---|
[2566] | 1070 | |
---|
[0] | 1071 | @param id <b>string</b> Blog ID |
---|
| 1072 | */ |
---|
| 1073 | public function delBlog($id) |
---|
| 1074 | { |
---|
| 1075 | if (!$this->auth->isSuperAdmin()) { |
---|
| 1076 | throw new Exception(__('You are not an administrator')); |
---|
| 1077 | } |
---|
[2566] | 1078 | |
---|
[0] | 1079 | $strReq = 'DELETE FROM '.$this->prefix.'blog '. |
---|
| 1080 | "WHERE blog_id = '".$this->con->escape($id)."' "; |
---|
[2566] | 1081 | |
---|
[0] | 1082 | $this->con->execute($strReq); |
---|
| 1083 | } |
---|
[2566] | 1084 | |
---|
[0] | 1085 | /** |
---|
| 1086 | Checks if a blog exist. |
---|
[2566] | 1087 | |
---|
[0] | 1088 | @param id <b>string</b> Blog ID |
---|
| 1089 | @return <b>boolean</b> |
---|
| 1090 | */ |
---|
| 1091 | public function blogExists($id) |
---|
| 1092 | { |
---|
| 1093 | $strReq = 'SELECT blog_id '. |
---|
| 1094 | 'FROM '.$this->prefix.'blog '. |
---|
| 1095 | "WHERE blog_id = '".$this->con->escape($id)."' "; |
---|
[2566] | 1096 | |
---|
[0] | 1097 | $rs = $this->con->select($strReq); |
---|
[2566] | 1098 | |
---|
[0] | 1099 | return !$rs->isEmpty(); |
---|
| 1100 | } |
---|
[2566] | 1101 | |
---|
[0] | 1102 | /** |
---|
| 1103 | Count posts on a blog |
---|
[2566] | 1104 | |
---|
[0] | 1105 | @param id <b>string</b> Blog ID |
---|
| 1106 | @param type <b>string</b> Post type |
---|
| 1107 | @return <b>boolean</b> |
---|
| 1108 | */ |
---|
| 1109 | public function countBlogPosts($id,$type=null) |
---|
| 1110 | { |
---|
| 1111 | $strReq = 'SELECT COUNT(post_id) '. |
---|
| 1112 | 'FROM '.$this->prefix.'post '. |
---|
| 1113 | "WHERE blog_id = '".$this->con->escape($id)."' "; |
---|
[2566] | 1114 | |
---|
[0] | 1115 | if ($type) { |
---|
| 1116 | $strReq .= "AND post_type = '".$this->con->escape($type)."' "; |
---|
| 1117 | } |
---|
[2566] | 1118 | |
---|
[0] | 1119 | return $this->con->select($strReq)->f(0); |
---|
| 1120 | } |
---|
| 1121 | //@} |
---|
[2566] | 1122 | |
---|
[0] | 1123 | /// @name HTML Filter methods |
---|
| 1124 | //@{ |
---|
| 1125 | /** |
---|
| 1126 | Calls HTML filter to drop bad tags and produce valid XHTML output (if |
---|
| 1127 | tidy extension is present). If <b>enable_html_filter</b> blog setting is |
---|
| 1128 | false, returns not filtered string. |
---|
[2566] | 1129 | |
---|
[0] | 1130 | @param str <b>string</b> String to filter |
---|
| 1131 | @return <b>string</b> Filtered string. |
---|
| 1132 | */ |
---|
| 1133 | public function HTMLfilter($str) |
---|
| 1134 | { |
---|
| 1135 | if ($this->blog instanceof dcBlog && !$this->blog->settings->system->enable_html_filter) { |
---|
| 1136 | return $str; |
---|
| 1137 | } |
---|
[2566] | 1138 | |
---|
[0] | 1139 | $filter = new htmlFilter; |
---|
| 1140 | $str = trim($filter->apply($str)); |
---|
| 1141 | return $str; |
---|
| 1142 | } |
---|
| 1143 | //@} |
---|
[2566] | 1144 | |
---|
[0] | 1145 | /// @name wiki2xhtml methods |
---|
| 1146 | //@{ |
---|
| 1147 | private function initWiki() |
---|
| 1148 | { |
---|
| 1149 | $this->wiki2xhtml = new wiki2xhtml; |
---|
| 1150 | } |
---|
[2566] | 1151 | |
---|
[0] | 1152 | /** |
---|
| 1153 | Returns a transformed string with wiki2xhtml. |
---|
[2566] | 1154 | |
---|
[0] | 1155 | @param str <b>string</b> String to transform |
---|
| 1156 | @return <b>string</b> Transformed string |
---|
| 1157 | */ |
---|
| 1158 | public function wikiTransform($str) |
---|
| 1159 | { |
---|
| 1160 | if (!($this->wiki2xhtml instanceof wiki2xhtml)) { |
---|
| 1161 | $this->initWiki(); |
---|
| 1162 | } |
---|
| 1163 | return $this->wiki2xhtml->transform($str); |
---|
| 1164 | } |
---|
[2566] | 1165 | |
---|
[0] | 1166 | /** |
---|
| 1167 | Inits <var>wiki2xhtml</var> property for blog post. |
---|
| 1168 | */ |
---|
| 1169 | public function initWikiPost() |
---|
| 1170 | { |
---|
| 1171 | $this->initWiki(); |
---|
[2566] | 1172 | |
---|
[0] | 1173 | $this->wiki2xhtml->setOpts(array( |
---|
| 1174 | 'active_title' => 1, |
---|
| 1175 | 'active_setext_title' => 0, |
---|
| 1176 | 'active_hr' => 1, |
---|
| 1177 | 'active_lists' => 1, |
---|
| 1178 | 'active_quote' => 1, |
---|
| 1179 | 'active_pre' => 1, |
---|
| 1180 | 'active_empty' => 1, |
---|
| 1181 | 'active_auto_br' => 0, |
---|
| 1182 | 'active_auto_urls' => 0, |
---|
| 1183 | 'active_urls' => 1, |
---|
| 1184 | 'active_auto_img' => 0, |
---|
| 1185 | 'active_img' => 1, |
---|
| 1186 | 'active_anchor' => 1, |
---|
| 1187 | 'active_em' => 1, |
---|
| 1188 | 'active_strong' => 1, |
---|
| 1189 | 'active_br' => 1, |
---|
| 1190 | 'active_q' => 1, |
---|
| 1191 | 'active_code' => 1, |
---|
| 1192 | 'active_acronym' => 1, |
---|
| 1193 | 'active_ins' => 1, |
---|
| 1194 | 'active_del' => 1, |
---|
| 1195 | 'active_footnotes' => 1, |
---|
| 1196 | 'active_wikiwords' => 0, |
---|
| 1197 | 'active_macros' => 1, |
---|
| 1198 | 'parse_pre' => 1, |
---|
| 1199 | 'active_fr_syntax' => 0, |
---|
| 1200 | 'first_title_level' => 3, |
---|
| 1201 | 'note_prefix' => 'wiki-footnote', |
---|
| 1202 | 'note_str' => '<div class="footnotes"><h4>Notes</h4>%s</div>' |
---|
| 1203 | )); |
---|
[2566] | 1204 | |
---|
[0] | 1205 | $this->wiki2xhtml->registerFunction('url:post',array($this,'wikiPostLink')); |
---|
[2566] | 1206 | |
---|
[0] | 1207 | # --BEHAVIOR-- coreWikiPostInit |
---|
| 1208 | $this->callBehavior('coreInitWikiPost',$this->wiki2xhtml); |
---|
| 1209 | } |
---|
[2566] | 1210 | |
---|
[0] | 1211 | /** |
---|
| 1212 | Inits <var>wiki2xhtml</var> property for simple blog comment (basic syntax). |
---|
| 1213 | */ |
---|
| 1214 | public function initWikiSimpleComment() |
---|
| 1215 | { |
---|
| 1216 | $this->initWiki(); |
---|
[2566] | 1217 | |
---|
[0] | 1218 | $this->wiki2xhtml->setOpts(array( |
---|
| 1219 | 'active_title' => 0, |
---|
| 1220 | 'active_setext_title' => 0, |
---|
| 1221 | 'active_hr' => 0, |
---|
| 1222 | 'active_lists' => 0, |
---|
| 1223 | 'active_quote' => 0, |
---|
| 1224 | 'active_pre' => 0, |
---|
| 1225 | 'active_empty' => 0, |
---|
| 1226 | 'active_auto_br' => 1, |
---|
| 1227 | 'active_auto_urls' => 1, |
---|
| 1228 | 'active_urls' => 0, |
---|
| 1229 | 'active_auto_img' => 0, |
---|
| 1230 | 'active_img' => 0, |
---|
| 1231 | 'active_anchor' => 0, |
---|
| 1232 | 'active_em' => 0, |
---|
| 1233 | 'active_strong' => 0, |
---|
| 1234 | 'active_br' => 0, |
---|
| 1235 | 'active_q' => 0, |
---|
| 1236 | 'active_code' => 0, |
---|
| 1237 | 'active_acronym' => 0, |
---|
| 1238 | 'active_ins' => 0, |
---|
| 1239 | 'active_del' => 0, |
---|
| 1240 | 'active_footnotes' => 0, |
---|
| 1241 | 'active_wikiwords' => 0, |
---|
| 1242 | 'active_macros' => 0, |
---|
| 1243 | 'parse_pre' => 0, |
---|
| 1244 | 'active_fr_syntax' => 0 |
---|
| 1245 | )); |
---|
[2566] | 1246 | |
---|
[0] | 1247 | # --BEHAVIOR-- coreInitWikiSimpleComment |
---|
| 1248 | $this->callBehavior('coreInitWikiSimpleComment',$this->wiki2xhtml); |
---|
| 1249 | } |
---|
[2566] | 1250 | |
---|
[0] | 1251 | /** |
---|
| 1252 | Inits <var>wiki2xhtml</var> property for blog comment. |
---|
| 1253 | */ |
---|
| 1254 | public function initWikiComment() |
---|
| 1255 | { |
---|
| 1256 | $this->initWiki(); |
---|
[2566] | 1257 | |
---|
[0] | 1258 | $this->wiki2xhtml->setOpts(array( |
---|
| 1259 | 'active_title' => 0, |
---|
| 1260 | 'active_setext_title' => 0, |
---|
| 1261 | 'active_hr' => 0, |
---|
| 1262 | 'active_lists' => 1, |
---|
| 1263 | 'active_quote' => 0, |
---|
| 1264 | 'active_pre' => 1, |
---|
| 1265 | 'active_empty' => 0, |
---|
| 1266 | 'active_auto_br' => 1, |
---|
| 1267 | 'active_auto_urls' => 1, |
---|
| 1268 | 'active_urls' => 1, |
---|
| 1269 | 'active_auto_img' => 0, |
---|
| 1270 | 'active_img' => 0, |
---|
| 1271 | 'active_anchor' => 0, |
---|
| 1272 | 'active_em' => 1, |
---|
| 1273 | 'active_strong' => 1, |
---|
| 1274 | 'active_br' => 1, |
---|
| 1275 | 'active_q' => 1, |
---|
| 1276 | 'active_code' => 1, |
---|
| 1277 | 'active_acronym' => 1, |
---|
| 1278 | 'active_ins' => 1, |
---|
| 1279 | 'active_del' => 1, |
---|
| 1280 | 'active_footnotes' => 0, |
---|
| 1281 | 'active_wikiwords' => 0, |
---|
| 1282 | 'active_macros' => 0, |
---|
| 1283 | 'parse_pre' => 0, |
---|
| 1284 | 'active_fr_syntax' => 0 |
---|
| 1285 | )); |
---|
[2566] | 1286 | |
---|
[0] | 1287 | # --BEHAVIOR-- coreInitWikiComment |
---|
| 1288 | $this->callBehavior('coreInitWikiComment',$this->wiki2xhtml); |
---|
| 1289 | } |
---|
[2566] | 1290 | |
---|
[0] | 1291 | public function wikiPostLink($url,$content) |
---|
| 1292 | { |
---|
[2566] | 1293 | if (!($this->blog instanceof dcBlog)) { |
---|
[0] | 1294 | return array(); |
---|
| 1295 | } |
---|
[2566] | 1296 | |
---|
[0] | 1297 | $post_id = abs((integer) substr($url,5)); |
---|
| 1298 | if (!$post_id) { |
---|
| 1299 | return array(); |
---|
| 1300 | } |
---|
[2566] | 1301 | |
---|
[0] | 1302 | $post = $this->blog->getPosts(array('post_id'=>$post_id)); |
---|
| 1303 | if ($post->isEmpty()) { |
---|
| 1304 | return array(); |
---|
| 1305 | } |
---|
[2566] | 1306 | |
---|
[0] | 1307 | $res = array('url' => $post->getURL()); |
---|
| 1308 | $post_title = $post->post_title; |
---|
[2566] | 1309 | |
---|
[0] | 1310 | if ($content != $url) { |
---|
| 1311 | $res['title'] = html::escapeHTML($post->post_title); |
---|
| 1312 | } |
---|
[2566] | 1313 | |
---|
[0] | 1314 | if ($content == '' || $content == $url) { |
---|
| 1315 | $res['content'] = html::escapeHTML($post->post_title); |
---|
| 1316 | } |
---|
[2566] | 1317 | |
---|
[0] | 1318 | if ($post->post_lang) { |
---|
| 1319 | $res['lang'] = $post->post_lang; |
---|
| 1320 | } |
---|
[2566] | 1321 | |
---|
[0] | 1322 | return $res; |
---|
| 1323 | } |
---|
| 1324 | //@} |
---|
[2566] | 1325 | |
---|
[0] | 1326 | /// @name Maintenance methods |
---|
| 1327 | //@{ |
---|
| 1328 | /** |
---|
| 1329 | Creates default settings for active blog. Optionnal parameter |
---|
| 1330 | <var>defaults</var> replaces default params while needed. |
---|
[2566] | 1331 | |
---|
[0] | 1332 | @param defaults <b>array</b> Default parameters |
---|
| 1333 | */ |
---|
| 1334 | public function blogDefaults($defaults=null) |
---|
| 1335 | { |
---|
| 1336 | if (!is_array($defaults)) |
---|
| 1337 | { |
---|
| 1338 | $defaults = array( |
---|
| 1339 | array('allow_comments','boolean',true, |
---|
| 1340 | 'Allow comments on blog'), |
---|
| 1341 | array('allow_trackbacks','boolean',true, |
---|
| 1342 | 'Allow trackbacks on blog'), |
---|
| 1343 | array('blog_timezone','string','Europe/London', |
---|
| 1344 | 'Blog timezone'), |
---|
| 1345 | array('comments_nofollow','boolean',true, |
---|
| 1346 | 'Add rel="nofollow" to comments URLs'), |
---|
| 1347 | array('comments_pub','boolean',true, |
---|
| 1348 | 'Publish comments immediately'), |
---|
| 1349 | array('comments_ttl','integer',0, |
---|
| 1350 | 'Number of days to keep comments open (0 means no ttl)'), |
---|
| 1351 | array('copyright_notice','string','','Copyright notice (simple text)'), |
---|
| 1352 | array('date_format','string','%A, %B %e %Y', |
---|
| 1353 | 'Date format. See PHP strftime function for patterns'), |
---|
| 1354 | array('editor','string','', |
---|
| 1355 | 'Person responsible of the content'), |
---|
| 1356 | array('enable_html_filter','boolean',0, |
---|
| 1357 | 'Enable HTML filter'), |
---|
| 1358 | array('enable_xmlrpc','boolean',0, |
---|
| 1359 | 'Enable XML/RPC interface'), |
---|
| 1360 | array('lang','string','en', |
---|
| 1361 | 'Default blog language'), |
---|
[706] | 1362 | array('media_exclusion','string','/\.php$/i', |
---|
[0] | 1363 | 'File name exclusion pattern in media manager. (PCRE value)'), |
---|
| 1364 | array('media_img_m_size','integer',448, |
---|
| 1365 | 'Image medium size in media manager'), |
---|
| 1366 | array('media_img_s_size','integer',240, |
---|
| 1367 | 'Image small size in media manager'), |
---|
| 1368 | array('media_img_t_size','integer',100, |
---|
| 1369 | 'Image thumbnail size in media manager'), |
---|
| 1370 | array('media_img_title_pattern','string','Title ;; Date(%b %Y) ;; separator(, )', |
---|
| 1371 | 'Pattern to set image title when you insert it in a post'), |
---|
[2577] | 1372 | array('nb_post_for_home','integer',20, |
---|
| 1373 | 'Number of entries on first home page'), |
---|
[0] | 1374 | array('nb_post_per_page','integer',20, |
---|
[2577] | 1375 | 'Number of entries on home pages and category pages'), |
---|
[0] | 1376 | array('nb_post_per_feed','integer',20, |
---|
| 1377 | 'Number of entries on feeds'), |
---|
| 1378 | array('nb_comment_per_feed','integer',20, |
---|
| 1379 | 'Number of comments on feeds'), |
---|
| 1380 | array('post_url_format','string','{y}/{m}/{d}/{t}', |
---|
| 1381 | 'Post URL format. {y}: year, {m}: month, {d}: day, {id}: post id, {t}: entry title'), |
---|
| 1382 | array('public_path','string','public', |
---|
| 1383 | 'Path to public directory, begins with a / for a full system path'), |
---|
| 1384 | array('public_url','string','/public', |
---|
| 1385 | 'URL to public directory'), |
---|
| 1386 | array('robots_policy','string','INDEX,FOLLOW', |
---|
| 1387 | 'Search engines robots policy'), |
---|
| 1388 | array('short_feed_items','boolean',false, |
---|
| 1389 | 'Display short feed items'), |
---|
[2641] | 1390 | array('theme','string','berlin', |
---|
[0] | 1391 | 'Blog theme'), |
---|
| 1392 | array('themes_path','string','themes', |
---|
| 1393 | 'Themes root path'), |
---|
| 1394 | array('themes_url','string','/themes', |
---|
| 1395 | 'Themes root URL'), |
---|
| 1396 | array('time_format','string','%H:%M', |
---|
| 1397 | 'Time format. See PHP strftime function for patterns'), |
---|
| 1398 | array('tpl_allow_php','boolean',false, |
---|
| 1399 | 'Allow PHP code in templates'), |
---|
| 1400 | array('tpl_use_cache','boolean',true, |
---|
| 1401 | 'Use template caching'), |
---|
| 1402 | array('trackbacks_pub','boolean',true, |
---|
| 1403 | 'Publish trackbacks immediately'), |
---|
| 1404 | array('trackbacks_ttl','integer',0, |
---|
| 1405 | 'Number of days to keep trackbacks open (0 means no ttl)'), |
---|
| 1406 | array('url_scan','string','query_string', |
---|
| 1407 | 'URL handle mode (path_info or query_string)'), |
---|
| 1408 | array('use_smilies','boolean',false, |
---|
| 1409 | 'Show smilies on entries and comments'), |
---|
[2198] | 1410 | array('inc_subcats','boolean',false, |
---|
| 1411 | 'Include sub-categories in category page and category posts feed'), |
---|
[0] | 1412 | array('wiki_comments','boolean',false, |
---|
| 1413 | 'Allow commenters to use a subset of wiki syntax') |
---|
| 1414 | ); |
---|
| 1415 | } |
---|
[2566] | 1416 | |
---|
[0] | 1417 | $settings = new dcSettings($this,null); |
---|
| 1418 | $settings->addNamespace('system'); |
---|
[2566] | 1419 | |
---|
[0] | 1420 | foreach ($defaults as $v) { |
---|
| 1421 | $settings->system->put($v[0],$v[2],$v[1],$v[3],false,true); |
---|
| 1422 | } |
---|
| 1423 | } |
---|
[2566] | 1424 | |
---|
[0] | 1425 | /** |
---|
| 1426 | Recreates entries search engine index. |
---|
[2566] | 1427 | |
---|
[0] | 1428 | @param start <b>integer</b> Start entry index |
---|
| 1429 | @param limit <b>integer</b> Number of entry to index |
---|
[2566] | 1430 | |
---|
[0] | 1431 | @return <b>integer</b> <var>$start</var> and <var>$limit</var> sum |
---|
| 1432 | */ |
---|
| 1433 | public function indexAllPosts($start=null,$limit=null) |
---|
| 1434 | { |
---|
| 1435 | $strReq = 'SELECT COUNT(post_id) '. |
---|
| 1436 | 'FROM '.$this->prefix.'post'; |
---|
| 1437 | $rs = $this->con->select($strReq); |
---|
| 1438 | $count = $rs->f(0); |
---|
[2566] | 1439 | |
---|
[0] | 1440 | $strReq = 'SELECT post_id, post_title, post_excerpt_xhtml, post_content_xhtml '. |
---|
| 1441 | 'FROM '.$this->prefix.'post '; |
---|
[2566] | 1442 | |
---|
[0] | 1443 | if ($start !== null && $limit !== null) { |
---|
| 1444 | $strReq .= $this->con->limit($start,$limit); |
---|
| 1445 | } |
---|
[2566] | 1446 | |
---|
[0] | 1447 | $rs = $this->con->select($strReq,true); |
---|
[2566] | 1448 | |
---|
[0] | 1449 | $cur = $this->con->openCursor($this->prefix.'post'); |
---|
[2566] | 1450 | |
---|
[0] | 1451 | while ($rs->fetch()) |
---|
| 1452 | { |
---|
| 1453 | $words = $rs->post_title.' '. $rs->post_excerpt_xhtml.' '. |
---|
| 1454 | $rs->post_content_xhtml; |
---|
[2566] | 1455 | |
---|
[0] | 1456 | $cur->post_words = implode(' ',text::splitWords($words)); |
---|
| 1457 | $cur->update('WHERE post_id = '.(integer) $rs->post_id); |
---|
| 1458 | $cur->clean(); |
---|
| 1459 | } |
---|
[2566] | 1460 | |
---|
[0] | 1461 | if ($start+$limit > $count) { |
---|
| 1462 | return null; |
---|
| 1463 | } |
---|
| 1464 | return $start+$limit; |
---|
| 1465 | } |
---|
[2566] | 1466 | |
---|
[0] | 1467 | /** |
---|
| 1468 | Recreates comments search engine index. |
---|
[2566] | 1469 | |
---|
[0] | 1470 | @param start <b>integer</b> Start comment index |
---|
| 1471 | @param limit <b>integer</b> Number of comments to index |
---|
[2566] | 1472 | |
---|
[0] | 1473 | @return <b>integer</b> <var>$start</var> and <var>$limit</var> sum |
---|
| 1474 | */ |
---|
| 1475 | public function indexAllComments($start=null,$limit=null) |
---|
| 1476 | { |
---|
| 1477 | $strReq = 'SELECT COUNT(comment_id) '. |
---|
| 1478 | 'FROM '.$this->prefix.'comment'; |
---|
| 1479 | $rs = $this->con->select($strReq); |
---|
| 1480 | $count = $rs->f(0); |
---|
[2566] | 1481 | |
---|
[0] | 1482 | $strReq = 'SELECT comment_id, comment_content '. |
---|
| 1483 | 'FROM '.$this->prefix.'comment '; |
---|
[2566] | 1484 | |
---|
[0] | 1485 | if ($start !== null && $limit !== null) { |
---|
| 1486 | $strReq .= $this->con->limit($start,$limit); |
---|
| 1487 | } |
---|
[2566] | 1488 | |
---|
[0] | 1489 | $rs = $this->con->select($strReq); |
---|
[2566] | 1490 | |
---|
[0] | 1491 | $cur = $this->con->openCursor($this->prefix.'comment'); |
---|
[2566] | 1492 | |
---|
[0] | 1493 | while ($rs->fetch()) |
---|
| 1494 | { |
---|
| 1495 | $cur->comment_words = implode(' ',text::splitWords($rs->comment_content)); |
---|
| 1496 | $cur->update('WHERE comment_id = '.(integer) $rs->comment_id); |
---|
| 1497 | $cur->clean(); |
---|
| 1498 | } |
---|
[2566] | 1499 | |
---|
[0] | 1500 | if ($start+$limit > $count) { |
---|
| 1501 | return null; |
---|
| 1502 | } |
---|
| 1503 | return $start+$limit; |
---|
| 1504 | } |
---|
[2566] | 1505 | |
---|
[0] | 1506 | /** |
---|
| 1507 | Reinits nb_comment and nb_trackback in post table. |
---|
| 1508 | */ |
---|
| 1509 | public function countAllComments() |
---|
| 1510 | { |
---|
[2566] | 1511 | |
---|
[136] | 1512 | $updCommentReq = 'UPDATE '.$this->prefix.'post P '. |
---|
| 1513 | 'SET nb_comment = ('. |
---|
| 1514 | 'SELECT COUNT(C.comment_id) from '.$this->prefix.'comment C '. |
---|
| 1515 | 'WHERE C.post_id = P.post_id AND C.comment_trackback <> 1 '. |
---|
| 1516 | 'AND C.comment_status = 1 '. |
---|
| 1517 | ')'; |
---|
| 1518 | $updTrackbackReq = 'UPDATE '.$this->prefix.'post P '. |
---|
| 1519 | 'SET nb_trackback = ('. |
---|
| 1520 | 'SELECT COUNT(C.comment_id) from '.$this->prefix.'comment C '. |
---|
| 1521 | 'WHERE C.post_id = P.post_id AND C.comment_trackback = 1 '. |
---|
| 1522 | 'AND C.comment_status = 1 '. |
---|
| 1523 | ')'; |
---|
| 1524 | $this->con->execute($updCommentReq); |
---|
| 1525 | $this->con->execute($updTrackbackReq); |
---|
[0] | 1526 | } |
---|
[2566] | 1527 | |
---|
[0] | 1528 | /** |
---|
| 1529 | Empty templates cache directory |
---|
| 1530 | */ |
---|
| 1531 | public function emptyTemplatesCache() |
---|
| 1532 | { |
---|
| 1533 | if (is_dir(DC_TPL_CACHE.'/cbtpl')) { |
---|
| 1534 | files::deltree(DC_TPL_CACHE.'/cbtpl'); |
---|
| 1535 | } |
---|
| 1536 | } |
---|
[2595] | 1537 | |
---|
| 1538 | /** |
---|
| 1539 | Return elapsed time since script has been started |
---|
[2679] | 1540 | @param $mtime <b>float</b> timestamp (microtime format) to evaluate delta from |
---|
| 1541 | current time is taken if null |
---|
| 1542 | @return <b>float</b> elapsed time |
---|
[2595] | 1543 | */ |
---|
| 1544 | public function getElapsedTime ($mtime=null) { |
---|
| 1545 | if ($mtime !== null) { |
---|
| 1546 | return $mtime-$this->stime; |
---|
| 1547 | } else { |
---|
| 1548 | return microtime(true)-$this->stime; |
---|
| 1549 | } |
---|
| 1550 | } |
---|
[0] | 1551 | //@} |
---|
[2595] | 1552 | |
---|
| 1553 | |
---|
| 1554 | |
---|
[0] | 1555 | } |
---|