コード例 #1
0
ファイル: SqliteConnector.php プロジェクト: tokushima/ebi
 /**
  * @param string $name
  * @param string $host
  * @param number $port
  * @param string $user
  * @param string $password
  * @param string $sock
  * @param boolean $autocommit
  */
 public function connect($name, $host, $port, $user, $password, $sock, $autocommit)
 {
     unset($port, $user, $password, $sock);
     if (!extension_loaded('pdo_sqlite')) {
         throw new \ebi\exception\ConnectionException('pdo_sqlite not supported');
     }
     $con = null;
     if (empty($name)) {
         $name = getcwd() . '/data.sqlite3';
     }
     if ($host != ':memory:') {
         if (strpos($name, '.') === false) {
             $name = $name . '.sqlite3';
         }
         $host = str_replace('\\', '/', $host);
         if (substr($host, -1) != '/') {
             $host = $host . '/';
         }
         $path = \ebi\Util::path_absolute($host, $name);
         \ebi\Util::mkdir(dirname($path));
     }
     try {
         $con = new \PDO(sprintf('sqlite:%s', $host == ':memory:' ? ':memory:' : $path));
         $con->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
     } catch (\PDOException $e) {
         throw new \ebi\exception\ConnectionException($e->getMessage());
     }
     if (!empty($this->timezone)) {
         $this->timezone_offset = (new \DateTimeZone($this->timezone))->getOffset(new \DateTime('now', new \DateTimeZone('UTC')));
     }
     return $con;
 }
コード例 #2
0
ファイル: Loader.php プロジェクト: tokushima/ebi
 /**
  * pharのパスを通す
  * @param string $path
  * @param string $namespace
  */
 public static function phar($path, $namespace = null)
 {
     /**
      * @param string $arg1 pahrが格納されているディレクトリ
      */
     $base = \ebi\Conf::get('path', getcwd());
     $path = realpath(\ebi\Util::path_absolute($base, $path));
     if (isset(self::$read[$path])) {
         return true;
     }
     if ($path === false) {
         throw new \ebi\exception\InvalidArgumentException($path . ' not found');
     }
     if (!empty($namespace)) {
         $namespace = str_replace("\\", '/', $namespace);
         if ($namespace[0] == '/') {
             $namespace = substr($namespace, 1);
         }
     }
     $package_dir = 'phar://' . (is_dir('phar://' . $path . '/src/') ? $path . '/src' : $path);
     spl_autoload_register(function ($c) use($package_dir, $namespace) {
         $c = str_replace(array('\\', '_'), '/', $c);
         if ((empty($namespace) || strpos($c, $namespace) === 0) && is_file($f = $package_dir . '/' . $c . '.php')) {
             require_once $f;
         }
         return false;
     }, true, false);
     self::$read[$path] = true;
     return true;
 }
コード例 #3
0
ファイル: TemplateParts.php プロジェクト: tokushima/ebi
 /**
  * @plugin ebi.Temaplte
  * @param string $src
  * @return Ambigous <string, string, mixed>|string
  */
 public function before_template($src)
 {
     /**
      * @param string $path テンプレートパーツのファイルがあるディレクトリ
      */
     $path = \ebi\Util::path_slash(\ebi\Conf::get('path', \ebi\Conf::resource_path('parts')), null, true);
     return \ebi\Xml::find_replace($src, 'rt:parts', function ($xml) use($path) {
         $href = \ebi\Util::path_absolute($path, $xml->in_attr('href'));
         if (!is_file($href)) {
             throw new \ebi\exception\InvalidArgumentException($href . ' not found');
         }
         return file_get_contents($href);
     });
 }
コード例 #4
0
ファイル: HtmlForm.php プロジェクト: tokushima/ebi
 private static function parse($html, $url)
 {
     $forms = [];
     try {
         foreach (\ebi\Xml::extract($html, 'body')->find('form') as $key => $formtag) {
             $form = new \stdClass();
             $form->name = $formtag->in_attr('name', $formtag->in_attr('id', $key));
             $form->action = \ebi\Util::path_absolute($url, $formtag->in_attr('action', $url));
             $form->method = strtolower($formtag->in_attr('method', 'get'));
             $form->multiple = false;
             $form->element = [];
             foreach ($formtag->find('input') as $count => $input) {
                 $obj = new \stdClass();
                 $obj->name = $input->in_attr('name', $input->in_attr('id', 'input_' . $count));
                 $obj->type = strtolower($input->in_attr('type', 'text'));
                 $obj->value = self::htmldecode($input->in_attr('value'));
                 $obj->selected = 'selected' === strtolower($input->in_attr('checked', $input->in_attr('checked')));
                 $obj->multiple = false;
                 $form->element[] = $obj;
             }
             foreach ($formtag->find('textarea') as $count => $input) {
                 $obj = new \stdClass();
                 $obj->name = $input->in_attr('name', $input->in_attr('id', 'textarea_' . $count));
                 $obj->type = 'textarea';
                 $obj->value = self::htmldecode($input->value());
                 $obj->selected = true;
                 $obj->multiple = false;
                 $form->element[] = $obj;
             }
             foreach ($formtag->find('select') as $count => $input) {
                 $obj = new \stdClass();
                 $obj->name = $input->in_attr('name', $input->in_attr('id', 'select_' . $count));
                 $obj->type = 'select';
                 $obj->value = [];
                 $obj->selected = true;
                 $obj->multiple = 'multiple' == strtolower($input->param('multiple', $input->attr('multiple')));
                 foreach ($input->find('option') as $count => $option) {
                     $op = new \stdClass();
                     $op->value = self::htmldecode($option->in_attr('value', $option->value()));
                     $op->selected = 'selected' == strtolower($option->in_attr('selected', $option->in_attr('selected')));
                     $obj->value[] = $op;
                 }
                 $form->element[] = $obj;
             }
             $forms[] = $form;
         }
     } catch (\ebi\exception\NotFoundException $e) {
     }
     return $forms;
 }
