/** * @module org.rhaco.store.db.Dbc * @param string $name * @param string $host * @param number $port * @param string $user * @param string $password * @param string $sock * @param boolean $autocommit * @see org\rhaco\store\db\module.Base::connect() */ public function connect($name, $host, $port, $user, $password, $sock, $autocommit) { if (!extension_loaded('pdo_sqlite')) { throw new \RuntimeException('pdo_sqlite not supported'); } $con = $path = null; if (empty($host)) { $host = \org\rhaco\Conf::get('host'); if (empty($host)) { $host = empty($name) ? ':memory:' : getcwd(); } } if ($host != ':memory:') { $host = str_replace('\\', '/', $host); if (substr($host, -1) != '/') { $host = $host . '/'; } $path = \org\rhaco\net\Path::absolute($host, $name); \org\rhaco\io\File::mkdir(dirname($path)); } try { $con = new \PDO(sprintf('sqlite:%s', $host == ':memory:' ? ':memory:' : $host . $name)); } catch (\PDOException $e) { throw new \org\rhaco\store\db\exception\ConnectionException($e->getMessage()); } return $con; }
/** * コンストラクタ * @param string{} $def 接続情報の配列 */ public function __construct(array $def = array()) { foreach (array('type', 'host', 'dbname', 'user', 'password', 'port', 'sock', 'encode', 'timezone') as $k) { if (isset($def[$k])) { $this->{$k} = $def[$k]; } } if (empty($this->type)) { $this->type = 'org.rhaco.store.db.module.Sqlite'; } if (empty($this->encode)) { $this->encode = 'utf8'; } $this->type = str_replace('.', '\\', $this->type); if ($this->type[0] !== '\\') { $this->type = '\\' . $this->type; } if (empty($this->type) || !class_exists($this->type)) { throw new \RuntimeException('could not find module `' . (substr($s = str_replace("\\", '.', $this->type), 0, 1) == '.' ? substr($s, 1) : $s) . '`'); } $r = new \ReflectionClass($this->type); $this->connection_module = $r->newInstanceArgs(array($this->encode, $this->timezone)); if ($this->connection_module instanceof \org\rhaco\store\db\module\Base) { if (self::$autocommit === null) { self::$autocommit = \org\rhaco\Conf::get('autocommit', false); } $this->connection = $this->connection_module->connect($this->dbname, $this->host, $this->port, $this->user, $this->password, $this->sock, self::$autocommit); } if (empty($this->connection)) { throw new \RuntimeException('connection fail ' . $this->dbname); } if (self::$autocommit !== true) { $this->connection->beginTransaction(); } }
/** * セッションを開始する * @param string $name * @return $this */ protected function __new__($name = 'sess') { $this->ses_n = $name; if ('' === session_id()) { $session_name = \org\rhaco\Conf::get('session_name', 'SID'); if (!ctype_alpha($session_name)) { throw new \InvalidArgumentException('session name is is not a alpha value'); } session_cache_limiter(\org\rhaco\Conf::get('session_limiter', 'nocache')); session_cache_expire((int) (\org\rhaco\Conf::get('session_expire', 10800) / 60)); session_name(); if (static::has_module('session_read')) { ini_set('session.save_handler', 'user'); session_set_save_handler(array($this, 'open'), array($this, 'close'), array($this, 'read'), array($this, 'write'), array($this, 'destroy'), array($this, 'gc')); if (isset($this->vars[$session_name])) { session_regenerate_id(true); } } session_start(); register_shutdown_function(function () { if ('' != session_id()) { session_write_close(); } }); } }
/** * @module org.rhaco.flow.parts.RequestFlow * @conf string{} $auth ユーザ:md5(sha1(パスワード)) * @param \org\rhaco\flow\parts\RequestFlow $request * @return boolean */ public function login_condition(\org\rhaco\flow\parts\RequestFlow $req) { if (empty($this->users)) { $this->users = \org\rhaco\Conf::get('auth'); } if ($req->is_post() && isset($this->users[$req->in_vars('user_name')]) && $this->users[$req->in_vars('user_name')] == md5(sha1($req->in_vars('password')))) { return true; } return false; }
private static function path($dir = null) { if ($dir === null) { $dir = \org\rhaco\Conf::get('path', \org\rhaco\io\File::work_path('templates')); } if (substr(str_replace("\\", '/', $dir), -1) == '/') { $dir = subustr($dir, 0, -1); } \org\rhaco\io\File::mkdir($dir, 0777); return $dir; }
protected function send($level, \org\rhaco\Log $log) { if (empty($this->template_base)) { $this->template_base = \org\rhaco\Conf::get('template_base', \org\rhaco\io\File::resource_path('log_mail')); } $template = \org\rhaco\net\Path::absolute($this->template_base, $level . '_log.xml'); if (is_file($template)) { $mail = new \org\rhaco\net\mail\Mail(); $mail->send_template($template, array('log' => $log, 'env' => new \org\rhaco\lang\Env())); } }
protected function __new__($login = null, $password = null) { list($this->login, $this->password) = \org\rhaco\Conf::get('account', null, array('login', 'password')); if (isset($login)) { $this->login = $login; $this->password = $password; } $this->resource = fsockopen('tls://smtp.gmail.com', 465, $errno, $errstr, 30); if ($this->resource === false) { throw new \org\rhaco\net\mail\module\SendGmail\Exception('connect fail'); } }
protected function __new__() { $d = debug_backtrace(false); $d = array_pop($d); $dir = dirname($d['file']); $entry = substr(basename($d['file']), 0, -4); $session_group = \org\rhaco\Conf::get('session_group'); $group = isset($session_group[$entry]) ? $session_group[$entry] : ''; $sess_name = $group == '*' ? $dir : ($group == '' ? $dir . '/' . $entry : ($group[0] == '@' ? $group : $dir . '#' . $group)); $this->login_id = $sess_name . '_LOGIN_'; $this->req = new \org\rhaco\Request(); $this->sess = new \org\rhaco\net\Session(md5($sess_name)); }
public function __construct($hostname = null, $username = null, $password = null, $port = 25, $timeout = 5) { if ($hostname === null) { list($hostname, $port, $timeout, $username, $password) = \org\rhaco\Conf::get('server', null, array('host', 'port', 'timeout', 'username', 'password')); if ($port === null) { $port = 25; } if ($timeout === null) { $timeout = 5; } } parent::__construct($hostname, $port, $timeout); $this->username = $username; $this->password = $password; }
<?php \org\rhaco\Conf::set(array('org.rhaco.store.db.Dao' => array('connection' => array('org.rhaco.flow.module.SessionDao' => array('type' => 'org.rhaco.store.db.module.Mysql', 'dbname' => 'rhaco3test'), 'org.rhaco.net.mail.module.SmtpBlackholeDao' => array('type' => 'org.rhaco.store.db.module.Mysql', 'dbname' => 'rhaco3test'), 'org.rhaco.store.queue.module.Dao.QueueDao' => array('type' => 'org.rhaco.store.db.module.Mysql', 'dbname' => 'rhaco3test'), 'org.rhaco.store.db.Dao.CrossChild' => array('type' => 'org.rhaco.store.db.module.Mysql', 'dbname' => 'rhaco3test'), 'test.model.CrossChild' => array('type' => 'org.rhaco.store.db.module.Mysql', 'dbname' => 'rhaco3test'), 'test.model' => array('type' => 'org.rhaco.store.db.module.Mysql', 'dbname' => 'rhaco3test'), '*' => array('type' => 'org.rhaco.store.db.module.Mysql', 'dbname' => 'rhaco3test'), 'test.model.ReplicationSlave' => array('type' => 'org.rhaco.store.db.module.MysqlUnbuffered', 'dbname' => 'rhaco3test'), 'test.model.Unbuffered' => array('type' => 'org.rhaco.store.db.module.MysqlUnbuffered', 'dbname' => 'rhaco3test'))))); \org\rhaco\Conf::set('org.rhaco.Flow', 'app_url', 'http://localhost/rhaco3'); \org\rhaco\Conf::set('org.rhaco.Flow', 'rewrite_entry', true); include 'local.php'; //\org\rhaco\Conf::set('org.rhaco.Dt','media_url','http://localhost/work/media2');
/** * DBへ保存する */ public final function save() { $q = new Q(); $new = false; foreach ($this->primary_columns() as $column) { $value = $this->{$column->name()}(); if ($this->prop_anon($column->name(), 'type') === 'serial' && empty($value)) { $new = true; break; } $q->add(Q::eq($column->name(), $value)); } $self = get_class($this); if (!$new && $self::find_count($q) === 0) { $new = true; } foreach ($this->self_columns() as $column) { if ($this->prop_anon($column->name(), 'auto_now') === true) { switch ($this->prop_anon($column->name(), 'type')) { case 'timestamp': case 'date': $this->{$column->name()}(time()); break; case 'intdate': $this->{$column->name()}(date('Ymd')); break; } } else { if ($new && ($this->{$column->name()}() === null || $this->{$column->name()}() === '')) { if ($this->prop_anon($column->name(), 'type') == 'string' && $this->prop_anon($column->name(), 'auto_code_add') === true) { $this->set_unique_code($column->name()); } else { if ($this->prop_anon($column->name(), 'auto_now_add') === true) { switch ($this->prop_anon($column->name(), 'type')) { case 'timestamp': case 'date': $this->{$column->name()}(time()); break; case 'intdate': $this->{$column->name()}(date('Ymd')); break; } } else { if ($this->prop_anon($column->name(), 'auto_future_add') === true) { $future = \org\rhaco\Conf::get('future_date', '2038/01/01 00:00:00'); $time = strtotime($future); switch ($this->prop_anon($column->name(), 'type')) { case 'timestamp': case 'date': $this->{$column->name()}($time); break; case 'intdate': $this->{$column->name()}(date('Ymd', $time)); break; } } } } } } } if ($new) { if (!self::$_co_anon_[$self][2]) { throw new \org\rhaco\store\db\exception\DaoBadMethodCallException('create save is not permitted'); } $this->__before_save__(); $this->__before_create__(); $this->save_verify_primary_unique(); $this->__create_verify__(); $this->__save_verify__(); $this->validate(); /** * createを実行するSQL文の生成 * @param self $this * @return org.rhaco.store.db.Daq */ $daq = $self::module('create_sql', $this); if ($this->update_query($daq) == 0) { throw new \RuntimeException('create failed'); } if ($daq->is_id()) { /** * AUTOINCREMENTの値を取得するSQL文の生成 * @param self $this * @return integer */ $result = $this->func_query(static::module('last_insert_id_sql', $this)); if (empty($result)) { throw new \RuntimeException('create failed'); } $this->{$daq->id()}($result[0]); } $this->__after_create__(); $this->__after_save__(); } else { if (!self::$_co_anon_[$self][3]) { throw new \org\rhaco\store\db\exception\DaoBadMethodCallException('update save is not permitted'); } $this->__before_save__(); $this->__before_update__(); $this->__update_verify__(); $this->__save_verify__(); $this->validate(); $args = func_get_args(); $query = new Q(); if (!empty($args)) { call_user_func_array(array($query, 'add'), $args); } /** * updateを実行するSQL文の生成 * @param self $this * @return \org\rhaco\store\db\Daq */ $daq = $self::module('update_sql', $this, $query); $affected_rows = $this->update_query($daq); if ($affected_rows === 0 && !empty($args)) { throw new \org\rhaco\store\db\exception\NoRowsAffectedException(); } $this->__after_update__(); $this->__after_save__(); } return $this; }
/** * ライブラリ一覧 * composerの場合はcomposer.jsonで定義しているPSR-0のもののみ * @return array */ public static function classes($parent_class = null) { $result = array(); $include_path = array(); if (is_dir(getcwd() . '/lib')) { $include_path[] = realpath(getcwd() . '/lib'); } if (class_exists('Composer\\Autoload\\ClassLoader')) { $r = new \ReflectionClass('Composer\\Autoload\\ClassLoader'); $vendor_dir = dirname(dirname($r->getFileName())); if (is_file($loader_php = $vendor_dir . '/autoload.php')) { $loader = (include $loader_php); // vendor以外の定義されているパスを探す foreach ($loader->getPrefixes() as $ns) { foreach ($ns as $path) { if (strpos($path, $vendor_dir) === false) { $include_path[] = $path; } } } } } foreach ($include_path as $libdir) { if ($libdir !== '.') { foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($libdir, \FilesystemIterator::CURRENT_AS_FILEINFO | \FilesystemIterator::SKIP_DOTS | \FilesystemIterator::UNIX_PATHS), \RecursiveIteratorIterator::SELF_FIRST) as $e) { if (strpos($e->getPathname(), '/.') === false && strpos($e->getPathname(), '/_') === false && ctype_upper(substr($e->getFilename(), 0, 1)) && substr($e->getFilename(), -4) == '.php') { try { include_once $e->getPathname(); } catch (\Exeption $ex) { } } } } } $set = function (&$result, $r, $include_path, $parent_class) { if (!$r->isInterface() && !$r->isAbstract() && (empty($parent_class) || is_subclass_of($r->getName(), $parent_class))) { $bool = empty($include_path); if (!$bool) { foreach ($include_path as $libdir) { if (strpos($r->getFileName(), $libdir) === 0) { $bool = true; break; } } } if ($bool) { $n = str_replace('\\', '/', $r->getName()); $result[str_replace('/', '.', $n)] = array('filename' => $r->getFileName(), 'class' => '\\' . $r->getName()); } } }; foreach (get_declared_classes() as $class) { $set($result, new \ReflectionClass($class), $include_path, $parent_class); } $add = \org\rhaco\Conf::get('use_vendor', array()); if (is_string($add)) { $add = array($add); } foreach ($add as $class) { $class = str_replace('.', '\\', $class); if (substr($class, 0, 1) != '\\') { $class = '\\' . $class; } $set($result, new \ReflectionClass($class), array(), $parent_class); } ksort($result); return $result; }
/** * 格納されたログを出力する */ public static final function flush() { if (!empty(self::$logs)) { $stdout = \org\rhaco\Conf::get('stdout', false); foreach (self::$logs as $log) { if (self::cur_level() >= $log->level()) { switch ($log->fm_level()) { /** * debugログの場合の処理 * @param self $log * @param string $id */ case 'debug': self::module('debug', $log, self::$id); break; /** * infoログの場合の処理 * @param self $log * @param string $id */ /** * infoログの場合の処理 * @param self $log * @param string $id */ case 'info': self::module('info', $log, self::$id); break; /** * warnログの場合の処理 * @param self $log * @param string $id */ /** * warnログの場合の処理 * @param self $log * @param string $id */ case 'warn': self::module('warn', $log, self::$id); break; /** * errorログの場合の処理 * @param self $log * @param string $id */ /** * errorログの場合の処理 * @param self $log * @param string $id */ case 'error': self::module('error', $log, self::$id); break; default: /** * traceログの場合の処理 * @param self $log * @param string $id */ self::module('trace', $log, self::$id); } if (!empty(self::$logfile) && is_file(self::$logfile)) { file_put_contents(self::$logfile, (\org\rhaco\Conf::get('nl2str') !== null ? str_replace(array("\r\n", "\r", "\n"), \org\rhaco\Conf::get('nl2str'), (string) $log) : (string) $log) . PHP_EOL, FILE_APPEND); } if (self::$disp === true && $stdout) { print (string) $log . PHP_EOL; } } } /** * フラッシュ時の処理 * @param self[] $logs * @param string $id * @param boolean $stdout 標準出力に出力するか */ self::module('flush', self::$logs, self::$id, $stdout); self::$logs = array(); } }
private function base() { $base = Path::slash(\org\rhaco\Conf::get('document_root'), null, true); return $base . Path::slash($this->map_arg('path'), null, true); }
<?php \testman\Conf::set('urls', \org\rhaco\Dt::get_urls()); \testman\Conf::set('ssl-verify', false); \testman\Conf::set('coverage-dir', dirname(__DIR__) . '/src'); \org\rhaco\Conf::set('org.rhaco.store.db.Dbc', 'autocommit', true);
/** * 出力する * @param array $map */ public function output($map_array) { $args = func_get_args(); $pathinfo = preg_replace("/(.*?)\\?.*/", "\\1", isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : null); /** * ハンドリングマップ配列を取得する * @return array */ $map = $this->has_object_module('flow_map_loader') ? call_user_func_array(array($this, 'object_module'), array_merge(array('flow_map_loader'), $args)) : $map_array; if (is_string($map) && preg_match('/^[\\w\\.]+$/', $map)) { $map = array('patterns' => array('' => array('action' => $map))); } $apps = $urls = array(); $idx = $pkg_id = 0; $put_block = null; if (isset($map['patterns']) && is_array($map['patterns'])) { $exp_patterns = array(); foreach ($map['patterns'] as $k => $v) { if (is_int($k) || isset($map['patterns'][$k]['patterns'])) { if (!empty($map['patterns'][$k])) { $maps = $map['patterns'][$k]; unset($map['patterns'][$k]); if (!isset($maps['patterns']) || !is_array($maps['patterns'])) { throw new \InvalidArgumentException('patterns not found'); } $maps_url = is_int($k) ? null : $k . '/'; $maps_nam = isset($maps['name']) ? $maps['name'] : null; $maps_act = isset($maps['action']) ? $maps['action'] : null; $maps_med = isset($maps['media_path']) ? $maps['media_path'] : null; $maps_tem = isset($maps['template_path']) ? $maps['template_path'] : null; $maps_err = isset($maps['error_template']) ? $maps['error_template'] : null; $maps_sup = isset($maps['template_super']) ? $maps['template_super'] : null; $maps_mod = isset($maps['modules']) && !empty($maps['modules']) ? is_array($maps['modules']) ? $maps['modules'] : array($maps['modules']) : array(); $maps_arg = isset($maps['args']) && !empty($maps['args']) ? is_array($maps['args']) ? $maps['args'] : array($maps['args']) : array(); foreach ($maps['patterns'] as $u => $m) { if (!empty($maps_act) && isset($m['action'])) { $m['action'] = $maps_act . '::' . $m['action']; } if (!empty($maps_nam)) { $m['name'] = $maps_nam . (isset($m['name']) && !empty($m['name']) ? '/' . $m['name'] : ''); } if (!empty($maps_med)) { $m['media_path'] = $maps_med; } if (!empty($maps_tem)) { $m['template_path'] = $maps_tem; } if (!empty($maps_err)) { $m['error_template'] = $maps_err; } if (!empty($maps_sup)) { $m['template_super'] = $maps_sup; } if (!empty($maps_mod)) { $m['modules'] = array_merge($maps_mod, isset($m['modules']) ? is_array($m['modules']) ? $m['modules'] : array($m['modules']) : array()); } if (!empty($maps_arg)) { $m['args'] = array_merge($maps_arg, isset($m['args']) ? is_array($m['args']) ? $m['args'] : array($m['args']) : array()); } $exp_patterns[$maps_url . $u] = $m; } } } else { $exp_patterns[$k] = $v; } } $map['patterns'] = $exp_patterns; foreach ($map['patterns'] as $k => $v) { if (preg_match('/^(.*)\\$(.+)$/', $k, $m)) { list($k, $v['name']) = array(trim($m[1]), trim($m[2])); } if (substr($k, 0, 1) == '/') { $k = substr($k, 1); } if (substr($k, -1) == '/') { $k = substr($k, 0, -1); } if (is_string($v)) { $v = array('class' => $v); } if (!isset($v['name'])) { $v['name'] = $k; } if (isset($v['action'])) { if (strpos($v['action'], '::') !== false) { list($v['class'], $v['method']) = explode('::', $v['action']); } else { $v['class'] = $v['action']; } unset($v['action']); } if (isset($v['class']) && !isset($v['method'])) { try { $pkg_id++; $n = isset($v['name']) ? $v['name'] : $v['class']; $r = new \ReflectionClass(str_replace('.', "\\", $v['class'])); $suffix = isset($v['suffix']) ? $v['suffix'] : ''; $automaps = $methodmaps = array(); foreach ($r->getMethods() as $m) { if ($m->isPublic() && !$m->isStatic() && substr($m->getName(), 0, 1) != '_') { $automap = (bool) preg_match('/@automap[\\s]*/', $m->getDocComment()); if (empty($automaps) || $automap) { $url = $k . ($m->getName() == 'index' ? '' : ($k == '' ? '' : '/') . $m->getName()) . str_repeat('/(.+)', $m->getNumberOfRequiredParameters()); $auto_anon = array(); if ($automap) { if (preg_match('/@automap\\s.*@(\\[.*\\])/', $m->getDocComment(), $a)) { if (preg_match_all('/([\\"\']).+?\\1/', $a[1], $dm)) { foreach ($dm[0] as $dv) { $a[1] = str_replace($dv, str_replace(array('[', ']'), array('#{#', '#}#'), $dv), $a[1]); } } $auto_anon = @eval('return ' . str_replace(array('[', ']', '#{#', '#}#'), array('array(', ')', '[', ']'), $a[1]) . ';'); if (!is_array($auto_anon)) { throw new \InvalidArgumentException($r->getName() . '::' . $m->getName() . ' automap annotation error'); } } } for ($i = 0; $i <= $m->getNumberOfParameters() - $m->getNumberOfRequiredParameters(); $i++) { $p = is_dir(substr($r->getFilename(), 0, -4)) ? substr($r->getFilename(), 0, -4) : dirname($r->getFilename()); $mapvar = array_merge($v, array('name' => $n . '/' . $m->getName(), 'class' => $v['class'], 'method' => $m->getName(), 'num' => $i, '@' => $p, 'pkg_id' => $pkg_id)); if ($automap) { if (!empty($auto_anon)) { $mapvar = array_merge($mapvar, $auto_anon); if (empty($suffix) && isset($mapvar['suffix'])) { $suffix = $mapvar['suffix']; } } $automaps[$url . $suffix] = $mapvar; } else { $methodmaps[$url . $suffix] = $mapvar; } $url .= '/(.+)'; } } } } $apps = array_merge($apps, empty($automaps) ? $methodmaps : $automaps); unset($automaps, $methodmaps); } catch (\ReflectionException $e) { throw new \InvalidArgumentException($v['class'] . ' not found'); } } else { $apps[(string) $k] = $v; } } list($url, $surl) = array($this->branch_url, str_replace('http://', 'https://', $this->branch_url)); $map_secure = isset($map['secure']) && $map['secure'] === true; $conf_secure = \org\rhaco\Conf::get('secure', true) === true; foreach ($apps as $u => $m) { $m['secure'] = $conf_secure && (isset($m['secure']) && $m['secure'] === true || !isset($m['secure']) && $map_secure); $cnt = 0; $fu = \org\rhaco\net\Path::absolute($m['secure'] ? $surl : $url, empty($u) ? '' : substr(preg_replace_callback("/([^\\\\])(\\(.*?[^\\\\]\\))/", function ($n) { return $n[1] . '%s'; }, ' ' . $u, -1, $cnt), 1)); foreach (array('template_path', 'media_path') as $k) { if (isset($map[$k]) || isset($m[$k])) { $m[$k] = \org\rhaco\net\Path::slash(isset($map[$k]) ? $map[$k] : '', null, true) . (isset($m[$k]) ? $m[$k] : ''); } } $apps[$u] = array_merge($m, array('url' => $u, 'format' => $fu, 'num' => $cnt, 'pattern' => str_replace(array("\\\\", "\\.", '_ESC_'), array('_ESC_', '.', "\\"), $fu))); } if (self::$get_maps) { self::$output_maps[basename($this->entry_file())] = $apps; self::$get_maps = false; return; } foreach (array_keys($apps) as $k => $u) { $urls[$u] = strlen(preg_replace("/[\\W]/", '', $u)); } arsort($urls); krsort($urls); if (preg_match("/^\\/" . str_replace("/", "\\/", $this->package_media_url) . "\\/(\\d+)\\/(.+)\$/", $pathinfo, $m) && sizeof($urls) >= $m[1]) { for (reset($urls), $i = 0; $i < $m[1]; $i++) { next($urls); } $v = $apps[key($urls)]; if (isset($v['@'])) { \org\rhaco\net\http\File::attach($v['@'] . '/resources/media/' . $m[2]); } \org\rhaco\net\http\Header::send_status(404); exit; } foreach ($urls as $k => $null) { if (preg_match("/^" . (empty($k) ? '' : "\\/") . str_replace(array("\\/", '/', '@#S'), array('@#S', "\\/", "\\/"), $k) . '[\\/]{0,1}$/', $pathinfo, $p)) { if (isset($apps[$k]['mode']) && !empty($apps[$k]['mode'])) { $mode = \org\rhaco\Conf::appmode(); $mode_alias = \org\rhaco\Conf::get('mode'); $bool = false; foreach (explode(',', $apps[$k]['mode']) as $m) { foreach (substr(trim($m), 0, 1) == '@' && isset($mode_alias[substr(trim($m), 1)]) ? explode(',', $mode_alias[substr(trim($m), 1)]) : array($m) as $me) { if ($mode == trim($me)) { $bool = true; break; } } } if (!$bool) { break; } } array_shift($p); $obj = $modules = array(); $current_url = \org\rhaco\Request::current_url(); if (isset($apps[$k]['secure']) && $apps[$k]['secure'] === true && \org\rhaco\Conf::get('secure', true) !== false) { $this->template->secure(true); if (substr($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", $current_url)); exit; } } if (isset($apps[$k]['redirect'])) { header('Location: ' . \org\rhaco\net\Path::absolute($this->branch_url, $apps[$k]['redirect'])); exit; } if (isset($map['modules'])) { foreach (is_array($map['modules']) ? $map['modules'] : array($map['modules']) as $m) { $modules[] = $this->str_reflection($m); } } if (isset($apps[$k]['modules'])) { foreach (is_array($apps[$k]['modules']) ? $apps[$k]['modules'] : array($apps[$k]['modules']) as $m) { $modules[] = $this->str_reflection($m); } } try { foreach ($modules as $m) { $this->set_object_module($m); } if (isset($apps[$k]['class'])) { if (!class_exists(str_replace('.', "\\", $apps[$k]['class']))) { throw new \InvalidArgumentException($apps[$k]['class'] . ' not found'); } $obj = $this->str_reflection($apps[$k]['class']); $func_exception = null; if ($obj instanceof \org\rhaco\Object) { foreach ($modules as $m) { $obj->set_object_module($m); } } if ($obj instanceof \org\rhaco\flow\FlowInterface) { $obj->set_select_map_name($apps[$k]['name']); $obj->set_maps($apps); $obj->set_args(isset($apps[$k]['args']) && is_array($apps[$k]['args']) ? $apps[$k]['args'] : array()); $ext_modules = $obj->get_template_modules(); if (!empty($ext_modules)) { if (!is_array($ext_modules)) { $ext_modules = array($ext_modules); } foreach ($ext_modules as $o) { $this->template->set_object_module($o); } } $obj->before(); $put_block = $obj->get_block(); } try { $result = call_user_func_array(array($obj, $apps[$k]['method']), $p); if ($result !== null) { $obj = $result; } } catch (\Exception $e) { $func_exception = $e; } if ($obj instanceof \org\rhaco\flow\FlowInterface) { $obj->after(); $put_block = $obj->get_block(); } if ($func_exception instanceof \Exception) { throw $func_exception; } } if (isset($apps[$k]['post_after']) && isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] == 'POST' && !\org\rhaco\Exceptions::has()) { $this->after_redirect($apps[$k]['post_after'], $apps[$k], $apps, $obj); } if (isset($apps[$k]['after']) && !\org\rhaco\Exceptions::has()) { $this->after_redirect($apps[$k]['after'], $apps[$k], $apps, $obj); } if (isset($apps[$k]['template'])) { $this->print_template($this->template_path, $apps[$k]['template'], $this->media_url, $put_block, $obj, $apps, $k); } else { if (isset($apps[$k]['@']) && is_file($t = $apps[$k]['@'] . '/resources/templates/' . $apps[$k]['method'] . '.html')) { $this->print_template(dirname($t) . '/', basename($t), $this->branch_url . $this->package_media_url . '/' . $idx, $put_block, $obj, $apps, $k, false); } else { if ($this->has_object_module('flow_output')) { /** * 結果出力 * @param mixed $obj */ $this->object_module('flow_output', $obj); } else { \org\rhaco\Exceptions::throw_over(); $xml = new \org\rhaco\Xml('result', $obj); $xml->output(); } } } exit; } catch (\Exception $e) { if (($level = \org\rhaco\Conf::get('exception_log_level')) !== null && in_array($level, array('error', 'warn', 'info', 'debug'))) { $es = $e instanceof \org\rhaco\Exceptions ? \org\rhaco\Exceptions::gets() : array($e); $ignore = \org\rhaco\Conf::get('exception_log_ignore'); foreach ($es as $ev) { $in = true; if (!empty($ignore)) { foreach (is_array($ignore) ? $ignore : array($ignore) as $p) { if (($in = !preg_match('/' . str_replace('/', '\\/', $p) . '/', (string) $ev)) === false) { break; } } } if ($in) { \org\rhaco\Log::$level($ev); } } } if ($this->has_object_module('flow_handle_exception')) { $this->object_module('flow_handle_exception', $e); } if (!$e instanceof \org\rhaco\Exceptions) { \org\rhaco\Exceptions::add($e); } if (isset($apps[$k]['error_status'])) { \org\rhaco\net\http\Header::send_status($apps[$k]['error_status']); } else { if (isset($map['error_status'])) { \org\rhaco\net\http\Header::send_status($map['error_status']); } } if ($this->has_object_module('flow_exception_output')) { /** * 例外発生時の出力 * @param mixed $obj actionで返却された値 * @param Exception $e 発生した例外 */ $this->object_module('flow_exception_output', $obj, $e); exit; } else { if (isset($apps[$k]['error_redirect'])) { $this->redirect($apps, $apps[$k]['error_redirect']); } else { if (isset($map['error_redirect'])) { $this->redirect($apps, $map['error_redirect']); } else { if (isset($apps[$k]['error_template'])) { $this->print_template($this->template_path, $apps[$k]['error_template'], $this->media_url, $put_block, $obj, $apps, $k); } else { if (isset($map['error_template'])) { $this->print_template($this->template_path, $map['error_template'], $this->media_url, $put_block, $obj, $apps, $k); } else { if (isset($apps[$k]['@']) && is_file($t = $apps[$k]['@'] . '/resources/templates/error.html')) { $this->print_template(dirname($t) . '/', basename($t), $this->branch_url . $this->package_media_url . '/' . $idx, $put_block, $obj, $apps, $k, false); } else { if (isset($apps[$k]['template']) || isset($apps[$k]['@']) && is_file($apps[$k]['@'] . '/resources/templates/' . $apps[$k]['method'] . '.html')) { if (!isset($map['error_status'])) { \org\rhaco\net\http\Header::send_status(500); } exit; } } } } } } } $xml = new \org\rhaco\Xml('error'); foreach (\org\rhaco\Exceptions::gets() as $g => $e) { $class_name = get_class($e); $message = new \org\rhaco\Xml('message', $e->getMessage()); $message->add('group', $g); $message->add('type', basename(str_replace("\\", '/', $class_name))); $xml->add($message); } $xml->output(); } } $idx++; } } if (isset($map['nomatch_redirect'])) { $this->redirect($apps, $map['nomatch_redirect']); } if (($level = \org\rhaco\Conf::get('notfound_log_level')) !== null && in_array($level, array('error', 'warn', 'info', 'debug'))) { \org\rhaco\Log::$level(\org\rhaco\Request::current_url() . ' (`' . $pathinfo . '`) bad request'); } \org\rhaco\net\http\Header::send_status(404); exit; }
<?php \org\rhaco\Conf::set(array('org.rhaco.store.db.Dao' => array('connection' => array('org.rhaco.flow.module.SessionDao' => array('dbname' => 'local_session.db'), 'org.rhaco.net.mail.module.SmtpBlackholeDao' => array('dbname' => 'local1.db'), 'org.rhaco.store.queue.module.Dao.QueueDao' => array('dbname' => 'local2.db'), 'org.rhaco.store.db.Dao.CrossChild' => array('dbname' => 'local3.db'), 'test.model.CrossChild' => array('dbname' => 'local4.db'), 'test.model' => array('dbname' => 'local5.db'), '*' => array('dbname' => 'local6.db'))), 'org.rhaco.store.db.module.Sqlite' => array('host' => dirname(__DIR__) . '/work/db'), 'org.rhaco.Flow' => array('exception_log_level' => 'warn', 'exception_log_ignore' => array('Unauthorized.+RequestFlow')), 'org.rhaco.Template' => array('display_exception' => true), 'org.rhaco.flow.module.SimpleAuth' => array('auth' => array('user_name' => md5(sha1('password')))), 'org.rhaco.Log' => array('level' => 'warn', 'file' => dirname(__DIR__) . '/work/rhaco3.log', 'stdout' => true), 'org.rhaco.io.File' => array('work_dir' => dirname(__DIR__) . '/work/'), 'org.rhaco.Dt' => array('use_vendor' => array('org.rhaco.store.queue.module.Dao.QueueDao', 'org.rhaco.net.mail.module.SmtpBlackholeDao', 'org.rhaco.flow.module.SessionDao')))); \org\rhaco\Object::set_module(array('org.rhaco.net.Session' => array('org.rhaco.flow.module.SessionDao'), 'org.rhaco.store.queue.Queue' => array('org.rhaco.store.queue.module.Dao'), 'org.rhaco.net.mail.Mail' => array('org.rhaco.net.mail.module.SmtpBlackholeDao')));
/** * ファイルの実際のパスを返す * @conf string $base_path ストレージのベースパス * @param string $path ベースパスからの相対パス * @return string */ public static function get_path($path) { $base_path = \org\rhaco\Conf::get('base_path'); if (empty($base_path)) { throw new \org\rhaco\io\Storage\StorageException('ベースパスが指定されていません'); } return \org\rhaco\net\Path::absolute(\org\rhaco\net\Path::slash($base_path, null, true), $path); }
/** * リソースディレクトリを返す * @return string */ public static function resource_path($path = null) { $dir = str_replace("\\", '/', \org\rhaco\Conf::get('resource_dir', getcwd() . '/resources/')); if (substr($dir, -1) != '/') { $dir = $dir . '/'; } return $dir . $path; }
/** * テンプレートから内容を取得しセットする * * テンプレートサンプル * <mail> * <from address="*****@*****.**" name="tokushima" /> * <subject>メールのタイトル</subject> * <body> * メールの本文 * </body> * </mail> * * @param string $template_path テンプレートファイルパス * @param mixed{} $vars テンプレートへ渡す変数 * @return $this */ public function set_template($template_path, $vars = array()) { $resource_path = empty($this->resource_path) ? \org\rhaco\Conf::get('resource_path', \org\rhaco\io\File::resource_path('mail')) : $this->resource_path; $template_path = \org\rhaco\net\Path::absolute($resource_path, $template_path); if (!is_file($template_path)) { throw new \InvalidArgumentException($template_path . ' not found'); } if (\org\rhaco\Xml::set($xml, file_get_contents($template_path), 'mail')) { $from = $xml->f('from'); if ($from !== null) { $this->from($from->in_attr('address'), $from->in_attr('name')); } foreach ($xml->in('to') as $to) { $this->to($to->in_attr('address'), $to->in_attr('name')); } $return_path = $xml->f('return_path'); if ($return_path !== null) { $this->return_path($return_path->in_attr('address')); } $notification = $xml->f('notification'); if ($notification !== null) { $this->notification($notification->in_attr('address')); } $reply_to = $xml->f('reply_to'); if ($reply_to !== null) { $this->reply_to($reply_to->in_attr('address')); } $errors_to = $xml->f('errors_to'); if ($errors_to !== null) { $this->errors_to($errors_to->in_attr('address')); } $subject = trim(str_replace(array("\r\n", "\r", "\n"), '', $xml->f('subject.value()'))); $body = $xml->f('body.value()'); $template = new \org\rhaco\Template(); $template->cp($vars); $template->vars('t', new \org\rhaco\flow\module\Helper()); $this->subject($template->get($subject)); $this->message(\org\rhaco\lang\Text::plain("\n" . $template->get($body) . "\n")); $html = $xml->f('html'); if ($html !== null) { $html_path = \org\rhaco\net\Path::absolute($resource_path, $html->in_attr('src')); foreach ($html->in('media') as $media) { $file = \org\rhaco\net\Path::absolute($resource_path, $media->in_attr('src')); if (!is_file($file)) { throw new \InvalidArgumentException($media->in_attr('src') . ' invalid media'); } $this->media($media->in_attr('src'), file_get_contents($file)); } $template = new \org\rhaco\Template(); $template->cp($vars); $template->vars('t', new \org\rhaco\flow\module\Helper()); $this->html($template->read($html_path)); } foreach ($xml->in('attach') as $attach) { $file = \org\rhaco\net\Path::absolute($resource_path, $attach->in_attr('src')); if (!is_file($file)) { throw new \InvalidArgumentException($attach->in_attr('src') . ' invalid media'); } $this->attach($attach->in_attr('name', $attach->in_attr('src')), file_get_contents($file)); } return $this; } throw new \InvalidArgumentException($template_path . ' invalid data'); }
private static function cmd() { return \org\rhaco\Conf::get('cmd', '/usr/bin/exiftool'); }
<?php if (\org\rhaco\Conf::appmode() == 'mamp') { $pre = <<<PRE <html> <body> \t<a href="http://rhaco.org"></a> \t<a href="http://localhost/rhaco3/test_index/"></a> \t<a href="https://localhost/rhaco3/test_index/secure"></a> \t<a href="https://localhost/rhaco3/test_login/secure"></a> \t<img src="http://localhost/images/abc.jpg" /> \t<img src="http://localhost/rhaco3/resources/media/images/def.jpg" /> \t<img src="http://localhost/rhaco3/resources/media/images/def.jpg" /> </body> </html> PRE; } else { $pre = <<<PRE <html> <body> \t<a href="http://rhaco.org"></a> \t<a href="http://localhost:8000/test_index.php/"></a> \t<a href="https://localhost:8000/test_index.php/secure"></a> \t<a href="https://localhost:8000/test_login.php/secure"></a> \t<img src="http://localhost/images/abc.jpg" /> \t<img src="http://localhost:8000/resources/media/images/def.jpg" /> \t<img src="http://localhost:8000/resources/media/images/def.jpg" /> </body> </html> PRE; }
private function exec($_src_) { /** * 実行前処理 * @param org.rhaco.lang.Str $obj */ $this->object_module('before_exec_template', \org\rhaco\lang\Str::ref($_obj_, $_src_)); foreach ($this->default_vars() as $k => $v) { $this->vars($k, $v); } ob_start(); if (is_array($this->vars) && !empty($this->vars)) { extract($this->vars); } eval('?><?php $_display_exception_=' . (\org\rhaco\Conf::get('display_exception') === true ? 'true' : 'false') . '; ?>' . (string) $_obj_); $_eval_src_ = ob_get_clean(); if (strpos($_eval_src_, 'Parse error: ') !== false) { if (preg_match("/Parse error\\:(.+?) in .+eval\\(\\)\\'d code on line (\\d+)/", $_eval_src_, $match)) { list($msg, $line) = array(trim($match[1]), (int) $match[2]); $lines = explode("\n", $_src_); $plrp = substr_count(implode("\n", array_slice($lines, 0, $line)), "<?php 'PLRP'; ?>\n"); \org\rhaco\Log::error($msg . ' on line ' . ($line - $plrp) . ' [compile]: ' . trim($lines[$line - 1])); $lines = explode("\n", $this->selected_src); \org\rhaco\Log::error($msg . ' on line ' . ($line - $plrp) . ' [plain]: ' . trim($lines[$line - 1 - $plrp])); if (\org\rhaco\Conf::get('display_exception') === true) { $_eval_src_ = $msg . ' on line ' . ($line - $plrp) . ': ' . trim($lines[$line - 1 - $plrp]); } } } $_src_ = $this->selected_src = null; /** * 実行後処理 * @param org.rhaco.lang.Str $obj */ $this->object_module('after_exec_template', \org\rhaco\lang\Str::ref($_obj_, $_eval_src_)); return (string) $_obj_; }