From 4aa6278f7fd40fdcbbde2be5c7e0e68c3117b44e Mon Sep 17 00:00:00 2001
From: Jacek Kowalski <Jacek@jacekk.info>
Date: Fri, 12 Feb 2016 23:18:17 +0000
Subject: [PATCH] [mod_tv] Lepsza obsługa błędów przy okresowym pobieraniu danych

---
 class/BotAPIGG.php |   95 +++++++++++++++++++++++++++++++++++++----------
 1 files changed, 75 insertions(+), 20 deletions(-)

diff --git a/class/BotAPIGG.php b/class/BotAPIGG.php
index c8108e1..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) {
@@ -44,7 +44,7 @@
 }
 
 class BotAPIGG extends config {
-	private $token;
+	private static $token;
 	
 	const STATUS_DOSTEPNY = 2;
 	const STATUS_DOSTEPNY_DESC = 4;
@@ -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