UsersController.php 12 KB

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