From 6b36e75843ad46385fea0a81da04a7c982bea2a3 Mon Sep 17 00:00:00 2001
From: Jacek Kowalski <Jacek@jacekk.info>
Date: Wed, 11 Jun 2014 20:57:24 +0000
Subject: [PATCH] [mod_data] Poprawka użycia niezdefiniowanej zmiennej

---
 class/BotAPIGG.php |   93 +++++++++++++++++++++++++++++++++++++---------
 1 files changed, 74 insertions(+), 19 deletions(-)

diff --git a/class/BotAPIGG.php b/class/BotAPIGG.php
index 4bb69a7..90981dd 100644
--- a/class/BotAPIGG.php
+++ b/class/BotAPIGG.php
@@ -6,7 +6,7 @@
 	function __construct($msg, $httpcode, $content) {
 		$this->httpcode = $httpcode;
 		$this->content = $content;
-		parent::__construct($msg);
+		parent::__construct($msg.' Błąd '.$httpcode);
 	}
 	
 	function __get($name) {
@@ -151,10 +151,10 @@
 		
 		$tok = $this->httpQuery('https://'.$token['host'].'/setStatus/'.$auth['numer'], array(
 			CURLOPT_POST => TRUE,
-			CURLOPT_POSTFIELDS => array(
+			CURLOPT_POSTFIELDS => http_build_query(array(
 				'status' => $status,
 				'desc' => $desc,
-			),
+			), '', '&'),
 		));
 		
 		if( (string)$tok->status != '0') {
@@ -181,22 +181,22 @@
 		$auth = $this->APIs['Gadu-Gadu'];
 		$token = $this->getToken();
 		
-		$tok = $this->httpQuery('https://'.$token['host'].'/botmaster/setUrl/'.$auth['numer'], array(
+		$tok = $this->httpQuery('https://botapi.gadu-gadu.pl/botmaster/getImage/'.$auth['numer'], array(
 			CURLOPT_POST => TRUE,
-			CURLOPT_POSTFIELDS => array('hash' => $hash),
+			CURLOPT_POSTFIELDS => http_build_query(array('hash' => $hash), '', '&'),
 		), TRUE, FALSE);
 		
 		return $tok;
 	}
 	
-	function existsImage() {
+	function existsImage($hash) {
 		$auth = $this->APIs['Gadu-Gadu'];
 		$token = $this->getToken();
 		
-		$tok = $this->httpQuery('https://'.$token['host'].'/botmaster/setUrl/'.$auth['numer'], array(
+		$tok = $this->httpQuery('https://botapi.gadu-gadu.pl/botmaster/existsImage/'.$auth['numer'], array(
 			CURLOPT_POST => TRUE,
-			CURLOPT_POSTFIELDS => array('hash' => $hash),
-		), TRUE, FALSE);
+			CURLOPT_POSTFIELDS => http_build_query(array('hash' => $hash), '', '&'),
+		));
 		
 		if( (string)$tok->status != '0') {
 			return FALSE;
@@ -206,19 +206,17 @@
 	}
 	
 	function putImage($path) {
-		$fp = fopen($path, 'r');
-		if(!$fp) {
-			return FALSE;
-		}
-		
 		$auth = $this->APIs['Gadu-Gadu'];
 		$token = $this->getToken();
 		
-		$tok = $this->httpQuery('https://'.$token['host'].'/botmaster/setUrl/'.$auth['numer'], array(
-			CURLOPT_HTTPHEADER => array('Content-Type: image/x-any'),
+		$tok = $this->httpQuery('https://botapi.gadu-gadu.pl/botmaster/putImage/'.$auth['numer'], array(
+			CURLOPT_HTTPHEADER => array(
+				'Content-Type: image/x-any',
+				'Expect: ',
+			),
 			CURLOPT_POST => TRUE,
-			CURLOPT_INFILE => $fp,
-		), TRUE, FALSE);
+			CURLOPT_POSTFIELDS => file_get_contents($path),
+		));
 		
 		if( (string)$tok->status != '0') {
 			throw new BotAPIGGReplyException('Przesyłanie obrazka do botmastera nie powiodło się.', $tok);
@@ -235,9 +233,21 @@
 	 * array( 'SendToOffline' => (bool)TRUE/FALSE )
 	 */
 	function sendMessage($toURL, BotMsg $msg, $params = array()) {
+		if(is_string($toURL)) {
+			$toURL = array($toURL);
+		}
+		
+		if(!is_array($toURL)) {
+			throw new Exception('Lista adresatów przekazywanych do funkcji BotAPIGG::sendMessage() winna być tablicą.');
+		}
+		
 		$to = array();
 		foreach($toURL as $url) {
 			$url = parse_url($url);
+			if($url === FALSE) {
+				continue;
+			}
+			
 			if($url['scheme'] != 'Gadu-Gadu') {
 				continue;
 			}
@@ -276,8 +286,9 @@
 				),
 			));
 			
+			// Brak obrazka w cache BotMastera...
 			if((string)$tok->status == '18') {
-				$tok = $this->httpQuery('https://'.$token['host'].'/sendMessage/'.$auth['numer'], FALSE, array(
+				$tok = $this->httpQuery('https://'.$token['host'].'/sendMessage/'.$auth['numer'], array(
 					CURLOPT_HTTPHEADER => $headers,
 					CURLOPT_POST => TRUE,
 					CURLOPT_POSTFIELDS => array(
@@ -294,5 +305,49 @@
 		
 		return TRUE;
 	}
+	
+	/**
+	 * Pobiera dane użytkownika z katalogu publicznego.
+	 * @param string|BotUser Numer użytkownika
+	 * @return array|false Tablica z danymi.
+	 */
+	function getPublicData($number) {
+		if($number instanceof BotUser) {
+			if($number->network != 'gadu-gadu.pl') {
+				return FALSE;
+			}
+			
+			$number = $number->uid;
+		}
+		
+		if(!ctype_digit($number)) {
+			throw new Exception('Numer użytkownika przekazany do funkcji BotAPIGG::getPublicData() jest niepoprawny.');
+		}
+		
+		try {
+			$data = file_get_contents('http://api.gadu-gadu.pl/users/'.$number.'.xml');
+			if(!$data) {
+				throw new Exception('Nie udało się pobrać danych użytkownika z katalogu publicznego.');
+			}
+		}
+		catch(Exception $e) {
+			throw new Exception('Nie udało się pobrać danych użytkownika z katalogu publicznego.');
+		}
+		
+		libxml_use_internal_errors();
+		$data = simplexml_load_string($data);
+		libxml_clear_errors();
+		
+		if(!$data) {
+			throw new Exception('Dane użytkownika otrzymane z API Gadu-Gadu mają niepoprawny format.');
+		}
+		
+		if(!$data) {
+			throw new Exception('Dane użytkownika otrzymane z API Gadu-Gadu mają niepoprawny format.');
+		}
+		
+		
+		return (array)$data->users->user;
+	}
 }
 ?>
\ No newline at end of file

--
Gitblit v1.9.1