346 lines
		
	
	
		
			8.8 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			346 lines
		
	
	
		
			8.8 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
| <?php
 | |
| 
 | |
| namespace ManiaControl\ManiaExchange;
 | |
| 
 | |
| use ManiaControl\ManiaControl;
 | |
| use ManiaControl\Maps\Map;
 | |
| use ManiaControl\Maps\MapManager;
 | |
| 
 | |
| /**
 | |
|  * Mania Exchange Info Searcher Class
 | |
|  *
 | |
|  * @author steeffeen & kremsy
 | |
|  */
 | |
| class ManiaExchangeManager {
 | |
| 	/**
 | |
| 	 * Constants
 | |
| 	 */
 | |
| 	//Search others
 | |
| 	const SEARCH_ORDER_NONE               = -1;
 | |
| 	const SEARCH_ORDER_TRACK_NAME         = 0;
 | |
| 	const SEARCH_ORDER_AUTHOR             = 1;
 | |
| 	const SEARCH_ORDER_UPLOADED_NEWEST    = 2;
 | |
| 	const SEARCH_ORDER_UPLOADED_OLDEST    = 3;
 | |
| 	const SEARCH_ORDER_UPDATED_NEWEST     = 4;
 | |
| 	const SEARCH_ORDER_UPDATED_OLDEST     = 5;
 | |
| 	const SEARCH_ORDER_ACTIVITY_LATEST    = 6;
 | |
| 	const SEARCH_ORDER_ACTIVITY_OLDEST    = 7;
 | |
| 	const SEARCH_ORDER_AWARDS_MOST        = 8;
 | |
| 	const SEARCH_ORDER_AWARDS_LEAST       = 9;
 | |
| 	const SEARCH_ORDER_COMMENTS_MOST      = 10;
 | |
| 	const SEARCH_ORDER_COMMENTS_LEAST     = 11;
 | |
| 	const SEARCH_ORDER_DIFFICULTY_EASIEST = 12;
 | |
| 	const SEARCH_ORDER_DIFFICULTY_HARDEST = 13;
 | |
| 	const SEARCH_ORDER_LENGHT_SHORTEST    = 14;
 | |
| 	const SEARCH_ORDER_LENGHT_LONGEST     = 15;
 | |
| 
 | |
| 	//Maximum Maps per request
 | |
| 	const MAPS_PER_MX_FETCH = 50;
 | |
| 
 | |
| 	/**
 | |
| 	 * Private Propertieswc
 | |
| 	 */
 | |
| 	private $maniaControl = null;
 | |
| 	private $mxIdUidVector = array();
 | |
| 
 | |
| 	/**
 | |
| 	 * Construct map manager
 | |
| 	 *
 | |
| 	 * @param \ManiaControl\ManiaControl $maniaControl
 | |
| 	 */
 | |
| 	public function __construct(ManiaControl $maniaControl) {
 | |
| 		$this->maniaControl = $maniaControl;
 | |
| 	}
 | |
| 
 | |
| 
 | |
| 	/**
 | |
| 	 * Store Map Info from MX and store the mxid in the database and the mx info in the map object
 | |
| 	 *
 | |
| 	 * @param $mxMapInfos
 | |
| 	 */
 | |
| 	public function updateMapObjectsWithManiaExchangeIds($mxMapInfos) {
 | |
| 		$mysqli = $this->maniaControl->database->mysqli;
 | |
| 		// Save map data
 | |
| 		$saveMapQuery     = "UPDATE `" . MapManager::TABLE_MAPS . "`
 | |
| 				SET `mxid` = ?
 | |
| 				WHERE `uid` = ?;";
 | |
| 		$saveMapStatement = $mysqli->prepare($saveMapQuery);
 | |
| 		if ($mysqli->error) {
 | |
| 			trigger_error($mysqli->error);
 | |
| 			return;
 | |
| 		}
 | |
| 		foreach($mxMapInfos as $mxMapInfo) {
 | |
| 			/** @var MXMapInfo $mxMapInfo */
 | |
| 			$saveMapStatement->bind_param('is', $mxMapInfo->id, $mxMapInfo->uid);
 | |
| 			$saveMapStatement->execute();
 | |
| 			if ($saveMapStatement->error) {
 | |
| 				trigger_error($saveMapStatement->error);
 | |
| 			}
 | |
| 
 | |
| 			//Take the uid out of the vektor
 | |
| 			if (isset($this->mxIdUidVector[$mxMapInfo->id])) {
 | |
| 				$uid = $this->mxIdUidVector[$mxMapInfo->id];
 | |
| 			} else {
 | |
| 				$uid = $mxMapInfo->uid;
 | |
| 			}
 | |
| 			$map = $this->maniaControl->mapManager->getMapByUid($uid);
 | |
| 
 | |
| 			/** @var Map $map */
 | |
| 			$map->mx = $mxMapInfo;
 | |
| 		}
 | |
| 		$saveMapStatement->close();
 | |
| 	}
 | |
| 
 | |
| 	/**
 | |
| 	 * Unset Map by Mx Id
 | |
| 	 *
 | |
| 	 * @param $mxId
 | |
| 	 */
 | |
| 	public function unsetMap($mxId) {
 | |
| 		unset($this->mxIdUidVector[$mxId]);
 | |
| 	}
 | |
| 
 | |
| 	/**
 | |
| 	 * Fetch Map Information from Mania Exchange
 | |
| 	 *
 | |
| 	 * @param null $map
 | |
| 	 */
 | |
| 	public function fetchManiaExchangeMapInformations($map = null) {
 | |
| 		if (!$map) {
 | |
| 			//Fetch Informations for whole Maplist
 | |
| 			$maps = $this->maniaControl->mapManager->getMaps();
 | |
| 		} else {
 | |
| 			//Fetch Information for a single map
 | |
| 			$maps[] = $map;
 | |
| 		}
 | |
| 
 | |
| 		$mysqli      = $this->maniaControl->database->mysqli;
 | |
| 		$mapIdString = '';
 | |
| 
 | |
| 		// Fetch mx ids
 | |
| 		$fetchMapQuery     = "SELECT `mxid`, `changed`  FROM `" . MapManager::TABLE_MAPS . "`
 | |
| 				WHERE `index` = ?;";
 | |
| 		$fetchMapStatement = $mysqli->prepare($fetchMapQuery);
 | |
| 		if ($mysqli->error) {
 | |
| 			trigger_error($mysqli->error);
 | |
| 			return;
 | |
| 		}
 | |
| 
 | |
| 		$id = 0;
 | |
| 		foreach($maps as $map) {
 | |
| 			/** @var Map $map */
 | |
| 			$fetchMapStatement->bind_param('i', $map->index);
 | |
| 			$fetchMapStatement->execute();
 | |
| 			if ($fetchMapStatement->error) {
 | |
| 				trigger_error($fetchMapStatement->error);
 | |
| 				continue;
 | |
| 			}
 | |
| 			$fetchMapStatement->store_result();
 | |
| 			$fetchMapStatement->bind_result($mxId, $changed);
 | |
| 			$fetchMapStatement->fetch();
 | |
| 			$fetchMapStatement->free_result();
 | |
| 
 | |
| 			//Set changed time into the map object
 | |
| 			$map->lastUpdate = strtotime($changed);
 | |
| 
 | |
| 			if ($mxId != 0) {
 | |
| 				$appendString = $mxId . ',';
 | |
| 				//Set the mx id to the mxidmapvektor
 | |
| 				$this->mxIdUidVector[$mxId] = $map->uid;
 | |
| 			} else {
 | |
| 				$appendString = $map->uid . ',';
 | |
| 			}
 | |
| 
 | |
| 			$id++;
 | |
| 
 | |
| 			//If Max Maplimit is reached, or string gets too long send the request
 | |
| 			if ($id % self::MAPS_PER_MX_FETCH == 0) {
 | |
| 				$mapIdString = substr($mapIdString, 0, -1);
 | |
| 				$maps        = $this->getMaplistByMixedUidIdString($mapIdString);
 | |
| 				$this->updateMapObjectsWithManiaExchangeIds($maps);
 | |
| 				$mapIdString = '';
 | |
| 			}
 | |
| 
 | |
| 			$mapIdString .= $appendString;
 | |
| 		}
 | |
| 
 | |
| 		if ($mapIdString != '') {
 | |
| 			$mapIdString = substr($mapIdString, 0, -1);
 | |
| 			$maps        = $this->getMaplistByMixedUidIdString($mapIdString);
 | |
| 			$this->updateMapObjectsWithManiaExchangeIds($maps);
 | |
| 		}
 | |
| 
 | |
| 		$fetchMapStatement->close();
 | |
| 	}
 | |
| 
 | |
| 
 | |
| 	/**
 | |
| 	 * Get the Whole Maplist from MX by Mixed Uid and Id String fetch
 | |
| 	 *
 | |
| 	 * @param $string
 | |
| 	 * @return array|null
 | |
| 	 */
 | |
| 	public function getMaplistByMixedUidIdString($string) {
 | |
| 		// Get Title Id
 | |
| 		$titleId     = $this->maniaControl->server->titleId;
 | |
| 		$titlePrefix = strtolower(substr($titleId, 0, 2));
 | |
| 
 | |
| 		// compile search URL
 | |
| 		$url = 'http://api.mania-exchange.com/' . $titlePrefix . '/maps/?ids=' . $string;
 | |
| 		// $mapInfo = FileUtil::loadFile($url, "application/json"); //TODO use mc fileutil
 | |
| 		$mapInfo = $this->get_file($url);
 | |
| 
 | |
| 
 | |
| 		if ($mapInfo === false) {
 | |
| 			$this->error = 'Connection or response error on ' . $url;
 | |
| 			return array();
 | |
| 		} elseif ($mapInfo === -1) {
 | |
| 			$this->error = 'Timed out while reading data from ' . $url;
 | |
| 			return array();
 | |
| 		} elseif ($mapInfo == '') {
 | |
| 			if (empty($maps)) {
 | |
| 				$this->error = 'No data returned from ' . $url;
 | |
| 				return array();
 | |
| 			}
 | |
| 		}
 | |
| 
 | |
| 		$mxMapList = json_decode($mapInfo);
 | |
| 		if ($mxMapList === null) {
 | |
| 			trigger_error('Cannot decode searched JSON data from ' . $url);
 | |
| 			return null;
 | |
| 		}
 | |
| 
 | |
| 		$maps = array();
 | |
| 		foreach($mxMapList as $map) {
 | |
| 			if (!empty($map)) {
 | |
| 				array_push($maps, new MXMapInfo($titlePrefix, $map));
 | |
| 			}
 | |
| 		}
 | |
| 		return $maps;
 | |
| 	}
 | |
| 
 | |
| 	/**
 | |
| 	 * Gets a Maplist from Mania Exchange
 | |
| 	 *
 | |
| 	 * @param string $name
 | |
| 	 * @param string $author
 | |
| 	 * @param string $env
 | |
| 	 * @param int    $maxMapsReturned
 | |
| 	 * @param int    $searchOrder
 | |
| 	 * @return array null
 | |
| 	 */
 | |
| 	public function getMaps($name = '', $author = '', $env = '', $maxMapsReturned = 100, $searchOrder = self::SEARCH_ORDER_UPDATED_NEWEST) {
 | |
| 		// Get Title Id
 | |
| 		$titleId     = $this->maniaControl->server->titleId;
 | |
| 		$titlePrefix = strtolower(substr($titleId, 0, 2));
 | |
| 
 | |
| 		// Get MapTypes
 | |
| 		$scriptInfos = $this->maniaControl->client->getModeScriptInfo();
 | |
| 
 | |
| 		$mapTypes = $scriptInfos->compatibleMapTypes;
 | |
| 
 | |
| 		// compile search URL
 | |
| 		$url = 'http://' . $titlePrefix . '.mania-exchange.com/tracksearch?api=on';
 | |
| 
 | |
| 		if ($env != '') {
 | |
| 			$url .= '&environments=' . $this->getEnvironment($env);
 | |
| 		}
 | |
| 		if ($name != '') {
 | |
| 			$url .= '&trackname=' . str_replace(" ", "%20", $name);
 | |
| 		}
 | |
| 		if ($author != '') {
 | |
| 			$url .= '&author=' . $author;
 | |
| 		}
 | |
| 
 | |
| 		$url .= '&priord=' . $searchOrder;
 | |
| 		$url .= '&limit=' . $maxMapsReturned;
 | |
| 		$url .= '&mtype=' . $mapTypes;
 | |
| 
 | |
| 		// $mapInfo = FileUtil::loadFile($url, "application/json"); //TODO use mc fileutil
 | |
| 		$mapInfo = $this->get_file($url);
 | |
| 
 | |
| 		if ($mapInfo === false) {
 | |
| 			$this->error = 'Connection or response error on ' . $url;
 | |
| 			return array();
 | |
| 		} elseif ($mapInfo === -1) {
 | |
| 			$this->error = 'Timed out while reading data from ' . $url;
 | |
| 			return array();
 | |
| 		} elseif ($mapInfo == '') {
 | |
| 			if (empty($maps)) {
 | |
| 				$this->error = 'No data returned from ' . $url;
 | |
| 				return array();
 | |
| 			}
 | |
| 		}
 | |
| 
 | |
| 		$mxMapList = json_decode($mapInfo);
 | |
| 		if ($mxMapList === null) {
 | |
| 			trigger_error('Cannot decode searched JSON data from ' . $url);
 | |
| 			return null;
 | |
| 		}
 | |
| 
 | |
| 		$maps = array();
 | |
| 		foreach($mxMapList as $map) {
 | |
| 			if (!empty($map)) {
 | |
| 				array_push($maps, new MXMapInfo($titlePrefix, $map));
 | |
| 			}
 | |
| 		}
 | |
| 		return $maps;
 | |
| 	}
 | |
| 
 | |
| 	/**
 | |
| 	 * Loads an file
 | |
| 	 *
 | |
| 	 * @param $url
 | |
| 	 * @return bool|int|string
 | |
| 	 */
 | |
| 	private function get_file($url) {
 | |
| 		$url   = parse_url($url);
 | |
| 		$port  = isset($url['port']) ? $url['port'] : 80;
 | |
| 		$query = isset($url['query']) ? "?" . $url['query'] : "";
 | |
| 
 | |
| 		$fp = @fsockopen($url['host'], $port, $errno, $errstr, 4);
 | |
| 		if (!$fp) {
 | |
| 			return false;
 | |
| 		}
 | |
| 
 | |
| 		fwrite($fp, 'GET ' . $url['path'] . $query . " HTTP/1.0\r\n" . 'Host: ' . $url['host'] . "\r\n" . 'Content-Type: application/json' . "\r\n" . 'User-Agent: ManiaControl v' . ManiaControl::VERSION . "\r\n\r\n");
 | |
| 		stream_set_timeout($fp, 2);
 | |
| 		$res               = '';
 | |
| 		$info['timed_out'] = false;
 | |
| 		while(!feof($fp) && !$info['timed_out']) {
 | |
| 			$res .= fread($fp, 512);
 | |
| 			$info = stream_get_meta_data($fp);
 | |
| 		}
 | |
| 		fclose($fp);
 | |
| 
 | |
| 		if ($info['timed_out']) {
 | |
| 			return -1;
 | |
| 		} else {
 | |
| 			if (substr($res, 9, 3) != '200') {
 | |
| 				return false;
 | |
| 			}
 | |
| 			$page = explode("\r\n\r\n", $res, 2);
 | |
| 			return trim($page[1]);
 | |
| 		}
 | |
| 	} // get_file
 | |
| 
 | |
| 	/**
 | |
| 	 * Gets the Current Environemnt by String
 | |
| 	 *
 | |
| 	 * @param $env
 | |
| 	 * @return int
 | |
| 	 */
 | |
| 	private function getEnvironment($env) {
 | |
| 		switch($env) {
 | |
| 			case 'TMCanyon':
 | |
| 			case 'SMStorm':
 | |
| 				return 1;
 | |
| 			case 'TMStadium':
 | |
| 				return 2;
 | |
| 			case 'TMValley':
 | |
| 				return 3;
 | |
| 			default:
 | |
| 				return -1;
 | |
| 		}
 | |
| 	}
 | |
| } |