Plugin Version check while installation

  • Hi Laurent....
    I'd now found a way to check the version in the gui

    Code
    1. public function install(iMSCP_Plugin_Manager $pluginManager) { /** @var iMSCP_Config_Handler_File $cfg */ $cfg = iMSCP_Registry::get('config'); $imscpMinVersion = '1.1.0.rc4'; if(version_compare($cfg->Version, $imscpMinVersion, '<') && $cfg->Version != 'Git Master'){ exec_query("UPDATE `plugin` SET `plugin_status` = ? WHERE `plugin_name` = 'OpenDKIM'", $cfg->ITEM_DISABLED_STATUS); set_page_message(tr('Unable to install the plugin OpenDKIM. The version of i-MSCP must be: %s. But you are using: %s', $imscpMinVersion, $cfg->Version), 'error'); } else { try { $this->createDbTable(); } catch(iMSCP_Exception_Database $e) { throw new iMSCP_Plugin_Exception($e->getMessage(), $e->getCode(), $e); } } }


    This works very good . But if i want to uninstall the plugin i got this error

    Code
    1. An unexpected error occurred:
    2. Modules::Plugin::_executePlugin: Can't locate /var/www/imscp/engine/Plugins/OpenDKIM.pm in @INC (@INC contains: /var/www/imscp/engine/PerlVendor /var/www/imscp/engine/PerlLib /var/www/imscp/engine /etc/perl /usr/local/lib/perl/5.14.2 /usr/local/share/perl/5.14.2 /usr/lib/perl5 /usr/share/perl5 /usr/lib/perl/5.14 /usr/share/perl/5.14 /usr/local/lib/site_perl .) at /var/www/imscp/engine/PerlLib/Modules/Plugin.pm line 257, <$fh> line 718


    I can only uninstall if i copy the backend file to the Plugins-folder of the engine.


    Is it possible to check wheter the file exist when the action is uninstall?

    Edited once, last by TheCry ().


  • I'll review this and show you an example.

    badge.php?id=1239063037&bid=2518&key=1747635596&format=png&z=547451206

  • Thanks Laurent for your PM.
    This works, but i'm unable to uninstall the plugn if the i-MSCP not matched. I think there should be the possibility to uninstall the plugin if the plugin rise up this error.


  • Thanks Laurent for your PM.
    This works, but i'm unable to uninstall the plugn if the i-MSCP not matched. I think there should be the possibility to uninstall the plugin if the plugin rise up this error.



    I'm working on that issue. Basically, my idea to solve such a problem is as follow:


    PHP
    1. /** * Register a callback for the given event(s). * * @param iMSCP_Events_Manager_Interface $controller */public function register(iMSCP_Events_Manager_Interface $controller){ $controller->registerListener(iMSCP_Events::onBeforeActivatePlugins, $this);}/** * onBeforeActivatePlugin event listener * * @param iMSCP_Events_Event $event */public function onBeforeActivatePlugin($event){ if($event->getParam('action') == 'install') { my $cfg = iMSCP_Registry::get('config'); # <= 20130723 ---> (version older than 1.1.0-rc4) if($cfg->Version != 'Git Master' && $cfg->Version <= 20130723) { $event->stopPropagation(); $pluginManager->setStatus($event->getParam('pluginName'), $cfg->ITEM_UNINSTALLED_STATUS); set_page_message(tr('Your i-MSCP version is incompatible with this plugin'), 'error'); } }}


    And in the plugin manager I'll change:


    PHP
    1. /** * Activates the given plugin * * @param string $pluginName Name of the plugin to activate * @param bool $force Force action * @return bool TRUE on sucess, false otherwise */ public function activate($pluginName, $force = false) { if ($this->isKnown($pluginName)) { $pluginStatus = $this->getStatus($pluginName); if ($force || in_array($pluginStatus, array('uninstalled', 'disabled'))) { $statusTo = array( 'uninstalled' => array('toinstall', 'install'), 'toinstall' => array('toinstall', 'install'), 'disabled' => array('toenable', 'enable') ); $pluginInstance = $this->load($pluginName, false, false); $this->setError($pluginName, null); $this->setStatus($pluginName, $statusTo[$pluginStatus][0]); try { iMSCP_Events_Manager::getInstance()->dispatch( iMSCP_Events::onBeforeActivatePlugin, array('pluginManager' => $this, 'pluginName' => $pluginName) ); $pluginInstance->{$statusTo[$pluginStatus][1]}($this); if($this->hasBackend($pluginName)) { $this->backendRequest = true; } else { $this->setStatus($pluginName, 'enabled'); } iMSCP_Events_Manager::getInstance()->dispatch( iMSCP_Events::onAfterActivatePlugin, array('pluginManager' => $this, 'pluginName' => $pluginName) ); } catch(iMSCP_Plugin_Exception $e) { $this->setError($pluginName, sprintf('Plugin activation has failed: %s', $e->getMessage())); write_log(sprintf('Plugin manager: %s plugin activation has failed', $pluginName), E_USER_ERROR); return false; } return true; } } return false; }


    by



    Note: I'll maybe improve more (eg, move the status change from the listener to the plugin manager. I'll post a concrete example after testing.

    badge.php?id=1239063037&bid=2518&key=1747635596&format=png&z=547451206

    Edited once, last by Nuxwin ().


  • Thanks Laurent... Sounds good.. I will test your workarround


    it's not really a workaround. It's the normal way to process (event responses)...

    badge.php?id=1239063037&bid=2518&key=1747635596&format=png&z=547451206

  • Then i missunderstood you..:angel:
    I will test it and if it works i will commit OpenDKIM to review.. ;)


    Ok, fixed in master. You have not to set the status back to its initial status. It's done automatically by Plugin manager. See https://github.com/i-MSCP/imsc…121617640697209feff749280


    If you want an example, say me.

    badge.php?id=1239063037&bid=2518&key=1747635596&format=png&z=547451206


  • Yes... Please give me an example..


    I hope, it's clear enough:


    [code=php]
    /**
    * Mailman Plugin.
    *
    * @category iMSCP
    * @package iMSCP_Plugin
    * @subpackage Mailman
    * @author Laurent Declercq <[email protected]>
    */
    class iMSCP_Plugin_Mailman extends iMSCP_Plugin_Action
    {
    /**
    * @var array Map mailman URI endpoint to mailman action script
    */
    protected $routes = array();


    /**
    * Register a callback for the given event(s).
    *
    * @param iMSCP_Events_Manager_Interface $controller
    */
    public function register(iMSCP_Events_Manager_Interface $controller)
    {
    $controller->registerListener(
    array(
    iMSCP_Events::onBeforeActivatePlugin,
    iMSCP_Events::onBeforePluginsRoute,
    iMSCP_Events::onClientScriptStart,
    iMSCP_Events::onAfterDeleteCustomer,
    ),
    $this
    );


    $controller->registerListener(iMSCP_Events::onBeforeAddSubdomain, $this, -999);
    }


    /**
    * onBeforeActivatePlugin event listener
    *
    * @param iMSCP_Events_Event $event
    * @return void
    */
    public function onBeforeActivatePlugin($event)
    {
    if($event->getParam('action') == 'install') {
    /** @var iMSCP_Config_Handler_File $cfg */
    $cfg = iMSCP_Registry::get('config');
    $stopPropagation = false;


    if($cfg->Version <= 20130723) {
    set_page_message(
    tr('Your i-MSCP version is not compatible with this plugin. Try with a newer version'), 'error'
    );
    $stopPropagation = true;
    } elseif(! $cfg->exists('MTA_SERVER') || $cfg->MTA_SERVER != 'postfix') {
    set_page_message(tr('Mailman plugin require i-MSCP Postfix server implementation'), 'error');
    $stopPropagation = true;
    } elseif(! $cfg->exists('HTTPD_SERVER') || strpos($cfg->HTTPD_SERVER, 'apache_') !== 0) {
    set_page_message(tr('Mailman plugin require i-MSCP Apache server implementation'), 'error');
    $stopPropagation = true;
    } elseif(! $cfg->exists('NAMED_SERVER') || $cfg->NAMED_SERVER != 'bind') {
    set_page_message(tr('Mailman plugin require i-MSCP bind9 server implementation'), 'error');
    $stopPropagation = true;
    }


    $event->stopPropagation($stopPropagation);
    }
    }


    /**
    * Process plugin installation
    *
    * @throws iMSCP_Plugin_Exception
    * @param iMSCP_Plugin_Manager
    * @return void
    */
    public function install(iMSCP_Plugin_Manager $pluginManager)
    {
    try {
    $this->createDbTable();
    } catch(iMSCP_Exception_Database $e) {
    throw new iMSCP_Plugin_Exception($e->getMessage(), $e->getCode(), $e);
    }
    }


    /**
    * Plugin update
    *
    * @throws iMSCP_Plugin_Exception
    * @param iMSCP_Plugin_Manager $pluginManager
    * @param string $fromVersion Version from which update is initiated
    * @param string $toVersion Version to which plugin is updated
    * @return void
    */
    public function update(iMSCP_Plugin_Manager $pluginManager, $fromVersion, $toVersion)
    {
    if($fromVersion != $toVersion && $fromVersion == '0.0.1') {
    try {
    exec_query(
    '
    UPDATE
    `domain_dns`
    SET
    `owned_by` = ?
    WHERE
    `domain_dns` LIKE ?
    AND
    `domain_class` = ?
    AND
    `domain_type` = ?
    AND
    `owned_by` = ?
    ',
    array('plugin_mailman', 'lists.%', 'IN', 'A', 'yes')
    );
    } catch(iMSCP_Exception_Database $e) {
    throw new iMSCP_Plugin_Exception($e->getMessage(), $e->getCode(), $e);
    }
    }
    }


    /**
    * onBeforePluginsRoute event listener
    *
    * @return void
    */
    public function onBeforePluginsRoute()
    {
    $this->routes = array(
    '/client/mailman.php' => PLUGINS_PATH . '/' . $this->getName() . '/frontend/mailman.php'
    );
    }


    /**
    * onClientScriptStart event listener
    *
    * @return void
    */
    public function onClientScriptStart()
    {
    $this->setupNavigation();
    }


    /**
    * onBeforeAddSubdomain event listener
    *
    * @param iMSCP_Events_Event $event
    * @return void
    */
    public function onBeforeAddSubdomain($event)
    {
    if($event->getParam('subdomainName') == 'lists' && $event->getParam('subdomainType') == 'dmn') {
    set_page_message(tr('This subdomain is reserved to mailing list usage.'), 'error');
    }


    redirectTo('subdomain_add.php');
    }


    /**
    * onAfterDeleteCustomer event listener
    *
    * @param iMSCP_Events_Event $event
    * @return void
    */
    public function onAfterDeleteCustomer($event)
    {
    /** @var iMSCP_Config_Handler_File $cfg */
    $cfg = iMSCP_Registry::get('config');


    exec_query(
    'UPDATE `mailman` SET `mailman_status` = ? WHERE `mailman_admin_id` = ?',
    array($cfg->ITEM_TODELETE_STATUS, $event->getParam('customerId'))
    );
    }


    /**
    * Get routes
    *
    * @return array
    */
    public function getRoutes()
    {
    return $this->routes;
    }


    /**
    * Setup navigation
    */
    protected function setupNavigation()
    {
    if (iMSCP_Registry::isRegistered('navigation')) {
    /** @var Zend_Navigation $navigation */
    $navigation = iMSCP_Registry::get('navigation');


    if (($page = $navigation->findOneBy('uri', '/client/mail_accounts.php'))) {
    $page->addPage(
    array(
    'label' => tohtml(tr('Mailing List management')),
    'uri' => '/client/mailman.php',
    'title_class' => 'email',
    'order' => 3
    )
    );
    }
    }
    }


    /**
    * Create mailman database table
    *
    * @return void
    */
    protected function createDbTable()
    {
    execute_query(
    '
    CREATE TABLE IF NOT EXISTS `mailman` (
    `mailman_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
    `mailman_admin_id` int(11) unsigned NOT NULL,
    `mailman_admin_email` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
    `mailman_admin_password` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
    `mailman_list_name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
    `mailman_status` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
    PRIMARY KEY (`mailman_id`),
    UNIQUE KEY `mailman_list_name` (`mailman_list_name`),
    KEY `mailman_admin_id` (`mailman_admin_id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
    '
    );
    }
    }
    [/php]

    badge.php?id=1239063037&bid=2518&key=1747635596&format=png&z=547451206

    Edited once, last by Nuxwin ().