This class is currently a stub, allowing a plugin to save settings in an object's private settings for each site.
Inheritance: extends ElggObject
Esempio n. 1
0
 public function testIgnoresNonRequiredUpgrade()
 {
     // Mock an upgrade that does not need to be ran
     $this->plugin->expects($this->any())->method('getStaticConfig')->will($this->returnCallback(function ($name) {
         if ($name == 'upgrades') {
             return [\Elgg\Upgrade\NonRequiredTestBatch::class];
         }
     }));
     $upgrades = _elgg_services()->upgradeLocator->getUpgrades($this->plugin);
     $this->assertEmpty($upgrades);
 }
Esempio n. 2
0
 /**
  * Creates new ElggUpgrade instance from plugin's static config
  *
  * @param \ElggPlugin $plugin Plugin
  * @return \ElggUpgrade[]
  */
 public function getUpgrades(\ElggPlugin $plugin)
 {
     $upgrades = [];
     $batches = $plugin->getStaticConfig('upgrades');
     if (empty($batches)) {
         // No upgrades available for this plugin
         return $upgrades;
     }
     $plugin_id = $plugin->getID();
     foreach ($batches as $class) {
         $batch = $this->getBatch($class);
         if (!$batch) {
             continue;
         }
         $version = $batch::VERSION;
         $upgrade_id = "{$plugin_id}:{$version}";
         // Database holds the information of which upgrades have been processed
         if ($this->upgradeExists($upgrade_id)) {
             $this->logger->info("Upgrade {$upgrade_id} has already been processed");
             continue;
         }
         // Create a new ElggUpgrade to represent the upgrade in the database
         $object = new ElggUpgrade();
         $object->setId($upgrade_id);
         $object->setClass($class);
         $object->title = "{$plugin_id}:upgrade:{$version}:title";
         $object->description = "{$plugin_id}:upgrade:{$version}:description";
         $object->offset = 0;
         try {
             $object->save();
             $upgrades[] = $object;
         } catch (\UnexpectedValueException $ex) {
             $this->logger->error($ex->getMessage());
         }
     }
     return $upgrades;
 }
Esempio n. 3
0
/**
 * Import plugin settings
 *
 * @param string $info
 * @param string $settings_mode Options to load plugin settings. One of overwrite, if_not_exists, or ignore
 * @return type bool
 */
function transfer_plugins_import($info, $settings_mode = 'if_not_exists')
{
    $info = unserialize($info);
    if (!$info) {
        return false;
    }
    $version = elgg_extract('transfer_plugins_format', $info);
    if ($version != TRANSFER_PLUGINS_FORMAT) {
        return false;
    }
    // @todo check elgg, plugin, and transfer_plugin version compatibility.
    if (!isset($info['plugins'])) {
        return false;
    }
    $r = true;
    foreach ($info['plugins'] as $plugin_info) {
        $plugin_id = $plugin_info['id'];
        $plugin = new ElggPlugin($plugin_id);
        // not installed
        if (!$plugin->isValid()) {
            continue;
        }
        $r &= $plugin->setPriority($plugin_info['priority']);
        if ($plugin_info['active'] && !$plugin->isActive()) {
            $r &= $plugin->activate();
        }
        if ($settings_mode != 'ignore' && $plugin_info['settings']) {
            foreach ($plugin_info['settings'] as $name => $value) {
                switch ($settings_mode) {
                    case 'overwrite':
                        $plugin->setSetting($name, $value);
                        break;
                    case 'if_not_exists':
                        // @todo not sure if this works because isset isn't overloaded in ElggPlugin
                        if (!isset($plugin->{$name})) {
                            $plugin->setSetting($name, $value);
                        }
                        break;
                }
            }
        }
    }
    return $r;
}
Esempio n. 4
0
/**
 * 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_regenerate_simplecache();
 *		elgg_reset_system_cache();
 *
 * @deprecated 1.8 Use ElggPlugin->deactivate()
 *
 * @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);
    $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->deactivate($site_guid);
}
Esempio n. 5
0
/**
 * 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);
}
Esempio n. 6
0
 /**
  * Discovers plugins in the plugins_path setting and creates \ElggPlugin
  * entities for them if they don't exist.  If there are plugins with entities
  * but not actual files, will disable the \ElggPlugin entities and mark as inactive.
  * The \ElggPlugin object holds config data, so don't delete.
  *
  * @return bool
  * @access private
  */
 function generateEntities()
 {
     $mod_dir = elgg_get_plugins_path();
     $db_prefix = elgg_get_config('dbprefix');
     // ignore access in case this is called with no admin logged in - needed for creating plugins perhaps?
     $old_ia = elgg_set_ignore_access(true);
     // show hidden entities so that we can enable them if appropriate
     $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);
     /* @var \ElggPlugin[] $known_plugins */
     if (!$known_plugins) {
         $known_plugins = array();
     }
     // map paths to indexes
     $id_map = array();
     foreach ($known_plugins as $i => $plugin) {
         // if the ID is wrong, delete the plugin because we can never load it.
         $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;
     }
     // check real plugins against known ones
     foreach ($physical_plugins as $plugin_id) {
         // is this already in the db?
         if (array_key_exists($plugin_id, $id_map)) {
             $index = $id_map[$plugin_id];
             $plugin = $known_plugins[$index];
             // was this plugin deleted and its entity disabled?
             if (!$plugin->isEnabled()) {
                 $plugin->enable();
                 $plugin->deactivate();
                 $plugin->setPriority('last');
             }
             // remove from the list of plugins to disable
             unset($known_plugins[$index]);
         } else {
             // create new plugin
             // priority is forced to last in save() if not set.
             $plugin = new \ElggPlugin($mod_dir . $plugin_id);
             $plugin->save();
         }
     }
     // everything remaining in $known_plugins needs to be disabled
     // because they are entities, but their dirs were removed.
     // don't delete the entities because they hold settings.
     foreach ($known_plugins as $plugin) {
         if ($plugin->isActive()) {
             $plugin->deactivate();
         }
         // remove the priority.
         $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;
 }
 public function testElggPluginGetID()
 {
     $test_plugin = new ElggPlugin('profile');
     $this->assertIdentical('profile', $test_plugin->getID());
 }
