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