Classes

File phpboost/update/Updates.class.php

File phpboost/update/Updates.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: 
<?php
/**
 * @package     PHPBoost
 * @subpackage  Update
 * @copyright   &copy; 2005-2019 PHPBoost
 * @license     https://www.gnu.org/licenses/gpl-3.0.html GNU/GPL-3.0
 * @author      Loic ROUCHON <horn@phpboost.com>
 * @version     PHPBoost 5.2 - last update: 2018 12 24
 * @since       PHPBoost 2.0 - 2008 08 17
 * @contributor Julien BRISWALTER <j1.seth@phpboost.com>
*/

define('CHECK_KERNEL', 0X01);
define('CHECK_MODULES', 0X02);
define('CHECK_THEMES', 0X04);
define('CHECK_ALL_UPDATES', CHECK_KERNEL|CHECK_MODULES|CHECK_THEMES);

class Updates
{
    private $repositories = array();
    private $apps = array();

    const PHPBOOST_OFFICIAL_REPOSITORY = 'https://www.phpboost.com/repository/main.xml';
    const PHP_MIN_VERSION_UPDATES = '5';

    /**
    * constructor of the class
    * @param $checks
    */
    public function __construct($checks = CHECK_ALL_UPDATES)
    {
        $this->load_apps($checks);
        $this->load_repositories();
        $this->check_repositories();
    }

    /**
    * Load Application Classes
    * @param $checks
    */
    private function load_apps($checks = CHECK_ALL_UPDATES)
    {
        if (ServerConfiguration::get_phpversion() > self::PHP_MIN_VERSION_UPDATES)
        {
            $user_locale = AppContext::get_current_user()->get_locale();
            if ($checks & CHECK_KERNEL)
            {   // Add the kernel to the check list
                $this->apps[] = new Application('kernel', $user_locale, Application::KERNEL_TYPE, Environment::get_phpboost_version(), Updates::PHPBOOST_OFFICIAL_REPOSITORY);
            }

            if ($checks & CHECK_MODULES)
            {
                $activated_modules = ModulesManager::get_activated_modules_map_sorted_by_localized_name();
                foreach ($activated_modules as $module)
                {
                    $this->apps[] = new Application($module->get_id(),
                    $user_locale, Application::MODULE_TYPE,
                    $module->get_configuration()->get_version(), $module->get_configuration()->get_repository());
                }
            }

            if ($checks & CHECK_THEMES)
            {
                // Add Themes
                $activated_themes = ThemesManager::get_activated_themes_map();
                foreach ($activated_themes as $id => $value)
                {
                    $repository = $value->get_configuration()->get_repository();
                    if (!empty($repository))
                    {
                        $this->apps[] = new Application($id, $user_locale, Application::TEMPLATE_TYPE, $value->get_configuration()->get_version(), $repository);
                    }
                }
            }
        }
    }

    /**
    * Load Repository Classes
    */
    private function load_repositories()
    {
        if (ServerConfiguration::get_phpversion() > self::PHP_MIN_VERSION_UPDATES)
        {
            foreach ($this->apps as $app)
            {
                $rep = $app->get_repository();
                if (!empty($rep) && !isset($this->repositories[$rep]))
                    $this->repositories[$rep] = new Repository($rep);
            }
        }
    }

    /**
    * Check Repository for Update Notification
    */
    private function check_repositories()
    {
        if (ServerConfiguration::get_phpversion() > self::PHP_MIN_VERSION_UPDATES)
        {
            foreach ($this->apps as $app)
            {
                $result = $this->repositories[$app->get_repository()]->check($app);
                if ($result !== null)
                {   // processing to the update notification
                    $this->add_update_alert($result);
                }
            }
        }
    }

    /**
    * Save an alert for Update Notification
    */
    private function add_update_alert($app)
    {
        $identifier = $app->get_identifier();
        // We verify that the alert is not already registered
        if (AdministratorAlertService::find_by_identifier($identifier, 'updates', 'kernel') === null)
        {
            $alert = new AdministratorAlert();

            if ($app->get_type() == Application::KERNEL_TYPE)
                $alert->set_entitled(sprintf(LangLoader::get_message('kernel_update_available', 'admin'), $app->get_version()));
            else
                $alert->set_entitled(sprintf(LangLoader::get_message('update_available', 'admin'), $app->get_type(), $app->get_name(), $app->get_version()));

            $alert->set_fixing_url('/admin/updates/detail.php?identifier=' . $identifier);
            $alert->set_priority($app->get_priority());
            $alert->set_properties(TextHelper::serialize_base64($app));
            $alert->set_type('updates');
            $alert->set_identifier($identifier);

            //Save
            AdministratorAlertService::save_alert($alert);
        }
    }
}
?>