UsersController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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. /*
  195. If the user is valid, but not confirmed,
  196. tell the user they haven't confirmed,
  197. offer to resend the confirmation email or changed their email address.
  198. */
  199. public function login($location = null, $args = null)
  200. {
  201. if (!empty($this->request->data)) {
  202. $user = Auth::check('default', $this->request);
  203. if ($user)
  204. {
  205. $user = User::find('first', array('conditions' => array('username' => $user['username'])));
  206. $user->lastLogin = date('Y-m-d H:i:s', time());
  207. $user->save(null, array('validate' => false));
  208. //If the user hasn't confirmed their account
  209. if(!$user->confirmed)
  210. {
  211. //Redirect them to the confirmation page.
  212. return $this->redirect('Users::confirm');
  213. }
  214. //If the user's account is not active they are probably banned
  215. if (!$user->active)
  216. {
  217. return $this->redirect('/pages/banned');
  218. }
  219. //If the user was trying to go somewhere, redirect them there
  220. $loc = Session::read('url');
  221. if (isset($loc))
  222. {
  223. Session::delete('url');
  224. return $this->redirect(array('controller' => $loc['controller'],
  225. 'action' => $loc['action'],
  226. 'args' => $loc['args']
  227. ));
  228. }
  229. //Otherwise send them to the hompa
  230. return $this->redirect('Users::feed');
  231. }
  232. else
  233. {
  234. FlashMessage::write('Username or Password Incorrect.');
  235. }
  236. }
  237. }
  238. //Logout
  239. public function logout()
  240. {
  241. //If the user logs out
  242. //Clear their auth cookie
  243. Auth::Clear('default');
  244. //Redirect them to the homepage.
  245. return $this->redirect('/');
  246. }
  247. public function changePassword()
  248. {
  249. //Get the user to verify their current password
  250. $input = $this->request->data;
  251. //If there is inputfrom the form
  252. if ($input)
  253. {
  254. //Get the user from auth
  255. $user = Auth::check('default');
  256. //Ensure that the passwords are the same.
  257. if ($input['newpass'] != $input['confirm'])
  258. {
  259. //return error
  260. return $this->redirect('/');
  261. }
  262. else if(!empty($user) && isset($input['newpass']))
  263. {
  264. //find the user by their ID
  265. $user = User::find($user['_id']);
  266. //Set the newpassword, this triggers the hash function in ->save()
  267. $user->newpass = $input['newpass'];
  268. //Save the data
  269. if ($user->save(null, array('validate' => false))) {
  270. //Tell the user their password was updated sucsessfully
  271. }
  272. //Else there was an error, so send them away
  273. /* If the compare is changed to a validator
  274. * returning the user object will show the error in the view.*/
  275. return compact('user');
  276. }
  277. }
  278. }
  279. public function settings()
  280. {
  281. //Get the user using their login information (from auth)
  282. $user = Auth::check('default');
  283. $user = User::find($user['id']);
  284. //If the request isn't empty
  285. if(!empty($this->request->data)) {
  286. //Foreach key/value pair in the request data
  287. foreach($this->request->data as $key => $value)
  288. {
  289. //TODO: Make sure to lock the schema to prevent wierd values
  290. $user->$key = $value;
  291. }
  292. //Save the user.
  293. $user->save(null, array('validate' => false));
  294. }
  295. return compact('user');
  296. }
  297. public function requestFriend($username = null) {
  298. if ($username == null)
  299. {
  300. $username = $this->request->query['username'];
  301. }
  302. //If the user isn't blocking this user,
  303. //And the user doesn't have private set
  304. //Send them a DM with a confirm key
  305. $key = confirmKey::create();
  306. $thisUser = auth::check('default');
  307. $url = \lithium\net\http\Router::match(array('controller' => 'users',
  308. 'action' => 'confirmFriend',
  309. 'args'=>array($thisUser['username'], $key->key)), null, array('absolute' => 'false'));
  310. $post = Post::create(array('body' => $thisUser['username'] . " want's to be your friend. Click $url to confirm"));
  311. $post->directMessage($thisUser);
  312. return $this->redirect('users::feed');
  313. }
  314. /* Potential hack here, in theory, a user could sign up a new account
  315. * then send a direct message manually using the confirm key from the email.
  316. */
  317. public function confirmFriend($username, $key) {
  318. /* Normally we could try and find the cKey, then if it doesn't exist
  319. * Do something about it,
  320. * However, ConfirmKey::validates basically counts the number of keys that
  321. * match $key, this is probably better than find since ->validates() may be a bit
  322. * more efficent */
  323. $cKey = confirmKey::create(compact('key'));
  324. if ($cKey->validates())
  325. {
  326. $thisUser = Auth::check('default');
  327. $thisUser = User::find('first', array('conditions' => array('username' => $thisUser['username'])));
  328. $requestingUser = User::find('first', array('conditions' => compact('username')));
  329. $requestingUser->addFriend($thisUser->username);
  330. $thisUser->addFriend($requetingUser->username);
  331. //Some action here if true :TODO:
  332. }
  333. //Some action here if false
  334. }
  335. public function step2()
  336. {
  337. //Check that step1 is completed sucsessfully,
  338. //Then take them to their profile in edit mode
  339. }
  340. }
  341. ?>