Classes

File phpboost/module/ModulesManager.class.php

File phpboost/module/ModulesManager.class.php

  1:   2:   3:   4:   5:   6:   7:   8:   9:  10:  11:  12:  13:  14:  15:  16:  17:  18:  19:  20:  21:  22:  23:  24:  25:  26:  27:  28:  29:  30:  31:  32:  33:  34:  35:  36:  37:  38:  39:  40:  41:  42:  43:  44:  45:  46:  47:  48:  49:  50:  51:  52:  53:  54:  55:  56:  57:  58:  59:  60:  61:  62:  63:  64:  65:  66:  67:  68:  69:  70:  71:  72:  73:  74:  75:  76:  77:  78:  79:  80:  81:  82:  83:  84:  85:  86:  87:  88:  89:  90:  91:  92:  93:  94:  95:  96:  97:  98:  99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278: 279: 280: 281: 282: 283: 284: 285: 286: 287: 288: 289: 290: 291: 292: 293: 294: 295: 296: 297: 298: 299: 300: 301: 302: 303: 304: 305: 306: 307: 308: 309: 310: 311: 312: 313: 314: 315: 316: 317: 318: 319: 320: 321: 322: 323: 324: 325: 326: 327: 328: 329: 330: 331: 332: 333: 334: 335: 336: 337: 338: 339: 340: 341: 342: 343: 344: 345: 346: 347: 348: 349: 350: 351: 352: 353: 354: 355: 356: 357: 358: 359: 360: 361: 362: 363: 364: 365: 366: 367: 368: 369: 370: 371: 372: 373: 374: 375: 376: 377: 378: 379: 380: 381: 382: 383: 384: 385: 386: 387: 388: 389: 390: 391: 392: 393: 394: 395: 396: 397: 398: 399: 400: 401: 402: 403: 404: 405: 406: 407: 408: 409: 410: 411: 412: 413: 414: 415: 416: 417: 418: 419: 420: 421: 422: 423: 424: 425: 426: 427: 428: 429: 430: 431: 432: 433: 434: 435: 436: 437: 438: 439: 440: 441: 442: 443: 444: 445: 446: 447: 448: 449: 450: 451: 452: 453: 454: 455: 456: 457: 458: 459: 460: 461: 462: 463: 464: 465: 466: 467: 468: 469: 470: 471: 472: 473: 474: 475: 476: 477: 478: 479: 480: 481: 482: 483: 484: 485: 486: 487: 488: 489: 490: 491: 492: 493: 494: 495: 496: 497: 498: 499: 500: 501: 502: 503: 504: 505: 506: 507: 508: 509: 510: 511: 512: 513: 514: 515: 516: 517: 518: 519: 520: 521: 522: 523: 524: 525: 526: 527: 528: 529: 530: 531: 532: 533: 
<?php
/**
 * This class enables you to manages the PHPBoost packages which are nothing else than the modules.
 * @package     PHPBoost
 * @subpackage  Module
 * @copyright   &copy; 2005-2019 PHPBoost
 * @license     https://www.gnu.org/licenses/gpl-3.0.html GNU/GPL-3.0
 * @author      Benoit SAUTEL <ben.popeye@phpboost.com>
 * @version     PHPBoost 5.2 - last update: 2019 01 12
 * @since       PHPBoost 2.0 - 2008 10 12
 * @contributor Kevin MASSY <reidlos@phpboost.com>
 * @contributor Julien BRISWALTER <j1.seth@phpboost.com>
 * @contributor Arnaud GENET <elenwii@phpboost.com>
*/

class ModulesManager
{
    const GENERATE_CACHE_AFTER_THE_OPERATION = true;
    const DO_NOT_GENERATE_CACHE_AFTER_THE_OPERATION = false;
    const MODULE_UNINSTALLED = 0;
    const MODULE_INSTALLED = 1;
    const UNEXISTING_MODULE = 2;
    const MODULE_ALREADY_INSTALLED = 3;
    const CONFIG_CONFLICT = 4;
    const NOT_INSTALLED_MODULE = 5;
    const MODULE_FILES_COULD_NOT_BE_DROPPED = 6;
    const PHP_VERSION_CONFLICT = 7;
    const PHPBOOST_VERSION_CONFLICT = 8;
    const MODULE_NOT_UPGRADABLE = 9;
    const UPGRADE_FAILED = 10;
    const MODULE_UPDATED = 11;

    /**
     * @return Module[string] the Modules map (name => module) of the installed modules (activated or not)
     */
    public static function get_installed_modules_map()
    {
        return ModulesConfig::load()->get_modules();
    }

