diff options
Diffstat (limited to 'Zotlabs')
-rw-r--r-- | Zotlabs/Extend/Hook.php | 76 | ||||
-rw-r--r-- | Zotlabs/Web/Router.php | 44 |
2 files changed, 107 insertions, 13 deletions
diff --git a/Zotlabs/Extend/Hook.php b/Zotlabs/Extend/Hook.php new file mode 100644 index 000000000..836b29db8 --- /dev/null +++ b/Zotlabs/Extend/Hook.php @@ -0,0 +1,76 @@ +<?php + +namespace Zotlabs\Extend; + + +class Hook { + + static public function register($hook,$file,$function,$version = 1,$priority = 0) { + $r = q("SELECT * FROM `hook` WHERE `hook` = '%s' AND `file` = '%s' AND `function` = '%s' and priority = %d and hook_version = %d LIMIT 1", + dbesc($hook), + dbesc($file), + dbesc($function), + intval($priority), + intval($version) + ); + if($r) + return true; + + $r = q("INSERT INTO `hook` (`hook`, `file`, `function`, `priority`, `hook_version`) VALUES ( '%s', '%s', '%s', %d, %d )", + dbesc($hook), + dbesc($file), + dbesc($function), + intval($priority), + intval($version) + ); + + return $r; + } + + static public function unregister($hook,$file,$function,$version = 1,$priority = 0) { + $r = q("DELETE FROM hook WHERE hook = '%s' AND `file` = '%s' AND `function` = '%s' and priority = %d and hook_version = %d", + dbesc($hook), + dbesc($file), + dbesc($function), + intval($priority), + intval($version) + ); + + return $r; + } + + + /** + * @brief Inserts a hook into a page request. + * + * Insert a short-lived hook into the running page request. + * Hooks are normally persistent so that they can be called + * across asynchronous processes such as delivery and poll + * processes. + * + * insert_hook lets you attach a hook callback immediately + * which will not persist beyond the life of this page request + * or the current process. + * + * @param string $hook + * name of hook to attach callback + * @param string $fn + * function name of callback handler + * @param int $version + * hook interface version, 0 uses two callback params, 1 uses one callback param + * @param int $priority + * currently not implemented in this function, would require the hook array to be resorted + */ + + static public function insert($hook, $fn, $version = 0, $priority = 0) { + + if(! is_array(App::$hooks)) + App::$hooks = array(); + + if(! array_key_exists($hook, App::$hooks)) + App::$hooks[$hook] = array(); + + App::$hooks[$hook][] = array('', $fn, $priority, $version); + } + +}
\ No newline at end of file diff --git a/Zotlabs/Web/Router.php b/Zotlabs/Web/Router.php index 6376f7697..e6733ffdb 100644 --- a/Zotlabs/Web/Router.php +++ b/Zotlabs/Web/Router.php @@ -16,7 +16,7 @@ class Router { * * App::$argv[0] is our module name. Let's call it 'foo'. We will load the * Zotlabs/Module/Foo.php (object) or file mod/foo.php (procedural) - * and use it for handling our URL request. + * and use it for handling our URL request to 'https://ourgreatwebsite.something/foo' . * The module file contains a few functions that we call in various circumstances * and in the following order: * @@ -24,7 +24,7 @@ class Router { * class Foo extends Zotlabs\Web\Controller { * function init() { init function } * function post() { post function } - * function get() { nomral page function } + * function get() { normal page function } * } * * Procedual interface: @@ -38,6 +38,7 @@ class Router { */ $module = \App::$module; + $modname = "Zotlabs\\Module\\" . ucfirst($module); if(strlen($module)) { @@ -50,8 +51,13 @@ class Router { if(is_array(\App::$plugins) && in_array($module,\App::$plugins) && file_exists("addon/{$module}/{$module}.php")) { include_once("addon/{$module}/{$module}.php"); - if(function_exists($module . '_module')) + if(class_exists($modname)) { + $this->controller = new $modname; \App::$module_loaded = true; + } + elseif(function_exists($module . '_module')) { + \App::$module_loaded = true; + } } if((strpos($module,'admin') === 0) && (! is_site_admin())) { @@ -62,18 +68,27 @@ class Router { /** * If the site has a custom module to over-ride the standard module, use it. - * Otherwise, look for the standard program module in the 'mod' directory + * Otherwise, look for the standard program module */ if(! (\App::$module_loaded)) { try { - $modname = "Zotlabs\\Module\\" . ucfirst($module); - $filename = 'Zotlabs/Module/'. ucfirst($module). '.php'; + $filename = 'Zotlabs/SiteModule/'. ucfirst($module). '.php'; if(file_exists($filename)) { + // This won't be picked up by the autoloader, so load it explicitly + require_once($filename); $this->controller = new $modname; \App::$module_loaded = true; } - else throw new \Exception('Module not found'); + else { + $filename = 'Zotlabs/Module/'. ucfirst($module). '.php'; + if(file_exists($filename)) { + $this->controller = new $modname; + \App::$module_loaded = true; + } + } + if(! \App::$module_loaded) + throw new \Exception('Module not found'); } catch(\Exception $e) { if(file_exists("mod/site/{$module}.php")) { @@ -84,22 +99,23 @@ class Router { include_once("mod/{$module}.php"); \App::$module_loaded = true; } - else logger("mod/{$module}.php not found."); } } - - + /** - * This provides a place for plugins to register module handlers which don't otherwise exist on the system. + * This provides a place for plugins to register module handlers which don't otherwise exist + * on the system, or to completely over-ride an existing module. * If the plugin sets 'installed' to true we won't throw a 404 error for the specified module even if * there is no specific module file or matching plugin name. * The plugin should catch at least one of the module hooks for this URL. */ - $x = array('module' => $module, 'installed' => false); + $x = array('module' => $module, 'installed' => \App::$module_loaded, 'controller' => $this->controller); call_hooks('module_loaded', $x); - if($x['installed']) + if($x['installed']) { \App::$module_loaded = true; + $this->controller = $x['controller']; + } /** * The URL provided does not resolve to a valid module. @@ -119,6 +135,8 @@ class Router { killme(); } + logger("Module {$module} not found.", LOGGER_DEBUG, LOG_WARNING); + if((x($_SERVER, 'QUERY_STRING')) && ($_SERVER['QUERY_STRING'] === 'q=internal_error.html') && \App::$config['system']['dreamhost_error_hack']) { logger('index.php: dreamhost_error_hack invoked. Original URI =' . $_SERVER['REQUEST_URI']); goaway(z_root() . $_SERVER['REQUEST_URI']); |