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