Exemple #1
0
 /**
  * 現在のURLを返す
  * @return string
  */
 public function current_url()
 {
     return \ebi\Request::current_url();
 }
Exemple #2
0
 /**
  * アプリケーションを実行する
  * @param mixed{} $map
  */
 public static function app($map = [])
 {
     if (empty($map)) {
         $map = ['patterns' => ['' => ['action' => 'ebi.Dt', 'mode' => 'local']]];
     } else {
         if (is_string($map)) {
             $map = ['patterns' => ['' => ['action' => $map]]];
         } else {
             if (is_array($map) && !isset($map['patterns'])) {
                 $map = ['patterns' => $map];
             }
         }
     }
     if (!isset($map['patterns']) || !is_array($map['patterns'])) {
         throw new \ebi\exception\InvalidArgumentException('patterns not found');
     }
     $entry_file = null;
     foreach (debug_backtrace(false) as $d) {
         if ($d['file'] !== __FILE__) {
             $entry_file = basename($d['file']);
             break;
         }
     }
     /**
      * @param string $val アプリケーションURL、末尾が * = 実行エントリ, ** = エントリファイル名(*.php)
      */
     $app_url = \ebi\Conf::get('app_url');
     if (is_array($app_url)) {
         if (isset($app_url[$entry_file])) {
             $app_url = $app_url[$entry_file];
         } else {
             if (isset($app_url['*'])) {
                 $app_url = $app_url['*'];
             } else {
                 $app_url = null;
             }
         }
     }
     self::$app_url = $app_url;
     /**
      * @param string $val メディアファイルのベースURL
      * http://localhost:8000/resources/media
      */
     self::$media_url = \ebi\Conf::get('media_url');
     if (empty(self::$app_url)) {
         $host = \ebi\Request::host();
         self::$app_url = (empty($host) ? 'http://localhost:8000/' : $host . '/') . $entry_file;
     } else {
         if (substr(self::$app_url, -1) == '*') {
             self::$app_url = substr(self::$app_url, 0, -1);
             self::$app_url = substr(self::$app_url, -1) == '*' ? substr(self::$app_url, 0, -1) . $entry_file : self::$app_url . substr($entry_file, 0, -4);
         }
     }
     self::$app_url = \ebi\Util::path_slash(str_replace('https://', 'http://', self::$app_url), null, true);
     if (empty(self::$media_url)) {
         $media_path = preg_replace('/\\/[^\\/]+\\.php[\\/]$/', '/', self::$app_url);
         self::$media_url = $media_path . 'resources/media/';
     }
     self::$media_url = \ebi\Util::path_slash(self::$media_url, null, true);
     /**
      * @param string $val テンプレートファイルのディレクトリ
      */
     self::$template_path = \ebi\Util::path_slash(\ebi\Conf::get('template_path', \ebi\Conf::resource_path('templates')), null, true);
     $automap_idx = 1;
     $selfmap = ['patterns' => self::expand_patterns('', $map['patterns'], [], $automap_idx)];
     unset($map['patterns']);
     $selfmap = array_merge($selfmap, $map);
     self::$workgroup = array_key_exists('workgroup', $selfmap) ? $selfmap['workgroup'] : basename($entry_file, '.php');
     /**
      * @param boolean $val HTTPSを有効にするか,falseの場合、mapのsecureフラグもすべてfalseとなる
      */
     $conf_secure = \ebi\Conf::get('secure');
     $map_secure = array_key_exists('secure', $selfmap) && $selfmap['secure'] === true;
     $is_secure_pattern_func = function ($pattern) use($conf_secure, $map_secure) {
         if ($conf_secure === false) {
             return false;
         }
         if (array_key_exists('secure', $pattern)) {
             return $pattern['secure'] === true;
         }
         return $map_secure == true;
     };
     $https = str_replace('http://', 'https://', self::$app_url);
     $url_format_func = function ($url, $pattern) use($https, $is_secure_pattern_func) {
         $num = 0;
         $format = \ebi\Util::path_absolute($is_secure_pattern_func($pattern) ? $https : self::$app_url, empty($url) ? '' : substr(preg_replace_callback("/([^\\\\])(\\(.*?[^\\\\]\\))/", function ($n) {
             return $n[1] . '%s';
         }, ' ' . $url, -1, $num), 1));
         return [str_replace(['\\\\', '\\.', '_ESC_'], ['_ESC_', '.', '\\'], $format), $num];
     };
     foreach ($selfmap['patterns'] as $k => $v) {
         list($selfmap['patterns'][$k]['format'], $selfmap['patterns'][$k]['num']) = $url_format_func($k, $v);
     }
     krsort($selfmap['patterns']);
     if (self::$is_get_map) {
         self::$is_get_map = false;
         self::$map = $selfmap;
         return;
     }
     $pathinfo = preg_replace("/(.*?)\\?.*/", "\\1", isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : '');
     if (preg_match('/^\\/' . preg_quote(self::$package_media_url, '/') . '\\/(\\d+)\\/(.+)$/', $pathinfo, $m)) {
         foreach ($selfmap['patterns'] as $p) {
             if (isset($p['@']) && isset($p['idx']) && (int) $p['idx'] === (int) $m[1] && is_file($file = $p['@'] . '/resources/media/' . $m[2])) {
                 \ebi\HttpFile::attach($file);
             }
         }
         \ebi\HttpHeader::send_status(404);
         return self::terminate();
     }
     foreach ($selfmap['patterns'] as $k => $pattern) {
         if (preg_match('/^' . (empty($k) ? '' : '\\/') . str_replace(['\\/', '/', '@#S'], ['@#S', '\\/', '\\/'], $k) . '[\\/]{0,1}$/', $pathinfo, $param_arr)) {
             if ($is_secure_pattern_func($pattern)) {
                 if (substr(\ebi\Request::current_url(), 0, 5) === 'http:' && (!isset($_SERVER['HTTP_X_FORWARDED_HOST']) || isset($_SERVER['HTTP_X_FORWARDED_PORT']) && $_SERVER['HTTP_X_FORWARDED_PORT'] != 443 || isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] != 'https')) {
                     header('Location: ' . preg_replace('/^.+(:\\/\\/.+)$/', 'https\\1', \ebi\Request::current_url()));
                     exit;
                 }
                 self::$media_url = str_replace('http://', 'https://', self::$media_url);
             }
             self::$template = new \ebi\Template();
             try {
                 $funcs = $class = $method = $template = $ins = null;
                 $exception = null;
                 $has_flow_plugin = false;
                 $result_vars = $plugins = [];
                 $accept_debug = \ebi\Conf::get('accept_debug', false) && strpos(strtolower((new \ebi\Env())->get('HTTP_ACCEPT')), 'application/debug') !== false;
                 array_shift($param_arr);
                 if (array_key_exists('action', $pattern)) {
                     if (is_string($pattern['action'])) {
                         list($class, $method) = explode('::', $pattern['action']);
                     } else {
                         if (is_callable($pattern['action'])) {
                             $funcs = $pattern['action'];
                         } else {
                             throw new \ebi\exception\InvalidArgumentException($pattern['name'] . ' action invalid');
                         }
                     }
                 }
                 foreach ($selfmap['patterns'] as $m) {
                     self::$url_pattern[$m['name']][$m['num']] = $m['format'];
                     if (array_key_exists('@', $pattern) && array_key_exists('@', $m) && $pattern['idx'] == $m['idx']) {
                         list(, $mm) = explode('::', $m['action']);
                         self::$selected_class_pattern[$mm][$m['num']] = ['format' => $m['format'], 'name' => $m['name']];
                     }
                 }
                 if (array_key_exists('vars', $selfmap) && is_array($selfmap['vars'])) {
                     $result_vars = $selfmap['vars'];
                 }
                 if (array_key_exists('vars', $pattern) && is_array($pattern['vars'])) {
                     $result_vars = array_merge($result_vars, $pattern['vars']);
                 }
                 if (array_key_exists('redirect', $pattern)) {
                     self::map_redirect($pattern['redirect'], $result_vars, $pattern);
                 }
                 foreach (array_merge(array_key_exists('plugins', $selfmap) ? is_array($selfmap['plugins']) ? $selfmap['plugins'] : [$selfmap['plugins']] : [], array_key_exists('plugins', $pattern) ? is_array($pattern['plugins']) ? $pattern['plugins'] : [$pattern['plugins']] : []) as $m) {
                     $o = \ebi\Util::strtoinstance($m);
                     self::set_class_plugin($o);
                     $plugins[] = $o;
                 }
                 if (!isset($funcs) && isset($class)) {
                     $ins = \ebi\Util::strtoinstance($class);
                     $traits = \ebi\Util::get_class_traits(get_class($ins));
                     if ($has_flow_plugin = in_array('ebi\\FlowPlugin', $traits)) {
                         foreach ($ins->get_flow_plugins() as $m) {
                             $o = \ebi\Util::strtoinstance($m);
                             $plugins[] = $o;
                             self::set_class_plugin($o);
                         }
                         $ins->set_pattern($pattern);
                     }
                     if (in_array('ebi\\Plugin', $traits)) {
                         foreach ($plugins as $o) {
                             $ins->set_object_plugin($o);
                         }
                     }
                     $funcs = [$ins, $method];
                 }
                 foreach ($plugins as $o) {
                     self::$template->set_object_plugin($o);
                 }
                 if (self::has_class_plugin('before_flow_action')) {
                     /**
                      * 前処理
                      */
                     self::call_class_plugin_funcs('before_flow_action');
                 }
                 if ($has_flow_plugin) {
                     $ins->before();
                     $before_redirect = $ins->get_before_redirect();
                     if (isset($before_redirect)) {
                         self::map_redirect($before_redirect, $result_vars, $pattern);
                     }
                 }
                 if (isset($funcs)) {
                     try {
                         $action_result_vars = call_user_func_array($funcs, $param_arr);
                         if (is_array($action_result_vars)) {
                             $result_vars = array_merge($result_vars, $action_result_vars);
                         }
                     } catch (\Exception $exception) {
                     }
                 }
                 if ($has_flow_plugin) {
                     $ins->after();
                     $template_block = $ins->get_template_block();
                     if (!empty($template_block)) {
                         self::$template->put_block(\ebi\Util::path_absolute(self::$template_path, $ins->get_template_block()));
                     }
                     $template = $ins->get_template();
                     if (!empty($template)) {
                         $pattern['template'] = $template;
                     }
                     $result_vars = array_merge($result_vars, $ins->get_after_vars());
                     $after_redirect = $ins->get_after_redirect();
                     if (isset($after_redirect) && !array_key_exists('after', $pattern) && !array_key_exists('cond_after', $pattern)) {
                         $pattern['after'] = $after_redirect;
                     }
                 }
                 if (self::has_class_plugin('after_flow_action')) {
                     /**
                      * 後処理
                      */
                     self::call_class_plugin_funcs('after_flow_action');
                 }
                 if (isset($exception)) {
                     throw $exception;
                 }
                 \ebi\Exceptions::throw_over();
                 if (!$accept_debug) {
                     if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] == 'POST') {
                         if (array_key_exists('post_cond_after', $pattern) && is_array($pattern['post_cond_after'])) {
                             foreach ($pattern['post_cond_after'] as $cak => $cav) {
                                 if (isset($result_vars[$cak])) {
                                     self::map_redirect($cav, $result_vars, $pattern);
                                 }
                             }
                         }
                         if (array_key_exists('post_after', $pattern)) {
                             self::map_redirect($pattern['post_after'], $result_vars, $pattern);
                         }
                     }
                     if (array_key_exists('cond_after', $pattern) && is_array($pattern['cond_after'])) {
                         foreach ($pattern['cond_after'] as $cak => $cav) {
                             if (isset($result_vars[$cak])) {
                                 self::map_redirect($cav, $result_vars, $pattern);
                             }
                         }
                     }
                     if (array_key_exists('after', $pattern)) {
                         self::map_redirect($pattern['after'], $result_vars, $pattern);
                     }
                     if (array_key_exists('template', $pattern)) {
                         if (array_key_exists('template_super', $pattern)) {
                             self::$template->template_super(\ebi\Util::path_absolute(self::$template_path, $pattern['template_super']));
                         }
                         return self::template($result_vars, $pattern, $ins, \ebi\Util::path_absolute(self::$template_path, $pattern['template']));
                     } else {
                         if (isset($template)) {
                             return self::template($result_vars, $pattern, $ins, \ebi\Util::path_absolute(self::$template_path, $template));
                         } else {
                             if (array_key_exists('@', $pattern) && is_file($t = \ebi\Util::path_absolute(self::$template_path, $pattern['name']) . '.html')) {
                                 return self::template($result_vars, $pattern, $ins, $t);
                             } else {
                                 if (array_key_exists('@', $pattern) && is_file($t = $pattern['@'] . '/resources/templates/' . preg_replace('/^.+::/', '', $pattern['action'] . '.html'))) {
                                     $app_url = $is_secure_pattern_func($pattern) ? str_replace('http://', 'https://', self::$app_url) : self::$app_url;
                                     return self::template($result_vars, $pattern, $ins, $t, $app_url . self::$package_media_url . '/' . $pattern['idx']);
                                 } else {
                                     if (array_key_exists('find_template', $selfmap) && $selfmap['find_template'] === true && is_file($t = \ebi\Util::path_absolute(self::$template_path, $pattern['name'] . '.html'))) {
                                         return self::template($result_vars, $pattern, $ins, $t);
                                     } else {
                                         if (self::has_class_plugin('flow_output')) {
                                             /**
                                              * 結果を出力する
                                              * @param mixed{} $result_vars actionで返却された変数
                                              */
                                             self::call_class_plugin_funcs('flow_output', $result_vars);
                                             return self::terminate();
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
                 \ebi\HttpHeader::send('Content-Type', 'application/json');
                 print \ebi\Json::encode(['result' => \ebi\Util::to_primitive($result_vars)]);
                 return self::terminate();
             } catch (\Exception $e) {
                 \ebi\FlowInvalid::set($e);
                 \ebi\Dao::rollback_all();
                 /**
                  * @param string[] $val ログに記録しない例外クラス名
                  */
                 if (!in_array(get_class($e), \ebi\Conf::gets('ignore_exceptions'))) {
                     \ebi\Log::warning($e);
                 }
                 if (isset($pattern['error_status'])) {
                     \ebi\HttpHeader::send_status($pattern['error_status']);
                 } else {
                     if (isset($selfmap['error_status'])) {
                         \ebi\HttpHeader::send_status($selfmap['error_status']);
                     }
                 }
                 if (isset($pattern['vars']) && !empty($pattern['vars']) && is_array($pattern['vars'])) {
                     $result_vars = array_merge($result_vars, $pattern['vars']);
                 }
                 if (!$accept_debug) {
                     if (isset($pattern['@']) && is_file($t = $pattern['@'] . '/resources/templates/error.html')) {
                         $app_url = $is_secure_pattern_func($pattern) ? str_replace('http://', 'https://', self::$app_url) : self::$app_url;
                         return self::template($result_vars, $pattern, $ins, $t, $app_url . self::$package_media_url . '/' . $pattern['idx']);
                     } else {
                         if (array_key_exists('error_redirect', $pattern)) {
                             return self::map_redirect($pattern['error_redirect'], [], $pattern);
                         } else {
                             if (array_key_exists('error_template', $pattern)) {
                                 return self::template($result_vars, $pattern, $ins, \ebi\Util::path_absolute(self::$template_path, $pattern['error_template']));
                             } else {
                                 if (array_key_exists('error_redirect', $selfmap)) {
                                     return self::map_redirect($selfmap['error_redirect'], [], $pattern);
                                 } else {
                                     if (array_key_exists('error_template', $selfmap)) {
                                         return self::template($result_vars, $pattern, $ins, \ebi\Util::path_absolute(self::$template_path, $selfmap['error_template']));
                                     } else {
                                         if (self::has_class_plugin('flow_exception')) {
                                             /**
                                              * 例外発生時の処理・出力
                                              * @param \Exception $e 発生した例外
                                              */
                                             self::call_class_plugin_funcs('flow_exception', $e);
                                             return self::terminate();
                                         } else {
                                             if (self::has_class_plugin('flow_output')) {
                                                 self::call_class_plugin_funcs('flow_output', ['error' => ['message' => $e->getMessage()]]);
                                                 return self::terminate();
                                             }
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
                 /**
                  *  @param boolean $val Error Json出力時にException traceも出力するフラグ
                  */
                 $trace = \ebi\Conf::get('exception_trace', false);
                 $message = [];
                 foreach (\ebi\FlowInvalid::get() as $g => $e) {
                     $em = ['message' => $e->getMessage(), 'type' => basename(str_replace("\\", '/', get_class($e)))];
                     if ($trace) {
                         $em['trace'] = $e->getTraceAsString();
                     }
                     if (!empty($g)) {
                         $em['group'] = $g;
                     }
                     $message[] = $em;
                 }
                 \ebi\HttpHeader::send('Content-Type', 'application/json');
                 print json_encode(['error' => $message]);
                 return self::terminate();
             }
         }
     }
     if (array_key_exists('nomatch_redirect', $selfmap) && strpos($selfmap['nomatch_redirect'], '://') === false) {
         foreach ($selfmap['patterns'] as $m) {
             if ($selfmap['nomatch_redirect'] == $m['name']) {
                 self::$url_pattern[$m['name']][$m['num']] = $m['format'];
                 break;
             }
         }
         self::map_redirect($selfmap['nomatch_redirect'], [], []);
     }
     \ebi\HttpHeader::send_status(404);
     return self::terminate();
 }
Exemple #3
0
 /**
  * ログインしていない場合にログイン処理を実行する
  */
 private function login_required()
 {
     $selected_pattern = $this->get_selected_pattern();
     if (!$this->is_user_logged_in() && array_key_exists('action', $selected_pattern) && strpos($selected_pattern['action'], '::do_login') === false) {
         if ($this->has_object_plugin('before_login_required')) {
             /**
              * ログイン処理の前処理
              * @param \ebi\flow\Request $arg1
              */
             $this->call_object_plugin_funcs('before_login_required', $this);
         }
         if (strpos($selected_pattern['action'], '::do_logout') === false) {
             $this->sessions('logged_in_redirect_to', \ebi\Request::current_url() . \ebi\Request::request_string(true));
         }
         $req = new \ebi\Request();
         $this->sess->vars(__CLASS__ . '_login_vars', [time(), $req->ar_vars()]);
         if (array_key_exists('@', $selected_pattern)) {
             $this->set_before_redirect('do_login');
         } else {
             $this->set_before_redirect('login');
         }
     }
 }
Exemple #4
0
 /**
  * RequestのPaginator
  * @param \ebi\Request $req
  * @param integer $default_paginate_by
  * @param integer $max_paginate_by
  * @return \ebi\Paginator
  */
 public static function request(\ebi\Request $req, $default_paginate_by = 20, $max_paginate_by = 100)
 {
     $paginate_by = $req->in_vars('paginate_by', $default_paginate_by);
     if ($paginate_by > $max_paginate_by) {
         $paginate_by = $max_paginate_by;
     }
     $self = new self($paginate_by, $req->in_vars('page', 1));
     if ($req->is_vars('order')) {
         $o = $req->in_vars('order');
         $p = $req->in_vars('porder');
         if ($o == $p) {
             if ($o[0] == '-') {
                 $o = substr($o, 1);
             } else {
                 $o = '-' . $o;
             }
             $req->vars('order', $o);
         }
         $self->order($o);
     }
     $self->cp($req->ar_vars());
     return $self;
 }
Exemple #5
0
 public function before_flow_action_request(\ebi\Request $req)
 {
     if ($req->is_vars('callback2')) {
         $this->callback = $req->in_vars('callback2');
     }
 }