diff options
Diffstat (limited to 'engine/lib/plugins.php')
| -rw-r--r-- | engine/lib/plugins.php | 879 |
1 files changed, 365 insertions, 514 deletions
diff --git a/engine/lib/plugins.php b/engine/lib/plugins.php index 9a3dd630a..d5d3db466 100644 --- a/engine/lib/plugins.php +++ b/engine/lib/plugins.php @@ -54,13 +54,15 @@ define('ELGG_PLUGIN_INTERNAL_PREFIX', 'elgg:internal:'); * @param string $dir A dir to scan for plugins. Defaults to config's plugins_path. * * @return array + * @since 1.8.0 + * @access private */ function elgg_get_plugin_ids_in_dir($dir = null) { if (!$dir) { - $dir = elgg_get_plugin_path(); + $dir = elgg_get_plugins_path(); } - $plugin_idss = array(); + $plugin_ids = array(); $handle = opendir($dir); if ($handle) { @@ -85,14 +87,21 @@ function elgg_get_plugin_ids_in_dir($dir = null) { * * @todo Crappy name? * @return bool + * @since 1.8.0 + * @access private */ function elgg_generate_plugin_entities() { + // @todo $site unused, can remove? $site = get_config('site'); - $dir = elgg_get_plugin_path(); + + $dir = elgg_get_plugins_path(); + $db_prefix = elgg_get_config('dbprefix'); $options = array( 'type' => 'object', 'subtype' => 'plugin', + 'selects' => array('plugin_oe.*'), + 'joins' => array("JOIN {$db_prefix}objects_entity plugin_oe on plugin_oe.guid = e.guid"), 'limit' => ELGG_ENTITIES_NO_VALUE ); @@ -100,6 +109,7 @@ function elgg_generate_plugin_entities() { $old_access = access_get_show_hidden_status(); access_show_hidden_entities(true); $known_plugins = elgg_get_entities_from_relationship($options); + /* @var ElggPlugin[] $known_plugins */ if (!$known_plugins) { $known_plugins = array(); @@ -124,8 +134,6 @@ function elgg_generate_plugin_entities() { return false; } - $new_plugin_priority = elgg_get_max_plugin_priority() + 1; - // check real plugins against known ones foreach ($physical_plugins as $plugin_id) { // is this already in the db? @@ -133,23 +141,19 @@ function elgg_generate_plugin_entities() { $index = $id_map[$plugin_id]; $plugin = $known_plugins[$index]; // was this plugin deleted and its entity disabled? - if ($plugin->enabled != 'yes') { + if (!$plugin->isEnabled()) { $plugin->enable(); $plugin->deactivate(); - $plugin->setPriority($new_plugin_priority); - - $new_plugin_priority++; + $plugin->setPriority('last'); } // remove from the list of plugins to disable unset($known_plugins[$index]); } else { // add new plugins + // priority is force to last in save() if not set. $plugin = new ElggPlugin($plugin_id); $plugin->save(); - $plugin->setPriority($new_plugin_priority); - - $new_plugin_priority++; } } @@ -175,20 +179,40 @@ function elgg_generate_plugin_entities() { } /** + * Cache a reference to this plugin by its ID + * + * @param ElggPlugin $plugin + * + * @access private + */ +function _elgg_cache_plugin_by_id(ElggPlugin $plugin) { + $map = (array) elgg_get_config('plugins_by_id_map'); + $map[$plugin->getID()] = $plugin; + elgg_set_config('plugins_by_id_map', $map); +} + +/** * Returns an ElggPlugin object with the path $path. * - * @param string $id The id (dir name) of the plugin. NOT the guid. - * @return mixed ElggPlugin or false. + * @param string $plugin_id The id (dir name) of the plugin. NOT the guid. + * @return ElggPlugin|false + * @since 1.8.0 */ -function elgg_get_plugin_from_id($id) { - $id = sanitize_string($id); +function elgg_get_plugin_from_id($plugin_id) { + $map = (array) elgg_get_config('plugins_by_id_map'); + if (isset($map[$plugin_id])) { + return $map[$plugin_id]; + } + + $plugin_id = sanitize_string($plugin_id); $db_prefix = get_config('dbprefix'); $options = array( 'type' => 'object', 'subtype' => 'plugin', 'joins' => array("JOIN {$db_prefix}objects_entity oe on oe.guid = e.guid"), - 'wheres' => array("oe.title = '$id'"), + 'selects' => array("oe.title", "oe.description"), + 'wheres' => array("oe.title = '$plugin_id'"), 'limit' => 1 ); @@ -209,6 +233,7 @@ function elgg_get_plugin_from_id($id) { * {@link elgg_generate_plugin_objects()} first. * * @param string $id The plugin ID. + * @since 1.8.0 * @return bool */ function elgg_plugin_exists($id) { @@ -221,6 +246,8 @@ function elgg_plugin_exists($id) { * Returns the highest priority of the plugins * * @return int + * @since 1.8.0 + * @access private */ function elgg_get_max_plugin_priority() { $db_prefix = get_config('dbprefix'); @@ -235,30 +262,60 @@ function elgg_get_max_plugin_priority() { $data = get_data($q); if ($data) { - return $data[0]->max; + $max = $data[0]->max; + } else { + $max = 1; } // can't have a priority of 0. - return 1; + return ($max) ? $max : 1; +} + +/** + * Returns if a plugin is active for a current site. + * + * @param string $plugin_id The plugin ID + * @param int $site_guid The site guid + * @since 1.8.0 + * @return bool + */ +function elgg_is_active_plugin($plugin_id, $site_guid = null) { + if ($site_guid) { + $site = get_entity($site_guid); + } else { + $site = elgg_get_site_entity(); + } + + if (!($site instanceof ElggSite)) { + return false; + } + + $plugin = elgg_get_plugin_from_id($plugin_id); + + if (!$plugin) { + return false; + } + + return $plugin->isActive($site->guid); } /** * Loads all active plugins in the order specified in the tool admin panel. * - * @note This is called on every page load and includes additional checking that plugins - * are fit to be loaded. If a plugin is active and problematic, it will be disabled - * and a visible error emitted. + * @note This is called on every page load. If a plugin is active and problematic, it + * will be disabled and a visible error emitted. This does not check the deps system because + * that was too slow. * * @return bool + * @since 1.8.0 + * @access private */ function elgg_load_plugins() { - global $CONFIG; - - $plugins_path = elgg_get_plugin_path(); - $start_flags = ELGG_PLUGIN_INCLUDE_START - | ELGG_PLUGIN_REGISTER_VIEWS - | ELGG_PLUGIN_REGISTER_LANGUAGES - | ELGG_PLUGIN_REGISTER_CLASSES; + $plugins_path = elgg_get_plugins_path(); + $start_flags = ELGG_PLUGIN_INCLUDE_START | + ELGG_PLUGIN_REGISTER_VIEWS | + ELGG_PLUGIN_REGISTER_LANGUAGES | + ELGG_PLUGIN_REGISTER_CLASSES; if (!$plugins_path) { return false; @@ -266,44 +323,31 @@ function elgg_load_plugins() { // temporary disable all plugins if there is a file called 'disabled' in the plugin dir if (file_exists("$plugins_path/disabled")) { + if (elgg_is_admin_logged_in() && elgg_in_context('admin')) { + system_message(elgg_echo('plugins:disabled')); + } return false; } - // Load view caches if available - $cached_view_paths = elgg_filepath_cache_load('views'); - $cached_view_types = elgg_filepath_cache_load('view_types'); - $cached_view_info = is_string($cached_view_paths) && is_string($cached_view_types); - - if ($cached_view_info) { - $CONFIG->views = unserialize($cached_view_paths); - $CONFIG->view_types = unserialize($cached_view_types); - - // don't need to register views + if (elgg_get_config('system_cache_loaded')) { $start_flags = $start_flags & ~ELGG_PLUGIN_REGISTER_VIEWS; } + if (elgg_get_config('i18n_loaded_from_cache')) { + $start_flags = $start_flags & ~ELGG_PLUGIN_REGISTER_LANGUAGES; + } + $return = true; $plugins = elgg_get_plugins('active'); if ($plugins) { foreach ($plugins as $plugin) { - // check if plugin can be started and try to start it. - // if anything is bad, disable it and emit a message. - if (!$plugin->isValid()) { - $plugin->deactivate(); - $msg = elgg_echo('PluginException:MisconfiguredPlugin', array($plugin->getID(), $plugin->guid)); - register_error($msg); - $return = false; - - continue; - } - try { $plugin->start($start_flags); } catch (Exception $e) { $plugin->deactivate(); $msg = elgg_echo('PluginException:CannotStart', array($plugin->getID(), $plugin->guid, $e->getMessage())); - register_error($msg); + elgg_add_admin_notice('cannot_start' . $plugin->getID(), $msg); $return = false; continue; @@ -311,24 +355,19 @@ function elgg_load_plugins() { } } - // Cache results - if (!$cached_view_info) { - elgg_filepath_cache_save('views', serialize($CONFIG->views)); - elgg_filepath_cache_save('view_types', serialize($CONFIG->view_types)); - } - return $return; } /** * Returns an ordered list of plugins * - * @param string $status The status of the plugins. active, inactive, or all. - * @param bool $include_deleted Include physically deleted (and so inactive and disabled) plugins? - * @param mixed $site_guid Optional site guid - * @return array + * @param string $status The status of the plugins. active, inactive, or all. + * @param mixed $site_guid Optional site guid + * @return ElggPlugin[] + * @since 1.8.0 + * @access private */ -function elgg_get_plugins($status = 'active', $include_deleted = false, $site_guid = NULL) { +function elgg_get_plugins($status = 'active', $site_guid = null) { $db_prefix = get_config('dbprefix'); $priority = elgg_namespace_plugin_private_setting('internal', 'priority'); @@ -342,7 +381,11 @@ function elgg_get_plugins($status = 'active', $include_deleted = false, $site_gu 'type' => 'object', 'subtype' => 'plugin', 'limit' => ELGG_ENTITIES_NO_VALUE, - 'joins' => array("JOIN {$db_prefix}private_settings ps on ps.entity_guid = e.guid"), + 'selects' => array('plugin_oe.*'), + 'joins' => array( + "JOIN {$db_prefix}private_settings ps on ps.entity_guid = e.guid", + "JOIN {$db_prefix}objects_entity plugin_oe on plugin_oe.guid = e.guid" + ), 'wheres' => array("ps.name = '$priority'"), 'order_by' => "CAST(ps.value as unsigned), e.guid" ); @@ -367,15 +410,9 @@ function elgg_get_plugins($status = 'active', $include_deleted = false, $site_gu break; } - if ($include_deleted) { - $old_id = elgg_set_ignore_access(true); - } - + $old_ia = elgg_set_ignore_access(true); $plugins = elgg_get_entities_from_relationship($options); - - if ($include_deleted) { - elgg_set_ignore_access($old_ia); - } + elgg_set_ignore_access($old_ia); return $plugins; } @@ -390,11 +427,13 @@ function elgg_get_plugins($status = 'active', $include_deleted = false, $site_gu * * @param array $order An array of plugin ids in the order to set them * @return bool + * @since 1.8.0 + * @access private */ function elgg_set_plugin_priorities(array $order) { $name = elgg_namespace_plugin_private_setting('internal', 'priority'); - $plugins = elgg_get_plugins('any', true); + $plugins = elgg_get_plugins('any'); if (!$plugins) { return false; } @@ -405,6 +444,7 @@ function elgg_set_plugin_priorities(array $order) { // though we do start with 1 $order = array_values($order); + $missing_plugins = array(); foreach ($plugins as $plugin) { $plugin_id = $plugin->getID(); @@ -421,9 +461,9 @@ function elgg_set_plugin_priorities(array $order) { } } - // set the missing plugins priorities + // set the missing plugins' priorities if ($return && $missing_plugins) { - if (!$priority) { + if (!isset($priority)) { $priority = 0; } foreach ($missing_plugins as $plugin) { @@ -443,99 +483,29 @@ function elgg_set_plugin_priorities(array $order) { * * @todo Can this be done in a single sql command? * @return bool + * @since 1.8.0 + * @access private */ function elgg_reindex_plugin_priorities() { return elgg_set_plugin_priorities(array()); } /** - * Returns a list of plugins to load, in the order that they should be loaded. - * - * @deprecated 1.8 - * - * @return array List of plugins - */ -function get_plugin_list() { - elgg_deprecated_notice('get_plugin_list() is deprecated by elgg_get_plugin_ids_in_dir() or elgg_get_plugins()', 1.8); - - $plugins = elgg_get_plugins('any'); - - $list = array(); - if ($plugins) { - foreach ($plugins as $i => $plugin) { - // in <=1.7 this returned indexed by multiples of 10. - // uh...sure...why not. - $index = ($i + 1) * 10; - $list[$index] = $plugin->getID(); - } - } - - return $list; -} - -/** - * Regenerates the list of known plugins and saves it to the current site - * - * Important: You should regenerate simplecache and the viewpath cache after executing this function - * otherwise you may experience view display artifacts. Do this with the following code: - * - * elgg_view_regenerate_simplecache(); - * elgg_filepath_cache_reset(); - * - * @deprecated 1.8 - * - * @param array $pluginorder Optionally, a list of existing plugins and their orders - * - * @return array The new list of plugins and their orders - */ -function regenerate_plugin_list($pluginorder = FALSE) { - $msg = 'regenerate_plugin_list() is (sorta) deprecated by elgg_generate_plugin_entities() and' - . ' elgg_set_plugin_priorities().'; - elgg_deprecated_notice($msg, 1.8); - - // they're probably trying to set it? - if ($pluginorder) { - if (elgg_generate_plugin_entities()) { - // sort the plugins by the index numerically since we used - // weird indexes in the old system. - ksort($pluginorder, SORT_NUMERIC); - return elgg_set_plugin_priorities($pluginorder); - } - return false; - } else { - // they're probably trying to regenerate from disk? - return elgg_generate_plugin_entities(); - } -} - - -/** - * Loads plugins - * - * @deprecate 1.8 - * - * @return bool - */ -function load_plugins() { - elgg_deprecated_notice('load_plugins() is deprecated by elgg_load_plugins()', 1.8); - - return elgg_load_plugins(); -} - - -/** * Namespaces a string to be used as a private setting for a plugin. * * @param string $type The type of value: user_setting or internal. * @param string $name The name to namespace. * @param string $id The plugin's ID to namespace with. Required for user_setting. * @return string + * @since 1.8.0 + * @access private */ function elgg_namespace_plugin_private_setting($type, $name, $id = null) { switch ($type) { -// case 'setting': -// $name = ELGG_PLUGIN_SETTING_PREFIX . $name; -// break; + // commented out because it breaks $plugin->$name access to variables + //case 'setting': + // $name = ELGG_PLUGIN_SETTING_PREFIX . $name; + // break; case 'user_setting': if (!$id) { @@ -562,9 +532,11 @@ function elgg_namespace_plugin_private_setting($type, $name, $id = null) { * context from the main script filename called by * the browser. Default = false. * - * @since 1.8 - * * @return string|false Plugin name, or false if no plugin name was called + * @since 1.8.0 + * @access private + * + * @todo get rid of this */ function elgg_get_calling_plugin_id($mainfilename = false) { if (!$mainfilename) { @@ -579,8 +551,9 @@ function elgg_get_calling_plugin_id($mainfilename = false) { } } } else { - if (preg_match("/pg\/([a-zA-Z0-9\-\_]*)\//", $_SERVER['REQUEST_URI'], $matches)) { - return $matches[1]; + //@todo this is a hack -- plugins do not have to match their page handler names! + if ($handler = get_input('handler', FALSE)) { + return $handler; } else { $file = $_SERVER["SCRIPT_NAME"]; $file = str_replace("\\", "/", $file); @@ -594,74 +567,6 @@ function elgg_get_calling_plugin_id($mainfilename = false) { } /** - * Get the name of the most recent plugin to be called in the - * call stack (or the plugin that owns the current page, if any). - * - * i.e., if the last plugin was in /mod/foobar/, get_plugin_name would return foo_bar. - * - * @deprecated 1.8 - * - * @param boolean $mainfilename If set to true, this will instead determine the - * context from the main script filename called by - * the browser. Default = false. - * - * @return string|false Plugin name, or false if no plugin name was called - */ -function get_plugin_name($mainfilename = false) { - elgg_deprecated_notice('get_plugin_name() is deprecated by elgg_get_calling_plugin_id()', 1.8); - - return elgg_get_calling_plugin_id($mainfilename); -} - -/** - * Load and parse a plugin manifest from a plugin XML file. - * - * @example plugins/manifest.xml Example 1.8-style manifest file. - * - * @deprecated 1.8 - * - * @param string $plugin Plugin name. - * @return array of values - */ -function load_plugin_manifest($plugin) { - elgg_deprecated_notice('load_plugin_manifest() is deprecated by ElggPlugin->getManifest()', 1.8); - - $xml_file = elgg_get_plugin_path() . "$plugin/manifest.xml"; - - try { - $manifest = new ElggPluginManifest($xml_file, $plugin); - } catch(Exception $e) { - return false; - } - - return $manifest->getManifest(); -} - -/** - * This function checks a plugin manifest 'elgg_version' value against the current install - * returning TRUE if the elgg_version is >= the current install's version. - * - * @deprecated 1.8 - * - * @param string $manifest_elgg_version_string The build version (eg 2009010201). - * @return bool - */ -function check_plugin_compatibility($manifest_elgg_version_string) { - elgg_deprecated_notice('check_plugin_compatibility() is deprecated by ElggPlugin->canActivate()', 1.8); - - $version = get_version(); - - if (strpos($manifest_elgg_version_string, '.') === false) { - // Using version - $req_version = (int)$manifest_elgg_version_string; - - return ($version >= $req_version); - } - - return false; -} - -/** * Returns an array of all provides from all active plugins. * * Array in the form array( @@ -677,24 +582,27 @@ function check_plugin_compatibility($manifest_elgg_version_string) { * @param string $name A specific provided name to return. Requires $provide_type. * * @return array - * @since 1.8 + * @since 1.8.0 + * @access private */ function elgg_get_plugins_provides($type = null, $name = null) { static $provides = null; - $active_plugins = get_installed_plugins('enabled'); + $active_plugins = elgg_get_plugins('active'); if (!isset($provides)) { $provides = array(); - foreach ($active_plugins as $plugin_id => $plugin_info) { - // @todo remove this when fully converted to ElggPluginPackage. - $package = new ElggPluginPackage($plugin_id); - - if ($plugin_provides = $package->getManifest()->getProvides()) { + foreach ($active_plugins as $plugin) { + $plugin_provides = array(); + $manifest = $plugin->getManifest(); + if ($manifest instanceof ElggPluginManifest) { + $plugin_provides = $plugin->getManifest()->getProvides(); + } + if ($plugin_provides) { foreach ($plugin_provides as $provided) { $provides[$provided['type']][$provided['name']] = array( 'version' => $provided['version'], - 'provided_by' => $plugin_id + 'provided_by' => $plugin->getID() ); } } @@ -731,28 +639,27 @@ function elgg_get_plugins_provides($type = null, $name = null) { * 'status' => bool Does the provide exist?, * 'value' => string The version provided * ) - * @since 1.8 + * @since 1.8.0 + * @access private */ function elgg_check_plugins_provides($type, $name, $version = null, $comparison = 'ge') { - if (!$provided = elgg_get_plugins_provides($type, $name)) { + $provided = elgg_get_plugins_provides($type, $name); + if (!$provided) { return array( 'status' => false, 'version' => '' ); } - if ($provided) { - $version = $provided['version']; - if ($version) { - $status = version_compare($provided['version'], $version, $comparison); - } else { - $status = true; - } + if ($version) { + $status = version_compare($provided['version'], $version, $comparison); + } else { + $status = true; } return array( - 'status' => $r, - 'value' => $version + 'status' => $status, + 'value' => $provided['version'] ); } @@ -768,11 +675,13 @@ function elgg_check_plugins_provides($type, $name, $version = null, $comparison * * @param array $dep An ElggPluginPackage dependency array * @return array + * @since 1.8.0 + * @access private */ function elgg_get_plugin_dependency_strings($dep) { - $dep_system = elgg_get_array_value('type', $dep); - $info = elgg_get_array_value('dep', $dep); - $type = elgg_get_array_value('type', $info); + $dep_system = elgg_extract('type', $dep); + $info = elgg_extract('dep', $dep); + $type = elgg_extract('type', $info); if (!$dep_system || !$info || !$type) { return false; @@ -801,7 +710,9 @@ function elgg_get_plugin_dependency_strings($dep) { 'requires' 'plugin oauth_lib' <1.3 1.3 'downgrade' 'requires' 'php setting bob' >3 3 'change it' 'conflicts' 'php setting' >3 4 'change it' + 'conflicted''plugin profile' any 1.8 'disable profile' 'provides' 'plugin oauth_lib' 1.3 -- -- + 'priority' 'before blog' -- after 'move it' */ $strings = array(); $strings['type'] = elgg_echo('ElggPlugin:Dependencies:' . ucwords($dep_system)); @@ -811,7 +722,7 @@ function elgg_get_plugin_dependency_strings($dep) { case 'elgg_release': // 'Elgg Version' $strings['name'] = elgg_echo('ElggPlugin:Dependencies:Elgg'); - $strings['value'] = "$comparison {$info['version']}"; + $strings['expected_value'] = "$comparison {$info['version']}"; $strings['local_value'] = $dep['value']; $strings['comment'] = ''; break; @@ -820,10 +731,10 @@ function elgg_get_plugin_dependency_strings($dep) { // PHP Extension %s [version] $strings['name'] = elgg_echo('ElggPlugin:Dependencies:PhpExtension', array($info['name'])); if ($info['version']) { - $strings['value'] = "$comparison {$info['version']}"; + $strings['expected_value'] = "$comparison {$info['version']}"; $strings['local_value'] = $dep['value']; } else { - $strings['value'] = ''; + $strings['expected_value'] = ''; $strings['local_value'] = ''; } $strings['comment'] = ''; @@ -831,52 +742,52 @@ function elgg_get_plugin_dependency_strings($dep) { case 'php_ini': $strings['name'] = elgg_echo('ElggPlugin:Dependencies:PhpIni', array($info['name'])); - $strings['value'] = "$comparison {$info['value']}"; + $strings['expected_value'] = "$comparison {$info['value']}"; $strings['local_value'] = $dep['value']; $strings['comment'] = ''; break; case 'plugin': $strings['name'] = elgg_echo('ElggPlugin:Dependencies:Plugin', array($info['name'])); - $strings['value'] = "$comparison {$info['version']}"; - $strings['local_value'] = $dep['version']; + $expected = $info['version'] ? "$comparison {$info['version']}" : elgg_echo('any'); + $strings['expected_value'] = $expected; + $strings['local_value'] = $dep['value'] ? $dep['value'] : '--'; $strings['comment'] = ''; break; - } - if ($dep['status']) { - $strings['comment'] = elgg_echo('ok'); + case 'priority': + $expected_priority = ucwords($info['priority']); + $real_priority = ucwords($dep['value']); + $strings['name'] = elgg_echo('ElggPlugin:Dependencies:Priority'); + $strings['expected_value'] = elgg_echo("ElggPlugin:Dependencies:Priority:$expected_priority", array($info['plugin'])); + $strings['local_value'] = elgg_echo("ElggPlugin:Dependencies:Priority:$real_priority", array($info['plugin'])); + $strings['comment'] = ''; + break; } - return $strings; -} - - - -/** - * Shorthand function for finding the plugin settings. - * - * @deprecated 1.8 - * - * @param string $plugin_id Optional plugin id, if not specified - * then it is detected from where you are calling. - * - * @return mixed - */ -function find_plugin_settings($plugin_id = null) { - elgg_deprecated_notice('find_plugin_setting() is deprecated by elgg_get_calling_plugin_entity() or elgg_get_plugin_from_id()', 1.8); - if ($plugin_id) { - return elgg_get_plugin_from_id($plugin_id); + if ($dep['type'] == 'suggests') { + if ($dep['status']) { + $strings['comment'] = elgg_echo('ok'); + } else { + $strings['comment'] = elgg_echo('ElggPlugin:Dependencies:Suggests:Unsatisfied'); + } } else { - return elgg_get_calling_plugin_entity(); + if ($dep['status']) { + $strings['comment'] = elgg_echo('ok'); + } else { + $strings['comment'] = elgg_echo('error'); + } } + + return $strings; } /** * Returns the ElggPlugin entity of the last plugin called. * * @return mixed ElggPlugin or false - * @since 1.8 + * @since 1.8.0 + * @access private */ function elgg_get_calling_plugin_entity() { $plugin_id = elgg_get_calling_plugin_id(); @@ -889,51 +800,39 @@ function elgg_get_calling_plugin_entity() { } /** - * Find the plugin settings for a user. - * - * @param string $plugin_id Plugin name. - * @param int $user_guid The guid who's settings to retrieve. + * Returns an array of all plugin settings for a user. * - * @return array of settings in an associative array minus prefix. + * @param mixed $user_guid The user GUID or null for the currently logged in user. + * @param string $plugin_id The plugin ID + * @param bool $return_obj Return settings as an object? This can be used to in reusable + * views where the settings are passed as $vars['entity']. + * @return array + * @since 1.8.0 */ -function find_plugin_usersettings($plugin_id = null, $user_guid = 0) { - $plugin_id = sanitise_string($plugin_id); - $user_guid = (int)$user_guid; - $db_prefix = get_config('db_prefix'); - $ps_prefix = elgg_namespace_plugin_private_setting('user_setting', "$plugin_id:"); - $ps_prefix_len = strlen($ps_prefix); - - if (!$plugin_id) { - $plugin_id = elgg_get_calling_plugin_id(); +function elgg_get_all_plugin_user_settings($user_guid = null, $plugin_id = null, $return_obj = false) { + if ($plugin_id) { + $plugin = elgg_get_plugin_from_id($plugin_id); + } else { + $plugin = elgg_get_calling_plugin_entity(); } - if ($user_guid == 0) { - $user_guid = get_loggedin_userid(); + if (!$plugin instanceof ElggPlugin) { + return false; } - // Get private settings for user - $q = "SELECT * FROM {$db_prefix}private_settings - WHERE entity_guid = $user_guid - AND name LIKE '$ps_prefix$plugin_id'"; + $settings = $plugin->getAllUserSettings($user_guid); - $private_settings = get_data($q); - if ($private_settings) { + if ($settings && $return_obj) { $return = new stdClass; - foreach ($private_settings as $setting) { - $name = substr($setting->name, $ps_prefix_len); - $value = $setting->value; - - // @todo why? - if (strpos($key, $ps_prefix) === 0) { - $return->$name = $value; - } + foreach ($settings as $k => $v) { + $return->$k = $v; } return $return; + } else { + return $settings; } - - return false; } /** @@ -946,112 +845,83 @@ function find_plugin_usersettings($plugin_id = null, $user_guid = 0) { * is detected from where you are calling from. * * @return bool + * @since 1.8.0 */ -function set_plugin_usersetting($name, $value, $user_guid = 0, $plugin_id = "") { - $plugin_id = sanitise_string($plugin_id); - $user_guid = (int)$user_guid; - $name = sanitise_string($name); - - if (!$plugin_id) { - $plugin_id = elgg_get_calling_plugin_id(); - } - - $user = get_entity($user_guid); - if (!$user) { - $user = get_loggedin_user(); +function elgg_set_plugin_user_setting($name, $value, $user_guid = null, $plugin_id = null) { + if ($plugin_id) { + $plugin = elgg_get_plugin_from_id($plugin_id); + } else { + $plugin = elgg_get_calling_plugin_entity(); } - if (($user) && ($user instanceof ElggUser)) { - $name = elgg_namespace_plugin_private_setting('user_setting', "$plugin_id:$name"); - - // Hook to validate setting - $value = elgg_trigger_plugin_hook('plugin:usersetting', 'user', array( - 'user' => $user, - 'plugin' => $plugin_id, - 'name' => $name, - 'value' => $value - ), $value); - - return set_private_setting($user->guid, $name, $value); + if (!$plugin) { + return false; } - return false; + return $plugin->setUserSetting($name, $value, $user_guid); } /** - * Clears a user-specific plugin setting + * Unsets a user-specific plugin setting * - * @param str $name Name of the plugin setting - * @param int $user_guid Defaults to logged in user - * @param str $plugin_id Defaults to contextual plugin name + * @param string $name Name of the setting + * @param int $user_guid Defaults to logged in user + * @param string $plugin_id Defaults to contextual plugin name * - * @return bool Success + * @return bool + * @since 1.8.0 */ -function clear_plugin_usersetting($name, $user_guid = 0, $plugin_id = '') { - $plugin_id = sanitise_string($plugin_id); - $name = sanitise_string($name); - - if (!$plugin_id) { - $plugin_id = elgg_get_calling_plugin_id(); - } - - $user = get_entity((int) $user_guid); - if (!$user) { - $user = get_loggedin_user(); +function elgg_unset_plugin_user_setting($name, $user_guid = null, $plugin_id = null) { + if ($plugin_id) { + $plugin = elgg_get_plugin_from_id($plugin_id); + } else { + $plugin = elgg_get_calling_plugin_entity(); } - if (($user) && ($user instanceof ElggUser)) { - $prefix = elgg_namespace_plugin_private_setting('user_setting', "$plugin_id:$name"); - - return remove_private_setting($user->getGUID(), $prefix); + if (!$plugin) { + return false; } - return FALSE; + return $plugin->unsetUserSetting($name, $user_guid); } /** * Get a user specific setting for a plugin. * - * @param string $name The name. + * @param string $name The name of the setting. * @param int $user_guid Guid of owning user * @param string $plugin_id Optional plugin name, if not specified - * then it is detected from where you are calling from. + * it is detected from where you are calling. * * @return mixed + * @since 1.8.0 */ -function get_plugin_usersetting($name, $user_guid = 0, $plugin_id = "") { - $plugin_id = sanitise_string($plugin_id); - $user_guid = (int)$user_guid; - $name = sanitise_string($name); - - if (!$plugin_id) { - $plugin_id = elgg_get_calling_plugin_id(); - } - - $user = get_entity($user_guid); - if (!$user) { - $user = get_loggedin_user(); +function elgg_get_plugin_user_setting($name, $user_guid = null, $plugin_id = null) { + if ($plugin_id) { + $plugin = elgg_get_plugin_from_id($plugin_id); + } else { + $plugin = elgg_get_calling_plugin_entity(); } - if (($user) && ($user instanceof ElggUser)) { - $name = elgg_namespace_plugin_private_setting('user_setting', "$plugin_id:$name"); - return get_private_setting($user->guid, $name); + if (!$plugin) { + return false; } - return false; + return $plugin->getUserSetting($name, $user_guid); } /** * Set a setting for a plugin. * - * @param string $name The name - note, can't be "title". + * @param string $name The name of the setting - note, can't be "title". * @param mixed $value The value. * @param string $plugin_id Optional plugin name, if not specified * then it is detected from where you are calling from. * - * @return int|false + * @return bool + * @since 1.8.0 */ -function set_plugin_setting($name, $value, $plugin_id = null) { +function elgg_set_plugin_setting($name, $value, $plugin_id = null) { if ($plugin_id) { $plugin = elgg_get_plugin_from_id($plugin_id); } else { @@ -1068,13 +938,15 @@ function set_plugin_setting($name, $value, $plugin_id = null) { /** * Get setting for a plugin. * - * @param string $name The name. + * @param string $name The name of the setting. * @param string $plugin_id Optional plugin name, if not specified * then it is detected from where you are calling from. * * @return mixed + * @since 1.8.0 + * @todo make $plugin_id required in future version */ -function get_plugin_setting($name, $plugin_id = "") { +function elgg_get_plugin_setting($name, $plugin_id = null) { if ($plugin_id) { $plugin = elgg_get_plugin_from_id($plugin_id); } else { @@ -1089,15 +961,16 @@ function get_plugin_setting($name, $plugin_id = "") { } /** - * Clear a plugin setting. + * Unsets a plugin setting. * - * @param string $name The name. + * @param string $name The name of the setting. * @param string $plugin_id Optional plugin name, if not specified * then it is detected from where you are calling from. * * @return bool + * @since 1.8.0 */ -function clear_plugin_setting($name, $plugin_id = "") { +function elgg_unset_plugin_setting($name, $plugin_id = null) { if ($plugin_id) { $plugin = elgg_get_plugin_from_id($plugin_id); } else { @@ -1108,194 +981,122 @@ function clear_plugin_setting($name, $plugin_id = "") { return false; } - return $plugin->removeSetting($name); + return $plugin->unsetSetting($name); } /** - * Clear all plugin settings. + * Unsets all plugin settings for a plugin. * * @param string $plugin_id Optional plugin name, if not specified * then it is detected from where you are calling from. * * @return bool - * @since 1.7.0 + * @since 1.8.0 */ -function clear_all_plugin_settings($plugin_id = "") { +function elgg_unset_all_plugin_settings($plugin_id = null) { if ($plugin_id) { $plugin = elgg_get_plugin_from_id($plugin_id); } else { $plugin = elgg_get_calling_plugin_entity(); } - if ($plugin) { - $plugin->removeAllSettings(); + if (!$plugin) { + return false; } - return false; + return $plugin->unsetAllSettings(); } /** - * Return an array of installed plugins. + * Returns entities based upon plugin settings. + * Takes all the options for {@see elgg_get_entities_from_private_settings()} + * in addition to the ones below. * - * @deprecated 1.8 + * @param array $options Array in the format: * - * @param string $status any|enabled|disabled - * @return array - */ -function get_installed_plugins($status = 'all') { - global $CONFIG; - - elgg_deprecated_notice('get_installed_plugins() was deprecated by elgg_get_plugins()', 1.8); - - $plugins = elgg_get_plugins($status); - - if (!$plugins) { - return array(); - } - - $installed_plugins = array(); - - foreach ($plugins as $plugin) { - if (!$plugin->isValid()) { - continue; - } - - $installed_plugins[$plugin->getID()] = array( - 'active' => $plugin->isActive(), - 'manifest' => $plugin->manifest->getManifest() - ); - } - - return $installed_plugins; -} - -/** - * Enable a plugin for a site (default current site) + * plugin_id => NULL|STR The plugin id. Defaults to calling plugin * - * Important: You should regenerate simplecache and the viewpath cache after executing this function - * otherwise you may experience view display artifacts. Do this with the following code: + * plugin_user_setting_names => NULL|ARR private setting names * - * elgg_view_regenerate_simplecache(); - * elgg_filepath_cache_reset(); + * plugin_user_setting_values => NULL|ARR metadata values * - * @deprecated 1.8 + * plugin_user_setting_name_value_pairs => NULL|ARR ( + * name => 'name', + * value => 'value', + * 'operand' => '=', + * ) + * Currently if multiple values are sent via + * an array (value => array('value1', 'value2') + * the pair's operand will be forced to "IN". * - * @param string $plugin The plugin name. - * @param int $site_guid The site id, if not specified then this is detected. + * plugin_user_setting_name_value_pairs_operator => NULL|STR The operator to use for combining + * (name = value) OPERATOR (name = value); default AND * - * @return array - * @throws InvalidClassException + * @return mixed int If count, int. If not count, array. false on errors. */ -function enable_plugin($plugin, $site_guid = null) { - elgg_deprecated_notice('enable_plugin() was deprecated by ElggPlugin->activate()', 1.8); - - $plugin = sanitise_string($plugin); - - $site_guid = (int) $site_guid; - if (!$site_guid) { - $site = get_config('site'); - $site_guid = $site->guid; +function elgg_get_entities_from_plugin_user_settings(array $options = array()) { + // if they're passing it don't bother + if (!isset($options['plugin_id'])) { + $options['plugin_id'] = elgg_get_calling_plugin_id(); } - try { - $plugin = new ElggPlugin($plugin); - } catch(Exception $e) { - return false; - } + $singulars = array('plugin_user_setting_name', 'plugin_user_setting_value', + 'plugin_user_setting_name_value_pair'); - if (!$plugin->canActivate($site_guid)) { - return false; - } + $options = elgg_normalise_plural_options_array($options, $singulars); - return $plugin->activate($site_guid); -} + // rewrite plugin_user_setting_name_* to the right PS ones. + $map = array( + 'plugin_user_setting_names' => 'private_setting_names', + 'plugin_user_setting_values' => 'private_setting_values', + 'plugin_user_setting_name_value_pairs' => 'private_setting_name_value_pairs', + 'plugin_user_setting_name_value_pairs_operator' => 'private_setting_name_value_pairs_operator' + ); -/** - * Disable a plugin for a site (default current site) - * - * Important: You should regenerate simplecache and the viewpath cache after executing this function - * otherwise you may experience view display artifacts. Do this with the following code: - * - * elgg_view_regenerate_simplecache(); - * elgg_filepath_cache_reset(); - * - * @deprecated 1.8 - * - * @param string $plugin The plugin name. - * @param int $site_guid The site id, if not specified then this is detected. - * - * @return bool - * @throws InvalidClassException - */ -function disable_plugin($plugin, $site_guid = 0) { - elgg_deprecated_notice('disable_plugin() was deprecated by ElggPlugin->deactivate()', 1.8); + foreach ($map as $plugin => $private) { + if (!isset($options[$plugin])) { + continue; + } - $plugin = sanitise_string($plugin); + if (isset($options[$private])) { + if (!is_array($options[$private])) { + $options[$private] = array($options[$private]); + } - $site_guid = (int) $site_guid; - if (!$site_guid) { - $site = get_config('site'); - $site_guid = $site->guid; + $options[$private] = array_merge($options[$private], $options[$plugin]); + } else { + $options[$private] = $options[$plugin]; + } } - try { - $plugin = new ElggPlugin($plugin); - } catch(Exception $e) { - return false; - } - return $plugin->deactivate($site_guid); -} + $plugin_id = $options['plugin_id']; + $prefix = elgg_namespace_plugin_private_setting('user_setting', '', $plugin_id); + $options['private_setting_name_prefix'] = $prefix; -/** - * Return whether a plugin is enabled or not. - * - * @deprecated 1.8 - * - * @param string $plugin The plugin name. - * @param int $site_guid The site id, if not specified then this is detected. - * - * @return bool - */ -function is_plugin_enabled($plugin, $site_guid = 0) { - elgg_deprecated_notice('is_plugin_enabled() was deprecated by ElggPlugin->isActive()', 1.8); - - $plugin = sanitise_string($plugin); - - $site_guid = (int) $site_guid; - if (!$site_guid) { - $site = get_config('site'); - $site_guid = $site->guid; - } - - try { - $plugin = new ElggPlugin($plugin); - } catch(Exception $e) { - return false; - } - - return $plugin->isActive($site_guid); + return elgg_get_entities_from_private_settings($options); } /** * Register object, plugin entities as ElggPlugin classes * - * @return void + * @return void + * @access private */ function plugin_run_once() { add_subtype("object", "plugin", "ElggPlugin"); } - /** * Runs unit tests for the entity objects. * - * @param sting $hook unit_test + * @param string $hook unit_test * @param string $type system * @param mixed $value Array of tests * @param mixed $params Params * * @return array + * @access private */ function plugins_test($hook, $type, $value, $params) { global $CONFIG; @@ -1304,15 +1105,63 @@ function plugins_test($hook, $type, $value, $params) { } /** - * Initialise the file modules. - * Listens to system boot and registers any appropriate file types and classes + * Checks on deactivate plugin event if disabling it won't create unmet dependencies and blocks disable in such case. + * + * @param string $event deactivate + * @param string $type plugin + * @param array $params Parameters array containing entry with ELggPlugin instance under 'plugin_entity' key + * @return bool false to block plugin deactivation action + * + * @access private + */ +function _plugins_deactivate_dependency_check($event, $type, $params) { + $plugin_id = $params['plugin_entity']->getManifest()->getPluginID(); + $plugin_name = $params['plugin_entity']->getManifest()->getName(); + + $active_plugins = elgg_get_plugins(); + + $dependents = array(); + foreach ($active_plugins as $plugin) { + $manifest = $plugin->getManifest(); + $requires = $manifest->getRequires(); + + foreach ($requires as $required) { + if ($required['type'] == 'plugin' && $required['name'] == $plugin_id) { + // there are active dependents + $dependents[$manifest->getPluginID()] = $plugin; + } + } + } + + if ($dependents) { + $list = '<ul>'; + // construct error message and prevent disabling + foreach ($dependents as $dependent) { + $list .= '<li>' . $dependent->getManifest()->getName() . '</li>'; + } + $list .= '</ul>'; + + register_error(elgg_echo('ElggPlugin:Dependencies:ActiveDependent', array($plugin_name, $list))); + + return false; + } +} + +/** + * Initialize the plugin system + * Listens to system init and registers actions * * @return void + * @access private */ function plugin_init() { run_function_once("plugin_run_once"); elgg_register_plugin_hook_handler('unit_test', 'system', 'plugins_test'); + + // note - plugins are booted by the time this handler is registered + // deactivation due to error may have already occurred + elgg_register_event_handler('deactivate', 'plugin', '_plugins_deactivate_dependency_check'); elgg_register_action("plugins/settings/save", '', 'admin'); elgg_register_action("plugins/usersettings/save"); @@ -1323,6 +1172,8 @@ function plugin_init() { elgg_register_action('admin/plugins/deactivate_all', '', 'admin'); elgg_register_action('admin/plugins/set_priority', '', 'admin'); + + elgg_register_library('elgg:markdown', elgg_get_root_path() . 'vendors/markdown/markdown.php'); } -elgg_register_event_handler('init', 'system', 'plugin_init');
\ No newline at end of file +elgg_register_event_handler('init', 'system', 'plugin_init'); |
