Beispiel #1
0
 /**
  * package serverを立ち上げる
  *
  * @param string $package_root パッケージ名
  */
 public static function handler($package_root = null)
 {
     if (empty($package_root) || Rhaco::path() == "") {
         $debug = debug_backtrace();
         $first_action = array_pop($debug);
         if ($package_root === null) {
             $package_root = basename(dirname($first_action["file"]));
         }
         if (Rhaco::path() == "") {
             Rhaco::init($first_action["file"]);
         }
     }
     $base_dir = Rhaco::path();
     $request = new Request();
     $package_root_path = str_replace(".", "/", $package_root);
     $preg_quote = (empty($package_root_path) ? "" : preg_quote($package_root_path, "/") . "\\/") . "(.+)";
     $tag = new Tag("rest");
     if (preg_match("/^\\/state\\/" . $preg_quote . "\$/", $request->args(), $match)) {
         $tag->add(new Tag("package", $match[1]));
         if (self::parse_package($package_root_path, $base_dir, $match[1], $tgz_filename)) {
             $tag->add(new Tag("status", "success"));
             $tag->output();
         }
     } else {
         if (preg_match("/^\\/download\\/" . $preg_quote . "\$/", $request->args(), $match)) {
             if (self::parse_package($package_root_path, $base_dir, $match[1], $tgz_filename)) {
                 Http::attach(new File($tgz_filename));
             }
         }
     }
     Http::status_header(403);
     $tag->add(new Tag("status", "fail"));
     $tag->output();
     exit;
 }
Beispiel #2
0
 /**
  * install serverを立ち上げる
  *
  * @param string $package_root パッケージ名
  */
 public static function handler()
 {
     $base_dir = Rhaco::path();
     $request = new Request();
     $tag = new Tag("rest");
     if (preg_match("/^\\/state\\/(.+)\$/", $request->args(), $match)) {
         $tag->add(new Tag("package", $match[1]));
         if (self::parse_package($base_dir, $match[1], $tgz_filename)) {
             $tag->add(new Tag("status", "success"));
             $tag->output();
         }
     } else {
         if (preg_match("/^\\/download\\/(.+)\$/", $request->args(), $match)) {
             if (self::parse_package($base_dir, $match[1], $tgz_filename)) {
                 Http::attach(new File($tgz_filename));
             }
         }
     }
     Http::status_header(403);
     $tag->add(new Tag("status", "fail"));
     $tag->output();
     exit;
 }
Beispiel #3
0
 /**
  * URLのパターンからTemplateを切り替える
  * @param array $urlconf
  */
 public function handler(array $urlconf = array())
 {
     $params = array();
     foreach ($urlconf as $pattern => $conf) {
         if (is_int($pattern)) {
             $pattern = $conf;
             $conf = null;
         }
         if (preg_match("/" . str_replace(array("\\/", "/", "__SLASH__"), array("__SLASH__", "\\/", "\\/"), $pattern) . "/", $this->args(), $params)) {
             if ($conf !== null) {
                 if (is_array($conf)) {
                     if (isset($conf["class"])) {
                         $this->class = $conf["class"];
                     }
                     if (isset($conf["method"])) {
                         $this->method = $conf["method"];
                     }
                     if (isset($conf["template"])) {
                         $this->template = $conf["template"];
                     }
                     if (isset($conf["name"])) {
                         $this->name = $conf["name"];
                     }
                 } else {
                     $this->dict($conf);
                 }
             }
             self::$match_pattern = empty($this->name) ? $params[0] : $this->name;
             if (!empty($this->class)) {
                 if (false !== strrpos($this->class, ".") || !class_exists($this->class)) {
                     $this->class = Rhaco::import($this->class);
                 }
                 if (empty($this->method) && !empty($pattern)) {
                     $method_patterns = array();
                     $patterns = explode("/", $pattern);
                     if ($patterns[0] == "^") {
                         array_shift($patterns);
                     }
                     foreach ($patterns as $p) {
                         if (!preg_match("/[\\w_]/", $p)) {
                             break;
                         }
                         $method_patterns[] = $p;
                     }
                     if (!empty($method_patterns)) {
                         $this->method = implode("_", $method_patterns);
                     }
                 }
             }
             if (empty($this->method) && !empty($this->template)) {
                 $obj = new self();
                 $obj->copy_module($this, true);
                 $obj->template($this->template);
             } else {
                 $method = empty($this->method) ? "index" : $this->method;
                 if (!method_exists($this->class, $method)) {
                     throw new Exception("Not found " . $this->class . "::" . $method);
                 }
                 array_shift($params);
                 try {
                     $class = $this->class;
                     $action = new $class();
                     $action->copy_module($this, true);
                     if ($action instanceof self) {
                         $action->handled();
                     }
                     $obj = call_user_func_array(array($action, $method), $params);
                 } catch (Exception $e) {
                     Log::debug($e);
                     $on_error = Rhaco::def("core.Flow@on_error");
                     if ($on_error === null) {
                         throw $e;
                     }
                     if (isset($on_error[0])) {
                         Http::status_header((int) $on_error[0]);
                     }
                     if (isset($on_error[2])) {
                         Http::redirect($on_error[2]);
                     }
                     if (isset($on_error[1])) {
                         $template = new Template();
                         $template->output($on_error[1]);
                     }
                     exit;
                 }
             }
             if ($obj instanceof self) {
                 $obj = $obj->templ();
             }
             if (!$obj instanceof Template) {
                 throw new Exception("Forbidden " . $this->args());
             }
             $obj->path($this->path());
             $obj->url($this->url());
             $this->templ = $obj;
             if (!$this->isTemplate()) {
                 $this->template($obj->filename());
             }
             if (!$this->isTemplate()) {
                 $cs = explode(".", $this->class);
                 $class = array_pop($cs);
                 $class = implode("/", $cs) . (!empty($cs) ? "/" : "") . strtolower($class[0]) . substr($class, 1);
                 $this->template($class . "/" . $method . ".html");
             }
             return $this;
         }
     }
     throw new Exception("no match pattern");
 }
