/**
  * Singleton
  * @return Application_Model_AuthAccountsMapper
  */
 public static function i()
 {
     if (self::$_instance == null) {
         self::$_instance = new self();
     }
     return self::$_instance;
 }
 function saveAction()
 {
     if (!$this->getRequest()->isPost()) {
         $this->_helper->flashMessenger(array('type' => 'error', 'text' => X_Env::_('p_auth_err_invalidrequest')));
         $this->_helper->redirector('accounts', 'auth');
         return;
     }
     $form = new Application_Form_AuthAccount();
     if ($this->getRequest()->getPost('id', false)) {
         $form->password->setAttrib('allowEmpty', true);
         $form->password->setRequired(false);
         $form->username->setRequired(false);
         $form->username->setAttrib('disabled', true);
     }
     $_permissions = X_VlcShares_Plugins::helpers()->acl()->getClasses();
     $permissions = array();
     $permissionsDefault = array();
     foreach ($_permissions as $perm) {
         /* @var $perm Application_Model_AclClass */
         $description = X_Env::_($perm->getDescription());
         $permissions[$perm->getName()] = "{$perm->getName()} - <i>{$description}</i>";
         //if ( in_array($perm->getName(), X_VlcShares_Plugins::helpers()->acl()->getPermissions($account->getUsername())) ) {
         //$permissionsDefault[] = $perm->getName();
         //}
     }
     $form->permissions->setMultiOptions($permissions);
     if ($form->isValid($this->getRequest()->getPost())) {
         $account = new Application_Model_AuthAccount();
         $id = $form->getValue('id');
         if ($id) {
             Application_Model_AuthAccountsMapper::i()->find($id, $account);
             if (is_null($account->getId())) {
                 $this->_helper->flashMessenger(array('type' => 'error', 'text' => X_Env::_('p_auth_err_invalidaccount')));
                 $this->_helper->redirector('accounts', 'auth');
                 return;
             }
             // empty passwords are ignored: empty = do not change
             if (strlen($form->getValue('password')) > 0) {
                 $account->setPassword(md5("{$account->getUsername()}:{$form->getValue('password')}"))->setPassphrase(md5("{$account->getUsername()}:{$form->getValue('password')}:" . rand(10000, 99999) . time()));
             }
         } else {
             $account->setUsername($form->getValue('username'));
             $account->setPassword(md5("{$form->getValue('username')}:{$form->getValue('password')}"))->setPassphrase(md5("{$form->getValue('username')}:{$form->getValue('password')}:" . rand(10000, 99999) . time()));
         }
         $account->setEnabled((bool) $form->getValue('enabled'))->setAltAllowed((bool) $form->getValue('altallowed'));
         try {
             Application_Model_AuthAccountsMapper::i()->save($account);
             // if is a new account, grant browse permission to the new account
             /*
             if ( !$id ) {
             	X_VlcShares_Plugins::helpers()->acl()->grantPermission($account->getUsername(), Application_Model_AclClass::CLASS_BROWSE);
             }
             */
             $acl = X_VlcShares_Plugins::helpers()->acl();
             $prevPermissions = array();
             if ($id) {
                 $prevPermissions = $acl->getPermissions($account->getUsername());
             }
             $newPermissions = $form->getValue('permissions');
             // first remove all old permissions that are not available anymore
             foreach ($prevPermissions as $pPerm) {
                 if (!in_array($pPerm, $newPermissions)) {
                     $acl->revokePermission($account->getUsername(), $pPerm);
                 }
             }
             // add new granted not in old permissions
             foreach ($newPermissions as $nPerm) {
                 if (!in_array($nPerm, $prevPermissions)) {
                     $acl->grantPermission($account->getUsername(), $nPerm);
                 }
             }
             $this->_helper->flashMessenger(array('type' => 'success', 'text' => X_Env::_('p_auth_accountstored')));
             $this->_helper->redirector('accounts', 'auth');
         } catch (Exception $e) {
             $this->_helper->flashMessenger(array('type' => 'error', 'text' => X_Env::_('p_auth_dberror', $e->getMessage())));
             $this->_helper->redirector('accounts', 'auth');
         }
     } else {
         $form->setAction($this->_helper->url('save', 'auth'));
         $form->setDefaults($this->getRequest()->getPost());
         $this->view->form = $form;
         $this->_helper->viewRenderer->setScriptAction('edit');
     }
 }
