mambax7 /
moduleinstaller
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
| 1 | <?php declare(strict_types=1); |
||
| 2 | |||
| 3 | /** |
||
| 4 | * You may not change or alter any portion of this comment or credits |
||
| 5 | * of supporting developers from this source code or any supporting source code |
||
| 6 | * which is considered copyrighted (c) material of the original comment or credit authors. |
||
| 7 | * This program is distributed in the hope that it will be useful, |
||
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
||
| 10 | * |
||
| 11 | * @copyright XOOPS Project (https://xoops.org) |
||
| 12 | * @license GNU GPL 2 (https://www.gnu.org/licenses/old-licenses/gpl-2.0.html) |
||
| 13 | * @since 2.5.9 |
||
| 14 | * @author Michael Beck (aka Mamba): https://github.com/mambax7 |
||
| 15 | */ |
||
| 16 | |||
| 17 | use Xmf\Database\TableLoad; |
||
| 18 | use Xmf\Request; |
||
| 19 | use Xmf\Yaml; |
||
| 20 | use XoopsModules\Moduleinstaller\{ |
||
| 21 | Common\Configurator, |
||
| 22 | Helper, |
||
| 23 | Utility |
||
| 24 | }; |
||
| 25 | /** @var Helper $helper */ |
||
| 26 | /** @var Utility $utility */ |
||
| 27 | /** @var Configurator $configurator */ |
||
| 28 | require \dirname(__DIR__, 3) . '/include/cp_header.php'; |
||
| 29 | require \dirname(__DIR__) . '/preloads/autoloader.php'; |
||
| 30 | |||
| 31 | $op = Request::getCmd('op', ''); |
||
| 32 | |||
| 33 | $moduleDirName = \basename(\dirname(__DIR__)); |
||
| 34 | $moduleDirNameUpper = \mb_strtoupper($moduleDirName); |
||
| 35 | |||
| 36 | $helper = Helper::getInstance(); |
||
| 37 | // Load language files |
||
| 38 | $helper->loadLanguage('common'); |
||
| 39 | |||
| 40 | switch ($op) { |
||
| 41 | case 'load': |
||
| 42 | if (Request::hasVar('ok', 'REQUEST') && 1 === Request::getInt('ok', 0)) { |
||
| 43 | if (!$GLOBALS['xoopsSecurity']->check()) { |
||
| 44 | redirect_header($helper->url('admin/index.php'), 3, implode(',', $GLOBALS['xoopsSecurity']->getErrors())); |
||
| 45 | } |
||
| 46 | loadSampleData(); |
||
| 47 | } else { |
||
| 48 | xoops_cp_header(); |
||
| 49 | xoops_confirm(['ok' => 1, 'op' => 'load'], 'index.php', constant('CO_' . $moduleDirNameUpper . '_' . 'LOAD_SAMPLEDATA_CONFIRM'), constant('CO_' . $moduleDirNameUpper . '_' . 'CONFIRM'), true); |
||
| 50 | xoops_cp_footer(); |
||
| 51 | } |
||
| 52 | break; |
||
| 53 | case 'save': |
||
| 54 | saveSampleData(); |
||
| 55 | break; |
||
| 56 | case 'clear': |
||
| 57 | if (Request::hasVar('ok', 'REQUEST') && 1 === Request::getInt('ok', 0)) { |
||
| 58 | if (!$GLOBALS['xoopsSecurity']->check()) { |
||
| 59 | redirect_header($helper->url('admin/index.php'), 3, implode(',', $GLOBALS['xoopsSecurity']->getErrors())); |
||
| 60 | } |
||
| 61 | clearSampleData(); |
||
| 62 | } else { |
||
| 63 | xoops_cp_header(); |
||
| 64 | xoops_confirm(['ok' => 1, 'op' => 'clear'], 'index.php', sprintf(constant('CO_' . $moduleDirNameUpper . '_' . 'CLEAR_SAMPLEDATA')), constant('CO_' . $moduleDirNameUpper . '_' . 'CONFIRM'), true); |
||
| 65 | xoops_cp_footer(); |
||
| 66 | } |
||
| 67 | break; |
||
| 68 | } |
||
| 69 | |||
| 70 | // XMF TableLoad for SAMPLE data |
||
| 71 | |||
| 72 | function loadSampleData(): void |
||
| 73 | { |
||
| 74 | global $xoopsConfig; |
||
| 75 | $moduleDirName = \basename(\dirname(__DIR__)); |
||
| 76 | $moduleDirNameUpper = \mb_strtoupper($moduleDirName); |
||
| 77 | |||
| 78 | $utility = new Utility(); |
||
| 79 | $configurator = new Configurator(); |
||
| 80 | |||
| 81 | $tables = \Xmf\Module\Helper::getHelper($moduleDirName)->getModule()->getInfo('tables'); |
||
| 82 | |||
| 83 | $language = 'english/'; |
||
| 84 | if (is_dir(__DIR__ . '/' . $xoopsConfig['language'])) { |
||
| 85 | $language = $xoopsConfig['language'] . '/'; |
||
| 86 | } |
||
| 87 | |||
| 88 | // load module tables |
||
| 89 | foreach ($tables as $table) { |
||
| 90 | $tabledata = Yaml::readWrapped($language . $table . '.yml'); |
||
| 91 | TableLoad::truncateTable($table); |
||
| 92 | TableLoad::loadTableFromArray($table, $tabledata); |
||
| 93 | } |
||
| 94 | |||
| 95 | // load permissions |
||
| 96 | $table = 'group_permission'; |
||
| 97 | $tabledata = Yaml::readWrapped($language . $table . '.yml'); |
||
| 98 | $mid = \Xmf\Module\Helper::getHelper($moduleDirName)->getModule()->getVar('mid'); |
||
| 99 | loadTableFromArrayWithReplace($table, $tabledata, 'gperm_modid', $mid); |
||
| 100 | |||
| 101 | // --- COPY test folder files --------------- |
||
| 102 | if (is_array($configurator->copyTestFolders) && count($configurator->copyTestFolders) > 0) { |
||
| 103 | // $file = \dirname(__DIR__) . '/testdata/images/'; |
||
| 104 | foreach (array_keys($configurator->copyTestFolders) as $i) { |
||
| 105 | $src = $configurator->copyTestFolders[$i][0]; |
||
| 106 | $dest = $configurator->copyTestFolders[$i][1]; |
||
| 107 | $utility::rcopy($src, $dest); |
||
| 108 | } |
||
| 109 | } |
||
| 110 | \redirect_header('../admin/index.php', 1, \constant('CO_' . $moduleDirNameUpper . '_' . 'LOAD_SAMPLEDATA_SUCCESS')); |
||
| 111 | } |
||
| 112 | |||
| 113 | function saveSampleData(): void |
||
| 114 | { |
||
| 115 | $skipColumns = []; |
||
| 116 | global $xoopsConfig; |
||
| 117 | $moduleDirName = \basename(\dirname(__DIR__)); |
||
| 118 | $moduleDirNameUpper = \mb_strtoupper($moduleDirName); |
||
| 119 | $helper = Helper::getInstance(); |
||
| 120 | $tables = $helper->getModule()->getInfo('tables'); |
||
| 121 | |||
| 122 | $languageFolder = __DIR__ . '/' . $xoopsConfig['language']; |
||
| 123 | if (!file_exists($languageFolder . '/')) { |
||
| 124 | Utility::createFolder($languageFolder . '/'); |
||
| 125 | } |
||
| 126 | $exportFolder = $languageFolder . '/Exports-' . date('Y-m-d-H-i-s') . '/'; |
||
| 127 | Utility::createFolder($exportFolder); |
||
| 128 | |||
| 129 | // save module tables |
||
| 130 | foreach ($tables as $table) { |
||
| 131 | TableLoad::saveTableToYamlFile($table, $exportFolder . $table . '.yml'); |
||
| 132 | } |
||
| 133 | |||
| 134 | // save permissions |
||
| 135 | $criteria = new \CriteriaCompo(); |
||
| 136 | $criteria->add(new \Criteria('gperm_modid', (string)$helper->getModule()->getVar('mid'))); |
||
| 137 | $skipColumns[] = 'gperm_id'; |
||
| 138 | TableLoad::saveTableToYamlFile('group_permission', $exportFolder . 'group_permission.yml', $criteria, $skipColumns); |
||
| 139 | unset($criteria); |
||
| 140 | |||
| 141 | \redirect_header('../admin/index.php', 1, \constant('CO_' . $moduleDirNameUpper . '_' . 'SAVE_SAMPLEDATA_SUCCESS')); |
||
| 142 | } |
||
| 143 | |||
| 144 | function exportSchema(): void |
||
| 145 | { |
||
| 146 | $moduleDirName = \basename(\dirname(__DIR__)); |
||
| 147 | $moduleDirNameUpper = \mb_strtoupper($moduleDirName); |
||
| 148 | |||
| 149 | try { |
||
| 150 | // TODO set exportSchema |
||
| 151 | // $migrate = new Migrate($moduleDirName); |
||
| 152 | // $migrate->saveCurrentSchema(); |
||
| 153 | // |
||
| 154 | // redirect_header('../admin/index.php', 1, constant('CO_' . $moduleDirNameUpper . '_' . 'EXPORT_SCHEMA_SUCCESS')); |
||
| 155 | } catch (\Throwable $exception) { |
||
| 156 | exit(constant('CO_' . $moduleDirNameUpper . '_' . 'EXPORT_SCHEMA_ERROR')); |
||
|
0 ignored issues
–
show
|
|||
| 157 | } |
||
| 158 | } |
||
| 159 | |||
| 160 | /** |
||
| 161 | * loadTableFromArrayWithReplace |
||
| 162 | * |
||
| 163 | * @param string $table value which should be used instead of original value of $search |
||
| 164 | * |
||
| 165 | * @param array $data array of rows to insert |
||
| 166 | * Each element of the outer array represents a single table row. |
||
| 167 | * Each row is an associative array in 'column' => 'value' format. |
||
| 168 | * @param string $search name of column for which the value should be replaced |
||
| 169 | * @param $replace |
||
| 170 | * @return int number of rows inserted |
||
| 171 | */ |
||
| 172 | function loadTableFromArrayWithReplace($table, $data, $search, $replace) |
||
| 173 | { |
||
| 174 | /** @var \XoopsMySQLDatabase $db */ |
||
| 175 | $db = \XoopsDatabaseFactory::getDatabaseConnection(); |
||
| 176 | |||
| 177 | $prefixedTable = $db->prefix($table); |
||
| 178 | $count = 0; |
||
| 179 | |||
| 180 | $sql = 'DELETE FROM ' . $prefixedTable . ' WHERE `' . $search . '`=' . $db->quote($replace); |
||
| 181 | |||
| 182 | $result = $db->queryF($sql); |
||
| 183 | |||
| 184 | foreach ($data as $row) { |
||
| 185 | $insertInto = 'INSERT INTO ' . $prefixedTable . ' ('; |
||
| 186 | $valueClause = ' VALUES ('; |
||
| 187 | $first = true; |
||
| 188 | foreach ($row as $column => $value) { |
||
| 189 | if ($first) { |
||
| 190 | $first = false; |
||
| 191 | } else { |
||
| 192 | $insertInto .= ', '; |
||
| 193 | $valueClause .= ', '; |
||
| 194 | } |
||
| 195 | |||
| 196 | $insertInto .= $column; |
||
| 197 | if ($search === $column) { |
||
| 198 | $valueClause .= $db->quote($replace); |
||
| 199 | } else { |
||
| 200 | $valueClause .= $db->quote($value); |
||
| 201 | } |
||
| 202 | } |
||
| 203 | |||
| 204 | $sql = $insertInto . ') ' . $valueClause . ')'; |
||
| 205 | |||
| 206 | $result = $db->queryF($sql); |
||
| 207 | if (false !== $result) { |
||
| 208 | ++$count; |
||
| 209 | } |
||
| 210 | } |
||
| 211 | |||
| 212 | return $count; |
||
| 213 | } |
||
| 214 | |||
| 215 | function clearSampleData(): void |
||
| 216 | { |
||
| 217 | $moduleDirName = \basename(\dirname(__DIR__)); |
||
| 218 | $moduleDirNameUpper = \mb_strtoupper($moduleDirName); |
||
| 219 | $helper = Helper::getInstance(); |
||
| 220 | // Load language files |
||
| 221 | $helper->loadLanguage('common'); |
||
| 222 | $tables = $helper->getModule()->getInfo('tables'); |
||
| 223 | // truncate module tables |
||
| 224 | foreach ($tables as $table) { |
||
| 225 | \Xmf\Database\TableLoad::truncateTable($table); |
||
| 226 | } |
||
| 227 | redirect_header($helper->url('admin/index.php'), 1, constant('CO_' . $moduleDirNameUpper . '_' . 'CLEAR_SAMPLEDATA_OK')); |
||
| 228 | } |
||
| 229 |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.