/** * Return the virtual filename. * * Starts with a slash, but does not end with one. * This is the virtual file the client requested, ie. if qb manages the * /blog/ directory and the URL http://example.com/blog/foo/bar is called, * the virtual filename is /foo/bar. */ public static function getVFile() { $path = QB_URIPATH; // PATH_INFO is set for Apache's "Alias" directive, it has precedence. if (isset($_SERVER['PATH_INFO']) && $_SERVER['PATH_INFO'] != '') { $path = $_SERVER['PATH_INFO']; } else { if (qbString::startsWith(self::getHandler(), $path)) { $path = substr($path, strlen(self::getHandler())); } } // Cut off the query string, normalize slashes, be done! return '/' . trim(preg_replace('/\\?.+$/', '', $path), '/'); }
function qb_buildpage($path, $template = 'html') { // Set $filename to the real filename. $filename = QB_SRC . $path . QB_SUF_SRC; // Check whether that really is under the source directory. if (!qbString::startsWith(realpath(QB_SRC), realpath($filename), false)) { return ''; } // Set $t to the contents of the file. if (($t = @file_get_contents($filename)) === false) { // If the file doesn't exist, return an error. // TODO: This never happens, because main logic already checks for the // existence of the file. return qb_template(QB_TPL_PAGE . '.' . $template, array('title' => 'file not found', 'content' => '<p class=\'qb_error\'>The file ' . htmlspecialchars($path) . ' could not be found.</p>')); } else { // File exists. Let $page[0] be the first line, $page[1] be the rest. $page = explode("\n", $t, 2); // Throw all tags in the first line into $tokens. preg_match_all('|<(.*):(.*)>(?!>)|U', $page[0], $tokens, PREG_SET_ORDER); // Initialize $meta as empty array. $meta = array(); foreach ($tokens as $token) { // Let $k be the name of the tag, $v its value. $k = $token[1]; $v = $token[2]; if ($k == '') { // Special case: the <:...> tag is a shortcut for <title:...> $k = 'title'; } if ($k == 'tags') { // Special case: expand the "tags" tag via a freaky regex that // splits them by spaces, multiword tags can be supplied by // using quotation marks. Should resemble Flickr's tag parsing // logic somehow. preg_match_all('%"(?! )([^"]+)(?<! )"|([^ ]+)%', $v, $tags, PREG_PATTERN_ORDER); // Initialize $parsedtags. $parsedtags = array(); foreach ($tags[0] as $tag) { // Remove quotation marks and spaces. $tag = str_replace('"', '', trim($tag)); // If there's something left, add it to $parsedtags array. if (strlen($tag) > 0) { $parsedtags[] = $tag; } } // Remove duplicate values, save the result in $v. $v = array_unique($parsedtags); if (count($v) > 0) { // If there are any tags left, create a "spantags" template // variable and put them in it. The "tags" variable stays // an array. No idea what's the use in it, but whatever. $meta['spantags'] = '<span class=\'tag\'>' . implode('</span> <span class=\'tag\'>', $v) . '</span>'; // Create a funky "ultags" for people who prefer that. $meta['ultags'] = '<ul class=\'tag\'><li>' . implode("</li>\n<li>", $v) . '</li></ul>'; } } // Create a template variable with that value. $meta[$k] = $v; } // "path" contains the handler plus $path, double slashes removed. $meta['path'] = preg_replace('|/+|', '/', qbURL::getHandler() . $path); // "content" contains the second and all following lines. $meta['content'] = $page[1]; // "escapedcontent" is like "content", but with escaped HTML characters. $meta['escapedcontent'] = htmlspecialchars($meta['content']); // "created" is the date the file was created. if (!array_key_exists('created', $meta)) { $meta['created'] = qb_created($path); } // "modified" is the modification date of the file. // "modified" and "created" can be overriden in the meta line. if (!array_key_exists('modified', $meta)) { $meta['modified'] = filemtime($filename); } if ($meta['modified'] == '!' || abs($meta['modified'] - $meta['created']) <= QB_OOPSTIME) { // If set to ! or in oops tolerance, simulate an unmodified file. $meta['modified'] = $meta['created']; } // If the two timestamps differ, set "wasmodified". if ($meta['created'] != $meta['modified']) { $meta['wasmodified'] = $meta['modified']; } // Run the template, return the result. return qb_template(QB_TPL_ARTICLE . '.' . $template, $meta); } }