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