mambax7 /
tdmcreate-1.91
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 namespace XoopsModules\Tdmcreate\Files\Sql; |
||
| 2 | |||
| 3 | use XoopsModules\Tdmcreate; |
||
| 4 | use XoopsModules\Tdmcreate\Files; |
||
| 5 | |||
| 6 | /* |
||
| 7 | You may not change or alter any portion of this comment or credits |
||
| 8 | of supporting developers from this source code or any supporting source code |
||
| 9 | which is considered copyrighted (c) material of the original comment or credit authors. |
||
| 10 | |||
| 11 | This program is distributed in the hope that it will be useful, |
||
| 12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
||
| 14 | */ |
||
| 15 | /** |
||
| 16 | * tdmcreate module. |
||
| 17 | * |
||
| 18 | * @copyright XOOPS Project (https://xoops.org) |
||
| 19 | * @license GNU GPL 2 (http://www.gnu.org/licenses/old-licenses/gpl-2.0.html) |
||
| 20 | * |
||
| 21 | * @since 2.5.0 |
||
| 22 | * |
||
| 23 | * @author Txmod Xoops http://www.txmodxoops.org |
||
| 24 | * |
||
| 25 | * @version $Id: SqlFile.php 12258 2014-01-02 09:33:29Z timgno $ |
||
| 26 | */ |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Class SqlFile. |
||
| 30 | */ |
||
| 31 | class SqlFile extends Files\CreateFile |
||
| 32 | { |
||
| 33 | /** |
||
| 34 | * @public function constructor |
||
| 35 | * |
||
| 36 | * @param null |
||
| 37 | */ |
||
| 38 | public function __construct() |
||
| 39 | { |
||
| 40 | parent::__construct(); |
||
| 41 | } |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @static function getInstance |
||
| 45 | * |
||
| 46 | * @param null |
||
| 47 | * |
||
| 48 | * @return SqlFile |
||
| 49 | */ |
||
| 50 | public static function getInstance() |
||
| 51 | { |
||
| 52 | static $instance = false; |
||
| 53 | if (!$instance) { |
||
| 54 | $instance = new self(); |
||
| 55 | } |
||
| 56 | |||
| 57 | return $instance; |
||
| 58 | } |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @public function write |
||
| 62 | * |
||
| 63 | * @param $module |
||
| 64 | * @param $filename |
||
| 65 | */ |
||
| 66 | public function write($module, $filename) |
||
| 67 | { |
||
| 68 | $this->setModule($module); |
||
| 69 | $this->setFileName($filename); |
||
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @private function getHeaderSqlComments |
||
| 74 | * |
||
| 75 | * @param $moduleName |
||
| 76 | * |
||
| 77 | * @return string |
||
| 78 | */ |
||
| 79 | private function getHeaderSqlComments($moduleName) |
||
| 80 | { |
||
| 81 | $date = date('D M d, Y'); |
||
| 82 | $time = date('H:i:s'); |
||
| 83 | $serverName = $_SERVER['SERVER_NAME']; |
||
| 84 | $serverVersion = $GLOBALS['xoopsDB']->getServerVersion(); |
||
| 85 | $phpVersion = PHP_VERSION; |
||
| 86 | // Header Sql Comments |
||
| 87 | $ret = null; |
||
| 88 | $arrayServerInfo = [ |
||
| 89 | "# SQL Dump for {$moduleName} module", '# PhpMyAdmin Version: 4.0.4', |
||
| 90 | '# http://www.phpmyadmin.net', '#', "# Host: {$serverName}", |
||
| 91 | "# Generated on: {$date} to {$time}", "# Server version: {$serverVersion}", |
||
| 92 | "# PHP Version: {$phpVersion}\n", |
||
| 93 | ]; |
||
| 94 | foreach ($arrayServerInfo as $serverInfo) { |
||
| 95 | $ret .= $this->getSimpleString($serverInfo); |
||
| 96 | } |
||
| 97 | |||
| 98 | return $ret; |
||
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @private function getHeadDatabaseTable |
||
| 103 | * @param $moduleDirname |
||
| 104 | * @param $tableName |
||
| 105 | * @param int $fieldsNumb |
||
| 106 | * |
||
| 107 | * Unused IF NOT EXISTS |
||
| 108 | * |
||
| 109 | * @return string |
||
| 110 | */ |
||
| 111 | private function getHeadDatabaseTable($moduleDirname, $tableName, $fieldsNumb) |
||
| 112 | { |
||
| 113 | $ret = null; |
||
| 114 | $arrayDbTable = [ |
||
| 115 | '#', "# Structure table for `{$moduleDirname}_{$tableName}` {$fieldsNumb}", |
||
| 116 | '#', "\nCREATE TABLE `{$moduleDirname}_{$tableName}` (", |
||
| 117 | ]; |
||
| 118 | foreach ($arrayDbTable as $dbTable) { |
||
| 119 | $ret .= $this->getSimpleString($dbTable); |
||
| 120 | } |
||
| 121 | |||
| 122 | return $ret; |
||
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * @private function getDatabaseTables |
||
| 127 | * |
||
| 128 | * @param $module |
||
| 129 | * |
||
| 130 | * @return null|string |
||
| 131 | */ |
||
| 132 | private function getDatabaseTables($module) |
||
| 133 | { |
||
| 134 | $ret = null; |
||
| 135 | $moduleDirname = mb_strtolower($module->getVar('mod_dirname')); |
||
| 136 | $tables = $this->getTableTables($module->getVar('mod_id'), 'table_order ASC, table_id'); |
||
| 137 | foreach (array_keys($tables) as $t) { |
||
| 138 | $tableId = $tables[$t]->getVar('table_id'); |
||
| 139 | $tableMid = $tables[$t]->getVar('table_mid'); |
||
| 140 | $tableName = $tables[$t]->getVar('table_name'); |
||
| 141 | $tableAutoincrement = $tables[$t]->getVar('table_autoincrement'); |
||
| 142 | $fieldsNumb = $tables[$t]->getVar('table_nbfields'); |
||
| 143 | $ret .= $this->getDatabaseFields($moduleDirname, $tableMid, $tableId, $tableName, $tableAutoincrement, $fieldsNumb); |
||
| 144 | } |
||
| 145 | |||
| 146 | return $ret; |
||
| 147 | } |
||
| 148 | |||
| 149 | /** |
||
| 150 | * @private function getDatabaseFields |
||
| 151 | * |
||
| 152 | * @param $moduleDirname |
||
| 153 | * @param $tableMid |
||
| 154 | * @param $tableId |
||
| 155 | * @param $tableName |
||
| 156 | * @param $tableAutoincrement |
||
| 157 | * @param $fieldsNumb |
||
| 158 | * @return null|string |
||
| 159 | */ |
||
| 160 | private function getDatabaseFields($moduleDirname, $tableMid, $tableId, $tableName, $tableAutoincrement, $fieldsNumb) |
||
| 161 | { |
||
| 162 | $helper = Tdmcreate\Helper::getInstance(); |
||
| 163 | $ret = null; |
||
| 164 | $j = 0; |
||
| 165 | $comma = []; |
||
| 166 | $row = []; |
||
| 167 | $type = ''; |
||
| 168 | $fields = $this->getTableFields($tableMid, $tableId, 'field_id ASC, field_name'); |
||
| 169 | foreach (array_keys($fields) as $f) { |
||
| 170 | // Creation of database table |
||
| 171 | $ret = $this->getHeadDatabaseTable($moduleDirname, $tableName, $fieldsNumb); |
||
| 172 | $fieldName = $fields[$f]->getVar('field_name'); |
||
| 173 | $fieldType = $fields[$f]->getVar('field_type'); |
||
| 174 | $fieldValue = $fields[$f]->getVar('field_value'); |
||
| 175 | $fieldAttribute = $fields[$f]->getVar('field_attribute'); |
||
| 176 | $fieldNull = $fields[$f]->getVar('field_null'); |
||
| 177 | $fieldDefault = $fields[$f]->getVar('field_default'); |
||
| 178 | $fieldKey = $fields[$f]->getVar('field_key'); |
||
| 179 | if ($fieldType > 1) { |
||
| 180 | $fType = $helper->getHandler('Fieldtype')->get($fieldType); |
||
| 181 | $fieldTypeName = $fType->getVar('fieldtype_name'); |
||
| 182 | } else { |
||
| 183 | $fieldType = null; |
||
| 184 | } |
||
| 185 | if ($fieldAttribute > 1) { |
||
| 186 | $fAttribute = $helper->getHandler('Fieldattributes')->get($fieldAttribute); |
||
| 187 | $fieldAttribute = $fAttribute->getVar('fieldattribute_name'); |
||
| 188 | } else { |
||
| 189 | $fieldAttribute = null; |
||
| 190 | } |
||
| 191 | if ($fieldNull > 1) { |
||
| 192 | $fNull = $helper->getHandler('Fieldnull')->get($fieldNull); |
||
| 193 | $fieldNull = $fNull->getVar('fieldnull_name'); |
||
| 194 | } else { |
||
| 195 | $fieldNull = null; |
||
| 196 | } |
||
| 197 | if (!empty($fieldName)) { |
||
| 198 | switch ($fieldType) { |
||
| 199 | case 2: |
||
| 200 | case 3: |
||
| 201 | case 4: |
||
| 202 | case 5: |
||
| 203 | $type = $fieldTypeName . '(' . $fieldValue . ')'; |
||
|
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Loading history...
|
|||
| 204 | if (empty($fieldDefault)) { |
||
| 205 | $default = "DEFAULT '0'"; |
||
| 206 | } else { |
||
| 207 | $default = "DEFAULT '{$fieldDefault}'"; |
||
| 208 | } |
||
| 209 | break; |
||
| 210 | case 6: |
||
| 211 | case 7: |
||
| 212 | case 8: |
||
| 213 | $type = $fieldTypeName . '(' . $fieldValue . ')'; |
||
| 214 | if (empty($fieldDefault)) { |
||
| 215 | $default = "DEFAULT '0.00'"; // From MySQL 5.7 Manual |
||
| 216 | } else { |
||
| 217 | $default = "DEFAULT '{$fieldDefault}'"; |
||
| 218 | } |
||
| 219 | break; |
||
| 220 | case 9: |
||
| 221 | case 10: |
||
| 222 | $fValues = str_replace(',', "', '", str_replace(' ', '', $fieldValue)); |
||
| 223 | $type = $fieldTypeName . '(\'' . $fValues . '\')'; // Used with comma separator |
||
| 224 | $default = "DEFAULT '{$fieldDefault}'"; |
||
| 225 | break; |
||
| 226 | case 11: |
||
| 227 | $type = $fieldTypeName . '(' . $fieldValue . ')'; |
||
| 228 | if (empty($fieldDefault)) { |
||
| 229 | $default = "DEFAULT '[email protected]'"; |
||
| 230 | } else { |
||
| 231 | $default = "DEFAULT '{$fieldDefault}'"; |
||
| 232 | } |
||
| 233 | break; |
||
| 234 | case 12: |
||
| 235 | $type = $fieldTypeName . '(' . $fieldValue . ')'; |
||
| 236 | if (empty($fieldDefault)) { |
||
| 237 | $default = "DEFAULT 'http:\\'"; |
||
| 238 | } else { |
||
| 239 | $default = "DEFAULT '{$fieldDefault}'"; |
||
| 240 | } |
||
| 241 | break; |
||
| 242 | case 13: |
||
| 243 | case 14: |
||
| 244 | $type = $fieldTypeName . '(' . $fieldValue . ')'; |
||
| 245 | $default = "DEFAULT '{$fieldDefault}'"; |
||
| 246 | break; |
||
| 247 | case 15: |
||
| 248 | case 16: |
||
| 249 | case 17: |
||
| 250 | case 18: |
||
| 251 | $type = $fieldTypeName; |
||
| 252 | $default = null; |
||
| 253 | break; |
||
| 254 | case 19: |
||
| 255 | case 20: |
||
| 256 | case 21: |
||
| 257 | case 22: |
||
| 258 | $type = $fieldTypeName . '(' . $fieldValue . ')'; |
||
| 259 | $default = "DEFAULT '{$fieldDefault}'"; |
||
| 260 | break; |
||
| 261 | case 23: |
||
| 262 | $type = $fieldTypeName; |
||
| 263 | if (empty($fieldDefault)) { |
||
| 264 | $default = "DEFAULT '1970'"; // From MySQL 5.7 Manual |
||
| 265 | } else { |
||
| 266 | $default = "DEFAULT '{$fieldDefault}'"; |
||
| 267 | } |
||
| 268 | break; |
||
| 269 | default: |
||
| 270 | $type = $fieldTypeName . '(' . $fieldValue . ')'; |
||
| 271 | $default = "DEFAULT '{$fieldDefault}'"; |
||
| 272 | break; |
||
| 273 | } |
||
| 274 | if ((0 == $f) && (1 == $tableAutoincrement)) { |
||
| 275 | $row[] = $this->getFieldRow($fieldName, $type, $fieldAttribute, $fieldNull, null, 'AUTO_INCREMENT'); |
||
| 276 | $comma[$j] = $this->getKey(2, $fieldName); |
||
| 277 | ++$j; |
||
| 278 | } elseif ((0 == $f) && (0 == $tableAutoincrement)) { |
||
| 279 | $row[] = $this->getFieldRow($fieldName, $type, $fieldAttribute, $fieldNull, $default); |
||
| 280 | $comma[$j] = $this->getKey(2, $fieldName); |
||
| 281 | ++$j; |
||
| 282 | } else { |
||
| 283 | if (3 == $fieldKey || 4 == $fieldKey || 5 == $fieldKey || 6 == $fieldKey) { |
||
| 284 | switch ($fieldKey) { |
||
| 285 | case 3: |
||
| 286 | $row[] = $this->getFieldRow($fieldName, $type, $fieldAttribute, $fieldNull, $default); |
||
| 287 | $comma[$j] = $this->getKey(3, $fieldName); |
||
| 288 | ++$j; |
||
| 289 | break; |
||
| 290 | case 4: |
||
| 291 | $row[] = $this->getFieldRow($fieldName, $type, $fieldAttribute, $fieldNull, $default); |
||
| 292 | $comma[$j] = $this->getKey(4, $fieldName); |
||
| 293 | ++$j; |
||
| 294 | break; |
||
| 295 | case 5: |
||
| 296 | $row[] = $this->getFieldRow($fieldName, $type, $fieldAttribute, $fieldNull, $default); |
||
| 297 | $comma[$j] = $this->getKey(5, $fieldName); |
||
| 298 | ++$j; |
||
| 299 | break; |
||
| 300 | case 6: |
||
| 301 | $row[] = $this->getFieldRow($fieldName, $type, $fieldAttribute, $fieldNull, $default); |
||
| 302 | $comma[$j] = $this->getKey(6, $fieldName); |
||
| 303 | ++$j; |
||
| 304 | break; |
||
| 305 | } |
||
| 306 | } else { |
||
| 307 | $row[] = $this->getFieldRow($fieldName, $type, $fieldAttribute, $fieldNull, $default); |
||
| 308 | } |
||
| 309 | } |
||
| 310 | } |
||
| 311 | } |
||
| 312 | // ================= COMMA ================= // |
||
| 313 | for ($i = 0; $i < $j; ++$i) { |
||
| 314 | if ($i != $j - 1) { |
||
| 315 | $row[] = $comma[$i] . ','; |
||
| 316 | } else { |
||
| 317 | $row[] = $comma[$i]; |
||
| 318 | } |
||
| 319 | } |
||
| 320 | // ================= COMMA CICLE ================= // |
||
| 321 | $ret .= implode("\n", $row); |
||
| 322 | unset($j); |
||
| 323 | $ret .= $this->getFootDatabaseTable(); |
||
| 324 | |||
| 325 | return $ret; |
||
| 326 | } |
||
| 327 | |||
| 328 | /** |
||
| 329 | * @private function getFootDatabaseTable |
||
| 330 | * |
||
| 331 | * @param null |
||
| 332 | * |
||
| 333 | * @return string |
||
| 334 | */ |
||
| 335 | private function getFootDatabaseTable() |
||
| 336 | { |
||
| 337 | return "\n) ENGINE=InnoDB;\n\n"; |
||
| 338 | } |
||
| 339 | |||
| 340 | /** |
||
| 341 | * @private function getFieldRow |
||
| 342 | * |
||
| 343 | * @param $fieldName |
||
| 344 | * @param $fieldTypeValue |
||
| 345 | * @param $fieldAttribute |
||
| 346 | * @param $fieldNull |
||
| 347 | * @param $fieldDefault |
||
| 348 | * @param $autoincrement |
||
| 349 | * |
||
| 350 | * @return string |
||
| 351 | */ |
||
| 352 | private function getFieldRow($fieldName, $fieldTypeValue, $fieldAttribute = null, $fieldNull = null, $fieldDefault = null, $autoincrement = null) |
||
| 353 | { |
||
| 354 | $retAutoincrement = " `{$fieldName}` {$fieldTypeValue} {$fieldAttribute} {$fieldNull} {$autoincrement},"; |
||
| 355 | $retFieldAttribute = " `{$fieldName}` {$fieldTypeValue} {$fieldAttribute} {$fieldNull} {$fieldDefault},"; |
||
| 356 | $fieldDefault = " `{$fieldName}` {$fieldTypeValue} {$fieldNull} {$fieldDefault},"; |
||
| 357 | $retShort = " `{$fieldName}` {$fieldTypeValue},"; |
||
| 358 | |||
| 359 | $ret = $retShort; |
||
| 360 | if (null != $autoincrement) { |
||
| 361 | $ret = $retAutoincrement; |
||
| 362 | } elseif (null != $fieldAttribute) { |
||
| 363 | $ret = $retFieldAttribute; |
||
| 364 | } elseif (null === $fieldAttribute) { |
||
| 365 | $ret = $fieldDefault; |
||
| 366 | } |
||
| 367 | |||
| 368 | return $ret; |
||
| 369 | } |
||
| 370 | |||
| 371 | /** |
||
| 372 | * @private function getKey |
||
| 373 | * |
||
| 374 | * @param $key |
||
| 375 | * @param $fieldName |
||
| 376 | * @return string |
||
| 377 | */ |
||
| 378 | private function getKey($key, $fieldName) |
||
| 379 | { |
||
| 380 | $ret = null; |
||
| 381 | switch ($key) { |
||
| 382 | case 2: // PRIMARY KEY |
||
| 383 | $ret = " PRIMARY KEY (`{$fieldName}`)"; |
||
| 384 | break; |
||
| 385 | case 3: // UNIQUE KEY |
||
| 386 | $ret = " UNIQUE KEY `{$fieldName}` (`{$fieldName}`)"; |
||
| 387 | break; |
||
| 388 | case 4: // KEY |
||
| 389 | $ret = " KEY `{$fieldName}` (`{$fieldName}`)"; |
||
| 390 | break; |
||
| 391 | case 5: // INDEX |
||
| 392 | $ret = " INDEX (`{$fieldName}`)"; |
||
| 393 | break; |
||
| 394 | case 6: // FULLTEXT KEY |
||
| 395 | $ret = " FULLTEXT KEY `{$fieldName}` (`{$fieldName}`)"; |
||
| 396 | break; |
||
| 397 | } |
||
| 398 | |||
| 399 | return $ret; |
||
| 400 | } |
||
| 401 | |||
| 402 | /** |
||
| 403 | * @private function getComma |
||
| 404 | * |
||
| 405 | * @param $row |
||
| 406 | * @param $comma |
||
| 407 | * |
||
| 408 | * @return string |
||
| 409 | */ |
||
| 410 | private function getComma($row, $comma = null) |
||
| 411 | { |
||
| 412 | return " {$row}{$comma}"; |
||
| 413 | } |
||
| 414 | |||
| 415 | /** |
||
| 416 | * @private function getCommaCicle |
||
| 417 | * |
||
| 418 | * @param $comma |
||
| 419 | * @param $index |
||
| 420 | * |
||
| 421 | * @return string |
||
| 422 | */ |
||
| 423 | private function getCommaCicle($comma, $index) |
||
|
0 ignored issues
–
show
|
|||
| 424 | { |
||
| 425 | $ret = null; |
||
| 426 | // Comma issue |
||
| 427 | for ($i = 1; $i <= $index; ++$i) { |
||
| 428 | if ($i != $index - 1) { |
||
| 429 | $ret = $this->getComma(isset($comma[$i]), ',') . "\n"; |
||
| 430 | } else { |
||
| 431 | $ret = $this->getComma(isset($comma[$i])) . "\n"; |
||
| 432 | } |
||
| 433 | } |
||
| 434 | |||
| 435 | return $ret; |
||
| 436 | } |
||
| 437 | |||
| 438 | /** |
||
| 439 | * @public function render |
||
| 440 | * |
||
| 441 | * @param null |
||
| 442 | * |
||
| 443 | * @return bool|string |
||
| 444 | */ |
||
| 445 | public function render() |
||
| 446 | { |
||
| 447 | $module = $this->getModule(); |
||
| 448 | $filename = $this->getFileName(); |
||
| 449 | $moduleName = mb_strtolower($module->getVar('mod_name')); |
||
| 450 | $moduleDirname = mb_strtolower($module->getVar('mod_dirname')); |
||
| 451 | $content = $this->getHeaderSqlComments($moduleName); |
||
| 452 | $content .= $this->getDatabaseTables($module); |
||
| 453 | |||
| 454 | $this->create($moduleDirname, 'sql', $filename, $content, _AM_TDMCREATE_FILE_CREATED, _AM_TDMCREATE_FILE_NOTCREATED); |
||
| 455 | |||
| 456 | return $this->renderFile(); |
||
| 457 | } |
||
| 458 | } |
||
| 459 |