    /**
     * @return Module[string] the Modules map (name => module) of the installed modules (and activated )
     */
    public static function get_activated_modules_map()
    {
        $activated_modules = array();
        foreach (ModulesConfig::load()->get_modules() as $module) {
            if ($module->is_activated()) {
                $activated_modules[$module->get_id()] = $module;
            }
        }
        return $activated_modules;
    }

    /**
     * @return Module[string] the Modules map (name => module) of the uninstalled modules (activated or not)
     */
    public static function get_uninstalled_modules_map()
    {
        throw new NotYetImplementedException();
    }

    /**
     * @return Module[string] the Modules map (name => module) of the installed modules (activated or not)
     * sorted by name
     */
    public static function get_installed_modules_map_sorted_by_localized_name()
    {
        $modules = self::get_installed_modules_map();
        try {
            usort($modules, array(__CLASS__, 'callback_sort_modules_by_name'));
        } catch (IOException $ex) {
        }
        return $modules;
    }

    /**
     * @return Module[string] the Modules map (name => module) of the installed modules (and activated)
     * sorted by name
     */
    public static function get_activated_modules_map_sorted_by_localized_name()
    {
        $modules = self::get_activated_modules_map();
        try {
            usort($modules, array(__CLASS__, 'callback_sort_modules_by_name'));
        } catch (IOException $ex) {
        }
        return $modules;
    }

    public static function callback_sort_modules_by_name(Module $module1, Module $module2)
    {
        if (TextHelper::strtolower($module1->get_configuration()->get_name()) > TextHelper::strtolower($module2->get_configuration()->get_name()))
        {
            return 1;
        }
        return -1;
    }

    /**
     * @return string[] the names list of the installed modules (activated or not)
     */
    public static function get_installed_modules_ids_list()
    {
        return array_keys(self::get_installed_modules_map());
    }

    /**
     * @return string[] the names list of the installed modules (and activated)
     */
    public static function get_activated_modules_ids_list()
    {
        return array_keys(self::get_activated_modules_map());
    }

    /**
     * @return string[]
     */
    public static function get_activated_feature_modules($feature_id)
    {
        $activated_feature_modules = array();
        foreach (ModulesConfig::load()->get_modules() as $module) {
            if ($module->is_activated()) {
                if ($module->get_configuration()->feature_is_enabled($feature_id))
                {
                    $activated_feature_modules[$module->get_id()] = $module;
                }
            }
        }
        return $activated_feature_modules;
    }

    /**
     * Returns the requested module
     * @param $module_id the id of the module
     * @return Module the requested module
     */
    public static function get_module($module_id)
    {
        return ModulesConfig::load()->get_module($module_id);
    }

    /**
     * tells whether the requested module is installed (activated or not)
     * @return bool true if the requested module is installed
     */
    public static function is_module_installed($module_id)
    {
        return in_array($module_id, self::get_installed_modules_ids_list());
    }

    /**
     * tells whether the requested module is activated
     * @return bool true if the requested module is activated
     */
    public static function is_module_activated($module_id)
    {
        return in_array($module_id, self::get_activated_modules_ids_list());
    }

    /**
     * @static
     * Installs a module.
     * @param string $module_identifier Module identifier (name of its folder)
     * @param bool $enable_module true if you want the module to be enabled, otherwise false.
     * @return int One of the following error codes:
     * <ul>
     *  <li>MODULE_INSTALLED: the installation succeded</li>
     *  <li>MODULE_ALREADY_INSTALLED: the module is already installed</li>
     *  <li>UNEXISTING_MODULE: the module you want to install doesn't exist</li>
     *  <li>PHP_VERSION_CONFLICT: the server PHP version is two old to be able to run the module code (config set in the config.ini module file)</li>
     *  <li>CONFIG_CONFLICT: the configuration field is already used</i>
     * </ul>
     */
    public static function install_module($module_identifier, $enable_module = true, $generate_cache = true)
    {
        self::update_class_list();

        if (empty($module_identifier) || !is_dir(PATH_TO_ROOT . '/' . $module_identifier))
        {
            return self::UNEXISTING_MODULE;
        }

        if (self::is_module_installed($module_identifier))
        {
            return self::MODULE_ALREADY_INSTALLED;
        }

        $module = new Module($module_identifier, $enable_module);
        $configuration = $module->get_configuration();

        $phpversion = ServerConfiguration::get_phpversion();
        if (version_compare($phpversion, $configuration->get_php_version(), 'lt'))
        {
            return self::PHP_VERSION_CONFLICT;
        }

        $phpboost_version = GeneralConfig::load()->get_phpboost_major_version();
        if (version_compare($phpboost_version, $configuration->get_compatibility(), '!='))
        {
            return self::PHPBOOST_VERSION_CONFLICT;
        }

        self::execute_module_installation($module_identifier);

        ModulesConfig::load()->add_module($module);
        ModulesConfig::save();

        // TODO Force initialization ExtensionProviderService for PHPBoost installation
        AppContext::init_extension_provider_service();

        MenuService::add_mini_module($module_identifier, $generate_cache);

        if ($generate_cache)
        {
            MenuService::generate_cache();

            if (ServerEnvironmentConfig::load()->is_url_rewriting_enabled())
            {
                HtaccessFileCache::regenerate();
            }
        }

        return self::MODULE_INSTALLED;
    }

