@@ -44,11 +44,11 @@ discard block |
||
44 | 44 | || ($isTimestampShown === true && Configure::read('debug') > 0) |
45 | 45 | ) { |
46 | 46 | $out .= $this->Html->scriptBlock( |
47 | - "var require = {urlArgs:" . |
|
47 | + "var require = {urlArgs:". |
|
48 | 48 | $this->Js->value( |
49 | - $this->Html->getAssetTimestamp($options['jsUrl'] . $dataMain . '.js' |
|
49 | + $this->Html->getAssetTimestamp($options['jsUrl'].$dataMain.'.js' |
|
50 | 50 | ) |
51 | - ) . " }" |
|
51 | + )." }" |
|
52 | 52 | ); |
53 | 53 | } |
54 | 54 | // require.js borks out when used with Cakes timestamp. |
@@ -78,7 +78,7 @@ discard block |
||
78 | 78 | if ($debug) { |
79 | 79 | return JS_URL; |
80 | 80 | } else { |
81 | - return JS_URL . '/../dist/'; |
|
81 | + return JS_URL.'/../dist/'; |
|
82 | 82 | } |
83 | 83 | } |
84 | 84 |
@@ -1,23 +1,23 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | - App::uses('AppHelper', 'View/Helper'); |
|
3 | + App::uses('AppHelper', 'View/Helper'); |
|
4 | 4 | |
5 | - class RequireJsHelper extends AppHelper { |
|
5 | + class RequireJsHelper extends AppHelper { |
|
6 | 6 | |
7 | - public $helpers = array( |
|
8 | - 'Html', |
|
9 | - 'Js' |
|
10 | - ); |
|
7 | + public $helpers = array( |
|
8 | + 'Html', |
|
9 | + 'Js' |
|
10 | + ); |
|
11 | 11 | |
12 | 12 | /** |
13 | 13 | * url to require.js relative app/webroot/js |
14 | 14 | * |
15 | 15 | * @var array |
16 | 16 | */ |
17 | - protected $_requireUrl = [ |
|
18 | - 'debug' => 'dist/require', |
|
19 | - 'prod' => 'dist/require.min' |
|
20 | - ]; |
|
17 | + protected $_requireUrl = [ |
|
18 | + 'debug' => 'dist/require', |
|
19 | + 'prod' => 'dist/require.min' |
|
20 | + ]; |
|
21 | 21 | |
22 | 22 | /** |
23 | 23 | * Inserts <script> tag for including require.js |
@@ -31,64 +31,64 @@ discard block |
||
31 | 31 | * @param array $options additional options |
32 | 32 | * @return string |
33 | 33 | */ |
34 | - public function scriptTag($dataMain, $options = array()) { |
|
35 | - // require.js should already be included in production js |
|
36 | - $options += array( |
|
37 | - 'jsUrl' => $this->_jsRoot(), |
|
38 | - 'requireUrl' => $this->_requireUrl() |
|
39 | - ); |
|
40 | - $out = ''; |
|
41 | - // add version as timestamp to require requests |
|
42 | - $isTimestampShown = Configure::read('Asset.timestamp'); |
|
43 | - if ($isTimestampShown === 'force' |
|
44 | - || ($isTimestampShown === true && Configure::read('debug') > 0) |
|
45 | - ) { |
|
46 | - $out .= $this->Html->scriptBlock( |
|
47 | - "var require = {urlArgs:" . |
|
48 | - $this->Js->value( |
|
49 | - $this->Html->getAssetTimestamp($options['jsUrl'] . $dataMain . '.js' |
|
50 | - ) |
|
51 | - ) . " }" |
|
52 | - ); |
|
53 | - } |
|
54 | - // require.js borks out when used with Cakes timestamp. |
|
55 | - // also we need the relative path for the main-script |
|
56 | - $_tmpAssetTimestampCache = Configure::read('Asset.timestamp'); |
|
57 | - Configure::write('Asset.timestamp', false); |
|
58 | - $out .= $this->Html->script( |
|
59 | - $this->Html->assetUrl( |
|
60 | - $options['requireUrl'], |
|
61 | - ['ext' => '.js', 'fullBase' => true] |
|
62 | - ), |
|
63 | - [ |
|
64 | - 'data-main' => $this->Html->assetUrl($dataMain, |
|
65 | - [ |
|
66 | - 'pathPrefix' => $options['jsUrl'], |
|
67 | - 'ext' => '.js' |
|
68 | - ] |
|
69 | - ) |
|
70 | - ] |
|
71 | - ); |
|
72 | - Configure::write('Asset.timestamp', $_tmpAssetTimestampCache); |
|
73 | - return $out; |
|
74 | - } |
|
34 | + public function scriptTag($dataMain, $options = array()) { |
|
35 | + // require.js should already be included in production js |
|
36 | + $options += array( |
|
37 | + 'jsUrl' => $this->_jsRoot(), |
|
38 | + 'requireUrl' => $this->_requireUrl() |
|
39 | + ); |
|
40 | + $out = ''; |
|
41 | + // add version as timestamp to require requests |
|
42 | + $isTimestampShown = Configure::read('Asset.timestamp'); |
|
43 | + if ($isTimestampShown === 'force' |
|
44 | + || ($isTimestampShown === true && Configure::read('debug') > 0) |
|
45 | + ) { |
|
46 | + $out .= $this->Html->scriptBlock( |
|
47 | + "var require = {urlArgs:" . |
|
48 | + $this->Js->value( |
|
49 | + $this->Html->getAssetTimestamp($options['jsUrl'] . $dataMain . '.js' |
|
50 | + ) |
|
51 | + ) . " }" |
|
52 | + ); |
|
53 | + } |
|
54 | + // require.js borks out when used with Cakes timestamp. |
|
55 | + // also we need the relative path for the main-script |
|
56 | + $_tmpAssetTimestampCache = Configure::read('Asset.timestamp'); |
|
57 | + Configure::write('Asset.timestamp', false); |
|
58 | + $out .= $this->Html->script( |
|
59 | + $this->Html->assetUrl( |
|
60 | + $options['requireUrl'], |
|
61 | + ['ext' => '.js', 'fullBase' => true] |
|
62 | + ), |
|
63 | + [ |
|
64 | + 'data-main' => $this->Html->assetUrl($dataMain, |
|
65 | + [ |
|
66 | + 'pathPrefix' => $options['jsUrl'], |
|
67 | + 'ext' => '.js' |
|
68 | + ] |
|
69 | + ) |
|
70 | + ] |
|
71 | + ); |
|
72 | + Configure::write('Asset.timestamp', $_tmpAssetTimestampCache); |
|
73 | + return $out; |
|
74 | + } |
|
75 | 75 | |
76 | - protected function _jsRoot() { |
|
77 | - $debug = Configure::read('debug') > 0; |
|
78 | - if ($debug) { |
|
79 | - return JS_URL; |
|
80 | - } else { |
|
81 | - return JS_URL . '/../dist/'; |
|
82 | - } |
|
83 | - } |
|
76 | + protected function _jsRoot() { |
|
77 | + $debug = Configure::read('debug') > 0; |
|
78 | + if ($debug) { |
|
79 | + return JS_URL; |
|
80 | + } else { |
|
81 | + return JS_URL . '/../dist/'; |
|
82 | + } |
|
83 | + } |
|
84 | 84 | |
85 | - protected function _requireUrl() { |
|
86 | - $debug = Configure::read('debug') > 0; |
|
87 | - if ($debug) { |
|
88 | - return $this->_requireUrl['debug']; |
|
89 | - } else { |
|
90 | - return $this->_requireUrl['prod']; |
|
91 | - } |
|
92 | - } |
|
85 | + protected function _requireUrl() { |
|
86 | + $debug = Configure::read('debug') > 0; |
|
87 | + if ($debug) { |
|
88 | + return $this->_requireUrl['debug']; |
|
89 | + } else { |
|
90 | + return $this->_requireUrl['prod']; |
|
91 | + } |
|
92 | + } |
|
93 | 93 | |
94 | - } |
|
94 | + } |
@@ -44,7 +44,7 @@ discard block |
||
44 | 44 | } |
45 | 45 | |
46 | 46 | public static function tagId() { |
47 | - return 'id' . static::$_tagId++; |
|
47 | + return 'id'.static::$_tagId++; |
|
48 | 48 | } |
49 | 49 | |
50 | 50 | /** |
@@ -59,7 +59,7 @@ discard block |
||
59 | 59 | // extracts integer unixtimestamp from `path/asset.ext?<unixtimestamp> |
60 | 60 | if ($pathWithTimestamp) { |
61 | 61 | if (preg_match('/(?<=\?)[\d]+(?=$|\?|\&)/', $pathWithTimestamp, $matches)) { |
62 | - return (int)$matches[0]; |
|
62 | + return (int) $matches[0]; |
|
63 | 63 | } |
64 | 64 | } |
65 | 65 | throw new InvalidArgumentException("File $path not found."); |
@@ -1,26 +1,26 @@ discard block |
||
1 | 1 | <?php |
2 | - /** |
|
3 | - * Application level View Helper |
|
4 | - * |
|
5 | - * This file is application-wide helper file. You can put all |
|
6 | - * application-wide helper-related methods here. |
|
7 | - * |
|
8 | - * PHP 5 |
|
9 | - * |
|
10 | - * CakePHP(tm) : Rapid Development Framework (http://cakephp.org) |
|
11 | - * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org) |
|
12 | - * |
|
13 | - * Licensed under The MIT License |
|
14 | - * Redistributions of files must retain the above copyright notice. |
|
15 | - * |
|
16 | - * @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org) |
|
17 | - * @link http://cakephp.org CakePHP(tm) Project |
|
18 | - * @package app.View.Helper |
|
19 | - * @since CakePHP(tm) v 0.2.9 |
|
20 | - * @license MIT License (http://www.opensource.org/licenses/mit-license.php) |
|
21 | - */ |
|
2 | + /** |
|
3 | + * Application level View Helper |
|
4 | + * |
|
5 | + * This file is application-wide helper file. You can put all |
|
6 | + * application-wide helper-related methods here. |
|
7 | + * |
|
8 | + * PHP 5 |
|
9 | + * |
|
10 | + * CakePHP(tm) : Rapid Development Framework (http://cakephp.org) |
|
11 | + * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org) |
|
12 | + * |
|
13 | + * Licensed under The MIT License |
|
14 | + * Redistributions of files must retain the above copyright notice. |
|
15 | + * |
|
16 | + * @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org) |
|
17 | + * @link http://cakephp.org CakePHP(tm) Project |
|
18 | + * @package app.View.Helper |
|
19 | + * @since CakePHP(tm) v 0.2.9 |
|
20 | + * @license MIT License (http://www.opensource.org/licenses/mit-license.php) |
|
21 | + */ |
|
22 | 22 | |
23 | - App::uses('Helper', 'View'); |
|
23 | + App::uses('Helper', 'View'); |
|
24 | 24 | |
25 | 25 | /** |
26 | 26 | * Application helper |
@@ -30,39 +30,39 @@ discard block |
||
30 | 30 | * |
31 | 31 | * @package app.View.Helper |
32 | 32 | */ |
33 | - class AppHelper extends Helper { |
|
33 | + class AppHelper extends Helper { |
|
34 | 34 | |
35 | - protected static $_tagId = 0; |
|
35 | + protected static $_tagId = 0; |
|
36 | 36 | |
37 | - public function __get($name) { |
|
38 | - switch ($name) { |
|
39 | - case 'dic': |
|
40 | - return ClassRegistry::getObject('dic'); |
|
41 | - default: |
|
42 | - return parent::__get($name); |
|
43 | - } |
|
44 | - } |
|
37 | + public function __get($name) { |
|
38 | + switch ($name) { |
|
39 | + case 'dic': |
|
40 | + return ClassRegistry::getObject('dic'); |
|
41 | + default: |
|
42 | + return parent::__get($name); |
|
43 | + } |
|
44 | + } |
|
45 | 45 | |
46 | - public static function tagId() { |
|
47 | - return 'id' . static::$_tagId++; |
|
48 | - } |
|
46 | + public static function tagId() { |
|
47 | + return 'id' . static::$_tagId++; |
|
48 | + } |
|
49 | 49 | |
50 | - /** |
|
51 | - * Returns the unix timestamp for a file |
|
52 | - * |
|
53 | - * @param $path as url `m/dist/theme.css |
|
54 | - * @return int |
|
55 | - * @throws InvalidArgumentException |
|
56 | - */ |
|
57 | - public function getAssetTimestamp($path) { |
|
58 | - $pathWithTimestamp = $this->assetTimestamp($path); |
|
59 | - // extracts integer unixtimestamp from `path/asset.ext?<unixtimestamp> |
|
60 | - if ($pathWithTimestamp) { |
|
61 | - if (preg_match('/(?<=\?)[\d]+(?=$|\?|\&)/', $pathWithTimestamp, $matches)) { |
|
62 | - return (int)$matches[0]; |
|
63 | - } |
|
64 | - } |
|
65 | - throw new InvalidArgumentException("File $path not found."); |
|
66 | - } |
|
50 | + /** |
|
51 | + * Returns the unix timestamp for a file |
|
52 | + * |
|
53 | + * @param $path as url `m/dist/theme.css |
|
54 | + * @return int |
|
55 | + * @throws InvalidArgumentException |
|
56 | + */ |
|
57 | + public function getAssetTimestamp($path) { |
|
58 | + $pathWithTimestamp = $this->assetTimestamp($path); |
|
59 | + // extracts integer unixtimestamp from `path/asset.ext?<unixtimestamp> |
|
60 | + if ($pathWithTimestamp) { |
|
61 | + if (preg_match('/(?<=\?)[\d]+(?=$|\?|\&)/', $pathWithTimestamp, $matches)) { |
|
62 | + return (int)$matches[0]; |
|
63 | + } |
|
64 | + } |
|
65 | + throw new InvalidArgumentException("File $path not found."); |
|
66 | + } |
|
67 | 67 | |
68 | - } |
|
69 | 68 | \ No newline at end of file |
69 | + } |
|
70 | 70 | \ No newline at end of file |
@@ -21,21 +21,21 @@ |
||
21 | 21 | define('DS', DIRECTORY_SEPARATOR); |
22 | 22 | } |
23 | 23 | |
24 | -$dispatcher = 'Cake' . DS . 'Console' . DS . 'ShellDispatcher.php'; |
|
24 | +$dispatcher = 'Cake'.DS.'Console'.DS.'ShellDispatcher.php'; |
|
25 | 25 | |
26 | 26 | if (function_exists('ini_set')) { |
27 | 27 | $root = dirname(dirname(dirname(__FILE__))); |
28 | 28 | $appDir = basename(dirname(dirname(__FILE__))); |
29 | - $install = $root . DS . 'lib'; |
|
30 | - $composerInstall = $root . DS . $appDir . DS . 'Vendor' . DS . 'cakephp' . DS . 'cakephp' . DS . 'lib'; |
|
29 | + $install = $root.DS.'lib'; |
|
30 | + $composerInstall = $root.DS.$appDir.DS.'Vendor'.DS.'cakephp'.DS.'cakephp'.DS.'lib'; |
|
31 | 31 | |
32 | 32 | // the following lines differ from its sibling |
33 | 33 | // /lib/Cake/Console/Templates/skel/Console/cake.php |
34 | - if (file_exists($composerInstall . DS . $dispatcher)) { |
|
34 | + if (file_exists($composerInstall.DS.$dispatcher)) { |
|
35 | 35 | $install = $composerInstall; |
36 | 36 | } |
37 | 37 | |
38 | - ini_set('include_path', $install . PATH_SEPARATOR . ini_get('include_path')); |
|
38 | + ini_set('include_path', $install.PATH_SEPARATOR.ini_get('include_path')); |
|
39 | 39 | unset($root, $appDir, $install, $composerInstall); |
40 | 40 | } |
41 | 41 |
@@ -18,29 +18,29 @@ |
||
18 | 18 | */ |
19 | 19 | |
20 | 20 | if (!defined('DS')) { |
21 | - define('DS', DIRECTORY_SEPARATOR); |
|
21 | + define('DS', DIRECTORY_SEPARATOR); |
|
22 | 22 | } |
23 | 23 | |
24 | 24 | $dispatcher = 'Cake' . DS . 'Console' . DS . 'ShellDispatcher.php'; |
25 | 25 | |
26 | 26 | if (function_exists('ini_set')) { |
27 | - $root = dirname(dirname(dirname(__FILE__))); |
|
28 | - $appDir = basename(dirname(dirname(__FILE__))); |
|
29 | - $install = $root . DS . 'lib'; |
|
30 | - $composerInstall = $root . DS . $appDir . DS . 'Vendor' . DS . 'cakephp' . DS . 'cakephp' . DS . 'lib'; |
|
27 | + $root = dirname(dirname(dirname(__FILE__))); |
|
28 | + $appDir = basename(dirname(dirname(__FILE__))); |
|
29 | + $install = $root . DS . 'lib'; |
|
30 | + $composerInstall = $root . DS . $appDir . DS . 'Vendor' . DS . 'cakephp' . DS . 'cakephp' . DS . 'lib'; |
|
31 | 31 | |
32 | - // the following lines differ from its sibling |
|
33 | - // /lib/Cake/Console/Templates/skel/Console/cake.php |
|
34 | - if (file_exists($composerInstall . DS . $dispatcher)) { |
|
35 | - $install = $composerInstall; |
|
36 | - } |
|
32 | + // the following lines differ from its sibling |
|
33 | + // /lib/Cake/Console/Templates/skel/Console/cake.php |
|
34 | + if (file_exists($composerInstall . DS . $dispatcher)) { |
|
35 | + $install = $composerInstall; |
|
36 | + } |
|
37 | 37 | |
38 | - ini_set('include_path', $install . PATH_SEPARATOR . ini_get('include_path')); |
|
39 | - unset($root, $appDir, $install, $composerInstall); |
|
38 | + ini_set('include_path', $install . PATH_SEPARATOR . ini_get('include_path')); |
|
39 | + unset($root, $appDir, $install, $composerInstall); |
|
40 | 40 | } |
41 | 41 | |
42 | 42 | if (!include $dispatcher) { |
43 | - trigger_error('Could not locate CakePHP core files.', E_USER_ERROR); |
|
43 | + trigger_error('Could not locate CakePHP core files.', E_USER_ERROR); |
|
44 | 44 | } |
45 | 45 | unset($dispatcher); |
46 | 46 |
@@ -3,8 +3,8 @@ |
||
3 | 3 | class TreeBehavior extends ModelBehavior { |
4 | 4 | |
5 | 5 | public function treeGetSubtree(Model $Model, $tree, $_nodeId) { |
6 | - $func = function (&$tree, &$entry, $_nodeId) { |
|
7 | - if ((int)$entry['Entry']['id'] === (int)$_nodeId) { |
|
6 | + $func = function(&$tree, &$entry, $_nodeId) { |
|
7 | + if ((int) $entry['Entry']['id'] === (int) $_nodeId) { |
|
8 | 8 | $tree = array($entry); |
9 | 9 | return 'break'; |
10 | 10 | } |
@@ -1,35 +1,35 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | - class TreeBehavior extends ModelBehavior { |
|
4 | - |
|
5 | - public function treeGetSubtree(Model $Model, $tree, $_nodeId) { |
|
6 | - $func = function (&$tree, &$entry, $_nodeId) { |
|
7 | - if ((int)$entry['Entry']['id'] === (int)$_nodeId) { |
|
8 | - $tree = array($entry); |
|
9 | - return 'break'; |
|
10 | - } |
|
11 | - }; |
|
12 | - Entry::mapTreeElements($tree, $func, $_nodeId); |
|
13 | - return $tree; |
|
14 | - } |
|
15 | - |
|
16 | - public function treeBuild(Model $Model, $threads) { |
|
17 | - $tree = array(); |
|
18 | - foreach ($threads as $thread) { |
|
19 | - $id = $thread[$Model->alias]['id']; |
|
20 | - $pid = $thread[$Model->alias]['pid']; |
|
21 | - $tree[$id] = isset($tree[$id]) ? $tree[$id] + $thread : $thread; |
|
22 | - $tree[$pid]['_children'][] = &$tree[$id]; |
|
23 | - } |
|
24 | - |
|
25 | - // It's possible to do uasort before tree build and get the same results, |
|
26 | - // without _sortTreesAfterTime |
|
27 | - // but then *all* entries have to be sorted whereas now only subthreads with childs |
|
28 | - // are sorted. So using _sortTreesAfterTime is actually faster in praxis. |
|
29 | - $_sortedTrees = $this->_sortTreesAfterTime($tree[0]['_children']); |
|
30 | - |
|
31 | - return $_sortedTrees; |
|
32 | - } |
|
3 | + class TreeBehavior extends ModelBehavior { |
|
4 | + |
|
5 | + public function treeGetSubtree(Model $Model, $tree, $_nodeId) { |
|
6 | + $func = function (&$tree, &$entry, $_nodeId) { |
|
7 | + if ((int)$entry['Entry']['id'] === (int)$_nodeId) { |
|
8 | + $tree = array($entry); |
|
9 | + return 'break'; |
|
10 | + } |
|
11 | + }; |
|
12 | + Entry::mapTreeElements($tree, $func, $_nodeId); |
|
13 | + return $tree; |
|
14 | + } |
|
15 | + |
|
16 | + public function treeBuild(Model $Model, $threads) { |
|
17 | + $tree = array(); |
|
18 | + foreach ($threads as $thread) { |
|
19 | + $id = $thread[$Model->alias]['id']; |
|
20 | + $pid = $thread[$Model->alias]['pid']; |
|
21 | + $tree[$id] = isset($tree[$id]) ? $tree[$id] + $thread : $thread; |
|
22 | + $tree[$pid]['_children'][] = &$tree[$id]; |
|
23 | + } |
|
24 | + |
|
25 | + // It's possible to do uasort before tree build and get the same results, |
|
26 | + // without _sortTreesAfterTime |
|
27 | + // but then *all* entries have to be sorted whereas now only subthreads with childs |
|
28 | + // are sorted. So using _sortTreesAfterTime is actually faster in praxis. |
|
29 | + $_sortedTrees = $this->_sortTreesAfterTime($tree[0]['_children']); |
|
30 | + |
|
31 | + return $_sortedTrees; |
|
32 | + } |
|
33 | 33 | |
34 | 34 | /** |
35 | 35 | * Sort all entries in trees after time |
@@ -38,25 +38,25 @@ discard block |
||
38 | 38 | * @param int $level |
39 | 39 | * @return array |
40 | 40 | */ |
41 | - protected function _sortTreesAfterTime($in, $level = 0) { |
|
42 | - if ($level > 0) { |
|
43 | - uasort($in, [$this, '_sort']); |
|
44 | - } |
|
45 | - |
|
46 | - foreach ($in as $k => $v) { |
|
47 | - if (isset($v['_children'])) { |
|
48 | - $in[$k]['_children'] = $this->_sortTreesAfterTime($v['_children'], $level + 1); |
|
49 | - } |
|
50 | - } |
|
51 | - return $in; |
|
52 | - } |
|
53 | - |
|
54 | - protected function _sort($a, $b) { |
|
55 | - if ($a['Entry']['time'] === $b['Entry']['time']) { |
|
56 | - return ($a['Entry']['id'] > $b['Entry']['id']) ? 1 : -1; |
|
57 | - } else { |
|
58 | - return ($a['Entry']['time'] > $b['Entry']['time']) ? 1 : -1; |
|
59 | - } |
|
60 | - } |
|
61 | - |
|
62 | - } |
|
41 | + protected function _sortTreesAfterTime($in, $level = 0) { |
|
42 | + if ($level > 0) { |
|
43 | + uasort($in, [$this, '_sort']); |
|
44 | + } |
|
45 | + |
|
46 | + foreach ($in as $k => $v) { |
|
47 | + if (isset($v['_children'])) { |
|
48 | + $in[$k]['_children'] = $this->_sortTreesAfterTime($v['_children'], $level + 1); |
|
49 | + } |
|
50 | + } |
|
51 | + return $in; |
|
52 | + } |
|
53 | + |
|
54 | + protected function _sort($a, $b) { |
|
55 | + if ($a['Entry']['time'] === $b['Entry']['time']) { |
|
56 | + return ($a['Entry']['id'] > $b['Entry']['id']) ? 1 : -1; |
|
57 | + } else { |
|
58 | + return ($a['Entry']['time'] > $b['Entry']['time']) ? 1 : -1; |
|
59 | + } |
|
60 | + } |
|
61 | + |
|
62 | + } |
@@ -67,7 +67,7 @@ |
||
67 | 67 | ) |
68 | 68 | ); |
69 | 69 | if ($lastShout) { |
70 | - $out = (int)current($lastShout); |
|
70 | + $out = (int) current($lastShout); |
|
71 | 71 | } |
72 | 72 | return $out; |
73 | 73 | } |
@@ -1,123 +1,123 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | - App::uses('AppModel', 'Model'); |
|
3 | + App::uses('AppModel', 'Model'); |
|
4 | 4 | |
5 | 5 | /** |
6 | 6 | * Shout Model |
7 | 7 | * |
8 | 8 | * @property User $User |
9 | 9 | */ |
10 | - class Shout extends AppModel { |
|
10 | + class Shout extends AppModel { |
|
11 | 11 | |
12 | 12 | /** |
13 | 13 | * Display field |
14 | 14 | * |
15 | 15 | * @var string |
16 | 16 | */ |
17 | - public $displayField = 'text'; |
|
17 | + public $displayField = 'text'; |
|
18 | 18 | |
19 | - public $actsAs = [ |
|
20 | - 'Markup', |
|
21 | - 'Containable' |
|
22 | - ]; |
|
19 | + public $actsAs = [ |
|
20 | + 'Markup', |
|
21 | + 'Containable' |
|
22 | + ]; |
|
23 | 23 | |
24 | - public $virtualFields = [ |
|
25 | - 'username' => 'User.username' |
|
26 | - ]; |
|
24 | + public $virtualFields = [ |
|
25 | + 'username' => 'User.username' |
|
26 | + ]; |
|
27 | 27 | |
28 | 28 | /** |
29 | 29 | * Validation rules |
30 | 30 | * |
31 | 31 | * @var array |
32 | 32 | */ |
33 | - public $validate = array( |
|
34 | - 'text' => array( |
|
35 | - 'maxlength' => array( |
|
36 | - 'rule' => array('maxlength', 255), |
|
37 | - ), |
|
38 | - ), |
|
39 | - ); |
|
33 | + public $validate = array( |
|
34 | + 'text' => array( |
|
35 | + 'maxlength' => array( |
|
36 | + 'rule' => array('maxlength', 255), |
|
37 | + ), |
|
38 | + ), |
|
39 | + ); |
|
40 | 40 | |
41 | 41 | /** |
42 | 42 | * belongsTo associations |
43 | 43 | * |
44 | 44 | * @var array |
45 | 45 | */ |
46 | - public $belongsTo = array( |
|
47 | - 'User' => array( |
|
48 | - 'className' => 'User', |
|
49 | - 'foreignKey' => 'user_id', |
|
50 | - 'conditions' => '', |
|
51 | - 'fields' => '', |
|
52 | - 'order' => '' |
|
53 | - ) |
|
54 | - ); |
|
46 | + public $belongsTo = array( |
|
47 | + 'User' => array( |
|
48 | + 'className' => 'User', |
|
49 | + 'foreignKey' => 'user_id', |
|
50 | + 'conditions' => '', |
|
51 | + 'fields' => '', |
|
52 | + 'order' => '' |
|
53 | + ) |
|
54 | + ); |
|
55 | 55 | |
56 | - public $maxNumberOfShouts = 10; |
|
56 | + public $maxNumberOfShouts = 10; |
|
57 | 57 | |
58 | - public function findLastId() { |
|
59 | - $out = 0; |
|
60 | - $lastShout = $this->find( |
|
61 | - 'list', |
|
62 | - array( |
|
63 | - 'contain' => false, |
|
64 | - 'fields' => 'id', |
|
65 | - 'order' => 'id desc', |
|
66 | - 'limit' => 1 |
|
67 | - ) |
|
68 | - ); |
|
69 | - if ($lastShout) { |
|
70 | - $out = (int)current($lastShout); |
|
71 | - } |
|
72 | - return $out; |
|
73 | - } |
|
58 | + public function findLastId() { |
|
59 | + $out = 0; |
|
60 | + $lastShout = $this->find( |
|
61 | + 'list', |
|
62 | + array( |
|
63 | + 'contain' => false, |
|
64 | + 'fields' => 'id', |
|
65 | + 'order' => 'id desc', |
|
66 | + 'limit' => 1 |
|
67 | + ) |
|
68 | + ); |
|
69 | + if ($lastShout) { |
|
70 | + $out = (int)current($lastShout); |
|
71 | + } |
|
72 | + return $out; |
|
73 | + } |
|
74 | 74 | |
75 | 75 | /** |
76 | 76 | * Get all shouts |
77 | 77 | * |
78 | 78 | * @return array |
79 | 79 | */ |
80 | - public function get() { |
|
81 | - $shouts = $this->find( |
|
82 | - 'all', |
|
83 | - [ |
|
84 | - 'contain' => 'User.username', |
|
85 | - 'order' => 'Shout.id DESC' |
|
86 | - ] |
|
87 | - ); |
|
88 | - return $shouts; |
|
89 | - } |
|
80 | + public function get() { |
|
81 | + $shouts = $this->find( |
|
82 | + 'all', |
|
83 | + [ |
|
84 | + 'contain' => 'User.username', |
|
85 | + 'order' => 'Shout.id DESC' |
|
86 | + ] |
|
87 | + ); |
|
88 | + return $shouts; |
|
89 | + } |
|
90 | 90 | |
91 | - public function push($data) { |
|
92 | - $data[$this->alias]['time'] = gmdate("Y-m-d H:i:s", time()); |
|
93 | - $this->create($data); |
|
94 | - $success = $this->save(); |
|
91 | + public function push($data) { |
|
92 | + $data[$this->alias]['time'] = gmdate("Y-m-d H:i:s", time()); |
|
93 | + $this->create($data); |
|
94 | + $success = $this->save(); |
|
95 | 95 | |
96 | - $count = $this->find('count'); |
|
97 | - while ($success && $count > $this->maxNumberOfShouts) { |
|
98 | - $success = $this->shift(); |
|
99 | - $count -= 1; |
|
100 | - } |
|
101 | - return $success; |
|
102 | - } |
|
96 | + $count = $this->find('count'); |
|
97 | + while ($success && $count > $this->maxNumberOfShouts) { |
|
98 | + $success = $this->shift(); |
|
99 | + $count -= 1; |
|
100 | + } |
|
101 | + return $success; |
|
102 | + } |
|
103 | 103 | |
104 | - public function shift() { |
|
105 | - $currentIds = $this->find( |
|
106 | - 'list', |
|
107 | - [ |
|
108 | - 'fields' => 'Shout.id', |
|
109 | - 'order' => 'Shout.id ASC' |
|
110 | - ] |
|
111 | - ); |
|
112 | - $oldestId = current($currentIds); |
|
113 | - return $this->delete($oldestId); |
|
114 | - } |
|
104 | + public function shift() { |
|
105 | + $currentIds = $this->find( |
|
106 | + 'list', |
|
107 | + [ |
|
108 | + 'fields' => 'Shout.id', |
|
109 | + 'order' => 'Shout.id ASC' |
|
110 | + ] |
|
111 | + ); |
|
112 | + $oldestId = current($currentIds); |
|
113 | + return $this->delete($oldestId); |
|
114 | + } |
|
115 | 115 | |
116 | - public function beforeSave($options = []) { |
|
117 | - if (empty($this->data[$this->alias]['text']) === false) { |
|
118 | - $this->data[$this->alias]['text'] = $this->prepareMarkup($this->data[$this->alias]['text']); |
|
119 | - } |
|
120 | - return true; |
|
121 | - } |
|
116 | + public function beforeSave($options = []) { |
|
117 | + if (empty($this->data[$this->alias]['text']) === false) { |
|
118 | + $this->data[$this->alias]['text'] = $this->prepareMarkup($this->data[$this->alias]['text']); |
|
119 | + } |
|
120 | + return true; |
|
121 | + } |
|
122 | 122 | |
123 | - } |
|
123 | + } |
@@ -40,7 +40,7 @@ |
||
40 | 40 | 'conditions' => array('user_id' => $userId) |
41 | 41 | ) |
42 | 42 | ); |
43 | - return (int)$number; |
|
43 | + return (int) $number; |
|
44 | 44 | } |
45 | 45 | |
46 | 46 | } |
47 | 47 | \ No newline at end of file |
@@ -7,24 +7,24 @@ discard block |
||
7 | 7 | */ |
8 | 8 | class Upload extends AppModel { |
9 | 9 | |
10 | - public $name = 'Upload'; |
|
10 | + public $name = 'Upload'; |
|
11 | 11 | |
12 | - public $recursive = -1; |
|
12 | + public $recursive = -1; |
|
13 | 13 | |
14 | - public $actsAs = array('FileUpload.FileUpload'); |
|
14 | + public $actsAs = array('FileUpload.FileUpload'); |
|
15 | 15 | |
16 | - public $belongsTo = array('User'); |
|
16 | + public $belongsTo = array('User'); |
|
17 | 17 | |
18 | - public function deleteAllFromUser($userId) { |
|
19 | - return $this->deleteAll( |
|
20 | - array( |
|
21 | - 'Upload.user_id' => $userId |
|
22 | - ), |
|
23 | - false, |
|
24 | - // call beforeDelete FileUploader plugin callback to remove files from disk |
|
25 | - true |
|
26 | - ); |
|
27 | - } |
|
18 | + public function deleteAllFromUser($userId) { |
|
19 | + return $this->deleteAll( |
|
20 | + array( |
|
21 | + 'Upload.user_id' => $userId |
|
22 | + ), |
|
23 | + false, |
|
24 | + // call beforeDelete FileUploader plugin callback to remove files from disk |
|
25 | + true |
|
26 | + ); |
|
27 | + } |
|
28 | 28 | |
29 | 29 | /** |
30 | 30 | * Returns the number of uploads a user `user_id` has made |
@@ -33,14 +33,14 @@ discard block |
||
33 | 33 | * |
34 | 34 | * @return int |
35 | 35 | */ |
36 | - public function countUser($userId) { |
|
37 | - $number = $this->find( |
|
38 | - 'count', |
|
39 | - array( |
|
40 | - 'conditions' => array('user_id' => $userId) |
|
41 | - ) |
|
42 | - ); |
|
43 | - return (int)$number; |
|
44 | - } |
|
36 | + public function countUser($userId) { |
|
37 | + $number = $this->find( |
|
38 | + 'count', |
|
39 | + array( |
|
40 | + 'conditions' => array('user_id' => $userId) |
|
41 | + ) |
|
42 | + ); |
|
43 | + return (int)$number; |
|
44 | + } |
|
45 | 45 | |
46 | 46 | } |
47 | 47 | \ No newline at end of file |
@@ -37,8 +37,8 @@ discard block |
||
37 | 37 | return $this->_cache[$accession]; |
38 | 38 | } |
39 | 39 | $this->_cache[$accession] = Cache::remember( |
40 | - 'Saito.Cache.CategoriesForAccession.' . $accession, |
|
41 | - function () use ($accession) { |
|
40 | + 'Saito.Cache.CategoriesForAccession.'.$accession, |
|
41 | + function() use ($accession) { |
|
42 | 42 | return $this->find('list', [ |
43 | 43 | 'conditions' => ['accession <=' => $accession], |
44 | 44 | 'fields' => ['Category.id', 'Category.category'], |
@@ -52,7 +52,7 @@ discard block |
||
52 | 52 | if (!isset($this->id)) { |
53 | 53 | return false; |
54 | 54 | } |
55 | - if ((int)$targetCategory === (int)$this->id) { |
|
55 | + if ((int) $targetCategory === (int) $this->id) { |
|
56 | 56 | return true; |
57 | 57 | } |
58 | 58 |
@@ -1,109 +1,109 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | - App::uses('AppSettingModel', 'Lib/Model'); |
|
4 | - |
|
5 | - class Category extends AppSettingModel { |
|
6 | - |
|
7 | - public $name = 'Category'; |
|
8 | - |
|
9 | - public $actsAs = array('Containable'); |
|
10 | - |
|
11 | - public $cacheQueries = true; |
|
12 | - |
|
13 | - public $hasMany = [ |
|
14 | - 'Entry' => [ |
|
15 | - 'className' => 'Entry', |
|
16 | - 'foreignKey' => 'category_id' |
|
17 | - ] |
|
18 | - ]; |
|
19 | - |
|
20 | - public $validate = [ |
|
21 | - 'category_order' => [ |
|
22 | - 'numeric' => [ |
|
23 | - 'rule' => ['numeric'] |
|
24 | - ] |
|
25 | - ], |
|
26 | - 'accession' => [ |
|
27 | - 'numeric' => [ |
|
28 | - 'rule' => ['numeric'] |
|
29 | - ] |
|
30 | - ] |
|
31 | - ]; |
|
32 | - |
|
33 | - protected $_cache = []; |
|
34 | - |
|
35 | - public function getCategoriesForAccession($accession) { |
|
36 | - if (!empty($this->_cache[$accession])) { |
|
37 | - return $this->_cache[$accession]; |
|
38 | - } |
|
39 | - $this->_cache[$accession] = Cache::remember( |
|
40 | - 'Saito.Cache.CategoriesForAccession.' . $accession, |
|
41 | - function () use ($accession) { |
|
42 | - return $this->find('list', [ |
|
43 | - 'conditions' => ['accession <=' => $accession], |
|
44 | - 'fields' => ['Category.id', 'Category.category'], |
|
45 | - 'order' => 'category_order ASC' |
|
46 | - ]); |
|
47 | - }); |
|
48 | - return $this->_cache[$accession]; |
|
49 | - } |
|
50 | - |
|
51 | - public function mergeIntoCategory($targetCategory) { |
|
52 | - if (!isset($this->id)) { |
|
53 | - return false; |
|
54 | - } |
|
55 | - if ((int)$targetCategory === (int)$this->id) { |
|
56 | - return true; |
|
57 | - } |
|
58 | - |
|
59 | - $this->Entry->contain(); |
|
60 | - return $this->Entry->updateAll( |
|
61 | - array('Entry.category' => $targetCategory), |
|
62 | - array('Entry.category' => $this->id) |
|
63 | - ); |
|
64 | - } |
|
65 | - |
|
66 | - public function deleteWithAllEntries() { |
|
67 | - if (!isset($this->id)) { |
|
68 | - return false; |
|
69 | - } |
|
70 | - |
|
71 | - $this->Entry->contain(); |
|
72 | - $entriesDeleted = $this->Entry->deleteAll( |
|
73 | - array('Entry.category' => $this->id), |
|
74 | - false |
|
75 | - ); |
|
76 | - |
|
77 | - return parent::delete($this->field('id'), false) && $entriesDeleted; |
|
78 | - } |
|
3 | + App::uses('AppSettingModel', 'Lib/Model'); |
|
4 | + |
|
5 | + class Category extends AppSettingModel { |
|
6 | + |
|
7 | + public $name = 'Category'; |
|
8 | + |
|
9 | + public $actsAs = array('Containable'); |
|
10 | + |
|
11 | + public $cacheQueries = true; |
|
12 | + |
|
13 | + public $hasMany = [ |
|
14 | + 'Entry' => [ |
|
15 | + 'className' => 'Entry', |
|
16 | + 'foreignKey' => 'category_id' |
|
17 | + ] |
|
18 | + ]; |
|
19 | + |
|
20 | + public $validate = [ |
|
21 | + 'category_order' => [ |
|
22 | + 'numeric' => [ |
|
23 | + 'rule' => ['numeric'] |
|
24 | + ] |
|
25 | + ], |
|
26 | + 'accession' => [ |
|
27 | + 'numeric' => [ |
|
28 | + 'rule' => ['numeric'] |
|
29 | + ] |
|
30 | + ] |
|
31 | + ]; |
|
32 | + |
|
33 | + protected $_cache = []; |
|
34 | + |
|
35 | + public function getCategoriesForAccession($accession) { |
|
36 | + if (!empty($this->_cache[$accession])) { |
|
37 | + return $this->_cache[$accession]; |
|
38 | + } |
|
39 | + $this->_cache[$accession] = Cache::remember( |
|
40 | + 'Saito.Cache.CategoriesForAccession.' . $accession, |
|
41 | + function () use ($accession) { |
|
42 | + return $this->find('list', [ |
|
43 | + 'conditions' => ['accession <=' => $accession], |
|
44 | + 'fields' => ['Category.id', 'Category.category'], |
|
45 | + 'order' => 'category_order ASC' |
|
46 | + ]); |
|
47 | + }); |
|
48 | + return $this->_cache[$accession]; |
|
49 | + } |
|
50 | + |
|
51 | + public function mergeIntoCategory($targetCategory) { |
|
52 | + if (!isset($this->id)) { |
|
53 | + return false; |
|
54 | + } |
|
55 | + if ((int)$targetCategory === (int)$this->id) { |
|
56 | + return true; |
|
57 | + } |
|
58 | + |
|
59 | + $this->Entry->contain(); |
|
60 | + return $this->Entry->updateAll( |
|
61 | + array('Entry.category' => $targetCategory), |
|
62 | + array('Entry.category' => $this->id) |
|
63 | + ); |
|
64 | + } |
|
65 | + |
|
66 | + public function deleteWithAllEntries() { |
|
67 | + if (!isset($this->id)) { |
|
68 | + return false; |
|
69 | + } |
|
70 | + |
|
71 | + $this->Entry->contain(); |
|
72 | + $entriesDeleted = $this->Entry->deleteAll( |
|
73 | + array('Entry.category' => $this->id), |
|
74 | + false |
|
75 | + ); |
|
76 | + |
|
77 | + return parent::delete($this->field('id'), false) && $entriesDeleted; |
|
78 | + } |
|
79 | 79 | |
80 | 80 | /** |
81 | 81 | * Updates thread counter from entry table |
82 | 82 | * |
83 | 83 | * @return integer current thread count |
84 | 84 | */ |
85 | - public function updateThreadCounter() { |
|
86 | - // @performance |
|
87 | - $count = $this->Entry->find( |
|
88 | - 'count', |
|
89 | - [ |
|
90 | - 'contain' => false, |
|
91 | - 'conditions' => [ |
|
92 | - 'pid' => 0, |
|
93 | - 'Entry.category_id' => $this->id |
|
94 | - ] |
|
95 | - ] |
|
96 | - ); |
|
97 | - $this->saveField('thread_count', $count); |
|
98 | - return $count; |
|
99 | - } |
|
100 | - |
|
101 | - public function afterSave($created, $options = array()) { |
|
102 | - // don't empty cache if it's only a thread count update |
|
103 | - if (!$created && !isset($this->data[$this->alias]['category'])) { |
|
104 | - $options['clearCache'] = false; |
|
105 | - } |
|
106 | - parent::afterSave($created, $options); |
|
107 | - } |
|
108 | - |
|
109 | - } |
|
110 | 85 | \ No newline at end of file |
86 | + public function updateThreadCounter() { |
|
87 | + // @performance |
|
88 | + $count = $this->Entry->find( |
|
89 | + 'count', |
|
90 | + [ |
|
91 | + 'contain' => false, |
|
92 | + 'conditions' => [ |
|
93 | + 'pid' => 0, |
|
94 | + 'Entry.category_id' => $this->id |
|
95 | + ] |
|
96 | + ] |
|
97 | + ); |
|
98 | + $this->saveField('thread_count', $count); |
|
99 | + return $count; |
|
100 | + } |
|
101 | + |
|
102 | + public function afterSave($created, $options = array()) { |
|
103 | + // don't empty cache if it's only a thread count update |
|
104 | + if (!$created && !isset($this->data[$this->alias]['category'])) { |
|
105 | + $options['clearCache'] = false; |
|
106 | + } |
|
107 | + parent::afterSave($created, $options); |
|
108 | + } |
|
109 | + |
|
110 | + } |
|
111 | 111 | \ No newline at end of file |
@@ -363,7 +363,7 @@ discard block |
||
363 | 363 | } |
364 | 364 | |
365 | 365 | public function paginate($conditions, $fields, $order, $limit, $page = 1, $recursive = null, $extra = array()) { |
366 | - $username = $this->alias . '.' . 'username'; |
|
366 | + $username = $this->alias.'.'.'username'; |
|
367 | 367 | if (isset($order[$username])) { |
368 | 368 | $direction = $order[$username]; |
369 | 369 | unset($order[$username]); |
@@ -553,8 +553,8 @@ discard block |
||
553 | 553 | $this->set('user_category_custom', $newCats); |
554 | 554 | $this->save(); |
555 | 555 | } else { |
556 | - $category = (int)$category; |
|
557 | - if ($category > 0 && $this->Entry->Category->exists((int)$category)) { |
|
556 | + $category = (int) $category; |
|
557 | + if ($category > 0 && $this->Entry->Category->exists((int) $category)) { |
|
558 | 558 | $this->set('user_category_active', $category); |
559 | 559 | $this->save(); |
560 | 560 | } else { |
@@ -601,8 +601,8 @@ discard block |
||
601 | 601 | if ($state === 'before') { |
602 | 602 | $query['contain'] = false; |
603 | 603 | $query['limit'] = 1; |
604 | - $query['conditions'][$this->alias . '.activate_code'] = 0; |
|
605 | - $query['order'] = $this->alias . '.id DESC'; |
|
604 | + $query['conditions'][$this->alias.'.activate_code'] = 0; |
|
605 | + $query['order'] = $this->alias.'.id DESC'; |
|
606 | 606 | return $query; |
607 | 607 | } |
608 | 608 | if (empty($results[0])) { |
@@ -1,230 +1,230 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | - App::uses('AppModel', 'Model'); |
|
4 | - App::uses('CakeEvent', 'Event'); |
|
3 | + App::uses('AppModel', 'Model'); |
|
4 | + App::uses('CakeEvent', 'Event'); |
|
5 | 5 | |
6 | - /* |
|
6 | + /* |
|
7 | 7 | * Authentication methods |
8 | 8 | */ |
9 | - App::uses('BlowfishPasswordHasher', 'Controller/Component/Auth'); |
|
10 | - App::uses('MlfPasswordHasher', 'Controller/Component/Auth'); |
|
11 | - App::uses('Mlf2PasswordHasher', 'Controller/Component/Auth'); |
|
9 | + App::uses('BlowfishPasswordHasher', 'Controller/Component/Auth'); |
|
10 | + App::uses('MlfPasswordHasher', 'Controller/Component/Auth'); |
|
11 | + App::uses('Mlf2PasswordHasher', 'Controller/Component/Auth'); |
|
12 | 12 | |
13 | 13 | /** |
14 | 14 | * @td verify that old pw is needed for changing pw(?) [See user.test.php both validatePw tests] |
15 | 15 | */ |
16 | - class User extends AppModel { |
|
17 | - |
|
18 | - public $name = 'User'; |
|
19 | - |
|
20 | - public $actsAs = [ |
|
21 | - 'Containable', |
|
22 | - 'Cron.Cron' => [ |
|
23 | - 'registerGc' => [ |
|
24 | - 'id' => 'User.registerGc', |
|
25 | - 'due' => 'daily', |
|
26 | - ], |
|
27 | - 'userBlockGc' => [ |
|
28 | - 'id' => 'User.userBlockGc', |
|
29 | - 'due' => '+15 minutes', |
|
30 | - ] |
|
31 | - ] |
|
32 | - ]; |
|
33 | - |
|
34 | - public $hasOne = array( |
|
35 | - 'UserOnline' => [ |
|
36 | - 'className' => 'UserOnline', |
|
37 | - 'foreignKey' => 'user_id', |
|
38 | - ] |
|
39 | - ); |
|
40 | - |
|
41 | - public $hasMany = array( |
|
42 | - 'UserBlock' => [ |
|
43 | - 'foreignKey' => 'user_id', |
|
44 | - 'dependent' => true, |
|
45 | - 'order' => ['UserBlock.ended IS NULL DESC', 'UserBlock.ended DESC', 'UserBlock.id DESC'] |
|
46 | - ], |
|
47 | - 'Bookmark' => array( |
|
48 | - 'foreignKey' => 'user_id', |
|
49 | - 'dependent' => true |
|
50 | - ), |
|
51 | - 'Esnotification' => array( |
|
52 | - 'foreignKey' => 'user_id' |
|
53 | - ), |
|
54 | - 'Entry' => array( |
|
55 | - 'className' => 'Entry', |
|
56 | - 'foreignKey' => 'user_id' |
|
57 | - ), |
|
58 | - 'UserRead' => [ |
|
59 | - 'className' => 'UserRead', |
|
60 | - 'foreignKey' => 'user_id', |
|
61 | - 'dependent' => true |
|
62 | - ], |
|
63 | - 'Upload' => array( |
|
64 | - 'className' => 'Upload', |
|
65 | - 'foreignKey' => 'user_id' |
|
66 | - ), |
|
67 | - 'Ignore' => [ |
|
68 | - 'className' => 'UserIgnore', |
|
69 | - 'foreignKey' => 'user_id' |
|
70 | - ] |
|
71 | - ); |
|
72 | - |
|
73 | - public $validate = [ |
|
74 | - 'username' => [ |
|
75 | - 'isUnique' => ['rule' => 'isUniqueCiString'], |
|
76 | - 'notEmpty' => ['rule' => 'notBlank'], |
|
77 | - 'hasAllowedChars' => ['rule' => ['validateHasAllowedChars']], |
|
78 | - 'isUsernameEqual' => [ |
|
79 | - 'on' => 'create', |
|
80 | - 'rule' => 'validateUsernameEqual' |
|
81 | - ] |
|
82 | - ], |
|
83 | - 'user_type' => [ |
|
84 | - 'allowedChoice' => ['rule' => ['inList', ['user', 'admin', 'mod']]] |
|
85 | - ], |
|
86 | - 'password' => [ |
|
87 | - 'notEmpty' => ['rule' => 'notBlank'], |
|
88 | - 'pwConfirm' => [ |
|
89 | - 'rule' => ['validateConfirmPassword'], |
|
90 | - 'message' => 'validation_error_pwConfirm' |
|
91 | - ] |
|
92 | - ], |
|
93 | - 'password_old' => [ |
|
94 | - 'notEmpty' => [ |
|
95 | - 'rule' => 'notBlank', |
|
96 | - 'last' => 'true', |
|
97 | - ], |
|
98 | - 'pwCheckOld' => [ |
|
99 | - 'rule' => ['validateCheckOldPassword'], |
|
100 | - 'last' => 'true', |
|
101 | - 'message' => 'validation_error_pwCheckOld' |
|
102 | - ] |
|
103 | - ], |
|
104 | - 'user_email' => [ |
|
105 | - 'isUnique' => ['rule' => 'isUnique', 'last' => 'true'], |
|
106 | - 'isEmail' => ['rule' => ['email', true], 'last' => 'true'] |
|
107 | - ], |
|
108 | - 'registered' => ['rule' => ['notBlank']], |
|
109 | - 'logins' => ['rule' => 'numeric'], |
|
110 | - 'personal_messages' => ['rule' => ['boolean']], |
|
111 | - 'user_lock' => ['rule' => ['boolean']], |
|
112 | - 'activate_code' => [ |
|
113 | - 'numeric' => ['rule' => 'numeric', 'allowEmpty' => false], |
|
114 | - 'between' => ['rule' => ['between', 0, 9999999]] |
|
115 | - ], |
|
116 | - 'user_signatures_hide' => ['rule' => ['boolean']], |
|
117 | - 'user_signature_images_hide' => ['rule' => ['boolean']], |
|
118 | - 'user_forum_refresh_time' => [ |
|
119 | - 'numeric' => ['rule' => 'numeric'], |
|
120 | - 'greaterNull' => ['rule' => ['comparison', '>=', 0]], |
|
121 | - 'maxLength' => ['rule' => ['maxLength', 3]], |
|
122 | - ], |
|
123 | - 'user_automaticaly_mark_as_read' => ['rule' => ['boolean']], |
|
124 | - 'user_sort_last_answer' => ['rule' => ['boolean']], |
|
125 | - 'user_color_new_postings' => [ |
|
126 | - 'allowEmpty' => true, |
|
127 | - 'rule' => '/^#?[a-f0-9]{0,6}$/i' |
|
128 | - ], |
|
129 | - 'user_color_old_postings' => [ |
|
130 | - 'allowEmpty' => true, |
|
131 | - 'rule' => '/^#?[a-f0-9]{0,6}$/i', |
|
132 | - 'message' => '*' |
|
133 | - ], |
|
134 | - 'user_color_actual_posting' => [ |
|
135 | - 'allowEmpty' => true, |
|
136 | - 'rule' => '/^#?[a-f0-9]{0,6}$/i' |
|
137 | - ], |
|
138 | - 'user_place_lat' => [ |
|
139 | - 'validLatitude' => ['rule' => ['inRange', -90, 90], |
|
140 | - 'allowEmpty' => true], |
|
141 | - ], |
|
142 | - 'user_place_lng' => [ |
|
143 | - 'validLongitude' => ['rule' => ['inRange', -180, 180], |
|
144 | - 'allowEmpty' => true], |
|
145 | - ], |
|
146 | - 'user_place_zoom' => [ |
|
147 | - 'numeric' => ['rule' => ['naturalNumber', 0], 'allowEmpty' => true], |
|
148 | - 'between' => ['rule' => ['inRange', 0, 25]] |
|
149 | - ] |
|
150 | - ]; |
|
151 | - |
|
152 | - public $findMethods = [ |
|
153 | - 'latest' => true |
|
154 | - ]; |
|
155 | - |
|
156 | - protected $_passwordHasher = [ |
|
157 | - 'BlowfishPasswordHasher', |
|
158 | - 'Mlf2PasswordHasher', |
|
159 | - 'MlfPasswordHasher' |
|
160 | - ]; |
|
161 | - |
|
162 | - protected $_settings = [ |
|
163 | - 'user_name_disallowed_chars' => ['\'', ';', '&', '<', '>'] |
|
164 | - ]; |
|
16 | + class User extends AppModel { |
|
17 | + |
|
18 | + public $name = 'User'; |
|
19 | + |
|
20 | + public $actsAs = [ |
|
21 | + 'Containable', |
|
22 | + 'Cron.Cron' => [ |
|
23 | + 'registerGc' => [ |
|
24 | + 'id' => 'User.registerGc', |
|
25 | + 'due' => 'daily', |
|
26 | + ], |
|
27 | + 'userBlockGc' => [ |
|
28 | + 'id' => 'User.userBlockGc', |
|
29 | + 'due' => '+15 minutes', |
|
30 | + ] |
|
31 | + ] |
|
32 | + ]; |
|
33 | + |
|
34 | + public $hasOne = array( |
|
35 | + 'UserOnline' => [ |
|
36 | + 'className' => 'UserOnline', |
|
37 | + 'foreignKey' => 'user_id', |
|
38 | + ] |
|
39 | + ); |
|
40 | + |
|
41 | + public $hasMany = array( |
|
42 | + 'UserBlock' => [ |
|
43 | + 'foreignKey' => 'user_id', |
|
44 | + 'dependent' => true, |
|
45 | + 'order' => ['UserBlock.ended IS NULL DESC', 'UserBlock.ended DESC', 'UserBlock.id DESC'] |
|
46 | + ], |
|
47 | + 'Bookmark' => array( |
|
48 | + 'foreignKey' => 'user_id', |
|
49 | + 'dependent' => true |
|
50 | + ), |
|
51 | + 'Esnotification' => array( |
|
52 | + 'foreignKey' => 'user_id' |
|
53 | + ), |
|
54 | + 'Entry' => array( |
|
55 | + 'className' => 'Entry', |
|
56 | + 'foreignKey' => 'user_id' |
|
57 | + ), |
|
58 | + 'UserRead' => [ |
|
59 | + 'className' => 'UserRead', |
|
60 | + 'foreignKey' => 'user_id', |
|
61 | + 'dependent' => true |
|
62 | + ], |
|
63 | + 'Upload' => array( |
|
64 | + 'className' => 'Upload', |
|
65 | + 'foreignKey' => 'user_id' |
|
66 | + ), |
|
67 | + 'Ignore' => [ |
|
68 | + 'className' => 'UserIgnore', |
|
69 | + 'foreignKey' => 'user_id' |
|
70 | + ] |
|
71 | + ); |
|
72 | + |
|
73 | + public $validate = [ |
|
74 | + 'username' => [ |
|
75 | + 'isUnique' => ['rule' => 'isUniqueCiString'], |
|
76 | + 'notEmpty' => ['rule' => 'notBlank'], |
|
77 | + 'hasAllowedChars' => ['rule' => ['validateHasAllowedChars']], |
|
78 | + 'isUsernameEqual' => [ |
|
79 | + 'on' => 'create', |
|
80 | + 'rule' => 'validateUsernameEqual' |
|
81 | + ] |
|
82 | + ], |
|
83 | + 'user_type' => [ |
|
84 | + 'allowedChoice' => ['rule' => ['inList', ['user', 'admin', 'mod']]] |
|
85 | + ], |
|
86 | + 'password' => [ |
|
87 | + 'notEmpty' => ['rule' => 'notBlank'], |
|
88 | + 'pwConfirm' => [ |
|
89 | + 'rule' => ['validateConfirmPassword'], |
|
90 | + 'message' => 'validation_error_pwConfirm' |
|
91 | + ] |
|
92 | + ], |
|
93 | + 'password_old' => [ |
|
94 | + 'notEmpty' => [ |
|
95 | + 'rule' => 'notBlank', |
|
96 | + 'last' => 'true', |
|
97 | + ], |
|
98 | + 'pwCheckOld' => [ |
|
99 | + 'rule' => ['validateCheckOldPassword'], |
|
100 | + 'last' => 'true', |
|
101 | + 'message' => 'validation_error_pwCheckOld' |
|
102 | + ] |
|
103 | + ], |
|
104 | + 'user_email' => [ |
|
105 | + 'isUnique' => ['rule' => 'isUnique', 'last' => 'true'], |
|
106 | + 'isEmail' => ['rule' => ['email', true], 'last' => 'true'] |
|
107 | + ], |
|
108 | + 'registered' => ['rule' => ['notBlank']], |
|
109 | + 'logins' => ['rule' => 'numeric'], |
|
110 | + 'personal_messages' => ['rule' => ['boolean']], |
|
111 | + 'user_lock' => ['rule' => ['boolean']], |
|
112 | + 'activate_code' => [ |
|
113 | + 'numeric' => ['rule' => 'numeric', 'allowEmpty' => false], |
|
114 | + 'between' => ['rule' => ['between', 0, 9999999]] |
|
115 | + ], |
|
116 | + 'user_signatures_hide' => ['rule' => ['boolean']], |
|
117 | + 'user_signature_images_hide' => ['rule' => ['boolean']], |
|
118 | + 'user_forum_refresh_time' => [ |
|
119 | + 'numeric' => ['rule' => 'numeric'], |
|
120 | + 'greaterNull' => ['rule' => ['comparison', '>=', 0]], |
|
121 | + 'maxLength' => ['rule' => ['maxLength', 3]], |
|
122 | + ], |
|
123 | + 'user_automaticaly_mark_as_read' => ['rule' => ['boolean']], |
|
124 | + 'user_sort_last_answer' => ['rule' => ['boolean']], |
|
125 | + 'user_color_new_postings' => [ |
|
126 | + 'allowEmpty' => true, |
|
127 | + 'rule' => '/^#?[a-f0-9]{0,6}$/i' |
|
128 | + ], |
|
129 | + 'user_color_old_postings' => [ |
|
130 | + 'allowEmpty' => true, |
|
131 | + 'rule' => '/^#?[a-f0-9]{0,6}$/i', |
|
132 | + 'message' => '*' |
|
133 | + ], |
|
134 | + 'user_color_actual_posting' => [ |
|
135 | + 'allowEmpty' => true, |
|
136 | + 'rule' => '/^#?[a-f0-9]{0,6}$/i' |
|
137 | + ], |
|
138 | + 'user_place_lat' => [ |
|
139 | + 'validLatitude' => ['rule' => ['inRange', -90, 90], |
|
140 | + 'allowEmpty' => true], |
|
141 | + ], |
|
142 | + 'user_place_lng' => [ |
|
143 | + 'validLongitude' => ['rule' => ['inRange', -180, 180], |
|
144 | + 'allowEmpty' => true], |
|
145 | + ], |
|
146 | + 'user_place_zoom' => [ |
|
147 | + 'numeric' => ['rule' => ['naturalNumber', 0], 'allowEmpty' => true], |
|
148 | + 'between' => ['rule' => ['inRange', 0, 25]] |
|
149 | + ] |
|
150 | + ]; |
|
151 | + |
|
152 | + public $findMethods = [ |
|
153 | + 'latest' => true |
|
154 | + ]; |
|
155 | + |
|
156 | + protected $_passwordHasher = [ |
|
157 | + 'BlowfishPasswordHasher', |
|
158 | + 'Mlf2PasswordHasher', |
|
159 | + 'MlfPasswordHasher' |
|
160 | + ]; |
|
161 | + |
|
162 | + protected $_settings = [ |
|
163 | + 'user_name_disallowed_chars' => ['\'', ';', '&', '<', '>'] |
|
164 | + ]; |
|
165 | 165 | |
166 | 166 | /** |
167 | 167 | * @param null $lastRefresh |
168 | 168 | * |
169 | 169 | * @throws Exception |
170 | 170 | */ |
171 | - public function setLastRefresh($lastRefresh = null) { |
|
172 | - Stopwatch::start('Users->setLastRefresh()'); |
|
173 | - $data[$this->alias]['last_refresh_tmp'] = bDate(); |
|
174 | - |
|
175 | - if ($lastRefresh) { |
|
176 | - $data[$this->alias]['last_refresh'] = $lastRefresh; |
|
177 | - } |
|
178 | - |
|
179 | - $this->contain(); |
|
180 | - $success = $this->save($data, |
|
181 | - [ |
|
182 | - 'callbacks' => false, |
|
183 | - 'counterCache' => false, |
|
184 | - 'validate' => false, |
|
185 | - 'fieldList' => ['last_refresh_tmp', 'last_refresh'] |
|
186 | - ]); |
|
187 | - if ($success == false) { |
|
188 | - throw new Exception("Updating last user refresh failed."); |
|
189 | - } |
|
190 | - Stopwatch::end('Users->setLastRefresh()'); |
|
191 | - } |
|
192 | - |
|
193 | - public function numberOfEntries() { |
|
194 | - /* |
|
171 | + public function setLastRefresh($lastRefresh = null) { |
|
172 | + Stopwatch::start('Users->setLastRefresh()'); |
|
173 | + $data[$this->alias]['last_refresh_tmp'] = bDate(); |
|
174 | + |
|
175 | + if ($lastRefresh) { |
|
176 | + $data[$this->alias]['last_refresh'] = $lastRefresh; |
|
177 | + } |
|
178 | + |
|
179 | + $this->contain(); |
|
180 | + $success = $this->save($data, |
|
181 | + [ |
|
182 | + 'callbacks' => false, |
|
183 | + 'counterCache' => false, |
|
184 | + 'validate' => false, |
|
185 | + 'fieldList' => ['last_refresh_tmp', 'last_refresh'] |
|
186 | + ]); |
|
187 | + if ($success == false) { |
|
188 | + throw new Exception("Updating last user refresh failed."); |
|
189 | + } |
|
190 | + Stopwatch::end('Users->setLastRefresh()'); |
|
191 | + } |
|
192 | + |
|
193 | + public function numberOfEntries() { |
|
194 | + /* |
|
195 | 195 | # @mlf change after mlf is gone, we only use `entry_count` then |
196 | 196 | $count = $this->data['User']['entry_count']; |
197 | 197 | if ( $count == 0 ) |
198 | 198 | */ { |
199 | - $count = $this->Entry->find('count', |
|
200 | - array( |
|
201 | - 'contain' => false, |
|
202 | - 'conditions' => array('Entry.user_id' => $this->id), |
|
203 | - ) |
|
204 | - ); |
|
205 | - } |
|
206 | - return $count; |
|
207 | - } |
|
208 | - |
|
209 | - public function incrementLogins($id, $amount = 1) { |
|
210 | - $data = [ |
|
211 | - $this->alias => [ |
|
212 | - 'id' => $id, |
|
213 | - 'logins' => $this->field('logins') + $amount, |
|
214 | - 'last_login' => bDate() |
|
215 | - ] |
|
216 | - ]; |
|
217 | - if ($this->save($data, true, ['logins', 'last_login']) == false) { |
|
218 | - throw new Exception('Increment logins failed.'); |
|
219 | - } |
|
220 | - } |
|
221 | - |
|
222 | - public function userlist() { |
|
223 | - return $this->find('list', [ |
|
224 | - 'contain' => false, |
|
225 | - 'fields' => 'username' |
|
226 | - ]); |
|
227 | - } |
|
199 | + $count = $this->Entry->find('count', |
|
200 | + array( |
|
201 | + 'contain' => false, |
|
202 | + 'conditions' => array('Entry.user_id' => $this->id), |
|
203 | + ) |
|
204 | + ); |
|
205 | + } |
|
206 | + return $count; |
|
207 | + } |
|
208 | + |
|
209 | + public function incrementLogins($id, $amount = 1) { |
|
210 | + $data = [ |
|
211 | + $this->alias => [ |
|
212 | + 'id' => $id, |
|
213 | + 'logins' => $this->field('logins') + $amount, |
|
214 | + 'last_login' => bDate() |
|
215 | + ] |
|
216 | + ]; |
|
217 | + if ($this->save($data, true, ['logins', 'last_login']) == false) { |
|
218 | + throw new Exception('Increment logins failed.'); |
|
219 | + } |
|
220 | + } |
|
221 | + |
|
222 | + public function userlist() { |
|
223 | + return $this->find('list', [ |
|
224 | + 'contain' => false, |
|
225 | + 'fields' => 'username' |
|
226 | + ]); |
|
227 | + } |
|
228 | 228 | |
229 | 229 | /** |
230 | 230 | * Removes a user and all his data execpt for his entries |
@@ -232,152 +232,152 @@ discard block |
||
232 | 232 | * @param int $id user-ID |
233 | 233 | * @return boolean |
234 | 234 | */ |
235 | - public function deleteAllExceptEntries($id) { |
|
236 | - if ($id == 1) { |
|
237 | - return false; |
|
238 | - } |
|
239 | - |
|
240 | - $success = true; |
|
241 | - $success = $success && $this->Upload->deleteAllFromUser($id); |
|
242 | - $success = $success && $this->Esnotification->deleteAllFromUser($id); |
|
243 | - $success = $success && $this->Entry->anonymizeEntriesFromUser($id); |
|
244 | - $success = $success && $this->Ignore->deleteUser($id); |
|
245 | - $success = $success && $this->UserOnline->deleteAll( |
|
246 | - ['user_id' => $id], |
|
247 | - false |
|
248 | - ); |
|
249 | - $success = $success && $this->delete($id, true); |
|
250 | - return $success; |
|
251 | - } |
|
252 | - |
|
253 | - public function autoUpdatePassword($id, $password) { |
|
254 | - $this->contain(); |
|
255 | - $data = $this->read(null, $id); |
|
256 | - $oldPassword = $data[$this->alias]['password']; |
|
257 | - $blowfishHashIdentifier = '$2a$'; |
|
258 | - if (strpos($oldPassword, $blowfishHashIdentifier) !== 0): |
|
259 | - $this->saveField('password', $password); |
|
260 | - endif; |
|
261 | - } |
|
262 | - |
|
263 | - public function afterFind($results, $primary = false) { |
|
264 | - $results = parent::afterFind($results, $primary); |
|
265 | - |
|
266 | - if (isset($results[0][$this->alias])) { |
|
267 | - if (array_key_exists('user_color_new_postings', |
|
268 | - $results[0][$this->alias]) |
|
269 | - ) { |
|
270 | - //* @td refactor this shit |
|
271 | - if (empty($results[0][$this->alias]['user_color_new_postings'])) { |
|
272 | - $results[0][$this->alias]['user_color_new_postings'] = '#'; |
|
273 | - $results[0][$this->alias]['user_color_old_postings'] = '#'; |
|
274 | - $results[0][$this->alias]['user_color_actual_posting'] = '#'; |
|
275 | - } |
|
276 | - } |
|
277 | - |
|
278 | - if (isset($results[0][$this->alias]['user_category_custom'])) { |
|
279 | - if (empty($results[0][$this->alias]['user_category_custom'])) { |
|
280 | - $results[0][$this->alias]['user_category_custom'] = []; |
|
281 | - } else { |
|
282 | - $results[0][$this->alias]['user_category_custom'] = |
|
283 | - unserialize($results[0][$this->alias]['user_category_custom']); |
|
284 | - } |
|
285 | - } |
|
286 | - } |
|
287 | - |
|
288 | - return $results; |
|
289 | - } |
|
290 | - |
|
291 | - public function afterSave($created, $options = []) { |
|
292 | - if ($created === false && isset($this->data[$this->alias]['username'])) { |
|
293 | - $this->_dispatchEvent('Cmd.Cache.clear', ['cache' => 'Thread']); |
|
294 | - } |
|
295 | - } |
|
296 | - |
|
297 | - public function beforeSave($options = array()) { |
|
298 | - parent::beforeSave($options); |
|
299 | - if (isset($this->data[$this->alias]['password'])) { |
|
300 | - if (!empty($this->data[$this->alias]['password'])) { |
|
301 | - $this->data[$this->alias]['password'] = $this->_hashPassword($this->data[$this->alias]['password']); |
|
302 | - } |
|
303 | - } |
|
304 | - |
|
305 | - if (isset($this->data[$this->alias]['user_category_custom'])) { |
|
306 | - $this->data[$this->alias]['user_category_custom'] = |
|
307 | - serialize($this->data[$this->alias]['user_category_custom']); |
|
308 | - } |
|
309 | - |
|
310 | - return true; |
|
311 | - } |
|
312 | - |
|
313 | - public function beforeValidate($options = array()) { |
|
314 | - parent::beforeValidate($options); |
|
315 | - |
|
316 | - if (isset($this->data[$this->alias]['user_forum_refresh_time']) |
|
317 | - && empty($this->data[$this->alias]['user_forum_refresh_time'])) { |
|
318 | - $this->data[$this->alias]['user_forum_refresh_time'] = 0; |
|
319 | - } |
|
320 | - } |
|
321 | - |
|
322 | - public function validateCheckOldPassword($data) { |
|
323 | - $this->contain('UserOnline'); |
|
324 | - $oldPw = $this->field('password'); |
|
325 | - return $this->_checkPassword($data['password_old'], $oldPw); |
|
326 | - } |
|
327 | - |
|
328 | - public function validateConfirmPassword($data) { |
|
329 | - $valid = false; |
|
330 | - if (isset($this->data[$this->alias]['password_confirm']) |
|
331 | - && $data['password'] == $this->data[$this->alias]['password_confirm']) { |
|
332 | - $valid = true; |
|
333 | - } |
|
334 | - return $valid; |
|
335 | - } |
|
336 | - |
|
337 | - public function validateHasAllowedChars($data) { |
|
338 | - foreach ($this->_setting('user_name_disallowed_chars') as $char) { |
|
339 | - if (mb_strpos($data['username'], $char) !== false) { |
|
340 | - return false; |
|
341 | - } |
|
342 | - } |
|
343 | - return true; |
|
344 | - } |
|
345 | - |
|
346 | - /** |
|
347 | - * checks if equal username exists |
|
348 | - */ |
|
349 | - public function validateUsernameEqual($data) { |
|
350 | - Stopwatch::start('validateUsernameEqual'); |
|
351 | - $users = $this->userlist(); |
|
352 | - foreach ($users as $name) { |
|
353 | - if ($name === $data['username']) { |
|
354 | - continue; |
|
355 | - } |
|
356 | - $distance = levenshtein($data['username'], $name); |
|
357 | - if ($distance < 2) { |
|
358 | - return __('error.name.equalExists', $name); |
|
359 | - } |
|
360 | - } |
|
361 | - Stopwatch::stop('validateUsernameEqual'); |
|
362 | - return true; |
|
363 | - } |
|
364 | - |
|
365 | - public function paginate($conditions, $fields, $order, $limit, $page = 1, $recursive = null, $extra = array()) { |
|
366 | - $username = $this->alias . '.' . 'username'; |
|
367 | - if (isset($order[$username])) { |
|
368 | - $direction = $order[$username]; |
|
369 | - unset($order[$username]); |
|
370 | - } else { |
|
371 | - $direction = 'asc'; |
|
372 | - } |
|
373 | - $order['LOWER(User.username)'] = $direction; |
|
374 | - |
|
375 | - // merge $extras for 'contain' parameter |
|
376 | - $params = array_merge(compact('conditions', 'fields', 'order', 'limit', |
|
377 | - 'page', 'recursive', 'group'), $extra); |
|
378 | - |
|
379 | - return $this->find('all', $params); |
|
380 | - } |
|
235 | + public function deleteAllExceptEntries($id) { |
|
236 | + if ($id == 1) { |
|
237 | + return false; |
|
238 | + } |
|
239 | + |
|
240 | + $success = true; |
|
241 | + $success = $success && $this->Upload->deleteAllFromUser($id); |
|
242 | + $success = $success && $this->Esnotification->deleteAllFromUser($id); |
|
243 | + $success = $success && $this->Entry->anonymizeEntriesFromUser($id); |
|
244 | + $success = $success && $this->Ignore->deleteUser($id); |
|
245 | + $success = $success && $this->UserOnline->deleteAll( |
|
246 | + ['user_id' => $id], |
|
247 | + false |
|
248 | + ); |
|
249 | + $success = $success && $this->delete($id, true); |
|
250 | + return $success; |
|
251 | + } |
|
252 | + |
|
253 | + public function autoUpdatePassword($id, $password) { |
|
254 | + $this->contain(); |
|
255 | + $data = $this->read(null, $id); |
|
256 | + $oldPassword = $data[$this->alias]['password']; |
|
257 | + $blowfishHashIdentifier = '$2a$'; |
|
258 | + if (strpos($oldPassword, $blowfishHashIdentifier) !== 0): |
|
259 | + $this->saveField('password', $password); |
|
260 | + endif; |
|
261 | + } |
|
262 | + |
|
263 | + public function afterFind($results, $primary = false) { |
|
264 | + $results = parent::afterFind($results, $primary); |
|
265 | + |
|
266 | + if (isset($results[0][$this->alias])) { |
|
267 | + if (array_key_exists('user_color_new_postings', |
|
268 | + $results[0][$this->alias]) |
|
269 | + ) { |
|
270 | + //* @td refactor this shit |
|
271 | + if (empty($results[0][$this->alias]['user_color_new_postings'])) { |
|
272 | + $results[0][$this->alias]['user_color_new_postings'] = '#'; |
|
273 | + $results[0][$this->alias]['user_color_old_postings'] = '#'; |
|
274 | + $results[0][$this->alias]['user_color_actual_posting'] = '#'; |
|
275 | + } |
|
276 | + } |
|
277 | + |
|
278 | + if (isset($results[0][$this->alias]['user_category_custom'])) { |
|
279 | + if (empty($results[0][$this->alias]['user_category_custom'])) { |
|
280 | + $results[0][$this->alias]['user_category_custom'] = []; |
|
281 | + } else { |
|
282 | + $results[0][$this->alias]['user_category_custom'] = |
|
283 | + unserialize($results[0][$this->alias]['user_category_custom']); |
|
284 | + } |
|
285 | + } |
|
286 | + } |
|
287 | + |
|
288 | + return $results; |
|
289 | + } |
|
290 | + |
|
291 | + public function afterSave($created, $options = []) { |
|
292 | + if ($created === false && isset($this->data[$this->alias]['username'])) { |
|
293 | + $this->_dispatchEvent('Cmd.Cache.clear', ['cache' => 'Thread']); |
|
294 | + } |
|
295 | + } |
|
296 | + |
|
297 | + public function beforeSave($options = array()) { |
|
298 | + parent::beforeSave($options); |
|
299 | + if (isset($this->data[$this->alias]['password'])) { |
|
300 | + if (!empty($this->data[$this->alias]['password'])) { |
|
301 | + $this->data[$this->alias]['password'] = $this->_hashPassword($this->data[$this->alias]['password']); |
|
302 | + } |
|
303 | + } |
|
304 | + |
|
305 | + if (isset($this->data[$this->alias]['user_category_custom'])) { |
|
306 | + $this->data[$this->alias]['user_category_custom'] = |
|
307 | + serialize($this->data[$this->alias]['user_category_custom']); |
|
308 | + } |
|
309 | + |
|
310 | + return true; |
|
311 | + } |
|
312 | + |
|
313 | + public function beforeValidate($options = array()) { |
|
314 | + parent::beforeValidate($options); |
|
315 | + |
|
316 | + if (isset($this->data[$this->alias]['user_forum_refresh_time']) |
|
317 | + && empty($this->data[$this->alias]['user_forum_refresh_time'])) { |
|
318 | + $this->data[$this->alias]['user_forum_refresh_time'] = 0; |
|
319 | + } |
|
320 | + } |
|
321 | + |
|
322 | + public function validateCheckOldPassword($data) { |
|
323 | + $this->contain('UserOnline'); |
|
324 | + $oldPw = $this->field('password'); |
|
325 | + return $this->_checkPassword($data['password_old'], $oldPw); |
|
326 | + } |
|
327 | + |
|
328 | + public function validateConfirmPassword($data) { |
|
329 | + $valid = false; |
|
330 | + if (isset($this->data[$this->alias]['password_confirm']) |
|
331 | + && $data['password'] == $this->data[$this->alias]['password_confirm']) { |
|
332 | + $valid = true; |
|
333 | + } |
|
334 | + return $valid; |
|
335 | + } |
|
336 | + |
|
337 | + public function validateHasAllowedChars($data) { |
|
338 | + foreach ($this->_setting('user_name_disallowed_chars') as $char) { |
|
339 | + if (mb_strpos($data['username'], $char) !== false) { |
|
340 | + return false; |
|
341 | + } |
|
342 | + } |
|
343 | + return true; |
|
344 | + } |
|
345 | + |
|
346 | + /** |
|
347 | + * checks if equal username exists |
|
348 | + */ |
|
349 | + public function validateUsernameEqual($data) { |
|
350 | + Stopwatch::start('validateUsernameEqual'); |
|
351 | + $users = $this->userlist(); |
|
352 | + foreach ($users as $name) { |
|
353 | + if ($name === $data['username']) { |
|
354 | + continue; |
|
355 | + } |
|
356 | + $distance = levenshtein($data['username'], $name); |
|
357 | + if ($distance < 2) { |
|
358 | + return __('error.name.equalExists', $name); |
|
359 | + } |
|
360 | + } |
|
361 | + Stopwatch::stop('validateUsernameEqual'); |
|
362 | + return true; |
|
363 | + } |
|
364 | + |
|
365 | + public function paginate($conditions, $fields, $order, $limit, $page = 1, $recursive = null, $extra = array()) { |
|
366 | + $username = $this->alias . '.' . 'username'; |
|
367 | + if (isset($order[$username])) { |
|
368 | + $direction = $order[$username]; |
|
369 | + unset($order[$username]); |
|
370 | + } else { |
|
371 | + $direction = 'asc'; |
|
372 | + } |
|
373 | + $order['LOWER(User.username)'] = $direction; |
|
374 | + |
|
375 | + // merge $extras for 'contain' parameter |
|
376 | + $params = array_merge(compact('conditions', 'fields', 'order', 'limit', |
|
377 | + 'page', 'recursive', 'group'), $extra); |
|
378 | + |
|
379 | + return $this->find('all', $params); |
|
380 | + } |
|
381 | 381 | |
382 | 382 | /** |
383 | 383 | * Registers new user |
@@ -385,141 +385,141 @@ discard block |
||
385 | 385 | * @param array $data |
386 | 386 | * @return bool true if user got registred false otherwise |
387 | 387 | */ |
388 | - public function register($data, $activate = false) { |
|
389 | - $defaults = [ |
|
390 | - 'registered' => bDate(), |
|
391 | - 'user_type' => 'user' |
|
392 | - ]; |
|
393 | - $fields = ['registered', 'username', |
|
394 | - 'user_email', 'password', 'user_type']; |
|
395 | - |
|
396 | - if ($activate !== true) { |
|
397 | - $defaults['activate_code'] = mt_rand(1000000, 9999999); |
|
398 | - $fields[] = 'activate_code'; |
|
399 | - } |
|
400 | - |
|
401 | - $data = array_merge($data[$this->alias], $defaults); |
|
402 | - |
|
403 | - if (!$this->requireFields($data, $fields) || !$this->unsetFields($data)) { |
|
404 | - return false; |
|
405 | - } |
|
406 | - |
|
407 | - $this->create(); |
|
408 | - $user = $this->save($data, true, $fields); |
|
409 | - if (empty($user)) { |
|
410 | - return false; |
|
411 | - } |
|
412 | - $user['User']['id'] = $this->id; |
|
413 | - return $user; |
|
414 | - } |
|
388 | + public function register($data, $activate = false) { |
|
389 | + $defaults = [ |
|
390 | + 'registered' => bDate(), |
|
391 | + 'user_type' => 'user' |
|
392 | + ]; |
|
393 | + $fields = ['registered', 'username', |
|
394 | + 'user_email', 'password', 'user_type']; |
|
395 | + |
|
396 | + if ($activate !== true) { |
|
397 | + $defaults['activate_code'] = mt_rand(1000000, 9999999); |
|
398 | + $fields[] = 'activate_code'; |
|
399 | + } |
|
400 | + |
|
401 | + $data = array_merge($data[$this->alias], $defaults); |
|
402 | + |
|
403 | + if (!$this->requireFields($data, $fields) || !$this->unsetFields($data)) { |
|
404 | + return false; |
|
405 | + } |
|
406 | + |
|
407 | + $this->create(); |
|
408 | + $user = $this->save($data, true, $fields); |
|
409 | + if (empty($user)) { |
|
410 | + return false; |
|
411 | + } |
|
412 | + $user['User']['id'] = $this->id; |
|
413 | + return $user; |
|
414 | + } |
|
415 | 415 | |
416 | 416 | /** |
417 | 417 | * Garbage collection for registration |
418 | 418 | * |
419 | 419 | * Deletes all timed out and unactivated registrations |
420 | 420 | */ |
421 | - public function registerGc() { |
|
422 | - $this->deleteAll([ |
|
423 | - 'activate_code >' => 0, |
|
424 | - 'registered <' => bDate(time() - 86400) |
|
425 | - ], |
|
426 | - false); |
|
427 | - } |
|
428 | - |
|
429 | - /** |
|
430 | - * calls garbage collection for UserBlock |
|
431 | - * |
|
432 | - * UserBlock is lazy-loaded rarely and gc may not trigger often enough (at |
|
433 | - * least with manual blocking and ignore blocking only) |
|
434 | - */ |
|
435 | - public function userBlockGc() { |
|
436 | - $this->UserBlock->gc(); |
|
437 | - } |
|
438 | - |
|
439 | - /** |
|
440 | - * activates user |
|
441 | - * |
|
442 | - * @param $id user-ID |
|
443 | - * @param $code activation code |
|
444 | - * @return array|bool false if activation failed; array with status and user data on success |
|
445 | - * @throws InvalidArgumentException |
|
446 | - */ |
|
447 | - public function activate($id, $code) { |
|
448 | - if (!is_int($id) || !is_string($code)) { |
|
449 | - throw new InvalidArgumentException(); |
|
450 | - } |
|
451 | - |
|
452 | - $user = $this->find('first', [ |
|
453 | - 'contain' => false, |
|
454 | - 'conditions' => ['id' => $id] |
|
455 | - ]); |
|
456 | - if (empty($user)) { |
|
457 | - throw new InvalidArgumentException(); |
|
458 | - } |
|
459 | - |
|
460 | - $user = $user[$this->alias]; |
|
461 | - $activateCode = strval($user['activate_code']); |
|
462 | - |
|
463 | - if (empty($activateCode)) { |
|
464 | - return ['status' => 'already', 'User' => $user]; |
|
465 | - } elseif ($activateCode !== $code) { |
|
466 | - return false; |
|
467 | - } |
|
468 | - |
|
469 | - $success = $this->save(['id' => $id, 'activate_code' => 0]); |
|
470 | - if (empty($success)) { |
|
471 | - return false; |
|
472 | - } |
|
473 | - $user['activate_code'] = 0; |
|
474 | - |
|
475 | - $this->_dispatchEvent('Model.User.afterActivate', ['User' => $user]); |
|
476 | - |
|
477 | - return ['status' => 'activated', 'User' => $user]; |
|
478 | - } |
|
421 | + public function registerGc() { |
|
422 | + $this->deleteAll([ |
|
423 | + 'activate_code >' => 0, |
|
424 | + 'registered <' => bDate(time() - 86400) |
|
425 | + ], |
|
426 | + false); |
|
427 | + } |
|
428 | + |
|
429 | + /** |
|
430 | + * calls garbage collection for UserBlock |
|
431 | + * |
|
432 | + * UserBlock is lazy-loaded rarely and gc may not trigger often enough (at |
|
433 | + * least with manual blocking and ignore blocking only) |
|
434 | + */ |
|
435 | + public function userBlockGc() { |
|
436 | + $this->UserBlock->gc(); |
|
437 | + } |
|
438 | + |
|
439 | + /** |
|
440 | + * activates user |
|
441 | + * |
|
442 | + * @param $id user-ID |
|
443 | + * @param $code activation code |
|
444 | + * @return array|bool false if activation failed; array with status and user data on success |
|
445 | + * @throws InvalidArgumentException |
|
446 | + */ |
|
447 | + public function activate($id, $code) { |
|
448 | + if (!is_int($id) || !is_string($code)) { |
|
449 | + throw new InvalidArgumentException(); |
|
450 | + } |
|
451 | + |
|
452 | + $user = $this->find('first', [ |
|
453 | + 'contain' => false, |
|
454 | + 'conditions' => ['id' => $id] |
|
455 | + ]); |
|
456 | + if (empty($user)) { |
|
457 | + throw new InvalidArgumentException(); |
|
458 | + } |
|
459 | + |
|
460 | + $user = $user[$this->alias]; |
|
461 | + $activateCode = strval($user['activate_code']); |
|
462 | + |
|
463 | + if (empty($activateCode)) { |
|
464 | + return ['status' => 'already', 'User' => $user]; |
|
465 | + } elseif ($activateCode !== $code) { |
|
466 | + return false; |
|
467 | + } |
|
468 | + |
|
469 | + $success = $this->save(['id' => $id, 'activate_code' => 0]); |
|
470 | + if (empty($success)) { |
|
471 | + return false; |
|
472 | + } |
|
473 | + $user['activate_code'] = 0; |
|
474 | + |
|
475 | + $this->_dispatchEvent('Model.User.afterActivate', ['User' => $user]); |
|
476 | + |
|
477 | + return ['status' => 'activated', 'User' => $user]; |
|
478 | + } |
|
479 | 479 | |
480 | 480 | /** |
481 | 481 | * |
482 | 482 | * @param int $id user-id |
483 | 483 | * @return bool|array false if not found, array otherwise |
484 | 484 | */ |
485 | - public function getProfile($id) { |
|
486 | - // @perf Ignore is currently retrieved via second query, consider moving |
|
487 | - // it as cache into user-table |
|
488 | - $user = $this->find( |
|
489 | - 'first', |
|
490 | - ['contain' => ['Ignore'], 'conditions' => ['id' => $id]] |
|
491 | - ); |
|
492 | - if ($user) { |
|
493 | - $user = $user[$this->alias] + [ |
|
494 | - 'ignores' => array_fill_keys(Hash::extract($user, 'Ignore.{n}.blocked_user_id'), 1) |
|
495 | - ]; |
|
496 | - } |
|
497 | - return $user; |
|
498 | - } |
|
499 | - |
|
500 | - public function countSolved($id) { |
|
501 | - $count = $this->Entry->find('count', |
|
502 | - [ |
|
503 | - 'contain' => false, |
|
504 | - 'conditions' => [ |
|
505 | - 'Entry.user_id' => $id, |
|
506 | - 'Entry.solves >' => '0' |
|
507 | - ], |
|
508 | - // only count if user is not thread starter/don't count self-answers |
|
509 | - 'joins' => [ |
|
510 | - [ |
|
511 | - 'table' => $this->Entry->table, |
|
512 | - 'alias' => 'Root', |
|
513 | - 'type' => 'INNER', |
|
514 | - 'conditions' => [ |
|
515 | - 'Root.id = Entry.solves', |
|
516 | - 'Root.user_id != Entry.user_id' |
|
517 | - ] |
|
518 | - ] |
|
519 | - ] |
|
520 | - ]); |
|
521 | - return $count; |
|
522 | - } |
|
485 | + public function getProfile($id) { |
|
486 | + // @perf Ignore is currently retrieved via second query, consider moving |
|
487 | + // it as cache into user-table |
|
488 | + $user = $this->find( |
|
489 | + 'first', |
|
490 | + ['contain' => ['Ignore'], 'conditions' => ['id' => $id]] |
|
491 | + ); |
|
492 | + if ($user) { |
|
493 | + $user = $user[$this->alias] + [ |
|
494 | + 'ignores' => array_fill_keys(Hash::extract($user, 'Ignore.{n}.blocked_user_id'), 1) |
|
495 | + ]; |
|
496 | + } |
|
497 | + return $user; |
|
498 | + } |
|
499 | + |
|
500 | + public function countSolved($id) { |
|
501 | + $count = $this->Entry->find('count', |
|
502 | + [ |
|
503 | + 'contain' => false, |
|
504 | + 'conditions' => [ |
|
505 | + 'Entry.user_id' => $id, |
|
506 | + 'Entry.solves >' => '0' |
|
507 | + ], |
|
508 | + // only count if user is not thread starter/don't count self-answers |
|
509 | + 'joins' => [ |
|
510 | + [ |
|
511 | + 'table' => $this->Entry->table, |
|
512 | + 'alias' => 'Root', |
|
513 | + 'type' => 'INNER', |
|
514 | + 'conditions' => [ |
|
515 | + 'Root.id = Entry.solves', |
|
516 | + 'Root.user_id != Entry.user_id' |
|
517 | + ] |
|
518 | + ] |
|
519 | + ] |
|
520 | + ]); |
|
521 | + return $count; |
|
522 | + } |
|
523 | 523 | |
524 | 524 | /** |
525 | 525 | * Set view categories preferences |
@@ -533,35 +533,35 @@ discard block |
||
533 | 533 | * @param string|int|array $category |
534 | 534 | * @throws InvalidArgumentException |
535 | 535 | */ |
536 | - public function setCategory($category) { |
|
537 | - if ($category === 'all') { |
|
538 | - // set meta category 'all' |
|
539 | - $this->set('user_category_active', -1); |
|
540 | - $this->save(); |
|
541 | - } elseif (is_array($category)) { |
|
542 | - // set custom set |
|
543 | - $availableCats = $this->Entry->Category->find('list'); |
|
544 | - $categories = array_intersect_key($category, $availableCats); |
|
545 | - if (count($categories) === 0) { |
|
546 | - throw new InvalidArgumentException(); |
|
547 | - } |
|
548 | - $newCats = []; |
|
549 | - foreach ($categories as $cat => $v) { |
|
550 | - $newCats[$cat] = ($v === true || $v === 1 || $v === '1'); |
|
551 | - } |
|
552 | - $this->set('user_category_active', 0); |
|
553 | - $this->set('user_category_custom', $newCats); |
|
554 | - $this->save(); |
|
555 | - } else { |
|
556 | - $category = (int)$category; |
|
557 | - if ($category > 0 && $this->Entry->Category->exists((int)$category)) { |
|
558 | - $this->set('user_category_active', $category); |
|
559 | - $this->save(); |
|
560 | - } else { |
|
561 | - throw new InvalidArgumentException(); |
|
562 | - } |
|
563 | - } |
|
564 | - } |
|
536 | + public function setCategory($category) { |
|
537 | + if ($category === 'all') { |
|
538 | + // set meta category 'all' |
|
539 | + $this->set('user_category_active', -1); |
|
540 | + $this->save(); |
|
541 | + } elseif (is_array($category)) { |
|
542 | + // set custom set |
|
543 | + $availableCats = $this->Entry->Category->find('list'); |
|
544 | + $categories = array_intersect_key($category, $availableCats); |
|
545 | + if (count($categories) === 0) { |
|
546 | + throw new InvalidArgumentException(); |
|
547 | + } |
|
548 | + $newCats = []; |
|
549 | + foreach ($categories as $cat => $v) { |
|
550 | + $newCats[$cat] = ($v === true || $v === 1 || $v === '1'); |
|
551 | + } |
|
552 | + $this->set('user_category_active', 0); |
|
553 | + $this->set('user_category_custom', $newCats); |
|
554 | + $this->save(); |
|
555 | + } else { |
|
556 | + $category = (int)$category; |
|
557 | + if ($category > 0 && $this->Entry->Category->exists((int)$category)) { |
|
558 | + $this->set('user_category_active', $category); |
|
559 | + $this->save(); |
|
560 | + } else { |
|
561 | + throw new InvalidArgumentException(); |
|
562 | + } |
|
563 | + } |
|
564 | + } |
|
565 | 565 | |
566 | 566 | /** |
567 | 567 | * Checks if password is valid against all supported auth methods |
@@ -570,18 +570,18 @@ discard block |
||
570 | 570 | * @param string $hash |
571 | 571 | * @return boolean TRUE if password match FALSE otherwise |
572 | 572 | */ |
573 | - protected function _checkPassword($password, $hash) { |
|
574 | - $valid = false; |
|
575 | - foreach ($this->_passwordHasher as $passwordHasher) { |
|
576 | - $PasswordHasherInstance = new $passwordHasher(); |
|
577 | - // @: if hash is not valid hash blowfish Security::_crypt() triggers warnings |
|
578 | - if (@$PasswordHasherInstance->check($password, $hash)) { |
|
579 | - $valid = true; |
|
580 | - break; |
|
581 | - } |
|
582 | - } |
|
583 | - return $valid; |
|
584 | - } |
|
573 | + protected function _checkPassword($password, $hash) { |
|
574 | + $valid = false; |
|
575 | + foreach ($this->_passwordHasher as $passwordHasher) { |
|
576 | + $PasswordHasherInstance = new $passwordHasher(); |
|
577 | + // @: if hash is not valid hash blowfish Security::_crypt() triggers warnings |
|
578 | + if (@$PasswordHasherInstance->check($password, $hash)) { |
|
579 | + $valid = true; |
|
580 | + break; |
|
581 | + } |
|
582 | + } |
|
583 | + return $valid; |
|
584 | + } |
|
585 | 585 | |
586 | 586 | /** |
587 | 587 | * Custom hash function used for authentication with Auth component |
@@ -589,26 +589,26 @@ discard block |
||
589 | 589 | * @param string $password |
590 | 590 | * @return string hashed password |
591 | 591 | */ |
592 | - protected function _hashPassword($password) { |
|
593 | - $auth = new BlowfishPasswordHasher(); |
|
594 | - return $auth->hash($password); |
|
595 | - } |
|
596 | - |
|
597 | - /** |
|
598 | - * Find the latest, successfully registered user |
|
599 | - */ |
|
600 | - protected function _findLatest($state, $query, $results = []) { |
|
601 | - if ($state === 'before') { |
|
602 | - $query['contain'] = false; |
|
603 | - $query['limit'] = 1; |
|
604 | - $query['conditions'][$this->alias . '.activate_code'] = 0; |
|
605 | - $query['order'] = $this->alias . '.id DESC'; |
|
606 | - return $query; |
|
607 | - } |
|
608 | - if (empty($results[0])) { |
|
609 | - return []; |
|
610 | - } |
|
611 | - return $results[0]; |
|
612 | - } |
|
613 | - |
|
614 | - } |
|
592 | + protected function _hashPassword($password) { |
|
593 | + $auth = new BlowfishPasswordHasher(); |
|
594 | + return $auth->hash($password); |
|
595 | + } |
|
596 | + |
|
597 | + /** |
|
598 | + * Find the latest, successfully registered user |
|
599 | + */ |
|
600 | + protected function _findLatest($state, $query, $results = []) { |
|
601 | + if ($state === 'before') { |
|
602 | + $query['contain'] = false; |
|
603 | + $query['limit'] = 1; |
|
604 | + $query['conditions'][$this->alias . '.activate_code'] = 0; |
|
605 | + $query['order'] = $this->alias . '.id DESC'; |
|
606 | + return $query; |
|
607 | + } |
|
608 | + if (empty($results[0])) { |
|
609 | + return []; |
|
610 | + } |
|
611 | + return $results[0]; |
|
612 | + } |
|
613 | + |
|
614 | + } |
@@ -73,7 +73,7 @@ |
||
73 | 73 | 1420485052 |
74 | 74 | ); |
75 | 75 | } |
76 | - $success = (bool)$this->save([ |
|
76 | + $success = (bool) $this->save([ |
|
77 | 77 | 'id' => $id, |
78 | 78 | 'ended' => bDate(), |
79 | 79 | 'ends' => null |
@@ -1,147 +1,147 @@ |
||
1 | 1 | <?php |
2 | 2 | |
3 | - App::uses('AppModel', 'Model'); |
|
4 | - App::import('Lib', 'UserBlocker'); |
|
3 | + App::uses('AppModel', 'Model'); |
|
4 | + App::import('Lib', 'UserBlocker'); |
|
5 | 5 | |
6 | - /** |
|
7 | - * UserBlock Model |
|
8 | - * |
|
9 | - * states |
|
10 | - */ |
|
11 | - class UserBlock extends AppModel { |
|
6 | + /** |
|
7 | + * UserBlock Model |
|
8 | + * |
|
9 | + * states |
|
10 | + */ |
|
11 | + class UserBlock extends AppModel { |
|
12 | 12 | |
13 | - public $actsAs = [ |
|
14 | - 'Containable' |
|
15 | - ]; |
|
13 | + public $actsAs = [ |
|
14 | + 'Containable' |
|
15 | + ]; |
|
16 | 16 | |
17 | - public $belongsTo = [ |
|
18 | - 'User' => [ |
|
19 | - 'className' => 'User', |
|
20 | - 'fields' => ['id', 'username'], |
|
21 | - 'foreignKey' => 'user_id' |
|
22 | - ], |
|
23 | - 'By' => [ |
|
24 | - 'className' => 'User', |
|
25 | - 'fields' => ['id', 'username'], |
|
26 | - 'foreignKey' => 'blocked_by_user_id' |
|
27 | - ] |
|
28 | - ]; |
|
17 | + public $belongsTo = [ |
|
18 | + 'User' => [ |
|
19 | + 'className' => 'User', |
|
20 | + 'fields' => ['id', 'username'], |
|
21 | + 'foreignKey' => 'user_id' |
|
22 | + ], |
|
23 | + 'By' => [ |
|
24 | + 'className' => 'User', |
|
25 | + 'fields' => ['id', 'username'], |
|
26 | + 'foreignKey' => 'blocked_by_user_id' |
|
27 | + ] |
|
28 | + ]; |
|
29 | 29 | |
30 | - public $findMethods = [ |
|
31 | - 'toGc' => true |
|
32 | - ]; |
|
30 | + public $findMethods = [ |
|
31 | + 'toGc' => true |
|
32 | + ]; |
|
33 | 33 | |
34 | - public $validate = [ |
|
35 | - 'ends' => [ |
|
36 | - 'rule' => ['datetime'], |
|
37 | - 'allowEmpty' => true |
|
38 | - ] |
|
39 | - ]; |
|
34 | + public $validate = [ |
|
35 | + 'ends' => [ |
|
36 | + 'rule' => ['datetime'], |
|
37 | + 'allowEmpty' => true |
|
38 | + ] |
|
39 | + ]; |
|
40 | 40 | |
41 | - public function block($Blocker, $userId, $options) { |
|
42 | - $Blocker->setUserBlockModel($this); |
|
43 | - $success = $Blocker->block($userId, $options); |
|
44 | - if ($success) { |
|
45 | - $this->_updateIsBlocked($userId); |
|
46 | - } |
|
47 | - return $success; |
|
48 | - } |
|
41 | + public function block($Blocker, $userId, $options) { |
|
42 | + $Blocker->setUserBlockModel($this); |
|
43 | + $success = $Blocker->block($userId, $options); |
|
44 | + if ($success) { |
|
45 | + $this->_updateIsBlocked($userId); |
|
46 | + } |
|
47 | + return $success; |
|
48 | + } |
|
49 | 49 | |
50 | - public function getBlockEndsForUser($userId) { |
|
51 | - $block = $this->find('first', [ |
|
52 | - 'contain' => false, |
|
53 | - 'conditions' => ['user_id' => $userId, 'ended' => null], |
|
54 | - 'order' => ['ends' => 'asc'] |
|
55 | - ]); |
|
56 | - return $block['UserBlock']['ends']; |
|
57 | - } |
|
50 | + public function getBlockEndsForUser($userId) { |
|
51 | + $block = $this->find('first', [ |
|
52 | + 'contain' => false, |
|
53 | + 'conditions' => ['user_id' => $userId, 'ended' => null], |
|
54 | + 'order' => ['ends' => 'asc'] |
|
55 | + ]); |
|
56 | + return $block['UserBlock']['ends']; |
|
57 | + } |
|
58 | 58 | |
59 | - /** |
|
60 | - * @param $id |
|
61 | - * @return bool true if unblocking was successful, false otherwise |
|
62 | - * @throws RuntimeException |
|
63 | - * @throws InvalidArgumentException |
|
64 | - */ |
|
65 | - public function unblock($id) { |
|
66 | - $block = $this->find('first', [ |
|
67 | - 'contain' => false, |
|
68 | - 'conditions' => ['id' => $id, 'ended' => null] |
|
69 | - ]); |
|
70 | - if (empty($block)) { |
|
71 | - throw new InvalidArgumentException( |
|
72 | - "No active block with id $id found.", |
|
73 | - 1420485052 |
|
74 | - ); |
|
75 | - } |
|
76 | - $success = (bool)$this->save([ |
|
77 | - 'id' => $id, |
|
78 | - 'ended' => bDate(), |
|
79 | - 'ends' => null |
|
80 | - ]); |
|
81 | - if (!$success) { |
|
82 | - throw new RuntimeException; |
|
83 | - } |
|
84 | - $this->_updateIsBlocked($block[$this->alias]['user_id']); |
|
85 | - return $success; |
|
86 | - } |
|
59 | + /** |
|
60 | + * @param $id |
|
61 | + * @return bool true if unblocking was successful, false otherwise |
|
62 | + * @throws RuntimeException |
|
63 | + * @throws InvalidArgumentException |
|
64 | + */ |
|
65 | + public function unblock($id) { |
|
66 | + $block = $this->find('first', [ |
|
67 | + 'contain' => false, |
|
68 | + 'conditions' => ['id' => $id, 'ended' => null] |
|
69 | + ]); |
|
70 | + if (empty($block)) { |
|
71 | + throw new InvalidArgumentException( |
|
72 | + "No active block with id $id found.", |
|
73 | + 1420485052 |
|
74 | + ); |
|
75 | + } |
|
76 | + $success = (bool)$this->save([ |
|
77 | + 'id' => $id, |
|
78 | + 'ended' => bDate(), |
|
79 | + 'ends' => null |
|
80 | + ]); |
|
81 | + if (!$success) { |
|
82 | + throw new RuntimeException; |
|
83 | + } |
|
84 | + $this->_updateIsBlocked($block[$this->alias]['user_id']); |
|
85 | + return $success; |
|
86 | + } |
|
87 | 87 | |
88 | - /** |
|
89 | - * Garbage collection |
|
90 | - * |
|
91 | - * called hourly from User model |
|
92 | - */ |
|
93 | - public function gc() { |
|
94 | - $expired = $this->find('toGc'); |
|
95 | - foreach ($expired as $block) { |
|
96 | - $this->unblock($block[$this->alias]['id']); |
|
97 | - } |
|
98 | - } |
|
88 | + /** |
|
89 | + * Garbage collection |
|
90 | + * |
|
91 | + * called hourly from User model |
|
92 | + */ |
|
93 | + public function gc() { |
|
94 | + $expired = $this->find('toGc'); |
|
95 | + foreach ($expired as $block) { |
|
96 | + $this->unblock($block[$this->alias]['id']); |
|
97 | + } |
|
98 | + } |
|
99 | 99 | |
100 | - public function getAll() { |
|
101 | - $blocklist = $this->find('all', [ |
|
102 | - 'contain' => ['By', 'User'], |
|
103 | - 'order' => ['UserBlock.id' => 'DESC'] |
|
104 | - ]); |
|
105 | - $o = []; |
|
106 | - foreach ($blocklist as $k => $b) { |
|
107 | - $o[$k] = $b['UserBlock']; |
|
108 | - $o[$k]['By'] = $b['By']; |
|
109 | - $o[$k]['User'] = $b['User']; |
|
110 | - } |
|
111 | - return $o; |
|
112 | - } |
|
100 | + public function getAll() { |
|
101 | + $blocklist = $this->find('all', [ |
|
102 | + 'contain' => ['By', 'User'], |
|
103 | + 'order' => ['UserBlock.id' => 'DESC'] |
|
104 | + ]); |
|
105 | + $o = []; |
|
106 | + foreach ($blocklist as $k => $b) { |
|
107 | + $o[$k] = $b['UserBlock']; |
|
108 | + $o[$k]['By'] = $b['By']; |
|
109 | + $o[$k]['User'] = $b['User']; |
|
110 | + } |
|
111 | + return $o; |
|
112 | + } |
|
113 | 113 | |
114 | - public function getAllActive() { |
|
115 | - $blocklist = $this->find('all', [ |
|
116 | - 'contain' => false, |
|
117 | - 'conditions' => ['ended' => null], |
|
118 | - 'order' => ['UserBlock.id' => 'DESC'] |
|
119 | - ]); |
|
120 | - return $blocklist; |
|
121 | - } |
|
114 | + public function getAllActive() { |
|
115 | + $blocklist = $this->find('all', [ |
|
116 | + 'contain' => false, |
|
117 | + 'conditions' => ['ended' => null], |
|
118 | + 'order' => ['UserBlock.id' => 'DESC'] |
|
119 | + ]); |
|
120 | + return $blocklist; |
|
121 | + } |
|
122 | 122 | |
123 | - protected function _findToGc($state, $query, $results = array()) { |
|
124 | - if ($state === 'before') { |
|
125 | - $query['contain'] = false; |
|
126 | - $query['conditions'] = [ |
|
127 | - 'ends !=' => null, |
|
128 | - 'ends <' => bDate(), |
|
129 | - 'ended' => null |
|
130 | - ]; |
|
131 | - return $query; |
|
132 | - } |
|
133 | - return $results; |
|
134 | - } |
|
123 | + protected function _findToGc($state, $query, $results = array()) { |
|
124 | + if ($state === 'before') { |
|
125 | + $query['contain'] = false; |
|
126 | + $query['conditions'] = [ |
|
127 | + 'ends !=' => null, |
|
128 | + 'ends <' => bDate(), |
|
129 | + 'ended' => null |
|
130 | + ]; |
|
131 | + return $query; |
|
132 | + } |
|
133 | + return $results; |
|
134 | + } |
|
135 | 135 | |
136 | - protected function _updateIsBlocked($userId) { |
|
137 | - $blocks = $this->find('first', [ |
|
138 | - 'contain' => false, |
|
139 | - 'conditions' => [ |
|
140 | - 'ended' => null, |
|
141 | - 'user_id' => $userId |
|
142 | - ] |
|
143 | - ]); |
|
144 | - $this->User->save(['id' => $userId, 'user_lock' => !empty($blocks)]); |
|
145 | - } |
|
136 | + protected function _updateIsBlocked($userId) { |
|
137 | + $blocks = $this->find('first', [ |
|
138 | + 'contain' => false, |
|
139 | + 'conditions' => [ |
|
140 | + 'ended' => null, |
|
141 | + 'user_id' => $userId |
|
142 | + ] |
|
143 | + ]); |
|
144 | + $this->User->save(['id' => $userId, 'user_lock' => !empty($blocks)]); |
|
145 | + } |
|
146 | 146 | |
147 | - } |
|
147 | + } |