@@ -106,6 +106,10 @@ |
||
| 106 | 106 | $this->out("Generated $i users."); |
| 107 | 107 | } |
| 108 | 108 | |
| 109 | + /** |
|
| 110 | + * @param integer $i |
|
| 111 | + * @param integer $off |
|
| 112 | + */ |
|
| 109 | 113 | protected function _progress($i, $off) { |
| 110 | 114 | if ($i < 1) { |
| 111 | 115 | return; |
@@ -31,11 +31,11 @@ discard block |
||
| 31 | 31 | } |
| 32 | 32 | |
| 33 | 33 | public function generate() { |
| 34 | - $nPostings = (int)$this->in('Number of postings to generate?', null, 100); |
|
| 34 | + $nPostings = (int) $this->in('Number of postings to generate?', null, 100); |
|
| 35 | 35 | if ($nPostings === 0) { |
| 36 | 36 | return; |
| 37 | 37 | } |
| 38 | - $ratio = (int)$this->in('Average answers per thread?', null, 10); |
|
| 38 | + $ratio = (int) $this->in('Average answers per thread?', null, 10); |
|
| 39 | 39 | $seed = $nPostings / $ratio; |
| 40 | 40 | |
| 41 | 41 | new Saito\Markup\Settings([ |
@@ -64,7 +64,7 @@ discard block |
||
| 64 | 64 | } |
| 65 | 65 | $entry = $this->Entry->createPosting(['Entry' => $entry]); |
| 66 | 66 | if (empty($entry)) { |
| 67 | - throw new RuntimeException('Could not create entry: ' . $entry); |
|
| 67 | + throw new RuntimeException('Could not create entry: '.$entry); |
|
| 68 | 68 | } |
| 69 | 69 | |
| 70 | 70 | $this->_progress($i, $nPostings); |
@@ -79,7 +79,7 @@ discard block |
||
| 79 | 79 | |
| 80 | 80 | public function user() { |
| 81 | 81 | $max = count($this->_users); |
| 82 | - $n = (int)$this->in("Number of users to generate (max: $max)?", null, 0); |
|
| 82 | + $n = (int) $this->in("Number of users to generate (max: $max)?", null, 0); |
|
| 83 | 83 | if ($n === 0) { |
| 84 | 84 | return; |
| 85 | 85 | } |
@@ -112,7 +112,7 @@ discard block |
||
| 112 | 112 | } |
| 113 | 113 | $this->out('.', 0); |
| 114 | 114 | if ($i > 1 && !($i % 50)) { |
| 115 | - $percent = (int)floor($i / $off * 100); |
|
| 115 | + $percent = (int) floor($i / $off * 100); |
|
| 116 | 116 | $this->out(sprintf(' %3s%%', $percent), 1); |
| 117 | 117 | } |
| 118 | 118 | } |
@@ -1,171 +1,171 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | - use Saito\User\Auth; |
|
| 4 | - use Saito\User\SaitoUser; |
|
| 5 | - |
|
| 6 | - class DummyDataShell extends AppShell { |
|
| 7 | - |
|
| 8 | - public $uses = ['Entry', 'User']; |
|
| 9 | - |
|
| 10 | - protected $_Categories = null; |
|
| 11 | - |
|
| 12 | - protected $_Users = null; |
|
| 13 | - |
|
| 14 | - protected $_text = null; |
|
| 15 | - |
|
| 16 | - protected $_Threads = []; |
|
| 17 | - |
|
| 18 | - protected $_users = ['aaron', 'Alex', 'Amy', 'Ana-Lucia', 'Anthony', 'Ben', |
|
| 19 | - 'Bernard', 'Boone', 'Carmen', 'Carole', 'Charles', 'Charlie', 'Charlotte', |
|
| 20 | - 'Christian', 'Claire', 'Daniel', 'Danielle', 'Desmond', 'Dogen', 'Eko', |
|
| 21 | - 'Eloise', 'Ethan', 'Frank', 'Frogurt', 'George', 'Gina', 'Horace', 'Hugo', |
|
| 22 | - 'Ilana', 'Jack', 'Jacob', 'James', 'Jin', 'John', 'Juliet', 'Kate', |
|
| 23 | - 'Kelvin', 'Liam', 'Libby', 'Martin', 'Maninbla', 'Michael', 'Michelle', |
|
| 24 | - 'Miles', 'Nadia', 'Naomi', 'Nikki', 'Omar', 'Paulo', 'Penny', 'Pierre', |
|
| 25 | - 'Richard', 'Sarah', 'Sayid', 'Shannon', 'Stuart', 'Sun', 'Teresa', 'Tom', |
|
| 26 | - 'walt']; |
|
| 27 | - |
|
| 28 | - public function main() { |
|
| 29 | - $this->user(); |
|
| 30 | - $this->generate(); |
|
| 31 | - } |
|
| 32 | - |
|
| 33 | - public function generate() { |
|
| 34 | - $nPostings = (int)$this->in('Number of postings to generate?', null, 100); |
|
| 35 | - if ($nPostings === 0) { |
|
| 36 | - return; |
|
| 37 | - } |
|
| 38 | - $ratio = (int)$this->in('Average answers per thread?', null, 10); |
|
| 39 | - $seed = $nPostings / $ratio; |
|
| 40 | - |
|
| 41 | - new Saito\Markup\Settings([ |
|
| 42 | - 'hashBaseUrl' => 'entries/view/', |
|
| 43 | - 'atBaseUrl' => 'users/name/', |
|
| 44 | - 'server' => Router::fullBaseUrl(), |
|
| 45 | - 'webroot' => Router::fullBaseUrl() |
|
| 46 | - ]); |
|
| 47 | - $this->Entry->SharedObjects['CurrentUser'] = new SaitoUserDummy(); |
|
| 48 | - |
|
| 49 | - for ($i = 0; $i < $nPostings; $i++) { |
|
| 50 | - $newThread = $i < $seed; |
|
| 51 | - |
|
| 52 | - $user = $this->_randomUser(); |
|
| 53 | - $this->Entry->CurrentUser->setSettings($user); |
|
| 54 | - |
|
| 55 | - $entry = [ |
|
| 56 | - 'subject' => $i, |
|
| 57 | - 'text' => rand(0, 1) ? $this->_randomText() : '', |
|
| 58 | - 'user_id' => $user['id'] |
|
| 59 | - ]; |
|
| 60 | - if ($newThread) { |
|
| 61 | - $entry['category'] = $this->_randomCategory(); |
|
| 62 | - } else { |
|
| 63 | - $entry['pid'] = array_rand($this->_Threads); |
|
| 64 | - } |
|
| 65 | - $entry = $this->Entry->createPosting(['Entry' => $entry]); |
|
| 66 | - if (empty($entry)) { |
|
| 67 | - throw new RuntimeException('Could not create entry: ' . $entry); |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - $this->_progress($i, $nPostings); |
|
| 71 | - |
|
| 72 | - $id = $entry['Entry']['id']; |
|
| 73 | - $this->_Threads[$id] = $id; |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - $this->out(); |
|
| 77 | - $this->out("Generated $i postings."); |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - public function user() { |
|
| 81 | - $max = count($this->_users); |
|
| 82 | - $n = (int)$this->in("Number of users to generate (max: $max)?", null, 0); |
|
| 83 | - if ($n === 0) { |
|
| 84 | - return; |
|
| 85 | - } |
|
| 86 | - if ($n > $max) { |
|
| 87 | - $n = $max; |
|
| 88 | - } |
|
| 89 | - $users = array_rand($this->_users, $n); |
|
| 90 | - $i = 0; |
|
| 91 | - foreach ($users as $user) { |
|
| 92 | - $name = $this->_users[$user]; |
|
| 93 | - $data = [ |
|
| 94 | - 'User' => [ |
|
| 95 | - 'username' => $name, |
|
| 96 | - 'password' => 'test', |
|
| 97 | - 'password_confirm' => 'test', |
|
| 98 | - 'user_email' => "[email protected]" |
|
| 99 | - ] |
|
| 100 | - ]; |
|
| 101 | - $this->User->register($data, true); |
|
| 102 | - $this->_progress($i++, $n); |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - $this->out(); |
|
| 106 | - $this->out("Generated $i users."); |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - protected function _progress($i, $off) { |
|
| 110 | - if ($i < 1) { |
|
| 111 | - return; |
|
| 112 | - } |
|
| 113 | - $this->out('.', 0); |
|
| 114 | - if ($i > 1 && !($i % 50)) { |
|
| 115 | - $percent = (int)floor($i / $off * 100); |
|
| 116 | - $this->out(sprintf(' %3s%%', $percent), 1); |
|
| 117 | - } |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - protected function _randomCategory() { |
|
| 121 | - if ($this->_Categories === null) { |
|
| 122 | - $this->_Categories = $this->Entry->Category->find('all', |
|
| 123 | - ['recursive' => 0, 'fields' => ['id']]); |
|
| 124 | - } |
|
| 125 | - $id = array_rand($this->_Categories); |
|
| 126 | - return $this->_Categories[$id]['Category']['id']; |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - protected function _randomUser() { |
|
| 130 | - if ($this->_Users === null) { |
|
| 131 | - $this->_Users = $this->User->find('all', |
|
| 132 | - ['recursive' => -1, 'conditions' => ['activate_code' => 0]]); |
|
| 133 | - } |
|
| 134 | - $id = array_rand($this->_Users); |
|
| 135 | - return $this->_Users[$id]['User']; |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - protected function _randomText() { |
|
| 139 | - if (empty($this->_text)) { |
|
| 140 | - $this->_text = file_get_contents('http://loripsum.net/api/short/plaintext'); |
|
| 141 | - } |
|
| 142 | - return $this->_text; |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - } |
|
| 146 | - |
|
| 147 | - class SaitoUserDummy extends SaitoUser { |
|
| 148 | - |
|
| 149 | - public function __construct($settings = null) { |
|
| 150 | - parent::__construct($settings); |
|
| 151 | - $this->Categories = new Auth\CategoryAuthorization($this); |
|
| 152 | - } |
|
| 153 | - |
|
| 154 | - public function getMaxAccession() { |
|
| 155 | - return 2; |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - public function isLoggedIn() { |
|
| 159 | - return true; |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - public function isAdmin() { |
|
| 163 | - return true; |
|
| 164 | - } |
|
| 165 | - |
|
| 166 | - public function hasBookmarked() { |
|
| 167 | - return false; |
|
| 168 | - } |
|
| 169 | - |
|
| 170 | - } |
|
| 3 | + use Saito\User\Auth; |
|
| 4 | + use Saito\User\SaitoUser; |
|
| 5 | + |
|
| 6 | + class DummyDataShell extends AppShell { |
|
| 7 | + |
|
| 8 | + public $uses = ['Entry', 'User']; |
|
| 9 | + |
|
| 10 | + protected $_Categories = null; |
|
| 11 | + |
|
| 12 | + protected $_Users = null; |
|
| 13 | + |
|
| 14 | + protected $_text = null; |
|
| 15 | + |
|
| 16 | + protected $_Threads = []; |
|
| 17 | + |
|
| 18 | + protected $_users = ['aaron', 'Alex', 'Amy', 'Ana-Lucia', 'Anthony', 'Ben', |
|
| 19 | + 'Bernard', 'Boone', 'Carmen', 'Carole', 'Charles', 'Charlie', 'Charlotte', |
|
| 20 | + 'Christian', 'Claire', 'Daniel', 'Danielle', 'Desmond', 'Dogen', 'Eko', |
|
| 21 | + 'Eloise', 'Ethan', 'Frank', 'Frogurt', 'George', 'Gina', 'Horace', 'Hugo', |
|
| 22 | + 'Ilana', 'Jack', 'Jacob', 'James', 'Jin', 'John', 'Juliet', 'Kate', |
|
| 23 | + 'Kelvin', 'Liam', 'Libby', 'Martin', 'Maninbla', 'Michael', 'Michelle', |
|
| 24 | + 'Miles', 'Nadia', 'Naomi', 'Nikki', 'Omar', 'Paulo', 'Penny', 'Pierre', |
|
| 25 | + 'Richard', 'Sarah', 'Sayid', 'Shannon', 'Stuart', 'Sun', 'Teresa', 'Tom', |
|
| 26 | + 'walt']; |
|
| 27 | + |
|
| 28 | + public function main() { |
|
| 29 | + $this->user(); |
|
| 30 | + $this->generate(); |
|
| 31 | + } |
|
| 32 | + |
|
| 33 | + public function generate() { |
|
| 34 | + $nPostings = (int)$this->in('Number of postings to generate?', null, 100); |
|
| 35 | + if ($nPostings === 0) { |
|
| 36 | + return; |
|
| 37 | + } |
|
| 38 | + $ratio = (int)$this->in('Average answers per thread?', null, 10); |
|
| 39 | + $seed = $nPostings / $ratio; |
|
| 40 | + |
|
| 41 | + new Saito\Markup\Settings([ |
|
| 42 | + 'hashBaseUrl' => 'entries/view/', |
|
| 43 | + 'atBaseUrl' => 'users/name/', |
|
| 44 | + 'server' => Router::fullBaseUrl(), |
|
| 45 | + 'webroot' => Router::fullBaseUrl() |
|
| 46 | + ]); |
|
| 47 | + $this->Entry->SharedObjects['CurrentUser'] = new SaitoUserDummy(); |
|
| 48 | + |
|
| 49 | + for ($i = 0; $i < $nPostings; $i++) { |
|
| 50 | + $newThread = $i < $seed; |
|
| 51 | + |
|
| 52 | + $user = $this->_randomUser(); |
|
| 53 | + $this->Entry->CurrentUser->setSettings($user); |
|
| 54 | + |
|
| 55 | + $entry = [ |
|
| 56 | + 'subject' => $i, |
|
| 57 | + 'text' => rand(0, 1) ? $this->_randomText() : '', |
|
| 58 | + 'user_id' => $user['id'] |
|
| 59 | + ]; |
|
| 60 | + if ($newThread) { |
|
| 61 | + $entry['category'] = $this->_randomCategory(); |
|
| 62 | + } else { |
|
| 63 | + $entry['pid'] = array_rand($this->_Threads); |
|
| 64 | + } |
|
| 65 | + $entry = $this->Entry->createPosting(['Entry' => $entry]); |
|
| 66 | + if (empty($entry)) { |
|
| 67 | + throw new RuntimeException('Could not create entry: ' . $entry); |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + $this->_progress($i, $nPostings); |
|
| 71 | + |
|
| 72 | + $id = $entry['Entry']['id']; |
|
| 73 | + $this->_Threads[$id] = $id; |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + $this->out(); |
|
| 77 | + $this->out("Generated $i postings."); |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + public function user() { |
|
| 81 | + $max = count($this->_users); |
|
| 82 | + $n = (int)$this->in("Number of users to generate (max: $max)?", null, 0); |
|
| 83 | + if ($n === 0) { |
|
| 84 | + return; |
|
| 85 | + } |
|
| 86 | + if ($n > $max) { |
|
| 87 | + $n = $max; |
|
| 88 | + } |
|
| 89 | + $users = array_rand($this->_users, $n); |
|
| 90 | + $i = 0; |
|
| 91 | + foreach ($users as $user) { |
|
| 92 | + $name = $this->_users[$user]; |
|
| 93 | + $data = [ |
|
| 94 | + 'User' => [ |
|
| 95 | + 'username' => $name, |
|
| 96 | + 'password' => 'test', |
|
| 97 | + 'password_confirm' => 'test', |
|
| 98 | + 'user_email' => "[email protected]" |
|
| 99 | + ] |
|
| 100 | + ]; |
|
| 101 | + $this->User->register($data, true); |
|
| 102 | + $this->_progress($i++, $n); |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + $this->out(); |
|
| 106 | + $this->out("Generated $i users."); |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + protected function _progress($i, $off) { |
|
| 110 | + if ($i < 1) { |
|
| 111 | + return; |
|
| 112 | + } |
|
| 113 | + $this->out('.', 0); |
|
| 114 | + if ($i > 1 && !($i % 50)) { |
|
| 115 | + $percent = (int)floor($i / $off * 100); |
|
| 116 | + $this->out(sprintf(' %3s%%', $percent), 1); |
|
| 117 | + } |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + protected function _randomCategory() { |
|
| 121 | + if ($this->_Categories === null) { |
|
| 122 | + $this->_Categories = $this->Entry->Category->find('all', |
|
| 123 | + ['recursive' => 0, 'fields' => ['id']]); |
|
| 124 | + } |
|
| 125 | + $id = array_rand($this->_Categories); |
|
| 126 | + return $this->_Categories[$id]['Category']['id']; |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + protected function _randomUser() { |
|
| 130 | + if ($this->_Users === null) { |
|
| 131 | + $this->_Users = $this->User->find('all', |
|
| 132 | + ['recursive' => -1, 'conditions' => ['activate_code' => 0]]); |
|
| 133 | + } |
|
| 134 | + $id = array_rand($this->_Users); |
|
| 135 | + return $this->_Users[$id]['User']; |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + protected function _randomText() { |
|
| 139 | + if (empty($this->_text)) { |
|
| 140 | + $this->_text = file_get_contents('http://loripsum.net/api/short/plaintext'); |
|
| 141 | + } |
|
| 142 | + return $this->_text; |
|
| 143 | + } |
|
| 144 | + |
|
| 145 | + } |
|
| 146 | + |
|
| 147 | + class SaitoUserDummy extends SaitoUser { |
|
| 148 | + |
|
| 149 | + public function __construct($settings = null) { |
|
| 150 | + parent::__construct($settings); |
|
| 151 | + $this->Categories = new Auth\CategoryAuthorization($this); |
|
| 152 | + } |
|
| 153 | + |
|
| 154 | + public function getMaxAccession() { |
|
| 155 | + return 2; |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + public function isLoggedIn() { |
|
| 159 | + return true; |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + public function isAdmin() { |
|
| 163 | + return true; |
|
| 164 | + } |
|
| 165 | + |
|
| 166 | + public function hasBookmarked() { |
|
| 167 | + return false; |
|
| 168 | + } |
|
| 169 | + |
|
| 170 | + } |
|
| 171 | 171 | |
@@ -42,12 +42,18 @@ |
||
| 42 | 42 | exec($command); |
| 43 | 43 | } |
| 44 | 44 | |
| 45 | + /** |
|
| 46 | + * @param string $urlFilePath |
|
| 47 | + */ |
|
| 45 | 48 | protected function _cleanup($urlFilePath) { |
| 46 | 49 | if (file_exists($urlFilePath)) { |
| 47 | 50 | unlink($urlFilePath); |
| 48 | 51 | } |
| 49 | 52 | } |
| 50 | 53 | |
| 54 | + /** |
|
| 55 | + * @param string[] $out |
|
| 56 | + */ |
|
| 51 | 57 | protected function _generateEntriesIndex(&$out) { |
| 52 | 58 | for ($i = 0; $i < 400; $i++) { |
| 53 | 59 | $out[] = $this->_prefix . 'entries/index/page:' . rand(1, 2); |
@@ -11,12 +11,12 @@ discard block |
||
| 11 | 11 | public function main() { |
| 12 | 12 | $out = []; |
| 13 | 13 | $base = rtrim($this->args[0], '/'); |
| 14 | - $in = $this->in('Base URL is: ' . $base, ['y', 'n'], 'y'); |
|
| 14 | + $in = $this->in('Base URL is: '.$base, ['y', 'n'], 'y'); |
|
| 15 | 15 | if ($in !== 'y') { |
| 16 | 16 | $this->out('Aborting.'); |
| 17 | 17 | return; |
| 18 | 18 | } |
| 19 | - $out[] = 'HOST=' . $base; |
|
| 19 | + $out[] = 'HOST='.$base; |
|
| 20 | 20 | $this->_generateEntriesIndex($out); |
| 21 | 21 | $this->_generateEntriesMix($out); |
| 22 | 22 | $this->_generateEntriesView($out); |
@@ -34,11 +34,11 @@ discard block |
||
| 34 | 34 | } |
| 35 | 35 | |
| 36 | 36 | protected function _siege(&$out) { |
| 37 | - $urlFilePath = TMP . 'url.txt'; |
|
| 37 | + $urlFilePath = TMP.'url.txt'; |
|
| 38 | 38 | $this->_cleanup($urlFilePath); |
| 39 | 39 | $this->createFile($urlFilePath, implode("\n", $out)); |
| 40 | - $command = 'siege -R ' . APP . '..' . DS . ".siegerc -f $urlFilePath"; |
|
| 41 | - $this->out('<info>Running: ' . $command . '</info>', 1, 'info'); |
|
| 40 | + $command = 'siege -R '.APP.'..'.DS.".siegerc -f $urlFilePath"; |
|
| 41 | + $this->out('<info>Running: '.$command.'</info>', 1, 'info'); |
|
| 42 | 42 | exec($command); |
| 43 | 43 | } |
| 44 | 44 | |
@@ -50,10 +50,10 @@ discard block |
||
| 50 | 50 | |
| 51 | 51 | protected function _generateEntriesIndex(&$out) { |
| 52 | 52 | for ($i = 0; $i < 400; $i++) { |
| 53 | - $out[] = $this->_prefix . 'entries/index/page:' . rand(1, 2); |
|
| 53 | + $out[] = $this->_prefix.'entries/index/page:'.rand(1, 2); |
|
| 54 | 54 | } |
| 55 | 55 | for ($i = 0; $i < 400; $i++) { |
| 56 | - $out[] = $this->_prefix . 'entries/index/page:' . rand(5, 10); |
|
| 56 | + $out[] = $this->_prefix.'entries/index/page:'.rand(5, 10); |
|
| 57 | 57 | } |
| 58 | 58 | } |
| 59 | 59 | |
@@ -66,7 +66,7 @@ discard block |
||
| 66 | 66 | ]); |
| 67 | 67 | |
| 68 | 68 | foreach ($entries as $entry) { |
| 69 | - $out[] = $this->_prefix . 'entries/mix/' . $entry['Entry']['id']; |
|
| 69 | + $out[] = $this->_prefix.'entries/mix/'.$entry['Entry']['id']; |
|
| 70 | 70 | } |
| 71 | 71 | } |
| 72 | 72 | |
@@ -79,7 +79,7 @@ discard block |
||
| 79 | 79 | ]); |
| 80 | 80 | |
| 81 | 81 | foreach ($entries as $entry) { |
| 82 | - $out[] = $this->_prefix . 'entries/view/' . $entry['Entry']['id']; |
|
| 82 | + $out[] = $this->_prefix.'entries/view/'.$entry['Entry']['id']; |
|
| 83 | 83 | } |
| 84 | 84 | } |
| 85 | 85 | |
@@ -1,87 +1,87 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | - class SiegeShell extends AppShell { |
|
| 3 | + class SiegeShell extends AppShell { |
|
| 4 | 4 | |
| 5 | - public $uses = [ |
|
| 6 | - 'Entry' |
|
| 7 | - ]; |
|
| 5 | + public $uses = [ |
|
| 6 | + 'Entry' |
|
| 7 | + ]; |
|
| 8 | 8 | |
| 9 | - protected $_prefix = '$(HOST)/'; |
|
| 9 | + protected $_prefix = '$(HOST)/'; |
|
| 10 | 10 | |
| 11 | - public function main() { |
|
| 12 | - $out = []; |
|
| 13 | - $base = rtrim($this->args[0], '/'); |
|
| 14 | - $in = $this->in('Base URL is: ' . $base, ['y', 'n'], 'y'); |
|
| 15 | - if ($in !== 'y') { |
|
| 16 | - $this->out('Aborting.'); |
|
| 17 | - return; |
|
| 18 | - } |
|
| 19 | - $out[] = 'HOST=' . $base; |
|
| 20 | - $this->_generateEntriesIndex($out); |
|
| 21 | - $this->_generateEntriesMix($out); |
|
| 22 | - $this->_generateEntriesView($out); |
|
| 23 | - $this->_siege($out); |
|
| 24 | - } |
|
| 11 | + public function main() { |
|
| 12 | + $out = []; |
|
| 13 | + $base = rtrim($this->args[0], '/'); |
|
| 14 | + $in = $this->in('Base URL is: ' . $base, ['y', 'n'], 'y'); |
|
| 15 | + if ($in !== 'y') { |
|
| 16 | + $this->out('Aborting.'); |
|
| 17 | + return; |
|
| 18 | + } |
|
| 19 | + $out[] = 'HOST=' . $base; |
|
| 20 | + $this->_generateEntriesIndex($out); |
|
| 21 | + $this->_generateEntriesMix($out); |
|
| 22 | + $this->_generateEntriesView($out); |
|
| 23 | + $this->_siege($out); |
|
| 24 | + } |
|
| 25 | 25 | |
| 26 | - public function getOptionParser() { |
|
| 27 | - $parser = parent::getOptionParser(); |
|
| 28 | - $parser->addArgument('url', |
|
| 29 | - [ |
|
| 30 | - 'help' => 'Saito base URL', |
|
| 31 | - 'required' => true |
|
| 32 | - ]); |
|
| 33 | - return $parser; |
|
| 34 | - } |
|
| 26 | + public function getOptionParser() { |
|
| 27 | + $parser = parent::getOptionParser(); |
|
| 28 | + $parser->addArgument('url', |
|
| 29 | + [ |
|
| 30 | + 'help' => 'Saito base URL', |
|
| 31 | + 'required' => true |
|
| 32 | + ]); |
|
| 33 | + return $parser; |
|
| 34 | + } |
|
| 35 | 35 | |
| 36 | - protected function _siege(&$out) { |
|
| 37 | - $urlFilePath = TMP . 'url.txt'; |
|
| 38 | - $this->_cleanup($urlFilePath); |
|
| 39 | - $this->createFile($urlFilePath, implode("\n", $out)); |
|
| 40 | - $command = 'siege -R ' . APP . '..' . DS . ".siegerc -f $urlFilePath"; |
|
| 41 | - $this->out('<info>Running: ' . $command . '</info>', 1, 'info'); |
|
| 42 | - exec($command); |
|
| 43 | - } |
|
| 36 | + protected function _siege(&$out) { |
|
| 37 | + $urlFilePath = TMP . 'url.txt'; |
|
| 38 | + $this->_cleanup($urlFilePath); |
|
| 39 | + $this->createFile($urlFilePath, implode("\n", $out)); |
|
| 40 | + $command = 'siege -R ' . APP . '..' . DS . ".siegerc -f $urlFilePath"; |
|
| 41 | + $this->out('<info>Running: ' . $command . '</info>', 1, 'info'); |
|
| 42 | + exec($command); |
|
| 43 | + } |
|
| 44 | 44 | |
| 45 | - protected function _cleanup($urlFilePath) { |
|
| 46 | - if (file_exists($urlFilePath)) { |
|
| 47 | - unlink($urlFilePath); |
|
| 48 | - } |
|
| 49 | - } |
|
| 45 | + protected function _cleanup($urlFilePath) { |
|
| 46 | + if (file_exists($urlFilePath)) { |
|
| 47 | + unlink($urlFilePath); |
|
| 48 | + } |
|
| 49 | + } |
|
| 50 | 50 | |
| 51 | - protected function _generateEntriesIndex(&$out) { |
|
| 52 | - for ($i = 0; $i < 400; $i++) { |
|
| 53 | - $out[] = $this->_prefix . 'entries/index/page:' . rand(1, 2); |
|
| 54 | - } |
|
| 55 | - for ($i = 0; $i < 400; $i++) { |
|
| 56 | - $out[] = $this->_prefix . 'entries/index/page:' . rand(5, 10); |
|
| 57 | - } |
|
| 58 | - } |
|
| 51 | + protected function _generateEntriesIndex(&$out) { |
|
| 52 | + for ($i = 0; $i < 400; $i++) { |
|
| 53 | + $out[] = $this->_prefix . 'entries/index/page:' . rand(1, 2); |
|
| 54 | + } |
|
| 55 | + for ($i = 0; $i < 400; $i++) { |
|
| 56 | + $out[] = $this->_prefix . 'entries/index/page:' . rand(5, 10); |
|
| 57 | + } |
|
| 58 | + } |
|
| 59 | 59 | |
| 60 | - protected function _generateEntriesMix(&$out) { |
|
| 61 | - $entries = $this->Entry->find('all', |
|
| 62 | - [ |
|
| 63 | - 'fields' => ['Entry.id'], |
|
| 64 | - 'conditions' => ['Entry.pid' => 0, 'Category.accession' => 1], |
|
| 65 | - 'limit' => '500' |
|
| 66 | - ]); |
|
| 60 | + protected function _generateEntriesMix(&$out) { |
|
| 61 | + $entries = $this->Entry->find('all', |
|
| 62 | + [ |
|
| 63 | + 'fields' => ['Entry.id'], |
|
| 64 | + 'conditions' => ['Entry.pid' => 0, 'Category.accession' => 1], |
|
| 65 | + 'limit' => '500' |
|
| 66 | + ]); |
|
| 67 | 67 | |
| 68 | - foreach ($entries as $entry) { |
|
| 69 | - $out[] = $this->_prefix . 'entries/mix/' . $entry['Entry']['id']; |
|
| 70 | - } |
|
| 71 | - } |
|
| 68 | + foreach ($entries as $entry) { |
|
| 69 | + $out[] = $this->_prefix . 'entries/mix/' . $entry['Entry']['id']; |
|
| 70 | + } |
|
| 71 | + } |
|
| 72 | 72 | |
| 73 | - protected function _generateEntriesView(&$out) { |
|
| 74 | - $entries = $this->Entry->find('all', |
|
| 75 | - [ |
|
| 76 | - 'fields' => ['Entry.id'], |
|
| 77 | - 'conditions' => ['Category.accession' => 1], |
|
| 78 | - 'limit' => '1000' |
|
| 79 | - ]); |
|
| 73 | + protected function _generateEntriesView(&$out) { |
|
| 74 | + $entries = $this->Entry->find('all', |
|
| 75 | + [ |
|
| 76 | + 'fields' => ['Entry.id'], |
|
| 77 | + 'conditions' => ['Category.accession' => 1], |
|
| 78 | + 'limit' => '1000' |
|
| 79 | + ]); |
|
| 80 | 80 | |
| 81 | - foreach ($entries as $entry) { |
|
| 82 | - $out[] = $this->_prefix . 'entries/view/' . $entry['Entry']['id']; |
|
| 83 | - } |
|
| 84 | - } |
|
| 81 | + foreach ($entries as $entry) { |
|
| 82 | + $out[] = $this->_prefix . 'entries/view/' . $entry['Entry']['id']; |
|
| 83 | + } |
|
| 84 | + } |
|
| 85 | 85 | |
| 86 | - } |
|
| 86 | + } |
|
| 87 | 87 | |
@@ -104,6 +104,10 @@ discard block |
||
| 104 | 104 | return $this->_countYearStats(['fields' => ['COUNT(DISTINCT `user_id`) AS `count`']]); |
| 105 | 105 | } |
| 106 | 106 | |
| 107 | + /** |
|
| 108 | + * @param string $model |
|
| 109 | + * @param string $field |
|
| 110 | + */ |
|
| 107 | 111 | protected function _getYearStats($model, $field) { |
| 108 | 112 | return $this->_countYearStats(['fields' => ['COUNT(*) AS `count`']], |
| 109 | 113 | [], $model, $field); |
@@ -147,6 +151,10 @@ discard block |
||
| 147 | 151 | return $this->_wrapData($data); |
| 148 | 152 | } |
| 149 | 153 | |
| 154 | + /** |
|
| 155 | + * @param string $model |
|
| 156 | + * @param string $field |
|
| 157 | + */ |
|
| 150 | 158 | protected function _getMonthStats($model, $field) { |
| 151 | 159 | $results = $this->_countYearStats( |
| 152 | 160 | ['fields' => ['COUNT(*) AS `count`']], |
@@ -23,7 +23,7 @@ discard block |
||
| 23 | 23 | // order here is output order in frontend |
| 24 | 24 | $_logsToRead = ['error', 'debug']; |
| 25 | 25 | |
| 26 | - $_logsToRead = glob(LOGS . '*.log'); |
|
| 26 | + $_logsToRead = glob(LOGS.'*.log'); |
|
| 27 | 27 | if (!$_logsToRead) { |
| 28 | 28 | return; |
| 29 | 29 | } |
@@ -142,7 +142,7 @@ discard block |
||
| 142 | 142 | $results = Hash::extract($results, '{n}.{n}'); |
| 143 | 143 | $data = []; |
| 144 | 144 | foreach ($results as $d) { |
| 145 | - $data[] = [(string)$d['date'], $d['count']]; |
|
| 145 | + $data[] = [(string) $d['date'], $d['count']]; |
|
| 146 | 146 | } |
| 147 | 147 | return $this->_wrapData($data); |
| 148 | 148 | } |
@@ -162,9 +162,9 @@ discard block |
||
| 162 | 162 | $diff = []; |
| 163 | 163 | $sum = 0; |
| 164 | 164 | foreach ($results as $reg) { |
| 165 | - $sum += (int)$reg[0]['count']; |
|
| 165 | + $sum += (int) $reg[0]['count']; |
|
| 166 | 166 | $cumulated[] = array($reg[0]['date'] * 1000, $sum); |
| 167 | - $diff[] = array($reg[0]['date'] * 1000, (int)$reg[0]['count']); |
|
| 167 | + $diff[] = array($reg[0]['date'] * 1000, (int) $reg[0]['count']); |
|
| 168 | 168 | } |
| 169 | 169 | return [ |
| 170 | 170 | $this->_wrapData($diff, ['label' => __('New')]), |
@@ -1,176 +1,176 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | - App::uses('AppController', 'Controller'); |
|
| 4 | - |
|
| 5 | - class AdminsController extends AppController { |
|
| 6 | - |
|
| 7 | - public $name = 'Admins'; |
|
| 8 | - |
|
| 9 | - public $helpers = ['Admin', 'Flot', 'Sitemap.Sitemap']; |
|
| 10 | - |
|
| 11 | - public function admin_index() { |
|
| 12 | - } |
|
| 13 | - |
|
| 14 | - /** |
|
| 15 | - * Show PHP-info |
|
| 16 | - * |
|
| 17 | - * @return void |
|
| 18 | - */ |
|
| 19 | - public function admin_phpinfo() { |
|
| 20 | - } |
|
| 21 | - |
|
| 22 | - public function admin_logs() { |
|
| 23 | - // order here is output order in frontend |
|
| 24 | - $_logsToRead = ['error', 'debug']; |
|
| 25 | - |
|
| 26 | - $_logsToRead = glob(LOGS . '*.log'); |
|
| 27 | - if (!$_logsToRead) { |
|
| 28 | - return; |
|
| 29 | - } |
|
| 30 | - |
|
| 31 | - // will contain ['error' => '<string>', 'debug' => '<string>'] |
|
| 32 | - $_logs = []; |
|
| 33 | - foreach ($_logsToRead as $_path) { |
|
| 34 | - $_content = ''; |
|
| 35 | - $_size = filesize($_path); |
|
| 36 | - $_content = file_get_contents($_path, false, null, $_size - 65536); |
|
| 37 | - $name = basename($_path); |
|
| 38 | - $_logs[$name] = $_content; |
|
| 39 | - } |
|
| 40 | - $this->set('logs', $_logs); |
|
| 41 | - } |
|
| 42 | - |
|
| 43 | - public function admin_plugins() { |
|
| 44 | - } |
|
| 45 | - |
|
| 46 | - public function admin_stats() { |
|
| 47 | - $postingsPA = $this->_getYearStats('Entry', 'time'); |
|
| 48 | - $registrationsPA = $this->_getYearStats('User', 'registered'); |
|
| 49 | - $activeUserPA = $this->_getUserWithPostingsPerYear(); |
|
| 50 | - $averagePostingsPerUserPA = $this->_getAveragePPU($postingsPA, |
|
| 51 | - $activeUserPA); |
|
| 52 | - $this->set(compact('averagePostingsPerUserPA', |
|
| 53 | - 'postingsPA', 'activeUserPA', 'registrationsPA')); |
|
| 54 | - $this->set('categoryPostingsPA', $this->_getCategoriesPerYear()); |
|
| 55 | - } |
|
| 56 | - |
|
| 57 | - public function admin_stats_details() { |
|
| 58 | - $this->set('registrations', |
|
| 59 | - $this->_getMonthStats('User', 'registered')); |
|
| 60 | - $this->set('entries', $this->_getMonthStats('Entry', 'time')); |
|
| 61 | - } |
|
| 62 | - |
|
| 63 | - protected function _getAveragePPU($postingsPerYear, $activeUserPerYear) { |
|
| 64 | - if (empty($postingsPerYear) || empty($activeUserPerYear)) { |
|
| 65 | - return false; |
|
| 66 | - } |
|
| 67 | - $avgPostingsPerUser = []; |
|
| 68 | - foreach ($postingsPerYear['data'] as $key => $data) { |
|
| 69 | - list($year, $postings) = $data; |
|
| 70 | - $activeUsers = $activeUserPerYear['data'][$key][1]; |
|
| 71 | - $avgPostingsPerUser[] = [$year, $postings / $activeUsers]; |
|
| 72 | - } |
|
| 73 | - return $this->_wrapData($avgPostingsPerUser); |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - protected function _getCategoriesPerYear() { |
|
| 77 | - $results = $this->_countYearStats([ |
|
| 78 | - 'contain' => ['Category'], |
|
| 79 | - 'fields' => ['COUNT(*) AS `count`', 'Category.category'], |
|
| 80 | - 'group' => ['Category.category'] |
|
| 81 | - ], |
|
| 82 | - ['raw' => true] |
|
| 83 | - ); |
|
| 84 | - if (empty($results)) { |
|
| 85 | - return false; |
|
| 86 | - } |
|
| 87 | - $data = []; |
|
| 88 | - foreach ($results as $dataset) { |
|
| 89 | - $category = $dataset['Category']['category']; |
|
| 90 | - $data[$category][] = [$dataset[0]['date'], $dataset[0]['count']]; |
|
| 91 | - } |
|
| 92 | - $out = []; |
|
| 93 | - foreach ($data as $category => $dataset) { |
|
| 94 | - $out[] = $this->_wrapData($dataset, ['label' => $category]); |
|
| 95 | - } |
|
| 96 | - return $out; |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - protected function _wrapData(&$data, array $options = []) { |
|
| 100 | - return ['data' => $data] + $options; |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - protected function _getUserWithPostingsPerYear() { |
|
| 104 | - return $this->_countYearStats(['fields' => ['COUNT(DISTINCT `user_id`) AS `count`']]); |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - protected function _getYearStats($model, $field) { |
|
| 108 | - return $this->_countYearStats(['fields' => ['COUNT(*) AS `count`']], |
|
| 109 | - [], $model, $field); |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - protected function _countYearStats($query, $params = [], $model = 'Entry', $field = 'time') { |
|
| 113 | - $params += [ |
|
| 114 | - 'raw' => false, |
|
| 115 | - 'resolution' => 'year' |
|
| 116 | - ]; |
|
| 117 | - |
|
| 118 | - $defaults = [ |
|
| 119 | - 'contain' => false, |
|
| 120 | - 'fields' => ["YEAR({$field}) as `date`"], |
|
| 121 | - 'group' => ["YEAR({$field}) ASC"] |
|
| 122 | - ]; |
|
| 123 | - if ($params['resolution'] === 'month') { |
|
| 124 | - $defaults['fields'] = "UNIX_TIMESTAMP(CONCAT(YEAR({$field}), '-', MONTH({$field}), '-01')) as `date`"; |
|
| 125 | - $defaults['group'][] = "MONTH({$field}) ASC"; |
|
| 126 | - } |
|
| 127 | - $query = array_merge_recursive($query, $defaults); |
|
| 128 | - if (empty($this->$model)) { |
|
| 129 | - $this->loadModel($model); |
|
| 130 | - } |
|
| 131 | - $results = $this->$model->find('all', $query); |
|
| 132 | - |
|
| 133 | - $periods = count($results); |
|
| 134 | - if (empty($results) || $periods < 2) { |
|
| 135 | - return false; |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - if ($params['raw']) { |
|
| 139 | - return $results; |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - $results = Hash::extract($results, '{n}.{n}'); |
|
| 143 | - $data = []; |
|
| 144 | - foreach ($results as $d) { |
|
| 145 | - $data[] = [(string)$d['date'], $d['count']]; |
|
| 146 | - } |
|
| 147 | - return $this->_wrapData($data); |
|
| 148 | - } |
|
| 149 | - |
|
| 150 | - protected function _getMonthStats($model, $field) { |
|
| 151 | - $results = $this->_countYearStats( |
|
| 152 | - ['fields' => ['COUNT(*) AS `count`']], |
|
| 153 | - ['raw' => true, 'resolution' => 'month'], |
|
| 154 | - $model, |
|
| 155 | - $field); |
|
| 156 | - |
|
| 157 | - if (empty($results)) { |
|
| 158 | - return false; |
|
| 159 | - } |
|
| 160 | - |
|
| 161 | - $cumulated = []; |
|
| 162 | - $diff = []; |
|
| 163 | - $sum = 0; |
|
| 164 | - foreach ($results as $reg) { |
|
| 165 | - $sum += (int)$reg[0]['count']; |
|
| 166 | - $cumulated[] = array($reg[0]['date'] * 1000, $sum); |
|
| 167 | - $diff[] = array($reg[0]['date'] * 1000, (int)$reg[0]['count']); |
|
| 168 | - } |
|
| 169 | - return [ |
|
| 170 | - $this->_wrapData($diff, ['label' => __('New')]), |
|
| 171 | - $this->_wrapData($cumulated, |
|
| 172 | - ['yaxis' => 2, 'label' => __('Cumulated')]) |
|
| 173 | - ]; |
|
| 174 | - } |
|
| 175 | - |
|
| 176 | - } |
|
| 3 | + App::uses('AppController', 'Controller'); |
|
| 4 | + |
|
| 5 | + class AdminsController extends AppController { |
|
| 6 | + |
|
| 7 | + public $name = 'Admins'; |
|
| 8 | + |
|
| 9 | + public $helpers = ['Admin', 'Flot', 'Sitemap.Sitemap']; |
|
| 10 | + |
|
| 11 | + public function admin_index() { |
|
| 12 | + } |
|
| 13 | + |
|
| 14 | + /** |
|
| 15 | + * Show PHP-info |
|
| 16 | + * |
|
| 17 | + * @return void |
|
| 18 | + */ |
|
| 19 | + public function admin_phpinfo() { |
|
| 20 | + } |
|
| 21 | + |
|
| 22 | + public function admin_logs() { |
|
| 23 | + // order here is output order in frontend |
|
| 24 | + $_logsToRead = ['error', 'debug']; |
|
| 25 | + |
|
| 26 | + $_logsToRead = glob(LOGS . '*.log'); |
|
| 27 | + if (!$_logsToRead) { |
|
| 28 | + return; |
|
| 29 | + } |
|
| 30 | + |
|
| 31 | + // will contain ['error' => '<string>', 'debug' => '<string>'] |
|
| 32 | + $_logs = []; |
|
| 33 | + foreach ($_logsToRead as $_path) { |
|
| 34 | + $_content = ''; |
|
| 35 | + $_size = filesize($_path); |
|
| 36 | + $_content = file_get_contents($_path, false, null, $_size - 65536); |
|
| 37 | + $name = basename($_path); |
|
| 38 | + $_logs[$name] = $_content; |
|
| 39 | + } |
|
| 40 | + $this->set('logs', $_logs); |
|
| 41 | + } |
|
| 42 | + |
|
| 43 | + public function admin_plugins() { |
|
| 44 | + } |
|
| 45 | + |
|
| 46 | + public function admin_stats() { |
|
| 47 | + $postingsPA = $this->_getYearStats('Entry', 'time'); |
|
| 48 | + $registrationsPA = $this->_getYearStats('User', 'registered'); |
|
| 49 | + $activeUserPA = $this->_getUserWithPostingsPerYear(); |
|
| 50 | + $averagePostingsPerUserPA = $this->_getAveragePPU($postingsPA, |
|
| 51 | + $activeUserPA); |
|
| 52 | + $this->set(compact('averagePostingsPerUserPA', |
|
| 53 | + 'postingsPA', 'activeUserPA', 'registrationsPA')); |
|
| 54 | + $this->set('categoryPostingsPA', $this->_getCategoriesPerYear()); |
|
| 55 | + } |
|
| 56 | + |
|
| 57 | + public function admin_stats_details() { |
|
| 58 | + $this->set('registrations', |
|
| 59 | + $this->_getMonthStats('User', 'registered')); |
|
| 60 | + $this->set('entries', $this->_getMonthStats('Entry', 'time')); |
|
| 61 | + } |
|
| 62 | + |
|
| 63 | + protected function _getAveragePPU($postingsPerYear, $activeUserPerYear) { |
|
| 64 | + if (empty($postingsPerYear) || empty($activeUserPerYear)) { |
|
| 65 | + return false; |
|
| 66 | + } |
|
| 67 | + $avgPostingsPerUser = []; |
|
| 68 | + foreach ($postingsPerYear['data'] as $key => $data) { |
|
| 69 | + list($year, $postings) = $data; |
|
| 70 | + $activeUsers = $activeUserPerYear['data'][$key][1]; |
|
| 71 | + $avgPostingsPerUser[] = [$year, $postings / $activeUsers]; |
|
| 72 | + } |
|
| 73 | + return $this->_wrapData($avgPostingsPerUser); |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + protected function _getCategoriesPerYear() { |
|
| 77 | + $results = $this->_countYearStats([ |
|
| 78 | + 'contain' => ['Category'], |
|
| 79 | + 'fields' => ['COUNT(*) AS `count`', 'Category.category'], |
|
| 80 | + 'group' => ['Category.category'] |
|
| 81 | + ], |
|
| 82 | + ['raw' => true] |
|
| 83 | + ); |
|
| 84 | + if (empty($results)) { |
|
| 85 | + return false; |
|
| 86 | + } |
|
| 87 | + $data = []; |
|
| 88 | + foreach ($results as $dataset) { |
|
| 89 | + $category = $dataset['Category']['category']; |
|
| 90 | + $data[$category][] = [$dataset[0]['date'], $dataset[0]['count']]; |
|
| 91 | + } |
|
| 92 | + $out = []; |
|
| 93 | + foreach ($data as $category => $dataset) { |
|
| 94 | + $out[] = $this->_wrapData($dataset, ['label' => $category]); |
|
| 95 | + } |
|
| 96 | + return $out; |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + protected function _wrapData(&$data, array $options = []) { |
|
| 100 | + return ['data' => $data] + $options; |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + protected function _getUserWithPostingsPerYear() { |
|
| 104 | + return $this->_countYearStats(['fields' => ['COUNT(DISTINCT `user_id`) AS `count`']]); |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + protected function _getYearStats($model, $field) { |
|
| 108 | + return $this->_countYearStats(['fields' => ['COUNT(*) AS `count`']], |
|
| 109 | + [], $model, $field); |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + protected function _countYearStats($query, $params = [], $model = 'Entry', $field = 'time') { |
|
| 113 | + $params += [ |
|
| 114 | + 'raw' => false, |
|
| 115 | + 'resolution' => 'year' |
|
| 116 | + ]; |
|
| 117 | + |
|
| 118 | + $defaults = [ |
|
| 119 | + 'contain' => false, |
|
| 120 | + 'fields' => ["YEAR({$field}) as `date`"], |
|
| 121 | + 'group' => ["YEAR({$field}) ASC"] |
|
| 122 | + ]; |
|
| 123 | + if ($params['resolution'] === 'month') { |
|
| 124 | + $defaults['fields'] = "UNIX_TIMESTAMP(CONCAT(YEAR({$field}), '-', MONTH({$field}), '-01')) as `date`"; |
|
| 125 | + $defaults['group'][] = "MONTH({$field}) ASC"; |
|
| 126 | + } |
|
| 127 | + $query = array_merge_recursive($query, $defaults); |
|
| 128 | + if (empty($this->$model)) { |
|
| 129 | + $this->loadModel($model); |
|
| 130 | + } |
|
| 131 | + $results = $this->$model->find('all', $query); |
|
| 132 | + |
|
| 133 | + $periods = count($results); |
|
| 134 | + if (empty($results) || $periods < 2) { |
|
| 135 | + return false; |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + if ($params['raw']) { |
|
| 139 | + return $results; |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + $results = Hash::extract($results, '{n}.{n}'); |
|
| 143 | + $data = []; |
|
| 144 | + foreach ($results as $d) { |
|
| 145 | + $data[] = [(string)$d['date'], $d['count']]; |
|
| 146 | + } |
|
| 147 | + return $this->_wrapData($data); |
|
| 148 | + } |
|
| 149 | + |
|
| 150 | + protected function _getMonthStats($model, $field) { |
|
| 151 | + $results = $this->_countYearStats( |
|
| 152 | + ['fields' => ['COUNT(*) AS `count`']], |
|
| 153 | + ['raw' => true, 'resolution' => 'month'], |
|
| 154 | + $model, |
|
| 155 | + $field); |
|
| 156 | + |
|
| 157 | + if (empty($results)) { |
|
| 158 | + return false; |
|
| 159 | + } |
|
| 160 | + |
|
| 161 | + $cumulated = []; |
|
| 162 | + $diff = []; |
|
| 163 | + $sum = 0; |
|
| 164 | + foreach ($results as $reg) { |
|
| 165 | + $sum += (int)$reg[0]['count']; |
|
| 166 | + $cumulated[] = array($reg[0]['date'] * 1000, $sum); |
|
| 167 | + $diff[] = array($reg[0]['date'] * 1000, (int)$reg[0]['count']); |
|
| 168 | + } |
|
| 169 | + return [ |
|
| 170 | + $this->_wrapData($diff, ['label' => __('New')]), |
|
| 171 | + $this->_wrapData($cumulated, |
|
| 172 | + ['yaxis' => 2, 'label' => __('Cumulated')]) |
|
| 173 | + ]; |
|
| 174 | + } |
|
| 175 | + |
|
| 176 | + } |
|
@@ -15,6 +15,9 @@ |
||
| 15 | 15 | return $hashWithSalt; |
| 16 | 16 | } |
| 17 | 17 | |
| 18 | + /** |
|
| 19 | + * @param integer $maxLength |
|
| 20 | + */ |
|
| 18 | 21 | protected static function _generateRandomString($maxLength = null) { |
| 19 | 22 | $string = Security::generateAuthKey(); |
| 20 | 23 | if ($maxLength) { |
@@ -10,8 +10,8 @@ discard block |
||
| 10 | 10 | public function hash($password) { |
| 11 | 11 | // compare to includes/functions.inc.php generate_pw_hash() mlf 2.3 |
| 12 | 12 | $salt = self::_generateRandomString(10); |
| 13 | - $saltedHash = sha1($password . $salt); |
|
| 14 | - $hashWithSalt = $saltedHash . $salt; |
|
| 13 | + $saltedHash = sha1($password.$salt); |
|
| 14 | + $hashWithSalt = $saltedHash.$salt; |
|
| 15 | 15 | return $hashWithSalt; |
| 16 | 16 | } |
| 17 | 17 | |
@@ -28,7 +28,7 @@ discard block |
||
| 28 | 28 | // compare to includes/functions.inc.php is_pw_correct() mlf 2.3 |
| 29 | 29 | $saltedHash = substr($hash, 0, 40); |
| 30 | 30 | $salt = substr($hash, 40, 10); |
| 31 | - if (sha1($password . $salt) == $saltedHash) : |
|
| 31 | + if (sha1($password.$salt) == $saltedHash) : |
|
| 32 | 32 | $out = true; |
| 33 | 33 | endif; |
| 34 | 34 | return $out; |
@@ -1,37 +1,37 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | - App::uses('AbstractPasswordHasher', 'Controller/Component/Auth'); |
|
| 3 | + App::uses('AbstractPasswordHasher', 'Controller/Component/Auth'); |
|
| 4 | 4 | |
| 5 | 5 | /** |
| 6 | 6 | * mylittleforum 2.x salted sha1 passwords |
| 7 | 7 | */ |
| 8 | - class Mlf2PasswordHasher extends AbstractPasswordHasher { |
|
| 8 | + class Mlf2PasswordHasher extends AbstractPasswordHasher { |
|
| 9 | 9 | |
| 10 | - public function hash($password) { |
|
| 11 | - // compare to includes/functions.inc.php generate_pw_hash() mlf 2.3 |
|
| 12 | - $salt = self::_generateRandomString(10); |
|
| 13 | - $saltedHash = sha1($password . $salt); |
|
| 14 | - $hashWithSalt = $saltedHash . $salt; |
|
| 15 | - return $hashWithSalt; |
|
| 16 | - } |
|
| 10 | + public function hash($password) { |
|
| 11 | + // compare to includes/functions.inc.php generate_pw_hash() mlf 2.3 |
|
| 12 | + $salt = self::_generateRandomString(10); |
|
| 13 | + $saltedHash = sha1($password . $salt); |
|
| 14 | + $hashWithSalt = $saltedHash . $salt; |
|
| 15 | + return $hashWithSalt; |
|
| 16 | + } |
|
| 17 | 17 | |
| 18 | - protected static function _generateRandomString($maxLength = null) { |
|
| 19 | - $string = Security::generateAuthKey(); |
|
| 20 | - if ($maxLength) { |
|
| 21 | - $string = substr($string, 0, $maxLength); |
|
| 22 | - } |
|
| 23 | - return $string; |
|
| 24 | - } |
|
| 18 | + protected static function _generateRandomString($maxLength = null) { |
|
| 19 | + $string = Security::generateAuthKey(); |
|
| 20 | + if ($maxLength) { |
|
| 21 | + $string = substr($string, 0, $maxLength); |
|
| 22 | + } |
|
| 23 | + return $string; |
|
| 24 | + } |
|
| 25 | 25 | |
| 26 | - public function check($password, $hash) { |
|
| 27 | - $out = false; |
|
| 28 | - // compare to includes/functions.inc.php is_pw_correct() mlf 2.3 |
|
| 29 | - $saltedHash = substr($hash, 0, 40); |
|
| 30 | - $salt = substr($hash, 40, 10); |
|
| 31 | - if (sha1($password . $salt) == $saltedHash) : |
|
| 32 | - $out = true; |
|
| 33 | - endif; |
|
| 34 | - return $out; |
|
| 35 | - } |
|
| 26 | + public function check($password, $hash) { |
|
| 27 | + $out = false; |
|
| 28 | + // compare to includes/functions.inc.php is_pw_correct() mlf 2.3 |
|
| 29 | + $saltedHash = substr($hash, 0, 40); |
|
| 30 | + $salt = substr($hash, 40, 10); |
|
| 31 | + if (sha1($password . $salt) == $saltedHash) : |
|
| 32 | + $out = true; |
|
| 33 | + endif; |
|
| 34 | + return $out; |
|
| 35 | + } |
|
| 36 | 36 | |
| 37 | - } |
|
| 37 | + } |
|
@@ -268,6 +268,7 @@ |
||
| 268 | 268 | |
| 269 | 269 | /** |
| 270 | 270 | * write the settings to the session, so that they are available on next request |
| 271 | + * @param Controller $controller |
|
| 271 | 272 | */ |
| 272 | 273 | protected function _writeSession(&$controller) { |
| 273 | 274 | if ($controller->action !== 'logout' && $controller->Auth->user()): |
@@ -1,318 +1,318 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | - use Saito\User\Auth\CategoryAuthorization; |
|
| 4 | - use Saito\User\Bookmarks; |
|
| 5 | - use Saito\User\ForumsUserInterface; |
|
| 6 | - use Saito\User\LastRefresh; |
|
| 7 | - use Saito\User\ReadPostings; |
|
| 8 | - use Saito\User\SaitoUserTrait; |
|
| 3 | + use Saito\User\Auth\CategoryAuthorization; |
|
| 4 | + use Saito\User\Bookmarks; |
|
| 5 | + use Saito\User\ForumsUserInterface; |
|
| 6 | + use Saito\User\LastRefresh; |
|
| 7 | + use Saito\User\ReadPostings; |
|
| 8 | + use Saito\User\SaitoUserTrait; |
|
| 9 | 9 | |
| 10 | - App::uses('Component', 'Controller'); |
|
| 10 | + App::uses('Component', 'Controller'); |
|
| 11 | 11 | |
| 12 | - class CurrentUserComponent extends Component implements |
|
| 13 | - ArrayAccess, |
|
| 14 | - ForumsUserInterface { |
|
| 12 | + class CurrentUserComponent extends Component implements |
|
| 13 | + ArrayAccess, |
|
| 14 | + ForumsUserInterface { |
|
| 15 | 15 | |
| 16 | - use SaitoUserTrait; |
|
| 16 | + use SaitoUserTrait; |
|
| 17 | 17 | |
| 18 | - /** |
|
| 19 | - * @var Saito\User\Auth\CategoryAuthorization |
|
| 20 | - */ |
|
| 21 | - public $Categories; |
|
| 18 | + /** |
|
| 19 | + * @var Saito\User\Auth\CategoryAuthorization |
|
| 20 | + */ |
|
| 21 | + public $Categories; |
|
| 22 | 22 | |
| 23 | 23 | /** |
| 24 | 24 | * Component name |
| 25 | 25 | * |
| 26 | 26 | * @var string |
| 27 | 27 | */ |
| 28 | - public $name = 'CurrentUser'; |
|
| 28 | + public $name = 'CurrentUser'; |
|
| 29 | 29 | |
| 30 | 30 | /** |
| 31 | 31 | * Component's components |
| 32 | 32 | * |
| 33 | 33 | * @var array |
| 34 | 34 | */ |
| 35 | - public $components = ['Cookie', 'Cron.Cron']; |
|
| 35 | + public $components = ['Cookie', 'Cron.Cron']; |
|
| 36 | 36 | |
| 37 | 37 | /** |
| 38 | 38 | * Manages the persistent login cookie |
| 39 | 39 | * |
| 40 | 40 | * @var \Saito\User\Cookie\CurrentUserCookie |
| 41 | 41 | */ |
| 42 | - public $PersistentCookie = null; |
|
| 42 | + public $PersistentCookie = null; |
|
| 43 | 43 | |
| 44 | 44 | /** |
| 45 | 45 | * Manages the last refresh/mark entries as read for the current user |
| 46 | 46 | * |
| 47 | 47 | * @var \Saito\User\LastRefresh\LastRefreshAbstract |
| 48 | 48 | */ |
| 49 | - public $LastRefresh = null; |
|
| 49 | + public $LastRefresh = null; |
|
| 50 | 50 | |
| 51 | - /** |
|
| 52 | - * @var ReadPostings |
|
| 53 | - */ |
|
| 54 | - public $ReadEntries; |
|
| 51 | + /** |
|
| 52 | + * @var ReadPostings |
|
| 53 | + */ |
|
| 54 | + public $ReadEntries; |
|
| 55 | 55 | |
| 56 | - /** |
|
| 57 | - * @var Bookmarks bookmarks of the current user |
|
| 58 | - */ |
|
| 59 | - protected $_Bookmarks; |
|
| 56 | + /** |
|
| 57 | + * @var Bookmarks bookmarks of the current user |
|
| 58 | + */ |
|
| 59 | + protected $_Bookmarks; |
|
| 60 | 60 | |
| 61 | 61 | /** |
| 62 | 62 | * Model User instance exclusive to the CurrentUserComponent |
| 63 | 63 | * |
| 64 | 64 | * @var User |
| 65 | 65 | */ |
| 66 | - protected $_User = null; |
|
| 66 | + protected $_User = null; |
|
| 67 | 67 | |
| 68 | 68 | /** |
| 69 | 69 | * Reference to the controller |
| 70 | 70 | * |
| 71 | 71 | * @var Controller |
| 72 | 72 | */ |
| 73 | - protected $_Controller = null; |
|
| 73 | + protected $_Controller = null; |
|
| 74 | 74 | |
| 75 | - public function initialize(Controller $Controller) { |
|
| 76 | - if ($Controller->name === 'CakeError') { |
|
| 77 | - return; |
|
| 78 | - } |
|
| 75 | + public function initialize(Controller $Controller) { |
|
| 76 | + if ($Controller->name === 'CakeError') { |
|
| 77 | + return; |
|
| 78 | + } |
|
| 79 | 79 | |
| 80 | - $this->_Controller = $Controller; |
|
| 81 | - if ($this->_Controller->modelClass) { |
|
| 82 | - $this->_Controller->{$this->_Controller->modelClass}->SharedObjects['CurrentUser'] = $this; |
|
| 83 | - } |
|
| 84 | - $this->Categories = new CategoryAuthorization($this); |
|
| 80 | + $this->_Controller = $Controller; |
|
| 81 | + if ($this->_Controller->modelClass) { |
|
| 82 | + $this->_Controller->{$this->_Controller->modelClass}->SharedObjects['CurrentUser'] = $this; |
|
| 83 | + } |
|
| 84 | + $this->Categories = new CategoryAuthorization($this); |
|
| 85 | 85 | |
| 86 | - $this->_Controller->dic->set('CU', $this); |
|
| 86 | + $this->_Controller->dic->set('CU', $this); |
|
| 87 | 87 | |
| 88 | - /* |
|
| 88 | + /* |
|
| 89 | 89 | * We create a new User Model instance. Otherwise we would overwrite $this->request->data |
| 90 | 90 | * when reading in refresh(), causing error e.g. saving the user prefs. |
| 91 | 91 | */ |
| 92 | - $this->_User = ClassRegistry::init( |
|
| 93 | - ['class' => 'User', 'alias' => 'currentUser'] |
|
| 94 | - ); |
|
| 95 | - |
|
| 96 | - $this->PersistentCookie = new \Saito\User\Cookie\CurrentUserCookie($this->Cookie, 'AU'); |
|
| 97 | - |
|
| 98 | - $this->_configureAuth(); |
|
| 99 | - |
|
| 100 | - // prevents session auto re-login from form's request->data: login is |
|
| 101 | - // called explicitly by controller on /users/login |
|
| 102 | - if ($this->_Controller->action !== 'login') { |
|
| 103 | - if (!$this->_reLoginSession()) { |
|
| 104 | - // don't auto-login on login related pages |
|
| 105 | - if ($this->_Controller->params['action'] !== 'login' && |
|
| 106 | - $this->_Controller->params['action'] !== 'register' && |
|
| 107 | - $this->_Controller->referer() !== '/users/login' |
|
| 108 | - ) { |
|
| 109 | - $this->_reLoginCookie(); |
|
| 110 | - } |
|
| 111 | - } |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - if ($this->isLoggedIn()) { |
|
| 115 | - $this->ReadEntries = new ReadPostings\ReadPostingsDatabase($this); |
|
| 116 | - } elseif ($this->isBot()) { |
|
| 117 | - $this->ReadEntries = new ReadPostings\ReadPostingsDummy($this); |
|
| 118 | - } else { |
|
| 119 | - $this->ReadEntries = new ReadPostings\ReadPostingsCookie($this); |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - $this->_Bookmarks = new Bookmarks($this); |
|
| 123 | - |
|
| 124 | - $this->_markOnline(); |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - public function startup(Controller $controller) { |
|
| 128 | - parent::startup($controller); |
|
| 129 | - |
|
| 130 | - if ($controller->action !== 'logout' && $this->isLoggedIn()) : |
|
| 131 | - if ($this->isForbidden()) : |
|
| 132 | - $this->_Controller->redirect( |
|
| 133 | - ['controller' => 'users', 'action' => 'logout'] |
|
| 134 | - ); |
|
| 135 | - endif; |
|
| 136 | - endif; |
|
| 137 | - } |
|
| 92 | + $this->_User = ClassRegistry::init( |
|
| 93 | + ['class' => 'User', 'alias' => 'currentUser'] |
|
| 94 | + ); |
|
| 95 | + |
|
| 96 | + $this->PersistentCookie = new \Saito\User\Cookie\CurrentUserCookie($this->Cookie, 'AU'); |
|
| 97 | + |
|
| 98 | + $this->_configureAuth(); |
|
| 99 | + |
|
| 100 | + // prevents session auto re-login from form's request->data: login is |
|
| 101 | + // called explicitly by controller on /users/login |
|
| 102 | + if ($this->_Controller->action !== 'login') { |
|
| 103 | + if (!$this->_reLoginSession()) { |
|
| 104 | + // don't auto-login on login related pages |
|
| 105 | + if ($this->_Controller->params['action'] !== 'login' && |
|
| 106 | + $this->_Controller->params['action'] !== 'register' && |
|
| 107 | + $this->_Controller->referer() !== '/users/login' |
|
| 108 | + ) { |
|
| 109 | + $this->_reLoginCookie(); |
|
| 110 | + } |
|
| 111 | + } |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + if ($this->isLoggedIn()) { |
|
| 115 | + $this->ReadEntries = new ReadPostings\ReadPostingsDatabase($this); |
|
| 116 | + } elseif ($this->isBot()) { |
|
| 117 | + $this->ReadEntries = new ReadPostings\ReadPostingsDummy($this); |
|
| 118 | + } else { |
|
| 119 | + $this->ReadEntries = new ReadPostings\ReadPostingsCookie($this); |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + $this->_Bookmarks = new Bookmarks($this); |
|
| 123 | + |
|
| 124 | + $this->_markOnline(); |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + public function startup(Controller $controller) { |
|
| 128 | + parent::startup($controller); |
|
| 129 | + |
|
| 130 | + if ($controller->action !== 'logout' && $this->isLoggedIn()) : |
|
| 131 | + if ($this->isForbidden()) : |
|
| 132 | + $this->_Controller->redirect( |
|
| 133 | + ['controller' => 'users', 'action' => 'logout'] |
|
| 134 | + ); |
|
| 135 | + endif; |
|
| 136 | + endif; |
|
| 137 | + } |
|
| 138 | 138 | |
| 139 | 139 | /** |
| 140 | 140 | * Marks users as online |
| 141 | 141 | */ |
| 142 | - protected function _markOnline() { |
|
| 143 | - Stopwatch::start('CurrentUser->_markOnline()'); |
|
| 144 | - $_isLoggedIn = $this->isLoggedIn(); |
|
| 145 | - if ($_isLoggedIn) { |
|
| 146 | - $_id = $this->getId(); |
|
| 147 | - } else { |
|
| 148 | - // don't count search bots as guests |
|
| 149 | - if ($this->isBot()) { |
|
| 150 | - return; |
|
| 151 | - } |
|
| 152 | - $_id = $this->_Controller->Session->id(); |
|
| 153 | - } |
|
| 154 | - $this->_User->UserOnline->setOnline($_id, $_isLoggedIn); |
|
| 155 | - Stopwatch::stop('CurrentUser->_markOnline()'); |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - /** |
|
| 159 | - * Detects if the current user is a bot |
|
| 160 | - * |
|
| 161 | - * @return boolean |
|
| 162 | - */ |
|
| 163 | - public function isBot() { |
|
| 164 | - return $this->_Controller->request->is('bot'); |
|
| 165 | - } |
|
| 166 | - |
|
| 167 | - /** |
|
| 168 | - * Logs-in registered users |
|
| 169 | - * |
|
| 170 | - * @param null|array $user user-data, if null request-data is used |
|
| 171 | - * @return bool true if user is logged in false otherwise |
|
| 172 | - */ |
|
| 173 | - protected function _login($user = null) { |
|
| 174 | - $this->_Controller->Auth->login($user); |
|
| 175 | - $this->refresh(); |
|
| 176 | - return $this->isLoggedIn(); |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - protected function _reLoginSession() { |
|
| 180 | - return $this->_login(); |
|
| 181 | - } |
|
| 182 | - |
|
| 183 | - protected function _reLoginCookie() { |
|
| 184 | - $cookie = $this->PersistentCookie->read(); |
|
| 185 | - if ($cookie) { |
|
| 186 | - $this->_login($cookie); |
|
| 187 | - return $this->isLoggedIn(); |
|
| 188 | - } |
|
| 189 | - return false; |
|
| 190 | - } |
|
| 191 | - |
|
| 192 | - public function login() { |
|
| 193 | - // non-logged in session-id is lost after successful login |
|
| 194 | - $sessionId = session_id(); |
|
| 195 | - |
|
| 196 | - if (!$this->_login()) { |
|
| 197 | - return false; |
|
| 198 | - } |
|
| 199 | - |
|
| 200 | - $this->_User->incrementLogins($this->getId()); |
|
| 201 | - $this->_User->UserOnline->setOffline($sessionId); |
|
| 202 | - //password update |
|
| 203 | - if (empty($this->_Controller->request->data['User']['password']) === false) { |
|
| 204 | - $this->_User->autoUpdatePassword( |
|
| 205 | - $this->getId(), |
|
| 206 | - $this->_Controller->request->data['User']['password'] |
|
| 207 | - ); |
|
| 208 | - } |
|
| 209 | - |
|
| 210 | - // set cookie |
|
| 211 | - if (empty($this->_Controller->request->data['User']['remember_me']) === false) { |
|
| 212 | - $this->PersistentCookie->write($this); |
|
| 213 | - }; |
|
| 214 | - |
|
| 215 | - return true; |
|
| 216 | - } |
|
| 217 | - |
|
| 218 | - /** |
|
| 219 | - * Sets user-data |
|
| 220 | - */ |
|
| 221 | - public function refresh() { |
|
| 222 | - // preliminary set user-data from Cake's Auth handler |
|
| 223 | - $this->setSettings($this->_Controller->Auth->user()); |
|
| 224 | - // set user-data from current DB data: ensures that *all sessions* |
|
| 225 | - // use the same set of data (user got locked, user-type was demoted …) |
|
| 226 | - if ($this->isLoggedIn()) { |
|
| 227 | - $this->_User->id = $this->getId(); |
|
| 228 | - $this->setSettings($this->_User->getProfile($this->getId())); |
|
| 229 | - $this->LastRefresh = new LastRefresh\LastRefreshDatabase($this); |
|
| 230 | - } elseif ($this->isBot()) { |
|
| 231 | - $this->LastRefresh = new LastRefresh\LastRefreshDummy($this); |
|
| 232 | - } else { |
|
| 233 | - $this->LastRefresh = new LastRefresh\LastRefreshCookie($this); |
|
| 234 | - } |
|
| 235 | - } |
|
| 236 | - |
|
| 237 | - public function logout() { |
|
| 238 | - if (!$this->isLoggedIn()) { |
|
| 239 | - return; |
|
| 240 | - } |
|
| 241 | - $this->PersistentCookie->delete(); |
|
| 242 | - $this->_User->id = $this->getId(); |
|
| 243 | - $this->_User->UserOnline->setOffline($this->getId()); |
|
| 244 | - $this->setSettings(null); |
|
| 245 | - $this->_Controller->Auth->logout(); |
|
| 246 | - } |
|
| 247 | - |
|
| 248 | - public function shutdown(Controller $Controller) { |
|
| 249 | - $this->_writeSession($Controller); |
|
| 250 | - } |
|
| 251 | - |
|
| 252 | - public function beforeRedirect(Controller $Controller, $url, $status = null, $exit = true) { |
|
| 253 | - $this->_writeSession($Controller); |
|
| 254 | - } |
|
| 255 | - |
|
| 256 | - public function beforeRender(Controller $Controller) { |
|
| 257 | - // write out the current user for access in the views |
|
| 258 | - $Controller->set('CurrentUser', $this); |
|
| 259 | - } |
|
| 260 | - |
|
| 261 | - public function getModel() { |
|
| 262 | - return $this->_User; |
|
| 263 | - } |
|
| 264 | - |
|
| 265 | - public function hasBookmarked($entryId) { |
|
| 266 | - return $this->_Bookmarks->isBookmarked($entryId); |
|
| 267 | - } |
|
| 142 | + protected function _markOnline() { |
|
| 143 | + Stopwatch::start('CurrentUser->_markOnline()'); |
|
| 144 | + $_isLoggedIn = $this->isLoggedIn(); |
|
| 145 | + if ($_isLoggedIn) { |
|
| 146 | + $_id = $this->getId(); |
|
| 147 | + } else { |
|
| 148 | + // don't count search bots as guests |
|
| 149 | + if ($this->isBot()) { |
|
| 150 | + return; |
|
| 151 | + } |
|
| 152 | + $_id = $this->_Controller->Session->id(); |
|
| 153 | + } |
|
| 154 | + $this->_User->UserOnline->setOnline($_id, $_isLoggedIn); |
|
| 155 | + Stopwatch::stop('CurrentUser->_markOnline()'); |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + /** |
|
| 159 | + * Detects if the current user is a bot |
|
| 160 | + * |
|
| 161 | + * @return boolean |
|
| 162 | + */ |
|
| 163 | + public function isBot() { |
|
| 164 | + return $this->_Controller->request->is('bot'); |
|
| 165 | + } |
|
| 166 | + |
|
| 167 | + /** |
|
| 168 | + * Logs-in registered users |
|
| 169 | + * |
|
| 170 | + * @param null|array $user user-data, if null request-data is used |
|
| 171 | + * @return bool true if user is logged in false otherwise |
|
| 172 | + */ |
|
| 173 | + protected function _login($user = null) { |
|
| 174 | + $this->_Controller->Auth->login($user); |
|
| 175 | + $this->refresh(); |
|
| 176 | + return $this->isLoggedIn(); |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + protected function _reLoginSession() { |
|
| 180 | + return $this->_login(); |
|
| 181 | + } |
|
| 182 | + |
|
| 183 | + protected function _reLoginCookie() { |
|
| 184 | + $cookie = $this->PersistentCookie->read(); |
|
| 185 | + if ($cookie) { |
|
| 186 | + $this->_login($cookie); |
|
| 187 | + return $this->isLoggedIn(); |
|
| 188 | + } |
|
| 189 | + return false; |
|
| 190 | + } |
|
| 191 | + |
|
| 192 | + public function login() { |
|
| 193 | + // non-logged in session-id is lost after successful login |
|
| 194 | + $sessionId = session_id(); |
|
| 195 | + |
|
| 196 | + if (!$this->_login()) { |
|
| 197 | + return false; |
|
| 198 | + } |
|
| 199 | + |
|
| 200 | + $this->_User->incrementLogins($this->getId()); |
|
| 201 | + $this->_User->UserOnline->setOffline($sessionId); |
|
| 202 | + //password update |
|
| 203 | + if (empty($this->_Controller->request->data['User']['password']) === false) { |
|
| 204 | + $this->_User->autoUpdatePassword( |
|
| 205 | + $this->getId(), |
|
| 206 | + $this->_Controller->request->data['User']['password'] |
|
| 207 | + ); |
|
| 208 | + } |
|
| 209 | + |
|
| 210 | + // set cookie |
|
| 211 | + if (empty($this->_Controller->request->data['User']['remember_me']) === false) { |
|
| 212 | + $this->PersistentCookie->write($this); |
|
| 213 | + }; |
|
| 214 | + |
|
| 215 | + return true; |
|
| 216 | + } |
|
| 217 | + |
|
| 218 | + /** |
|
| 219 | + * Sets user-data |
|
| 220 | + */ |
|
| 221 | + public function refresh() { |
|
| 222 | + // preliminary set user-data from Cake's Auth handler |
|
| 223 | + $this->setSettings($this->_Controller->Auth->user()); |
|
| 224 | + // set user-data from current DB data: ensures that *all sessions* |
|
| 225 | + // use the same set of data (user got locked, user-type was demoted …) |
|
| 226 | + if ($this->isLoggedIn()) { |
|
| 227 | + $this->_User->id = $this->getId(); |
|
| 228 | + $this->setSettings($this->_User->getProfile($this->getId())); |
|
| 229 | + $this->LastRefresh = new LastRefresh\LastRefreshDatabase($this); |
|
| 230 | + } elseif ($this->isBot()) { |
|
| 231 | + $this->LastRefresh = new LastRefresh\LastRefreshDummy($this); |
|
| 232 | + } else { |
|
| 233 | + $this->LastRefresh = new LastRefresh\LastRefreshCookie($this); |
|
| 234 | + } |
|
| 235 | + } |
|
| 236 | + |
|
| 237 | + public function logout() { |
|
| 238 | + if (!$this->isLoggedIn()) { |
|
| 239 | + return; |
|
| 240 | + } |
|
| 241 | + $this->PersistentCookie->delete(); |
|
| 242 | + $this->_User->id = $this->getId(); |
|
| 243 | + $this->_User->UserOnline->setOffline($this->getId()); |
|
| 244 | + $this->setSettings(null); |
|
| 245 | + $this->_Controller->Auth->logout(); |
|
| 246 | + } |
|
| 247 | + |
|
| 248 | + public function shutdown(Controller $Controller) { |
|
| 249 | + $this->_writeSession($Controller); |
|
| 250 | + } |
|
| 251 | + |
|
| 252 | + public function beforeRedirect(Controller $Controller, $url, $status = null, $exit = true) { |
|
| 253 | + $this->_writeSession($Controller); |
|
| 254 | + } |
|
| 255 | + |
|
| 256 | + public function beforeRender(Controller $Controller) { |
|
| 257 | + // write out the current user for access in the views |
|
| 258 | + $Controller->set('CurrentUser', $this); |
|
| 259 | + } |
|
| 260 | + |
|
| 261 | + public function getModel() { |
|
| 262 | + return $this->_User; |
|
| 263 | + } |
|
| 264 | + |
|
| 265 | + public function hasBookmarked($entryId) { |
|
| 266 | + return $this->_Bookmarks->isBookmarked($entryId); |
|
| 267 | + } |
|
| 268 | 268 | |
| 269 | 269 | /** |
| 270 | 270 | * write the settings to the session, so that they are available on next request |
| 271 | 271 | */ |
| 272 | - protected function _writeSession(&$controller) { |
|
| 273 | - if ($controller->action !== 'logout' && $controller->Auth->user()): |
|
| 274 | - $controller->Session->write( |
|
| 275 | - 'Auth.User', |
|
| 276 | - $this->getSettings() |
|
| 277 | - ); |
|
| 278 | - endif; |
|
| 279 | - } |
|
| 272 | + protected function _writeSession(&$controller) { |
|
| 273 | + if ($controller->action !== 'logout' && $controller->Auth->user()): |
|
| 274 | + $controller->Session->write( |
|
| 275 | + 'Auth.User', |
|
| 276 | + $this->getSettings() |
|
| 277 | + ); |
|
| 278 | + endif; |
|
| 279 | + } |
|
| 280 | 280 | |
| 281 | 281 | /** |
| 282 | 282 | * Configures the auth component |
| 283 | 283 | */ |
| 284 | - protected function _configureAuth() { |
|
| 285 | - // delegate authenticate method |
|
| 286 | - // $this->_Controller->Auth->authenticate = $this->_User; |
|
| 287 | - |
|
| 288 | - $this->_Controller->Auth->authenticate = [ |
|
| 289 | - AuthComponent::ALL => [ |
|
| 290 | - 'useModel' => 'User', |
|
| 291 | - 'contain' => false, |
|
| 292 | - 'scope' => [ |
|
| 293 | - // user has activated his account (e.g. email confirmation) |
|
| 294 | - 'User.activate_code' => 0, |
|
| 295 | - // user is not banned by admin or mod |
|
| 296 | - 'User.user_lock' => 0 |
|
| 297 | - ] |
|
| 298 | - ], |
|
| 299 | - // 'Mlf' and 'Mlf2' could be 'Form' with different passwordHasher, but |
|
| 300 | - // see: https://cakephp.lighthouseapp.com/projects/42648/tickets/3907-allow-multiple-passwordhasher-with-same-authenticate-class-in-auth-config#ticket-3907-1 |
|
| 301 | - 'Mlf', // mylittleforum 1 auth |
|
| 302 | - 'Mlf2', // mylittleforum 2 auth |
|
| 303 | - 'Form' => ['passwordHasher' => 'Blowfish'] // blowfish saito standard |
|
| 304 | - ]; |
|
| 305 | - |
|
| 306 | - if ($this->isLoggedIn()): |
|
| 307 | - $this->_Controller->Auth->allow(); |
|
| 308 | - else: |
|
| 309 | - $this->_Controller->Auth->deny(); |
|
| 310 | - endif; |
|
| 311 | - |
|
| 312 | - $this->_Controller->Auth->autoRedirect = false; // don't redirect after Auth->login() |
|
| 313 | - $this->_Controller->Auth->allow('display'); // access to static pages in views/pages is allowed |
|
| 314 | - $this->_Controller->Auth->authError = __('auth_autherror'); // l10n |
|
| 315 | - } |
|
| 316 | - |
|
| 317 | - } |
|
| 284 | + protected function _configureAuth() { |
|
| 285 | + // delegate authenticate method |
|
| 286 | + // $this->_Controller->Auth->authenticate = $this->_User; |
|
| 287 | + |
|
| 288 | + $this->_Controller->Auth->authenticate = [ |
|
| 289 | + AuthComponent::ALL => [ |
|
| 290 | + 'useModel' => 'User', |
|
| 291 | + 'contain' => false, |
|
| 292 | + 'scope' => [ |
|
| 293 | + // user has activated his account (e.g. email confirmation) |
|
| 294 | + 'User.activate_code' => 0, |
|
| 295 | + // user is not banned by admin or mod |
|
| 296 | + 'User.user_lock' => 0 |
|
| 297 | + ] |
|
| 298 | + ], |
|
| 299 | + // 'Mlf' and 'Mlf2' could be 'Form' with different passwordHasher, but |
|
| 300 | + // see: https://cakephp.lighthouseapp.com/projects/42648/tickets/3907-allow-multiple-passwordhasher-with-same-authenticate-class-in-auth-config#ticket-3907-1 |
|
| 301 | + 'Mlf', // mylittleforum 1 auth |
|
| 302 | + 'Mlf2', // mylittleforum 2 auth |
|
| 303 | + 'Form' => ['passwordHasher' => 'Blowfish'] // blowfish saito standard |
|
| 304 | + ]; |
|
| 305 | + |
|
| 306 | + if ($this->isLoggedIn()): |
|
| 307 | + $this->_Controller->Auth->allow(); |
|
| 308 | + else: |
|
| 309 | + $this->_Controller->Auth->deny(); |
|
| 310 | + endif; |
|
| 311 | + |
|
| 312 | + $this->_Controller->Auth->autoRedirect = false; // don't redirect after Auth->login() |
|
| 313 | + $this->_Controller->Auth->allow('display'); // access to static pages in views/pages is allowed |
|
| 314 | + $this->_Controller->Auth->authError = __('auth_autherror'); // l10n |
|
| 315 | + } |
|
| 316 | + |
|
| 317 | + } |
|
| 318 | 318 | |
@@ -305,8 +305,10 @@ |
||
| 305 | 305 | |
| 306 | 306 | if ($this->isLoggedIn()): |
| 307 | 307 | $this->_Controller->Auth->allow(); |
| 308 | - else: |
|
| 308 | + else { |
|
| 309 | + : |
|
| 309 | 310 | $this->_Controller->Auth->deny(); |
| 311 | + } |
|
| 310 | 312 | endif; |
| 311 | 313 | |
| 312 | 314 | $this->_Controller->Auth->autoRedirect = false; // don't redirect after Auth->login() |
@@ -203,6 +203,9 @@ |
||
| 203 | 203 | $this->_CakeEmail->config('saito'); |
| 204 | 204 | } |
| 205 | 205 | |
| 206 | + /** |
|
| 207 | + * @param string $type |
|
| 208 | + */ |
|
| 206 | 209 | public function getPredefinedSender($type) { |
| 207 | 210 | $this->_init(); |
| 208 | 211 | return ['User' => [ |
@@ -68,7 +68,7 @@ discard block |
||
| 68 | 68 | } |
| 69 | 69 | } |
| 70 | 70 | |
| 71 | - $this->_emailConfigExists = file_exists(APP . 'Config' . DS . 'email' . '.php'); |
|
| 71 | + $this->_emailConfigExists = file_exists(APP.'Config'.DS.'email'.'.php'); |
|
| 72 | 72 | |
| 73 | 73 | $this->_forumName = Configure::read('Saito.Settings.forum_name'); |
| 74 | 74 | |
@@ -112,7 +112,7 @@ discard block |
||
| 112 | 112 | protected function _config($params = []) { |
| 113 | 113 | $defaults = [ |
| 114 | 114 | 'viewVars' => [ |
| 115 | - 'webroot' => Router::fullBaseUrl() . $this->_webroot, |
|
| 115 | + 'webroot' => Router::fullBaseUrl().$this->_webroot, |
|
| 116 | 116 | ], |
| 117 | 117 | ]; |
| 118 | 118 | $params = array_merge_recursive($defaults, $params); |
@@ -1,79 +1,79 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | - App::uses('Component', 'Controller'); |
|
| 4 | - App::uses('CakeEmail', 'Network/Email'); |
|
| 3 | + App::uses('Component', 'Controller'); |
|
| 4 | + App::uses('CakeEmail', 'Network/Email'); |
|
| 5 | 5 | |
| 6 | - class SaitoEmailComponent extends Component { |
|
| 6 | + class SaitoEmailComponent extends Component { |
|
| 7 | 7 | |
| 8 | - protected $_emailConfigExists = null; |
|
| 8 | + protected $_emailConfigExists = null; |
|
| 9 | 9 | |
| 10 | - protected $_User = null; |
|
| 10 | + protected $_User = null; |
|
| 11 | 11 | |
| 12 | - protected $_config = array(); |
|
| 12 | + protected $_config = array(); |
|
| 13 | 13 | |
| 14 | - protected $_viewVars = array(); |
|
| 14 | + protected $_viewVars = array(); |
|
| 15 | 15 | |
| 16 | - /** |
|
| 17 | - * @var array ['User' => ['username' => , 'user_email' =>] |
|
| 18 | - */ |
|
| 19 | - protected $_recipient = null; |
|
| 16 | + /** |
|
| 17 | + * @var array ['User' => ['username' => , 'user_email' =>] |
|
| 18 | + */ |
|
| 19 | + protected $_recipient = null; |
|
| 20 | 20 | |
| 21 | - /** |
|
| 22 | - * @var array ['User' => ['username' => , 'user_email' =>] |
|
| 23 | - */ |
|
| 24 | - protected $_sender = null; |
|
| 21 | + /** |
|
| 22 | + * @var array ['User' => ['username' => , 'user_email' =>] |
|
| 23 | + */ |
|
| 24 | + protected $_sender = null; |
|
| 25 | 25 | |
| 26 | - /** |
|
| 27 | - * @var CakeEmail |
|
| 28 | - */ |
|
| 29 | - protected $_CakeEmail; |
|
| 26 | + /** |
|
| 27 | + * @var CakeEmail |
|
| 28 | + */ |
|
| 29 | + protected $_CakeEmail; |
|
| 30 | 30 | |
| 31 | - protected $_webroot; |
|
| 31 | + protected $_webroot; |
|
| 32 | 32 | |
| 33 | - protected $_appAddress; |
|
| 33 | + protected $_appAddress; |
|
| 34 | 34 | |
| 35 | - protected $_addresses; |
|
| 35 | + protected $_addresses; |
|
| 36 | 36 | |
| 37 | - protected $_forumName; |
|
| 37 | + protected $_forumName; |
|
| 38 | 38 | |
| 39 | - protected $_headerSent; |
|
| 39 | + protected $_headerSent; |
|
| 40 | 40 | |
| 41 | - protected $_predefined = ['contact', 'main', 'register', 'system']; |
|
| 41 | + protected $_predefined = ['contact', 'main', 'register', 'system']; |
|
| 42 | 42 | |
| 43 | - public function startup(Controller $Controller) { |
|
| 44 | - $this->_webroot = $Controller->request->webroot; |
|
| 45 | - $this->_User = $Controller->User; |
|
| 46 | - } |
|
| 43 | + public function startup(Controller $Controller) { |
|
| 44 | + $this->_webroot = $Controller->request->webroot; |
|
| 45 | + $this->_User = $Controller->User; |
|
| 46 | + } |
|
| 47 | 47 | |
| 48 | - /** |
|
| 49 | - * init only if mail is actually send during request |
|
| 50 | - * |
|
| 51 | - * @throws InvalidArgumentException |
|
| 52 | - */ |
|
| 53 | - protected function _init() { |
|
| 54 | - if ($this->_addresses !== null) { |
|
| 55 | - return; |
|
| 56 | - } |
|
| 48 | + /** |
|
| 49 | + * init only if mail is actually send during request |
|
| 50 | + * |
|
| 51 | + * @throws InvalidArgumentException |
|
| 52 | + */ |
|
| 53 | + protected function _init() { |
|
| 54 | + if ($this->_addresses !== null) { |
|
| 55 | + return; |
|
| 56 | + } |
|
| 57 | 57 | |
| 58 | - $this->_addresses = [ |
|
| 59 | - 'main' => Configure::read('Saito.Settings.forum_email'), |
|
| 60 | - 'contact' => Configure::read('Saito.Settings.email_contact'), |
|
| 61 | - 'register' => Configure::read('Saito.Settings.email_register'), |
|
| 62 | - 'system' => Configure::read('Saito.Settings.email_system') |
|
| 63 | - ]; |
|
| 58 | + $this->_addresses = [ |
|
| 59 | + 'main' => Configure::read('Saito.Settings.forum_email'), |
|
| 60 | + 'contact' => Configure::read('Saito.Settings.email_contact'), |
|
| 61 | + 'register' => Configure::read('Saito.Settings.email_register'), |
|
| 62 | + 'system' => Configure::read('Saito.Settings.email_system') |
|
| 63 | + ]; |
|
| 64 | 64 | |
| 65 | - foreach ($this->_addresses as $title => $address) { |
|
| 66 | - if (empty($address)) { |
|
| 67 | - throw new InvalidArgumentException("Email address not set: $title"); |
|
| 68 | - } |
|
| 69 | - } |
|
| 65 | + foreach ($this->_addresses as $title => $address) { |
|
| 66 | + if (empty($address)) { |
|
| 67 | + throw new InvalidArgumentException("Email address not set: $title"); |
|
| 68 | + } |
|
| 69 | + } |
|
| 70 | 70 | |
| 71 | - $this->_emailConfigExists = file_exists(APP . 'Config' . DS . 'email' . '.php'); |
|
| 71 | + $this->_emailConfigExists = file_exists(APP . 'Config' . DS . 'email' . '.php'); |
|
| 72 | 72 | |
| 73 | - $this->_forumName = Configure::read('Saito.Settings.forum_name'); |
|
| 73 | + $this->_forumName = Configure::read('Saito.Settings.forum_name'); |
|
| 74 | 74 | |
| 75 | - $this->_CakeEmail = new CakeEmail(); |
|
| 76 | - } |
|
| 75 | + $this->_CakeEmail = new CakeEmail(); |
|
| 76 | + } |
|
| 77 | 77 | |
| 78 | 78 | /** |
| 79 | 79 | * |
@@ -89,172 +89,172 @@ discard block |
||
| 89 | 89 | * @param array $options |
| 90 | 90 | * @return array |
| 91 | 91 | */ |
| 92 | - public function email($options = array()) { |
|
| 93 | - $this->_init(); |
|
| 94 | - $this->_resetConfig(); |
|
| 95 | - $this->_config($options); |
|
| 96 | - $result = $this->_send($this->_config, $this->_viewVars); |
|
| 97 | - |
|
| 98 | - if (isset($options['ccsender']) && $options['ccsender'] === true) { |
|
| 99 | - $result = $this->_sendCopyToOriginalSender($this->_config, |
|
| 100 | - $this->_viewVars); |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - return $result; |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - protected function _resetConfig() { |
|
| 107 | - $this->_config = []; |
|
| 108 | - $this->_viewVars = []; |
|
| 109 | - $this->_CakeEmail->reset(); |
|
| 110 | - } |
|
| 111 | - |
|
| 112 | - protected function _config($params = []) { |
|
| 113 | - $defaults = [ |
|
| 114 | - 'viewVars' => [ |
|
| 115 | - 'webroot' => Router::fullBaseUrl() . $this->_webroot, |
|
| 116 | - ], |
|
| 117 | - ]; |
|
| 118 | - $params = array_merge_recursive($defaults, $params); |
|
| 119 | - |
|
| 120 | - $this->_initConfigFromFile(); |
|
| 121 | - |
|
| 122 | - $this->_sender = $this->_getSender($params['sender']); |
|
| 123 | - $this->_recipient = $this->_getRecipient($params['recipient']); |
|
| 124 | - |
|
| 125 | - $this->_config = [ |
|
| 126 | - 'from' => $this->_pA($this->_sender, |
|
| 127 | - $this->_sender['User']['username']), |
|
| 128 | - 'to' => $this->_recipient['User']['user_email'], |
|
| 129 | - 'subject' => $params['subject'], |
|
| 130 | - 'emailFormat' => 'text', |
|
| 131 | - ]; |
|
| 132 | - |
|
| 133 | - //# set 'sender' header |
|
| 134 | - $headerSender = $this->_getHeaderSender(); |
|
| 135 | - if ($headerSender) { |
|
| 136 | - $this->_config['sender'] = $headerSender; |
|
| 137 | - } |
|
| 138 | - |
|
| 139 | - if (isset($params['template'])) { |
|
| 140 | - $this->_config['template'] = $params['template']; |
|
| 141 | - } |
|
| 142 | - |
|
| 143 | - if (isset($params['message'])) { |
|
| 144 | - $this->_viewVars['message'] = $params['message']; |
|
| 145 | - } |
|
| 146 | - $this->_viewVars += $params['viewVars']; |
|
| 147 | - |
|
| 148 | - $this->_configTransport(); |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - protected function _configTransport() { |
|
| 152 | - if (Configure::read('debug') > 2 || Configure::read('Saito.Debug.email')) { |
|
| 153 | - $this->_config['transport'] = 'Debug'; |
|
| 154 | - }; |
|
| 155 | - if (Configure::read('debug') > 2) { |
|
| 156 | - $this->_config['log'] = true; |
|
| 157 | - }; |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - protected function _getHeaderSender() { |
|
| 161 | - if ($this->_emailConfigExists && $this->_CakeEmail->from()) { |
|
| 162 | - // set the forum app address from email.php |
|
| 163 | - $hs = $this->_CakeEmail->from(); |
|
| 164 | - |
|
| 165 | - // set app address name to forum's name if it's not set in email.php |
|
| 166 | - if ((is_array($hs) && key($hs) === current($hs))) { |
|
| 167 | - $hs = $this->_pA(key($hs)); |
|
| 168 | - } |
|
| 169 | - } elseif ($this->_headerSent) { |
|
| 170 | - $type = $this->_headerSent; |
|
| 171 | - $hs = $this->_pA($this->_addresses[$type]); |
|
| 172 | - } else { |
|
| 173 | - $hs = false; |
|
| 174 | - } |
|
| 175 | - return $hs; |
|
| 176 | - } |
|
| 177 | - |
|
| 178 | - /** |
|
| 179 | - * returns participant array (Cake mail array) |
|
| 180 | - * |
|
| 181 | - * @param $address string with address or ['User']-sender/recipient |
|
| 182 | - * @param $name |
|
| 183 | - * @return array [<address> => <name>] |
|
| 184 | - */ |
|
| 185 | - protected function _pA($address, $name = null) { |
|
| 186 | - if (is_array($address) && isset($address['User'])) { |
|
| 187 | - $name = $address['User']['username']; |
|
| 188 | - $address = $address['User']['user_email']; |
|
| 189 | - } |
|
| 190 | - if ($name === null) { |
|
| 191 | - $name = $this->_forumName; |
|
| 192 | - } |
|
| 193 | - return [$address => $name]; |
|
| 194 | - } |
|
| 195 | - |
|
| 196 | - /** |
|
| 197 | - * set base config from app/config/email.php |
|
| 198 | - */ |
|
| 199 | - protected function _initConfigFromFile() { |
|
| 200 | - if (!$this->_emailConfigExists) { |
|
| 201 | - return; |
|
| 202 | - } |
|
| 203 | - $this->_CakeEmail->config('saito'); |
|
| 204 | - } |
|
| 205 | - |
|
| 206 | - public function getPredefinedSender($type) { |
|
| 207 | - $this->_init(); |
|
| 208 | - return ['User' => [ |
|
| 209 | - 'username' => $this->_forumName, |
|
| 210 | - 'user_email' => $this->_addresses[$type] |
|
| 211 | - ]]; |
|
| 212 | - } |
|
| 213 | - |
|
| 214 | - protected function _getRecipient($recipient) { |
|
| 215 | - return $this->_getParticipant($recipient); |
|
| 216 | - } |
|
| 217 | - |
|
| 218 | - protected function _getSender($sender) { |
|
| 219 | - if (!is_string($sender) || |
|
| 220 | - !in_array($sender, $this->_predefined) |
|
| 221 | - ) { |
|
| 222 | - // sender-address does not belong to system: is external address |
|
| 223 | - // and should be send 'in behalf off' |
|
| 224 | - $this->_headerSent = 'system'; |
|
| 225 | - } |
|
| 226 | - return $this->_getParticipant($sender); |
|
| 227 | - } |
|
| 228 | - |
|
| 229 | - /** |
|
| 230 | - * @param $value |
|
| 231 | - * @return array |
|
| 232 | - * @throws Exception |
|
| 233 | - */ |
|
| 234 | - protected function _getParticipant($value) { |
|
| 235 | - //# participant-address is valid address |
|
| 236 | - if (is_array($value)) { |
|
| 237 | - return $value; |
|
| 238 | - } |
|
| 239 | - |
|
| 240 | - //# participant-address belongs to system |
|
| 241 | - if (is_string($value) && |
|
| 242 | - in_array($value, $this->_predefined) |
|
| 243 | - ) { |
|
| 244 | - return $this->getPredefinedSender($value); |
|
| 245 | - } |
|
| 246 | - |
|
| 247 | - //# participant-address belongs to external user |
|
| 248 | - $this->_User->id = $value; |
|
| 249 | - $this->_User->contain(); |
|
| 250 | - $participant = $this->_User->read(); |
|
| 251 | - |
|
| 252 | - if (empty($participant)) { |
|
| 253 | - throw new Exception("Can't find participant for email."); |
|
| 254 | - } |
|
| 255 | - |
|
| 256 | - return $participant; |
|
| 257 | - } |
|
| 92 | + public function email($options = array()) { |
|
| 93 | + $this->_init(); |
|
| 94 | + $this->_resetConfig(); |
|
| 95 | + $this->_config($options); |
|
| 96 | + $result = $this->_send($this->_config, $this->_viewVars); |
|
| 97 | + |
|
| 98 | + if (isset($options['ccsender']) && $options['ccsender'] === true) { |
|
| 99 | + $result = $this->_sendCopyToOriginalSender($this->_config, |
|
| 100 | + $this->_viewVars); |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + return $result; |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + protected function _resetConfig() { |
|
| 107 | + $this->_config = []; |
|
| 108 | + $this->_viewVars = []; |
|
| 109 | + $this->_CakeEmail->reset(); |
|
| 110 | + } |
|
| 111 | + |
|
| 112 | + protected function _config($params = []) { |
|
| 113 | + $defaults = [ |
|
| 114 | + 'viewVars' => [ |
|
| 115 | + 'webroot' => Router::fullBaseUrl() . $this->_webroot, |
|
| 116 | + ], |
|
| 117 | + ]; |
|
| 118 | + $params = array_merge_recursive($defaults, $params); |
|
| 119 | + |
|
| 120 | + $this->_initConfigFromFile(); |
|
| 121 | + |
|
| 122 | + $this->_sender = $this->_getSender($params['sender']); |
|
| 123 | + $this->_recipient = $this->_getRecipient($params['recipient']); |
|
| 124 | + |
|
| 125 | + $this->_config = [ |
|
| 126 | + 'from' => $this->_pA($this->_sender, |
|
| 127 | + $this->_sender['User']['username']), |
|
| 128 | + 'to' => $this->_recipient['User']['user_email'], |
|
| 129 | + 'subject' => $params['subject'], |
|
| 130 | + 'emailFormat' => 'text', |
|
| 131 | + ]; |
|
| 132 | + |
|
| 133 | + //# set 'sender' header |
|
| 134 | + $headerSender = $this->_getHeaderSender(); |
|
| 135 | + if ($headerSender) { |
|
| 136 | + $this->_config['sender'] = $headerSender; |
|
| 137 | + } |
|
| 138 | + |
|
| 139 | + if (isset($params['template'])) { |
|
| 140 | + $this->_config['template'] = $params['template']; |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + if (isset($params['message'])) { |
|
| 144 | + $this->_viewVars['message'] = $params['message']; |
|
| 145 | + } |
|
| 146 | + $this->_viewVars += $params['viewVars']; |
|
| 147 | + |
|
| 148 | + $this->_configTransport(); |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + protected function _configTransport() { |
|
| 152 | + if (Configure::read('debug') > 2 || Configure::read('Saito.Debug.email')) { |
|
| 153 | + $this->_config['transport'] = 'Debug'; |
|
| 154 | + }; |
|
| 155 | + if (Configure::read('debug') > 2) { |
|
| 156 | + $this->_config['log'] = true; |
|
| 157 | + }; |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + protected function _getHeaderSender() { |
|
| 161 | + if ($this->_emailConfigExists && $this->_CakeEmail->from()) { |
|
| 162 | + // set the forum app address from email.php |
|
| 163 | + $hs = $this->_CakeEmail->from(); |
|
| 164 | + |
|
| 165 | + // set app address name to forum's name if it's not set in email.php |
|
| 166 | + if ((is_array($hs) && key($hs) === current($hs))) { |
|
| 167 | + $hs = $this->_pA(key($hs)); |
|
| 168 | + } |
|
| 169 | + } elseif ($this->_headerSent) { |
|
| 170 | + $type = $this->_headerSent; |
|
| 171 | + $hs = $this->_pA($this->_addresses[$type]); |
|
| 172 | + } else { |
|
| 173 | + $hs = false; |
|
| 174 | + } |
|
| 175 | + return $hs; |
|
| 176 | + } |
|
| 177 | + |
|
| 178 | + /** |
|
| 179 | + * returns participant array (Cake mail array) |
|
| 180 | + * |
|
| 181 | + * @param $address string with address or ['User']-sender/recipient |
|
| 182 | + * @param $name |
|
| 183 | + * @return array [<address> => <name>] |
|
| 184 | + */ |
|
| 185 | + protected function _pA($address, $name = null) { |
|
| 186 | + if (is_array($address) && isset($address['User'])) { |
|
| 187 | + $name = $address['User']['username']; |
|
| 188 | + $address = $address['User']['user_email']; |
|
| 189 | + } |
|
| 190 | + if ($name === null) { |
|
| 191 | + $name = $this->_forumName; |
|
| 192 | + } |
|
| 193 | + return [$address => $name]; |
|
| 194 | + } |
|
| 195 | + |
|
| 196 | + /** |
|
| 197 | + * set base config from app/config/email.php |
|
| 198 | + */ |
|
| 199 | + protected function _initConfigFromFile() { |
|
| 200 | + if (!$this->_emailConfigExists) { |
|
| 201 | + return; |
|
| 202 | + } |
|
| 203 | + $this->_CakeEmail->config('saito'); |
|
| 204 | + } |
|
| 205 | + |
|
| 206 | + public function getPredefinedSender($type) { |
|
| 207 | + $this->_init(); |
|
| 208 | + return ['User' => [ |
|
| 209 | + 'username' => $this->_forumName, |
|
| 210 | + 'user_email' => $this->_addresses[$type] |
|
| 211 | + ]]; |
|
| 212 | + } |
|
| 213 | + |
|
| 214 | + protected function _getRecipient($recipient) { |
|
| 215 | + return $this->_getParticipant($recipient); |
|
| 216 | + } |
|
| 217 | + |
|
| 218 | + protected function _getSender($sender) { |
|
| 219 | + if (!is_string($sender) || |
|
| 220 | + !in_array($sender, $this->_predefined) |
|
| 221 | + ) { |
|
| 222 | + // sender-address does not belong to system: is external address |
|
| 223 | + // and should be send 'in behalf off' |
|
| 224 | + $this->_headerSent = 'system'; |
|
| 225 | + } |
|
| 226 | + return $this->_getParticipant($sender); |
|
| 227 | + } |
|
| 228 | + |
|
| 229 | + /** |
|
| 230 | + * @param $value |
|
| 231 | + * @return array |
|
| 232 | + * @throws Exception |
|
| 233 | + */ |
|
| 234 | + protected function _getParticipant($value) { |
|
| 235 | + //# participant-address is valid address |
|
| 236 | + if (is_array($value)) { |
|
| 237 | + return $value; |
|
| 238 | + } |
|
| 239 | + |
|
| 240 | + //# participant-address belongs to system |
|
| 241 | + if (is_string($value) && |
|
| 242 | + in_array($value, $this->_predefined) |
|
| 243 | + ) { |
|
| 244 | + return $this->getPredefinedSender($value); |
|
| 245 | + } |
|
| 246 | + |
|
| 247 | + //# participant-address belongs to external user |
|
| 248 | + $this->_User->id = $value; |
|
| 249 | + $this->_User->contain(); |
|
| 250 | + $participant = $this->_User->read(); |
|
| 251 | + |
|
| 252 | + if (empty($participant)) { |
|
| 253 | + throw new Exception("Can't find participant for email."); |
|
| 254 | + } |
|
| 255 | + |
|
| 256 | + return $participant; |
|
| 257 | + } |
|
| 258 | 258 | |
| 259 | 259 | /** |
| 260 | 260 | * Sends a copy of a completely configured email to the author |
@@ -262,33 +262,33 @@ discard block |
||
| 262 | 262 | * @param $config |
| 263 | 263 | * @param $viewVars |
| 264 | 264 | */ |
| 265 | - protected function _sendCopyToOriginalSender($config, $viewVars) { |
|
| 266 | - // use name for recipient if available |
|
| 267 | - if (!empty($this->_recipient['User']['username'])) { |
|
| 268 | - $emailConfig['to'] = $this->_pA($this->_recipient['User']['user_email'], |
|
| 269 | - $this->_recipient['User']['username']); |
|
| 270 | - } |
|
| 271 | - |
|
| 272 | - // set new subject |
|
| 273 | - $data = array('subject' => $config['subject']); |
|
| 274 | - if (is_array($config['to'])) { |
|
| 275 | - $data['recipient-name'] = current($config['to']); |
|
| 276 | - $str = __('Copy of your message: ":subject" to ":recipient-name"'); |
|
| 277 | - } else { |
|
| 278 | - $str = __('Copy of your message: ":subject"'); |
|
| 279 | - } |
|
| 280 | - $config['subject'] = CakeText::insert($str, $data); |
|
| 281 | - |
|
| 282 | - // set new addresses |
|
| 283 | - $config['to'] = $config['from']; |
|
| 284 | - // @todo should be system message |
|
| 285 | - $config['from'] = $this->_pA($this->getPredefinedSender('system')); |
|
| 286 | - |
|
| 287 | - // CC is always send by system |
|
| 288 | - unset($config['sender']); |
|
| 289 | - |
|
| 290 | - return $this->_send($config, $viewVars); |
|
| 291 | - } |
|
| 265 | + protected function _sendCopyToOriginalSender($config, $viewVars) { |
|
| 266 | + // use name for recipient if available |
|
| 267 | + if (!empty($this->_recipient['User']['username'])) { |
|
| 268 | + $emailConfig['to'] = $this->_pA($this->_recipient['User']['user_email'], |
|
| 269 | + $this->_recipient['User']['username']); |
|
| 270 | + } |
|
| 271 | + |
|
| 272 | + // set new subject |
|
| 273 | + $data = array('subject' => $config['subject']); |
|
| 274 | + if (is_array($config['to'])) { |
|
| 275 | + $data['recipient-name'] = current($config['to']); |
|
| 276 | + $str = __('Copy of your message: ":subject" to ":recipient-name"'); |
|
| 277 | + } else { |
|
| 278 | + $str = __('Copy of your message: ":subject"'); |
|
| 279 | + } |
|
| 280 | + $config['subject'] = CakeText::insert($str, $data); |
|
| 281 | + |
|
| 282 | + // set new addresses |
|
| 283 | + $config['to'] = $config['from']; |
|
| 284 | + // @todo should be system message |
|
| 285 | + $config['from'] = $this->_pA($this->getPredefinedSender('system')); |
|
| 286 | + |
|
| 287 | + // CC is always send by system |
|
| 288 | + unset($config['sender']); |
|
| 289 | + |
|
| 290 | + return $this->_send($config, $viewVars); |
|
| 291 | + } |
|
| 292 | 292 | |
| 293 | 293 | /** |
| 294 | 294 | * Sends the completely configured email |
@@ -296,12 +296,12 @@ discard block |
||
| 296 | 296 | * @param $config |
| 297 | 297 | * @param $viewVars |
| 298 | 298 | */ |
| 299 | - protected function _send($config, $viewVars) { |
|
| 300 | - $email = $this->_CakeEmail; |
|
| 301 | - // workaround for http://cakephp.lighthouseapp.com/projects/42648/tickets/2855-cakeemail-transports-have-ambiguous-config-behaviors |
|
| 302 | - $email->config(array_merge($this->_CakeEmail->config(), $config)); |
|
| 303 | - $email->viewVars($viewVars); |
|
| 304 | - return $email->send(); |
|
| 305 | - } |
|
| 306 | - |
|
| 307 | - } |
|
| 308 | 299 | \ No newline at end of file |
| 300 | + protected function _send($config, $viewVars) { |
|
| 301 | + $email = $this->_CakeEmail; |
|
| 302 | + // workaround for http://cakephp.lighthouseapp.com/projects/42648/tickets/2855-cakeemail-transports-have-ambiguous-config-behaviors |
|
| 303 | + $email->config(array_merge($this->_CakeEmail->config(), $config)); |
|
| 304 | + $email->viewVars($viewVars); |
|
| 305 | + return $email->send(); |
|
| 306 | + } |
|
| 307 | + |
|
| 308 | + } |
|
| 309 | 309 | \ No newline at end of file |
@@ -650,7 +650,7 @@ discard block |
||
| 650 | 650 | * @param null $id |
| 651 | 651 | * @param null $toggle |
| 652 | 652 | * |
| 653 | - * @return translated |
|
| 653 | + * @return null|string |
|
| 654 | 654 | */ |
| 655 | 655 | public function ajax_toggle($id = null, $toggle = null) { |
| 656 | 656 | $this->autoLayout = false; |
@@ -781,6 +781,9 @@ discard block |
||
| 781 | 781 | } |
| 782 | 782 | } |
| 783 | 783 | |
| 784 | + /** |
|
| 785 | + * @param string $type |
|
| 786 | + */ |
|
| 784 | 787 | protected function _incrementViews($entry, $type = null) { |
| 785 | 788 | if ($this->CurrentUser->isBot()) { |
| 786 | 789 | return; |
@@ -947,6 +950,9 @@ discard block |
||
| 947 | 950 | $this->set('rootEntry', $_rootEntry); |
| 948 | 951 | } |
| 949 | 952 | |
| 953 | + /** |
|
| 954 | + * @param string $type |
|
| 955 | + */ |
|
| 950 | 956 | protected function _setTitleFromEntry($entry, $type = null) { |
| 951 | 957 | if ($type === null) { |
| 952 | 958 | $template = __(':subject | :category'); |
@@ -43,8 +43,8 @@ discard block |
||
| 43 | 43 | |
| 44 | 44 | $currentPage = 1; |
| 45 | 45 | if (isset($this->request->named['page']) && $this->request->named['page'] != 1) { |
| 46 | - $currentPage = (int)$this->request->named['page']; |
|
| 47 | - $this->set('title_for_layout', __('page') . ' ' . $currentPage); |
|
| 46 | + $currentPage = (int) $this->request->named['page']; |
|
| 47 | + $this->set('title_for_layout', __('page').' '.$currentPage); |
|
| 48 | 48 | } |
| 49 | 49 | if ($currentPage === 1 && $this->CurrentUser->isLoggedIn() |
| 50 | 50 | && $this->CurrentUser['user_automaticaly_mark_as_read'] |
@@ -146,11 +146,11 @@ discard block |
||
| 146 | 146 | public function source($id = null) { |
| 147 | 147 | $this->autoRender = false; |
| 148 | 148 | |
| 149 | - $data = $this->requestAction('/entries/view/' . $id); |
|
| 149 | + $data = $this->requestAction('/entries/view/'.$id); |
|
| 150 | 150 | |
| 151 | 151 | $out = array(); |
| 152 | 152 | $out[] = '<pre style="white-space: pre-wrap;">'; |
| 153 | - $out[] = $data['Entry']['subject'] . "\n"; |
|
| 153 | + $out[] = $data['Entry']['subject']."\n"; |
|
| 154 | 154 | $out[] = $data['Entry']['text']; |
| 155 | 155 | $out[] = '</pre>'; |
| 156 | 156 | return implode("\n", $out); |
@@ -233,9 +233,9 @@ discard block |
||
| 233 | 233 | |
| 234 | 234 | return json_encode( |
| 235 | 235 | [ |
| 236 | - 'id' => (int)$newPosting['Entry']['id'], |
|
| 237 | - 'pid' => (int)$newPosting['Entry']['pid'], |
|
| 238 | - 'tid' => (int)$newPosting['Entry']['tid'] |
|
| 236 | + 'id' => (int) $newPosting['Entry']['id'], |
|
| 237 | + 'pid' => (int) $newPosting['Entry']['pid'], |
|
| 238 | + 'tid' => (int) $newPosting['Entry']['tid'] |
|
| 239 | 239 | ] |
| 240 | 240 | ); |
| 241 | 241 | } else { |
@@ -336,8 +336,8 @@ discard block |
||
| 336 | 336 | } |
| 337 | 337 | } |
| 338 | 338 | |
| 339 | - $this->set('is_answer', (int)$this->request->data['Entry']['pid'] !== 0); |
|
| 340 | - $this->set('is_inline', (int)$this->request->data['Entry']['pid'] !== 0); |
|
| 339 | + $this->set('is_answer', (int) $this->request->data['Entry']['pid'] !== 0); |
|
| 340 | + $this->set('is_inline', (int) $this->request->data['Entry']['pid'] !== 0); |
|
| 341 | 341 | $this->set('form_id', $this->request->data['Entry']['pid']); |
| 342 | 342 | $this->_setAddViewVars(); |
| 343 | 343 | } |
@@ -439,7 +439,7 @@ discard block |
||
| 439 | 439 | ); |
| 440 | 440 | $this->set('notis', $notis); |
| 441 | 441 | |
| 442 | - $this->set('is_answer', (int)$this->request->data['Entry']['pid'] !== 0); |
|
| 442 | + $this->set('is_answer', (int) $this->request->data['Entry']['pid'] !== 0); |
|
| 443 | 443 | $this->set('is_inline', false); |
| 444 | 444 | $this->set('form_id', $this->request->data['Entry']['pid']); |
| 445 | 445 | |
@@ -487,7 +487,7 @@ discard block |
||
| 487 | 487 | $this->redirect('/'); |
| 488 | 488 | } else { |
| 489 | 489 | $this->Session->setFlash(__('delete_subtree_success'), 'flash/success'); |
| 490 | - $this->redirect('/entries/view/' . $entry['Entry']['pid']); |
|
| 490 | + $this->redirect('/entries/view/'.$entry['Entry']['pid']); |
|
| 491 | 491 | } |
| 492 | 492 | } else { |
| 493 | 493 | $this->Session->setFlash(__('delete_tree_error'), 'flash/error'); |
@@ -587,16 +587,16 @@ discard block |
||
| 587 | 587 | else : |
| 588 | 588 | // validation errors |
| 589 | 589 | foreach ($errors as $field => $error) { |
| 590 | - $message = __d('nondynamic', $field) . ": " . __d( 'nondynamic', $error[0]); |
|
| 590 | + $message = __d('nondynamic', $field).": ".__d('nondynamic', $error[0]); |
|
| 591 | 591 | $this->JsData->addAppJsMessage( |
| 592 | 592 | $message, |
| 593 | 593 | [ |
| 594 | 594 | 'type' => 'error', |
| 595 | 595 | 'channel' => 'form', |
| 596 | - 'element' => '#Entry' . array_reduce( |
|
| 596 | + 'element' => '#Entry'.array_reduce( |
|
| 597 | 597 | explode('_', $field), |
| 598 | - function ($carry, $item) { |
|
| 599 | - return $carry . ucfirst($item); |
|
| 598 | + function($carry, $item) { |
|
| 599 | + return $carry.ucfirst($item); |
|
| 600 | 600 | }, |
| 601 | 601 | '' |
| 602 | 602 | ) |
@@ -626,7 +626,7 @@ discard block |
||
| 626 | 626 | $this->Entry->contain(); |
| 627 | 627 | $data = $this->Entry->findById($id); |
| 628 | 628 | |
| 629 | - if (!$data || (int)$data['Entry']['pid'] !== 0) { |
|
| 629 | + if (!$data || (int) $data['Entry']['pid'] !== 0) { |
|
| 630 | 630 | throw new NotFoundException(); |
| 631 | 631 | } |
| 632 | 632 | |
@@ -635,7 +635,7 @@ discard block |
||
| 635 | 635 | $targetId = $this->request->data['Entry']['targetId']; |
| 636 | 636 | $this->Entry->id = $id; |
| 637 | 637 | if ($this->Entry->threadMerge($targetId)) { |
| 638 | - $this->redirect('/entries/view/' . $id); |
|
| 638 | + $this->redirect('/entries/view/'.$id); |
|
| 639 | 639 | return; |
| 640 | 640 | } else { |
| 641 | 641 | $this->Session->setFlash(__("Error"), 'flash/error'); |
@@ -676,10 +676,10 @@ discard block |
||
| 676 | 676 | //* let's toggle |
| 677 | 677 | $this->Entry->id = $id; |
| 678 | 678 | $this->request->data = $this->Entry->toggle($toggle); |
| 679 | - return ($this->request->data == 0) ? __d('nondynamic', $toggle . '_set_entry_link') : __d('nondynamic', $toggle . '_unset_entry_link'); |
|
| 679 | + return ($this->request->data == 0) ? __d('nondynamic', $toggle.'_set_entry_link') : __d('nondynamic', $toggle.'_unset_entry_link'); |
|
| 680 | 680 | } |
| 681 | 681 | |
| 682 | - $this->set('json_data', (string)$this->request->data); |
|
| 682 | + $this->set('json_data', (string) $this->request->data); |
|
| 683 | 683 | $this->render('/Elements/json/json_data'); |
| 684 | 684 | } |
| 685 | 685 | |
@@ -925,7 +925,7 @@ discard block |
||
| 925 | 925 | // … directly in entries/mix |
| 926 | 926 | || $this->request->action === 'mix' |
| 927 | 927 | // … inline viewing … on entries/index. |
| 928 | - || ( $this->localReferer('controller') === 'entries' && $this->localReferer('action') === 'index') |
|
| 928 | + || ($this->localReferer('controller') === 'entries' && $this->localReferer('action') === 'index') |
|
| 929 | 929 | ): |
| 930 | 930 | $showAnsweringPanel = true; |
| 931 | 931 | endif; |
@@ -934,7 +934,7 @@ discard block |
||
| 934 | 934 | } |
| 935 | 935 | |
| 936 | 936 | protected function _setRootEntry($entry) { |
| 937 | - if ((int)$entry['Entry']['pid'] !== 0) { |
|
| 937 | + if ((int) $entry['Entry']['pid'] !== 0) { |
|
| 938 | 938 | $_rootEntry = $this->Entry->find('first', |
| 939 | 939 | [ |
| 940 | 940 | 'contain' => false, |
@@ -949,9 +949,9 @@ discard block |
||
| 949 | 949 | |
| 950 | 950 | protected function _setTitleFromEntry($entry, $type = null) { |
| 951 | 951 | if ($type === null) { |
| 952 | - $template = __(':subject | :category'); |
|
| 952 | + $template = __(':subject | :category'); |
|
| 953 | 953 | } else { |
| 954 | - $template = __(':subject (:type) | :category'); |
|
| 954 | + $template = __(':subject (:type) | :category'); |
|
| 955 | 955 | } |
| 956 | 956 | $this->set('title_for_layout', |
| 957 | 957 | CakeText::insert($template, |
@@ -1,140 +1,140 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | - use Saito\User\Categories; |
|
| 4 | - |
|
| 5 | - App::uses('AppController', 'Controller'); |
|
| 6 | - |
|
| 7 | - class EntriesController extends AppController { |
|
| 8 | - |
|
| 9 | - public $name = 'Entries'; |
|
| 10 | - |
|
| 11 | - public $helpers = [ |
|
| 12 | - 'EntryH', |
|
| 13 | - 'MarkitupEditor', |
|
| 14 | - 'Shouts', |
|
| 15 | - 'Text', |
|
| 16 | - ]; |
|
| 17 | - |
|
| 18 | - public $components = [ |
|
| 19 | - 'Shouts' |
|
| 20 | - ]; |
|
| 21 | - |
|
| 22 | - public function index() { |
|
| 23 | - Stopwatch::start('Entries->index()'); |
|
| 24 | - |
|
| 25 | - $this->_prepareSlidetabData(); |
|
| 26 | - |
|
| 27 | - //= determine user sort order |
|
| 28 | - $sortKey = 'Entry.'; |
|
| 29 | - if (!$this->CurrentUser->isLoggedIn() || $this->CurrentUser['user_sort_last_answer']) { |
|
| 30 | - $sortKey .= 'last_answer'; |
|
| 31 | - } else { |
|
| 32 | - $sortKey .= 'time'; |
|
| 33 | - } |
|
| 34 | - $order = ['Entry.fixed' => 'DESC', $sortKey => 'DESC']; |
|
| 35 | - |
|
| 36 | - //= get threads |
|
| 37 | - $initials = $this->_getInitialThreads($this->CurrentUser, $order); |
|
| 38 | - $threads = $this->Entry->treesForThreads($initials, $order); |
|
| 39 | - foreach ($threads as $tid => $thread) { |
|
| 40 | - $threads[$tid] = $this->dic->newInstance('\Saito\Posting\Posting', ['rawData' => $thread]); |
|
| 41 | - } |
|
| 42 | - $this->set('entries', $threads); |
|
| 43 | - |
|
| 44 | - $currentPage = 1; |
|
| 45 | - if (isset($this->request->named['page']) && $this->request->named['page'] != 1) { |
|
| 46 | - $currentPage = (int)$this->request->named['page']; |
|
| 47 | - $this->set('title_for_layout', __('page') . ' ' . $currentPage); |
|
| 48 | - } |
|
| 49 | - if ($currentPage === 1 && $this->CurrentUser->isLoggedIn() |
|
| 50 | - && $this->CurrentUser['user_automaticaly_mark_as_read'] |
|
| 51 | - ) { |
|
| 52 | - $this->set('markAsRead', true); |
|
| 53 | - } |
|
| 54 | - // @bogus |
|
| 55 | - $this->Session->write('paginator.lastPage', $currentPage); |
|
| 56 | - $this->showDisclaimer = true; |
|
| 57 | - $this->set('allowThreadCollapse', true); |
|
| 58 | - |
|
| 59 | - Stopwatch::stop('Entries->index()'); |
|
| 60 | - } |
|
| 61 | - |
|
| 62 | - public function feed() { |
|
| 63 | - Configure::write('debug', 0); |
|
| 64 | - |
|
| 65 | - if (isset($this->request->params['named']['depth']) && |
|
| 66 | - $this->request->params['named']['depth'] === 'start' |
|
| 67 | - ) { |
|
| 68 | - $title = __('Last started threads'); |
|
| 69 | - $order = 'time DESC'; |
|
| 70 | - $conditions['pid'] = 0; |
|
| 71 | - } else { |
|
| 72 | - $title = __('Last entries'); |
|
| 73 | - $order = 'last_answer DESC'; |
|
| 74 | - } |
|
| 75 | - |
|
| 76 | - $conditions['category_id'] = $this->CurrentUser->Categories->getAllowed(); |
|
| 77 | - |
|
| 78 | - $entries = $this->Entry->find( |
|
| 79 | - 'feed', |
|
| 80 | - [ |
|
| 81 | - 'conditions' => $conditions, |
|
| 82 | - 'order' => $order |
|
| 83 | - ] |
|
| 84 | - ); |
|
| 85 | - $this->set('entries', $entries); |
|
| 86 | - |
|
| 87 | - // serialize for JSON |
|
| 88 | - $this->set('_serialize', 'entries'); |
|
| 89 | - $this->set('title', $title); |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - /** |
|
| 93 | - * Mix view |
|
| 94 | - * |
|
| 95 | - * @param $tid |
|
| 96 | - * @throws NotFoundException |
|
| 97 | - */ |
|
| 98 | - public function mix($tid) { |
|
| 99 | - if (!$tid) { |
|
| 100 | - $this->redirect('/'); |
|
| 101 | - } |
|
| 102 | - $entries = $this->Entry->treeForNode($tid, |
|
| 103 | - ['root' => true, 'complete' => true]); |
|
| 104 | - |
|
| 105 | - if (empty($entries)) { |
|
| 106 | - throw new NotFoundException(); |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - // check if anonymous tries to access internal categories |
|
| 110 | - $root = reset($entries); |
|
| 111 | - $accession = $root['Category']['accession']; |
|
| 112 | - if (!$this->CurrentUser->Categories->isAccessionAuthorized($accession)) { |
|
| 113 | - $this->_requireAuth(); |
|
| 114 | - return; |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - $this->_setRootEntry($root); |
|
| 118 | - $this->_setTitleFromEntry($root, __('view.type.mix')); |
|
| 119 | - |
|
| 120 | - $entries = $this->dic->newInstance('\Saito\Posting\Posting', ['rawData' => $root]); |
|
| 121 | - $this->set('entries', $entries); |
|
| 122 | - |
|
| 123 | - $this->_showAnsweringPanel(); |
|
| 124 | - |
|
| 125 | - $this->_incrementViews($root, 'thread'); |
|
| 126 | - |
|
| 127 | - $this->_marMixThread = $tid; |
|
| 128 | - } |
|
| 3 | + use Saito\User\Categories; |
|
| 4 | + |
|
| 5 | + App::uses('AppController', 'Controller'); |
|
| 6 | + |
|
| 7 | + class EntriesController extends AppController { |
|
| 8 | + |
|
| 9 | + public $name = 'Entries'; |
|
| 10 | + |
|
| 11 | + public $helpers = [ |
|
| 12 | + 'EntryH', |
|
| 13 | + 'MarkitupEditor', |
|
| 14 | + 'Shouts', |
|
| 15 | + 'Text', |
|
| 16 | + ]; |
|
| 17 | + |
|
| 18 | + public $components = [ |
|
| 19 | + 'Shouts' |
|
| 20 | + ]; |
|
| 21 | + |
|
| 22 | + public function index() { |
|
| 23 | + Stopwatch::start('Entries->index()'); |
|
| 24 | + |
|
| 25 | + $this->_prepareSlidetabData(); |
|
| 26 | + |
|
| 27 | + //= determine user sort order |
|
| 28 | + $sortKey = 'Entry.'; |
|
| 29 | + if (!$this->CurrentUser->isLoggedIn() || $this->CurrentUser['user_sort_last_answer']) { |
|
| 30 | + $sortKey .= 'last_answer'; |
|
| 31 | + } else { |
|
| 32 | + $sortKey .= 'time'; |
|
| 33 | + } |
|
| 34 | + $order = ['Entry.fixed' => 'DESC', $sortKey => 'DESC']; |
|
| 35 | + |
|
| 36 | + //= get threads |
|
| 37 | + $initials = $this->_getInitialThreads($this->CurrentUser, $order); |
|
| 38 | + $threads = $this->Entry->treesForThreads($initials, $order); |
|
| 39 | + foreach ($threads as $tid => $thread) { |
|
| 40 | + $threads[$tid] = $this->dic->newInstance('\Saito\Posting\Posting', ['rawData' => $thread]); |
|
| 41 | + } |
|
| 42 | + $this->set('entries', $threads); |
|
| 43 | + |
|
| 44 | + $currentPage = 1; |
|
| 45 | + if (isset($this->request->named['page']) && $this->request->named['page'] != 1) { |
|
| 46 | + $currentPage = (int)$this->request->named['page']; |
|
| 47 | + $this->set('title_for_layout', __('page') . ' ' . $currentPage); |
|
| 48 | + } |
|
| 49 | + if ($currentPage === 1 && $this->CurrentUser->isLoggedIn() |
|
| 50 | + && $this->CurrentUser['user_automaticaly_mark_as_read'] |
|
| 51 | + ) { |
|
| 52 | + $this->set('markAsRead', true); |
|
| 53 | + } |
|
| 54 | + // @bogus |
|
| 55 | + $this->Session->write('paginator.lastPage', $currentPage); |
|
| 56 | + $this->showDisclaimer = true; |
|
| 57 | + $this->set('allowThreadCollapse', true); |
|
| 58 | + |
|
| 59 | + Stopwatch::stop('Entries->index()'); |
|
| 60 | + } |
|
| 61 | + |
|
| 62 | + public function feed() { |
|
| 63 | + Configure::write('debug', 0); |
|
| 64 | + |
|
| 65 | + if (isset($this->request->params['named']['depth']) && |
|
| 66 | + $this->request->params['named']['depth'] === 'start' |
|
| 67 | + ) { |
|
| 68 | + $title = __('Last started threads'); |
|
| 69 | + $order = 'time DESC'; |
|
| 70 | + $conditions['pid'] = 0; |
|
| 71 | + } else { |
|
| 72 | + $title = __('Last entries'); |
|
| 73 | + $order = 'last_answer DESC'; |
|
| 74 | + } |
|
| 75 | + |
|
| 76 | + $conditions['category_id'] = $this->CurrentUser->Categories->getAllowed(); |
|
| 77 | + |
|
| 78 | + $entries = $this->Entry->find( |
|
| 79 | + 'feed', |
|
| 80 | + [ |
|
| 81 | + 'conditions' => $conditions, |
|
| 82 | + 'order' => $order |
|
| 83 | + ] |
|
| 84 | + ); |
|
| 85 | + $this->set('entries', $entries); |
|
| 86 | + |
|
| 87 | + // serialize for JSON |
|
| 88 | + $this->set('_serialize', 'entries'); |
|
| 89 | + $this->set('title', $title); |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + /** |
|
| 93 | + * Mix view |
|
| 94 | + * |
|
| 95 | + * @param $tid |
|
| 96 | + * @throws NotFoundException |
|
| 97 | + */ |
|
| 98 | + public function mix($tid) { |
|
| 99 | + if (!$tid) { |
|
| 100 | + $this->redirect('/'); |
|
| 101 | + } |
|
| 102 | + $entries = $this->Entry->treeForNode($tid, |
|
| 103 | + ['root' => true, 'complete' => true]); |
|
| 104 | + |
|
| 105 | + if (empty($entries)) { |
|
| 106 | + throw new NotFoundException(); |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + // check if anonymous tries to access internal categories |
|
| 110 | + $root = reset($entries); |
|
| 111 | + $accession = $root['Category']['accession']; |
|
| 112 | + if (!$this->CurrentUser->Categories->isAccessionAuthorized($accession)) { |
|
| 113 | + $this->_requireAuth(); |
|
| 114 | + return; |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + $this->_setRootEntry($root); |
|
| 118 | + $this->_setTitleFromEntry($root, __('view.type.mix')); |
|
| 119 | + |
|
| 120 | + $entries = $this->dic->newInstance('\Saito\Posting\Posting', ['rawData' => $root]); |
|
| 121 | + $this->set('entries', $entries); |
|
| 122 | + |
|
| 123 | + $this->_showAnsweringPanel(); |
|
| 124 | + |
|
| 125 | + $this->_incrementViews($root, 'thread'); |
|
| 126 | + |
|
| 127 | + $this->_marMixThread = $tid; |
|
| 128 | + } |
|
| 129 | 129 | |
| 130 | 130 | /** |
| 131 | 131 | * load front page force all entries mark-as-read |
| 132 | 132 | */ |
| 133 | - public function update() { |
|
| 134 | - $this->autoRender = false; |
|
| 135 | - $this->CurrentUser->LastRefresh->set('now'); |
|
| 136 | - $this->redirect('/entries/index'); |
|
| 137 | - } |
|
| 133 | + public function update() { |
|
| 134 | + $this->autoRender = false; |
|
| 135 | + $this->CurrentUser->LastRefresh->set('now'); |
|
| 136 | + $this->redirect('/entries/index'); |
|
| 137 | + } |
|
| 138 | 138 | |
| 139 | 139 | /** |
| 140 | 140 | * Outputs raw markup of an posting $id |
@@ -143,72 +143,72 @@ discard block |
||
| 143 | 143 | * |
| 144 | 144 | * @return string |
| 145 | 145 | */ |
| 146 | - public function source($id = null) { |
|
| 147 | - $this->autoRender = false; |
|
| 146 | + public function source($id = null) { |
|
| 147 | + $this->autoRender = false; |
|
| 148 | 148 | |
| 149 | - $data = $this->requestAction('/entries/view/' . $id); |
|
| 149 | + $data = $this->requestAction('/entries/view/' . $id); |
|
| 150 | 150 | |
| 151 | - $out = array(); |
|
| 152 | - $out[] = '<pre style="white-space: pre-wrap;">'; |
|
| 153 | - $out[] = $data['Entry']['subject'] . "\n"; |
|
| 154 | - $out[] = $data['Entry']['text']; |
|
| 155 | - $out[] = '</pre>'; |
|
| 156 | - return implode("\n", $out); |
|
| 157 | - } |
|
| 151 | + $out = array(); |
|
| 152 | + $out[] = '<pre style="white-space: pre-wrap;">'; |
|
| 153 | + $out[] = $data['Entry']['subject'] . "\n"; |
|
| 154 | + $out[] = $data['Entry']['text']; |
|
| 155 | + $out[] = '</pre>'; |
|
| 156 | + return implode("\n", $out); |
|
| 157 | + } |
|
| 158 | 158 | |
| 159 | - public function view($id = null) { |
|
| 160 | - Stopwatch::start('Entries->view()'); |
|
| 159 | + public function view($id = null) { |
|
| 160 | + Stopwatch::start('Entries->view()'); |
|
| 161 | 161 | |
| 162 | - // redirect if no id is given |
|
| 163 | - if (!$id) { |
|
| 164 | - $this->Session->setFlash(__('Invalid post')); |
|
| 165 | - return $this->redirect(['action' => 'index']); |
|
| 166 | - } |
|
| 162 | + // redirect if no id is given |
|
| 163 | + if (!$id) { |
|
| 164 | + $this->Session->setFlash(__('Invalid post')); |
|
| 165 | + return $this->redirect(['action' => 'index']); |
|
| 166 | + } |
|
| 167 | 167 | |
| 168 | - $this->Entry->id = $id; |
|
| 169 | - $entry = $this->Entry->get($id); |
|
| 168 | + $this->Entry->id = $id; |
|
| 169 | + $entry = $this->Entry->get($id); |
|
| 170 | 170 | |
| 171 | - // redirect if posting doesn't exists |
|
| 172 | - if ($entry == false) { |
|
| 173 | - $this->Session->setFlash(__('Invalid post')); |
|
| 174 | - $this->redirect('/'); |
|
| 175 | - return; |
|
| 176 | - } |
|
| 171 | + // redirect if posting doesn't exists |
|
| 172 | + if ($entry == false) { |
|
| 173 | + $this->Session->setFlash(__('Invalid post')); |
|
| 174 | + $this->redirect('/'); |
|
| 175 | + return; |
|
| 176 | + } |
|
| 177 | 177 | |
| 178 | - // check if anonymous tries to access internal categories |
|
| 179 | - $accession = $entry['Category']['accession']; |
|
| 180 | - if (!$this->CurrentUser->Categories->isAccessionAuthorized($accession)) { |
|
| 181 | - $this->_requireAuth(); |
|
| 182 | - return; |
|
| 183 | - } |
|
| 178 | + // check if anonymous tries to access internal categories |
|
| 179 | + $accession = $entry['Category']['accession']; |
|
| 180 | + if (!$this->CurrentUser->Categories->isAccessionAuthorized($accession)) { |
|
| 181 | + $this->_requireAuth(); |
|
| 182 | + return; |
|
| 183 | + } |
|
| 184 | 184 | |
| 185 | - $this->_incrementViews($entry); |
|
| 185 | + $this->_incrementViews($entry); |
|
| 186 | 186 | |
| 187 | - // for /source/<id> view |
|
| 188 | - if (!empty($this->request->params['requested'])) { |
|
| 189 | - return $entry; |
|
| 190 | - } |
|
| 187 | + // for /source/<id> view |
|
| 188 | + if (!empty($this->request->params['requested'])) { |
|
| 189 | + return $entry; |
|
| 190 | + } |
|
| 191 | 191 | |
| 192 | - $this->set('entry', $entry); |
|
| 192 | + $this->set('entry', $entry); |
|
| 193 | 193 | |
| 194 | - $this->_setRootEntry($entry); |
|
| 195 | - $this->_showAnsweringPanel(); |
|
| 194 | + $this->_setRootEntry($entry); |
|
| 195 | + $this->_showAnsweringPanel(); |
|
| 196 | 196 | |
| 197 | - $this->CurrentUser->ReadEntries->set($entry); |
|
| 197 | + $this->CurrentUser->ReadEntries->set($entry); |
|
| 198 | 198 | |
| 199 | - // inline open |
|
| 200 | - if ($this->request->is('ajax')) { |
|
| 201 | - $this->render('/Elements/entry/view_posting'); |
|
| 202 | - return; |
|
| 203 | - } |
|
| 199 | + // inline open |
|
| 200 | + if ($this->request->is('ajax')) { |
|
| 201 | + $this->render('/Elements/entry/view_posting'); |
|
| 202 | + return; |
|
| 203 | + } |
|
| 204 | 204 | |
| 205 | - // full page request |
|
| 206 | - $this->set('tree', |
|
| 207 | - $this->Entry->treeForNode($entry['Entry']['tid'], ['root' => true])); |
|
| 208 | - $this->_setTitleFromEntry($entry); |
|
| 205 | + // full page request |
|
| 206 | + $this->set('tree', |
|
| 207 | + $this->Entry->treeForNode($entry['Entry']['tid'], ['root' => true])); |
|
| 208 | + $this->_setTitleFromEntry($entry); |
|
| 209 | 209 | |
| 210 | - Stopwatch::stop('Entries->view()'); |
|
| 211 | - } |
|
| 210 | + Stopwatch::stop('Entries->view()'); |
|
| 211 | + } |
|
| 212 | 212 | |
| 213 | 213 | /** |
| 214 | 214 | * @param null $id |
@@ -216,143 +216,143 @@ discard block |
||
| 216 | 216 | * @return string |
| 217 | 217 | * @throws ForbiddenException |
| 218 | 218 | */ |
| 219 | - public function add($id = null) { |
|
| 220 | - $this->set('title_for_layout', __('Write a New Entry')); |
|
| 221 | - |
|
| 222 | - //# insert new entry |
|
| 223 | - if (!empty($this->request->data)) { |
|
| 224 | - $newPosting = $this->Entry->createPosting($this->request->data); |
|
| 225 | - |
|
| 226 | - // inserting new posting was successful |
|
| 227 | - if ($newPosting !== false) : |
|
| 228 | - $this->_setNotifications($newPosting + $this->request->data); |
|
| 229 | - if ($this->request->is('ajax')) : |
|
| 230 | - // Ajax request came from front answer on front page /entries/index |
|
| 231 | - if ($this->localReferer('action') === 'index') { |
|
| 232 | - $this->autoRender = false; |
|
| 233 | - |
|
| 234 | - return json_encode( |
|
| 235 | - [ |
|
| 236 | - 'id' => (int)$newPosting['Entry']['id'], |
|
| 237 | - 'pid' => (int)$newPosting['Entry']['pid'], |
|
| 238 | - 'tid' => (int)$newPosting['Entry']['tid'] |
|
| 239 | - ] |
|
| 240 | - ); |
|
| 241 | - } else { |
|
| 242 | - $this->_stop(); |
|
| 243 | - } |
|
| 244 | - // answering through POST request |
|
| 245 | - else : |
|
| 246 | - if ($this->localReferer('action') === 'mix') { |
|
| 247 | - // answer request came from mix ansicht |
|
| 248 | - $this->redirect( |
|
| 249 | - [ |
|
| 250 | - 'controller' => 'entries', |
|
| 251 | - 'action' => 'mix', |
|
| 252 | - $newPosting['Entry']['tid'], |
|
| 253 | - '#' => $this->Entry->id |
|
| 254 | - ] |
|
| 255 | - ); |
|
| 256 | - |
|
| 257 | - } else { |
|
| 258 | - // normal posting from entries/add or entries/view |
|
| 259 | - $this->redirect( |
|
| 260 | - [ |
|
| 261 | - 'controller' => 'entries', |
|
| 262 | - 'action' => 'view', |
|
| 263 | - $this->Entry->id |
|
| 264 | - ] |
|
| 265 | - ); |
|
| 266 | - |
|
| 267 | - } |
|
| 268 | - return; |
|
| 269 | - endif; |
|
| 270 | - else : |
|
| 271 | - // Error while trying to save a post |
|
| 272 | - if (count($this->Entry->validationErrors) === 0) { |
|
| 273 | - $this->Session->setFlash( |
|
| 274 | - __( |
|
| 275 | - 'Something clogged the tubes. Could not save entry. Try again.' |
|
| 276 | - ), |
|
| 277 | - 'flash/error' |
|
| 278 | - ); |
|
| 279 | - } |
|
| 280 | - endif; |
|
| 281 | - |
|
| 282 | - // show add form |
|
| 283 | - } else { |
|
| 284 | - $isAnswer = $id !== null; |
|
| 285 | - $this->request->data = null; |
|
| 286 | - |
|
| 287 | - if ($isAnswer) { |
|
| 288 | - if ($this->request->is('ajax') === false) { |
|
| 289 | - $this->redirect($this->referer()); |
|
| 290 | - return; |
|
| 291 | - } |
|
| 292 | - |
|
| 293 | - $this->request->data = $this->Entry->get($id); |
|
| 294 | - $posting = $this->dic->newInstance('\Saito\Posting\Posting', |
|
| 295 | - ['rawData' => $this->request->data]); |
|
| 296 | - |
|
| 297 | - if ($posting->isAnsweringForbidden()) { |
|
| 298 | - throw new ForbiddenException; |
|
| 299 | - } |
|
| 300 | - |
|
| 301 | - // create new subentry |
|
| 302 | - unset($this->request->data['Entry']['id']); |
|
| 303 | - $this->request->data['Entry']['pid'] = $id; |
|
| 304 | - $this->set('citeSubject', $this->request->data['Entry']['subject']); |
|
| 305 | - // subject is empty in answer-form |
|
| 306 | - unset($this->request->data['Entry']['subject']); |
|
| 307 | - $this->set('citeText', $this->request->data['Entry']['text']); |
|
| 308 | - // text field is empty in answer |
|
| 309 | - unset($this->request->data['Entry']['text']); |
|
| 310 | - |
|
| 311 | - // get notifications |
|
| 312 | - $notis = $this->Entry->Esevent->checkEventsForUser( |
|
| 313 | - $this->CurrentUser->getId(), |
|
| 314 | - array( |
|
| 315 | - 1 => array( |
|
| 316 | - 'subject' => $this->request->data['Entry']['tid'], |
|
| 317 | - 'event' => 'Model.Entry.replyToThread', |
|
| 318 | - 'receiver' => 'EmailNotification', |
|
| 319 | - ), |
|
| 320 | - ) |
|
| 321 | - ); |
|
| 322 | - $this->set('notis', $notis); |
|
| 323 | - |
|
| 324 | - // set Subnav |
|
| 325 | - $headerSubnavLeftTitle = __( |
|
| 326 | - 'back_to_posting_from_linkname', |
|
| 327 | - $this->request->data['User']['username'] |
|
| 328 | - ); |
|
| 329 | - $this->set('headerSubnavLeftTitle', $headerSubnavLeftTitle); |
|
| 330 | - |
|
| 331 | - $this->set('title_for_layout', __('Write a Reply')); |
|
| 332 | - } else { |
|
| 333 | - // new posting which creates new thread |
|
| 334 | - $this->request->data['Entry']['pid'] = 0; |
|
| 335 | - $this->request->data['Entry']['tid'] = 0; |
|
| 336 | - } |
|
| 337 | - } |
|
| 338 | - |
|
| 339 | - $this->set('is_answer', (int)$this->request->data['Entry']['pid'] !== 0); |
|
| 340 | - $this->set('is_inline', (int)$this->request->data['Entry']['pid'] !== 0); |
|
| 341 | - $this->set('form_id', $this->request->data['Entry']['pid']); |
|
| 342 | - $this->_setAddViewVars(); |
|
| 343 | - } |
|
| 344 | - |
|
| 345 | - public function threadLine($id) { |
|
| 346 | - $entry = $this->Entry->get($id); |
|
| 347 | - $accession = $entry['Category']['accession']; |
|
| 348 | - if (!$this->CurrentUser->Categories->isAccessionAuthorized($accession)) { |
|
| 349 | - $this->_requireAuth(); |
|
| 350 | - return; |
|
| 351 | - } |
|
| 352 | - $this->set('entry_sub', $entry); |
|
| 353 | - // ajax requests so far are always answers |
|
| 354 | - $this->set('level', '1'); |
|
| 355 | - } |
|
| 219 | + public function add($id = null) { |
|
| 220 | + $this->set('title_for_layout', __('Write a New Entry')); |
|
| 221 | + |
|
| 222 | + //# insert new entry |
|
| 223 | + if (!empty($this->request->data)) { |
|
| 224 | + $newPosting = $this->Entry->createPosting($this->request->data); |
|
| 225 | + |
|
| 226 | + // inserting new posting was successful |
|
| 227 | + if ($newPosting !== false) : |
|
| 228 | + $this->_setNotifications($newPosting + $this->request->data); |
|
| 229 | + if ($this->request->is('ajax')) : |
|
| 230 | + // Ajax request came from front answer on front page /entries/index |
|
| 231 | + if ($this->localReferer('action') === 'index') { |
|
| 232 | + $this->autoRender = false; |
|
| 233 | + |
|
| 234 | + return json_encode( |
|
| 235 | + [ |
|
| 236 | + 'id' => (int)$newPosting['Entry']['id'], |
|
| 237 | + 'pid' => (int)$newPosting['Entry']['pid'], |
|
| 238 | + 'tid' => (int)$newPosting['Entry']['tid'] |
|
| 239 | + ] |
|
| 240 | + ); |
|
| 241 | + } else { |
|
| 242 | + $this->_stop(); |
|
| 243 | + } |
|
| 244 | + // answering through POST request |
|
| 245 | + else : |
|
| 246 | + if ($this->localReferer('action') === 'mix') { |
|
| 247 | + // answer request came from mix ansicht |
|
| 248 | + $this->redirect( |
|
| 249 | + [ |
|
| 250 | + 'controller' => 'entries', |
|
| 251 | + 'action' => 'mix', |
|
| 252 | + $newPosting['Entry']['tid'], |
|
| 253 | + '#' => $this->Entry->id |
|
| 254 | + ] |
|
| 255 | + ); |
|
| 256 | + |
|
| 257 | + } else { |
|
| 258 | + // normal posting from entries/add or entries/view |
|
| 259 | + $this->redirect( |
|
| 260 | + [ |
|
| 261 | + 'controller' => 'entries', |
|
| 262 | + 'action' => 'view', |
|
| 263 | + $this->Entry->id |
|
| 264 | + ] |
|
| 265 | + ); |
|
| 266 | + |
|
| 267 | + } |
|
| 268 | + return; |
|
| 269 | + endif; |
|
| 270 | + else : |
|
| 271 | + // Error while trying to save a post |
|
| 272 | + if (count($this->Entry->validationErrors) === 0) { |
|
| 273 | + $this->Session->setFlash( |
|
| 274 | + __( |
|
| 275 | + 'Something clogged the tubes. Could not save entry. Try again.' |
|
| 276 | + ), |
|
| 277 | + 'flash/error' |
|
| 278 | + ); |
|
| 279 | + } |
|
| 280 | + endif; |
|
| 281 | + |
|
| 282 | + // show add form |
|
| 283 | + } else { |
|
| 284 | + $isAnswer = $id !== null; |
|
| 285 | + $this->request->data = null; |
|
| 286 | + |
|
| 287 | + if ($isAnswer) { |
|
| 288 | + if ($this->request->is('ajax') === false) { |
|
| 289 | + $this->redirect($this->referer()); |
|
| 290 | + return; |
|
| 291 | + } |
|
| 292 | + |
|
| 293 | + $this->request->data = $this->Entry->get($id); |
|
| 294 | + $posting = $this->dic->newInstance('\Saito\Posting\Posting', |
|
| 295 | + ['rawData' => $this->request->data]); |
|
| 296 | + |
|
| 297 | + if ($posting->isAnsweringForbidden()) { |
|
| 298 | + throw new ForbiddenException; |
|
| 299 | + } |
|
| 300 | + |
|
| 301 | + // create new subentry |
|
| 302 | + unset($this->request->data['Entry']['id']); |
|
| 303 | + $this->request->data['Entry']['pid'] = $id; |
|
| 304 | + $this->set('citeSubject', $this->request->data['Entry']['subject']); |
|
| 305 | + // subject is empty in answer-form |
|
| 306 | + unset($this->request->data['Entry']['subject']); |
|
| 307 | + $this->set('citeText', $this->request->data['Entry']['text']); |
|
| 308 | + // text field is empty in answer |
|
| 309 | + unset($this->request->data['Entry']['text']); |
|
| 310 | + |
|
| 311 | + // get notifications |
|
| 312 | + $notis = $this->Entry->Esevent->checkEventsForUser( |
|
| 313 | + $this->CurrentUser->getId(), |
|
| 314 | + array( |
|
| 315 | + 1 => array( |
|
| 316 | + 'subject' => $this->request->data['Entry']['tid'], |
|
| 317 | + 'event' => 'Model.Entry.replyToThread', |
|
| 318 | + 'receiver' => 'EmailNotification', |
|
| 319 | + ), |
|
| 320 | + ) |
|
| 321 | + ); |
|
| 322 | + $this->set('notis', $notis); |
|
| 323 | + |
|
| 324 | + // set Subnav |
|
| 325 | + $headerSubnavLeftTitle = __( |
|
| 326 | + 'back_to_posting_from_linkname', |
|
| 327 | + $this->request->data['User']['username'] |
|
| 328 | + ); |
|
| 329 | + $this->set('headerSubnavLeftTitle', $headerSubnavLeftTitle); |
|
| 330 | + |
|
| 331 | + $this->set('title_for_layout', __('Write a Reply')); |
|
| 332 | + } else { |
|
| 333 | + // new posting which creates new thread |
|
| 334 | + $this->request->data['Entry']['pid'] = 0; |
|
| 335 | + $this->request->data['Entry']['tid'] = 0; |
|
| 336 | + } |
|
| 337 | + } |
|
| 338 | + |
|
| 339 | + $this->set('is_answer', (int)$this->request->data['Entry']['pid'] !== 0); |
|
| 340 | + $this->set('is_inline', (int)$this->request->data['Entry']['pid'] !== 0); |
|
| 341 | + $this->set('form_id', $this->request->data['Entry']['pid']); |
|
| 342 | + $this->_setAddViewVars(); |
|
| 343 | + } |
|
| 344 | + |
|
| 345 | + public function threadLine($id) { |
|
| 346 | + $entry = $this->Entry->get($id); |
|
| 347 | + $accession = $entry['Category']['accession']; |
|
| 348 | + if (!$this->CurrentUser->Categories->isAccessionAuthorized($accession)) { |
|
| 349 | + $this->_requireAuth(); |
|
| 350 | + return; |
|
| 351 | + } |
|
| 352 | + $this->set('entry_sub', $entry); |
|
| 353 | + // ajax requests so far are always answers |
|
| 354 | + $this->set('level', '1'); |
|
| 355 | + } |
|
| 356 | 356 | |
| 357 | 357 | /** |
| 358 | 358 | * @param null $id |
@@ -360,99 +360,99 @@ discard block |
||
| 360 | 360 | * @throws NotFoundException |
| 361 | 361 | * @throws BadRequestException |
| 362 | 362 | */ |
| 363 | - public function edit($id = null) { |
|
| 364 | - if (empty($id)) { |
|
| 365 | - throw new BadRequestException(); |
|
| 366 | - } |
|
| 367 | - |
|
| 368 | - $oldEntry = $this->Entry->get($id); |
|
| 369 | - if (!$oldEntry) { |
|
| 370 | - throw new NotFoundException(); |
|
| 371 | - } |
|
| 372 | - |
|
| 373 | - /** * @var \Saito\Posting\Posting */ |
|
| 374 | - $posting = $this->dic->newInstance('\Saito\Posting\Posting', ['rawData' => $oldEntry]); |
|
| 375 | - |
|
| 376 | - switch ($posting->isEditingAsCurrentUserForbidden()) { |
|
| 377 | - case 'time': |
|
| 378 | - $this->Session->setFlash( |
|
| 379 | - 'Stand by your word bro\', it\'s too late. @lo', |
|
| 380 | - 'flash/error' |
|
| 381 | - ); |
|
| 382 | - $this->redirect(['action' => 'view', $id]); |
|
| 383 | - return; |
|
| 384 | - case 'user': |
|
| 385 | - $this->Session->setFlash('Not your horse, Hoss! @lo', 'flash/error'); |
|
| 386 | - $this->redirect(['action' => 'view', $id]); |
|
| 387 | - return; |
|
| 388 | - case true : |
|
| 389 | - $this->Session->setFlash( |
|
| 390 | - 'Something went terribly wrong. Alert the authorities now! @lo', |
|
| 391 | - 'flash/error' |
|
| 392 | - ); |
|
| 393 | - return; |
|
| 394 | - } |
|
| 395 | - |
|
| 396 | - // try to save edit |
|
| 397 | - if (!empty($this->request->data)) { |
|
| 398 | - $data = $this->request->data; |
|
| 399 | - $data['Entry']['id'] = $id; |
|
| 400 | - $newEntry = $this->Entry->update($data); |
|
| 401 | - if ($newEntry) { |
|
| 402 | - $this->_setNotifications(am($this->request['data'], $oldEntry)); |
|
| 403 | - $this->redirect(['action' => 'view', $id]); |
|
| 404 | - return; |
|
| 405 | - } else { |
|
| 406 | - $this->Session->setFlash(__('Something clogged the tubes. Could not save entry. Try again.')); |
|
| 407 | - } |
|
| 408 | - } |
|
| 409 | - |
|
| 410 | - // show editing form |
|
| 411 | - if ($posting->isEditingWithRoleUserForbidden()) { |
|
| 412 | - $this->Session->setFlash(__('notice_you_are_editing_as_mod'), 'flash/warning'); |
|
| 413 | - } |
|
| 414 | - |
|
| 415 | - $this->request->data = Hash::merge($oldEntry, $this->request->data); |
|
| 416 | - |
|
| 417 | - // get text of parent entry for citation |
|
| 418 | - $parentEntryId = $oldEntry['Entry']['pid']; |
|
| 419 | - if ($parentEntryId > 0) { |
|
| 420 | - $parentEntry = $this->Entry->get($parentEntryId); |
|
| 421 | - $this->set('citeText', $parentEntry['Entry']['text']); |
|
| 422 | - } |
|
| 423 | - |
|
| 424 | - // get notifications |
|
| 425 | - $notis = $this->Entry->Esevent->checkEventsForUser( |
|
| 426 | - $oldEntry['Entry']['user_id'], |
|
| 427 | - array( |
|
| 428 | - array( |
|
| 429 | - 'subject' => $oldEntry['Entry']['id'], |
|
| 430 | - 'event' => 'Model.Entry.replyToEntry', |
|
| 431 | - 'receiver' => 'EmailNotification', |
|
| 432 | - ), |
|
| 433 | - array( |
|
| 434 | - 'subject' => $oldEntry['Entry']['tid'], |
|
| 435 | - 'event' => 'Model.Entry.replyToThread', |
|
| 436 | - 'receiver' => 'EmailNotification', |
|
| 437 | - ), |
|
| 438 | - ) |
|
| 439 | - ); |
|
| 440 | - $this->set('notis', $notis); |
|
| 441 | - |
|
| 442 | - $this->set('is_answer', (int)$this->request->data['Entry']['pid'] !== 0); |
|
| 443 | - $this->set('is_inline', false); |
|
| 444 | - $this->set('form_id', $this->request->data['Entry']['pid']); |
|
| 445 | - |
|
| 446 | - // set headers |
|
| 447 | - $this->set( |
|
| 448 | - 'headerSubnavLeftTitle', |
|
| 449 | - __('back_to_posting_from_linkname', $this->request->data['User']['username']) |
|
| 450 | - ); |
|
| 451 | - $this->set('headerSubnavLeftUrl', ['action' => 'view', $id]); |
|
| 452 | - $this->set('form_title', __('edit_linkname')); |
|
| 453 | - $this->_setAddViewVars(); |
|
| 454 | - $this->render('/Entries/add'); |
|
| 455 | - } |
|
| 363 | + public function edit($id = null) { |
|
| 364 | + if (empty($id)) { |
|
| 365 | + throw new BadRequestException(); |
|
| 366 | + } |
|
| 367 | + |
|
| 368 | + $oldEntry = $this->Entry->get($id); |
|
| 369 | + if (!$oldEntry) { |
|
| 370 | + throw new NotFoundException(); |
|
| 371 | + } |
|
| 372 | + |
|
| 373 | + /** * @var \Saito\Posting\Posting */ |
|
| 374 | + $posting = $this->dic->newInstance('\Saito\Posting\Posting', ['rawData' => $oldEntry]); |
|
| 375 | + |
|
| 376 | + switch ($posting->isEditingAsCurrentUserForbidden()) { |
|
| 377 | + case 'time': |
|
| 378 | + $this->Session->setFlash( |
|
| 379 | + 'Stand by your word bro\', it\'s too late. @lo', |
|
| 380 | + 'flash/error' |
|
| 381 | + ); |
|
| 382 | + $this->redirect(['action' => 'view', $id]); |
|
| 383 | + return; |
|
| 384 | + case 'user': |
|
| 385 | + $this->Session->setFlash('Not your horse, Hoss! @lo', 'flash/error'); |
|
| 386 | + $this->redirect(['action' => 'view', $id]); |
|
| 387 | + return; |
|
| 388 | + case true : |
|
| 389 | + $this->Session->setFlash( |
|
| 390 | + 'Something went terribly wrong. Alert the authorities now! @lo', |
|
| 391 | + 'flash/error' |
|
| 392 | + ); |
|
| 393 | + return; |
|
| 394 | + } |
|
| 395 | + |
|
| 396 | + // try to save edit |
|
| 397 | + if (!empty($this->request->data)) { |
|
| 398 | + $data = $this->request->data; |
|
| 399 | + $data['Entry']['id'] = $id; |
|
| 400 | + $newEntry = $this->Entry->update($data); |
|
| 401 | + if ($newEntry) { |
|
| 402 | + $this->_setNotifications(am($this->request['data'], $oldEntry)); |
|
| 403 | + $this->redirect(['action' => 'view', $id]); |
|
| 404 | + return; |
|
| 405 | + } else { |
|
| 406 | + $this->Session->setFlash(__('Something clogged the tubes. Could not save entry. Try again.')); |
|
| 407 | + } |
|
| 408 | + } |
|
| 409 | + |
|
| 410 | + // show editing form |
|
| 411 | + if ($posting->isEditingWithRoleUserForbidden()) { |
|
| 412 | + $this->Session->setFlash(__('notice_you_are_editing_as_mod'), 'flash/warning'); |
|
| 413 | + } |
|
| 414 | + |
|
| 415 | + $this->request->data = Hash::merge($oldEntry, $this->request->data); |
|
| 416 | + |
|
| 417 | + // get text of parent entry for citation |
|
| 418 | + $parentEntryId = $oldEntry['Entry']['pid']; |
|
| 419 | + if ($parentEntryId > 0) { |
|
| 420 | + $parentEntry = $this->Entry->get($parentEntryId); |
|
| 421 | + $this->set('citeText', $parentEntry['Entry']['text']); |
|
| 422 | + } |
|
| 423 | + |
|
| 424 | + // get notifications |
|
| 425 | + $notis = $this->Entry->Esevent->checkEventsForUser( |
|
| 426 | + $oldEntry['Entry']['user_id'], |
|
| 427 | + array( |
|
| 428 | + array( |
|
| 429 | + 'subject' => $oldEntry['Entry']['id'], |
|
| 430 | + 'event' => 'Model.Entry.replyToEntry', |
|
| 431 | + 'receiver' => 'EmailNotification', |
|
| 432 | + ), |
|
| 433 | + array( |
|
| 434 | + 'subject' => $oldEntry['Entry']['tid'], |
|
| 435 | + 'event' => 'Model.Entry.replyToThread', |
|
| 436 | + 'receiver' => 'EmailNotification', |
|
| 437 | + ), |
|
| 438 | + ) |
|
| 439 | + ); |
|
| 440 | + $this->set('notis', $notis); |
|
| 441 | + |
|
| 442 | + $this->set('is_answer', (int)$this->request->data['Entry']['pid'] !== 0); |
|
| 443 | + $this->set('is_inline', false); |
|
| 444 | + $this->set('form_id', $this->request->data['Entry']['pid']); |
|
| 445 | + |
|
| 446 | + // set headers |
|
| 447 | + $this->set( |
|
| 448 | + 'headerSubnavLeftTitle', |
|
| 449 | + __('back_to_posting_from_linkname', $this->request->data['User']['username']) |
|
| 450 | + ); |
|
| 451 | + $this->set('headerSubnavLeftUrl', ['action' => 'view', $id]); |
|
| 452 | + $this->set('form_title', __('edit_linkname')); |
|
| 453 | + $this->_setAddViewVars(); |
|
| 454 | + $this->render('/Entries/add'); |
|
| 455 | + } |
|
| 456 | 456 | |
| 457 | 457 | /** |
| 458 | 458 | * @param null $id |
@@ -460,72 +460,72 @@ discard block |
||
| 460 | 460 | * @throws NotFoundException |
| 461 | 461 | * @throws MethodNotAllowedException |
| 462 | 462 | */ |
| 463 | - public function delete($id = null) { |
|
| 464 | - if (!$id) { |
|
| 465 | - throw new NotFoundException; |
|
| 466 | - } |
|
| 467 | - |
|
| 468 | - if (!$this->CurrentUser->isMod()) { |
|
| 469 | - throw new MethodNotAllowedException; |
|
| 470 | - } |
|
| 471 | - |
|
| 472 | - $this->Entry->id = $id; |
|
| 473 | - $this->Entry->contain(); |
|
| 474 | - $entry = $this->Entry->findById($id); |
|
| 475 | - |
|
| 476 | - if (!$entry) { |
|
| 477 | - throw new NotFoundException; |
|
| 478 | - } |
|
| 479 | - |
|
| 480 | - // Delete Entry |
|
| 481 | - $success = $this->Entry->deleteNode($id); |
|
| 482 | - |
|
| 483 | - // Redirect |
|
| 484 | - if ($success) { |
|
| 485 | - if ($this->Entry->isRoot($entry)) { |
|
| 486 | - $this->Session->setFlash(__('delete_tree_success'), 'flash/success'); |
|
| 487 | - $this->redirect('/'); |
|
| 488 | - } else { |
|
| 489 | - $this->Session->setFlash(__('delete_subtree_success'), 'flash/success'); |
|
| 490 | - $this->redirect('/entries/view/' . $entry['Entry']['pid']); |
|
| 491 | - } |
|
| 492 | - } else { |
|
| 493 | - $this->Session->setFlash(__('delete_tree_error'), 'flash/error'); |
|
| 494 | - $this->redirect($this->referer()); |
|
| 495 | - } |
|
| 496 | - $this->redirect('/'); |
|
| 497 | - } |
|
| 463 | + public function delete($id = null) { |
|
| 464 | + if (!$id) { |
|
| 465 | + throw new NotFoundException; |
|
| 466 | + } |
|
| 467 | + |
|
| 468 | + if (!$this->CurrentUser->isMod()) { |
|
| 469 | + throw new MethodNotAllowedException; |
|
| 470 | + } |
|
| 471 | + |
|
| 472 | + $this->Entry->id = $id; |
|
| 473 | + $this->Entry->contain(); |
|
| 474 | + $entry = $this->Entry->findById($id); |
|
| 475 | + |
|
| 476 | + if (!$entry) { |
|
| 477 | + throw new NotFoundException; |
|
| 478 | + } |
|
| 479 | + |
|
| 480 | + // Delete Entry |
|
| 481 | + $success = $this->Entry->deleteNode($id); |
|
| 482 | + |
|
| 483 | + // Redirect |
|
| 484 | + if ($success) { |
|
| 485 | + if ($this->Entry->isRoot($entry)) { |
|
| 486 | + $this->Session->setFlash(__('delete_tree_success'), 'flash/success'); |
|
| 487 | + $this->redirect('/'); |
|
| 488 | + } else { |
|
| 489 | + $this->Session->setFlash(__('delete_subtree_success'), 'flash/success'); |
|
| 490 | + $this->redirect('/entries/view/' . $entry['Entry']['pid']); |
|
| 491 | + } |
|
| 492 | + } else { |
|
| 493 | + $this->Session->setFlash(__('delete_tree_error'), 'flash/error'); |
|
| 494 | + $this->redirect($this->referer()); |
|
| 495 | + } |
|
| 496 | + $this->redirect('/'); |
|
| 497 | + } |
|
| 498 | 498 | |
| 499 | 499 | /** |
| 500 | 500 | * Empty function for benchmarking |
| 501 | 501 | */ |
| 502 | - public function e() { |
|
| 503 | - Stopwatch::start('Entries->e()'); |
|
| 504 | - Stopwatch::stop('Entries->e()'); |
|
| 505 | - } |
|
| 506 | - |
|
| 507 | - /** |
|
| 508 | - * Marks sub-entry $id as solution to its current root-entry |
|
| 509 | - * |
|
| 510 | - * @param $id |
|
| 511 | - * @throws BadRequestException |
|
| 512 | - * @throws ForbiddenException |
|
| 513 | - * @throws Exception |
|
| 514 | - */ |
|
| 515 | - public function solve($id) { |
|
| 516 | - if (!$this->CurrentUser->isLoggedIn()) { |
|
| 517 | - throw new ForbiddenException; |
|
| 518 | - } |
|
| 519 | - $this->autoRender = false; |
|
| 520 | - try { |
|
| 521 | - $success = $this->Entry->toggleSolve($id); |
|
| 522 | - if (!$success) { |
|
| 523 | - throw new Exception; |
|
| 524 | - } |
|
| 525 | - } catch (Exception $e) { |
|
| 526 | - throw new BadRequestException; |
|
| 527 | - } |
|
| 528 | - } |
|
| 502 | + public function e() { |
|
| 503 | + Stopwatch::start('Entries->e()'); |
|
| 504 | + Stopwatch::stop('Entries->e()'); |
|
| 505 | + } |
|
| 506 | + |
|
| 507 | + /** |
|
| 508 | + * Marks sub-entry $id as solution to its current root-entry |
|
| 509 | + * |
|
| 510 | + * @param $id |
|
| 511 | + * @throws BadRequestException |
|
| 512 | + * @throws ForbiddenException |
|
| 513 | + * @throws Exception |
|
| 514 | + */ |
|
| 515 | + public function solve($id) { |
|
| 516 | + if (!$this->CurrentUser->isLoggedIn()) { |
|
| 517 | + throw new ForbiddenException; |
|
| 518 | + } |
|
| 519 | + $this->autoRender = false; |
|
| 520 | + try { |
|
| 521 | + $success = $this->Entry->toggleSolve($id); |
|
| 522 | + if (!$success) { |
|
| 523 | + throw new Exception; |
|
| 524 | + } |
|
| 525 | + } catch (Exception $e) { |
|
| 526 | + throw new BadRequestException; |
|
| 527 | + } |
|
| 528 | + } |
|
| 529 | 529 | |
| 530 | 530 | /** |
| 531 | 531 | * @return string |
@@ -533,80 +533,80 @@ discard block |
||
| 533 | 533 | * @throws BadRequestException |
| 534 | 534 | * @throws ForbiddenException |
| 535 | 535 | */ |
| 536 | - public function preview() { |
|
| 537 | - if ($this->CurrentUser->isLoggedIn() === false) { |
|
| 538 | - throw new ForbiddenException(); |
|
| 539 | - } |
|
| 540 | - if ($this->request->is('ajax') === false) { |
|
| 541 | - throw new BadRequestException(); |
|
| 542 | - } |
|
| 543 | - if ($this->request->is('get')) { |
|
| 544 | - throw new MethodNotAllowedException(); |
|
| 545 | - } |
|
| 546 | - |
|
| 547 | - $data = $this->request->data; |
|
| 548 | - $data = $data['Entry']; |
|
| 549 | - $newEntry = array( |
|
| 550 | - 'Entry' => array( |
|
| 551 | - 'id' => 'preview', |
|
| 552 | - 'pid' => $data['pid'], |
|
| 553 | - 'subject' => $data['subject'], |
|
| 554 | - 'text' => $data['text'], |
|
| 555 | - 'category_id' => $data['category_id'], |
|
| 556 | - 'edited_by' => null, |
|
| 557 | - 'fixed' => false, |
|
| 558 | - 'solves' => 0, |
|
| 559 | - 'views' => 0, |
|
| 560 | - 'ip' => '', |
|
| 561 | - 'time' => date("Y-m-d H:i:s") |
|
| 562 | - ) |
|
| 563 | - ); |
|
| 564 | - $this->Entry->prepare($newEntry); |
|
| 565 | - $this->Entry->set($newEntry); |
|
| 566 | - |
|
| 567 | - $this->Entry->validates(['fieldList' => ['subject', 'text', 'category_id']]); |
|
| 568 | - $errors = $this->Entry->validationErrors; |
|
| 569 | - |
|
| 570 | - if (count($errors) === 0) : |
|
| 571 | - // no validation errors |
|
| 572 | - $newEntry['User'] = $this->CurrentUser->getSettings(); |
|
| 573 | - $newEntry = array_merge( |
|
| 574 | - $newEntry, |
|
| 575 | - $this->Entry->Category->find( |
|
| 576 | - 'first', |
|
| 577 | - array( |
|
| 578 | - 'conditions' => array( |
|
| 579 | - 'id' => $newEntry['Entry']['category_id'] |
|
| 580 | - ), |
|
| 581 | - 'contain' => false, |
|
| 582 | - ) |
|
| 583 | - ) |
|
| 584 | - ); |
|
| 585 | - $entry = $this->dic->newInstance('\Saito\Posting\Posting', ['rawData' => $newEntry]); |
|
| 586 | - $this->set('entry', $entry); |
|
| 587 | - else : |
|
| 588 | - // validation errors |
|
| 589 | - foreach ($errors as $field => $error) { |
|
| 590 | - $message = __d('nondynamic', $field) . ": " . __d( 'nondynamic', $error[0]); |
|
| 591 | - $this->JsData->addAppJsMessage( |
|
| 592 | - $message, |
|
| 593 | - [ |
|
| 594 | - 'type' => 'error', |
|
| 595 | - 'channel' => 'form', |
|
| 596 | - 'element' => '#Entry' . array_reduce( |
|
| 597 | - explode('_', $field), |
|
| 598 | - function ($carry, $item) { |
|
| 599 | - return $carry . ucfirst($item); |
|
| 600 | - }, |
|
| 601 | - '' |
|
| 602 | - ) |
|
| 603 | - ] |
|
| 604 | - ); |
|
| 605 | - } |
|
| 606 | - $this->autoRender = false; |
|
| 607 | - return json_encode($this->JsData->getAppJsMessages()); |
|
| 608 | - endif; |
|
| 609 | - } |
|
| 536 | + public function preview() { |
|
| 537 | + if ($this->CurrentUser->isLoggedIn() === false) { |
|
| 538 | + throw new ForbiddenException(); |
|
| 539 | + } |
|
| 540 | + if ($this->request->is('ajax') === false) { |
|
| 541 | + throw new BadRequestException(); |
|
| 542 | + } |
|
| 543 | + if ($this->request->is('get')) { |
|
| 544 | + throw new MethodNotAllowedException(); |
|
| 545 | + } |
|
| 546 | + |
|
| 547 | + $data = $this->request->data; |
|
| 548 | + $data = $data['Entry']; |
|
| 549 | + $newEntry = array( |
|
| 550 | + 'Entry' => array( |
|
| 551 | + 'id' => 'preview', |
|
| 552 | + 'pid' => $data['pid'], |
|
| 553 | + 'subject' => $data['subject'], |
|
| 554 | + 'text' => $data['text'], |
|
| 555 | + 'category_id' => $data['category_id'], |
|
| 556 | + 'edited_by' => null, |
|
| 557 | + 'fixed' => false, |
|
| 558 | + 'solves' => 0, |
|
| 559 | + 'views' => 0, |
|
| 560 | + 'ip' => '', |
|
| 561 | + 'time' => date("Y-m-d H:i:s") |
|
| 562 | + ) |
|
| 563 | + ); |
|
| 564 | + $this->Entry->prepare($newEntry); |
|
| 565 | + $this->Entry->set($newEntry); |
|
| 566 | + |
|
| 567 | + $this->Entry->validates(['fieldList' => ['subject', 'text', 'category_id']]); |
|
| 568 | + $errors = $this->Entry->validationErrors; |
|
| 569 | + |
|
| 570 | + if (count($errors) === 0) : |
|
| 571 | + // no validation errors |
|
| 572 | + $newEntry['User'] = $this->CurrentUser->getSettings(); |
|
| 573 | + $newEntry = array_merge( |
|
| 574 | + $newEntry, |
|
| 575 | + $this->Entry->Category->find( |
|
| 576 | + 'first', |
|
| 577 | + array( |
|
| 578 | + 'conditions' => array( |
|
| 579 | + 'id' => $newEntry['Entry']['category_id'] |
|
| 580 | + ), |
|
| 581 | + 'contain' => false, |
|
| 582 | + ) |
|
| 583 | + ) |
|
| 584 | + ); |
|
| 585 | + $entry = $this->dic->newInstance('\Saito\Posting\Posting', ['rawData' => $newEntry]); |
|
| 586 | + $this->set('entry', $entry); |
|
| 587 | + else : |
|
| 588 | + // validation errors |
|
| 589 | + foreach ($errors as $field => $error) { |
|
| 590 | + $message = __d('nondynamic', $field) . ": " . __d( 'nondynamic', $error[0]); |
|
| 591 | + $this->JsData->addAppJsMessage( |
|
| 592 | + $message, |
|
| 593 | + [ |
|
| 594 | + 'type' => 'error', |
|
| 595 | + 'channel' => 'form', |
|
| 596 | + 'element' => '#Entry' . array_reduce( |
|
| 597 | + explode('_', $field), |
|
| 598 | + function ($carry, $item) { |
|
| 599 | + return $carry . ucfirst($item); |
|
| 600 | + }, |
|
| 601 | + '' |
|
| 602 | + ) |
|
| 603 | + ] |
|
| 604 | + ); |
|
| 605 | + } |
|
| 606 | + $this->autoRender = false; |
|
| 607 | + return json_encode($this->JsData->getAppJsMessages()); |
|
| 608 | + endif; |
|
| 609 | + } |
|
| 610 | 610 | |
| 611 | 611 | /** |
| 612 | 612 | * @param null $id |
@@ -614,37 +614,37 @@ discard block |
||
| 614 | 614 | * @throws NotFoundException |
| 615 | 615 | * @throws MethodNotAllowedException |
| 616 | 616 | */ |
| 617 | - public function merge($id = null) { |
|
| 618 | - if (!$id) { |
|
| 619 | - throw new NotFoundException(); |
|
| 620 | - } |
|
| 621 | - |
|
| 622 | - if (!$this->CurrentUser->isMod() && !$this->CurrentUser->isAdmin()) { |
|
| 623 | - throw new MethodNotAllowedException; |
|
| 624 | - } |
|
| 625 | - |
|
| 626 | - $this->Entry->contain(); |
|
| 627 | - $data = $this->Entry->findById($id); |
|
| 628 | - |
|
| 629 | - if (!$data || (int)$data['Entry']['pid'] !== 0) { |
|
| 630 | - throw new NotFoundException(); |
|
| 631 | - } |
|
| 632 | - |
|
| 633 | - // perform move operation |
|
| 634 | - if (isset($this->request->data['Entry']['targetId'])) { |
|
| 635 | - $targetId = $this->request->data['Entry']['targetId']; |
|
| 636 | - $this->Entry->id = $id; |
|
| 637 | - if ($this->Entry->threadMerge($targetId)) { |
|
| 638 | - $this->redirect('/entries/view/' . $id); |
|
| 639 | - return; |
|
| 640 | - } else { |
|
| 641 | - $this->Session->setFlash(__("Error"), 'flash/error'); |
|
| 642 | - } |
|
| 643 | - } |
|
| 644 | - |
|
| 645 | - $this->layout = 'admin'; |
|
| 646 | - $this->request->data = $data; |
|
| 647 | - } |
|
| 617 | + public function merge($id = null) { |
|
| 618 | + if (!$id) { |
|
| 619 | + throw new NotFoundException(); |
|
| 620 | + } |
|
| 621 | + |
|
| 622 | + if (!$this->CurrentUser->isMod() && !$this->CurrentUser->isAdmin()) { |
|
| 623 | + throw new MethodNotAllowedException; |
|
| 624 | + } |
|
| 625 | + |
|
| 626 | + $this->Entry->contain(); |
|
| 627 | + $data = $this->Entry->findById($id); |
|
| 628 | + |
|
| 629 | + if (!$data || (int)$data['Entry']['pid'] !== 0) { |
|
| 630 | + throw new NotFoundException(); |
|
| 631 | + } |
|
| 632 | + |
|
| 633 | + // perform move operation |
|
| 634 | + if (isset($this->request->data['Entry']['targetId'])) { |
|
| 635 | + $targetId = $this->request->data['Entry']['targetId']; |
|
| 636 | + $this->Entry->id = $id; |
|
| 637 | + if ($this->Entry->threadMerge($targetId)) { |
|
| 638 | + $this->redirect('/entries/view/' . $id); |
|
| 639 | + return; |
|
| 640 | + } else { |
|
| 641 | + $this->Session->setFlash(__("Error"), 'flash/error'); |
|
| 642 | + } |
|
| 643 | + } |
|
| 644 | + |
|
| 645 | + $this->layout = 'admin'; |
|
| 646 | + $this->request->data = $data; |
|
| 647 | + } |
|
| 648 | 648 | |
| 649 | 649 | /** |
| 650 | 650 | * @param null $id |
@@ -652,316 +652,316 @@ discard block |
||
| 652 | 652 | * |
| 653 | 653 | * @return translated |
| 654 | 654 | */ |
| 655 | - public function ajax_toggle($id = null, $toggle = null) { |
|
| 656 | - $this->autoLayout = false; |
|
| 657 | - $this->autoRender = false; |
|
| 658 | - |
|
| 659 | - if (!$id || !$toggle || !$this->request->is('ajax')) { |
|
| 660 | - return; |
|
| 661 | - } |
|
| 662 | - |
|
| 663 | - // check if the requested toggle is allowed to be changed via this function |
|
| 664 | - $allowedToggles = array( |
|
| 665 | - 'fixed', |
|
| 666 | - 'locked', |
|
| 667 | - ); |
|
| 668 | - if (!in_array($toggle, $allowedToggles)) { |
|
| 669 | - $this->request->data = false; |
|
| 670 | - |
|
| 671 | - // check is user is allowed to perform operation |
|
| 672 | - // luckily we only mod options in the allowed toggles |
|
| 673 | - } elseif ($this->CurrentUser->isMod() === false) { |
|
| 674 | - $this->request->data = false; |
|
| 675 | - } else { |
|
| 676 | - //* let's toggle |
|
| 677 | - $this->Entry->id = $id; |
|
| 678 | - $this->request->data = $this->Entry->toggle($toggle); |
|
| 679 | - return ($this->request->data == 0) ? __d('nondynamic', $toggle . '_set_entry_link') : __d('nondynamic', $toggle . '_unset_entry_link'); |
|
| 680 | - } |
|
| 681 | - |
|
| 682 | - $this->set('json_data', (string)$this->request->data); |
|
| 683 | - $this->render('/Elements/json/json_data'); |
|
| 684 | - } |
|
| 685 | - |
|
| 686 | - public function beforeFilter() { |
|
| 687 | - parent::beforeFilter(); |
|
| 688 | - Stopwatch::start('Entries->beforeFilter()'); |
|
| 689 | - |
|
| 690 | - $this->_automaticalyMarkAsRead(); |
|
| 691 | - |
|
| 692 | - $this->Security->unlockedActions = ['preview', 'solve', 'view']; |
|
| 693 | - $this->Auth->allow('feed', 'index', 'view', 'mix', 'update'); |
|
| 694 | - |
|
| 695 | - switch ($this->request->action) { |
|
| 696 | - case 'index': |
|
| 697 | - $this->_setAutoRefreshTime(); |
|
| 698 | - break; |
|
| 699 | - case 'add': |
|
| 700 | - case 'edit': |
|
| 701 | - if ($this->Components->enabled('Security')) { |
|
| 702 | - $this->Components->disable('Security'); |
|
| 703 | - } |
|
| 704 | - } |
|
| 705 | - |
|
| 706 | - Stopwatch::stop('Entries->beforeFilter()'); |
|
| 707 | - } |
|
| 708 | - |
|
| 709 | - public function afterFilter() { |
|
| 710 | - if (isset($this->_marMixThread)) { |
|
| 711 | - $entries = $this->Entry->find('all', |
|
| 712 | - [ |
|
| 713 | - 'contain' => false, |
|
| 714 | - 'conditions' => ['Entry.tid' => $this->_marMixThread], |
|
| 715 | - 'fields' => ['Entry.id', 'Entry.time'] |
|
| 716 | - ]); |
|
| 717 | - $this->CurrentUser->ReadEntries->set($entries); |
|
| 718 | - } |
|
| 719 | - } |
|
| 720 | - |
|
| 721 | - protected function _automaticalyMarkAsRead() { |
|
| 722 | - if (!$this->CurrentUser->isLoggedIn() || |
|
| 723 | - !$this->CurrentUser['user_automaticaly_mark_as_read'] |
|
| 724 | - ) { |
|
| 725 | - return; |
|
| 726 | - } |
|
| 727 | - |
|
| 728 | - if ($this->request->action === "index" && |
|
| 729 | - !$this->Session->read('User.last_refresh_tmp') |
|
| 730 | - ) { |
|
| 731 | - // initiate sessions last_refresh_tmp for new sessions |
|
| 732 | - $this->Session->write('User.last_refresh_tmp', time()); |
|
| 733 | - } |
|
| 734 | - |
|
| 735 | - /* // old |
|
| 655 | + public function ajax_toggle($id = null, $toggle = null) { |
|
| 656 | + $this->autoLayout = false; |
|
| 657 | + $this->autoRender = false; |
|
| 658 | + |
|
| 659 | + if (!$id || !$toggle || !$this->request->is('ajax')) { |
|
| 660 | + return; |
|
| 661 | + } |
|
| 662 | + |
|
| 663 | + // check if the requested toggle is allowed to be changed via this function |
|
| 664 | + $allowedToggles = array( |
|
| 665 | + 'fixed', |
|
| 666 | + 'locked', |
|
| 667 | + ); |
|
| 668 | + if (!in_array($toggle, $allowedToggles)) { |
|
| 669 | + $this->request->data = false; |
|
| 670 | + |
|
| 671 | + // check is user is allowed to perform operation |
|
| 672 | + // luckily we only mod options in the allowed toggles |
|
| 673 | + } elseif ($this->CurrentUser->isMod() === false) { |
|
| 674 | + $this->request->data = false; |
|
| 675 | + } else { |
|
| 676 | + //* let's toggle |
|
| 677 | + $this->Entry->id = $id; |
|
| 678 | + $this->request->data = $this->Entry->toggle($toggle); |
|
| 679 | + return ($this->request->data == 0) ? __d('nondynamic', $toggle . '_set_entry_link') : __d('nondynamic', $toggle . '_unset_entry_link'); |
|
| 680 | + } |
|
| 681 | + |
|
| 682 | + $this->set('json_data', (string)$this->request->data); |
|
| 683 | + $this->render('/Elements/json/json_data'); |
|
| 684 | + } |
|
| 685 | + |
|
| 686 | + public function beforeFilter() { |
|
| 687 | + parent::beforeFilter(); |
|
| 688 | + Stopwatch::start('Entries->beforeFilter()'); |
|
| 689 | + |
|
| 690 | + $this->_automaticalyMarkAsRead(); |
|
| 691 | + |
|
| 692 | + $this->Security->unlockedActions = ['preview', 'solve', 'view']; |
|
| 693 | + $this->Auth->allow('feed', 'index', 'view', 'mix', 'update'); |
|
| 694 | + |
|
| 695 | + switch ($this->request->action) { |
|
| 696 | + case 'index': |
|
| 697 | + $this->_setAutoRefreshTime(); |
|
| 698 | + break; |
|
| 699 | + case 'add': |
|
| 700 | + case 'edit': |
|
| 701 | + if ($this->Components->enabled('Security')) { |
|
| 702 | + $this->Components->disable('Security'); |
|
| 703 | + } |
|
| 704 | + } |
|
| 705 | + |
|
| 706 | + Stopwatch::stop('Entries->beforeFilter()'); |
|
| 707 | + } |
|
| 708 | + |
|
| 709 | + public function afterFilter() { |
|
| 710 | + if (isset($this->_marMixThread)) { |
|
| 711 | + $entries = $this->Entry->find('all', |
|
| 712 | + [ |
|
| 713 | + 'contain' => false, |
|
| 714 | + 'conditions' => ['Entry.tid' => $this->_marMixThread], |
|
| 715 | + 'fields' => ['Entry.id', 'Entry.time'] |
|
| 716 | + ]); |
|
| 717 | + $this->CurrentUser->ReadEntries->set($entries); |
|
| 718 | + } |
|
| 719 | + } |
|
| 720 | + |
|
| 721 | + protected function _automaticalyMarkAsRead() { |
|
| 722 | + if (!$this->CurrentUser->isLoggedIn() || |
|
| 723 | + !$this->CurrentUser['user_automaticaly_mark_as_read'] |
|
| 724 | + ) { |
|
| 725 | + return; |
|
| 726 | + } |
|
| 727 | + |
|
| 728 | + if ($this->request->action === "index" && |
|
| 729 | + !$this->Session->read('User.last_refresh_tmp') |
|
| 730 | + ) { |
|
| 731 | + // initiate sessions last_refresh_tmp for new sessions |
|
| 732 | + $this->Session->write('User.last_refresh_tmp', time()); |
|
| 733 | + } |
|
| 734 | + |
|
| 735 | + /* // old |
|
| 736 | 736 | $isMarkAsReadRequest = $this->localReferer('controller') === 'entries' && |
| 737 | 737 | $this->localReferer('action') === 'index' && |
| 738 | 738 | $this->request->action === "index"; |
| 739 | 739 | */ |
| 740 | 740 | |
| 741 | - $isMarkAsReadRequest = isset($this->request->query['mar']) && |
|
| 742 | - $this->request->query['mar'] === ''; |
|
| 743 | - |
|
| 744 | - if ($isMarkAsReadRequest && |
|
| 745 | - $this->request->isPreview() === false |
|
| 746 | - ) { |
|
| 747 | - // a second session A shall not accidentally mark something as read that isn't read on session B |
|
| 748 | - if ($this->Session->read('User.last_refresh_tmp') > $this->CurrentUser['last_refresh_unix']) { |
|
| 749 | - $this->CurrentUser->LastRefresh->set(); |
|
| 750 | - } |
|
| 751 | - $this->Session->write('User.last_refresh_tmp', time()); |
|
| 752 | - $this->redirect('/'); |
|
| 753 | - return; |
|
| 754 | - } elseif ($this->request->action === "index") { |
|
| 755 | - $this->CurrentUser->LastRefresh->setMarker(); |
|
| 756 | - } |
|
| 757 | - } |
|
| 758 | - |
|
| 759 | - protected function _prepareSlidetabData() { |
|
| 760 | - if ($this->CurrentUser->isLoggedIn()) { |
|
| 761 | - // get current user's recent entries for slidetab |
|
| 762 | - $this->set( |
|
| 763 | - 'recentPosts', |
|
| 764 | - $this->Entry->getRecentEntries( |
|
| 765 | - $this->CurrentUser, |
|
| 766 | - [ |
|
| 767 | - 'user_id' => $this->CurrentUser->getId(), |
|
| 768 | - 'limit' => 5 |
|
| 769 | - ] |
|
| 770 | - ) |
|
| 771 | - ); |
|
| 772 | - // get last 10 recent entries for slidetab |
|
| 773 | - $this->set( |
|
| 774 | - 'recentEntries', |
|
| 775 | - $this->Entry->getRecentEntries($this->CurrentUser) |
|
| 776 | - ); |
|
| 777 | - // get shouts |
|
| 778 | - if (in_array('slidetab_shoutbox', $this->viewVars['slidetabs'])) { |
|
| 779 | - $this->Shouts->setShoutsForView(); |
|
| 780 | - } |
|
| 781 | - } |
|
| 782 | - } |
|
| 783 | - |
|
| 784 | - protected function _incrementViews($entry, $type = null) { |
|
| 785 | - if ($this->CurrentUser->isBot()) { |
|
| 786 | - return; |
|
| 787 | - } |
|
| 788 | - $cUserId = $this->CurrentUser->getId(); |
|
| 789 | - |
|
| 790 | - if ($type === 'thread') { |
|
| 791 | - $this->Entry->threadIncrementViews($entry['Entry']['tid'], $cUserId); |
|
| 792 | - } elseif ($entry['Entry']['user_id'] != $cUserId) { |
|
| 793 | - $this->Entry->incrementViews($entry['Entry']['id']); |
|
| 794 | - } |
|
| 795 | - } |
|
| 796 | - |
|
| 797 | - protected function _setNotifications($newEntry) { |
|
| 798 | - if (isset($newEntry['Event'])) { |
|
| 799 | - $notis = [ |
|
| 800 | - [ |
|
| 801 | - 'subject' => $newEntry['Entry']['id'], |
|
| 802 | - 'event' => 'Model.Entry.replyToEntry', |
|
| 803 | - 'receiver' => 'EmailNotification', |
|
| 804 | - 'set' => $newEntry['Event'][1]['event_type_id'], |
|
| 805 | - ], |
|
| 806 | - [ |
|
| 807 | - 'subject' => $newEntry['Entry']['tid'], |
|
| 808 | - 'event' => 'Model.Entry.replyToThread', |
|
| 809 | - 'receiver' => 'EmailNotification', |
|
| 810 | - 'set' => $newEntry['Event'][2]['event_type_id'], |
|
| 811 | - ] |
|
| 812 | - ]; |
|
| 813 | - $this->Entry->Esevent->notifyUserOnEvents( |
|
| 814 | - $newEntry['Entry']['user_id'], |
|
| 815 | - $notis |
|
| 816 | - ); |
|
| 817 | - } |
|
| 818 | - } |
|
| 819 | - |
|
| 820 | - protected function _setAutoRefreshTime() { |
|
| 821 | - if (!$this->CurrentUser->isLoggedIn()) { |
|
| 822 | - return; |
|
| 823 | - } |
|
| 824 | - if ($this->CurrentUser['user_forum_refresh_time'] > 0) { |
|
| 825 | - $this->set( |
|
| 826 | - 'autoPageReload', |
|
| 827 | - $this->CurrentUser['user_forum_refresh_time'] * 60 |
|
| 828 | - ); |
|
| 829 | - } |
|
| 830 | - } |
|
| 831 | - |
|
| 832 | - /** |
|
| 833 | - * Gets thread ids for paginated entries/index. |
|
| 834 | - * |
|
| 835 | - * @param CurrentUserComponent $User |
|
| 836 | - * @param array $order sort order |
|
| 837 | - * @return array thread ids |
|
| 838 | - */ |
|
| 839 | - protected function _getInitialThreads(CurrentUserComponent $User, $order) { |
|
| 840 | - Stopwatch::start('Entries->_getInitialThreads() Paginate'); |
|
| 841 | - |
|
| 842 | - $categories = $this->_setupCategoryChooser($User); |
|
| 843 | - |
|
| 844 | - //! Check DB performance after changing conditions/sorting! |
|
| 845 | - $this->paginate = [ |
|
| 846 | - 'conditions' => [ |
|
| 847 | - 'pid' => 0, |
|
| 848 | - 'Entry.category_id' => $categories |
|
| 849 | - ], |
|
| 850 | - 'contain' => false, |
|
| 851 | - 'fields' => 'id, pid, tid, time, last_answer, fixed', |
|
| 852 | - 'limit' => Configure::read('Saito.Settings.topics_per_page'), |
|
| 853 | - 'order' => $order, |
|
| 854 | - 'getInitialThreads' => 1, |
|
| 855 | - ]; |
|
| 856 | - |
|
| 857 | - // disallows overwriting pagination params from request |
|
| 858 | - unset( |
|
| 859 | - $this->request->params['named']['direction'], |
|
| 860 | - $this->request->params['named']['sort'] |
|
| 861 | - ); |
|
| 862 | - $initialThreads = $this->paginate(null, null, array_keys($order)); |
|
| 863 | - |
|
| 864 | - $initialThreadsNew = []; |
|
| 865 | - foreach ($initialThreads as $k => $v) { |
|
| 866 | - $initialThreadsNew[$k] = $v['Entry']['id']; |
|
| 867 | - } |
|
| 868 | - Stopwatch::stop('Entries->_getInitialThreads() Paginate'); |
|
| 869 | - |
|
| 870 | - return $initialThreadsNew; |
|
| 871 | - } |
|
| 872 | - |
|
| 873 | - protected function _setupCategoryChooser(\Saito\User\ForumsUserInterface $User) { |
|
| 874 | - $categories = $User->Categories->getAllowed(); |
|
| 875 | - |
|
| 876 | - $isUsed = $User->isLoggedIn() && |
|
| 877 | - ( |
|
| 878 | - Configure::read('Saito.Settings.category_chooser_global') || |
|
| 879 | - ( |
|
| 880 | - Configure::read( |
|
| 881 | - 'Saito.Settings.category_chooser_user_override' |
|
| 882 | - ) && $User['user_category_override'] |
|
| 883 | - ) |
|
| 884 | - ); |
|
| 885 | - |
|
| 886 | - if ($isUsed) { |
|
| 887 | - // @todo find right place for this; also: User::getCategories(); |
|
| 888 | - $UserCategories = new Categories($User->getSettings(), $categories); |
|
| 889 | - list($categories, $type, $custom) = $UserCategories->get(); |
|
| 890 | - |
|
| 891 | - $this->set('categoryChooserChecked', $custom); |
|
| 892 | - |
|
| 893 | - switch ($type) { |
|
| 894 | - case 'single': |
|
| 895 | - $title = $User['user_category_active']; |
|
| 896 | - break; |
|
| 897 | - case 'custom': |
|
| 898 | - $title = __('Custom'); |
|
| 899 | - break; |
|
| 900 | - default: |
|
| 901 | - $title = __('All Categories'); |
|
| 902 | - } |
|
| 903 | - $this->set('categoryChooserTitleId', $title); |
|
| 904 | - $this->set('categoryChooser', $User->Categories->getAllowed('list')); |
|
| 905 | - } |
|
| 906 | - return $categories; |
|
| 907 | - } |
|
| 908 | - |
|
| 909 | - protected function _setAddViewVars() { |
|
| 910 | - //= categories for dropdown |
|
| 911 | - $categories = $this->CurrentUser->Categories->getAllowed('list'); |
|
| 912 | - $this->set('categories', $categories); |
|
| 913 | - } |
|
| 741 | + $isMarkAsReadRequest = isset($this->request->query['mar']) && |
|
| 742 | + $this->request->query['mar'] === ''; |
|
| 743 | + |
|
| 744 | + if ($isMarkAsReadRequest && |
|
| 745 | + $this->request->isPreview() === false |
|
| 746 | + ) { |
|
| 747 | + // a second session A shall not accidentally mark something as read that isn't read on session B |
|
| 748 | + if ($this->Session->read('User.last_refresh_tmp') > $this->CurrentUser['last_refresh_unix']) { |
|
| 749 | + $this->CurrentUser->LastRefresh->set(); |
|
| 750 | + } |
|
| 751 | + $this->Session->write('User.last_refresh_tmp', time()); |
|
| 752 | + $this->redirect('/'); |
|
| 753 | + return; |
|
| 754 | + } elseif ($this->request->action === "index") { |
|
| 755 | + $this->CurrentUser->LastRefresh->setMarker(); |
|
| 756 | + } |
|
| 757 | + } |
|
| 758 | + |
|
| 759 | + protected function _prepareSlidetabData() { |
|
| 760 | + if ($this->CurrentUser->isLoggedIn()) { |
|
| 761 | + // get current user's recent entries for slidetab |
|
| 762 | + $this->set( |
|
| 763 | + 'recentPosts', |
|
| 764 | + $this->Entry->getRecentEntries( |
|
| 765 | + $this->CurrentUser, |
|
| 766 | + [ |
|
| 767 | + 'user_id' => $this->CurrentUser->getId(), |
|
| 768 | + 'limit' => 5 |
|
| 769 | + ] |
|
| 770 | + ) |
|
| 771 | + ); |
|
| 772 | + // get last 10 recent entries for slidetab |
|
| 773 | + $this->set( |
|
| 774 | + 'recentEntries', |
|
| 775 | + $this->Entry->getRecentEntries($this->CurrentUser) |
|
| 776 | + ); |
|
| 777 | + // get shouts |
|
| 778 | + if (in_array('slidetab_shoutbox', $this->viewVars['slidetabs'])) { |
|
| 779 | + $this->Shouts->setShoutsForView(); |
|
| 780 | + } |
|
| 781 | + } |
|
| 782 | + } |
|
| 783 | + |
|
| 784 | + protected function _incrementViews($entry, $type = null) { |
|
| 785 | + if ($this->CurrentUser->isBot()) { |
|
| 786 | + return; |
|
| 787 | + } |
|
| 788 | + $cUserId = $this->CurrentUser->getId(); |
|
| 789 | + |
|
| 790 | + if ($type === 'thread') { |
|
| 791 | + $this->Entry->threadIncrementViews($entry['Entry']['tid'], $cUserId); |
|
| 792 | + } elseif ($entry['Entry']['user_id'] != $cUserId) { |
|
| 793 | + $this->Entry->incrementViews($entry['Entry']['id']); |
|
| 794 | + } |
|
| 795 | + } |
|
| 796 | + |
|
| 797 | + protected function _setNotifications($newEntry) { |
|
| 798 | + if (isset($newEntry['Event'])) { |
|
| 799 | + $notis = [ |
|
| 800 | + [ |
|
| 801 | + 'subject' => $newEntry['Entry']['id'], |
|
| 802 | + 'event' => 'Model.Entry.replyToEntry', |
|
| 803 | + 'receiver' => 'EmailNotification', |
|
| 804 | + 'set' => $newEntry['Event'][1]['event_type_id'], |
|
| 805 | + ], |
|
| 806 | + [ |
|
| 807 | + 'subject' => $newEntry['Entry']['tid'], |
|
| 808 | + 'event' => 'Model.Entry.replyToThread', |
|
| 809 | + 'receiver' => 'EmailNotification', |
|
| 810 | + 'set' => $newEntry['Event'][2]['event_type_id'], |
|
| 811 | + ] |
|
| 812 | + ]; |
|
| 813 | + $this->Entry->Esevent->notifyUserOnEvents( |
|
| 814 | + $newEntry['Entry']['user_id'], |
|
| 815 | + $notis |
|
| 816 | + ); |
|
| 817 | + } |
|
| 818 | + } |
|
| 819 | + |
|
| 820 | + protected function _setAutoRefreshTime() { |
|
| 821 | + if (!$this->CurrentUser->isLoggedIn()) { |
|
| 822 | + return; |
|
| 823 | + } |
|
| 824 | + if ($this->CurrentUser['user_forum_refresh_time'] > 0) { |
|
| 825 | + $this->set( |
|
| 826 | + 'autoPageReload', |
|
| 827 | + $this->CurrentUser['user_forum_refresh_time'] * 60 |
|
| 828 | + ); |
|
| 829 | + } |
|
| 830 | + } |
|
| 831 | + |
|
| 832 | + /** |
|
| 833 | + * Gets thread ids for paginated entries/index. |
|
| 834 | + * |
|
| 835 | + * @param CurrentUserComponent $User |
|
| 836 | + * @param array $order sort order |
|
| 837 | + * @return array thread ids |
|
| 838 | + */ |
|
| 839 | + protected function _getInitialThreads(CurrentUserComponent $User, $order) { |
|
| 840 | + Stopwatch::start('Entries->_getInitialThreads() Paginate'); |
|
| 841 | + |
|
| 842 | + $categories = $this->_setupCategoryChooser($User); |
|
| 843 | + |
|
| 844 | + //! Check DB performance after changing conditions/sorting! |
|
| 845 | + $this->paginate = [ |
|
| 846 | + 'conditions' => [ |
|
| 847 | + 'pid' => 0, |
|
| 848 | + 'Entry.category_id' => $categories |
|
| 849 | + ], |
|
| 850 | + 'contain' => false, |
|
| 851 | + 'fields' => 'id, pid, tid, time, last_answer, fixed', |
|
| 852 | + 'limit' => Configure::read('Saito.Settings.topics_per_page'), |
|
| 853 | + 'order' => $order, |
|
| 854 | + 'getInitialThreads' => 1, |
|
| 855 | + ]; |
|
| 856 | + |
|
| 857 | + // disallows overwriting pagination params from request |
|
| 858 | + unset( |
|
| 859 | + $this->request->params['named']['direction'], |
|
| 860 | + $this->request->params['named']['sort'] |
|
| 861 | + ); |
|
| 862 | + $initialThreads = $this->paginate(null, null, array_keys($order)); |
|
| 863 | + |
|
| 864 | + $initialThreadsNew = []; |
|
| 865 | + foreach ($initialThreads as $k => $v) { |
|
| 866 | + $initialThreadsNew[$k] = $v['Entry']['id']; |
|
| 867 | + } |
|
| 868 | + Stopwatch::stop('Entries->_getInitialThreads() Paginate'); |
|
| 869 | + |
|
| 870 | + return $initialThreadsNew; |
|
| 871 | + } |
|
| 872 | + |
|
| 873 | + protected function _setupCategoryChooser(\Saito\User\ForumsUserInterface $User) { |
|
| 874 | + $categories = $User->Categories->getAllowed(); |
|
| 875 | + |
|
| 876 | + $isUsed = $User->isLoggedIn() && |
|
| 877 | + ( |
|
| 878 | + Configure::read('Saito.Settings.category_chooser_global') || |
|
| 879 | + ( |
|
| 880 | + Configure::read( |
|
| 881 | + 'Saito.Settings.category_chooser_user_override' |
|
| 882 | + ) && $User['user_category_override'] |
|
| 883 | + ) |
|
| 884 | + ); |
|
| 885 | + |
|
| 886 | + if ($isUsed) { |
|
| 887 | + // @todo find right place for this; also: User::getCategories(); |
|
| 888 | + $UserCategories = new Categories($User->getSettings(), $categories); |
|
| 889 | + list($categories, $type, $custom) = $UserCategories->get(); |
|
| 890 | + |
|
| 891 | + $this->set('categoryChooserChecked', $custom); |
|
| 892 | + |
|
| 893 | + switch ($type) { |
|
| 894 | + case 'single': |
|
| 895 | + $title = $User['user_category_active']; |
|
| 896 | + break; |
|
| 897 | + case 'custom': |
|
| 898 | + $title = __('Custom'); |
|
| 899 | + break; |
|
| 900 | + default: |
|
| 901 | + $title = __('All Categories'); |
|
| 902 | + } |
|
| 903 | + $this->set('categoryChooserTitleId', $title); |
|
| 904 | + $this->set('categoryChooser', $User->Categories->getAllowed('list')); |
|
| 905 | + } |
|
| 906 | + return $categories; |
|
| 907 | + } |
|
| 908 | + |
|
| 909 | + protected function _setAddViewVars() { |
|
| 910 | + //= categories for dropdown |
|
| 911 | + $categories = $this->CurrentUser->Categories->getAllowed('list'); |
|
| 912 | + $this->set('categories', $categories); |
|
| 913 | + } |
|
| 914 | 914 | |
| 915 | 915 | /** |
| 916 | 916 | * Decide if an answering panel is show when rendering a posting |
| 917 | 917 | */ |
| 918 | - protected function _showAnsweringPanel() { |
|
| 919 | - $showAnsweringPanel = false; |
|
| 920 | - |
|
| 921 | - if ($this->CurrentUser->isLoggedIn()) { |
|
| 922 | - // Only logged in users see the answering buttons if they … |
|
| 923 | - if (// … directly on entries/view but not inline |
|
| 924 | - ($this->request->action === 'view' && !$this->request->is('ajax')) |
|
| 925 | - // … directly in entries/mix |
|
| 926 | - || $this->request->action === 'mix' |
|
| 927 | - // … inline viewing … on entries/index. |
|
| 928 | - || ( $this->localReferer('controller') === 'entries' && $this->localReferer('action') === 'index') |
|
| 929 | - ): |
|
| 930 | - $showAnsweringPanel = true; |
|
| 931 | - endif; |
|
| 932 | - } |
|
| 933 | - $this->set('showAnsweringPanel', $showAnsweringPanel); |
|
| 934 | - } |
|
| 935 | - |
|
| 936 | - protected function _setRootEntry($entry) { |
|
| 937 | - if ((int)$entry['Entry']['pid'] !== 0) { |
|
| 938 | - $_rootEntry = $this->Entry->find('first', |
|
| 939 | - [ |
|
| 940 | - 'contain' => false, |
|
| 941 | - 'conditions' => ['Entry.id' => $entry['Entry']['tid']], |
|
| 942 | - 'fields' => ['Entry.user_id'] |
|
| 943 | - ]); |
|
| 944 | - } else { |
|
| 945 | - $_rootEntry = $entry; |
|
| 946 | - } |
|
| 947 | - $this->set('rootEntry', $_rootEntry); |
|
| 948 | - } |
|
| 949 | - |
|
| 950 | - protected function _setTitleFromEntry($entry, $type = null) { |
|
| 951 | - if ($type === null) { |
|
| 952 | - $template = __(':subject | :category'); |
|
| 953 | - } else { |
|
| 954 | - $template = __(':subject (:type) | :category'); |
|
| 955 | - } |
|
| 956 | - $this->set('title_for_layout', |
|
| 957 | - CakeText::insert($template, |
|
| 958 | - [ |
|
| 959 | - 'category' => $entry['Category']['category'], |
|
| 960 | - 'subject' => $entry['Entry']['subject'], |
|
| 961 | - 'type' => $type |
|
| 962 | - ] |
|
| 963 | - ) |
|
| 964 | - ); |
|
| 965 | - } |
|
| 966 | - |
|
| 967 | - } |
|
| 918 | + protected function _showAnsweringPanel() { |
|
| 919 | + $showAnsweringPanel = false; |
|
| 920 | + |
|
| 921 | + if ($this->CurrentUser->isLoggedIn()) { |
|
| 922 | + // Only logged in users see the answering buttons if they … |
|
| 923 | + if (// … directly on entries/view but not inline |
|
| 924 | + ($this->request->action === 'view' && !$this->request->is('ajax')) |
|
| 925 | + // … directly in entries/mix |
|
| 926 | + || $this->request->action === 'mix' |
|
| 927 | + // … inline viewing … on entries/index. |
|
| 928 | + || ( $this->localReferer('controller') === 'entries' && $this->localReferer('action') === 'index') |
|
| 929 | + ): |
|
| 930 | + $showAnsweringPanel = true; |
|
| 931 | + endif; |
|
| 932 | + } |
|
| 933 | + $this->set('showAnsweringPanel', $showAnsweringPanel); |
|
| 934 | + } |
|
| 935 | + |
|
| 936 | + protected function _setRootEntry($entry) { |
|
| 937 | + if ((int)$entry['Entry']['pid'] !== 0) { |
|
| 938 | + $_rootEntry = $this->Entry->find('first', |
|
| 939 | + [ |
|
| 940 | + 'contain' => false, |
|
| 941 | + 'conditions' => ['Entry.id' => $entry['Entry']['tid']], |
|
| 942 | + 'fields' => ['Entry.user_id'] |
|
| 943 | + ]); |
|
| 944 | + } else { |
|
| 945 | + $_rootEntry = $entry; |
|
| 946 | + } |
|
| 947 | + $this->set('rootEntry', $_rootEntry); |
|
| 948 | + } |
|
| 949 | + |
|
| 950 | + protected function _setTitleFromEntry($entry, $type = null) { |
|
| 951 | + if ($type === null) { |
|
| 952 | + $template = __(':subject | :category'); |
|
| 953 | + } else { |
|
| 954 | + $template = __(':subject (:type) | :category'); |
|
| 955 | + } |
|
| 956 | + $this->set('title_for_layout', |
|
| 957 | + CakeText::insert($template, |
|
| 958 | + [ |
|
| 959 | + 'category' => $entry['Category']['category'], |
|
| 960 | + 'subject' => $entry['Entry']['subject'], |
|
| 961 | + 'type' => $type |
|
| 962 | + ] |
|
| 963 | + ) |
|
| 964 | + ); |
|
| 965 | + } |
|
| 966 | + |
|
| 967 | + } |
|
@@ -242,7 +242,8 @@ discard block |
||
| 242 | 242 | $this->_stop(); |
| 243 | 243 | } |
| 244 | 244 | // answering through POST request |
| 245 | - else : |
|
| 245 | + else { |
|
| 246 | + : |
|
| 246 | 247 | if ($this->localReferer('action') === 'mix') { |
| 247 | 248 | // answer request came from mix ansicht |
| 248 | 249 | $this->redirect( |
@@ -253,6 +254,7 @@ discard block |
||
| 253 | 254 | '#' => $this->Entry->id |
| 254 | 255 | ] |
| 255 | 256 | ); |
| 257 | + } |
|
| 256 | 258 | |
| 257 | 259 | } else { |
| 258 | 260 | // normal posting from entries/add or entries/view |
@@ -267,7 +269,8 @@ discard block |
||
| 267 | 269 | } |
| 268 | 270 | return; |
| 269 | 271 | endif; |
| 270 | - else : |
|
| 272 | + else { |
|
| 273 | + : |
|
| 271 | 274 | // Error while trying to save a post |
| 272 | 275 | if (count($this->Entry->validationErrors) === 0) { |
| 273 | 276 | $this->Session->setFlash( |
@@ -276,6 +279,7 @@ discard block |
||
| 276 | 279 | ), |
| 277 | 280 | 'flash/error' |
| 278 | 281 | ); |
| 282 | + } |
|
| 279 | 283 | } |
| 280 | 284 | endif; |
| 281 | 285 | |
@@ -584,10 +588,12 @@ discard block |
||
| 584 | 588 | ); |
| 585 | 589 | $entry = $this->dic->newInstance('\Saito\Posting\Posting', ['rawData' => $newEntry]); |
| 586 | 590 | $this->set('entry', $entry); |
| 587 | - else : |
|
| 591 | + else { |
|
| 592 | + : |
|
| 588 | 593 | // validation errors |
| 589 | 594 | foreach ($errors as $field => $error) { |
| 590 | 595 | $message = __d('nondynamic', $field) . ": " . __d( 'nondynamic', $error[0]); |
| 596 | + } |
|
| 591 | 597 | $this->JsData->addAppJsMessage( |
| 592 | 598 | $message, |
| 593 | 599 | [ |
@@ -28,6 +28,9 @@ discard block |
||
| 28 | 28 | } |
| 29 | 29 | } |
| 30 | 30 | |
| 31 | + /** |
|
| 32 | + * @param string $data |
|
| 33 | + */ |
|
| 31 | 34 | protected function _statusAsEventStream($data) { |
| 32 | 35 | // time in ms to next request |
| 33 | 36 | $_retry = '10000'; |
@@ -40,6 +43,9 @@ discard block |
||
| 40 | 43 | return $_out; |
| 41 | 44 | } |
| 42 | 45 | |
| 46 | + /** |
|
| 47 | + * @param string $data |
|
| 48 | + */ |
|
| 43 | 49 | protected function _statusAsJson($data) { |
| 44 | 50 | if ($this->request->is('ajax') === false) { |
| 45 | 51 | throw new BadRequestException(); |
@@ -36,7 +36,7 @@ |
||
| 36 | 36 | $this->response->disableCache(); |
| 37 | 37 | $_out = ''; |
| 38 | 38 | $_out .= "retry: $_retry\n"; |
| 39 | - $_out .= 'data: ' . $data . "\n\n"; |
|
| 39 | + $_out .= 'data: '.$data."\n\n"; |
|
| 40 | 40 | return $_out; |
| 41 | 41 | } |
| 42 | 42 | |
@@ -1,57 +1,57 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | - App::uses('AppController', 'Controller'); |
|
| 4 | - |
|
| 5 | - class StatusController extends AppController { |
|
| 6 | - |
|
| 7 | - public $uses = [ |
|
| 8 | - 'Shout' |
|
| 9 | - ]; |
|
| 10 | - |
|
| 11 | - public $autoRender = false; |
|
| 12 | - |
|
| 13 | - /** |
|
| 14 | - * Current app status ping |
|
| 15 | - * |
|
| 16 | - * @return string |
|
| 17 | - * @throws BadRequestException |
|
| 18 | - */ |
|
| 19 | - public function status() { |
|
| 20 | - $data = [ |
|
| 21 | - 'lastShoutId' => $this->Shout->findLastId() |
|
| 22 | - ]; |
|
| 23 | - $data = json_encode($data); |
|
| 24 | - if ($this->request->accepts('text/event-streams')) { |
|
| 25 | - return $this->_statusAsEventStream($data); |
|
| 26 | - } else { |
|
| 27 | - return $this->_statusAsJson($data); |
|
| 28 | - } |
|
| 29 | - } |
|
| 30 | - |
|
| 31 | - protected function _statusAsEventStream($data) { |
|
| 32 | - // time in ms to next request |
|
| 33 | - $_retry = '10000'; |
|
| 34 | - $this->response->type(['eventstream' => 'text/event-stream']); |
|
| 35 | - $this->response->type('eventstream'); |
|
| 36 | - $this->response->disableCache(); |
|
| 37 | - $_out = ''; |
|
| 38 | - $_out .= "retry: $_retry\n"; |
|
| 39 | - $_out .= 'data: ' . $data . "\n\n"; |
|
| 40 | - return $_out; |
|
| 41 | - } |
|
| 42 | - |
|
| 43 | - protected function _statusAsJson($data) { |
|
| 44 | - if ($this->request->is('ajax') === false) { |
|
| 45 | - throw new BadRequestException(); |
|
| 46 | - } |
|
| 47 | - return $data; |
|
| 48 | - } |
|
| 49 | - |
|
| 50 | - public function beforeFilter() { |
|
| 51 | - parent::beforeFilter(); |
|
| 52 | - if ($this->Components->enabled('Auth')) { |
|
| 53 | - $this->Components->disable('Auth'); |
|
| 54 | - } |
|
| 55 | - } |
|
| 56 | - |
|
| 57 | - } |
|
| 3 | + App::uses('AppController', 'Controller'); |
|
| 4 | + |
|
| 5 | + class StatusController extends AppController { |
|
| 6 | + |
|
| 7 | + public $uses = [ |
|
| 8 | + 'Shout' |
|
| 9 | + ]; |
|
| 10 | + |
|
| 11 | + public $autoRender = false; |
|
| 12 | + |
|
| 13 | + /** |
|
| 14 | + * Current app status ping |
|
| 15 | + * |
|
| 16 | + * @return string |
|
| 17 | + * @throws BadRequestException |
|
| 18 | + */ |
|
| 19 | + public function status() { |
|
| 20 | + $data = [ |
|
| 21 | + 'lastShoutId' => $this->Shout->findLastId() |
|
| 22 | + ]; |
|
| 23 | + $data = json_encode($data); |
|
| 24 | + if ($this->request->accepts('text/event-streams')) { |
|
| 25 | + return $this->_statusAsEventStream($data); |
|
| 26 | + } else { |
|
| 27 | + return $this->_statusAsJson($data); |
|
| 28 | + } |
|
| 29 | + } |
|
| 30 | + |
|
| 31 | + protected function _statusAsEventStream($data) { |
|
| 32 | + // time in ms to next request |
|
| 33 | + $_retry = '10000'; |
|
| 34 | + $this->response->type(['eventstream' => 'text/event-stream']); |
|
| 35 | + $this->response->type('eventstream'); |
|
| 36 | + $this->response->disableCache(); |
|
| 37 | + $_out = ''; |
|
| 38 | + $_out .= "retry: $_retry\n"; |
|
| 39 | + $_out .= 'data: ' . $data . "\n\n"; |
|
| 40 | + return $_out; |
|
| 41 | + } |
|
| 42 | + |
|
| 43 | + protected function _statusAsJson($data) { |
|
| 44 | + if ($this->request->is('ajax') === false) { |
|
| 45 | + throw new BadRequestException(); |
|
| 46 | + } |
|
| 47 | + return $data; |
|
| 48 | + } |
|
| 49 | + |
|
| 50 | + public function beforeFilter() { |
|
| 51 | + parent::beforeFilter(); |
|
| 52 | + if ($this->Components->enabled('Auth')) { |
|
| 53 | + $this->Components->disable('Auth'); |
|
| 54 | + } |
|
| 55 | + } |
|
| 56 | + |
|
| 57 | + } |
|
@@ -221,6 +221,9 @@ discard block |
||
| 221 | 221 | $this->_ignore($blockedId, false); |
| 222 | 222 | } |
| 223 | 223 | |
| 224 | + /** |
|
| 225 | + * @param boolean $set |
|
| 226 | + */ |
|
| 224 | 227 | protected function _ignore($blockedId, $set) { |
| 225 | 228 | if (!$this->CurrentUser->isLoggedIn() || !is_numeric($blockedId)) { |
| 226 | 229 | throw new BadRequestException(); |
@@ -661,7 +664,7 @@ discard block |
||
| 661 | 664 | * |
| 662 | 665 | * @param SaitoUser $CurrentUser |
| 663 | 666 | * @param int $userId |
| 664 | - * @return type |
|
| 667 | + * @return boolean |
|
| 665 | 668 | */ |
| 666 | 669 | protected function _isEditingAllowed(\Saito\User\ForumsUserInterface $CurrentUser, $userId) { |
| 667 | 670 | if ($CurrentUser->isAdmin()) { |
@@ -157,7 +157,7 @@ discard block |
||
| 157 | 157 | $code = $this->request->query('c'); |
| 158 | 158 | |
| 159 | 159 | try { |
| 160 | - $activated = $this->User->activate((int)$id, $code); |
|
| 160 | + $activated = $this->User->activate((int) $id, $code); |
|
| 161 | 161 | } catch (Exception $e) { |
| 162 | 162 | $activated = false; |
| 163 | 163 | } |
@@ -421,7 +421,7 @@ discard block |
||
| 421 | 421 | return; |
| 422 | 422 | } |
| 423 | 423 | |
| 424 | - $id = (int)$this->request->data('User.lockUserId'); |
|
| 424 | + $id = (int) $this->request->data('User.lockUserId'); |
|
| 425 | 425 | if (!$id) { |
| 426 | 426 | throw new BadRequestException; |
| 427 | 427 | } |
@@ -445,7 +445,7 @@ discard block |
||
| 445 | 445 | ); |
| 446 | 446 | } else { |
| 447 | 447 | try { |
| 448 | - $duration = (int)$this->request->data('User.lockPeriod'); |
|
| 448 | + $duration = (int) $this->request->data('User.lockPeriod'); |
|
| 449 | 449 | $status = $this->User->UserBlock->block( |
| 450 | 450 | new \Saito\User\Blocker\ManualBlocker, |
| 451 | 451 | $id, |
@@ -608,7 +608,7 @@ discard block |
||
| 608 | 608 | } |
| 609 | 609 | |
| 610 | 610 | $allowed = $this->viewVars['slidetabs']; |
| 611 | - $order = array_filter( $order, function ($item) use ($allowed) { |
|
| 611 | + $order = array_filter($order, function($item) use ($allowed) { |
|
| 612 | 612 | return in_array($item, $allowed); |
| 613 | 613 | }); |
| 614 | 614 | $order = serialize($order); |
@@ -667,7 +667,7 @@ discard block |
||
| 667 | 667 | if ($CurrentUser->isAdmin()) { |
| 668 | 668 | return true; |
| 669 | 669 | } |
| 670 | - return $CurrentUser->getId() === (int)$userId; |
|
| 670 | + return $CurrentUser->getId() === (int) $userId; |
|
| 671 | 671 | } |
| 672 | 672 | |
| 673 | 673 | protected function _passwordAuthSwitch($data) { |
@@ -1,679 +1,679 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | - use Saito\Exception\Logger\ExceptionLogger; |
|
| 4 | - use Saito\Exception\Logger\ForbiddenLogger; |
|
| 5 | - use Saito\Exception\SaitoForbiddenException; |
|
| 6 | - use Saito\User\SaitoUser; |
|
| 7 | - |
|
| 8 | - App::uses('AppController', 'Controller'); |
|
| 9 | - |
|
| 10 | - class UsersController extends AppController { |
|
| 11 | - |
|
| 12 | - public $name = 'Users'; |
|
| 13 | - |
|
| 14 | - public $helpers = [ |
|
| 15 | - 'Farbtastic', |
|
| 16 | - 'SimpleCaptcha.SimpleCaptcha', |
|
| 17 | - 'EntryH', |
|
| 18 | - 'Map', |
|
| 19 | - 'Text' |
|
| 20 | - ]; |
|
| 21 | - |
|
| 22 | - public function login() { |
|
| 23 | - $this->CurrentUser->logOut(); |
|
| 24 | - |
|
| 25 | - //# just show form |
|
| 26 | - if (empty($this->request->data['User']['username'])) { |
|
| 27 | - return; |
|
| 28 | - } |
|
| 29 | - |
|
| 30 | - //# successful login with request data |
|
| 31 | - if ($this->CurrentUser->login()) { |
|
| 32 | - if ($this->localReferer('action') === 'login') { |
|
| 33 | - $this->redirect($this->Auth->redirectUrl()); |
|
| 34 | - } else { |
|
| 35 | - $this->redirect($this->referer()); |
|
| 36 | - } |
|
| 37 | - return; |
|
| 38 | - } |
|
| 39 | - |
|
| 40 | - //# error on login |
|
| 41 | - $this->User->contain(); |
|
| 42 | - $username = $this->request->data['User']['username']; |
|
| 43 | - $readUser = $this->User->findByUsername($username); |
|
| 44 | - |
|
| 45 | - $status = null; |
|
| 46 | - |
|
| 47 | - if (!empty($readUser)) { |
|
| 48 | - $User = new SaitoUser($readUser['User']); |
|
| 49 | - $status = $User->isForbidden(); |
|
| 50 | - } |
|
| 51 | - |
|
| 52 | - switch ($status) { |
|
| 53 | - case 'locked': |
|
| 54 | - $ends = $this->User->UserBlock |
|
| 55 | - ->getBlockEndsForUser($readUser['User']['id']); |
|
| 56 | - if ($ends) { |
|
| 57 | - $message = __('user.block.pubExpEnds', [$username, |
|
| 58 | - CakeTime::timeAgoInWords($ends, ['accuracy' => 'hour'])]); |
|
| 59 | - } else { |
|
| 60 | - $message = __('user.block.pubExp', $username); |
|
| 61 | - } |
|
| 62 | - break; |
|
| 63 | - case 'unactivated': |
|
| 64 | - $message = __('User %s is not activated yet.', $readUser['User']['username']); |
|
| 65 | - break; |
|
| 66 | - default: |
|
| 67 | - $message = __('auth_loginerror'); |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - // don't autofill password |
|
| 71 | - unset($this->request->data['User']['password']); |
|
| 72 | - |
|
| 73 | - $Logger = new ForbiddenLogger; |
|
| 74 | - $Logger->write("Unsuccessful login for user: $username", |
|
| 75 | - ['msgs' => [$message]]); |
|
| 76 | - |
|
| 77 | - $this->Session->setFlash($message, 'default', [], 'auth'); |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - public function logout() { |
|
| 81 | - $this->CurrentUser->logout(); |
|
| 82 | - } |
|
| 83 | - |
|
| 84 | - public function register() { |
|
| 85 | - $this->set('status', 'view'); |
|
| 86 | - |
|
| 87 | - $this->CurrentUser->logout(); |
|
| 88 | - |
|
| 89 | - $tosRequired = Configure::read('Saito.Settings.tos_enabled'); |
|
| 90 | - $this->set(compact('tosRequired')); |
|
| 91 | - |
|
| 92 | - // display empty form |
|
| 93 | - if (empty($this->request->data)) { |
|
| 94 | - return; |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - $data = $this->request->data; |
|
| 98 | - |
|
| 99 | - if (!$tosRequired) { |
|
| 100 | - $data['User']['tos_confirm'] = true; |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - $tosConfirmed = $data['User']['tos_confirm']; |
|
| 104 | - if (!$tosConfirmed) { |
|
| 105 | - return; |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - $data = $this->_passwordAuthSwitch($data); |
|
| 109 | - $this->User->Behaviors->attach('SimpleCaptcha.SimpleCaptcha'); |
|
| 110 | - $user = $this->User->register($data); |
|
| 111 | - |
|
| 112 | - // registering failed, show form again |
|
| 113 | - if (!$user) { |
|
| 114 | - // undo the passwordAuthSwitch() to display error message for the field |
|
| 115 | - if (isset($this->User->validationErrors['password'])) { |
|
| 116 | - $this->User->validationErrors['user_password'] = $this->User->validationErrors['password']; |
|
| 117 | - } |
|
| 118 | - $data['User']['tos_confirm'] = false; |
|
| 119 | - $this->request->data = $data; |
|
| 120 | - return; |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - // registered successfully |
|
| 124 | - try { |
|
| 125 | - $forumName = Configure::read('Saito.Settings.forum_name'); |
|
| 126 | - $subject = __('register_email_subject', $forumName); |
|
| 127 | - $email = $this->SaitoEmail->email([ |
|
| 128 | - 'recipient' => $data, |
|
| 129 | - 'subject' => $subject, |
|
| 130 | - 'sender' => 'register', |
|
| 131 | - 'template' => 'user_register', |
|
| 132 | - 'viewVars' => ['user' => $user] |
|
| 133 | - ]); |
|
| 134 | - // only used in test cases |
|
| 135 | - $this->set('email', $email); |
|
| 136 | - } catch (Exception $e) { |
|
| 137 | - $Logger = new ExceptionLogger(); |
|
| 138 | - $Logger->write('Registering email confirmation failed', ['e' => $e]); |
|
| 139 | - $this->set('status', 'fail: email'); |
|
| 140 | - return; |
|
| 141 | - } |
|
| 142 | - |
|
| 143 | - $this->set('status', 'success'); |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - /** |
|
| 147 | - * register success (user clicked link in confirm mail) |
|
| 148 | - * |
|
| 149 | - * @param $id |
|
| 150 | - * @throws BadRequestException |
|
| 151 | - */ |
|
| 152 | - public function rs($id = null) { |
|
| 153 | - if (!$id) { |
|
| 154 | - throw new BadRequestException(); |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - $code = $this->request->query('c'); |
|
| 158 | - |
|
| 159 | - try { |
|
| 160 | - $activated = $this->User->activate((int)$id, $code); |
|
| 161 | - } catch (Exception $e) { |
|
| 162 | - $activated = false; |
|
| 163 | - } |
|
| 164 | - |
|
| 165 | - if (!$activated) { |
|
| 166 | - $activated = ['status' => 'fail']; |
|
| 167 | - } |
|
| 168 | - $this->set('status', $activated['status']); |
|
| 169 | - } |
|
| 170 | - |
|
| 171 | - public function admin_index() { |
|
| 172 | - $data = $this->User->find( |
|
| 173 | - 'all', |
|
| 174 | - [ |
|
| 175 | - 'contain' => false, |
|
| 176 | - 'fields' => [ |
|
| 177 | - 'id', |
|
| 178 | - 'username', |
|
| 179 | - 'user_type', |
|
| 180 | - 'user_email', |
|
| 181 | - 'registered', |
|
| 182 | - 'user_lock' |
|
| 183 | - ], |
|
| 184 | - 'order' => ['User.username' => 'asc'] |
|
| 185 | - ] |
|
| 186 | - ); |
|
| 187 | - $this->set('users', $data); |
|
| 188 | - } |
|
| 189 | - |
|
| 190 | - public function index() { |
|
| 191 | - $menuItems = [ |
|
| 192 | - 'username' => [__('username_marking'), []], |
|
| 193 | - 'user_type' => [__('user_type'), []], |
|
| 194 | - 'UserOnline.logged_in' => [__('userlist_online'), ['direction' => 'desc']], |
|
| 195 | - 'registered' => [__('registered'), ['direction' => 'desc']] |
|
| 196 | - ]; |
|
| 197 | - $showBlocked = Configure::read('Saito.Settings.block_user_ui'); |
|
| 198 | - if ($showBlocked) { |
|
| 199 | - $menuItems['user_lock'] = [__('user.set.lock.t'), ['direction' => 'desc']]; |
|
| 200 | - } |
|
| 201 | - |
|
| 202 | - $this->paginate = [ |
|
| 203 | - 'contain' => 'UserOnline', |
|
| 204 | - 'limit' => 400, |
|
| 205 | - 'order' => ['UserOnline.logged_in' => 'desc', 'User.username' => 'asc'] |
|
| 206 | - ]; |
|
| 207 | - $users = $this->paginate('User', null, array_keys($menuItems)); |
|
| 208 | - |
|
| 209 | - $this->set(compact('menuItems', 'users')); |
|
| 210 | - } |
|
| 211 | - |
|
| 212 | - public function ignore() { |
|
| 213 | - $this->request->allowMethod('POST'); |
|
| 214 | - $blockedId = $this->request->data('id'); |
|
| 215 | - $this->_ignore($blockedId, true); |
|
| 216 | - } |
|
| 217 | - |
|
| 218 | - public function unignore() { |
|
| 219 | - $this->request->allowMethod('POST'); |
|
| 220 | - $blockedId = $this->request->data('id'); |
|
| 221 | - $this->_ignore($blockedId, false); |
|
| 222 | - } |
|
| 223 | - |
|
| 224 | - protected function _ignore($blockedId, $set) { |
|
| 225 | - if (!$this->CurrentUser->isLoggedIn() || !is_numeric($blockedId)) { |
|
| 226 | - throw new BadRequestException(); |
|
| 227 | - } |
|
| 228 | - $userId = $this->CurrentUser->getId(); |
|
| 229 | - $this->User->id = $userId; |
|
| 230 | - if (!$this->User->exists($userId) || $userId == $blockedId) { |
|
| 231 | - throw new BadRequestException(); |
|
| 232 | - } |
|
| 233 | - if ($set) { |
|
| 234 | - $this->User->Ignore->ignore($userId, $blockedId); |
|
| 235 | - } else { |
|
| 236 | - $this->User->Ignore->unignore($userId, $blockedId); |
|
| 237 | - } |
|
| 238 | - $this->redirect($this->referer()); |
|
| 239 | - } |
|
| 240 | - |
|
| 241 | - public function admin_add() { |
|
| 242 | - if (!empty($this->request->data)) : |
|
| 243 | - $this->request->data = $this->_passwordAuthSwitch($this->request->data); |
|
| 244 | - if ($this->User->register($this->request->data, true)) { |
|
| 245 | - $this->Session->setFlash(__('user.admin.add.success'), |
|
| 246 | - 'flash/success'); |
|
| 247 | - $this->redirect(['action' => 'view', $this->User->id, 'admin' => false]); |
|
| 248 | - } |
|
| 249 | - endif; |
|
| 250 | - } |
|
| 251 | - |
|
| 252 | - public function map() { |
|
| 253 | - if (!Configure::read('Saito.Settings.map_enabled')) { |
|
| 254 | - $this->Session->setFlash(__('admin.setting.disabled', __('admin.feat.map')), 'flash/error'); |
|
| 255 | - $this->redirect('/'); |
|
| 256 | - return; |
|
| 257 | - } |
|
| 258 | - $users = $this->User->find('all', |
|
| 259 | - [ |
|
| 260 | - 'contain' => false, |
|
| 261 | - 'conditions' => ['user_place_lat !=' => null], |
|
| 262 | - 'fields' => [ |
|
| 263 | - 'User.id', |
|
| 264 | - 'User.username', |
|
| 265 | - 'User.user_place_lat', |
|
| 266 | - 'User.user_place_lng' |
|
| 267 | - ] |
|
| 268 | - ] |
|
| 269 | - ); |
|
| 270 | - $this->set(compact('users')); |
|
| 271 | - } |
|
| 272 | - |
|
| 273 | - public function name($id = null) { |
|
| 274 | - if (!empty($id)) { |
|
| 275 | - $this->User->contain(); |
|
| 276 | - $viewedUser = $this->User->findByUsername($id); |
|
| 277 | - if (!empty($viewedUser)) { |
|
| 278 | - $this->redirect( |
|
| 279 | - [ |
|
| 280 | - 'controller' => 'users', |
|
| 281 | - 'action' => 'view', |
|
| 282 | - $viewedUser['User']['id'] |
|
| 283 | - ] |
|
| 284 | - ); |
|
| 285 | - return; |
|
| 286 | - } |
|
| 287 | - } |
|
| 288 | - $this->Session->setFlash(__('Invalid user'), 'flash/error'); |
|
| 289 | - $this->redirect('/'); |
|
| 290 | - } |
|
| 291 | - |
|
| 292 | - public function view($id = null) { |
|
| 293 | - // redirect view/<username> to name/<username> |
|
| 294 | - if (!empty($id) && !is_numeric($id)) { |
|
| 295 | - $this->redirect( |
|
| 296 | - [ |
|
| 297 | - 'controller' => 'users', |
|
| 298 | - 'action' => 'name', |
|
| 299 | - $id |
|
| 300 | - ] |
|
| 301 | - ); |
|
| 302 | - return; // test case return |
|
| 303 | - } |
|
| 304 | - |
|
| 305 | - $this->User->id = $id; |
|
| 306 | - $this->User->contain(['UserBlock' => ['By'], 'UserOnline']); |
|
| 307 | - $viewedUser = $this->User->read(); |
|
| 308 | - |
|
| 309 | - if ($id === null || empty($viewedUser)) { |
|
| 310 | - $this->Session->setFlash(__('Invalid user'), 'flash/error'); |
|
| 311 | - $this->redirect('/'); |
|
| 312 | - return; |
|
| 313 | - } |
|
| 314 | - |
|
| 315 | - $viewedUser['User']['number_of_entries'] = $this->User->numberOfEntries(); |
|
| 316 | - |
|
| 317 | - $entriesShownOnPage = 20; |
|
| 318 | - $this->set( |
|
| 319 | - 'lastEntries', |
|
| 320 | - $this->User->Entry->getRecentEntries( |
|
| 321 | - $this->CurrentUser, |
|
| 322 | - [ |
|
| 323 | - 'user_id' => $this->User->id, |
|
| 324 | - 'limit' => $entriesShownOnPage |
|
| 325 | - ] |
|
| 326 | - ) |
|
| 327 | - ); |
|
| 328 | - |
|
| 329 | - $this->set( |
|
| 330 | - 'hasMoreEntriesThanShownOnPage', |
|
| 331 | - ($viewedUser['User']['number_of_entries'] - $entriesShownOnPage) > 0 |
|
| 332 | - ); |
|
| 333 | - |
|
| 334 | - if ($this->CurrentUser->getId() == $id) { |
|
| 335 | - $viewedUser['User']['ignores'] = $this->User->Ignore->ignoredBy($id); |
|
| 336 | - } |
|
| 337 | - $viewedUser['User']['solves_count'] = $this->User->countSolved($id); |
|
| 338 | - $this->set('user', $viewedUser); |
|
| 339 | - $this->set( |
|
| 340 | - 'title_for_layout', |
|
| 341 | - $viewedUser['User']['username'] |
|
| 342 | - ); |
|
| 343 | - } |
|
| 344 | - |
|
| 345 | - /** |
|
| 346 | - * @param null $id |
|
| 347 | - * @throws Saito\Exception\SaitoForbiddenException |
|
| 348 | - * @throws BadRequestException |
|
| 349 | - */ |
|
| 350 | - public function edit($id = null) { |
|
| 351 | - if (!$id) { |
|
| 352 | - throw new BadRequestException; |
|
| 353 | - } |
|
| 354 | - if (!$this->_isEditingAllowed($this->CurrentUser, $id)) { |
|
| 355 | - throw new \Saito\Exception\SaitoForbiddenException("Attempt to edit user $id.", [ |
|
| 356 | - 'CurrentUser' => $this->CurrentUser |
|
| 357 | - ]); |
|
| 358 | - } |
|
| 359 | - |
|
| 360 | - $this->set('userId', $id); |
|
| 361 | - |
|
| 362 | - // try to save entry |
|
| 363 | - if (!empty($this->request->data)) { |
|
| 364 | - $data = $this->request->data['User']; |
|
| 365 | - |
|
| 366 | - unset($data['id']); |
|
| 367 | - //# make sure only admin can edit these fields |
|
| 368 | - if ($this->CurrentUser['user_type'] !== 'admin') { |
|
| 369 | - // @todo DRY: refactor this admin fields together with view |
|
| 370 | - unset($data['username'], $data['user_email'], $data['user_type']); |
|
| 371 | - } |
|
| 372 | - |
|
| 373 | - $this->User->id = $id; |
|
| 374 | - $success = $this->User->save($data); |
|
| 375 | - if ($success) { |
|
| 376 | - $this->redirect(['action' => 'view', $id]); |
|
| 377 | - return; |
|
| 378 | - } else { |
|
| 379 | - // if empty fields are missing from send form read user again |
|
| 380 | - $this->User->contain(); |
|
| 381 | - $user = $this->User->read(); |
|
| 382 | - $this->request->data['User'] = array_merge($user['User'], |
|
| 383 | - $this->request->data['User']); |
|
| 384 | - |
|
| 385 | - $this->User->set($this->request->data); |
|
| 386 | - $this->User->validates(); |
|
| 387 | - |
|
| 388 | - $this->JsData->addAppJsMessage( |
|
| 389 | - __('The user could not be saved. Please, try again.'), |
|
| 390 | - ['type' => 'error']); |
|
| 391 | - } |
|
| 392 | - } |
|
| 393 | - |
|
| 394 | - if (empty($this->request->data)) { |
|
| 395 | - //* View Entry by id |
|
| 396 | - $this->User->id = $id; |
|
| 397 | - $this->User->contain('UserOnline'); |
|
| 398 | - $this->request->data = $this->User->read(); |
|
| 399 | - } |
|
| 400 | - |
|
| 401 | - $themes = $this->Themes->getAvailable(); |
|
| 402 | - $this->set('availableThemes', array_combine($themes, $themes)); |
|
| 403 | - $this->set('user', $this->request->data); |
|
| 404 | - $this->set( |
|
| 405 | - 'title_for_layout', |
|
| 406 | - __('Edit %s Profil', |
|
| 407 | - Saito\String\Properize::prop($this->request->data['User']['username'])) |
|
| 408 | - ); |
|
| 409 | - } |
|
| 410 | - |
|
| 411 | - public function admin_block() { |
|
| 412 | - $this->set('UserBlock', $this->User->UserBlock->getAll()); |
|
| 413 | - } |
|
| 414 | - |
|
| 415 | - /** |
|
| 416 | - * @throws BadRequestException |
|
| 417 | - */ |
|
| 418 | - public function lock() { |
|
| 419 | - if (!($this->CurrentUser->isAdmin() || $this->viewVars['modLocking'])) { |
|
| 420 | - $this->redirect('/'); |
|
| 421 | - return; |
|
| 422 | - } |
|
| 423 | - |
|
| 424 | - $id = (int)$this->request->data('User.lockUserId'); |
|
| 425 | - if (!$id) { |
|
| 426 | - throw new BadRequestException; |
|
| 427 | - } |
|
| 428 | - |
|
| 429 | - $this->User->contain(); |
|
| 430 | - $readUser = $this->User->findById($id); |
|
| 431 | - if (!$readUser) { |
|
| 432 | - $this->Session->setFlash(__('User not found.'), 'flash/error'); |
|
| 433 | - $this->redirect('/'); |
|
| 434 | - return; |
|
| 435 | - } |
|
| 436 | - |
|
| 437 | - $editedUser = new SaitoUser($readUser['User']); |
|
| 438 | - |
|
| 439 | - if ($id == $this->CurrentUser->getId()) { |
|
| 440 | - $this->Session->setFlash(__("You can't lock yourself."), 'flash/error'); |
|
| 441 | - } elseif ($editedUser->isAdmin()) { |
|
| 442 | - $this->Session->setFlash( |
|
| 443 | - __("You can't lock administrators.", 'flash/error'), |
|
| 444 | - 'flash/error' |
|
| 445 | - ); |
|
| 446 | - } else { |
|
| 447 | - try { |
|
| 448 | - $duration = (int)$this->request->data('User.lockPeriod'); |
|
| 449 | - $status = $this->User->UserBlock->block( |
|
| 450 | - new \Saito\User\Blocker\ManualBlocker, |
|
| 451 | - $id, |
|
| 452 | - [ |
|
| 453 | - 'adminId' => $this->CurrentUser->getId(), |
|
| 454 | - 'duration' => $duration |
|
| 455 | - ] |
|
| 456 | - ); |
|
| 457 | - $username = $readUser['User']['username']; |
|
| 458 | - if ($status === true) { |
|
| 459 | - $message = __('User %s is locked.', $username); |
|
| 460 | - } else { |
|
| 461 | - $message = __('User %s is unlocked.', $username); |
|
| 462 | - } |
|
| 463 | - $this->Session->setFlash($message, 'flash/success'); |
|
| 464 | - } catch (Exception $e) { |
|
| 465 | - $this->Session->setFlash(__('Error while un/locking.'), 'flash/error'); |
|
| 466 | - } |
|
| 467 | - } |
|
| 468 | - $this->redirect($this->referer()); |
|
| 469 | - } |
|
| 470 | - |
|
| 471 | - public function unlock($id) { |
|
| 472 | - if (!$id || !($this->CurrentUser->isAdmin() || $this->viewVars['modLocking'])) { |
|
| 473 | - throw new BadRequestException; |
|
| 474 | - } |
|
| 475 | - if (!$this->User->UserBlock->unblock($id)) { |
|
| 476 | - $this->Session->setFlash(__('Error while unlocking.'), 'flash/error'); |
|
| 477 | - } |
|
| 478 | - $this->redirect($this->referer()); |
|
| 479 | - } |
|
| 480 | - |
|
| 481 | - public function admin_delete($id = null) { |
|
| 482 | - $this->User->contain(); |
|
| 483 | - $readUser = $this->User->findById($id); |
|
| 484 | - if (!$readUser) { |
|
| 485 | - $this->Session->setFlash(__('User not found.'), 'flash/error'); |
|
| 486 | - $this->redirect('/'); |
|
| 487 | - return; |
|
| 488 | - } |
|
| 489 | - |
|
| 490 | - if (isset($this->request->data['User']['modeDelete'])) { |
|
| 491 | - if ($id == $this->CurrentUser->getId()) { |
|
| 492 | - $this->Session->setFlash(__("You can't delete yourself."), 'flash/error'); |
|
| 493 | - } elseif ($id == 1) { |
|
| 494 | - $this->Session->setFlash(__("You can't delete the installation account."), 'flash/error'); |
|
| 495 | - } elseif ($this->User->deleteAllExceptEntries($id)) { |
|
| 496 | - $this->Session->setFlash(__('User %s deleted.', $readUser['User']['username']), 'flash/success'); |
|
| 497 | - $this->redirect('/'); |
|
| 498 | - return; |
|
| 499 | - } else { |
|
| 500 | - $this->Session->setFlash(__("Couldn't delete user."), 'flash/error'); |
|
| 501 | - } |
|
| 502 | - $this->redirect(['controller' => 'users', 'action' => 'view', $id]); |
|
| 503 | - return; |
|
| 504 | - } |
|
| 505 | - $this->set('user', $readUser); |
|
| 506 | - } |
|
| 507 | - |
|
| 508 | - /** |
|
| 509 | - * changes user password |
|
| 510 | - * |
|
| 511 | - * @param null $id |
|
| 512 | - * @throws \Saito\Exception\SaitoForbiddenException |
|
| 513 | - * @throws BadRequestException |
|
| 514 | - */ |
|
| 515 | - public function changepassword($id = null) { |
|
| 516 | - if (!$id) { |
|
| 517 | - throw new BadRequestException(); |
|
| 518 | - } |
|
| 519 | - |
|
| 520 | - $user = $this->User->getProfile($id); |
|
| 521 | - $allowed = $this->_isEditingAllowed($this->CurrentUser, $id); |
|
| 522 | - if (empty($user) || !$allowed) { |
|
| 523 | - throw new SaitoForbiddenException("Attempt to change password for user $id.", |
|
| 524 | - ['CurrentUser' => $this->CurrentUser]); |
|
| 525 | - } |
|
| 526 | - $this->set('userId', $id); |
|
| 527 | - $this->set('username', $user['username']); |
|
| 528 | - |
|
| 529 | - //# just show empty form |
|
| 530 | - if (empty($this->request->data)) { |
|
| 531 | - return; |
|
| 532 | - } |
|
| 533 | - |
|
| 534 | - //# process submitted form |
|
| 535 | - $this->request->data = $this->_passwordAuthSwitch($this->request->data); |
|
| 536 | - $data = [ |
|
| 537 | - 'id' => $id, |
|
| 538 | - 'password_old' => $this->request->data['User']['password_old'], |
|
| 539 | - 'password' => $this->request->data['User']['password'], |
|
| 540 | - 'password_confirm' => $this->request->data['User']['password_confirm'] |
|
| 541 | - ]; |
|
| 542 | - $success = $this->User->save($data); |
|
| 543 | - |
|
| 544 | - if ($success) { |
|
| 545 | - $this->Session->setFlash(__('change_password_success'), |
|
| 546 | - 'flash/success'); |
|
| 547 | - $this->redirect(['controller' => 'users', 'action' => 'edit', $id]); |
|
| 548 | - return; |
|
| 549 | - } |
|
| 550 | - |
|
| 551 | - $this->Session->setFlash( |
|
| 552 | - __d('nondynamic', current(array_pop($this->User->validationErrors))), |
|
| 553 | - 'flash/error' |
|
| 554 | - ); |
|
| 555 | - |
|
| 556 | - // unset all autofill form data |
|
| 557 | - $this->request->data = []; |
|
| 558 | - } |
|
| 559 | - |
|
| 560 | - /** |
|
| 561 | - * @throws BadRequestException |
|
| 562 | - */ |
|
| 563 | - private function __ajaxBeforeFilter() { |
|
| 564 | - if (!$this->request->is('ajax')) { |
|
| 565 | - throw new BadRequestException; |
|
| 566 | - } |
|
| 567 | - $this->autoRender = false; |
|
| 568 | - } |
|
| 569 | - |
|
| 570 | - /** |
|
| 571 | - * toggles slidetabs open/close |
|
| 572 | - * |
|
| 573 | - * @return $this|mixed |
|
| 574 | - * @throws BadRequestException |
|
| 575 | - */ |
|
| 576 | - public function slidetab_toggle() { |
|
| 577 | - $this->__ajaxBeforeFilter(); |
|
| 578 | - |
|
| 579 | - $toggle = $this->request->data('slidetabKey'); |
|
| 580 | - $allowed = [ |
|
| 581 | - 'show_userlist', |
|
| 582 | - 'show_recentposts', |
|
| 583 | - 'show_recententries', |
|
| 584 | - 'show_shoutbox' |
|
| 585 | - ]; |
|
| 586 | - if (!$toggle || !in_array($toggle, $allowed)) { |
|
| 587 | - throw new BadRequestException(null, 1412949882); |
|
| 588 | - } |
|
| 589 | - |
|
| 590 | - $this->User->id = $this->CurrentUser->getId(); |
|
| 591 | - $newValue = $this->User->toggle($toggle); |
|
| 592 | - $this->CurrentUser[$toggle] = $newValue; |
|
| 593 | - return $toggle; |
|
| 594 | - } |
|
| 595 | - |
|
| 596 | - /** |
|
| 597 | - * sets slidetab-order |
|
| 598 | - * |
|
| 599 | - * @return bool |
|
| 600 | - * @throws BadRequestException |
|
| 601 | - */ |
|
| 602 | - public function slidetab_order() { |
|
| 603 | - $this->__ajaxBeforeFilter(); |
|
| 604 | - |
|
| 605 | - $order = $this->request->data('slidetabOrder'); |
|
| 606 | - if (!$order) { |
|
| 607 | - throw new BadRequestException; |
|
| 608 | - } |
|
| 609 | - |
|
| 610 | - $allowed = $this->viewVars['slidetabs']; |
|
| 611 | - $order = array_filter( $order, function ($item) use ($allowed) { |
|
| 612 | - return in_array($item, $allowed); |
|
| 613 | - }); |
|
| 614 | - $order = serialize($order); |
|
| 615 | - |
|
| 616 | - $this->User->id = $this->CurrentUser->getId(); |
|
| 617 | - $this->User->saveField('slidetab_order', $order); |
|
| 618 | - $this->CurrentUser['slidetab_order'] = $order; |
|
| 619 | - |
|
| 620 | - return true; |
|
| 621 | - } |
|
| 622 | - |
|
| 623 | - /** |
|
| 624 | - * @param null $id |
|
| 625 | - * |
|
| 626 | - * @throws ForbiddenException |
|
| 627 | - */ |
|
| 628 | - public function setcategory($id = null) { |
|
| 629 | - if (!$this->CurrentUser->isLoggedIn()) { |
|
| 630 | - throw new ForbiddenException(); |
|
| 631 | - } |
|
| 632 | - $this->User->id = $this->CurrentUser->getId(); |
|
| 633 | - if ($id === 'all') { |
|
| 634 | - $this->User->setCategory('all'); |
|
| 635 | - } elseif (!$id && $this->request->data) { |
|
| 636 | - $this->User->setCategory($this->request->data['CatChooser']); |
|
| 637 | - } else { |
|
| 638 | - $this->User->setCategory($id); |
|
| 639 | - } |
|
| 640 | - $this->redirect($this->referer()); |
|
| 641 | - } |
|
| 642 | - |
|
| 643 | - public function beforeFilter() { |
|
| 644 | - Stopwatch::start('Users->beforeFilter()'); |
|
| 645 | - parent::beforeFilter(); |
|
| 646 | - |
|
| 647 | - // @todo CSRF protection |
|
| 648 | - $this->Security->unlockedActions[] = 'slidetab_toggle'; |
|
| 649 | - $this->Security->unlockedActions[] = 'slidetab_order'; |
|
| 650 | - |
|
| 651 | - $this->Auth->allow('login', 'register', 'rs'); |
|
| 652 | - $this->set('modLocking', |
|
| 653 | - $this->CurrentUser->isMod() && Configure::read('Saito.Settings.block_user_ui') |
|
| 654 | - ); |
|
| 655 | - |
|
| 656 | - Stopwatch::stop('Users->beforeFilter()'); |
|
| 657 | - } |
|
| 658 | - |
|
| 659 | - /** |
|
| 660 | - * Checks if the current user is allowed to edit user $userId |
|
| 661 | - * |
|
| 662 | - * @param SaitoUser $CurrentUser |
|
| 663 | - * @param int $userId |
|
| 664 | - * @return type |
|
| 665 | - */ |
|
| 666 | - protected function _isEditingAllowed(\Saito\User\ForumsUserInterface $CurrentUser, $userId) { |
|
| 667 | - if ($CurrentUser->isAdmin()) { |
|
| 668 | - return true; |
|
| 669 | - } |
|
| 670 | - return $CurrentUser->getId() === (int)$userId; |
|
| 671 | - } |
|
| 672 | - |
|
| 673 | - protected function _passwordAuthSwitch($data) { |
|
| 674 | - $data['User']['password'] = $data['User']['user_password']; |
|
| 675 | - unset($data['User']['user_password']); |
|
| 676 | - return $data; |
|
| 677 | - } |
|
| 678 | - |
|
| 679 | - } |
|
| 3 | + use Saito\Exception\Logger\ExceptionLogger; |
|
| 4 | + use Saito\Exception\Logger\ForbiddenLogger; |
|
| 5 | + use Saito\Exception\SaitoForbiddenException; |
|
| 6 | + use Saito\User\SaitoUser; |
|
| 7 | + |
|
| 8 | + App::uses('AppController', 'Controller'); |
|
| 9 | + |
|
| 10 | + class UsersController extends AppController { |
|
| 11 | + |
|
| 12 | + public $name = 'Users'; |
|
| 13 | + |
|
| 14 | + public $helpers = [ |
|
| 15 | + 'Farbtastic', |
|
| 16 | + 'SimpleCaptcha.SimpleCaptcha', |
|
| 17 | + 'EntryH', |
|
| 18 | + 'Map', |
|
| 19 | + 'Text' |
|
| 20 | + ]; |
|
| 21 | + |
|
| 22 | + public function login() { |
|
| 23 | + $this->CurrentUser->logOut(); |
|
| 24 | + |
|
| 25 | + //# just show form |
|
| 26 | + if (empty($this->request->data['User']['username'])) { |
|
| 27 | + return; |
|
| 28 | + } |
|
| 29 | + |
|
| 30 | + //# successful login with request data |
|
| 31 | + if ($this->CurrentUser->login()) { |
|
| 32 | + if ($this->localReferer('action') === 'login') { |
|
| 33 | + $this->redirect($this->Auth->redirectUrl()); |
|
| 34 | + } else { |
|
| 35 | + $this->redirect($this->referer()); |
|
| 36 | + } |
|
| 37 | + return; |
|
| 38 | + } |
|
| 39 | + |
|
| 40 | + //# error on login |
|
| 41 | + $this->User->contain(); |
|
| 42 | + $username = $this->request->data['User']['username']; |
|
| 43 | + $readUser = $this->User->findByUsername($username); |
|
| 44 | + |
|
| 45 | + $status = null; |
|
| 46 | + |
|
| 47 | + if (!empty($readUser)) { |
|
| 48 | + $User = new SaitoUser($readUser['User']); |
|
| 49 | + $status = $User->isForbidden(); |
|
| 50 | + } |
|
| 51 | + |
|
| 52 | + switch ($status) { |
|
| 53 | + case 'locked': |
|
| 54 | + $ends = $this->User->UserBlock |
|
| 55 | + ->getBlockEndsForUser($readUser['User']['id']); |
|
| 56 | + if ($ends) { |
|
| 57 | + $message = __('user.block.pubExpEnds', [$username, |
|
| 58 | + CakeTime::timeAgoInWords($ends, ['accuracy' => 'hour'])]); |
|
| 59 | + } else { |
|
| 60 | + $message = __('user.block.pubExp', $username); |
|
| 61 | + } |
|
| 62 | + break; |
|
| 63 | + case 'unactivated': |
|
| 64 | + $message = __('User %s is not activated yet.', $readUser['User']['username']); |
|
| 65 | + break; |
|
| 66 | + default: |
|
| 67 | + $message = __('auth_loginerror'); |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + // don't autofill password |
|
| 71 | + unset($this->request->data['User']['password']); |
|
| 72 | + |
|
| 73 | + $Logger = new ForbiddenLogger; |
|
| 74 | + $Logger->write("Unsuccessful login for user: $username", |
|
| 75 | + ['msgs' => [$message]]); |
|
| 76 | + |
|
| 77 | + $this->Session->setFlash($message, 'default', [], 'auth'); |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + public function logout() { |
|
| 81 | + $this->CurrentUser->logout(); |
|
| 82 | + } |
|
| 83 | + |
|
| 84 | + public function register() { |
|
| 85 | + $this->set('status', 'view'); |
|
| 86 | + |
|
| 87 | + $this->CurrentUser->logout(); |
|
| 88 | + |
|
| 89 | + $tosRequired = Configure::read('Saito.Settings.tos_enabled'); |
|
| 90 | + $this->set(compact('tosRequired')); |
|
| 91 | + |
|
| 92 | + // display empty form |
|
| 93 | + if (empty($this->request->data)) { |
|
| 94 | + return; |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + $data = $this->request->data; |
|
| 98 | + |
|
| 99 | + if (!$tosRequired) { |
|
| 100 | + $data['User']['tos_confirm'] = true; |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + $tosConfirmed = $data['User']['tos_confirm']; |
|
| 104 | + if (!$tosConfirmed) { |
|
| 105 | + return; |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + $data = $this->_passwordAuthSwitch($data); |
|
| 109 | + $this->User->Behaviors->attach('SimpleCaptcha.SimpleCaptcha'); |
|
| 110 | + $user = $this->User->register($data); |
|
| 111 | + |
|
| 112 | + // registering failed, show form again |
|
| 113 | + if (!$user) { |
|
| 114 | + // undo the passwordAuthSwitch() to display error message for the field |
|
| 115 | + if (isset($this->User->validationErrors['password'])) { |
|
| 116 | + $this->User->validationErrors['user_password'] = $this->User->validationErrors['password']; |
|
| 117 | + } |
|
| 118 | + $data['User']['tos_confirm'] = false; |
|
| 119 | + $this->request->data = $data; |
|
| 120 | + return; |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + // registered successfully |
|
| 124 | + try { |
|
| 125 | + $forumName = Configure::read('Saito.Settings.forum_name'); |
|
| 126 | + $subject = __('register_email_subject', $forumName); |
|
| 127 | + $email = $this->SaitoEmail->email([ |
|
| 128 | + 'recipient' => $data, |
|
| 129 | + 'subject' => $subject, |
|
| 130 | + 'sender' => 'register', |
|
| 131 | + 'template' => 'user_register', |
|
| 132 | + 'viewVars' => ['user' => $user] |
|
| 133 | + ]); |
|
| 134 | + // only used in test cases |
|
| 135 | + $this->set('email', $email); |
|
| 136 | + } catch (Exception $e) { |
|
| 137 | + $Logger = new ExceptionLogger(); |
|
| 138 | + $Logger->write('Registering email confirmation failed', ['e' => $e]); |
|
| 139 | + $this->set('status', 'fail: email'); |
|
| 140 | + return; |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + $this->set('status', 'success'); |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + /** |
|
| 147 | + * register success (user clicked link in confirm mail) |
|
| 148 | + * |
|
| 149 | + * @param $id |
|
| 150 | + * @throws BadRequestException |
|
| 151 | + */ |
|
| 152 | + public function rs($id = null) { |
|
| 153 | + if (!$id) { |
|
| 154 | + throw new BadRequestException(); |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + $code = $this->request->query('c'); |
|
| 158 | + |
|
| 159 | + try { |
|
| 160 | + $activated = $this->User->activate((int)$id, $code); |
|
| 161 | + } catch (Exception $e) { |
|
| 162 | + $activated = false; |
|
| 163 | + } |
|
| 164 | + |
|
| 165 | + if (!$activated) { |
|
| 166 | + $activated = ['status' => 'fail']; |
|
| 167 | + } |
|
| 168 | + $this->set('status', $activated['status']); |
|
| 169 | + } |
|
| 170 | + |
|
| 171 | + public function admin_index() { |
|
| 172 | + $data = $this->User->find( |
|
| 173 | + 'all', |
|
| 174 | + [ |
|
| 175 | + 'contain' => false, |
|
| 176 | + 'fields' => [ |
|
| 177 | + 'id', |
|
| 178 | + 'username', |
|
| 179 | + 'user_type', |
|
| 180 | + 'user_email', |
|
| 181 | + 'registered', |
|
| 182 | + 'user_lock' |
|
| 183 | + ], |
|
| 184 | + 'order' => ['User.username' => 'asc'] |
|
| 185 | + ] |
|
| 186 | + ); |
|
| 187 | + $this->set('users', $data); |
|
| 188 | + } |
|
| 189 | + |
|
| 190 | + public function index() { |
|
| 191 | + $menuItems = [ |
|
| 192 | + 'username' => [__('username_marking'), []], |
|
| 193 | + 'user_type' => [__('user_type'), []], |
|
| 194 | + 'UserOnline.logged_in' => [__('userlist_online'), ['direction' => 'desc']], |
|
| 195 | + 'registered' => [__('registered'), ['direction' => 'desc']] |
|
| 196 | + ]; |
|
| 197 | + $showBlocked = Configure::read('Saito.Settings.block_user_ui'); |
|
| 198 | + if ($showBlocked) { |
|
| 199 | + $menuItems['user_lock'] = [__('user.set.lock.t'), ['direction' => 'desc']]; |
|
| 200 | + } |
|
| 201 | + |
|
| 202 | + $this->paginate = [ |
|
| 203 | + 'contain' => 'UserOnline', |
|
| 204 | + 'limit' => 400, |
|
| 205 | + 'order' => ['UserOnline.logged_in' => 'desc', 'User.username' => 'asc'] |
|
| 206 | + ]; |
|
| 207 | + $users = $this->paginate('User', null, array_keys($menuItems)); |
|
| 208 | + |
|
| 209 | + $this->set(compact('menuItems', 'users')); |
|
| 210 | + } |
|
| 211 | + |
|
| 212 | + public function ignore() { |
|
| 213 | + $this->request->allowMethod('POST'); |
|
| 214 | + $blockedId = $this->request->data('id'); |
|
| 215 | + $this->_ignore($blockedId, true); |
|
| 216 | + } |
|
| 217 | + |
|
| 218 | + public function unignore() { |
|
| 219 | + $this->request->allowMethod('POST'); |
|
| 220 | + $blockedId = $this->request->data('id'); |
|
| 221 | + $this->_ignore($blockedId, false); |
|
| 222 | + } |
|
| 223 | + |
|
| 224 | + protected function _ignore($blockedId, $set) { |
|
| 225 | + if (!$this->CurrentUser->isLoggedIn() || !is_numeric($blockedId)) { |
|
| 226 | + throw new BadRequestException(); |
|
| 227 | + } |
|
| 228 | + $userId = $this->CurrentUser->getId(); |
|
| 229 | + $this->User->id = $userId; |
|
| 230 | + if (!$this->User->exists($userId) || $userId == $blockedId) { |
|
| 231 | + throw new BadRequestException(); |
|
| 232 | + } |
|
| 233 | + if ($set) { |
|
| 234 | + $this->User->Ignore->ignore($userId, $blockedId); |
|
| 235 | + } else { |
|
| 236 | + $this->User->Ignore->unignore($userId, $blockedId); |
|
| 237 | + } |
|
| 238 | + $this->redirect($this->referer()); |
|
| 239 | + } |
|
| 240 | + |
|
| 241 | + public function admin_add() { |
|
| 242 | + if (!empty($this->request->data)) : |
|
| 243 | + $this->request->data = $this->_passwordAuthSwitch($this->request->data); |
|
| 244 | + if ($this->User->register($this->request->data, true)) { |
|
| 245 | + $this->Session->setFlash(__('user.admin.add.success'), |
|
| 246 | + 'flash/success'); |
|
| 247 | + $this->redirect(['action' => 'view', $this->User->id, 'admin' => false]); |
|
| 248 | + } |
|
| 249 | + endif; |
|
| 250 | + } |
|
| 251 | + |
|
| 252 | + public function map() { |
|
| 253 | + if (!Configure::read('Saito.Settings.map_enabled')) { |
|
| 254 | + $this->Session->setFlash(__('admin.setting.disabled', __('admin.feat.map')), 'flash/error'); |
|
| 255 | + $this->redirect('/'); |
|
| 256 | + return; |
|
| 257 | + } |
|
| 258 | + $users = $this->User->find('all', |
|
| 259 | + [ |
|
| 260 | + 'contain' => false, |
|
| 261 | + 'conditions' => ['user_place_lat !=' => null], |
|
| 262 | + 'fields' => [ |
|
| 263 | + 'User.id', |
|
| 264 | + 'User.username', |
|
| 265 | + 'User.user_place_lat', |
|
| 266 | + 'User.user_place_lng' |
|
| 267 | + ] |
|
| 268 | + ] |
|
| 269 | + ); |
|
| 270 | + $this->set(compact('users')); |
|
| 271 | + } |
|
| 272 | + |
|
| 273 | + public function name($id = null) { |
|
| 274 | + if (!empty($id)) { |
|
| 275 | + $this->User->contain(); |
|
| 276 | + $viewedUser = $this->User->findByUsername($id); |
|
| 277 | + if (!empty($viewedUser)) { |
|
| 278 | + $this->redirect( |
|
| 279 | + [ |
|
| 280 | + 'controller' => 'users', |
|
| 281 | + 'action' => 'view', |
|
| 282 | + $viewedUser['User']['id'] |
|
| 283 | + ] |
|
| 284 | + ); |
|
| 285 | + return; |
|
| 286 | + } |
|
| 287 | + } |
|
| 288 | + $this->Session->setFlash(__('Invalid user'), 'flash/error'); |
|
| 289 | + $this->redirect('/'); |
|
| 290 | + } |
|
| 291 | + |
|
| 292 | + public function view($id = null) { |
|
| 293 | + // redirect view/<username> to name/<username> |
|
| 294 | + if (!empty($id) && !is_numeric($id)) { |
|
| 295 | + $this->redirect( |
|
| 296 | + [ |
|
| 297 | + 'controller' => 'users', |
|
| 298 | + 'action' => 'name', |
|
| 299 | + $id |
|
| 300 | + ] |
|
| 301 | + ); |
|
| 302 | + return; // test case return |
|
| 303 | + } |
|
| 304 | + |
|
| 305 | + $this->User->id = $id; |
|
| 306 | + $this->User->contain(['UserBlock' => ['By'], 'UserOnline']); |
|
| 307 | + $viewedUser = $this->User->read(); |
|
| 308 | + |
|
| 309 | + if ($id === null || empty($viewedUser)) { |
|
| 310 | + $this->Session->setFlash(__('Invalid user'), 'flash/error'); |
|
| 311 | + $this->redirect('/'); |
|
| 312 | + return; |
|
| 313 | + } |
|
| 314 | + |
|
| 315 | + $viewedUser['User']['number_of_entries'] = $this->User->numberOfEntries(); |
|
| 316 | + |
|
| 317 | + $entriesShownOnPage = 20; |
|
| 318 | + $this->set( |
|
| 319 | + 'lastEntries', |
|
| 320 | + $this->User->Entry->getRecentEntries( |
|
| 321 | + $this->CurrentUser, |
|
| 322 | + [ |
|
| 323 | + 'user_id' => $this->User->id, |
|
| 324 | + 'limit' => $entriesShownOnPage |
|
| 325 | + ] |
|
| 326 | + ) |
|
| 327 | + ); |
|
| 328 | + |
|
| 329 | + $this->set( |
|
| 330 | + 'hasMoreEntriesThanShownOnPage', |
|
| 331 | + ($viewedUser['User']['number_of_entries'] - $entriesShownOnPage) > 0 |
|
| 332 | + ); |
|
| 333 | + |
|
| 334 | + if ($this->CurrentUser->getId() == $id) { |
|
| 335 | + $viewedUser['User']['ignores'] = $this->User->Ignore->ignoredBy($id); |
|
| 336 | + } |
|
| 337 | + $viewedUser['User']['solves_count'] = $this->User->countSolved($id); |
|
| 338 | + $this->set('user', $viewedUser); |
|
| 339 | + $this->set( |
|
| 340 | + 'title_for_layout', |
|
| 341 | + $viewedUser['User']['username'] |
|
| 342 | + ); |
|
| 343 | + } |
|
| 344 | + |
|
| 345 | + /** |
|
| 346 | + * @param null $id |
|
| 347 | + * @throws Saito\Exception\SaitoForbiddenException |
|
| 348 | + * @throws BadRequestException |
|
| 349 | + */ |
|
| 350 | + public function edit($id = null) { |
|
| 351 | + if (!$id) { |
|
| 352 | + throw new BadRequestException; |
|
| 353 | + } |
|
| 354 | + if (!$this->_isEditingAllowed($this->CurrentUser, $id)) { |
|
| 355 | + throw new \Saito\Exception\SaitoForbiddenException("Attempt to edit user $id.", [ |
|
| 356 | + 'CurrentUser' => $this->CurrentUser |
|
| 357 | + ]); |
|
| 358 | + } |
|
| 359 | + |
|
| 360 | + $this->set('userId', $id); |
|
| 361 | + |
|
| 362 | + // try to save entry |
|
| 363 | + if (!empty($this->request->data)) { |
|
| 364 | + $data = $this->request->data['User']; |
|
| 365 | + |
|
| 366 | + unset($data['id']); |
|
| 367 | + //# make sure only admin can edit these fields |
|
| 368 | + if ($this->CurrentUser['user_type'] !== 'admin') { |
|
| 369 | + // @todo DRY: refactor this admin fields together with view |
|
| 370 | + unset($data['username'], $data['user_email'], $data['user_type']); |
|
| 371 | + } |
|
| 372 | + |
|
| 373 | + $this->User->id = $id; |
|
| 374 | + $success = $this->User->save($data); |
|
| 375 | + if ($success) { |
|
| 376 | + $this->redirect(['action' => 'view', $id]); |
|
| 377 | + return; |
|
| 378 | + } else { |
|
| 379 | + // if empty fields are missing from send form read user again |
|
| 380 | + $this->User->contain(); |
|
| 381 | + $user = $this->User->read(); |
|
| 382 | + $this->request->data['User'] = array_merge($user['User'], |
|
| 383 | + $this->request->data['User']); |
|
| 384 | + |
|
| 385 | + $this->User->set($this->request->data); |
|
| 386 | + $this->User->validates(); |
|
| 387 | + |
|
| 388 | + $this->JsData->addAppJsMessage( |
|
| 389 | + __('The user could not be saved. Please, try again.'), |
|
| 390 | + ['type' => 'error']); |
|
| 391 | + } |
|
| 392 | + } |
|
| 393 | + |
|
| 394 | + if (empty($this->request->data)) { |
|
| 395 | + //* View Entry by id |
|
| 396 | + $this->User->id = $id; |
|
| 397 | + $this->User->contain('UserOnline'); |
|
| 398 | + $this->request->data = $this->User->read(); |
|
| 399 | + } |
|
| 400 | + |
|
| 401 | + $themes = $this->Themes->getAvailable(); |
|
| 402 | + $this->set('availableThemes', array_combine($themes, $themes)); |
|
| 403 | + $this->set('user', $this->request->data); |
|
| 404 | + $this->set( |
|
| 405 | + 'title_for_layout', |
|
| 406 | + __('Edit %s Profil', |
|
| 407 | + Saito\String\Properize::prop($this->request->data['User']['username'])) |
|
| 408 | + ); |
|
| 409 | + } |
|
| 410 | + |
|
| 411 | + public function admin_block() { |
|
| 412 | + $this->set('UserBlock', $this->User->UserBlock->getAll()); |
|
| 413 | + } |
|
| 414 | + |
|
| 415 | + /** |
|
| 416 | + * @throws BadRequestException |
|
| 417 | + */ |
|
| 418 | + public function lock() { |
|
| 419 | + if (!($this->CurrentUser->isAdmin() || $this->viewVars['modLocking'])) { |
|
| 420 | + $this->redirect('/'); |
|
| 421 | + return; |
|
| 422 | + } |
|
| 423 | + |
|
| 424 | + $id = (int)$this->request->data('User.lockUserId'); |
|
| 425 | + if (!$id) { |
|
| 426 | + throw new BadRequestException; |
|
| 427 | + } |
|
| 428 | + |
|
| 429 | + $this->User->contain(); |
|
| 430 | + $readUser = $this->User->findById($id); |
|
| 431 | + if (!$readUser) { |
|
| 432 | + $this->Session->setFlash(__('User not found.'), 'flash/error'); |
|
| 433 | + $this->redirect('/'); |
|
| 434 | + return; |
|
| 435 | + } |
|
| 436 | + |
|
| 437 | + $editedUser = new SaitoUser($readUser['User']); |
|
| 438 | + |
|
| 439 | + if ($id == $this->CurrentUser->getId()) { |
|
| 440 | + $this->Session->setFlash(__("You can't lock yourself."), 'flash/error'); |
|
| 441 | + } elseif ($editedUser->isAdmin()) { |
|
| 442 | + $this->Session->setFlash( |
|
| 443 | + __("You can't lock administrators.", 'flash/error'), |
|
| 444 | + 'flash/error' |
|
| 445 | + ); |
|
| 446 | + } else { |
|
| 447 | + try { |
|
| 448 | + $duration = (int)$this->request->data('User.lockPeriod'); |
|
| 449 | + $status = $this->User->UserBlock->block( |
|
| 450 | + new \Saito\User\Blocker\ManualBlocker, |
|
| 451 | + $id, |
|
| 452 | + [ |
|
| 453 | + 'adminId' => $this->CurrentUser->getId(), |
|
| 454 | + 'duration' => $duration |
|
| 455 | + ] |
|
| 456 | + ); |
|
| 457 | + $username = $readUser['User']['username']; |
|
| 458 | + if ($status === true) { |
|
| 459 | + $message = __('User %s is locked.', $username); |
|
| 460 | + } else { |
|
| 461 | + $message = __('User %s is unlocked.', $username); |
|
| 462 | + } |
|
| 463 | + $this->Session->setFlash($message, 'flash/success'); |
|
| 464 | + } catch (Exception $e) { |
|
| 465 | + $this->Session->setFlash(__('Error while un/locking.'), 'flash/error'); |
|
| 466 | + } |
|
| 467 | + } |
|
| 468 | + $this->redirect($this->referer()); |
|
| 469 | + } |
|
| 470 | + |
|
| 471 | + public function unlock($id) { |
|
| 472 | + if (!$id || !($this->CurrentUser->isAdmin() || $this->viewVars['modLocking'])) { |
|
| 473 | + throw new BadRequestException; |
|
| 474 | + } |
|
| 475 | + if (!$this->User->UserBlock->unblock($id)) { |
|
| 476 | + $this->Session->setFlash(__('Error while unlocking.'), 'flash/error'); |
|
| 477 | + } |
|
| 478 | + $this->redirect($this->referer()); |
|
| 479 | + } |
|
| 480 | + |
|
| 481 | + public function admin_delete($id = null) { |
|
| 482 | + $this->User->contain(); |
|
| 483 | + $readUser = $this->User->findById($id); |
|
| 484 | + if (!$readUser) { |
|
| 485 | + $this->Session->setFlash(__('User not found.'), 'flash/error'); |
|
| 486 | + $this->redirect('/'); |
|
| 487 | + return; |
|
| 488 | + } |
|
| 489 | + |
|
| 490 | + if (isset($this->request->data['User']['modeDelete'])) { |
|
| 491 | + if ($id == $this->CurrentUser->getId()) { |
|
| 492 | + $this->Session->setFlash(__("You can't delete yourself."), 'flash/error'); |
|
| 493 | + } elseif ($id == 1) { |
|
| 494 | + $this->Session->setFlash(__("You can't delete the installation account."), 'flash/error'); |
|
| 495 | + } elseif ($this->User->deleteAllExceptEntries($id)) { |
|
| 496 | + $this->Session->setFlash(__('User %s deleted.', $readUser['User']['username']), 'flash/success'); |
|
| 497 | + $this->redirect('/'); |
|
| 498 | + return; |
|
| 499 | + } else { |
|
| 500 | + $this->Session->setFlash(__("Couldn't delete user."), 'flash/error'); |
|
| 501 | + } |
|
| 502 | + $this->redirect(['controller' => 'users', 'action' => 'view', $id]); |
|
| 503 | + return; |
|
| 504 | + } |
|
| 505 | + $this->set('user', $readUser); |
|
| 506 | + } |
|
| 507 | + |
|
| 508 | + /** |
|
| 509 | + * changes user password |
|
| 510 | + * |
|
| 511 | + * @param null $id |
|
| 512 | + * @throws \Saito\Exception\SaitoForbiddenException |
|
| 513 | + * @throws BadRequestException |
|
| 514 | + */ |
|
| 515 | + public function changepassword($id = null) { |
|
| 516 | + if (!$id) { |
|
| 517 | + throw new BadRequestException(); |
|
| 518 | + } |
|
| 519 | + |
|
| 520 | + $user = $this->User->getProfile($id); |
|
| 521 | + $allowed = $this->_isEditingAllowed($this->CurrentUser, $id); |
|
| 522 | + if (empty($user) || !$allowed) { |
|
| 523 | + throw new SaitoForbiddenException("Attempt to change password for user $id.", |
|
| 524 | + ['CurrentUser' => $this->CurrentUser]); |
|
| 525 | + } |
|
| 526 | + $this->set('userId', $id); |
|
| 527 | + $this->set('username', $user['username']); |
|
| 528 | + |
|
| 529 | + //# just show empty form |
|
| 530 | + if (empty($this->request->data)) { |
|
| 531 | + return; |
|
| 532 | + } |
|
| 533 | + |
|
| 534 | + //# process submitted form |
|
| 535 | + $this->request->data = $this->_passwordAuthSwitch($this->request->data); |
|
| 536 | + $data = [ |
|
| 537 | + 'id' => $id, |
|
| 538 | + 'password_old' => $this->request->data['User']['password_old'], |
|
| 539 | + 'password' => $this->request->data['User']['password'], |
|
| 540 | + 'password_confirm' => $this->request->data['User']['password_confirm'] |
|
| 541 | + ]; |
|
| 542 | + $success = $this->User->save($data); |
|
| 543 | + |
|
| 544 | + if ($success) { |
|
| 545 | + $this->Session->setFlash(__('change_password_success'), |
|
| 546 | + 'flash/success'); |
|
| 547 | + $this->redirect(['controller' => 'users', 'action' => 'edit', $id]); |
|
| 548 | + return; |
|
| 549 | + } |
|
| 550 | + |
|
| 551 | + $this->Session->setFlash( |
|
| 552 | + __d('nondynamic', current(array_pop($this->User->validationErrors))), |
|
| 553 | + 'flash/error' |
|
| 554 | + ); |
|
| 555 | + |
|
| 556 | + // unset all autofill form data |
|
| 557 | + $this->request->data = []; |
|
| 558 | + } |
|
| 559 | + |
|
| 560 | + /** |
|
| 561 | + * @throws BadRequestException |
|
| 562 | + */ |
|
| 563 | + private function __ajaxBeforeFilter() { |
|
| 564 | + if (!$this->request->is('ajax')) { |
|
| 565 | + throw new BadRequestException; |
|
| 566 | + } |
|
| 567 | + $this->autoRender = false; |
|
| 568 | + } |
|
| 569 | + |
|
| 570 | + /** |
|
| 571 | + * toggles slidetabs open/close |
|
| 572 | + * |
|
| 573 | + * @return $this|mixed |
|
| 574 | + * @throws BadRequestException |
|
| 575 | + */ |
|
| 576 | + public function slidetab_toggle() { |
|
| 577 | + $this->__ajaxBeforeFilter(); |
|
| 578 | + |
|
| 579 | + $toggle = $this->request->data('slidetabKey'); |
|
| 580 | + $allowed = [ |
|
| 581 | + 'show_userlist', |
|
| 582 | + 'show_recentposts', |
|
| 583 | + 'show_recententries', |
|
| 584 | + 'show_shoutbox' |
|
| 585 | + ]; |
|
| 586 | + if (!$toggle || !in_array($toggle, $allowed)) { |
|
| 587 | + throw new BadRequestException(null, 1412949882); |
|
| 588 | + } |
|
| 589 | + |
|
| 590 | + $this->User->id = $this->CurrentUser->getId(); |
|
| 591 | + $newValue = $this->User->toggle($toggle); |
|
| 592 | + $this->CurrentUser[$toggle] = $newValue; |
|
| 593 | + return $toggle; |
|
| 594 | + } |
|
| 595 | + |
|
| 596 | + /** |
|
| 597 | + * sets slidetab-order |
|
| 598 | + * |
|
| 599 | + * @return bool |
|
| 600 | + * @throws BadRequestException |
|
| 601 | + */ |
|
| 602 | + public function slidetab_order() { |
|
| 603 | + $this->__ajaxBeforeFilter(); |
|
| 604 | + |
|
| 605 | + $order = $this->request->data('slidetabOrder'); |
|
| 606 | + if (!$order) { |
|
| 607 | + throw new BadRequestException; |
|
| 608 | + } |
|
| 609 | + |
|
| 610 | + $allowed = $this->viewVars['slidetabs']; |
|
| 611 | + $order = array_filter( $order, function ($item) use ($allowed) { |
|
| 612 | + return in_array($item, $allowed); |
|
| 613 | + }); |
|
| 614 | + $order = serialize($order); |
|
| 615 | + |
|
| 616 | + $this->User->id = $this->CurrentUser->getId(); |
|
| 617 | + $this->User->saveField('slidetab_order', $order); |
|
| 618 | + $this->CurrentUser['slidetab_order'] = $order; |
|
| 619 | + |
|
| 620 | + return true; |
|
| 621 | + } |
|
| 622 | + |
|
| 623 | + /** |
|
| 624 | + * @param null $id |
|
| 625 | + * |
|
| 626 | + * @throws ForbiddenException |
|
| 627 | + */ |
|
| 628 | + public function setcategory($id = null) { |
|
| 629 | + if (!$this->CurrentUser->isLoggedIn()) { |
|
| 630 | + throw new ForbiddenException(); |
|
| 631 | + } |
|
| 632 | + $this->User->id = $this->CurrentUser->getId(); |
|
| 633 | + if ($id === 'all') { |
|
| 634 | + $this->User->setCategory('all'); |
|
| 635 | + } elseif (!$id && $this->request->data) { |
|
| 636 | + $this->User->setCategory($this->request->data['CatChooser']); |
|
| 637 | + } else { |
|
| 638 | + $this->User->setCategory($id); |
|
| 639 | + } |
|
| 640 | + $this->redirect($this->referer()); |
|
| 641 | + } |
|
| 642 | + |
|
| 643 | + public function beforeFilter() { |
|
| 644 | + Stopwatch::start('Users->beforeFilter()'); |
|
| 645 | + parent::beforeFilter(); |
|
| 646 | + |
|
| 647 | + // @todo CSRF protection |
|
| 648 | + $this->Security->unlockedActions[] = 'slidetab_toggle'; |
|
| 649 | + $this->Security->unlockedActions[] = 'slidetab_order'; |
|
| 650 | + |
|
| 651 | + $this->Auth->allow('login', 'register', 'rs'); |
|
| 652 | + $this->set('modLocking', |
|
| 653 | + $this->CurrentUser->isMod() && Configure::read('Saito.Settings.block_user_ui') |
|
| 654 | + ); |
|
| 655 | + |
|
| 656 | + Stopwatch::stop('Users->beforeFilter()'); |
|
| 657 | + } |
|
| 658 | + |
|
| 659 | + /** |
|
| 660 | + * Checks if the current user is allowed to edit user $userId |
|
| 661 | + * |
|
| 662 | + * @param SaitoUser $CurrentUser |
|
| 663 | + * @param int $userId |
|
| 664 | + * @return type |
|
| 665 | + */ |
|
| 666 | + protected function _isEditingAllowed(\Saito\User\ForumsUserInterface $CurrentUser, $userId) { |
|
| 667 | + if ($CurrentUser->isAdmin()) { |
|
| 668 | + return true; |
|
| 669 | + } |
|
| 670 | + return $CurrentUser->getId() === (int)$userId; |
|
| 671 | + } |
|
| 672 | + |
|
| 673 | + protected function _passwordAuthSwitch($data) { |
|
| 674 | + $data['User']['password'] = $data['User']['user_password']; |
|
| 675 | + unset($data['User']['user_password']); |
|
| 676 | + return $data; |
|
| 677 | + } |
|
| 678 | + |
|
| 679 | + } |
|