public function invoke($rmi)
 {
     if ($rmi->getEventId() == Config::$CORE['shutdown_code'] && GlobalState::$TYPE == 'LOCAL') {
         Log::writeWarn("CodeRunner was stopped from console!");
         exit(1);
     }
     if ($this->event_handlers_model == null) {
         Log::write("Event handler model is null...", $target = 'file');
         return;
     }
     $event_handler = $this->event_handlers_model->getEventHandler($rmi->getEventid(), $rmi->getTarget());
     $invocation_task = new InvocationTask($rmi, $event_handler);
     $invocation_task->runImpl();
 }
 private function isValidTimer($handler)
 {
     $current_time = time() * 1000;
     $timer_info = json_decode(str_replace("'", "\"", $handler->getTarget()), true);
     if (isset($timer_info["expire"])) {
         if ($timer_info["expire"] < $current_time) {
             $masg = "Timer '" . $timer_info["timername"] . "' already expired";
             Log::writeError($msg);
             throw new CodeRunnerException($msg);
         }
     }
     if ($timer_info["startDate"] < $current_time) {
         if ($timer_info["frequency"]["schedule"] == "once") {
             Log::writeError("Timer's '" . $timer_info['timername'] . "' start time is in the past, unable to run the timer.");
             return false;
         }
         Log::writeWarn("Timer's '" . $timer_info['timername'] . "' start time is in the past. The timer will run accordingly to the schedule.");
     }
     return true;
 }
 public static function phpEnviromentInit()
 {
     if (GlobalState::$TYPE === 'LOCAL') {
         //for LOCAL use user time zone or UTC if not set
         $timezone = @date_default_timezone_get();
         // if not set default timezone set as UTC
         if ($timezone == 'UTC') {
             date_default_timezone_set('UTC');
         }
     } else {
         date_default_timezone_set('UTC');
         // for CLOUDE use UTC
     }
     // check if available openssl for use https
     if (!extension_loaded("openssl")) {
         Log::writeWarn('PHP module "openssl" not installed or not switch on in php.ini file', $target = 'file');
         Config::setServerUrl(preg_replace('/^http:\\/\\/|https:\\/\\/(.*)$/', 'http://${1}', Config::$SERVER_URL));
         Log::writeWarn('All https requests to ' . Config::$SERVER_URL . 'changed on http requests', $target = 'file');
     }
 }
 public function publishCode()
 {
     if (!file_exists(Config::$CORE['tmp_dir_path'])) {
         mkdir(Config::$CORE['tmp_dir_path'], 0777);
     }
     $handlers = $this->event_handlers_model->getCountEventHandlers() == 1 ? "handler" : "handlers";
     $timers = $this->event_handlers_model->getCountTimers() == 1 ? "timer" : "timers";
     Log::writeInfo("Deploying {$this->event_handlers_model->getCountEventHandlers()} event " . $handlers . " and {$this->event_handlers_model->getCountTimers()} " . $timers . " to the server… ");
     try {
         $code_zip_path = realpath(getcwd() . DS . Config::$CORE['tmp_dir_path']) . DS . 'code.zip';
         $classes_path = realpath(getcwd() . DS . Config::$CLASS_LOCATION);
         $zip = new ZipArchive();
         $zip->open($code_zip_path, ZipArchive::CREATE);
         $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($classes_path), RecursiveIteratorIterator::LEAVES_ONLY);
         $class_location_folder_name = basename(Config::$CLASS_LOCATION);
         foreach ($files as $file) {
             if ($file->getFileName() === '.' || $file->getFileName() == '..') {
                 continue;
             }
             $path_part = explode($class_location_folder_name, $file);
             $zip->addFile($file, $class_location_folder_name . $path_part[1]);
         }
         $lib_path = realpath(getcwd() . DS . ".." . DS . 'lib');
         $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($lib_path), RecursiveIteratorIterator::LEAVES_ONLY);
         foreach ($files as $file) {
             if ($file->getFileName() === '.' || $file->getFileName() == '..') {
                 continue;
             }
             $path_part = explode('lib', $file);
             $zip->addFile($file, 'lib' . $path_part[1]);
         }
         $model_file_path = realpath(getcwd() . DS . Config::$CORE['tmp_dir_path']);
         $model_file_path .= DS . 'model.json';
         file_put_contents($model_file_path, $this->event_handlers_model->getJson(true));
         $zip->addFile($model_file_path, 'model.json');
         $zip->close();
         unlink($model_file_path);
         CodeRunnerUtil::getInstance()->publish($code_zip_path);
         GlobalState::$STATE = 'PUBLISH';
         Log::writeInfo("Successfully deployed all event handlers and timers.");
         if ($this->event_handlers_model->getCountEventHandlers() + $this->event_handlers_model->getCountTimers() > 5) {
             Log::writeWarn("The deployment will result in additional charges as it exceeds the free plan limit.");
             Log::writeInfo("See the Billing screen in Backendless Console for details.");
             Log::writeInfo("The billing screen is available at Manage > Billing.");
         }
         Log::writeInfo("CodeRunner will shutdown now.");
         $this->rrmdir(Config::$CORE['tmp_dir_path']);
         exit(0);
     } catch (CodeRunnerException $e) {
         Log::writeError($e->getMessage(), $target = 'all');
         //change to file
         Log::writeError("Code publishing failed..", $target = 'all');
         if (file_exists(Config::$CORE['tmp_dir_path'])) {
             $this->rrmdir(Config::$CORE['tmp_dir_path']);
         }
     }
 }
 public function publishCode($hosted = false)
 {
     $this->resetTmpFolder($hosted);
     if ($hosted) {
         $hosted_events = $this->hosted_model->getCountOfEvents();
         if ($hosted_events <= 0) {
             return;
         }
         Log::writeInfo("Deploying {$hosted_events} hosted service event to the server… ");
     } else {
         $handlers = $this->event_handlers_model->getCountEventHandlers() == 1 ? "handler" : "handlers";
         $timers = $this->event_handlers_model->getCountTimers() == 1 ? "timer" : "timers";
         if ($this->event_handlers_model->getCountEventHandlers() + $this->event_handlers_model->getCountTimers() <= 0) {
             return;
         }
         Log::writeInfo("Deploying {$this->event_handlers_model->getCountEventHandlers()} event " . $handlers . " and {$this->event_handlers_model->getCountTimers()} " . $timers . " to the server… ");
     }
     try {
         $code_zip_path = realpath(getcwd() . DS . Config::$CORE['tmp_dir_path']);
         if ($hosted) {
             $code_zip_path .= DS . 'hosted' . DS . 'code.zip';
         } else {
             $code_zip_path .= DS . 'events' . DS . 'code.zip';
         }
         $this->createArchive($code_zip_path, $hosted);
         CodeRunnerUtil::getInstance()->publish($code_zip_path, $hosted);
         if ($hosted) {
             Log::writeInfo("Successfully deployed all hosted user code.");
         } else {
             Log::writeInfo("Successfully deployed all event handlers and timers.");
         }
         if ($this->event_handlers_model->getCountEventHandlers() + $this->event_handlers_model->getCountTimers() > 5) {
             Log::writeWarn("The deployment will result in additional charges as it exceeds the free plan limit.");
             Log::writeInfo("See the Billing screen in Backendless Console for details.");
             Log::writeInfo("The billing screen is available at Manage > Billing.");
         }
     } catch (CodeRunnerException $e) {
         Log::writeError($e->getMessage(), $target = 'file');
         Log::writeError("Code publishing failed..", $target = 'all');
         $this->removeTmpFolder();
     }
 }