    /**
     * @static
     * Uninstalls a module.
     * @param int $module_id Module id (in the DB_TABLE_MODULES table)
     * @param bool $drop_files true if you want the module files to be dropped, otherwise false.
     * @return int One of the following error codes:
     * <ul>
     *  <li>MODULE_FILES_COULD_NOT_BE_DROPPED: the module files couldn't be deleted (probably due to an authorization issue) but it has been uninstalled .</li>
     *  <li>MODULE_UNINSTALLED: the module was successfully uninstalled.</li>
     *  <li>NOT_INSTALLED_MODULE: the module to uninstall doesn't exist!</li>
     * </ul>
     */
    public static function uninstall_module($module_id, $drop_files = false, $generate_cache = true)
    {
        if (!empty($module_id) && self::is_module_installed($module_id))
        {
            $error = self::execute_module_uninstallation($module_id);
            if ($error === null)
            {
                ContributionService::delete_contribution_module($module_id);

                NotationService::delete_notes_module($module_id);

                CommentsService::delete_comments_module($module_id);

                PersistenceContext::get_querier()->delete(DB_TABLE_CONFIGS, "WHERE name = :name", array('name' => $module_id));

                //Régénération des feeds.
                Feed::clear_cache($module_id);

                MenuService::delete_mini_module($module_id);
                MenuService::delete_module_feeds_menus($module_id);

                ModulesConfig::load()->remove_module_by_id($module_id);
                ModulesConfig::save();

                //Module home page ?
                $general_config = GeneralConfig::load();
                $module_home_page_selected = $general_config->get_module_home_page();
                if ($module_home_page_selected == $module_id)
                {
                    $general_config->set_module_home_page('');
                    $general_config->set_other_home_page('index.php');
                }

                //Suppression des fichiers du module
                if ($drop_files)
                {
                    $folder = new Folder(PATH_TO_ROOT . '/' . $module_id);
                    try
                    {
                        $folder->delete();
                        self::update_class_list();
                        AppContext::init_extension_provider_service();
                    }
                    catch (IOException $ex)
                    {
                        return self::MODULE_FILES_COULD_NOT_BE_DROPPED;
                    }
                }

                if ($generate_cache)
                {
                    AppContext::get_cache_service()->clear_cache();

                    if (ServerEnvironmentConfig::load()->is_url_rewriting_enabled())
                    {
                        HtaccessFileCache::regenerate();
                    }
                }

                return self::MODULE_UNINSTALLED;
            }
            return $error;
        }
        else
        {
            return self::NOT_INSTALLED_MODULE;
        }
    }

    public static function upgrade_module($module_identifier, $generate_cache = true)
    {
        if (!empty($module_identifier) && is_dir(PATH_TO_ROOT . '/' . $module_identifier))
        {
            if (self::is_module_installed($module_identifier))
            {
                if (self::module_is_upgradable($module_identifier))
                {
                    $module = self::get_module($module_identifier);

                    $version_upgrading = self::execute_module_upgrade($module_identifier, $module->get_installed_version());

                    if ($version_upgrading !== null)
                    {
                        $module->set_installed_version($version_upgrading);
                        ModulesConfig::load()->update($module);
                        ModulesConfig::save();

                        if ($generate_cache)
                        {
                            AppContext::get_cache_service()->clear_cache();

                            if (ServerEnvironmentConfig::load()->is_url_rewriting_enabled())
                            {
                                HtaccessFileCache::regenerate();
                            }
                        }
                    }
                    else
                    {
                        return self::UPGRADE_FAILED;
                    }
                }
                else
                {
                    return self::MODULE_NOT_UPGRADABLE;
                }
            }
            else
            {
                return self::NOT_INSTALLED_MODULE;
            }
        }
        else
        {
            return self::UNEXISTING_MODULE;
        }

        return self::MODULE_UPDATED;
    }