示例#3
0
 public function checkAuth($username, $password, $altMethod = false)
 {
     if (!$altMethod) {
         return Application_Model_AuthAccountsMapper::i()->fetchByUsernamePassword($username, $password);
     } else {
         return Application_Model_AuthAccountsMapper::i()->fetchByUsernamePassphrase($username, $password);
     }
 }
 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;
     }
 }
 public function saveAction()
 {
     $lang = $this->getRequest()->getParam('lang', false);
     $lang = $lang !== false ? str_replace('../', '', $lang) : $lang;
     if ($lang !== false && file_exists(APPLICATION_PATH . "/../languages/{$lang}")) {
         $config = new Application_Model_Config();
         Application_Model_ConfigsMapper::i()->fetchByKey('languageFile', $config);
         if ($config->getId() !== null) {
             $config->setValue($lang);
             try {
                 Application_Model_ConfigsMapper::i()->save($config);
                 $this->_helper->flashMessenger(array('type' => 'success', 'text' => X_Env::_('installer_language_done')));
                 //$this->_helper->redirector('execute');
             } catch (Exception $e) {
                 $this->_helper->flashMessenger(array('type' => 'fatal', 'text' => X_Env::_("installer_err_db") . ": {$e->getMessage()}"));
             }
         }
     } else {
         $this->_helper->flashMessenger(array('type' => 'error', 'text' => X_Env::_('installer_invalid_language')));
         $this->_helper->redirector('index');
     }
     // check for admin username/password
     $form = new Application_Form_Installer();
     $form->removeElement('lang');
     $form->removeElement('plugins');
     if (!$form->isValid($this->getRequest()->getPost())) {
         $ns = new Zend_Session_Namespace('vlc-shares::installer');
         $ns->errors = true;
         $ns->data = $this->getRequest()->getPost();
         $this->_helper->flashMessenger(array('type' => 'error', 'text' => X_Env::_('installer_invalid_data')));
         $this->_helper->redirector('index');
         return;
     }
     $username = $form->getValue('username');
     $password = $form->getValue('password');
     if (Application_Model_AuthAccountsMapper::i()->getCount(true) == 0) {
         // try to reenable/create a new account
         try {
             $account = new Application_Model_AuthAccount();
             Application_Model_AuthAccountsMapper::i()->fetchByUsername($username);
             $account->setUsername($username)->setPassword(md5("{$username}:{$password}"))->setEnabled(true)->setPassphrase(md5("{$username}:{$password}:" . rand(10000, 99999) . time()))->setAltAllowed(true);
             Application_Model_AuthAccountsMapper::i()->save($account);
             // adding permissions
             X_VlcShares_Plugins::helpers()->acl()->grantPermission($username, Application_Model_AclClass::CLASS_ADMIN);
             X_VlcShares_Plugins::helpers()->acl()->grantPermission($username, Application_Model_AclClass::CLASS_BROWSE);
             $this->_helper->flashMessenger(array('type' => 'success', 'text' => X_Env::_('installer_newaccount_done')));
         } catch (Exception $e) {
             $this->_helper->flashMessenger(X_Env::_('installer_err_db') . ": {$e->getMessage()}");
             $this->_helper->redirector('index');
             return;
         }
         // after that account is stored, try to do a login
         if (X_VlcShares_Plugins::broker()->isRegistered('auth')) {
             $auth = X_VlcShares_Plugins::broker()->getPlugins('auth');
         } else {
             $auth = new X_VlcShares_Plugins_Auth();
         }
         if (!$auth->isLoggedIn()) {
             $auth->doLogin($username);
         }
     }
     try {
         // enable auth plugin after authentication
         if ($form->getValue('auth', '0') == '1') {
             /*
             		    	$plugin = new Application_Model_Plugin();
             		    	Application_Model_PluginsMapper::i()->fetchByClass('X_VlcShares_Plugins_Auth', $plugin);
             		    	//Application_Model_PluginsMapper::i()->delete($plugin);
             $plugin->setEnabled(true);
             Application_Model_PluginsMapper::i()->save($plugin);
             */
             $config = new Application_Model_Config();
             Application_Model_ConfigsMapper::i()->fetchByKey('auth.login.enabled', $config);
             $config->setValue('1');
             Application_Model_ConfigsMapper::i()->save($config);
         }
         // check forker url
         $forkerUrl = $form->getValue('threads', false);
         if ($forkerUrl) {
             $config = new Application_Model_Config();
             Application_Model_ConfigsMapper::i()->fetchByKey('threads.forker', $config);
             $config->setValue($forkerUrl);
             Application_Model_ConfigsMapper::i()->save($config);
         }
     } catch (Exception $e) {
         $this->_helper->flashMessenger(X_Env::_('installer_err_db') . ": {$e->getMessage()}");
         $this->_helper->redirector('index');
     }
     $plugins = $this->getRequest()->getParam('plugins', array());
     //ini_set('max_execution_time', 0);
     ignore_user_abort(true);
     if (is_array($plugins)) {
         /* @var $pluginInstaller X_VlcShares_Plugins_PluginInstaller */
         $pluginInstaller = X_VlcShares_Plugins::broker()->getPlugins('plugininstaller');
         foreach ($plugins as $plugin) {
             // allow 30 seconds for each plugin
             set_time_limit(30);
             try {
                 $pluginInstaller->installPlugin($plugin, true);
                 $this->_helper->flashMessenger(array('type' => 'success', 'text' => X_Env::_('plugin_install_done') . ": {$plugin}"));
             } catch (Exception $e) {
                 $this->_helper->flashMessenger(array('text' => X_Env::_('plugin_err_installerror') . ": " . $e->getMessage(), 'type' => 'error'));
             }
             // download the plugin file
             /*
             $http = new Zend_Http_Client($plugin);
             $http->setStream(true);
             
             $response = $http->request();
             
             if ( $this->_installPlugin($response->getStreamName()) ) {
             	$this->_helper->flashMessenger(array('type' => 'success', 'text' => X_Env::_('plugin_install_done') . ": $plugin"));
             }
             */
         }
     }
     $this->_helper->redirector('execute');
 }