/** * The {{ all_the_vars }} tag * * @return string */ public function index() { $remove_underscored = $this->fetchParam('remove_underscored', true, null, true); // Tidy up the context values $context = $this->context; foreach ($context as $key => $val) { // Remove objects. You can't use them in templates. if (is_object($val)) { unset($context[$key]); } // Remove underscored variables. if ($remove_underscored && Pattern::startsWith($key, '_')) { unset($context[$key]); } } // Get extra page data $this->page_data = Content::get(URL::getCurrent()); // CSS $output = $this->css->link('all_the_vars'); if (!$this->fetchParam('websafe_font', false, null, true)) { $output .= '<link rel="stylesheet" href="//fonts.googleapis.com/css?family=Ubuntu+Mono" />'; } // Create table $output .= $this->createTable($context, false); // Display on the screen die($output); }
public function deleteFile() { if (!Config::get('allow_file_field_deletions')) { return $this->abortDeletion('file_deleting_not_permitted'); } if (!($path = Request::get('path'))) { return $this->abortDeletion('file_no_path'); } if (!($destination = Request::get('config'))) { return $this->abortDeletion('file_no_config'); } $destination = Path::addStartingSlash(Helper::decrypt(urldecode($destination))); if (!Pattern::startsWith($path, $destination) || strpos($path, '../')) { return $this->abortDeletion('error'); } $full_path = Path::assemble(BASE_PATH, $path); if (!File::exists($full_path)) { return $this->abortDeletion('file_doesnt_exist'); } File::delete($full_path); return array('success' => true, 'message' => Localization::fetch('file_deleted')); }
public function count() { // grab parameters $from = $this->fetchParam('from', URL::getCurrent()); $exclude = $this->fetchParam('exclude', false); $max_depth = $this->fetchParam('max_depth', 1, 'is_numeric'); $include_entries = $this->fetchParam('include_entries', false, false, true); $folders_only = $this->fetchParam('folders_only', true, false, true); $include_content = $this->fetchParam('include_content', false, false, true); $show_hidden = $this->fetchParam('show_hidden', false, null, true); // add in left-/ if not present if (substr($from, 0, 1) !== '/') { $from = '/' . $from; } // if this doesn't start with the site root, add the site root if (!Pattern::startsWith($from, Config::getSiteRoot())) { $from = Path::tidy(Config::getSiteRoot() . '/' . $from); } // standardize excludes if ($exclude && !is_array($exclude)) { $exclude = Helper::explodeOptions($exclude, array()); foreach ($exclude as $key => $value) { $exclude[$key] = Path::tidy(Config::getSiteRoot() . '/' . $value); } } // option hash $hash = Helper::makeHash($from, $exclude, $max_depth, $include_entries, $folders_only, $include_content, $show_hidden); // load the content tree from cache if ($this->blink->exists($hash)) { $tree = $this->blink->get($hash); } else { $tree = ContentService::getContentTree($from, $max_depth, $folders_only, $include_entries, $show_hidden, $include_content, $exclude); $this->blink->set($hash, $tree); } if ($this->content) { return Parse::template($this->content, array('count' => count($tree))); } elseif (count($tree)) { return count($tree); } return ''; }
/** * Gets a tree of content information * * @param string $base_url URL for the base of the tree to load * @param int $depth Number of levels deep to return * @param boolean $folders_only Folders only * @param boolean $include_entries Should we include entries in our tree? * @param boolean $show_hidden Should we not include hidden content * @param boolean $include_content Should we include content from the found info? * @param mixed $exclude Array of URLs to exclude * @return array */ public static function getContentTree($base_url, $depth = 12, $folders_only = true, $include_entries = false, $show_hidden = false, $include_content = false, $exclude = false) { // load structure and set up variables self::loadStructure(); $output = array(); // exclude URLs $exclude = Helper::ensureArray($exclude); // no depth asked for if ($depth == 0) { return array(); } // make sure we can find the requested URL in the structure if (!isset(self::$structure[$base_url])) { Log::debug('Could not find URL in structure cache.', 'core', 'ContentService'); return array(); } // depth measurements $starting_depth = self::$structure[$base_url]['depth'] + 1; // start one deeper than the base URL's depth $current_depth = $starting_depth; // recursively grab the tree foreach (self::$structure as $url => $data) { // is this the right depth and not the 404 page? if ($data['depth'] !== $current_depth || $url == "/404") { continue; } // is this under the appropriate parent? if (!Pattern::startsWith(Path::tidy($data['parent'] . '/'), Path::tidy($base_url . '/'))) { continue; } // is this hidden? if ($data['is_draft'] || !$show_hidden && $data['is_hidden']) { continue; } // is this an entry when we don't want them? if (!$include_entries && $data['is_entry'] && !$data['is_page']) { continue; } // is this a non-folder when all we want is folders? if ($folders_only && $data['type'] != 'folder') { continue; } // is this in the excluded URLs list? if (in_array($url, $exclude)) { continue; } // get parent url $parent_url = substr($url, 0, strrpos($url, '/')); $parent_url = $parent_url == "" ? Config::getSiteRoot() : $parent_url; // look up parent data in cache if (!isset(self::$parent_cache[$parent_url])) { // doesn't exist, load it up $parent_data = Content::get($parent_url, $include_content, false); if ($include_content) { // give them everything $parent = $parent_data; } else { // just the bare necessities $parent = array('title' => isset($parent_data['title']) ? $parent_data['title'] : '', 'url' => isset($parent_data['url']) ? $parent_data['url'] : ''); } // now stick this in the cache for next time self::$parent_cache[$parent_url] = $parent; } // get information $content = Content::get($url, $include_content, false); // data to be returned to the tree $for_output = array('type' => $data['type'], 'title' => isset($content['title']) ? $content['title'] : '', 'slug' => $content['slug'], 'url' => $url, 'depth' => $current_depth, 'children' => self::getContentTree($url, $depth - 1, $folders_only, $include_entries, $show_hidden, $include_content, $exclude), 'is_current' => URL::getCurrent() == $url, 'is_parent' => URL::getCurrent() != $url && Pattern::startsWith(URL::getCurrent(), $url . '/'), 'is_entry' => $data['is_entry'], 'is_page' => $data['is_page'], 'is_folder' => $data['type'] == 'folder', 'order_key' => $data['order_key'], 'sub_order_key' => $data['sub_order_key'], 'parent' => array(self::$parent_cache[$parent_url])); // if we're including content, merge that in if ($include_content) { $for_output = $content + $for_output; } // add it to the list $output[] = $for_output; } // now we need to sort the nav items uasort($output, function ($a, $b) { // sort on order_key $result = Helper::compareValues($a['order_key'], $b['order_key']); // if those matched, sort on sub_order_key if ($result === 0) { $result = Helper::compareValues($a['sub_order_key'], $b['sub_order_key']); } // return 1 or 0 or -1, whatever we ended up with return $result; }); // re-key the array $output = array_values($output); // return what we know return $output; }
/** * Creates a URL-friendly path from webroot * * @param string $path Path to trim * @return string */ public static function toAsset($path, $as_variable = false) { $asset_path = Path::trimFilesystem($path); if (Pattern::startsWith($asset_path, '{{ _site_root }}')) { $asset_path = str_replace('{{ _site_root }}', '/', $asset_path); } if (!Pattern::startsWith($asset_path, Config::getSiteRoot())) { $asset_path = $as_variable ? '{{ _site_root }}' . $asset_path : Config::getSiteRoot() . '/' . $asset_path; } return rtrim(self::tidy($asset_path), '/'); }
/** * Checks for and parses front matter * * @param string $string Content to parse * @return array */ public static function frontMatter($string, $yamlize = true) { $data = array(); $content = $string; if (Pattern::startsWith($string, "---")) { list($yaml, $content) = preg_split("/\n---/", $string, 2, PREG_SPLIT_NO_EMPTY); if ($yamlize) { $data = self::yaml($yaml); } } return compact('data', 'content'); }
public static function get_content_tree($directory = '/', $depth = 1, $max_depth = 5, $folders_only = false, $include_entries = false, $hide_hidden = true, $include_content = false, $site_root = false) { // $folders_only = true only page.md // folders_only = false includes any numbered or non-numbered page (excluding anything with a fields.yaml file) // if include_entries is true then any numbered files are included $content_root = Config::getContentRoot(); $content_type = Config::getContentType(); $site_root = $site_root ? $site_root : Config::getSiteRoot(); $current_url = Path::tidy($site_root . '/' . Request::getResourceURI()); $taxonomy_url = false; if (Taxonomy::isTaxonomyURL($current_url)) { list($taxonomy_type, $taxonomy_name) = Taxonomy::getCriteria($current_url); $taxonomy_url = self::remove_taxonomy_from_path($current_url, $taxonomy_type, $taxonomy_name); } $directory = '/' . $directory . '/'; #ensure proper slashing if ($directory != '/') { $base = Path::tidy("{$content_root}/{$directory}"); } elseif ($directory == '/') { $base = "{$content_root}"; } else { $base = "{$content_root}"; } $files = glob("{$base}/*"); $data = array(); if ($files) { foreach ($files as $path) { $current_name = basename($path); if (!Pattern::endsWith($current_name, '.yaml')) { // Hidden page that should be removed if ($hide_hidden && Pattern::startsWith($current_name, '_')) { continue; } $node = array(); $file = substr($path, strlen($base) + 1, strlen($path) - strlen($base) - strlen($content_type) - 2); if (is_dir($path)) { $folder = substr($path, strlen($base) + 1); $node['type'] = 'folder'; $node['slug'] = basename($folder); $node['title'] = ucwords(basename($folder)); $node['numeric'] = Slug::getOrderNumber($folder); $node['file_path'] = Path::tidy($site_root . '/' . $directory . '/' . $folder . '/page'); if (Slug::isNumeric($folder)) { $pos = strpos($folder, "."); if ($pos !== false) { $node['raw_url'] = Path::tidy(Path::clean($site_root . '/' . $directory . '/' . $folder)); $node['url'] = Path::clean($node['raw_url']); $node['title'] = ucwords(basename(substr($folder, $pos + 1))); } else { $node['title'] = ucwords(basename($folder)); $node['raw_url'] = Path::tidy($site_root . '/' . $directory . '/' . $folder); $node['url'] = Path::clean($node['raw_url']); } } else { $node['title'] = ucwords(basename($folder)); $node['raw_url'] = Path::tidy($site_root . '/' . $directory . '/' . $folder); $node['url'] = Path::clean($node['raw_url']); } $node['depth'] = $depth; $node['children'] = $depth < $max_depth ? self::get_content_tree($directory . $folder . '/', $depth + 1, $max_depth, $folders_only, $include_entries, $hide_hidden, $include_content, $site_root) : null; $node['is_current'] = $node['raw_url'] == $current_url || $node['url'] == $current_url ? true : false; $node['is_parent'] = false; if ($node['url'] == URL::popLastSegment($current_url) || $taxonomy_url && $node['url'] == $taxonomy_url) { $node['is_parent'] = true; } $node['has_children'] = $node['children'] ? true : false; // has entries? if (File::exists(Path::tidy($path . "/fields.yaml"))) { $node['has_entries'] = true; } else { $node['has_entries'] = false; } $meta = self::get_content_meta("page", Path::tidy($directory . "/" . $folder), false, true); //$meta = self::get_content_meta("page", Statamic_Helper::reduce_double_slashes($directory."/".$folder)); if (isset($meta['title'])) { $node['title'] = $meta['title']; } if (isset($meta['last_modified'])) { $node['last_modified'] = $meta['last_modified']; } if ($hide_hidden === true && (isset($meta['status']) && ($meta['status'] == 'hidden' || $meta['status'] == 'draft'))) { // placeholder condition } else { $data[] = $include_content ? array_merge($meta, $node) : $node; // print_r($data); } } else { if (Pattern::endsWith($path, $content_type)) { if ($folders_only == false) { if ($file == 'page' || $file == 'feed' || $file == '404') { // $node['url'] = $directory; // $node['title'] = basename($directory); // $meta = self::get_content_meta('page', substr($directory, 1)); // $node['depth'] = $depth; } else { $include = true; // date based is never included if (Config::getEntryTimestamps() && Slug::isDateTime(basename($path))) { $include = false; } elseif (Slug::isDate(basename($path))) { $include = false; } elseif (Slug::isNumeric(basename($path))) { if ($include_entries == false) { if (File::exists(Path::tidy(dirname($path) . "/fields.yaml"))) { $include = false; } } } if ($include) { $node['type'] = 'file'; $node['raw_url'] = Path::tidy($directory) . basename($path); $pretty_url = Path::clean($node['raw_url']); $node['url'] = substr($pretty_url, 0, -1 * (strlen($content_type) + 1)); $node['is_current'] = $node['url'] == $current_url || $node['url'] == $current_url ? true : false; $node['slug'] = substr(basename($path), 0, -1 * (strlen($content_type) + 1)); $meta = self::get_content_meta(substr(basename($path), 0, -1 * (strlen($content_type) + 1)), substr($directory, 1), false, true); //$node['meta'] = $meta; if (isset($meta['title'])) { $node['title'] = $meta['title']; } $node['depth'] = $depth; if ($hide_hidden === true && (isset($meta['status']) && ($meta['status'] == 'hidden' || $meta['status'] == 'draft'))) { } else { $data[] = $include_content ? array_merge($meta, $node) : $node; } } } } } } } } } return $data; }
/** * Checks whether a URL is external or not * @param string $url * @return boolean */ public static function isExternalUrl($url) { return !Pattern::startsWith(URL::makeFull($url), URL::getSiteUrl()); }
/** * starts_with * Determines if a given $haystack starts with $needle * * @deprecated Use Pattern::startsWith() instead * * @param string $haystack String to inspect * @param string $needle Character to look for * @return boolean */ public static function starts_with($haystack, $needle) { Log::warn("Use of Statamic_Helper::starts_with() is deprecated. Use Pattern::startsWith() instead.", "core", "Statamic_Helper"); return Pattern::startsWith($haystack, $needle); }
/** * Finds a given path on the server, adding in any ordering elements missing * * @param string $path Path to resolve * @return string */ public static function resolve($path) { $content_root = Config::getContentRoot(); $content_type = Config::getContentType(); if (strpos($path, "/") === 0) { $parts = explode("/", substr($path, 1)); } else { $parts = explode("/", $path); } $fixedpath = "/"; foreach ($parts as $part) { if (!File::exists(URL::assemble($content_root, $path . '.' . $content_type)) && !is_dir(URL::assemble($content_root, $part))) { // check folders $list = Statamic::get_content_tree($fixedpath, 1, 1, FALSE, TRUE, FALSE); foreach ($list as $item) { $t = basename($item['slug']); if (Slug::isNumeric($t)) { $nl = strlen(Slug::getOrderNumber($t)) + 1; if (strlen($part) >= strlen($item['slug']) - $nl && Pattern::endsWith($item['slug'], $part)) { $part = $item['slug']; break; } } else { if (Pattern::endsWith($item['slug'], $part)) { if (strlen($part) >= strlen($t)) { $part = $item['slug']; break; } } } } // check files $list = Statamic::get_file_list($fixedpath); foreach ($list as $key => $item) { if (Pattern::endsWith($key, $part)) { $t = basename($item); $offset = 0; if (Pattern::startsWith($key, '__')) { $offset = 2; } elseif (Pattern::startsWith($key, '_')) { $offset = 1; } if (Config::getEntryTimestamps() && Slug::isDateTime($t)) { if (strlen($part) >= strlen($key) - 16 - $offset) { $part = $key; break; } } elseif (Slug::isDate($t)) { if (strlen($part) >= strlen($key) - 12 - $offset) { $part = $key; break; } } elseif (Slug::isNumeric($t)) { $nl = strlen(Slug::getOrderNumber($key)) + 1; if (strlen($part) >= strlen($key) - $nl - $offset) { $part = $key; break; } } else { $t = basename($item); if (strlen($part) >= strlen($t) - $offset) { $part = $key; break; } } } } } if ($fixedpath != '/') { $fixedpath .= '/'; } $fixedpath .= $part; } // /2-blog/hidden return $fixedpath; }