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/BotMsgGG.php |   65 +++++++++++++++++++-------------
 1 files changed, 39 insertions(+), 26 deletions(-)

diff --git a/class/BotMsgGG.php b/class/BotMsgGG.php
index 4ab34a1..d14e99e 100644
--- a/class/BotMsgGG.php
+++ b/class/BotMsgGG.php
@@ -6,7 +6,6 @@
 	private $parser;
 	private $html = '';
 	private $old = '';
-	private $img = '';
 	private $format = '';
 	
 	private $images = array();
@@ -21,10 +20,6 @@
 	const FORMAT_UNDERLINE =0x04;
 	const FORMAT_COLOR =	0x08;
 	const FORMAT_IMAGE =	0x80;
-	
-	const COLOR_RED =	0x00010000;
-	const COLOR_GREEN =	0x00000100;
-	const COLOR_BLUE =	0x00000001;
 	
 	/**
 	 * @param BotMsg $msg Wiadomość do przekonwertowania
@@ -49,19 +44,36 @@
 	
 	/**
 	 * Zwraca wiadomość zgodną z BotAPI Gadu-Gadu, którą można przekazać bezpośrednio do BotMastera
-	 * @param bool $img Czy dołączać obrazki?
+	 * @param NULL|bool $img Czy dołączać obrazki?
 	 * @return string
 	 */
-	function getGG($image = TRUE) {
-		if($image) {
-			$image = $this->img;
+	function getGG($image = NULL) {
+		if($image === FALSE) {
+			$image = '';
+		}
+		elseif($image === TRUE) {
+			$last = array_pop($this->images);
+			if(count($this->images) > 0) {
+				$push = new BotAPIGG();
+				foreach($this->images as $data) {
+					$push->putImage($image[3]);
+				}
+			}
+			
+			$image = $last[2].file_get_contents($last[3]);
 		}
 		else
 		{
-			$image = '';
-			foreach($this->images as $img) {
-				$image .= $img[2];
+			if(count($this->images) > 0) {
+				$push = new BotAPIGG();
+				foreach($this->images as $image) {
+					if(!$push->existsImage($image[2])) {
+						$push->putImage($image[3]);
+					}
+				}
 			}
+			
+			$image = '';
 		}
 		
 		$format = $this->getFormat();
@@ -99,7 +111,13 @@
 	 * @return string
 	 */
 	function getFormat() {
-		return pack('Cv', 0x02, strlen($this->format)).$this->format;
+		if($this->format == '') {
+			return '';
+		}
+		else
+		{
+			return pack('Cv', 0x02, strlen($this->format)).$this->format;
+		}
 	}
 	
 	/**
@@ -107,11 +125,7 @@
 	 */
 	function sendPullResponse() {
 		header('Content-Type: application/x-gadu-gadu; charset=utf-8');
-		echo $this;
-	}
-	
-	private function rewriteOne($node, $saveto) {
-		
+		echo $this->getGG();
 	}
 	
 	private function rewrite($dom, $saveto, $top = TRUE) {
@@ -211,7 +225,7 @@
 						DOMHelper::rtrim($saveto);
 						DOMHelper::insertElement('br', $saveto);
 						
-						$tag = DOMHelper::cloneNode($node, $saveto, 'u');
+						$tag = DOMHelper::cloneNode($node, $saveto, 'span');
 						$this->rewrite($node, $tag, FALSE);
 						$saveto->appendChild($tag);
 					break;
@@ -223,7 +237,7 @@
 							$node->setAttribute('start', 1);
 						}
 						
-						$tag = DOMHelper::cloneNode($node, $saveto, 'u');
+						$tag = DOMHelper::cloneNode($node, $saveto, 'span');
 						$this->rewrite($node, $tag, FALSE);
 						$saveto->appendChild($tag);
 					break;
@@ -312,20 +326,19 @@
 				$crc = hash_file('crc32b', $src);
 				$name = sprintf('%08s%08x', $crc, $size);
 				
-				$this->images[$src] = array($crc, $size, $name);
+				$this->images[$src] = array($crc, $size, $name, $src);
 			}
 			
 			$node->setAttribute('name', $name);
-			$this->img .= $name.file_get_contents($src);
 			
-			$this->format .= pack('vC', mb_strlen($this->old), self::FORMAT_IMAGE/*|$this->f_type*/)
-			//		//.$this->f_color
+			$this->format .= pack('vC', mb_strlen($this->old), self::FORMAT_IMAGE)
 					.pack('CCVV', 0x09, 0x01, $size, hexdec($crc));
+			$this->f_old = '';
 		}
 	}
 	
 	private function format(&$node) {
-		$node->setAttribute('beforeFormatType', ord($this->f_type));
+		$node->setAttribute('beforeFormatType', dechex($this->f_type));
 		$node->setAttribute('beforeFormatColor', base64_encode($this->f_color));
 		
 		if($node->hasAttribute('color')) {
@@ -366,7 +379,7 @@
 	}
 	
 	private function unformat($node) {
-		$this->f_type = chr($node->getAttribute('beforeFormatType'));
+		$this->f_type = hexdec($node->getAttribute('beforeFormatType'));
 		$node->removeAttribute('beforeFormatType');
 		
 		$this->f_color = base64_decode($node->getAttribute('beforeFormatColor'));

--
Gitblit v1.9.1