PageRenderTime 50ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/app/controllers/users_controller.php

http://github.com/Datawalke/Coordino
PHP | 569 lines | 459 code | 64 blank | 46 comment | 58 complexity | 1c3187a7fc0a83374160ecbd3e8ea4f7 MD5 | raw file
  1. <?php
  2. class UsersController extends AppController {
  3. var $name = 'Users';
  4. var $uses = array('User', 'Post', 'History', 'Setting', 'Widget');
  5. var $components = array('Auth', 'Session', 'Cookie', 'Email');
  6. var $helpers = array('Time', 'Html', 'Form', 'Javascript', 'Number', 'Thumbnail', 'TrickyFileInput', 'Session');
  7. var $allowedTypes = array(
  8. 'image/jpeg',
  9. 'image/gif',
  10. 'image/png',
  11. 'image/pjpeg',
  12. 'image/x-png'
  13. );
  14. public function beforeRender() {
  15. $this->getWidgets();
  16. $this->underMaintenance();
  17. }
  18. public function beforeFilter() {
  19. parent::beforeFilter();
  20. $this->Auth->fields = array(
  21. 'username' => 'email',
  22. 'password' => 'password'
  23. );
  24. $this->getWidgets();
  25. $this->isAdmin();
  26. $this->Auth->allow('view', 'register', 'userbar', 'remoteLogin', 'users', 'mini_user_search', 'lost_password',
  27. 'userList', 'miniSearch');
  28. }
  29. public function login() {
  30. if($this->Setting->getValue('remote_auth_only') == 'yes') {
  31. $this->redirect($this->Setting->getValue('remote_auth_login_url'));
  32. }
  33. }
  34. // public function edit($id = null) {
  35. // if ($this->Auth->user('id') == $this->User->findById($id)) {
  36. // $this->User->id = $id;
  37. // if (empty($this->data)) {
  38. // $this->data = $this->User->read();
  39. // } else {
  40. // if ($this->User->save($this->data)) {
  41. // $this->Session->setFlash('Your user information has been updated.');
  42. // }
  43. // }
  44. // }
  45. // $this->redirect(array('action' => 'index'));
  46. // }
  47. public function lost_password() {
  48. if(!empty($this->data)) {
  49. $email_exists = $this->User->find(
  50. 'first', array(
  51. 'conditions' => array(
  52. 'email' => $this->data['User']['email']
  53. )
  54. )
  55. );
  56. if(!empty($email_exists)) {
  57. $pass = rand(8, 12);
  58. $this->data['User']['password'] = $this->Auth->password($pass);
  59. $this->data['User']['id'] = $email_exists['User']['id'];
  60. $this->User->save($this->data);
  61. $this->set('user', $email_exists);
  62. $this->set('password', $pass);
  63. $this->Email->from = 'Engine Juice <sam@bravegamer.com>';
  64. $this->Email->to = $email_exists['User']['email'];
  65. $this->Email->subject = 'Engine Juice password recovery.';
  66. $this->Email->template = 'recovery';
  67. $this->Email->sendAs = 'both';
  68. $this->Email->send();
  69. $this->Session->setFlash('Go check your email!', 'error');
  70. }else {
  71. $this->Session->setFlash('No user has that email address.', 'error');
  72. }
  73. $this->redirect('/login');
  74. }
  75. }
  76. public function logout(){
  77. if($this->Setting->getValue('remote_auth_only') == 'yes') {
  78. $this->Auth->logout();
  79. $this->redirect($this->Setting->getValue('remote_auth_logout_url'));
  80. }
  81. $this->redirect($this->Auth->logout());
  82. }
  83. public function view($public_key) {
  84. $user = $this->User->findByPublicKey($public_key);
  85. $this->pageTitle = $user['User']['username'] . '\'s Profile';
  86. $this->set('user', $user);
  87. $this->set('recent', $this->History->retrieve($user['User']['id']));
  88. }
  89. public function user_settings($public_key) {
  90. if($this->Auth->user('public_key') != $public_key) {
  91. $this->Session->setFlash('Those are not your settings to change.', 'error');
  92. $this->redirect('/');
  93. }
  94. $user = $this->User->find(
  95. 'first', array(
  96. 'conditions' => array(
  97. 'public_key' => $public_key
  98. )
  99. )
  100. );
  101. if(empty($this->data)) {
  102. $this->set('user_info', $user);
  103. }else {
  104. $this->set('user_info', $user);
  105. if($this->Auth->password($this->data['User']['old_password']) == $user['User']['password']) {
  106. $this->data['User']['password'] = $this->Auth->password($this->data['User']['new_password']);
  107. $this->data['User']['id'] = $user['User']['id'];
  108. $this->User->save($this->data);
  109. $this->Session->setFlash('Settings updated!', 'error');
  110. }elseif(empty($this->data['User']['old_password'])) {
  111. unset($this->data['old_password']);
  112. unset($this->data['new_password']);
  113. $this->data['User']['id'] = $user['User']['id'];
  114. $this->User->save($this->data);
  115. $this->Session->setFlash('Settings updated, except password.', 'error');
  116. }else {
  117. $this->Session->setFlash('Old Password incorrect. Settings remain unchanged.', 'error');
  118. $this->redirect('/users/settings/' . $public_key);
  119. }
  120. }
  121. }
  122. /**
  123. * Logs in the user via a remote method.
  124. *
  125. * @param string $name
  126. * @param string $email
  127. * @param string $hash md5($name . $email . $api_key)
  128. * @return void
  129. */
  130. public function remoteLogin($name, $email, $timestamp, $hash) {
  131. $serverHash = md5($name . $email . $timestamp . $this->Setting->getValue('remote_auth_key'));
  132. if($serverHash != $hash) {
  133. $this->Session->setFlash('Invalid name, email, timestamp, or authentication key. Please check your conditions and try again.', 'error');
  134. $this->redirect('/');
  135. }
  136. if((time() - $timestamp) > 1800) {
  137. $this->Session->setFlash('The provided timestamp is too old. Please try again.', 'error');
  138. $this->redirect('/');
  139. }
  140. $account = $this->User->findByEmail($email);
  141. if(!empty($account)) {
  142. $this->Auth->login($account);
  143. } else {
  144. $data['User']['username'] = $name;
  145. $data['User']['email'] = $email;
  146. $data['User']['registered'] = 1;
  147. $this->Auth->login($this->__userSave($data));
  148. }
  149. $this->redirect('/');
  150. }
  151. public function __userSave($data) {
  152. $data['User']['public_key'] = uniqid();
  153. $data['User']['password'] = $this->Auth->password(uniqid('p'));
  154. $data['User']['joined'] = time();
  155. $data['User']['ip'] = $_SERVER['REMOTE_ADDR'];
  156. $data['User']['url_title'] = $this->Post->niceUrl($data['User']['username']);
  157. /**
  158. * Set up cookie data incase they leave the site and the session ends and they have not registered yet
  159. */
  160. $this->Cookie->write(array('User' => $data['User']));
  161. /**
  162. * Save the data
  163. */
  164. $this->User->save($data);
  165. $data['User']['id'] = $this->User->id;
  166. return $data;
  167. }
  168. public function userList() {
  169. }
  170. public function register() {
  171. $this->pageTitle = 'Register';
  172. if($this->Session->read('Auth.User.registered') == 1) {
  173. $this->Session->setFlash('You are already registered.');
  174. $this->redirect('/');
  175. }
  176. /**
  177. * If the user has an unregistered account update the password and set them to registered.
  178. */
  179. if(!empty($this->data)) {
  180. /**
  181. * If the user is logged in via Session or Cookie
  182. */
  183. if($this->Auth->user('id')) {
  184. $user = $this->User->read(null, $this->Auth->user('id'));
  185. $user['User']['password'] = $this->Auth->password($this->data['User']['secret']);
  186. $user['User']['registered'] = '1';
  187. /**
  188. * Save the user information.
  189. */
  190. if($this->User->save($user)) {
  191. /**
  192. * Push the new registered state to the session.
  193. */
  194. $this->Session->write('Auth.User.registered', 1);
  195. $this->Session->setFlash('You have been registered! Welcome to the community.');
  196. $this->redirect('/users/' . $this->Auth->user('public_key') . '/' . $this->Auth->user('url_title'));
  197. } else {
  198. $this->Session->setFlash('There was an error with your request.');
  199. }
  200. } else {
  201. /**
  202. * Register a new user
  203. */
  204. $this->data['User']['password'] = $this->Auth->password($this->data['User']['secret']);
  205. $this->data['User']['registered'] = '1';
  206. $this->data['User']['public_key'] = uniqid();
  207. $this->data['User']['joined'] = time();
  208. $this->data['User']['url_title'] = $this->Post->niceUrl($this->data['User']['username']);
  209. if($this->User->save($this->data)) {
  210. $this->Auth->login($this->data);
  211. $this->redirect('/');
  212. }
  213. }
  214. }
  215. }
  216. public function admin() {
  217. $this->pageTitle = 'Settings';
  218. if(!$this->Auth->user('id')) {
  219. $this->Session->setFlash('You must be logged in to do that.', 'error');
  220. $this->redirect('/login');
  221. }
  222. if($this->User->adminCheck($this->Auth->user('id'), 'update')) {
  223. $this->Session->setFlash('You are not allowed to do that.', 'error');
  224. $this->redirect('/');
  225. }
  226. $settings = $this->Setting->find(
  227. 'all', array(
  228. 'conditions' => array(
  229. 'OR' => array(
  230. 'name' => array('rep_vote_up', 'rep_comment', 'rep_vote_down',
  231. 'rep_advertising', 'rep_edit', 'rep_flag',
  232. 'flag_display_limit')
  233. )
  234. )
  235. )
  236. );
  237. $this->set('settings', $settings);
  238. if($this->data) {
  239. foreach($this->data['Setting'] as $key => $value) {
  240. $data = array(
  241. 'id' => $key + 1,
  242. 'value' => $this->data['Setting'][$key]['value']
  243. );
  244. $this->Setting->save($data);
  245. $count = count($this->data);
  246. }
  247. $this->Session->setFlash('Settings updated.', 'error');
  248. $this->redirect('/admin');
  249. }
  250. }
  251. public function flagged() {
  252. $this->pageTitle = 'Flagged Posts';
  253. if(!$this->Auth->user('id')) {
  254. $this->Session->setFlash('You must be logged in to do that.', 'error');
  255. $this->redirect('/login');
  256. }elseif($this->User->adminCheck($this->Auth->user('id'), 'update')) {
  257. $this->Session->setFlash('You are not allowed to do that.', 'error');
  258. $this->redirect('/');
  259. }
  260. $setting = $this->Setting->find(
  261. 'first', array(
  262. 'conditions' => array(
  263. 'Setting.name' => 'flag_display_limit'
  264. )
  265. )
  266. );
  267. $posts = $this->Post->find(
  268. 'all', array(
  269. 'conditions' => array(
  270. 'Post.flags >=' => $setting['Setting']['value']
  271. )
  272. )
  273. );
  274. $posts = array_reverse($posts);
  275. $this->set('questions', $posts);
  276. }
  277. public function adminDelete($public_key) {
  278. if(!$this->Auth->user('id')) {
  279. $this->Session->setFlash('You must be logged in.', 'error');
  280. $this->redirect('/login');
  281. }elseif($this->User->adminCheck($this->Auth->user('id'), 'delete')) {
  282. $this->Session->setFlash('You are not allowed to do that.', 'error');
  283. $this->redirect('/');
  284. }
  285. $post_id = $this->Post->find(
  286. 'first', array('conditions' => array('Post.public_key' => $public_key),
  287. 'fields' => array('Post.id'))
  288. );
  289. $this->Post->del($post_id['Post']['id']);
  290. $this->Session->setFlash('Post deleted successfully!', 'error');
  291. $this->redirect('/admin/flagged');
  292. }
  293. public function adminRestore($public_key) {
  294. if(!$this->Auth->user('id')) {
  295. $this->Session->setFlash('You must be logged in to do that.', 'error');
  296. $this->redirect('/login');
  297. }elseif($this->User->adminCheck($this->Auth->user('id'), 'update')) {
  298. $this->Session->setFlash('You are not allowed to do that.', 'error');
  299. $this->redirect('/');
  300. }
  301. $post = $this->Post->find(
  302. 'first', array('conditions' => array('Post.public_key' => $public_key),
  303. 'fields' => array('Post.id'))
  304. );
  305. $restored_post = array(
  306. 'id' => $post['Post']['id'],
  307. 'flags' => 0
  308. );
  309. $this->Post->save($restored_post);
  310. $this->Session->setFlash('Post restored successfully!', 'error');
  311. $this->redirect('/admin/flagged');
  312. }
  313. public function miniSearch($page=null) {
  314. Configure::write('debug', 0);
  315. $this->autoLayout = false;
  316. $users = $this->User->find('all', array(
  317. 'conditions' => array(
  318. "User.username LIKE" => '%' . $_GET['query'] . '%'),
  319. 'fields' => array('User.username', 'User.public_key', 'User.reputation', 'User.image'),
  320. 'order' => 'User.reputation DESC',
  321. 'limit' => 42
  322. ));
  323. $this->set('users', $users);
  324. }
  325. public function admin_list($page=null) {
  326. $this->pageTitle = 'Appoint An Admin';
  327. if($page < 1 || !is_numeric($page) || !isset($page)) {
  328. $page = 1;
  329. }
  330. $users = $this->User->find('all', array('order' => 'username ASC'));
  331. $user_count = count($users);
  332. if(($user_count - ($page * 100)) > 0) {
  333. $this->set('next', $page + 1);
  334. }
  335. if($page >= 2) {
  336. $this->set('previous', $page - 1);
  337. }
  338. if(($user_count % 100) == 0) {
  339. $end_page = $user_count / 100;
  340. }else {
  341. $end_page = floor($user_count / 100) + 1;
  342. }
  343. $loop_fuel = (($page * 100) - 100) - 1;
  344. $this->set('end_page', $end_page);
  345. $this->set('current', $page);
  346. $this->set('users', $users);
  347. $this->set('loop_fuel', $loop_fuel);
  348. }
  349. public function adminPromote($public_key) {
  350. $this->pageTitle = 'Promote a user to Administrator';
  351. if(!$this->Auth->user('id')) {
  352. $this->Session->setFlash('You must be logged in to do that!', 'error');
  353. $this->redirect('/login');
  354. }elseif($this->User->adminCheck($this->Auth->user('id'), 'create')) {
  355. $this->Session->setFlash('You are not allowed to do that.', 'error');
  356. $this->redirect('/');
  357. }
  358. $user = $this->User->find(
  359. 'first', array('conditions' => array('public_key' => $public_key))
  360. );
  361. $permission = serialize(array('create', 'read', 'update', 'delete', 'admin'));
  362. $new_admin = array('id' => $user['User']['id'], 'permission' => $permission);
  363. $this->User->save($new_admin);
  364. $this->Session->setFlash('' . $user['User']['username'] . ' is now an administrator.', 'error');
  365. $this->redirect('/admin/users');
  366. }
  367. public function adminDemote($public_key) {
  368. if(!$this->Auth->user('id')) {
  369. $this->Session->setFlash('You must be logged in to do that!', 'error');
  370. $this->redirect('/login');
  371. }elseif($this->User->adminCheck($this->Auth->user('id'), 'create')) {
  372. $this->Session->setFlash('You are not allowed to do that.', 'error');
  373. $this->redirect('/');
  374. }
  375. $user = $this->User->find(
  376. 'first', array('conditions' => array('public_key' => $public_key))
  377. );
  378. $new_admin = array('id' => $user['User']['id'], 'permission' => '');
  379. $this->User->save($new_admin);
  380. $this->Session->setFlash('' . $user['User']['username'] . ' is no longer an administrator.', 'error');
  381. $this->redirect('/admin/users');
  382. }
  383. public function list_blacklist() {
  384. $this->pageTitle = 'Spam Filter Words';
  385. if($this->User->adminCheck($this->Auth->user('id'), 'update')) {
  386. $this->Session->setFlash('You can\'t do that', 'error');
  387. $this->redirect('/');
  388. }
  389. $this->set('list', $this->Setting->getBlacklist());
  390. }
  391. public function removeWord($word) {
  392. if($this->User->adminCheck($this->Auth->user('id'), 'update')) {
  393. $this->Session->setFlash('You can\'t do that', 'error');
  394. $this->redirect('/');
  395. }
  396. $blacklist = $this->Setting->getBlacklist();
  397. if(in_array($word, $blacklist)) {
  398. $unset_this = array_keys($blacklist, $word);
  399. unset($blacklist[$unset_this['0']]);
  400. $this->Setting->updateBlacklist(array_values($blacklist));
  401. $this->Session->setFlash('Word removed from the blacklist.', 'error');
  402. $this->redirect('/admin/blacklist');
  403. }else {
  404. $this->Session->setFlash('That word isn\'t on the list', 'error');
  405. $this->redirect('/');
  406. }
  407. }
  408. public function add_word() {
  409. $this->pageTitle = 'Add Spam Words';
  410. if($this->User->adminCheck($this->Auth->user('id'), 'update')) {
  411. $this->Session->setFlash('You can\'t do that', 'error');
  412. $this->redirect('/');
  413. }
  414. $blacklist = $this->Setting->getBlacklist();
  415. if(!empty($this->data)) {
  416. $blacklist[] = $this->data['Setting']['word'];
  417. $this->Setting->updateBlacklist($blacklist);
  418. $this->Session->setFlash('Your word is now on the blacklist!', 'error');
  419. $this->redirect('/admin/blacklist');
  420. }
  421. }
  422. public function remote_settings() {
  423. $this->pageTitle = 'Remote Auth Settings';
  424. if($this->User->adminCheck($this->Auth->user('id'), 'update')) {
  425. $this->Session->setFlash('You can\'t do that!', 'error');
  426. $this->redirect('/');
  427. }
  428. $this->set('selected', 'Remote Settings');
  429. $find = $this->Setting->find('all', array('conditions' => array('name LIKE' => 'remote%')));
  430. $this->set('settings', $find);
  431. if(!empty($this->data)) {
  432. $this->Setting->save(array('id' => $find['1']['Setting']['id'], 'value' => $this->data['1']['Setting']['value']));
  433. $this->Setting->save(array('id' => $find['2']['Setting']['id'], 'value' => $this->data['2']['Setting']['value']));
  434. $this->Setting->save(array('id' => $find['0']['Setting']['id'], 'value' => $this->data['0']['Setting']['value']));
  435. $this->Session->setFlash('Settings updated.', 'error');
  436. }
  437. }
  438. public function avatar() {
  439. if(!empty($this->data['Upload']['file'])) {
  440. /* check all image parameters */
  441. $this->__checkImgParams();
  442. $user = $this->User->findById($this->Auth->user('id'));
  443. $uploadPath = WWW_ROOT . 'img/uploads/users/';
  444. $uploadFile = $uploadPath . $this->Auth->user('public_key') . '-' . $this->data['Upload']['file']['name'];
  445. $directory = dir($uploadPath);
  446. if(!empty($user['User']['image'])) {
  447. unlink(WWW_ROOT . $user['User']['image']);
  448. }
  449. $directory->close();
  450. if(move_uploaded_file($this->data['Upload']['file']['tmp_name'], $uploadFile)) {
  451. $user['User']['image'] = '/img/uploads/users/' . $this->Auth->user('public_key') . '-' . $this->data['Upload']['file']['name'];
  452. $this->User->id = $user['User']['id'];
  453. $this->User->save($user);
  454. $this->Session->setFlash('Your profile picture has been set!', 'error');
  455. $this->redirect(Controller::referer('/'));
  456. }
  457. else {
  458. $this->Session->setFlash('Something went wrong uploading your avatar...', 'error');
  459. $this->redirect(Controller::referer('/'));
  460. }
  461. } else {
  462. $this->Session->setFlash('We didn\'t catch that avatar, please try again...', 'error');
  463. $this->redirect(Controller::referer('/'));
  464. }
  465. }
  466. function __checkImgParams() {
  467. /* check file type */
  468. $this->__checkType($this->data['Upload']['file']['type']);
  469. /* check file size */
  470. $this->__checkSize($this->data['Upload']['file']['size']);
  471. /* check image dimensions */
  472. $this->__checkDimensions($this->data['Upload']['file']['tmp_name']);
  473. }
  474. function __checkType($type = null) {
  475. $valid = false;
  476. foreach($this->allowedTypes as $allowedType) {
  477. if(strtolower($type) == strtolower($allowedType)){
  478. $valid = true;
  479. }
  480. }
  481. if(!$valid) {
  482. $this->Session->setFlash('You tried to upload an invalid type! Please upload your pictures in jpeg, gif, or png format!', 'error');
  483. $this->redirect(Controller::referer('/'));
  484. }
  485. }
  486. function __checkSize($size = null) {
  487. if($size > 1024 * 1024 * 2) {
  488. $this->Session->setFlash('You tried to upload an image that was too large! Images must be under 2MB.', 'error');
  489. $this->redirect(Controller::referer('/'));
  490. }
  491. }
  492. function __checkDimensions($filePath) {
  493. $size = getimagesize($filePath);
  494. if(!$size) {
  495. $this->Session->setFlash('We could not check that image\'s size, so we can\'t upload it.', 'error');
  496. $this->redirect(Controller::referer('/'));
  497. }
  498. $error = '';
  499. if($size[0] > 800 || $size[1] > 800) {
  500. $this->Session->setFlash('Images cannot be any larger than 800 by 800 pixels.', 'error');
  501. $this->redirect(Controller::referer('/'));
  502. }
  503. }
  504. }
  505. ?>