PageRenderTime 40ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/parse.php

https://gitlab.com/fabiorf/parse.com-php-library
PHP | 220 lines | 178 code | 26 blank | 16 comment | 31 complexity | 6d2f19164c878ec03cf43cc9cc661bc3 MD5 | raw file
  1. <?php
  2. include 'parseConfig.php';
  3. include 'parseObject.php';
  4. include 'parseQuery.php';
  5. include 'parseUser.php';
  6. include 'parseFile.php';
  7. include 'parsePush.php';
  8. include 'parseGeoPoint.php';
  9. include 'parseACL.php';
  10. include 'parseCloud.php';
  11. class parseRestClient{
  12. private $_appid = '';
  13. private $_masterkey = '';
  14. private $_restkey = '';
  15. private $_parseurl = '';
  16. public $data;
  17. public $requestUrl = '';
  18. public $returnData = '';
  19. public function __construct(){
  20. $parseConfig = new parseConfig;
  21. $this->_appid = $parseConfig::APPID;
  22. $this->_masterkey = $parseConfig::MASTERKEY;
  23. $this->_restkey = $parseConfig::RESTKEY;
  24. $this->_parseurl = $parseConfig::PARSEURL;
  25. if(empty($this->_appid) || empty($this->_restkey) || empty($this->_masterkey)){
  26. $this->throwError('You must set your Application ID, Master Key and REST API Key');
  27. }
  28. $version = curl_version();
  29. $ssl_supported = ( $version['features'] & CURL_VERSION_SSL );
  30. if(!$ssl_supported){
  31. $this->throwError('CURL ssl support not found');
  32. }
  33. }
  34. /*
  35. * All requests go through this function
  36. *
  37. *
  38. */
  39. public function request($args){
  40. $isFile = false;
  41. $c = curl_init();
  42. curl_setopt($c, CURLOPT_TIMEOUT, 30);
  43. curl_setopt($c, CURLOPT_USERAGENT, 'parse.com-php-library/2.0');
  44. curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
  45. curl_setopt($c, CURLINFO_HEADER_OUT, true);
  46. if(substr($args['requestUrl'],0,5) == 'files'){
  47. curl_setopt($c, CURLOPT_HTTPHEADER, array(
  48. 'Content-Type: '.$args['contentType'],
  49. 'X-Parse-Application-Id: '.$this->_appid,
  50. 'X-Parse-Master-Key: '.$this->_masterkey
  51. ));
  52. $isFile = true;
  53. }
  54. else if(substr($args['requestUrl'],0,5) == 'users' && isset($args['sessionToken'])){
  55. curl_setopt($c, CURLOPT_HTTPHEADER, array(
  56. 'Content-Type: application/json',
  57. 'X-Parse-Application-Id: '.$this->_appid,
  58. 'X-Parse-REST-API-Key: '.$this->_restkey,
  59. 'X-Parse-Session-Token: '.$args['sessionToken']
  60. ));
  61. }
  62. else{
  63. curl_setopt($c, CURLOPT_HTTPHEADER, array(
  64. 'Content-Type: application/json',
  65. 'X-Parse-Application-Id: '.$this->_appid,
  66. 'X-Parse-REST-API-Key: '.$this->_restkey,
  67. 'X-Parse-Master-Key: '.$this->_masterkey
  68. ));
  69. }
  70. curl_setopt($c, CURLOPT_CUSTOMREQUEST, $args['method']);
  71. $url = $this->_parseurl . $args['requestUrl'];
  72. if($args['method'] == 'PUT' || $args['method'] == 'POST'){
  73. if($isFile){
  74. $postData = $args['data'];
  75. }
  76. else{
  77. $postData = json_encode($args['data']);
  78. }
  79. curl_setopt($c, CURLOPT_POSTFIELDS, $postData );
  80. }
  81. if($args['requestUrl'] == 'login'){
  82. $urlParams = http_build_query($args['data'], '', '&');
  83. $url = $url.'?'.$urlParams;
  84. }
  85. if(array_key_exists('urlParams',$args)){
  86. $urlParams = http_build_query($args['urlParams'], '', '&');
  87. $url = $url.'?'.$urlParams;
  88. }
  89. curl_setopt($c, CURLOPT_URL, $url);
  90. $response = curl_exec($c);
  91. $responseCode = curl_getinfo($c, CURLINFO_HTTP_CODE);
  92. $expectedCode = array('200');
  93. if($args['method'] == 'POST' && substr($args['requestUrl'],0,4) != 'push'){
  94. // checking if it is not cloud code - it returns code 200
  95. if(substr($args['requestUrl'],0,9) != 'functions'){
  96. $expectedCode = array('200','201');
  97. }
  98. }
  99. //BELOW HELPS WITH DEBUGGING
  100. /*
  101. if(!in_array($responseCode,$expectedCode)){
  102. //print_r($response);
  103. //print_r($args);
  104. }
  105. */
  106. return $this->checkResponse($response,$responseCode,$expectedCode);
  107. }
  108. public function dataType($type,$params){
  109. if($type != ''){
  110. switch($type){
  111. case 'date':
  112. $return = array(
  113. "__type" => "Date",
  114. "iso" => date("c", strtotime($params))
  115. );
  116. break;
  117. case 'bytes':
  118. $return = array(
  119. "__type" => "Bytes",
  120. "base64" => base64_encode($params)
  121. );
  122. break;
  123. case 'pointer':
  124. $return = array(
  125. "__type" => "Pointer",
  126. "className" => $params[0],
  127. "objectId" => $params[1]
  128. );
  129. break;
  130. case 'geopoint':
  131. $return = array(
  132. "__type" => "GeoPoint",
  133. "latitude" => floatval($params[0]),
  134. "longitude" => floatval($params[1])
  135. );
  136. break;
  137. case 'file':
  138. $return = array(
  139. "__type" => "File",
  140. "name" => $params[0],
  141. );
  142. break;
  143. case 'increment':
  144. $return = array(
  145. "__op" => "Increment",
  146. "amount" => $params[0]
  147. );
  148. break;
  149. case 'decrement':
  150. $return = array(
  151. "__op" => "Decrement",
  152. "amount" => $params[0]
  153. );
  154. break;
  155. default:
  156. $return = false;
  157. break;
  158. }
  159. return $return;
  160. }
  161. }
  162. public function throwError($msg,$code=0){
  163. throw new ParseLibraryException($msg,$code);
  164. }
  165. private function checkResponse($response,$responseCode,$expectedCode){
  166. //TODO: Need to also check for response for a correct result from parse.com
  167. if(!in_array($responseCode,$expectedCode)){
  168. $error = json_decode($response);
  169. $this->throwError($error->error,$error->code);
  170. }
  171. else{
  172. //check for empty return
  173. if($response == '{}'){
  174. return true;
  175. }
  176. else{
  177. return json_decode($response);
  178. }
  179. }
  180. }
  181. }
  182. class ParseLibraryException extends Exception{
  183. public function __construct($message, $code = 0, Exception $previous = null) {
  184. //codes are only set by a parse.com error
  185. if($code != 0){
  186. $message = "parse.com error: ".$message;
  187. }
  188. parent::__construct($message, $code, $previous);
  189. }
  190. public function __toString() {
  191. return __CLASS__ . ": [{$this->code}]: {$this->message}\n";
  192. }
  193. }
  194. ?>