api.php 13 KB

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