コード例 #5
0
ファイル: Xml.php プロジェクト: tokushima/ebi
 public function flow_output($array)
 {
     if (strpos(strtolower((new \ebi\Env())->get('HTTP_ACCEPT')), 'application/json') !== false) {
         \ebi\HttpHeader::send('Content-Type', 'application/json');
         print \ebi\Json::encode(['result' => \ebi\Util::to_primitive($array)]);
     } else {
         $xml = new \ebi\Xml('result');
         $xml->add($array);
         \ebi\HttpHeader::send('Content-Type', 'application/xml');
         /**
          * @param string $encoding XMLのencodingに指定するエンコード名
          */
         print $xml->get(\ebi\Conf::get('encoding'));
     }
 }
コード例 #6
0
ファイル: Db.php プロジェクト: tokushima/ebi
 /**
  * コンストラクタ
  * @param string{} $def 接続情報 [type,host,name,port,user,password,sock,encode,timezone]
  */
 public function __construct(array $def = [])
 {
     if (empty($def)) {
         /**
          * @param string{} $connection デフォルトの接続情報 [type,host,name,port,user,password,sock,encode,timezone]
          */
         $def = \ebi\Conf::gets('connection');
     }
     $type = isset($def['type']) ? $def['type'] : null;
     $host = isset($def['host']) ? $def['host'] : null;
     $dbname = isset($def['name']) ? $def['name'] : null;
     $port = isset($def['port']) ? $def['port'] : null;
     $user = isset($def['user']) ? $def['user'] : null;
     $password = isset($def['password']) ? $def['password'] : null;
     $sock = isset($def['sock']) ? $def['sock'] : null;
     $encode = isset($def['encode']) ? $def['encode'] : null;
     $timezone = isset($def['timezone']) ? $def['timezone'] : null;
     if (empty($type)) {
         $type = \ebi\SqliteConnector::type();
     }
     if (empty($encode)) {
         $encode = 'utf8';
     }
     try {
         $type = \ebi\Util::get_class_name($type);
     } catch (\InvalidArgumentException $e) {
         throw new \ebi\exception\ConnectionException('could not find connector `' . $type . '`');
     }
     $r = new \ReflectionClass($type);
     $this->dbname = $dbname;
     $this->connector = $r->newInstanceArgs([$encode, $timezone]);
     if (!$this->connector instanceof \ebi\DbConnector) {
         throw new \ebi\exception\ConnectionException('must be an instance of \\ebi\\DbConnector');
     }
     if (self::$autocommit === null) {
         /**
          * @param boolean $autocommit オートコミットを行うかの真偽値
          */
         self::$autocommit = \ebi\Conf::get('autocommit', false);
     }
     $this->connection = $this->connector->connect($this->dbname, $host, $port, $user, $password, $sock, self::$autocommit);
     if (empty($this->connection)) {
         throw new \ebi\exception\ConnectionException('connection fail ' . $this->dbname);
     }
     if (self::$autocommit !== true) {
         $this->connection->beginTransaction();
     }
 }
コード例 #7
0
ファイル: Helper.php プロジェクト: tokushima/ebi
 /**
  * print_r
  * @param mixed $obj
  */
 public function dump($obj)
 {
     $result = [];
     foreach ($obj as $k => $v) {
         if (isset($obj[$k])) {
             if (!is_array($obj[$k]) || !empty($obj[$k])) {
                 $result[$k] = $v;
             }
         }
         if (is_bool($obj[$k])) {
             $result[$k] = $result[$k] ? 'true' : 'false';
         }
     }
     if ($result['class'] && is_string($result['class'])) {
         $result['class'] = \ebi\Util::get_class_name($result['class']);
     }
     $value = print_r($result, true);
     $value = str_replace('=>' . PHP_EOL, ': ', trim($value));
     $value = preg_replace('/\\[\\d+\\]/', '&nbsp;&nbsp;\\0', $value);
     return implode(PHP_EOL, array_slice(explode(PHP_EOL, $value), 2, -1));
 }
コード例 #8
0
ファイル: Request.php プロジェクト: tokushima/ebi
 /**
  * 前処理、入力値のバリデーションやログイン処理を行う
  * __before__メソッドを定義することで拡張する
  */
 public function before()
 {
     list(, $method) = explode('::', $this->get_selected_pattern()['action']);
     $annon = \ebi\Annotation::get_method(get_class($this), $method, ['http_method', 'request', 'user_role']);
     if (isset($annon['http_method']['value']) && strtoupper($annon['http_method']['value']) != \ebi\Request::method()) {
         throw new \ebi\exception\BadMethodCallException('Method Not Allowed');
     }
     if (isset($annon['request'])) {
         foreach ($annon['request'] as $k => $an) {
             if (isset($an['type'])) {
                 try {
                     \ebi\Validator::type($k, $this->in_vars($k), $an);
                 } catch (\ebi\exception\InvalidArgumentException $e) {
                     \ebi\Exceptions::add($e, $k);
                 }
                 \ebi\Validator::value($k, $this->in_vars($k), $an);
             }
         }
     }
     \ebi\Exceptions::throw_over();
     if (method_exists($this, '__before__')) {
         $this->__before__();
     }
     if ($this->has_object_plugin('before_flow_action_request')) {
         /**
          * 前処理
          * @param \ebi\flow\Request $arg1
          */
         $this->call_object_plugin_funcs('before_flow_action_request', $this);
     }
     if (!$this->is_user_logged_in() && (isset($this->login_anon) || $this->has_object_plugin('login_condition'))) {
         $this->login_required();
     }
     if ($this->is_user_logged_in() && (isset($annon['user_role']) || isset($this->login_anon['user_role']))) {
         if (!in_array(\ebi\UserRole::class, \ebi\Util::get_class_traits(get_class($this->user()))) || isset($this->login_anon['user_role']) && !in_array($this->login_anon['user_role'], $this->user()->get_role()) || isset($annon['user_role']['value']) && !in_array($annon['user_role']['value'], $this->user()->get_role())) {
             throw new \ebi\exception\NotPermittedException();
         }
     }
 }
