public function onResponseSent() { if (!$this->settings->hasSetting("debug_log") or !$this->environment->request()) { return; } $path = $this->environment->request()->path(); if (count($path) > 0 and strcasecmp($path[0], "debug") == 0) { return; } $log = $this->settings->setting("debug_log"); $handle = @fopen($log, "a"); if (!$handle) { Logging::logError("Could not write to log file: " . $log); return; } $trace = Logging::getTrace(); try { foreach ($trace as $d) { fwrite($handle, Util::toString($d)); } fclose($handle); } catch (Exception $e) { Logging::logError("Could not write to log file: " . $log); Logging::logException($e); } }
public function testToStringNotReadable() { $handle = fopen(PSX_PATH_CACHE . '/StreamUtilTest.txt', 'w'); fwrite($handle, 'foobar'); $stream = new TempStream($handle); $this->assertFalse($stream->isReadable()); $this->assertEquals(6, $stream->tell()); $this->assertEquals('', Util::toString($stream)); $this->assertEquals(6, $stream->tell()); }
/** Create a User object from a SimpleXMLElement. * * @param SimpleXMLElement $xml A SimpleXMLElement. * @return User A User object. * * @static * @access public * @internal */ public static function fromSimpleXMLElement(SimpleXMLElement $xml) { $images = array(); foreach ($xml->image as $image) { $images[Util::toImageType($image['size'])] = Util::toString($image); } return new User(Util::toString($xml->name), Util::toString($xml->realname), Util::toString($xml->url), Util::toString($xml->lang), Util::toString($xml->country), Util::toInteger($xml->age), Util::toString($xml->gender), Util::toInteger($xml->subscriber), Util::toInteger($xml->playcount), Util::toInteger($xml->playlists), $images, $xml->recenttrack ? Track::fromSimpleXMLElement($xml->recenttrack) : null, Util::toFloat($xml->match), Util::toInteger($xml->weight), Util::toInteger($xml->registered['unixtime'])); }
/** Create a Track object from a SimpleXMLElement. * * @param SimpleXMLElement $xml A SimpleXMLElement. * @return Track A Track object. * * @static * @access public * @internal */ public static function fromSimpleXMLElement(SimpleXMLElement $xml) { $images = array(); $topTags = array(); if (count($xml->image) > 1) { foreach ($xml->image as $image) { $images[Util::toImageType($image['size'])] = Util::toString($image); } } else { $images[Media::IMAGE_UNKNOWN] = Util::toString($xml->image); } if ($xml->toptags) { foreach ($xml->toptags->children() as $tag) { $topTags[] = Tag::fromSimpleXMLElement($tag); } } if ($xml->artist) { if ($xml->artist->name && $xml->artist->mbid && $xml->artist->url) { $artist = new Artist(Util::toString($xml->artist->name), Util::toString($xml->artist->mbid), Util::toString($xml->artist->url), array(), 0, 0, 0, array(), array(), '', 0.0); } else { $artist = Util::toString($xml->artist); } } else { if ($xml->creator) { $artist = Util::toString($xml->creator); } else { $artist = ''; } } if ($xml->name) { $name = Util::toString($xml->name); } else { if ($xml->title) { $name = Util::toString($xml->title); } else { $name = ''; } } // TODO: <extension application="http://www.last.fm"> return new Track($artist, Util::toString($xml->album), $name, Util::toString($xml->mbid), Util::toString($xml->url), $images, Util::toInteger($xml->listeners), Util::toInteger($xml->playcount), Util::toInteger($xml->duration), $topTags, Util::toInteger($xml->id), Util::toString($xml->location), Util::toBoolean($xml->streamable), Util::toBoolean($xml->streamable['fulltrack']), $xml->wiki, Util::toTimestamp($xml->date)); }
/** Send a query using a specified request-method. * * @param string $query Query to send. (Required) * @param string $requestMethod Request-method for calling (defaults to 'GET'). (Optional) * @return SimpleXMLElement A SimpleXMLElement object. * * @access private * @internal */ protected function internalCall($params, $requestMethod = 'GET') { /* Create caching hash. */ $hash = Cache::createHash($params); /* Check if response is cached. */ if ($this->cache != null && $this->cache->contains($hash) && !$this->cache->isExpired($hash)) { /* Get cached response. */ $response = $this->cache->load($hash); } else { /* Build request query */ $query = http_build_query($params, '', '&'); /* Set request options. */ if ($requestMethod === 'POST') { curl_setopt($this->curl, CURLOPT_URL, self::API_URL); curl_setopt($this->curl, CURLOPT_POST, 1); curl_setopt($this->curl, CURLOPT_POSTFIELDS, $query); } else { curl_setopt($this->curl, CURLOPT_URL, self::API_URL . '?' . $query); curl_setopt($this->curl, CURLOPT_POST, 0); } /* Clear response headers. */ $this->headers = array(); /* Get response. */ $response = curl_exec($this->curl); /* Cache it. */ if ($this->cache != null) { if (array_key_exists('Expires', $this->headers)) { $this->cache->store($hash, $response, strtotime($this->headers['Expires'])); } else { $expiration = $this->cache->getPolicy()->getExpirationTime($params); if ($expiration > 0) { $this->cache->store($hash, $response, time() + $expiration); } } } } /* Create SimpleXMLElement from response. */ $response = new SimpleXMLElement($response); /* Return response or throw an error. */ if (Util::toString($response['status']) === 'ok') { if ($response->children()->{0}) { return $response->children()->{0}; } } else { throw new Error(Util::toString($response->error), Util::toInteger($response->error['code'])); } }
/** Create a Location object from a SimpleXMLElement. * * @param SimpleXMLElement $xml A SimpleXMLElement. * @return Location A Location object. * * @static * @access public * @internal */ public static function fromSimpleXMLElement(SimpleXMLElement $xml) { $geo = $xml->children('http://www.w3.org/2003/01/geo/wgs84_pos#'); return new Location(Util::toString($xml->city), Util::toString($xml->country), Util::toString($xml->street), Util::toString($xml->postalcode), $geo->point ? new Point(Util::toFloat($geo->point->lat), Util::toFloat($geo->point->long)) : null, Util::toString($xml->timezome)); }
/** Create a Event object from a SimpleXMLElement. * * @param SimpleXMLElement $xml A SimpleXMLElement. * @return Event A Event object. * * @static * @access public * @internal */ public static function fromSimpleXMLElement(SimpleXMLElement $xml) { $artists = array(); $images = array(); if ($xml->artists) { foreach ($xml->artists->artist as $artist) { $artists[] = Util::toString($artist); } $artists['headliner'] = Util::toString($xml->artists->headliner); } if ($xml->image) { foreach ($xml->image as $image) { $images[Util::toImageType($image['size'])] = Util::toString($image); } } return new Event(Util::toInteger($xml->id), Util::toString($xml->title), $artists, $xml->venue ? Venue::fromSimpleXMLElement($xml->venue) : null, Util::toTimestamp($xml->startDate), Util::toString($xml->description), $images, Util::toString($xml->url), Util::toInteger($xml->attendance), Util::toInteger($xml->reviews), Util::toString($xml->tag)); }
/** Fetch an unauthorized request token for an API account. This is step 2 of the authentication process for desktop applications. Web applications do not need to use this service. * * @return string A 32-character ASCII hexadecimal MD5 hash. * * @static * @access public * @throws Error */ public static function getToken() { $xml = CallerFactory::getDefaultCaller()->signedCall('auth.getToken'); return Util::toString($xml); }
/** Create a Venue object from a SimpleXMLElement. * * @param SimpleXMLElement $xml A SimpleXMLElement. * @return Venue A Venue object. * * @static * @access public * @internal */ public static function fromSimpleXMLElement(SimpleXMLElement $xml) { return new Venue(Util::toString($xml->name), Location::fromSimpleXMLElement($xml->location), Util::toString($xml->url)); }
/** Create an Artist object from a SimpleXMLElement object. * * @param SimpleXMLElement $xml A SimpleXMLElement object. * @return Artist An Artist object. * * @static * @access public * @internal */ public static function fromSimpleXMLElement(SimpleXMLElement $xml) { $images = array(); $tags = array(); $similar = array(); /* NOTE: image, image_small... this sucks! */ if ($xml->image) { if (count($xml->image) > 1) { foreach ($xml->image as $image) { $images[Util::toImageType($image['size'])] = Util::toString($image); } } else { $images[Media::IMAGE_LARGE] = Util::toString($image); } } if ($xml->image_small) { $images[Media::IMAGE_SMALL] = Util::toString($xml->image_small); } if ($xml->tags) { foreach ($xml->tags->children() as $tag) { $tags[] = Tag::fromSimpleXMLElement($tag); } } if ($xml->similar) { foreach ($xml->similar->children() as $artist) { $similar[] = Artist::fromSimpleXMLElement($artist); } } return new Artist(Util::toString($xml->name), Util::toString($xml->mbid), Util::toString($xml->url), $images, Util::toBoolean($xml->streamable), Util::toInteger($xml->listeners), Util::toInteger($xml->playcount), $tags, $similar, $xml->bio ? Util::toString($xml->bio->summary) : "", Util::toFloat($xml->match)); }
/** Send a query using a specified request-method. * * @param string $query Query to send. (Required) * @param string $requestMethod Request-method for calling (defaults to 'GET'). (Optional) * @return SimpleXMLElement A SimpleXMLElement object. * * @access private * @internal */ protected function internalCall($params, $requestMethod = 'GET') { /* Create caching hash. */ $hash = Cache::createHash($params); /* Check if response is cached. */ if ($this->cache != null && $this->cache->contains($hash) && !$this->cache->isExpired($hash)) { /* Get cached response. */ $response = $this->cache->load($hash); } else { /* Build request query. */ $query = http_build_query($params, '', '&'); /* Extract URL information. */ $info = parse_url(self::API_URL); /* TODO: Accept-Encoding: deflate, gzip */ /* Set request data. */ if ($requestMethod === 'POST') { $data = "POST " . $info['path'] . " HTTP/1.1\r\n"; $data .= "Host: " . $info['host'] . "\r\n"; $data .= "User-Agent: PHP last.fm API (PHP/" . phpversion() . ")\r\n"; $data .= "Content-Type: application/x-www-form-urlencoded\r\n"; $data .= "Content-Length: " . strlen($query) . "\r\n"; $data .= "Connection: Close\r\n\r\n"; $data .= $query; } else { $data = "GET " . $info['path'] . "?" . $query . " HTTP/1.1\r\n"; $data .= "Host: " . $info['host'] . "\r\n"; $data .= "User-Agent: PHP last.fm API (PHP/" . phpversion() . ")\r\n"; $data .= "Connection: Close\r\n\r\n"; } /* Open socket. */ $socket = fsockopen($info['host'], 80); /* Write request. */ fwrite($socket, $data); /* Clear response headers. */ $this->headers = array(); /* Read headers. */ while (($line = fgets($socket)) !== false && $line != "\r\n") { $this->header($line); } /* Read response. */ $response = ""; while (($line = fgets($socket)) !== false && $line != "\r\n") { $response .= $line; } /* Close socket. */ fclose($socket); /* Cache it. */ if ($this->cache != null) { if (array_key_exists('Expires', $this->headers)) { $this->cache->store($hash, $response, strtotime($this->headers['Expires'])); } else { $expiration = $this->cache->getPolicy()->getExpirationTime($params); if ($expiration > 0) { $this->cache->store($hash, $response, time() + $expiration); } } } } /* Create SimpleXMLElement from response. */ $response = new SimpleXMLElement($response); /* Return response or throw an error. */ if (Util::toString($response['status']) === 'ok') { if ($response->children()->{0}) { return $response->children()->{0}; } } else { throw new Error(Util::toString($response->error), Util::toInteger($response->error['code'])); } }
/** Create an Album object from a SimpleXMLElement object. * * @param SimpleXMLElement $xml A SimpleXMLElement object. * @return Album An Album object. * * @static * @access public * @internal */ public static function fromSimpleXMLElement(SimpleXMLElement $xml) { $images = array(); $topTags = array(); /* TODO: tagcount | library.getAlbums */ if ($xml->mbid) { $mbid = Util::toString($xml->mbid); } else { if ($xml['mbid']) { $mbid = Util::toString($xml['mbid']); } else { $mbid = ''; } } foreach ($xml->image as $image) { $images[Util::toImageType($image['size'])] = Util::toString($image); } if ($xml->toptags) { foreach ($xml->toptags->children() as $tag) { $topTags[] = Tag::fromSimpleXMLElement($tag); } } if ($xml->artist->name && $xml->artist->mbid && $xml->artist->url) { $artist = new Artist(Util::toString($xml->artist->name), Util::toString($xml->artist->mbid), Util::toString($xml->artist->url), array(), 0, 0, 0, array(), array(), '', 0.0); } else { if ($xml->artist && $xml->artist['mbid']) { $artist = new Artist(Util::toString($xml->artist), Util::toString($xml->artist['mbid']), '', array(), 0, 0, 0, array(), array(), '', 0.0); } else { $artist = Util::toString($xml->artist); } } return new Album($artist, Util::toString($xml->name), Util::toInteger($xml->id), $mbid, Util::toString($xml->url), $images, Util::toInteger($xml->listeners), Util::toInteger($xml->playcount), Util::toTimestamp($xml->releasedate), $topTags); }
/** Returns the image type value of a variable. * * @param mixed $var An object. * @return integer An image type. * * @static * @access public */ public static function toImageType($var) { switch (Util::toString($var)) { case 'small': return Media::IMAGE_SMALL; case 'medium': return Media::IMAGE_MEDIUM; case 'large': return Media::IMAGE_LARGE; case 'huge': return Media::IMAGE_HUGE; case 'extralarge': return Media::IMAGE_EXTRALARGE; case 'original': return Media::IMAGE_ORIGINAL; default: return Media::IMAGE_UNKNOWN; } }
/** Create a Shout object from a SimpleXMLElement. * * @param SimpleXMLElement $xml A SimpleXMLElement. * @return Venue A Venue object. * * @static * @access public * @internal */ public static function fromSimpleXMLElement(SimpleXMLElement $xml) { return new Shout(Util::toString($xml->auhtor), Util::toTimestamp($xml->date), Util::toString($xml->body)); }
/** Send a query using a specified request-method. * * @param string $query Query to send. (Required) * @param string $requestMethod Request-method for calling (defaults to 'GET'). (Optional) * @return SimpleXMLElement A SimpleXMLElement object. * * @access protected * @internal */ protected function internalCall($params, $requestMethod = 'GET') { /* Create caching hash. */ $hash = Cache::createHash($params); /* Check if response is cached. */ if ($this->cache != null && $this->cache->contains($hash) && !$this->cache->isExpired($hash)) { /* Get cached response. */ $response = $this->cache->load($hash); } else { /* Build request query. */ $query = http_build_str($params, '', '&'); /* Set request options. */ $options = array('useragent' => 'PHP last.fm API (PHP/' . phpversion() . ')'); /* Clear response headers. */ $this->headers = array(); /* Get response */ if ($requestMethod === 'POST') { $response = http_post_data(self::API_URL, $query, $options, $info); } else { $response = http_get(self::API_URL . '?' . $query, $options, $info); } $response = http_parse_message($response); foreach ($response->headers as $header => $value) { $this->headers[$header] = $value; } $response = $response->body; /* Cache it. */ if ($this->cache != null) { if (array_key_exists('Expires', $this->headers)) { $this->cache->store($hash, $response, strtotime($this->headers['Expires'])); } else { $expiration = $this->cache->getPolicy()->getExpirationTime($params); if ($expiration > 0) { $this->cache->store($hash, $response, time() + $expiration); } } } } /* Create SimpleXMLElement from response. */ $response = new SimpleXMLElement($response); /* Return response or throw an error. */ if (Util::toString($response['status']) === 'ok') { if ($response->children()->{0}) { return $response->children()->{0}; } } else { throw new Error(Util::toString($response->error), Util::toInteger($response->error['code'])); } }
/** Create a Playlist object from a SimpleXMLElement. * * @param SimpleXMLElement $xml A SimpleXMLElement object. * @return Playlist A Playlist object. * * @static * @access public * @internal */ public static function fromSimpleXMLElement(SimpleXMLElement $xml) { $tracks = array(); if (isset($xml->trackList)) { foreach ($xml->trackList->children() as $track) { $tracks[] = Track::fromSimpleXMLElement($track); } } return new Playlist(Util::toInteger($xml->id), Util::toString($xml->title), Util::toString($xml->description), Util::toTimestamp($xml->date), Util::toInteger($xml->size), Util::toInteger($xml->duration), Util::toInteger($xml->streamable), Util::toString($xml->creator), Util::toString($xml->url), $tracks); }
public function log() { Logging::logDebug("REQUEST: method=" . $this->method . ", path=" . Util::array2str($this->parts) . ", ip=" . $this->ip . ", params=" . Util::array2str($this->params) . ", data=" . Util::toString($this->data)); }
/** Create a Tag object from a SimpleXMLElement. * * @param SimpleXMLElement $xml A SimpleXMLElement. * @return Tag A Tag object. * * @static * @access public * @internal */ public static function fromSimpleXMLElement(SimpleXMLElement $xml) { return new Tag(Util::toString($xml->name), Util::toInteger($xml->count), Util::toString($xml->url)); }
private function getErrorResponse($err, $details, $data = NULL) { if (Logging::isDebug()) { Logging::logDebug("RESPONSE error " . Util::toString($err) . " " . Util::toString($details) . " " . Util::toString($data)); return array("code" => $err[0], "error" => $err[1], "details" => $details, "data" => $data, "trace" => Logging::getTrace()); } return array("code" => $err[0], "error" => $err[1], "details" => $details, "data" => $data); }
/** Create a Session object from a SimpleXMLElement. * * @param SimpleXMLElement $xml A SimpleXMLElement. * @return Session A Session object. * * @static * @access public * @internal */ public static function fromSimpleXMLElement(SimpleXMLElement $xml) { return new Session(Util::toString($xml->name), Util::toString($xml->key), Util::toBoolean($xml->subscriber)); }