Exemplo n.º 1
1
/**
 * Upload plugin archive into the gui/plugins directory
 *
 * Supported archives: zip tar.gz and tar.bz2
 *
 * @param PluginManager $pluginManager
 * @return bool TRUE on success, FALSE on failure
 */
function uploadPlugin($pluginManager)
{
    $pluginDirectory = $pluginManager->pluginGetDirectory();
    $tmpDirectory = GUI_ROOT_DIR . '/data/tmp';
    $ret = false;
    if (isset($_FILES['plugin_archive'])) {
        $beforeMove = function ($tmpDirectory) {
            $tmpFilePath = $_FILES['plugin_archive']['tmp_name'];
            if (!checkMimeType($tmpFilePath, array('application/x-gzip', 'application/x-bzip2', 'application/zip'))) {
                set_page_message(tr('Only tar.gz, tar.bz2 and zip archives are accepted.'), 'error');
                return false;
            }
            $pluginArchiveSize = $_FILES['plugin_archive']['size'];
            $maxUploadFileSize = utils_getMaxFileUpload();
            if ($pluginArchiveSize > $maxUploadFileSize) {
                set_page_message(tr('Plugin archive exceeds the maximum upload size (%s). Max upload size is: %s.', bytesHuman($pluginArchiveSize), bytesHuman($maxUploadFileSize)), 'error');
                return false;
            }
            return $tmpDirectory . '/' . $_FILES['plugin_archive']['name'];
        };
        # Upload plugin archive into gui/data/tmp directory ( eg. gui/data/tmp/PluginName.zip )
        $tmpArchPath = utils_uploadFile('plugin_archive', array($beforeMove, $tmpDirectory));
        if ($tmpArchPath !== false) {
            $zipArch = strtolower(pathinfo($tmpArchPath, PATHINFO_EXTENSION)) == 'zip';
            try {
                if (!$zipArch) {
                    $arch = new PharData($tmpArchPath);
                    $pluginName = $arch->getBasename();
                    if (!isset($arch["{$pluginName}/{$pluginName}.php"])) {
                        throw new iMSCPException(tr('File %s is missing in plugin archive.', "{$pluginName}.php"));
                    }
                    $arch->extractTo($tmpDirectory, "{$pluginName}/info.php", true);
                    $pluginManager->pluginCheckCompat($pluginName, include "{$tmpDirectory}/{$pluginName}/info.php");
                } else {
                    $arch = new ZipArchive();
                    if ($arch->open($tmpArchPath) === true) {
                        if (($pluginName = $arch->getNameIndex(0, ZIPARCHIVE::FL_UNCHANGED)) !== false) {
                            $pluginName = rtrim($pluginName, '/');
                            $index = $arch->locateName("{$pluginName}.php", ZipArchive::FL_NODIR);
                            if ($index !== false) {
                                if ($stats = $arch->statIndex($index)) {
                                    if ($stats['name'] != "{$pluginName}/{$pluginName}.php") {
                                        throw new iMSCPException(tr('File %s is missing in plugin archive.', "{$pluginName}.php"));
                                    }
                                } else {
                                    throw new iMSCPException(tr('Unable to get stats for file %s.', "{$pluginName}.php"));
                                }
                            } else {
                                throw new iMSCPException(tr('File %s is missing in plugin archive.', "{$pluginName}.php"));
                            }
                        } else {
                            throw new iMSCPException(tr('Unable to find plugin root directory withing archive.'));
                        }
                        if ($arch->extractTo($tmpDirectory, "{$pluginName}/info.php")) {
                            $pluginManager->pluginCheckCompat($pluginName, include "{$tmpDirectory}/{$pluginName}/info.php");
                        } else {
                            throw new iMSCPException(tr('Unable to extract info.php file'));
                        }
                    } else {
                        throw new iMSCPException(tr('Unable to open plugin archive.'));
                    }
                }
                if ($pluginManager->pluginIsKnown($pluginName) && $pluginManager->pluginIsProtected($pluginName)) {
                    throw new iMSCPException(tr('You are not allowed to update a protected plugin.'));
                }
                # Backup current plugin directory in temporary directory if exists
                if (is_dir("{$pluginDirectory}/{$pluginName}")) {
                    if (!@rename("{$pluginDirectory}/{$pluginName}", "{$tmpDirectory}/{$pluginName}" . '-old')) {
                        throw new iMSCPException(tr('Unable to backup %s plugin directory.', $pluginName));
                    }
                }
                if (!$zipArch) {
                    $arch->extractTo($pluginDirectory, null, true);
                } elseif (!$arch->extractTo($pluginDirectory)) {
                    throw new iMSCPException(tr('Unable to extract plugin archive.'));
                }
                $ret = true;
            } catch (Exception $e) {
                if ($e instanceof iMSCPException) {
                    set_page_message($e->getMessage(), 'error');
                } else {
                    set_page_message(tr('Unable to extract plugin archive: %s', $e->getMessage()), 'error');
                }
                if (!empty($pluginName) && is_dir("{$tmpDirectory}/{$pluginName}" . '-old')) {
                    // Try to restore previous plugin directory on error
                    if (!@rename("{$tmpDirectory}/{$pluginName}" . '-old', "{$pluginDirectory}/{$pluginName}")) {
                        set_page_message(tr('Unable to restore %s plugin directory', $pluginName), 'error');
                    }
                }
            }
            // Cleanup
            @unlink($tmpArchPath);
            if (!empty($pluginName)) {
                utils_removeDir("{$tmpDirectory}/{$pluginName}");
                utils_removeDir("{$tmpDirectory}/{$pluginName}" . '-old');
            }
        } else {
            redirectTo('settings_plugins.php');
        }
    } else {
        showBadRequestErrorPage();
    }
    return $ret;
}
Exemplo n.º 2
0
                if (!empty($members)) {
                    exec_query("UPDATE `ftp_group` SET `members` = ? WHERE `gid` = ?", array(implode(',', $members), $ftpUserGid));
                } else {
                    exec_query('DELETE FROM `ftp_group` WHERE `groupname` = ?', $groupName);
                    exec_query('DELETE FROM `quotalimits` WHERE `name` = ?', $groupName);
                    exec_query('DELETE FROM `quotatallies` WHERE `name` = ?', $groupName);
                }
            }
        }
        exec_query('DELETE FROM `ftp_users` WHERE `userid` = ?', $ftpUserId);
        /** @var $cfg iMSCP_Config_Handler_File */
        $cfg = iMSCP_Registry::get('config');
        if (isset($cfg->FILEMANAGER_PACKAGE) && $cfg->FILEMANAGER_PACKAGE == 'Pydio') {
            // Quick fix to delete Ftp preferences directory as created by Pydio
            // FIXME: Move this statement at engine level
            $userPrefDir = $cfg->GUI_PUBLIC_DIR . '/tools/ftp/data/plugins/auth.serial/' . $ftpUserId;
            if (is_dir($userPrefDir)) {
                utils_removeDir($userPrefDir);
            }
        }
        $db->commit();
        iMSCP_Events_Aggregator::getInstance()->dispatch(iMSCP_Events::onAfterDeleteFtp, array('ftpUserId' => $ftpUserId));
        write_log(sprintf("%s: deleted FTP account: %s", $_SESSION['user_logged'], $ftpUserId), E_USER_NOTICE);
        set_page_message(tr('FTP account successfully deleted.'), 'success');
    } catch (iMSCP_Exception_Database $e) {
        $db->rollBack();
        throw $e;
    }
    redirectTo('ftp_accounts.php');
}
showBadRequestErrorPage();
Exemplo n.º 3
0
/**
 * Remove the given directory recusively
 *
 * @param string $directory Path of directory to remove
 * @return boolean TRUE on success, FALSE otherwise
 */
