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