Esempio n. 8
0
     // is this already in the db?
     if (array_key_exists($plugin_id, $id_map)) {
         $index = $id_map[$plugin_id];
         $plugin = $known_plugins[$index];
         // was this plugin deleted and its entity disabled?
         if ($plugin->enabled != 'yes') {
             $plugin->enable();
             $plugin->deactivate();
             $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();
     }
 }
 // everything remaining in $known_plugins needs to be disabled
 // because they are entities, but their dirs were removed.
 // don't delete the entities because they hold settings.
 foreach ($known_plugins as $plugin) {
     if ($plugin->isActive()) {
         $plugin->deactivate();
     }
     // remove the priority.
     $name = elgg_namespace_plugin_private_setting('internal', 'priority');
     remove_private_setting($plugin->guid, $name);
     $plugin->disable();
 }
Esempio n. 9
0
/**
 * Serves up screenshots for plugins from
 * admin_plugin_screenshot/<plugin_id>/<size>/<ss_name>.<ext>
 *
 * @param array $pages The pages array
 * @return bool
 * @access private
 */
function admin_plugin_screenshot_page_handler($pages)
{
    // only admins can use this for security
    admin_gatekeeper();
    $plugin_id = elgg_extract(0, $pages);
    // only thumbnail or full.
    $size = elgg_extract(1, $pages, 'thumbnail');
    // the rest of the string is the filename
    $filename_parts = array_slice($pages, 2);
    $filename = implode('/', $filename_parts);
    $filename = sanitise_filepath($filename, false);
    $plugin = new ElggPlugin($plugin_id);
    if (!$plugin) {
        $file = elgg_get_root_path() . '_graphics/icons/default/medium.png';
    } else {
        $file = $plugin->getPath() . $filename;
        if (!file_exists($file)) {
            $file = elgg_get_root_path() . '_graphics/icons/default/medium.png';
        }
    }
    header("Content-type: image/jpeg");
    // resize to 100x100 for thumbnails
    switch ($size) {
        case 'thumbnail':
            echo get_resized_image_from_existing_file($file, 100, 100, true);
            break;
        case 'full':
        default:
            echo file_get_contents($file);
            break;
    }
    return true;
}
Esempio n. 10
0
/**
 * Set a setting for a plugin.
 *
 * @param string $name The name - note, can't be "title".
 * @param mixed $value The value.
 * @param string $plugin_name Optional plugin name, if not specified then it is detected from where you are calling from.
 */
function set_plugin_setting($name, $value, $plugin_name = "")
{
    if (!$plugin_name) {
        $plugin_name = get_plugin_name();
    }
    $plugin = find_plugin_settings($plugin_name);
    if (!$plugin) {
        $plugin = new ElggPlugin();
    }
    if ($name != 'title') {
        // Hook to validate setting
        $value = trigger_plugin_hook('plugin:setting', 'plugin', array('plugin' => $plugin_name, 'name' => $name, 'value' => $value), $value);
        $plugin->title = $plugin_name;
        $plugin->access_id = ACCESS_PUBLIC;
        $plugin->save();
        $plugin->{$name} = $value;
        return $plugin->getGUID();
    }
    return false;
}
Esempio n. 11
0
/**
 * Discovers plugins in the plugins_path setting and creates ElggPlugin
 * entities for them if they don't exist.  If there are plugins with entities
 * but not actual files, will disable the ElggPlugin entities and mark as inactive.
 * The ElggPlugin object holds config data, so don't delete.
 *
 * @todo Crappy name?
 * @return bool
 * @since 1.8.0
 * @access private
 */
