Make WordPress Core

source: trunk/xmlrpc.php @ 1580

Last change on this file since 1580 was 1580, checked in by michelvaldrighi, 21 years ago

added Emmanuel Frecon's fix to create directories in mw.newMediaObject, removed some useless (and/or potentially dangerous) debug strings

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 22.7 KB
Line 
1<?php
2
3# fix for mozBlog and other cases where '<?xml' isn't on the very first line
4$HTTP_RAW_POST_DATA = trim($HTTP_RAW_POST_DATA);
5
6include('../wp-config.php');
7include_once(ABSPATH . WPINC . '/class-IXR.php');
8include_once(ABSPATH . WPINC . '/functions-post.php');
9
10// Turn off all warnings and errors.
11// error_reporting(0);
12
13$post_default_title = ""; // posts submitted via the xmlrpc interface get that title
14$post_default_category = 1; // posts submitted via the xmlrpc interface go into that category
15
16$xmlrpc_logging = 0;
17
18function logIO($io,$msg) {
19        global $xmlrpc_logging;
20        if ($xmlrpc_logging) {
21                $fp = fopen("../xmlrpc.log","a+");
22                $date = gmdate("Y-m-d H:i:s ");
23                $iot = ($io == "I") ? " Input: " : " Output: ";
24                fwrite($fp, "\n\n".$date.$iot.$msg);
25                fclose($fp);
26        }
27        return true;
28        }
29
30function starify($string) {
31        $i = strlen($string);
32        return str_repeat('*', $i);
33}
34
35logIO("I", $HTTP_RAW_POST_DATA);
36
37
38function printr($var, $do_not_echo = false) {
39        // from php.net/print_r user contributed notes
40        ob_start();
41        print_r($var);
42        $code =  htmlentities(ob_get_contents());
43        ob_clean();
44        if (!$do_not_echo) {
45          echo "<pre>$code</pre>";
46        }
47        return $code;
48}
49
50function mkdir_p($target) {
51        // from php.net/mkdir user contributed notes
52        if (file_exists($target)) {
53          if (!is_dir($target)) {
54            return false;
55          } else {
56            return true;
57          }
58        }
59
60        // Attempting to create the directory may clutter up our display.
61        if (@mkdir($target)) {
62          return true;
63        }
64
65        // If the above failed, attempt to create the parent node, then try again.
66        if (mkdir_p(dirname($target))) {
67          return mkdir_p($target);
68        }
69
70        return false;
71}
72
73
74class wp_xmlrpc_server extends IXR_Server {
75
76        function wp_xmlrpc_server() {
77                $this->IXR_Server(array(
78                  'blogger.getUsersBlogs' => 'this:blogger_getUsersBlogs',
79                  'blogger.getUserInfo' => 'this:blogger_getUserInfo',
80                  'blogger.getPost' => 'this:blogger_getPost',
81                  'blogger.getRecentPosts' => 'this:blogger_getRecentPosts',
82                  'blogger.getTemplate' => 'this:blogger_getTemplate',
83                  'blogger.setTemplate' => 'this:blogger_setTemplate',
84                  'blogger.newPost' => 'this:blogger_newPost',
85                  'blogger.editPost' => 'this:blogger_editPost',
86                  'blogger.deletePost' => 'this:blogger_deletePost',
87
88                  'metaWeblog.newPost' => 'this:mw_newPost',
89                  'metaWeblog.editPost' => 'this:mw_editPost',
90                  'metaWeblog.getPost' => 'this:mw_getPost',
91                  'metaWeblog.getRecentPosts' => 'this:mw_getRecentPosts',
92                  'metaWeblog.getCategories' => 'this:mw_getCategories',
93                  'metaWeblog.newMediaObject' => 'this:mw_newMediaObject',
94
95                  'demo.sayHello' => 'this:sayHello',
96                  'demo.addTwoNumbers' => 'this:addTwoNumbers'
97                ));
98        }
99
100        function sayHello($args) {
101                return 'Hello!';
102        }
103
104        function addTwoNumbers($args) {
105                $number1 = $args[0];
106                $number2 = $args[1];
107                return $number1 + $number2;
108        }
109
110        function login_pass_ok($user_login, $user_pass) {
111          if (!user_pass_ok($user_login, $user_pass)) {
112            $this->error = new IXR_Error(403, 'Bad login/pass combination.');
113            return false;
114          }
115          return true;
116        }
117
118
119
120
121        /* Blogger API functions
122         * specs on http://plant.blogger.com/api and http://groups.yahoo.com/group/bloggerDev/
123         */
124
125
126        /* blogger.getUsersBlogs will make more sense once we support multiple blogs */
127        function blogger_getUsersBlogs($args) {
128
129          $user_login = $args[1];
130          $user_pass  = $args[2];
131
132          if (!$this->login_pass_ok($user_login, $user_pass)) {
133            return $this->error;
134          }
135
136          $user_data = get_userdatabylogin($user_login);
137          $is_admin = $user_data->user_level > 3;
138
139          $struct = array(
140            'isAdmin'  => $is_admin,
141            'url'      => get_settings('home') .'/'.get_settings('blogfilename'),
142            'blogid'   => 1,
143            'blogName' => get_settings('blogname')
144          );
145
146          return array($struct);
147        }
148
149
150        /* blogger.getUsersInfo gives your client some info about you, so you don't have to */
151        function blogger_getUserInfo($args) {
152
153          $user_login = $args[1];
154          $user_pass  = $args[2];
155
156          if (!$this->login_pass_ok($user_login, $user_pass)) {
157            return $this->error;
158          }
159
160          $user_data = get_userdatabylogin($user_login);
161
162          $struct = array(
163            'nickname'  => $user_data->user_nickname,
164            'userid'    => $user_data->ID,
165            'url'       => $user_data->user_url,
166            'email'     => $user_data->user_email,
167            'lastname'  => $user_data->user_lastname,
168            'firstname' => $user_data->user_firstname
169          );
170
171          return $struct;
172        }
173
174
175        /* blogger.getPost ...gets a post */
176        function blogger_getPost($args) {
177
178          $post_ID    = $args[1];
179          $user_login = $args[2];
180          $user_pass  = $args[3];
181
182          if (!$this->login_pass_ok($user_login, $user_pass)) {
183            return $this->error;
184          }
185
186          $user_data = get_userdatabylogin($user_login);
187          $post_data = wp_get_single_post($post_ID, ARRAY_A);
188
189          $categories = implode(',', wp_get_post_cats(1, $post_ID));
190
191          $content  = '<title>'.stripslashes($post_data['post_title']).'</title>';
192          $content .= '<category>'.$categories.'</category>';
193          $content .= stripslashes($post_data['post_content']);
194
195          $struct = array(
196            'userid'    => $post_data['post_author'],
197            'dateCreated' => new IXR_Date(mysql2date('Ymd\TH:i:s', $post_data['post_date'])),
198            'content'     => $content,
199            'postid'  => $post_data['ID']
200          );
201
202          return $struct;
203        }
204
205
206        /* blogger.getRecentPosts ...gets recent posts */
207        function blogger_getRecentPosts($args) {
208
209          global $wpdb;
210
211          $blog_ID    = $args[1]; /* though we don't use it yet */
212          $user_login = $args[2];
213          $user_pass  = $args[3];
214          $num_posts  = $args[4];
215
216          if (!$this->login_pass_ok($user_login, $user_pass)) {
217            return $this->error;
218          }
219
220          $posts_list = wp_get_recent_posts($num_posts);
221
222          if (!$posts_list) {
223            $this->error = new IXR_Error(500, 'Either there are no posts, or something went wrong.');
224            return $this->error;
225          }
226
227          foreach ($posts_list as $entry) {
228         
229            $post_date = mysql2date('Ymd\TH:i:s', $entry['post_date']);
230            $categories = implode(',', wp_get_post_cats(1, $entry['ID']));
231
232            $content  = '<title>'.stripslashes($entry['post_itle']).'</title>';
233            $content .= '<category>'.$categories.'</category>';
234            $content .= stripslashes($entry['post_content']);
235
236            $struct[] = array(
237              'userid' => $entry['post_author'],
238              'dateCreated' => new IXR_Date($post_date),
239              'content' => $content,
240              'postid' => $entry['ID'],
241            );
242
243          }
244
245          $recent_posts = array();
246          for ($j=0; $j<count($struct); $j++) {
247            array_push($recent_posts, $struct[$j]);
248          }
249
250          return $recent_posts;
251        }
252
253
254        /* blogger.getTemplate returns your blog_filename */
255        function blogger_getTemplate($args) {
256
257          $blog_ID    = $args[1];
258          $user_login = $args[2];
259          $user_pass  = $args[3];
260          $template   = $args[4]; /* could be 'main' or 'archiveIndex', but we don't use it */
261
262          if (!$this->login_pass_ok($user_login, $user_pass)) {
263            return $this->error;
264          }
265
266          $user_data = get_userdatabylogin($user_login);
267
268          if ($user_data->user_level < 3) {
269            return new IXR_Error(401, 'Sorry, users whose level is less than 3, can not edit the template.');
270          }
271
272          /* warning: here we make the assumption that the weblog's URI is on the same server */
273          $filename = get_settings('home').'/'.get_settings('blogfilename');
274          $filename = preg_replace('#http://.+?/#', $_SERVER['DOCUMENT_ROOT'].'/', $filename);
275
276          $f = fopen($filename, 'r');
277          $content = fread($f, filesize($filename));
278          fclose($f);
279
280          /* so it is actually editable with a windows/mac client */
281          // FIXME: (or delete me) do we really want to cater to bad clients at the expense of good ones by BEEPing up their line breaks? commented.     $content = str_replace("\n", "\r\n", $content);
282
283          return $content;
284        }
285
286
287        /* blogger.setTemplate updates the content of blog_filename */
288        function blogger_setTemplate($args) {
289
290          $blog_ID    = $args[1];
291          $user_login = $args[2];
292          $user_pass  = $args[3];
293          $content    = $args[4];
294          $template   = $args[5]; /* could be 'main' or 'archiveIndex', but we don't use it */
295
296          if (!$this->login_pass_ok($user_login, $user_pass)) {
297            return $this->error;
298          }
299
300          $user_data = get_userdatabylogin($user_login);
301
302          if ($user_data->user_level < 3) {
303            return new IXR_Error(401, 'Sorry, users whose level is less than 3, can not edit the template.');
304          }
305
306          /* warning: here we make the assumption that the weblog's URI is on the same server */
307          $filename = get_settings('home').'/'.get_settings('blogfilename');
308          $filename = preg_replace('#http://.+?/#', $_SERVER['DOCUMENT_ROOT'].'/', $filename);
309
310          if ($f = fopen($filename, 'w+')) {
311            fwrite($f, $content);
312            fclose($f);
313          } else {
314            return new IXR_Error(500, 'Either the file is not writable, or something wrong happened. The file has not been updated.');
315          }
316
317          return true;
318        }
319
320
321        /* blogger.newPost ...creates a new post */
322        function blogger_newPost($args) {
323
324          global $wpdb;
325
326          $blog_ID    = $args[1]; /* though we don't use it yet */
327          $user_login = $args[2];
328          $user_pass  = $args[3];
329          $content    = $args[4];
330          $publish    = $args[5];
331
332          if (!$this->login_pass_ok($user_login, $user_pass)) {
333            return $this->error;
334          }
335
336          $user_data = get_userdatabylogin($user_login);
337          if (!user_can_create_post($user_data->ID, $blog_ID)) {
338            return new IXR_Error(401, 'Sorry, you can not post on this weblog or category.');
339          }
340
341          $post_status = ($publish) ? 'publish' : 'draft';
342
343          $post_author = $user_data->ID;
344
345          $post_title = xmlrpc_getposttitle($content);
346          $post_category = xmlrpc_getpostcategory($content);
347
348          $content = xmlrpc_removepostdata($content);
349          $post_content = format_to_post($content);
350
351          $post_date = current_time('mysql');
352          $post_date_gmt = current_time('mysql', 1);
353
354          $post_data = compact('blog_ID', 'post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_title', 'post_category', 'post_status');
355
356          $post_ID = wp_insert_post($post_data);
357
358          if (!$post_ID) {
359            return new IXR_Error(500, 'Sorry, your entry could not be posted. Something wrong happened.');
360          }
361
362          logIO('O', "Posted ! ID: $post_ID");
363
364          return $post_ID;
365        }
366
367
368        /* blogger.editPost ...edits a post */
369        function blogger_editPost($args) {
370
371          global $wpdb;
372
373          $post_ID     = $args[1];
374          $user_login  = $args[2];
375          $user_pass   = $args[3];
376          $new_content = $args[4];
377          $publish     = $args[5];
378
379          if (!$this->login_pass_ok($user_login, $user_pass)) {
380            return $this->error;
381          }
382
383          $actual_post = wp_get_single_post($post_ID,ARRAY_A);
384
385          if (!$actual_post) {
386                return new IXR_Error(404, 'Sorry, no such post.');
387          }
388
389          $post_author_data = get_userdata($actual_post['post_author']);
390          $user_data = get_userdatabylogin($user_login);
391
392          if (!user_can_edit_post($user_data->ID, $post_ID)) {
393            return new IXR_Error(401, 'Sorry, you do not have the right to edit this post.');
394          }
395
396          extract($actual_post);
397          $content = $newcontent;
398
399          $post_title = xmlrpc_getposttitle($content);
400          $post_category = xmlrpc_getpostcategory($content);
401
402          $content = xmlrpc_removepostdata($content);
403          $post_content = format_to_post($content);
404
405          $postdata = compact('ID', 'post_content', 'post_title', 'post_category', 'post_status', 'post_excerpt');
406
407          $result = wp_update_post($postdata);
408
409          if (!$result) {
410                return new IXR_Error(500, 'For some strange yet very annoying reason, this post could not be edited.');
411          }
412
413          return true;
414        }
415
416
417        /* blogger.deletePost ...deletes a post */
418        function blogger_deletePost($args) {
419
420          global $wpdb;
421
422          $post_ID     = $args[1];
423          $user_login  = $args[2];
424          $user_pass   = $args[3];
425          $publish     = $args[4];
426
427          if (!$this->login_pass_ok($user_login, $user_pass)) {
428            return $this->error;
429          }
430
431          $actual_post = wp_get_single_post($post_ID,ARRAY_A);
432
433          if (!$actual_post) {
434                return new IXR_Error(404, 'Sorry, no such post.');
435          }
436
437          $user_data = get_userdatabylogin($user_login);
438
439          if (!user_can_delete_post($user_data->ID, $post_ID)) {
440            return new IXR_Error(401, 'Sorry, you do not have the right to delete this post.');
441          }
442
443          $result = wp_delete_post($post_ID);
444
445          if (!$result) {
446                return new IXR_Error(500, 'For some strange yet very annoying reason, this post could not be deleted.');
447          }
448
449          return true;
450        }
451
452
453
454        /* MetaWeblog API functions
455         * specs on wherever Dave Winer wants them to be
456         */
457
458        /* metaweblog.newPost creates a post */
459        function mw_newPost($args) {
460
461          global $wpdb;
462
463          $blog_ID     = $args[0]; // we will support this in the near future
464          $user_login  = $args[1];
465          $user_pass   = $args[2];
466          $content_struct = $args[3];
467          $publish     = $args[4];
468
469          if (!$this->login_pass_ok($user_login, $user_pass)) {
470            return $this->error;
471          }
472
473          $user_data = get_userdatabylogin($user_login);
474          if (!user_can_create_post($user_data->ID, $blog_ID)) {
475            return new IXR_Error(401, 'Sorry, you can not post on this weblog or category.');
476          }
477
478          $post_author = $userdata->ID;
479
480          $post_title = $content_struct['title'];
481          $post_content = format_to_post($content_struct['description']);
482          $post_status = $publish ? 'publish' : 'draft';
483
484          $post_excerpt = $content_struct['mt_excerpt'];
485          $post_more = $content_struct['mt_text_more'];
486
487          $comment_status = $content_struct['mt_allow_comments'] ? 'open' : 'closed';
488          $ping_status = $content_struct['mt_allow_pings'] ? 'open' : 'closed';
489
490          if ($post_more) {
491            $post_content = $post_content . "\n<!--more-->\n" . $post_more;
492          }
493               
494          // Do some timestamp voodoo
495          $dateCreated = $content_struct['dateCreated'];
496          if (!empty($dateCreated)) {
497            $post_date     = get_date_from_gmt(iso8601_to_datetime($dateCreated));
498            $post_date_gmt = iso8601_to_datetime($dateCreated, GMT);
499          } else {
500            $post_date     = current_time('mysql');
501            $post_date_gmt = current_time('mysql', 1);
502          }
503
504          $catnames = $content_struct['categories'];
505          logio('O', 'Post cats: ' . printr($catnames,true));
506          $post_category = array();
507
508          if ($catnames) {
509            foreach ($catnames as $cat) {
510              $post_category[] = get_cat_ID($cat);
511            }
512          } else {
513            $post_category[] = 1;
514          }
515               
516          // We've got all the data -- post it:
517          $postdata = compact('post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_title', 'post_category', 'post_status', 'post_excerpt', 'comment_status', 'ping_status');
518
519          $post_ID = wp_insert_post($postdata);
520
521          if (!$post_ID) {
522            return new IXR_Error(500, 'Sorry, your entry could not be posted. Something wrong happened.');
523          }
524
525          logIO('O', "Posted ! ID: $post_ID");
526
527          // FIXME: do we pingback always? pingback($content, $post_ID);
528          trackback_url_list($content_struct['mt_tb_ping_urls'],$post_ID);
529
530          return $post_ID;
531        }
532
533
534        /* metaweblog.editPost ...edits a post */
535        function mw_editPost($args) {
536
537          global $wpdb;
538
539          $post_ID     = $args[0];
540          $user_login  = $args[1];
541          $user_pass   = $args[2];
542          $content_struct = $args[3];
543          $publish     = $args[4];
544
545          if (!$this->login_pass_ok($user_login, $user_pass)) {
546            return $this->error;
547          }
548
549          $user_data = get_userdatabylogin($user_login);
550          if (!user_can_edit_post($user_data->ID, $post_ID)) {
551            return new IXR_Error(401, 'Sorry, you can not edit this post.');
552          }
553
554          extract($postdata);
555
556          $post_title = $content_struct['title'];
557          $post_content = format_to_post($content_struct['description']);
558          $catnames = $content_struct['categories'];
559               
560          if ($catnames) {
561            foreach ($catnames as $cat) {
562              $post_category[] = get_cat_ID($cat);
563            }
564          }
565
566          $post_excerpt = $content_struct['mt_excerpt'];
567          $post_more = $content_struct['mt_text_more'];
568          $post_status = $publish ? 'publish' : 'draft';
569
570          if ($post_more) {
571            $post_content = $post_content . "\n<!--more-->\n" . $post_more;
572          }
573
574          $comment_status = (1 == $content_struct['mt_allow_comments']) ? 'open' : 'closed';
575          $ping_status = $content_struct['mt_allow_pings'] ? 'open' : 'closed';
576
577          // Do some timestamp voodoo
578          $dateCreated = $content_struct['dateCreated'];
579          if (!empty($dateCreated)) {
580            $post_date     = get_date_from_gmt(iso8601_to_datetime($dateCreated));
581            $post_date_gmt = iso8601_to_datetime($dateCreated, GMT);
582          } else {
583            $post_date     = $postdata['post_date'];
584            $post_date_gmt = $postdata['post_date_gmt'];
585          }
586
587          // We've got all the data -- post it:
588          $newpost = compact('ID', 'post_content', 'post_title', 'post_category', 'post_status', 'post_excerpt', 'comment_status', 'ping_status', 'post_date', 'post_date_gmt');
589
590          $post_ID = wp_update_post($newpost);
591          if (!$post_ID) {
592            return new IXR_Error(500, 'Sorry, your entry could not be edited. Something wrong happened.');
593          }
594
595          logIO('O',"(MW) Edited ! ID: $post_ID");
596
597          // FIXME: do we pingback always? pingback($content, $post_ID);
598          trackback_url_list($content_struct['mt_tb_ping_urls'], $post_ID);
599
600          return $post_ID;
601        }
602
603
604        /* metaweblog.getPost ...returns a post */
605        function mw_getPost($args) {
606
607          global $wpdb;
608
609          $post_ID     = $args[0];
610          $user_login  = $args[1];
611          $user_pass   = $args[2];
612
613          if (!$this->login_pass_ok($user_login, $user_pass)) {
614            return $this->error;
615          }
616
617          $postdata = wp_get_single_post($post_ID, ARRAY_A);
618
619          if ($postdata['post_date'] != '') {
620
621            $post_date = mysql2date('Ymd\TH:i:s', $postdata['post_date']);
622
623            $categories = array();
624            $catids = wp_get_post_cats('', $post_ID);
625            foreach($catids as $catid) {
626              $categories[] = get_cat_name($catid);
627            }
628
629            $post = get_extended($postdata['post_content']);
630            $link = post_permalink($entry['ID']);
631
632            $allow_comments = ('open' == $postdata['comment_status']) ? 1 : 0;
633            $allow_pings = ('open' == $postdata['ping_status']) ? 1 : 0;
634
635            $resp = array(
636                                'link' => $link,
637                                'title' => $postdata['post_title'],
638                                'description' => $post['main'],
639                                'dateCreated' => new IXR_Date($post_date),
640                                'userid' => $postdata['post_author'],
641                                'postid' => $postdata['ID'],
642                                'content' => $postdata['post_content'],
643                                'permaLink' => $link,
644                                'categories' => $categories,
645                                'mt_excerpt' => $postdata['post_excerpt'],
646                                'mt_allow_comments' => $allow_comments,
647                                'mt_allow_pings' => $allow_pings,
648                                'mt_text_more' => $post['extended']
649            );
650
651            return $resp;
652          } else {
653                return new IXR_Error(404, 'Sorry, no such post.');
654          }
655        }
656
657
658        /* metaweblog.getRecentPosts ...returns recent posts */
659        function mw_getRecentPosts($args) {
660
661          $blog_ID     = $args[0];
662          $user_login  = $args[1];
663          $user_pass   = $args[2];
664          $num_posts  = $args[4];
665
666          if (!$this->login_pass_ok($user_login, $user_pass)) {
667            return $this->error;
668          }
669
670          $posts_list = wp_get_recent_posts($num_posts);
671
672          if (!$posts_list) {
673            $this->error = new IXR_Error(500, 'Either there are no posts, or something went wrong.');
674            return $this->error;
675          }
676
677          foreach ($posts_list as $entry) {
678         
679            $post_date = mysql2date('Ymd\TH:i:s', $entry['post_date']);
680            $categories = array();
681            $catids = wp_get_post_cats('', $entry['ID']);
682            foreach($catids as $catid) {
683              $categories[] = get_cat_name($catid);
684            }
685
686            $post = get_extended($entry['post_content']);
687            $link = post_permalink($entry['ID']);
688
689            $allow_comments = ('open' == $entry['comment_status']) ? 1 : 0;
690            $allow_pings = ('open' == $entry['ping_status']) ? 1 : 0;
691
692            $struct[] = array(
693              'link' => $link,
694              'title' => $entry['post_title'],
695              'description' => $post['main'],
696              'dateCreated' => new IXR_Date($post_date),
697              'userid' => $entry['post_author'],
698              'postid' => $entry['ID'],
699              'content' => $entry['post_content'],
700              'permalink' => $link,
701              'categories' => $categories,
702              'mt_excerpt' => $entry['post_excerpt'],
703              'mt_allow_comments' => $allow_comments,
704              'mt_allow_pings' => $allow_pings,
705              'mt_text_more' => $post['extended']
706            );
707
708          }
709
710          $recent_posts = array();
711          for ($j=0; $j<count($struct); $j++) {
712            array_push($recent_posts, $struct[$j]);
713          }
714         
715          return $recent_posts;
716        }
717
718
719        /* metaweblog.getCategories ...returns the list of categories on a given weblog */
720        function mw_getCategories($args) {
721
722          global $wpdb;
723
724          $blog_ID     = $args[0];
725          $user_login  = $args[1];
726          $user_pass   = $args[2];
727
728          if (!$this->login_pass_ok($user_login, $user_pass)) {
729            return $this->error;
730          }
731
732          $categories_struct = array();
733
734          // FIXME: can we avoid using direct SQL there?
735          if ($cats = $wpdb->get_results("SELECT cat_ID,cat_name FROM $wpdb->categories", ARRAY_A)) {
736            foreach ($cats as $cat) {
737              $struct['categoryId'] = $cat['cat_ID'];
738              $struct['description'] = $cat['cat_name'];
739              $struct['categoryName'] = $cat['cat_name'];
740              $struct['htmlUrl'] = htmlspecialchars(get_category_link(false, $cat['cat_ID'], $cat['cat_name']));
741              $struct['rssUrl'] = htmlspecialchars(get_category_rss_link(false, $cat['cat_ID'], $cat['cat_name']));
742
743              $categories_struct[] = $struct;
744            }
745          }
746
747          return $categories_struct;
748        }
749
750
751        /* metaweblog.newMediaObject uploads a file, following your settings */
752        function mw_newMediaObject($args) {
753          // adapted from a patch by Johann Richard
754          // http://mycvs.org/archives/2004/06/30/file-upload-to-wordpress-in-ecto/
755
756          $blog_ID     = $args[0];
757          $user_login  = $args[1];
758          $user_pass   = $args[2];
759          $data        = $args[3];
760
761          $name = $data['name'];
762          $type = $data['type'];
763          $bits = $data['bits'];
764
765          $file_realpath = get_settings('fileupload_realpath'); 
766          $file_url = get_settings('fileupload_url');
767
768          logIO('O', '(MW) Received '.strlen($bits).' bytes');
769
770          if (!$this->login_pass_ok($user_login, $user_pass)) {
771            return $this->error;
772          }
773
774          $user_data = get_userdatabylogin($user_login);
775
776          if(!get_settings('use_fileupload')) {
777            // Uploads not allowed
778            logIO('O', '(MW) Uploads not allowed');
779            $this->error = new IXR_Error(405, 'No uploads allowed for this site.');
780            return $this->error;
781          } 
782
783          if(get_settings('fileupload_minlevel') > $user_data->user_level) {
784            // User has not enough privileges
785            logIO('O', '(MW) Not enough privilege: user level too low');
786            $this->error = new IXR_Error(401, 'You are not allowed to upload files to this site.');
787            return $this->error;
788          }
789
790          if(trim($file_realpath) == '' || trim($file_url) == '' ) {
791            // WordPress is not correctly configured
792            logIO('O', '(MW) Bad configuration. Real/URL path not defined');
793            $this->error = new IXR_Error(500, 'Please configure WordPress with valid paths for file upload.');
794            return $this->error;
795          }
796
797          $prefix = '/';
798
799          if(!empty($name)) {
800            // Create the path
801            $localpath = $file_realpath.$prefix.$name;
802            $url = $file_url.$prefix.$name;
803
804            if (mkdir_p(dirname($localpath))) {
805
806              /* encode & write data (binary) */
807              $ifp = fopen($localpath, 'wb');
808              $success = fwrite($ifp, $bits);
809              fclose($ifp);
810              @chmod($localpath, 0666);
811
812              if($success) {
813                $resp = array($url);
814                return $resp;
815              } else {
816                logIO('O', '(MW) Could not write file '.$name.' to '.$localpath);
817                return new IXR_Error(500, 'Could not write file '.$name);
818              }
819
820            } else {
821              return new IXR_Error(500, 'Could not create directories for '.$name);
822            }
823          }
824        }
825
826}
827
828
829$wp_xmlrpc_server = new wp_xmlrpc_server();
830
831?>
Note: See TracBrowser for help on using the repository browser.