function read($file) { return \IO\File::read($file); }
public static function read($path) { $ext = \IO\File::ext($path); $type = \IO\Helpers::mimetype($ext); $dir = \Tailor\Config::get(static::guess($path)); $path = trim(strtr($path, '\\/', '//'), '/'); $file = substr($path, strpos($path, '/') + 1); $base = path($dir, $file); $test = \Tailor\Helpers::findfile("{$base}*", 0); if (!is_file($test)) { throw new \Exception("File '{$base}' not found"); } switch ($ext) { case 'css': case 'js': if (\IO\File::ext($test) === $ext) { $output = \IO\File::read($test); } else { $tmp = path(\Tailor\Config::get('cache_dir'), strtr($file, '\\/', '__')); if (is_file($tmp)) { if (filemtime($test) > filemtime($tmp)) { unlink($tmp); } } if (!is_file($tmp)) { $tpl = \Tailor\Base::compile($test); $now = date('Y-m-d H:i:s', filemtime($test)); $output = "/* {$now} ./assets/{$ext}/{$file} */\n{$tpl}"; \IO\File::write($tmp, $output); } else { $output = \IO\File::read($tmp); } } break; default: $output = \IO\File::read($test); break; } return compact('output', 'type'); }
/** * Handle a single request * * @param string method request method * @param string query query string * @param [:string] headers request headers * @param string data post data * @param peer.Socket socket * @return int */ public function handleRequest($method, $query, array $headers, $data, Socket $socket) { $url = parse_url($query); $f = new File($this->docroot, strtr(preg_replace('#\\.\\./?#', '/', urldecode($url['path'])), '/', DIRECTORY_SEPARATOR)); if (!is_file($f->getURI())) { return call_user_func($this->notFound, $this, $socket, $url['path']); } // Implement If-Modified-Since/304 Not modified $lastModified = $f->lastModified(); if ($mod = $this->header($headers, 'If-Modified-Since')) { $d = strtotime($mod); if ($lastModified <= $d) { $this->sendHeader($socket, 304, 'Not modified', []); return 304; } } clearstatcache(); try { $f->open(File::READ); } catch (IOException $e) { $this->sendErrorMessage($socket, 500, 'Internal server error', $e->getMessage()); $f->close(); return 500; } // Send OK header and data in 8192 byte chunks $this->sendHeader($socket, 200, 'OK', ['Last-Modified: ' . gmdate('D, d M Y H:i:s T', $lastModified), 'Content-Type: ' . MimeType::getByFileName($f->getFilename()), 'Content-Length: ' . $f->size()]); while (!$f->eof()) { $socket->write($f->read(8192)); } $f->close(); return 200; }
/** * Reads the whole message, applies the header information, * sets the body as a plain text (thus does not parse any * MIME-Information and returns the created Message object. * * @param string filename * @return peer.mail.Message * @throws io.IOException if file cannot be read */ protected function _readMessageRaw($filename) { $header = ''; $body = ''; $f = new File($filename); $f->open(); $d = $f->read($f->size()); $f->close(); if (false === ($hdrEnd = strpos($d, "\n\r\n\r"))) { $hdrEnd = 0; } $h = substr($c, 0, $hdrEnd); $b = substr($c, $hdrEnd); $msg = new \peer\mail\Message(); $msg->setHdrString($h); $msg->setBody($b); return $msg; }