/** * @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; }
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())); } }
/** * コマンドを実行しプロセスをオープする * @param string $command 実行するコマンド * @param string $out_file 結果を保存するファイルパス * @param string $error_file エラー結果を保存するファイルパス */ public function open($command, $out_file = null, $error_file = null) { \org\rhaco\Log::debug($command); $this->close(); if (!empty($out_file)) { \org\rhaco\io\File::write($out_file); } if (!empty($error_file)) { \org\rhaco\io\File::write($error_file); } $out = empty($out_file) ? array('pipe', 'w') : array('file', $out_file, 'w'); $err = empty($error_file) ? array('pipe', 'w') : array('file', $error_file, 'w'); $this->proc = proc_open($command, array(array('pipe', 'r'), $out, $err), $this->resource); $this->close = false; }
public function __construct($app_url = null) { $entry_file = $this->entry_file(); $branch_url = basename($entry_file); $rewrite = \org\rhaco\Conf::get('rewrite_entry'); $this->app_url = \org\rhaco\Conf::get('app_url'); $this->media_url = \org\rhaco\Conf::get('media_url'); $path = function ($url) { if (!empty($url)) { $url = str_replace('\\', '/', $url); if (substr($url, -1) != '/') { $url .= '/'; } } return $url; }; if (empty($this->app_url)) { $host = \org\rhaco\Request::host(); if (!empty($host)) { $hasport = (bool) preg_match('/:\\d+/', $host); $this->app_url = $host . ($hasport ? '' : '/' . dirname(preg_replace("/.+\\/workspace\\/(.+)/", "\\1", $entry_file))); if (!isset($rewrite)) { $rewrite = $hasport ? false : true; } } else { $this->app_url = 'http://localhost:8000/'; $rewrite = false; } } if (substr($branch_url, 0, 1) == '/') { $branch_url = substr($branch_url, 1); } if ($rewrite === null || $rewrite === true) { $branch_url = basename($branch_url, '.php'); if ($branch_url == 'index') { $branch_url = ''; } } $this->app_url = str_replace('https://', 'http://', $path($this->app_url)); $this->branch_url = $this->app_url . $path($branch_url); $this->media_url = $path(empty($this->media_url) ? $this->app_url . 'resources/media/' : $this->media_url); $this->template_path = $path(\org\rhaco\Conf::get('template_path', \org\rhaco\io\File::resource_path('templates'))); $this->template = new \org\rhaco\Template(); }
/** * 書き込みテストを行う * @throws RuntimeException */ public static function test() { $nodes = \org\rhaco\Conf::get('save_nodes'); if (empty($nodes)) { throw new \org\rhaco\io\Storage\StorageException('ノードが設定されていません'); } $service = \org\rhaco\Conf::get('service_name', 'new_service'); foreach ($nodes as $node) { try { $file = self::get_path($node . '/node_con_test'); \org\rhaco\io\File::write($file, __CLASS__); \org\rhaco\io\File::read($file); \org\rhaco\io\File::rm($file); } catch (\Exception $e) { \org\rhaco\Exceptions::add($e, $node); } } \org\rhaco\Exceptions::throw_over(); }
/** * ファイルに出力する * @param string $filename * @param string $type * @return string */ public function write($filename, $type = null) { if (!is_dir(dirname($filename))) { \org\rhaco\io\File::mkdir(dirname($filename)); } if ($type !== null) { $this->type($type); } $bool = false; $ext = image_type_to_extension($this->type_no(), true); if ($ext == '.jpeg') { $ext = '.jpg'; } if (!preg_match('/' . preg_quote($ext) . '$/i', $filename)) { $filename = $filename . $ext; } switch ($this->type) { case 'gif': $bool = imagegif($this->resource, $filename); break; case 'jpg': $bool = imagejpeg($this->resource, $filename, ceil($this->quality * 10)); break; case 'png': $bool = imagepng($this->resource, $filename, 10 - ceil($this->quality)); break; case 'bmp': $bool = imagewbmp($this->resource, $filename); break; } if (!$bool) { throw new Image\ImageException('invalid type'); } return $filename; }
/** * 添付ファイルを移動します * @param array $file_info * @param string $newname */ public function move_file($file_info, $newname) { if (!$this->has_file($file_info)) { throw new \LogicException('file not found '); } if (!is_dir(dirname($newname))) { \org\rhaco\io\File::mkdir(dirname($newname)); } copy($file_info['tmp_name'], $newname); chmod($newname, 0777); unlink($file_info['tmp_name']); }
/** * テンプレートから内容を取得しセットする * * テンプレートサンプル * <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'); }
/** * @module org.rhaco.Template * @param string $cname * @return boolean */ public function get_template_cache($cname) { return \org\rhaco\io\File::read($this->path . '/' . $cname); }
<?php /** * dao data export * @param string $file */ if (empty($file)) { $file = getcwd() . '/dump.ddj'; } \org\rhaco\io\File::write($file, ''); foreach (\org\rhaco\Dt::classes('\\org\\rhaco\\store\\db\\Dao') as $class_info) { $r = new \ReflectionClass($class_info['class']); if ($r->getParentClass()->getName() == 'org\\rhaco\\store\\db\\Dao') { \cmdman\Std::println_info('Find ' . $r->getName()); \org\rhaco\io\File::append($file, '[[' . $r->getName() . ']]' . PHP_EOL); $find = call_user_func(array($r->getName(), 'find')); foreach ($find as $obj) { \org\rhaco\io\File::append($file, json_encode($obj->props()) . PHP_EOL); } } } \cmdman\Std::println_success('Written ' . $file);
/** * @module org.rhaco.net.listener.SocketListener * @param org.rhaco.net.listener.Channel $channel */ public function connect($channel) { $head = $body = null; $method = $uri = $query = $boundary = null; $POST = $GET = $FILES = $SERVER = array(); $message_len = $null_cnt = 0; try { $uid = uniqid(''); while (true) { $message = $channel->read(); if ($message === '') { $null_cnt++; } if ($null_cnt > 5) { break; } if ($method === null) { $head .= $message; if (substr($head, -4) === "\r\n\r\n") { $lines = explode("\n", trim($head)); if (!empty($lines)) { $exp = explode(' ', array_shift($lines)); if (sizeof($exp) >= 2) { list($method, $uri) = $exp; } if (strpos($uri, '?')) { list($uri, $SERVER['QUERY_STRING']) = explode('?', $uri); parse_str($SERVER['QUERY_STRING'], $GET); } foreach ($lines as $line) { $exp = explode(':', $line, 2); if (sizeof($exp) == 2) { list($name, $value) = $exp; $SERVER['HTTP_' . str_replace(array('-'), array('_'), strtoupper(trim($name)))] = trim($value); } } } if ($method === null || $method == 'GET') { break; } if (isset($SERVER['HTTP_CONTENT_TYPE']) && preg_match("/multipart\\/form-data; boundary=(.+)\$/", $SERVER['HTTP_CONTENT_TYPE'], $m)) { $boundary = '--' . $m[1]; } } } else { if ($method == 'POST') { $message_len += strlen($message); $body .= $message; if (isset($SERVER['HTTP_CONTENT_LENGTH']) && $message_len >= $SERVER['HTTP_CONTENT_LENGTH'] || !isset($SERVER['HTTP_CONTENT_LENGTH']) && substr($body, -4) === "\r\n\r\n") { if (isset($boundary)) { list($body) = explode($boundary . "--\r\n", $body, 2); foreach (explode($boundary . "\r\n", $body) as $k => $block) { if (!empty($block)) { list($h, $b) = explode("\r\n\r\n", $block); list($b) = explode("\r\n", $b, 2); if (preg_match("/\\sname=([\"'])(.+?)\\1/", $h, $m)) { $name = $m[2]; if (preg_match("/filename=([\"'])(.+?)\\1/", $h, $m)) { $tmp_name = self::work_path($uid, $k); \org\rhaco\io\File::write($tmp_name, $b); $FILES[$name] = array('name' => $m[2], 'tmp_name' => $tmp_name, 'size' => filesize($tmp_name), 'error' => 0); } else { $POST[$name] = $b; } } } } } else { parse_str($body, $POST); } break; } else { if (!isset($SERVER['HTTP_CONTENT_LENGTH'])) { break; } } } else { $this->output('Unknown method: ' . $method); break; } } } if (!empty($uri)) { $request_uri = $uri; $this->output('request uri: ' . $uri); $uri = preg_replace("/\\/+/", "/", $uri); $uri_path = \org\rhaco\net\Path::absolute(getcwd(), \org\rhaco\net\Path::slash($uri, false, null)); if (strpos($uri, '.php') === false && !is_file($uri_path) && $uri != '/favicon.ico') { $exp = explode('/', \org\rhaco\net\Path::slash($uri, false, null), 2); if (is_file(\org\rhaco\net\Path::absolute(getcwd(), $exp[0] . '.php')) && isset($exp[1])) { $uri = '/' . $exp[0] . '.php/' . $exp[1]; } else { if (is_file(\org\rhaco\net\Path::absolute(getcwd(), 'index.php'))) { $uri = '/index.php' . $uri; } } } if ($request_uri != $uri) { $this->output(' - rewrite uri: ' . $uri); } if (strpos($uri, '.php/') !== false) { $exp = explode('.php/', $uri, 2); $uri = $exp[0] . '.php'; $SERVER['PATH_INFO'] = '/' . $exp[1]; } $path = \org\rhaco\net\Path::absolute(getcwd(), \org\rhaco\net\Path::slash($uri, false, null)); if (is_file($path)) { $file = new \org\rhaco\io\File($path); if (substr($path, -4) == '.php') { $file->mime('text/html'); } $headers = array(); $headers[] = 'Content-Type: ' . $file->mime(); $headers[] = 'Connection: close'; if (substr($path, -4) == '.php') { $SERVER['REQUEST_METHOD'] = $method; $REQUEST = array('_SERVER' => $SERVER, '_POST' => $POST, '_GET' => $GET, '_FILES' => $FILES); \org\rhaco\io\File::write(self::work_path($uid, 'request'), serialize($REQUEST)); $exec_command = $this->php_cmd . ' ' . $file->fullname() . ' -emulator ' . $uid; $this->output(' -- ' . $exec_command); $cmd = new \org\rhaco\Command($exec_command); if (is_file(self::work_path($uid, 'header'))) { $send_header = unserialize(\org\rhaco\io\File::read(self::work_path($uid, 'header'))); if (!empty($send_header) && is_array($send_header)) { $headers = array_merge($headers, $send_header); } } foreach ($headers as $k => $v) { if (strpos($v, ':') === false) { $top = $headers[$k]; unset($headers[$k]); array_unshift($headers, $top); break; } } $output_header = trim(implode("\r\n", $headers)); if (strpos($output_header, 'HTTP/') !== 0) { if (strpos($output_header, 'Location: ') !== false) { $output_header = "HTTP/1.1 302 Found\r\n" . $output_header; } else { $output_header = "HTTP/1.1 200 OK\r\n" . $output_header; } } $channel->write($output_header . "\r\n\r\n"); $channel->write($cmd->stdout()); \org\rhaco\io\File::rm(self::work_path($uid)); } else { $channel->write("HTTP/1.1 200 OK\r\n" . implode("\r\n", $headers) . "\r\n\r\n"); $fp = fopen($file->fullname(), 'rb'); while (!feof($fp)) { $channel->write(fread($fp, 4096)); } fclose($fp); } } else { $this->output($path . ' not found'); $channel->write($this->error(404, 'Not Found', 'The requested URL ' . $uri . ' was not found on this server.')); } } } catch (\org\rhaco\net\listener\exception\ErrorException $e) { $this->output($e->getMessage()); } }
public static function unzip($inpath, $outpath) { if (substr($outpath, -1) != '/') { $outpath = $outpath . '/'; } if (!is_dir($outpath)) { \org\rhaco\io\File::mkdir($outpath, 0777); } $zip = new \ZipArchive(); if ($zip->open($inpath) !== true) { throw new \ErrorException('failed to open stream'); } $zip->extractTo($outpath); $zip->close(); }