This repository has been archived by the owner on Feb 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
gplus.php
720 lines (679 loc) · 27.1 KB
/
gplus.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
<?php
/*
* J. Eckman - editing for use with WPBOok
*/
// Based on: Dmitry Sandalov's Twitter 2 Google Plus CrossPost PHP script
// Credits for original script:
// Luka Pusic [email protected]
// Vladimir Smirnoff http://orl.sumy.ua [email protected]
// Kichrum http://Kichrum.org.ua
// (!) Works only with Google 2-step auth turned off AND mobile terms of service accepted
function wpgplus_safe_post_google($post_id) {
wpgplus_debug(date("Y-m-d H:i:s",time())." : wgplus_safe_post_google running, post_id is " . $post_id ."\n");
wpgplus_login(wpgplus_login_data());
wpgplus_debug(date("Y-m-d H:i:s",time())." : wgplus_safe_post_google running, past log in\n");
sleep(5);
wpgplus_update_profile_status($post_id);
sleep(5);
wpgplus_debug(date("Y-m-d H:i:s",time())." : wgplus_safe_post_google running, past update\n");
wpgplus_logout();
return true;
}
// GET: http://plus.google.com/, Parse the webpage and collect form data
// returns the login data to be used in the wpgplus_login function
function wpgplus_login_data() {
$wpgplusOptions = wpgplus_getAdminOptions();
if (!empty($wpgplusOptions)) {
foreach ($wpgplusOptions as $key => $option)
$wpgplusOptions[$key] = $option;
}
$my_args = array('method' => 'GET',
'timeout' => '5',
'redirection' => '5',
'user-agent' => 'Mozilla/4.0 (compatible; MSIE 5.0; S60/3.0 NokiaN73-1/2.0(2.0617.0.0.7) Profile/MIDP-2.0 Configuration/CLDC-1.1)',
'blocking' => true,
'compress' => false,
'decompress' => true,
'ssl-verify' => false
);
$buf = wp_remote_get('https://plus.google.com/',$my_args);
wpgplus_debug(date("Y-m-d H:i:s",time())." : just requested the login info\n");
//wpgplus_debug("\nBuffer is\n". print_r($buf,true) . "\n");
//wpgplus_debug("\nWriting cookies from login get\n");
if(is_wp_error($buf)) {
wp_die($buf);
}
foreach($buf['cookies'] as $cookie) {
//wpgplus_debug("\nThis cookie is ". print_r($cookie,true) . "\n");
wpgplus_set_cookie($cookie);
}
$toreturn = '';
$doc = new DOMDocument;
@$doc->loadHTML($buf['body']);
$inputs = $doc->getElementsByTagName('input');
foreach ($inputs as $input) {
switch ($input->getAttribute('name')) {
case 'Email': $toreturn .= 'Email=' . urlencode($wpgplusOptions['wpgplus_username']) . '&'; break;
case 'Passwd':$toreturn .= 'Passwd=' . urlencode($wpgplusOptions['wpgplus_password']) . '&'; break;
default: $toreturn .= $input->getAttribute('name') . '=' . urlencode($input->getAttribute('value')) . '&';
}
}
// return array (string postdata, string postaction)
$my_form = $doc->getElementsByTagName('form');
if($my_form) {
$my_action_url = $my_form->item(0)->getAttribute('action');
return array(wpgplus_tidy($toreturn), $doc->getElementsByTagName('form')->item(0)->getAttribute('action'));
} else {
wpgplus_debug(date("Y-m-d H:i:s",time())." : Did not get a form to post login to\n");
wp_die('WPGPlus did not get a form in the response from google+');
}
}
// POST login: https://accounts.google.com/ServiceLoginAuth
// Follow a series of redirects and set cookies
function wpgplus_login($postdata) {
wpgplus_debug("\nPOSTing username and pass to: " . $postdata[1] . "\n\n");
$cookies = array();
$my_cookie = wpgplus_get_cookie('GAPS'); // recreate cookie object
if($my_cookie) {
$cookies[] = $my_cookie;
}
$my_cookie2 = wpgplus_get_cookie('GALX'); // recreate cookie object
if($my_cookie2) {
$cookies[] = $my_cookie2;
}
$my_args = array('method' => 'POST',
'timeout' => '45',
'redirection' => 0,
'user-agent' => 'Mozilla/4.0 (compatible; MSIE 5.0; S60/3.0 NokiaN73-1/2.0(2.0617.0.0.7) Profile/MIDP-2.0 Configuration/CLDC-1.1)',
'blocking' => true,
'compress' => false,
'decompress' => true,
'ssl-verify' => false,
'body' => $postdata[0],
'cookies' => $cookies,
);
// First POST, to ServiceLoginAuth
$buf = wp_remote_post($postdata[1],$my_args);
if(is_wp_error($buf)) {
wp_die($buf);
}
// need to check to see what cookies are new or updated
// $new_cookies = array of cookies returned by post
// $cookies = array of existing cookies sent
$new_cookies = $buf['cookies'];
for($x = count($cookies); $x>0; $x--) {
// if cookie is already in $cookies array, we remove old version
foreach($new_cookies as $new_cookie) {
if($new_cookie->name == $cookies[$x]->name) {
unset($cookies[$x]);
}
}
}
// now that existing cookies are out of the array, add all back in
foreach($new_cookies as $cookie) {
wpgplus_set_cookie($cookie);
$cookies[] = $cookie;
}
$my_redirect = $buf['headers']['location'];
//wpgplus_debug("\nLine 120, My Redirect was ". $my_redirect ."\n");
if($my_redirect) {
$my_args = array('method' => 'GET',
'timeout' => '45',
'user-agent' => 'Mozilla/4.0 (compatible; MSIE 5.0; S60/3.0 NokiaN73-1/2.0(2.0617.0.0.7) Profile/MIDP-2.0 Configuration/CLDC-1.1)',
'blocking' => true,
'ssl-verify' => false,
'redirection' => 0,
'cookies' => $cookies,
);
$buf = wp_remote_get($my_redirect,$my_args);
if(is_wp_error($buf)) {
wp_die($buf);
}
// need to check to see what cookies are new or updated
// $new_cookies = array of cookies returned by post
// $cookies = array of existing cookies sent
$new_cookies = $buf['cookies'];
for($x = count($cookies); $x>0; $x--) {
// if cookie is already in $cookies array, we remove old version
foreach($new_cookies as $new_cookie) {
if($new_cookie->name == $cookies[$x]->name) {
unset($cookies[$x]);
}
}
}
// now that existing cookies are out of the array, add all back in
foreach($new_cookies as $cookie) {
wpgplus_set_cookie($cookie);
$cookies[] = $cookie;
}
$my_redirect = $buf['headers']['location'];
//wpgplus_debug("\nLine 154, My Redirect was ". $my_redirect ."\n");
} // end of if loop for additional redirect
if($my_redirect) {
$my_args = array('method' => 'GET',
'timeout' => '45',
'user-agent' => 'Mozilla/4.0 (compatible; MSIE 5.0; S60/3.0 NokiaN73-1/2.0(2.0617.0.0.7) Profile/MIDP-2.0 Configuration/CLDC-1.1)',
'blocking' => true,
'ssl-verify' => false,
'cookies' => $cookies,
'redirection' => 0,
);
/* * GET to https://accounts.google.com/CheckCookie
* = 302 redirect to https://plus.google.com/app/plus/x/?login
*/
$buf = wp_remote_get($my_redirect,$my_args);
if(is_wp_error($buf)) {
wp_die($buf);
}
// need to check to see what cookies are new or updated
// $new_cookies = array of cookies returned by post
// $cookies = array of existing cookies sent
$new_cookies = $buf['cookies'];
for($x = count($cookies); $x>0; $x--) {
// if cookie is already in $cookies array, we remove old version
foreach($new_cookies as $new_cookie) {
if($new_cookie->name == $cookies[$x]->name) {
unset($cookies[$x]);
}
}
}
// now that existing cookies are out of the array, add all back in
foreach($new_cookies as $cookie) {
wpgplus_set_cookie($cookie);
$cookies[] = $cookie;
}
$my_redirect = $buf['headers']['location'];
//wpgplus_debug("\nLine 189, My Redirect was ". $my_redirect ."\n");
} // end of if loop for redirect
if(substr($my_redirect, 0, 1) == '/') {
$my_redirect = 'http://plus.google.com' . $my_redirect;
}
if($my_redirect) {
$my_args = array('method' => 'GET',
'timeout' => '45',
'user-agent' => 'Mozilla/4.0 (compatible; MSIE 5.0; S60/3.0 NokiaN73-1/2.0(2.0617.0.0.7) Profile/MIDP-2.0 Configuration/CLDC-1.1)',
'blocking' => true,
'ssl-verify' => false,
'cookies' => $cookies,
'redirection' => 0,
);
/* GET to https://plus.google.com/app/plus/x/?login
* = 302 redirect to https://plus.google.com/app/plus/x/?login=1
*/
$buf = wp_remote_get($my_redirect,$my_args);
if(is_wp_error($buf)) {
wp_die($buf);
}
// need to check to see what cookies are new or updated
// $new_cookies = array of cookies returned by post
// $cookies = array of existing cookies sent
$new_cookies = $buf['cookies'];
for($x = count($cookies); $x>0; $x--) {
// if cookie is already in $cookies array, we remove old version
foreach($new_cookies as $new_cookie) {
if($new_cookie->name == $cookies[$x]->name) {
unset($cookies[$x]);
}
}
}
// now that existing cookies are out of the array, add all back in
foreach($new_cookies as $cookie) {
wpgplus_set_cookie($cookie);
$cookies[] = $cookie;
}
$my_redirect = $buf['headers']['location'];
wpgplus_debug("\nLine 224, My Redirect was ". $my_redirect ."\n");
} // end of if loop for redirect
if ($my_redirect) {
$my_args = array('method' => 'GET',
'timeout' => '45',
'user-agent' => 'Mozilla/4.0 (compatible; MSIE 5.0; S60/3.0 NokiaN73-1/2.0(2.0617.0.0.7) Profile/MIDP-2.0 Configuration/CLDC-1.1)',
'blocking' => true,
'ssl-verify' => false,
'cookies' => $cookies,
'redirection' => 0,
);
/* GET to https://plus.google.com/app/plus/x/?login=1
* = 302 redirect to https://plus.google.com/app/plus/x/#/?login=1
*/
$buf = wp_remote_get($my_redirect,$my_args);
if(is_wp_error($buf)) {
wp_die($buf);
}
// need to check to see what cookies are new or updated
// $new_cookies = array of cookies returned by post
// $cookies = array of existing cookies sent
$new_cookies = $buf['cookies'];
for($x = count($cookies); $x>0; $x--) {
// if cookie is already in $cookies array, we remove old version
foreach($new_cookies as $new_cookie) {
if($new_cookie->name == $cookies[$x]->name) {
unset($cookies[$x]);
}
}
}
// now that existing cookies are out of the array, add all back in
foreach($new_cookies as $cookie) {
wpgplus_set_cookie($cookie);
$cookies[] = $cookie;
}
$my_redirect = 'https://plus.google.com' . $buf['headers']['location']; // for some reason this time the url is relative
wpgplus_debug("\nLine 259, My Redirect was ". $my_redirect ."\n");
} // end of if loop for redirect
if ($my_redirect) {
$my_args = array('method' => 'GET',
'timeout' => '45',
'user-agent' => 'Mozilla/4.0 (compatible; MSIE 5.0; S60/3.0 NokiaN73-1/2.0(2.0617.0.0.7) Profile/MIDP-2.0 Configuration/CLDC-1.1)',
'blocking' => true,
'ssl-verify' => false,
'cookies' => $cookies,
'redirection' => 0,
);
/* GET to https://plus.google.com/app/plus/x/#/?login=1
* = 302 redirect to https://plus.google.com/app/plus/x/?v=stream
*/
$buf = wp_remote_get($my_redirect,$my_args);
if(is_wp_error($buf)) {
wp_die($buf);
}
// need to check to see what cookies are new or updated
// $new_cookies = array of cookies returned by post
// $cookies = array of existing cookies sent
$new_cookies = $buf['cookies'];
for($x = count($cookies); $x>0; $x--) {
// if cookie is already in $cookies array, we remove old version
foreach($new_cookies as $new_cookie) {
if($new_cookie->name == $cookies[$x]->name) {
unset($cookies[$x]);
}
}
}
// now that existing cookies are out of the array, add all back in
foreach($new_cookies as $cookie) {
wpgplus_set_cookie($cookie);
$cookies[] = $cookie;
}
$my_redirect = $buf['headers']['location'];
wpgplus_debug("\nLine 294, My Redirect was ". $my_redirect ."\n");
} // end of if loop for redirect
if ($my_redirect) {
$my_args = array('method' => 'GET',
'timeout' => '45',
'user-agent' => 'Mozilla/4.0 (compatible; MSIE 5.0; S60/3.0 NokiaN73-1/2.0(2.0617.0.0.7) Profile/MIDP-2.0 Configuration/CLDC-1.1)',
'blocking' => true,
'ssl-verify' => false,
'cookies' => $cookies,
'redirection' => 0,
);
/* GET to https://plus.google.com/app/plus/x/?v=stream
* = 302 redirect to https://plus.google.com/app/plus/x/code/?v=stream
*/
$buf = wp_remote_get($my_redirect,$my_args);
if(is_wp_error($buf)) {
wp_die($buf);
}
// need to check to see what cookies are new or updated
// $new_cookies = array of cookies returned by post
// $cookies = array of existing cookies sent
$new_cookies = $buf['cookies'];
for($x = count($cookies); $x>0; $x--) {
// if cookie is already in $cookies array, we remove old version
foreach($new_cookies as $new_cookie) {
if($new_cookie->name == $cookies[$x]->name) {
unset($cookies[$x]);
}
}
}
// now that existing cookies are out of the array, add all back in
foreach($new_cookies as $cookie) {
wpgplus_set_cookie($cookie);
$cookies[] = $cookie;
}
wpgplus_debug(date("Y-m-d H:i:s",time())." : login data posted\n");
//wpgplus_debug("\n cookies were \n" . print_r($cookies,true) ."\n");
//wpgplus_debug("\n Postdata was \n" . print_r($postdata[0],true) ."\n");
wpgplus_debug("\n Response from Google was \n" . print_r($buf['body'],true) ."\n");
} // last if loop for redirect in login sequence
}
// Prepare the update, get the right form, post the update
function wpgplus_update_profile_status($post_id) { $wpgplus_debug_file= WP_PLUGIN_DIR .'/wpgplus/debug.txt';
/* Set up the post */
global $more;
$more = 0; //only the post teaser please
$my_post = get_post($post_id);
wpgplus_debug(date("Y-m-d H:i:s",time())." : Inside update_profile_states with post_id ". $post_id ." \n");
if(!empty($my_post->post_password)) { // post is password protected, don't post
wpgplus_debug(date("Y-m-d H:i:s",time())." : Post is password protected\n");
return;
}
if(get_post_type($my_post->ID) != 'post') { // only do this for posts
wpgplus_debug(date("Y-m-d H:i:s",time())." : Post type is ". get_post_type($my_post->ID) ."\n");
return;
}
$my_post_text = get_post_meta($post_id,'wpgplus_message',true); // if we have a post_message
if ($my_post_text == '') {
if(($my_post->post_excerpt) && ($my_post->post_excerpt != '')) {
$my_post_text = stripslashes(wp_filter_nohtml_kses(apply_filters('the_content',$my_post->post_excerpt)));
} else {
$my_post_text = stripslashes(wp_filter_nohtml_kses(apply_filters('the_content',$my_post->post_content)));
}
}
if(strlen($my_post_text) >= 995) {
$space_index = strrpos(substr($my_post_text, 0, 995), ' ');
$short_desc = substr($my_post_text, 0, $space_index);
$short_desc .= '...';
$my_post_text = $short_desc;
}
/* Now let's go get the form */
wpgplus_debug(date("Y-m-d H:i:s",time())." : Getting form for posting\n");
//wpgplus_debug(date("Y-m-d H:i:s",time())." : Post text is ". $my_post_text ."\n");
// These are the cookies I know of - not sure all are needed
$cookies = array();
$my_cookies = array('GAPS','GALX','NID','SID','LSID','HSID','SSID','APISID','SAPISID');
foreach ($my_cookies as $name) {
$new_cookie = wpgplus_get_cookie($name);
if($new_cookie) {
$cookies[] = wpgplus_get_cookie($name);
}
}
//wpgplus_debug("\nAbout to get form, cookies are ". print_r($cookies,true) ."\n");
$my_args = array('method' => 'GET',
'timeout' => '45',
'redirection' => 0,
'user-agent' => 'Mozilla/4.0 (compatible; MSIE 5.0; S60/3.0 NokiaN73-1/2.0(2.0617.0.0.7) Profile/MIDP-2.0 Configuration/CLDC-1.1)',
'blocking' => true,
'compress' => false,
'decompress' => true,
'ssl-verify' => false,
'cookies' => $cookies,
);
// Need to get from this URL and follow redirects
$buf = wp_remote_request('https://plus.google.com/app/basic/share',$my_args);
if(is_wp_error($buf)) {
wp_die($buf);
}
// need to check to see what cookies are new or updated
// $new_cookies = array of cookies returned by post
// $cookies = array of existing cookies sent
$new_cookies = $buf['cookies'];
for($x = count($cookies); $x>0; $x--) {
// if cookie is already in $cookies array, we remove old version
foreach($new_cookies as $new_cookie) {
if($new_cookie->name == $cookies[$x]->name) {
//wpgplus_debug("\nUnsetting cookie for ". $cookies[$x]->name);
unset($cookies[$x]);
}
}
}
// now that existing cookies are out of the array, add all back in
foreach($new_cookies as $cookie) {
wpgplus_set_cookie($cookie);
$cookies[] = $cookie;
}
// in case form gets redirected to a new base url
$my_redirect = $buf['headers']['location'];
if(substr($my_redirect, 0, 1) == '/') {
$my_redirect = 'http://plus.google.com' . $my_redirect;
}
if ($my_redirect) {
wpgplus_debug("\nShould be past redirect for form, response was ". print_r($buf,true) ."\n");
wpgplus_debug("\nLine 430, My Redirect was ". $my_redirect ."\n");
wpgplus_debug("\nGetting form at redirected url, cookies are ". print_r($cookies,true) ."\n");
// need to reget the form at the new url
$my_args = array('method' => 'GET',
'timeout' => '45',
'redirection' => 0,
'user-agent' => 'Mozilla/4.0 (compatible; MSIE 5.0; S60/3.0 NokiaN73-1/2.0(2.0617.0.0.7) Profile/MIDP-2.0 Configuration/CLDC-1.1)',
'blocking' => true,
'compress' => false,
'decompress' => true,
'ssl-verify' => false,
'cookies' => $cookies,
);
$buf = wp_remote_request($my_redirect, $my_args);
if(is_wp_error($buf)) {
wp_die($buf);
}
$new_cookies = $buf['cookies'];
for($x = count($cookies); $x>0; $x--) {
// if cookie is already in $cookies array, we remove old version
foreach($new_cookies as $new_cookie) {
if($new_cookie->name == $cookies[$x]->name) {
//wpgplus_debug("\nUnsetting cookie for ". $cookies[$x]->name);
unset($cookies[$x]);
}
}
}
// now that existing cookies are out of the array, add all back in
foreach($new_cookies as $cookie) {
wpgplus_set_cookie($cookie);
$cookies[] = $cookie;
}
wpgplus_debug("\nGot new form at new url, cookies are now ". print_r($cookies,true) ."\n");
}
// now we get the form inputs, including hidden ones
wpgplus_debug("\nForm being parsed is this page: ". $buf['body'] ."\n");
$params = array();
$doc = new DOMDocument;
$doc->loadHTML($buf['body']);
$inputs = $doc->getElementsByTagName('input');
foreach ($inputs as $input) {
if (($input->getAttribute('type') == 'hidden')) {
$params[$input->getAttribute('name')] = $input->getAttribute('value');
}
}
if (function_exists('get_the_post_thumbnail') && has_post_thumbnail($my_post->ID)) {
$my_thumb_id = get_post_thumbnail_id($my_post->ID);
$my_thumb_array = wp_get_attachment_image_src($my_thumb_id);
$my_image = $my_thumb_array[0]; // this should be the url
} else {
$my_image = '';
}
/* first, lets submit the current form using the 'add image' button */
$params['cpPostMsg'] = '';
$params['currentPage'] = '1';
$params['buttonPressed'] = '2'; // pushign the second button should open the add image dialog
$params['cpPhotoTitle'] = '';
foreach ($params as $key => $value)
{
$post_items[] = $key . '=' . urlencode($value);
}
$params = implode ('&', $post_items);
// need to determine form url from the form action
sleep(6);
$forms = $doc->getElementsByTagName('form');
$baseurl = 'https://plus.google.com'. $forms->item(0)->getAttribute('action');
wpgplus_debug(date("Y-m-d H:i:s",time())." : Posting share form, to get image form\n");
//wpgplus_debug(date("Y-m-d H:i:s",time())." : and base url is ". $baseurl ."\n");
// This last post should NOT follow redirects
$my_args = array('method' => 'POST',
'timeout' => 45,
'user-agent' => 'Mozilla/4.0 (compatible; MSIE 5.0; S60/3.0 NokiaN73-1/2.0(2.0617.0.0.7) Profile/MIDP-2.0 Configuration/CLDC-1.1)',
'redirection' => 0,
'blocking' => true,
'compress' => false,
'decompress' => true,
'ssl-verify' => false,
'body' => $params,
'cookies' => $cookies,
'headers' => array('Referer' => 'https://plus.google.com/app/basic/share',
'Content-Type' => 'application/x-www-form-urlencoded',
'Content-Length' => strlen($params))
);
//wpgplus_debug(date("Y-m-d H:i:s",time())." : About to post form, my_args are ". print_r($my_args,true). "\n");
$buf = wp_remote_post($baseurl,$my_args);
if(is_wp_error($buf)) {
wp_die($buf);
}
$header = $buf['headers'];
//wpgplus_debug(date("Y-m-d H:i:s",time())." : Requested form, status was ". $buf['response']['code'] . "\n");
/* now we need to parse that form, to figure out how to send the images */
wpgplus_debug("\nForm being parsed for image posting \n");
$params = array();
$doc = new DOMDocument;
$doc->loadHTML($buf['body']);
$inputs = $doc->getElementsByTagName('input');
foreach ($inputs as $input) {
if (($input->getAttribute('type') == 'hidden')) {
$params[$input->getAttribute('name')] = $input->getAttribute('value');
}
}
$forms = $doc->getElementsByTagName('form');
/* the image posting form is the third form on the page */
$image_file = get_attached_file($my_thumb_id);
$referer = $baseurl;
$baseurl = 'https://plus.google.com'. $forms->item(2)->getAttribute('action');
wpgplus_debug(date("Y-m-d H:i:s",time())." : Image file is " . $image_file . "\n");
wpgplus_debug(date("Y-m-d H:i:s",time())." : Going to post image upload form, ");
wpgplus_debug(date("Y-m-d H:i:s",time())." : and base url is ". $baseurl ."\n");
$boundary = md5(time());
$payload = '';
// First, add the standard POST fields:
foreach ($params as $key => $value) {
$payload .= '--'. $boundary;
$payload .= "\r\n";
$payload .= 'Content-Disposition: form-data; name="'. $key .'"'."\r\n\r\n";
$payload .= $value;
$payload .= "\r\n";
}
// Add the file
$payload .= '--'. $boundary;
$payload .= "\r\n";
$payload .= 'Content-Disposition: form-data; name="photo_upload_file_name"; filename="' . basename($image_file) . '"' . "\r\n";
$payload .= 'Content-Type: image/png' . "\r\n"; // If you know the mime-type
$payload .= 'Content-Transfer-Encoding: binary' . "\r\n";
$payload .= "\r\n";
$payload .= file_get_contents($image_file);
$payload .= "\r\n";
$payload .= '--' . $boundary . '--';
$payload .= "\r\n\r\n";
$my_args = array('method' => 'POST',
'timeout' => 45,
'user-agent' => 'Mozilla/4.0 (compatible; MSIE 5.0; S60/3.0 NokiaN73-1/2.0(2.0617.0.0.7) Profile/MIDP-2.0 Configuration/CLDC-1.1)',
'redirection' => 0,
'blocking' => true,
'compress' => false,
'decompress' => true,
'ssl-verify' => false,
'body' => $payload,
'cookies' => $cookies,
'headers' => array('Referer' => $referer,
'content-type' => 'multipart/form-data; boundary='. $boundary,
'Content-Length' => strlen($payload))
);
wpgplus_debug(date("Y-m-d H:i:s",time())." : About to post form for uploading image, payload is ". $payload ."\n");
$buf = wp_remote_post($baseurl,$my_args);
if(is_wp_error($buf)) {
wp_die($buf);
}
$header = $buf['headers'];
wpgplus_debug(date("Y-m-d H:i:s",time())." : Posted form (image upload), status was ". $buf['response']['code'] . "\n");
//wpgplus_debug(date("Y-m-d H:i:s",time())." : Post text was ". $my_post_text ."\n");
//wpgplus_debug(date("Y-m-d H:i:s",time())." : Header of response was ". print_r($header,true) ."\n");
wpgplus_debug(date("Y-m-d H:i:s",time())." : Response was ". $buf['body'] ."\n");
/* then we're ready to post the whole thing, including image references */
/* really at this point we need to go get the whole original form perhaps, or at least reset the action */
$params['cpPostMsg'] = $my_post_text . ' ' . get_permalink($my_post);
$params['currentPage'] = '1';
$params['buttonPressed'] = '1';
$params['cpPhotoTitle'] = '';
foreach ($params as $key => $value)
{
$post_items[] = $key . '=' . urlencode($value);
}
$params = implode ('&', $post_items);
// need to determine form url from the form action
sleep(6);
$forms = $doc->getElementsByTagName('form');
$baseurl = 'https://plus.google.com'. $forms->item(0)->getAttribute('action');
wpgplus_debug(date("Y-m-d H:i:s",time())." : Going to publish, params is ". print_r($params,true) ."\n");
wpgplus_debug(date("Y-m-d H:i:s",time())." : and base url is ". $baseurl ."\n");
// This last post should NOT follow redirects
$my_args = array('method' => 'POST',
'timeout' => 45,
'user-agent' => 'Mozilla/4.0 (compatible; MSIE 5.0; S60/3.0 NokiaN73-1/2.0(2.0617.0.0.7) Profile/MIDP-2.0 Configuration/CLDC-1.1)',
'redirection' => 0,
'blocking' => true,
'compress' => false,
'decompress' => true,
'ssl-verify' => false,
'body' => $params,
'cookies' => $cookies,
'headers' => array('Referer' => 'https://plus.google.com/app/basic/share',
'Content-Type' => 'application/x-www-form-urlencoded',
'Content-Length' => strlen($params))
);
wpgplus_debug(date("Y-m-d H:i:s",time())." : About to post form, my_args are ". print_r($my_args,true). "\n");
$buf = wp_remote_post($baseurl,$my_args);
if(is_wp_error($buf)) {
wp_die($buf);
}
$header = $buf['headers'];
wpgplus_debug(date("Y-m-d H:i:s",time())." : Posted form, status was ". $buf['response']['code'] . "\n");
//wpgplus_debug(date("Y-m-d H:i:s",time())." : Post text was ". $my_post_text ."\n");
//wpgplus_debug(date("Y-m-d H:i:s",time())." : Header of response was ". print_r($header,true) ."\n");
wpgplus_debug(date("Y-m-d H:i:s",time())." : Body was ". $buf['body'] ."\n");
// seems like the item still isn't posted at this point?
}
// GET logout: Just logout to look more human like and reset cookie :)
function wpgplus_logout() {
wpgplus_debug("\nLogging out: \n");
// do we need to send cookies on logout?
$my_args = array('method' => 'GET',
'user-agent' => 'Mozilla/4.0 (compatible; MSIE 5.0; S60/3.0 NokiaN73-1/2.0(2.0617.0.0.7) Profile/MIDP-2.0 Configuration/CLDC-1.1)',
'blocking' => false,
'redirection' => 0,
);
$buf = wp_remote_request('https://www.google.com/m/logout',$my_args);
if(is_wp_error($buf)) {
wp_die($buf);
}// should we kill cookies here or just let the transients expire?
}
function wpgplus_tidy($str) {
return rtrim($str, "&");
}
// Expects an WP_Http_cookie object
function wpgplus_set_Cookie($my_cookie) {
// cookies which have an expiration date and it is past should not be set
if((!empty($my_cookie->expires)) && ($my_cookie->expires < time())) {
//wpgplus_debug("\nNot setting expired cookie for " . $my_cookie->name . "\n");
return false;
}
if(get_transient('wpgplus_cookie_' . $my_cookie->name)) {
//wpgplus_debug("\nUpdating cookie for " . $my_cookie->name . "\n");
} else {
//wpgplus_debug("\nSetting cookie for " . $my_cookie->name . "\n");
}
set_transient('wpgplus_cookie_'. $my_cookie->name,$my_cookie,60*60);
}
// Returns an WP_Http_Cookie object
function wpgplus_get_cookie($name) {
$my_cookie = get_transient('wpgplus_cookie_'. $name);
// wpgplus_debug("\nCookie is " . print_r($my_cookie,true) . "\n");
// Cookies which have an expiration date and it is passed should not be returned
if(!$my_cookie || ((!empty($my_cookie->expires)) && ($my_cookie->expires < time()))) {
//wpgplus_debug("\nNo cookies found for ". $name . "\n");
return false;
} else {
//wpgplus_debug("\nGetting cookie for ". $my_cookie->name . "\n");
return $my_cookie;
}
}
function wpgplus_debug($string) {
$wpgplusOptions = wpgplus_getAdminOptions();
if (!empty($wpgplusOptions)) {
foreach ($wpgplusOptions as $key => $option)
$wpgplusOptions[$key] = $option;
}
if($wpgplusOptions['wpgplus_debug'] == true) {
$wpgplus_debug_file= WP_PLUGIN_DIR .'/wpgplus/wpgplus_debug.txt';
$fp = @fopen($wpgplus_debug_file, 'a');
if ($fp) {
fwrite($fp, $string);
fclose($fp);
}
} else {
return false;
}
}
?>