| Total Complexity | 108 |
| Total Lines | 815 |
| Duplicated Lines | 0 % |
| Changes | 5 | ||
| Bugs | 1 | Features | 1 |
Complex classes like Upgrade_2511 often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Upgrade_2511, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 15 | class Upgrade_2511 extends XoopsUpgrade |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * __construct |
||
| 19 | */ |
||
| 20 | public function __construct() |
||
| 21 | { |
||
| 22 | parent::__construct(basename(__DIR__)); |
||
| 23 | $this->tasks = array( |
||
| 24 | 'cleancache', |
||
| 25 | 'bannerintsize', |
||
| 26 | 'captchadata', |
||
| 27 | 'configkey', |
||
| 28 | 'modulesvarchar', |
||
| 29 | 'qmail', |
||
| 30 | 'rmindexhtml', |
||
| 31 | 'textsanitizer', |
||
| 32 | 'xoopsconfig', |
||
| 33 | 'templates', |
||
| 34 | 'templatesadmin', |
||
| 35 | 'zapsmarty', |
||
| 36 | 'notificationmethod', |
||
| 37 | ); |
||
| 38 | $this->usedFiles = array(); |
||
| 39 | $this->pathsToCheck = array( |
||
| 40 | XOOPS_ROOT_PATH . '/cache', |
||
| 41 | XOOPS_ROOT_PATH . '/class', |
||
| 42 | XOOPS_ROOT_PATH . '/Frameworks', |
||
| 43 | XOOPS_ROOT_PATH . '/images', |
||
| 44 | XOOPS_ROOT_PATH . '/include', |
||
| 45 | XOOPS_ROOT_PATH . '/kernel', |
||
| 46 | XOOPS_ROOT_PATH . '/language', |
||
| 47 | XOOPS_ROOT_PATH . '/media', |
||
| 48 | XOOPS_ROOT_PATH . '/modules/pm', |
||
| 49 | XOOPS_ROOT_PATH . '/modules/profile', |
||
| 50 | XOOPS_ROOT_PATH . '/modules/protector', |
||
| 51 | XOOPS_ROOT_PATH . '/modules/system', |
||
| 52 | XOOPS_ROOT_PATH . '/templates_c', |
||
| 53 | XOOPS_ROOT_PATH . '/themes/default', |
||
| 54 | XOOPS_ROOT_PATH . '/themes/xbootstrap', |
||
| 55 | XOOPS_ROOT_PATH . '/themes/xswatch', |
||
| 56 | XOOPS_ROOT_PATH . '/themes/xswatch4', |
||
| 57 | XOOPS_ROOT_PATH . '/uploads', |
||
| 58 | XOOPS_VAR_PATH, |
||
| 59 | XOOPS_PATH, |
||
| 60 | ); |
||
| 61 | } |
||
| 62 | |||
| 63 | protected $cleanCacheKey = 'cache-cleaned'; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * We must remove stale template caches and compiles |
||
| 67 | * |
||
| 68 | * @return bool true if patch IS applied, false if NOT applied |
||
| 69 | */ |
||
| 70 | public function check_cleancache() |
||
| 71 | { |
||
| 72 | if (!array_key_exists($this->cleanCacheKey, $_SESSION) |
||
| 73 | || $_SESSION[$this->cleanCacheKey]===false) { |
||
| 74 | return false; |
||
| 75 | } |
||
| 76 | return true; |
||
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Remove all caches and compiles |
||
| 81 | * |
||
| 82 | * @return bool true if applied, false if failed |
||
| 83 | */ |
||
| 84 | public function apply_cleancache() |
||
| 85 | { |
||
| 86 | require_once XOOPS_ROOT_PATH . '/modules/system/class/maintenance.php'; |
||
| 87 | $maintenance = new SystemMaintenance(); |
||
| 88 | $result = $maintenance->CleanCache(array(1,2,3)); |
||
| 89 | if ($result===true) { |
||
| 90 | $_SESSION[$this->cleanCacheKey] = true; |
||
| 91 | } |
||
| 92 | return $result; |
||
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Determine if columns are declared mediumint, and if |
||
| 97 | * so, queue ddl to alter to int. |
||
| 98 | * |
||
| 99 | * @param Tables $migrate |
||
| 100 | * @param string $bannerTableName |
||
| 101 | * @param string[] $bannerColumnNames array of columns to check |
||
| 102 | * |
||
| 103 | * @return integer count of queue items added |
||
| 104 | */ |
||
| 105 | protected function fromMediumToInt(Tables $migrate, $bannerTableName, $bannerColumnNames) |
||
| 106 | { |
||
| 107 | $migrate->useTable($bannerTableName); |
||
| 108 | $count = 0; |
||
| 109 | foreach ($bannerColumnNames as $column) { |
||
| 110 | $attributes = $migrate->getColumnAttributes($bannerTableName, $column); |
||
| 111 | if (0 === strpos(trim($attributes), 'mediumint')) { |
||
|
|
|||
| 112 | $count++; |
||
| 113 | $migrate->alterColumn($bannerTableName, $column, 'int(10) UNSIGNED NOT NULL DEFAULT \'0\''); |
||
| 114 | } |
||
| 115 | } |
||
| 116 | return $count; |
||
| 117 | } |
||
| 118 | |||
| 119 | private $bannerTableName = 'banner'; |
||
| 120 | private $bannerColumnNames = array('impmade', 'clicks'); |
||
| 121 | |||
| 122 | /** |
||
| 123 | * Increase count columns from mediumint to int |
||
| 124 | * |
||
| 125 | * @return bool true if patch IS applied, false if NOT applied |
||
| 126 | */ |
||
| 127 | public function check_bannerintsize() |
||
| 128 | { |
||
| 129 | $migrate = new Tables(); |
||
| 130 | $count = $this->fromMediumToInt($migrate, $this->bannerTableName, $this->bannerColumnNames); |
||
| 131 | |||
| 132 | return $count==0; |
||
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Increase count columns from mediumint to int (Think BIG!) |
||
| 137 | * |
||
| 138 | * @return bool true if applied, false if failed |
||
| 139 | */ |
||
| 140 | public function apply_bannerintsize() |
||
| 141 | { |
||
| 142 | $migrate = new Tables(); |
||
| 143 | |||
| 144 | $count = $this->fromMediumToInt($migrate, $this->bannerTableName, $this->bannerColumnNames); |
||
| 145 | |||
| 146 | $result = $migrate->executeQueue(true); |
||
| 147 | if (false === $result) { |
||
| 148 | $this->logs[] = sprintf( |
||
| 149 | 'Migration of %s table failed. Error: %s - %s' . |
||
| 150 | $this->bannerTableName, |
||
| 151 | $migrate->getLastErrNo(), |
||
| 152 | $migrate->getLastError() |
||
| 153 | ); |
||
| 154 | return false; |
||
| 155 | } |
||
| 156 | |||
| 157 | return $count!==0; |
||
| 158 | } |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Add qmail as valid mailmethod |
||
| 162 | * |
||
| 163 | * @return bool |
||
| 164 | */ |
||
| 165 | public function check_qmail() |
||
| 166 | { |
||
| 167 | /** @var XoopsMySQLDatabase $db */ |
||
| 168 | $db = XoopsDatabaseFactory::getDatabaseConnection(); |
||
| 169 | |||
| 170 | $table = $db->prefix('configoption'); |
||
| 171 | |||
| 172 | $sql = sprintf( |
||
| 173 | 'SELECT count(*) FROM `%s` ' |
||
| 174 | . "WHERE `conf_id` = 64 AND `confop_name` = 'qmail'", |
||
| 175 | $db->escape($table) |
||
| 176 | ); |
||
| 177 | |||
| 178 | /** @var mysqli_result $result */ |
||
| 179 | $result = $db->query($sql); |
||
| 180 | if ($db->isResultSet($result)) { |
||
| 181 | $row = $db->fetchRow($result); |
||
| 182 | if ($row) { |
||
| 183 | $count = $row[0]; |
||
| 184 | return (0 === (int) $count) ? false : true; |
||
| 185 | } |
||
| 186 | } |
||
| 187 | return false; |
||
| 188 | } |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Add qmail as valid mailmethod |
||
| 192 | * |
||
| 193 | * phpMailer has qmail support, similar to but slightly different than sendmail |
||
| 194 | * This will allow webmasters to utilize qmail if it is provisioned on server. |
||
| 195 | * |
||
| 196 | * @return bool |
||
| 197 | */ |
||
| 198 | public function apply_qmail() |
||
| 199 | { |
||
| 200 | $migrate = new Tables(); |
||
| 201 | $migrate->useTable('configoption'); |
||
| 202 | $migrate->insert( |
||
| 203 | 'configoption', |
||
| 204 | array('confop_name' => 'qmail', 'confop_value' => 'qmail', 'conf_id' => 64) |
||
| 205 | ); |
||
| 206 | return $migrate->executeQueue(true); |
||
| 207 | } |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Do we need to move captcha writable data? |
||
| 211 | * |
||
| 212 | * @return bool true if patch IS applied, false if NOT applied |
||
| 213 | */ |
||
| 214 | public function check_captchadata() |
||
| 215 | { |
||
| 216 | $captchaConfigFile = XOOPS_VAR_PATH . '/configs/captcha/config.php'; |
||
| 217 | $oldCaptchaConfigFile = XOOPS_ROOT_PATH . '/class/captcha/config.php'; |
||
| 218 | if (!file_exists($oldCaptchaConfigFile)) { // nothing to copy |
||
| 219 | return true; |
||
| 220 | } |
||
| 221 | return file_exists($captchaConfigFile); |
||
| 222 | } |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Attempt to make the supplied path |
||
| 226 | * |
||
| 227 | * @param string $newPath |
||
| 228 | * |
||
| 229 | * @return bool |
||
| 230 | */ |
||
| 231 | private function makeDirectory($newPath) |
||
| 232 | { |
||
| 233 | if (!mkdir($newPath) && !is_dir($newPath)) { |
||
| 234 | $this->logs[] = sprintf('Captcha config directory %s was not created', $newPath); |
||
| 235 | return false; |
||
| 236 | } |
||
| 237 | return true; |
||
| 238 | } |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Copy file $source to $destination |
||
| 242 | * |
||
| 243 | * @param string $source |
||
| 244 | * @param string $destination |
||
| 245 | * |
||
| 246 | * @return bool true if successful, false on error |
||
| 247 | */ |
||
| 248 | private function copyFile($source, $destination) |
||
| 249 | { |
||
| 250 | if (!file_exists($destination)) { // don't overwrite anything |
||
| 251 | $result = copy($source, $destination); |
||
| 252 | if (false === $result) { |
||
| 253 | $this->logs[] = sprintf('Captcha config file copy %s failed', basename($source)); |
||
| 254 | return false; |
||
| 255 | } |
||
| 256 | } |
||
| 257 | return true; |
||
| 258 | } |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Move captcha configs to xoops_data to segregate writable data |
||
| 262 | * |
||
| 263 | * @return bool |
||
| 264 | */ |
||
| 265 | public function apply_captchadata() |
||
| 266 | { |
||
| 267 | $returnResult = false; |
||
| 268 | $sourcePath = XOOPS_ROOT_PATH . '/class/captcha/'; |
||
| 269 | $destinationPath = XOOPS_VAR_PATH . '/configs/captcha/'; |
||
| 270 | |||
| 271 | if (!file_exists($destinationPath)) { |
||
| 272 | $this->makeDirectory($destinationPath); |
||
| 273 | } |
||
| 274 | $directory = dir($sourcePath); |
||
| 275 | if (false === $directory) { |
||
| 276 | $this->logs[] = sprintf('Failed to read source %s', $sourcePath); |
||
| 277 | return false; |
||
| 278 | } |
||
| 279 | while (false !== ($entry = $directory->read())) { |
||
| 280 | if (false === strpos($entry, '.dist.') |
||
| 281 | && strpos($entry, 'config.') === 0 && '.php' === substr($entry, -4)) { |
||
| 282 | $src = $sourcePath . $entry; |
||
| 283 | $dest = $destinationPath . $entry; |
||
| 284 | $status = $this->copyFile($src, $dest); |
||
| 285 | if (false === $status) { |
||
| 286 | $returnResult = false; |
||
| 287 | } |
||
| 288 | } |
||
| 289 | } |
||
| 290 | $directory->close(); |
||
| 291 | |||
| 292 | return $returnResult; |
||
| 293 | } |
||
| 294 | |||
| 295 | //config |
||
| 296 | /** |
||
| 297 | * Increase primary key columns from smallint to int |
||
| 298 | * |
||
| 299 | * @return bool true if patch IS applied, false if NOT applied |
||
| 300 | */ |
||
| 301 | public function check_configkey() |
||
| 302 | { |
||
| 303 | $tableName = 'config'; |
||
| 304 | $columnName = 'conf_id'; |
||
| 305 | |||
| 306 | $migrate = new Tables(); |
||
| 307 | $migrate->useTable($tableName); |
||
| 308 | $count = 0; |
||
| 309 | $attributes = $migrate->getColumnAttributes($tableName, $columnName); |
||
| 310 | if (0 === strpos(trim($attributes), 'smallint')) { |
||
| 311 | $count++; |
||
| 312 | $migrate->alterColumn($tableName, $columnName, 'int(10) UNSIGNED NOT NULL'); |
||
| 313 | } |
||
| 314 | |||
| 315 | return $count==0; |
||
| 316 | } |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Increase primary key columns from smallint to int |
||
| 320 | * |
||
| 321 | * @return bool true if applied, false if failed |
||
| 322 | */ |
||
| 323 | public function apply_configkey() |
||
| 324 | { |
||
| 325 | $tableName = 'config'; |
||
| 326 | $columnName = 'conf_id'; |
||
| 327 | |||
| 328 | $migrate = new Tables(); |
||
| 329 | $migrate->useTable($tableName); |
||
| 330 | $count = 0; |
||
| 331 | $attributes = $migrate->getColumnAttributes($tableName, $columnName); |
||
| 332 | if (0 === strpos(trim($attributes), 'smallint')) { |
||
| 333 | $count++; |
||
| 334 | $migrate->alterColumn($tableName, $columnName, 'int(10) UNSIGNED NOT NULL AUTO_INCREMENT'); |
||
| 335 | } |
||
| 336 | |||
| 337 | $result = $migrate->executeQueue(true); |
||
| 338 | if (false === $result) { |
||
| 339 | $this->logs[] = sprintf( |
||
| 340 | 'Migration of %s table failed. Error: %s - %s' . |
||
| 341 | $tableName, |
||
| 342 | $migrate->getLastErrNo(), |
||
| 343 | $migrate->getLastError() |
||
| 344 | ); |
||
| 345 | return false; |
||
| 346 | } |
||
| 347 | |||
| 348 | return $count!==0; |
||
| 349 | } |
||
| 350 | //configend |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Do we need to create a xoops_data/configs/xoopsconfig.php? |
||
| 354 | * |
||
| 355 | * @return bool true if patch IS applied, false if NOT applied |
||
| 356 | */ |
||
| 357 | public function check_xoopsconfig() |
||
| 358 | { |
||
| 359 | $xoopsConfigFile = XOOPS_VAR_PATH . '/configs/xoopsconfig.php'; |
||
| 360 | return file_exists($xoopsConfigFile); |
||
| 361 | } |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Create xoops_data/configs/xoopsconfig.php from xoopsconfig.dist.php |
||
| 365 | * |
||
| 366 | * @return bool true if applied, false if failed |
||
| 367 | */ |
||
| 368 | public function apply_xoopsconfig() |
||
| 369 | { |
||
| 370 | $source = XOOPS_VAR_PATH . '/configs/xoopsconfig.dist.php'; |
||
| 371 | $destination = XOOPS_VAR_PATH . '/configs/xoopsconfig.php'; |
||
| 372 | if (!file_exists($destination)) { // don't overwrite anything |
||
| 373 | $result = copy($source, $destination); |
||
| 374 | if (false === $result) { |
||
| 375 | $this->logs[] = 'xoopsconfig.php file copy failed'; |
||
| 376 | return false; |
||
| 377 | } |
||
| 378 | } |
||
| 379 | return true; |
||
| 380 | } |
||
| 381 | |||
| 382 | /** |
||
| 383 | * This is a default list based on extensions as supplied by XOOPS. |
||
| 384 | * If possible, we will build a list based on contents of class/textsanitizer/ |
||
| 385 | * key is file path relative to XOOPS_ROOT_PATH . '/class/textsanitizer/ |
||
| 386 | * value is file path relative to XOOPS_VAR_PATH . '/configs/textsanitizer/' |
||
| 387 | * |
||
| 388 | * @var string[] |
||
| 389 | */ |
||
| 390 | protected $textsanitizerConfigFiles = array( |
||
| 391 | 'config.php' => 'config.php', |
||
| 392 | 'censor/config.php' => 'config.censor.php', |
||
| 393 | 'flash/config.php' => 'config.flash.php', |
||
| 394 | 'image/config.php' => 'config.image.php', |
||
| 395 | 'mms/config.php' => 'config.mms.php', |
||
| 396 | 'rtsp/config.php' => 'config.rtsp.php', |
||
| 397 | 'syntaxhighlight/config.php' => 'config.syntaxhighlight.php', |
||
| 398 | 'textfilter/config.php' => 'config.textfilter.php', |
||
| 399 | 'wiki/config.php' => 'config.wiki.php', |
||
| 400 | 'wmp/config.php' => 'config.wmp.php', |
||
| 401 | ); |
||
| 402 | |||
| 403 | /** |
||
| 404 | * Build a list of config files using the existing textsanitizer/config.php |
||
| 405 | * each as source name => destination name in $this->textsanitizerConfigFiles |
||
| 406 | * |
||
| 407 | * This should prevent some issues with customized systems. |
||
| 408 | * |
||
| 409 | * @return void |
||
| 410 | */ |
||
| 411 | protected function buildListTSConfigs() |
||
| 429 | } |
||
| 430 | |||
| 431 | /** |
||
| 432 | * Do we need to move any existing files to xoops_data/configs/textsanitizer/ ? |
||
| 433 | * |
||
| 434 | * @return bool true if patch IS applied, false if NOT applied |
||
| 435 | */ |
||
| 436 | public function check_textsanitizer() |
||
| 437 | { |
||
| 438 | $this->buildListTSConfigs(); |
||
| 439 | foreach ($this->textsanitizerConfigFiles as $source => $destination) { |
||
| 440 | $src = XOOPS_ROOT_PATH . '/class/textsanitizer/' . $source; |
||
| 441 | $dest = XOOPS_VAR_PATH . '/configs/textsanitizer/' . $destination; |
||
| 442 | if (!file_exists($dest) && file_exists($src)) { |
||
| 443 | return false; |
||
| 444 | } |
||
| 445 | } |
||
| 446 | return true; |
||
| 447 | } |
||
| 448 | |||
| 449 | /** |
||
| 450 | * Copy and rename any existing class/textsanitizer/ config files to xoops_data/configs/textsanitizer/ |
||
| 451 | * |
||
| 452 | * @return bool true if applied, false if failed |
||
| 453 | */ |
||
| 454 | public function apply_textsanitizer() |
||
| 455 | { |
||
| 456 | $this->buildListTSConfigs(); |
||
| 457 | $return = true; |
||
| 458 | foreach ($this->textsanitizerConfigFiles as $source => $destination) { |
||
| 459 | $src = XOOPS_ROOT_PATH . '/class/textsanitizer/' . $source; |
||
| 460 | $dest = XOOPS_VAR_PATH . '/configs/textsanitizer/' . $destination; |
||
| 461 | if (!file_exists($dest) && file_exists($src)) { |
||
| 462 | $result = copy($src, $dest); |
||
| 463 | if (false === $result) { |
||
| 464 | $this->logs[] = sprintf('textsanitizer file copy to %s failed', $destination); |
||
| 465 | $return = false; |
||
| 466 | } |
||
| 467 | } |
||
| 468 | } |
||
| 469 | return $return; |
||
| 470 | } |
||
| 471 | |||
| 472 | /** |
||
| 473 | * Attempt to remove index.html files replaced by index.php |
||
| 474 | */ |
||
| 475 | /** |
||
| 476 | * List of directories supplied by XOOPS. This is used to try and keep us out |
||
| 477 | * of things added to the system locally. (Set in __construct() for php BC.) |
||
| 478 | * |
||
| 479 | * @var string[] |
||
| 480 | */ |
||
| 481 | private $pathsToCheck; |
||
| 482 | |||
| 483 | /** |
||
| 484 | * Do we need to remove any index.html files that were replaced by index.php files? |
||
| 485 | * |
||
| 486 | * @return bool true if patch IS applied, false if NOT applied |
||
| 487 | */ |
||
| 488 | public function check_rmindexhtml() |
||
| 489 | { |
||
| 490 | /** |
||
| 491 | * If we find an index.html that is writable, we know there is work to do |
||
| 492 | * |
||
| 493 | * @param string $name file name to check |
||
| 494 | * |
||
| 495 | * @return bool true to continue, false to stop scan |
||
| 496 | */ |
||
| 497 | $stopIfFound = function ($name) { |
||
| 498 | $ok = is_writable($name); |
||
| 499 | return !($ok); |
||
| 500 | }; |
||
| 501 | |||
| 502 | clearstatcache(); |
||
| 503 | |||
| 504 | return $this->dirWalker($stopIfFound); |
||
| 505 | } |
||
| 506 | |||
| 507 | /** |
||
| 508 | * Unlink any index.html files that have been replaced by index.php files |
||
| 509 | * |
||
| 510 | * @return bool true if patch applied, false if failed |
||
| 511 | */ |
||
| 512 | public function apply_rmindexhtml() |
||
| 513 | { |
||
| 514 | /** |
||
| 515 | * Do unlink() on file |
||
| 516 | * Always return true so we process each writable index.html |
||
| 517 | * |
||
| 518 | * @param string $name file name to unlink |
||
| 519 | * |
||
| 520 | * @return true always report true, even if we can't delete -- best effort only |
||
| 521 | */ |
||
| 522 | $unlinkByName = function ($name) { |
||
| 523 | if (is_writable($name)) { |
||
| 524 | $result = unlink($name); |
||
| 525 | } |
||
| 526 | return true; |
||
| 527 | }; |
||
| 528 | |||
| 529 | |||
| 530 | return $this->dirWalker($unlinkByName); |
||
| 531 | } |
||
| 532 | |||
| 533 | /** |
||
| 534 | * Walk list of directories in $pathsToCheck |
||
| 535 | * |
||
| 536 | * @param \Closure $onFound |
||
| 537 | * |
||
| 538 | * @return bool |
||
| 539 | */ |
||
| 540 | private function dirWalker(\Closure $onFound) |
||
| 541 | { |
||
| 542 | $check = true; |
||
| 543 | foreach ($this->pathsToCheck as $path) { |
||
| 544 | $check = $this->checkDirForIndexHtml($path, $onFound); |
||
| 545 | if (false === $check) { |
||
| 546 | break; |
||
| 547 | } |
||
| 548 | } |
||
| 549 | if (false !== $check) { |
||
| 550 | $check = true; |
||
| 551 | } |
||
| 552 | return $check; |
||
| 553 | } |
||
| 554 | |||
| 555 | /** |
||
| 556 | * Recursively check for index.html files that have a corresponding index.php file |
||
| 557 | * in the supplied path. |
||
| 558 | * |
||
| 559 | * @param string $startingPath |
||
| 560 | * @param \Closure $onFound |
||
| 561 | * |
||
| 562 | * @return false|int false if onFound returned false (don't continue) else count of matches |
||
| 563 | */ |
||
| 564 | private function checkDirForIndexHtml($startingPath, \Closure $onFound) |
||
| 565 | { |
||
| 566 | if (!is_dir($startingPath)) { |
||
| 567 | return 0; |
||
| 568 | } |
||
| 569 | $i = 0; |
||
| 570 | $rdi = new \RecursiveDirectoryIterator($startingPath); |
||
| 571 | $rii = new \RecursiveIteratorIterator($rdi); |
||
| 572 | /** @var \SplFileInfo $fileinfo */ |
||
| 573 | foreach ($rii as $fileinfo) { |
||
| 574 | if ($fileinfo->isFile() && 'index.html' === $fileinfo->getFilename() && 60 > $fileinfo->getSize()) { |
||
| 575 | $path = $fileinfo->getPath(); |
||
| 576 | $testFilename = $path . '/index.php'; |
||
| 577 | if (file_exists($testFilename)) { |
||
| 578 | $unlinkName = $path . '/' . $fileinfo->getFilename(); |
||
| 579 | ++$i; |
||
| 580 | $continue = $onFound($unlinkName); |
||
| 581 | if (false === $continue) { |
||
| 582 | return $continue; |
||
| 583 | } |
||
| 584 | } |
||
| 585 | } |
||
| 586 | } |
||
| 587 | return $i; |
||
| 588 | } |
||
| 589 | |||
| 590 | /** |
||
| 591 | * Determine if columns are declared smallint, and if |
||
| 592 | * so, queue ddl to alter to varchar. |
||
| 593 | * |
||
| 594 | * @param Tables $migrate |
||
| 595 | * @param string $modulesTableName |
||
| 596 | * @param string[] $modulesColumnNames array of columns to check |
||
| 597 | * |
||
| 598 | * @return integer count of queue items added |
||
| 599 | */ |
||
| 600 | protected function fromSmallintToVarchar(Tables $migrate, $modulesTableName, $modulesColumnNames) |
||
| 601 | { |
||
| 602 | $migrate->useTable($modulesTableName); |
||
| 603 | $count = 0; |
||
| 604 | foreach ($modulesColumnNames as $column) { |
||
| 605 | $attributes = $migrate->getColumnAttributes($modulesTableName, $column); |
||
| 606 | if (is_string($attributes) && 0 === strpos(trim($attributes), 'smallint')) { |
||
| 607 | $count++; |
||
| 608 | $migrate->alterColumn($modulesTableName, $column, 'varchar(32) NOT NULL DEFAULT \'\''); |
||
| 609 | } |
||
| 610 | } |
||
| 611 | return $count; |
||
| 612 | } |
||
| 613 | |||
| 614 | private $modulesTableName = 'modules'; |
||
| 615 | private $modulesColumnNames = array('version'); |
||
| 616 | |||
| 617 | /** |
||
| 618 | * Increase version columns from smallint to varchar |
||
| 619 | * |
||
| 620 | * @return bool true if patch IS applied, false if NOT applied |
||
| 621 | */ |
||
| 622 | public function check_modulesvarchar() |
||
| 623 | { |
||
| 624 | $migrate = new Tables(); |
||
| 625 | $count = $this->fromSmallintToVarchar($migrate, $this->modulesTableName, $this->modulesColumnNames); |
||
| 626 | return $count==0; |
||
| 627 | } |
||
| 628 | |||
| 629 | /** |
||
| 630 | * Increase version columns from smallint to varchar |
||
| 631 | * |
||
| 632 | * @return bool true if applied, false if failed |
||
| 633 | */ |
||
| 634 | public function apply_modulesvarchar() |
||
| 635 | { |
||
| 636 | $migrate = new Tables(); |
||
| 637 | |||
| 638 | $count = $this->fromSmallintToVarchar($migrate, $this->modulesTableName, $this->modulesColumnNames); |
||
| 639 | |||
| 640 | $result = $migrate->executeQueue(true); |
||
| 641 | if (false === $result) { |
||
| 642 | $this->logs[] = sprintf( |
||
| 643 | 'Migration of %s table failed. Error: %s - %s' . |
||
| 644 | $this->modulesTableName, |
||
| 645 | $migrate->getLastErrNo(), |
||
| 646 | $migrate->getLastError() |
||
| 647 | ); |
||
| 648 | return false; |
||
| 649 | } |
||
| 650 | |||
| 651 | return true; |
||
| 652 | } |
||
| 653 | |||
| 654 | /** |
||
| 655 | * @return bool |
||
| 656 | */ |
||
| 657 | public function check_templates() |
||
| 658 | { |
||
| 659 | $sql = 'SELECT COUNT(*) FROM `' . $GLOBALS['xoopsDB']->prefix('tplfile') . "` WHERE `tpl_file` IN ('system_confirm.tpl') AND `tpl_type` = 'module'"; |
||
| 660 | $result = $GLOBALS['xoopsDB']->queryF($sql); |
||
| 661 | if (!$GLOBALS['xoopsDB']->isResultSet($result)) { |
||
| 662 | return false; |
||
| 663 | } |
||
| 664 | list($count) = $GLOBALS['xoopsDB']->fetchRow($result); |
||
| 665 | |||
| 666 | return ($count != 0); |
||
| 667 | } |
||
| 668 | |||
| 669 | |||
| 670 | /** |
||
| 671 | * @return bool |
||
| 672 | */ |
||
| 673 | public function apply_templates() |
||
| 694 | } |
||
| 695 | |||
| 696 | /** |
||
| 697 | * @return bool |
||
| 698 | */ |
||
| 699 | public function check_templatesadmin() |
||
| 709 | } |
||
| 710 | |||
| 711 | /** |
||
| 712 | * @return bool |
||
| 713 | */ |
||
| 714 | public function apply_templatesadmin() |
||
| 715 | { |
||
| 716 | include XOOPS_ROOT_PATH . '/modules/system/xoops_version.php'; |
||
| 717 | $dbm = new Db_manager(); |
||
| 718 | $time = time(); |
||
| 719 | foreach ($modversion['templates'] as $tplfile) { |
||
| 720 | // Admin templates |
||
| 721 | if (isset($tplfile['type']) && $tplfile['type'] === 'admin' && $fp = fopen('../modules/system/templates/admin/' . $tplfile['file'], 'r')) { |
||
| 722 | $newtplid = $dbm->insert('tplfile', " VALUES (0, 1, 'system', 'default', '" . addslashes($tplfile['file']) . "', '" . addslashes($tplfile['description']) . "', " . $time . ', ' . $time . ", 'admin')"); |
||
| 723 | $tplsource = fread($fp, filesize('../modules/system/templates/admin/' . $tplfile['file'])); |
||
| 724 | fclose($fp); |
||
| 725 | $dbm->insert('tplsource', ' (tpl_id, tpl_source) VALUES (' . $newtplid . ", '" . addslashes($tplsource) . "')"); |
||
| 726 | } |
||
| 727 | } |
||
| 728 | |||
| 729 | return true; |
||
| 730 | } |
||
| 731 | |||
| 732 | //modules/system/themes/legacy/legacy.php |
||
| 733 | /** |
||
| 734 | * Do we need to delete obsolete Smarty files? |
||
| 735 | * |
||
| 736 | * @return bool |
||
| 737 | */ |
||
| 738 | public function check_zapsmarty() |
||
| 739 | { |
||
| 740 | return !file_exists('../class/smarty/smarty.class.php'); |
||
| 741 | } |
||
| 742 | |||
| 743 | /** |
||
| 744 | * Delete obsolete Smarty files |
||
| 745 | * |
||
| 746 | * @return bool |
||
| 747 | */ |
||
| 748 | public function apply_zapsmarty() |
||
| 749 | { |
||
| 750 | // Define the base directory |
||
| 751 | $baseDir = '../class/smarty/'; |
||
| 752 | |||
| 753 | // List of sub-folders and files to delete |
||
| 754 | $itemsToDelete = array( |
||
| 755 | 'configs', |
||
| 756 | 'internals', |
||
| 757 | 'xoops_plugins', |
||
| 758 | 'Config_File.class.php', |
||
| 759 | 'debug.tpl', |
||
| 760 | 'Smarty.class.php', |
||
| 761 | 'Smarty_Compiler.class.php' |
||
| 762 | ); |
||
| 763 | |||
| 764 | // Loop through each item and delete it |
||
| 765 | foreach ($itemsToDelete as $item) { |
||
| 766 | $path = $baseDir . $item; |
||
| 767 | |||
| 768 | // Check if it's a directory or a file |
||
| 769 | if (is_dir($path)) { |
||
| 770 | // Delete directory and its contents |
||
| 771 | array_map('unlink', glob("$path/*.*")); |
||
| 772 | rmdir($path); |
||
| 773 | } elseif (is_file($path)) { |
||
| 774 | // Delete file |
||
| 775 | if (is_writable($path)) { |
||
| 776 | unlink($path); |
||
| 777 | } |
||
| 778 | } |
||
| 779 | } |
||
| 780 | |||
| 781 | return true; |
||
| 782 | } |
||
| 783 | |||
| 784 | /** |
||
| 785 | * Check if default notification method already exists |
||
| 786 | * |
||
| 787 | */ |
||
| 788 | public function check_notificationmethod() |
||
| 789 | { |
||
| 790 | $sql = 'SELECT COUNT(*) FROM `' . $GLOBALS['xoopsDB']->prefix('config') . "` WHERE `conf_name` IN ('default_notification')"; |
||
| 791 | if (!$result = $GLOBALS['xoopsDB']->queryF($sql)) { |
||
| 792 | return false; |
||
| 793 | } |
||
| 794 | return true; |
||
| 795 | } |
||
| 796 | |||
| 797 | /** |
||
| 798 | * @return bool |
||
| 799 | */ |
||
| 800 | public function apply_notificationmethod() |
||
| 830 | } |
||
| 831 | |||
| 832 | |||
| 833 | } |
||
| 834 | |||
| 835 | return new Upgrade_2511(); |
||
| 836 |