-
Notifications
You must be signed in to change notification settings - Fork 6
/
xmlrpc.php
1800 lines (1347 loc) · 55.8 KB
/
xmlrpc.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
# fix for mozBlog and other cases where '<?xml' isn't on the very first line
$HTTP_RAW_POST_DATA=trim($HTTP_RAW_POST_DATA);
include("b2config.php");
require_once($b2inc."/xmlrpc.inc");
require_once($b2inc."/xmlrpcs.inc");
require_once($b2inc."/b2template.functions.php");
require_once($b2inc."/b2functions.php");
require_once($b2inc."/b2vars.php");
$use_cache = 1;
$post_autobr = 1;
$post_default_title = ""; // posts submitted via the xmlrpc interface get that title
$post_default_category = 1; // posts submitted via the xmlrpc interface go into that category
$xmlrpc_logging = 0;
function logIO($io,$msg) {
global $xmlrpc_logging;
if ($xmlrpc_logging) {
$fp = fopen("./xmlrpc.log","a+");
$date = date("Y-m-d H:i:s ");
$iot = ($io == "I") ? " Input: " : " Output: ";
fwrite($fp, "\n\n".$date.$iot.$msg);
fclose($fp);
}
return true;
}
function starify($string) {
$i = strlen($string);
return str_repeat('*', $i);
}
logIO("I",$HTTP_RAW_POST_DATA);
/**** B2 API ****/
# note: the b2 API currently consists of the Blogger API,
# plus the following methods:
#
# b2.newPost , b2.getCategories
# Note: the b2 API will be replaced by the standard Weblogs.API once the specs are defined.
### b2.newPost ###
$b2newpost_sig=array(array($xmlrpcString, $xmlrpcString, $xmlrpcString, $xmlrpcString, $xmlrpcString, $xmlrpcString, $xmlrpcBoolean, $xmlrpcString, $xmlrpcString, $xmlrpcString));
$b2newpost_doc='Adds a post, blogger-api like, +title +category +postdate';
function b2newpost($m) {
global $xmlrpcerruser; // import user errcode value
global $blog_ID,$cache_userdata,$tableposts,$use_rss,$use_weblogsping,$post_autobr;
global $post_default_title,$post_default_category;
global $cafelogID, $sleep_after_edit;
$err="";
dbconnect();
$username=$m->getParam(2);
$password=$m->getParam(3);
$content=$m->getParam(4);
$title=$m->getParam(6);
$category=$m->getParam(7);
$postdate=$m->getParam(8);
$username = $username->scalarval();
$password = $password->scalarval();
$content = $content->scalarval();
$title = $title->scalarval();
$category = $category->scalarval();
$postdate = $postdate->scalarval();
if (user_pass_ok($username,$password)) {
$userdata = get_userdatabylogin($username);
$user_ID = $userdata["ID"];
$user_level = $userdata["user_level"];
if ($user_level < 1) {
return new xmlrpcresp(0, $xmlrpcerruser+1, // user error 1
"Sorry, level 0 users can not post");
}
$content = format_to_post($content);
$post_title = addslashes($title);
$time_difference = get_settings("time_difference");
if ($postdate != "") {
$now = $postdate;
} else {
$now = date("Y-m-d H:i:s",(time() + ($time_difference * 3600)));
}
$sql = "INSERT INTO $tableposts (post_author, post_date, post_content, post_title, post_category) VALUES ('$user_ID','$now','$content','$post_title','$category')";
$result = mysql_query($sql);
if (!$result)
return new xmlrpcresp(0, $xmlrpcerruser+2, // user error 2
"For some strange yet very annoying reason, your entry couldn't be posted.");
$post_ID = mysql_insert_id();
if (!isset($blog_ID)) { $blog_ID = 1; }
if (isset($sleep_after_edit) && $sleep_after_edit > 0) {
sleep($sleep_after_edit);
}
rss_update($blog_ID);
pingWeblogs($blog_ID);
pingCafelog($cafelogID, $post_title, $post_ID);
pingBlogs($blog_ID);
pingback($content, $post_ID);
return new xmlrpcresp(new xmlrpcval("$post_ID"));
} else {
return new xmlrpcresp(0, $xmlrpcerruser+3, // user error 3
'Wrong username/password combination '.$username.' / '.starify($password));
}
}
### b2.getCategories ###
$b2getcategories_sig=array(array($xmlrpcString, $xmlrpcString, $xmlrpcString, $xmlrpcString));
$b2getcategories_doc='given a blogID, gives a struct that list categories in that blog, using categoryID and categoryName. categoryName is there so the user would choose a category name from the client, rather than just a number. however, when using b2.newPost, only the category ID number should be sent.';
function b2getcategories($m) {
global $xmlrpcerruser,$tablecategories;
dbconnect();
$blogid=$m->getParam(0);
$blogid = $blogid->scalarval(); // we dot not use that yet, that will be used with multiple blogs
$username=$m->getParam(1);
$username = $username->scalarval();
$password=$m->getParam(2);
$password = $password->scalarval();
$userdata = get_userdatabylogin($username);
if (user_pass_ok($username,$password)) {
$sql = "SELECT * FROM $tablecategories ORDER BY cat_ID ASC";
$result = mysql_query($sql) or die($sql);
$i = 0;
while($row = mysql_fetch_object($result)) {
$cat_name = $row->cat_name;
$cat_ID = $row->cat_ID;
$struct[$i] = new xmlrpcval(array("categoryID" => new xmlrpcval($cat_ID),
"categoryName" => new xmlrpcval($cat_name)
),"struct");
$i = $i + 1;
}
$data = array($struct[0]);
for ($j=1; $j<$i; $j++) {
array_push($data, $struct[$j]);
}
$resp = new xmlrpcval($data, "array");
return new xmlrpcresp($resp);
} else {
return new xmlrpcresp(0, $xmlrpcerruser+3, // user error 3
'Wrong username/password combination '.$username.' / '.starify($password));
}
}
### b2.getPostURL ###
$b2_getPostURL_sig = array(array($xmlrpcString, $xmlrpcString, $xmlrpcString, $xmlrpcString, $xmlrpcString, $xmlrpcString));
$b2_getPostURL_doc = 'Given a blog ID, username, password, and a post ID, returns the URL to that post.';
function b2_getPostURL($m) {
global $xmlrpcerruser, $tableposts;
global $siteurl, $blogfilename, $querystring_start, $querystring_equal, $querystring_separator;
dbconnect();
// ideally, this would be used:
// $blog_ID = $m->getParam(0);
// $blog_ID = $blog_ID->scalarval();
// but right now, b2 handles only one blog, so... :P
$blog_ID = 1;
$username=$m->getParam(2);
$username = $username->scalarval();
$password=$m->getParam(3);
$password = $password->scalarval();
$post_ID = $m->getParam(4);
$post_ID = intval($post_ID->scalarval());
$userdata = get_userdatabylogin($username);
if ($userdata["user_level"] < 1) {
return new xmlrpcresp(0, $xmlrpcerruser+1, // user error 1
"Sorry, users whose level is zero, can not use this method.");
}
if (user_pass_ok($username,$password)) {
$blog_URL = $siteurl.'/'.$blogfilename;
$postdata = get_postdata($post_ID);
if (!($postdata===false)) {
$title = preg_replace('/[^a-zA-Z0-9_\.-]/', '_', $postdata['Title']);
// this code is blatantly derived from permalink_link()
$archive_mode = get_settings('archive_mode');
switch($archive_mode) {
case 'daily':
$post_URL = $blog_URL.$querystring_start.'m'.$querystring_equal.substr($postdata['Date'],0,4).substr($postdata['Date'],5,2).substr($postdata['Date'],8,2).'#'.$title;
break;
case 'monthly':
$post_URL = $blog_URL.$querystring_start.'m'.$querystring_equal.substr($postdata['Date'],0,4).substr($postdata['Date'],5,2).'#'.$title;
break;
case 'weekly':
if((!isset($cacheweekly)) || (empty($cacheweekly[$postdata['Date']]))) {
$sql = "SELECT WEEK('".$postdata['Date']."')";
$result = mysql_query($sql);
$row = mysql_fetch_row($result);
$cacheweekly[$postdata['Date']] = $row[0];
}
$post_URL = $blog_URL.$querystring_start.'m'.$querystring_equal.substr($postdata['Date'],0,4).$querystring_separator.'w'.$querystring_equal.$cacheweekly[$postdata['Date']].'#'.$title;
break;
case 'postbypost':
$post_URL = $blog_URL.$querystring_start.'p'.$querystring_equal.$post_ID;
break;
}
} else {
$err = 'This post ID ('.$post_ID.') does not correspond to any post here.';
}
if ($err) {
return new xmlrpcresp(0, $xmlrpcerruser, $err);
} else {
return new xmlrpcresp(new xmlrpcval($post_URL));;
}
} else {
return new xmlrpcresp(0, $xmlrpcerruser+3, // user error 3
'Wrong username/password combination '.$username.' / '.starify($password));
}
}
/**** /B2 API ****/
/**** Blogger API ****/
# as described on http://plant.blogger.com/api and in various messages in http://groups.yahoo.com/group/bloggerDev/
#
# another list of these methods is there http://www.tswoam.co.uk/blogger_method_listing.html
# so you won't have to browse the eGroup to find all the methods
#
# special note: Evan please keep _your_ API page up to date :p
### blogger.newPost ###
$bloggernewpost_sig=array(array($xmlrpcString, $xmlrpcString, $xmlrpcString, $xmlrpcString, $xmlrpcString, $xmlrpcString, $xmlrpcBoolean));
$bloggernewpost_doc='Adds a post, blogger-api like';
function bloggernewpost($m) {
global $xmlrpcerruser; // import user errcode value
global $blog_ID,$cache_userdata,$tableposts,$use_rss,$use_weblogsping,$post_autobr;
global $post_default_title,$post_default_category;
global $cafelogID, $sleep_after_edit;
$err="";
dbconnect();
$username=$m->getParam(2);
$password=$m->getParam(3);
$content=$m->getParam(4);
$username = $username->scalarval();
$password = $password->scalarval();
$content = $content->scalarval();
if (user_pass_ok($username,$password)) {
$userdata = get_userdatabylogin($username);
$user_ID = $userdata["ID"];
$user_level = $userdata["user_level"];
if ($user_level < 1) {
return new xmlrpcresp(0, $xmlrpcerruser+1, // user error 1
"Sorry, level 0 users can not post");
}
$post_title = addslashes(xmlrpc_getposttitle($content));
$post_category = xmlrpc_getpostcategory($content);
$content = xmlrpc_removepostdata($content);
$content = format_to_post($content);
$time_difference = get_settings("time_difference");
$now = date("Y-m-d H:i:s",(time() + ($time_difference * 3600)));
$sql = "INSERT INTO $tableposts (post_author, post_date, post_content, post_title, post_category) VALUES ('$user_ID','$now','$content','$post_title','$post_category')";
$result = mysql_query($sql);
if (!$result)
return new xmlrpcresp(0, $xmlrpcerruser+2, // user error 2
"For some strange yet very annoying reason, your entry couldn't be posted.");
$post_ID = mysql_insert_id();
if (!isset($blog_ID)) { $blog_ID = 1; }
if (isset($sleep_after_edit) && $sleep_after_edit > 0) {
sleep($sleep_after_edit);
}
rss_update($blog_ID);
pingWeblogs($blog_ID);
pingCafelog($cafelogID, $post_title, $post_ID);
pingBlogs($blog_ID);
pingback($content, $post_ID);
logIO("O","Posted ! ID: $post_ID");
return new xmlrpcresp(new xmlrpcval("$post_ID"));
} else {
logIO("O","Wrong username/password combination <b>$username / $password</b>");
return new xmlrpcresp(0, $xmlrpcerruser+3, // user error 3
'Wrong username/password combination '.$username.' / '.starify($password));
}
}
### blogger.editPost ###
$bloggereditpost_sig=array(array($xmlrpcString, $xmlrpcString, $xmlrpcString, $xmlrpcString, $xmlrpcString, $xmlrpcString, $xmlrpcBoolean));
$bloggereditpost_doc='Edits a post, blogger-api like';
function bloggereditpost($m) {
global $xmlrpcerruser; // import user errcode value
global $blog_ID,$cache_userdata,$tableposts,$use_rss,$use_weblogsping,$post_autobr;
global $post_default_title,$post_default_category, $sleep_after_edit;
$err="";
dbconnect();
$post_ID=$m->getParam(1);
$username=$m->getParam(2);
$password=$m->getParam(3);
$newcontent=$m->getParam(4);
$post_ID = $post_ID->scalarval();
$username = $username->scalarval();
$password = $password->scalarval();
$newcontent = $newcontent->scalarval();
$sql = "SELECT * FROM $tableposts WHERE ID = '$post_ID'";
$result = @mysql_query($sql);
if (!$result)
return new xmlrpcresp(0, $xmlrpcerruser+2, // user error 2
"No such post.");
$userdata = get_userdatabylogin($username);
$user_ID = $userdata["ID"];
$user_level = $userdata["user_level"];
$postdata=get_postdata($post_ID);
$post_authordata=get_userdata($postdata["Author_ID"]);
$post_author_ID=$postdata["Author_ID"];
if (($user_ID != $post_author_ID) && ($user_level <= $post_authordata["user_level"])) {
return new xmlrpcresp(0, $xmlrpcerruser+1, // user error 1
"Sorry, you do not have the right to edit this post");
}
if (user_pass_ok($username,$password)) {
if ($user_level < 1) {
return new xmlrpcresp(0, $xmlrpcerruser+1, // user error 1
"Sorry, level 0 users can not edit posts");
}
$content = $newcontent;
$post_title = addslashes(xmlrpc_getposttitle($content));
$post_category = xmlrpc_getpostcategory($content);
$content = xmlrpc_removepostdata($content);
$content = format_to_post($content);
$sql = "UPDATE $tableposts SET post_content='$content', post_title='$post_title', post_category='$post_category' WHERE ID = '$post_ID'";
$result = mysql_query($sql);
if (!$result)
return new xmlrpcresp(0, $xmlrpcerruser+2, // user error 2
"For some strange yet very annoying reason, the entry couldn't be edited.");
if (!isset($blog_ID)) { $blog_ID = 1; }
if (isset($sleep_after_edit) && $sleep_after_edit > 0) {
sleep($sleep_after_edit);
}
rss_update($blog_ID);
pingWeblogs($blog_ID);
return new xmlrpcresp(new xmlrpcval("1", "boolean"));
} else {
return new xmlrpcresp(0, $xmlrpcerruser+3, // user error 3
'Wrong username/password combination '.$username.' / '.starify($password));
}
}
### blogger.deletePost ###
$bloggerdeletepost_sig=array(array($xmlrpcString, $xmlrpcString, $xmlrpcString, $xmlrpcString, $xmlrpcString, $xmlrpcBoolean));
$bloggerdeletepost_doc='Deletes a post, blogger-api like';
function bloggerdeletepost($m) {
global $xmlrpcerruser; // import user errcode value
global $blog_ID,$cache_userdata,$tableposts,$use_rss,$use_weblogsping,$post_autobr;
global $post_default_title,$post_default_category, $sleep_after_edit;
$err="";
dbconnect();
$post_ID=$m->getParam(1);
$username=$m->getParam(2);
$password=$m->getParam(3);
$newcontent=$m->getParam(4);
$post_ID = $post_ID->scalarval();
$username = $username->scalarval();
$password = $password->scalarval();
$newcontent = $newcontent->scalarval();
$sql = "SELECT * FROM $tableposts WHERE ID = '$post_ID'";
$result = @mysql_query($sql);
if (!$result)
return new xmlrpcresp(0, $xmlrpcerruser+2, // user error 2
"No such post.");
$userdata = get_userdatabylogin($username);
$user_ID = $userdata["ID"];
$user_level = $userdata["user_level"];
$postdata=get_postdata($post_ID);
$post_authordata=get_userdata($postdata["Author_ID"]);
$post_author_ID=$postdata["Author_ID"];
if (($user_ID != $post_author_ID) && ($user_level <= $post_authordata["user_level"])) {
return new xmlrpcresp(0, $xmlrpcerruser+1, // user error 1
"Sorry, you do not have the right to delete this post");
}
if (user_pass_ok($username,$password)) {
if ($user_level < 1) {
return new xmlrpcresp(0, $xmlrpcerruser+1, // user error 1
"Sorry, level 0 users can not delete posts");
}
$sql = "DELETE FROM $tableposts WHERE ID = '$post_ID'";
$result = mysql_query($sql);
if (!$result)
return new xmlrpcresp(0, $xmlrpcerruser+2, // user error 2
"For some strange yet very annoying reason, the entry couldn't be deleted.");
if (!isset($blog_ID)) { $blog_ID = 1; }
if (isset($sleep_after_edit) && $sleep_after_edit > 0) {
sleep($sleep_after_edit);
}
rss_update($blog_ID);
pingWeblogs($blog_ID);
return new xmlrpcresp(new xmlrpcval(1));
} else {
return new xmlrpcresp(0, $xmlrpcerruser+3, // user error 3
'Wrong username/password combination '.$username.' / '.starify($password));
}
}
### blogger.getUsersBlogs ###
$bloggergetusersblogs_sig=array(array($xmlrpcString, $xmlrpcString, $xmlrpcString, $xmlrpcString));
$bloggergetusersblogs_doc='returns the user\'s blogs - this is a dummy function, just so that BlogBuddy and other blogs-retrieving apps work';
function bloggergetusersblogs($m) {
// this function will have a real purpose with CafeLog's multiple blogs capability
global $xmlrpcerruser,$siteurl,$blogfilename,$blogname;
global $tableusers;
$user_login = $m->getParam(1);
$user_login = $user_login->scalarval();
dbconnect();
$sql = "SELECT user_level FROM $tableusers WHERE user_login = '$user_login' AND user_level > 3";
$result = mysql_query($sql) or die($sql."<br />".mysql_error());
$is_admin = mysql_num_rows($result);
$struct = new xmlrpcval(array("isAdmin" => new xmlrpcval($is_admin,"boolean"),
"url" => new xmlrpcval($siteurl."/".$blogfilename),
"blogid" => new xmlrpcval("1"),
"blogName" => new xmlrpcval($blogname)
),"struct");
$resp = new xmlrpcval(array($struct), "array");
return new xmlrpcresp($resp);
}
### blogger.getUserInfo ###
$bloggergetuserinfo_sig=array(array($xmlrpcString, $xmlrpcString, $xmlrpcString, $xmlrpcString));
$bloggergetuserinfo_doc='gives the info about a user';
function bloggergetuserinfo($m) {
global $xmlrpcerruser,$tableusers;
dbconnect();
$username=$m->getParam(1);
$username = $username->scalarval();
$password=$m->getParam(2);
$password = $password->scalarval();
$userdata = get_userdatabylogin($username);
if (user_pass_ok($username,$password)) {
$struct = new xmlrpcval(array("nickname" => new xmlrpcval($userdata["user_nickname"]),
"userid" => new xmlrpcval($userdata["ID"]),
"url" => new xmlrpcval($userdata["user_url"]),
"email" => new xmlrpcval($userdata["user_email"]),
"lastname" => new xmlrpcval($userdata["user_lastname"]),
"firstName" => new xmlrpcval($userdata["user_firstname"])
),"struct");
$resp = $struct;
return new xmlrpcresp($resp);
} else {
return new xmlrpcresp(0, $xmlrpcerruser+3, // user error 3
'Wrong username/password combination '.$username.' / '.starify($password));
}
}
### blogger.getPost ###
$bloggergetpost_sig=array(array($xmlrpcString, $xmlrpcString, $xmlrpcString, $xmlrpcString, $xmlrpcString));
$bloggergetpost_doc='fetches a post, blogger-api like';
function bloggergetpost($m) {
global $xmlrpcerruser,$tableposts;
dbconnect();
$post_ID=$m->getParam(1);
$post_ID = $post_ID->scalarval();
$username=$m->getParam(2);
$username = $username->scalarval();
$password=$m->getParam(3);
$password = $password->scalarval();
if (user_pass_ok($username,$password)) {
$postdata = get_postdata($post_ID);
if ($postdata["Date"] != "") {
$post_date = mysql2date("U", $postdata["Date"]);
$post_date = gmdate("Ymd", $post_date)."T".gmdate("H:i:s", $post_date);
$content = "<title>".stripslashes($postdata["Title"])."</title>";
$content .= "<category>".$postdata["Category"]."</category>";
$content .= stripslashes($postdata["Content"]);
$struct = new xmlrpcval(array("userid" => new xmlrpcval($postdata["Author_ID"]),
"dateCreated" => new xmlrpcval($post_date,"dateTime.iso8601"),
"content" => new xmlrpcval($content),
"postid" => new xmlrpcval($postdata["ID"])
),"struct");
$resp = $struct;
return new xmlrpcresp($resp);
} else {
return new xmlrpcresp(0, $xmlrpcerruser+3, // user error 4
"No such post #$post_ID");
}
} else {
return new xmlrpcresp(0, $xmlrpcerruser+3, // user error 3
'Wrong username/password combination '.$username.' / '.starify($password));
}
}
### blogger.getRecentPosts ###
$bloggergetrecentposts_sig=array(array($xmlrpcString, $xmlrpcString, $xmlrpcString, $xmlrpcString, $xmlrpcString, $xmlrpcInt));
$bloggergetrecentposts_doc='fetches X most recent posts, blogger-api like';
function bloggergetrecentposts($m) {
global $xmlrpcerruser,$tableposts;
error_reporting(0); // there is a bug in phpxmlrpc that makes it say there are errors while the output is actually valid, so let's disable errors for that function
dbconnect();
$blogid = 1; // we don't need that yet
$numposts=$m->getParam(4);
$numposts = $numposts->scalarval();
if ($numposts > 0) {
$limit = " LIMIT $numposts";
} else {
$limit = "";
}
$username=$m->getParam(2);
$username = $username->scalarval();
$password=$m->getParam(3);
$password = $password->scalarval();
if (user_pass_ok($username,$password)) {
$sql = "SELECT * FROM $tableposts WHERE post_category > 0 ORDER BY post_date DESC".$limit;
$result = mysql_query($sql);
if (!$result)
return new xmlrpcresp(0, $xmlrpcerruser+2, // user error 2
"For some strange yet very annoying reason, the entries couldn't be fetched.".mysql_error());
$data = new xmlrpcval("","array");
$i = 0;
while($row = mysql_fetch_object($result)) {
$postdata = array(
"ID" => $row->ID,
"Author_ID" => $row->post_author,
"Date" => $row->post_date,
"Content" => $row->post_content,
"Title" => $row->post_title,
"Category" => $row->post_category
);
$post_date = mysql2date("U", $postdata["Date"]);
$post_date = gmdate("Ymd", $post_date)."T".gmdate("H:i:s", $post_date);
$content = "<title>".stripslashes($postdata["Title"])."</title>";
$content .= "<category>".$postdata["Category"]."</category>";
$content .= stripslashes($postdata["Content"]);
# $content = convert_chars($content,"html");
# $content = $postdata["Title"];
$authordata = get_userdata($postdata["Author_ID"]);
switch($authordata["user_idmode"]) {
case "nickname":
$authorname = $authordata["user_nickname"];
case "login":
$authorname = $authordata["user_login"];
break;
case "firstname":
$authorname = $authordata["user_firstname"];
break;
case "lastname":
$authorname = $authordata["user_lastname"];
break;
case "namefl":
$authorname = $authordata["user_firstname"]." ".$authordata["user_lastname"];
break;
case "namelf":
$authorname = $authordata["user_lastname"]." ".$authordata["user_firstname"];
break;
default:
$authorname = $authordata["user_nickname"];
break;
}
$struct[$i] = new xmlrpcval(array("authorName" => new xmlrpcval($authorname),
"userid" => new xmlrpcval($postdata["Author_ID"]),
"dateCreated" => new xmlrpcval($post_date,"dateTime.iso8601"),
"content" => new xmlrpcval($content),
"postid" => new xmlrpcval($postdata["ID"])
),"struct");
$i = $i + 1;
}
$data = array($struct[0]);
for ($j=1; $j<$i; $j++) {
array_push($data, $struct[$j]);
}
$resp = new xmlrpcval($data, "array");
return new xmlrpcresp($resp);
} else {
return new xmlrpcresp(0, $xmlrpcerruser+3, // user error 3
'Wrong username/password combination '.$username.' / '.starify($password));
}
}
### blogger.getTemplate ###
# note: on b2, it fetches your $blogfilename, or b2.php if you didn't specify the variable
$bloggergettemplate_sig=array(array($xmlrpcString, $xmlrpcString, $xmlrpcString, $xmlrpcString, $xmlrpcString, $xmlrpcString));
$bloggergettemplate_doc='returns the default template file\'s code';
function bloggergettemplate($m) {
global $xmlrpcerruser,$tableusers,$blogfilename;
error_reporting(0); // there is a bug in phpxmlrpc that makes it say there are errors while the output is actually valid, so let's disable errors for that function
dbconnect();
$blogid = 1; // we do not need this yet
$templateType=$m->getParam(4);
$templateType = $templateType->scalarval();
$username=$m->getParam(2);
$username = $username->scalarval();
$password=$m->getParam(3);
$password = $password->scalarval();
$userdata = get_userdatabylogin($username);
if ($userdata["user_level"] < 3) {
return new xmlrpcresp(0, $xmlrpcerruser+1, // user error 1
"Sorry, users whose level is less than 3, can not edit the template.");
}
if (user_pass_ok($username,$password)) {
if ($templateType == "main") {
if ($blogfilename != "") {
$file = $blogfilename;
} else {
$file = "b2.php";
}
} elseif ($templateType == "archiveIndex") {
$file = "b2archives.php";
}
$f = fopen($file,"r");
$content = fread($f,filesize($file));
fclose($file);
$content = str_replace("\n","\r\n",$content); // so it is actually editable with a windows/mac client, instead of being returned as a looooooooooong line of code
return new xmlrpcresp(new xmlrpcval("$content"));
} else {
return new xmlrpcresp(0, $xmlrpcerruser+3, // user error 3
'Wrong username/password combination '.$username.' / '.starify($password));
}
}
### blogger.setTemplate ###
# note: on b2, it saves that in your $blogfilename, or b2.php if you didn't specify the variable
$bloggersettemplate_sig=array(array($xmlrpcString, $xmlrpcString, $xmlrpcString, $xmlrpcString, $xmlrpcString, $xmlrpcString, $xmlrpcString));
$bloggersettemplate_doc='saves the default template file\'s code';
function bloggersettemplate($m) {
global $xmlrpcerruser,$tableusers,$blogfilename;
error_reporting(0); // there is a bug in phpxmlrpc that makes it say there are errors while the output is actually valid, so let's disable errors for that function
dbconnect();
$blogid = 1; // we do not need this yet
$template=$m->getParam(4);
$template = $template->scalarval();
$templateType=$m->getParam(5);
$templateType = $templateType->scalarval();
$username=$m->getParam(2);
$username = $username->scalarval();
$password=$m->getParam(3);
$password = $password->scalarval();
$userdata = get_userdatabylogin($username);
if ($userdata["user_level"] < 3) {
return new xmlrpcresp(0, $xmlrpcerruser+1, // user error 1
"Sorry, users whose level is less than 3, can not edit the template.");
}
if (user_pass_ok($username,$password)) {
if ($templateType == "main") {
if ($blogfilename != "") {
$file = $blogfilename;
} else {
$file = "b2.php";
}
} elseif ($templateType == "archiveIndex") {
$file = "b2archives.php";
}
$f = fopen($file,"w+");
fwrite($f, $template);
fclose($file);
return new xmlrpcresp(new xmlrpcval("1", "boolean"));
} else {
return new xmlrpcresp(0, $xmlrpcerruser+3, // user error 3
'Wrong username/password combination '.$username.' / '.starify($password));
}
}
/**** /Blogger API ****/
/**** PingBack functions ****/
$pingback_ping_sig = array(array($xmlrpcString, $xmlrpcString, $xmlrpcString));
$pingback_ping_doc = 'gets a pingback and registers it as a comment prefixed by <pingback />';
function pingback_ping($m) {
// original code by Mort (http://mort.mine.nu:8080)
global $tableposts, $tablecomments, $comments_notify;
global $siteurl, $blogfilename, $b2_version, $use_pingback;
global $HTTP_SERVER_VARS;
if (!$use_pingback) {
return new xmlrpcresp(new xmlrpcval('Sorry, this weblog does not allow you to pingback its posts.'));
}
dbconnect();
$log = debug_fopen('./xmlrpc.log', 'w');
$title='';
$pagelinkedfrom = $m->getParam(0);
$pagelinkedfrom = $pagelinkedfrom->scalarval();
$pagelinkedto = $m->getParam(1);
$pagelinkedto = $pagelinkedto->scalarval();
$pagelinkedfrom = str_replace('&', '&', $pagelinkedfrom);
$pagelinkedto = preg_replace('#&([^amp\;])#is', '&$1', $pagelinkedto);
debug_fwrite($log, 'BEGIN '.time().' - '.date('Y-m-d H:i:s')."\n\n");
debug_fwrite($log, 'Page linked from: '.$pagelinkedfrom."\n");
debug_fwrite($log, 'Page linked to: '.$pagelinkedto."\n");
$messages = array(
htmlentities("Pingback from ".$pagelinkedfrom." to ".$pagelinkedto." registered. Keep the web talking! :-)"),
htmlentities("We can't find the URL to the post you are trying to link to in your entry. Please check how you wrote the post's permalink in your entry."),
htmlentities("We can't find the post you are trying to link to. Please check the post's permalink.")
);
$message = $messages[0];
// Check if the page linked to is in our site
$pos1 = strpos($pagelinkedto, str_replace('http://', '', str_replace('www.', '', $siteurl)));
if($pos1) {
// let's find which post is linked to
$urltest = parse_url($pagelinkedto);
if (preg_match('#p/[0-9]{1,}#', $urltest['path'], $match)) {
// the path defines the post_ID (archives/p/XXXX)
$blah = explode('/', $match[0]);
$post_ID = $blah[1];
$way = 'from the path';
} elseif (preg_match('#p=[0-9]{1,}#', $urltest['query'], $match)) {
// the querystring defines the post_ID (?p=XXXX)
$blah = explode('=', $match[0]);
$post_ID = $blah[1];
$way = 'from the querystring';
} elseif (isset($urltest['fragment'])) {
// an #anchor is there, it's either...
if (intval($urltest['fragment'])) {
// ...an integer #XXXX (simpliest case)
$post_ID = $urltest['fragment'];
$way = 'from the fragment (numeric)';
} elseif (is_string($urltest['fragment'])) {
// ...or a string #title, a little more complicated
$title = preg_replace('/[^a-zA-Z0-9]/', '.', $urltest['fragment']);
$sql = "SELECT ID FROM $tableposts WHERE post_title RLIKE '$title'";
$result = mysql_query($sql) or die("Query: $sql\n\nError: ".mysql_error());
$blah = mysql_fetch_array($result);
$post_ID = $blah['ID'];
$way = 'from the fragment (title)';
}
} else {
$post_ID = -1;
}
debug_fwrite($log, "Found post ID $way: $post_ID\n");
$sql = 'SELECT post_author FROM '.$tableposts.' WHERE ID = '.$post_ID;
$result = mysql_query($sql);
if (mysql_num_rows($result)) {
debug_fwrite($log, 'Post exists'."\n");
// Let's check that the remote site didn't already pingback this entry
$sql = 'SELECT * FROM '.$tablecomments.' WHERE comment_post_ID = '.$post_ID.' AND comment_author_url = \''.$pagelinkedfrom.'\' AND comment_content LIKE \'%<pingback />%\'';
$result = mysql_query($sql);
if (mysql_num_rows($result) || (1==1)) {
// very stupid, but gives time to the 'from' server to publish !
sleep(1);
// Let's check the remote site
$fp = @fopen($pagelinkedfrom, 'r');
$puntero = 4096;
while($linea = fread($fp, $puntero)) {
$linea = strip_tags($linea, '<title><a>');
$linea = strip_all_but_one_link($linea, $pagelinkedto);
$linea = preg_replace('#&([^amp\;])#is', '&$1', $linea);
if (empty($matchtitle)) {
preg_match('|<title>([^<]*?)</title>|is', $linea, $matchtitle);
}
$pos2 = strpos($linea, $pagelinkedto);
$pos3 = strpos($linea, str_replace('http://www.', 'http://', $pagelinkedto));
if (is_integer($pos2) || is_integer($pos3)) {
debug_fwrite($log, 'The page really links to us :)'."\n");
$pos4 = (is_integer($pos2)) ? $pos2 : $pos3;