function elgg_generate_plugin_entities()
{
    $site = get_config('site');
    $dir = elgg_get_plugins_path();
    $options = array('type' => 'object', 'subtype' => 'plugin', 'limit' => ELGG_ENTITIES_NO_VALUE);
    $old_ia = elgg_set_ignore_access(true);
    $old_access = access_get_show_hidden_status();
    access_show_hidden_entities(true);
    $known_plugins = elgg_get_entities_from_relationship($options);
    if (!$known_plugins) {
        $known_plugins = array();
    }
    // map paths to indexes
    $id_map = array();
    foreach ($known_plugins as $i => $plugin) {
        // if the ID is wrong, delete the plugin because we can never load it.
        $id = $plugin->getID();
        if (!$id) {
            $plugin->delete();
            unset($known_plugins[$i]);
            continue;
        }
        $id_map[$plugin->getID()] = $i;
    }
    $physical_plugins = elgg_get_plugin_ids_in_dir($dir);
    if (!$physical_plugins) {
        return false;
    }
    // check real plugins against known ones
    foreach ($physical_plugins as $plugin_id) {
        // is this already in the db?
        if (array_key_exists($plugin_id, $id_map)) {
            $index = $id_map[$plugin_id];
            $plugin = $known_plugins[$index];
            // was this plugin deleted and its entity disabled?
            if ($plugin->enabled != 'yes') {
                $plugin->enable();
                $plugin->deactivate();
                $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();
        }
    }
    // everything remaining in $known_plugins needs to be disabled
    // because they are entities, but their dirs were removed.
    // don't delete the entities because they hold settings.
    foreach ($known_plugins as $plugin) {
        if ($plugin->isActive()) {
            $plugin->deactivate();
        }
        // remove the priority.
        $name = elgg_namespace_plugin_private_setting('internal', 'priority');
        remove_private_setting($plugin->guid, $name);
        $plugin->disable();
    }
    access_show_hidden_entities($old_access);
    elgg_set_ignore_access($old_ia);
    elgg_reindex_plugin_priorities();
    return true;
}
Esempio n. 12
0
 protected function getFallbackPlugin()
 {
     if (!isset($this->fallback_plugin)) {
         $this->fallback_plugin = false;
         if (($site = elgg_get_site_entity($this->site_guid)) && elgg_instanceof($site, "site", Subsite::SUBTYPE, "Subsite")) {
             if (subsite_manager_check_global_plugin_setting($this->getID(), "fallback_to_main_settings")) {
                 // ignore access for this part, as plugins are public
                 $old_ia = elgg_set_ignore_access(true);
                 // do we already know the fallback plugin
                 $fallback_plugin_guid = (int) parent::getPrivateSetting("fallback_plugin_guid");
                 if (!empty($fallback_plugin_guid)) {
                     if ($temp_plugin = get_entity($fallback_plugin_guid)) {
                         if (elgg_instanceof($temp_plugin, "object", "plugin")) {
                             $this->fallback_plugin = $temp_plugin;
                         }
                     }
                     // something is wrong with the guid, cleanup
                     if (empty($this->fallback_plugin)) {
                         parent::removePrivateSetting("fallback_plugin_guid");
                     }
                 }
                 // we haven't found a falback plugin yet (or it is invalid)
                 if (empty($this->fallback_plugin)) {
                     $options = array("type" => "object", "subtype" => "plugin", "limit" => 1, "site_guids" => array($site->getOwnerGUID()), "joins" => array("JOIN " . elgg_get_config("dbprefix") . "objects_entity oe ON e.guid = oe.guid"), "wheres" => array("(oe.title = '" . $this->getID() . "')"));
                     if ($plugins = elgg_get_entities($options)) {
                         $temp_plugin = $plugins[0];
                         $this->fallback_plugin = $temp_plugin;
                         parent::setPrivateSetting("fallback_plugin_guid", $temp_plugin->getGUID());
                     } else {
                         // we should have found a main plugin, but didn't log this
                         elgg_log("Subsite plugin(" . $this->getID() . ") with fallback didnt find main plugin", "ERROR");
                     }
                 }
                 // restore access settings
                 elgg_set_ignore_access($old_ia);
             }
         }
     }
     return $this->fallback_plugin;
 }
Esempio n. 13
0
 public function testElggPluginGetID()
 {
     $test_plugin = new \ElggPlugin(elgg_get_plugins_path() . 'profile');
     $this->assertIdentical('profile', $test_plugin->getID());
 }