Make WordPress Core

Changeset 1670


Ignore:
Timestamp:
09/16/2004 01:26:06 PM (22 years ago)
Author:
michelvaldrighi
Message:

added pingback (now with accurate fault codes and more easily readable code), added aliases for metaweblog stuff that mimics blogger api

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/xmlrpc.php

    r1666 r1670  
    7676        function wp_xmlrpc_server() {
    7777                $this->IXR_Server(array(
     78                  // Blogger API
    7879                  'blogger.getUsersBlogs' => 'this:blogger_getUsersBlogs',
    7980                  'blogger.getUserInfo' => 'this:blogger_getUserInfo',
    … …  
    8687                  'blogger.deletePost' => 'this:blogger_deletePost',
    8788
     89                  // MetaWeblog API
    8890                  'metaWeblog.newPost' => 'this:mw_newPost',
    8991                  'metaWeblog.editPost' => 'this:mw_editPost',
    … …  
    9294                  'metaWeblog.getCategories' => 'this:mw_getCategories',
    9395                  'metaWeblog.newMediaObject' => 'this:mw_newMediaObject',
     96
     97                  // MetaWeblog API aliases for Blogger API
     98                  // see http://www.xmlrpc.com/stories/storyReader$2460
     99                  'metaWeblog.deletePost' => 'this:blogger_deletePost',
     100                  'metaWeblog.getTemplate' => 'this:blogger_getTemplate',
     101                  'metaWeblog.setTemplate' => 'this:blogger_setTemplate',
     102                  'metaWeblog.getUsersBlogs' => 'this:blogger_getUsersBlogs',
     103
     104                  // PingBack
     105                  'pingback.ping' => 'this:pingback_ping',
    94106
    95107                  'demo.sayHello' => 'this:sayHello',
    … …  
    914926        }
    915927
     928
     929
     930        /* PingBack functions
     931         * specs on www.hixie.ch/specs/pingback/pingback
     932         */
     933
     934        /* pingback.ping gets a pingback and registers it */
     935        function pingback_ping($args) {
     936                // original code by Mort (http://mort.mine.nu:8080 -- site seems dead)
     937                // refactored to return error codes and avoid deep ifififif headaches
     938                global $wpdb, $wp_version;
     939
     940                $pagelinkedfrom = $args[0];
     941                $pagelinkedto   = $args[1];
     942
     943                $title = '';
     944
     945                $pagelinkedfrom = str_replace('&', '&', $pagelinkedfrom);
     946                $pagelinkedto   = preg_replace('#&([^amp\;])#is', '&$1', $pagelinkedto);
     947
     948                $error_code = -1;
     949
     950                // Check if the page linked to is in our site
     951                $pos1 = strpos($pagelinkedto, str_replace('http://', '', str_replace('www.', '', get_settings('home'))));
     952                if(!$pos1) {
     953                        return '0';
     954                }
     955
     956
     957                // let's find which post is linked to
     958                $urltest = parse_url($pagelinkedto);
     959                if ($post_ID = url_to_postid($pagelinkedto)) {
     960                        $way = 'url_to_postid()';
     961                } elseif (preg_match('#p/[0-9]{1,}#', $urltest['path'], $match)) {
     962                        // the path defines the post_ID (archives/p/XXXX)
     963                        $blah = explode('/', $match[0]);
     964                        $post_ID = $blah[1];
     965                        $way = 'from the path';
     966                } elseif (preg_match('#p=[0-9]{1,}#', $urltest['query'], $match)) {
     967                        // the querystring defines the post_ID (?p=XXXX)
     968                        $blah = explode('=', $match[0]);
     969                        $post_ID = $blah[1];
     970                        $way = 'from the querystring';
     971                } elseif (isset($urltest['fragment'])) {
     972                        // an #anchor is there, it's either...
     973                        if (intval($urltest['fragment'])) {
     974                                // ...an integer #XXXX (simpliest case)
     975                                $post_ID = $urltest['fragment'];
     976                                $way = 'from the fragment (numeric)';
     977                        } elseif (preg_match('/post-[0-9]+/',$urltest['fragment'])) {
     978                                // ...a post id in the form 'post-###'
     979                                $post_ID = preg_replace('/[^0-9]+/', '', $urltest['fragment']);
     980                                $way = 'from the fragment (post-###)';
     981                        } elseif (is_string($urltest['fragment'])) {
     982                                // ...or a string #title, a little more complicated
     983                                $title = preg_replace('/[^a-zA-Z0-9]/', '.', $urltest['fragment']);
     984                                $sql = "SELECT ID FROM $wpdb->posts WHERE post_title RLIKE '$title'";
     985                                if (! ($post_ID = $wpdb->get_var($sql)) ) {
     986                                        // returning unknown error '0' is better than die()ing
     987                                        return '0';
     988                                }
     989                                $way = 'from the fragment (title)';
     990                        }
     991                } else {
     992                        // TODO: Attempt to extract a post ID from the given URL
     993                        return '0x0021';
     994                }
     995
     996
     997                logIO("O","(PB) URI='$pagelinkedto' ID='$post_ID' Found='$way'");
     998
     999                $sql = 'SELECT post_author FROM '.$wpdb->posts.' WHERE ID = '.$post_ID;
     1000                $result = $wpdb->get_results($sql);
     1001
     1002                if (!$wpdb->num_rows) {
     1003                        // Post_ID not found
     1004                        return '0x0021';
     1005                }
     1006
     1007
     1008                // Let's check that the remote site didn't already pingback this entry
     1009                $sql = 'SELECT * FROM '.$wpdb->comments.'
     1010                        WHERE comment_post_ID = '.$post_ID.'
     1011                                AND comment_author_url = \''.$pagelinkedfrom.'\'
     1012                                AND comment_type = \'pingback\'';
     1013                $result = $wpdb->get_results($sql);
     1014//return($sql);
     1015
     1016                if ($wpdb->num_rows) {
     1017                        // We already have a Pingback from this URL
     1018                        return '0x0030';
     1019                }
     1020
     1021
     1022                // very stupid, but gives time to the 'from' server to publish !
     1023                sleep(1);
     1024
     1025                // Let's check the remote site
     1026                $fp = @fopen($pagelinkedfrom, 'r');
     1027                if (!$fp) {
     1028                        // The source URI does not exist
     1029                        return '0x0010';
     1030                }
     1031
     1032                $puntero = 4096;
     1033                while($remote_read = fread($fp, $puntero)) {
     1034                        $linea .= $remote_read;
     1035                }
     1036
     1037                // Work around bug in strip_tags():
     1038                $linea = str_replace('<!DOCTYPE','<DOCTYPE',$linea);
     1039                $linea = strip_tags($linea, '<title><a>');
     1040                $linea = strip_all_but_one_link($linea, $pagelinkedto);
     1041                // I don't think we need this? -- emc3
     1042                //$linea = preg_replace('#&([^amp\;])#is', '&amp;$1', $linea);
     1043                if (empty($matchtitle)) {
     1044                        preg_match('|<title>([^<]*?)</title>|is', $linea, $matchtitle);
     1045                }
     1046                $pos2 = strpos($linea, $pagelinkedto);
     1047                $pos3 = strpos($linea, str_replace('http://www.', 'http://', $pagelinkedto));
     1048                if (is_integer($pos2) || is_integer($pos3)) {
     1049                        // The page really links to us :)
     1050                        $pos4 = (is_integer($pos2)) ? $pos2 : $pos3;
     1051                        $start = $pos4-100;
     1052                        $context = substr($linea, $start, 250);
     1053                        $context = str_replace("\n", ' ', $context);
     1054                        $context = str_replace('&amp;', '&', $context);
     1055                }
     1056                                       
     1057                fclose($fp);
     1058
     1059                if (empty($context)) {
     1060                        // URL pattern not found
     1061                        return '0x0011';
     1062                }
     1063
     1064
     1065                // Check if pings are on, inelegant exit
     1066                $pingstatus = $wpdb->get_var("SELECT ping_status FROM $wpdb->posts WHERE ID = $post_ID");
     1067                if ('closed' == $pingstatus) {
     1068                        return '0x0021';
     1069                }
     1070
     1071
     1072                $pagelinkedfrom = preg_replace('#&([^amp\;])#is', '&amp;$1', $pagelinkedfrom);
     1073                $title = (!strlen($matchtitle[1])) ? $pagelinkedfrom : $matchtitle[1];
     1074                $original_context = strip_tags($context);
     1075                $context = '[...] ';
     1076                $context = htmlspecialchars($original_context);
     1077                $context .= ' [...]';
     1078                $original_pagelinkedfrom = $pagelinkedfrom;
     1079                $pagelinkedfrom = addslashes($pagelinkedfrom);
     1080                $original_title = $title;
     1081                $title = addslashes(strip_tags(trim($title)));
     1082                $user_ip = $_SERVER['REMOTE_ADDR'];
     1083                $user_agent = addslashes($_SERVER['HTTP_USER_AGENT']);
     1084                $now = current_time('mysql');
     1085                $now_gmt = current_time('mysql', 1);
     1086
     1087                // Check if the entry allows pings
     1088                if( !check_comment($title, '', $pagelinkedfrom, $context, $user_ip, $user_agent) ) {
     1089                        return '0x0031';
     1090                }
     1091
     1092
     1093                $consulta = $wpdb->query("INSERT INTO $wpdb->comments
     1094                        (comment_post_ID, comment_author, comment_author_url, comment_author_IP, comment_date, comment_date_gmt, comment_content, comment_approved, comment_agent, comment_type)
     1095                        VALUES
     1096                        ($post_ID, '$title', '$pagelinkedfrom', '$user_ip', '$now', '$now_gmt', '$context', '1', '$user_agent', 'pingback')
     1097                ");
     1098
     1099                $comment_ID = $wpdb->get_var('SELECT last_insert_id()');
     1100
     1101                if (get_settings('comments_notify')) {
     1102                        wp_notify_postauthor($comment_ID, 'pingback');
     1103                }
     1104
     1105                do_action('pingback_post', $comment_ID);
     1106               
     1107                return "Pingback from $pagelinkedfrom to $pagelinkedto registered. Keep the web talking! :-)";
     1108        }
    9161109}
    9171110
Note: See TracChangeset for help on using the changeset viewer.