api.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. back(true);
  12. if(!isset($_GET['template'])){
  13. $ret['template'] = file_get_contents(PATH_DATA.'pages/user.template');
  14. }
  15. $user = userObj($id);
  16. $context = Array(
  17. 'name'=>$user['name'],
  18. 'email'=>$user['email']
  19. );
  20. if($LOGGEDIN){
  21. $context['key'] = true;
  22. $context['user'] = userObj($_SESSION['username']);
  23. };
  24. $ret['context'] = $context;
  25. retj($ret,'User - '.$context['name']);
  26. break;
  27. case 'group':
  28. back(true);
  29. // TODO - handle group requests
  30. break;
  31. case 'issue':
  32. back(true);
  33. // TODO - handle issue requests
  34. break;
  35. case 'scrum':
  36. back(true);
  37. // TODO - handle scrum requests
  38. break;
  39. case 'project':
  40. back(true);
  41. if(!isset($_GET['template'])){
  42. $ret['template'] = file_get_contents(PATH_DATA.'pages/project.template');
  43. }
  44. $context = projectObj($id);
  45. $context['user'] = userObj($context['user']);
  46. if($LOGGEDIN){
  47. $context['key'] = true;
  48. $context['user'] = userObj($_SESSION['username']);
  49. };
  50. $ret['context'] = $context;
  51. retj($ret,'Project - '.$context['title']);
  52. break;
  53. case 'message':
  54. // TODO - handle message requests
  55. break;
  56. case 'admin':
  57. back(true);
  58. // TODO - handle admin requests
  59. break;
  60. case 'page':
  61. $title = $id;
  62. if(file_exists(PATH_DATA.'pages/'.$id.'.template')){
  63. if(!isset($_GET['template'])||$_GET['template']=='true'){
  64. $ret['template'] = file_get_contents(PATH_DATA.'pages/'.$id.'.template');
  65. }
  66. $context = Array();
  67. if($LOGGEDIN){
  68. $context['key'] = true;
  69. $context['user'] = userObj($_SESSION['username']);
  70. };
  71. if(file_exists(PATH_DATA.'pages/'.$id.'.options')){
  72. $options = objectToArray(json_decode(file_get_contents(PATH_DATA.'pages/'.$id.'.options'),true));
  73. if(isset($options['secure'])&&$options['secure']&&!$LOGGEDIN){
  74. back(true);
  75. }
  76. if(isset($options['title'])){
  77. $title = $options['title'];
  78. }
  79. if(isset($options['context'])){
  80. foreach($options['context'] as $key){
  81. switch($key){
  82. case 'users':
  83. if($res = query("SELECT name FROM `users`;")){
  84. $context['users'] = fetch_all($res,MYSQLI_ASSOC);
  85. }
  86. break;
  87. case 'projects':
  88. if($res = query("SELECT p.title,p.id,p.description,u.name as user FROM `projects` p JOIN `users` u ON u.id = p.u_id")){
  89. $context['projects'] = fetch_all($res,MYSQLI_ASSOC);
  90. foreach($context['projects'] as $key => $project){
  91. $context['projects'][$key]['user'] = userObj($project['user']);
  92. }
  93. }
  94. break;
  95. case 'messages':
  96. if($LOGGEDIN){
  97. $context['messages'] = messages($context['user']['id'],'user');
  98. }else{
  99. $context['messages'] = Array();
  100. }
  101. break;
  102. }
  103. }
  104. }
  105. }
  106. $ret['context'] = $context;
  107. }else{
  108. $ret['error'] = 'That page does not exist';
  109. }
  110. retj($ret,$title);
  111. break;
  112. case 'action':
  113. switch($id){
  114. case 'login':
  115. $ret['state'] = Array(
  116. 'data'=>Array(
  117. 'type'=>'page',
  118. 'id'=>'login',
  119. )
  120. );
  121. if(isset($_GET['username'])&&isset($_GET['password'])){
  122. $key = login($_GET['username'],$_GET['password']);
  123. if($key){
  124. $_SESSION['username'] = $_GET['username'];
  125. }else{
  126. $ret['error'] = "Login failed. Username or Password didn't match.";
  127. }
  128. }else{
  129. $ret['error'] = "Please provide a valid username and password.";
  130. }
  131. retj($ret,$id);
  132. break;
  133. case 'register':
  134. $ret['state'] = Array(
  135. 'data'=>Array(
  136. 'type'=>'page',
  137. 'id'=>'register'
  138. )
  139. );
  140. if(is_valid('username')&&is_valid('password')&&is_valid('password1')&&is_valid('email')&&is_valid('captcha')){
  141. if($_GET['password']==$_GET['password1']){
  142. if(compare_captcha($_GET['captcha'])){
  143. if(addUser($_GET['username'],$_GET['password'],$_GET['email'])){
  144. $key = login($_GET['username'],$_GET['password']);
  145. $_SESSION['username'] = $_GET['username'];
  146. sendMail('welcome','Welcome!',$_GET['email'],get('email'),Array($_GET['username'],$_GET['password'],get('email')));
  147. }else{
  148. $ret['error'] = "Could not add user. ".$mysqli->error;
  149. }
  150. }else{
  151. $ret['error'] = "Captcha did not match.";
  152. }
  153. }else{
  154. $ret['error'] = "Passwords didn't match.";
  155. }
  156. }else{
  157. $ret['error'] = "Please fill in all the fields.";
  158. }
  159. retj($ret,$id);
  160. break;
  161. case 'project':
  162. back(true);
  163. $ret['state'] = Array(
  164. 'data'=>Array(
  165. 'type'=>'page',
  166. 'id'=>$id,
  167. )
  168. );
  169. if(isset($_GET['pid'])){
  170. $ret['error'] = 'Invalid Action';
  171. }elseif(is_valid('title')&&is_valid('description')){
  172. if(!newProject($_GET['title'],$_GET['description'])){
  173. $ret['error'] = 'Unable to create project.';
  174. }
  175. }else{
  176. $ret['error'] = 'Fill in all the details.';
  177. }
  178. retj($ret,$id);
  179. break;
  180. case 'comment':
  181. $ret = Array(
  182. 'state'=>stateObj($_GET['comment_type'],$_GET['comment_id'])
  183. );
  184. retj($ret,$ret['state']['title']);
  185. break;
  186. default:
  187. retj(Array(
  188. 'error'=>'Invalid action.'
  189. ));
  190. }
  191. break;
  192. default:
  193. retj(Array(
  194. 'error'=>'Invalid type.'
  195. ));
  196. }
  197. }else{
  198. retj(Array(
  199. 'error'=>'ID missing.'
  200. ));
  201. }
  202. }else{
  203. retj(Array(
  204. 'error'=>'Type missing.'
  205. ));
  206. }
  207. ?>