コード例 #9
0
ファイル: Archive.php プロジェクト: tokushima/ebi
 /**
  * zipを解凍してファイル書き出しを行う
  * @param string $zipfile 解凍するZIPファイル
  * @param string $outpath 解凍先のファイルパス
  */
 public static function unzip($zipfile, $outpath)
 {
     $zip = new \ZipArchive();
     if ($zip->open($zipfile) !== true) {
         throw new \ebi\exception\InvalidArgumentException('failed to open stream');
     }
     if (substr($outpath, -1) != '/') {
         $outpath = $outpath . '/';
     }
     if (!is_dir($outpath)) {
         \ebi\Util::mkdir($outpath, 0777);
     }
     $zip->extractTo($outpath);
     $zip->close();
 }
コード例 #10
0
ファイル: Request.php プロジェクト: tokushima/ebi
 /**
  * 添付ファイルを移動します
  * @param array $file_info
  * @param string $newname
  */
 public function move_file($file_info, $newname)
 {
     if (is_string($file_info)) {
         $file_info = $this->in_files($file_info);
     }
     if (!$this->has_file($file_info)) {
         throw new \ebi\exception\NotFoundException('file not found ');
     }
     if (!is_dir(dirname($newname))) {
         \ebi\Util::mkdir(dirname($newname));
     }
     \ebi\Util::copy($file_info['tmp_name'], $newname);
     \ebi\Util::rm($file_info['tmp_name']);
 }
コード例 #11
0
ファイル: Dt.php プロジェクト: tokushima/ebi
 /**
  * テーブルを削除後作成する
  */
 public static function reset_tables()
 {
     foreach (\ebi\Dt::classes(\ebi\Dao::class) as $class_info) {
         $class = \ebi\Util::get_class_name($class_info['class']);
         call_user_func([$class, 'drop_table']);
         call_user_func([$class, 'create_table']);
     }
 }
コード例 #12
0
ファイル: Mail.php プロジェクト: tokushima/ebi
 /**
  * Set template
  * @param string $template_path Relative path from resource_path
  * @param mixed{} $vars bind variables
  * @return $this
  */
 public function set_template($template_path, $vars = [])
 {
     /**
      * @param string $path Email template resources root
      */
     $resource_path = \ebi\Conf::get('resource_path', \ebi\Conf::resource_path('mail'));
     $path = \ebi\Util::path_absolute($resource_path, $template_path);
     if (!is_file($path)) {
         throw new \ebi\exception\InvalidArgumentException($template_path . ' not found');
     }
     $xml = \ebi\Xml::extract(file_get_contents($path), 'mail');
     try {
         try {
             $from = $xml->find_get('from');
             $this->from($from->in_attr('address'), $from->in_attr('name'));
         } catch (\ebi\exception\NotFoundException $e) {
         }
         foreach ($xml->find('to') as $to) {
             $this->to($to->in_attr('address'), $to->in_attr('name'));
         }
         try {
             $this->return_path($xml->find_get('return_path')->in_attr('address'));
         } catch (\ebi\exception\NotFoundException $e) {
         }
         /**
          * @param string $xtc_name xtc query key
          */
         $xtc_name = \ebi\Conf::get('xtc_name', 'xtc');
         $xtc = self::xtc($template_path);
         $this->header['X-T-Code'] = $xtc;
         $vars['t'] = new \ebi\FlowHelper();
         $vars['xtc'] = [$xtc_name => $xtc];
         $subject = trim(str_replace(["\r\n", "\r", "\n"], '', $xml->find_get('subject')->value()));
         $template = new \ebi\Template();
         $template->cp($vars);
         $body_xml = $xml->find_get('body');
         $signature = $body_xml->in_attr('signature');
         $signature_text = '';
         if (!empty($signature)) {
             $sig_path = \ebi\Util::path_absolute($resource_path, $signature);
             if (!is_file($sig_path)) {
                 throw new \ebi\exception\InvalidArgumentException($signature . ' not found');
             }
             $sig_xml = \ebi\Xml::extract(file_get_contents($sig_path), 'mail');
             $signature_text = \ebi\Util::plain_text(PHP_EOL . $sig_xml->find_get('signature')->value() . PHP_EOL);
         }
         $message = $template->get(\ebi\Util::plain_text(PHP_EOL . $body_xml->value() . PHP_EOL) . $signature_text);
         $this->message($message);
         $this->subject($template->get($subject));
         try {
             $html = $xml->find_get('html');
             $html_path = \ebi\Util::path_absolute($resource_path, $html->in_attr('src', preg_replace('/^(.+)\\.\\w+$/', '\\1', $path) . '.html'));
             foreach ($html->find('media') as $media) {
                 $file = \ebi\Util::path_absolute($resource_path, $media->in_attr('src'));
                 if (!is_file($file)) {
                     throw new \ebi\exception\InvalidArgumentException($media->in_attr('src') . ' invalid media');
                 }
                 $this->media($media->in_attr('src'), file_get_contents($file));
             }
             $template = new \ebi\Template();
             $template->cp($vars);
             $this->html($template->read($html_path));
         } catch (\ebi\exception\NotFoundException $e) {
         }
         foreach ($xml->find('attach') as $attach) {
             $file = \ebi\Util::path_absolute($resource_path, $attach->in_attr('src'));
             if (!is_file($file)) {
                 throw new \ebi\exception\InvalidArgumentException($attach->in_attr('src') . ' invalid media');
             }
             $this->attach($attach->in_attr('name', $attach->in_attr('src')), file_get_contents($file));
         }
         return $this;
     } catch (\ebi\exception\NotFoundException $e) {
         throw new \ebi\exception\InvalidArgumentException($template_path . ' invalid data');
     }
 }
