user.path.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. Bugs::actions(
  3. 'user.update'
  4. );
  5. Router::paths(array(
  6. '/~{user}'=>function($res,$args){
  7. $user = Bugs::user($args->user);
  8. if($user->active){
  9. $res->write(
  10. Bugs::template('user')
  11. ->run($user)
  12. );
  13. }else{
  14. trigger_error("User {$args->user} is inactive");
  15. }
  16. },
  17. '/user/{user}'=>function($res,$args){
  18. Bugs::authorized('user.read');
  19. Router::redirect(Router::url(Router::$base.'/~'.$args->user));
  20. },
  21. '/user/{user}/update'=>function($res,$args){
  22. error_handle_type('json');
  23. if(Bugs::$user){
  24. if(!empty($_POST['password'])&&!empty($_POST['id'])&&!empty($_POST['name'])&&!empty($_POST['email'])){
  25. $user = Bugs::user(intval($args->user));
  26. if($user->id != Bugs::$user->id){
  27. Bugs::authorized('user.update');
  28. }
  29. if($user->password == $user->hash($_POST['password'])){
  30. $user->name = $_POST['name'];
  31. $user->email = $_POST['email'];
  32. $res->json(array(
  33. 'name'=>$user->name
  34. ));
  35. if($user->id == Bugs::$user->id){
  36. Bugs::login($user,$_POST['password']);
  37. }
  38. Bugs::activity('user.update',$user);
  39. }else{
  40. $res->json(array(
  41. 'error'=>'Invalid password.'
  42. ));
  43. }
  44. }else{
  45. $res->json(array(
  46. 'error'=>'Missing information.'
  47. ));
  48. }
  49. }else{
  50. $res->json(array(
  51. 'error'=>'Not logged in.'
  52. ));
  53. }
  54. }
  55. ));
  56. ?>