コード例 #1
0
ファイル: whatsprot.class.php プロジェクト: micschk/WhatsAPI
 /**
  * Process media upload response
  *
  * @param ProtocolNode $node
  *  Message node
  * @return bool
  */
 protected function processUploadResponse($node)
 {
     $id = $node->getAttribute("id");
     $messageNode = @$this->mediaQueue[$id];
     if ($messageNode == null) {
         //message not found, can't send!
         $this->eventManager()->fireMediaUploadFailed($this->phoneNumber, $id, $node, $messageNode, "Message node not found in queue");
         return false;
     }
     $duplicate = $node->getChild("duplicate");
     if ($duplicate != null) {
         //file already on whatsapp servers
         $url = $duplicate->getAttribute("url");
         $filesize = $duplicate->getAttribute("size");
         //            $mimetype = $duplicate->getAttribute("mimetype");
         $filehash = $duplicate->getAttribute("filehash");
         $filetype = $duplicate->getAttribute("type");
         //            $width = $duplicate->getAttribute("width");
         //            $height = $duplicate->getAttribute("height");
         $exploded = explode("/", $url);
         $filename = array_pop($exploded);
     } else {
         //upload new file
         $json = WhatsMediaUploader::pushFile($node, $messageNode, $this->mediaFileInfo, $this->phoneNumber);
         if (!$json) {
             //failed upload
             $this->eventManager()->fireMediaUploadFailed($this->phoneNumber, $id, $node, $messageNode, "Failed to push file to server");
             return false;
         }
         $url = $json->url;
         $filesize = $json->size;
         //            $mimetype = $json->mimetype;
         $filehash = $json->filehash;
         $filetype = $json->type;
         //            $width = $json->width;
         //            $height = $json->height;
         $filename = $json->name;
     }
     $mediaAttribs = array();
     $mediaAttribs["xmlns"] = "urn:xmpp:whatsapp:mms";
     $mediaAttribs["type"] = $filetype;
     $mediaAttribs["url"] = $url;
     $mediaAttribs["file"] = $filename;
     $mediaAttribs["size"] = $filesize;
     $mediaAttribs["hash"] = $filehash;
     $filepath = $this->mediaQueue[$id]['filePath'];
     $to = $this->mediaQueue[$id]['to'];
     switch ($filetype) {
         case "image":
             $icon = createIcon($filepath);
             break;
         case "video":
             $icon = createVideoIcon($filepath);
             break;
         default:
             $icon = '';
             break;
     }
     $mediaNode = new ProtocolNode("media", $mediaAttribs, null, $icon);
     if (is_array($to)) {
         $this->sendBroadcast($to, $mediaNode, "media");
     } else {
         $this->sendMessageNode($to, $mediaNode);
     }
     $this->eventManager()->fireMediaMessageSent($this->phoneNumber, $to, $id, $filetype, $url, $filename, $filesize, $filehash, $icon);
     return true;
 }
コード例 #2
0
ファイル: whatsprot.class.php プロジェクト: Veivan/WABuh
 /**
  * Process media upload response.
  *
  * @param ProtocolNode $node Message node
  *
  * @return bool
  */
 public function processUploadResponse($node)
 {
     $id = $node->getAttribute('id');
     $messageNode = @$this->mediaQueue[$id];
     if ($messageNode == null) {
         //message not found, can't send!
         $this->eventManager()->fire('onMediaUploadFailed', [$this->phoneNumber, $id, $node, $messageNode, 'Message node not found in queue']);
         return false;
     }
     $duplicate = $node->getChild('duplicate');
     if ($duplicate != null) {
         //file already on whatsapp servers
         $url = $duplicate->getAttribute('url');
         $filesize = $duplicate->getAttribute('size');
         //          $mimetype = $duplicate->getAttribute("mimetype");
         $filehash = $duplicate->getAttribute('filehash');
         $filetype = $duplicate->getAttribute('type');
         //          $width = $duplicate->getAttribute("width");
         //          $height = $duplicate->getAttribute("height");
         $exploded = explode('/', $url);
         $filename = array_pop($exploded);
     } else {
         //upload new file
         $json = WhatsMediaUploader::pushFile($node, $messageNode, $this->mediaFileInfo, $this->phoneNumber);
         if (!$json) {
             //failed upload
             $this->eventManager()->fire('onMediaUploadFailed', [$this->phoneNumber, $id, $node, $messageNode, 'Failed to push file to server']);
             return false;
         }
         $url = $json->url;
         $filesize = $json->size;
         //          $mimetype = $json->mimetype;
         $filehash = $json->filehash;
         $filetype = $json->type;
         //          $width = $json->width;
         //          $height = $json->height;
         $filename = $json->name;
     }
     $mediaAttribs = [];
     $mediaAttribs['type'] = $filetype;
     $mediaAttribs['url'] = $url;
     $mediaAttribs['encoding'] = 'raw';
     $mediaAttribs['file'] = $filename;
     $mediaAttribs['size'] = $filesize;
     if ($this->mediaQueue[$id]['caption'] != '') {
         $mediaAttribs['caption'] = $this->mediaQueue[$id]['caption'];
     }
     if ($this->voice == true) {
         $mediaAttribs['origin'] = 'live';
         $this->voice = false;
     }
     $filepath = $this->mediaQueue[$id]['filePath'];
     $to = $this->mediaQueue[$id]['to'];
     switch ($filetype) {
         case 'image':
             $caption = $this->mediaQueue[$id]['caption'];
             $icon = createIcon($filepath);
             break;
         case 'video':
             $caption = $this->mediaQueue[$id]['caption'];
             $icon = createVideoIcon($filepath);
             break;
         default:
             $caption = '';
             $icon = '';
             break;
     }
     //Retrieve Message ID
     $message_id = $messageNode['message_id'];
     $mediaNode = new ProtocolNode('media', $mediaAttribs, null, $icon);
     if (is_array($to)) {
         $this->sendBroadcast($to, $mediaNode, 'media');
     } else {
         $this->sendMessageNode($to, $mediaNode, $message_id);
     }
     $this->eventManager()->fire('onMediaMessageSent', [$this->phoneNumber, $to, $message_id, $filetype, $url, $filename, $filesize, $filehash, $caption, $icon]);
     return true;
 }