From fb87e02c9868427de4a97427f9190de4d2d93163 Mon Sep 17 00:00:00 2001
From: Jacek Kowalski <Jacek@jacekk.info>
Date: Sun, 12 Aug 2012 12:26:10 +0000
Subject: [PATCH] Dodanie pełnej obsługi obrazków, zgodnie z ostatnimi zmianami w BotAPI.
---
class/BotAPIGG.php | 119 ++++++++++++++++++++++++++++++---------
class/BotMessage.php | 6 ++
class/BotMsgGG.php | 25 ++++++--
class/BotMessageGG.php | 9 +++
4 files changed, 125 insertions(+), 34 deletions(-)
diff --git a/class/BotAPIGG.php b/class/BotAPIGG.php
index 0801ac6..c8108e1 100644
--- a/class/BotAPIGG.php
+++ b/class/BotAPIGG.php
@@ -71,7 +71,7 @@
const STATUS_INVISIBLE = 20;
const STATUS_INVISIBLE_DESC = 22;
- private function httpQuery($address, $useToken = TRUE, $curlopts = array()) {
+ private function httpQuery($address, $curlopts = array(), $useToken = TRUE, $parseXML = TRUE) {
if(!is_array($curlopts)) {
$curlopts = array();
}
@@ -97,21 +97,29 @@
$tok2 = $tok = curl_exec($dane);
$info = curl_getinfo($dane);
- try {
- libxml_use_internal_errors(TRUE);
- $tok = new SimpleXMLElement($tok);
+ if($parseXML) {
+ try {
+ libxml_use_internal_errors(TRUE);
+ $tok = new SimpleXMLElement($tok);
+ }
+ catch(Exception $e) {
+ throw new BotAPIGGXMLException('Otrzymano błędny XML od botmastera.', $tok2);
+ }
+
+ if(!$tok) {
+ if($info['http_code'] != 200) {
+ throw new BotAPIGGHTTPException('Nie udało się wykonać zapytania HTTP.', $info['http_code'], $tok2);
+ }
+ else
+ {
+ throw new BotAPIGGXMLException('Otrzymano błędny XML od botmastera.', $tok2);
+ }
+ }
}
- catch(Exception $e) {
- throw new BotAPIGGXMLException('Otrzymano błędny XML od botmastera.', $tok2);
- }
-
- if(!$tok) {
+ else
+ {
if($info['http_code'] != 200) {
throw new BotAPIGGHTTPException('Nie udało się wykonać zapytania HTTP.', $info['http_code'], $tok2);
- }
- else
- {
- throw new BotAPIGGXMLException('Otrzymano błędny XML od botmastera.', $tok2);
}
}
@@ -119,32 +127,34 @@
}
function getToken($force = FALSE) {
- if($force || !$this->token) {
+ if($force || self::$token === NULL) {
$auth = $this->APIs['Gadu-Gadu'];
- $tok = $this->httpQuery('https://botapi.gadu-gadu.pl/botmaster/getToken/'.$auth['numer'], FALSE, array(
+ $tok = $this->httpQuery('https://botapi.gadu-gadu.pl/botmaster/getToken/'.$auth['numer'], array(
CURLOPT_USERPWD => $auth['login'].':'.$auth['haslo'],
CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
- ));
+ ), FALSE);
if($tok->errorMsg) {
throw new BotAPIGGReplyException('Pobieranie tokena nie powiodło się.', $tok);
}
- $this->token = array('token' => (string)$tok->token, 'host' => (string)$tok->server, 'port' => (int)$tok->port);
+ self::$token = array('token' => (string)$tok->token, 'host' => (string)$tok->server, 'port' => (int)$tok->port);
}
- return $this->token;
+ return self::$token;
}
function setStatus($status, $desc = '') {
$auth = $this->APIs['Gadu-Gadu'];
$token = $this->getToken();
- $tok = $this->httpQuery('https://'.$token['host'].'/setStatus/'.$auth['numer'], FALSE, array(
- CURLOPT_HTTPHEADER => array('Content-Type: application/x-www-form-urlencoded'),
+ $tok = $this->httpQuery('https://'.$token['host'].'/setStatus/'.$auth['numer'], array(
CURLOPT_POST => TRUE,
- CURLOPT_POSTFIELDS => 'token='.urlencode($token['token']).'&status='.urlencode($status).'&desc='.urlencode($desc),
+ CURLOPT_POSTFIELDS => array(
+ 'status' => $status,
+ 'desc' => $desc,
+ ),
));
if( (string)$tok->status != '0') {
@@ -155,7 +165,7 @@
function setUrl($url) {
$auth = $this->APIs['Gadu-Gadu'];
- $tok = $this->httpQuery('https://botapi.gadu-gadu.pl/botmaster/setUrl/'.$auth['numer'], TRUE, array(
+ $tok = $this->httpQuery('https://botapi.gadu-gadu.pl/botmaster/setUrl/'.$auth['numer'], array(
CURLOPT_POST => TRUE,
CURLOPT_POSTFIELDS => $url,
));
@@ -167,6 +177,56 @@
return $tok;
}
+ function getImage($hash) {
+ $auth = $this->APIs['Gadu-Gadu'];
+ $token = $this->getToken();
+
+ $tok = $this->httpQuery('https://'.$token['host'].'/botmaster/setUrl/'.$auth['numer'], array(
+ CURLOPT_POST => TRUE,
+ CURLOPT_POSTFIELDS => array('hash' => $hash),
+ ), TRUE, FALSE);
+
+ return $tok;
+ }
+
+ function existsImage() {
+ $auth = $this->APIs['Gadu-Gadu'];
+ $token = $this->getToken();
+
+ $tok = $this->httpQuery('https://'.$token['host'].'/botmaster/setUrl/'.$auth['numer'], array(
+ CURLOPT_POST => TRUE,
+ CURLOPT_POSTFIELDS => array('hash' => $hash),
+ ), TRUE, FALSE);
+
+ if( (string)$tok->status != '0') {
+ return FALSE;
+ }
+
+ return TRUE;
+ }
+
+ 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'),
+ CURLOPT_POST => TRUE,
+ CURLOPT_INFILE => $fp,
+ ), TRUE, FALSE);
+
+ if( (string)$tok->status != '0') {
+ throw new BotAPIGGReplyException('Przesyłanie obrazka do botmastera nie powiodło się.', $tok);
+ }
+
+ return (string)$tok->hash;
+ }
+
/**
* Wysyła wiadomość do podanych użytkowników
* @param array $toURL Lista adresatów wiadomości w postaci: array('Gadu-Gadu://NUMER@gadu-gadu.pl', ...)
@@ -174,7 +234,6 @@
* @param array $params Parametry przekazywane funkcji. Aktualnie dostępne:
* array( 'SendToOffline' => (bool)TRUE/FALSE )
*/
-
function sendMessage($toURL, BotMsg $msg, $params = array()) {
$to = array();
foreach($toURL as $url) {
@@ -183,7 +242,7 @@
continue;
}
- if($url['user']=='' || !ctype_digit($url['user'])) {
+ if($url['user'] == '' || !ctype_digit($url['user'])) {
throw new Exception('Nieznany użytkownik sieci Gadu-Gadu, któremu należy dostarczyć wiadomość.');
}
@@ -208,17 +267,23 @@
while(!empty($to)) {
$to_part = implode(',', array_splice($to, -5000));
- $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 => 'token='.urlencode($token['token']).'&to='.urlencode($to_part).'&msg='.urlencode($msg->getGG(FALSE)),
+ CURLOPT_POSTFIELDS => array(
+ 'to' => $to_part,
+ 'msg' => $msg->getGG(FALSE),
+ ),
));
if((string)$tok->status == '18') {
$tok = $this->httpQuery('https://'.$token['host'].'/sendMessage/'.$auth['numer'], FALSE, array(
CURLOPT_HTTPHEADER => $headers,
CURLOPT_POST => TRUE,
- CURLOPT_POSTFIELDS => 'token='.urlencode($token['token']).'&to='.urlencode($to_part).'&msg='.urlencode($msg->getGG(FALSE)),
+ CURLOPT_POSTFIELDS => array(
+ 'to' => $to_part,
+ 'msg' => $msg->getGG(TRUE),
+ ),
));
}
diff --git a/class/BotMessage.php b/class/BotMessage.php
index 4b446bf..a958902 100644
--- a/class/BotMessage.php
+++ b/class/BotMessage.php
@@ -31,6 +31,12 @@
protected $text;
/**
+ * Tablica obrazków (zobacz klasę BotImage) przesłanych do bota przez użytkownika.
+ * @var array
+ */
+ protected $images = array();
+
+ /**
* Komenda, tylko znaki ASCII, małe litery
* @var string
*/
diff --git a/class/BotMessageGG.php b/class/BotMessageGG.php
index fd5ee9d..aa07ac8 100644
--- a/class/BotMessageGG.php
+++ b/class/BotMessageGG.php
@@ -15,6 +15,15 @@
$this->user = new BotUser($uid.'/'.$_GET['to']);
$this->session = new BotSession($uid);
$this->setText(file_get_contents('php://input'));
+
+ if(isset($_GET['images'])) {
+ $images = explode(',', $_GET['images']);
+ foreach($images as $image) {
+ if(strlen($image) == 16 && ctype_xdigit($image)) {
+ $this->images[] = new BotImageGG($image);
+ }
+ }
+ }
}
}
?>
\ No newline at end of file
diff --git a/class/BotMsgGG.php b/class/BotMsgGG.php
index 4f5638e..6b0ff53 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();
@@ -49,12 +48,25 @@
/**
* 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;
+ }
+
+ $image = $last[2].file_get_contents($last[3]);
}
else
{
@@ -309,11 +321,10 @@
$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)
.pack('CCVV', 0x09, 0x01, $size, hexdec($crc));
--
Gitblit v1.9.1