function utils_removeDir($directory)
{
    $directory = rtrim($directory, '/');
    if (!is_dir($directory)) {
        return false;
    } elseif (is_readable($directory)) {
        $handle = opendir($directory);
        while (false !== ($item = readdir($handle))) {
            if ($item != '.' && $item != '..') {
                $path = $directory . '/' . $item;
                if (is_dir($path)) {
                    utils_removeDir($path);
                } else {
                    @unlink($path);
                }
            }
        }
        closedir($handle);
        if (!@rmdir($directory)) {
            return false;
        }
    }
    return true;
}
Exemplo n.º 4
0
/**
 * Build languages index from machine object files.
 *
 * @throws iMSCP_Exception
 * @return void
 */
function i18n_buildLanguageIndex()
{
    /** @var $cfg iMSCP_Config_Handler_File */
    $cfg = iMSCP_Registry::get('config');
    // Clear translation cache
    /** @var Zend_Translate $translator */
    $translator = iMSCP_Registry::get('translator');
    if ($translator->hasCache()) {
        $translator->clearCache('iMSCP');
    }
    # Remove all cached navigation translation files
    if (@is_dir(CACHE_PATH . '/translations/navigation')) {
        if (!utils_removeDir(CACHE_PATH . '/translations/navigation')) {
            throw new iMSCP_Exception('Unable to remove directory for cached navigation translation files');
        }
    }
    # Clear opcode cache if any
    iMSCP_Utility_OpcodeCache::clearAllActive();
    $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($cfg->GUI_ROOT_DIR . '/i18n/locales/', FilesystemIterator::SKIP_DOTS));
    $availableLanguages = array();
    /** @var $item SplFileInfo */
    foreach ($iterator as $item) {
        if (strlen($basename = $item->getBasename()) > 8) {
            continue;
        }
        if ($item->isReadable()) {
            $parser = new iMSCP_I18n_Parser_Gettext($item->getPathname());
            $translationTable = $parser->getTranslationTable();
            if (!empty($translationTable)) {
                $poRevisionDate = DateTime::createFromFormat('Y-m-d H:i O', $parser->getPotCreationDate());
                $availableLanguages[$basename] = array('locale' => $parser->getLanguage(), 'revision' => $poRevisionDate->format('Y-m-d H:i'), 'translatedStrings' => $parser->getNumberOfTranslatedStrings(), 'lastTranslator' => $parser->getLastTranslator());
                // Getting localized language name
                if (!isset($translationTable['_: Localised language'])) {
                    $availableLanguages[$basename]['language'] = tr('Unknown');
                } else {
                    $availableLanguages[$basename]['language'] = $translationTable['_: Localised language'];
                }
            } else {
                set_page_message(tr('The %s translation file has been ignored: Translation table is empty.', $basename), 'warning');
            }
        }
    }
    /** @var $dbConfig iMSCP_Config_Handler_Db */
    $dbConfig = iMSCP_Registry::get('dbConfig');
    sort($availableLanguages);
    $serializedData = serialize($availableLanguages);
    $dbConfig['AVAILABLE_LANGUAGES'] = $serializedData;
    $cfg['AVAILABLE_LANGUAGES'] = $serializedData;
}
Exemplo n.º 5
0
 /**
  * Delete the given plugin
  *
  * @throws iMSCP_Plugin_Exception
  * @param string $name Plugin name
  * @return self::ACTION_SUCCESS|self::ACTION_STOPPED|self::ACTION_FAILURE
  */
 public function pluginDelete($name)
 {
     if ($this->pluginIsKnown($name)) {
         $pluginStatus = $this->pluginGetStatus($name);
         if (in_array($pluginStatus, array('todelete', 'uninstalled', 'disabled'))) {
             try {
                 $pluginInstance = $this->pluginLoad($name);
                 $this->pluginSetStatus($name, 'todelete');
                 $this->pluginSetError($name, null);
                 $responses = $this->eventsManager->dispatch(iMSCP_Events::onBeforeDeletePlugin, array('pluginManager' => $this, 'pluginName' => $name));
                 if (!$responses->isStopped()) {
                     $pluginInstance->delete($this);
                     $this->pluginDeleteData($name);
                     $pluginDir = $this->pluginsDirectory . '/' . $name;
                     if (is_dir($pluginDir)) {
                         if (!utils_removeDir($pluginDir)) {
                             set_page_message(tr('Plugin Manager: Unable to delete %s plugin files. You should run the set-gui-permissions.pl script and try again.', $name), 'warning');
                             write_log(sprintf('Plugin Manager: Unable to delete %s plugin files', $name), E_USER_WARNING);
                         }
                     }
                     $this->eventsManager->dispatch(iMSCP_Events::onAfterDeletePlugin, array('pluginManager' => $this, 'pluginName' => $name));
                     return self::ACTION_SUCCESS;
                 }
                 $this->pluginSetStatus($name, $pluginStatus);
                 return self::ACTION_STOPPED;
             } catch (iMSCP_Plugin_Exception $e) {
                 $this->pluginSetError($name, sprintf('Plugin deletion has failed: %s', $e->getMessage()));
                 write_log(sprintf('Plugin Manager: %s plugin deletion has failed', $name), E_USER_ERROR);
             }
         }
     }
     return self::ACTION_FAILURE;
 }