├── .gitignore ├── backendless ├── autoload.php └── src │ ├── Backendless.php │ ├── BackendlessAutoloader.php │ ├── commons │ ├── IBackendlessService.php │ └── InvocationContext.php │ ├── exception │ └── BackendlessException.php │ ├── lib │ ├── HttpRequest.php │ ├── Log.php │ └── RequestBuilder.php │ ├── model │ ├── BackendlessCollection.php │ ├── BackendlessUser.php │ ├── Data.php │ ├── GeoCategory.php │ └── GeoPoint.php │ └── services │ ├── Cache.php │ ├── Counters.php │ ├── Events.php │ ├── Files.php │ ├── Geo.php │ ├── Logging.php │ ├── Messaging.php │ ├── Persistence.php │ ├── UserService.php │ ├── files │ ├── File.php │ └── FileInfo.php │ ├── geo │ └── BackendlessGeoQuery.php │ ├── logging │ └── Logger.php │ ├── messaging │ ├── DeliveryOptions.php │ ├── Message.php │ ├── PublishOptions.php │ └── SubscriptionOptions.php │ ├── persistence │ └── BackendlessDataQuery.php │ └── user │ ├── FacebookFieldMappings.php │ └── TwitterFieldMappings.php └── composer.json /.gitignore: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Backendless/PHP-SDK/7edb137c3e6e43560bc5b2d78542273565278356/.gitignore -------------------------------------------------------------------------------- /backendless/autoload.php: -------------------------------------------------------------------------------- 1 | $class_name ]; 163 | 164 | } 165 | 166 | public static function getModelByClass( $class_name ) { 167 | 168 | if( isset( self::$classes_map[ $class_name ] ) ) { 169 | 170 | return self::$classes_map[ $class_name ][ 'class_name' ]; 171 | 172 | } 173 | 174 | return null; 175 | 176 | } 177 | 178 | public static function setInvocationContext( $invocation_context ) { 179 | 180 | self::$InvocationContext = $invocation_context; 181 | 182 | } 183 | 184 | public static function switchOnBlMode() { 185 | 186 | self::$sdk_mode_bl = true; 187 | 188 | } 189 | 190 | public static function switchOffBlMode() { 191 | 192 | self::$sdk_mode_bl = false; 193 | 194 | } 195 | 196 | public static function isBlMode() { 197 | 198 | return self::$sdk_mode_bl; 199 | 200 | } 201 | 202 | protected static function phpEnviromentInit() { 203 | 204 | //set default timezone need for WIN and OS X 205 | date_default_timezone_set( 'UTC' ); 206 | 207 | // check if available openssl for use https 208 | if( ! extension_loaded( 'openssl') ) { 209 | 210 | self::$url = preg_replace( '/^http:\/\/|https:\/\/(.*)$/', 'http://${1}', self::$url ); 211 | 212 | } 213 | 214 | } 215 | 216 | public static function devMode() { 217 | 218 | define( 'DEV_MODE', true ); 219 | Log::init(); 220 | 221 | } 222 | 223 | 224 | } Backendless::staticConstruct(); 225 | -------------------------------------------------------------------------------- /backendless/src/BackendlessAutoloader.php: -------------------------------------------------------------------------------- 1 | prefixes[$prefix]) === false) { 39 | self::$_instance->prefixes[$prefix] = array(); 40 | } 41 | 42 | if ($prepend) { 43 | array_unshift(self::$_instance->prefixes[$prefix], $base_dir); 44 | } else { 45 | array_push(self::$_instance->prefixes[$prefix], $base_dir); 46 | } 47 | } 48 | 49 | public function loadClass($class) { 50 | 51 | $prefix = $class; 52 | 53 | while (false !== $pos = strrpos($prefix, '\\')) { 54 | 55 | $prefix = substr($class, 0, $pos + 1); 56 | $relative_class = substr($class, $pos + 1); 57 | 58 | if ( $this->loadFile($prefix, $relative_class) ) { 59 | return true; 60 | } 61 | 62 | $prefix = rtrim($prefix, '\\'); 63 | } 64 | 65 | return false; 66 | } 67 | 68 | protected function loadFile($prefix, $relative_class) { 69 | 70 | if (isset($this->prefixes[$prefix]) === false) { 71 | return false; 72 | } 73 | 74 | foreach ($this->prefixes[$prefix] as $base_dir) { 75 | 76 | $file = $base_dir . str_replace('\\', DS, $relative_class) . '.php'; 77 | 78 | if ($this->requireFile($file)) { 79 | return true; 80 | } 81 | } 82 | 83 | return false; 84 | } 85 | 86 | protected function requireFile($file) { 87 | 88 | if (file_exists($file)) { 89 | require $file; 90 | return true; 91 | } 92 | 93 | return false; 94 | 95 | } 96 | 97 | } 98 | -------------------------------------------------------------------------------- /backendless/src/commons/IBackendlessService.php: -------------------------------------------------------------------------------- 1 | app_id = $data_array[ 'appId' ]; 18 | $this->user_id = $data_array[ 'userId' ]; 19 | $this->user_roles = $data_array[ 'userRoles' ]; 20 | $this->device_type = $data_array[ 'deviceType' ]; 21 | $this->configuration_items = $data_array[ 'configurationItems' ]; 22 | 23 | } 24 | 25 | } 26 | 27 | } -------------------------------------------------------------------------------- /backendless/src/exception/BackendlessException.php: -------------------------------------------------------------------------------- 1 | headers = []; 21 | 22 | } 23 | 24 | private function resetResponse() { 25 | 26 | $this->response = null; 27 | $this->response_code = null; 28 | $this->response_status = null; 29 | $this->response_headers = null; 30 | 31 | } 32 | 33 | public function setTargetUrl( $target ) { 34 | 35 | $target = trim( $target ); 36 | 37 | if( ! preg_match( "/http:\/\/|https:\/\//", $target, $matches ) ) { 38 | 39 | $target = 'http://' . $target; 40 | 41 | } 42 | 43 | $this->target_url = $target; 44 | 45 | return $this; 46 | 47 | } 48 | 49 | public function setHeader( $name, $val ) { 50 | 51 | $this->request_headers[$name] = $val; 52 | 53 | return $this; 54 | 55 | } 56 | 57 | public function request( $content, $method = 'POST' ) { 58 | 59 | $this->resetResponse(); 60 | 61 | if( Backendless::isBlMode() ) { 62 | 63 | $this->setHeader( 'application-type', 'BL' ); 64 | 65 | } 66 | 67 | if( $content !== 'null' ) { 68 | 69 | $this->request_headers[ 'Content-length' ] = strlen( $content ); 70 | 71 | } 72 | 73 | $headers = []; 74 | 75 | if( is_array( $this->request_headers ) ) { 76 | 77 | $headers = array_map( 78 | function ( $v, $k ) { 79 | return sprintf( "%s: %s", $k, $v ); 80 | }, 81 | 82 | $this->request_headers, 83 | array_keys( $this->request_headers ) 84 | 85 | ); 86 | 87 | } 88 | 89 | $http = [ 90 | 91 | 'ignore_errors' => true, 92 | 'method' => $method, 93 | 'header' => implode("\r\n", $headers), 94 | 'timeout' => 10 95 | 96 | ]; 97 | 98 | if( $content !== 'null' ) { 99 | 100 | $http['content'] = $content; 101 | 102 | } 103 | 104 | $context = stream_context_create( 105 | [ 106 | 'http' => $http, 107 | 108 | 'ssl' => [ 109 | 'verify_peer' => false, //1 110 | 'allow_self_signed' => false, 111 | //'cafile' => '/etc/ssl/certs/ca-certificates.crt', // <-- EDIT FOR NON-DEBIAN/UBUNTU SYSTEMS //1 112 | 113 | ] 114 | ] 115 | ); 116 | 117 | 118 | $this->beforeRequest( $http, $method ); 119 | 120 | $this->response = file_get_contents( $this->target_url, false, $context ); 121 | 122 | $this->afterRequest(); 123 | 124 | $this->response_headers = $http_response_header; 125 | 126 | } 127 | 128 | public function getResponseCode(){ 129 | 130 | if( isset( $this->response_code ) ) { 131 | 132 | return $this->response_code; 133 | 134 | } else{ 135 | 136 | $this->parseResponseCode(); 137 | 138 | } 139 | 140 | return $this->response_code; 141 | 142 | } 143 | 144 | public function getResponseStatus() { 145 | 146 | if( isset( $this->response_status ) ) { 147 | 148 | return $this->response_status; 149 | 150 | } else{ 151 | 152 | $this->parseResponseCode(); 153 | } 154 | 155 | return $this->response_status; 156 | 157 | } 158 | 159 | protected function parseResponseCode() { 160 | 161 | foreach ( $this->response_headers as $key => $header ) { 162 | 163 | if ( strpos( $header, 'HTTP' ) !== false ) { 164 | 165 | list( , $this->response_code, $this->response_status) = explode( ' ', $header ); 166 | 167 | } 168 | } 169 | 170 | } 171 | 172 | public function getResponseHeader( $header ) { 173 | 174 | if( isset( $this->response_headers ) ) { 175 | 176 | foreach ( $this->response_headers as $key => $response_header ) { 177 | 178 | if ( stripos( $response_header, $header ) !== false ) { 179 | 180 | list( $headername, $headervalue ) = explode( ":", $response_header ); 181 | return trim( $headervalue ); 182 | 183 | } 184 | } 185 | } 186 | 187 | return null; 188 | 189 | } 190 | 191 | public function getResponse() { 192 | 193 | return $this->response; 194 | 195 | } 196 | 197 | private function beforeRequest( &$http, $method ) { 198 | 199 | if( defined( 'DEV_MODE' ) ) { 200 | 201 | Log::writeInfo( '---------------------------------------------------------------------------------------------', 'file' ); 202 | Log::writeInfo( 'Request to: ' . $this->target_url , 'file' ); 203 | Log::writeInfo( 'Method: ' . $method , 'file' ); 204 | 205 | if( isset( $http[ 'content' ] ) ) { 206 | 207 | Log::writeInfo( 'Request content: \'' . $http[ 'content' ] . '\'', 'file' ); 208 | 209 | } 210 | 211 | echo '\n'; 212 | Log::writeInfo( 'Send request...', 'console' ); 213 | 214 | } 215 | } 216 | 217 | private function afterRequest() { 218 | 219 | if( defined( 'DEV_MODE' ) ) { 220 | 221 | Log::writeInfo( 'Server response: ' . $this->getResponse(), 'file' ); 222 | Log::writeInfo( '---------------------------------------------------------------------------------------------', 'file' ); 223 | 224 | echo 'n'; 225 | Log::writeInfo( 'Processing results', 'console' ); 226 | 227 | } 228 | 229 | } 230 | 231 | } 232 | -------------------------------------------------------------------------------- /backendless/src/lib/Log.php: -------------------------------------------------------------------------------- 1 | '0;34', 'yellow' => '1;33', 'red' => '0;31' ]; 12 | 13 | public static function init( ) { 14 | 15 | self::$log_path = dirname(dirname(dirname(__FILE__))) . DIRECTORY_SEPARATOR . self::$app_log_dir; 16 | 17 | if( !file_exists( self::$log_path ) ) { 18 | 19 | mkdir( self::$log_path ); 20 | 21 | } 22 | 23 | } 24 | 25 | public static function write( $msg, $target = 'all', $colored = true ) { 26 | 27 | self::doWrite( $msg, $target, "\r", $colored, 'none'); 28 | 29 | } 30 | 31 | public static function writeInfo( $msg, $target = 'all', $colored = true ) { 32 | 33 | self::doWrite( $msg, $target, "[INFO]", $colored, 'blue'); 34 | 35 | } 36 | 37 | public static function writeWarn( $msg, $target = 'all', $colored = true ) { 38 | 39 | self::doWrite( $msg, $target, "[WARN]", $colored, 'yellow'); 40 | 41 | } 42 | 43 | public static function writeError( $msg, $target = 'all', $colored = true ) { 44 | 45 | self::doWrite( $msg, $target, "[ERROR]", $colored, 'red'); 46 | 47 | } 48 | 49 | public static function writeTrace( $msg, $target = 'file', $colored = false ) { 50 | 51 | self::doWrite( $msg, $target, "[ERROR]", $colored, 'red'); 52 | 53 | } 54 | 55 | protected static function doWrite( $msg, $target, $msg_prefix, $colored, $color ) { 56 | 57 | if( $colored ) { 58 | 59 | $msg_colored_prefix = self::addColor($msg_prefix, $color ); 60 | 61 | }else{ 62 | 63 | $msg_colored_prefix = $msg_prefix; 64 | 65 | } 66 | 67 | $space = ''; 68 | 69 | if( strlen($msg_prefix) > 1 ) { 70 | 71 | $space = ' '; 72 | 73 | } 74 | 75 | $msg_colored = $msg_colored_prefix . $space . $msg; 76 | $msg = $msg_prefix . $space . $msg; 77 | 78 | if( $target == 'all') { 79 | 80 | self::writeToConsole($msg_colored); 81 | self::writeToFile($msg); 82 | 83 | }elseif( $target == 'console') { 84 | 85 | self::writeToConsole($msg_colored); 86 | 87 | }elseif($target == 'file') { 88 | 89 | self::writeToFile($msg); 90 | 91 | } 92 | } 93 | 94 | protected static function addColor( $string, $color ) { 95 | 96 | $colored_string = $string; 97 | 98 | if( isset(self::$colors[$color]) ) { 99 | 100 | $color_code = self::$colors[$color]; 101 | $colored_string = "\033[" . $color_code . "m" . $string . "\033[0m"; 102 | } 103 | 104 | return $colored_string; 105 | 106 | } 107 | 108 | protected static function writeToConsole( $msg ) { 109 | 110 | echo $msg . "\n"; 111 | 112 | } 113 | 114 | protected static function writeToFile( $msg ) { 115 | 116 | $log_file_path = self::$log_path . DS . self::$app_log_file; 117 | 118 | if( !file_exists($log_file_path) ) { 119 | 120 | file_put_contents($log_file_path, ''); 121 | 122 | } 123 | 124 | file_put_contents( $log_file_path, date("Y-m-d H:i:s" ,time()) ." " . $msg . "\n", FILE_APPEND ); 125 | 126 | } 127 | 128 | } 129 | 130 | 131 | -------------------------------------------------------------------------------- /backendless/src/lib/RequestBuilder.php: -------------------------------------------------------------------------------- 1 | setTargetUrl( $url ) 50 | ->setHeader( 'application-id', Backendless::getApplicationId() ) 51 | ->setHeader( 'secret-key', Backendless::getSecretKey() ) 52 | ->setHeader( 'application-type', 'REST' ) 53 | ->setHeader( 'Accept', '*/*' ) 54 | ->setHeader( 'Content-Type', 'application/json' ); 55 | 56 | foreach ( self::$headers as $heder_n => $h_val ) { 57 | 58 | $http_request->setHeader( $heder_n, $h_val ); 59 | 60 | } 61 | 62 | self::addUserTokenHeader( $http_request ); 63 | 64 | self::$headers = []; 65 | 66 | $http_request->request( json_encode( $data_array ), $method ); 67 | 68 | self::handleError( $http_request ); 69 | 70 | return json_decode( $http_request->getResponse(), true ); 71 | 72 | } 73 | 74 | public static function doRequestWithHeaders( $service = null, $action = null, $data_array = null, $method = 'POST', $headers = [] ) { 75 | 76 | $url = Backendless::getUrl() . '/' . Backendless::getVersion(); 77 | 78 | if( $service !== null ) { 79 | 80 | $url .= '/' . $service; 81 | 82 | } 83 | 84 | if( $action !== null ) { 85 | 86 | $url .= '/' . $action; 87 | 88 | } 89 | 90 | $http_request = new HttpRequest(); 91 | 92 | $http_request->setTargetUrl( $url ) 93 | ->setHeader( 'application-id', Backendless::getApplicationId() ) 94 | ->setHeader( 'secret-key', Backendless::getSecretKey() ); 95 | 96 | 97 | foreach ( $headers as $name => $val ) { 98 | 99 | $http_request->setHeader( $name, $val ); 100 | 101 | } 102 | 103 | foreach ( self::$headers as $heder_n => $h_val ) { 104 | 105 | $http_request->setHeader( $heder_n, $h_val ); 106 | 107 | } 108 | 109 | self::addUserTokenHeader( $http_request ); 110 | 111 | self::$headers = []; 112 | 113 | $http_request->request( json_encode( $data_array ), $method ); 114 | 115 | self::handleError( $http_request ); 116 | 117 | return json_decode( $http_request->getResponse(), true ); 118 | 119 | } 120 | 121 | public static function Get( $url ) { 122 | 123 | $http_request = new HttpRequest(); 124 | 125 | $http_request->setTargetUrl( $url ) 126 | ->setHeader( 'application-id', Backendless::getApplicationId() ) 127 | ->setHeader( 'secret-key', Backendless::getSecretKey() ) 128 | ->setHeader( 'application-type', 'REST' ) 129 | ->setHeader( 'Accept:', '*/*' ) 130 | ->setHeader( 'Content-Type', 'application/json' ); 131 | 132 | foreach ( self::$headers as $heder_n => $h_val ) { 133 | 134 | $http_request->setHeader( $heder_n, $h_val ); 135 | 136 | } 137 | 138 | self::addUserTokenHeader( $http_request ); 139 | 140 | self::$headers = []; 141 | 142 | $http_request->request( '', 'GET' ); 143 | 144 | self::handleError( $http_request ); 145 | 146 | return $http_request->getResponse(); 147 | 148 | } 149 | 150 | protected static function handleError( $http_request ) { 151 | 152 | if( $http_request->getResponseCode() != 200 ) { 153 | 154 | $error = json_decode( $http_request->getResponse(), true ); 155 | 156 | if( !isset( $error[ 'message' ] ) ) { 157 | 158 | throw new BackendlessException( 'API responce ' .$http_request->getResponseStatus() . ' ' . $http_request->getResponseCode() . $http_request->getResponse() ); 159 | 160 | } else { 161 | 162 | throw new BackendlessException( 'Backendless API return error: ' . $error[ 'message' ] . ' Error code:' .$error[ 'code' ] , $error[ 'code' ] ); 163 | 164 | } 165 | 166 | } 167 | 168 | } 169 | 170 | public static function addUserTokenHeader( $http_request ) { 171 | 172 | $current_user = Backendless::$UserService->getCurrentUser(); 173 | 174 | if( $current_user == null ) { 175 | 176 | return; 177 | 178 | } 179 | 180 | $user_token = $current_user->getUserToken(); 181 | 182 | if( $user_token == null ) { 183 | 184 | return; 185 | 186 | } 187 | 188 | $http_request->setHeader( 'user-token', $user_token ); 189 | 190 | } 191 | 192 | } 193 | -------------------------------------------------------------------------------- /backendless/src/model/BackendlessCollection.php: -------------------------------------------------------------------------------- 1 | data = $data; 20 | 21 | } 22 | 23 | static public function prepareSingleItem( $data_item, $convert_type ) { 24 | 25 | $collection_item = new BackendlessCollection( $data_item ); 26 | 27 | switch ( $convert_type ) { 28 | 29 | case "std_class": return $collection_item->convertToStdClasses(); 30 | case "user_class": return $collection_item->convertToUserClasses(); 31 | case "array": return $collection_item->convertToArray(); 32 | 33 | } 34 | 35 | } 36 | 37 | public function getAsObjects( ) { 38 | 39 | return $this->convertToStdClasses(); 40 | 41 | } 42 | 43 | public function getAsArrays() { 44 | 45 | return $this->convertToArray(); 46 | 47 | } 48 | 49 | public function getAsClasses() { 50 | 51 | return $this->convertToUserClasses(); 52 | 53 | } 54 | 55 | public function getAsObject( ) { 56 | 57 | return $this->convertToStdClasses(); 58 | 59 | } 60 | 61 | public function getAsArray() { 62 | 63 | return $this->convertToArray(); 64 | 65 | } 66 | 67 | public function getAsClass() { 68 | 69 | return $this->convertToUserClasses(); 70 | 71 | } 72 | 73 | public function pageSize() { 74 | 75 | if( isset( $this->data["data"] ) ) { 76 | 77 | return count($this->data['data']); 78 | 79 | } else{ 80 | 81 | return count( $this->data ); 82 | 83 | } 84 | 85 | } 86 | 87 | public function totalObjectsCount() { 88 | 89 | if( isset( $this->data["data"] ) ) { 90 | 91 | return $this->data['totalObjects']; 92 | 93 | } else{ 94 | 95 | return count( $this->data ); 96 | 97 | } 98 | 99 | } 100 | 101 | public function loadPrevPage() { 102 | 103 | if( isset( $this->data["nextPage"] ) || isset( $this->stored_next_page_link ) ) { 104 | 105 | $step_value = 0; 106 | 107 | if( isset( $this->data["nextPage"] ) ) { 108 | 109 | $page_link = $this->data["nextPage"]; 110 | $step_value = 2; 111 | 112 | } else { 113 | 114 | $page_link = $this->stored_next_page_link; 115 | $step_value = 1; 116 | 117 | } 118 | 119 | $matches = []; 120 | 121 | preg_match( "/^.*pageSize=(\d+)&offset=(\d+)(.*)$/", $page_link, $matches ); 122 | 123 | $page_size = $matches[1]; 124 | $offset = $matches[2]; 125 | 126 | if( $offset > 0) { 127 | 128 | $new_offset = $offset - ( $page_size * $step_value ); 129 | 130 | if( $new_offset < 0 ) { 131 | 132 | if( isset( $this->data["data"]) ) { 133 | 134 | $this->data = []; 135 | return; 136 | 137 | } 138 | 139 | } 140 | 141 | $prev_page_link = preg_replace('/^(.*&offset=)(\d+)(.*)$/', '${1}' . $new_offset . '${3}', $page_link ); 142 | 143 | $this->data = $this->retrivePageByUrl( $prev_page_link ); 144 | 145 | if( isset( $this->data["nextPage"] ) ) { 146 | 147 | $this->stored_next_page_link = $this->data["nextPage"]; 148 | 149 | } 150 | 151 | } 152 | 153 | }elseif( isset( $this->data["data"]) ) { 154 | 155 | $this->data = []; 156 | 157 | } 158 | 159 | } 160 | 161 | public function loadNextPage() { 162 | 163 | if( isset( $this->data["nextPage"] ) || isset( $this->stored_next_page_link ) ) { 164 | 165 | if( isset( $this->data["nextPage"] ) ) { 166 | 167 | $page_link = $this->data["nextPage"]; 168 | 169 | } else { 170 | 171 | $page_link = $this->stored_next_page_link; 172 | 173 | } 174 | 175 | $this->data = $this->retrivePageByUrl( $page_link ); 176 | 177 | if( isset( $this->data["nextPage"] ) ) { 178 | 179 | $this->stored_next_page_link = $this->data["nextPage"]; 180 | } 181 | 182 | }elseif( isset ($this->data["data"] ) ) { 183 | 184 | $this->data = []; 185 | 186 | } 187 | 188 | } 189 | 190 | public function loadPage( $page_size, $offset ) { 191 | 192 | if( $offset < 0) { 193 | 194 | throw new BackendlessException( 'Argument $offset cannot be less than 0.' ); 195 | 196 | } 197 | 198 | if( $page_size < 0) { 199 | 200 | throw new BackendlessException( 'Argument $page_size cannot be less than 0.' ); 201 | 202 | } 203 | 204 | if( isset( $this->data["nextPage"] ) || isset( $this->stored_next_page_link ) ) { 205 | 206 | if( isset( $this->data["nextPage"] ) ) { 207 | 208 | $page_link = $this->data["nextPage"]; 209 | 210 | } else { 211 | 212 | $page_link = $this->stored_next_page_link; 213 | 214 | } 215 | 216 | $page_link = preg_replace('/^(.*pageSize=)(\d+)(&offset=)(\d+)(.*)$/', '${1}' . $page_size . '${3}'. $offset . '${5}', $page_link ); 217 | 218 | $this->data = $this->retrivePageByUrl( $page_link ); 219 | 220 | if( isset( $this->data["nextPage"] ) ) { 221 | 222 | $this->stored_next_page_link = $this->data["nextPage"]; 223 | } 224 | 225 | 226 | }elseif( isset( $this->data["data"] ) ) { 227 | 228 | $this->data = []; 229 | 230 | } 231 | 232 | } 233 | 234 | 235 | // internal logic 236 | 237 | protected function convertToStdClasses() { 238 | 239 | if( isset( $this->data['data'] ) ) { 240 | 241 | $collection = []; 242 | 243 | foreach ( $this->data['data'] as $index => $collection_item ) { 244 | 245 | $collection[ $index ] = $this->convertToStdClassesItem( $collection_item ); 246 | 247 | } 248 | 249 | return $collection; 250 | 251 | } else { 252 | 253 | return $this->convertToStdClassesItem( $this->data ); 254 | } 255 | 256 | } 257 | 258 | protected function convertToUserClasses() { 259 | 260 | if( isset( $this->data['data']) ) { 261 | 262 | $collection = []; 263 | 264 | foreach ( $this->data['data'] as $index => $collection_item ) { 265 | 266 | $collection[ $index ] = $this->convertToUserClassesItem( $collection_item ); 267 | 268 | } 269 | 270 | return $collection; 271 | 272 | } else { 273 | 274 | return $this->convertToUserClassesItem( $this->data ); 275 | } 276 | 277 | } 278 | 279 | protected function convertToArray() { 280 | 281 | if( isset( $this->data['data']) ) { 282 | 283 | $collection = []; 284 | 285 | foreach ( $this->data['data'] as $index => $collection_item ) { 286 | 287 | $collection[ $index ] = $collection_item; 288 | $this->normalizeArray( $collection[ $index ] ); 289 | 290 | } 291 | 292 | return $collection; 293 | 294 | } else { 295 | 296 | $item = $this->data; 297 | $this->normalizeArray( $item ); 298 | 299 | return $item; 300 | } 301 | 302 | } 303 | 304 | protected function convertToStdClassesItem( $data ) { 305 | 306 | if( isset( $data["___class"] ) ) { 307 | 308 | if( $data["___class"] === "GeoPoint" ) { // if "___class" == GEOPINT need create geopoint 309 | 310 | return $this->fillGeoPoint( $data, "std_class" ); 311 | 312 | } 313 | 314 | $obj = new stdClass(); 315 | $skip_step = false; 316 | 317 | } else { 318 | 319 | // если не задан класс для маппинга модели она остается мултимассивом + рекурсивно проверяются его ключи. 320 | if( is_array( $data ) ) { 321 | 322 | foreach ( $data as $prop_name => $prop_val ) { 323 | 324 | $data[$prop_name] = $this->convertToStdClassesItem( $prop_val ); 325 | } 326 | } 327 | 328 | return $data; 329 | 330 | } 331 | 332 | foreach ( $data as $property_name => $property_value ) { 333 | 334 | if( is_array( $property_value ) ) { 335 | 336 | if( isset( $property_value["___class"] ) ) { 337 | 338 | $obj->{$property_name} = $this->convertToStdClassesItem( $property_value ); 339 | continue; 340 | } 341 | 342 | if( isset( $property_value[0] ) ) { // check is relation one to many 343 | 344 | foreach ( $property_value as $prop_index => $prop_val) { 345 | 346 | if( isset( $prop_val["___class"] ) ) { 347 | 348 | $obj->{$property_name}[$prop_index] = $this->convertToStdClassesItem( $prop_val ); 349 | $skip_step = true; 350 | 351 | } else { 352 | 353 | $obj->{$property_name}[$prop_index] = $prop_val; 354 | 355 | } 356 | 357 | } 358 | 359 | if( $skip_step ) { $skip_step = false; continue;} 360 | 361 | } 362 | 363 | $obj->{$property_name} = $property_value; 364 | continue; 365 | } 366 | 367 | if( $property_name != "___class" ) { 368 | 369 | $obj->{$property_name} = $property_value; 370 | 371 | } else { 372 | 373 | $obj->table_name = $property_value; 374 | 375 | } 376 | } 377 | 378 | return $obj; 379 | 380 | } 381 | 382 | // create geopoint set data to class create and set related objects. 383 | protected function fillGeoPoint( $data, $mode ) { 384 | 385 | $geo_point = new GeoPoint(); 386 | 387 | $props = ( new ReflectionClass( $geo_point ) )->getProperties(); 388 | 389 | foreach ( $props as $prop ) { 390 | 391 | $prop->setAccessible( true ); 392 | 393 | if( isset( $data[ $prop->getName() ] ) ) { 394 | 395 | $prop->setValue( $geo_point, $data[ $prop->getName() ] ); 396 | 397 | if( $prop->getName() == "metadata" ) { 398 | 399 | $metadata = $data[ $prop->getName() ]; 400 | 401 | switch ( $mode ) { 402 | 403 | case "std_class": foreach ( $metadata as $index => $meta_val ) { 404 | 405 | $metadata[ $index ] = $this->convertToStdClassesItem( $meta_val ); 406 | 407 | 408 | } unset( $data["___class"] ); break; 409 | 410 | case "array": foreach ( $metadata as $index => $meta_val ) { 411 | 412 | $metadata[ $index ] = $this->normalizeArray( $meta_val ); 413 | 414 | } break; 415 | 416 | case "user_class": foreach ( $metadata as $index => $meta_val ) { 417 | 418 | $metadata[ $index ] = $this->convertToUserClassesItem( $meta_val ); 419 | 420 | } break; 421 | 422 | } 423 | 424 | $prop->setValue( $geo_point, $metadata ); 425 | 426 | } 427 | 428 | } 429 | 430 | unset( $data[ $prop->getName() ] ); 431 | } 432 | 433 | foreach ( $data as $key => $val) { 434 | 435 | $geo_point->{$key} = $val; 436 | 437 | } 438 | 439 | return $geo_point; 440 | } 441 | 442 | protected function convertToUserClassesItem( $data ) { 443 | 444 | $dont_set_class_for_mapping = false; 445 | 446 | if( isset( $data["___class"] ) ) { 447 | 448 | if( $data["___class"] === "GeoPoint" ) { // if "___class" == GEOPINT need create geopoint 449 | 450 | return $this->fillGeoPoint( $data, "user_class" ); 451 | 452 | } 453 | 454 | $class_name = Backendless::getModelByClass( $data["___class"] ); 455 | 456 | if( $class_name != null ) { 457 | 458 | $obj = $this->getObjectByClass( $class_name ); 459 | 460 | } else { 461 | 462 | $dont_set_class_for_mapping = true; 463 | } 464 | 465 | }else{ 466 | 467 | $dont_set_class_for_mapping = true; 468 | } 469 | 470 | if( $dont_set_class_for_mapping === true) { 471 | // если не задан класс для маппинга модели она остается мултимассивом + рекурсивно проверяются его ключи. 472 | if( is_array( $data ) ) { 473 | 474 | foreach ( $data as $prop_name => $prop_val ) { 475 | 476 | $data[$prop_name] = $this->convertToUserClassesItem( $prop_val ); 477 | } 478 | } 479 | 480 | return $data; 481 | 482 | } 483 | 484 | $props = (new ReflectionClass( $obj ) )->getProperties(); 485 | 486 | foreach ( $props as $prop) { 487 | 488 | $prop->setAccessible(true); 489 | 490 | if( isset( $data[ $prop->getName() ] ) ) { 491 | 492 | if( !is_array( $data[ $prop->getName() ] ) ) { 493 | 494 | $prop->setValue( $obj, $data[ $prop->getName() ] ); 495 | 496 | }else{ 497 | 498 | $prop->setValue( $obj, $this->prepareUserClassRelation( $data[ $prop->getName() ] ) ); 499 | 500 | } 501 | 502 | } 503 | 504 | unset( $data[ $prop->getName() ] ); 505 | } 506 | 507 | // set undeclared in model properties 508 | $this->setUndeclaredProperties( $data, $obj ); 509 | 510 | 511 | return $obj; 512 | 513 | } 514 | 515 | protected function setUndeclaredProperties( &$data, &$obj ) { 516 | 517 | foreach ( $data as $name => $val ) { 518 | 519 | if( !is_array( $data[ $name ] ) ) { 520 | 521 | $obj->{$name} = $val; 522 | 523 | } else { 524 | 525 | $obj->{$name} = $this->prepareUserClassRelation( $data[ $name ] ); 526 | 527 | } 528 | 529 | } 530 | 531 | } 532 | 533 | protected function prepareUserClassRelation( $class_property ) { 534 | 535 | 536 | if( isset( $class_property["___class"] ) ) { 537 | 538 | if( $class_property["___class"] === "GeoPoint" ) { // if "___class" == GEOPINT need create geopoint 539 | 540 | return $this->fillGeoPoint( $class_property, "user_class" ); 541 | 542 | } 543 | 544 | return $this->convertToUserClassesItem( $class_property ); 545 | 546 | } 547 | 548 | if( isset( $class_property[0] ) ) { 549 | 550 | foreach ( $class_property as $propery_index => $prop_value ) { 551 | 552 | if( isset( $prop_value["___class"] ) ) { 553 | 554 | if( $prop_value["___class"] === "GeoPoint" ) { // if "___class" == GEOPINT need create geopoint 555 | 556 | $class_property[$propery_index] = $this->fillGeoPoint( $prop_value, "user_class" ); 557 | 558 | } else { 559 | 560 | $class_property[$propery_index] = $this->convertToUserClassesItem( $prop_value ); 561 | 562 | } 563 | 564 | } 565 | 566 | } 567 | 568 | return $class_property; 569 | 570 | } 571 | 572 | } 573 | 574 | protected function normalizeArray( &$data ) { 575 | 576 | 577 | if ( is_array( $data ) ) { 578 | 579 | if( isset( $data["___class"] ) ) { 580 | 581 | $data["table-name"] = $data["___class"]; 582 | unset( $data["___class"] ); 583 | 584 | if( $data["table-name"] == "GeoPoint" ) { 585 | 586 | $this->normalizeGeoPointInArray( $data ); 587 | 588 | } 589 | 590 | 591 | } 592 | 593 | foreach ( $data as $property_name => $property_value ) { 594 | 595 | if( is_array( $property_value ) ) { 596 | 597 | if( isset($property_value["___class"]) ) { 598 | 599 | $data[$property_name]["table-name"] = $property_value["___class"]; 600 | unset($data[$property_name]["___class"]); 601 | 602 | $this->normalizeArray( $data[$property_name] ); 603 | 604 | if( $data[$property_name]["table-name"] == "GeoPoint" ) { 605 | 606 | $this->normalizeGeoPointInArray( $data[$property_name] ); 607 | 608 | } 609 | 610 | 611 | } 612 | 613 | if( isset( $property_value[0] ) || isset( $property_value["metadata"] ) ) { 614 | 615 | foreach ( $data[$property_name] as $index=>$val ) { 616 | 617 | if( is_array($data[$property_name][$index]) ) { 618 | 619 | $this->normalizeArray( $data[$property_name][$index] ); 620 | 621 | } 622 | 623 | } 624 | 625 | } 626 | 627 | 628 | } 629 | 630 | } 631 | 632 | } 633 | 634 | } 635 | 636 | protected function normalizeGeoPointInArray( &$point ) { 637 | 638 | if( isset( $point["metadata"] ) ) { 639 | 640 | if( is_array( $point["metadata"] ) ) { 641 | 642 | foreach ( $point["metadata"] as $index => $meta_val ) { 643 | 644 | if( is_array( $point["metadata"][$index] ) ) { 645 | 646 | $this->normalizeArray( $point["metadata"][$index] ); 647 | 648 | } 649 | } 650 | } 651 | 652 | } 653 | 654 | } 655 | 656 | protected function getObjectByClass( $class_name ) { 657 | 658 | return new $class_name(); 659 | 660 | } 661 | 662 | public function retrivePageByUrl( $url ) { 663 | 664 | return RequestBuilder::doRequestByUrl( $url, null, 'GET' ); 665 | 666 | } 667 | 668 | } 669 | -------------------------------------------------------------------------------- /backendless/src/model/BackendlessUser.php: -------------------------------------------------------------------------------- 1 | getObjectId(); 16 | 17 | } 18 | 19 | public function getUserToken() { 20 | 21 | return ( isset( $this->{'user-token'} ) ) ? $this->{'user-token'} : null; 22 | 23 | } 24 | 25 | public function unsetUserToken() { 26 | 27 | if ( isset( $this->{'user-token'} ) ) { 28 | 29 | unset( $this->{'user-token'} ); 30 | 31 | } 32 | 33 | } 34 | 35 | public function putProperties( $properties ) { 36 | 37 | if( is_array( $properties) ) { 38 | 39 | foreach ( $properties as $key=>$value ) { 40 | 41 | $this->{ $key } = $value; 42 | 43 | } 44 | 45 | } else { 46 | 47 | throw new BackendlessException( '"putProperties" method argument "$properties" must be array' ); 48 | 49 | } 50 | 51 | } 52 | 53 | public function setProperties( $properties ) { 54 | 55 | $props = ( new ReflectionClass( $this ) )->getProperties(); 56 | 57 | foreach ( $props as $prop ) { 58 | 59 | $prop->setAccessible( true ); 60 | unset( $this->{ $prop->getName() }); 61 | 62 | } 63 | 64 | $object_vars = get_object_vars( $this ); 65 | 66 | foreach ( $object_vars as $name=>$val ) { 67 | 68 | unset( $this->{ $name }); 69 | 70 | } 71 | 72 | if( is_array( $properties) ) { 73 | 74 | foreach ( $properties as $key=>$value ) { 75 | 76 | $this->{ $key } = $value; 77 | 78 | } 79 | 80 | } else { 81 | 82 | throw new BackendlessException( '"setProperties" method argument "$properties" must be array' ); 83 | 84 | } 85 | 86 | } 87 | 88 | public function setProperty( $key, $value) { 89 | 90 | $this->{ $key } = $value; 91 | 92 | } 93 | 94 | public function getProperty( $key ) { 95 | 96 | if( isset( $this->{ $key } ) ) { 97 | 98 | return $this->{ $key }; 99 | 100 | } 101 | 102 | return null; 103 | 104 | } 105 | 106 | public function getProperties() { 107 | 108 | $data_array = []; 109 | 110 | $props = ( new ReflectionClass( $this ) )->getProperties(); 111 | 112 | foreach ( $props as $prop ) { 113 | 114 | $prop->setAccessible( true ); 115 | $data_array[] = $prop->getValue(); 116 | 117 | } 118 | 119 | $object_vars = get_object_vars( $this ); 120 | 121 | return array_merge( $data_array, $object_vars ); 122 | 123 | } 124 | 125 | public function __call( $name, $arguments ) { 126 | 127 | $action_name = substr( $name, 0, 3 ); 128 | $property = substr( $name, 3 ); 129 | 130 | $property = lcfirst( $property ); 131 | 132 | switch ( $action_name ) { 133 | 134 | case 'set': $this->setProperty( $property, $arguments[0] ); break; 135 | 136 | case 'get': return $this->getProperty( $property ); 137 | 138 | default : $action_name = substr( $name, 0, 5 ); 139 | $property = substr( $name, 5 ); 140 | 141 | $property = lcfirst( $property ); 142 | 143 | if( $action_name === 'unset' ) { 144 | 145 | if( isset( $this->{ $property } ) ) { 146 | 147 | unset( $this->{ $property } ); 148 | 149 | } 150 | 151 | } else { 152 | 153 | throw new BackendlessException( "Called undefined function $name." ); 154 | 155 | } 156 | 157 | } 158 | 159 | } 160 | 161 | } 162 | -------------------------------------------------------------------------------- /backendless/src/model/Data.php: -------------------------------------------------------------------------------- 1 | { $key } = $value; 13 | 14 | } 15 | 16 | public function __get( $key ) { 17 | 18 | if( isset( $this->{ $key } ) ) { 19 | 20 | return $this->{ $key }; 21 | 22 | } 23 | 24 | return null; 25 | 26 | } 27 | 28 | public function putProperties( $properties ) { 29 | 30 | if( is_array( $properties) ) { 31 | 32 | foreach ( $properties as $key=>$value ) { 33 | 34 | $this->{ $key } = $value; 35 | 36 | } 37 | 38 | } else { 39 | 40 | throw new BackendlessException( '"putProperties" method argument "$properties" must be array' ); 41 | 42 | } 43 | 44 | } 45 | 46 | public function setProperties( $properties ) { 47 | 48 | $props = ( new ReflectionClass( $this ) )->getProperties(); 49 | 50 | foreach ( $props as $prop ) { 51 | 52 | $prop->setAccessible( true ); 53 | unset( $this->{ $prop->getName() }); 54 | 55 | } 56 | 57 | $object_vars = get_object_vars( $this ); 58 | 59 | foreach ( $object_vars as $name => $val ) { 60 | 61 | unset( $this->{ $name }); 62 | 63 | } 64 | 65 | if( is_array( $properties) ) { 66 | 67 | foreach ( $properties as $key => $value ) { 68 | 69 | $this->{ $key } = $value; 70 | 71 | } 72 | 73 | } else { 74 | 75 | throw new BackendlessException( '"setProperties" method argument "$properties" must be array' ); 76 | 77 | } 78 | 79 | } 80 | 81 | public function setProperty( $key, $value) { 82 | 83 | $this->{ $key } = $value; 84 | 85 | } 86 | 87 | public function getProperty( $key ) { 88 | 89 | if( isset( $this->{ $key } ) ) { 90 | 91 | return $this->{ $key }; 92 | 93 | } 94 | 95 | return null; 96 | 97 | } 98 | 99 | public function getProperties() { 100 | 101 | $data_array = []; 102 | 103 | $props = ( new ReflectionClass( $this ) )->getProperties(); 104 | 105 | foreach ( $props as $prop ) { 106 | 107 | $prop->setAccessible( true ); 108 | $data_array[] = $prop->getValue(); 109 | 110 | } 111 | 112 | $object_vars = get_object_vars( $this ); 113 | 114 | return array_merge( $data_array, $object_vars ); 115 | 116 | } 117 | 118 | public function __call( $name, $arguments ) { 119 | 120 | $action_name = substr( $name, 0, 3 ); 121 | $property = substr( $name, 3 ); 122 | 123 | $property = lcfirst( $property ); 124 | 125 | switch ( $action_name ) { 126 | 127 | case 'set': $this->setProperty( $property, $arguments[ 1 ] ); break; 128 | 129 | case 'get': return $this->getProperty( $property ); 130 | 131 | default : $action_name = substr( $name, 0, 5 ); 132 | $property = substr( $name, 5 ); 133 | 134 | $property = lcfirst( $property ); 135 | 136 | if( $action_name === 'unset' ) { 137 | 138 | if( isset( $this->{ $property } ) ) { 139 | 140 | unset( $this->{ $property } ); 141 | 142 | } 143 | 144 | } else { 145 | 146 | throw new BackendlessException( "Called undefined function $name." ); 147 | 148 | } 149 | 150 | } 151 | 152 | } 153 | 154 | } 155 | -------------------------------------------------------------------------------- /backendless/src/model/GeoCategory.php: -------------------------------------------------------------------------------- 1 | objectId; 18 | } 19 | 20 | 21 | public function getName() { 22 | 23 | return $this->name; 24 | } 25 | 26 | 27 | public function getSize() { 28 | 29 | return $this->size; 30 | } 31 | 32 | public function setName( $name ) { 33 | 34 | $this->name = $name; 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /backendless/src/model/GeoPoint.php: -------------------------------------------------------------------------------- 1 | objectId; 19 | 20 | } 21 | 22 | public function setObjectId( $object_id){ 23 | 24 | $this->objectId = $object_id; 25 | return $this; 26 | 27 | } 28 | 29 | public function getLatitude(){ 30 | 31 | return $this->latitude; 32 | 33 | } 34 | 35 | public function setLatitude( $latitude ){ 36 | 37 | $this->latitude = $latitude; 38 | return $this; 39 | 40 | } 41 | 42 | public function getLongitude(){ 43 | 44 | return $this->longitude; 45 | 46 | } 47 | 48 | public function setlongitude( $longitude ){ 49 | 50 | $this->longitude = $longitude; 51 | return $this; 52 | 53 | } 54 | 55 | public function getCategories(){ 56 | 57 | return $this->categories; 58 | 59 | } 60 | 61 | public function setCategories( $categories ){ 62 | 63 | if( is_array( $categories ) ) { 64 | 65 | $this->categories = $categories; 66 | 67 | }else{ 68 | 69 | $this->categories[] = $categories; 70 | 71 | } 72 | 73 | return $this; 74 | 75 | } 76 | 77 | public function addCategory( $category ){ 78 | 79 | if( ! in_array($category, $this->categories) ) { 80 | 81 | $this->categories[] = $category; 82 | 83 | } 84 | 85 | return $this; 86 | 87 | } 88 | 89 | public function getMetadata(){ 90 | 91 | return $this->metadata; 92 | 93 | } 94 | 95 | public function setMetadata( $metadata ){ 96 | 97 | $this->metadata = $metadata; 98 | return $this; 99 | 100 | } 101 | 102 | public function addMetadata( $key, $data ){ 103 | 104 | $this->metadata[$key] = $data; 105 | return $this; 106 | 107 | } 108 | 109 | } -------------------------------------------------------------------------------- /backendless/src/services/Cache.php: -------------------------------------------------------------------------------- 1 | setPath( $path ); 41 | 42 | } 43 | 44 | $file->validate(); 45 | 46 | $target = Backendless::getUrl() . '/' . Backendless::getApplicationId(). '/' . Backendless::getVersion() . '/files'; 47 | 48 | if( $remote_path !== null ) { 49 | 50 | $remote_path = trim( $remote_path," \t\n\r\0\x0B\\" ); 51 | $remote_path = str_replace( "\\", "/", $remote_path ); 52 | 53 | $target .= '/' . $remote_path; 54 | 55 | } 56 | 57 | $target .= '/' . $file->getFileName(); 58 | 59 | $http_request = new HttpRequest(); 60 | 61 | $multipart_boundary = "------BackendlessFormBoundary" . md5( uniqid() ) . microtime( true ); 62 | 63 | $file_contents = file_get_contents($file->getPath()); 64 | 65 | $content = "--". $multipart_boundary ."\r\n". 66 | "Content-Disposition: form-data; name=\"model-file\"; filename=\"" . $file->getFileName() . "\"\r\n". 67 | "Content-Type: application/json\r\n\r\n". 68 | $file_contents."\r\n"; 69 | 70 | $content .= "--".$multipart_boundary."--\r\n"; 71 | 72 | RequestBuilder::addUserTokenHeader( $http_request ); 73 | 74 | $http_request->setTargetUrl( $target ) 75 | ->setHeader( self::$APP_ID_KEY, Backendless::getApplicationId() ) 76 | ->setHeader( self::$SECRET_KEY, Backendless::getSecretKey() ) 77 | ->setHeader( self::$VERSION, Backendless::getVersion() ) 78 | ->setHeader( 'Content-type', ' multipart/form-data; boundary=' .$multipart_boundary ) 79 | ->request( $content ); 80 | 81 | 82 | if( $http_request->getResponseCode() != 200 ) { 83 | 84 | $error = json_decode( $http_request->getResponse(), true ); 85 | 86 | if( !isset( $error[ 'message' ] ) ) { 87 | 88 | throw new BackendlessException( 'API responce ' .$http_request->getResponseStatus() . ' ' . $http_request->getResponseCode() . $http_request->getResponse() ); 89 | 90 | } else { 91 | 92 | throw new BackendlessException( $error[ 'message' ], $error[ 'code' ] ); 93 | 94 | } 95 | 96 | } 97 | 98 | return json_decode( $http_request->getResponse(), true ); 99 | 100 | } 101 | 102 | public function saveFile( $file_path_name, $file_content = '', $overwrite = false ) { 103 | 104 | if( is_object( $file_path_name ) ) { 105 | 106 | $overwrite = $file_path_name->getOverwrite(); 107 | $file_content = $file_path_name->getFileContent(); 108 | $file_path_name = $file_path_name->getPathWithName(); 109 | 110 | } 111 | 112 | $file_path_name = trim( $file_path_name, " \t\n\r\0\x0B/" ); 113 | 114 | $target = Backendless::getUrl() . "/" . Backendless::getApplicationId(). "/" . Backendless::getVersion() . "/files/binary/" . $file_path_name; 115 | 116 | if( $overwrite ) { $target .= "?overwrite=true"; } 117 | 118 | if( is_array( $file_content ) ) { 119 | 120 | $file_content = implode( $file_content ); 121 | 122 | } 123 | 124 | $file_content = base64_encode( $file_content ); 125 | 126 | $http_request = new HttpRequest(); 127 | 128 | $multipart_boundary ="------BackendlessFormBoundary" . md5( uniqid() ) . microtime( true ); 129 | 130 | $content = "--" . $multipart_boundary . "\r\n". 131 | "Content-Disposition: form-data; name=\"model-file\"; filename=\"" . basename( $file_path_name ) . "\"\r\n". 132 | "Content-Type: text/plain\r\n\r\n". 133 | $file_content."\r\n"; 134 | 135 | $content .= "--" . $multipart_boundary . "--\r\n"; 136 | 137 | RequestBuilder::addUserTokenHeader( $http_request ); 138 | 139 | $http_request->setTargetUrl( $target ) 140 | ->setHeader( self::$APP_ID_KEY, Backendless::getApplicationId() ) 141 | ->setHeader( self::$SECRET_KEY, Backendless::getSecretKey() ) 142 | ->setHeader( self::$VERSION, Backendless::getVersion() ) 143 | ->setHeader( 'Content-type', 'multipart/form-data; boundary=' . $multipart_boundary ) 144 | ->setHeader( 'application-type:', 'REST' ) 145 | ->request( $content ); 146 | 147 | 148 | if( $http_request->getResponseCode() != 200 ) { 149 | 150 | $error = json_decode( $http_request->getResponse(), true ); 151 | 152 | if( !isset( $error[ 'message' ] ) ) { 153 | 154 | throw new BackendlessException( 'API responce ' . $http_request->getResponseStatus() . ' ' . $http_request->getResponseCode() . $http_request->getResponse() ); 155 | 156 | }else{ 157 | 158 | throw new BackendlessException( $error[ 'message' ], $error[ 'code' ] ); 159 | 160 | } 161 | 162 | } 163 | 164 | return json_decode( $http_request->getResponse(), true ); 165 | 166 | } 167 | 168 | public function download( $file_path ) { 169 | 170 | $file_path = trim($file_path); 171 | $file_path = trim($file_path, "\\\/"); 172 | 173 | $url_part = Backendless::getUrl() . "/" . Backendless::getApplicationId() . "/" . Backendless::getVersion(); 174 | 175 | return RequestBuilder::Get( $url_part . '/files/' . $file_path); 176 | 177 | } 178 | 179 | public function remove( $file_path ) { 180 | 181 | $file_path = trim($file_path); 182 | $file_path = trim($file_path, "\\\/"); 183 | 184 | return RequestBuilder::doRequest('files', $file_path, '', 'DELETE'); 185 | 186 | } 187 | 188 | public function removeDirectory( $directory_path ) { 189 | 190 | $directory_path = trim( $directory_path ); 191 | $directory_path = trim($directory_path, "\\\/"); 192 | 193 | return RequestBuilder::doRequest('files', $directory_path, '', 'DELETE'); 194 | 195 | } 196 | 197 | public function renameFile( $old_path_mame, $new_name) { 198 | 199 | $file_path = trim( $old_path_mame ); 200 | $file_path = trim( $old_path_mame, "\\\/" ); 201 | $new_name = trim( $new_name ); 202 | $new_name = trim( $new_name, "\\\/" ); 203 | 204 | $url_part = Backendless::getUrl() . "/" . Backendless::getApplicationId() . "/" . Backendless::getVersion(); 205 | 206 | $request_body = [ 207 | 208 | 'oldPathName' => $old_path_mame, 209 | 'newName' => $new_name 210 | 211 | ]; 212 | 213 | return RequestBuilder::doRequest( 'files', 'rename', $request_body, 'PUT' ); 214 | 215 | 216 | } 217 | 218 | public function copyFile( $source_path_name, $target_path ) { 219 | 220 | $source_path_name = trim( $source_path_name ); 221 | $source_path_name = trim( $source_path_name, "\\\/" ); 222 | $target_path = trim( $target_path ); 223 | $target_path = trim( $target_path, "\\\/" ); 224 | 225 | $url_part = Backendless::getUrl() . "/" . Backendless::getApplicationId() . "/" . Backendless::getVersion(); 226 | 227 | $request_body = [ 228 | 229 | 'sourcePath' => $source_path_name, 230 | 'targetPath' => $target_path 231 | 232 | ]; 233 | 234 | return RequestBuilder::doRequest( 'files', 'copy', $request_body, 'PUT' ); 235 | 236 | } 237 | 238 | public function moveFile( $source_path_name, $target_path ) { 239 | 240 | $source_path_name = trim( $source_path_name ); 241 | $source_path_name = trim( $source_path_name, "\\\/" ); 242 | $target_path = trim( $target_path ); 243 | $target_path = trim( $target_path, "\\\/" ); 244 | 245 | $request_body = [ 246 | 247 | 'sourcePath' => $source_path_name, 248 | 'targetPath' => $target_path 249 | 250 | ]; 251 | 252 | return RequestBuilder::doRequest( 'files', 'move', $request_body, 'PUT' ); 253 | 254 | } 255 | 256 | public function listing( $path, $pattern = null, $recursive = false, $page_size = null, $offset = null ) { 257 | 258 | $path = trim( $path ); 259 | $path = trim( $path, "\\\/" ); 260 | $path = "/" . $path; 261 | 262 | $pattern = trim( $pattern ); 263 | 264 | $url = "" ; 265 | $url = $path; 266 | 267 | $query_data = []; 268 | 269 | if( $pattern != null ) { 270 | 271 | $query_data[ 'pattern' ] = $pattern; 272 | 273 | } 274 | 275 | if( $recursive != false ) { 276 | 277 | $query_data[ 'sub' ] = 'true'; 278 | 279 | } 280 | 281 | if( $page_size != null && $offset != null ) { 282 | 283 | $query_data[ 'pagesize' ] = $page_size; 284 | $query_data[ 'offset' ] = $offset; 285 | 286 | 287 | } 288 | 289 | $query = http_build_query( $query_data); 290 | 291 | if( !empty( $query ) ) { 292 | 293 | $url .= '?' . $query; 294 | 295 | } 296 | 297 | return new BackendlessCollection( RequestBuilder::doRequest( 'files', $url, '', 'GET' ) ); 298 | 299 | } 300 | 301 | public function exists( $file_path ) { 302 | 303 | $file_path = trim( $file_path ); 304 | $file_path = trim( $file_path, "\\\/" ); 305 | 306 | if( empty( $file_path ) ) { 307 | 308 | throw new BackendlessException( 'File path variable empty' ); 309 | 310 | } 311 | 312 | return RequestBuilder::doRequest( 'files/exists', $file_path, '', 'GET' ); 313 | 314 | } 315 | 316 | } 317 | -------------------------------------------------------------------------------- /backendless/src/services/Geo.php: -------------------------------------------------------------------------------- 1 | name; 44 | 45 | } else { 46 | 47 | $category_name = $category->getName(); 48 | 49 | } 50 | 51 | } elseif( is_string( $category ) ) { 52 | 53 | $category_name = $category; 54 | 55 | } elseif( is_array( $category ) ) { 56 | 57 | $category_name = $category[ 'name' ]; 58 | 59 | } 60 | 61 | $result = RequestBuilder::doRequest( 'geo', 'categories/' . $category_name, '', 'PUT' ); 62 | 63 | if( is_object( $category ) ) { 64 | 65 | if( is_a( $category, "stdClass" ) ) { 66 | 67 | foreach ( $result as $prop_name => $val) { 68 | 69 | $category->{$prop_name} = $val; 70 | 71 | } 72 | 73 | } else { 74 | 75 | $props = (new ReflectionClass( $category ))->getProperties(); 76 | 77 | foreach ( $props as $prop ) { 78 | 79 | $prop->setAccessible( true ); 80 | 81 | if( isset( $result[ $prop->getName() ] ) ) { 82 | 83 | $prop->setValue( $category, $result[ $prop->getName() ] ); 84 | 85 | } 86 | 87 | } 88 | 89 | } 90 | 91 | return $category; 92 | 93 | } 94 | 95 | return $result; 96 | 97 | } 98 | 99 | public function deleteCategory( $category ) { 100 | 101 | $category_name = ''; 102 | 103 | if( is_object( $category ) ) { 104 | 105 | if( is_a( $category, "stdClass") ) { 106 | 107 | $category_name = $category->name; 108 | 109 | } else { 110 | 111 | $category_name = $category->getName(); 112 | 113 | } 114 | 115 | } elseif( is_string( $category ) ) { 116 | 117 | $category_name = $category; 118 | 119 | } elseif( is_array( $category ) ) { 120 | 121 | $category_name = $category['name']; 122 | 123 | } 124 | 125 | return RequestBuilder::doRequest( 'geo', 'categories/' . $category_name, '', 'DELETE' )['result']; 126 | 127 | 128 | } 129 | 130 | public function getCategories( $return_array = false ) { 131 | 132 | $result_collection = RequestBuilder::doRequest( 'geo', 'categories', '', 'GET' ); 133 | 134 | if( !$return_array ) { 135 | 136 | foreach ( $result_collection as $index => $data) { 137 | 138 | $result_collection[$index] = new GeoCategory(); 139 | $this->setDataToObject($result_collection[$index], $data); 140 | } 141 | 142 | } 143 | 144 | return $result_collection; 145 | 146 | } 147 | 148 | public function savePoint( $latitude_or_point, $longitude = null, $categories = null, $metadata = null ) { 149 | 150 | 151 | if( $categories != null ) { 152 | 153 | if( !is_array( $categories) ) { throw new BackendlessException( 'Variable $categories must be set as array.' ); } 154 | 155 | } 156 | 157 | if( $metadata != null ) { 158 | 159 | if( !is_array( $metadata ) ) { throw new BackendlessException( 'Variable $metadata must be set as array.' ); } 160 | 161 | } 162 | 163 | $geopoint_data = []; 164 | 165 | if( is_object( $latitude_or_point ) ) { 166 | 167 | if( isset( $latitude_or_point->objectId ) ) { 168 | 169 | return $this->updatePoint( $latitude_or_point ); 170 | 171 | } 172 | 173 | $geopoint_data['categories'] = $latitude_or_point->getCategories(); 174 | $geopoint_data['latitude'] = $latitude_or_point->getLatitude(); 175 | $geopoint_data['longitude'] = $latitude_or_point->getLongitude(); 176 | $geopoint_data['metadata'] = $latitude_or_point->getMetadata(); 177 | 178 | } else { 179 | 180 | $geopoint_data['categories'] = $categories; 181 | $geopoint_data['latitude'] = $latitude_or_point; 182 | $geopoint_data['longitude'] = $longitude; 183 | $geopoint_data['metadata'] = $metadata; 184 | 185 | } 186 | 187 | $this->prepareGeoPointDataBeforeSave( $geopoint_data ); 188 | 189 | $url_data = []; 190 | $url_data[] = 'lat=' . $geopoint_data['latitude']; 191 | $url_data[] = 'lon=' . $geopoint_data['longitude']; 192 | 193 | if( count($geopoint_data['categories']) > 0) { 194 | 195 | if( is_array($geopoint_data['categories']) ) { 196 | 197 | $url_data[] = 'categories=' . implode(',', $geopoint_data['categories']); 198 | 199 | }else{ 200 | 201 | $url_data[] = 'categories=' . $geopoint_data['categories']; 202 | 203 | } 204 | } 205 | 206 | if( isset($geopoint_data["metadata"]) ) { 207 | 208 | $url_data[] = 'metadata=' . urlencode( $geopoint_data['metadata'] ); 209 | 210 | } 211 | 212 | $url_data = implode( '&', $url_data ); 213 | 214 | $result = RequestBuilder::doRequestWithHeaders( 'geo', 'points?' . $url_data, null, 'PUT', ['application-type' => 'REST', 'Accept:' => '*/*'] ); 215 | 216 | $geo_point = new GeoPoint(); 217 | 218 | $this->setDataToObject( $geo_point, $result["geopoint"] ); 219 | 220 | return $geo_point; 221 | 222 | } 223 | 224 | public function updatePoint( $point ) { 225 | 226 | 227 | if( $point->getObjectId() == null ) { 228 | 229 | throw new BackendlessException( 'GeoPoint object missing objectId property needed for update' ); 230 | 231 | } 232 | 233 | $geopoint_data['categories'] = $point->getCategories(); 234 | $geopoint_data['latitude'] = $point->getLatitude(); 235 | $geopoint_data['longitude'] = $point->getLongitude(); 236 | $geopoint_data['metadata'] = $point->getMetadata(); 237 | 238 | $this->prepareGeoPointDataBeforeSave( $geopoint_data ); 239 | 240 | $url_data = []; 241 | $url_data[] = 'lat=' . $geopoint_data['latitude']; 242 | $url_data[] = 'lon=' . $geopoint_data['longitude']; 243 | 244 | if( count($geopoint_data['categories']) > 0) { 245 | 246 | if( is_array($geopoint_data['categories']) ) { 247 | 248 | $url_data[] = 'categories=' . implode(',', $geopoint_data['categories']); 249 | 250 | }else{ 251 | 252 | $url_data[] = 'categories=' . $geopoint_data['categories']; 253 | 254 | } 255 | } 256 | 257 | if( isset($geopoint_data['metadata']) ) { 258 | 259 | $url_data[] = 'metadata=' . urlencode( $geopoint_data['metadata'] ); 260 | 261 | } 262 | 263 | $url_data = implode( '&', $url_data ); 264 | 265 | 266 | $result = RequestBuilder::doRequestWithHeaders( 'geo', 'points/'. $point->getObjectId() . "?" . $url_data, null, 'PUT', ['application-type' => 'REST', 'Accept:' => '*/*'] ); 267 | 268 | $geo_point = new GeoPoint(); 269 | 270 | $this->setDataToObject($geo_point, $result); 271 | 272 | return $geo_point; 273 | 274 | } 275 | 276 | public function removePoint( $point ) { 277 | 278 | $point_id = ''; 279 | 280 | if( is_object( $point ) ) { 281 | 282 | if( $point->getObjectId() != null ) { 283 | 284 | $point_id = $point->getObjectId() ; 285 | 286 | } else { 287 | 288 | throw new BackendlessException( 'GeoPoint object missing objectId property needed for remove point' ); 289 | 290 | } 291 | 292 | } else{ 293 | 294 | $point_id = $point; 295 | 296 | } 297 | 298 | RequestBuilder::doRequest( 'geo', 'points/' . $point_id , '', 'DELETE' ); // API return null if success 299 | 300 | } 301 | 302 | public function getPoints( $geo_query ) { 303 | 304 | return new BackendlessCollection( RequestBuilder::doRequest( 'geo', $geo_query->buildUrl(), '', 'GET')[ 'collection' ] ); 305 | 306 | } 307 | 308 | public function getGeoFencePoints( $geofence_name, $geo_query ){ 309 | 310 | $url = "points?geoFence=" . $geofence_name; 311 | 312 | $url_part = $geo_query->buildCategoryUrl(); 313 | 314 | if( strlen($url_part) >0 ) { 315 | 316 | $url .= "&" . $url_part; 317 | 318 | } 319 | 320 | return new BackendlessCollection( RequestBuilder::doRequest( 'geo', $url, null, 'GET')[ 'collection' ] ); 321 | 322 | } 323 | 324 | public function runOnEnterAction( $geofence_name, $geopoint = null ) { 325 | 326 | return $this->runAction( $action_name = 'onenter', $geofence_name, $geopoint ); 327 | 328 | } 329 | 330 | public function runOnStayAction( $geofence_name, $geopoint = null ) { 331 | 332 | return $this->runAction( $action_name = 'onstay', $geofence_name, $geopoint ); 333 | 334 | } 335 | 336 | public function runOnExitAction( $geofence_name, $geopoint = null ) { 337 | 338 | return $this->runAction( $action_name = 'onexit', $geofence_name, $geopoint ); 339 | 340 | } 341 | 342 | // internal logic 343 | 344 | private function prepareGeoPointDataBeforeSave( &$point_array ) { 345 | 346 | if( count( $point_array[ 'metadata' ] ) >= 1 ) { 347 | 348 | $persistance = Persistence::getInstance(); 349 | $persistance->prepareGeoPointMetadata( $point_array[ 'metadata' ] ); 350 | 351 | } 352 | 353 | $point_array[ 'metadata' ] = json_encode( $point_array[ 'metadata' ] ); 354 | 355 | } 356 | 357 | private function setDataToObject( &$object, $data ) { 358 | 359 | $props = (new ReflectionClass( $object ))->getProperties(); 360 | 361 | foreach ( $props as $prop) { 362 | 363 | $prop->setAccessible( true ); 364 | 365 | if( isset( $data[ $prop->getName() ] ) ) { 366 | 367 | $prop->setValue( $object, $data[ $prop->getName() ] ); 368 | unset( $data[ $prop->getName() ] ); 369 | 370 | } 371 | 372 | } 373 | 374 | foreach ( $data as $key => $val ) { 375 | 376 | $object->{$key} = $val; 377 | 378 | } 379 | 380 | 381 | } 382 | 383 | private function runAction( $action_name, $geofence_name, $geopoint = null ) { 384 | 385 | $url = "fence/" .$action_name . "?geoFence=" . $geofence_name; 386 | 387 | $headers = []; 388 | $headers['application-id'] = Backendless::getApplicationId(); 389 | $headers['secret-key'] = Backendless::getSecretKey(); 390 | $headers['application-type'] = 'REST'; 391 | 392 | $geopoint_data = []; 393 | 394 | if( $geopoint !== null ) { 395 | 396 | if( is_object( $geopoint ) ) { 397 | 398 | $geopoint_data['latitude'] = $geopoint->getLatitude(); 399 | $geopoint_data['longitude'] = $geopoint->getLongitude(); 400 | 401 | } else { 402 | 403 | $geopoint_data = $geopoint; 404 | 405 | } 406 | 407 | $headers['Content-Type'] = 'application/json'; 408 | 409 | } else { 410 | 411 | $geopoint_data = null; 412 | 413 | } 414 | 415 | return RequestBuilder::doRequestWithHeaders( "geo", $url, $geopoint_data, "POST", $headers )["totalObjects"]; 416 | 417 | } 418 | 419 | // public function loadMetadata( $point ) { 420 | 421 | //var_dump($point); 422 | //$result = RequestBuilder::doRequest( 'geo/points/' . $point->objectId .'/metadata', null, 'GET'); 423 | //var_dump($result); 424 | //var_dump($point); 425 | //} 426 | 427 | } 428 | -------------------------------------------------------------------------------- /backendless/src/services/Logging.php: -------------------------------------------------------------------------------- 1 | setName( $logger_name ); 31 | 32 | return $logger; 33 | 34 | } 35 | 36 | } 37 | 38 | -------------------------------------------------------------------------------- /backendless/src/services/Messaging.php: -------------------------------------------------------------------------------- 1 | getChannel(); 36 | 37 | if( $ch !== null ) { 38 | 39 | $channel = $ch; 40 | 41 | } 42 | 43 | } 44 | 45 | $msg_data = ["message" => $msg]; 46 | 47 | $url_part ="/messaging/" . $channel; 48 | 49 | $this->prepareMsgData($msg_data, $publish_options, $delivery_options); 50 | 51 | 52 | return RequestBuilder::doRequestByUrl( Backendless::getUrl() . "/" . Backendless::getVersion() . $url_part, $msg_data, 'POST' ); 53 | 54 | } 55 | 56 | public function cancel( $msg_id ) { 57 | 58 | return RequestBuilder::doRequestByUrl( Backendless::getUrl() . "/" . Backendless::getVersion() . "/messaging/" . $msg_id, '', 'DELETE' ); 59 | 60 | } 61 | 62 | public function subscribe( $channel = null, $subscription_options = null ) { 63 | 64 | $ch = "default"; 65 | 66 | if( $channel !== null ) { 67 | 68 | $ch = $channel; 69 | 70 | } 71 | 72 | $data = []; 73 | 74 | if( $subscription_options !== null ){ 75 | 76 | $this->prepareSubscribeData($data, $subscription_options); 77 | } 78 | 79 | return RequestBuilder::doRequestByUrl( Backendless::getUrl() . "/" . Backendless::getVersion() . "/messaging/" . $ch . "/subscribe", $data, 'POST' )['subscriptionId']; 80 | 81 | } 82 | 83 | public function retrieveMessages( $channel_name, $subscription_id ) { 84 | 85 | $msg_array = RequestBuilder::doRequestByUrl( Backendless::getUrl() . "/" . Backendless::getVersion() . "/messaging/" . $channel_name . "/" . $subscription_id, '', 'GET' ); 86 | 87 | foreach ($msg_array["messages"] as $index=>$msg_data) { 88 | 89 | $msg = new Message(); 90 | 91 | $msg->setData($msg_data["data"]); 92 | $msg->setHeaders($msg_data["headers"]); 93 | $msg->setMessageId($msg_data["messageId"]); 94 | $msg->setPublisherId($msg_data["publisherId"]); 95 | $msg->setTimestamp($msg_data["publishedAt"]/1000); 96 | 97 | $msg_array["messages"][$index] = $msg; 98 | } 99 | 100 | 101 | return $msg_array; 102 | 103 | } 104 | 105 | private function prepareSubscribeData( &$data, $subscription_options ) { 106 | 107 | if( $subscription_options->getSubscriberId() !== null ) { 108 | 109 | $data["subscriberId"] = $subscription_options->getSubscriberId(); 110 | } 111 | 112 | if( $subscription_options->getSubtopic() !== null ) { 113 | 114 | $data["subtopic"] = $subscription_options->getSubtopic(); 115 | } 116 | 117 | if( $subscription_options->getSelector() !== null ) { 118 | 119 | $data["selector"] = $subscription_options->getSelector(); 120 | 121 | } 122 | 123 | } 124 | 125 | private function prepareMsgData( &$msg_data, $publish_options = null, $delivery_options = null) { 126 | 127 | 128 | if( $publish_options !== null ) { 129 | 130 | if( $publish_options->getPublisherId() !== null ) { 131 | 132 | $msg_data["publisherId"] = $publish_options->getPublisherId(); 133 | } 134 | 135 | if( count($publish_options->getHeaders()) > 0 ) { 136 | 137 | $msg_data["headers"] = $publish_options->getHeaders(); 138 | } 139 | 140 | if( $publish_options->getSubtopic() !== null ) { 141 | 142 | $msg_data["subtopic"] = $publish_options->getSubtopic(); 143 | 144 | } 145 | 146 | } 147 | 148 | if( $delivery_options !== null ) { 149 | 150 | if( $delivery_options->getPushBroadcast() !== null ) { 151 | 152 | $msg_data["pushBroadcast"] = $delivery_options->getPushBroadcast(); 153 | } 154 | 155 | if( $delivery_options->getPushPolicy() !== null ) { 156 | 157 | $msg_data["pushPolicy"] = $delivery_options->getPushPolicy(); 158 | } 159 | 160 | if( count($delivery_options->getPushSinglecast()) > 0 ) { 161 | 162 | $msg_data["pushSinglecast"] = $delivery_options->getPushSinglecast(); 163 | } 164 | 165 | 166 | if( $delivery_options->getPublishAt() !== null ) { 167 | 168 | $msg_data["publishAt"] = ( $delivery_options->getPublishAt()*1000 ); 169 | } 170 | 171 | if( $delivery_options->getRepeatEvery() !== null ) { 172 | 173 | $msg_data["repeatEvery"] = $delivery_options->getRepeatEvery(); 174 | } 175 | 176 | if( $delivery_options->getRepeatExpiresAt() !== null ) { 177 | 178 | $msg_data["repeatExpiresAt"] = ( $delivery_options->getRepeatExpiresAt()*1000 ); 179 | 180 | } 181 | } 182 | 183 | 184 | } 185 | 186 | public function sendTextEmail($subject, $body, $to, $attachments = null) { 187 | 188 | return $this->sendEmail($subject, $body, $to, $attachments, $html = false); 189 | 190 | } 191 | 192 | public function sendHTMLEmail($subject, $body, $to, $attachments = null) { 193 | 194 | return $this->sendEmail($subject, $body, $to, $attachments, $html = true); 195 | 196 | } 197 | 198 | public function sendEmail( $subject, $body, $to, $attachments = null, $html ) { 199 | 200 | if( !is_array($to) ) { 201 | 202 | $to = [$to]; 203 | 204 | } 205 | 206 | $data = [ 207 | "subject" => $subject, 208 | "to" => $to 209 | ]; 210 | 211 | if( $html ){ 212 | 213 | $data["bodyparts"] = ["htmlmessage" => $body ]; 214 | 215 | } else { 216 | 217 | $data["bodyparts"] = [ "textmessage" => $body ]; 218 | 219 | } 220 | 221 | if( $attachments !== null ) { 222 | 223 | $data["attachment"] = $attachments; 224 | 225 | } 226 | 227 | return RequestBuilder::doRequestByUrl( Backendless::getUrl() . "/" . Backendless::getVersion() . "/messaging/email", $data, 'POST' ); 228 | 229 | } 230 | 231 | } -------------------------------------------------------------------------------- /backendless/src/services/Persistence.php: -------------------------------------------------------------------------------- 1 | 'Users' 19 | ]; 20 | 21 | protected static $instance; 22 | 23 | private function __construct() { } 24 | 25 | public static function getInstance() { 26 | 27 | if( !isset( self::$instance ) ) { 28 | 29 | self::$instance = new Persistence(); 30 | 31 | } 32 | 33 | return self::$instance; 34 | 35 | } 36 | 37 | public function of( $table_name ) { 38 | 39 | $this->table_name = $table_name; 40 | 41 | return $this; 42 | 43 | } 44 | 45 | public function save( $data ) { 46 | 47 | $data_array = $this->convertDataToArray( $data ); 48 | 49 | $this->cheackTargetTable( $data_array['table'], __METHOD__ ); // if call method of() check target table and table in data structure 50 | $this->unsetTableName(); //delete table name if user call mehod of() 51 | 52 | if( isset( $data_array['data']["objectId"] ) ) { //update 53 | 54 | return BackendlessCollection::prepareSingleItem( 55 | RequestBuilder::doRequest( 'data', $data_array['table'] . '/'. $data_array['data']['objectId'], $data_array['data'], 'PUT' ), 56 | $data_array["type"] 57 | ); 58 | 59 | } else { 60 | 61 | return BackendlessCollection::prepareSingleItem( 62 | RequestBuilder::doRequest( 'data', $data_array['table'], $data_array['data'] ), 63 | $data_array["type"] 64 | ); 65 | 66 | } 67 | 68 | } 69 | 70 | public function update( $data ) { 71 | 72 | $data_array = $this->convertDataToArray( $data ); 73 | 74 | if( !isset( $data_array['data']['objectId'] ) ) { 75 | 76 | throw new BackendlessException( 'Missing objectId property value for update' ); 77 | 78 | } 79 | 80 | $this->cheackTargetTable( $data_array['table'], __METHOD__ ); // if call method of() check target table and table in data structure 81 | $this->unsetTableName(); //delete table name if user call mehod of() 82 | 83 | return BackendlessCollection::prepareSingleItem( 84 | RequestBuilder::doRequest( 'data', $data_array['table'] . '/'. $data_array['data']['objectId'], $data_array['data'], 'PUT' ), 85 | $data_array["type"] 86 | ); 87 | } 88 | 89 | public function updateBulk( $data, $condition ) { 90 | 91 | if( !isset( $condition ) || $condition == '' ) { 92 | 93 | throw new BackendlessException( 'Missing condition for bulk update' ); 94 | 95 | } 96 | 97 | $table = $this->tryGetTableName(); 98 | 99 | $this->cheackTargetTable( $table, __METHOD__ ); // if call method of() check target table and table in data structure 100 | $this->unsetTableName(); 101 | 102 | if( $table != null ) { 103 | 104 | $data[ 'table-name' ] = $table; 105 | 106 | } 107 | 108 | $data_array = $this->convertDataToArray( $data ); 109 | 110 | $this->recursiveDeleteEmptyProps( $data_array['data'] ); 111 | 112 | if( empty( $data_array['data'] ) || $data_array['data'] == null ) { 113 | 114 | throw new BackendlessException("Do not set new value of properties for updating."); 115 | 116 | } 117 | 118 | $condition = 'where=' . urlencode( $condition ); 119 | 120 | return RequestBuilder::doRequest( 'data/bulk', $data_array['table'] . '?' . $condition, $data_array['data'], 'PUT' ); 121 | 122 | } 123 | 124 | public function remove( $data ) { 125 | 126 | $object_id = $this->extractObjectId( $data ); 127 | 128 | if( $object_id == null || $object_id == "" ) { 129 | 130 | throw new BackendlessException( "Missing objectId for remove data", $code = null ); 131 | 132 | } 133 | 134 | $this->cheackTargetTable( $this->extractObjectType( $data ), __METHOD__ ); // if call method of() check target table and table in data structure 135 | 136 | return RequestBuilder::doRequest( 'data', $this->extractObjectType( $data ) . '/' . $object_id, [], 'DELETE' ); 137 | 138 | } 139 | 140 | public function removeBulk( $condition ) { 141 | 142 | if( !isset( $condition ) || $condition == '' ) { 143 | 144 | throw new BackendlessException( "Missing condition for bulk remove" ); 145 | 146 | } 147 | 148 | $condition = 'where=' . urlencode( $condition ); 149 | 150 | return RequestBuilder::doRequest( 'data/bulk', $this->getTableName() . '?' . $condition, [], 'DELETE' ); 151 | 152 | } 153 | 154 | public function findById( $id, $relations_depth = null ) { 155 | 156 | $object_id = null; 157 | 158 | if( is_string( $id ) ) { 159 | 160 | $object_id = $id; 161 | 162 | } else { 163 | 164 | $data_array = $this->convertDataToArray( $id ); 165 | 166 | $this->cheackTargetTable( $data_array['table'] , __METHOD__ ); 167 | 168 | if( isset( $data_array['data']['objectId'] ) ){ 169 | 170 | $object_id = $data_array['data']['objectId']; 171 | 172 | } 173 | 174 | } 175 | 176 | if( $object_id == null ) { 177 | 178 | throw new BackendlessException("Missing objectId for find object by objectId"); 179 | 180 | } 181 | 182 | $relations_depth = ( $relations_depth === null )? "" : "?relationsDepth={$relations_depth}"; 183 | 184 | return ( new BackendlessCollection( RequestBuilder::doRequest( 'data', $this->getTableName() .'/'. $object_id . $relations_depth, null, 'GET') ) )->getAsClass(); 185 | 186 | } 187 | 188 | public function findFirst( $relations_depth = null ) { 189 | 190 | $relations_depth = ( $relations_depth === null )? "" : "?relationsDepth={$relations_depth}"; 191 | 192 | return ( new BackendlessCollection( RequestBuilder::doRequest( 'data', $this->getTableName() . '/first' . $relations_depth, null, 'GET') ) )->getAsClass(); 193 | 194 | } 195 | 196 | public function findLast( $relations_depth = null ) { 197 | 198 | $relations_depth = ( $relations_depth === null )? "" : "?relationsDepth={$relations_depth}"; 199 | 200 | return ( new BackendlessCollection( RequestBuilder::doRequest( 'data', $this->getTableName() . '/last' . $relations_depth, null, 'GET') ) )->getAsClass(); 201 | 202 | } 203 | 204 | public function find( $data_query_or_relation_depth = null ) { 205 | 206 | if( is_a( $data_query_or_relation_depth, '\backendless\services\persistence\BackendlessDataQuery') ) { 207 | 208 | return new BackendlessCollection( RequestBuilder::doRequest( 'data', $this->getTableName() ."?".$data_query_or_relation_depth->buildUrlVars(), null, 'GET') ); 209 | 210 | } else { 211 | 212 | $relations_depth = $data_query_or_relation_depth; 213 | 214 | $relations_depth = ( $relations_depth === null )? "" : "?relationsDepth={$relations_depth}"; 215 | 216 | return new BackendlessCollection( RequestBuilder::doRequest( 'data', $this->getTableName() . $relations_depth, null, 'GET') ); 217 | 218 | } 219 | 220 | } 221 | 222 | public function loadRelations( $data, $relations ) { 223 | 224 | if( !isset($relations) || $relations == '') { 225 | 226 | throw new BackendlessException("Missing relations for load relations"); 227 | 228 | } 229 | 230 | if( !is_array( $relations ) ) { 231 | 232 | throw new BackendlessException("Relations option must contain array of relations"); 233 | 234 | } 235 | 236 | $object_id = $this->extractObjectId( $data ); 237 | 238 | if( $object_id == null ) { 239 | 240 | throw new BackendlessException('Missing objectId for load relations, data structure must be saved and contained in backendless storage before load relations'); 241 | 242 | } 243 | 244 | $query = new BackendlessDataQuery(); 245 | 246 | $query->setRelated( $relations )->setWhereClause( "objectId='{$object_id}'" ); 247 | 248 | return $this->find( $query ); 249 | 250 | } 251 | 252 | public function describe( $entity_name ) { 253 | 254 | return RequestBuilder::doRequest( 'data', $entity_name . "/properties", null, 'GET'); 255 | 256 | } 257 | 258 | 259 | // internal logic 260 | 261 | protected function getTableName( ) { 262 | 263 | if( empty( $this->table_name ) ) { 264 | 265 | throw new BackendlessException('It is impossible get entity name, use the method "..->of("entity_name")->..." to set a specific entity.', $code = null ); 266 | 267 | } 268 | 269 | $table_name = $this->table_name; 270 | 271 | $this->unsetTableName(); 272 | 273 | return $table_name; 274 | 275 | } 276 | 277 | protected function tryGetTableName( ) { 278 | 279 | if( empty( $this->table_name ) ) { 280 | 281 | return null; 282 | 283 | } 284 | 285 | $table_name = $this->table_name; 286 | 287 | $this->unsetTableName(); 288 | 289 | return $table_name; 290 | 291 | } 292 | 293 | protected function readTableName( ) { 294 | 295 | if( empty( $this->table_name ) ) { 296 | 297 | return null; 298 | 299 | } 300 | 301 | return $this->table_name; 302 | 303 | } 304 | 305 | protected function unsetTableName(){ 306 | 307 | unset( $this->table_name ); 308 | 309 | } 310 | 311 | protected function cheackTargetTable( $object_type, $method_name ) { 312 | 313 | $declared_table = $this->readTableName(); 314 | 315 | if( $declared_table != null ) { 316 | 317 | if( array_key_exists( $object_type, $this->aliases ) ) { 318 | 319 | $object_type = $this->aliases[ $object_type ]; 320 | 321 | } 322 | 323 | if( $declared_table != $object_type ) { 324 | 325 | $method_name = explode( "::", $method_name )[1]; 326 | 327 | throw new BackendlessException( "Argument type error in call method: '{$method_name}()'. Argument data structure have type '$object_type' but " 328 | . "sets as '$declared_table'. Make sure that method ...->of('entity_name') equals argument " 329 | . "type of data structure for method '{$method_name}()'", $code = null ); 330 | 331 | } 332 | 333 | } 334 | 335 | } 336 | 337 | protected function convertDataToArray( $data ) { 338 | 339 | $table_name = null; 340 | $data_array = []; 341 | $type = ''; 342 | 343 | if( !is_array( $data ) && is_object( $data ) ) { 344 | 345 | $table_name = ( new ReflectionClass( $data) )->getShortName(); 346 | 347 | if( is_a( $data, "stdClass" ) ) { 348 | 349 | if( isset( $data->table_name ) ) { 350 | 351 | $table_name = $data->table_name; 352 | 353 | } else { 354 | 355 | throw new BackendlessException('Missing property "table_name" in php stdClass'); 356 | 357 | } 358 | 359 | $data_array = $this->getRecursiveData( $data ); 360 | $type = 'std_class'; 361 | 362 | } else { 363 | 364 | $data_array = $this->getRecursiveData( $data ); 365 | $type = 'user_class'; 366 | 367 | } 368 | 369 | } else { 370 | 371 | if( isset( $data[ 'table-name' ] ) ) { 372 | 373 | $table_name = $data[ 'table-name' ]; 374 | 375 | } elseif( isset( $data[ '___class' ] ) ) { 376 | 377 | $table_name = $data[ '___class' ]; 378 | 379 | } else { 380 | 381 | throw new BackendlessException( 'Missing "table-name" in data multi array' ); 382 | 383 | } 384 | 385 | $data_array = $data; 386 | $this->prepareArray( $data_array ); 387 | 388 | $type = 'array'; 389 | 390 | } 391 | 392 | if( isset( $data_array['table-name'] ) ) { 393 | 394 | unset( $data_array['table-name'] ); 395 | 396 | } 397 | 398 | return [ 'table' => $table_name, 'data' => $data_array, 'type' => $type ]; 399 | 400 | } 401 | 402 | // convertation object (or object with relations ) to array 403 | protected function getRecursiveData( $data ) { 404 | 405 | $data_array = []; 406 | 407 | $reflection = new ReflectionClass( $data ); 408 | $props = $reflection->getProperties(); 409 | 410 | foreach ( $props as $prop ) { 411 | 412 | $prop->setAccessible( true ); 413 | $data_array[ $prop->getName() ] = $prop->getValue( $data ); 414 | 415 | } 416 | 417 | // dinamic declared 418 | $obj_vars = get_object_vars( $data ); 419 | 420 | if( isset( $data_array ) && $data_array !== null ){ 421 | 422 | $data_array = array_merge( $data_array, $obj_vars ); 423 | 424 | } else { 425 | 426 | $data_array = $obj_vars; 427 | 428 | } 429 | 430 | $data_array['___class'] = $reflection->getShortName(); 431 | 432 | if( isset( $data_array['table_name'] ) ) { // for std classes 433 | 434 | $data_array['___class'] = $data_array['table_name']; 435 | 436 | unset( $data_array['table_name'] ); 437 | } 438 | 439 | if( isset( $this->aliases[ $data_array['___class'] ] ) ){ 440 | 441 | $data_array['___class'] = $this->aliases[ $data_array['___class'] ]; 442 | 443 | } 444 | 445 | if( $data_array['___class'] == "GeoPoint" ) { 446 | 447 | $this->clearGeoPoint( $data_array ); 448 | 449 | } 450 | 451 | foreach ( $data_array as $data_key => $data_val ) { 452 | 453 | if( gettype( $data_val ) == "object" ) { 454 | 455 | $data_array[$data_key] = $this->getRecursiveData( $data_val ); 456 | 457 | }elseif( is_array( $data_val ) ) { // if relation one to many 458 | 459 | foreach ( $data_val as $index => $val ) { 460 | 461 | if( gettype( $val ) == "object" ) { 462 | 463 | $data_array[$data_key][$index] = $this->getRecursiveData( $val ); 464 | 465 | } 466 | } 467 | 468 | } 469 | } 470 | 471 | return $data_array; 472 | 473 | } 474 | 475 | protected function clearGeoPoint( &$point ) { 476 | 477 | if( empty( $point['objectId'] ) ) { 478 | 479 | unset( $point['objectId'] ); 480 | 481 | } 482 | 483 | if( empty( $point['metadata'] ) ) { 484 | 485 | unset( $point['metadata'] ); 486 | 487 | } 488 | 489 | } 490 | 491 | protected function prepareArray( &$data ) { 492 | 493 | //convert table-name to ___class and convert geopoint to array 494 | 495 | if( is_array( $data ) ) { 496 | 497 | if( isset( $data["table-name"] ) ) { 498 | 499 | $data["___class"] = $data["table-name"]; 500 | unset( $data["table-name"] ); 501 | 502 | if( isset( $this->aliases[ $data['___class'] ] ) ) { 503 | 504 | $data['___class'] = $this->aliases[ $data['___class'] ]; 505 | 506 | } 507 | 508 | foreach ( $data as $property_name => $property_value ) { 509 | 510 | if( is_array( $property_value ) ) { 511 | 512 | if( isset( $property_value["table-name"] ) ) { 513 | 514 | $data[ $property_name ][ '___class' ] = $property_value[ 'table-name' ]; 515 | unset( $data[ $property_name ][ 'table-name'] ); 516 | 517 | if( isset( $this->aliases[ $data[ $property_name ][ '___class' ] ] ) ) { 518 | 519 | $data[ $property_name ][ '___class' ] = $this->aliases[ $data[ $property_name ][ '___class' ] ]; 520 | 521 | } 522 | 523 | $this->prepareArray( $data[ $property_name ] ); 524 | 525 | } 526 | 527 | if( isset( $property_value[0] ) ) { 528 | 529 | foreach ( $data[ $property_name ] as $index => $val ){ 530 | 531 | $this->prepareArray( $data[ $property_name ][$index] ); 532 | 533 | } 534 | 535 | } 536 | 537 | } 538 | // if array have geopoints 539 | $this->prepareGeoPointInArray( $data[$property_name] ); 540 | 541 | } 542 | 543 | } 544 | 545 | } 546 | 547 | $this->prepareGeoPointInArray( $data ); 548 | 549 | } 550 | 551 | protected function recursiveDeleteEmptyProps( &$data ) { 552 | 553 | if( is_array( $data ) ) { 554 | 555 | if( isset( $data["___class"] ) ) { 556 | 557 | foreach ( $data as $property_name => $property_value ) { 558 | 559 | if( empty( $data[$property_name] ) ) { 560 | 561 | unset( $data[$property_name] ); 562 | 563 | } 564 | 565 | if( is_array( $property_value ) ) { 566 | 567 | if( isset( $property_value["___class"] ) ) { 568 | 569 | $this->recursiveDeleteEmptyProps( $data[$property_name] ); 570 | 571 | } 572 | 573 | if( isset( $property_value[0] ) ) { 574 | 575 | foreach ( $data[ $property_name ] as $index => $val ){ 576 | 577 | $this->recursiveDeleteEmptyProps( $data[ $property_name ][$index] ); 578 | 579 | } 580 | 581 | } 582 | 583 | } 584 | 585 | } 586 | 587 | } 588 | 589 | } 590 | 591 | } 592 | 593 | protected function prepareGeoPointInArray( &$property ) { 594 | 595 | if( gettype( $property ) == "object" ) { 596 | 597 | if( ( new ReflectionClass( $property ) )->getShortName() == "GeoPoint" ) { 598 | 599 | $geo_point_array = []; 600 | $metadata = []; 601 | 602 | $props = ( new ReflectionClass( $property ) )->getProperties(); 603 | 604 | foreach ( $props as $prop ) { 605 | 606 | $prop->setAccessible( true ); 607 | 608 | if( $prop->getName() == "objectId" ) { 609 | 610 | if( $prop->getValue( $property ) == "" ) { 611 | 612 | continue; 613 | 614 | } 615 | } 616 | 617 | $geo_point_array[ $prop->getName() ] = $prop->getValue( $property ); 618 | 619 | if( $prop->getName() == "metadata" ) { 620 | 621 | $metadata = $prop->getValue( $property ); 622 | 623 | foreach ( $metadata as $index => $meta_val ) { 624 | 625 | $this->prepareArray( $metadata[$index] ); 626 | 627 | } 628 | 629 | $geo_point_array["metadata"] = $metadata; 630 | $geo_point_array["___class"] = "GeoPoint"; 631 | 632 | if( empty($geo_point_array["metadata"]) ) { 633 | 634 | unset( $geo_point_array["metadata"] ); 635 | 636 | } 637 | } 638 | 639 | } 640 | 641 | $property = $geo_point_array; 642 | 643 | } 644 | 645 | } 646 | 647 | } 648 | 649 | protected function extractObjectId( $object ) { 650 | 651 | if( !is_array( $object ) && is_object( $object ) ) { 652 | 653 | if( is_a( $object, "stdClass" ) ) { 654 | 655 | if( isset( $object->objectId ) ) { 656 | 657 | return $object->objectId; 658 | 659 | } else { 660 | 661 | return null; 662 | 663 | } 664 | 665 | }else{ 666 | 667 | $object_id = null; 668 | 669 | $reflection = new ReflectionClass( $object ); 670 | $props = $reflection->getProperties(); 671 | 672 | foreach ( $props as $prop ) { 673 | 674 | $prop->setAccessible( true ); 675 | 676 | if( $prop->getName() == "objectId" ) { 677 | 678 | $object_id = $prop->getValue( $object ); 679 | 680 | } 681 | 682 | } 683 | 684 | if( $object_id != null ) { 685 | 686 | return $object_id; 687 | 688 | } 689 | 690 | // dinamic declared 691 | return $this->extractObjectId( get_object_vars( $object ) ); 692 | 693 | } 694 | 695 | 696 | } elseif( is_array( $object ) ) { 697 | 698 | if( isset( $object["objectId"] ) ) { 699 | 700 | return $object["objectId"]; 701 | 702 | } else { 703 | 704 | return null; 705 | 706 | } 707 | 708 | } 709 | 710 | return null; 711 | 712 | } 713 | 714 | protected function extractObjectType( $object ) { 715 | 716 | $table_name = null; 717 | 718 | if( !is_array( $object ) && is_object( $object ) ) { 719 | 720 | $table_name = ( new ReflectionClass( $object ) )->getShortName(); 721 | 722 | if( is_a( $object, "stdClass" ) ) { 723 | 724 | if( isset( $object->table_name ) ) { 725 | 726 | $table_name = $object->table_name; 727 | 728 | } 729 | 730 | } 731 | 732 | } else { 733 | 734 | if( isset( $data['table-name'] ) ) { 735 | 736 | $table_name = $data['table-name']; 737 | 738 | } 739 | 740 | } 741 | 742 | if( isset( $this->aliases[ $table_name ] ) ) { 743 | 744 | $table_name = $this->aliases[ $table_name ]; 745 | 746 | } 747 | 748 | return $table_name; 749 | 750 | } 751 | 752 | // method call inside geo service 753 | public function prepareGeoPointMetadata( &$metadata_array ) { 754 | 755 | foreach ( $metadata_array as $meta_name => $meta_val ) { 756 | 757 | if( is_object( $meta_val ) ) { 758 | 759 | $metadata_array[ $meta_name ] = $this->getRecursiveData( $meta_val ); 760 | 761 | } 762 | 763 | } 764 | 765 | } 766 | 767 | } 768 | -------------------------------------------------------------------------------- /backendless/src/services/UserService.php: -------------------------------------------------------------------------------- 1 | putProperties( RequestBuilder::doRequest( 'users', 'register', $user->getProperties() ) ); 42 | 43 | return $user; 44 | 45 | } 46 | 47 | public function login( $login_identity, $password ) { 48 | 49 | if( !isset($login_identity) || !isset($password) ) { 50 | 51 | throw new BackendlessException("Not set login identity or password."); 52 | 53 | } 54 | 55 | $user = new BackendlessUser(); 56 | $user->setLogin( $login_identity ); 57 | $user->setPassword( $password ); 58 | 59 | $user->setProperties( RequestBuilder::doRequest( 'users', 'login', $user->getProperties() ) ); 60 | 61 | $this->setCurrentUser( $user ); 62 | 63 | return $user; 64 | 65 | } 66 | 67 | public function update( $user ) { 68 | 69 | 70 | self::checkUserToBeProperForUpdate( $user ); 71 | 72 | if( ( $user->getUserId() == null && $user->getUserId() == '') || ( $user->getUserToken() == null && $user->getUserToken() == '') ) { 73 | 74 | throw new BackendlessException("User not logged in or wrong user id."); 75 | 76 | } else { 77 | 78 | $user->putProperties( RequestBuilder::doRequest( 'users', $user->getUserId(), $user->getProperties(), 'PUT' ) ); 79 | 80 | return $user; 81 | } 82 | 83 | } 84 | 85 | public function logout( ) { 86 | 87 | RequestBuilder::doRequest('users', 'logout', null, 'GET'); 88 | 89 | $this->unsetCurrentUser(); 90 | 91 | } 92 | 93 | public function restorePassword( $identity ) { 94 | 95 | if( !isset( $identity ) || $identity == null ) { 96 | 97 | throw new BackendlessException( 'Identity cannot be null' ); 98 | 99 | } else { 100 | 101 | RequestBuilder::doRequest( 'users', 'restorepassword/' . urlencode( $identity ), null, 'GET' ); 102 | 103 | } 104 | 105 | } 106 | 107 | public function resendEmailConfirmation( $email_address ) { 108 | 109 | RequestBuilder::doRequest( 'users', 'resendconfirmation?email=' . $email_address , null, 'POST' ); 110 | 111 | } 112 | 113 | public function getCurrentUser() { 114 | 115 | return $this->CurrentUser(); 116 | 117 | } 118 | 119 | public function CurrentUser() { 120 | 121 | if( isset( $this->current_user ) ) { 122 | 123 | return $this->current_user; 124 | 125 | } 126 | 127 | return null; 128 | 129 | } 130 | 131 | public function setCurrentUser( $user ) { 132 | 133 | if ( is_a( $user, '\backendless\model\BackendlessUser' ) ) { 134 | 135 | $this->current_user = $user; 136 | 137 | } else { 138 | 139 | throw new BackendlessException('Method argument var $user must be BackendlessUser class instance'); 140 | 141 | } 142 | 143 | } 144 | 145 | protected function unsetCurrentUser() { 146 | 147 | unset( $this->current_user ); 148 | 149 | } 150 | 151 | 152 | protected static function checkUserToBeProper( $user ) { 153 | 154 | self::checkUserToBeProperForUpdate($user); 155 | 156 | if( $user->getPassword() == null || $user->getPassword() == '' ) { 157 | 158 | throw new BackendlessException("User password cannot be null or empty."); 159 | 160 | } 161 | 162 | } 163 | 164 | protected static function checkUserToBeProperForUpdate( $user ) { 165 | 166 | if( $user == null ) { 167 | 168 | throw new BackendlessException("User cannot be null or empty."); 169 | 170 | } 171 | 172 | } 173 | 174 | public function getUserToken() { 175 | 176 | if( isset( $this->current_user ) ) { 177 | 178 | return $this->current_user->getUserToken(); 179 | 180 | } 181 | 182 | return null; 183 | 184 | } 185 | 186 | } 187 | -------------------------------------------------------------------------------- /backendless/src/services/files/File.php: -------------------------------------------------------------------------------- 1 | path = $path; 16 | return $this; 17 | 18 | } 19 | 20 | public function getPath() { 21 | 22 | return $this->path; 23 | } 24 | 25 | public function validate() { 26 | 27 | if( ! file_exists( $this->path ) ) { 28 | 29 | throw new BackendlessException( 'File with file path {$this->path} does not exist.' ); 30 | 31 | } 32 | 33 | } 34 | 35 | public function setFileName( $file_name ) { 36 | 37 | $this->file_name = $file_name; 38 | return $this; 39 | 40 | } 41 | 42 | 43 | public function getFileName() { 44 | 45 | if( empty( $this->file_name ) ) { 46 | 47 | $this->file_name = basename( $this->path ); 48 | 49 | } 50 | 51 | return $this->file_name; 52 | 53 | } 54 | 55 | public function setFileContent( $content ) { 56 | 57 | $this->file_content = $content; 58 | return $this; 59 | 60 | } 61 | 62 | public function getFileContent(){ 63 | 64 | return $this->file_content; 65 | 66 | } 67 | 68 | public function overwrite( $overwrite = false ) { 69 | 70 | $this->overwrite = $overwrite; 71 | return $this; 72 | 73 | } 74 | 75 | public function getOverwrite() { 76 | 77 | return $this->overwrite; 78 | 79 | } 80 | 81 | public function isOverwrite( ) { 82 | 83 | return $this->overwrite; 84 | 85 | } 86 | 87 | public function getPathWithName() { 88 | 89 | $path_string = ''; 90 | 91 | if ( isset( $this->path ) ) { 92 | 93 | $path_string = trim( $this->path, $charlist = '/' ); 94 | $path_string .= '/'; 95 | 96 | } 97 | 98 | return $path_string .= trim( $this->file_name, $charlist = '/' ); 99 | 100 | } 101 | 102 | } 103 | -------------------------------------------------------------------------------- /backendless/src/services/files/FileInfo.php: -------------------------------------------------------------------------------- 1 | name; 16 | 17 | } 18 | 19 | public function setName( $name ) { 20 | 21 | $this->name = $name; 22 | return $this; 23 | 24 | } 25 | 26 | public function getCreatedOn() { 27 | 28 | return $this->createOn/1000; 29 | 30 | } 31 | 32 | public function setCreatedOn( $create_on ) { 33 | 34 | $this->create_on = $create_on*1000; 35 | return $this; 36 | } 37 | 38 | public function getPublicUrl() { 39 | 40 | return $this->public_url; 41 | 42 | } 43 | 44 | public function setPublicUrl( $public_url ) { 45 | 46 | $this->public_url = $public_url; 47 | return $this; 48 | 49 | } 50 | 51 | public function getURL() { 52 | 53 | return $this->url; 54 | 55 | } 56 | 57 | public function setURL( $url ) { 58 | 59 | $this->url = $url; 60 | return $this; 61 | 62 | } 63 | 64 | public function getSize() { 65 | 66 | return $this->size; 67 | 68 | } 69 | 70 | public function setSize( $size ) { 71 | 72 | $this->size; 73 | 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /backendless/src/services/geo/BackendlessGeoQuery.php: -------------------------------------------------------------------------------- 1 | include_metadata = $val; 44 | 45 | } 46 | 47 | public function addCategory( $category ) { 48 | 49 | if( !in_array($category, $this->categories) ) { 50 | 51 | $this->categories[] = $category; 52 | 53 | } 54 | 55 | } 56 | 57 | public function setCategories( $categories ) { 58 | 59 | $this->categories = $categories; 60 | 61 | } 62 | 63 | public function setMetadata( $metadata ) { 64 | 65 | if( is_array($metadata) ) { 66 | 67 | $this->metadata = $metadata; 68 | 69 | } else { 70 | 71 | throw new BackendlessException( "Metadata must be set as array." ); 72 | 73 | } 74 | 75 | } 76 | 77 | public function setWhereClause( $where_clause ) { 78 | 79 | $this->where_clause = $where_clause; 80 | 81 | } 82 | 83 | public function setPageSize( $page_size ) { 84 | 85 | $this->page_size = $page_size; 86 | 87 | } 88 | 89 | public function setOffset( $offset ){ 90 | 91 | $this->offset = $offset; 92 | 93 | } 94 | 95 | public function setLatitude( $latitude ) { 96 | 97 | $this->latitude = $latitude; 98 | $this->search_type = 'radius'; 99 | 100 | } 101 | 102 | public function setLongitude( $longitude ) { 103 | 104 | $this->longitude = $longitude; 105 | $this->search_type = 'radius'; 106 | 107 | } 108 | 109 | public function setRadius( $radius ) { 110 | 111 | $this->radius = $radius; 112 | $this->search_type = 'radius'; 113 | 114 | } 115 | 116 | public function setUnits( $units ){ 117 | 118 | $this->units = $units; 119 | $this->search_type = 'radius'; 120 | 121 | } 122 | 123 | public function setSearchRectangle( $nw_latitude, $nw_longitude, $se_latitude, $se_longitude ) { 124 | 125 | $this->nw_latitude = $nw_latitude; 126 | $this->nw_longitude = $nw_longitude; 127 | $this->se_latitude = $se_latitude; 128 | $this->se_longitude = $se_longitude; 129 | 130 | $this->search_type = 'rectangle'; 131 | 132 | } 133 | 134 | public function buildUrl() { 135 | 136 | $url = ''; 137 | 138 | switch ( $this->search_type ) { 139 | 140 | case 'category': $url = "points?" . $this->buildCategoryUrl(); break; 141 | 142 | case 'radius' : 143 | $url_part = $this->buildCategoryUrl(); 144 | 145 | $url = "points?"; 146 | $url .= ( $url_part !== null ) ? $url_part . '&' : ''; 147 | 148 | $url .= $this->buildRadiusUrl(); break; 149 | 150 | case 'rectangle': 151 | $url_part = $this->buildCategoryUrl(); 152 | 153 | $url = "rect?"; 154 | $url .= ( $url_part !== null ) ? $url_part . '&' : ''; 155 | $url .= $this->buildRectangleUrl(); break; 156 | 157 | case 'clustering': 158 | $url_part = $this->buildCategoryUrl(); 159 | 160 | $url = "points?"; 161 | $url .= ( $url_part !== null ) ? $url_part . '&' : ''; 162 | $url .= $this->buildClusterUrl(); break; 163 | } 164 | 165 | return $url; 166 | 167 | } 168 | 169 | public function buildCategoryUrl() { 170 | 171 | $url_vars = []; 172 | 173 | if( isset($this->categories) && count($this->categories) == 1 ) { 174 | 175 | $url_vars['categories'] = "categories=" . $this->categories[0]; 176 | 177 | }elseif( isset($this->categories) && is_array($this->categories) && !empty($this->categories) ){ 178 | 179 | $url_vars['categories'] = "categories=" . implode( ',' , $this->categories ); 180 | 181 | } 182 | 183 | if( isset($this->where_clause) ) { 184 | 185 | $url_vars['where'] = "where=" . urlencode( $this->where_clause ); 186 | 187 | } 188 | 189 | if( isset($this->offset) ) { 190 | 191 | $url_vars['offset'] = "offset=" . $this->offset; 192 | 193 | } 194 | 195 | if( isset($this->page_size) ) { 196 | 197 | $url_vars['page_size'] = "pagesize=" . $this->page_size; 198 | 199 | } 200 | 201 | if( count($this->metadata) > 0 ) { 202 | 203 | $url_vars['metadata'] = "metadata=" . urlencode(json_encode($this->metadata)) ; 204 | 205 | } 206 | 207 | if( isset($this->include_metadata) ) { 208 | 209 | if( $this->include_metadata == true ) { 210 | $url_vars['includemetadata'] = "includemetadata=true"; 211 | } 212 | 213 | } 214 | 215 | if( count($url_vars) > 0 ) { 216 | 217 | return implode("&", $url_vars); 218 | 219 | } 220 | 221 | return null; 222 | 223 | } 224 | 225 | private function buildRadiusUrl() { 226 | 227 | $url_vars = []; 228 | 229 | if( isset( $this->latitude) ) { 230 | 231 | $url_vars[] = 'lat=' . $this->latitude; 232 | 233 | } else { 234 | 235 | throw new BackendlessException('Missing latitude value for radius search'); 236 | 237 | } 238 | 239 | if( isset( $this->longitude) ) { 240 | 241 | $url_vars[] = 'lon=' . $this->longitude; 242 | 243 | } else { 244 | 245 | throw new BackendlessException('Missing longitude value for radius search'); 246 | 247 | } 248 | 249 | if( isset( $this->radius) ) { 250 | 251 | $url_vars[] = 'r=' . $this->radius; 252 | 253 | } else { 254 | 255 | throw new BackendlessException('Missing radius value for radius search'); 256 | 257 | } 258 | 259 | 260 | 261 | if( isset( $this->units) ) { 262 | 263 | if( in_array($this->units, $this->possible_units ) ) { 264 | 265 | $url_vars[] = 'units=' . $this->units; 266 | 267 | } else { 268 | 269 | throw new BackendlessException('Wrong units value for radius search'); 270 | 271 | } 272 | 273 | 274 | } else { 275 | 276 | throw new BackendlessException('Missing units value for radius search'); 277 | 278 | } 279 | 280 | return implode("&", $url_vars); 281 | 282 | 283 | } 284 | 285 | private function buildRectangleUrl() { 286 | 287 | $url_vars = []; 288 | 289 | if( isset( $this->nw_latitude) ) { 290 | 291 | $url_vars[] = 'nwlat=' . $this->nw_latitude; 292 | 293 | } else { 294 | 295 | throw new BackendlessException('Don\'t have set nw-latitude value for rectangle search'); 296 | 297 | } 298 | 299 | if( isset( $this->nw_longitude) ) { 300 | 301 | $url_vars[] = 'nwlon=' . $this->nw_longitude; 302 | 303 | } else { 304 | 305 | throw new BackendlessException('Don\'t have set nw-longitude value for rectangle search'); 306 | 307 | } 308 | 309 | 310 | if( isset( $this->se_latitude) ) { 311 | 312 | $url_vars[] = 'selat=' . $this->se_latitude; 313 | 314 | } else { 315 | 316 | throw new BackendlessException('Don\'t have set se-latitude value for rectangle search'); 317 | 318 | } 319 | 320 | if( isset( $this->se_longitude) ) { 321 | 322 | $url_vars[] = 'selon=' . $this->se_longitude; 323 | 324 | } else { 325 | 326 | throw new BackendlessException('Don\'t have set se-longitude value for rectangle search'); 327 | 328 | } 329 | 330 | 331 | return implode("&", $url_vars); 332 | 333 | } 334 | 335 | private function buildClusterUrl() { 336 | 337 | $url_vars = []; 338 | 339 | if( isset( $this->cluster_grid_size) ) { 340 | 341 | $url_vars[] = 'clusterGridSize=' . $this->cluster_grid_size; 342 | 343 | } else { 344 | 345 | throw new BackendlessException('Don\'t have set clusterGridSize value for cluster search'); 346 | 347 | } 348 | 349 | if( isset( $this->dpp) ) { 350 | 351 | $url_vars[] = 'dpp=' . $this->dpp; 352 | 353 | } else { 354 | 355 | throw new BackendlessException('Can\'t calculate dpp value for cluster search'); 356 | 357 | } 358 | 359 | return implode("&", $url_vars); 360 | 361 | } 362 | 363 | public function setClusteringParams( $west_longitude, $east_longitude, $map_width, $cluster_grid_size = 100 ) { 364 | 365 | $this->search_type = 'clustering'; 366 | 367 | $this->west_longitude = $west_longitude; 368 | $this->east_longitude = $east_longitude; 369 | $this->map_width = $map_width; 370 | $this->cluster_grid_size = $cluster_grid_size; 371 | 372 | if ( ($this->east_longitude - $this->west_longitude) < 0 ) { 373 | 374 | $this->dpp = ((($this->east_longitude - $this->west_longitude) + 360) / $this->map_width ); 375 | 376 | }else{ 377 | 378 | $this->dpp = (($this->east_longitude - $this->west_longitude) / $this->map_width); 379 | } 380 | } 381 | 382 | } -------------------------------------------------------------------------------- /backendless/src/services/logging/Logger.php: -------------------------------------------------------------------------------- 1 | name = $name; 12 | 13 | } 14 | 15 | public function getName() { 16 | 17 | return $this->name; 18 | 19 | } 20 | 21 | public function debug( $message ) { 22 | 23 | $this->writeLog($message, "DEBUG" ); 24 | 25 | } 26 | 27 | public function info( $message ) { 28 | 29 | $this->writeLog($message, "INFO" ); 30 | 31 | } 32 | 33 | public function warn( $message ) { 34 | 35 | $this->writeLog($message, "WARN" ); 36 | 37 | } 38 | 39 | 40 | public function error( $message ) { 41 | 42 | $this->writeLog($message, "ERROR" ); 43 | 44 | } 45 | 46 | 47 | public function fatal( $message ) { 48 | 49 | $this->writeLog($message, "FATAL" ); 50 | 51 | } 52 | 53 | public function trace( $message ) { 54 | 55 | $this->writeLog($message, "TRACE" ); 56 | 57 | } 58 | 59 | public function writeLog( $message, $type ) { 60 | 61 | $log_data = []; 62 | $log_data[0] = []; 63 | $log_data[0]["log-level"] = $type; 64 | $log_data[0]["logger"] = $this->getName(); 65 | $log_data[0]["timestamp"] = time(); 66 | $log_data[0]["message"] = $message; 67 | $log_data[0]["exception"] = ""; 68 | 69 | RequestBuilder::doRequest( 'log', null, $log_data, 'PUT' ); 70 | 71 | } 72 | 73 | } 74 | -------------------------------------------------------------------------------- /backendless/src/services/messaging/DeliveryOptions.php: -------------------------------------------------------------------------------- 1 | push_broadcast = $val; 21 | return $this; 22 | 23 | } 24 | 25 | public function getPushBroadcast() { 26 | 27 | return $this->push_broadcast; 28 | 29 | } 30 | 31 | public function setPushPolicy( $val ) { 32 | 33 | $this->push_policy = $val; 34 | return $this; 35 | 36 | } 37 | 38 | public function getPushPolicy() { 39 | 40 | return $this->push_policy; 41 | 42 | } 43 | 44 | public function setPushSinglecast( $val ) { 45 | 46 | $this->push_singlecast = $val; 47 | return $this; 48 | 49 | } 50 | 51 | public function getPushSinglecast() { 52 | 53 | return $this->push_singlecast; 54 | 55 | } 56 | 57 | public function addPushSinglecast( $val ) { 58 | 59 | $this->push_singlecast[ ] = $val; 60 | return $this; 61 | 62 | } 63 | 64 | public function setPublishAt( $val ) { 65 | 66 | $this->publish_at = $val; 67 | return $this; 68 | 69 | } 70 | 71 | public function getPublishAt() { 72 | 73 | return $this->publish_at; 74 | 75 | } 76 | 77 | public function setRepeatEvery( $val ) { 78 | 79 | $this->repeat_every = $val; 80 | return $this; 81 | 82 | } 83 | 84 | public function getRepeatEvery(){ 85 | 86 | return $this->repeat_every; 87 | 88 | } 89 | 90 | public function setRepeatExpiresAt( $val ) { 91 | 92 | $this->repeat_expires_at = $val; 93 | return $this; 94 | 95 | } 96 | 97 | public function getRepeatExpiresAt() { 98 | 99 | return $this->repeat_expires_at; 100 | 101 | } 102 | 103 | } 104 | -------------------------------------------------------------------------------- /backendless/src/services/messaging/Message.php: -------------------------------------------------------------------------------- 1 | message_id; 16 | 17 | } 18 | 19 | public function getHeaders() { 20 | 21 | return $this->headers; 22 | 23 | } 24 | 25 | public function getData() { 26 | 27 | return $this->data; 28 | 29 | } 30 | 31 | public function getPublisherId() { 32 | 33 | return $this->publisher_id; 34 | 35 | } 36 | 37 | public function getTimestamp() { 38 | 39 | return $this->timestamp; 40 | 41 | } 42 | 43 | public function setMessageId( $message_id ) { 44 | 45 | $this->message_id = $message_id; 46 | return $this; 47 | } 48 | 49 | public function setHeaders( $headers ) { 50 | 51 | $this->headers = $headers; 52 | return $this; 53 | } 54 | 55 | public function setData( $data ) { 56 | 57 | $this->data = $data; 58 | return $this; 59 | 60 | } 61 | 62 | public function setPublisherId( $publisher_id ) { 63 | 64 | $this->publisher_id = $publisher_id; 65 | return $this; 66 | 67 | } 68 | 69 | public function setTimestamp( $timestamp ) { 70 | 71 | $this->timestamp = $timestamp; 72 | return $this; 73 | 74 | } 75 | 76 | } 77 | -------------------------------------------------------------------------------- /backendless/src/services/messaging/PublishOptions.php: -------------------------------------------------------------------------------- 1 | publisher_id = $publisher_id; 39 | return $this; 40 | 41 | } 42 | 43 | public function getPublisherId() { 44 | 45 | return $this->publisher_id; 46 | 47 | } 48 | 49 | public function setSubtopic( $subtopic) { 50 | 51 | $this->subtopic = $subtopic; 52 | return $this; 53 | 54 | } 55 | 56 | public function getSubtopic() { 57 | 58 | return $this->subtopic; 59 | 60 | } 61 | 62 | public function setHeaders( $headers_array ) { 63 | 64 | $this->headers = $headers_array; 65 | 66 | return $this; 67 | 68 | } 69 | 70 | public function getHeaders() { 71 | 72 | return $this->headers; 73 | 74 | } 75 | 76 | public function putHeader( $name, $value ) { 77 | 78 | $this->headers[$name] = $value; 79 | 80 | } 81 | 82 | public function setChannel( $channel ) { 83 | 84 | $this->channel_name = $channel; 85 | return $this; 86 | 87 | } 88 | 89 | public function getChannel() { 90 | 91 | if( isset( $this->channel_name)) { 92 | 93 | return $this->channel_name; 94 | 95 | }else{ 96 | 97 | return null; 98 | 99 | } 100 | 101 | } 102 | 103 | 104 | } -------------------------------------------------------------------------------- /backendless/src/services/messaging/SubscriptionOptions.php: -------------------------------------------------------------------------------- 1 | subscriber_id; 14 | 15 | } 16 | 17 | public function setSubscriberId( $subscriber_id ) { 18 | 19 | $this->subscriber_id = $subscriber_id; 20 | return $this; 21 | 22 | } 23 | 24 | public function getSubtopic() { 25 | 26 | return $this->subtopic; 27 | 28 | } 29 | 30 | 31 | public function setSubtopic( $subtopic ) { 32 | 33 | $this->subtopic = $subtopic; 34 | return $this; 35 | 36 | } 37 | 38 | public function getSelector() { 39 | 40 | return $this->selector; 41 | } 42 | 43 | public function setSelector( $selector ) { 44 | 45 | $this->selector = $selector; 46 | return $this; 47 | 48 | } 49 | 50 | } -------------------------------------------------------------------------------- /backendless/src/services/persistence/BackendlessDataQuery.php: -------------------------------------------------------------------------------- 1 | props = $props; 23 | 24 | } 25 | 26 | return $this; 27 | 28 | } 29 | 30 | public function addProp( $prop ) { 31 | 32 | if( ! in_array($prop, $this->props)) { 33 | 34 | $this->props[] = $prop; 35 | } 36 | 37 | return $this; 38 | 39 | } 40 | 41 | public function getProps() { 42 | 43 | return $this->props; 44 | 45 | } 46 | 47 | public function setRelated( $relations ) { 48 | 49 | if( is_array($relations) && !empty($relations) && $relations !== null ) { 50 | 51 | $this->load_relations = $relations; 52 | 53 | 54 | } 55 | 56 | return $this; 57 | 58 | } 59 | 60 | public function addRelated( $relation ) { 61 | 62 | if( ! in_array($relation, $this->load_relations)) { 63 | 64 | $this->load_relations[] = $relation; 65 | 66 | } 67 | 68 | return $this; 69 | 70 | } 71 | 72 | public function getRelations() { 73 | 74 | return $this->load_relations; 75 | 76 | } 77 | 78 | public function setWhereClause( $where_clause ) { 79 | 80 | if( !empty($where_clause) && $where_clause !== null && $where_clause != '' ) { 81 | 82 | $this->where_clause = $where_clause; 83 | 84 | } 85 | 86 | return $this; 87 | 88 | } 89 | 90 | public function getWhereClause( ) { 91 | 92 | return $this->where_clause; 93 | 94 | } 95 | 96 | public function setPageSize( $page_size) { 97 | 98 | $this->page_size = $page_size; 99 | 100 | return $this; 101 | 102 | } 103 | 104 | public function getPageSize() { 105 | 106 | $this->page_size; 107 | 108 | } 109 | 110 | public function setOffset( $offset ) { 111 | 112 | $this->offset = $offset; 113 | 114 | return $this; 115 | 116 | } 117 | 118 | public function getOffset() { 119 | 120 | $this->offset; 121 | 122 | } 123 | 124 | public function setSortBy( $sort_by ) { 125 | 126 | if( is_array($sort_by) && !empty($sort_by) && $sort_by !== null ) { 127 | 128 | $this->sort_by = $sort_by; 129 | 130 | } 131 | 132 | return $this; 133 | } 134 | 135 | public function addSortBy( $sort_by ) { 136 | 137 | if( ! in_array($sort_by, $this->sort_by) ) { 138 | 139 | $this->sort_by[] = $sort_by; 140 | 141 | } 142 | 143 | return $this; 144 | 145 | } 146 | 147 | public function getSortBy() { 148 | 149 | return $this->sort_by; 150 | 151 | } 152 | 153 | public function setDepth( $depth ) { 154 | 155 | $this->depth = $depth; 156 | 157 | return $this; 158 | 159 | } 160 | 161 | public function buildUrlVars() { 162 | 163 | $var_array = []; 164 | 165 | $var_array['props'] = null; 166 | 167 | if( isset($this->props) && count($this->props) == 1 && is_array($this->props) ) { 168 | 169 | $var_array['props'] = "props=" . $this->props[0]; 170 | 171 | }elseif( isset($this->props) && is_array($this->props) && !empty($this->props) ){ 172 | 173 | $var_array['props'] = "props=" . implode( ',' , $this->props); 174 | 175 | } 176 | 177 | $var_array['load_relations'] = null; 178 | 179 | if( isset($this->load_relations) && count($this->load_relations) == 1 && is_array($this->load_relations) ) { 180 | 181 | $var_array['load_relations'] = "loadRelations=" . $this->load_relations[0]; 182 | 183 | }elseif( isset($this->load_relations) && is_array($this->load_relations) && !empty($this->load_relations) ){ 184 | 185 | $var_array['load_relations'] = "loadRelations=" . implode( ',' , $this->load_relations); 186 | 187 | } 188 | 189 | $var_array['where'] = null; 190 | 191 | if( isset($this->where_clause) ) { 192 | 193 | $var_array['where'] = "where=" . urlencode( $this->where_clause ); 194 | 195 | } 196 | 197 | $var_array['page_size'] = null; 198 | 199 | if( isset($this->page_size) ) { 200 | 201 | $var_array['page_size'] = "pageSize=" . $this->page_size; 202 | 203 | } 204 | 205 | $var_array['sort_by'] = null; 206 | 207 | if( isset($this->sort_by) && count($this->sort_by) == 1 && is_array($this->sort_by) ) { 208 | 209 | $var_array['sort_by'] = "sortBy=" . urlencode( $this->sort_by[ 0 ] ); 210 | 211 | }elseif( isset($this->sort_by) && is_array($this->sort_by) && !empty($this->sort_by) ){ 212 | 213 | $var_array['sort_by'] = "sortBy=" . urlencode( implode( ',' , $this->sort_by) ); 214 | 215 | } 216 | 217 | $var_array['offset'] = null; 218 | 219 | if( isset($this->offset) ) { 220 | 221 | $var_array['offset'] = "offset=" . $this->offset; 222 | 223 | } 224 | 225 | $var_array['relations_depth'] = null; 226 | 227 | if( isset($this->depth) ) { 228 | 229 | $var_array['relations_depth'] = "relationsDepth=" . $this->depth; 230 | 231 | } 232 | 233 | 234 | foreach ($var_array as $var=>$val){ 235 | 236 | if( $val == null) { 237 | unset($var_array[$var]); 238 | } 239 | 240 | } 241 | 242 | 243 | return implode("&", $var_array); 244 | } 245 | 246 | 247 | } -------------------------------------------------------------------------------- /backendless/src/services/user/FacebookFieldMappings.php: -------------------------------------------------------------------------------- 1 | data_keys[$key] = $val; 15 | 16 | } 17 | 18 | public function getFields() { 19 | 20 | return $this->data_keys; 21 | 22 | } 23 | 24 | } -------------------------------------------------------------------------------- /backendless/src/services/user/TwitterFieldMappings.php: -------------------------------------------------------------------------------- 1 | =5.4.45" 15 | }, 16 | "minimum-stability": "dev", 17 | "autoload": { 18 | "psr-4": { 19 | "backendless\\": "backendless/src" 20 | } 21 | } 22 | } --------------------------------------------------------------------------------