123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059 |
- <?php
- namespace Elgg\Database;
- use Exception;
- global $ELGG_PLUGINS_PROVIDES_CACHE;
- class Plugins {
-
- protected $active_ids = array();
-
- protected $active_ids_known = false;
-
- protected $plugins_by_id;
-
- public function __construct(\Elgg\EventsService $events, \Elgg\Cache\MemoryPool $pool) {
- $this->plugins_by_id = $pool;
- }
-
- function getDirsInDir($dir = null) {
- if (!$dir) {
- $dir = elgg_get_plugins_path();
- }
-
- $plugin_dirs = array();
- $handle = opendir($dir);
-
- if ($handle) {
- while ($plugin_dir = readdir($handle)) {
-
- if (substr($plugin_dir, 0, 1) !== '.' && is_dir($dir . $plugin_dir)) {
- $plugin_dirs[] = $plugin_dir;
- }
- }
- }
-
- sort($plugin_dirs);
-
- return $plugin_dirs;
- }
-
-
- function generateEntities() {
-
- $mod_dir = elgg_get_plugins_path();
- $db_prefix = elgg_get_config('dbprefix');
-
-
- $old_ia = elgg_set_ignore_access(true);
-
-
- $old_access = access_get_show_hidden_status();
- access_show_hidden_entities(true);
-
- $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,
- );
- $known_plugins = elgg_get_entities_from_relationship($options);
-
-
- if (!$known_plugins) {
- $known_plugins = array();
- }
-
-
- $id_map = array();
- foreach ($known_plugins as $i => $plugin) {
-
- $id = $plugin->getID();
- if (!$id) {
- $plugin->delete();
- unset($known_plugins[$i]);
- continue;
- }
- $id_map[$plugin->getID()] = $i;
- }
-
- $physical_plugins = _elgg_get_plugin_dirs_in_dir($mod_dir);
-
- if (!$physical_plugins) {
- return false;
- }
-
-
- foreach ($physical_plugins as $plugin_id) {
-
- if (array_key_exists($plugin_id, $id_map)) {
- $index = $id_map[$plugin_id];
- $plugin = $known_plugins[$index];
-
- if (!$plugin->isEnabled()) {
- $plugin->enable();
- $plugin->deactivate();
- $plugin->setPriority('last');
- }
-
-
- unset($known_plugins[$index]);
- } else {
-
-
- $plugin = new \ElggPlugin($mod_dir . $plugin_id);
- $plugin->save();
- }
- }
-
-
-
-
- foreach ($known_plugins as $plugin) {
- if ($plugin->isActive()) {
- $plugin->deactivate();
- }
-
- $name = _elgg_namespace_plugin_private_setting('internal', 'priority');
- remove_private_setting($plugin->guid, $name);
- if ($plugin->isEnabled()) {
- $plugin->disable();
- }
- }
-
- access_show_hidden_entities($old_access);
- elgg_set_ignore_access($old_ia);
-
- _elgg_reindex_plugin_priorities();
-
- return true;
- }
-
-
- function cache(\ElggPlugin $plugin) {
- $this->plugins_by_id->put($plugin->getID(), $plugin);
- }
-
-
- function get($plugin_id) {
- return $this->plugins_by_id->get($plugin_id, function () use ($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"),
- 'selects' => array("oe.title", "oe.description"),
- 'wheres' => array("oe.title = '$plugin_id'"),
- 'limit' => 1,
- 'distinct' => false,
- );
- $plugins = elgg_get_entities($options);
- if ($plugins) {
- return $plugins[0];
- }
- return null;
- });
- }
-
-
- function exists($id) {
- $plugin = elgg_get_plugin_from_id($id);
-
- return ($plugin) ? true : false;
- }
-
-
- function getMaxPriority() {
- $db_prefix = get_config('dbprefix');
- $priority = _elgg_namespace_plugin_private_setting('internal', 'priority');
- $plugin_subtype = get_subtype_id('object', 'plugin');
-
- $q = "SELECT MAX(CAST(ps.value AS unsigned)) as max
- FROM {$db_prefix}entities e, {$db_prefix}private_settings ps
- WHERE ps.name = '$priority'
- AND ps.entity_guid = e.guid
- AND e.type = 'object' and e.subtype = $plugin_subtype";
-
- $data = get_data($q);
- if ($data) {
- $max = $data[0]->max;
- } else {
- $max = 1;
- }
-
-
- return ($max) ? $max : 1;
- }
-
-
- function isActive($plugin_id, $site_guid = null) {
- $current_site_guid = elgg_get_site_entity()->guid;
- if ($this->active_ids_known
- && ($site_guid === null || $site_guid == $current_site_guid)) {
- return isset($this->active_ids[$plugin_id]);
- }
- 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);
- }
-
-
- function load() {
- $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;
- }
-
-
- if (file_exists("$plugins_path/disabled")) {
- if (elgg_is_admin_logged_in() && elgg_in_context('admin')) {
- system_message(_elgg_services()->translator->translate('plugins:disabled'));
- }
- return false;
- }
-
- 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 = $this->find('active');
- if ($plugins) {
- foreach ($plugins as $plugin) {
- $id = $plugin->getID();
- try {
- $plugin->start($start_flags);
- $this->active_ids[$id] = true;
- } catch (Exception $e) {
- $plugin->deactivate();
- $msg = _elgg_services()->translator->translate('PluginException:CannotStart',
- array($id, $plugin->guid, $e->getMessage()));
- elgg_add_admin_notice("cannot_start $id", $msg);
- $return = false;
-
- continue;
- }
- }
- }
- $this->active_ids_known = true;
- return $return;
- }
-
-
- function find($status = 'active', $site_guid = null) {
- $db_prefix = get_config('dbprefix');
- $priority = _elgg_namespace_plugin_private_setting('internal', 'priority');
-
- if (!$site_guid) {
- $site = get_config('site');
- $site_guid = $site->guid;
- }
-
-
- $options = array(
- 'type' => 'object',
- 'subtype' => 'plugin',
- 'limit' => ELGG_ENTITIES_NO_VALUE,
- '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",
- 'distinct' => false,
- );
-
- switch ($status) {
- case 'active':
- $options['relationship'] = 'active_plugin';
- $options['relationship_guid'] = $site_guid;
- $options['inverse_relationship'] = true;
- break;
-
- case 'inactive':
- $options['wheres'][] = "NOT EXISTS (
- SELECT 1 FROM {$db_prefix}entity_relationships active_er
- WHERE active_er.guid_one = e.guid
- AND active_er.relationship = 'active_plugin'
- AND active_er.guid_two = $site_guid)";
- break;
-
- case 'all':
- default:
- break;
- }
-
- $old_ia = elgg_set_ignore_access(true);
- $plugins = elgg_get_entities_from_relationship($options);
- elgg_set_ignore_access($old_ia);
-
- return $plugins;
- }
-
-
- function setPriorities(array $order) {
- $name = _elgg_namespace_plugin_private_setting('internal', 'priority');
-
- $plugins = elgg_get_plugins('any');
- if (!$plugins) {
- return false;
- }
-
- $return = true;
-
-
-
- $order = array_values($order);
-
- $missing_plugins = array();
-
-
- foreach ($plugins as $plugin) {
- $plugin_id = $plugin->getID();
-
- if (!in_array($plugin_id, $order)) {
- $missing_plugins[] = $plugin;
- continue;
- }
-
- $priority = array_search($plugin_id, $order) + 1;
-
- if (!$plugin->setPrivateSetting($name, $priority)) {
- $return = false;
- break;
- }
- }
-
-
- if ($return && $missing_plugins) {
- if (!isset($priority)) {
- $priority = 0;
- }
- foreach ($missing_plugins as $plugin) {
- $priority++;
- if (!$plugin->setPrivateSetting($name, $priority)) {
- $return = false;
- break;
- }
- }
- }
-
- return $return;
- }
-
-
- function reindexPriorities() {
- return _elgg_set_plugin_priorities(array());
- }
-
-
- function namespacePrivateSetting($type, $name, $id = null) {
- switch ($type) {
-
-
-
-
-
- case 'user_setting':
- if (!$id) {
- elgg_deprecated_notice("You must pass the plugin id to _elgg_namespace_plugin_private_setting() for user settings", 1.9);
- $id = elgg_get_calling_plugin_id();
- }
- $name = ELGG_PLUGIN_USER_SETTING_PREFIX . "$id:$name";
- break;
-
- case 'internal':
- $name = ELGG_PLUGIN_INTERNAL_PREFIX . $name;
- break;
- }
-
- return $name;
- }
-
-
-
- function getProvides($type = null, $name = null) {
- global $ELGG_PLUGINS_PROVIDES_CACHE;
- if (!isset($ELGG_PLUGINS_PROVIDES_CACHE)) {
- $active_plugins = elgg_get_plugins('active');
-
- $provides = array();
-
- 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->getID()
- );
- }
- }
- }
-
- $ELGG_PLUGINS_PROVIDES_CACHE = $provides;
- }
-
- if ($type && $name) {
- if (isset($ELGG_PLUGINS_PROVIDES_CACHE[$type][$name])) {
- return $ELGG_PLUGINS_PROVIDES_CACHE[$type][$name];
- } else {
- return false;
- }
- } elseif ($type) {
- if (isset($ELGG_PLUGINS_PROVIDES_CACHE[$type])) {
- return $ELGG_PLUGINS_PROVIDES_CACHE[$type];
- } else {
- return false;
- }
- }
-
- return $ELGG_PLUGINS_PROVIDES_CACHE;
- }
-
-
- function invalidateProvidesCache() {
- global $ELGG_PLUGINS_PROVIDES_CACHE;
- $ELGG_PLUGINS_PROVIDES_CACHE = null;
- return true;
- }
-
- public function invalidateIsActiveCache() {
- $this->active_ids = array();
- $this->active_ids_known = false;
- }
-
-
- function checkProvides($type, $name, $version = null, $comparison = 'ge') {
- $provided = _elgg_get_plugins_provides($type, $name);
- if (!$provided) {
- return array(
- 'status' => false,
- 'value' => ''
- );
- }
-
- if ($version) {
- $status = version_compare($provided['version'], $version, $comparison);
- } else {
- $status = true;
- }
-
- return array(
- 'status' => $status,
- 'value' => $provided['version']
- );
- }
-
-
- function getDependencyStrings($dep) {
- $translator = _elgg_services()->translator;
- $dep_system = elgg_extract('type', $dep);
- $info = elgg_extract('dep', $dep);
- $type = elgg_extract('type', $info);
-
- if (!$dep_system || !$info || !$type) {
- return false;
- }
-
-
- $comparison = elgg_extract('comparison', $info);
- switch($comparison) {
- case 'lt':
- $comparison = '<';
- break;
- case 'gt':
- $comparison = '>';
- break;
- case 'ge':
- $comparison = '>=';
- break;
- case 'le':
- $comparison = '<=';
- break;
- default:
-
- break;
- }
-
-
- $strings = array();
- $strings['type'] = $translator->translate('ElggPlugin:Dependencies:' . ucwords($dep_system));
-
- switch ($type) {
- case 'elgg_version':
- case 'elgg_release':
-
- $strings['name'] = $translator->translate('ElggPlugin:Dependencies:Elgg');
- $strings['expected_value'] = "$comparison {$info['version']}";
- $strings['local_value'] = $dep['value'];
- $strings['comment'] = '';
- break;
-
- case 'php_version':
-
- $strings['name'] = $translator->translate('ElggPlugin:Dependencies:PhpVersion');
- $strings['expected_value'] = "$comparison {$info['version']}";
- $strings['local_value'] = $dep['value'];
- $strings['comment'] = '';
- break;
-
- case 'php_extension':
-
- $strings['name'] = $translator->translate('ElggPlugin:Dependencies:PhpExtension', array($info['name']));
- if ($info['version']) {
- $strings['expected_value'] = "$comparison {$info['version']}";
- $strings['local_value'] = $dep['value'];
- } else {
- $strings['expected_value'] = '';
- $strings['local_value'] = '';
- }
- $strings['comment'] = '';
- break;
-
- case 'php_ini':
- $strings['name'] = $translator->translate('ElggPlugin:Dependencies:PhpIni', array($info['name']));
- $strings['expected_value'] = "$comparison {$info['value']}";
- $strings['local_value'] = $dep['value'];
- $strings['comment'] = '';
- break;
-
- case 'plugin':
- $strings['name'] = $translator->translate('ElggPlugin:Dependencies:Plugin', array($info['name']));
- $expected = $info['version'] ? "$comparison {$info['version']}" : $translator->translate('any');
- $strings['expected_value'] = $expected;
- $strings['local_value'] = $dep['value'] ? $dep['value'] : '--';
- $strings['comment'] = '';
- break;
-
- case 'priority':
- $expected_priority = ucwords($info['priority']);
- $real_priority = ucwords($dep['value']);
- $strings['name'] = $translator->translate('ElggPlugin:Dependencies:Priority');
- $strings['expected_value'] = $translator->translate("ElggPlugin:Dependencies:Priority:$expected_priority", array($info['plugin']));
- $strings['local_value'] = $translator->translate("ElggPlugin:Dependencies:Priority:$real_priority", array($info['plugin']));
- $strings['comment'] = '';
- break;
- }
-
- if ($dep['type'] == 'suggests') {
- if ($dep['status']) {
- $strings['comment'] = $translator->translate('ok');
- } else {
- $strings['comment'] = $translator->translate('ElggPlugin:Dependencies:Suggests:Unsatisfied');
- }
- } else {
- if ($dep['status']) {
- $strings['comment'] = $translator->translate('ok');
- } else {
- $strings['comment'] = $translator->translate('error');
- }
- }
-
- return $strings;
- }
-
-
- function getAllUserSettings($user_guid = 0, $plugin_id = null, $return_obj = false) {
- if ($plugin_id) {
- $plugin = elgg_get_plugin_from_id($plugin_id);
- } else {
- elgg_deprecated_notice('elgg_get_all_plugin_user_settings() requires plugin_id to be set', 1.9);
- $plugin = elgg_get_calling_plugin_entity();
- }
-
- if (!$plugin instanceof \ElggPlugin) {
- return false;
- }
-
- $settings = $plugin->getAllUserSettings((int)$user_guid);
-
- if ($settings && $return_obj) {
- $return = new \stdClass;
-
- foreach ($settings as $k => $v) {
- $return->$k = $v;
- }
-
- return $return;
- } else {
- return $settings;
- }
- }
-
-
- function setUserSetting($name, $value, $user_guid = 0, $plugin_id = null) {
- if ($plugin_id) {
- $plugin = elgg_get_plugin_from_id($plugin_id);
- } else {
- elgg_deprecated_notice('elgg_set_plugin_user_setting() requires plugin_id to be set', 1.9);
- $plugin = elgg_get_calling_plugin_entity();
- }
-
- if (!$plugin) {
- return false;
- }
-
- return $plugin->setUserSetting($name, $value, (int)$user_guid);
- }
-
-
- function unsetUserSetting($name, $user_guid = 0, $plugin_id = null) {
- if ($plugin_id) {
- $plugin = elgg_get_plugin_from_id($plugin_id);
- } else {
- elgg_deprecated_notice('elgg_unset_plugin_user_setting() requires plugin_id to be set', 1.9);
- $plugin = elgg_get_calling_plugin_entity();
- }
-
- if (!$plugin) {
- return false;
- }
-
- return $plugin->unsetUserSetting($name, (int)$user_guid);
- }
-
-
- function getUserSetting($name, $user_guid = 0, $plugin_id = null, $default = null) {
- if ($plugin_id) {
- $plugin = elgg_get_plugin_from_id($plugin_id);
- } else {
- elgg_deprecated_notice('elgg_get_plugin_user_setting() requires plugin_id to be set', 1.9);
- $plugin = elgg_get_calling_plugin_entity();
- }
-
- if (!$plugin) {
- return false;
- }
-
- return $plugin->getUserSetting($name, (int)$user_guid, $default);
- }
-
-
- function setSetting($name, $value, $plugin_id = null) {
- if ($plugin_id) {
- $plugin = elgg_get_plugin_from_id($plugin_id);
- } else {
- elgg_deprecated_notice('elgg_set_plugin_setting() requires plugin_id to be set', 1.9);
- $plugin = elgg_get_calling_plugin_entity();
- }
-
- if (!$plugin) {
- return false;
- }
-
- return $plugin->setSetting($name, $value);
- }
-
-
- function getSetting($name, $plugin_id = null, $default = null) {
- if ($plugin_id) {
- $plugin = elgg_get_plugin_from_id($plugin_id);
- } else {
- elgg_deprecated_notice('elgg_get_plugin_setting() requires plugin_id to be set', 1.9);
- $plugin = elgg_get_calling_plugin_entity();
- }
-
- if (!$plugin) {
- return false;
- }
-
- return $plugin->getSetting($name, $default);
- }
-
-
- function unsetSetting($name, $plugin_id = null) {
- if ($plugin_id) {
- $plugin = elgg_get_plugin_from_id($plugin_id);
- } else {
- elgg_deprecated_notice('elgg_unset_plugin_setting() requires plugin_id to be set', 1.9);
- $plugin = elgg_get_calling_plugin_entity();
- }
-
- if (!$plugin) {
- return false;
- }
-
- return $plugin->unsetSetting($name);
- }
-
-
- function unsetAllSettings($plugin_id = null) {
- if ($plugin_id) {
- $plugin = elgg_get_plugin_from_id($plugin_id);
- } else {
- elgg_deprecated_notice('elgg_unset_all_plugin_settings() requires plugin_id to be set', 1.9);
- $plugin = elgg_get_calling_plugin_entity();
- }
-
- if (!$plugin) {
- return false;
- }
-
- return $plugin->unsetAllSettings();
- }
-
-
- function getEntitiesFromUserSettings(array $options = array()) {
- if (!isset($options['plugin_id'])) {
- elgg_deprecated_notice("'plugin_id' is now required for elgg_get_entities_from_plugin_user_settings()", 1.9);
- $options['plugin_id'] = elgg_get_calling_plugin_id();
- }
-
- $singulars = array('plugin_user_setting_name', 'plugin_user_setting_value',
- 'plugin_user_setting_name_value_pair');
-
- $options = _elgg_normalize_plural_options_array($options, $singulars);
-
-
- $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',
- );
-
- foreach ($map as $plugin => $private) {
- if (!isset($options[$plugin])) {
- continue;
- }
-
- if (isset($options[$private])) {
- if (!is_array($options[$private])) {
- $options[$private] = array($options[$private]);
- }
-
- $options[$private] = array_merge($options[$private], $options[$plugin]);
- } else {
- $options[$private] = $options[$plugin];
- }
- }
-
- $prefix = _elgg_namespace_plugin_private_setting('user_setting', '', $options['plugin_id']);
- $options['private_setting_name_prefix'] = $prefix;
-
- return elgg_get_entities_from_private_settings($options);
- }
- }
|