@@ -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 |
@@ -1,291 +1,291 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | - App::uses('Model', 'Model'); |
|
| 4 | - App::uses('Sanitize', 'Utility'); |
|
| 5 | - App::uses('CakeEvent', 'Event'); |
|
| 6 | - App::uses('SaitoEventManager', 'Lib/Saito/Event'); |
|
| 3 | + App::uses('Model', 'Model'); |
|
| 4 | + App::uses('Sanitize', 'Utility'); |
|
| 5 | + App::uses('CakeEvent', 'Event'); |
|
| 6 | + App::uses('SaitoEventManager', 'Lib/Saito/Event'); |
|
| 7 | 7 | |
| 8 | - // import here so that `cake schema ...` cli works |
|
| 9 | - App::import('Lib', 'Stopwatch.Stopwatch'); |
|
| 8 | + // import here so that `cake schema ...` cli works |
|
| 9 | + App::import('Lib', 'Stopwatch.Stopwatch'); |
|
| 10 | 10 | |
| 11 | - class AppModel extends Model { |
|
| 11 | + class AppModel extends Model { |
|
| 12 | 12 | |
| 13 | - /** |
|
| 14 | - * @var array model settings; can be overwritten by DB or config Settings |
|
| 15 | - */ |
|
| 16 | - protected $_settings = []; |
|
| 13 | + /** |
|
| 14 | + * @var array model settings; can be overwritten by DB or config Settings |
|
| 15 | + */ |
|
| 16 | + protected $_settings = []; |
|
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * @var array predefined fields for filterFields() |
|
| 20 | - */ |
|
| 21 | - public $allowedInputFields = []; |
|
| 18 | + /** |
|
| 19 | + * @var array predefined fields for filterFields() |
|
| 20 | + */ |
|
| 21 | + public $allowedInputFields = []; |
|
| 22 | 22 | |
| 23 | - # Entry->User->UserOnline |
|
| 24 | - public $recursive = 1; |
|
| 23 | + # Entry->User->UserOnline |
|
| 24 | + public $recursive = 1; |
|
| 25 | 25 | |
| 26 | - public $SharedObjects; |
|
| 26 | + public $SharedObjects; |
|
| 27 | 27 | |
| 28 | - /** * @var SaitoEventManager */ |
|
| 29 | - protected $_SEM; |
|
| 28 | + /** * @var SaitoEventManager */ |
|
| 29 | + protected $_SEM; |
|
| 30 | 30 | |
| 31 | - public function __construct($id = false, $table = null, $ds = null) { |
|
| 32 | - parent::__construct($id, $table, $ds); |
|
| 33 | - $this->_dispatchEvent('Event.Saito.Model.initialize'); |
|
| 34 | - } |
|
| 31 | + public function __construct($id = false, $table = null, $ds = null) { |
|
| 32 | + parent::__construct($id, $table, $ds); |
|
| 33 | + $this->_dispatchEvent('Event.Saito.Model.initialize'); |
|
| 34 | + } |
|
| 35 | 35 | |
| 36 | - public function __get($name) { |
|
| 37 | - switch ($name) { |
|
| 38 | - case 'dic': |
|
| 39 | - return ClassRegistry::getObject('dic'); |
|
| 40 | - default: |
|
| 41 | - if (isset($this->SharedObjects[$name])) { |
|
| 42 | - return $this->SharedObjects[$name]; |
|
| 43 | - } |
|
| 44 | - } |
|
| 45 | - return parent::__get($name); |
|
| 46 | - } |
|
| 36 | + public function __get($name) { |
|
| 37 | + switch ($name) { |
|
| 38 | + case 'dic': |
|
| 39 | + return ClassRegistry::getObject('dic'); |
|
| 40 | + default: |
|
| 41 | + if (isset($this->SharedObjects[$name])) { |
|
| 42 | + return $this->SharedObjects[$name]; |
|
| 43 | + } |
|
| 44 | + } |
|
| 45 | + return parent::__get($name); |
|
| 46 | + } |
|
| 47 | 47 | |
| 48 | - public function toggle($key) { |
|
| 49 | - $this->contain(); |
|
| 50 | - $value = $this->read($key); |
|
| 51 | - $value = ($value[$this->alias][$key] == 0) ? 1 : 0; |
|
| 52 | - $this->set($key, $value); |
|
| 53 | - $this->save(); |
|
| 54 | - return $value; |
|
| 55 | - } |
|
| 48 | + public function toggle($key) { |
|
| 49 | + $this->contain(); |
|
| 50 | + $value = $this->read($key); |
|
| 51 | + $value = ($value[$this->alias][$key] == 0) ? 1 : 0; |
|
| 52 | + $this->set($key, $value); |
|
| 53 | + $this->save(); |
|
| 54 | + return $value; |
|
| 55 | + } |
|
| 56 | 56 | |
| 57 | - /** |
|
| 58 | - * filters out all fields $fields in $data |
|
| 59 | - * |
|
| 60 | - * works only on current model, not associations |
|
| 61 | - * |
|
| 62 | - * @param $data |
|
| 63 | - * @param $fields |
|
| 64 | - */ |
|
| 65 | - public function filterFields(&$data, $fields) { |
|
| 66 | - if (is_string($fields) && isset($this->allowedInputFields[$fields])) { |
|
| 67 | - $fields = $this->allowedInputFields[$fields]; |
|
| 68 | - } |
|
| 69 | - $fields = array_flip($fields); |
|
| 70 | - $data = [ |
|
| 71 | - $this->alias => array_intersect_key($data[$this->alias], $fields) |
|
| 72 | - ]; |
|
| 73 | - } |
|
| 57 | + /** |
|
| 58 | + * filters out all fields $fields in $data |
|
| 59 | + * |
|
| 60 | + * works only on current model, not associations |
|
| 61 | + * |
|
| 62 | + * @param $data |
|
| 63 | + * @param $fields |
|
| 64 | + */ |
|
| 65 | + public function filterFields(&$data, $fields) { |
|
| 66 | + if (is_string($fields) && isset($this->allowedInputFields[$fields])) { |
|
| 67 | + $fields = $this->allowedInputFields[$fields]; |
|
| 68 | + } |
|
| 69 | + $fields = array_flip($fields); |
|
| 70 | + $data = [ |
|
| 71 | + $this->alias => array_intersect_key($data[$this->alias], $fields) |
|
| 72 | + ]; |
|
| 73 | + } |
|
| 74 | 74 | |
| 75 | - public function requireFields(&$data, array $required) { |
|
| 76 | - return $this->_mapFields($data, $required, |
|
| 77 | - function (&$data, $model, $field) { |
|
| 78 | - if (!isset($data[$model][$field])) { |
|
| 79 | - return false; |
|
| 80 | - } |
|
| 81 | - return true; |
|
| 82 | - }); |
|
| 83 | - } |
|
| 75 | + public function requireFields(&$data, array $required) { |
|
| 76 | + return $this->_mapFields($data, $required, |
|
| 77 | + function (&$data, $model, $field) { |
|
| 78 | + if (!isset($data[$model][$field])) { |
|
| 79 | + return false; |
|
| 80 | + } |
|
| 81 | + return true; |
|
| 82 | + }); |
|
| 83 | + } |
|
| 84 | 84 | |
| 85 | - public function unsetFields(&$data, array $unset = ['id']) { |
|
| 86 | - return $this->_mapFields($data, $unset, |
|
| 87 | - function (&$data, $model, $field) { |
|
| 88 | - if (isset($data[$model][$field])) { |
|
| 89 | - unset($data[$model][$field]); |
|
| 90 | - } |
|
| 91 | - return true; |
|
| 92 | - }); |
|
| 93 | - } |
|
| 85 | + public function unsetFields(&$data, array $unset = ['id']) { |
|
| 86 | + return $this->_mapFields($data, $unset, |
|
| 87 | + function (&$data, $model, $field) { |
|
| 88 | + if (isset($data[$model][$field])) { |
|
| 89 | + unset($data[$model][$field]); |
|
| 90 | + } |
|
| 91 | + return true; |
|
| 92 | + }); |
|
| 93 | + } |
|
| 94 | 94 | |
| 95 | - protected function _mapFields(&$data, $fields, callable $func) { |
|
| 96 | - if (isset(reset($data)[$this->alias])) { |
|
| 97 | - foreach ($data as &$d) { |
|
| 98 | - if (!$this->_mapFields($d, $fields, $func)) { |
|
| 99 | - return false; |
|
| 100 | - } |
|
| 101 | - } |
|
| 102 | - return true; |
|
| 103 | - } |
|
| 95 | + protected function _mapFields(&$data, $fields, callable $func) { |
|
| 96 | + if (isset(reset($data)[$this->alias])) { |
|
| 97 | + foreach ($data as &$d) { |
|
| 98 | + if (!$this->_mapFields($d, $fields, $func)) { |
|
| 99 | + return false; |
|
| 100 | + } |
|
| 101 | + } |
|
| 102 | + return true; |
|
| 103 | + } |
|
| 104 | 104 | |
| 105 | - if (!isset($data[$this->alias])) { |
|
| 106 | - $data = [$this->alias => $data]; |
|
| 107 | - } |
|
| 108 | - foreach ($fields as $field) { |
|
| 109 | - if (strpos($field, '.') !== false) { |
|
| 110 | - list($model, $field) = explode('.', $field, 2); |
|
| 111 | - } else { |
|
| 112 | - $model = $this->alias; |
|
| 113 | - } |
|
| 114 | - if ($model !== $this->alias) { |
|
| 115 | - continue; |
|
| 116 | - } |
|
| 117 | - if (!$func($data, $model, $field)) { |
|
| 118 | - return false; |
|
| 119 | - } |
|
| 120 | - } |
|
| 121 | - return true; |
|
| 122 | - } |
|
| 105 | + if (!isset($data[$this->alias])) { |
|
| 106 | + $data = [$this->alias => $data]; |
|
| 107 | + } |
|
| 108 | + foreach ($fields as $field) { |
|
| 109 | + if (strpos($field, '.') !== false) { |
|
| 110 | + list($model, $field) = explode('.', $field, 2); |
|
| 111 | + } else { |
|
| 112 | + $model = $this->alias; |
|
| 113 | + } |
|
| 114 | + if ($model !== $this->alias) { |
|
| 115 | + continue; |
|
| 116 | + } |
|
| 117 | + if (!$func($data, $model, $field)) { |
|
| 118 | + return false; |
|
| 119 | + } |
|
| 120 | + } |
|
| 121 | + return true; |
|
| 122 | + } |
|
| 123 | 123 | |
| 124 | - /** |
|
| 125 | - * Increments value of a field |
|
| 126 | - * |
|
| 127 | - * @param $id |
|
| 128 | - * @param $field |
|
| 129 | - * @param int $amount |
|
| 130 | - * @throws InvalidArgumentException |
|
| 131 | - */ |
|
| 132 | - public function increment($id, $field, $amount = 1) { |
|
| 133 | - if (!is_int($amount)) { |
|
| 134 | - throw new InvalidArgumentException; |
|
| 135 | - } |
|
| 136 | - $operator = '+'; |
|
| 137 | - if ($amount < 0) { |
|
| 138 | - $operator = '-'; |
|
| 139 | - $amount *= -1; |
|
| 140 | - } |
|
| 141 | - $field = $this->alias . '.' . $field; |
|
| 142 | - $this->updateAll([$field => "$field $operator $amount"], |
|
| 143 | - [$this->alias . '.id' => $id]); |
|
| 144 | - } |
|
| 124 | + /** |
|
| 125 | + * Increments value of a field |
|
| 126 | + * |
|
| 127 | + * @param $id |
|
| 128 | + * @param $field |
|
| 129 | + * @param int $amount |
|
| 130 | + * @throws InvalidArgumentException |
|
| 131 | + */ |
|
| 132 | + public function increment($id, $field, $amount = 1) { |
|
| 133 | + if (!is_int($amount)) { |
|
| 134 | + throw new InvalidArgumentException; |
|
| 135 | + } |
|
| 136 | + $operator = '+'; |
|
| 137 | + if ($amount < 0) { |
|
| 138 | + $operator = '-'; |
|
| 139 | + $amount *= -1; |
|
| 140 | + } |
|
| 141 | + $field = $this->alias . '.' . $field; |
|
| 142 | + $this->updateAll([$field => "$field $operator $amount"], |
|
| 143 | + [$this->alias . '.id' => $id]); |
|
| 144 | + } |
|
| 145 | 145 | |
| 146 | - public function pipeMerger(array $data) { |
|
| 147 | - $out = []; |
|
| 148 | - foreach ($data as $key => $value) { |
|
| 149 | - $out[] = "$key=$value"; |
|
| 150 | - } |
|
| 151 | - return implode(' | ', $out); |
|
| 152 | - } |
|
| 146 | + public function pipeMerger(array $data) { |
|
| 147 | + $out = []; |
|
| 148 | + foreach ($data as $key => $value) { |
|
| 149 | + $out[] = "$key=$value"; |
|
| 150 | + } |
|
| 151 | + return implode(' | ', $out); |
|
| 152 | + } |
|
| 153 | 153 | |
| 154 | - /** |
|
| 155 | - * Splits String 'a=b|c=d|e=f' into an array('a'=>'b', 'c'=>'d', 'e'=>'f') |
|
| 156 | - * |
|
| 157 | - * @param string $pipeString |
|
| 158 | - * @return array |
|
| 159 | - */ |
|
| 160 | - protected function _pipeSplitter($pipeString) { |
|
| 161 | - $unpipedArray = array(); |
|
| 162 | - $ranks = explode('|', $pipeString); |
|
| 163 | - foreach ($ranks as $rank) : |
|
| 164 | - $matches = array(); |
|
| 165 | - $matched = preg_match('/(\w+)\s*=\s*(.*)/', trim($rank), $matches); |
|
| 166 | - if ($matched) { |
|
| 167 | - $unpipedArray[$matches[1]] = $matches[2]; |
|
| 168 | - } |
|
| 169 | - endforeach; |
|
| 170 | - return $unpipedArray; |
|
| 171 | - } |
|
| 154 | + /** |
|
| 155 | + * Splits String 'a=b|c=d|e=f' into an array('a'=>'b', 'c'=>'d', 'e'=>'f') |
|
| 156 | + * |
|
| 157 | + * @param string $pipeString |
|
| 158 | + * @return array |
|
| 159 | + */ |
|
| 160 | + protected function _pipeSplitter($pipeString) { |
|
| 161 | + $unpipedArray = array(); |
|
| 162 | + $ranks = explode('|', $pipeString); |
|
| 163 | + foreach ($ranks as $rank) : |
|
| 164 | + $matches = array(); |
|
| 165 | + $matched = preg_match('/(\w+)\s*=\s*(.*)/', trim($rank), $matches); |
|
| 166 | + if ($matched) { |
|
| 167 | + $unpipedArray[$matches[1]] = $matches[2]; |
|
| 168 | + } |
|
| 169 | + endforeach; |
|
| 170 | + return $unpipedArray; |
|
| 171 | + } |
|
| 172 | 172 | |
| 173 | - protected static function _getIp() { |
|
| 174 | - $ip = null; |
|
| 175 | - if (Configure::read('Saito.Settings.store_ip')): |
|
| 176 | - $ip = env('REMOTE_ADDR'); |
|
| 177 | - if (Configure::read('Saito.Settings.store_ip_anonymized')): |
|
| 178 | - $ip = self::_anonymizeIp($ip); |
|
| 179 | - endif; |
|
| 180 | - endif; |
|
| 181 | - return $ip; |
|
| 182 | - } |
|
| 173 | + protected static function _getIp() { |
|
| 174 | + $ip = null; |
|
| 175 | + if (Configure::read('Saito.Settings.store_ip')): |
|
| 176 | + $ip = env('REMOTE_ADDR'); |
|
| 177 | + if (Configure::read('Saito.Settings.store_ip_anonymized')): |
|
| 178 | + $ip = self::_anonymizeIp($ip); |
|
| 179 | + endif; |
|
| 180 | + endif; |
|
| 181 | + return $ip; |
|
| 182 | + } |
|
| 183 | 183 | |
| 184 | - /** |
|
| 185 | - * Dispatches an event |
|
| 186 | - * |
|
| 187 | - * - Always passes the issuing model class as subject |
|
| 188 | - * - Wrapper for CakeEvent boilerplate code |
|
| 189 | - * - Easier to test |
|
| 190 | - * |
|
| 191 | - * @param string $event event identifier `Model.<modelname>.<event>` |
|
| 192 | - * @param array $data additional event data |
|
| 193 | - */ |
|
| 194 | - protected function _dispatchEvent($event, $data = []) { |
|
| 195 | - $this->getEventManager()->dispatch(new CakeEvent($event, $this, $data)); |
|
| 196 | - // propagate event on Saito's event bus |
|
| 197 | - $this->dispatchSaitoEvent($event, $data); |
|
| 198 | - } |
|
| 184 | + /** |
|
| 185 | + * Dispatches an event |
|
| 186 | + * |
|
| 187 | + * - Always passes the issuing model class as subject |
|
| 188 | + * - Wrapper for CakeEvent boilerplate code |
|
| 189 | + * - Easier to test |
|
| 190 | + * |
|
| 191 | + * @param string $event event identifier `Model.<modelname>.<event>` |
|
| 192 | + * @param array $data additional event data |
|
| 193 | + */ |
|
| 194 | + protected function _dispatchEvent($event, $data = []) { |
|
| 195 | + $this->getEventManager()->dispatch(new CakeEvent($event, $this, $data)); |
|
| 196 | + // propagate event on Saito's event bus |
|
| 197 | + $this->dispatchSaitoEvent($event, $data); |
|
| 198 | + } |
|
| 199 | 199 | |
| 200 | - public function dispatchSaitoEvent($event, $data) { |
|
| 201 | - if (!$this->_SEM) { |
|
| 202 | - $this->_SEM = SaitoEventManager::getInstance(); |
|
| 203 | - } |
|
| 204 | - $this->_SEM->dispatch($event, $data + ['Model' => $this]); |
|
| 205 | - } |
|
| 200 | + public function dispatchSaitoEvent($event, $data) { |
|
| 201 | + if (!$this->_SEM) { |
|
| 202 | + $this->_SEM = SaitoEventManager::getInstance(); |
|
| 203 | + } |
|
| 204 | + $this->_SEM->dispatch($event, $data + ['Model' => $this]); |
|
| 205 | + } |
|
| 206 | 206 | |
| 207 | - /** |
|
| 208 | - * Rough and tough ip anonymizer |
|
| 209 | - * |
|
| 210 | - * @param string $ip |
|
| 211 | - * @return string |
|
| 212 | - */ |
|
| 213 | - protected static function _anonymizeIp($ip) { |
|
| 214 | - $strlen = strlen($ip); |
|
| 215 | - if ($strlen > 6) : |
|
| 216 | - $divider = (int)floor($strlen / 4) + 1; |
|
| 217 | - $ip = substr_replace($ip, '…', $divider, $strlen - (2 * $divider)); |
|
| 218 | - endif; |
|
| 207 | + /** |
|
| 208 | + * Rough and tough ip anonymizer |
|
| 209 | + * |
|
| 210 | + * @param string $ip |
|
| 211 | + * @return string |
|
| 212 | + */ |
|
| 213 | + protected static function _anonymizeIp($ip) { |
|
| 214 | + $strlen = strlen($ip); |
|
| 215 | + if ($strlen > 6) : |
|
| 216 | + $divider = (int)floor($strlen / 4) + 1; |
|
| 217 | + $ip = substr_replace($ip, '…', $divider, $strlen - (2 * $divider)); |
|
| 218 | + endif; |
|
| 219 | 219 | |
| 220 | - return $ip; |
|
| 221 | - } |
|
| 220 | + return $ip; |
|
| 221 | + } |
|
| 222 | 222 | |
| 223 | - /** |
|
| 224 | - * gets app setting |
|
| 225 | - * |
|
| 226 | - * falls back to local definition if available |
|
| 227 | - * |
|
| 228 | - * @param $name |
|
| 229 | - * @return mixed |
|
| 230 | - * @throws UnexpectedValueException |
|
| 231 | - */ |
|
| 232 | - protected function _setting($name) { |
|
| 233 | - $setting = Configure::read('Saito.Settings.' . $name); |
|
| 234 | - if ($setting !== null) { |
|
| 235 | - return $setting; |
|
| 236 | - } |
|
| 237 | - if (isset($this->_settings[$name])) { |
|
| 238 | - return $this->_settings[$name]; |
|
| 239 | - } |
|
| 240 | - throw new UnexpectedValueException; |
|
| 241 | - } |
|
| 223 | + /** |
|
| 224 | + * gets app setting |
|
| 225 | + * |
|
| 226 | + * falls back to local definition if available |
|
| 227 | + * |
|
| 228 | + * @param $name |
|
| 229 | + * @return mixed |
|
| 230 | + * @throws UnexpectedValueException |
|
| 231 | + */ |
|
| 232 | + protected function _setting($name) { |
|
| 233 | + $setting = Configure::read('Saito.Settings.' . $name); |
|
| 234 | + if ($setting !== null) { |
|
| 235 | + return $setting; |
|
| 236 | + } |
|
| 237 | + if (isset($this->_settings[$name])) { |
|
| 238 | + return $this->_settings[$name]; |
|
| 239 | + } |
|
| 240 | + throw new UnexpectedValueException; |
|
| 241 | + } |
|
| 242 | 242 | |
| 243 | - public function isUniqueCiString($fields) { |
|
| 244 | - // lazy: only one field |
|
| 245 | - if (!is_array($fields) || count($fields) > 1) { |
|
| 246 | - throw InvalidArgumentException(); |
|
| 247 | - } |
|
| 248 | - $key = key($fields); |
|
| 249 | - $fields = [ |
|
| 250 | - "LOWER({$this->alias}.{$key})" => mb_strtolower(current($fields)) |
|
| 251 | - ]; |
|
| 252 | - return $this->isUnique($fields); |
|
| 253 | - } |
|
| 243 | + public function isUniqueCiString($fields) { |
|
| 244 | + // lazy: only one field |
|
| 245 | + if (!is_array($fields) || count($fields) > 1) { |
|
| 246 | + throw InvalidArgumentException(); |
|
| 247 | + } |
|
| 248 | + $key = key($fields); |
|
| 249 | + $fields = [ |
|
| 250 | + "LOWER({$this->alias}.{$key})" => mb_strtolower(current($fields)) |
|
| 251 | + ]; |
|
| 252 | + return $this->isUnique($fields); |
|
| 253 | + } |
|
| 254 | 254 | |
| 255 | - /** |
|
| 256 | - * Inclusive Validation::range() |
|
| 257 | - * |
|
| 258 | - * @param array $check |
|
| 259 | - * @param float $lower |
|
| 260 | - * @param float $upper |
|
| 261 | - * @return bool |
|
| 262 | - * @see https://github.com/cakephp/cakephp/issues/3304 |
|
| 263 | - */ |
|
| 264 | - public function inRange($check, $lower = null, $upper = null) { |
|
| 265 | - $check = reset($check); |
|
| 266 | - if (!is_numeric($check)) { |
|
| 267 | - return false; |
|
| 268 | - } |
|
| 269 | - if (isset($lower) && isset($upper)) { |
|
| 270 | - return ($check >= $lower && $check <= $upper); |
|
| 271 | - } |
|
| 272 | - // fallback to 'parent' |
|
| 273 | - return Validation::range($check, $lower, $upper); |
|
| 274 | - } |
|
| 255 | + /** |
|
| 256 | + * Inclusive Validation::range() |
|
| 257 | + * |
|
| 258 | + * @param array $check |
|
| 259 | + * @param float $lower |
|
| 260 | + * @param float $upper |
|
| 261 | + * @return bool |
|
| 262 | + * @see https://github.com/cakephp/cakephp/issues/3304 |
|
| 263 | + */ |
|
| 264 | + public function inRange($check, $lower = null, $upper = null) { |
|
| 265 | + $check = reset($check); |
|
| 266 | + if (!is_numeric($check)) { |
|
| 267 | + return false; |
|
| 268 | + } |
|
| 269 | + if (isset($lower) && isset($upper)) { |
|
| 270 | + return ($check >= $lower && $check <= $upper); |
|
| 271 | + } |
|
| 272 | + // fallback to 'parent' |
|
| 273 | + return Validation::range($check, $lower, $upper); |
|
| 274 | + } |
|
| 275 | 275 | |
| 276 | - /** |
|
| 277 | - * Logs current SQL log |
|
| 278 | - * |
|
| 279 | - * Set debug to 2 to enable SQL logging! |
|
| 280 | - */ |
|
| 281 | - public function logSql() { |
|
| 282 | - if (Configure::read('debug') < 2) { |
|
| 283 | - trigger_error('You must set debug level to at least 2 to enable SQL-logging', |
|
| 284 | - E_USER_NOTICE); |
|
| 285 | - } |
|
| 286 | - $dbo = $this->getDatasource(); |
|
| 287 | - $logs = $dbo->getLog(); |
|
| 288 | - $this->log($logs['log']); |
|
| 289 | - } |
|
| 276 | + /** |
|
| 277 | + * Logs current SQL log |
|
| 278 | + * |
|
| 279 | + * Set debug to 2 to enable SQL logging! |
|
| 280 | + */ |
|
| 281 | + public function logSql() { |
|
| 282 | + if (Configure::read('debug') < 2) { |
|
| 283 | + trigger_error('You must set debug level to at least 2 to enable SQL-logging', |
|
| 284 | + E_USER_NOTICE); |
|
| 285 | + } |
|
| 286 | + $dbo = $this->getDatasource(); |
|
| 287 | + $logs = $dbo->getLog(); |
|
| 288 | + $this->log($logs['log']); |
|
| 289 | + } |
|
| 290 | 290 | |
| 291 | - } |
|
| 291 | + } |
|
@@ -3,35 +3,35 @@ discard block |
||
| 3 | 3 | /** |
| 4 | 4 | * Class UserOnline |
| 5 | 5 | */ |
| 6 | - class UserOnline extends AppModel { |
|
| 6 | + class UserOnline extends AppModel { |
|
| 7 | 7 | |
| 8 | - public $name = 'UserOnline'; |
|
| 8 | + public $name = 'UserOnline'; |
|
| 9 | 9 | |
| 10 | - public $useTable = 'useronline'; |
|
| 10 | + public $useTable = 'useronline'; |
|
| 11 | 11 | |
| 12 | - public $actsAs = ['Containable']; |
|
| 12 | + public $actsAs = ['Containable']; |
|
| 13 | 13 | |
| 14 | - public $belongsTo = [ |
|
| 15 | - 'User' => [ |
|
| 16 | - 'className' => 'User', |
|
| 17 | - 'foreignKey' => 'user_id' |
|
| 18 | - ] |
|
| 19 | - ]; |
|
| 14 | + public $belongsTo = [ |
|
| 15 | + 'User' => [ |
|
| 16 | + 'className' => 'User', |
|
| 17 | + 'foreignKey' => 'user_id' |
|
| 18 | + ] |
|
| 19 | + ]; |
|
| 20 | 20 | |
| 21 | - public $validate = [ |
|
| 22 | - 'uuid' => [ |
|
| 23 | - 'rule' => 'isUnique', |
|
| 24 | - 'required' => true, |
|
| 25 | - 'allowEmpty' => false |
|
| 26 | - ] |
|
| 27 | - ]; |
|
| 21 | + public $validate = [ |
|
| 22 | + 'uuid' => [ |
|
| 23 | + 'rule' => 'isUnique', |
|
| 24 | + 'required' => true, |
|
| 25 | + 'allowEmpty' => false |
|
| 26 | + ] |
|
| 27 | + ]; |
|
| 28 | 28 | |
| 29 | 29 | /** |
| 30 | 30 | * Time in seconds until a user is considered offline |
| 31 | 31 | * |
| 32 | 32 | * @var int |
| 33 | 33 | */ |
| 34 | - public $timeUntilOffline = 1200; |
|
| 34 | + public $timeUntilOffline = 1200; |
|
| 35 | 35 | |
| 36 | 36 | /** |
| 37 | 37 | * Sets user with `$id` online |
@@ -40,72 +40,72 @@ discard block |
||
| 40 | 40 | * @param boolean $loggedIn user is logged-in |
| 41 | 41 | * @throws InvalidArgumentException |
| 42 | 42 | */ |
| 43 | - public function setOnline($id, $loggedIn) { |
|
| 44 | - if (empty($id)) { |
|
| 45 | - throw new InvalidArgumentException('Invalid Argument $id in setOnline()'); |
|
| 46 | - } |
|
| 47 | - if (!is_bool($loggedIn)) { |
|
| 48 | - throw new InvalidArgumentException('Invalid Argument $logged_in in setOnline()'); |
|
| 49 | - } |
|
| 50 | - $now = time(); |
|
| 51 | - |
|
| 52 | - $id = $this->_getShortendedId($id); |
|
| 53 | - $data = [ |
|
| 54 | - 'UserOnline' => [ |
|
| 55 | - 'uuid' => $id, |
|
| 56 | - 'logged_in' => $loggedIn, |
|
| 57 | - 'time' => $now |
|
| 58 | - ] |
|
| 59 | - ]; |
|
| 60 | - |
|
| 61 | - if ($loggedIn) { |
|
| 62 | - $data['UserOnline']['user_id'] = $id; |
|
| 63 | - } |
|
| 64 | - |
|
| 65 | - $user = $this->find('first', ['conditions' => ['uuid' => $id], |
|
| 66 | - 'recursive' => -1, 'callbacks' => false]); |
|
| 67 | - |
|
| 68 | - if ($user) { |
|
| 69 | - // only hit database if timestamp is outdated |
|
| 70 | - if ($user['UserOnline']['time'] < ($now - $this->timeUntilOffline)) { |
|
| 71 | - $this->id = $user['UserOnline']['id']; |
|
| 72 | - $this->save($data); |
|
| 73 | - } |
|
| 74 | - } else { |
|
| 75 | - $this->id = null; |
|
| 76 | - $this->create(); |
|
| 77 | - $this->save($data); |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - $this->_deleteOutdated(); |
|
| 81 | - } |
|
| 82 | - |
|
| 83 | - /** |
|
| 84 | - * Removes user with uuid `$id` from UserOnline |
|
| 85 | - * |
|
| 86 | - * @param $id |
|
| 87 | - * |
|
| 88 | - * @return bool |
|
| 89 | - */ |
|
| 90 | - public function setOffline($id) { |
|
| 91 | - $id = $this->_getShortendedId($id); |
|
| 92 | - return $this->deleteAll(['UserOnline.uuid' => $id], false); |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - public function getLoggedIn() { |
|
| 96 | - Stopwatch::start('UserOnline->getLoggedIn()'); |
|
| 97 | - $loggedInUsers = $this->find( |
|
| 98 | - 'all', |
|
| 99 | - [ |
|
| 100 | - 'contain' => 'User', |
|
| 101 | - 'conditions' => ['UserOnline.logged_in' => true], |
|
| 102 | - 'fields' => ['User.id', 'User.username', 'User.user_type'], |
|
| 103 | - 'order' => ['LOWER(User.username)' => 'ASC'] |
|
| 104 | - ] |
|
| 105 | - ); |
|
| 106 | - Stopwatch::stop('UserOnline->getLoggedIn()'); |
|
| 107 | - return $loggedInUsers; |
|
| 108 | - } |
|
| 43 | + public function setOnline($id, $loggedIn) { |
|
| 44 | + if (empty($id)) { |
|
| 45 | + throw new InvalidArgumentException('Invalid Argument $id in setOnline()'); |
|
| 46 | + } |
|
| 47 | + if (!is_bool($loggedIn)) { |
|
| 48 | + throw new InvalidArgumentException('Invalid Argument $logged_in in setOnline()'); |
|
| 49 | + } |
|
| 50 | + $now = time(); |
|
| 51 | + |
|
| 52 | + $id = $this->_getShortendedId($id); |
|
| 53 | + $data = [ |
|
| 54 | + 'UserOnline' => [ |
|
| 55 | + 'uuid' => $id, |
|
| 56 | + 'logged_in' => $loggedIn, |
|
| 57 | + 'time' => $now |
|
| 58 | + ] |
|
| 59 | + ]; |
|
| 60 | + |
|
| 61 | + if ($loggedIn) { |
|
| 62 | + $data['UserOnline']['user_id'] = $id; |
|
| 63 | + } |
|
| 64 | + |
|
| 65 | + $user = $this->find('first', ['conditions' => ['uuid' => $id], |
|
| 66 | + 'recursive' => -1, 'callbacks' => false]); |
|
| 67 | + |
|
| 68 | + if ($user) { |
|
| 69 | + // only hit database if timestamp is outdated |
|
| 70 | + if ($user['UserOnline']['time'] < ($now - $this->timeUntilOffline)) { |
|
| 71 | + $this->id = $user['UserOnline']['id']; |
|
| 72 | + $this->save($data); |
|
| 73 | + } |
|
| 74 | + } else { |
|
| 75 | + $this->id = null; |
|
| 76 | + $this->create(); |
|
| 77 | + $this->save($data); |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + $this->_deleteOutdated(); |
|
| 81 | + } |
|
| 82 | + |
|
| 83 | + /** |
|
| 84 | + * Removes user with uuid `$id` from UserOnline |
|
| 85 | + * |
|
| 86 | + * @param $id |
|
| 87 | + * |
|
| 88 | + * @return bool |
|
| 89 | + */ |
|
| 90 | + public function setOffline($id) { |
|
| 91 | + $id = $this->_getShortendedId($id); |
|
| 92 | + return $this->deleteAll(['UserOnline.uuid' => $id], false); |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + public function getLoggedIn() { |
|
| 96 | + Stopwatch::start('UserOnline->getLoggedIn()'); |
|
| 97 | + $loggedInUsers = $this->find( |
|
| 98 | + 'all', |
|
| 99 | + [ |
|
| 100 | + 'contain' => 'User', |
|
| 101 | + 'conditions' => ['UserOnline.logged_in' => true], |
|
| 102 | + 'fields' => ['User.id', 'User.username', 'User.user_type'], |
|
| 103 | + 'order' => ['LOWER(User.username)' => 'ASC'] |
|
| 104 | + ] |
|
| 105 | + ); |
|
| 106 | + Stopwatch::stop('UserOnline->getLoggedIn()'); |
|
| 107 | + return $loggedInUsers; |
|
| 108 | + } |
|
| 109 | 109 | |
| 110 | 110 | /** |
| 111 | 111 | * deletes gone user |
@@ -114,15 +114,15 @@ discard block |
||
| 114 | 114 | * |
| 115 | 115 | * @param string $timeDiff in minutes |
| 116 | 116 | */ |
| 117 | - protected function _deleteOutdated($timeDiff = null) { |
|
| 118 | - if ($timeDiff === null) { |
|
| 119 | - $timeDiff = $this->timeUntilOffline; |
|
| 120 | - } |
|
| 121 | - $this->deleteAll(['time <' => time() - ($timeDiff)], false); |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - protected function _getShortendedId($id) { |
|
| 125 | - return substr($id, 0, 32); |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - } |
|
| 129 | 117 | \ No newline at end of file |
| 118 | + protected function _deleteOutdated($timeDiff = null) { |
|
| 119 | + if ($timeDiff === null) { |
|
| 120 | + $timeDiff = $this->timeUntilOffline; |
|
| 121 | + } |
|
| 122 | + $this->deleteAll(['time <' => time() - ($timeDiff)], false); |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + protected function _getShortendedId($id) { |
|
| 126 | + return substr($id, 0, 32); |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + } |
|
| 130 | 130 | \ No newline at end of file |
@@ -1,79 +1,79 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | - App::uses('AppSettingModel', 'Lib/Model'); |
|
| 3 | + App::uses('AppSettingModel', 'Lib/Model'); |
|
| 4 | 4 | |
| 5 | - class Smiley extends AppSettingModel { |
|
| 5 | + class Smiley extends AppSettingModel { |
|
| 6 | 6 | |
| 7 | - public $name = 'Smiley'; |
|
| 7 | + public $name = 'Smiley'; |
|
| 8 | 8 | |
| 9 | - public $validate = [ |
|
| 10 | - 'sort' => [ |
|
| 11 | - 'numeric' => [ |
|
| 12 | - 'rule' => ['numeric'] |
|
| 13 | - ] |
|
| 14 | - ] |
|
| 15 | - ]; |
|
| 9 | + public $validate = [ |
|
| 10 | + 'sort' => [ |
|
| 11 | + 'numeric' => [ |
|
| 12 | + 'rule' => ['numeric'] |
|
| 13 | + ] |
|
| 14 | + ] |
|
| 15 | + ]; |
|
| 16 | 16 | |
| 17 | - public $hasMany = [ |
|
| 18 | - 'SmileyCode' => [ |
|
| 19 | - 'className' => 'SmileyCode', |
|
| 20 | - 'foreignKey' => 'smiley_id' |
|
| 21 | - ] |
|
| 22 | - ]; |
|
| 17 | + public $hasMany = [ |
|
| 18 | + 'SmileyCode' => [ |
|
| 19 | + 'className' => 'SmileyCode', |
|
| 20 | + 'foreignKey' => 'smiley_id' |
|
| 21 | + ] |
|
| 22 | + ]; |
|
| 23 | 23 | |
| 24 | - protected $_smilies; |
|
| 24 | + protected $_smilies; |
|
| 25 | 25 | |
| 26 | - public function load($force = false) { |
|
| 27 | - if ($force) { |
|
| 28 | - $this->_smilies = null; |
|
| 29 | - $this->clearCache(); |
|
| 30 | - } |
|
| 26 | + public function load($force = false) { |
|
| 27 | + if ($force) { |
|
| 28 | + $this->_smilies = null; |
|
| 29 | + $this->clearCache(); |
|
| 30 | + } |
|
| 31 | 31 | |
| 32 | - if ($this->_smilies !== null) { |
|
| 33 | - return $this->_smilies; |
|
| 34 | - } |
|
| 32 | + if ($this->_smilies !== null) { |
|
| 33 | + return $this->_smilies; |
|
| 34 | + } |
|
| 35 | 35 | |
| 36 | - $this->_smilies = []; |
|
| 37 | - $smiliesRaw = $this->find('all', ['order' => 'Smiley.sort ASC']); |
|
| 36 | + $this->_smilies = []; |
|
| 37 | + $smiliesRaw = $this->find('all', ['order' => 'Smiley.sort ASC']); |
|
| 38 | 38 | |
| 39 | - foreach ($smiliesRaw as $smileyRaw) { |
|
| 40 | - // 'image' defaults to 'icon' |
|
| 41 | - if (empty($smileyRaw['Smiley']['image'])) { |
|
| 42 | - $smileyRaw['Smiley']['image'] = $smileyRaw['Smiley']['icon']; |
|
| 43 | - } |
|
| 44 | - // @bogus: if title is unknown it should be a problem |
|
| 45 | - if ($smileyRaw['Smiley']['title'] === null) { |
|
| 46 | - $smileyRaw['Smiley']['title'] = ''; |
|
| 47 | - } |
|
| 48 | - // set type |
|
| 49 | - $smileyRaw['Smiley']['type'] = $this->_getType($smileyRaw['Smiley']); |
|
| 39 | + foreach ($smiliesRaw as $smileyRaw) { |
|
| 40 | + // 'image' defaults to 'icon' |
|
| 41 | + if (empty($smileyRaw['Smiley']['image'])) { |
|
| 42 | + $smileyRaw['Smiley']['image'] = $smileyRaw['Smiley']['icon']; |
|
| 43 | + } |
|
| 44 | + // @bogus: if title is unknown it should be a problem |
|
| 45 | + if ($smileyRaw['Smiley']['title'] === null) { |
|
| 46 | + $smileyRaw['Smiley']['title'] = ''; |
|
| 47 | + } |
|
| 48 | + // set type |
|
| 49 | + $smileyRaw['Smiley']['type'] = $this->_getType($smileyRaw['Smiley']); |
|
| 50 | 50 | |
| 51 | - //= adds smiley-data to every smiley-code |
|
| 52 | - if (isset($smileyRaw['SmileyCode'])) { |
|
| 53 | - foreach ($smileyRaw['SmileyCode'] as $smileyRawCode) { |
|
| 54 | - unset($smileyRaw['Smiley']['id']); |
|
| 55 | - $smileyRaw['Smiley']['code'] = $smileyRawCode['code']; |
|
| 56 | - $this->_smilies[] = $smileyRaw['Smiley']; |
|
| 57 | - } |
|
| 58 | - } |
|
| 59 | - } |
|
| 51 | + //= adds smiley-data to every smiley-code |
|
| 52 | + if (isset($smileyRaw['SmileyCode'])) { |
|
| 53 | + foreach ($smileyRaw['SmileyCode'] as $smileyRawCode) { |
|
| 54 | + unset($smileyRaw['Smiley']['id']); |
|
| 55 | + $smileyRaw['Smiley']['code'] = $smileyRawCode['code']; |
|
| 56 | + $this->_smilies[] = $smileyRaw['Smiley']; |
|
| 57 | + } |
|
| 58 | + } |
|
| 59 | + } |
|
| 60 | 60 | |
| 61 | - Stopwatch::stop('Smiley::load'); |
|
| 62 | - return $this->_smilies; |
|
| 63 | - } |
|
| 61 | + Stopwatch::stop('Smiley::load'); |
|
| 62 | + return $this->_smilies; |
|
| 63 | + } |
|
| 64 | 64 | |
| 65 | - /** |
|
| 66 | - * detects smiley type |
|
| 67 | - * |
|
| 68 | - * @param array $smiley |
|
| 69 | - * @return string image|font |
|
| 70 | - */ |
|
| 71 | - protected function _getType($smiley) { |
|
| 72 | - if (preg_match('/^.*\.[\w]{3,4}$/i', $smiley['image'])) { |
|
| 73 | - return 'image'; |
|
| 74 | - } else { |
|
| 75 | - return 'font'; |
|
| 76 | - } |
|
| 77 | - } |
|
| 65 | + /** |
|
| 66 | + * detects smiley type |
|
| 67 | + * |
|
| 68 | + * @param array $smiley |
|
| 69 | + * @return string image|font |
|
| 70 | + */ |
|
| 71 | + protected function _getType($smiley) { |
|
| 72 | + if (preg_match('/^.*\.[\w]{3,4}$/i', $smiley['image'])) { |
|
| 73 | + return 'image'; |
|
| 74 | + } else { |
|
| 75 | + return 'font'; |
|
| 76 | + } |
|
| 77 | + } |
|
| 78 | 78 | |
| 79 | - } |
|
| 79 | + } |
|
@@ -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 | + } |
|
@@ -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 | + } |
|
@@ -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 | + } |
|
@@ -1,58 +1,58 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | - App::uses('AppModel', 'Model'); |
|
| 3 | + App::uses('AppModel', 'Model'); |
|
| 4 | 4 | |
| 5 | - /** |
|
| 6 | - * Esevent Model |
|
| 7 | - * |
|
| 8 | - * @property Essubject $Essubject |
|
| 9 | - * @property Esnotification $Esnotification |
|
| 10 | - */ |
|
| 11 | - class Esevent extends AppModel { |
|
| 5 | + /** |
|
| 6 | + * Esevent Model |
|
| 7 | + * |
|
| 8 | + * @property Essubject $Essubject |
|
| 9 | + * @property Esnotification $Esnotification |
|
| 10 | + */ |
|
| 11 | + class Esevent extends AppModel { |
|
| 12 | 12 | |
| 13 | - public $actsAs = array('Containable'); |
|
| 13 | + public $actsAs = array('Containable'); |
|
| 14 | 14 | |
| 15 | - //The Associations below have been created with all possible keys, those that are not needed can be removed |
|
| 15 | + //The Associations below have been created with all possible keys, those that are not needed can be removed |
|
| 16 | 16 | |
| 17 | 17 | /** |
| 18 | 18 | * hasMany associations |
| 19 | 19 | * |
| 20 | 20 | * @var array |
| 21 | 21 | */ |
| 22 | - public $hasMany = array( |
|
| 23 | - 'Esnotification' => array( |
|
| 24 | - 'className' => 'Esnotification', |
|
| 25 | - 'foreignKey' => 'esevent_id', |
|
| 26 | - 'dependent' => true, |
|
| 27 | - 'conditions' => '', |
|
| 28 | - 'fields' => '', |
|
| 29 | - 'order' => '', |
|
| 30 | - 'limit' => '', |
|
| 31 | - 'offset' => '', |
|
| 32 | - 'exclusive' => '', |
|
| 33 | - 'finderQuery' => '', |
|
| 34 | - 'counterQuery' => '' |
|
| 35 | - ) |
|
| 36 | - ); |
|
| 22 | + public $hasMany = array( |
|
| 23 | + 'Esnotification' => array( |
|
| 24 | + 'className' => 'Esnotification', |
|
| 25 | + 'foreignKey' => 'esevent_id', |
|
| 26 | + 'dependent' => true, |
|
| 27 | + 'conditions' => '', |
|
| 28 | + 'fields' => '', |
|
| 29 | + 'order' => '', |
|
| 30 | + 'limit' => '', |
|
| 31 | + 'offset' => '', |
|
| 32 | + 'exclusive' => '', |
|
| 33 | + 'finderQuery' => '', |
|
| 34 | + 'counterQuery' => '' |
|
| 35 | + ) |
|
| 36 | + ); |
|
| 37 | 37 | |
| 38 | - protected $_eventTypes = array( |
|
| 39 | - 'Model.Entry.replyToEntry' => 1, |
|
| 40 | - 'Model.Entry.replyToThread' => 2 |
|
| 41 | - ); |
|
| 38 | + protected $_eventTypes = array( |
|
| 39 | + 'Model.Entry.replyToEntry' => 1, |
|
| 40 | + 'Model.Entry.replyToThread' => 2 |
|
| 41 | + ); |
|
| 42 | 42 | |
| 43 | 43 | /** |
| 44 | 44 | * Subject types for $eventsTypes |
| 45 | 45 | * |
| 46 | 46 | * @var array |
| 47 | 47 | */ |
| 48 | - protected $_subjectTypes = array( |
|
| 49 | - 'entry' => array(1), |
|
| 50 | - 'thread' => array(2), |
|
| 51 | - ); |
|
| 48 | + protected $_subjectTypes = array( |
|
| 49 | + 'entry' => array(1), |
|
| 50 | + 'thread' => array(2), |
|
| 51 | + ); |
|
| 52 | 52 | |
| 53 | - protected $_receivers = array( |
|
| 54 | - 'EmailNotification' => 1, |
|
| 55 | - ); |
|
| 53 | + protected $_receivers = array( |
|
| 54 | + 'EmailNotification' => 1, |
|
| 55 | + ); |
|
| 56 | 56 | |
| 57 | 57 | /** |
| 58 | 58 | * |
@@ -60,57 +60,57 @@ discard block |
||
| 60 | 60 | * @param int $newSubject |
| 61 | 61 | * @param string $subjectType one of the strings in $_subjectTypes |
| 62 | 62 | */ |
| 63 | - public function transferSubjectForEventType($oldSubject, $newSubject, $subjectType) { |
|
| 64 | - $old = $this->find( |
|
| 65 | - 'all', |
|
| 66 | - array( |
|
| 67 | - 'contain' => array('Esnotification'), |
|
| 68 | - 'conditions' => array( |
|
| 69 | - 'subject' => $oldSubject, |
|
| 70 | - 'event' => $this->_subjectTypes[$subjectType], |
|
| 71 | - ) |
|
| 72 | - ) |
|
| 73 | - ); |
|
| 63 | + public function transferSubjectForEventType($oldSubject, $newSubject, $subjectType) { |
|
| 64 | + $old = $this->find( |
|
| 65 | + 'all', |
|
| 66 | + array( |
|
| 67 | + 'contain' => array('Esnotification'), |
|
| 68 | + 'conditions' => array( |
|
| 69 | + 'subject' => $oldSubject, |
|
| 70 | + 'event' => $this->_subjectTypes[$subjectType], |
|
| 71 | + ) |
|
| 72 | + ) |
|
| 73 | + ); |
|
| 74 | 74 | |
| 75 | - // there are no affected subjects |
|
| 76 | - if (!$old) { |
|
| 77 | - return; |
|
| 78 | - } |
|
| 75 | + // there are no affected subjects |
|
| 76 | + if (!$old) { |
|
| 77 | + return; |
|
| 78 | + } |
|
| 79 | 79 | |
| 80 | - $current = $this->find( |
|
| 81 | - 'all', |
|
| 82 | - array( |
|
| 83 | - 'contain' => array('Esnotification'), |
|
| 84 | - 'conditions' => array( |
|
| 85 | - 'subject' => $newSubject, |
|
| 86 | - 'event' => $this->_subjectTypes[$subjectType], |
|
| 87 | - ) |
|
| 88 | - ) |
|
| 89 | - ); |
|
| 80 | + $current = $this->find( |
|
| 81 | + 'all', |
|
| 82 | + array( |
|
| 83 | + 'contain' => array('Esnotification'), |
|
| 84 | + 'conditions' => array( |
|
| 85 | + 'subject' => $newSubject, |
|
| 86 | + 'event' => $this->_subjectTypes[$subjectType], |
|
| 87 | + ) |
|
| 88 | + ) |
|
| 89 | + ); |
|
| 90 | 90 | |
| 91 | - $oldEvents = Hash::combine($old, '{n}.Esevent.event', '{n}'); |
|
| 92 | - $currentEvents = Hash::combine($current, '{n}.Esevent.event', '{n}'); |
|
| 91 | + $oldEvents = Hash::combine($old, '{n}.Esevent.event', '{n}'); |
|
| 92 | + $currentEvents = Hash::combine($current, '{n}.Esevent.event', '{n}'); |
|
| 93 | 93 | |
| 94 | - foreach ($oldEvents as $eventType => $oldEvent) { |
|
| 95 | - $newData = array(); |
|
| 96 | - $newData['Esnotification'] = $oldEvent['Esnotification']; |
|
| 97 | - if (isset($currentEvents[$eventType])) { |
|
| 98 | - $newData['Esevent']['id'] = $currentEvents[$eventType]['Esevent']['id']; |
|
| 99 | - } else { |
|
| 100 | - $newData['Esevent']['subject'] = $newSubject; |
|
| 101 | - $newData['Esevent']['event'] = $eventType; |
|
| 102 | - $this->create(); |
|
| 103 | - } |
|
| 104 | - $this->saveAssociated($newData); |
|
| 105 | - } |
|
| 106 | - $this->deleteAll( |
|
| 107 | - array( |
|
| 108 | - 'subject' => $oldSubject, |
|
| 109 | - 'event' => $this->_subjectTypes[$subjectType], |
|
| 110 | - ), |
|
| 111 | - false |
|
| 112 | - ); |
|
| 113 | - } |
|
| 94 | + foreach ($oldEvents as $eventType => $oldEvent) { |
|
| 95 | + $newData = array(); |
|
| 96 | + $newData['Esnotification'] = $oldEvent['Esnotification']; |
|
| 97 | + if (isset($currentEvents[$eventType])) { |
|
| 98 | + $newData['Esevent']['id'] = $currentEvents[$eventType]['Esevent']['id']; |
|
| 99 | + } else { |
|
| 100 | + $newData['Esevent']['subject'] = $newSubject; |
|
| 101 | + $newData['Esevent']['event'] = $eventType; |
|
| 102 | + $this->create(); |
|
| 103 | + } |
|
| 104 | + $this->saveAssociated($newData); |
|
| 105 | + } |
|
| 106 | + $this->deleteAll( |
|
| 107 | + array( |
|
| 108 | + 'subject' => $oldSubject, |
|
| 109 | + 'event' => $this->_subjectTypes[$subjectType], |
|
| 110 | + ), |
|
| 111 | + false |
|
| 112 | + ); |
|
| 113 | + } |
|
| 114 | 114 | |
| 115 | 115 | /** |
| 116 | 116 | * |
@@ -129,21 +129,21 @@ discard block |
||
| 129 | 129 | * ) |
| 130 | 130 | * </pre> |
| 131 | 131 | */ |
| 132 | - public function notifyUserOnEvents($user, $events) { |
|
| 133 | - foreach ($events as $event) { |
|
| 134 | - $params = array( |
|
| 135 | - 'user_id' => $user, |
|
| 136 | - 'event' => $this->_eventTypes[$event['event']], |
|
| 137 | - 'subject' => $event['subject'], |
|
| 138 | - 'receiver' => $this->_receivers[$event['receiver']], |
|
| 139 | - ); |
|
| 140 | - if ($event['set']) { |
|
| 141 | - $this->setNotification($params); |
|
| 142 | - } else { |
|
| 143 | - $this->deleteNotification($params); |
|
| 144 | - } |
|
| 145 | - } |
|
| 146 | - } |
|
| 132 | + public function notifyUserOnEvents($user, $events) { |
|
| 133 | + foreach ($events as $event) { |
|
| 134 | + $params = array( |
|
| 135 | + 'user_id' => $user, |
|
| 136 | + 'event' => $this->_eventTypes[$event['event']], |
|
| 137 | + 'subject' => $event['subject'], |
|
| 138 | + 'receiver' => $this->_receivers[$event['receiver']], |
|
| 139 | + ); |
|
| 140 | + if ($event['set']) { |
|
| 141 | + $this->setNotification($params); |
|
| 142 | + } else { |
|
| 143 | + $this->deleteNotification($params); |
|
| 144 | + } |
|
| 145 | + } |
|
| 146 | + } |
|
| 147 | 147 | |
| 148 | 148 | /** |
| 149 | 149 | * |
@@ -159,54 +159,54 @@ discard block |
||
| 159 | 159 | * |
| 160 | 160 | * @return type |
| 161 | 161 | */ |
| 162 | - public function setNotification($params) { |
|
| 163 | - $isSet = $this->_getEventSet($params); |
|
| 164 | - $success = true; |
|
| 165 | - $eventData = []; |
|
| 166 | - if ($isSet['Esevent']) { |
|
| 167 | - $eventData = array( |
|
| 168 | - 'id' => $isSet['Esevent']['id'], |
|
| 169 | - ); |
|
| 170 | - } else { |
|
| 171 | - $eventData = array( |
|
| 172 | - 'subject' => $params['subject'], |
|
| 173 | - 'event' => $params['event'], |
|
| 174 | - ); |
|
| 175 | - } |
|
| 176 | - if (!$isSet['Esnotification']) { |
|
| 177 | - $data = array( |
|
| 178 | - 'Esevent' => $eventData, |
|
| 179 | - 'Esnotification' => array( |
|
| 180 | - array( |
|
| 181 | - 'user_id' => $params['user_id'], |
|
| 182 | - 'esreceiver_id' => $params['receiver'], |
|
| 183 | - ), |
|
| 184 | - ), |
|
| 185 | - ); |
|
| 186 | - $success = $success && $this->saveAssociated($data); |
|
| 187 | - } |
|
| 188 | - return $success; |
|
| 189 | - } |
|
| 162 | + public function setNotification($params) { |
|
| 163 | + $isSet = $this->_getEventSet($params); |
|
| 164 | + $success = true; |
|
| 165 | + $eventData = []; |
|
| 166 | + if ($isSet['Esevent']) { |
|
| 167 | + $eventData = array( |
|
| 168 | + 'id' => $isSet['Esevent']['id'], |
|
| 169 | + ); |
|
| 170 | + } else { |
|
| 171 | + $eventData = array( |
|
| 172 | + 'subject' => $params['subject'], |
|
| 173 | + 'event' => $params['event'], |
|
| 174 | + ); |
|
| 175 | + } |
|
| 176 | + if (!$isSet['Esnotification']) { |
|
| 177 | + $data = array( |
|
| 178 | + 'Esevent' => $eventData, |
|
| 179 | + 'Esnotification' => array( |
|
| 180 | + array( |
|
| 181 | + 'user_id' => $params['user_id'], |
|
| 182 | + 'esreceiver_id' => $params['receiver'], |
|
| 183 | + ), |
|
| 184 | + ), |
|
| 185 | + ); |
|
| 186 | + $success = $success && $this->saveAssociated($data); |
|
| 187 | + } |
|
| 188 | + return $success; |
|
| 189 | + } |
|
| 190 | 190 | |
| 191 | 191 | /** |
| 192 | 192 | * Deletes Subject and all its Notifications |
| 193 | 193 | */ |
| 194 | - public function deleteSubject($subjectId, $subjectType) { |
|
| 195 | - // remove all events |
|
| 196 | - $this->deleteAll(array( |
|
| 197 | - 'subject' => $subjectId, |
|
| 198 | - 'event' => $this->_subjectTypes[$subjectType], |
|
| 199 | - ), true); |
|
| 200 | - } |
|
| 194 | + public function deleteSubject($subjectId, $subjectType) { |
|
| 195 | + // remove all events |
|
| 196 | + $this->deleteAll(array( |
|
| 197 | + 'subject' => $subjectId, |
|
| 198 | + 'event' => $this->_subjectTypes[$subjectType], |
|
| 199 | + ), true); |
|
| 200 | + } |
|
| 201 | 201 | |
| 202 | - public function deleteNotification($params) { |
|
| 203 | - extract($params); |
|
| 202 | + public function deleteNotification($params) { |
|
| 203 | + extract($params); |
|
| 204 | 204 | |
| 205 | - $isSet = $this->_getEventSet($params); |
|
| 206 | - if ($isSet['Esnotification']) { |
|
| 207 | - return $this->Esnotification->deleteNotificationWithId($isSet['Esnotification'][0]['id']); |
|
| 208 | - } |
|
| 209 | - } |
|
| 205 | + $isSet = $this->_getEventSet($params); |
|
| 206 | + if ($isSet['Esnotification']) { |
|
| 207 | + return $this->Esnotification->deleteNotificationWithId($isSet['Esnotification'][0]['id']); |
|
| 208 | + } |
|
| 209 | + } |
|
| 210 | 210 | |
| 211 | 211 | /** |
| 212 | 212 | * Checks if specific notification is set |
@@ -224,26 +224,26 @@ discard block |
||
| 224 | 224 | * |
| 225 | 225 | * @return mixed array with found set or false otherwise |
| 226 | 226 | */ |
| 227 | - protected function _getEventSet($params) { |
|
| 228 | - $results = $this->find( |
|
| 229 | - 'first', |
|
| 230 | - array( |
|
| 231 | - 'contain' => array( |
|
| 232 | - 'Esnotification' => array( |
|
| 233 | - 'conditions' => array( |
|
| 234 | - 'user_id' => $params['user_id'], |
|
| 235 | - 'esreceiver_id' => $params['receiver'], |
|
| 236 | - ), |
|
| 237 | - ), |
|
| 238 | - ), |
|
| 239 | - 'conditions' => array( |
|
| 240 | - 'Esevent.subject' => $params['subject'], |
|
| 241 | - 'Esevent.event' => $params['event'], |
|
| 242 | - ) |
|
| 243 | - ) |
|
| 244 | - ); |
|
| 245 | - return empty($results) ? false : $results; |
|
| 246 | - } |
|
| 227 | + protected function _getEventSet($params) { |
|
| 228 | + $results = $this->find( |
|
| 229 | + 'first', |
|
| 230 | + array( |
|
| 231 | + 'contain' => array( |
|
| 232 | + 'Esnotification' => array( |
|
| 233 | + 'conditions' => array( |
|
| 234 | + 'user_id' => $params['user_id'], |
|
| 235 | + 'esreceiver_id' => $params['receiver'], |
|
| 236 | + ), |
|
| 237 | + ), |
|
| 238 | + ), |
|
| 239 | + 'conditions' => array( |
|
| 240 | + 'Esevent.subject' => $params['subject'], |
|
| 241 | + 'Esevent.event' => $params['event'], |
|
| 242 | + ) |
|
| 243 | + ) |
|
| 244 | + ); |
|
| 245 | + return empty($results) ? false : $results; |
|
| 246 | + } |
|
| 247 | 247 | |
| 248 | 248 | /** |
| 249 | 249 | * |
@@ -269,41 +269,41 @@ discard block |
||
| 269 | 269 | * ) |
| 270 | 270 | * </pre> |
| 271 | 271 | */ |
| 272 | - public function getUsersForEventOnSubjectWithReceiver($eventName, $subject, $receiver) { |
|
| 273 | - $recipients = array(); |
|
| 274 | - $results = $this->find( |
|
| 275 | - 'all', |
|
| 276 | - array( |
|
| 277 | - 'conditions' => array( |
|
| 278 | - 'Esevent.event' => $this->_eventTypes[$eventName], |
|
| 279 | - 'Esevent.subject' => $subject, |
|
| 280 | - ), |
|
| 281 | - 'contain' => array( |
|
| 282 | - 'Esnotification' => array( |
|
| 283 | - 'fields' => array( |
|
| 284 | - 'Esnotification.id', |
|
| 285 | - 'Esnotification.deactivate' |
|
| 286 | - ), |
|
| 287 | - 'conditions' => array( |
|
| 288 | - 'esreceiver_id' => $this->_receivers[$receiver], |
|
| 289 | - ), |
|
| 290 | - 'User' => array( |
|
| 291 | - 'fields' => array('id', 'username', 'user_email'), |
|
| 292 | - ) |
|
| 293 | - ), |
|
| 294 | - ), |
|
| 295 | - ) |
|
| 296 | - ); |
|
| 297 | - if ($results) { |
|
| 298 | - $recipients = Hash::map($results, '{n}.Esnotification.{n}', function ($values) { |
|
| 299 | - $out = $values['User']; |
|
| 300 | - unset($values['User']); |
|
| 301 | - $out['Esnotification'] = $values; |
|
| 302 | - return $out; |
|
| 303 | - }); |
|
| 304 | - } |
|
| 305 | - return $recipients; |
|
| 306 | - } |
|
| 272 | + public function getUsersForEventOnSubjectWithReceiver($eventName, $subject, $receiver) { |
|
| 273 | + $recipients = array(); |
|
| 274 | + $results = $this->find( |
|
| 275 | + 'all', |
|
| 276 | + array( |
|
| 277 | + 'conditions' => array( |
|
| 278 | + 'Esevent.event' => $this->_eventTypes[$eventName], |
|
| 279 | + 'Esevent.subject' => $subject, |
|
| 280 | + ), |
|
| 281 | + 'contain' => array( |
|
| 282 | + 'Esnotification' => array( |
|
| 283 | + 'fields' => array( |
|
| 284 | + 'Esnotification.id', |
|
| 285 | + 'Esnotification.deactivate' |
|
| 286 | + ), |
|
| 287 | + 'conditions' => array( |
|
| 288 | + 'esreceiver_id' => $this->_receivers[$receiver], |
|
| 289 | + ), |
|
| 290 | + 'User' => array( |
|
| 291 | + 'fields' => array('id', 'username', 'user_email'), |
|
| 292 | + ) |
|
| 293 | + ), |
|
| 294 | + ), |
|
| 295 | + ) |
|
| 296 | + ); |
|
| 297 | + if ($results) { |
|
| 298 | + $recipients = Hash::map($results, '{n}.Esnotification.{n}', function ($values) { |
|
| 299 | + $out = $values['User']; |
|
| 300 | + unset($values['User']); |
|
| 301 | + $out['Esnotification'] = $values; |
|
| 302 | + return $out; |
|
| 303 | + }); |
|
| 304 | + } |
|
| 305 | + return $recipients; |
|
| 306 | + } |
|
| 307 | 307 | |
| 308 | 308 | /** |
| 309 | 309 | * |
@@ -326,32 +326,32 @@ discard block |
||
| 326 | 326 | * 1 => [true|false], |
| 327 | 327 | * ) |
| 328 | 328 | */ |
| 329 | - public function checkEventsForUser($user, $events) { |
|
| 330 | - // Stopwatch::enable(); Stopwatch::start('Event->checkEventsForUser()'); |
|
| 329 | + public function checkEventsForUser($user, $events) { |
|
| 330 | + // Stopwatch::enable(); Stopwatch::start('Event->checkEventsForUser()'); |
|
| 331 | 331 | |
| 332 | - $subjects = array(); |
|
| 333 | - foreach ($events as $event) { |
|
| 334 | - $subjects[] = $event['subject']; |
|
| 335 | - } |
|
| 332 | + $subjects = array(); |
|
| 333 | + foreach ($events as $event) { |
|
| 334 | + $subjects[] = $event['subject']; |
|
| 335 | + } |
|
| 336 | 336 | |
| 337 | - $notis = $this->_getEventsForUserOnSubjects($user, $subjects); |
|
| 338 | - $out = array(); |
|
| 337 | + $notis = $this->_getEventsForUserOnSubjects($user, $subjects); |
|
| 338 | + $out = array(); |
|
| 339 | 339 | |
| 340 | - foreach ($events as $k => $event) { |
|
| 341 | - foreach ($notis as $noti) { |
|
| 342 | - if ($noti['subject'] == $event['subject'] |
|
| 343 | - && $noti['event'] == $this->_eventTypes[$event['event']] |
|
| 344 | - && $noti['receiver'] == $this->_receivers[$event['receiver']]) { |
|
| 345 | - $out[$k] = true; |
|
| 346 | - break; |
|
| 347 | - } else { |
|
| 348 | - $out[$k] = false; |
|
| 349 | - } |
|
| 350 | - } |
|
| 351 | - } |
|
| 352 | - // Stopwatch::end('Event->checkEventsForUser()'); debug(Stopwatch::getString()); |
|
| 353 | - return $out; |
|
| 354 | - } |
|
| 340 | + foreach ($events as $k => $event) { |
|
| 341 | + foreach ($notis as $noti) { |
|
| 342 | + if ($noti['subject'] == $event['subject'] |
|
| 343 | + && $noti['event'] == $this->_eventTypes[$event['event']] |
|
| 344 | + && $noti['receiver'] == $this->_receivers[$event['receiver']]) { |
|
| 345 | + $out[$k] = true; |
|
| 346 | + break; |
|
| 347 | + } else { |
|
| 348 | + $out[$k] = false; |
|
| 349 | + } |
|
| 350 | + } |
|
| 351 | + } |
|
| 352 | + // Stopwatch::end('Event->checkEventsForUser()'); debug(Stopwatch::getString()); |
|
| 353 | + return $out; |
|
| 354 | + } |
|
| 355 | 355 | |
| 356 | 356 | /** |
| 357 | 357 | * |
@@ -369,49 +369,49 @@ discard block |
||
| 369 | 369 | * |
| 370 | 370 | * ) |
| 371 | 371 | */ |
| 372 | - protected function _getEventsForUserOnSubjects($user, $subjects) { |
|
| 373 | - $notis = $this->Esnotification->find( |
|
| 374 | - 'all', |
|
| 375 | - array( |
|
| 376 | - 'joins' => array( |
|
| 377 | - array( |
|
| 378 | - 'table' => 'esevents', |
|
| 379 | - 'alias' => 'Eseventa', |
|
| 380 | - 'type' => 'LEFT', |
|
| 381 | - 'conditions' => array( |
|
| 382 | - 'Eseventa.id = Esnotification.esevent_id' |
|
| 383 | - ) |
|
| 384 | - ) |
|
| 385 | - ), |
|
| 386 | - 'conditions' => array( |
|
| 387 | - 'Esnotification.user_id' => $user, |
|
| 388 | - 'Esevent.subject' => $subjects |
|
| 389 | - ), |
|
| 390 | - 'fields' => array( |
|
| 391 | - 'Esnotification.id', |
|
| 392 | - 'Esnotification.user_id', |
|
| 393 | - 'Esnotification.esevent_id', |
|
| 394 | - 'Esnotification.esreceiver_id', |
|
| 395 | - 'Esevent.event', |
|
| 396 | - 'Esevent.subject' |
|
| 397 | - ) |
|
| 398 | - ) |
|
| 399 | - ); |
|
| 400 | - $out = $notis; |
|
| 401 | - if ($notis) { |
|
| 402 | - $out = array(); |
|
| 403 | - foreach ($notis as $noti) { |
|
| 404 | - $out[] = array( |
|
| 405 | - 'user_id' => $noti['Esnotification']['user_id'], |
|
| 406 | - 'esevent_id' => $noti['Esnotification']['esevent_id'], |
|
| 407 | - 'esnotification_id' => $noti['Esnotification']['id'], |
|
| 408 | - 'event' => $noti['Esevent']['event'], |
|
| 409 | - 'subject' => $noti['Esevent']['subject'], |
|
| 410 | - 'receiver' => $noti['Esnotification']['esreceiver_id'], |
|
| 411 | - ); |
|
| 412 | - } |
|
| 413 | - } |
|
| 414 | - return $out; |
|
| 415 | - } |
|
| 372 | + protected function _getEventsForUserOnSubjects($user, $subjects) { |
|
| 373 | + $notis = $this->Esnotification->find( |
|
| 374 | + 'all', |
|
| 375 | + array( |
|
| 376 | + 'joins' => array( |
|
| 377 | + array( |
|
| 378 | + 'table' => 'esevents', |
|
| 379 | + 'alias' => 'Eseventa', |
|
| 380 | + 'type' => 'LEFT', |
|
| 381 | + 'conditions' => array( |
|
| 382 | + 'Eseventa.id = Esnotification.esevent_id' |
|
| 383 | + ) |
|
| 384 | + ) |
|
| 385 | + ), |
|
| 386 | + 'conditions' => array( |
|
| 387 | + 'Esnotification.user_id' => $user, |
|
| 388 | + 'Esevent.subject' => $subjects |
|
| 389 | + ), |
|
| 390 | + 'fields' => array( |
|
| 391 | + 'Esnotification.id', |
|
| 392 | + 'Esnotification.user_id', |
|
| 393 | + 'Esnotification.esevent_id', |
|
| 394 | + 'Esnotification.esreceiver_id', |
|
| 395 | + 'Esevent.event', |
|
| 396 | + 'Esevent.subject' |
|
| 397 | + ) |
|
| 398 | + ) |
|
| 399 | + ); |
|
| 400 | + $out = $notis; |
|
| 401 | + if ($notis) { |
|
| 402 | + $out = array(); |
|
| 403 | + foreach ($notis as $noti) { |
|
| 404 | + $out[] = array( |
|
| 405 | + 'user_id' => $noti['Esnotification']['user_id'], |
|
| 406 | + 'esevent_id' => $noti['Esnotification']['esevent_id'], |
|
| 407 | + 'esnotification_id' => $noti['Esnotification']['id'], |
|
| 408 | + 'event' => $noti['Esevent']['event'], |
|
| 409 | + 'subject' => $noti['Esevent']['subject'], |
|
| 410 | + 'receiver' => $noti['Esnotification']['esreceiver_id'], |
|
| 411 | + ); |
|
| 412 | + } |
|
| 413 | + } |
|
| 414 | + return $out; |
|
| 415 | + } |
|
| 416 | 416 | |
| 417 | - } |
|
| 417 | + } |
|
@@ -1,6 +1,6 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | - App::uses('AppModel', 'Model'); |
|
| 3 | + App::uses('AppModel', 'Model'); |
|
| 4 | 4 | |
| 5 | 5 | /** |
| 6 | 6 | * Bookmark Model |
@@ -8,73 +8,73 @@ discard block |
||
| 8 | 8 | * @property User $User |
| 9 | 9 | * @property Entry $Entry |
| 10 | 10 | */ |
| 11 | - class Bookmark extends AppModel { |
|
| 11 | + class Bookmark extends AppModel { |
|
| 12 | 12 | |
| 13 | - public $actsAs = array( |
|
| 14 | - 'Containable', |
|
| 15 | - ); |
|
| 13 | + public $actsAs = array( |
|
| 14 | + 'Containable', |
|
| 15 | + ); |
|
| 16 | 16 | |
| 17 | 17 | /** |
| 18 | 18 | * Validation rules |
| 19 | 19 | * |
| 20 | 20 | * @var array |
| 21 | 21 | */ |
| 22 | - public $validate = array( |
|
| 23 | - 'user_id' => array( |
|
| 24 | - 'numeric' => array( |
|
| 25 | - 'rule' => array('validateUniqueBookmark'), |
|
| 26 | - //'message' => 'Your custom message here', |
|
| 27 | - //'allowEmpty' => false, |
|
| 28 | - 'required' => false, |
|
| 29 | - //'last' => false, // Stop validation after this rule |
|
| 30 | - 'on' => 'create', |
|
| 31 | - // Limit validation to 'create' or 'update' operations |
|
| 32 | - ), |
|
| 33 | - ), |
|
| 34 | - 'entry_id' => array( |
|
| 35 | - 'numeric' => array( |
|
| 36 | - 'rule' => array('validateUniqueBookmark'), |
|
| 37 | - //'message' => 'Your custom message here', |
|
| 38 | - //'allowEmpty' => false, |
|
| 39 | - 'required' => false, |
|
| 40 | - //'last' => false, // Stop validation after this rule |
|
| 41 | - 'on' => 'create', |
|
| 42 | - // Limit validation to 'create' or 'update' operations |
|
| 43 | - ), |
|
| 44 | - ), |
|
| 45 | - ); |
|
| 22 | + public $validate = array( |
|
| 23 | + 'user_id' => array( |
|
| 24 | + 'numeric' => array( |
|
| 25 | + 'rule' => array('validateUniqueBookmark'), |
|
| 26 | + //'message' => 'Your custom message here', |
|
| 27 | + //'allowEmpty' => false, |
|
| 28 | + 'required' => false, |
|
| 29 | + //'last' => false, // Stop validation after this rule |
|
| 30 | + 'on' => 'create', |
|
| 31 | + // Limit validation to 'create' or 'update' operations |
|
| 32 | + ), |
|
| 33 | + ), |
|
| 34 | + 'entry_id' => array( |
|
| 35 | + 'numeric' => array( |
|
| 36 | + 'rule' => array('validateUniqueBookmark'), |
|
| 37 | + //'message' => 'Your custom message here', |
|
| 38 | + //'allowEmpty' => false, |
|
| 39 | + 'required' => false, |
|
| 40 | + //'last' => false, // Stop validation after this rule |
|
| 41 | + 'on' => 'create', |
|
| 42 | + // Limit validation to 'create' or 'update' operations |
|
| 43 | + ), |
|
| 44 | + ), |
|
| 45 | + ); |
|
| 46 | 46 | |
| 47 | - //The Associations below have been created with all possible keys, those that are not needed can be removed |
|
| 47 | + //The Associations below have been created with all possible keys, those that are not needed can be removed |
|
| 48 | 48 | |
| 49 | 49 | /** |
| 50 | 50 | * belongsTo associations |
| 51 | 51 | * |
| 52 | 52 | * @var array |
| 53 | 53 | */ |
| 54 | - public $belongsTo = array( |
|
| 55 | - 'User' => array( |
|
| 56 | - 'className' => 'User', |
|
| 57 | - 'foreignKey' => 'user_id', |
|
| 58 | - 'conditions' => '', |
|
| 59 | - 'fields' => '', |
|
| 60 | - 'order' => '' |
|
| 61 | - ), |
|
| 62 | - 'Entry' => array( |
|
| 63 | - 'className' => 'Entry', |
|
| 64 | - 'foreignKey' => 'entry_id', |
|
| 65 | - 'conditions' => '', |
|
| 66 | - 'fields' => '', |
|
| 67 | - 'order' => '' |
|
| 68 | - ) |
|
| 69 | - ); |
|
| 54 | + public $belongsTo = array( |
|
| 55 | + 'User' => array( |
|
| 56 | + 'className' => 'User', |
|
| 57 | + 'foreignKey' => 'user_id', |
|
| 58 | + 'conditions' => '', |
|
| 59 | + 'fields' => '', |
|
| 60 | + 'order' => '' |
|
| 61 | + ), |
|
| 62 | + 'Entry' => array( |
|
| 63 | + 'className' => 'Entry', |
|
| 64 | + 'foreignKey' => 'entry_id', |
|
| 65 | + 'conditions' => '', |
|
| 66 | + 'fields' => '', |
|
| 67 | + 'order' => '' |
|
| 68 | + ) |
|
| 69 | + ); |
|
| 70 | 70 | |
| 71 | - public function validateUniqueBookmark() { |
|
| 72 | - $fields = array( |
|
| 73 | - $this->alias . '.user_id' => $this->data['Bookmark']['user_id'], |
|
| 74 | - $this->alias . '.entry_id' => $this->data['Bookmark']['entry_id'], |
|
| 75 | - ); |
|
| 76 | - return $this->isUnique($fields, false); |
|
| 77 | - } |
|
| 71 | + public function validateUniqueBookmark() { |
|
| 72 | + $fields = array( |
|
| 73 | + $this->alias . '.user_id' => $this->data['Bookmark']['user_id'], |
|
| 74 | + $this->alias . '.entry_id' => $this->data['Bookmark']['entry_id'], |
|
| 75 | + ); |
|
| 76 | + return $this->isUnique($fields, false); |
|
| 77 | + } |
|
| 78 | 78 | |
| 79 | 79 | /** |
| 80 | 80 | * |
@@ -82,18 +82,18 @@ discard block |
||
| 82 | 82 | * @param int $user_id |
| 83 | 83 | * @return bool |
| 84 | 84 | */ |
| 85 | - public function isBookmarked($entryId, $userId) { |
|
| 86 | - $result = $this->find( |
|
| 87 | - 'first', |
|
| 88 | - array( |
|
| 89 | - 'contain' => false, |
|
| 90 | - 'conditions' => array( |
|
| 91 | - $this->alias . '.entry_id' => $entryId, |
|
| 92 | - $this->alias . '.user_id' => $userId, |
|
| 93 | - ) |
|
| 94 | - ) |
|
| 95 | - ); |
|
| 96 | - return $result == true; |
|
| 97 | - } |
|
| 85 | + public function isBookmarked($entryId, $userId) { |
|
| 86 | + $result = $this->find( |
|
| 87 | + 'first', |
|
| 88 | + array( |
|
| 89 | + 'contain' => false, |
|
| 90 | + 'conditions' => array( |
|
| 91 | + $this->alias . '.entry_id' => $entryId, |
|
| 92 | + $this->alias . '.user_id' => $userId, |
|
| 93 | + ) |
|
| 94 | + ) |
|
| 95 | + ); |
|
| 96 | + return $result == true; |
|
| 97 | + } |
|
| 98 | 98 | |
| 99 | - } |
|
| 99 | + } |
|