root/de.easy-coding.wbb.trackback.old/files/lib/util/XMLRPCServer.class.php @ 817

Revision 817, 5.8 kB (checked in by d0nut, 4 years ago)

update for old trackback tool

Line 
1<?php
2require_once(WCF_DIR.'lib/util/StringUtil.class.php');
3
4require_once(WBB_DIR.'lib/data/post/Post.class.php');
5require_once(WBB_DIR.'lib/util/IXR.class.php');
6require_once(WBB_DIR.'lib/util/TrackbackUtil.class.php');
7require_once(WBB_DIR.'lib/data/thread/Thread.class.php');
8require_once(WBB_DIR.'lib/page/PublicSEORewriterWBB.class.php');
9
10// Some browser-embedded clients send cookies. We don't want them.
11$_COOKIE = array();
12
13// A bug in PHP < 5.2.2 makes $HTTP_RAW_POST_DATA not set by default,
14// but we can do it ourself.
15if(!isset($HTTP_RAW_POST_DATA)) {
16        $HTTP_RAW_POST_DATA = file_get_contents('php://input');
17}
18
19# fix for mozBlog and other cases where '<?xml' isn't on the very first line
20if(isset($HTTP_RAW_POST_DATA)) {
21        $HTTP_RAW_POST_DATA = trim($HTTP_RAW_POST_DATA);
22}
23
24
25/**
26 * xmlrpc util
27 *
28 * @author      Torben Brodt
29 * @package     de.easy-coding.wbb.trackback
30 * @license     GNU General Public License <http://opensource.org/licenses/gpl-3.0.html>
31 */
32class XMLRPCServer extends IXR_Server {
33        protected $rewriter;
34
35        /**
36         * constructor
37         */
38        public function __construct() {
39                $this->methods = array(
40                        'pingback.ping' => 'this:pingback_ping'
41                );
42               
43                $this->rewriter = new PublicSEORewriterWBB();
44
45                parent::IXR_Server($this->methods);
46        }
47       
48        /**
49         * escapes strings
50         * @param arr
51         */
52        protected function escape(&$arr) {
53                foreach ($arr as $k => $v ) {
54                        $arr[$k] = escapeString($v);
55                }
56        }
57
58        /**
59         * gets a pingback and registers it
60         * @param args array(linkedfrom, linkedto)
61         */
62        public function pingback_ping($args) {
63                $this->escape($args);
64
65                $pagelinkedfrom = StringUtil::decodeHTML($args[0]);
66                $pagelinkedto   = StringUtil::decodeHTML($args[1]);
67
68                $title = '';
69
70                $error_code = -1;
71
72                // Check if the page linked to is in our site
73                $pos1 = strpos($pagelinkedto, str_replace(array('http://www.','http://','https://www.','https://'), '', PAGE_URL));
74                if(!$pos1) {
75                        return new IXR_Error(0, 'Is there no link to us?');
76                }
77
78                // let's find which post is linked to
79                $threadID = $this->rewriter->thread2threadID($pagelinkedto);
80                if($threadID === NULL) {
81                        return new IXR_Error(33, 'The specified target URL cannot be resolved as a thread url.');
82                }
83               
84                $thread = new Thread($threadID);
85
86                // Check if post exists
87                if($thread->firstPostID) {
88                        $post = new Post($thread->firstPostID);
89                } else {
90                        return new IXR_Error(33, 'The specified target URL cannot be used as a target. It doesn\'t exist.');
91                }
92
93                // Check if pings are on
94                if(!$post->hasTrackback) {
95                        return new IXR_Error(33, 'The specified target URL cannot be used as a target. It is not a pingback-enabled resource.');
96                }
97
98                // check for duplicates
99                $sql = "SELECT  COUNT(*) AS c
100                        FROM    wbb".WBB_N."_trackback
101                        WHERE   postID = ".$post->postID."
102                        AND     url = '".escapeString($pagelinkedfrom)."' ";
103                       
104                $row = WBBCore::getDB()->getFirstRow($sql);
105                if(intval($row['c']) > 0) {
106                        return new IXR_Error(48, 'The pingback has already been registered.');
107                }
108
109                // very stupid, but gives time to the 'from' server to publish !
110                sleep(1);
111
112                // Let's check the remote site
113                extract(parse_url($pagelinkedfrom), EXTR_SKIP);
114
115                $path  = ( !isset($path) ) ? '/'          : $path;
116                $path .= ( isset($query) ) ? '?' . $query : '';
117                $port  = ( isset($port)  ) ? $port        : 80;
118
119                // Try to connect to the server at $host
120                $fp = @fsockopen($host, $port, $errno, $errstr, 2);
121                if(!$fp) {
122                        return new IXR_Error(16, 'The source URL does not exist.');
123                }
124               
125                $request = "GET $path HTTP/1.1\r\nHost: $host\r\nUser-Agent: ".TrackbackUtil::$agent." \r\n\r\n";
126                fputs($fp, $request);
127                $linea = '';
128                while (!feof($fp)) {
129                        $linea .= fgets($fp, 4096);
130                }
131
132                // Work around bug in strip_tags():
133                $linea = str_replace('<!DOC', '<DOC', $linea);
134                $linea = preg_replace( '/[\s\r\n\t]+/', ' ', $linea ); // normalize spaces
135                $linea = preg_replace( "/ <(h1|h2|h3|h4|h5|h6|p|th|td|li|dt|dd|pre|caption|input|textarea|button|body)[^>]*>/", "\n\n", $linea );
136
137                preg_match('|<title>([^<]*?)</title>|is', $linea, $matchtitle);
138                $title = $matchtitle[1];
139                if(empty($title)) {
140                        return new IXR_Error(32, 'We cannot find a title on that page.');
141                }
142
143                $linea = strip_tags($linea, '<a>'); // just keep the tag we need
144                $p = explode("\n\n", $linea);
145
146                $preg_target = preg_quote($pagelinkedto);
147
148                foreach($p as $para) {
149                        if(strpos($para, $pagelinkedto) !== false) { // it exists, but is it a link?
150                                preg_match("|<a[^>]+?".$preg_target."[^>]*>([^>]+?)</a>|", $para, $context);
151
152                                // If the URL isn't in a link context, keep looking
153                                if(empty($context)) {
154                                        continue;
155                                }
156
157                                // We're going to use this fake tag to mark the context in a bit
158                                // the marker is needed in case the link text appears more than once in the paragraph
159                                $excerpt = preg_replace('|\</?wpcontext\>|', '', $para);
160
161                                // prevent really long link text
162                                if(strlen($context[1]) > 100) {
163                                        $context[1] = substr($context[1], 0, 100).'...';
164                                }
165
166                                $marker = '<wpcontext>'.$context[1].'</wpcontext>';    // set up our marker
167                                $excerpt= str_replace($context[0], $marker, $excerpt); // swap out the link for our marker
168                                $excerpt = strip_tags($excerpt, '<wpcontext>');        // strip all tags but our context marker
169                                $excerpt = trim($excerpt);
170                                $preg_marker = preg_quote($marker);
171                                $excerpt = preg_replace("|.*?\s(.{0,100}$preg_marker.{0,100})\s.*|s", '$1', $excerpt);
172                                $excerpt = strip_tags($excerpt); // YES, again, to remove the marker wrapper
173                                break;
174                        }
175                }
176
177                if(empty($context)) { 
178                        // Link to target not found
179                        return new IXR_Error(17, 'The source URL does not contain a link to the target URL, and so cannot be used as a source.');
180                }
181
182                // save incoming
183                TrackbackUtil::save($postID, $pagelinkedto, $title, $excerpt, $pagelinkedfrom, $title);
184
185                return sprintf('Pingback from %1$s to %2$s registered. Keep the web talking! :-)', $pagelinkedfrom, $pagelinkedto);
186        }
187}
188?>
Note: See TracBrowser for help on using the browser.