/
whatsprot.class.php
2456 lines (2253 loc) · 91.3 KB
/
whatsprot.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
require_once 'protocol.class.php';
require_once 'WhatsAppEvent.php';
require_once 'func.php';
require_once 'rc4.php';
require_once 'mediauploader.php';
class WhatsProt
{
/**
* Constant declarations.
*/
const CONNECTED_STATUS = 'connected'; // Describes the connection status with the WhatsApp server.
const DISCONNECTED_STATUS = 'disconnected'; // Describes the connection status with the WhatsApp server.
const MEDIA_FOLDER = 'media'; // The relative folder to store received media files
const PICTURES_FOLDER = 'pictures'; // The relative folder to store picture files
const PORT = 5222; // The port of the WhatsApp server.
const TIMEOUT_SEC = 2; // The timeout for the connection with the WhatsApp servers.
const TIMEOUT_USEC = 0; //
const WHATSAPP_CHECK_HOST = 'v.whatsapp.net/v2/exist'; // The check credentials host.
const WHATSAPP_GROUP_SERVER = 'g.us'; // The Group server hostname
const WHATSAPP_HOST = 'c.whatsapp.net'; // The hostname of the WhatsApp server.
const WHATSAPP_REGISTER_HOST = 'v.whatsapp.net/v2/register'; // The register code host.
const WHATSAPP_REQUEST_HOST = 'v.whatsapp.net/v2/code'; // The request code host.
const WHATSAPP_SERVER = 's.whatsapp.net'; // The hostname used to login/send messages.
const WHATSAPP_UPLOAD_HOST = 'https://mms.whatsapp.net/client/iphone/upload.php'; // The upload host.
const WHATSAPP_DEVICE = 'Android'; // The device name.
const WHATSAPP_VER = '2.10.750'; // The WhatsApp version.
const WHATSAPP_TOKEN = "30820332308202f0a00302010202044c2536a4300b06072a8648ce3804030500307c310b3009060355040613025553311330110603550408130a43616c69666f726e6961311430120603550407130b53616e746120436c61726131163014060355040a130d576861747341707020496e632e31143012060355040b130b456e67696e656572696e67311430120603550403130b427269616e204163746f6e301e170d3130303632353233303731365a170d3434303231353233303731365a307c310b3009060355040613025553311330110603550408130a43616c69666f726e6961311430120603550407130b53616e746120436c61726131163014060355040a130d576861747341707020496e632e31143012060355040b130b456e67696e656572696e67311430120603550403130b427269616e204163746f6e308201b83082012c06072a8648ce3804013082011f02818100fd7f53811d75122952df4a9c2eece4e7f611b7523cef4400c31e3f80b6512669455d402251fb593d8d58fabfc5f5ba30f6cb9b556cd7813b801d346ff26660b76b9950a5a49f9fe8047b1022c24fbba9d7feb7c61bf83b57e7c6a8a6150f04fb83f6d3c51ec3023554135a169132f675f3ae2b61d72aeff22203199dd14801c70215009760508f15230bccb292b982a2eb840bf0581cf502818100f7e1a085d69b3ddecbbcab5c36b857b97994afbbfa3aea82f9574c0b3d0782675159578ebad4594fe67107108180b449167123e84c281613b7cf09328cc8a6e13c167a8b547c8d28e0a3ae1e2bb3a675916ea37f0bfa213562f1fb627a01243bcca4f1bea8519089a883dfe15ae59f06928b665e807b552564014c3bfecf492a0381850002818100d1198b4b81687bcf246d41a8a725f0a989a51bce326e84c828e1f556648bd71da487054d6de70fff4b49432b6862aa48fc2a93161b2c15a2ff5e671672dfb576e9d12aaff7369b9a99d04fb29d2bbbb2a503ee41b1ff37887064f41fe2805609063500a8e547349282d15981cdb58a08bede51dd7e9867295b3dfb45ffc6b259300b06072a8648ce3804030500032f00302c021400a602a7477acf841077237be090df436582ca2f0214350ce0268d07e71e55774ab4eacd4d071cd1efad022e923a364bfacff3a80de3f950b1e0";//'Od52pFozHNWF9XbTN5lrqDtnsiZGL2G3l9yw1GiQ21a31a2d9dbdc9a8ce324ef2df918064fd26e30a'; // Token used in request/registration code.
const WHATSAPP_USER_AGENT = 'WhatsApp/2.10.750 Android/4.2.1 Device/GalaxyS3';//'WhatsApp/2.10.523 WP7/7.10.8858 Device/HTC-HTC-H0002'; // User agent used in request/registration code.
/**
* Property declarations.
*/
protected $accountInfo; // The AccountInfo object.
protected $challengeData; //
protected $debug; // Determines whether debug mode is on or off.
protected $event; // An instance of the WhatsAppEvent class.
protected $groupList = array(); // An array with all the groups a user belongs in.
protected $identity; // The Device Identity token. Obtained during registration with this API or using Missvenom to sniff from your phone.
protected $incompleteMessage = ''; // A list of bytes for incomplete messages.
protected $inputKey; // Instances of the KeyStream class.
protected $outputKey; // Instances of the KeyStream class.
protected $groupId = false; // Id of the group created.
protected $lastId = false; // Id to the last message sent.
protected $loginStatus; // Holds the login status.
protected $mediaFileInfo = array(); // Media File Information
protected $mediaQueue = array(); // Queue for media message nodes
protected $messageCounter = 1; // Message counter for auto-id.
protected $messageQueue = array(); // Queue for received messages.
protected $name; // The user name.
protected $newMsgBind = false; //
protected $outQueue = array(); // Queue for outgoing messages.
protected $password; // The user password.
protected $phoneNumber; // The user phone number including the country code without '+' or '00'.
protected $reader; // An instance of the BinaryTreeNodeReader class.
protected $serverReceivedId; // Confirm that the *server* has received your command.
protected $socket; // A socket to connect to the WhatsApp network.
protected $writer; // An instance of the BinaryTreeNodeWriter class.
/**
* Default class constructor.
*
* @param string $number
* The user phone number including the country code without '+' or '00'.
* @param string $identity
* The Device Identity token. Obtained during registration with this API
* or using Missvenom to sniff from your phone.
* @param string $nickname
* The user name.
* @param $debug
* Debug on or off, false by default.
*/
public function __construct($number, $identity, $nickname, $debug = false)
{
$dict = getDictionary();
$this->writer = new BinTreeNodeWriter($dict);
$this->reader = new BinTreeNodeReader($dict);
$this->debug = $debug;
$this->phoneNumber = $number;
if (!$this->checkIdentity($identity)) {
//compute sha identity hash
$this->identity = $this->buildIdentity($identity);
} else {
//use provided identity hash
$this->identity = $identity;
}
$this->name = $nickname;
$this->loginStatus = static::DISCONNECTED_STATUS;
}
/**
* Add message to the outgoing queue.
*/
public function addMsgOutQueue($node)
{
$this->outQueue[] = $node;
}
/**
* Check if account credentials are valid.
*
* WARNING: WhatsApp now changes your password everytime you use this.
* Make sure you update your config file if the output informs about
* a password change.
*
* @return object
* An object with server response.
* - status: Account status.
* - login: Phone number with country code.
* - pw: Account password.
* - type: Type of account.
* - expiration: Expiration date in UNIX TimeStamp.
* - kind: Kind of account.
* - price: Formatted price of account.
* - cost: Decimal amount of account.
* - currency: Currency price of account.
* - price_expiration: Price expiration in UNIX TimeStamp.
*/
public function checkCredentials()
{
if (!$phone = $this->dissectPhone()) {
throw new Exception('The prived phone number is not valid.');
}
// Build the url.
$host = 'https://' . static::WHATSAPP_CHECK_HOST;
$query = array(
'cc' => $phone['cc'],
'in' => $phone['phone'],
'id' => $this->identity,
'c' => 'cookie',
);
$response = $this->getResponse($host, $query);
if ($response->status != 'ok') {
$this->eventManager()->fire('onCredentialsBad', array($this->phoneNumber, $response->status, $response->reason));
if ($this->debug) {
print_r($query);
print_r($response);
}
throw new Exception('There was a problem trying to request the code.');
} else {
$this->eventManager()->fire('onCredentialsGood', array(
$this->phoneNumber,
$response->login,
$response->pw,
$response->type,
$response->expiration,
$response->kind,
$response->price,
$response->cost,
$response->currency,
$response->price_expiration
));
}
return $response;
}
/**
* Register account on WhatsApp using the provided code.
*
* @param integer $code
* Numeric code value provided on requestCode().
*
* @return object
* An object with server response.
* - status: Account status.
* - login: Phone number with country code.
* - pw: Account password.
* - type: Type of account.
* - expiration: Expiration date in UNIX TimeStamp.
* - kind: Kind of account.
* - price: Formatted price of account.
* - cost: Decimal amount of account.
* - currency: Currency price of account.
* - price_expiration: Price expiration in UNIX TimeStamp.
*/
public function codeRegister($code)
{
if (!$phone = $this->dissectPhone()) {
throw new Exception('The prived phone number is not valid.');
}
// Build the url.
$host = 'https://' . static::WHATSAPP_REGISTER_HOST;
$query = array(
'cc' => $phone['cc'],
'in' => $phone['phone'],
'id' => $this->identity,
'code' => $code,
'c' => 'cookie',
);
$response = $this->getResponse($host, $query);
if ($response->status != 'ok') {
$this->eventManager()->fire('onCodeRegisterFailed', array($this->phoneNumber, $response->status, $response->reason, $response->retry_after));
if ($this->debug) {
print_r($query);
print_r($response);
}
throw new Exception('An error occurred registering the registration code from WhatsApp.');
} else {
$this->eventManager()->fire('onCodeRegister', array(
$this->phoneNumber,
$response->login,
$response->pw,
$response->type,
$response->expiration,
$response->kind,
$response->price,
$response->cost,
$response->currency,
$response->price_expiration
));
}
return $response;
}
/**
* Request a registration code from WhatsApp.
*
* @param string $method
* Accepts only 'sms' or 'voice' as a value.
* @param string $countryCody
* ISO Country Code, 2 Digit.
* @param string $langCode
* ISO 639-1 Language Code: two-letter codes.
*
* @return object
* An object with server response.
* - status: Status of the request (sent/fail).
* - length: Registration code lenght.
* - method: Used method.
* - reason: Reason of the status (e.g. too_recent/missing_param/bad_param).
* - param: The missing_param/bad_param.
* - retry_after: Waiting time before requesting a new code.
*/
public function codeRequest($method = 'sms', $countryCode = false, $langCode = false)
{
if (!$phone = $this->dissectPhone()) {
throw new Exception('The provided phone number is not valid.');
}
if ($countryCode === false && $phone['ISO3166'] != '') {
$countryCode = $phone['ISO3166'];
}
if ($countryCode === false) {
$countryCode = 'US';
}
if ($langCode === false && $phone['ISO639'] != '') {
$langCode = $phone['ISO639'];
}
if ($langCode === false) {
$langCode = 'en';
}
// Build the token.
$token = md5(static::WHATSAPP_TOKEN . $phone['phone']);
// Build the url.
$host = 'https://' . static::WHATSAPP_REQUEST_HOST;
$query = array(
'cc' => $phone['cc'],
'in' => $phone['phone'],
'to' => $this->phoneNumber,
'lg' => $langCode,
'lc' => $countryCode,
'method' => $method,
'mcc' => $phone['mcc'],
'mnc' => '001',
'token' => $token,
'id' => $this->identity,
);
if ($this->debug) {
print_r($query);
}
$response = $this->getResponse($host, $query);
if ($this->debug) {
print_r($response);
}
if ($response->status == 'ok') {
$this->eventManager()->fire('onCodeRegister', array(
$this->phoneNumber,
$response->login,
$response->pw,
$response->type,
$response->expiration,
$response->kind,
$response->price,
$response->cost,
$response->currency,
$response->price_expiration
));
} else if ($response->status != 'sent') {
if (isset($response->reason) && $response->reason == "too_recent") {
$this->eventManager()->fire('onCodeRequestFailedTooRecent', array($this->phoneNumber, $method, $response->reason, $response->retry_after));
$minutes = round($response->retry_after / 60);
throw new Exception("Code already sent. Retry after $minutes minutes.");
} else {
$this->eventManager()->fire('onCodeRequestFailed', array($this->phoneNumber, $method, $response->reason, $response->param));
throw new Exception('There was a problem trying to request the code.');
}
} else {
$this->eventManager()->fire('onCodeRequest', array($this->phoneNumber, $method, $response->length));
}
return $response;
}
/**
* Connect (create a socket) to the WhatsApp network.
*/
public function connect()
{
$Socket = fsockopen(static::WHATSAPP_HOST, static::PORT);
stream_set_timeout($Socket, static::TIMEOUT_SEC, static::TIMEOUT_USEC);
$this->socket = $Socket;
$this->eventManager()->fire('onConnect', array($this->phoneNumber, $this->socket));
}
/**
* Disconnect to the WhatsApp network.
*/
public function disconnect()
{
fclose($this->socket);
$this->eventManager()->fire('onDisconnect', array($this->phoneNumber, $this->socket));
}
/**
* Gets a new micro event dispatcher.
*/
public function eventManager()
{
if (!is_object($this->event)) {
$this->event = new WhatsAppEvent();
}
return $this->event;
}
/**
* Drain the message queue for application processing.
*
* @return array
* Return the message queue list.
*/
public function getMessages()
{
$ret = $this->messageQueue;
$this->messageQueue = array();
return $ret;
}
/**
* Log into the Whatsapp server.
*
* ###Warning### using this method will generate a new password
* from the WhatsApp servers each time.
*
* If you know your password and wish to use it without generating
* a new password - use the loginWithPassword() method instead.
*
* @param bool $profileSubscribe
*
* Set this to true if you would like Whatsapp to send a
* notification to your phone when one of your contacts
* changes/update their picture.
*/
public function login($profileSubscribe = false)
{
$this->accountInfo = (array) $this->checkCredentials();
if ($this->accountInfo['status'] == 'ok') {
if ($this->debug) {
echo "New password received: " . $this->accountInfo['pw'] . "\r\n";
}
$this->password = $this->accountInfo['pw'];
}
$this->doLogin($profileSubscribe);
}
/**
* Login to the Whatsapp server with your password
*
* If you already know your password you can log into the Whatsapp server
* using this method.
*
* @param string $password Your whatsapp password. You must already know this!
* @param bool $profileSubscribe Add a feature
*/
public function loginWithPassword($password, $profileSubscribe = false)
{
$this->password = $password;
$challengeData = @file_get_contents("nextChallenge.dat");
if($challengeData) {
$this->challengeData = $challengeData;
}
$this->doLogin($profileSubscribe);
}
/**
* Pull from the socket, and place incoming messages in the message queue.
*/
public function pollMessages()
{
$this->processInboundData($this->readData());
}
/**
* Send the active status. User will show up as "Online" (as long as socket is connected).
*/
public function sendActiveStatus()
{
$messageNode = new ProtocolNode("presence", array("type" => "active"), null, "");
$this->sendNode($messageNode);
}
/**
* Send a Broadcast Message with audio.
*
* The receiptiant MUST have your number (synced) and in their contact list
* otherwise the message will not deliver to that person.
*
* Approx 20 (unverified) is the maximum number of targets
*
* @param array $targets An array of numbers to send to.
* @param string $path URL or local path to the audio file to send
* @param bool $storeURLmedia Keep a copy of the audio file on your server
*/
public function sendBroadcastAudio($targets, $path, $storeURLmedia = false)
{
if (!is_array($targets)) {
$targets = array($targets);
}
$this->sendMessageAudio($targets, $path, $storeURLmedia);
}
/**
* Send a Broadcast Message with an image.
*
* The receiptiant MUST have your number (synced) and in their contact list
* otherwise the message will not deliver to that person.
*
* Approx 20 (unverified) is the maximum number of targets
*
* @param array $targets An array of numbers to send to.
* @param string $path URL or local path to the image file to send
* @param bool $storeURLmedia Keep a copy of the audio file on your server
*/
public function sendBroadcastImage($targets, $path, $storeURLmedia = false)
{
if (!is_array($targets)) {
$targets = array($targets);
}
$this->sendMessageImage($targets, $path, $storeURLmedia);
}
/**
* Send a Broadcast Message with location data.
*
* The receiptiant MUST have your number (synced) and in their contact list
* otherwise the message will not deliver to that person.
*
* If no name is supplied , receiver will see large sized google map
* thumbnail of entered Lat/Long but NO name/url for location.
*
* With name supplied, a combined map thumbnail/name box is displayed
* Approx 20 (unverified) is the maximum number of targets
*
* @param array $targets An array of numbers to send to.
* @param float $long The longitude of the location eg 54.31652
* @param float $lat The latitude if the location eg -6.833496
* @param string $name (Optional) A name to describe the location
* @param string $url (Optional) A URL to link location to web resource
*/
public function sendBroadcastLocation($targets, $long, $lat, $name = null, $url = null)
{
if (!is_array($targets)) {
$targets = array($targets);
}
$this->sendMessageLocation($targets, $long, $lat, $name, $url);
}
/**
* Send a Broadcast Message
*
* The receiptiant MUST have your number (synced) and in their contact list
* otherwise the message will not deliver to that person.
*
* Approx 20 (unverified) is the maximum number of targets
*
* @param array $targets An array of numbers to send to.
* @param string $message Your message
*/
public function sendBroadcastMessage($targets, $message)
{
$bodyNode = new ProtocolNode("body", null, null, $message);
$this->sendBroadcast($targets, $bodyNode, "chat");
}
/**
* Send a Broadcast Message with a video.
*
* The receiptiant MUST have your number (synced) and in their contact list
* otherwise the message will not deliver to that person.
*
* Approx 20 (unverified) is the maximum number of targets
*
* @param array $targets An array of numbers to send to.
* @param string $path URL or local path to the video file to send
* @param bool $storeURLmedia Keep a copy of the audio file on your server
*/
public function sendBroadcastVideo($targets, $path, $storeURLmedia = false)
{
if (!is_array($targets)) {
$targets = array($targets);
}
$this->sendMessageVideo($targets, $path, $storeURLmedia);
}
/**
* Clears the "dirty" status on your account
*
* @param array $categories
*/
public function sendClearDirty($categories)
{
$msgId = $this->createMsgId("cleardirty");
$catnodes = array();
foreach ($categories as $category) {
$catnode = new ProtocolNode("category", array("name" => $category), null, null);
$catnodes[] = $catnode;
}
$clean = new ProtocolNode("clean", array("xmlns" => "urn:xmpp:whatsapp:dirty"), $catnodes, null);
$node = new ProtocolNode("iq", array(
"id" => $msgId,
"type" => "set",
"to" => "s.whatsapp.net"
), array($clean), null);
$this->sendNode($node);
}
public function sendClientConfig()
{
$phone = $this->dissectPhone();
$attr = array();
$attr["xmlns"] = "urn:xmpp:whatsapp:push";
$attr["platform"] = "none";
$attr["lc"] = $phone["ISO3166"];
$attr["lg"] = $phone["ISO639"];
$child = new ProtocolNode("config", $attr, null, "");
$node = new ProtocolNode("iq", array("id" => $this->createMsgId("config"), "type" => "set", "to" => static::WHATSAPP_SERVER), array($child), null);
$this->sendNode($node);
}
/**
* TODO
*
*/
public function sendGetClientConfig()
{
$msgId = $this->createMsgId("sendconfig");
$child = new ProtocolNode("config", array("xmlns" => "urn:xmpp:whatsapp:push", "sound" => 'sound'), null, null);
$node = new ProtocolNode("iq", array(
"id" => $msgId,
"type" => "set",
"to" => static::WHATSAPP_SERVER
), array($child), null);
$this->sendNode($node);
$this->waitForServer($msgId);
}
/**
* Send a request to return a list of groups user is currently participating
* in.
*
* To capture this list you will need to bind the "onGetGroups" event.
*/
public function sendGetGroups()
{
$this->sendGetGroupsFiltered("participating");
}
/**
* Send a request to get information about a specific group
*
* @param string $gjid The specific group id
*/
public function sendGetGroupsInfo($gjid)
{
$msgId = $this->createMsgId("getgroupinfo");
$child = new ProtocolNode("query", array("xmlns" => "w:g"), null, null);
$node = new ProtocolNode("iq", array(
"id" => $msgId,
"type" => "get",
"to" => $this->getJID($gjid)
), array($child), null);
$this->sendNode($node);
$this->waitForServer($msgId);
}
/**
* Send a request to return a list of groups user has started
* in.
*
* To capture this list you will need to bind the "onGetGroups" event.
*/
public function sendGetGroupsOwning()
{
$this->sendGetGroupsFiltered("owning");
}
/**
* Send a request to return a list of people participating in a specific
* group.
*
* @param string $gjid The specific group id
*/
public function sendGetGroupsParticipants($gjid)
{
$msgId = $this->createMsgId("getparticipants");
$child = new ProtocolNode("list", array(
"xmlns" => "w:g"
), null, null);
$node = new ProtocolNode("iq", array(
"id" => $msgId,
"type" => "get",
"to" => $this->getJID($gjid)
), array($child), null);
$this->sendNode($node);
$this->waitForServer($msgId);
}
/**
* Send a request to get a list of people you have currently blocked
*/
public function sendGetPrivacyBlockedList()
{
$msgId = $this->createMsgId("getprivacy");
$child = new ProtocolNode("list", array(
"name" => "default"
), null, null);
$child2 = new ProtocolNode("query", array(
"xmlns" => "jabber:iq:privacy"
), array($child), null);
$node = new ProtocolNode("iq", array(
"id" => $msgId,
"type" => "get"
), array($child2), null);
$this->sendNode($node);
$this->waitForServer($msgId);
}
/**
* Get profile picture of specified user
*
* @param string $number
* Number or JID of user
*
* @param bool $large
* Request large picture
*/
public function sendGetProfilePicture($number, $large = false)
{
$hash = array();
$hash["xmlns"] = "w:profile:picture";
$hash["type"] = "image";
if (!$large) {
$hash["type"] = "preview";
}
$picture = new ProtocolNode("picture", $hash, null, null);
$hash = array();
$hash["id"] = $this->createMsgId("getpicture");
$hash["type"] = "get";
$hash["to"] = $this->getJID($number);
$node = new ProtocolNode("iq", $hash, array($picture), null);
$this->sendNode($node);
$this->waitForServer($hash["id"]);
}
/**
* Request to retrieve the last online time of specific user.
*
* @param string $to
* Number or JID of user
*/
public function sendGetRequestLastSeen($to)
{
$queryHash = array();
$queryHash['xmlns'] = "jabber:iq:last";
$queryNode = new ProtocolNode("query", $queryHash, null, null);
$messageHash = array();
$messageHash["to"] = $this->getJID($to);
$messageHash["type"] = "get";
$messageHash["id"] = $this->createMsgId("lastseen");
$messageHash["from"] = $this->getJID($this->phoneNumber);
$messageNode = new ProtocolNode("iq", $messageHash, array($queryNode), "");
$this->sendNode($messageNode);
$this->waitForServer($messageHash["id"]);
}
/**
* Send a request to get the current server properties
*/
public function sendGetServerProperties()
{
$child = new ProtocolNode("props", array(
"xmlns" => "w"
), null, null);
$node = new ProtocolNode("iq", array(
"id" => $this->createMsgId("getproperties"),
"type" => "get",
"to" => "s.whatsapp.net"
), array($child), null);
$this->sendNode($node);
}
/**
* Get the current status message of a specific user.
*
* @param string $jid The user JID
*/
public function sendGetStatus($jid)
{
$parts = explode("@", $jid);
$to = $parts[0] . "@s.us";
$child = new ProtocolNode("action", array("type" => "get"), null, null);
$node = new ProtocolNode("message", array(
"to" => $to,
"type" => "action",
"id" => $this->createMsgId("message")
), array($child), null);
$this->sendNode($node);
}
/**
* Create a group chat.
*
* @param string $subject
* The group Subject
* @param array $participants
* An array with the participants numbers.
*
* @return string
* The group ID.
*/
public function sendGroupsChatCreate($subject, $participants = array())
{
$groupHash = array();
$groupHash["xmlns"] = "w:g";
$groupHash["action"] = "create";
$groupHash["subject"] = $subject;
$group = new ProtocolNode("group", $groupHash, null, "");
$setHash = array();
$setHash["id"] = $this->createMsgId("creategroup");
$setHash["type"] = "set";
$setHash["to"] = static::WHATSAPP_GROUP_SERVER;
$groupNode = new ProtocolNode("iq", $setHash, array($group), "");
$this->sendNode($groupNode);
$this->waitForServer($setHash["id"]);
$groupId = $this->groupId;
if (count($participants) > 0) {
$this->sendGroupsParticipantsAdd($groupId, $participants);
}
return $groupId;
}
/**
* End or delete a group chat
*
* @param string $gjid The group ID
*/
public function sendGroupsChatEnd($gjid)
{
$gjid = $this->getJID($gjid);
$msgID = $this->createMsgId("endgroup");
$groupData = array();
$groupData['id'] = $gjid;
$groupNode = new ProtocolNode('group', $groupData, null, null);
$leaveData = array();
$leaveData["xmlns"] = "w:g";
$leaveData["action"] = "delete";
$leaveNode = new ProtocolNode("leave", $leaveData, array($groupNode), null);
$iqData = array();
$iqData["id"] = $msgID;
$iqData["type"] = "set";
$iqData["to"] = static::WHATSAPP_GROUP_SERVER;
$iqNode = new ProtocolNode("iq", $iqData, array($leaveNode), null);
$this->sendNode($iqNode);
$this->waitForServer($msgID);
}
/**
* Leave a group chat
*
* @param array $gjids An array of group IDs
*/
public function sendGroupsLeave($gjids)
{
if (!is_array($gjids)) {
$gjids = array($this->getJID($gjids));
}
$nodes = array();
foreach ($gjids as $gjid) {
$nodes[] = new ProtocolNode("group", array("id" => $this->getJID($gjid)), null, null);
}
$leave = new ProtocolNode("leave", array("xmlns" => "w:g", 'action'=>'delete'), $nodes, null);
$hash = array();
$hash["id"] = $this->createMsgId("leavegroups");
$hash["to"] = static::WHATSAPP_GROUP_SERVER;
$hash["type"] = "set";
$node = new ProtocolNode("iq", $hash, array($leave), null);
$this->sendNode($node);
$this->waitForServer($hash["id"]);
}
/**
* Add participant(s) to a group.
*
* @param string $groupId
* The group ID.
* @param array $participants
* An array with the participants numbers to add
*/
public function sendGroupsParticipantsAdd($groupId, $participants)
{
if(!is_array($participants)) {
$participants = array($participants);
}
$this->sendGroupsChangeParticipants($groupId, $participants, 'add');
}
/**
* Remove participant(s) from a group.
*
* @param string $groupId
* The group ID.
* @param array $participants
* An array with the participants numbers to remove
*/
public function sendGroupsParticipantsRemove($groupId, $participants)
{
if(!is_array($participants)) {
$participants = array($participants);
}
$this->sendGroupsChangeParticipants($groupId, $participants, 'remove');
}
/**
* Send a text message to the user/group.
*
* @param $to
* The recipient.
* @param string $txt
* The text message.
*/
public function sendMessage($to, $txt)
{
$txt = $this->parseMessageForEmojis($txt);
$bodyNode = new ProtocolNode("body", null, null, $txt);
$this->sendMessageNode($to, $bodyNode);
}
/**
* Send audio to the user/group. *
*
* @param $to
* The recipient.
* @param string $filepath
* The url/uri to the audio file.
* @param bool $storeURLmedia Keep copy of file
* @return bool
*/
public function sendMessageAudio($to, $filepath, $storeURLmedia = false)
{
$allowedExtensions = array('3gp', 'caf', 'wav', 'mp3', 'wma', 'ogg', 'aif', 'aac', 'm4a');
$size = 10 * 1024 * 1024; // Easy way to set maximum file size for this media type.
return $this->sendCheckAndSendMedia($filepath, $size, $to, 'audio', $allowedExtensions, $storeURLmedia);
}
/**
* Send the composing message status. When typing a message.
*
* @param string $to
* The recipient to send status to.
*/
public function sendMessageComposing($to)
{
$comphash = array();
$comphash['xmlns'] = 'http://jabber.org/protocol/chatstates';
$compose = new ProtocolNode("composing", $comphash, null, "");
$messageHash = array();
$messageHash["to"] = $this->getJID($to);
$messageHash["type"] = "chat";
$messageHash["id"] = $this->createMsgId("composing");
$messageHash["t"] = time();
$messageNode = new ProtocolNode("message", $messageHash, array($compose), "");
$this->sendNode($messageNode);
}
/**
* Send an image file to group/user
*
* @param string $to
* Recipient number
* @param string $filepath
* The url/uri to the image file.
* @param bool $storeURLmedia Keep copy of file
* @return bool
*/
public function sendMessageImage($to, $filepath, $storeURLmedia = false)
{
$allowedExtensions = array('jpg', 'jpeg', 'gif', 'png');
$size = 5 * 1024 * 1024; // Easy way to set maximum file size for this media type.
return $this->sendCheckAndSendMedia($filepath, $size, $to, 'image', $allowedExtensions, $storeURLmedia);
}
/**
* Send a location to the user/group.
*
* If no name is supplied , receiver will see large sized google map
* thumbnail of entered Lat/Long but NO name/url for location.
*
* With name supplied, a combined map thumbnail/name box is displayed
*
* @param array|string $to The recipient(s) to send to.
* @param float $long The longitude of the location eg 54.31652
* @param float $lat The latitude if the location eg -6.833496
* @param string $name (Optional) The custom name you would like to give this location.
* @param string $url (Optional) A URL to attach to the location.
*/
public function sendMessageLocation($to, $long, $lat, $name = null, $url = null)
{
$mediaHash = array();
$mediaHash['xmlns'] = "urn:xmpp:whatsapp:mms";
$mediaHash['type'] = "location";
$mediaHash['latitude'] = $lat;
$mediaHash['longitude'] = $long;
$mediaHash['name'] = $name;
$mediaHash['url'] = $url;
$mediaNode = new ProtocolNode("media", $mediaHash, null, null);
if (is_array($to)) {
$this->sendBroadcast($to, $mediaNode);
} else {
$this->sendMessageNode($to, $mediaNode);
}
}
/**
* Send the 'paused composing message' status.
*
* @param string $to
* The recipient number or ID.
*/
public function sendMessagePaused($to)
{
$comphash = array();
$comphash['xmlns'] = 'http://jabber.org/protocol/chatstates';
$compose = new ProtocolNode("paused", $comphash, null, "");
$messageHash = array();
$messageHash["to"] = $this->getJID($to);
$messageHash["type"] = "chat";
$messageHash["id"] = $this->createMsgId("paused");
$messageHash["t"] = time();