<commit_before><?php
namespace wcf\system\package;
use wcf\data\package\update\server\PackageUpdateServer;
use wcf\data\package\update\server\PackageUpdateServerEditor;
use wcf\data\package\Package;
use wcf\system\cache\builder\PackageUpdateCacheBuilder;
use wcf\system\database\util\PreparedStatementConditionBuilder;
use wcf\system\exception\HTTPUnauthorizedException;
use wcf\system\exception\SystemException;
use wcf\system\io\RemoteFile;
use wcf\system\package\validation\PackageValidationException;
use wcf\system\SingletonFactory;
use wcf\system\WCF;
use wcf\util\HTTPRequest;
use wcf\util\JSON;
use wcf\util\XML;

/**
 * Provides functions to manage package updates.
 * 
 * @author	<NAME>
 * @copyright	2001-2019 WoltLab GmbH
 * @license	GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
 * @package	WoltLabSuite\Core\System\Package
 */
class PackageUpdateDispatcher extends SingletonFactory {
	protected $hasAuthCode = false;
	protected $purchasedVersions = [
		'woltlab' => [],
		'pluginstore' => []
	];
	
	/**
	 * Refreshes the package database.
	 * 
	 * @param	integer[]		$packageUpdateServerIDs
	 * @param	boolean			$ignoreCache
	 */
	public function refreshPackageDatabase(array $packageUpdateServerIDs = [], $ignoreCache = false) {
		// get update server data
		$tmp = PackageUpdateServer::getActiveUpdateServers($packageUpdateServerIDs);
		
		// loop servers
		$updateServers = [];
		$foundWoltLabServer = false;
		$requirePurchasedVersions = false;
		foreach ($tmp as $updateServer) {
			if ($ignoreCache || $updateServer->lastUpdateTime < TIME_NOW - 600) {
				if (preg_match('~^https?://(?:update|store)\.woltlab\.com\/~', $updateServer->serverURL)) {
					$requirePurchasedVersions = true;
					
					// move a woltlab.com update server to the front of the queue to probe for SSL support
					if (!$foundWoltLabServer) {
						array_unshift($updateServers, $updateServer);
						$foundWoltLabServer = true;
						
						continue;
					}
				}
				
				$updateServers[] = $updateServer;
			}
		}
		
		if ($requirePurchasedVersions && PACKAGE_SERVER_AUTH_CODE) {
			$this->getPurchasedVersions();
		}
		
		// loop servers
		$refreshedPackageLists = false;
		foreach ($updateServers as $updateServer) {
			$errorMessage = '';
			
			try {
				$this->getPackageUpdateXML($updateServer);
				$refreshedPackageLists = true;
			}
			catch (SystemException $e) {
				$errorMessage = $e->getMessage();
			}
			catch (PackageUpdateUnauthorizedException $e) {
				$reply = $e->getRequest()->getReply();
				list($errorMessage) = reset($reply['httpHeaders']);
			}<commit_msg>Display a somewhat meaningful message when the package list inaccessible<commit_after><?php
namespace wcf\system\package;
use wcf\data\package\update\server\PackageUpdateServer;
use wcf\data\package\update\server\PackageUpdateServerEditor;
use wcf\data\package\Package;
use wcf\system\cache\builder\PackageUpdateCacheBuilder;
use wcf\system\database\util\PreparedStatementConditionBuilder;
use wcf\system\exception\HTTPUnauthorizedException;
use wcf\system\exception\SystemException;
use wcf\system\io\RemoteFile;
use wcf\system\package\validation\PackageValidationException;
use wcf\system\SingletonFactory;
use wcf\system\WCF;
use wcf\util\HTTPRequest;
use wcf\util\JSON;
use wcf\util\StringUtil;
use wcf\util\XML;

/**
 * Provides functions to manage package updates.
 * 
 * @author	<NAME>
 * @copyright	2001-2019 WoltLab GmbH
 * @license	GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
 * @package	WoltLabSuite\Core\System\Package
 */
class PackageUpdateDispatcher extends SingletonFactory {
	protected $hasAuthCode = false;
	protected $purchasedVersions = [
		'woltlab' => [],
		'pluginstore' => []
	];
	
	/**
	 * Refreshes the package database.
	 * 
	 * @param	integer[]		$packageUpdateServerIDs
	 * @param	boolean			$ignoreCache
	 */
	public function refreshPackageDatabase(array $packageUpdateServerIDs = [], $ignoreCache = false) {
		// get update server data
		$tmp = PackageUpdateServer::getActiveUpdateServers($packageUpdateServerIDs);
		
		// loop servers
		$updateServers = [];
		$foundWoltLabServer = false;
		$requirePurchasedVersions = false;
		foreach ($tmp as $updateServer) {
			if ($ignoreCache || $updateServer->lastUpdateTime < TIME_NOW - 600) {
				if (preg_match('~^https?://(?:update|store)\.woltlab\.com\/~', $updateServer->serverURL)) {
					$requirePurchasedVersions = true;
					
					// move a woltlab.com update server to the front of the queue to probe for SSL support
					if (!$foundWoltLabServer) {
						array_unshift($updateServers, $updateServer);
						$foundWoltLabServer = true;
						
						continue;
					}
				}
				
				$updateServers[] = $updateServer;
			}
		}
		
		if ($requirePurchasedVersions && PACKAGE_SERVER_AUTH_CODE) {
			$this->getPurchasedVersions();
		}
		
		// loop servers
		$refreshedPackageLists = false;
		foreach ($updateServers as $updateServer) {
			$errorMessage = '';
			
			try {
				$this->getPackageUpdateXML($updateServer);
				$refreshedPackageLists = true;
			}
			catch (SystemException $e) {
				$errorMessage = $e->getMessage();
			}
			catch (PackageUpdateUnauthorizedException $e) {
				$body = $e->getRequest()->getReply()['body'];
				
				// Try to find the page <title>.
				if (preg_match('~<title>(?<title>.*?)</title>~', $body, $matches)) {
					$errorMessage = $matches['title'];
				}
				else {
					$errorMessage = $body;
				}
				
				$errorMessage = mb_substr(StringUtil::trim(strip_tags($errorMessage)), 0, 65000);
			}
{'fraction_non_alphanumeric': 0.12733477011494254, 'fraction_numerical': 0.005028735632183908, 'mean_word_length': 4.095150960658738, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5219826', 'n_tokens_mistral': 2051, 'n_tokens_neox': 1717, 'n_words': 348}