2017-05-13 14:31:18 -06:00
< ? php
2017-12-16 14:25:31 -07:00
/* This Source Code Form is subject to the terms of the Mozilla Public
* License , v . 2.0 . If a copy of the MPL was not distributed with this
* file , You can obtain one at http :// mozilla . org / MPL / 2.0 /. */
2017-05-13 14:31:18 -06:00
/**
* Simple JSON API to allow other apps to access data from this app .
2018-04-09 19:18:19 -06:00
*
2017-05-13 14:31:18 -06:00
* Requests can be sent via either GET or POST requests . POST is recommended
* as it has a lower chance of being logged on the server , exposing unencrypted
* user passwords .
*/
require __DIR__ . '/required.php' ;
header ( " Content-Type: application/json " );
2018-09-07 19:19:07 -06:00
/**
* Checks if the given AccountHub API key is valid by attempting to
* access the API with it .
* @ param String $key The API key to check
* @ return boolean TRUE if the key is valid , FALSE if invalid or something went wrong
*/
function checkAPIKey ( $key ) {
try {
$client = new GuzzleHttp\Client ();
$response = $client
-> request ( 'POST' , PORTAL_API , [
'form_params' => [
'key' => $key ,
'action' => " ping "
]
]);
if ( $response -> getStatusCode () === 200 ) {
return true ;
}
return false ;
} catch ( Exception $e ) {
return false ;
}
}
2017-05-13 14:31:18 -06:00
$username = $VARS [ 'username' ];
$password = $VARS [ 'password' ];
2018-09-07 15:03:42 -06:00
$user = User :: byUsername ( $username );
2018-09-07 19:19:07 -06:00
if ( $user -> exists () !== true || (( Login :: auth ( $username , $password ) !== Login :: LOGIN_OK ) && ! checkAPIKey ( $password ))) {
2017-05-13 14:31:18 -06:00
header ( " HTTP/1.1 403 Unauthorized " );
die ( " \" 403 Unauthorized \" " );
}
2017-05-28 23:52:39 -06:00
2018-09-07 19:19:07 -06:00
if ( ! $user -> hasPermission ( " TASKFLOOR " )) {
2017-05-28 23:52:39 -06:00
header ( " HTTP/1.1 403 Unauthorized " );
die ( " \" 403 Unauthorized \" " );
}
2017-05-13 14:31:18 -06:00
// query max results
$max = 20 ;
2018-09-07 19:19:07 -06:00
if ( isset ( $VARS [ 'max' ]) && preg_match ( " /^[0-9]+ $ / " , $VARS [ 'max' ]) === 1 && $VARS [ 'max' ] <= 1000 ) {
2017-05-13 14:31:18 -06:00
$max = ( int ) $VARS [ 'max' ];
}
switch ( $VARS [ 'action' ]) {
case " gettasks " :
2018-09-07 19:19:07 -06:00
$tasks = $database -> query ( " SELECT * FROM assigned_tasks LEFT JOIN tasks ON assigned_tasks.taskid = tasks.taskid WHERE assigned_tasks.userid = ' " . $user -> getUID () . " ' AND assigned_tasks.statusid IN (0,1,3,4) AND taskassignedon <= NOW() AND tasks.deleted = 0 ORDER BY 0 - taskdueby DESC LIMIT $max " ) -> fetchAll ();
2017-05-13 14:31:18 -06:00
$out = [ " status " => " OK " , " maxresults " => $max , " tasks " => []];
foreach ( $tasks as $task ) {
$icon = " ellipsis-h " ;
switch ( $task [ 'statusid' ]) {
case 1 :
$icon = " play " ;
break ;
case 2 :
$icon = " check " ;
break ;
case 3 :
$icon = " pause " ;
break ;
case 4 :
$icon = " exclamation " ;
break ;
}
$out [ 'tasks' ][] = [
2018-01-23 18:57:01 -07:00
" id " => $task [ 'taskid' ],
2017-05-13 14:31:18 -06:00
" title " => $task [ 'tasktitle' ],
" description " => $task [ 'taskdesc' ],
" assigned " => date ( " F j, Y, g:i a " , strtotime ( $task [ 'taskassignedon' ])),
" due " => ( $task [ 'taskdueby' ] > 0 ? date ( " F j, Y, g:i a " , strtotime ( $task [ 'taskdueby' ])) : null ),
" status " => $task [ 'statusid' ],
" icon " => $icon
];
}
exit ( json_encode ( $out ));
case " getmsgs " :
$messages = $database -> select (
'messages' , [
'messageid (id)' ,
'messagetext (text)' ,
'messagedate (date)' ,
'to' ,
'from'
], [
" AND " => [
" OR " => [
2018-09-07 19:19:07 -06:00
" to " => $user -> getUID (),
2018-01-23 16:39:50 -07:00
" to #null " => null ,
2018-09-07 19:19:07 -06:00
" from " => $user -> getUID ()
2017-05-13 14:31:18 -06:00
],
" deleted " => 0
],
" ORDER " => [
" messagedate " => " DESC "
],
" LIMIT " => $max ]
);
$out = [ " status " => " OK " , " maxresults " => $max , " messages " => []];
$usercache = [];
foreach ( $messages as $msg ) {
$to = null ;
if ( ! isset ( $usercache [ $msg [ 'from' ]])) {
2018-09-07 19:19:07 -06:00
$usercache [ $msg [ 'from' ]] = new User ( $msg [ 'from' ]);
2017-05-13 14:31:18 -06:00
}
if ( is_null ( $msg [ 'to' ])) {
$to [ 'name' ] = lang ( " all users " , false );
$to [ 'username' ] = lang ( " all users " , false );
} else {
if ( ! isset ( $usercache [ $msg [ 'to' ]])) {
2018-09-07 19:19:07 -06:00
$usercache [ $msg [ 'to' ]] = new User ( $msg [ 'to' ]);
2017-05-13 14:31:18 -06:00
}
$to = $usercache [ $msg [ 'to' ]];
}
$out [ 'messages' ][ $msg [ 'id' ]] = [
" text " => $msg [ 'text' ],
" from " => [
2018-09-07 19:19:07 -06:00
" username " => $usercache [ $msg [ 'from' ]] -> getUsername (),
" name " => $usercache [ $msg [ 'from' ]] -> getName ()
2017-05-13 14:31:18 -06:00
],
" to " => [
2018-09-07 19:19:07 -06:00
" username " => $to -> getUsername (),
" name " => $to -> getName ()
2017-05-13 14:31:18 -06:00
],
" sent " => date ( " F j, Y, g:i a " , strtotime ( $msg [ 'date' ]))
];
}
exit ( json_encode ( $out ));
2018-01-23 18:57:01 -07:00
case " updatetask " :
2018-09-07 19:19:07 -06:00
if ( ! $database -> has ( 'assigned_tasks' , [ " AND " => [ 'taskid' => $VARS [ 'taskid' ], 'userid' => $user -> getUID ()]])) {
2018-01-23 18:57:01 -07:00
die ( '{"status": "ERROR", "msg": "You are not assigned to this task!"}' );
}
switch ( $VARS [ 'status' ]) {
case " start " :
2018-09-07 19:19:07 -06:00
$database -> update ( 'assigned_tasks' , [ 'starttime' => date ( " Y-m-d H:i:s " ), 'statusid' => 1 ], [ " AND " => [ 'taskid' => $VARS [ 'taskid' ], 'userid' => $user -> getUID ()]]);
2018-01-23 18:57:01 -07:00
break ;
case " resume " :
2018-09-07 19:19:07 -06:00
if ( ! $database -> has ( 'assigned_tasks' , [ " AND " => [ 'taskid' => $VARS [ 'taskid' ], 'userid' => $user -> getUID (), 'starttime[!]' => null ]])) {
2018-01-23 18:57:01 -07:00
die ( '{"status": "ERROR", "msg": "Cannot resume non-started task."}' );
}
2018-09-07 19:19:07 -06:00
$database -> update ( 'assigned_tasks' , [ 'statusid' => 1 ], [ " AND " => [ 'taskid' => $VARS [ 'taskid' ], 'userid' => $user -> getUID ()]]);
2018-01-23 18:57:01 -07:00
break ;
case " finish " :
2018-09-07 19:19:07 -06:00
$database -> update ( 'assigned_tasks' , [ 'endtime' => date ( " Y-m-d H:i:s " ), 'statusid' => 2 ], [ " AND " => [ 'taskid' => $VARS [ 'taskid' ], 'userid' => $user -> getUID ()]]);
2018-01-23 18:57:01 -07:00
break ;
case " pause " :
2018-09-07 19:19:07 -06:00
$database -> update ( 'assigned_tasks' , [ 'statusid' => 3 ], [ " AND " => [ 'taskid' => $VARS [ 'taskid' ], 'userid' => $user -> getUID ()]]);
2018-01-23 18:57:01 -07:00
break ;
case " problem " :
2018-09-07 19:19:07 -06:00
$database -> update ( 'assigned_tasks' , [ 'statusid' => 4 ], [ " AND " => [ 'taskid' => $VARS [ 'taskid' ], 'userid' => $user -> getUID ()]]);
2018-01-23 18:57:01 -07:00
break ;
default :
die ( '{"status": "ERROR", "msg": "Invalid status requested."}' );
}
die ( '{"status": "OK", "msg": "Task updated."}' );
case " sendmsg " :
2018-01-23 19:03:44 -07:00
$msg = strip_tags ( $VARS [ 'msg' ]);
if ( user_exists ( $VARS [ 'to' ])) {
2018-09-07 19:19:07 -06:00
$to = User :: byUsername ( $VARS [ 'to' ]) -> getUID ();
2018-01-23 19:03:44 -07:00
} else {
die ( '{"status": "ERROR", "msg": "Invalid user."}' );
}
if ( is_empty ( $msg )) {
die ( '{"status": "ERROR", "msg": "Missing message."}' );
}
2018-09-07 19:19:07 -06:00
$database -> insert ( 'messages' , [ 'messagetext' => $msg , 'messagedate' => date ( " Y-m-d H:i:s " ), 'from' => $user -> getUID (), 'to' => $to ]);
2018-01-23 19:03:44 -07:00
die ( '{"status": "OK"}' );
2017-05-13 14:31:18 -06:00
default :
header ( " HTTP/1.1 400 Bad Request " );
die ( " \" 400 Bad Request \" " );
2018-04-08 16:49:59 -06:00
}