コード例 #13
0
ファイル: Conf.php プロジェクト: tokushima/ebi
 /**
  * Pluginに遅延セットする
  * @param string $class_name
  * @param string $obj
  */
 public static function set_class_plugin($class_name, $obj = null)
 {
     if (is_array($class_name)) {
         foreach ($class_name as $c => $v) {
             static::set_class_plugin($c, $v);
         }
     } else {
         if (!empty($obj)) {
             $class_name = \ebi\Util::get_class_name($class_name);
             if (!is_array($obj)) {
                 $obj = [$obj];
             }
             foreach ($obj as $o) {
                 if (is_string($o)) {
                     $o = \ebi\Util::get_class_name($o);
                 }
                 self::$plugins[$class_name][] = $o;
             }
         }
     }
 }
コード例 #14
0
ファイル: add_date.php プロジェクト: tokushima/ebi
<?php

eq('2014/10/04', date('Y/m/d', \ebi\Util::add_date('38 year', '1976/10/04')));
eq('2014/10/04', date('Y/m/d', \ebi\Util::add_date('1 day', '2014/10/03')));
eq('2014/10/04', date('Y/m/d', \ebi\Util::add_date('1 month', '2014/09/04')));
eq('2014/10/04', date('Y/m/d', \ebi\Util::add_date('-1 month', '2014/11/04')));
try {
    eq('2014/10/04', \ebi\Util::add_date('1', '2014/11/04'));
    fail();
} catch (\ebi\exception\InvalidArgumentException $e) {
}
eq('2016/10/03 00:00:00', date('Y/m/d H:i:s', \ebi\Util::add_date('yesterday', '2016/10/04 12:34:56')));
eq('2016/10/04 00:00:00', date('Y/m/d H:i:s', \ebi\Util::add_date('today', '2016/10/04 12:34:56')));
eq('2016/10/05 00:00:00', date('Y/m/d H:i:s', \ebi\Util::add_date('tomorrow', '2016/10/04 12:34:56')));
eq('2016/10/01 00:00:00', date('Y/m/d H:i:s', \ebi\Util::add_date('first', '2016/10/04 12:34:56')));
eq('2016/10/31 23:59:59', date('Y/m/d H:i:s', \ebi\Util::add_date('last', '2016/10/04 12:34:56')));
コード例 #15
0
ファイル: Browser.php プロジェクト: tokushima/ebi
 private function request($method, $url, $download_path = null)
 {
     if (!isset($this->resource)) {
         $this->resource = curl_init();
     }
     $url_info = parse_url($url);
     $cookie_base_domain = (isset($url_info['host']) ? $url_info['host'] : '') . (isset($url_info['path']) ? $url_info['path'] : '');
     switch ($method) {
         case 'RAW':
         case 'POST':
             curl_setopt($this->resource, CURLOPT_POST, true);
             break;
         case 'GET':
             if (isset($url_info['query'])) {
                 parse_str($url_info['query'], $vars);
                 foreach ($vars as $k => $v) {
                     if (!isset($this->request_vars[$k])) {
                         $this->request_vars[$k] = $v;
                     }
                 }
                 list($url) = explode('?', $url, 2);
             }
             curl_setopt($this->resource, CURLOPT_HTTPGET, true);
             break;
         case 'HEAD':
             curl_setopt($this->resource, CURLOPT_NOBODY, true);
             break;
         case 'PUT':
             curl_setopt($this->resource, CURLOPT_PUT, true);
             break;
         case 'DELETE':
             curl_setopt($this->resource, CURLOPT_CUSTOMREQUEST, 'DELETE');
             break;
     }
     switch ($method) {
         case 'POST':
             if (!empty($this->request_file_vars)) {
                 $vars = [];
                 if (!empty($this->request_vars)) {
                     foreach (explode('&', http_build_query($this->request_vars)) as $q) {
                         $s = explode('=', $q, 2);
                         $vars[urldecode($s[0])] = isset($s[1]) ? urldecode($s[1]) : null;
                     }
                 }
                 foreach (explode('&', http_build_query($this->request_file_vars)) as $q) {
                     $s = explode('=', $q, 2);
                     if (isset($s[1])) {
                         if (!is_file($f = urldecode($s[1]))) {
                             throw new \ebi\exception\InvalidArgumentException($f . ' not found');
                         }
                         $vars[urldecode($s[0])] = class_exists('\\CURLFile', false) ? new \CURLFile($f) : '@' . $f;
                     }
                 }
                 curl_setopt($this->resource, CURLOPT_POSTFIELDS, $vars);
             } else {
                 curl_setopt($this->resource, CURLOPT_POSTFIELDS, http_build_query($this->request_vars));
             }
             break;
         case 'RAW':
             if (!isset($this->request_header['Content-Type'])) {
                 $this->request_header['Content-Type'] = 'text/plain';
             }
             curl_setopt($this->resource, CURLOPT_POSTFIELDS, $this->raw);
             break;
         case 'GET':
         case 'HEAD':
         case 'PUT':
         case 'DELETE':
             $url = $url . (!empty($this->request_vars) ? '?' . http_build_query($this->request_vars) : '');
     }
     curl_setopt($this->resource, CURLOPT_URL, $url);
     curl_setopt($this->resource, CURLOPT_FOLLOWLOCATION, false);
     curl_setopt($this->resource, CURLOPT_HEADER, false);
     curl_setopt($this->resource, CURLOPT_RETURNTRANSFER, false);
     curl_setopt($this->resource, CURLOPT_FORBID_REUSE, true);
     curl_setopt($this->resource, CURLOPT_FAILONERROR, false);
     curl_setopt($this->resource, CURLOPT_TIMEOUT, $this->timeout);
     if (self::$recording_request) {
         curl_setopt($this->resource, CURLINFO_HEADER_OUT, true);
     }
     /**
      * @param boolean $ssl_verify SSL証明書を確認するかの真偽値
      */
     if (\ebi\Conf::get('ssl-verify', true) === false) {
         curl_setopt($this->resource, CURLOPT_SSL_VERIFYHOST, false);
         curl_setopt($this->resource, CURLOPT_SSL_VERIFYPEER, false);
     }
     if (!empty($this->user)) {
         curl_setopt($this->resource, CURLOPT_USERPWD, $this->user . ':' . $this->password);
     }
     if (!isset($this->request_header['Expect'])) {
         $this->request_header['Expect'] = null;
     }
     if (!isset($this->request_header['Cookie'])) {
         $cookies = '';
         foreach ($this->cookie as $domain => $cookie_value) {
             if (strpos($cookie_base_domain, $domain) === 0 || strpos($cookie_base_domain, $domain[0] == '.' ? $domain : '.' . $domain) !== false) {
                 foreach ($cookie_value as $k => $v) {
                     if (!$v['secure'] || $v['secure'] && substr($url, 0, 8) == 'https://') {
                         $cookies .= sprintf('%s=%s; ', $k, $v['value']);
                     }
                 }
             }
         }
         curl_setopt($this->resource, CURLOPT_COOKIE, $cookies);
     }
     if (!isset($this->request_header['User-Agent'])) {
         curl_setopt($this->resource, CURLOPT_USERAGENT, empty($this->agent) ? isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : null : $this->agent);
     }
     curl_setopt($this->resource, CURLOPT_HTTPHEADER, array_map(function ($k, $v) {
         return $k . ': ' . $v;
     }, array_keys($this->request_header), $this->request_header));
     curl_setopt($this->resource, CURLOPT_HEADERFUNCTION, [$this, 'callback_head']);
     if (empty($download_path)) {
         curl_setopt($this->resource, CURLOPT_WRITEFUNCTION, [$this, 'callback_body']);
     } else {
         if (strpos($download_path, '://') === false && !is_dir(dirname($download_path))) {
             mkdir(dirname($download_path), 0777, true);
         }
         $fp = fopen($download_path, 'wb');
         curl_setopt($this->resource, CURLOPT_WRITEFUNCTION, function ($resource, $data) use(&$fp) {
             if ($fp) {
                 fwrite($fp, $data);
             }
             return strlen($data);
         });
     }
     $this->request_header = $this->request_vars = [];
     $this->head = $this->body = $this->raw = '';
     curl_exec($this->resource);
     if (!empty($download_path) && $fp) {
         fclose($fp);
     }
     if (($err_code = curl_errno($this->resource)) > 0) {
         if ($err_code == 47 || $err_code == 52) {
             return $this;
         }
         throw new \ebi\exception\ConnectionException($err_code . ': ' . curl_error($this->resource));
     }
     $this->url = curl_getinfo($this->resource, CURLINFO_EFFECTIVE_URL);
     $this->status = curl_getinfo($this->resource, CURLINFO_HTTP_CODE);
     if (self::$recording_request) {
         self::$record_request[] = curl_getinfo($this->resource, CURLINFO_HEADER_OUT);
     }
     if (preg_match_all('/Set-Cookie:[\\s]*(.+)/i', $this->head, $match)) {
         foreach ($match[1] as $cookies) {
             $cookie_name = $cookie_value = $cookie_domain = $cookie_path = $cookie_expires = null;
             $cookie_domain = $cookie_base_domain;
             $cookie_path = '/';
             $secure = false;
             foreach (explode(';', $cookies) as $cookie) {
                 $cookie = trim($cookie);
                 if (strpos($cookie, '=') !== false) {
                     list($k, $v) = explode('=', $cookie, 2);
                     $k = trim($k);
                     $v = trim($v);
                     switch (strtolower($k)) {
                         case 'expires':
                             $cookie_expires = ctype_digit($v) ? (int) $v : strtotime($v);
                             break;
                         case 'domain':
                             $cookie_domain = preg_replace('/^[\\w]+:\\/\\/(.+)$/', '\\1', $v);
                             break;
                         case 'path':
                             $cookie_path = $v;
                             break;
                         default:
                             if (!isset($cookie_name)) {
                                 $cookie_name = $k;
                                 $cookie_value = $v;
                             }
                     }
                 } else {
                     if (strtolower($cookie) == 'secure') {
                         $secure = true;
                     }
                 }
             }
             $cookie_domain = substr(\ebi\Util::path_absolute('http://' . $cookie_domain, $cookie_path), 7);
             if ($cookie_expires !== null && $cookie_expires < time()) {
                 if (isset($this->cookie[$cookie_domain][$cookie_name])) {
                     unset($this->cookie[$cookie_domain][$cookie_name]);
                 }
             } else {
                 $this->cookie[$cookie_domain][$cookie_name] = ['value' => $cookie_value, 'expires' => $cookie_expires, 'secure' => $secure];
             }
         }
     }
     curl_close($this->resource);
     unset($this->resource);
     if ($this->redirect_count++ < $this->redirect_max) {
         switch ($this->status) {
             case 300:
             case 301:
             case 302:
             case 303:
             case 307:
                 if (preg_match('/Location:[\\040](.*)/i', $this->head, $redirect_url)) {
                     return $this->request('GET', trim(\ebi\Util::path_absolute($url, $redirect_url[1])), $download_path);
                 }
         }
     }
     $this->redirect_count = 1;
     return $this;
 }
