public static function extract($file, $line) { $content = f::read($file); $lines = preg_split('/\\r\\n|\\n|\\r/', $content); $begin = $line - 5; if ($begin < 0) { $begin = 0; } $end = 10; $lines = array_slice($lines, $begin, $end); $html = ''; $n = $begin + 1; foreach ($lines as $l) { if (empty($l)) { $l = ' '; } $num = '<span class="code-line-number">' . $n . '</span>'; if ($n == $line) { $html .= '<span class="code-line code-line-highlighted">' . $num . htmlspecialchars($l) . '</span>'; } else { $html .= '<span class="code-line">' . $num . htmlspecialchars($l) . '</span>'; } $n++; } return $html; }
private static function routes() { kirby()->routes(array(array('pattern' => self::$route, 'action' => function () { echo SplitviewTool::html(); }), array('pattern' => self::$route . '/assets/css/tool.css', 'action' => function () { $path = self::$assets . 'css' . DS . 'tool.css'; return new Response(f::read($path), 'css'); }), array('pattern' => self::$route . '/assets/css/tool.min.css', 'action' => function () { $path = self::$assets . 'css' . DS . 'tool.min.css'; return new Response(f::read($path), 'css'); }), array('pattern' => self::$route . '/assets/js/dist/tool.js', 'action' => function () { $path = self::$assets . 'js' . DS . 'dist/tool.js'; return new Response(f::read($path), 'js'); }), array('pattern' => self::$route . '/assets/js/dist/tool.min.js', 'action' => function () { $path = self::$assets . 'js' . DS . 'dist/tool.min.js'; return new Response(f::read($path), 'js'); }), array('pattern' => self::$route . '/assets/js/src/site.js', 'action' => function () { $path = self::$assets . 'js' . DS . 'src/site/site.js'; return new Response(f::read($path), 'js'); }), array('pattern' => self::$route . '/assets/js/dist/site.min.js', 'action' => function () { $path = self::$assets . 'js' . DS . 'dist/site.min.js'; return new Response(f::read($path), 'js'); }), array('pattern' => self::$route . '/assets/fonts/font-awesome.woff', 'action' => function () { $path = self::$assets . 'fonts' . DS . 'font-awesome-4.5.0' . DS . 'fontawesome-webfont.woff'; return new Response(f::read($path), 'woff'); }), array('pattern' => self::$route . '/assets/fonts/source-sans-pro.woff', 'action' => function () { $path = self::$assets . 'fonts' . DS . 'source-sans-pro' . DS . 'SourceSansPro-Regular.otf.woff'; return new Response(f::read($path), 'woff'); }))); }
public function assets($type, $root, $files) { $output = array(); foreach ($files as $filename) { $output[] = f::read(dirname($root) . DS . 'assets' . DS . $type . DS . $filename); } $this->{$type} .= implode(PHP_EOL . PHP_EOL, $output); }
public function runTests($result) { $root = TEST_ROOT_ETC . DS . 'kirbytext'; $dirs = dir::read($root); foreach ($dirs as $dir) { $testFile = $root . DS . $dir . DS . 'test.txt'; $expectedFile = $root . DS . $dir . DS . 'expected.html'; $this->assertEquals(f::read($expectedFile), $result(f::read($testFile)), 'test: ' . $dir); } }
protected function copy() { if ($this->input->getOption('bare')) { $blueprint = f::read($this->template()); $blueprint = str::template($blueprint, ['title' => ucfirst($this->name())]); f::write($this->file(), $blueprint); } else { $this->questions(); } }
static function get($file, $raw = false) { if (!c::get('cache')) { return false; } $content = f::read(self::file($file)); if ($raw == false) { $content = @unserialize($content); } return $content; }
public static function read($file, $type = null) { // type autodetection if (is_null($type)) { $type = f::extension($file); } // get the adapter $adapter = static::adapter($type); if (isset($adapter['read'])) { return call($adapter['read'], $file); } else { return data::decode(f::read($file), $type); } }
public function check_cache($type) { $cache_path = __DIR__ . '/../cache/' . $type . '.json'; if (\f::exists($cache_path)) { $cache = json_decode(\f::read($cache_path, 'json')); if ($cache->to < time()) { return false; } else { return $cache->payload; } } else { return false; } }
static function get($file, $raw = false, $expires = false) { if (!c::get('cache')) { return false; } // check for an expired cache if ($expires && self::expired($file, $expires)) { return false; } $content = f::read(self::file($file)); if ($raw == false) { $content = @unserialize($content); } return $content; }
private static function routes() { kirby()->routes(array(array('pattern' => self::$route, 'action' => function () { echo SplitviewTool::html(); }), array('pattern' => self::$route . '/css/(:any)', 'action' => function ($slug) { $path = self::$assets . 'css' . DS . $slug . '.min.css'; return new Response(f::read($path), 'css'); }), array('pattern' => self::$route . '/js/dist/(:any)', 'action' => function ($slug) { $path = self::$assets . 'js' . DS . 'dist' . DS . $slug . '.min.js'; return new Response(f::read($path), 'js'); }), array('pattern' => self::$route . '/js/src/(:any)', 'action' => function ($slug) { $path = self::$assets . 'js' . DS . 'src' . DS . $slug . DS . $slug . '.js'; return new Response(f::read($path), 'js'); }), array('pattern' => self::$route . '/svg/(:any)', 'action' => function ($slug) { $path = self::$assets . 'images' . DS . 'svg' . DS . $slug . '.svg'; return new Response(f::read($path), 'svg'); }), array('pattern' => self::$route . '/svg/inverted/(:any)', 'action' => function ($slug) { $path = self::$assets . 'images' . DS . 'svg' . DS . 'inverted' . DS . $slug . '.svg'; return new Response(f::read($path), 'svg'); }))); }
static function fetch($file) { if (!file_exists($file)) { return array('raw' => false, 'data' => array()); } $content = f::read($file); $content = str_replace("", '', $content); $sections = preg_split('![\\r\\n]+[-]{4,}!i', $content); $data = array(); foreach ($sections as $s) { $parts = explode(':', $s); if (count($parts) == 1 && count($sections) == 1) { return $content; } $key = str::lower(preg_replace('![^a-z0-9]+!i', '_', trim($parts[0]))); if (empty($key)) { continue; } $value = trim(implode(':', array_slice($parts, 1))); $data[$key] = $value; } return array('raw' => $content, 'data' => $data); }
static function fetch($file) { if (!file_exists($file)) { return array(); } $content = f::read($file); $content = str_replace("", '', $content); $sections = preg_split('!\\R[-]{4,}!', $content); $data = array(); foreach ($sections as $s) { $parts = explode(':', $s); if (count($parts) == 1 && count($sections) == 1) { return $content; } $key = str::urlify($parts[0]); if (empty($key)) { continue; } $value = trim(implode(':', array_slice($parts, 1))); $data[$key] = $value; } return $data; }
public function assets($type) { $output = []; $defaultRoot = panel()->roots()->fields(); foreach (kirby()->get('field') as $name => $field) { $root = $field->root(); $base = dirname($root); // only fetch assets for custom fields if ($base == $defaultRoot) { continue; } $classname = $field->class(); if (!class_exists($classname)) { throw new Exception('The field class is missing for: ' . $classname); } if (!isset($classname::$assets) || !isset($classname::$assets[$type])) { continue; } foreach ($classname::$assets[$type] as $filename) { $output[] = f::read($field->root() . DS . 'assets' . DS . $type . DS . $filename); } } return implode(PHP_EOL . PHP_EOL, $output); }
public static function assets($type, $compress = true) { $files = static::files(); $output = array(); foreach (static::files() as $field => $file) { if (isset($field::$assets) and isset($field::$assets[$type])) { foreach ($field::$assets[$type] as $f) { $output[] = f::read(dirname($file) . DS . 'assets' . DS . $type . DS . $f); } } } $output = implode(PHP_EOL . PHP_EOL, $output); if ($compress) { $output = preg_replace('!/\\*[^*]*\\*+([^/][^*]*\\*+)*/!', '', $output); $output = trim(str_replace(array("\r\n", "\r", "\n", "\t", ' ', ' ', ' '), ' ', $output)); } return $output; }
/** * Registers all routes * * @param array $routes New routes * @return array */ public function routes($routes = array()) { // extend the existing routes if (!empty($routes) and is_array($routes)) { return $this->options['routes'] = array_merge($this->options['routes'], $routes); } $routes = $this->options['routes']; $kirby = $this; $site = $this->site(); if ($site->multilang()) { foreach ($site->languages() as $lang) { $routes[] = array('pattern' => ltrim($lang->url . '/(:all?)', '/'), 'method' => 'ALL', 'lang' => $lang, 'action' => function ($path = null) use($kirby, $site) { return $site->visit($path, $kirby->route->lang->code()); }); } // fallback for the homepage $routes[] = array('pattern' => '/', 'method' => 'ALL', 'action' => function () use($kirby, $site) { // check if the language detector is activated if ($kirby->option('language.detect')) { if (s::get('language') and $language = $kirby->site()->sessionLanguage()) { // $language is already set but the user wants to // select the default language $referer = r::referer(); if (!empty($referer) && str::startsWith($referer, $this->urls()->index())) { $language = $kirby->site()->defaultLanguage(); } } else { // detect the user language $language = $kirby->site()->detectedLanguage(); } } else { // always use the default language if the detector is disabled $language = $kirby->site()->defaultLanguage(); } // redirect to the language homepage if necessary if ($language->url != '/' and $language->url != '') { go($language->url()); } // plain home pages return $site->visit('/', $language->code()); }); } // tinyurl handling $routes['tinyurl'] = $this->component('tinyurl')->route(); // home redirect $routes['homeRedirect'] = array('pattern' => $this->options['home'], 'action' => function () { redirect::send(page('home')->url(), 307); }); // plugin assets $routes['pluginAssets'] = array('pattern' => 'assets/plugins/(:any)/(:all)', 'method' => 'GET', 'action' => function ($plugin, $path) use($kirby) { $root = $kirby->roots()->plugins() . DS . $plugin . DS . 'assets' . DS . $path; $file = new Media($root); if ($file->exists()) { return new Response(f::read($root), f::extension($root)); } else { return new Response('The file could not be found', f::extension($path), 404); } }); // all other urls $routes['others'] = array('pattern' => '(:all)', 'method' => 'ALL', 'action' => function ($path = null) use($site, $kirby) { // visit the currently active page $page = $site->visit($path); // react on errors for invalid URLs if ($page->isErrorPage() and $page->uri() != $path) { // get the filename $filename = rawurldecode(basename($path)); $pagepath = dirname($path); // check if there's a page for the parent path if ($page = $site->find($pagepath)) { // check if there's a file for the last element of the path if ($file = $page->file($filename)) { go($file->url()); } } // return the error page if there's no such page return $site->errorPage(); } return $page; }); return $routes; }
public static function download($file, $name = null) { // stop the download if the file does not exist or is not readable if (!is_file($file) or !is_readable($file)) { return false; } header::download(array('name' => $name ? $name : f::filename($file), 'size' => f::size($file), 'mime' => f::mime($file), 'modified' => f::modified($file))); die(f::read($file)); }
<div class="gcapc-info"> <ul class="gcapc-info-list"> <li>Branch: <?php echo c::get('gcapc-branch', 'master'); ?> </li> <li>Committing: <i class="icon fa fa-<?php echo c::get('gcapc-commit', false) ? 'check' : 'times'; ?> "></i></li> <li>Pulling: <i class="icon fa fa-<?php echo c::get('gcapc-pull', false) ? 'check' : 'times'; ?> "></i></li> <li>Pushing: <i class="icon fa fa-<?php echo c::get('gcapc-push', false) ? 'check' : 'times'; ?> "></i></li> </ul> </div> <div class="gcapc-status"></div> <script> <?php echo 'window.gcapcSettings = "' . kirby()->urls()->index() . '/gcapc/";'; echo f::read(__DIR__ . '/assets/js/git-commit-and-push-content.js'); ?> </script>
protected function _template($what) { $template = f::read(__DIR__ . '/templates/plugin/' . $what); $template = str::template($template, ['name' => $this->name()]); return $template; }
/** * Retrieve an item from the cache. * * @param string $key * @return object CacheValue */ public function retrieve($key) { // unserialized value array (see $this->value()) return unserialize(f::read($this->file($key))); }
function tweets($username, $params = array()) { $defaults = array('limit' => 10, 'cache' => true, 'hiderep' => false, 'refresh' => 60 * 20); // add the username to the defaults array $defaults['username'] = $username; $options = array_merge($defaults, $params); // check the cache dir $cacheDir = c::get('root.cache') . '/tweets'; dir::make($cacheDir); // disable the cache if adding the cache dir failed if (!is_dir($cacheDir) || !is_writable($cacheDir)) { $options['cache'] = false; } // sanitize the limit if ($options['limit'] > 200) { $options['limit'] = 200; } // generate a unique cache ID $cacheID = 'tweets/tweets.' . md5($options['username']) . '.' . $options['limit'] . '.php'; /** * Below is Studio Dumbar's take on the Cache functionality * Implemented because we want to bypass the global Kirby caching system */ // Check is the caching option is set if ($options['cache']) { // Get cache file and modified times $cacheFile = c::get('root.cache') . '/' . $cacheID; $cacheModTime = filemtime($cacheFile); $timeago = time() - $options["refresh"]; if ($cacheModTime < $timeago) { // File is too old, set a refresh flag $updateCache = true; } else { // File is within cache time limit, set don't update cache flag $updateCache = false; // Get the cache string from the file, using Kirby toolkit file method $cache = @unserialize(f::read($cacheFile)); } } else { $cache = false; } // If cache variable is not empty, return the cache! if (!empty($cache)) { return $cache; } // Encode the key and secret from the Twitter config. $twitterKey = urlencode(c::get('twitter.key')); $twitterSecret = urlencode(c::get('twitter.secret')); // combine and base64 encode the key and secret with a colon seperator $twitterCode = base64_encode($twitterKey . ':' . $twitterSecret); // obtain a bearer token from the api, by building a request //url to use $url = 'https://api.twitter.com/oauth2/token'; //create header $header = array('http' => array('method' => "POST", 'header' => "Content-type: application/x-www-form-urlencoded;charset=UTF-8\r\n" . "Authorization: Basic " . $twitterCode . "\r\n", 'content' => "grant_type=client_credentials")); //send the request $context = stream_context_create($header); $bearer = file_get_contents($url, false, $context); // decode the json response $bearer = json_decode($bearer); // send the rquest for tweets $url = 'https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=' . $options['username'] . '&count=' . $options['limit'] . '&include_rts=true' . '&exclude_replies=' . $options['hiderep']; $header = array('http' => array('method' => "GET", 'header' => "Authorization: Bearer " . $bearer->access_token . "\r\n")); $context = stream_context_create($header); $json = file_get_contents($url, false, $context); $data = json_decode($json); if (!$data) { return false; } $result = array(); foreach ($data as $tweet) { $user = $tweet->user; $result[] = new tweet(array('url' => 'http://twitter.com/' . $options['username'] . '/status/' . $tweet->id_str, 'text' => $tweet->text, 'date' => strtotime($tweet->created_at), 'source' => $tweet->source, 'user' => new obj(array('name' => $user->name, 'bio' => $user->description, 'username' => $user->screen_name, 'url' => 'http://twitter.com/' . $user->screen_name, 'image' => 'http://twitter.com/api/users/profile_image/' . $user->screen_name, 'following' => $user->friends_count, 'followers' => $user->followers_count)))); } $result = new obj($result); /** * Below is Studio Dumbar's take on writing new cache * Implemented because we want to bypass the global Kirby caching system */ // If the cache option is set, and the flag to update cache is set if ($options['cache'] && $updateCache) { // Open the cacheFile $fp = fopen($cacheFile, 'w'); // Check if file isn't locked if (flock($fp, LOCK_EX)) { // Write the serialized $result object into the fi;e fwrite($fp, @serialize($result)); // Unlock file flock($fp, LOCK_EX); } // Close the cacheFile fclose($fp); } return $result; }
public function testRead() { $this->assertEquals('my content is awesome', f::read($this->tmpFile)); }
public function run($path = '/') { if ($this->kirby->option('patterns.lock') && !$this->kirby->site()->user()) { go($this->kirby->option('error')); } // error handling $whoops = new \Whoops\Run(); $whoops->pushHandler(function ($e) { throw $e; return \Whoops\Handler\Handler::QUIT; }); $whoops->register(); tpl::$data = ['site' => $this->kirby->site(), 'pages' => $this->kirby->site()->children(), 'page' => $this->kirby->site()->find($this->kirby->option('home')), 'lab' => $this]; $router = new Router(); $router->register([['pattern' => '/', 'action' => function () { $readme = $this->root() . DS . 'readme.md'; if (!is_dir($this->root())) { $modal = $this->view('modals/folder'); } else { $modal = null; } if (is_file($readme)) { $markdown = kirbytext(f::read($readme)); } else { $markdown = null; } return $this->view('layouts/main', ['title' => $this->title(), 'menu' => $this->menu(), 'content' => $this->view('views/dashboard', ['markdown' => $markdown]), 'modal' => $modal]); }], ['pattern' => 'assets/(:any)', 'action' => function ($file) { switch ($file) { case 'index.js': case 'index.min.js': $mime = 'text/javascript'; break; case 'index.css': case 'index.min.css': $mime = 'text/css'; break; default: return new Response('Not found', 'text/html', 404); break; } // build the root for the file $file = dirname(__DIR__) . DS . 'assets/dist/' . $file; return new Response(f::read($file), $mime); }], ['pattern' => '(:all)/preview', 'action' => function ($path) { lab::$mode = 'preview'; $pattern = new Pattern($path); $config = $pattern->config(); try { $html = $pattern->render(); } catch (Exception $e) { $html = ''; } return $this->view('views/preview', ['pattern' => $pattern, 'html' => $html, 'background' => a::get($config, 'background', $this->kirby->option('patterns.preview.background')), 'css' => $this->kirby->option('patterns.preview.css', 'assets/css/index.css'), 'js' => $this->kirby->option('patterns.preview.js', 'assets/js/index.js')]); }], ['pattern' => '(:all)', 'action' => function ($path) { $pattern = new Pattern($path); $file = null; if (!$pattern->exists()) { $filename = basename($path); $path = dirname($path); if ($path == '.') { $preview = $this->view('previews/error', ['error' => 'The pattern could not be found']); } else { $pattern = new Pattern($path); $file = $pattern->files()->get($filename); if ($file) { $preview = $this->preview($pattern, $file); } else { $preview = $this->view('previews/error', ['error' => 'The file could not be found']); } } } else { if ($file = $pattern->files()->get($pattern->name() . '.html.php')) { go($pattern->url() . '/' . $file->filename()); } else { if ($file = $pattern->files()->first()) { go($pattern->url() . '/' . $file->filename()); } else { $preview = $this->view('previews/empty'); } } } if ($pattern->isHidden()) { go($this->url()); } return $this->view('layouts/main', ['title' => $this->title() . ' / ' . $pattern->title(), 'menu' => $this->menu(null, $path), 'content' => $this->view('views/pattern', ['preview' => $preview, 'info' => $this->view('snippets/info', ['pattern' => $pattern, 'file' => $file])])]); }]]); if ($route = $router->run($path ? $path : '/')) { return new Response(call($route->action(), $route->arguments())); } else { go('error'); } }
/** * Combines the contents of all files (of a certain type). * * @param string $files Files to combine. * @return string */ protected function combine($files) { $root = $this->hub()->finder()->assets(); $output = ''; foreach ($files as $file) { $file = $root . DS . $file; $output .= f::read($file); } return $output; }
<?php /////////////////////////////////////////////////////// // ---------------------------------------------------------- // PARTIAL :: MOBILE // ---------------------------------------------------------- ///////////////////////////////////////////////////////////// // Read assets json $assets_css = f::read(server::get('document_root') . (c::get('url') != '/' ? c::get('url') . '/' : c::get('url', '/')) . 'assets/stylesheets/min/hash.css.json'); $assets_js = f::read(server::get('document_root') . (c::get('url') != '/' ? c::get('url') . '/' : c::get('url', '/')) . 'assets/javascript/min/hash.js.json'); // Set assets based on environment if (c::get('environment') == 'local') { $env_suffix = 'dev'; $mobile_css = 'mobile.dev'; $print_css = 'print.dev'; $head_js = 'head.scripts.dev'; $mobile_js = 'mobile.scripts.dev'; } else { $env_suffix = 'min'; $mobile_css = json_decode($assets_css)->mobile; $print_css = json_decode($assets_css)->print; $head_js = json_decode($assets_js)->head; $mobile_js = json_decode($assets_js)->mobile; } // Page title if ($page->isHomePage() && $site->descriptor() != '') { $pagetitle = $site->descriptor()->smartypants()->titlecase(); } elseif ($page->subtitle() != '') { $pagetitle = $page->subtitle()->smartypants()->titlecase(); } else { $pagetitle = $page->title()->smartypants()->titlecase();