pages/manager/PluginManagementHandler.inc.php

Go to the documentation of this file.
00001 <?php
00002 
00015 // $Id$
00016 
00017 define('VERSION_FILE', '/version.xml');
00018 define('INSTALL_FILE', '/install.xml');
00019 define('UPGRADE_FILE', '/upgrade.xml');
00020 
00021 import('site.Version');
00022 import('site.VersionCheck');
00023 import('file.FileManager');
00024 import('install.Install');
00025 import('pages.manager.ManagerHandler');
00026 
00027 class PluginManagementHandler extends ManagerHandler {
00031    function PluginManagementHandler() {
00032       parent::ManagerHandler();
00033    }
00034    
00038    function managePlugins($args) {
00039       $this->validate();
00040       $path = isset($args[0])?$args[0]:null;
00041       $plugin = isset($args[1])?$args[1]:null;
00042       
00043       switch($path) {
00044          case 'install':
00045             PluginManagementHandler::showInstallForm();
00046             break;
00047          case 'installPlugin':
00048             PluginManagementHandler::uploadPlugin('install');
00049             break;
00050          case 'upgrade':
00051             PluginManagementHandler::showUpgradeForm($plugin);
00052             break;
00053          case 'upgradePlugin':
00054             PluginManagementHandler::uploadPlugin('upgrade');
00055             break;
00056          case 'delete':
00057             PluginManagementHandler::showDeleteForm($plugin);
00058             break;
00059          case 'deletePlugin':
00060             PluginManagementHandler::deletePlugin($plugin);
00061             break;
00062          default:
00063             Request::redirect(null, 'manager', 'plugins');           
00064       }
00065    
00066       $this->setupTemplate(true);
00067    }
00068 
00072    function showInstallForm() {
00073       $this->validate();
00074       $templateMgr =& TemplateManager::getManager();
00075       $this->setupTemplate(true);
00076       
00077       $templateMgr->assign('path', 'install');
00078       $templateMgr->assign('uploaded', false);
00079       $templateMgr->assign('error', false);
00080       $templateMgr->assign('pageHierarchy', PluginManagementHandler::setBreadcrumbs(true));
00081 
00082       $templateMgr->display('manager/plugins/managePlugins.tpl');
00083    }
00084    
00089    function showUpgradeForm($plugin) {
00090       $this->validate();
00091       $templateMgr =& TemplateManager::getManager();
00092       $this->setupTemplate(true);
00093 
00094       $templateMgr->assign('path', 'upgrade');
00095       $templateMgr->assign('plugin', $plugin);
00096       $templateMgr->assign('uploaded', false);
00097       
00098       $category = $this->getPluginCategory($plugin);
00099       $templateMgr->assign('pageHierarchy', $this->setBreadcrumbs(true, $category));
00100 
00101       $templateMgr->display('manager/plugins/managePlugins.tpl');
00102    }
00103    
00108    function showDeleteForm($plugin) {
00109       $this->validate();
00110       $templateMgr =& TemplateManager::getManager();
00111       $this->setupTemplate(true);
00112       
00113       $templateMgr->assign('path', 'delete');
00114       $templateMgr->assign('plugin', $plugin);
00115       $templateMgr->assign('deleted', false);
00116       $templateMgr->assign('error', false);
00117 
00118       $category = $this->getPluginCategory($plugin);
00119       $templateMgr->assign('pageHierarchy', $this->setBreadcrumbs(true, $category));
00120 
00121       $templateMgr->display('manager/plugins/managePlugins.tpl');
00122    }
00123 
00124    
00129    function uploadPlugin($function) {
00130       $this->validate();
00131       $templateMgr =& TemplateManager::getManager();
00132       $this->setupTemplate(true);
00133       
00134       $templateMgr->assign('error', false);
00135       $templateMgr->assign('uploaded', false);
00136       $templateMgr->assign('path', $function);
00137       $templateMgr->assign('pageHierarchy', PluginManagementHandler::setBreadcrumbs(true));
00138       
00139       if (Request::getUserVar('uploadPlugin')) {
00140          import('file.TemporaryFileManager');
00141          $temporaryFileManager = new TemporaryFileManager();
00142          $user =& Request::getUser();
00143       
00144          if ($temporaryFile = $temporaryFileManager->handleUpload('newPlugin', $user->getId())) {
00145             // tar archive basename must equal plugin directory name, and plugin files must be in root directory
00146             $pluginName = basename($temporaryFile->getOriginalFileName(), '.tar.gz');
00147             $pluginDir = dirname($temporaryFile->getFilePath());
00148             exec('tar -xzf ' . escapeshellarg($temporaryFile->getFilePath()) . ' -C ' . escapeshellarg($pluginDir));
00149             
00150             if ($function == 'install') {
00151                PluginManagementHandler::installPlugin($pluginDir . DIRECTORY_SEPARATOR . $pluginName, $templateMgr);
00152             } else if ($function == 'upgrade') { 
00153                PluginManagementHandler::upgradePlugin($pluginDir . DIRECTORY_SEPARATOR . $pluginName, $templateMgr);
00154             }
00155          } else {
00156             $templateMgr->assign('error', true);
00157             $templateMgr->assign('message', 'manager.plugins.uploadError');
00158          }
00159       } else if (Request::getUserVar('installPlugin')) {
00160          if(Request::getUserVar('pluginUploadLocation') == '') {
00161             $templateMgr->assign('error', true);
00162             $templateMgr->assign('message', 'manager.plugins.fileSelectError');
00163          }
00164       }
00165 
00166       $templateMgr->display('manager/plugins/managePlugins.tpl');
00167    }
00168    
00175    function installPlugin($path, &$templateMgr) {
00176       $this->validate();
00177       $versionFile = $path . VERSION_FILE;
00178       $templateMgr->assign('error', true);
00179       $templateMgr->assign('path', 'install');
00180 
00181       if (FileManager::fileExists($versionFile)) {
00182          $versionInfo =& VersionCheck::parseVersionXML($versionFile);
00183       } else {
00184          $templateMgr->assign('message', 'manager.plugins.versionFileNotFound');
00185          return false;
00186       }
00187       
00188       $pluginVersion = $versionInfo['version'];
00189       $pluginName = $pluginVersion->getProduct();
00190       $category = $this->getPluginCategory($plugin);
00191       
00192       $versionDao =& DAORegistry::getDAO('VersionDAO'); 
00193       $installedPlugin = $versionDao->getCurrentVersion($pluginName);
00194       
00195       if(!$installedPlugin) {
00196          $pluginDest = Core::getBaseDir() . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . $category . DIRECTORY_SEPARATOR . $pluginName;
00197 
00198          if(!FileManager::copyDir($path, $pluginDest)) {
00199             $templateMgr->assign('message', 'manager.plugins.copyError');
00200             return false;
00201          }
00202          
00203          // If plugin has an install.xml file, update database with it
00204          $installFile = $pluginDest . DIRECTORY_SEPARATOR . INSTALL_FILE;
00205          if(FileManager::fileExists($installFile)) {
00206             $params = PluginManagementHandler::setConnectionParams();
00207             $installer = new Install($params, $installFile, true);
00208 
00209             if ($installer->execute()) {
00210                $newVersion =& $installer->getNewVersion();
00211             } else {
00212                // Roll back the copy
00213                FileManager::rmtree($pluginDest);
00214                $templateMgr->assign('message', array('manager.plugins.installFailed', $installer->getErrorString()));
00215                return false;
00216             }
00217          } else {
00218             $newVersion = $pluginVersion;
00219          }
00220          
00221          $message = array('manager.plugins.installSuccessful', $newVersion->getVersionString());
00222          $templateMgr->assign('message', $message);
00223          $templateMgr->assign('uploaded', true);
00224          $templateMgr->assign('error', false);
00225          
00226          $newVersion->setCurrent(1);
00227          $versionDao->insertVersion($newVersion);
00228          return true;
00229       } else {
00230          if (PluginManagementHandler::checkIfNewer($pluginName, $pluginVersion)) {
00231             $templateMgr->assign('message', 'manager.plugins.pleaseUpgrade');
00232             return false;
00233          }
00234          if (!PluginManagementHandler::checkIfNewer($pluginName, $pluginVersion)) {
00235             $templateMgr->assign('message', 'manager.plugins.installedVersionOlder');
00236             return false;
00237          }
00238       }
00239    }
00240    
00247    function upgradePlugin($path, &$templateMgr) {
00248       $this->validate();
00249       $versionFile = $path . VERSION_FILE;
00250       $templateMgr->assign('error', true);
00251       $templateMgr->assign('path', 'upgrade');
00252       
00253       if (FileManager::fileExists($versionFile)) {
00254          $versionInfo =& VersionCheck::parseVersionXML($versionFile);
00255       } else {
00256          $templateMgr->assign('message', 'manager.plugins.versionFileNotFound');
00257          return false;
00258       }
00259       
00260       $pluginVersion = $versionInfo['version'];
00261       $pluginName = $versionInfo['application'];
00262       $category = explode(".", $pluginVersion->getProductType());
00263       
00264       $versionDao =& DAORegistry::getDAO('VersionDAO');
00265       $installedPlugin = $versionDao->getCurrentVersion($pluginName);
00266       if(!$installedPlugin) {
00267          $templateMgr->assign('message', 'manager.plugins.pleaseInstall');
00268          return false;
00269       }
00270       
00271       if (PluginManagementHandler::checkIfNewer($pluginName, $pluginVersion)) {
00272          $templateMgr->assign('message', 'manager.plugins.installedVersionNewer');
00273          return false;
00274       } else {
00275          $pluginDest = Core::getBaseDir() . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . $category . DIRECTORY_SEPARATOR . $pluginName;
00276          
00277          FileManager::rmtree($pluginDest);
00278          if(FileManager::fileExists($pluginDest, 'dir')) {
00279             $templateMgr->assign('message', 'manager.plugins.deleteError');
00280             return false;
00281          }
00282          if(!FileManager::copyDir($path, $pluginDest)) {
00283             $templateMgr->assign('message', 'manager.plugins.copyError');
00284             return false;
00285          }
00286          
00287          $upgradeFile = $pluginDest . DIRECTORY_SEPARATOR . UPGRADE_FILE;
00288          if(FileManager::fileExists($upgradeFile)) {
00289             $params = PluginManagementHandler::setConnectionParams();
00290             $installer = new Upgrade($params, $upgradeFile, true);
00291 
00292             if (!$installer->execute()) {
00293                $templateMgr->assign('message', array('manager.plugins.upgradeFailed', $installer->getErrorString()));
00294                return false;
00295             }
00296          }
00297          
00298          $installedPlugin->setCurrent(0);
00299          $pluginVersion->setCurrent(1);
00300          $versionDao->insertVersion($pluginVersion);
00301          
00302          $templateMgr->assign('message', array('manager.plugins.upgradeSuccessful', $pluginVersion->getVersionString()));
00303          $templateMgr->assign('uploaded', true);
00304          $templateMgr->assign('error', false);
00305          
00306          return true;
00307       }
00308    }
00309    
00314    function deletePlugin($plugin) {
00315       $this->validate();
00316       $templateMgr =& TemplateManager::getManager();
00317       $this->setupTemplate(true);
00318       
00319       $templateMgr->assign('path', 'delete');
00320       $templateMgr->assign('deleted', false);
00321       $templateMgr->assign('error', false);
00322       $templateMgr->assign('pageHierarchy', PluginManagementHandler::setBreadcrumbs(true));
00323 
00324       $versionDao =& DAORegistry::getDAO('VersionDAO'); 
00325       $installedPlugin = $versionDao->getCurrentVersion($plugin);
00326       $category = $this->getPluginCategory($plugin);
00327       
00328       if ($installedPlugin) {
00329          $pluginDest = Core::getBaseDir() . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . $category . DIRECTORY_SEPARATOR . $plugin;
00330 
00331          //make sure plugin type is valid and then delete the files
00332          if (in_array($category, PluginRegistry::getCategories())) {
00333             FileManager::rmtree($pluginDest);
00334          }
00335          
00336          if(FileManager::fileExists($pluginDest, 'dir')) {
00337             $templateMgr->assign('error', true);
00338             $templateMgr->assign('message', 'manager.plugins.deleteError');
00339          } else {
00340             $versionDao->disableVersion($plugin);
00341             $templateMgr->assign('deleted', true); 
00342          }
00343          
00344       } else {
00345          $templateMgr->assign('error', true);
00346          $templateMgr->assign('message', 'manager.plugins.doesNotExist');
00347       }
00348       
00349       $templateMgr->assign('pageHierarchy', $this->setBreadcrumbs(true, $category));
00350       $templateMgr->display('manager/plugins/managePlugins.tpl');
00351    }
00352    
00359    function checkIfNewer($pluginName, $newVersion) {
00360       $versionDao =& DAORegistry::getDAO('VersionDAO');
00361       $installedPlugin = $versionDao->getCurrentVersion($pluginName);
00362 
00363       if (!$installedPlugin) return false;
00364       if ($installedPlugin->compare($newVersion) > 0) return true;
00365       else return false;
00366    }
00367    
00373    function setBreadcrumbs($subclass = false, $category = null) {
00374       $templateMgr =& TemplateManager::getManager();
00375       $pageCrumbs = array(
00376          array(
00377             Request::url(null, null, 'user'),
00378             'navigation.user',
00379             false
00380          ),
00381          array(
00382             Request::url(null, null, 'manager'),
00383             'manager.conferenceSiteManagement',
00384             false
00385          )
00386       );
00387       
00388       if ($subclass) {
00389          $pageCrumbs[] = array(
00390             Request::url(null, null, 'manager', 'plugins'),
00391             'manager.plugins.pluginManagement',
00392             false
00393          );
00394       }
00395 
00396       if ($category) {
00397          $pageCrumbs[] = array(
00398             Request::url(null, null, 'manager', 'plugins', $category),
00399             "plugins.categories.$category",
00400             false
00401          );
00402       }
00403       
00404       return $pageCrumbs;
00405    }
00406       
00412    function getPluginCategory($plugin) {
00413       $versionDao =& DAORegistry::getDAO('VersionDAO'); 
00414       $installedPlugin = $versionDao->getCurrentVersion($plugin);
00415       
00416       $pluginType = explode(".", $installedPlugin->getProductType());
00417       
00418       return $pluginType[1];
00419    }
00420    
00425    function setConnectionParams() {
00426       return array(
00427          'clientCharset' => Config::getVar('i18n', 'client_charset'),
00428          'connectionCharset' => Config::getVar('i18n', 'connection_charset'),
00429          'databaseCharset' => Config::getVar('i18n', 'database_charset'),
00430          'databaseDriver' => Config::getVar('database', 'driver'),
00431          'databaseHost' => Config::getVar('database', 'host'),
00432          'databaseUsername' => Config::getVar('database', 'username'),
00433          'databasePassword' => Config::getVar('database', 'password'),
00434          'databaseName' => Config::getVar('database', 'name')
00435       );
00436    }
00437 }
00438 
00439 ?>

Generated on 25 Jul 2013 for Open Conference Systems by  doxygen 1.4.7