| Conditions | 81 |
| Paths | > 20000 |
| Total Lines | 327 |
| Code Lines | 187 |
| Lines | 22 |
| Ratio | 6.73 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 200 | function PackageGBrowse() |
||
| 201 | { |
||
| 202 | global $txt, $context, $scripturl, $sourcedir, $forum_version, $smcFunc; |
||
| 203 | |||
| 204 | if (isset($_GET['server'])) |
||
| 205 | { |
||
| 206 | if ($_GET['server'] == '') |
||
| 207 | redirectexit('action=admin;area=packages;get'); |
||
| 208 | |||
| 209 | $server = (int) $_GET['server']; |
||
| 210 | |||
| 211 | // Query the server list to find the current server. |
||
| 212 | $request = $smcFunc['db_query']('', ' |
||
| 213 | SELECT name, url |
||
| 214 | FROM {db_prefix}package_servers |
||
| 215 | WHERE id_server = {int:current_server} |
||
| 216 | LIMIT 1', |
||
| 217 | array( |
||
| 218 | 'current_server' => $server, |
||
| 219 | ) |
||
| 220 | ); |
||
| 221 | list ($name, $url) = $smcFunc['db_fetch_row']($request); |
||
| 222 | $smcFunc['db_free_result']($request); |
||
| 223 | |||
| 224 | // If the server does not exist, dump out. |
||
| 225 | if (empty($url)) |
||
| 226 | fatal_lang_error('couldnt_connect', false); |
||
| 227 | |||
| 228 | // If there is a relative link, append to the stored server url. |
||
| 229 | if (isset($_GET['relative'])) |
||
| 230 | $url = $url . (substr($url, -1) == '/' ? '' : '/') . $_GET['relative']; |
||
| 231 | |||
| 232 | // Clear any "absolute" URL. Since "server" is present, "absolute" is garbage. |
||
| 233 | unset($_GET['absolute']); |
||
| 234 | } |
||
| 235 | elseif (isset($_GET['absolute']) && $_GET['absolute'] != '') |
||
| 236 | { |
||
| 237 | // Initialize the requried variables. |
||
| 238 | $server = ''; |
||
| 239 | $url = $_GET['absolute']; |
||
| 240 | $name = ''; |
||
| 241 | $_GET['package'] = $url . '/packages.xml?language=' . $context['user']['language']; |
||
| 242 | |||
| 243 | // Clear any "relative" URL. Since "server" is not present, "relative" is garbage. |
||
| 244 | unset($_GET['relative']); |
||
| 245 | |||
| 246 | $token = checkConfirm('get_absolute_url'); |
||
| 247 | if ($token !== true) |
||
| 248 | { |
||
| 249 | $context['sub_template'] = 'package_confirm'; |
||
| 250 | |||
| 251 | $context['page_title'] = $txt['package_servers']; |
||
| 252 | $context['confirm_message'] = sprintf($txt['package_confirm_view_package_content'], $smcFunc['htmlspecialchars']($_GET['absolute'])); |
||
| 253 | $context['proceed_href'] = $scripturl . '?action=admin;area=packages;get;sa=browse;absolute=' . urlencode($_GET['absolute']) . ';confirm=' . $token; |
||
| 254 | |||
| 255 | return; |
||
| 256 | } |
||
| 257 | } |
||
| 258 | // Minimum required parameter did not exist so dump out. |
||
| 259 | else |
||
| 260 | fatal_lang_error('couldnt_connect', false); |
||
| 261 | |||
| 262 | // Attempt to connect. If unsuccessful... try the URL. |
||
| 263 | if (!isset($_GET['package']) || file_exists($_GET['package'])) |
||
| 264 | $_GET['package'] = $url . '/packages.xml?language=' . $context['user']['language']; |
||
| 265 | |||
| 266 | // Check to be sure the packages.xml file actually exists where it is should be... or dump out. |
||
| 267 | if ((isset($_GET['absolute']) || isset($_GET['relative'])) && !url_exists($_GET['package'])) |
||
| 268 | fatal_lang_error('packageget_unable', false, array($url . '/index.php')); |
||
| 269 | |||
| 270 | // Might take some time. |
||
| 271 | @set_time_limit(600); |
||
| 272 | |||
| 273 | // Read packages.xml and parse into xmlArray. (the true tells it to trim things ;).) |
||
| 274 | require_once($sourcedir . '/Class-Package.php'); |
||
| 275 | $listing = new xmlArray(fetch_web_data($_GET['package']), true); |
||
| 276 | |||
| 277 | // Errm.... empty file? Try the URL.... |
||
| 278 | if (!$listing->exists('package-list')) |
||
| 279 | fatal_lang_error('packageget_unable', false, array($url . '/index.php')); |
||
| 280 | |||
| 281 | // List out the packages... |
||
| 282 | $context['package_list'] = array(); |
||
| 283 | |||
| 284 | $listing = $listing->path('package-list[0]'); |
||
| 285 | |||
| 286 | // Use the package list's name if it exists. |
||
| 287 | if ($listing->exists('list-title')) |
||
| 288 | $name = $smcFunc['htmlspecialchars']($listing->fetch('list-title')); |
||
| 289 | |||
| 290 | // Pick the correct template. |
||
| 291 | $context['sub_template'] = 'package_list'; |
||
| 292 | |||
| 293 | $context['page_title'] = $txt['package_servers'] . ($name != '' ? ' - ' . $name : ''); |
||
| 294 | $context['package_server'] = $server; |
||
| 295 | |||
| 296 | // By default we use an unordered list, unless there are no lists with more than one package. |
||
| 297 | $context['list_type'] = 'ul'; |
||
| 298 | |||
| 299 | $instmods = loadInstalledPackages(); |
||
| 300 | |||
| 301 | $installed_mods = array(); |
||
| 302 | // Look through the list of installed mods... |
||
| 303 | foreach ($instmods as $installed_mod) |
||
| 304 | $installed_mods[$installed_mod['package_id']] = $installed_mod['version']; |
||
| 305 | |||
| 306 | // Get default author and email if they exist. |
||
| 307 | if ($listing->exists('default-author')) |
||
| 308 | { |
||
| 309 | $default_author = $smcFunc['htmlspecialchars']($listing->fetch('default-author')); |
||
| 310 | if ($listing->exists('default-author/@email') && filter_var($listing->fetch('default-author/@email'), FILTER_VALIDATE_EMAIL)) |
||
| 311 | $default_email = $smcFunc['htmlspecialchars']($listing->fetch('default-author/@email')); |
||
| 312 | } |
||
| 313 | |||
| 314 | // Get default web site if it exists. |
||
| 315 | View Code Duplication | if ($listing->exists('default-website')) |
|
| 316 | { |
||
| 317 | $default_website = $smcFunc['htmlspecialchars']($listing->fetch('default-website')); |
||
| 318 | if ($listing->exists('default-website/@title')) |
||
| 319 | $default_title = $smcFunc['htmlspecialchars']($listing->fetch('default-website/@title')); |
||
| 320 | } |
||
| 321 | |||
| 322 | $the_version = strtr($forum_version, array('SMF ' => '')); |
||
| 323 | if (!empty($_SESSION['version_emulate'])) |
||
| 324 | $the_version = $_SESSION['version_emulate']; |
||
| 325 | |||
| 326 | $packageNum = 0; |
||
| 327 | $packageSection = 0; |
||
| 328 | |||
| 329 | $sections = $listing->set('section'); |
||
| 330 | foreach ($sections as $i => $section) |
||
| 331 | { |
||
| 332 | $context['package_list'][$packageSection] = array( |
||
| 333 | 'title' => '', |
||
| 334 | 'text' => '', |
||
| 335 | 'items' => array(), |
||
| 336 | ); |
||
| 337 | |||
| 338 | $packages = $section->set('title|heading|text|remote|rule|modification|language|avatar-pack|theme|smiley-set'); |
||
| 339 | foreach ($packages as $thisPackage) |
||
| 340 | { |
||
| 341 | $package = array( |
||
| 342 | 'type' => $thisPackage->name(), |
||
| 343 | ); |
||
| 344 | |||
| 345 | if (in_array($package['type'], array('title', 'text'))) |
||
| 346 | $context['package_list'][$packageSection][$package['type']] = $smcFunc['htmlspecialchars']($thisPackage->fetch('.')); |
||
| 347 | // It's a Title, Heading, Rule or Text. |
||
| 348 | elseif (in_array($package['type'], array('heading', 'rule'))) |
||
| 349 | $package['name'] = $smcFunc['htmlspecialchars']($thisPackage->fetch('.')); |
||
| 350 | // It's a Remote link. |
||
| 351 | elseif ($package['type'] == 'remote') |
||
| 352 | { |
||
| 353 | $remote_type = $thisPackage->exists('@type') ? $thisPackage->fetch('@type') : 'relative'; |
||
| 354 | |||
| 355 | if ($remote_type == 'relative' && substr($thisPackage->fetch('@href'), 0, 7) != 'http://' && substr($thisPackage->fetch('@href'), 0, 8) != 'https://') |
||
| 356 | { |
||
| 357 | View Code Duplication | if (isset($_GET['absolute'])) |
|
| 358 | $current_url = $_GET['absolute'] . '/'; |
||
| 359 | elseif (isset($_GET['relative'])) |
||
| 360 | $current_url = $_GET['relative'] . '/'; |
||
| 361 | else |
||
| 362 | $current_url = ''; |
||
| 363 | |||
| 364 | $current_url .= $thisPackage->fetch('@href'); |
||
| 365 | if (isset($_GET['absolute'])) |
||
| 366 | $package['href'] = $scripturl . '?action=admin;area=packages;get;sa=browse;absolute=' . $current_url; |
||
| 367 | else |
||
| 368 | $package['href'] = $scripturl . '?action=admin;area=packages;get;sa=browse;server=' . $context['package_server'] . ';relative=' . $current_url; |
||
| 369 | } |
||
| 370 | else |
||
| 371 | { |
||
| 372 | $current_url = $thisPackage->fetch('@href'); |
||
| 373 | $package['href'] = $scripturl . '?action=admin;area=packages;get;sa=browse;absolute=' . $current_url; |
||
| 374 | } |
||
| 375 | |||
| 376 | $package['name'] = $smcFunc['htmlspecialchars']($thisPackage->fetch('.')); |
||
| 377 | $package['link'] = '<a href="' . $package['href'] . '">' . $package['name'] . '</a>'; |
||
| 378 | } |
||
| 379 | // It's a package... |
||
| 380 | else |
||
| 381 | { |
||
| 382 | View Code Duplication | if (isset($_GET['absolute'])) |
|
| 383 | $current_url = $_GET['absolute'] . '/'; |
||
| 384 | elseif (isset($_GET['relative'])) |
||
| 385 | $current_url = $_GET['relative'] . '/'; |
||
| 386 | else |
||
| 387 | $current_url = ''; |
||
| 388 | |||
| 389 | $server_att = $server != '' ? ';server=' . $server : ''; |
||
| 390 | |||
| 391 | $package += $thisPackage->to_array(); |
||
| 392 | |||
| 393 | if (isset($package['website'])) |
||
| 394 | unset($package['website']); |
||
| 395 | $package['author'] = array(); |
||
| 396 | |||
| 397 | if ($package['description'] == '') |
||
| 398 | $package['description'] = $txt['package_no_description']; |
||
| 399 | else |
||
| 400 | $package['description'] = parse_bbc(preg_replace('~\[[/]?html\]~i', '', $smcFunc['htmlspecialchars']($package['description']))); |
||
| 401 | |||
| 402 | $package['is_installed'] = isset($installed_mods[$package['id']]); |
||
| 403 | $package['is_current'] = $package['is_installed'] && ($installed_mods[$package['id']] == $package['version']); |
||
| 404 | $package['is_newer'] = $package['is_installed'] && ($installed_mods[$package['id']] > $package['version']); |
||
| 405 | |||
| 406 | // This package is either not installed, or installed but old. Is it supported on this version of SMF? |
||
| 407 | if (!$package['is_installed'] || (!$package['is_current'] && !$package['is_newer'])) |
||
| 408 | { |
||
| 409 | if ($thisPackage->exists('version/@for')) |
||
| 410 | $package['can_install'] = matchPackageVersion($the_version, $thisPackage->fetch('version/@for')); |
||
| 411 | } |
||
| 412 | // Okay, it's already installed AND up to date. |
||
| 413 | else |
||
| 414 | $package['can_install'] = false; |
||
| 415 | |||
| 416 | $already_exists = getPackageInfo(basename($package['filename'])); |
||
| 417 | $package['download_conflict'] = is_array($already_exists) && $already_exists['id'] == $package['id'] && $already_exists['version'] != $package['version']; |
||
| 418 | |||
| 419 | $package['href'] = $url . '/' . $package['filename']; |
||
| 420 | $package['link'] = '<a href="' . $package['href'] . '">' . $package['name'] . '</a>'; |
||
| 421 | $package['download']['href'] = $scripturl . '?action=admin;area=packages;get;sa=download' . $server_att . ';package=' . $current_url . $package['filename'] . ($package['download_conflict'] ? ';conflict' : '') . ';' . $context['session_var'] . '=' . $context['session_id']; |
||
| 422 | $package['download']['link'] = '<a href="' . $package['download']['href'] . '">' . $package['name'] . '</a>'; |
||
| 423 | |||
| 424 | if ($thisPackage->exists('author') || isset($default_author)) |
||
| 425 | { |
||
| 426 | if ($thisPackage->exists('author/@email') && filter_var($thisPackage->fetch('author/@email'), FILTER_VALIDATE_EMAIL)) |
||
| 427 | $package['author']['email'] = $thisPackage->fetch('author/@email'); |
||
| 428 | elseif (isset($default_email)) |
||
| 429 | $package['author']['email'] = $default_email; |
||
| 430 | |||
| 431 | if ($thisPackage->exists('author') && $thisPackage->fetch('author') != '') |
||
| 432 | $package['author']['name'] = $smcFunc['htmlspecialchars']($thisPackage->fetch('author')); |
||
| 433 | else |
||
| 434 | $package['author']['name'] = $default_author; |
||
| 435 | |||
| 436 | if (!empty($package['author']['email'])) |
||
| 437 | $package['author']['link'] = '<a href="mailto:' . $package['author']['email'] . '">' . $package['author']['name'] . '</a>'; |
||
| 438 | } |
||
| 439 | |||
| 440 | if ($thisPackage->exists('website') || isset($default_website)) |
||
| 441 | { |
||
| 442 | if ($thisPackage->exists('website') && $thisPackage->exists('website/@title')) |
||
| 443 | $package['author']['website']['name'] = $smcFunc['htmlspecialchars']($thisPackage->fetch('website/@title')); |
||
| 444 | elseif (isset($default_title)) |
||
| 445 | $package['author']['website']['name'] = $default_title; |
||
| 446 | elseif ($thisPackage->exists('website')) |
||
| 447 | $package['author']['website']['name'] = $smcFunc['htmlspecialchars']($thisPackage->fetch('website')); |
||
| 448 | else |
||
| 449 | $package['author']['website']['name'] = $default_website; |
||
| 450 | |||
| 451 | View Code Duplication | if ($thisPackage->exists('website') && $thisPackage->fetch('website') != '') |
|
| 452 | $authorhompage = $smcFunc['htmlspecialchars']($thisPackage->fetch('website')); |
||
| 453 | else |
||
| 454 | $authorhompage = $default_website; |
||
| 455 | |||
| 456 | $package['author']['website']['href'] = $authorhompage; |
||
| 457 | $package['author']['website']['link'] = '<a href="' . $authorhompage . '">' . $package['author']['website']['name'] . '</a>'; |
||
| 458 | } |
||
| 459 | else |
||
| 460 | { |
||
| 461 | $package['author']['website']['href'] = ''; |
||
| 462 | $package['author']['website']['link'] = ''; |
||
| 463 | } |
||
| 464 | } |
||
| 465 | |||
| 466 | $package['is_remote'] = $package['type'] == 'remote'; |
||
| 467 | $package['is_title'] = $package['type'] == 'title'; |
||
| 468 | $package['is_heading'] = $package['type'] == 'heading'; |
||
| 469 | $package['is_text'] = $package['type'] == 'text'; |
||
| 470 | $package['is_line'] = $package['type'] == 'rule'; |
||
| 471 | |||
| 472 | $packageNum = in_array($package['type'], array('title', 'heading', 'text', 'remote', 'rule')) ? 0 : $packageNum + 1; |
||
| 473 | $package['count'] = $packageNum; |
||
| 474 | |||
| 475 | if (!in_array($package['type'], array('title', 'text'))) |
||
| 476 | $context['package_list'][$packageSection]['items'][] = $package; |
||
| 477 | |||
| 478 | if ($package['count'] > 1) |
||
| 479 | $context['list_type'] = 'ol'; |
||
| 480 | } |
||
| 481 | |||
| 482 | $packageSection++; |
||
| 483 | } |
||
| 484 | |||
| 485 | // Lets make sure we get a nice new spiffy clean $package to work with. Otherwise we get PAIN! |
||
| 486 | unset($package); |
||
| 487 | |||
| 488 | foreach ($context['package_list'] as $ps_id => $packageSection) |
||
| 489 | { |
||
| 490 | foreach ($packageSection['items'] as $i => $package) |
||
| 491 | { |
||
| 492 | if ($package['count'] == 0 || isset($package['can_install'])) |
||
| 493 | continue; |
||
| 494 | |||
| 495 | $context['package_list'][$ps_id]['items'][$i]['can_install'] = false; |
||
| 496 | |||
| 497 | $packageInfo = getPackageInfo($url . '/' . $package['filename']); |
||
| 498 | if (is_array($packageInfo) && $packageInfo['xml']->exists('install')) |
||
| 499 | { |
||
| 500 | $installs = $packageInfo['xml']->set('install'); |
||
| 501 | foreach ($installs as $install) |
||
| 502 | { |
||
| 503 | if (!$install->exists('@for') || matchPackageVersion($the_version, $install->fetch('@for'))) |
||
| 504 | { |
||
| 505 | // Okay, this one is good to go. |
||
| 506 | $context['package_list'][$ps_id]['items'][$i]['can_install'] = true; |
||
| 507 | break; |
||
| 508 | } |
||
| 509 | |||
| 510 | // no install found for this version, lets see if one exists for another |
||
| 511 | if ($context['package_list'][$ps_id]['items'][$i]['can_install'] === false && $install->exists('@for')) |
||
| 512 | { |
||
| 513 | $reset = true; |
||
| 514 | |||
| 515 | // Get the highest install version that is available from the package |
||
| 516 | foreach ($installs as $install) |
||
| 517 | { |
||
| 518 | $context['package_list'][$ps_id]['items'][$i]['can_emulate_install'] = matchHighestPackageVersion($install->fetch('@for'), $reset, $the_version); |
||
| 519 | $reset = false; |
||
| 520 | } |
||
| 521 | } |
||
| 522 | } |
||
| 523 | } |
||
| 524 | } |
||
| 525 | } |
||
| 526 | } |
||
| 527 | |||
| 785 | ?> |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.