/** * When visiting any page on the site, check if the user is already logged in, * or they are visiting a page that is allowed when logged out. Otherwise, * redirect to the login page. If visiting the login page, check the browser * supports cookies. */ public function check() { $uri = new URI(); // Skip check when accessing the data services, as it is redundant but would slow the services down. // Also no need to login when running the scheduled tasks. if ($uri->segment(1) == 'services' || $uri->segment(1) == 'scheduled_tasks') { return; } // check for setup request // if ($uri->segment(1) == 'setup_check') { // get kohana paths // $ipaths = Kohana::include_paths(); // check if indicia_setup module folder exists // clearstatcache(); foreach ($ipaths as $path) { if (preg_match("/indicia_setup/", $path) && file_exists($path)) { return; } } } // Always logged in $auth = new Auth(); if (!$auth->logged_in() and !$auth->auto_login() and $uri->segment(1) != 'login' and $uri->segment(1) != 'logout' and $uri->segment(1) != 'new_password' and $uri->segment(1) != 'forgotten_password') { $_SESSION['requested_page'] = $uri->string(); url::redirect('login'); } else { if ($auth->logged_in() and is_null($_SESSION['auth_user']->password) and $uri->segment(1) != 'new_password' and $uri->segment(1) != 'logout' and $uri->segment(1) != 'setup_check') { $_SESSION['requested_page'] = $uri->string(); url::redirect('new_password'); } } }
public static function instance() { if (!Kotwig::$instance) { Kotwig::$instance = new static(); // Load Twig configuration Kotwig::$instance->config = Kohana::$config->load('kotwig'); // Create the the loader $views = Kohana::include_paths(); $look_in = array(); foreach ($views as $key => $view) { $dir = $view . Kotwig::$instance->config->templates; if (is_dir($dir)) { $look_in[] = $dir; } } $loader = new Twig_Loader_Filesystem($look_in); // Set up Twig Kotwig::$instance->twig = new Twig_Environment($loader, Kotwig::$instance->config->environment); foreach (Kotwig::$instance->config->extensions as $extension) { // Load extensions Kotwig::$instance->twig->addExtension(new $extension()); } foreach (Kotwig::$instance->config->globals as $global => $object) { // Load globals Kotwig::$instance->twig->addGlobal($global, $object); } foreach (Kotwig::$instance->config->filters as $filter => $object) { // Load filters Kotwig::$instance->twig->addFilter($filter, $object); } } return Kotwig::$instance; }
public function __call($method, $args) { // Concat all the arguments into a filepath array_unshift($args, $method); $path = join('/', $args); // Strip the extension from the filepath $path = substr($path, 0, -strlen($this->extension) - 1); // Search for file using cascading file system $filename = Kohana::find_file($this->directory, $path, FALSE, $this->extension); if (!$filename) { Event::run('system.404'); } // Add all the module JavaScript directories to the include paths list foreach (Kohana::include_paths() as $path) { $this->include_paths[] = $path . $this->directory; } // Load file, along with all dependencies $preprocessor = new JavaScriptPreprocessor($this->include_paths, $this->vars); $output = $preprocessor->requires($filename); // Compress JavaScript, if desired if ($this->compress) { $output = $this->compress($output, $this->compress_config); } echo $output; }
/** * Find all plugins defined in your Kohana codebase */ public function find_plugins() { $paths = Kohana::include_paths(); foreach ($paths as $path) { $dir = $path . self::$plugins_dir; //if there's no plugin dir skip to the next path if (!file_exists($dir)) { continue; } // Instantiate a new directory iterator object $directory = new DirectoryIterator($dir); if ($directory->isDir()) { foreach ($directory as $plugin) { // Is directory? if ($plugin->isDir() && !$plugin->isDot()) { // if there's no plugin.php in this folder, ignore it if (!file_exists($plugin->getPath() . DIRECTORY_SEPARATOR . $directory->getBasename() . DIRECTORY_SEPARATOR . 'plugin.php')) { continue; } // Store plugin in our pool self::$plugins_pool[$plugin->getFilename()]['plugin'] = ucfirst($plugin->getFilename()); self::$plugins_pool[$plugin->getFilename()]['path'] = $plugin->getPath() . DIRECTORY_SEPARATOR . $directory->getBasename(); } } } } return $this; }
public function test_get_all_class_names_considers_class_paths_from_config() { Kohana::$config->load('doctrine')->set('entity_paths', array('Some/Entity/Path' => TRUE, 'Other/Entities' => TRUE)); $reader = new SimpleAnnotationReader(); $reader->addNamespace('Doctrine\\ORM\\Mapping'); $driver = new Doctrine_KohanaAnnotationDriver(new CachedReader($reader, new ArrayCache()), Kohana::include_paths()); $this->assertEquals(array('Some_Entity_Path_Entity', 'Other_Entities_Entity'), $driver->getAllClassNames()); }
/** * Execute the task and compile the LESS files * * @param array $params the passed command options * * @throws Exception * @return void */ protected function _execute(array $params) { // Prepare the options for recess $recess_opts = array(); // Create the list of valid assets/less paths found in the CFS and format them as recess arguments $paths = Kohana::include_paths(); foreach ($paths as $path) { $path .= 'assets/less'; if (file_exists($path)) { $recess_opts[] = '--includePath ' . escapeshellarg($path); } } // Add paths for fontawesome and twitter bootstrap // Note that these are deliberately set at organisation level to namespace the less files below $recess_opts[] = '--includePath ' . escapeshellarg(realpath($params['vendor-path'] . '/fortawesome')); $recess_opts[] = '--includePath ' . escapeshellarg(realpath($params['vendor-path'] . '/twbs')); // Determine whether to lint or compile, and whether the output should be compressed if ($params['lint-only']) { $output = ''; } else { $recess_opts[] = '--compile'; // Find the path for the output file $output = ' > ' . escapeshellarg($params['public-path'] . '/css/' . $params['css-output'] . '.css'); if (!$params['no-compress']) { $recess_opts[] = '--compress'; } } // Find the input file to use $input = Kohana::find_file('assets/less', $params['less-input'], 'less'); if (!$input) { throw new Exception("Could not find a valid input file source for {$params['less-input']}"); } // Build and log the recess command $command = 'recess ' . implode(' ', $recess_opts) . ' ' . escapeshellarg($input) . $output; Minion_CLI::write(Minion_CLI::color('Preparing to execute recess with following command', 'green')); Minion_CLI::write(Minion_CLI::color('> ' . $command, 'green')); $loop = $params['loop-after'] > 0; do { Minion_CLI::write(Minion_CLI::color('Building CSS', 'light_gray')); // Execute and check the result system($command, $exit_code); if ($exit_code != 0) { throw new Exception("Command '" . $command . '" failed with exit code ' . $exit_code); } Minion_CLI::write(Minion_CLI::color('Compiled ' . $input . ' to ' . $output, 'green')); if ($loop) { sleep($params['loop-after']); } } while ($loop); }
/** * A list of item commands * @return array */ public function commands() { $commands = Item::list_commands(); $list_c = array(); foreach ($commands as $cmd) { $name = str_replace(Kohana::include_paths(), '', $cmd); $name = str_replace(array('\\', '/'), '_', $name); $value = 'Item_Command_' . $name; $command = new $value(); if ($command->is_default() == FALSE) { $list_c[] = array('name' => str_replace('_', ' - ', $name), 'value' => $value); } } return $list_c; }
public static function autoload($class) { //Make use of Kohanas transparent file structure foreach (Kohana::include_paths() as $path) { $orig = $path; $path1 = $path . "models" . DIRECTORY_SEPARATOR . strtolower($class) . '.php'; $path2 = $path . "models/generated" . DIRECTORY_SEPARATOR . strtolower($class) . '.php'; //^^ Bit of a hack to allow for generated doctrine models //Only add the directory if it is valid if (file_exists($path1)) { include $path1; return true; } else { if (file_exists($path2)) { include $path2; return true; } } } }
public function __call($method, $args) { if (count($segments = $this->uri->segment_array(1)) > 1) { // Find directory (type) and filename $type = array_shift($segments); $file = implode('/', $segments); if (substr($file, -strlen(EXT)) === EXT) { // Remove extension $file = substr($file, 0, -strlen(EXT)); } if ($type === 'config') { if ($file === 'config') { // This file can only exist in one location $file = APPPATH . $type . '/config' . EXT; } else { foreach (array_reverse(Kohana::include_paths()) as $path) { if (is_file($path . $type . '/' . $file . EXT)) { // Found the file $file = $path . $type . '/' . $file . EXT; break; } } } } else { // Get absolute path to file $file = Kohana::find_file($type, $file); } if (in_array($type, Kodoc::get_types())) { // Load Kodoc $this->kodoc = new Kodoc($type, $file); // Set the title $this->template->title = implode('/', $this->uri->segment_array(1)); // Load documentation for this file $this->template->content = new View('kodoc/documentation'); // Exit this method return; } } // Nothing to document url::redirect('kodoc'); }
public function __construct() { Event::add('system.routing', array($this, 'install')); Event::add('system.display', array($this, 'render')); $modules = Kohana::config('core.modules'); if (!is_dir(DOCROOT . 'installation')) { // Delete install app from Config::$include_paths unset($modules[0]); } $installed_mods = array(); foreach (Module_Model::factory()->find(ALL) as $module) { /** * @todo Validate the modules via /config/[module].php or something */ is_dir(MODPATH . $module->name) and $installed_mods[] = MODPATH . $module->name; } // Set module include paths Kohana::config_set('core.modules', array_merge($installed_mods, $modules)); // Re-process the include paths Kohana::include_paths(TRUE); Kohana::log('debug', 'Hanami Core Hook initialized'); }
private static function _remove_from_path($module) { $available = (array) static::$available; $kohana_modules = Kohana::modules(); if (in_array($module, array_keys($available)) && isset($available[$module])) { $module = $available[$module]; if (($key = array_search($module->path, $kohana_modules)) !== false) { unset($kohana_modules[$key]); $kohana_modules = array_values($kohana_modules); // reindex } Kohana::modules($kohana_modules); Kohana::include_paths(true); } }
/** * Lists all files and directories in a resource path. * * @param string directory to search * @param boolean list all files to the maximum depth? * @param string full path to search (used for recursion, *never* set this manually) * @return array filenames and directories */ public static function list_files($directory, $recursive = FALSE, $path = FALSE) { $files = array(); if ($path === FALSE) { $paths = array_reverse(Kohana::include_paths()); foreach ($paths as $path) { // Recursively get and merge all files $files = array_merge($files, self::list_files($directory, $recursive, $path . $directory)); } } else { $path = rtrim($path, '/') . '/'; if (is_readable($path)) { $items = (array) glob($path . '*'); foreach ($items as $index => $item) { $files[] = $item = str_replace('\\', '/', $item); // Handle recursion if (is_dir($item) and $recursive == TRUE) { // Filename should only be the basename $item = pathinfo($item, PATHINFO_BASENAME); // Append sub-directory search $files = array_merge($files, self::list_files($directory, TRUE, $path . $item)); } } } } return $files; }
/** * Router setup routine. Automatically called during Kohana setup process. * * @return void */ public static function setup() { if (!empty($_SERVER['QUERY_STRING'])) { // Set the query string to the current query string Router::$query_string = '?' . trim($_SERVER['QUERY_STRING'], '&/'); } if (Router::$routes === NULL) { // Load routes Router::$routes = Kohana::config('routes'); } // Default route status $default_route = FALSE; if (Router::$current_uri === '') { // Make sure the default route is set if (!isset(Router::$routes['_default'])) { throw new Kohana_Exception('core.no_default_route'); } // Use the default route when no segments exist Router::$current_uri = Router::$routes['_default']; // Default route is in use $default_route = TRUE; } // Make sure the URL is not tainted with HTML characters Router::$current_uri = html::specialchars(Router::$current_uri, FALSE); // Remove all dot-paths from the URI, they are not valid Router::$current_uri = preg_replace('#\\.[\\s./]*/#', '', Router::$current_uri); // At this point segments, rsegments, and current URI are all the same Router::$segments = Router::$rsegments = Router::$current_uri = trim(Router::$current_uri, '/'); // Set the complete URI Router::$complete_uri = Router::$current_uri . Router::$query_string; // Explode the segments by slashes Router::$segments = ($default_route === TRUE or Router::$segments === '') ? array() : explode('/', Router::$segments); if ($default_route === FALSE and count(Router::$routes) > 1) { // Custom routing Router::$rsegments = Router::routed_uri(Router::$current_uri); } // The routed URI is now complete Router::$routed_uri = Router::$rsegments; // Routed segments will never be empty Router::$rsegments = explode('/', Router::$rsegments); // Prepare to find the controller $controller_path = ''; $method_segment = NULL; // Paths to search $paths = Kohana::include_paths(); foreach (Router::$rsegments as $key => $segment) { // Add the segment to the search path $controller_path .= $segment; $found = FALSE; foreach ($paths as $dir) { // Search within controllers only $dir .= 'controllers/'; if (is_dir($dir . $controller_path) or is_file($dir . $controller_path . EXT)) { // Valid path $found = TRUE; // The controller must be a file that exists with the search path if ($c = str_replace('\\', '/', realpath($dir . $controller_path . EXT)) and is_file($c) and strpos($c, $dir) === 0) { // Set controller name Router::$controller = $segment; // Change controller path Router::$controller_path = $c; // Set the method segment $method_segment = $key + 1; // Stop searching break; } } } if ($found === FALSE) { // Maximum depth has been reached, stop searching break; } // Add another slash $controller_path .= '/'; } if ($method_segment !== NULL and isset(Router::$rsegments[$method_segment])) { // Set method Router::$method = Router::$rsegments[$method_segment]; if (isset(Router::$rsegments[$method_segment + 1])) { // Set arguments Router::$arguments = array_slice(Router::$rsegments, $method_segment + 1); } } // Last chance to set routing before a 404 is triggered Event::run('system.post_routing'); if (Router::$controller === NULL) { // No controller was found, so no page can be rendered Event::run('system.404'); } }
protected static function _list_classes($files = NULL) { if (is_null($files)) { // Remove core module paths form search $loaded_modules = Kohana::modules(); $exclude_modules = array('database', 'orm', 'sprig', 'auth', 'sprig-auth', 'userguide', 'image', 'codebench', 'unittest', 'pagination'); $paths = Kohana::include_paths(); // Remove known core module paths foreach ($loaded_modules as $module => $path) { if (in_array($module, $exclude_modules)) { unset($paths[array_search($path . DIRECTORY_SEPARATOR, $paths)]); } } // Remove system path unset($paths[array_search(SYSPATH, $paths)]); $files = Kohana::list_files('classes', $paths); } $classes = array(); foreach ($files as $name => $path) { if (is_array($path)) { $classes = array_merge($classes, self::_list_classes($path)); } else { // Strip 'classes/' off start $name = substr($name, 8); // Strip '.php' off end $name = substr($name, 0, 0 - strlen(EXT)); // Convert to class name $classes[] = str_replace(DIRECTORY_SEPARATOR, '_', $name); } } return $classes; }
/** * Create an instance of the Kohana annotation driver, which supports loading model files from across the CFS. Also * handles registering the Doctrine annotations in the Annotation registry - which would normally be handled by * Doctrine\ORM\Configuration::newDefaultAnnotationDriver. * * @return Doctrine_KohanaAnnotationDriver * @see Doctrine\ORM\Configuration::newDefaultAnnotationDriver */ protected function create_annotation_driver() { $config = $this->config->load('doctrine'); // Register the Doctrine annotations $vendor_path = $config->get('composer_vendor_path'); AnnotationRegistry::registerFile($vendor_path . 'doctrine/orm/lib/Doctrine/ORM/Mapping/Driver/DoctrineAnnotations.php'); if ($config->get('use_simple_annotation_reader')) { // Register the ORM Annotations in the AnnotationReader $reader = new SimpleAnnotationReader(); $reader->addNamespace('Doctrine\\ORM\\Mapping'); } else { $reader = new AnnotationReader(); } $cachedReader = new CachedReader($reader, new ArrayCache()); return new Doctrine_KohanaAnnotationDriver($cachedReader, Kohana::include_paths()); }
/** * * @return array */ public static function find_all() { $class = get_called_class(); $object = new $class(); $files = array(); $paths = Kohana::include_paths(); $found_files = Kohana::list_files($object->_folder, $paths); foreach ($found_files as $file) { if (is_array($file) or strpos($file, EXT) === FALSE) { continue; } $files[] = new $class($file); } return $files; }
public static function module_exists($module_name = null) { if (is_null($module_name)) { return null; } foreach (Kohana::include_paths() as $value) { if (substr_count($value, $module_name)) { if (is_dir($value)) { return true; } } } return false; }
private static function configs() { if (Kohana::config('debug_toolbar.skip_configs') === TRUE) { return array(); } $inc_paths = Kohana::include_paths(); $configs = array(); foreach ($inc_paths as $inc_path) { foreach (glob($inc_path . '/config/*.php') as $filename) { $filename = pathinfo($filename, PATHINFO_FILENAME); if (in_array($filename, (array) Kohana::config('debug_toolbar.skip_configs'))) { continue; } if (!isset($configs[$filename])) { $configs[$filename] = Kohana::Config($filename); } } } return $configs; }
/** * Tests Kohana::include_paths() * * The include paths must contain the apppath and syspath * @test * @covers Kohana::include_paths */ public function test_include_paths() { $include_paths = Kohana::include_paths(); $modules = Kohana::modules(); $this->assertInternalType('array', $include_paths); // We must have at least 2 items in include paths (APP / SYS) $this->assertGreaterThan(2, count($include_paths)); // Make sure said paths are in the include paths // And make sure they're in the correct positions $this->assertSame(APPPATH, reset($include_paths)); $this->assertSame(SYSPATH, end($include_paths)); foreach ($modules as $module) { $this->assertContains($module, $include_paths); } }
public static function autoloadLibraries($class) { if (class_exists($class, FALSE)) { return TRUE; } // Convert underscores to directories $class = str_replace('_', '/', $class); // Go through all known Kohana paths and look for libraries/class foreach (Kohana::include_paths() as $path) { if (is_file($path . 'libraries/' . $class . '.php')) { require_once $path . 'libraries/' . $class . '.php'; return TRUE; } } return FALSE; }
/** * Tests Kohana::include_paths() * * @test * @dataProvider providerIncludePaths * @covers Kohana::include_paths * @param boolean $expected Input for Kohana::include_paths */ function testIncludePaths($expected) { $this->assertEquals($expected, Kohana::include_paths()); }
/** * Changes the currently enabled modules. Module paths may be relative * or absolute, but must point to a directory: * * Kohana::modules(array('modules/foo', MODPATH.'bar')); * * @param array module paths * @return void */ public static function modules($modules = NULL) { if ($modules === NULL) { return self::$modules; } // Start a new list of include paths, APPPATH first $include_paths = array(APPPATH); foreach ($modules as $name => $path) { if (is_dir($path)) { // Get the absolute path to the module $path = realpath($path); if (Kohana::$is_windows === TRUE) { // Remove backslashes $path = str_replace('\\', '/', $path); } // Add the module to include paths $include_paths[] = $path . '/'; } else { // This module is invalid, remove it unset($modules[$name]); } } // Finish the include paths by adding SYSPATH $include_paths[] = SYSPATH; // Set the new include paths self::$include_paths = $include_paths; // Set the current module list return self::$modules = $modules; }
/** * Sets a new value to the configuration * * @param string key * @param mixed value * @return bool * @access public */ public function set($key, $value) { // Do this to make sure that the config array is already loaded $this->get($key); if (substr($key, 0, 7) === 'routes.') { // Routes cannot contain sub keys due to possible dots in regex $keys = explode('.', $key, 2); } else { // Convert dot-noted key string to an array $keys = explode('.', $key); } // Used for recursion $conf =& $this->config; $last = count($keys) - 1; foreach ($keys as $i => $k) { if ($i === $last) { $conf[$k] = $value; } else { $conf =& $conf[$k]; } } if (substr($key, 0, 12) === 'core.modules') { // Reprocess the include paths Kohana::include_paths(TRUE); } // Set config to changed return $this->changed = TRUE; }
/** * Load all item command classes * @return array */ public static function list_commands() { static $commands = NULL; if ($commands == NULL) { // Include paths must be searched in reverse $paths = array_reverse(Kohana::list_files('classes/Item/Command/')); // Array of class names that have been found $found = array(); foreach ($paths as $files) { $replacements = array_merge(Kohana::include_paths(), array('classes' . DIRECTORY_SEPARATOR . 'Item' . DIRECTORY_SEPARATOR . 'Command' . DIRECTORY_SEPARATOR, '.php')); if (is_array($files)) { foreach ($files as $file) { foreach ($replacements as $replace) { $file = str_replace($replace, '', $file); } $found[] = $file; } } } $commands = $found; } return $commands; }
private static function _remove_from_path($module_name) { $config = Kohana_Config::instance(); $kohana_modules = $config->get("core.modules"); if (($key = array_search(MODPATH . $module_name, $kohana_modules)) !== false) { unset($kohana_modules[$key]); $kohana_modules = array_values($kohana_modules); // reindex } $config->set("core.modules", $kohana_modules); Kohana::include_paths(true); }
/** * Install a module. This will call <module>_installer::install(), which is responsible for * creating database tables, setting module variables and calling module::set_version(). * Note that after installing, the module must be activated before it is available for use. * @param string $module_name */ static function install($module_name) { $kohana_modules = Kohana::config("core.modules"); array_unshift($kohana_modules, MODPATH . $module_name); Kohana::config_set("core.modules", $kohana_modules); // Rebuild the include path so the module installer can benefit from auto loading Kohana::include_paths(true); $installer_class = "{$module_name}_installer"; if (method_exists($installer_class, "install")) { call_user_func_array(array($installer_class, "install"), array()); } else { module::set_version($module_name, 1); } module::load_modules(); // Now the module is installed but inactive, so don't leave it in the active path array_shift($kohana_modules); Kohana::config_set("core.modules", $kohana_modules); log::success("module", t("Installed module %module_name", array("module_name" => $module_name))); }
/** * Get all include paths. APPPATH is the first path, followed by module * paths in the order they are configured, follow by the SYSPATH. * * @param boolean re-process the include paths * @return array */ public static function include_paths($process = FALSE) { if ($process === TRUE) { // Add APPPATH as the first path self::$include_paths = array(APPPATH); foreach (self::$configuration['core']['modules'] as $path) { if ($path = str_replace('\\', '/', realpath($path))) { // Add a valid path self::$include_paths[] = $path . '/'; } } // Add SYSPATH as the last path self::$include_paths[] = SYSPATH; } return self::$include_paths; }
/** * Get all include paths. APPPATH is the first path, followed by module * paths in the order they are configured, follow by the SYSPATH. * * @param boolean re-process the include paths * @return array */ public static function include_paths($process = FALSE) { if ($process === TRUE) { // Add APPPATH as the first path self::$include_paths = array(APPPATH); foreach (self::$configuration['core']['modules'] as $path) { if ($path = str_replace('\\', '/', realpath($path))) { // Add a valid path self::$include_paths[] = $path . '/'; } } // Add SYSPATH as the last path self::$include_paths[] = SYSPATH; // Local fix for Kohana Ticket:2276 self::$internal_cache['find_file_paths'] = array(); if (!isset(self::$write_cache['find_file_paths'])) { // Write cache at shutdown self::$write_cache['find_file_paths'] = TRUE; } } return self::$include_paths; }