コード例 #16
0
ファイル: Template.php プロジェクト: tokushima/ebi
 private function rtblock($src, $filename)
 {
     if (strpos($src, 'rt:block') !== false || strpos($src, 'rt:extends') !== false) {
         $base_filename = $filename;
         $blocks = $paths = $blockvars = [];
         try {
             while (true) {
                 $e = \ebi\Xml::extract($this->rtcomment($src), 'rt:extends');
                 $href = $e->in_attr('href');
                 if (substr($href, 0, 1) == '#') {
                     $href = $filename . $href;
                 }
                 $href = \ebi\Util::path_absolute(str_replace("\\", '/', dirname($filename)) . '/', $href);
                 if (empty($href) || !is_file(preg_replace('/^(.+)#.*$/', '\\1', $href))) {
                     throw new \ebi\exception\InvalidTemplateException('href not found ' . $filename);
                 }
                 if ($filename === $href) {
                     throw new \ebi\exception\InvalidTemplateException('Infinite Recursion Error' . $filename);
                 }
                 $readxml = \ebi\Xml::anonymous($this->rtcomment($src));
                 foreach ($readxml->find('rt:block') as $b) {
                     $n = $b->in_attr('name');
                     if (!empty($n) && !array_key_exists($n, $blocks)) {
                         $blocks[$n] = $b->value();
                         $paths[$n] = $filename;
                     }
                 }
                 foreach ($readxml->find('rt:blockvar') as $b) {
                     if (!isset($blockvars[$b->in_attr('name')])) {
                         $blockvars[$b->in_attr('name')] = $b->value();
                     }
                 }
                 $src = $this->replace_xtag($this->read_src($href));
                 $filename = preg_replace('/^(.+)#.*$/', '\\1', $href);
             }
         } catch (\ebi\exception\NotFoundException $e) {
         }
         /**
          * ブロック処理前の処理
          * @param string $src
          * @return $src
          */
         foreach ($this->get_object_plugin_funcs('before_block_template') as $o) {
             $src = static::call_func($o, $src);
         }
         if (empty($blocks)) {
             foreach (\ebi\Xml::anonymous($src)->find('rt:block') as $b) {
                 $src = str_replace($b->plain(), $b->value(), $src);
             }
         } else {
             if (!empty($this->template_super)) {
                 $src = $this->read_src(\ebi\Util::path_absolute(str_replace("\\", '/', dirname($base_filename) . '/'), $this->template_super));
             }
             try {
                 while (true) {
                     $b = \ebi\Xml::extract($src, 'rt:block');
                     $n = $b->in_attr('name');
                     $src = str_replace($b->plain(), array_key_exists($n, $blocks) ? $blocks[$n] : $b->value(), $src);
                 }
             } catch (\ebi\exception\NotFoundException $e) {
             }
         }
         $this->file = $filename;
         foreach ($blockvars as $k => $v) {
             $this->vars($k, $v);
         }
     }
     return $src;
 }
