UsersController.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. <?php
  2. namespace app\controllers;
  3. use lithium\storage\Session;
  4. use app\models\User;
  5. use app\models\confirmKey;
  6. use app\models\ProfilePic;
  7. use app\models\Post;
  8. use lithium\security\Auth;
  9. use lithium\util\String;
  10. use \MongoDate;
  11. use li3_flash_message\extensions\storage\FlashMessage;
  12. use lithium\template\helper\Html;
  13. class UsersController extends \lithium\action\Controller {
  14. public $secret = "marshmellows"; //I don't know why either?
  15. //Make login a public action.
  16. public $publicActions = array('login', 'logout', 'signup', 'confirm');
  17. /* User profile implementation */
  18. /* So, like twitter if you friend someone but they don't friend you,
  19. * You can see their posts, but they don't see yours. Furthermore, posts that start with a @username
  20. * shouldn't be shown to the user either.
  21. *
  22. * Now say for instance the the user isn't logged in but visits the profile of another user. It should show the users posts
  23. * that aren't set to private. this should be as easy as Posts::find('all', array('conditions' => array('level' != private)))
  24. * However, we need to differentiate posts that are Hidden (posts that begin with a @username), Friends only (set by user) or
  25. * Public (by default all posts.) there should be a method that filters the new post method, that takes a post, determines
  26. * what access level it should be then passes that back to the calling method (in save?)
  27. *
  28. * Of course, for logged in users, we need to do a multiselect I guess, something like find posts where access level is hidden
  29. * friends only, and public, ordered by date (descending) limit 20 or so (use pagination plugin)
  30. *
  31. * Finally, there should be an an option to make all posts private, this can be done by the postlevel() method, it can check i
  32. * the user has the private option set, then if true return private for all posts :)
  33. */
  34. public function index($username = null)
  35. {
  36. //If no username was entered,
  37. if ($username == null)
  38. {
  39. //Show a user list?
  40. // TODO: Pagination
  41. //Show a list of all users
  42. $users = User::all();
  43. return compact('users');
  44. //maybe we can use the endpoint hook and show a "network activity feed"
  45. //For example show all public posts as they get posted using ajax ?
  46. //Maybe we should have a route for this? /activity maybe?
  47. }
  48. //Otherwise get that user
  49. $user = User::find('first', array('conditions' => compact('username')));
  50. //find all posts, sort by date, order descending, limit to 20
  51. $feed;
  52. //Dont know if php will keep counts return on the stack so I'm storing it to avoid calling it 3 times.
  53. $tmpCount = count($user->feed);
  54. //If the user has 20 or more posts then query 20, otherwise query the number of posts the user has
  55. $count = ($tmpCount >= 20) ? 20 : $tmpCount;
  56. //If the user is logged in
  57. $user = Auth::check('default');
  58. if ($user)
  59. {
  60. if ($user->username == $username)
  61. {
  62. $feed = $user->getPosts(array('level' => null));
  63. return compact($feed, $user);
  64. }
  65. //if the user is logged in and the users are friends
  66. if (User::areFriend(Auth::check('default'), $user))
  67. {
  68. $feed = $user->getPosts(array('level' => null));
  69. return compact($feed, $user);
  70. }
  71. } //If they aren't friend, or if the user isn't logged in it defaults to the public version (filter private posts)
  72. //If the user isn't logged in, then we should show a signup partial like twitter
  73. else
  74. {
  75. //find all non-private posts, limit 20, order descending (decending was taken care of with array_unshift btw)
  76. $feed = $user->getPost(array('level' => array('$ne' => "private")));
  77. return compact($feed, $user);
  78. }
  79. }
  80. /**
  81. * How this method should work:
  82. * Since this network is default open like twitter, there is three types of relationships:
  83. * 1) Mutually Oblivious: Neither user knows each other.
  84. * 2) You Follow: You follow a user and sees their non-private posts
  85. * 3) Friends: being friends allows you to see each others privates posts
  86. * (@see app\models\post) for an example of how this works
  87. *
  88. * Now, for now there are two types of requests: Friend requests which causes both users to automatically follow eachother
  89. * And follows. Follows are unidirectional and thus don't require the followees permission
  90. */
  91. public function follow($username = null)
  92. {
  93. $status;
  94. $message;
  95. if($this->request->data)
  96. {
  97. $user = Auth::check('default');
  98. if ($user)
  99. {
  100. $user = User::find('first', array('conditions' => array('username' => $user['username'])));
  101. if ($user->follow($username))
  102. {
  103. $status = true;
  104. return array('sucseeded' => true, 'message' => 'You are now following $username');
  105. }
  106. }
  107. }
  108. }
  109. /**
  110. * Calls the user to addFriend method, then returns the status.
  111. * @param username The name of the user to add as a friend
  112. */
  113. public function addfriend($username = null)
  114. {
  115. }
  116. public function reconfirm()
  117. {
  118. //Search the database for email address
  119. if (ConfirmKey::count(array('conditions' => array('email' => $this->request->data['email']))) != 0)
  120. {
  121. //Resend the confirmation email
  122. }
  123. else
  124. {
  125. //Show them a message
  126. }
  127. //Send a new key
  128. }
  129. public function profile($username)
  130. {
  131. //If no username is passed in.
  132. if (empty($username))
  133. {
  134. $this->redirect('/');
  135. }
  136. //Otherwise
  137. else
  138. {
  139. //Find that user, and go to their profile page.
  140. $user = User::find('first', array('conditions' => compact('username')));
  141. //$photo = (empty($user->profilepic) ? ProfilePic::create() : $user->profilepic);
  142. return compact('user', 'photo');
  143. //Render the profile layout
  144. }
  145. }
  146. public function post()
  147. {
  148. if ($this->request->data)
  149. {
  150. $user = Auth::check('default');
  151. if ($user) {
  152. $user = User::find($user['_id']);
  153. $user->post($this->request->data);
  154. }
  155. /* :TODO: Need to return a status here */
  156. $this->redirect('Users::feed');
  157. }
  158. }
  159. public function feed()
  160. {
  161. //Get the currently logged in user
  162. $user = Auth::check('default');
  163. //If there is a user logged in (There should be since feed isn't a public function)
  164. if ($user)
  165. {
  166. //Get that user from the database
  167. $user = User::find('first', array('conditions' => array('username' => $user['username'])));
  168. //Set the feed variable scope (since we are going to use it outside the loop)
  169. $feed;
  170. if (isset($user->feed)) {
  171. //For each post ID in $users feed,
  172. foreach ($user->feed as $post)
  173. {
  174. //Find the post by it's ID
  175. $post = Post::find($post);
  176. //If a post was found,
  177. if (!empty($post))
  178. {
  179. //Add it to the feed
  180. $feed[] = $post;
  181. }
  182. //Else we should remove the the ID from the users feed.
  183. }
  184. /* new posts are appended to the end of the feed array, therefore new posts naturally end up at the bottom of the feed
  185. * therefore, we reverse the order of the array so that new posts end up at the top of the feed.
  186. * This is probably faster than doing sorting by date at the database level, though it for some reason
  187. * posts don't get inserted in the right order it could cause them to come out wrong in the view */
  188. $feed = array_reverse($feed);
  189. //This renders a custom layout we use for the feed, then passes user and feed to the view for the variables.
  190. }
  191. return compact('user', 'feed');
  192. }
  193. }
  194. public function signup()
  195. {
  196. $user = null;
  197. //If the request isn't empty
  198. if($this->request->data)
  199. {
  200. //Create a user from the data
  201. $user = User::Create($this->request->data);
  202. //The user isn't active until after they confirm.
  203. $user->confirmed = false;
  204. $user->active = false;
  205. $user->joinedOn = new MongoDate();
  206. //By default save does validation at the same time,
  207. //If there are errors its stuffs them into the $user->_erorrs variable,
  208. //Accessible from $user->errors(), this is automatically passed to the view.
  209. if ($user->save())
  210. {
  211. //Generate a confirmation key for the user
  212. $key = confirmKey::Create(array('key' => confirmKey::generate($user->email), 'username' => $user->username));
  213. //Save it to the database
  214. $key->save();
  215. //For testing, we return the link to the view, so they can click it,
  216. //This will be replaced with an email in production
  217. $link = "/users/confirm";
  218. return compact('key', 'link', 'user');
  219. }
  220. }
  221. return compact('user');
  222. }
  223. /*
  224. If the user is valid, but not confirmed,
  225. tell the user they haven't confirmed,
  226. offer to resend the confirmation email or changed their email address.
  227. */
  228. public function login($location = null, $args = null)
  229. {
  230. if (!empty($this->request->data)) {
  231. $user = Auth::check('default', $this->request);
  232. if ($user)
  233. {
  234. $user = User::find('first', array('conditions' => array('username' => $user['username'])));
  235. $user->lastLogin = new MongoDate();
  236. $user->save(null, array('validate' => false));
  237. //If the user hasn't confirmed their account
  238. if(!$user->confirmed)
  239. {
  240. //Redirect them to the confirmation page.
  241. return $this->redirect('Users::confirm');
  242. }
  243. //If the user's account is not active they are probably banned
  244. if (!$user->active)
  245. {
  246. return $this->redirect('/pages/banned');
  247. }
  248. //If the user was trying to go somewhere, redirect them there
  249. $loc = Session::read('url');
  250. if (isset($loc))
  251. {
  252. Session::delete('url');
  253. return $this->redirect(array('controller' => $loc['controller'],
  254. 'action' => $loc['action'],
  255. 'args' => $loc['args']
  256. ));
  257. }
  258. //Otherwise send them to the hompa
  259. return $this->redirect('Users::feed');
  260. }
  261. else
  262. {
  263. FlashMessage::write('Username or Password Incorrect.');
  264. }
  265. }
  266. }
  267. //Logout
  268. public function logout()
  269. {
  270. //If the user logs out
  271. //Clear their auth cookie
  272. Auth::Clear('default');
  273. //Redirect them to the homepage.
  274. return $this->redirect('/');
  275. }
  276. public function changePassword()
  277. {
  278. //Get the user to verify their current password
  279. $input = $this->request->data;
  280. //If there is inputfrom the form
  281. if ($input)
  282. {
  283. //Get the user from auth
  284. $user = Auth::check('default');
  285. //Ensure that the passwords are the same.
  286. if ($input['newpass'] != $input['confirm'])
  287. {
  288. //return error
  289. return $this->redirect('/');
  290. }
  291. else if(!empty($user) && isset($input['newpass']))
  292. {
  293. //find the user by their ID
  294. $user = User::find($user['_id']);
  295. //Set the newpassword, this triggers the hash function in ->save()
  296. $user->newpass = $input['newpass'];
  297. //Save the data
  298. if ($user->save(null, array('validate' => false))) {
  299. //Tell the user their password was updated sucsessfully
  300. }
  301. //Else there was an error, so send them away
  302. /* If the compare is changed to a validator
  303. * returning the user object will show the error in the view.*/
  304. return compact('user');
  305. }
  306. }
  307. }
  308. public function settings()
  309. {
  310. //Get the user using their login information (from auth)
  311. $user = Auth::check('default');
  312. $user = User::find($user['_id']);
  313. //If the request isn't empty
  314. if(!empty($this->request->data)) {
  315. //Foreach key/value pair in the request data
  316. foreach($this->request->data as $key => $value)
  317. {
  318. //TODO: Make sure to lock the schema to prevent wierd values
  319. $user->$key = $value;
  320. }
  321. //Save the user.
  322. $user->save(null, array('validate' => false));
  323. }
  324. return compact('user');
  325. }
  326. public function requestFriend($username = null) {
  327. if ($username == null)
  328. {
  329. $username = $this->request->query['username'];
  330. }
  331. //If the user isn't blocking this user,
  332. //And the user doesn't have private set
  333. //Send them a DM with a confirm key
  334. $key = confirmKey::create();
  335. $thisUser = auth::check('default');
  336. $url = \lithium\net\http\Router::match(array('controller' => 'users',
  337. 'action' => 'confirmFriend',
  338. 'args'=>array($thisUser['username'], $key->key)), null, array('absolute' => 'false'));
  339. $post = Post::create(array('body' => $thisUser['username'] . " want's to be your friend. Click $url to confirm"));
  340. $post->directMessage($thisUser);
  341. return $this->redirect('users::feed');
  342. }
  343. /* Potential hack here, in theory, a user could sign up a new account
  344. * then send a direct message manually using the confirm key from the email.
  345. */
  346. public function confirmFriend($username, $key) {
  347. /* Normally we could try and find the cKey, then if it doesn't exist
  348. * Do something about it,
  349. * However, ConfirmKey::validates basically counts the number of keys that
  350. * match $key, this is probably better than find since ->validates() may be a bit
  351. * more efficent */
  352. $cKey = confirmKey::create(compact('key'));
  353. if ($cKey->validates())
  354. {
  355. $thisUser = Auth::check('default');
  356. $thisUser = User::find('first', array('conditions' => array('username' => $thisUser['username'])));
  357. $requestingUser = User::find('first', array('conditions' => compact('username')));
  358. $requestingUser->addFriend($thisUser->username);
  359. $thisUser->addFriend($requetingUser->username);
  360. //Some action here if true :TODO:
  361. }
  362. //Some action here if false
  363. }
  364. public function confirm($key = null)
  365. {
  366. //Situation one
  367. //They have a key
  368. if (!(empty($key)))
  369. {
  370. //Find the key in the database
  371. $foundKey = confirmKey::find('first', array('conditions' => compact('key')));
  372. //If the key exists
  373. if($foundKey != NULL)
  374. {
  375. /* Note: foundKey->validates() does the same check, but it was added incase more validation is needed */
  376. //Find that user in the database
  377. $foundUser = User::find('first', array('conditions' => array("username" => $foundKey->username)));
  378. $valid = ($foundUser != NULL);
  379. //Set the users account active and confirmed.
  380. $foundUser->confirmed = true;
  381. $foundUser->active = true;
  382. //If the user is saved sucsessfully,
  383. if($foundUser->save(null, array('validate' => false)))
  384. {
  385. /* If the save is sucsessful we are done */
  386. //Delete their key,
  387. $foundKey->delete();
  388. //Send them to the homepage (probably login though)
  389. $this->redirect("/");
  390. }
  391. else
  392. {
  393. FlashMessage::set("There was an error.");
  394. }
  395. }
  396. else
  397. {
  398. //Otherwise
  399. FlashMessage::set("There was an error finding the key.");
  400. return;
  401. }
  402. }
  403. }
  404. public function step2()
  405. {
  406. //Check that step1 is completed sucsessfully,
  407. //Then take them to their profile in edit mode
  408. }
  409. }
  410. ?>