public function request($content, $method = 'POST') { $this->resetResponse(); if (Backendless::isBlMode()) { $this->setHeader("application-type", "BL"); } if ($content !== 'null') { $this->request_headers['Content-length'] = strlen($content); } $headers = []; if (is_array($this->request_headers)) { $headers = array_map(function ($v, $k) { return sprintf("%s: %s", $k, $v); }, $this->request_headers, array_keys($this->request_headers)); } $http = ['ignore_errors' => true, 'method' => $method, 'header' => implode("\r\n", $headers), 'timeout' => 10]; if ($content !== 'null') { $http['content'] = $content; } $context = stream_context_create(['http' => $http, 'ssl' => ['verify_peer' => false, 'allow_self_signed' => false]]); $this->beforeRequest($http, $method); $this->response = file_get_contents($this->target_url, false, $context); $this->afterRequest(); $this->response_headers = $http_response_header; }
private function initSdk() { $invocation_context = new InvocationContext($this->rsi->getInvocationContext()); $init_app_data = $this->rsi->getInitAppData(); Backendless::setUrl($init_app_data->getUrl()); Backendless::initApp($invocation_context->app_id, $init_app_data->getSecretKey(), $init_app_data->getAppVersionName()); Backendless::switchOnBlMode(); Backendless::setInvocationContext($invocation_context); }
public static function Get($url) { $http_request = new HttpRequest(); $http_request->setTargetUrl($url)->setHeader('application-id', Backendless::getApplicationId())->setHeader('secret-key', Backendless::getSecretKey())->setHeader('application-type', 'REST')->setHeader('Accept:', '*/*')->setHeader('Content-Type', 'application/json'); foreach (self::$headers as $heder_n => $h_val) { $http_request->setHeader($heder_n, $h_val); } self::addUserTokenHeader($http_request); self::$headers = []; $http_request->request('', 'GET'); self::handleError($http_request); return $http_request->getResponse(); }
public function runImpl() { Log::writeInfo("Called invocation task: " . $this->rmi, $target = 'file'); if ($this->rmi == null || $this->event_handler == null) { Log::writeInfo("Something is null in InvocationTask..."); return; } $invocation_result = new InvocationResult(); try { $definition = self::$event_definition_holder->getDefinitionById($this->rmi->getEventId()); $arguments = self::$argument_adapter_list->beforeExecuting($definition, $this->rmi, $this->rmi->getDecodedArguments()); if ($definition['name'] == 'handleEvent') { // Object context = arguments[ 0 ]; // Class runnerContextClass = classLoader.loadClass( RunnerContext.class.getName() ); // List<String> userRoleList = (List<String>) runnerContextClass.getMethod( "getUserRole" ).invoke( context ); // // String[] userRoles = userRoleList == null ? null : userRoleList.toArray( new String[ userRoleList.size() ] ); // String userId = (String) runnerContextClass.getMethod( "getUserId" ).invoke( context ); // AccessValidator.validateAccess( clazz, userRoles, userId ); } $instance_class_name = $this->event_handler->getProvider(); $method = self::findMethod($instance_class_name, $definition, count($arguments)); // bootstrap onEnter action $backendless_globals = ClassManager::getClassInstanceByName("BackendlessGlobals"); $backendless_globals->onEnter($instance_class_name, $method, $arguments); // end bootstrap onEnter action // switch sdk from rest mode to bl Backendless::switchOnBlMode(); $reflection_method = new ReflectionMethod($instance_class_name, $method); $result = $arguments; // invokeArgs pass $arguments as link and we get changed data after invoke $reflection_method->invokeArgs(new $instance_class_name(), $result); // bootstrap onExit action $backendless_globals->onExit($instance_class_name, $method, $result); // end bootstrap onExit action if ($this->rmi->isAsync()) { return; } $arguments = self::$argument_adapter_list->afterExecuting($definition, $this->rmi, $arguments, $result); if (is_a($arguments[0], "\\backendless\\core\\servercode\\RunnerContext")) { $arguments[0] = $arguments[0]->getConvertedToArray(); } $invocation_result->setArguments($arguments); ResponderProcessor::sendResult($this->rmi->getId(), $invocation_result); } catch (Exception $e) { Log::writeError($e->getMessage()); } catch (BackendlessException $e) { Log::writeError("In Backendless SDK occurred error with message: \"" . $e->getMessage() . "\""); exit; } }
public static function analyze($path_to_folder, $map_calsses = true) { //Log::writeInfo( "ClassManager start analyze classes", "file" ); if ($path_to_folder == false) { throw new CodeRunnerException("Wrong path to code source"); } $all_files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path_to_folder)); $php_files = new RegexIterator($all_files, '/\\.php$/'); $classes = []; $namespaces_path = []; foreach ($php_files as $php_file) { $class_info = []; $class_info['class_name'] = basename($php_file->getRealPath(), ".php"); $class_info['namespace'] = self::getNamespace(file_get_contents($php_file->getRealPath())); if ($class_info['namespace'] != '') { $class_info['full_name'] = $class_info['namespace'] . '\\' . $class_info['class_name']; } else { $class_info['full_name'] = $class_info['class_name']; } $class_info['path'] = $php_file->getRealPath(); self::putToHolder($class_info, 'class_name'); $namespaces_path[$class_info['namespace']] = pathinfo($class_info['path'])['dirname']; //key = namespace key = path to folder; $classes[] = ['name' => $class_info['class_name'], 'namespace' => $class_info['namespace'] . "\\" . $class_info['class_name']]; } foreach ($namespaces_path as $namespace => $path) { Autoload::addNamespace($namespace, $path); // add autoloading for user classes } if ($map_calsses == true) { Backendless::ignoreMapException(); foreach ($classes as $class) { Backendless::mapTableToClass($class['name'], $class['namespace']); // set mapping for SDK. } } }
public static function analyze() { Log::writeInfo("ClassManager start analyze classes", "file"); $all_files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator(PathBuilder::getClasses())); $php_files = new RegexIterator($all_files, '/\\.php$/'); $classes_namespaces = []; foreach ($php_files as $php_file) { $class_info = []; //$class_info["parent_class"] = self::getParentClass( file_get_contents( $php_file->getRealPath() ) ); $class_info['namespace'] = str_replace("/", "\\", trim(dirname(substr($php_file->getRealPath(), strlen(PathBuilder::getClasses()))), "/")); $class_info['class_name'] = basename($php_file->getRealPath(), ".php"); $class_info['path'] = $php_file->getRealPath(); self::putToHolder($class_info); $classes_namespaces[$class_info['namespace']] = pathinfo($class_info["path"])["dirname"]; //key = namespace key = path to folder; Backendless::mapTableToClass($class_info['class_name'], $class_info['namespace'] . "\\" . $class_info['class_name']); // set mapping for SDK. } foreach ($classes_namespaces as $namespace => $path) { Autoload::addNamespace($namespace, $path); // add autoloading for user classes } Log::writeInfo("ClassManager finished analyze classes", "file"); }
public function sendEmail($subject, $body, $to, $attachments = null, $html) { if (!is_array($to)) { $to = [$to]; } $data = ["subject" => $subject, "to" => $to]; if ($html) { $data["bodyparts"] = ["htmlmessage" => $body]; } else { $data["bodyparts"] = ["textmessage" => $body]; } if ($attachments !== null) { $data["attachment"] = $attachments; } return RequestBuilder::doRequestByUrl(Backendless::getUrl() . "/" . Backendless::getVersion() . "/messaging/email", $data, 'POST'); }
public function copyFile($source_path_name, $target_path) { $source_path_name = trim($source_path_name); $source_path_name = trim($source_path_name, "\\\\/"); $target_path = trim($target_path); $target_path = trim($target_path, "\\\\/"); $url_part = Backendless::getUrl() . "/" . Backendless::getApplicationId() . "/" . Backendless::getVersion(); $request_body = ["sourcePath" => $source_path_name, "targetPath" => $target_path]; return RequestBuilder::doRequest('files', 'copy', $request_body, 'PUT'); }
public function reset($counter_name) { RequestBuilder::doRequestByUrl(Backendless::getUrl() . "/" . Backendless::getVersion() . "/counters/" . $counter_name . "/reset", '', 'PUT'); }
private function initSdk() { $init_app_data = new InitAppData($this->rmi->getInitAppData()); Backendless::setUrl($init_app_data->getUrl()); Backendless::initApp($this->rmi->getApplicationId(), $init_app_data->getSecretKey(), $init_app_data->getAppVersionName()); Backendless::switchOnBlMode(); }
public function expireAt($key, $timestamp) { $timestamp = $timestamp * 1000; RequestBuilder::doRequestByUrl(Backendless::getUrl() . "/" . Backendless::getVersion() . "/cache/" . $key . "expireAt?timestamp=" . $timestamp, '', 'PUT'); }
public function dispatch($event_name, $event_args_array = null) { return RequestBuilder::doRequestByUrl(Backendless::getUrl() . "/" . Backendless::getVersion() . "/servercode/events/" . $event_name, $event_args_array, 'POST'); }
self::$InvocationContext = $invocation_context; } public static function switchOnBlMode() { self::$sdk_mode_bl = true; } public static function switchOffBlMode() { self::$sdk_mode_bl = false; } public static function isBlMode() { return self::$sdk_mode_bl; } protected static function phpEnviromentInit() { //set default timezone need for WIN and OS X date_default_timezone_set('UTC'); // check if available openssl for use https if (!extension_loaded('openssl')) { self::$url = preg_replace('/^http:\\/\\/|https:\\/\\/(.*)$/', 'http://${1}', self::$url); } } public static function devMode() { define('DEV_MODE', true); Log::init(); } } Backendless::staticConstruct();
protected function convertToUserClassesItem($data) { $dont_set_class_for_mapping = false; if (isset($data["___class"])) { if ($data["___class"] === "GeoPoint") { // if "___class" == GEOPINT need create geopoint return $this->fillGeoPoint($data, "user_class"); } $class_name = Backendless::getModelByClass($data["___class"]); if ($class_name != null) { $obj = $this->getObjectByClass($class_name); } else { $dont_set_class_for_mapping = true; } } else { $dont_set_class_for_mapping = true; } if ($dont_set_class_for_mapping === true) { // если не задан класс для маппинга модели она остается мултимассивом + рекурсивно проверяются его ключи. if (is_array($data)) { foreach ($data as $prop_name => $prop_val) { $data[$prop_name] = $this->convertToUserClassesItem($prop_val); } } return $data; } $props = (new ReflectionClass($obj))->getProperties(); foreach ($props as $prop) { $prop->setAccessible(true); if (isset($data[$prop->getName()])) { if (!is_array($data[$prop->getName()])) { $prop->setValue($obj, $data[$prop->getName()]); } else { $prop->setValue($obj, $this->prepareUserClassRelation($data[$prop->getName()])); } } unset($data[$prop->getName()]); } // set undeclared in model properties $this->setUndeclaredProperties($data, $obj); return $obj; }
private function runAction($action_name, $geofence_name, $geopoint = null) { $url = "fence/" . $action_name . "?geoFence=" . $geofence_name; $headers = []; $headers['application-id'] = Backendless::getApplicationId(); $headers['secret-key'] = Backendless::getSecretKey(); $headers['application-type'] = 'REST'; $geopoint_data = []; if ($geopoint !== null) { if (is_object($geopoint)) { $geopoint_data['latitude'] = $geopoint->getLatitude(); $geopoint_data['longitude'] = $geopoint->getLongitude(); } else { $geopoint_data = $geopoint; } $headers['Content-Type'] = 'application/json'; } else { $geopoint_data = null; } return RequestBuilder::doRequestWithHeaders("geo", $url, $geopoint_data, "POST", $headers)["totalObjects"]; }