/** * Return all Fuel, or specified ProcessWire API variable, or NULL if it doesn't exist. * * Same as Wire::getFuel($name) and Wire::getAllFuel(); * When a $name is specified, this function is identical to the wire() function. * Both functions exist more for consistent naming depending on usage. * * @param string $name If ommitted, returns a Fuel object with references to all the fuel. * @return mixed Fuel value if available, NULL if not. * */ function fuel($name = '') { if (!$name) { return Wire::getAllFuel(); } return Wire::getFuel($name); }
/** * Include a PHP file passing it all API variables and optionally your own specified variables * * This is the same as PHP's include() function except for the following: * - It receives all API variables and optionally your custom variables * - If your filename is not absolute, it doesn't look in PHP's include path, only in the current dir. * - It only allows including files that are part of the PW installation: templates, core modules or site modules * - It will assume a ".php" extension if filename has no extension. * * Note this function produced direct output. To retrieve output as a return value, use the * wireTemplateFile function instead. * * @param $filename * @param array $vars Optional variables you want to hand to the include (associative array) * @param array $options Array of options to modify behavior: * - func: Function to use: include, include_once, require or require_once (default=include) * - autoExtension: Extension to assume when no ext in filename, make blank for no auto assumption (default=php) * - allowedPaths: Array of paths include files are allowed from. Note current dir is always allowed. * @return bool Returns true * @throws WireException if file doesn't exist or is not allowed * */ function wireIncludeFile($filename, array $vars = array(), array $options = array()) { $paths = wire('config')->paths; $defaults = array('func' => 'include', 'autoExtension' => 'php', 'allowedPaths' => array($paths->templates, $paths->adminTemplates, $paths->modules, $paths->siteModules)); $options = array_merge($defaults, $options); $filename = trim($filename); if (DIRECTORY_SEPARATOR != '/') { $filename = str_replace(DIRECTORY_SEPARATOR, '/', $filename); } // add .php extension if filename doesn't already have an extension if ($options['autoExtension'] && !strrpos(basename($filename), '.')) { $filename .= "." . $options['autoExtension']; } if (strpos($filename, '..') !== false) { // if backtrack/relative components, convert to real path $_filename = $filename; $filename = realpath($filename); if ($filename === false) { throw new WireException("File does not exist: {$_filename}"); } } if (strpos($filename, '//') !== false) { throw new WireException("File is not allowed (double-slash): {$filename}"); } if (strpos($filename, './') !== 0) { // file does not specify "current directory" $slashPos = strpos($filename, '/'); // If no absolute path specified, ensure it only looks in current directory if ($slashPos !== 0 && strpos($filename, ':/') === false) { $filename = "./{$filename}"; } } if (strpos($filename, '/') === 0 || strpos($filename, ':/') !== false) { // absolute path, make sure it's part of PW's installation $allowed = false; foreach ($options['allowedPaths'] as $path) { if (strpos($filename, $path) === 0) { $allowed = true; } } if (!$allowed) { throw new WireException("File is not in an allowed path: {$filename}"); } } if (!file_exists($filename)) { throw new WireException("File does not exist: {$filename}"); } // extract all API vars $fuel = array_merge(Wire::getAllFuel()->getArray(), $vars); extract($fuel); // include the file $func = $options['func']; if ($func == 'require') { require $filename; } else { if ($func == 'require_once') { require_once $filename; } else { if ($func == 'include_once') { include_once $filename; } else { include $filename; } } } return true; }