Beispiel #4
0
 /**
  * xml定義からhandlerを処理する
  * @param string $file アプリケーションXMLのファイルパス
  */
 public static final function load($file = null)
 {
     if (!isset($file)) {
         $file = App::mode() . App::called_filename();
     }
     if (!self::$is_app_cache || !Store::has($file)) {
         $parse_app = self::parse_app($file, false);
         if (self::$is_app_cache) {
             Store::set($file, $parse_app);
         }
     }
     if (self::$is_app_cache) {
         $parse_app = Store::get($file);
     }
     if (empty($parse_app['apps'])) {
         throw new RuntimeException('undef app');
     }
     $app_result = null;
     $in_app = $match_handle = false;
     $app_index = 0;
     try {
         foreach ($parse_app['apps'] as $app) {
             switch ($app['type']) {
                 case 'handle':
                     $self = new self('_inc_session_=false');
                     foreach ($app['modules'] as $module) {
                         $self->add_module(self::import_instance($module));
                     }
                     if ($self->has_module('flow_handle_begin')) {
                         $self->call_module('flow_handle_begin', $self);
                     }
                     try {
                         if ($self->handler($app['maps'], $app_index++)->is_pattern()) {
                             $self->cp(self::execute_var($app['vars']));
                             $src = $self->read();
                             if ($self->has_module('flow_handle_end')) {
                                 $self->call_module('flow_handle_end', $src, $self);
                             }
                             print $src;
                             $in_app = true;
                             $match_handle = true;
                             if (!$parse_app["handler_multiple"]) {
                                 exit;
                             }
                         }
                     } catch (Exception $e) {
                         Log::warn($e);
                         if (isset($app['on_error']['status'])) {
                             Http::status_header((int) $app['on_error']['status']);
                         }
                         if (isset($app['on_error']['redirect'])) {
                             $this->save_exception($e);
                             $this->redirect($app['on_error']['redirect']);
                         } else {
                             if (isset($app['on_error']['template'])) {
                                 if (!$e instanceof Exceptions) {
                                     Exceptions::add($e);
                                 }
                                 $self->output($app['on_error']['template']);
                             } else {
                                 throw $e;
                             }
                         }
                     }
                     break;
                 case 'invoke':
                     $class_name = isset($app['class']) ? Lib::import($app['class']) : get_class($app_result);
                     $ref_class = new ReflectionClass($class_name);
                     foreach ($app['methods'] as $method) {
                         $invoke_class = $ref_class->getMethod($method['method'])->isStatic() ? $class_name : (isset($app['class']) ? new $class_name() : $app_result);
                         $args = array();
                         foreach ($method['args'] as $arg) {
                             if ($arg['type'] === 'result') {
                                 $args[] =& $app_result;
                             } else {
                                 $args[] = $arg['value'];
                             }
                         }
                         if (is_object($invoke_class)) {
                             foreach ($app['modules'] as $module) {
                                 $invoke_class->add_module(self::import_instance($module));
                             }
                         }
                         $app_result = call_user_func_array(array($invoke_class, $method['method']), $args);
                         $in_app = true;
                     }
                     break;
             }
         }
         if (!$match_handle) {
             Log::debug("nomatch");
             if ($parse_app["nomatch_redirect"] !== null) {
                 Http::redirect(App::url($parse_app["nomatch_redirect"]));
             }
             if ($parse_app["nomatch_template"] !== null) {
                 Http::status_header(404);
                 $self = new self();
                 $self->output($parse_app["nomatch_template"]);
             }
         }
         if (!$in_app) {
             Http::status_header(404);
         }
     } catch (Exception $e) {
         if (!$e instanceof Exceptions) {
             Exceptions::add($e);
         }
     }
     exit;
 }
Beispiel #5
0
 /**
  * not found (http status 404)
  */
 protected function not_found(Exception $e)
 {
     Log::debug('404');
     Http::status_header(404);
     $this->output('error/not_found.html');
     exit;
 }
Beispiel #6
0
 /**
  * リポジトリサーバの実行
  */
 public static function handler()
 {
     Log::disable_display();
     $request = new Request("_inc_session_=false");
     if (strpos($request->args(), "/check") === 0) {
         exit;
     }
     $repository = new Repository();
     self::lib($repository);
     self::app($repository);
     Object::C(__CLASS__)->call_module("repository", $repository);
     foreach ($repository->names as $type) {
         if (preg_match("/^\\/" . $type . "\\/download\\/(.+)\$/", $request->args(), $match)) {
             if (self::is_tgz($type, $match[1], $filename)) {
                 self::dl($filename);
             }
         }
         if (preg_match("/^\\/" . $type . "\\/state\\/(.+)\$/", $request->args(), $match)) {
             if (self::is_tgz($type, $match[1], $filename)) {
                 exit;
             }
         }
         if (preg_match("/^\\/" . $type . "\\/list\$/", $request->args(), $match)) {
             print self::read_xml($type);
             exit;
         }
         if (preg_match("/^\\/" . $type . "\\/list\\/json\$/", $request->args(), $match)) {
             if (Tag::setof($tag, self::read_xml($type))) {
                 Text::output_jsonp($tag, $request->in_vars("callback"));
             }
         }
     }
     Http::status_header(403);
     exit;
 }