/** * Register mustache view handler * * @param Gdn_Dispatcher $sender */ public function Gdn_Dispatcher_AppStartup_Handler($sender) { $pfolder = $this->getPluginFolder(true); // Register mustache layer as singleton $mustachePath = paths($pfolder, 'library/class.mustache.php'); Gdn::factoryInstall('Mustache', 'Mustache', $mustachePath, Gdn::FactorySingleton); // Register mustache engine $enginePath = paths($pfolder, 'library/class.mustache_engine.php'); Gdn::factoryInstall('Mustache_Engine', 'Mustache_Engine', $enginePath); // Register mustache view handler Gdn::factoryInstall('ViewHandler.mustache', 'Mustache'); }
/** * Code to be run upon enabling the API * * @since 0.1.0 * @access public * @return void */ public function setup() { if (!c("API.Secret")) { saveToConfig("API.Secret", APIAuth::generateUniqueID()); } // Empty fallback array $ApplicationInfo = []; // Load the API application info include paths(PATH_APPLICATIONS, "api/settings/about.php"); $info = val("api", $ApplicationInfo, []); $version = val("Version", $info, "Undefined"); saveToConfig("API.Version", $version); }
function paths(&$validPaths, &$locations, &$paths, $path = '') { foreach ($locations as $location) { if (isset($paths[$path])) { unset($paths[$path]); } $newPath = "{$path}:{$location}"; $parts = explode(':', $newPath); $size = count($parts); if ($size > 2 && !isset($validPaths[$parts[$size - 2]][$parts[$size - 1]])) { continue; } $paths[$newPath] = 0; $newLocations = array_diff($locations, $parts); if (count($newLocations)) { paths($validPaths, $newLocations, $paths, $newPath); } } }
<?php if (!defined("APPLICATION")) { exit; } $path = paths(PATH_APPLICATIONS, "api/library"); // Register API library with the Garden Autoloader Gdn_Autoloader::registerMap(Gdn_Autoloader::MAP_LIBRARY, Gdn_Autoloader::CONTEXT_APPLICATION, $path, ["Extension" => "api"]); // Include Composer autoloader require_once paths($path, "vendors/autoload.php");
/** * Checks if the user is previewing a theme and, if so, updates the default master view. * * @param Gdn_Controller $sender */ public function base_beforeFetchMaster_handler($sender) { $session = Gdn::session(); if (!$session->isValid()) { return; } if (isMobile()) { $theme = htmlspecialchars($session->getPreference('PreviewMobileThemeFolder', '')); } else { $theme = htmlspecialchars($session->getPreference('PreviewThemeFolder', '')); } $isDefaultMaster = $sender->MasterView == 'default' || $sender->MasterView == ''; if ($theme != '' && $isDefaultMaster) { $htmlFile = paths(PATH_THEMES, $theme, 'views', 'default.master.tpl'); if (file_exists($htmlFile)) { $sender->EventArguments['MasterViewPath'] = $htmlFile; } else { // for default theme $sender->EventArguments['MasterViewPath'] = $sender->fetchViewLocation('default.master', '', 'dashboard'); } } }
/** * Resolve relative static resources into full paths * * This method is used to translate CSS, Js and Template relative file lists * into absolute paths. * * Element values should conform to the following format: * * [] => array( * 'FileName' => // filename (relative, absolute, or URL) * 'AppFolder' => // optional application folder to target (default controller app) * ); * * @param array $resourceList * @param string $stub * @param array $options Optional. List of check options. * - 'GlobalLibrary' // Check $Stub/library in global section * - 'StripRoot' // Strip PATH_ROOT from final results * - 'CDNS' // List of external CDN replacements * @param array $checkLocations Optional. List of locations to check. * - 'themes' * - 'plugins' * - 'applications' * - 'global' */ public static function resolveStaticResources($resourceList, $stub, $options = null, $checkLocations = null) { // All locations by default if (!is_array($checkLocations)) { $checkLocations = array('themes', 'plugins', 'applications', 'global'); } // Default options $defaultOptions = array('GlobalLibrary' => true, 'StripRoot' => true, 'CDNS' => array(), 'AutoVersion' => true); if (!is_array($options)) { $options = array(); } $options = array_merge($defaultOptions, $options); // Parse options $checkGlobalLibrary = val('GlobalLibrary', $options); $stripRoot = val('StripRoot', $options); $autoDetectVersion = val('AutoVersion', $options); // See if we're allowing any CDN replacements $CDNs = val('CDNS', $options, array()); // Pre-get controller info $controllerAppFolder = false; $controllerTheme = false; if (Gdn::Controller() instanceof Gdn_Controller) { $controllerAppFolder = Gdn::controller()->ApplicationFolder; $controllerTheme = Gdn::controller()->Theme; } $fileList = array(); foreach ($resourceList as $index => $resourceInfo) { $resourceFile = $resourceInfo['FileName']; $resourceFolder = val('AppFolder', $resourceInfo); $resourceOptions = (array) val('Options', $resourceInfo, false); if ($resourceFile === false) { if (!$resourceOptions) { continue; } $rawCSS = val('Css', $resourceOptions, false); if (!$rawCSS) { continue; } $cssHash = md5($rawCSS); $fileList[$resourceFolder] = array('options' => $resourceOptions); continue; } $skipFileCheck = false; // Resolve CDN resources if (array_key_exists($resourceFile, $CDNs)) { $resourceFile = $CDNs[$resourceFile]; } if (strpos($resourceFile, '//') !== false) { // This is a link to an external file. $skipFileCheck = true; $testPaths = array($resourceFile); } elseif (strpos($resourceFile, '/') === 0) { // A direct path to the file was given. $testPaths = array(paths(PATH_ROOT, $resourceFile)); } elseif (strpos($resourceFile, '~') === 0) { $skipFileCheck = true; $resourceFile = substr($resourceFile, 1); $testPaths = array(paths(PATH_ROOT, $resourceFile)); } else { // Relative path $appFolder = val('AppFolder', $resourceInfo, false); if ($appFolder == '') { $appFolder = $controllerAppFolder; } if ($appFolder == 'false') { $appFolder = false; } // Resources can come from: // - a theme // - an application // - a plugin // - global garden resource-specific folder // - global garden resource-specific library folder $testPaths = array(); // Theme if (in_array('themes', $checkLocations) && $controllerTheme) { // Application-specific theme override if ($appFolder) { $testPaths[] = paths(PATH_THEMES, $controllerTheme, $appFolder, $stub, $resourceFile); } // Garden-wide theme override $testPaths[] = paths(PATH_THEMES, $controllerTheme, $stub, $resourceFile); } // Application or plugin $isPluginFolder = stringBeginsWith(trim($appFolder, '/'), 'plugins/', true, false); if ($isPluginFolder) { $pluginFolder = stringBeginsWith(trim($appFolder, '/'), 'plugins/', true, true); } if (in_array('plugins', $checkLocations) && $isPluginFolder) { // Plugin $testPaths[] = paths(PATH_PLUGINS, $pluginFolder, $stub, $resourceFile); $testPaths[] = paths(PATH_PLUGINS, $pluginFolder, $resourceFile); } if (in_array('applications', $checkLocations) && !$isPluginFolder) { // Application if ($appFolder) { $testPaths[] = paths(PATH_APPLICATIONS, $appFolder, $stub, $resourceFile); } // Dashboard app is added by default if ($appFolder != 'dashboard') { $testPaths[] = paths(PATH_APPLICATIONS, 'dashboard', $stub, $resourceFile); } } if (in_array('global', $checkLocations)) { // Global folder. eg. root/js/ $testPaths[] = paths(PATH_ROOT, $stub, $resourceFile); if ($checkGlobalLibrary) { // Global library folder. eg. root/js/library/ $testPaths[] = paths(PATH_ROOT, $stub, 'library', $resourceFile); } } } // Find the first file that matches the path. $resourcePath = false; if (!$skipFileCheck) { foreach ($testPaths as $glob) { $paths = safeGlob($glob); if (is_array($paths) && count($paths) > 0) { $resourcePath = $paths[0]; break; } } // Get version $version = val('Version', $resourceInfo, false); // If a path was matched, make sure it has a version if ($resourcePath && !$version && $autoDetectVersion) { // Theme file if (!$version && preg_match('`themes/([^/]+)/`i', $resourcePath, $matches)) { $themeName = $matches[1]; $themeInfo = Gdn::themeManager()->getThemeInfo($themeName); $version = val('Version', $themeInfo); $versionSource = "theme {$themeName}"; } // Plugin file if (!$version && preg_match('`plugins/([^/]+)/`i', $resourcePath, $matches)) { $pluginName = $matches[1]; $pluginInfo = Gdn::pluginManager()->getPluginInfo($pluginName, Gdn_PluginManager::ACCESS_PLUGINNAME); $version = val('Version', $pluginInfo); $versionSource = "plugin {$pluginName}"; } // Application file if (!$version && preg_match('`applications/([^/]+)/`i', $resourcePath, $matches)) { $applicationName = $matches[1]; $applicationInfo = Gdn::applicationManager()->getApplicationInfo($applicationName); $version = val('Version', $applicationInfo); $versionSource = "app {$applicationName}"; } } } else { $version = null; } // Global file if (!$version) { $version = APPLICATION_VERSION; } // If a path was succesfully matched if ($resourcePath !== false || $skipFileCheck) { // We enact SkipFileCheck for virtual paths, targeting controllers // perhaps, or full URLs from the CDN resolver. if ($skipFileCheck) { $resourcePath = array_pop($testPaths); } // Strip PATH_ROOT from absolute path $resourceResolved = $resourcePath; if ($stripRoot) { $resourceResolved = str_replace(array(PATH_ROOT, DS), array('', '/'), $resourcePath); } // Bring options into response structure $resource = array('path' => $resourcePath); $resourceOptions = (array) val('Options', $resourceInfo, array()); touchValue('version', $resource, $version); if ($resourceOptions) { touchValue('options', $resource, $resourceOptions); } $fileList[$resourceResolved] = $resource; } } return $fileList; }
function search() { if (strcmp('4.1.0', phpversion()) > 0) { die("Error: PHP version 4.1.0 or above required!"); } $paths = paths(); $files = array(); $j = 0; for ($i = 0; $i < sizeof($paths); $i++) { if (!($f = @fopen($paths[$i] . "search.idx", "rb"))) { die("Error: Search index file could NOT be opened!"); continue; } $files[$j++] = $f; if (readHeader($f) != "DOXS") { die("Error: Header of index file is invalid!"); } } $query = ""; if (array_key_exists("query", $_GET)) { $query = $_GET["query"]; } //end_form($query); echo " \n<div class=\"searchresults\">\n"; $results = array(); $requiredWords = array(); $forbiddenWords = array(); $foundWords = array(); $word = strtok($query, " "); while ($word) { if ($word[0] == '+') { $word = substr($word, 1); $requiredWords[] = $word; } if ($word[0] == '-') { $word = substr($word, 1); $forbiddenWords[] = $word; } if (!in_array($word, $foundWords)) { $foundWords[] = $word; for ($i = 0; $i < sizeof($files); $i++) { do_search($paths[$i], $files[$i], strtolower($word), $results); } } $word = strtok(" "); } $docs = array(); combine_results($results, $docs); // filter out documents with forbidden word or that do not contain // required words $filteredDocs = filter_results($docs, $requiredWords, $forbiddenWords); // sort the results based on rank $sorted = array(); sort_results($filteredDocs, $sorted); // report results to the user report_results($sorted); echo "</div>\n"; foreach ($files as $file) { fclose($file); } }
/** * Map a resource to its corresponding controller * * @since 0.1.0 * @access public * @param array $path URI path array * @param string $method HTTP method * @param array $data Request arguments * @return array Dispatch instruction for Garden. * @static */ public static function map($resource, $class, $path, $method, $data) { $router = new AltoRouter(); $router->setBasePath("/api"); $endpoints = $class->endpoints($data); if ($method == "options") { $supports = strtoupper(implode(", ", $class::supports())); $documentation = []; foreach ($endpoints as $method => $endpoints) { foreach ($endpoints as $endpoint => $data) { $documentation[$method][] = paths($resource, $endpoint); } } $documentation = base64_encode(json_encode($documentation)); return ["application" => "API", "controller" => "API", "method" => "options", "arguments" => [$supports, $documentation], "authenticate" => false]; } else { // Register all endpoints in the router foreach ($endpoints as $method => $endpoints) { foreach ($endpoints as $endpoint => $data) { $endpoint = "/" . $resource . rtrim($endpoint, "/"); $router->map($method, $endpoint, $data); } } $match = $router->match("/" . rtrim(join("/", $path), "/")); if (!$match) { throw new Exception(t("API.Error.MethodNotAllowed"), 405); } $target = val("target", $match); $arguments = array_merge(val("params", $match, []), val("arguments", $target, [])); return ["application" => val("application", $target, false), "controller" => val("controller", $target), "method" => val("method", $target, "index"), "authenticate" => val("authenticate", $target), "arguments" => $arguments]; } }