Last active
August 29, 2015 14:14
-
-
Save thirdknife/0d456fa128b4a2b03379 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once(APPPATH.'/libraries/REST_Controller.php'); | |
require_once(APPPATH . '/libraries/TSBAuth.php'); | |
require_once(APPPATH . '/libraries/DeviceDetect.php'); | |
class Messages extends REST_Controller { | |
var $requestUser = null; | |
public static $DEVICE ; | |
function __construct() { | |
parent::__construct(); | |
$this->load->model('messagedb'); | |
$this->load->model('userdb'); | |
$this->load->model('feedsdb'); | |
Messages::$DEVICE = DeviceDetect::browser(); | |
Messages::$DEVICE = Messages::$DEVICE['browser']; | |
} | |
private function isArgument($i, $name, $count, $arguments) { | |
if ($count > $i) { | |
$argumentName = $arguments[$i]; | |
if ($argumentName == $name) { | |
return true; | |
} | |
else { | |
return false; | |
} | |
} | |
else { | |
return false; | |
} | |
} | |
private function getArgument($i, $count, $arguments) { | |
if ($count > $i) { | |
$argumentVal = $arguments[$i]; | |
return $argumentVal; | |
} | |
else { | |
return null; | |
} | |
} | |
private function sendUnauthorizedResponse() { | |
/* | |
* THIS VIOLATES OUR APPLICATION FLOW | |
* Only for the time being, let the server | |
* redirect it | |
* But eventually, client should be doing the | |
* redirection | |
*/ | |
header("Location: /signin.html"); | |
exit; | |
// $this->response(array("error" => "not authorized")); | |
} | |
private function sendWrongArgumentsResponse() { | |
$this->response(array("error" => "Invalid input parameters")); | |
} | |
protected function early_checks() | |
{ | |
if (isset($_COOKIE['tsat'])) { | |
$user = json_decode($_COOKIE['user']); | |
if (isset($user->id) && $user->id) { | |
if (TSBAuth::isAuthorized($_COOKIE['tsat'])) { | |
$this->requestUser = $user; | |
} else { | |
$this->sendUnauthorizedResponse(); | |
} | |
} else { | |
$this->sendUnauthorizedResponse(); | |
} | |
} else { | |
$this->sendUnauthorizedResponse(); | |
} | |
} | |
public function urgent_post() { | |
$response = new stdClass(); | |
if ($this->userdb->getFeatureUrgent($this->requestUser->business->neighborhood->id)) { | |
$message = $this->post('message'); | |
if (!$message) { | |
$this->sendWrongArgumentsResponse(); | |
} | |
if ($this->userdb->isAdmin($this->requestUser->id)) { | |
if (strlen($message) > 110) { | |
$response->err = 1; | |
$response->status = 0; | |
$response->message = "Message length should be smaller than 110 characters"; | |
$this->response($response); | |
} | |
else { | |
// post message | |
try { | |
if (FeatureToggles::isDemo()) { | |
$response->err = 0; | |
$response->status = 1; | |
$response->message = "Urgent alert sent"; | |
$this->response($response); | |
} | |
else { | |
$data = new stdClass(); | |
$data->nids = array($this->requestUser->business->neighborhood->id); | |
$data->message = $message; | |
$data = json_encode($data); | |
$ch = curl_init(); | |
curl_setopt( | |
$ch, | |
CURLOPT_URL, URGENT_SERVICE_URL . "message?uid=" . $this->requestUser->id . "&at=" . URGENT_SERVICE_TOKEN | |
); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, array( | |
'Content-Type: application/json', | |
)); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); | |
$out = curl_exec($ch); | |
$res = json_decode($out); | |
if (isset($res->message) && $res->message = "Message Posted") { | |
// post message | |
// for feed post type urgent - share with all users | |
$sharing['staff']['owners'] = 1; | |
$sharing['staff']['managers'] = 1; | |
$sharing['staff']['employees'] = 1; | |
$sharing['networks']['neighborhood'] = 1; | |
$sharing['networks']['connections'] = 0; | |
$sharing['networks']['business'] = 0; | |
$subject = "Urgent Alert"; | |
// Tag with urgent message post | |
$tags = array('11'); | |
$message = $this->feedsdb->postMessage( | |
/*$this->requestUser->getId()*/ $this->requestUser->id, | |
/*$this->requestUser->getBusiness()->getId()*/ $this->requestUser->business->id, | |
$sharing, $subject, $message, $tags, 0, null, | |
null, | |
array($this->requestUser->business->neighborhood->id)); | |
$response->err = 0; | |
$response->status = 1; | |
$response->message = "Urgent alert sent"; | |
$response->feed_snippet = $message; | |
$this->response($response); | |
} | |
else { | |
throw new Exception; | |
} | |
} | |
} catch(Exception $e) { | |
$response->err = 1; | |
$response->status = 0; | |
$response->message = "There was an error contacting the urgent app server"; | |
$this->response($response); | |
} | |
} | |
} | |
else { | |
$response->err = 1; | |
$response->status = 0; | |
$response->message = "Only admin users can post urgent alerts"; | |
$this->response($response); | |
} | |
} | |
else { | |
$response->err = 1; | |
$response->status = 0; | |
$response->message = "This feature is not enabled for your neighborhood yet"; | |
$this->response($response); | |
} | |
} | |
/** | |
* @input | |
* to: {users: [id], businesses: [id]} OR null OR '' OR {} (only when thread is null) | |
* subject (only when thread is null): string | |
* message: string | |
* thread: id OR null (null signifies a new thread) | |
* picture: url of the picture OR null | |
*/ | |
public function index_post() { | |
$to = $this->post('to'); | |
$subject = $this->post('subject'); | |
$message = $this->post('message'); | |
$thread = $this->post('thread'); | |
$picture = $this->post('picture'); | |
$files = $this->post('files'); | |
$fileName = $this->post('fileName'); | |
$threadId = null; | |
if (!$subject) { | |
$subject = '<no subject>'; | |
} | |
/* | |
* "message" is mandatory | |
* "thread" id signifies a previously started thread, so "to" and "subject" cannot exist with "thread" | |
* but one of them must be present | |
*/ | |
if ( | |
(!$message) || | |
($to && $thread) || | |
(!$to && !$thread) | |
) { | |
$this->sendWrongArgumentsResponse(); | |
} | |
else { | |
if ($to) { | |
// convert $to array to $to object | |
$to = json_decode($to); | |
if (!$to->users && !$to->businesses) { | |
// $this->sendWrongArgumentsResponse(); | |
} | |
// #log private message created | |
$this->userdb->logEvent(15, $this->requestUser->id, | |
$this->requestUser->business->id, Messages::$DEVICE) ; | |
// create a new thread | |
$threadId = $this->messagedb->createMessageThread( | |
$this->requestUser->id, | |
$to, | |
$subject, | |
$message, | |
$picture | |
); | |
$threadSnippet = $this->messagedb->getThreadSnippetById($threadId, $this->requestUser->id); | |
if (isset($files) && $files && isset($fileName) && $fileName && $files!="undefined" && $fileName!="undefined") { | |
$uploadPath = "uploads/private_messages/"; | |
$fileName = $threadId . $fileName; | |
if (!file_exists($uploadPath)) { | |
$oldumask = umask(0); | |
mkdir($uploadPath, 0777, true); | |
umask($oldumask); | |
} | |
rename($files, | |
$uploadPath . $fileName) ; | |
$this->messagedb->addMessageImage($threadId, $fileName); | |
$this->response(array( | |
"status" => 1, | |
"threadId" => $threadId, | |
"threadSnippet" => $threadSnippet | |
)); | |
} | |
$this->response(array( | |
"status" => 1, | |
"threadId" => $threadId, | |
"threadSnippet" => $threadSnippet | |
)); | |
} | |
else if ($thread) { | |
// convert thread to int | |
$thread = intval($thread); | |
// #log private message replied | |
$this->userdb->logEvent(16, $this->requestUser->id, | |
$this->requestUser->business->id, Messages::$DEVICE) ; | |
// reply to an already started thread | |
$timeOfMessage = time(); | |
$message = $this->messagedb->replyToMessageThread( | |
$thread, | |
$this->requestUser->id, | |
$message, | |
$picture | |
); | |
// #log private message replied | |
$this->userdb->logEvent(16, $this->requestUser->id, | |
$this->requestUser->business->id, Messages::$DEVICE) ; | |
if (isset($files) && $files && isset($fileName) && $fileName && $files!="undefined" && $fileName!="undefined") { | |
$uploadPath = "uploads/private_messages/"; | |
$fileName = $thread . $fileName; | |
if (!file_exists($uploadPath)) { | |
$oldumask = umask(0); | |
mkdir($uploadPath, 0777, true); | |
umask($oldumask); | |
} | |
rename($files, | |
$uploadPath . $fileName) ; | |
$this->messagedb->addMessageReplyImage($message['messages'][0]->message_id, $fileName); | |
$message = $this->messagedb->getMessagesOfThread($thread, | |
$this->requestUser->id, | |
$timeOfMessage); | |
$this->response(array( | |
"status" => 1, | |
"message" => $message, | |
)); | |
} | |
$this->response(array( | |
"status" => 1, | |
"message" => $message, | |
)); | |
} | |
else { | |
$this->sendWrongArgumentsResponse(); | |
} | |
} | |
} | |
/** | |
* get list of threads for a current user | |
* | |
* custom limit will only be applied if offset is | |
* supplied otherwise limit will default to 10 | |
* | |
* @input for no id provided | |
* l = limit | |
* o = offset_ts in unix_timestamp | |
* gets current user from cookies | |
* | |
* @input for id | |
* l = limit | |
* r = only get messages which are newer than r (unix_timestamp) | |
* o = if o=1, only get messages which are older than r | |
*/ | |
public function threads_get() { | |
$count = func_num_args(); | |
$arguments = func_get_args(); | |
// get messages for a specific ID | |
if ($this->isArgument(0, "id", $count, $arguments)) { | |
// process input | |
$limit = $this->get('l'); | |
$readTs = $this->get('r'); | |
$older = $this->get('o'); | |
// if limit and readTs and older provided | |
// get messages older than readTs | |
// and limit them up to limit | |
if ($limit && $readTs && $older) { | |
$response = $this->messagedb->getMessagesOfThread( | |
$this->getArgument(1, $count, $arguments), | |
$this->requestUser->id, | |
$readTs, | |
$limit, | |
true | |
); | |
} | |
// if limit and readTs provided | |
// get messages newer than readTs | |
// and limit them up to limit | |
else if ($limit && $readTs) { | |
$response = $this->messagedb->getMessagesOfThread( | |
$this->getArgument(1, $count, $arguments), | |
$this->requestUser->id, | |
$readTs, | |
$limit | |
); | |
} | |
else { | |
$response = $this->messagedb->getMessagesOfThread( | |
$this->getArgument(1, $count, $arguments), | |
$this->requestUser->id | |
); | |
} | |
$this->response($response); | |
} | |
// get all threads for a user | |
else { | |
$limit = $this->get('l'); | |
$offsetTs = $this->get('o'); | |
if ($limit && $offsetTs) { | |
$threads = $this->messagedb->getMessageThreadsOfUser( | |
$this->requestUser->id, | |
$limit, | |
$offsetTs | |
); | |
} | |
else { | |
$threads = $this->messagedb->getMessageThreadsOfUser( | |
$this->requestUser->id | |
); | |
} | |
$this->response($threads); | |
} | |
} | |
public function threads_post() { | |
$count = func_num_args(); | |
$arguments = func_get_args(); | |
$threadId = $this->getArgument(1, $count, $arguments); | |
// all implemented operations require $threadId | |
if (!$threadId) { | |
$this->sendWrongArgumentsResponse(); | |
} | |
if ($this->isArgument(2, "delete", $count, $arguments)) { | |
// delete the thread for this user | |
$response = $this->messagedb->deleteThread( | |
$threadId, | |
$this->requestUser->id | |
); | |
$this->response($response); | |
} | |
else { | |
$this->sendWrongArgumentsResponse(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment