/**
  * Singleton
  * @return Application_Model_PluginsMapper
  */
 public static function i()
 {
     if (self::$_instance == null) {
         self::$_instance = new self();
     }
     return self::$_instance;
 }
 /**
  * Show configs page
  */
 public function indexAction()
 {
     $request = $this->getRequest();
     if ($request instanceof Zend_Controller_Request_Http) {
         if ($request->isXmlHttpRequest()) {
             $this->_helper->layout->disableLayout();
         }
     }
     $section = 'plugins';
     $key = $this->getRequest()->getParam('key', false);
     if ($key === false) {
         $this->_helper->flashMessenger(array('type' => 'error', 'text' => X_Env::_('config_invalidkey')));
         $this->_helper->redirector('index', 'manage');
     }
     $configs = Application_Model_ConfigsMapper::i()->fetchBySectionNamespace($section, $key);
     $form = $this->_initConfigsForm($section, $key, $configs);
     //$section = $this->getRequest()->getParam('section', false);
     $redirect = X_Env::decode($this->getRequest()->getParam('r', ''));
     if ($redirect == '') {
         $redirect = 'manage:index';
     }
     if (!is_null($form->getElement('redirect'))) {
         $form->redirect->setValue($redirect);
     }
     $defaultValues = array();
     foreach ($configs as $config) {
         /* @var $config Application_Model_Config */
         $elementName = $config->getSection() . '_' . str_replace('.', '_', $config->getKey());
         $defaultValues[$elementName] = $config->getValue();
     }
     $form->setDefaults($defaultValues);
     $plugins = Application_Model_PluginsMapper::i()->fetchAll();
     $this->view->plugins = $plugins;
     $this->view->form = $form;
     $this->view->messages = array_merge($this->_helper->flashMessenger->getMessages(), $this->_helper->flashMessenger->getCurrentMessages());
     $this->view->key = strtolower($key);
 }
Esempio n. 3
0
 public static function init($options, $helpersOptions = array())
 {
     // plugins are registered in plugin broker
     self::$_pluginBroker = new X_VlcShares_Plugins_Broker();
     if (!$options instanceof Zend_Config) {
         if (!is_array($options)) {
             $options = array();
         }
         $options = new Zend_Config($options);
     }
     if (!$helpersOptions instanceof Zend_Config) {
         if (!is_array($helpersOptions)) {
             $helpersOptions = array();
         }
         $helpersOptions = new Zend_Config($helpersOptions);
     }
     $plugins = Application_Model_PluginsMapper::i()->fetchAll();
     //foreach ($options as $o_k => $o_v ) {
     // 	$pValue = $o_v->toArray();
     //	$pKey = $o_k;
     foreach ($plugins as $plugin) {
         /* @var $plugin Application_Model_Plugin */
         if (!$plugin->isEnabled() && $plugin->getType() != Application_Model_Plugin::SYSTEM) {
             continue;
         }
         $pKey = $plugin->getKey();
         try {
             if ($options->{$pKey}) {
                 $pValue = $options->{$pKey}->toArray();
             } else {
                 // no configs
                 $pValue = array();
             }
         } catch (Exception $e) {
             // no configs
             $pValue = array();
         }
         $pValue['class'] = $plugin->getClass();
         if ($plugin->getFile() != null) {
             $pValue['path'] = APPLICATION_PATH . "/../library/" . $plugin->getFile();
         }
         $className = $pValue['class'];
         $path = @$pValue['path'];
         // se class non e' settato, il plugin nn e' valido
         if (!$className) {
             continue;
         }
         if ($path && substr($path, -4) == '.php' && file_exists($path)) {
             require_once $path;
         }
         if (class_exists($className) && is_subclass_of($className, 'X_VlcShares_Plugins_Abstract')) {
             $pValue['id'] = $pKey;
             // si auto referenzia
             //new $className(new Zend_Config($pValue));
             // plugins system from
             //	event-based -> function-based
             $plugin = new $className();
             //X_Debug::i($pKey . ":\n".var_export($pValue, true));
             $plugin->setConfigs(new Zend_Config($pValue));
             self::$_pluginBroker->registerPlugin($pKey, $plugin, true);
         }
     }
     self::$_helperBroker = new X_VlcShares_Plugins_Helper_Broker($helpersOptions);
     X_Debug::i("Plugin system enabled");
     $pluginlist = implode(', ', array_keys(self::$_pluginBroker->getPlugins()));
     X_Debug::i("Plugins registered: {$pluginlist}");
     $helperlist = implode(', ', array_keys(self::$_helperBroker->getHelpers()));
     X_Debug::i("Helpers registered: {$helperlist}");
     self::$_pluginBroker->gen_afterPluginsInitialized(self::$_pluginBroker);
 }
 public function enableAction()
 {
     /* @var $request Zend_Controller_Request_Http */
     $request = $this->getRequest();
     $pluginId = $request->getParam('pluginId', false);
     $plugin = new Application_Model_Plugin();
     $csrfValue = $request->getParam('csrf', false);
     $csrf = new Zend_Form_Element_Hash('csrf', array('salt' => __CLASS__));
     if ($csrf->isValid($csrfValue)) {
         if ($pluginId !== false) {
             Application_Model_PluginsMapper::i()->find($pluginId, $plugin);
             if ($plugin->getId() != null && $plugin->getId() == $pluginId) {
                 if ($plugin->getType() != Application_Model_Plugin::SYSTEM) {
                     try {
                         $plugin->setEnabled(true);
                         Application_Model_PluginsMapper::i()->save($plugin);
                         $this->_helper->flashMessenger(X_Env::_('configs_plugins_pluginenabled'));
                     } catch (Exception $e) {
                         $this->_helper->flashMessenger(X_Env::_('configs_plugins_err_db') . ": {$e->getMessage()}");
                     }
                 } else {
                     $this->_helper->flashMessenger(X_Env::_('configs_plugins_err_pluginId_notenable'));
                 }
             } else {
                 $this->_helper->flashMessenger(X_Env::_('configs_plugins_err_pluginId_unknown'));
             }
         } else {
             $this->_helper->flashMessenger(X_Env::_('configs_plugins_err_pluginId_missing'));
         }
     } else {
         $this->_helper->flashMessenger(array('type' => 'error', 'text' => X_Env::_('configs_plugins_err_invalidtoken')));
     }
     $this->_helper->redirector('index', 'configs');
 }
 /**
  * Restore core configs and plugins list in db 
  * This is not a trigger of plugin API. It's called by Backupper plugin
  * @param array $items Array format: 
  * 		array(
  * 			'plugins' => array(
  * 				array(
  * 					'key' => PLUGIN_KEY
  * 					'enabled' => 1|0
  *				),...
  *			),
  *			'configs' => array(
  *				array(
  *					'key' => CONFIG_KEY
  *					'value' => CONFIG_VALUE
  *					'section' => CONFIG_SECTION
  *				)
  *			)
  *		)
  */
 function restoreItems($items)
 {
     $pluginsStatusChanged = 0;
     $pluginsStatusErrors = 0;
     $configsStatusChanged = 0;
     $configsStatusErrors = 0;
     // as first thing i restore plugins status
     $plugins = Application_Model_PluginsMapper::i()->fetchAll();
     foreach ($plugins as $plugin) {
         /* @var $plugin Application_Model_Plugin */
         if (array_key_exists($plugin->getKey(), $items['plugins'])) {
             if ($plugin->isEnabled() != (bool) @$items['plugins'][$plugin->getKey()]['enabled']) {
                 $plugin->setEnabled((bool) @$items['plugins'][$plugin->getKey()]['enabled']);
                 // i need to commit change
                 try {
                     Application_Model_PluginsMapper::i()->save($plugin);
                     $pluginsStatusChanged++;
                 } catch (Exception $e) {
                     X_Debug::e("Failed to update plugin {$plugin->getKey()} status to {$items['plugins'][$plugin->getKey()]['enabled']}");
                     $pluginsStatusErrors++;
                 }
             }
         }
     }
     // then i restore configs status
     $configs = Application_Model_ConfigsMapper::i()->fetchAll();
     foreach ($configs as $config) {
         /* @var $config Application_Model_Config */
         if (array_key_exists($config->getKey(), $items['configs'])) {
             if ($config->getSection() == @$items['configs'][$config->getKey()]['section'] && $config->getValue() != $items['configs'][$config->getKey()]['value']) {
                 $config->setValue(@$items['configs'][$config->getKey()]['value']);
                 // i need to commit change
                 try {
                     Application_Model_ConfigsMapper::i()->save($config);
                     $configsStatusChanged++;
                 } catch (Exception $e) {
                     X_Debug::e("Failed to update plugin {$config->getKey()} status to {$items['configs'][$config->getKey()]['value']}");
                     $configsStatusErrors++;
                 }
             }
         }
     }
     // return a custom message with restore results
     return X_Env::_('p_backupper_restorereport_main') . '<br/><dl style="margin: 1em 3em;">' . '<dt>' . X_Env::_('p_backupper_restorereport_configchanged') . '</dt>' . '<dd>' . $configsStatusChanged . '</dd>' . '<dt>' . X_Env::_('p_backupper_restorereport_configerrors') . '</dt>' . '<dd>' . $configsStatusErrors . '</dd>' . '<dt>' . X_Env::_('p_backupper_restorereport_pluginchanged') . '</dt>' . '<dd>' . $pluginsStatusChanged . '</dd>' . '<dt>' . X_Env::_('p_backupper_restorereport_pluginerrors') . '</dt>' . '<dd>' . $pluginsStatusErrors . '</dd>' . '</dl>';
 }
 public function installPlugin($source, $isUrl = false)
 {
     X_Debug::i("Installing plugin from {{$source}}: isUrl = {{$isUrl}}");
     if ($isUrl) {
         // perform a download in a temp file
         $http = new Zend_Http_Client($source, array('headers' => array('User-Agent' => "vlc-shares/" . X_VlcShares::VERSION . " plugininstaller/" . X_VlcShares::VERSION)));
         $http->setStream(true);
         $source = $http->request()->getStreamName();
     }
     try {
         // unzip and manifest parse
         $egg = X_Egg::factory($source, APPLICATION_PATH . '/../', APPLICATION_PATH . '/../data/plugin/tmp/', true);
         $pluginKey = $egg->getKey();
         // first we must check if key already exists in the db
         $plugin = new Application_Model_Plugin();
         Application_Model_PluginsMapper::i()->fetchByKey($pluginKey, $plugin);
         if ($plugin->getId() !== null) {
             throw new Exception(X_Env::_('plugin_err_installerror_keyexists') . ": {$pluginKey}");
         }
         // time to check if plugin support this vlc-shares version
         $vFrom = $egg->getCompatibilityFrom();
         $vTo = $egg->getCompatibilityTo();
         if (version_compare(X_VlcShares::VERSION_CLEAN, $vFrom, '<') || $vTo !== null && version_compare(X_VlcShares::VERSION_CLEAN, $vTo, '>=')) {
             throw new Exception(X_Env::_('plugin_err_installerror_unsupported') . ": {$vFrom} - {$vTo}");
         }
         // copy the files: first check if some file exists...
         $toBeCopied = array();
         foreach ($egg->getFiles() as $file) {
             /* @var $file X_Egg_File */
             if (!$file->getProperty(X_Egg_File::P_REPLACE, false) && file_exists($file->getDestination())) {
                 throw new Exception(X_Env::_('plugin_err_installerror_fileexists') . ": {$file->getDestination()}");
             }
             if (!file_exists($file->getSource())) {
                 if (!$file->getProperty(X_Egg_File::P_IGNOREIFNOTEXISTS, false)) {
                     throw new Exception(X_Env::_('plugin_err_installerror_sourcenotexists') . ": {$file->getSource()}");
                 }
                 // ignore this item if P_IGNOREIFNOTEXISTS is true and file not exists
                 continue;
             }
             $toBeCopied[] = array('src' => $file->getSource(), 'dest' => $file->getDestination(), 'resource' => $file);
         }
         // before copy act, i must be sure to be able to revert changes
         $plugin = new Application_Model_Plugin();
         $plugin->setLabel($egg->getLabel())->setKey($pluginKey)->setDescription($egg->getDescription())->setFile($egg->getFile())->setClass($egg->getClass())->setType(Application_Model_Plugin::USER)->setVersion($egg->getVersion());
         Application_Model_PluginsMapper::i()->save($plugin);
         // so i must copy uninstall information inside a uninstall dir in data
         $dest = APPLICATION_PATH . '/../data/plugin/_uninstall/' . $pluginKey;
         // i have to create the directory
         if (!mkdir($dest, 0777, true)) {
             throw new Exception(X_Env::_('plugin_err_installerror_uninstalldircreation') . ": {$dest}");
         }
         if (!copy($egg->getManifestFile(), "{$dest}/manifest.xml")) {
             throw new Exception(X_Env::_('plugin_err_installerror_uninstallmanifestcopy') . ": " . $egg->getManifestFile() . " -> {$dest}/manifest.xml");
         }
         $uninstallSql = $egg->getUninstallSQL();
         if ($uninstallSql !== null && file_exists($uninstallSql)) {
             if (!copy($uninstallSql, "{$dest}/uninstall.sql")) {
                 throw new Exception(X_Env::_('plugin_err_installerror_uninstallsqlcopy') . ": {$dest}");
             }
         }
         // ... then copy
         foreach ($toBeCopied as $copyInfo) {
             $copied = false;
             if (!file_exists(dirname($copyInfo['dest']))) {
                 @mkdir(dirname($copyInfo['dest']), 0777, true);
             }
             if (!copy($copyInfo['src'], $copyInfo['dest'])) {
                 $this->_helper->flashMessenger(array('text' => X_Env::_('plugin_err_installerror_copyerror') . ": <br/>" . $copyInfo['src'] . '<br/>' . $copyInfo['dest'], 'type' => 'error'));
             } else {
                 X_Debug::i("File copied {{$copyInfo['dest']}}");
                 $copied = true;
             }
             /* @var $xeggFile X_Egg_File */
             $xeggFile = $copyInfo['resource'];
             if ($copied) {
                 // check permission
                 $permission = $xeggFile->getProperty(X_Egg_File::P_PERMISSIONS, false);
                 if ($permission !== false) {
                     if (!chmod($copyInfo['dest'], octdec($permission))) {
                         X_Debug::e("Chmod {{$permission}} failed for file {{$copyInfo['dest']}}");
                     } else {
                         X_Debug::i("Permissions set to {{$permission}} for file {{$copyInfo['dest']}} as required");
                     }
                 }
             } else {
                 if ($xeggFile->getProperty(X_Egg_File::P_HALTONCOPYERROR, false)) {
                     X_Debug::f("File not copied {{$copyInfo['dest']}} and flagged as HaltOnCopyError");
                     break;
                 }
             }
         }
         // change database
         $installSql = $egg->getInstallSQL();
         if ($installSql !== null && file_exists($installSql)) {
             try {
                 $dataSql = file_get_contents($installSql);
                 if (trim($dataSql) !== '') {
                     $bootstrap = Zend_Controller_Front::getInstance()->getParam('bootstrap');
                     $db = $bootstrap->getResource('db');
                     $db->getConnection()->exec($dataSql);
                 }
             } catch (Exception $e) {
                 X_Debug::e("DB Error while installind: {$e->getMessage()}");
                 $this->_helper->flashMessenger(X_Env::_('plugin_err_installerror_sqlerror') . ": {$e->getMessage()}");
                 //throw $e;
             }
         }
         // process acl fragment
         $aclHelper = X_VlcShares_Plugins::helpers()->acl();
         // new classes
         $accounts = Application_Model_AuthAccountsMapper::i()->fetchAll();
         foreach ($egg->getAclClasses() as $aclClass) {
             /* @var $aclClass X_Egg_AclClass */
             $res = $aclHelper->addClass($aclClass->getName(), $aclClass->getProperty(X_Egg_AclClass::P_DESCRIPTION, ''));
             if (!$res) {
                 $this->_helper->flashMessenger(array('text' => X_Env::_('plugin_err_installerror_aclclass', $aclClass->getName()), 'type' => 'warning'));
                 continue;
             }
             $extends = $aclClass->getExtends();
             if (count($extends)) {
                 foreach ($accounts as $account) {
                     /* @var $account Application_Model_AuthAccount */
                     foreach ($extends as $baseClass) {
                         if (in_array($baseClass, $aclHelper->getPermissions($account->getUsername()))) {
                             $aclHelper->grantPermission($account->getUsername(), $aclClass->getName());
                         }
                     }
                 }
             }
         }
         //new resources
         foreach ($egg->getAclResources() as $resource) {
             /* @var $resource X_Egg_AclResource */
             $aclHelper->addResource($resource->getKey(), $resource->getClass(), $egg->getKey(), false);
         }
         $egg->cleanTmp();
         unlink($source);
         return true;
     } catch (Exception $e) {
         if ($egg !== null) {
             $egg->cleanTmp();
         }
         // delete the uploaded file
         unlink($source);
         //$this->_helper->flashMessenger(array('text' => X_Env::_('plugin_err_installerror').": ".$e->getMessage(), 'type' => 'error'));
         //return false;
         throw $e;
     }
 }
 function uninstallAction()
 {
     /* @var $request Zend_Controller_Request_Http */
     $request = $this->getRequest();
     if (!$request->isPost()) {
         $this->_helper->flashMessenger(array('text' => X_Env::_('plugin_err_invalidrequest'), 'type' => 'error'));
         $this->_helper->redirector('index', 'plugin');
     }
     $form = new Application_Form_PluginUConfirm();
     if (!$form->isValid($request->getPost())) {
         $this->_helper->flashMessenger(array('text' => X_Env::_('plugin_err_invalidrequest'), 'type' => 'error'));
         $this->_helper->redirector('index', 'plugin');
     }
     $key = $form->getValue('key', false);
     if ($key === false) {
         $this->_helper->flashMessenger(array('text' => X_Env::_('plugin_err_invalidkey'), 'type' => 'error'));
         $this->_helper->redirector('index', 'plugin');
     }
     $plugin = new Application_Model_Plugin();
     Application_Model_PluginsMapper::i()->fetchByKey($key, $plugin);
     if ($plugin->getId() === null || $plugin->getKey() != $key || $plugin->getType() != Application_Model_Plugin::USER) {
         $this->_helper->flashMessenger(array('text' => X_Env::_('plugin_err_invalidplugin'), 'type' => 'error'));
         $this->_helper->redirector('index', 'plugin');
     }
     // time to get uninstall informations
     $manifest = APPLICATION_PATH . '/../data/plugin/_uninstall/' . $plugin->getKey() . '/manifest.xml';
     if (file_exists($manifest)) {
         try {
             $this->_uninstall($manifest);
             // rebuild update notifier cache
             if (X_VlcShares_Plugins::broker()->isRegistered('updatenotifier')) {
                 $notifier = X_VlcShares_Plugins::broker()->getPlugins('updatenotifier');
                 if (method_exists($notifier, 'clearLastCheck')) {
                     $notifier->clearLastCheck();
                 }
             }
             // all done, continue
         } catch (Exception $e) {
             $this->_helper->flashMessenger(array('text' => X_Env::_('plugin_err_uninstall_processingmanifest') . ": {$e->getMessage()}", 'type' => 'error'));
             $this->_helper->redirector('index', 'plugin');
         }
     } else {
         $this->_helper->flashMessenger(array('text' => X_Env::_('plugin_warn_uninstall_manifestnotfound'), 'type' => 'warning'));
     }
     Application_Model_PluginsMapper::i()->delete($plugin);
     $this->_helper->flashMessenger(array('text' => X_Env::_('plugin_uninstall_done'), 'type' => 'info'));
     $this->_helper->redirector('index', 'plugin');
 }
 public function executeAction()
 {
     // propagate current messages
     $messages = $this->_helper->flashMessenger->getMessages();
     foreach ($messages as $message) {
         $this->_helper->flashMessenger->addMessage($message);
     }
     try {
         try {
             if (file_exists(APPLICATION_PATH . '/../scripts/backup.sqlite.sql')) {
                 // an update is needed
                 // script should use transition
                 $dataSql = file_get_contents(APPLICATION_PATH . '/../scripts/backup.sqlite.sql');
                 if (trim($dataSql) !== '') {
                     $bootstrap = $this->getFrontController()->getParam('bootstrap');
                     $db = $bootstrap->getResource('db');
                     $db->getConnection()->exec($dataSql);
                 }
             }
         } catch (Exception $e) {
             X_Debug::e("DB Error while restoring: {$e->getMessage()}");
             $this->_helper->flashMessenger(X_Env::_('installer_err_db') . ": {$e->getMessage()}");
         }
         // after all, i will delete first run plugin from the db
         $plugin = new Application_Model_Plugin();
         Application_Model_PluginsMapper::i()->fetchByClass('X_VlcShares_Plugins_FirstRunSetup', $plugin);
         //Application_Model_PluginsMapper::i()->delete($plugin);
         $plugin->setEnabled(false);
         Application_Model_PluginsMapper::i()->save($plugin);
         $this->_helper->flashMessenger(X_Env::_('installer_op_completed'));
         // all done, redirect to config page
         $this->_helper->redirector('index', 'gconfigs', null, array('filter' => 'vlc'));
     } catch (Exception $e) {
         $this->_helper->flashMessenger(X_Env::_('installer_err_db') . ": {$e->getMessage()}");
     }
 }