/** * Create a list of the files in the torrent and their sizes as well as the total torrent size * * @return array with a list of files and file sizes */ public function file_list() { if (empty($this->Dec)) { return false; } $InfoDict =& $this->Dec['info']; if (!isset($InfoDict['files'])) { // Single-file torrent $this->Size = Int64::is_int($InfoDict['length']) ? Int64::get($InfoDict['length']) : $InfoDict['length']; $Name = isset($InfoDict['name.utf-8']) ? $InfoDict['name.utf-8'] : $InfoDict['name']; $this->Files[] = array($this->Size, $Name); } else { if (isset($InfoDict['path.utf-8']['files'][0])) { $this->PathKey = 'path.utf-8'; } foreach ($InfoDict['files'] as $File) { $TmpPath = array(); foreach ($File[$this->PathKey] as $SubPath) { $TmpPath[] = $SubPath; } $CurSize = Int64::is_int($File['length']) ? Int64::get($File['length']) : $File['length']; $this->Files[] = array($CurSize, implode('/', $TmpPath)); $this->Size += $CurSize; } uasort($this->Files, function ($a, $b) { return strnatcasecmp($a[1], $b[1]); }); } return array($this->Size, $this->Files); }
/** * Internal encoding function that does the actual job * * @return bencoded string */ private function _benc() { if (!is_array($this->Data)) { if (Int64::is_int($this->Data)) { // Integer return 'i' . Int64::get($this->Data) . 'e'; } if ($this->Data === true) { // Empty dictionary return 'de'; } return strlen($this->Data) . ':' . $this->Data; // String } if (empty($this->Data) || Int64::is_int(key($this->Data))) { $IsDict = false; } else { $IsDict = true; ksort($this->Data); // Dictionaries must be sorted } $Ret = $IsDict ? 'd' : 'l'; foreach ($this->Data as $Key => $Value) { if ($IsDict) { $Ret .= strlen($Key) . ':' . $Key; } $this->Data = $Value; $Ret .= $this->_benc(); } return $Ret . 'e'; }
/** * Convert everything to the correct data types and optionally escape strings * * @param bool $Escape whether to escape the textual data * @param mixed $Data decoded data or false to use the $Dec property * @return decoded data with more useful data types */ public function dump($Escape = true, $Data = false) { if ($Data === false) { $Data = $this->Dec; } if (Int64::is_int($Data)) { return Int64::get($Data); } if (is_bool($Data)) { return array(); } if (is_array($Data)) { $Output = array(); foreach ($Data as $Key => $Val) { $Output[$Key] = $this->dump($Escape, $Val); } return $Output; } return $Escape ? htmlentities($Data) : $Data; }