From 040185cc215026a7c5c1a37d5741cf441ea613e3 Mon Sep 17 00:00:00 2001 From: Loic Blot Date: Wed, 8 Apr 2015 13:07:22 +0000 Subject: [PATCH] Split smscontroller into smscontroller and apicontroller. One for webapp, one for API --- .travis.yml | 17 ---- appinfo/ocsmsapp.php | 11 +++ appinfo/routes.php | 13 +-- controller/apicontroller.php | 166 +++++++++++++++++++++++++++++++++++ controller/smscontroller.php | 121 ------------------------- 5 files changed, 184 insertions(+), 144 deletions(-) delete mode 100644 .travis.yml create mode 100644 controller/apicontroller.php diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 444c6db..0000000 --- a/.travis.yml +++ /dev/null @@ -1,17 +0,0 @@ -language: php -php: - - 5.3 - - 5.4 - - 5.5 - -before_install: - - cd .. - - git clone https://github.com/owncloud/core.git - - mv ocsms core/apps/ - - cd core - - git submodule init - - git submodule update - - cd apps/ocsms - -script: - - phpunit tests \ No newline at end of file diff --git a/appinfo/ocsmsapp.php b/appinfo/ocsmsapp.php index 4c04f6a..dd5e5a4 100644 --- a/appinfo/ocsmsapp.php +++ b/appinfo/ocsmsapp.php @@ -15,6 +15,7 @@ namespace OCA\OcSms\AppInfo; use \OCP\AppFramework\App; +use \OCA\OcSms\Controller\ApiController; use \OCA\OcSms\Controller\SmsController; use \OCA\OcSms\Db\Sms; @@ -84,6 +85,16 @@ class OcSmsApp extends App { ); }); + $container->registerService('ApiController', function($c) use($app) { + return new ApiController( + $c->query('AppName'), + $c->query('Request'), + $c->query('UserId'), + $c->query('SmsMapper'), + $app + ); + }); + /** * Managers */ diff --git a/appinfo/routes.php b/appinfo/routes.php index 4e892e9..ba54209 100644 --- a/appinfo/routes.php +++ b/appinfo/routes.php @@ -15,15 +15,16 @@ $application = new OcSmsApp(); $application->registerRoutes($this, array('routes' => array( array('name' => 'sms#index', 'url' => '/', 'verb' => 'GET'), - array('name' => 'sms#push', 'url' => '/push', 'verb' => 'POST'), - array('name' => 'sms#replace', 'url' => '/replace', 'verb' => 'POST'), - array('name' => 'sms#retrieve_all_ids', 'url' => '/get/smsidlist', 'verb' => 'GET'), - array('name' => 'sms#retrieve_all_ids_with_status', 'url' => '/get/smsidstate', 'verb' => 'GET'), - array('name' => 'sms#retrieve_last_timestamp', 'url' => '/get/lastmsgtime', 'verb' => 'GET'), + array('name' => 'api#push', 'url' => '/push', 'verb' => 'POST'), // Android API + array('name' => 'api#replace', 'url' => '/replace', 'verb' => 'POST'), // Android API + array('name' => 'api#retrieve_all_ids', 'url' => '/get/smsidlist', 'verb' => 'GET'), // Android APIv1 + array('name' => 'api#retrieve_all_ids_with_status', 'url' => '/get/smsidstate', 'verb' => 'GET'), // Android APIv1 + array('name' => 'api#retrieve_last_timestamp', 'url' => '/get/lastmsgtime', 'verb' => 'GET'), // Android APIv1 array('name' => 'sms#retrieve_all_peers', 'url' => '/get/peerlist', 'verb' => 'GET'), array('name' => 'sms#get_conversation', 'url' => '/get/conversation', 'verb' => 'GET'), array('name' => 'sms#check_new_messages', 'url' => '/get/new_messages', 'verb' => 'GET'), - array('name' => 'sms#get_api_version', 'url' => '/get/apiversion', 'verb' => 'GET'), + array('name' => 'api#get_api_version', 'url' => '/get/apiversion', 'verb' => 'GET'), // Android APIv1 array('name' => 'sms#set_country', 'url'=> '/set/country', 'verb' => 'POST'), array('name' => 'sms#get_country', 'url'=> '/get/country', 'verb' => 'GET'), + array('name' => 'api#get_phones_sms_number', 'url' => 'get/phones/smsnumber', 'verb' => 'GET'), // Android APIv2 ))); diff --git a/controller/apicontroller.php b/controller/apicontroller.php new file mode 100644 index 0000000..a893e31 --- /dev/null +++ b/controller/apicontroller.php @@ -0,0 +1,166 @@ + + * @copyright Loic Blot 2014-2015 + */ + +namespace OCA\OcSms\Controller; + + +use \OCP\IRequest; +use \OCP\AppFramework\Controller; +use \OCP\AppFramework\Http\JSONResponse; + +use \OCA\OcSms\AppInfo\OcSmsApp; + +use \OCA\OcSms\Db\SmsMapper; + +class ApiController extends Controller { + + private $app; + private $userId; + private $smsMapper; + private $errorMsg; + + public function __construct ($appName, IRequest $request, $userId, SmsMapper $mapper, OcSmsApp $app) { + parent::__construct($appName, $request); + $this->app = $app; + $this->userId = $userId; + $this->smsMapper = $mapper; + } + + /** + * @NoAdminRequired + * @NoCSRFRequired + */ + public function getApiVersion () { + return new JSONResponse(array("version" => 1)); + } + + /** + * @NoAdminRequired + * @NoCSRFRequired + * + * This function is used by API v1 + * Phone will compare its own message list with this + * message list and send the missing messages + * This call will remain as secure slow sync mode (1 per hour) + */ + public function retrieveAllIds () { + $smsList = $this->smsMapper->getAllIds($this->userId); + return new JSONResponse(array("smslist" => $smsList)); + } + + /** + * @NoAdminRequired + * @NoCSRFRequired + * + * This function is used by API v2 + * Phone will get this ID to push recent messages + * This call will be used combined with retrieveAllIds + * but will be used more times + */ + public function retrieveLastTimestamp () { + $ts = $this->smsMapper->getLastTimestamp($this->userId); + return new JSONResponse(array("timestamp" => $ts)); + } + + /** + * @NoAdminRequired + * @NoCSRFRequired + */ + public function retrieveAllIdsWithStatus () { + $smsList = $this->smsMapper->getAllIdsWithStatus($this->userId); + return new JSONResponse(array("smslist" => $smsList)); + } + + /** + * @NoAdminRequired + * @NoCSRFRequired + */ + public function getPhonesSmsNumber () { + $phoneList = array(); + return new JSONResponse(array("phoneList" => $phoneList)); + } + + /** + * @NoAdminRequired + * @NoCSRFRequired + */ + public function push ($smsCount, $smsDatas) { + if ($this->checkPushStructure($smsCount, $smsDatas, true) === false) { + return new JSONResponse(array("status" => false, "msg" => $this->errorMsg)); + } + + $this->smsMapper->writeToDB($this->userId, $smsDatas); + return new JSONResponse(array("status" => true, "msg" => "OK")); + } + + /** + * @NoAdminRequired + */ + public function replace($smsCount, $smsDatas) { + if ($this->checkPushStructure($smsCount, $smsDatas, true) === false) { + return new JSONResponse(array("status" => false, "msg" => $this->errorMsg)); + } + + $this->smsMapper->writeToDB($this->userId, $smsDatas, true); + return new JSONResponse(array("status" => true, "msg" => "OK")); + } + + private function checkPushStructure ($smsCount, $smsDatas) { + if ($smsCount != count($smsDatas)) { + $this->errorMsg = "Error: sms count invalid"; + return false; + } + + foreach ($smsDatas as &$sms) { + if (!array_key_exists("_id", $sms) || !array_key_exists("read", $sms) || + !array_key_exists("date", $sms) || !array_key_exists("seen", $sms) || + !array_key_exists("mbox", $sms) || !array_key_exists("type", $sms) || + !array_key_exists("body", $sms) || !array_key_exists("address", $sms)) { + $this->errorMsg = "Error: bad SMS entry"; + return false; + } + + if (!is_numeric($sms["_id"])) { + $this->errorMsg = sprintf("Error: Invalid SMS ID '%s'", $sms["_id"]); + return false; + } + + if (!is_numeric($sms["type"])) { + $this->errorMsg = sprintf("Error: Invalid SMS type '%s'", $sms["type"]); + return false; + } + + if (!is_numeric($sms["mbox"]) && $sms["mbox"] != 0 && $sms["mbox"] != 1 && + $sms["mbox"] != 2) { + $this->errorMsg = sprintf("Error: Invalid Mailbox ID '%s'", $sms["mbox"]); + return false; + } + + if ($sms["read"] !== "true" && $sms["read"] !== "false") { + $this->errorMsg = sprintf("Error: Invalid SMS Read state '%s'", $sms["read"]); + return false; + } + + if ($sms["seen"] !== "true" && $sms["seen"] !== "false") { + $this->errorMsg = "Error: Invalid SMS Seen state"; + return false; + } + + if (!is_numeric($sms["date"]) && $sms["date"] != 0 && $sms["date"] != 1) { + $this->errorMsg = "Error: Invalid SMS date"; + return false; + } + + // @ TODO: test address and body ? + } + return true; + } +} diff --git a/controller/smscontroller.php b/controller/smscontroller.php index 4fce87c..3ad942e 100644 --- a/controller/smscontroller.php +++ b/controller/smscontroller.php @@ -67,51 +67,6 @@ class SmsController extends Controller { return new TemplateResponse($this->appName, 'main', $params); } - /** - * @NoAdminRequired - * @NoCSRFRequired - */ - public function getApiVersion () { - return new JSONResponse(array("version" => 1)); - } - - /** - * @NoAdminRequired - * @NoCSRFRequired - * - * This function is used by API v1 - * Phone will compare its own message list with this - * message list and send the missing messages - * This call will remain as secure slow sync mode (1 per hour) - */ - public function retrieveAllIds () { - $smsList = $this->smsMapper->getAllIds($this->userId); - return new JSONResponse(array("smslist" => $smsList)); - } - - /** - * @NoAdminRequired - * @NoCSRFRequired - * - * This function is used by API v2 - * Phone will get this ID to push recent messages - * This call will be used combined with retrieveAllIds - * but will be used more times - */ - public function retrieveLastTimestamp () { - $ts = $this->smsMapper->getLastTimestamp($this->userId); - return new JSONResponse(array("timestamp" => $ts)); - } - - /** - * @NoAdminRequired - * @NoCSRFRequired - */ - public function retrieveAllIdsWithStatus () { - $smsList = $this->smsMapper->getAllIdsWithStatus($this->userId); - return new JSONResponse(array("smslist" => $smsList)); - } - /** * @NoAdminRequired * @NoCSRFRequired @@ -230,82 +185,6 @@ class SmsController extends Controller { return new JSONResponse(array("phonelist" => $phoneList, "contacts" => $contacts, "photos" => $photos)); } - /** - * @NoAdminRequired - * @NoCSRFRequired - */ - public function push ($smsCount, $smsDatas) { - if ($this->checkPushStructure($smsCount, $smsDatas, true) === false) { - return new JSONResponse(array("status" => false, "msg" => $this->errorMsg)); - } - - $this->smsMapper->writeToDB($this->userId, $smsDatas); - return new JSONResponse(array("status" => true, "msg" => "OK")); - } - - /** - * @NoAdminRequired - */ - public function replace($smsCount, $smsDatas) { - if ($this->checkPushStructure($smsCount, $smsDatas, true) === false) { - return new JSONResponse(array("status" => false, "msg" => $this->errorMsg)); - } - - $this->smsMapper->writeToDB($this->userId, $smsDatas, true); - return new JSONResponse(array("status" => true, "msg" => "OK")); - } - - private function checkPushStructure ($smsCount, $smsDatas) { - if ($smsCount != count($smsDatas)) { - $this->errorMsg = "Error: sms count invalid"; - return false; - } - - foreach ($smsDatas as &$sms) { - if (!array_key_exists("_id", $sms) || !array_key_exists("read", $sms) || - !array_key_exists("date", $sms) || !array_key_exists("seen", $sms) || - !array_key_exists("mbox", $sms) || !array_key_exists("type", $sms) || - !array_key_exists("body", $sms) || !array_key_exists("address", $sms)) { - $this->errorMsg = "Error: bad SMS entry"; - return false; - } - - if (!is_numeric($sms["_id"])) { - $this->errorMsg = sprintf("Error: Invalid SMS ID '%s'", $sms["_id"]); - return false; - } - - if (!is_numeric($sms["type"])) { - $this->errorMsg = sprintf("Error: Invalid SMS type '%s'", $sms["type"]); - return false; - } - - if (!is_numeric($sms["mbox"]) && $sms["mbox"] != 0 && $sms["mbox"] != 1 && - $sms["mbox"] != 2) { - $this->errorMsg = sprintf("Error: Invalid Mailbox ID '%s'", $sms["mbox"]); - return false; - } - - if ($sms["read"] !== "true" && $sms["read"] !== "false") { - $this->errorMsg = sprintf("Error: Invalid SMS Read state '%s'", $sms["read"]); - return false; - } - - if ($sms["seen"] !== "true" && $sms["seen"] !== "false") { - $this->errorMsg = "Error: Invalid SMS Seen state"; - return false; - } - - if (!is_numeric($sms["date"]) && $sms["date"] != 0 && $sms["date"] != 1) { - $this->errorMsg = "Error: Invalid SMS date"; - return false; - } - - // @ TODO: test address and body ? - } - return true; - } - /** * @NoAdminRequired */