    public static function module_is_upgradable($module_identifier)
    {
        if (!empty($module_identifier) && is_dir(PATH_TO_ROOT . '/' . $module_identifier))
        {
            if (self::is_module_installed($module_identifier))
            {
                $module = self::get_module($module_identifier);

                if (version_compare($module->get_installed_version(), $module->get_configuration()->get_version()) == -1 && $module->get_configuration()->get_compatibility() == GeneralConfig::load()->get_phpboost_major_version())
                {
                    return true;
                }
            }
        }
        return false;
    }

    public static function update_module($module_id, $activated, $generate_cache = true)
    {
        $error = '';
        if (!$activated)
        {
            MenuService::delete_mini_module($module_id);
            MenuService::delete_module_feeds_menus($module_id);

            $general_config = GeneralConfig::load();
            $module_home_page_selected = $general_config->get_module_home_page();
            if ($module_home_page_selected == $module_id)
            {
                $general_config->set_module_home_page('');
                $general_config->set_other_home_page('index.php');
            }

            $editors = AppContext::get_content_formatting_service()->get_available_editors();
            if (in_array($module_id, $editors))
            {
                if (count($editors) > 1)
                {
                    $default_editor = ContentFormattingConfig::load()->get_default_editor();
                    if ($default_editor !== $module_id)
                    {
                        PersistenceContext::get_querier()->update(DB_TABLE_MEMBER, array('editor' => $default_editor),
                            'WHERE editor=:old_editor', array('old_editor' => $module_id
                        ));
                    }
                    else
                        $error = LangLoader::get_message('is_default_editor', 'editor-common');
                }
                else
                    $error = LangLoader::get_message('last_editor_installed', 'editor-common');
            }

            $captchas = AppContext::get_captcha_service()->get_available_captchas();
            if (in_array($module_id, $captchas))
            {
                if (count($captchas) > 1)
                {
                    $default_captcha = ContentManagementConfig::load()->get_used_captcha_module();
                    if ($default_captcha == $module_id)
                        $error = LangLoader::get_message('captcha.is_default', 'status-messages-common');
                }
                else
                    $error = LangLoader::get_message('captcha.last_installed', 'status-messages-common');
            }
        }
        else
        {
            $module = self::get_module($module_id);

            if ($module->get_configuration()->get_compatibility() != GeneralConfig::load()->get_phpboost_major_version())
                $error = LangLoader::get_message('modules.not_compatible', 'admin-modules-common');
        }

        if (empty($error))
        {
            $module = self::get_module($module_id);
            $module->set_activated($activated);
            ModulesConfig::load()->update($module);
            ModulesConfig::save();

            MenuService::add_mini_module($module_id);
            Feed::clear_cache($module_id);

            if ($generate_cache)
            {
                AppContext::get_cache_service()->clear_cache();

                if (ServerEnvironmentConfig::load()->is_url_rewriting_enabled())
                {
                    HtaccessFileCache::regenerate();
                }
            }
        }

        return $error;
    }

    public static function set_module_activation($module_id, bool $activated)
    {
        $module = self::get_module($module_id);
        $module->set_activated($activated);
        ModulesConfig::load()->update($module);
        ModulesConfig::save();
    }

    private static function execute_module_installation($module_id)
    {
        $module_setup = self::get_module_setup($module_id);
        $environment_check = $module_setup->check_environment();
        if (!$environment_check->has_errors())
        {
            $module_setup->install();
        }
        else
        {
            // TODO process module installation errors
        }
    }

    private static function execute_module_uninstallation($module_id)
    {
        $module_setup = self::get_module_setup($module_id);
        return $module_setup->uninstall();
    }

    private static function execute_module_upgrade($module_id, $installed_version)
    {
        $module_setup = self::get_module_setup($module_id);
        return $module_setup->upgrade($installed_version);
    }

    /**
     *
     * @param string $module_id
     * @return ModuleSetup
     */
    private static function get_module_setup($module_id)
    {
        $module_setup_classname = TextHelper::ucfirst($module_id) . 'Setup';
        if (self::module_setup_exists($module_setup_classname))
        {
            return new $module_setup_classname();
        }
        return new DefaultModuleSetup();
    }

    private static function module_setup_exists($module_setup_classname)
    {
        return ClassLoader::is_class_registered_and_valid($module_setup_classname);
    }

    private static function update_class_list()
    {
        ClassLoader::generate_classlist();
    }

    /**
     * @return string[] the names list of the modules for unauthorized FormFieldSelectChoiceOption
     */
    public static function generate_unauthorized_module_option($type)
    {
        $options = array();

        $modules = self::get_activated_feature_modules($type);
        foreach ($modules as $id => $module)
        {

            $options[] = new FormFieldSelectChoiceOption($module->get_configuration()->get_name(), $module->get_id());
        }
        return $options;
    }

}
?>