コード例 #17
0
ファイル: path_slash.php プロジェクト: tokushima/ebi
<?php

eq("/abc/", \ebi\Util::path_slash("/abc/", null, null));
eq("/abc/", \ebi\Util::path_slash("abc", true, true));
eq("/abc/", \ebi\Util::path_slash("/abc/", true, true));
eq("abc/", \ebi\Util::path_slash("/abc/", false, true));
eq("/abc", \ebi\Util::path_slash("/abc/", true, false));
eq("abc", \ebi\Util::path_slash("/abc/", false, false));
コード例 #18
0
ファイル: plain_text.php プロジェクト: tokushima/ebi
<?php

$text = \ebi\Util::plain_text('
	aaa
	bbb
');
eq('aaa' . "\n" . 'bbb', $text);
$text = \ebi\Util::plain_text('
hoge
hoge
');
eq('hoge' . "\n" . 'hoge', $text);
$text = \ebi\Util::plain_text('
hoge
hoge
hoge
hoge
');
eq('hoge' . "\n" . 'hoge' . "\n" . 'hoge' . "\n" . 'hoge', $text);
コード例 #19
0
ファイル: get_class_traits.php プロジェクト: tokushima/ebi
<?php

eq(['test\\model\\TraitA', 'test\\model\\TraitB', 'test\\model\\TraitC'], \ebi\Util::get_class_traits(\test\model\AbcTraitAB::class));
eq([], \ebi\Util::get_class_traits(\stdClass::class));
コード例 #20
0
ファイル: dao_export.php プロジェクト: tokushima/ebi
<?php

/**
 * Data export
 * @param string $file
 */
if (empty($file)) {
    $file = getcwd() . '/dao.dump';
}
\ebi\Util::file_write($file, '');
foreach (\ebi\Dt::classes(\ebi\Dao::class) as $class_info) {
    $class_name = \ebi\Util::get_class_name($class_info['class']);
    $cnt = 0;
    foreach (call_user_func([$class_name, 'find']) as $obj) {
        \ebi\Util::file_append($file, json_encode(['model' => $class_name, 'data' => $obj->props()]) . PHP_EOL);
        $cnt++;
    }
    \cmdman\Std::println_info('Export ' . $class_name . ' (' . $cnt . ')');
}
\cmdman\Std::println_success(PHP_EOL . 'Writen: ' . $file);
コード例 #21
0
ファイル: FlowHelper.php プロジェクト: tokushima/ebi
 /**
  * 文字列を丸める
  * @param string $str 対象の文字列
  * @param integer $width 指定の幅
  * @param string $postfix 文字列がまるめられた場合に末尾に接続される文字列
  * @return string
  */
 public function trim_width($str, $width, $postfix = '')
 {
     return \ebi\Util::trim_width($str, $width, $postfix);
 }
コード例 #22
0
ファイル: dao_create_table.php プロジェクト: tokushima/ebi
<?php

/**
 * Create table
 * @param string $model 
 * @param boolean $drop 
 */
$model_list = [];
if (empty($model)) {
    foreach (\ebi\Dt::classes(\ebi\Dao::class) as $class_info) {
        $model_list[] = \ebi\Util::get_class_name($class_info['class']);
    }
} else {
    $model_list[] = \ebi\Util::get_class_name($model);
}
foreach ($model_list as $m) {
    if ($drop === true) {
        call_user_func([$m, 'drop_table']);
        \cmdman\Std::println('dropped ' . $m);
    }
    call_user_func([$m, 'create_table']);
    \cmdman\Std::println('created ' . $m);
}
コード例 #23
0
ファイル: check.php プロジェクト: tokushima/ebi
    } catch (\ebi\exception\ConnectionException $e) {
        $failure['db']++;
        \cmdman\Std::println_danger(' ' . $class_name . ' Failure, ' . $e->getMessage());
    } catch (\Exception $e) {
        $failure['db']++;
        \cmdman\Std::println_warning(' ' . $class_name . ' Failure, ' . $e->getMessage());
    }
}
\cmdman\Std::println();
\cmdman\Std::println_info('Entry:');
foreach (\ebi\Util::ls(getcwd()) as $f) {
    $src = file_get_contents($f->getPathname());
    if (strpos($src, '\\ebi\\Flow::app(') !== false) {
        $map = \ebi\Flow::get_map($f->getPathname());
        $entry = str_replace(getcwd(), '', $f->getPathname());
        foreach ($map['patterns'] as $p) {
            if (array_key_exists('action', $p) && is_string($p['action'])) {
                try {
                    list($c, $m) = explode('::', $p['action']);
                    $mr = new \ReflectionMethod(\ebi\Util::get_class_name($c), $m);
                    \cmdman\Std::println_success($entry . ' ' . $p['name'] . ' OK');
                } catch (\ReflectionException $e) {
                    $failure['entry']++;
                    \cmdman\Std::println_danger($entry . ' ' . $p['name'] . ' Failure');
                }
            }
        }
    }
}
\cmdman\Std::println();
\cmdman\Std::println_danger('Failure: ' . (!empty($failure['db']) ? 'Database(' . $failure['db'] . ') ' : '') . (!empty($failure['entry']) ? 'Entry(' . $failure['entry'] . ') ' : ''));
コード例 #24
0
ファイル: path_absolute.php プロジェクト: tokushima/ebi
eq("http://www.email.address/index.html", \ebi\Util::path_absolute("http://www.email.address/doc/ja/", "../././.././index.html"));
eq("/www/ebi/index.html", \ebi\Util::path_absolute("/www/ebi/", "index.html"));
eq("/www.email.address/doc/ja/index.html", \ebi\Util::path_absolute("/www.email.address/doc/ja/", "index.html"));
eq("/www.email.address/doc/index.html", \ebi\Util::path_absolute("/www.email.address/doc/ja/", "../index.html"));
eq("/www.email.address/doc/ja/action.html/index.html", \ebi\Util::path_absolute('/www.email.address/doc/ja/action.html', 'index.html'));
eq("/www.email.address/index.html", \ebi\Util::path_absolute("/www.email.address/doc/ja/", "../../index.html"));
eq("/www.email.address/index.html", \ebi\Util::path_absolute("/www.email.address/doc/ja/", "../././.././index.html"));
eq("c:/www.email.address/doc/index.html", \ebi\Util::path_absolute("c:/www.email.address/doc/ja/", "../index.html"));
eq("http://www.email.address/index.html", \ebi\Util::path_absolute("http://www.email.address/doc/ja", "/index.html"));
eq("http://www.email.address/doc/ja/index.html", \ebi\Util::path_absolute('http://www.email.address/doc/ja/action.html', 'index.html'));
eq("http://www.email.address/doc/ja/sample.cgi?param=test", \ebi\Util::path_absolute('http://www.email.address/doc/ja/sample.cgi?query=key', '?param=test'));
eq("http://www.email.address/doc/index.html", \ebi\Util::path_absolute('http://www.email.address/doc/ja/action.html', '../../index.html'));
eq("http://www.email.address/?param=test", \ebi\Util::path_absolute('http://www.email.address/doc/ja/sample.cgi?query=key', '../../../?param=test'));
eq("/doc/ja/index.html", \ebi\Util::path_absolute('/', "/doc/ja/index.html"));
eq("/index.html", \ebi\Util::path_absolute('/', "index.html"));
eq("http://www.email.address/login", \ebi\Util::path_absolute("http://www.email.address", "/login"));
eq("http://www.email.address/login", \ebi\Util::path_absolute("http://www.email.address/login", ""));
eq("http://www.email.address/login.cgi", \ebi\Util::path_absolute("http://www.email.address/logout.cgi", "login.cgi"));
eq("http://www.email.address/hoge/login.cgi", \ebi\Util::path_absolute("http://www.email.address/hoge/logout.cgi", "login.cgi"));
eq("http://www.email.address/hoge/login.cgi", \ebi\Util::path_absolute("http://www.email.address/hoge/#abc/aa", "login.cgi"));
eq("http://www.email.address/hoge/abc.html#login", \ebi\Util::path_absolute("http://www.email.address/hoge/abc.html", "#login"));
eq("http://www.email.address/hoge/abc.html#login", \ebi\Util::path_absolute("http://www.email.address/hoge/abc.html#logout", "#login"));
eq("http://www.email.address/hoge/abc.html?abc=aa#login", \ebi\Util::path_absolute("http://www.email.address/hoge/abc.html?abc=aa#logout", "#login"));
eq("javascript::alert('')", \ebi\Util::path_absolute("http://www.email.address/hoge/abc.html", "javascript::alert('')"));
eq("mailto::hoge@email.address", \ebi\Util::path_absolute("http://www.email.address/hoge/abc.html", "mailto::hoge@email.address"));
eq("http://www.email.address/hoge/login.cgi", \ebi\Util::path_absolute("http://www.email.address/hoge/?aa=bb/", "login.cgi"));
eq("http://www.email.address/login", \ebi\Util::path_absolute("http://email.address/hoge/hoge", "http://www.email.address/login"));
eq("http://localhost:8888/spec/css/style.css", \ebi\Util::path_absolute("http://localhost:8888/spec/", "./css/style.css"));
eq('phar://C:/abc/def/ghi/xyz.html', \ebi\Util::path_absolute("phar://C:/abc/def/ghi/", "xyz.html"));
eq('phar://C:/abc/def/xyz.html', \ebi\Util::path_absolute("phar://C:/abc/def/ghi", "../xyz.html"));
コード例 #25
0
ファイル: Man.php プロジェクト: tokushima/ebi
 public static function mail_template_list()
 {
     $path = \ebi\Conf::get(\ebi\Mail::class . '@resource_path', \ebi\Conf::resource_path('mail'));
     $template_list = [];
     try {
         foreach (\ebi\Util::ls($path, true, '/\\.xml$/') as $f) {
             $info = new \ebi\Dt\DocInfo();
             $info->name(str_replace(\ebi\Util::path_slash($path, null, true), '', $f->getPathname()));
             try {
                 $xml = \ebi\Xml::extract(file_get_contents($f->getPathname()), 'mail');
                 $info->document($xml->find_get('subject')->value());
                 $info->set_opt('x_t_code', \ebi\Mail::xtc($info->name()));
                 $template_list[] = $info;
             } catch (\ebi\exception\NotFoundException $e) {
             }
         }
     } catch (\ebi\exception\InvalidArgumentException $e) {
     }
     return $template_list;
 }
コード例 #26
0
ファイル: Flow.php プロジェクト: tokushima/ebi
 /**
  * アプリケーションを実行する
  * @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();
 }
コード例 #27
0
ファイル: fstring.php プロジェクト: tokushima/ebi
<?php

$params = array("A", "B", "C");
eq("aAbBcCde", \ebi\Util::fstring("a{1}b{2}c{3}d{4}e", $params));
eq("aAbBcAde", \ebi\Util::fstring("a{1}b{2}c{1}d{4}e", $params));
eq("aAbBcAde", \ebi\Util::fstring("a{1}b{2}c{1}d{4}e", "A", "B", "C"));
コード例 #28
0
ファイル: install_testman.php プロジェクト: tokushima/ebi
<?php

/**
 * Install of testman
 */
$path = getcwd();
if (!is_file($f = $path . '/test/testman.phar')) {
    \ebi\Util::mkdir($path . '/test');
    file_put_contents($f = $path . '/test/testman.phar', file_get_contents('http://git.io/testman.phar'));
    \cmdman\Std::println_success('Created ' . $f);
}
if (!is_file($f = $path . '/test/testman.settings.php')) {
    file_put_contents($f, <<<'_C'
<?php
\ebi\Conf::set(ebi.Db::class,'autocommit',true);
\testman\Conf::set('urls',\ebi\Dt::get_urls());
_C
);
    \cmdman\Std::println_success('Created ' . $f);
}
if (!is_file($f = $path . '/test/testman.fixture.php')) {
    file_put_contents($f, <<<'_C'
<?php
_C
);
    \cmdman\Std::println_success('Created ' . $f);
}
if (!is_file($f = $path . '/test/__setup__.php')) {
    file_put_contents($f, <<<'_C'
<?php
\ebi\Exceptions::clear();
コード例 #29
0
ファイル: setup.php プロジェクト: tokushima/ebi
\ebi\Conf::set([
]);
__SRC__
);
    \cmdman\Std::println_success('Written: ' . realpath($f));
}
if ($mode != $appmode) {
    \cmdman\Std::println_info('Application mode changed.');
    return;
} else {
    \cmdman\Std::println_info('Application mode is `' . $mode . '`');
}
if (!is_file($f = $path . '/bootstrap.php')) {
    $autoload_file = '';
    if (class_exists('Composer\\Autoload\\ClassLoader')) {
        $r = new \ReflectionClass('Composer\\Autoload\\ClassLoader');
        $composer_dir = dirname($r->getFileName());
        if (is_file($bf = realpath(dirname($composer_dir) . '/autoload.php'))) {
            $autoload_file = str_replace(str_replace("\\", '/', getcwd()) . '/', '', str_replace("\\", '/', $bf));
        }
    } else {
        foreach (\ebi\Util::ls($path, true, '/ebi\\.phar$/') as $p) {
            $autoload_file = str_replace(str_replace("\\", '/', getcwd()) . '/', '', str_replace("\\", '/', $p));
            break;
        }
    }
    if (!empty($autoload_file)) {
        file_put_contents($f, '<?php' . PHP_EOL . 'include_once(\'' . $autoload_file . '\');');
        \cmdman\Std::println_success('Written file ' . $f);
    }
}