From 86f69ca396572ca4d7668a33e84cb4f3b523fc4e Mon Sep 17 00:00:00 2001 From: Marien Fressinaud Date: Thu, 4 Dec 2014 19:33:29 +0100 Subject: First draft for the new extension feature - Only system extensions can be loaded for the moment by adding them in the config.php file. - Remove previous system (it will be added properly in the new system in the next step). --- lib/Minz/Extension.php | 96 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 lib/Minz/Extension.php (limited to 'lib/Minz/Extension.php') diff --git a/lib/Minz/Extension.php b/lib/Minz/Extension.php new file mode 100644 index 000000000..f442344a3 --- /dev/null +++ b/lib/Minz/Extension.php @@ -0,0 +1,96 @@ +name = $meta_info['name']; + $this->entrypoint = $meta_info['entrypoint']; + $this->path = $meta_info['path']; + $this->author = isset($meta_info['author']) ? $meta_info['author'] : ''; + $this->description = isset($meta_info['description']) ? $meta_info['description'] : ''; + $this->version = isset($meta_info['version']) ? $meta_info['version'] : '0.1'; + $this->setType(isset($meta_info['type']) ? $meta_info['type'] : 'user'); + } + + /** + * Used when installing an extension (e.g. update the database scheme). + * + * It must be redefined by child classes. + */ + public function install() {} + + /** + * Used when uninstalling an extension (e.g. revert the database scheme to + * cancel changes from install). + * + * It must be redefined by child classes. + */ + public function uninstall() {} + + /** + * Call at the initialization of the extension (i.e. when the extension is + * enabled by the extension manager). + * + * It must be redefined by child classes. + */ + public function init() {} + + /** + * Getters and setters. + */ + public function getName() { + return $this->name; + } + public function getEntrypoint() { + return $this->entrypoint; + } + public function getAuthor() { + return $this->author; + } + public function getDescription() { + return $this->description; + } + public function getVersion() { + return $this->version; + } + public function getType() { + return $this->type; + } + private function setType($type) { + if (!in_array($type, self::$authorized_types)) { + throw new Minz_ExtensionException('invalid `type` info', $this->name); + } + $this->type = $type; + } +} -- cgit v1.2.3 From f9b037742a0aeb49cab86782d1a59913c2de47bf Mon Sep 17 00:00:00 2001 From: Marien Fressinaud Date: Thu, 4 Dec 2014 20:41:01 +0100 Subject: Update ext.php to serve any file from extensions Add an extension->getFileUrl() method to facilitate url generation --- lib/Minz/Extension.php | 23 +++++++++++++++++++++++ p/ext.php | 37 +++++++++++++++++++------------------ 2 files changed, 42 insertions(+), 18 deletions(-) (limited to 'lib/Minz/Extension.php') diff --git a/lib/Minz/Extension.php b/lib/Minz/Extension.php index f442344a3..72a375a6d 100644 --- a/lib/Minz/Extension.php +++ b/lib/Minz/Extension.php @@ -75,6 +75,9 @@ class Minz_Extension { public function getEntrypoint() { return $this->entrypoint; } + public function getPath() { + return $this->path; + } public function getAuthor() { return $this->author; } @@ -93,4 +96,24 @@ class Minz_Extension { } $this->type = $type; } + + /** + * Return the url for a given file. + * + * @param $filename name of the file to serve. + * @param $type the type (js or css) of the file to serve. + * @return the url corresponding to the file. + */ + public function getFileUrl($filename, $type) { + $dir = end(explode('/', $this->path)); + $file_name_url = urlencode($dir . '/' . $filename); + + $absolute_path = $this->path . '/' . $filename; + $mtime = @filemtime($absolute_path); + + $url = '/ext.php?f=' . $file_name_url . + '&t=' . $type . + '&' . $mtime; + return Minz_Url::display($url); + } } diff --git a/p/ext.php b/p/ext.php index a1dde2f93..39c224a84 100644 --- a/p/ext.php +++ b/p/ext.php @@ -1,32 +1,33 @@ Date: Fri, 5 Dec 2014 10:51:34 +0100 Subject: Fix security hole from ext.php script. Now, ext.php can only serve file under a EXTENSIONS_PATH/ext_dir/static/ directory. A 400 Bad Request error will be returned for other files. See https://github.com/FreshRSS/FreshRSS/issues/252 And https://github.com/FreshRSS/FreshRSS/commit/f9b037742a0aeb49cab86782d1a59913c2de47b --- lib/Minz/Extension.php | 4 ++-- p/ext.php | 34 +++++++++++++++++++++++++++++++++- 2 files changed, 35 insertions(+), 3 deletions(-) (limited to 'lib/Minz/Extension.php') diff --git a/lib/Minz/Extension.php b/lib/Minz/Extension.php index 72a375a6d..ecf510ea2 100644 --- a/lib/Minz/Extension.php +++ b/lib/Minz/Extension.php @@ -106,9 +106,9 @@ class Minz_Extension { */ public function getFileUrl($filename, $type) { $dir = end(explode('/', $this->path)); - $file_name_url = urlencode($dir . '/' . $filename); + $file_name_url = urlencode($dir . '/static/' . $filename); - $absolute_path = $this->path . '/' . $filename; + $absolute_path = $this->path . '/static/' . $filename; $mtime = @filemtime($absolute_path); $url = '/ext.php?f=' . $file_name_url . diff --git a/p/ext.php b/p/ext.php index 39c224a84..5c9f9125f 100644 --- a/p/ext.php +++ b/p/ext.php @@ -7,10 +7,42 @@ if (!isset($_GET['f']) || require('../constants.php'); +/** + * Check if a file can be served by ext.php. A valid file is under a + * EXTENSIONS_PATH/extension_name/static/ directory. + * + * You should sanitize path by using the realpath() function. + * + * @param $path the path to the file we want to serve. + * @return true if it can be served, false else. + * + */ +function is_valid_path($path) { + // It must be under the extension path. + $in_ext_path = (substr($path, 0, strlen(EXTENSIONS_PATH)) === EXTENSIONS_PATH); + if (!$in_ext_path) { + return false; + } + + // File to serve must be under a `ext_dir/static/` directory. + $path_relative_to_ext = substr($path, strlen(EXTENSIONS_PATH) + 1); + $path_splitted = explode('/', $path_relative_to_ext); + if (count($path_splitted) < 3 || $path_splitted[1] !== 'static') { + return false; + } + + return true; +} + $file_name = urldecode($_GET['f']); $file_type = $_GET['t']; -$absolute_filename = EXTENSIONS_PATH . '/' . $file_name; +$absolute_filename = realpath(EXTENSIONS_PATH . '/' . $file_name); + +if (!is_valid_path($absolute_filename)) { + header('HTTP/1.1 400 Bad Request'); + die(); +} switch ($file_type) { case 'css': -- cgit v1.2.3 From 9fc60317eecba785b66011f04b9a5150296f2df6 Mon Sep 17 00:00:00 2001 From: Marien Fressinaud Date: Fri, 5 Dec 2014 14:17:02 +0100 Subject: First draft for listing and manipulate extensions See https://github.com/FreshRSS/FreshRSS/issues/252 --- app/Controllers/extensionController.php | 43 +++++++++++++++++++++++++++++++++ app/layout/aside_configure.phtml | 4 +++ app/layout/header.phtml | 1 + app/views/extension/index.phtml | 35 +++++++++++++++++++++++++++ lib/Minz/Extension.php | 20 +++++++++++++++ lib/Minz/ExtensionManager.php | 17 +++++++++++++ 6 files changed, 120 insertions(+) create mode 100644 app/Controllers/extensionController.php create mode 100644 app/views/extension/index.phtml (limited to 'lib/Minz/Extension.php') diff --git a/app/Controllers/extensionController.php b/app/Controllers/extensionController.php new file mode 100644 index 000000000..504be56d3 --- /dev/null +++ b/app/Controllers/extensionController.php @@ -0,0 +1,43 @@ +view->extension_list = Minz_ExtensionManager::list_extensions(); + } + + public function configureAction() { + if (Minz_Request::param('ajax')) { + $this->view->_useLayout(false); + } + } + + public function enableAction() { + + } + + public function disableAction() { + + } + + public function removeAction() { + + } +} diff --git a/app/layout/aside_configure.phtml b/app/layout/aside_configure.phtml index 53c52d3e3..f7f3617c4 100644 --- a/app/layout/aside_configure.phtml +++ b/app/layout/aside_configure.phtml @@ -22,6 +22,10 @@ Minz_Request::actionName() === 'profile'? ' active' : ''; ?>"> +
  • + +
  • +
  • diff --git a/app/views/extension/index.phtml b/app/views/extension/index.phtml new file mode 100644 index 000000000..c6b7c84a1 --- /dev/null +++ b/app/views/extension/index.phtml @@ -0,0 +1,35 @@ +partial('aside_configure'); ?> + +
    + + +

    + + extension_list)) { ?> + + extension_list as $ext) { ?> +
      +
    • + getName()); ?> +
      + + is_enabled()) { ?> + + + + + + + +
      +
    • +
    • getName(); ?>
    • +
    + + +

    + +
    + + +
    diff --git a/lib/Minz/Extension.php b/lib/Minz/Extension.php index ecf510ea2..a1fdd659b 100644 --- a/lib/Minz/Extension.php +++ b/lib/Minz/Extension.php @@ -17,6 +17,8 @@ class Minz_Extension { 'user', ); + private $is_enabled; + /** * The constructor to assign specific information to the extension. * @@ -41,6 +43,8 @@ class Minz_Extension { $this->description = isset($meta_info['description']) ? $meta_info['description'] : ''; $this->version = isset($meta_info['version']) ? $meta_info['version'] : '0.1'; $this->setType(isset($meta_info['type']) ? $meta_info['type'] : 'user'); + + $this->is_enabled = false; } /** @@ -66,6 +70,22 @@ class Minz_Extension { */ public function init() {} + /** + * Set the current extension to enable. + */ + public function enable() { + $this->is_enabled = true; + } + + /** + * Return if the extension is currently enabled. + * + * @return true if extension is enabled, false else. + */ + public function is_enabled() { + return $this->is_enabled; + } + /** * Getters and setters. */ diff --git a/lib/Minz/ExtensionManager.php b/lib/Minz/ExtensionManager.php index 789557b9e..6c32ccf19 100644 --- a/lib/Minz/ExtensionManager.php +++ b/lib/Minz/ExtensionManager.php @@ -144,6 +144,7 @@ class Minz_ExtensionManager { if (isset(self::$ext_list[$ext_name])) { $ext = self::$ext_list[$ext_name]; self::$ext_list_enabled[$ext_name] = $ext; + $ext->enable(); $ext->init(); } } @@ -158,4 +159,20 @@ class Minz_ExtensionManager { self::enable($ext_name); } } + + + + /** + * Returns a list of extensions. + * + * @param $only_enabled if true returns only the enabled extensions (false by default). + * @return an array of extensions. + */ + public static function list_extensions($only_enabled = false) { + if ($only_enabled) { + return self::$ext_list_enabled; + } else { + return self::$ext_list; + } + } } -- cgit v1.2.3 From f8aa66152fcab24ae7cd9663dab0c0c96a45ca24 Mon Sep 17 00:00:00 2001 From: Marien Fressinaud Date: Fri, 5 Dec 2014 14:48:09 +0100 Subject: Give possibility to register a new Controller. - Add a Extension->registerController(name) method - Controllers must be written in extension_dir/controllers/nameController.php - Controllers must be named as FreshExtension_name_Controller - Controllers must extend Minz_ActionController See https://github.com/FreshRSS/FreshRSS/issues/252 --- lib/Minz/Dispatcher.php | 47 ++++++++++++++++++++++++++++++++++++++++++++--- lib/Minz/Extension.php | 13 +++++++++++++ 2 files changed, 57 insertions(+), 3 deletions(-) (limited to 'lib/Minz/Extension.php') diff --git a/lib/Minz/Dispatcher.php b/lib/Minz/Dispatcher.php index f62a92911..66789a3d3 100644 --- a/lib/Minz/Dispatcher.php +++ b/lib/Minz/Dispatcher.php @@ -15,6 +15,7 @@ class Minz_Dispatcher { /* singleton */ private static $instance = null; private static $needsReset; + private static $registrations = array(); private $controller; @@ -38,7 +39,7 @@ class Minz_Dispatcher { self::$needsReset = false; try { - $this->createController ('FreshRSS_' . Minz_Request::controllerName () . '_Controller'); + $this->createController (Minz_Request::controllerName ()); $this->controller->init (); $this->controller->firstAction (); if (!self::$needsReset) { @@ -73,8 +74,11 @@ class Minz_Dispatcher { * > pas une instance de ActionController */ private function createController ($controller_name) { - $filename = APP_PATH . self::CONTROLLERS_PATH_NAME . '/' - . $controller_name . '.php'; + if (self::isRegistered($controller_name)) { + $controller_name = self::loadController($controller_name); + } else { + $controller_name = 'FreshRSS_' . $controller_name . '_Controller'; + } if (!class_exists ($controller_name)) { throw new Minz_ControllerNotExistException ( @@ -114,4 +118,41 @@ class Minz_Dispatcher { $action_name )); } + + /** + * Register a controller file. + * + * @param $base_name the base name of the controller (i.e. ./?c=) + * @param $controller_name the name of the controller (e.g. HelloWorldController). + * @param $filename the file which contains the controller. + */ + public static function registerController($base_name, $controller_name, $filename) { + if (file_exists($filename)) { + self::$registrations[$base_name] = array( + $controller_name, + $filename, + ); + } + } + + /** + * Return if a controller is registered. + * + * @param $base_name the base name of the controller. + * @return true if the controller has been registered, false else. + */ + public static function isRegistered($base_name) { + return isset(self::$registrations[$base_name]); + } + + /** + * Load a controller file (include) and return its name. + * + * @param $base_name the base name of the controller. + */ + private static function loadController($base_name) { + list($controller_name, $filename) = self::$registrations[$base_name]; + include($filename); + return $controller_name; + } } diff --git a/lib/Minz/Extension.php b/lib/Minz/Extension.php index a1fdd659b..ad3465640 100644 --- a/lib/Minz/Extension.php +++ b/lib/Minz/Extension.php @@ -136,4 +136,17 @@ class Minz_Extension { '&' . $mtime; return Minz_Url::display($url); } + + /** + * Register a controller in the Dispatcher. + * + * @param @base_name the base name of the controller. Final name will be: + * FreshExtension__Controller. + */ + public function registerController($base_name) { + $controller_name = 'FreshExtension_' . $base_name . '_Controller'; + $filename = $this->path . '/controllers/' . $base_name . 'Controller.php'; + + Minz_Dispatcher::registerController($base_name, $controller_name, $filename); + } } -- cgit v1.2.3 From c6a682deb94111c1e14cf10e565da3f4214f02dc Mon Sep 17 00:00:00 2001 From: Marien Fressinaud Date: Fri, 5 Dec 2014 15:27:56 +0100 Subject: Extensions can define new views - View base pathname is set to the extension directory - An extension can now override an existing controller / view See https://github.com/FreshRSS/FreshRSS/issues/252 --- lib/Minz/Dispatcher.php | 40 +++++++++++++++++++++++----------------- lib/Minz/Extension.php | 5 +---- lib/Minz/View.php | 10 +++++++--- 3 files changed, 31 insertions(+), 24 deletions(-) (limited to 'lib/Minz/Extension.php') diff --git a/lib/Minz/Dispatcher.php b/lib/Minz/Dispatcher.php index 66789a3d3..edd59c7cc 100644 --- a/lib/Minz/Dispatcher.php +++ b/lib/Minz/Dispatcher.php @@ -68,16 +68,17 @@ class Minz_Dispatcher { /** * Instancie le Controller - * @param $controller_name le nom du controller à instancier + * @param $base_name le nom du controller à instancier * @exception ControllerNotExistException le controller n'existe pas * @exception ControllerNotActionControllerException controller n'est * > pas une instance de ActionController */ - private function createController ($controller_name) { - if (self::isRegistered($controller_name)) { - $controller_name = self::loadController($controller_name); + private function createController ($base_name) { + if (self::isRegistered($base_name)) { + self::loadController($base_name); + $controller_name = 'FreshExtension_' . $base_name . '_Controller'; } else { - $controller_name = 'FreshRSS_' . $controller_name . '_Controller'; + $controller_name = 'FreshRSS_' . $base_name . '_Controller'; } if (!class_exists ($controller_name)) { @@ -94,6 +95,10 @@ class Minz_Dispatcher { Minz_Exception::ERROR ); } + + if (self::isRegistered($base_name)) { + $this->setViewPath($this->controller, $base_name); + } } /** @@ -123,15 +128,11 @@ class Minz_Dispatcher { * Register a controller file. * * @param $base_name the base name of the controller (i.e. ./?c=) - * @param $controller_name the name of the controller (e.g. HelloWorldController). - * @param $filename the file which contains the controller. + * @param $base_path the base path where we should look into to find info. */ - public static function registerController($base_name, $controller_name, $filename) { - if (file_exists($filename)) { - self::$registrations[$base_name] = array( - $controller_name, - $filename, - ); + public static function registerController($base_name, $base_path) { + if (!self::isRegistered($base_name)) { + self::$registrations[$base_name] = $base_path; } } @@ -146,13 +147,18 @@ class Minz_Dispatcher { } /** - * Load a controller file (include) and return its name. + * Load a controller file (include). * * @param $base_name the base name of the controller. */ private static function loadController($base_name) { - list($controller_name, $filename) = self::$registrations[$base_name]; - include($filename); - return $controller_name; + $base_path = self::$registrations[$base_name]; + $controller_filename = $base_path . '/controllers/' . $base_name . 'Controller.php'; + include($controller_filename); + } + + private static function setViewPath($controller, $base_name) { + $base_path = self::$registrations[$base_name]; + $controller->view()->setBasePathname($base_path); } } diff --git a/lib/Minz/Extension.php b/lib/Minz/Extension.php index ad3465640..5a61ba2e0 100644 --- a/lib/Minz/Extension.php +++ b/lib/Minz/Extension.php @@ -144,9 +144,6 @@ class Minz_Extension { * FreshExtension__Controller. */ public function registerController($base_name) { - $controller_name = 'FreshExtension_' . $base_name . '_Controller'; - $filename = $this->path . '/controllers/' . $base_name . 'Controller.php'; - - Minz_Dispatcher::registerController($base_name, $controller_name, $filename); + Minz_Dispatcher::registerController($base_name, $this->path); } } diff --git a/lib/Minz/View.php b/lib/Minz/View.php index b40448491..bdfbbe63c 100644 --- a/lib/Minz/View.php +++ b/lib/Minz/View.php @@ -12,6 +12,7 @@ class Minz_View { const LAYOUT_PATH_NAME = '/layout'; const LAYOUT_FILENAME = '/layout.phtml'; + private $base_pathname = APP_PATH; private $view_filename = ''; private $use_layout = null; @@ -35,12 +36,15 @@ class Minz_View { * Change le fichier de vue en fonction d'un controller / action */ public function change_view($controller_name, $action_name) { - $this->view_filename = APP_PATH - . self::VIEWS_PATH_NAME . '/' + $this->view_filename = self::VIEWS_PATH_NAME . '/' . $controller_name . '/' . $action_name . '.phtml'; } + public function setBasePathname($base_pathname) { + $this->base_pathname = $base_pathname; + } + /** * Construit la vue */ @@ -70,7 +74,7 @@ class Minz_View { * Affiche la Vue en elle-même */ public function render () { - if ((include($this->view_filename)) === false) { + if ((include($this->base_pathname . $this->view_filename)) === false) { Minz_Log::notice('File not found: `' . $this->view_filename . '`'); } } -- cgit v1.2.3 From a08c382e0651f22a7db06feba225f3d49289763d Mon Sep 17 00:00:00 2001 From: Marien Fressinaud Date: Sat, 6 Dec 2014 15:20:20 +0100 Subject: Separate views registration from controllers one. - Add an Extension->registerViews() method. - Views are first searched in extension paths, then in APP_PATH. - It gives a way to override easily existing controllers / views. - Change include into an include_once in Dispatcher for new controllers. See https://github.com/FreshRSS/FreshRSS/issues/252 --- lib/Minz/Dispatcher.php | 6 +----- lib/Minz/Extension.php | 7 +++++++ lib/Minz/View.php | 28 ++++++++++++++++++++++++---- 3 files changed, 32 insertions(+), 9 deletions(-) (limited to 'lib/Minz/Extension.php') diff --git a/lib/Minz/Dispatcher.php b/lib/Minz/Dispatcher.php index edd59c7cc..125ce5757 100644 --- a/lib/Minz/Dispatcher.php +++ b/lib/Minz/Dispatcher.php @@ -95,10 +95,6 @@ class Minz_Dispatcher { Minz_Exception::ERROR ); } - - if (self::isRegistered($base_name)) { - $this->setViewPath($this->controller, $base_name); - } } /** @@ -154,7 +150,7 @@ class Minz_Dispatcher { private static function loadController($base_name) { $base_path = self::$registrations[$base_name]; $controller_filename = $base_path . '/controllers/' . $base_name . 'Controller.php'; - include($controller_filename); + include_once $controller_filename; } private static function setViewPath($controller, $base_name) { diff --git a/lib/Minz/Extension.php b/lib/Minz/Extension.php index 5a61ba2e0..490a5c5cb 100644 --- a/lib/Minz/Extension.php +++ b/lib/Minz/Extension.php @@ -146,4 +146,11 @@ class Minz_Extension { public function registerController($base_name) { Minz_Dispatcher::registerController($base_name, $this->path); } + + /** + * Register the views in order to be accessible by the application. + */ + public function registerViews() { + Minz_View::addBasePathname($this->path); + } } diff --git a/lib/Minz/View.php b/lib/Minz/View.php index bdfbbe63c..1bc2e862d 100644 --- a/lib/Minz/View.php +++ b/lib/Minz/View.php @@ -12,10 +12,10 @@ class Minz_View { const LAYOUT_PATH_NAME = '/layout'; const LAYOUT_FILENAME = '/layout.phtml'; - private $base_pathname = APP_PATH; private $view_filename = ''; private $use_layout = null; + private static $base_pathnames = array(APP_PATH); private static $title = ''; private static $styles = array (); private static $scripts = array (); @@ -41,8 +41,15 @@ class Minz_View { . $action_name . '.phtml'; } - public function setBasePathname($base_pathname) { - $this->base_pathname = $base_pathname; + /** + * Add a base pathname to search views. + * + * New pathnames will be added at the beginning of the list. + * + * @param $base_pathname the new base pathname. + */ + public static function addBasePathname($base_pathname) { + array_unshift(self::$base_pathnames, $base_pathname); } /** @@ -74,7 +81,20 @@ class Minz_View { * Affiche la Vue en elle-même */ public function render () { - if ((include($this->base_pathname . $this->view_filename)) === false) { + $view_found = false; + + // We search the view in the list of base pathnames. Only the first view + // found is considered. + foreach (self::$base_pathnames as $base) { + $filename = $base . $this->view_filename; + if (file_exists($filename)) { + include $filename; + $view_found = true; + break; + } + } + + if (!$view_found) { Minz_Log::notice('File not found: `' . $this->view_filename . '`'); } } -- cgit v1.2.3 From 08546af75ff9a25eac3409649ea4660fe070720c Mon Sep 17 00:00:00 2001 From: Marien Fressinaud Date: Sat, 6 Dec 2014 18:48:00 +0100 Subject: Add a first draft for hooks - New Extension->registerHook($hook_name, $hook_function) method to register a new hook - Only one hook works for the moment: entry_before_insert - ExtensionManager::callHook will need to evolve based on future hooks See https://github.com/FreshRSS/FreshRSS/issues/252 --- app/Controllers/feedController.php | 11 ++++++-- lib/Minz/Extension.php | 10 +++++++ lib/Minz/ExtensionManager.php | 55 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 74 insertions(+), 2 deletions(-) (limited to 'lib/Minz/Extension.php') diff --git a/app/Controllers/feedController.php b/app/Controllers/feedController.php index 9990a852c..dce79c57a 100755 --- a/app/Controllers/feedController.php +++ b/app/Controllers/feedController.php @@ -329,9 +329,16 @@ class FreshRSS_feed_Controller extends Minz_ActionController { $id = min(time(), $entry_date) . uSecString(); } + $entry->_id($id); + $entry->_isRead($is_read); + + $entry = Minz_ExtensionManager::callHook('entry_before_insert', $entry); + if (is_null($entry)) { + // An extension has returned a null value, there is nothing to insert. + continue; + } + $values = $entry->toArray(); - $values['id'] = $id; - $values['is_read'] = $is_read; $entryDAO->addEntry($values, $prepared_statement); } } diff --git a/lib/Minz/Extension.php b/lib/Minz/Extension.php index 490a5c5cb..c93ba2520 100644 --- a/lib/Minz/Extension.php +++ b/lib/Minz/Extension.php @@ -153,4 +153,14 @@ class Minz_Extension { public function registerViews() { Minz_View::addBasePathname($this->path); } + + /** + * Register a new hook. + * + * @param $hook_name the hook name (must exist). + * @param $hook_function the function name to call (must be callable). + */ + public function registerHook($hook_name, $hook_function) { + Minz_ExtensionManager::addHook($hook_name, $hook_function, $this); + } } diff --git a/lib/Minz/ExtensionManager.php b/lib/Minz/ExtensionManager.php index 46e421bac..7557e178f 100644 --- a/lib/Minz/ExtensionManager.php +++ b/lib/Minz/ExtensionManager.php @@ -2,6 +2,8 @@ /** * An extension manager to load extensions present in EXTENSIONS_PATH. + * + * @todo see coding style for methods!! */ class Minz_ExtensionManager { private static $ext_metaname = 'metadata.json'; @@ -11,6 +13,11 @@ class Minz_ExtensionManager { private static $ext_auto_enabled = array(); + private static $hook_list = array( + 'entry_before_insert' => array(), // function($entry) + ); + private static $ext_to_hooks = array(); + /** * Initialize the extension manager by loading extensions in EXTENSIONS_PATH. * @@ -131,6 +138,8 @@ class Minz_ExtensionManager { in_array($name, self::$ext_auto_enabled)) { self::enable($ext->getName()); } + + self::$ext_to_hooks[$name] = array(); } /** @@ -187,4 +196,50 @@ class Minz_ExtensionManager { return self::$ext_list[$ext_name]; } + + /** + * Add a hook function to a given hook. + * + * The hook name must be a valid one. For the valid list, see self::$hook_list + * array keys. + * + * @param $hook_name the hook name (must exist). + * @param $hook_function the function name to call (must be callable). + * @param $ext the extension which register the hook. + */ + public static function addHook($hook_name, $hook_function, $ext) { + if (isset(self::$hook_list[$hook_name]) && is_callable($hook_function)) { + self::$hook_list[$hook_name][] = $hook_function; + self::$ext_to_hooks[$ext->getName()][] = $hook_name; + } + } + + /** + * Call functions related to a given hook. + * + * The hook name must be a valid one. For the valid list, see self::$hook_list + * array keys. + * + * @param $hook_name the hook to call. + * @param additionnal parameters (for signature, please see self::$hook_list comments) + * @todo hook functions will have different signatures. So the $res = func($args); + * $args = $res; will not work for all of them in the future. We must + * find a better way to call hooks. + */ + public static function callHook($hook_name) { + $args = func_get_args(); + unset($args[0]); + + $result = $args; + foreach (self::$hook_list[$hook_name] as $function) { + $result = call_user_func_array($function, $args); + + if (is_null($result)) { + break; + } + + $args = $result; + } + return $result; + } } -- cgit v1.2.3 From c6dfec3ad351ee3b828c6a2c0a273bad5d9ac0df Mon Sep 17 00:00:00 2001 From: Marien Fressinaud Date: Mon, 8 Dec 2014 12:01:47 +0100 Subject: Add behaviour to configure action (extensions) - Put extension configure view in dir_ext/configure.phtml - Handle POST action in Extension->handleConfigureAction() method See https://github.com/FreshRSS/FreshRSS/issues/252 --- app/Controllers/extensionController.php | 16 ++++++++++++++++ app/views/extension/configure.phtml | 17 ++++++++++++++--- lib/Minz/Extension.php | 21 +++++++++++++++++++++ 3 files changed, 51 insertions(+), 3 deletions(-) (limited to 'lib/Minz/Extension.php') diff --git a/app/Controllers/extensionController.php b/app/Controllers/extensionController.php index a1e39af37..73b8070cb 100644 --- a/app/Controllers/extensionController.php +++ b/app/Controllers/extensionController.php @@ -27,6 +27,22 @@ class FreshRSS_extension_Controller extends Minz_ActionController { if (Minz_Request::param('ajax')) { $this->view->_useLayout(false); } + + $ext_name = urldecode(Minz_Request::param('e')); + $ext = Minz_ExtensionManager::find_extension($ext_name); + + if (is_null($ext)) { + Minz_Error::error(404); + } + if ($ext->getType() === 'system' && !FreshRSS_Auth::hasAccess('admin')) { + Minz_Error::error(403); + } + + $this->view->extension = $ext; + + if (Minz_Request::isPost()) { + $this->view->extension->handleConfigureAction(); + } } /** diff --git a/app/views/extension/configure.phtml b/app/views/extension/configure.phtml index a79e9baac..295080d5e 100644 --- a/app/views/extension/configure.phtml +++ b/app/views/extension/configure.phtml @@ -7,6 +7,17 @@ if (!Minz_Request::param('ajax')) { ?>
    -

    Extension name

    - Not implemented yet! -
    \ No newline at end of file +

    extension->getName(); ?> (extension->getVersion(); ?>) — extension->getType(); ?>

    + +

    extension->getDescription(); ?> — extension->getAuthor(); ?>

    + +

    + extension->getConfigureView(); + if ($configure_view !== false) { + echo $configure_view; + } else { + ?> +

    + + diff --git a/lib/Minz/Extension.php b/lib/Minz/Extension.php index c93ba2520..1d706ed80 100644 --- a/lib/Minz/Extension.php +++ b/lib/Minz/Extension.php @@ -86,6 +86,27 @@ class Minz_Extension { return $this->is_enabled; } + /** + * Return the content of the configure view for the current extension. + * + * @return the html content from ext_dir/configure.phtml, false if it does + * not exist. + */ + public function getConfigureView() { + $filename = $this->path . '/configure.phtml'; + if (!file_exists($filename)) { + return false; + } + return @file_get_contents($filename); + } + + /** + * Handle the configure POST action. + * + * It must be redefined by child classes. + */ + public function handleConfigureAction() {} + /** * Getters and setters. */ -- cgit v1.2.3 From 251d5a78ce893676c7e32e2a85c9ba45775a72bf Mon Sep 17 00:00:00 2001 From: Marien Fressinaud Date: Thu, 18 Dec 2014 13:57:25 +0100 Subject: Fix php interpretation in configure.phtml file See https://github.com/FreshRSS/FreshRSS/issues/252 --- lib/Minz/Extension.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'lib/Minz/Extension.php') diff --git a/lib/Minz/Extension.php b/lib/Minz/Extension.php index 1d706ed80..e7b38448f 100644 --- a/lib/Minz/Extension.php +++ b/lib/Minz/Extension.php @@ -97,7 +97,10 @@ class Minz_Extension { if (!file_exists($filename)) { return false; } - return @file_get_contents($filename); + + ob_start(); + include($filename); + return ob_get_clean(); } /** -- cgit v1.2.3 From 26da4aa448906f857a252507b34d369a386043c6 Mon Sep 17 00:00:00 2001 From: Marien Fressinaud Date: Thu, 18 Dec 2014 18:59:13 +0100 Subject: Update Minz_Translation - Give possibility to register new i18n files - Add a extension->registerTranslates() method - extensions can define new strings or override previous ones Fix https://github.com/FreshRSS/FreshRSS/issues/731 --- lib/Minz/Extension.php | 8 +++++ lib/Minz/Translate.php | 79 +++++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 76 insertions(+), 11 deletions(-) (limited to 'lib/Minz/Extension.php') diff --git a/lib/Minz/Extension.php b/lib/Minz/Extension.php index e7b38448f..a24c718c3 100644 --- a/lib/Minz/Extension.php +++ b/lib/Minz/Extension.php @@ -178,6 +178,14 @@ class Minz_Extension { Minz_View::addBasePathname($this->path); } + /** + * Register i18n files from ext_dir/i18n/ + */ + public function registerTranslates() { + $i18n_dir = $this->path . '/i18n'; + Minz_Translate::registerPath($i18n_dir); + } + /** * Register a new hook. * diff --git a/lib/Minz/Translate.php b/lib/Minz/Translate.php index e7efb8665..aa96728f2 100644 --- a/lib/Minz/Translate.php +++ b/lib/Minz/Translate.php @@ -15,9 +15,9 @@ class Minz_Translate { private static $lang_name; /** - * $lang_path is the pathname of i18n files (e.g. ./app/i18n/en/). + * $lang_files is a list of registered i18n files. */ - private static $lang_path; + private static $lang_files = array(); /** * $translates is a cache for i18n translation. @@ -30,7 +30,10 @@ class Minz_Translate { public static function init() { $l = Minz_Configuration::language(); self::$lang_name = Minz_Session::param('language', $l); - self::$lang_path = APP_PATH . '/i18n/' . self::$lang_name . '/'; + self::$lang_files = array(); + self::$translates = array(); + + self::registerPath(APP_PATH . '/i18n'); } /** @@ -40,6 +43,63 @@ class Minz_Translate { self::init(); } + /** + * Register a new path and load i18n files inside. + * + * @param $path a path containing i18n directories (e.g. ./en/, ./fr/). + */ + public static function registerPath($path) { + // We load first i18n files for the current language. + $lang_path = $path . '/' . self::$lang_name; + $list_i18n_files = array_values(array_diff( + scandir($lang_path), + array('..', '.') + )); + + // Each file basename correspond to a top-level i18n key. For each of + // these keys we store the file pathname and mark translations must be + // reloaded (by setting $translates[$i18n_key] to null). + foreach ($list_i18n_files as $i18n_filename) { + $i18n_key = basename($i18n_filename, '.php'); + if (!isset(self::$lang_files[$i18n_key])) { + self::$lang_files[$i18n_key] = array(); + } + self::$lang_files[$i18n_key][] = $lang_path . '/' . $i18n_filename; + self::$translates[$i18n_key] = null; + } + } + + /** + * Load the files associated to $key into $translates. + * + * @param $key the top level i18n key we want to load. + */ + private static function loadKey($key) { + // The top level key is not in $lang_files, it means it does not exist! + if (!isset(self::$lang_files[$key])) { + Minz_Log::debug($key . ' is not a valid top level key'); + return false; + } + + self::$translates[$key] = array(); + + foreach (self::$lang_files[$key] as $lang_pathname) { + $i18n_array = include($lang_pathname); + if (!is_array($i18n_array)) { + Minz_Log::warning('`' . $lang_pathname . '` does not contain a PHP array'); + continue; + } + + // We must avoid to erase previous data so we just override them if + // needed. + self::$translates[$key] = array_replace_recursive( + self::$translates[$key], $i18n_array + ); + } + + return true; + } + /** * Translate a key into its corresponding value based on selected language. * @param $key the key to translate. @@ -57,16 +117,13 @@ class Minz_Translate { $top_level = array_shift($group); } - $filename = self::$lang_path . $top_level . '.php'; - - // Try to load the i18n file if it's not done yet. - if (!isset(self::$translates[$top_level])) { - if (!file_exists($filename)) { - Minz_Log::debug($top_level . ' is not a valid top level key'); + // If $translates[$top_level] is null it means we have to load the + // corresponding files. + if (is_null(self::$translates[$top_level])) { + $res = self::loadKey($top_level); + if (!$res) { return $key; } - - self::$translates[$top_level] = include($filename); } // Go through the i18n keys to get the correct translation value. -- cgit v1.2.3 From 5bd7997d41d052420e2a4c164a4f60fe982760e2 Mon Sep 17 00:00:00 2001 From: Marien Fressinaud Date: Thu, 8 Jan 2015 14:30:05 +0100 Subject: Call handleConfigureAction() even for GET requests See https://github.com/FreshRSS/FreshRSS/issues/252 --- app/Controllers/extensionController.php | 5 +---- lib/Minz/Extension.php | 2 +- 2 files changed, 2 insertions(+), 5 deletions(-) (limited to 'lib/Minz/Extension.php') diff --git a/app/Controllers/extensionController.php b/app/Controllers/extensionController.php index 3eedcd949..e013bb369 100644 --- a/app/Controllers/extensionController.php +++ b/app/Controllers/extensionController.php @@ -49,10 +49,7 @@ class FreshRSS_extension_Controller extends Minz_ActionController { } $this->view->extension = $ext; - - if (Minz_Request::isPost()) { - $this->view->extension->handleConfigureAction(); - } + $this->view->extension->handleConfigureAction(); } /** diff --git a/lib/Minz/Extension.php b/lib/Minz/Extension.php index a24c718c3..b3991c129 100644 --- a/lib/Minz/Extension.php +++ b/lib/Minz/Extension.php @@ -104,7 +104,7 @@ class Minz_Extension { } /** - * Handle the configure POST action. + * Handle the configure action. * * It must be redefined by child classes. */ -- cgit v1.2.3 From 806b4de54ffd1dd682ec7b42a5be8343a464e5f0 Mon Sep 17 00:00:00 2001 From: Marien Fressinaud Date: Wed, 14 Jan 2015 21:38:35 +0100 Subject: Add mechanism at (un)install of an extension Test if the install or uninstall has been successfully performed. If these methods return true, all is ok but if a string is returned, the string must explain the problem. This problem is stored in log file. A feedback is given to explain to check log file. This commit fix a problem in the english translation of feedback.sub.feed.internal_problem. --- app/Controllers/extensionController.php | 40 +++++++++++++++++++++------------ app/i18n/en/feedback.php | 12 +++++++--- app/i18n/fr/feedback.php | 10 +++++++-- lib/Minz/Extension.php | 14 ++++++++++-- 4 files changed, 55 insertions(+), 21 deletions(-) (limited to 'lib/Minz/Extension.php') diff --git a/app/Controllers/extensionController.php b/app/Controllers/extensionController.php index e013bb369..692595a4c 100644 --- a/app/Controllers/extensionController.php +++ b/app/Controllers/extensionController.php @@ -88,15 +88,21 @@ class FreshRSS_extension_Controller extends Minz_ActionController { $url_redirect); } - $ext->install(); + $res = $ext->install(); - $ext_list = $conf->extensions_enabled; - array_push_unique($ext_list, $ext_name); - $conf->extensions_enabled = $ext_list; - $conf->save(); + if ($res === true) { + $ext_list = $conf->extensions_enabled; + array_push_unique($ext_list, $ext_name); + $conf->extensions_enabled = $ext_list; + $conf->save(); - Minz_Request::good(_t('feedback.extensions.enabled', $ext_name), - $url_redirect); + Minz_Request::good(_t('feedback.extensions.enable.ok', $ext_name), + $url_redirect); + } else { + Minz_Log::warning('Can not enable extension ' . $ext_name . ': ' . $res); + Minz_Request::bad(_t('feedback.extensions.enable.ko', $ext_name, _url('index', 'logs')), + $url_redirect); + } } Minz_Request::forward($url_redirect, true); @@ -138,15 +144,21 @@ class FreshRSS_extension_Controller extends Minz_ActionController { $url_redirect); } - $ext->uninstall(); + $res = $ext->uninstall(); - $ext_list = $conf->extensions_enabled; - array_remove($ext_list, $ext_name); - $conf->extensions_enabled = $ext_list; - $conf->save(); + if ($res === true) { + $ext_list = $conf->extensions_enabled; + array_remove($ext_list, $ext_name); + $conf->extensions_enabled = $ext_list; + $conf->save(); - Minz_Request::good(_t('feedback.extensions.disabled', $ext_name), - $url_redirect); + Minz_Request::good(_t('feedback.extensions.disable.ok', $ext_name), + $url_redirect); + } else { + Minz_Log::warning('Can not unable extension ' . $ext_name . ': ' . $res); + Minz_Request::bad(_t('feedback.extensions.disable.ko', $ext_name, _url('index', 'logs')), + $url_redirect); + } } Minz_Request::forward($url_redirect, true); diff --git a/app/i18n/en/feedback.php b/app/i18n/en/feedback.php index e9499ed46..19af81e5b 100644 --- a/app/i18n/en/feedback.php +++ b/app/i18n/en/feedback.php @@ -31,8 +31,14 @@ return array( ), 'extensions' => array( 'already_enabled' => '%s is already enabled', - 'disabled' => '%s is now disabled', - 'enabled' => '%s is now enabled', + 'disable' => array( + 'ko' => '%s cannot be disabled. Check FressRSS logs for details.', + 'ok' => '%s is now disabled', + ), + 'enable' => array( + 'ko' => '%s cannot be enabled. Check FressRSS logs for details.', + 'ok' => '%s is now enabled', + ), 'no_access' => 'You have no access on %s', 'not_enabled' => '%s is not enabled yet', 'not_found' => '%s does not exist', @@ -67,7 +73,7 @@ return array( 'already_subscribed' => 'You have already subscribed to %s', 'deleted' => 'Feed has been deleted', 'error' => 'Feed cannot be updated', - 'internal_problem_feed' => 'The RSS feed could not be added. Check FressRSS logs for details.', + 'internal_problem' => 'The RSS feed could not be added. Check FressRSS logs for details.', 'invalid_url' => 'URL %s is invalid', 'marked_read' => 'Feeds have been marked as read', 'n_actualized' => '%d feeds have been updated', diff --git a/app/i18n/fr/feedback.php b/app/i18n/fr/feedback.php index c99314411..e2364a251 100644 --- a/app/i18n/fr/feedback.php +++ b/app/i18n/fr/feedback.php @@ -31,8 +31,14 @@ return array( ), 'extensions' => array( 'already_enabled' => '%s est déjà activée', - 'disabled' => '%s est désormais désactivée', - 'enabled' => '%s est désormais activée', + 'disable' => array( + 'ko' => '%s ne peut pas être désactivée. Consulter les logs de FreshRSS pour plus de détails.', + 'ok' => '%s est désormais désactivée', + ), + 'enable' => array( + 'ko' => '%s ne peut pas être activée. Consulter les logs de FreshRSS pour plus de détails.', + 'ok' => '%s est désormais activée', + ), 'no_access' => 'Vous n’avez aucun accès sur %s', 'not_enabled' => '%s n’est pas encore activée', 'not_found' => '%s n’existe pas', diff --git a/lib/Minz/Extension.php b/lib/Minz/Extension.php index b3991c129..ac7231e9c 100644 --- a/lib/Minz/Extension.php +++ b/lib/Minz/Extension.php @@ -51,16 +51,26 @@ class Minz_Extension { * Used when installing an extension (e.g. update the database scheme). * * It must be redefined by child classes. + * + * @return true if the extension has been installed or a string explaining + * the problem. */ - public function install() {} + public function install() { + return true; + } /** * Used when uninstalling an extension (e.g. revert the database scheme to * cancel changes from install). * * It must be redefined by child classes. + * + * @return true if the extension has been uninstalled or a string explaining + * the problem. */ - public function uninstall() {} + public function uninstall() { + return true; + } /** * Call at the initialization of the extension (i.e. when the extension is -- cgit v1.2.3 From 89682886005f1e6b36e664f47b3b0862d62846e6 Mon Sep 17 00:00:00 2001 From: Marien Fressinaud Date: Wed, 14 Jan 2015 22:22:30 +0100 Subject: Fix extension API (coding style) is_enabled() becomes isEnabled(). Sorry for this coding style mistake :s --- app/Controllers/extensionController.php | 4 ++-- app/views/extension/index.phtml | 2 +- lib/Minz/Extension.php | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) (limited to 'lib/Minz/Extension.php') diff --git a/app/Controllers/extensionController.php b/app/Controllers/extensionController.php index 692595a4c..5dd7eac89 100644 --- a/app/Controllers/extensionController.php +++ b/app/Controllers/extensionController.php @@ -73,7 +73,7 @@ class FreshRSS_extension_Controller extends Minz_ActionController { $url_redirect); } - if ($ext->is_enabled()) { + if ($ext->isEnabled()) { Minz_Request::bad(_t('feedback.extensions.already_enabled', $ext_name), $url_redirect); } @@ -129,7 +129,7 @@ class FreshRSS_extension_Controller extends Minz_ActionController { $url_redirect); } - if (!$ext->is_enabled()) { + if (!$ext->isEnabled()) { Minz_Request::bad(_t('feedback.extensions.not_enabled', $ext_name), $url_redirect); } diff --git a/app/views/extension/index.phtml b/app/views/extension/index.phtml index fd97c5e81..25b0eb9ee 100644 --- a/app/views/extension/index.phtml +++ b/app/views/extension/index.phtml @@ -14,7 +14,7 @@ getName()); ?>
    - is_enabled()) { ?> + isEnabled()) { ?> diff --git a/lib/Minz/Extension.php b/lib/Minz/Extension.php index ac7231e9c..5069362b5 100644 --- a/lib/Minz/Extension.php +++ b/lib/Minz/Extension.php @@ -92,7 +92,7 @@ class Minz_Extension { * * @return true if extension is enabled, false else. */ - public function is_enabled() { + public function isEnabled() { return $this->is_enabled; } -- cgit v1.2.3 From 2039710d12f8f705b7eb9233ae3cc130f6e4e7c9 Mon Sep 17 00:00:00 2001 From: Marien Fressinaud Date: Thu, 15 Jan 2015 13:43:05 +0100 Subject: Avoid "PHP Strict Standards" error A "Only variables should be passed by reference" error was raised in the extension->getFileUrl() method. --- lib/Minz/Extension.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib/Minz/Extension.php') diff --git a/lib/Minz/Extension.php b/lib/Minz/Extension.php index 5069362b5..d7ee8fe81 100644 --- a/lib/Minz/Extension.php +++ b/lib/Minz/Extension.php @@ -159,7 +159,7 @@ class Minz_Extension { * @return the url corresponding to the file. */ public function getFileUrl($filename, $type) { - $dir = end(explode('/', $this->path)); + $dir = substr(strrchr($this->path, '/'), 1); $file_name_url = urlencode($dir . '/static/' . $filename); $absolute_path = $this->path . '/static/' . $filename; -- cgit v1.2.3