api.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. require_once('php/include.php');
  3. // TODO - Add API handling.
  4. $method = $_SERVER['REQUEST_METHOD'];
  5. $ret = Array();
  6. if(isset($_GET['type'])){
  7. if(isset($_GET['id'])){
  8. $id = $_GET['id'];
  9. switch($_GET['type']){
  10. case 'user':
  11. $ret['template'] = file_get_contents(PATH_DATA.'pages/user.template');
  12. $user = userObj($id);
  13. $context = Array(
  14. 'name'=>$user['name'],
  15. 'email'=>$user['email']
  16. );
  17. if($LOGGEDIN){
  18. $context['key'] = true;
  19. $context['user'] = userObj($_SESSION['username']);
  20. };
  21. $ret['context'] = $context;
  22. retj($ret,$id);
  23. break;
  24. case 'group':
  25. // TODO - handle group requests
  26. break;
  27. case 'issue':
  28. // TODO - handle issue requests
  29. break;
  30. case 'scrum':
  31. // TODO - handle scrum requests
  32. break;
  33. case 'admin':
  34. // TODO - handle admin requests
  35. break;
  36. case 'template':
  37. $ret['template'] = file_get_contents(PATH_DATA.'pages/'.$id.'.template');
  38. if(file_exists(PATH_DATA.'context/'.$id.'.json')){
  39. $context = objectToArray(json_decode(file_get_contents(PATH_DATA.'context/'.$id.'.json')));
  40. }else{
  41. $context = Array();
  42. }
  43. if($LOGGEDIN){
  44. $context['key'] = true;
  45. $context['user'] = userObj($_SESSION['username']);
  46. };
  47. $ret['context'] = $context;
  48. retj($ret,$id);
  49. break;
  50. case 'action':
  51. switch($id){
  52. case 'login':
  53. $ret['state'] = Array(
  54. 'data'=>Array(
  55. 'type'=>'template',
  56. 'id'=>'login',
  57. )
  58. );
  59. if(isset($_GET['username'])&&isset($_GET['password'])){
  60. $key = login($_GET['username'],$_GET['password']);
  61. if($key){
  62. $_SESSION['username'] = $_GET['username'];
  63. }else{
  64. $ret['error'] = "Login failed. Username or Password didn't match.";
  65. }
  66. }else{
  67. $ret['error'] = "Please provide a valid username and password.";
  68. }
  69. retj($ret,$id);
  70. break;
  71. case 'register':
  72. $ret['state'] = Array(
  73. 'data'=>Array(
  74. 'type'=>'template',
  75. 'id'=>'register'
  76. )
  77. );
  78. if(isset($_GET['username'])&&isset($_GET['password'])&&isset($_GET['email'])){
  79. if(addUser($_GET['username'],$_GET['password'],$_GET['email'])){
  80. $key = login($_GET['username'],$_GET['password']);
  81. $_SESSION['username'] = $_GET['username'];
  82. }else{
  83. $ret['error'] = "Could not add user. ".$mysqli->error;
  84. }
  85. }else{
  86. $ret['error'] = "That username already exists!";
  87. }
  88. retj($ret,$id);
  89. break;
  90. default:
  91. die('invalid action');
  92. }
  93. break;
  94. default:
  95. die("invalid type");
  96. }
  97. }else{
  98. die("id missing");
  99. }
  100. }else{
  101. die("type missing");
  102. }
  103. ?>