api.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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. $ret['template'] = array(
  13. 'type'=>'pages',
  14. 'name'=>'user'
  15. );
  16. if($user = userObj($id)){
  17. $context = array(
  18. 'name'=>$user['name'],
  19. 'email'=>$user['email']
  20. );
  21. if($LOGGEDIN){
  22. $context['key'] = true;
  23. $context['user'] = userObj($_SESSION['username']);
  24. };
  25. $ret['context'] = $context;
  26. }else{
  27. $ret['state'] = array(
  28. 'url'=>isset($_GET['back'])?$_GET['back']:'page-index'
  29. );
  30. }
  31. retj($ret,'User - '.$context['name']);
  32. break;
  33. case 'group':
  34. back(true);
  35. // TODO - handle group requests
  36. if(false){
  37. // TODO
  38. }else{
  39. $ret['state'] = array(
  40. 'url'=>isset($_GET['back'])?$_GET['back']:'page-index'
  41. );
  42. }
  43. retj($ret);
  44. break;
  45. case 'issue':
  46. back(true);
  47. $ret['template'] = array(
  48. 'type'=>'pages',
  49. 'name'=>'issue'
  50. );
  51. if($context = issueObj($id)){
  52. $context['user'] = userObj($context['user']);
  53. if($LOGGEDIN){
  54. $context['key'] = true;
  55. $context['user'] = userObj($_SESSION['username']);
  56. };
  57. $ret['context'] = $context;
  58. }else{
  59. $ret['state'] = array(
  60. 'url'=>isset($_GET['back'])?$_GET['back']:'page-index'
  61. );
  62. }
  63. retj($ret,'Issue #'.$id. ' - '.$context['title']);
  64. break;
  65. case 'scrum':
  66. back(true);
  67. // TODO - handle scrum requests
  68. if(false){
  69. // TODO
  70. }else{
  71. $ret['state'] = array(
  72. 'url'=>isset($_GET['back'])?$_GET['back']:'page-index'
  73. );
  74. }
  75. retj($ret);
  76. break;
  77. case 'project':
  78. back(true);
  79. $ret['template'] = array(
  80. 'type'=>'pages',
  81. 'name'=>'project'
  82. );
  83. if($context = projectObj($id)){
  84. $context['user'] = userObj($context['user']);
  85. if($LOGGEDIN){
  86. $context['key'] = true;
  87. $context['user'] = userObj($_SESSION['username']);
  88. };
  89. $ret['context'] = $context;
  90. }else{
  91. $ret['state'] = array(
  92. 'url'=>isset($_GET['back'])?$_GET['back']:'page-index'
  93. );
  94. }
  95. retj($ret,'Project - '.$context['title']);
  96. break;
  97. case 'message':
  98. // TODO - handle message requests
  99. $context = array();
  100. if(false){
  101. // TODO
  102. }else{
  103. $ret['state'] = array(
  104. 'url'=>isset($_GET['back'])?$_GET['back']:'page-index'
  105. );
  106. }
  107. retj($ret,'Project - '.$context['title']);
  108. break;
  109. case 'admin':
  110. back(true);
  111. // TODO - handle admin requests
  112. if(false){
  113. // TODO
  114. }else{
  115. $ret['state'] = array(
  116. 'url'=>isset($_GET['back'])?$_GET['back']:'page-index'
  117. );
  118. }
  119. retj($ret);
  120. break;
  121. case 'page':
  122. $title = $id;
  123. if(file_exists(PATH_DATA.'pages/'.$id.'.template')){
  124. $context = array();
  125. $ret['template'] = array(
  126. 'type'=>'pages',
  127. 'name'=>$id
  128. );
  129. if($LOGGEDIN){
  130. $context['key'] = true;
  131. $context['user'] = userObj($_SESSION['username']);
  132. };
  133. if(file_exists(PATH_DATA.'pages/'.$id.'.options')){
  134. $options = objectToarray(json_decode(file_get_contents(PATH_DATA.'pages/'.$id.'.options'),true));
  135. if(isset($options['secure'])&&$options['secure']&&!$LOGGEDIN){
  136. back(true);
  137. }
  138. if(isset($options['title'])){
  139. $title = $options['title'];
  140. }
  141. if(isset($options['context'])){
  142. foreach($options['context'] as $key){
  143. switch($key){
  144. case 'users':
  145. if($res = query("SELECT name FROM `users`;")){
  146. $context['users'] = fetch_all($res,MYSQLI_ASSOC);
  147. }
  148. break;
  149. case 'projects':
  150. 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")){
  151. $context['projects'] = fetch_all($res,MYSQLI_ASSOC);
  152. foreach($context['projects'] as $key => $project){
  153. $context['projects'][$key]['user'] = userObj($project['user']);
  154. }
  155. }
  156. break;
  157. case 'messages':
  158. if($LOGGEDIN){
  159. $context['messages'] = messages($context['user']['id'],'user');
  160. }else{
  161. $context['messages'] = array();
  162. }
  163. break;
  164. case 'issues':
  165. if($res = query("SELECT i.id,i.title,i.description,u.name as user,s.name as status,p.name as priority,p.color FROM `issues` i JOIN `users` u ON u.id = i.u_id LEFT JOIN `statuses` s ON s.id = i.st_id LEFT JOIN `priorities` p ON p.id = i.pr_id")){
  166. $context['issues'] = fetch_all($res,MYSQLI_ASSOC);
  167. foreach($context['issues'] as $key => $issue){
  168. $context['issues'][$key]['user'] = userObj($issue['user']);
  169. }
  170. }
  171. break;
  172. }
  173. }
  174. }
  175. if(isset($options['actions'])){
  176. foreach($options['actions'] as $key){
  177. switch($key){
  178. case 'pm_mark_read':
  179. query("UPDATE `users` SET last_pm_check=CURRENT_TIMESTAMP WHERE id='%d'; ",array(userId($_SESSION['username'])));
  180. break;
  181. }
  182. }
  183. }
  184. }
  185. $ret['context'] = $context;
  186. }else{
  187. $ret['error'] = 'That page does not exist';
  188. }
  189. retj($ret,$title);
  190. break;
  191. case 'manifest':
  192. case 'pages':
  193. if(isset($_GET['id'])){
  194. if($_GET['id'] != 'emails'){
  195. $manifest = array();
  196. $files = array_diff(scandir(PATH_DATA.'/'.$_GET['id']),array('..', '.','.htaccess','version'));
  197. foreach($files as $k => $file){
  198. if(pathinfo(PATH_DATA.'/'.$_GET['id'].'/'.$file,PATHINFO_EXTENSION) == 'template'){
  199. array_push($manifest,array(
  200. 'name'=>basename($file,'.template'),
  201. 'hash'=>md5_file(PATH_DATA.'/'.$_GET['id'].'/'.$file)
  202. ));
  203. }
  204. }
  205. retj(array(
  206. 'manifest'=>$manifest,
  207. 'type'=>$_GET['id']
  208. ));
  209. }else{
  210. retj(array(
  211. 'error'=>'Cannot return that manifest'
  212. ));
  213. }
  214. }else{
  215. retj(array(
  216. 'error'=>'Manifest ID not defined'
  217. ));
  218. }
  219. break;
  220. break;
  221. case 'template':
  222. if(isset($_GET['name'])){
  223. if($_GET['id'] != 'emails'){
  224. retj(array(
  225. 'template'=>file_get_contents(PATH_DATA.'/'.$_GET['id'].'/'.$_GET['name'].'.template'),
  226. 'name'=>$_GET['name'],
  227. 'type'=>$_GET['id'],
  228. 'hash'=>md5_file(PATH_DATA.'/'.$_GET['id'].'/'.$_GET['name'].'.template')
  229. ));
  230. }else{
  231. retj(array(
  232. 'error'=>'Cannot return that type of template'
  233. ));
  234. }
  235. }else{
  236. retj(array(
  237. 'error'=>'Template name missing'
  238. ));
  239. }
  240. break;
  241. case 'action':
  242. switch($id){
  243. case 'login':
  244. $ret['state'] = array(
  245. 'data'=>array(
  246. 'type'=>'page',
  247. 'id'=>'login',
  248. )
  249. );
  250. if(isset($_GET['username'])&&isset($_GET['password'])){
  251. $key = login($_GET['username'],$_GET['password']);
  252. if($key){
  253. $_SESSION['username'] = $_GET['username'];
  254. }else{
  255. $ret['error'] = "Login failed. Username or Password didn't match.";
  256. }
  257. }else{
  258. $ret['error'] = "Please provide a valid username and password.";
  259. }
  260. retj($ret,$id);
  261. break;
  262. case 'register':
  263. $ret['state'] = array(
  264. 'data'=>array(
  265. 'type'=>'page',
  266. 'id'=>'register'
  267. )
  268. );
  269. if(is_valid('username')&&is_valid('password')&&is_valid('password1')&&is_valid('email')&&is_valid('captcha')){
  270. if($_GET['password']==$_GET['password1']){
  271. if(compare_captcha($_GET['captcha'])){
  272. if(addUser($_GET['username'],$_GET['password'],$_GET['email'])){
  273. $key = login($_GET['username'],$_GET['password']);
  274. $_SESSION['username'] = $_GET['username'];
  275. sendMail('welcome','Welcome!',$_GET['email'],get('email'),array($_GET['username'],$_GET['password'],get('email')));
  276. }else{
  277. $ret['error'] = "Could not add user. ".$mysqli->error;
  278. }
  279. }else{
  280. $ret['error'] = "Captcha did not match.";
  281. }
  282. }else{
  283. $ret['error'] = "Passwords didn't match.";
  284. }
  285. }else{
  286. $ret['error'] = "Please fill in all the fields.";
  287. }
  288. retj($ret,$id);
  289. break;
  290. case 'project':
  291. back(true);
  292. $ret['state'] = array(
  293. 'data'=>array(
  294. 'type'=>'page',
  295. 'id'=>$id,
  296. )
  297. );
  298. if(isset($_GET['pid'])){
  299. $ret['error'] = 'Invalid Action';
  300. }elseif(is_valid('title')&&is_valid('description')){
  301. if(!newProject($_GET['title'],$_GET['description'])){
  302. $ret['error'] = 'Unable to create project.';
  303. }
  304. }else{
  305. $ret['error'] = 'Fill in all the details.';
  306. }
  307. retj($ret,$id);
  308. break;
  309. case 'issue':
  310. back(true);
  311. $ret['state'] = array(
  312. 'data'=>array(
  313. 'type'=>'page',
  314. 'id'=>$id,
  315. )
  316. );
  317. if(isset($_GET['pid'])){
  318. $ret['error'] = 'Invalid Action';
  319. }elseif(is_valid('title')&&is_valid('description')){
  320. if(!newIssue($_GET['title'],$_GET['description'])){
  321. $ret['error'] = 'Unable to create issue. ';
  322. }
  323. }else{
  324. $ret['error'] = 'Fill in all the details.';
  325. }
  326. retj($ret,$id);
  327. break;
  328. case 'message':
  329. back(true);
  330. if(isset($_GET['to'])&&isset($_GET['message'])){
  331. if($uid = userId($_GET['to'])){
  332. if(!personal_message($uid,$_GET['message'])){
  333. $ret['error'] = 'Could not send message';
  334. }
  335. }else{
  336. $ret['error'] = "That user doesn't exist";
  337. }
  338. }else{
  339. $ret['error'] = 'Empty details';
  340. }
  341. retj($ret,$id);
  342. break;
  343. case 'notifications':
  344. if($LOGGEDIN){
  345. if($res = query("SELECT count(m.id) as notifications,UNIX_TIMESTAMP(max(m.timestamp)) as timestamp FROM `messages` m JOIN `users` u ON u.id = m.to_id WHERE u.id = %d AND u.last_pm_check < m.timestamp;",array(userId($_SESSION['username'])))){
  346. $res = $res->fetch_assoc();
  347. $ret['count'] = $res['notifications'];
  348. $ret['timestamp'] = $res['timestamp'];
  349. }
  350. }
  351. retj($ret,$_GET['title']);
  352. break;
  353. case 'comment':
  354. if(isset($_GET['comment_type'])&&isset($_GET['comment_id'])&&isset($_GET['message'])){
  355. $cid = $_GET['comment_id'];
  356. $ret = array(
  357. 'state'=>stateObj($_GET['comment_type'],$cid)
  358. );
  359. switch($_GET['comment_type']){
  360. case 'project':
  361. if(!function_exists('project_comment')){
  362. $ret['error'] = "fn doesn't exist!";
  363. }
  364. if(!project_comment($cid,$_GET['message'])){
  365. $ret = array(
  366. 'error'=>'Could not comment on project'
  367. );
  368. }
  369. break;
  370. case 'issue':
  371. if(!function_exists('issue_comment')){
  372. $ret['error'] = "fn doesn't exist!";
  373. }
  374. if(!issue_comment($cid,$_GET['message'])){
  375. $ret = array(
  376. 'error'=>'Could not comment on project'
  377. );
  378. }
  379. break;
  380. default:
  381. $ret['error'] = 'Comment type not implemented';
  382. }
  383. }else{
  384. $ret['error'] = 'Missing comment paremeters';
  385. $ret['state'] = array(
  386. 'title'=>'error'
  387. );
  388. }
  389. retj($ret,$ret['state']['title']);
  390. break;
  391. case 'more':
  392. if(isset($_GET['of']) && isset($_GET['pid'])){
  393. $ret = array();
  394. $limit = array(
  395. isset($_GET['at'])?$_GET['at']:0,
  396. isset($_GET['amount'])?$_GET['amount']:10
  397. );
  398. $ret['messages'] = messages($_GET['pid'],$_GET['of'],$limit[0],$limit[1]);
  399. $ret['params'] = array($_GET['pid'],$_GET['of'],$limit[0],$limit[1]);
  400. }else{
  401. $ret['error'] = 'Missing comment parameters';
  402. }
  403. retj($ret);
  404. break;
  405. default:
  406. retj(array(
  407. 'error'=>'Invalid action.'
  408. ));
  409. }
  410. break;
  411. default:
  412. retj(array(
  413. 'error'=>'Invalid type.'
  414. ));
  415. }
  416. }else{
  417. retj(array(
  418. 'error'=>'ID missing.'
  419. ));
  420. }
  421. }else{
  422. $_GET['type'] = '';
  423. retj(array(
  424. 'error'=>'Type missing.'
  425. ));
  426. }
  427. ?>