blob: d53f17fef04cdf4598e968db53cc6ca9325bc001 [file] [log] [blame]
Derek Allard2067d1a2008-11-13 22:59:24 +00001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2/**
3 * CodeIgniter
4 *
5 * An open source application development framework for PHP 4.3.2 or newer
6 *
7 * @package CodeIgniter
8 * @author ExpressionEngine Dev Team
9 * @copyright Copyright (c) 2008, EllisLab, Inc.
10 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
12 * @since Version 1.0
13 * @filesource
14 */
15
16// ------------------------------------------------------------------------
17
18/**
19 * Trackback Class
20 *
21 * Trackback Sending/Receiving Class
22 *
23 * @package CodeIgniter
24 * @subpackage Libraries
25 * @category Trackbacks
26 * @author ExpressionEngine Dev Team
27 * @link http://codeigniter.com/user_guide/libraries/trackback.html
28 */
29class CI_Trackback {
30
31 var $time_format = 'local';
32 var $charset = 'UTF-8';
33 var $data = array('url' => '', 'title' => '', 'excerpt' => '', 'blog_name' => '', 'charset' => '');
34 var $convert_ascii = TRUE;
35 var $response = '';
36 var $error_msg = array();
37
38 /**
39 * Constructor
40 *
41 * @access public
42 */
43 function CI_Trackback()
44 {
45 log_message('debug', "Trackback Class Initialized");
46 }
47
48 // --------------------------------------------------------------------
49
50 /**
51 * Send Trackback
52 *
53 * @access public
54 * @param array
55 * @return bool
56 */
57 function send($tb_data)
58 {
59 if ( ! is_array($tb_data))
60 {
61 $this->set_error('The send() method must be passed an array');
62 return FALSE;
63 }
64
65 // Pre-process the Trackback Data
66 foreach (array('url', 'title', 'excerpt', 'blog_name', 'ping_url') as $item)
67 {
68 if ( ! isset($tb_data[$item]))
69 {
70 $this->set_error('Required item missing: '.$item);
71 return FALSE;
72 }
73
74 switch ($item)
75 {
76 case 'ping_url' : $$item = $this->extract_urls($tb_data[$item]);
77 break;
78 case 'excerpt' : $$item = $this->limit_characters($this->convert_xml(strip_tags(stripslashes($tb_data[$item]))));
79 break;
80 case 'url' : $$item = str_replace('&#45;', '-', $this->convert_xml(strip_tags(stripslashes($tb_data[$item]))));
81 break;
82 default : $$item = $this->convert_xml(strip_tags(stripslashes($tb_data[$item])));
83 break;
84 }
85
86 // Convert High ASCII Characters
87 if ($this->convert_ascii == TRUE)
88 {
89 if ($item == 'excerpt')
90 {
91 $$item = $this->convert_ascii($$item);
92 }
93 elseif ($item == 'title')
94 {
95 $$item = $this->convert_ascii($$item);
96 }
97 elseif($item == 'blog_name')
98 {
99 $$item = $this->convert_ascii($$item);
100 }
101 }
102 }
103
104 // Build the Trackback data string
105 $charset = ( ! isset($tb_data['charset'])) ? $this->charset : $tb_data['charset'];
106
107 $data = "url=".rawurlencode($url)."&title=".rawurlencode($title)."&blog_name=".rawurlencode($blog_name)."&excerpt=".rawurlencode($excerpt)."&charset=".rawurlencode($charset);
108
109 // Send Trackback(s)
110 $return = TRUE;
111 if (count($ping_url) > 0)
112 {
113 foreach ($ping_url as $url)
114 {
115 if ($this->process($url, $data) == FALSE)
116 {
117 $return = FALSE;
118 }
119 }
120 }
121
122 return $return;
123 }
124
125 // --------------------------------------------------------------------
126
127 /**
128 * Receive Trackback Data
129 *
130 * This function simply validates the incoming TB data.
131 * It returns false on failure and true on success.
132 * If the data is valid it is set to the $this->data array
133 * so that it can be inserted into a database.
134 *
135 * @access public
136 * @return bool
137 */
138 function receive()
139 {
140 foreach (array('url', 'title', 'blog_name', 'excerpt') as $val)
141 {
142 if ( ! isset($_POST[$val]) OR $_POST[$val] == '')
143 {
144 $this->set_error('The following required POST variable is missing: '.$val);
145 return FALSE;
146 }
147
148 $this->data['charset'] = ( ! isset($_POST['charset'])) ? 'auto' : strtoupper(trim($_POST['charset']));
149
150 if ($val != 'url' && function_exists('mb_convert_encoding'))
151 {
152 $_POST[$val] = mb_convert_encoding($_POST[$val], $this->charset, $this->data['charset']);
153 }
154
155 $_POST[$val] = ($val != 'url') ? $this->convert_xml(strip_tags($_POST[$val])) : strip_tags($_POST[$val]);
156
157 if ($val == 'excerpt')
158 {
159 $_POST['excerpt'] = $this->limit_characters($_POST['excerpt']);
160 }
161
162 $this->data[$val] = $_POST[$val];
163 }
164
165 return TRUE;
166 }
167
168 // --------------------------------------------------------------------
169
170 /**
171 * Send Trackback Error Message
172 *
173 * Allows custom errors to be set. By default it
174 * sends the "incomplete information" error, as that's
175 * the most common one.
176 *
177 * @access public
178 * @param string
179 * @return void
180 */
181 function send_error($message = 'Incomplete Information')
182 {
183 echo "<?xml version=\"1.0\" encoding=\"utf-8\"?".">\n<response>\n<error>1</error>\n<message>".$message."</message>\n</response>";
184 exit;
185 }
186
187 // --------------------------------------------------------------------
188
189 /**
190 * Send Trackback Success Message
191 *
192 * This should be called when a trackback has been
193 * successfully received and inserted.
194 *
195 * @access public
196 * @return void
197 */
198 function send_success()
199 {
200 echo "<?xml version=\"1.0\" encoding=\"utf-8\"?".">\n<response>\n<error>0</error>\n</response>";
201 exit;
202 }
203
204 // --------------------------------------------------------------------
205
206 /**
207 * Fetch a particular item
208 *
209 * @access public
210 * @param string
211 * @return string
212 */
213 function data($item)
214 {
215 return ( ! isset($this->data[$item])) ? '' : $this->data[$item];
216 }
217
218 // --------------------------------------------------------------------
219
220 /**
221 * Process Trackback
222 *
223 * Opens a socket connection and passes the data to
224 * the server. Returns true on success, false on failure
225 *
226 * @access public
227 * @param string
228 * @param string
229 * @return bool
230 */
231 function process($url, $data)
232 {
233 $target = parse_url($url);
234
235 // Open the socket
236 if ( ! $fp = @fsockopen($target['host'], 80))
237 {
238 $this->set_error('Invalid Connection: '.$url);
239 return FALSE;
240 }
241
242 // Build the path
243 $ppath = ( ! isset($target['path'])) ? $url : $target['path'];
244
245 $path = (isset($target['query']) && $target['query'] != "") ? $ppath.'?'.$target['query'] : $ppath;
246
247 // Add the Trackback ID to the data string
248 if ($id = $this->get_id($url))
249 {
250 $data = "tb_id=".$id."&".$data;
251 }
252
253 // Transfer the data
254 fputs ($fp, "POST " . $path . " HTTP/1.0\r\n" );
255 fputs ($fp, "Host: " . $target['host'] . "\r\n" );
256 fputs ($fp, "Content-type: application/x-www-form-urlencoded\r\n" );
257 fputs ($fp, "Content-length: " . strlen($data) . "\r\n" );
258 fputs ($fp, "Connection: close\r\n\r\n" );
259 fputs ($fp, $data);
260
261 // Was it successful?
262 $this->response = "";
263
264 while( ! feof($fp))
265 {
266 $this->response .= fgets($fp, 128);
267 }
268 @fclose($fp);
269
Derek Jones1322ec52009-03-11 17:01:14 +0000270 if (stristr($this->response, '<error>0</error>') === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000271 {
272 $message = 'An unknown error was encountered';
273
274 if (preg_match("/<message>(.*?)<\/message>/is", $this->response, $match))
275 {
276 $message = trim($match['1']);
277 }
278
279 $this->set_error($message);
280 return FALSE;
281 }
282
283 return TRUE;
284 }
285
286 // --------------------------------------------------------------------
287
288 /**
289 * Extract Trackback URLs
290 *
291 * This function lets multiple trackbacks be sent.
292 * It takes a string of URLs (separated by comma or
293 * space) and puts each URL into an array
294 *
295 * @access public
296 * @param string
297 * @return string
298 */
299 function extract_urls($urls)
300 {
301 // Remove the pesky white space and replace with a comma.
302 $urls = preg_replace("/\s*(\S+)\s*/", "\\1,", $urls);
303
304 // If they use commas get rid of the doubles.
305 $urls = str_replace(",,", ",", $urls);
306
307 // Remove any comma that might be at the end
308 if (substr($urls, -1) == ",")
309 {
310 $urls = substr($urls, 0, -1);
311 }
312
313 // Break into an array via commas
314 $urls = preg_split('/[,]/', $urls);
315
316 // Removes duplicates
317 $urls = array_unique($urls);
318
319 array_walk($urls, array($this, 'validate_url'));
320
321 return $urls;
322 }
323
324 // --------------------------------------------------------------------
325
326 /**
327 * Validate URL
328 *
329 * Simply adds "http://" if missing
330 *
331 * @access public
332 * @param string
333 * @return string
334 */
335 function validate_url($url)
336 {
337 $url = trim($url);
338
339 if (substr($url, 0, 4) != "http")
340 {
341 $url = "http://".$url;
342 }
343 }
344
345 // --------------------------------------------------------------------
346
347 /**
348 * Find the Trackback URL's ID
349 *
350 * @access public
351 * @param string
352 * @return string
353 */
354 function get_id($url)
355 {
356 $tb_id = "";
357
358 if (strstr($url, '?'))
359 {
360 $tb_array = explode('/', $url);
361 $tb_end = $tb_array[count($tb_array)-1];
362
363 if ( ! is_numeric($tb_end))
364 {
365 $tb_end = $tb_array[count($tb_array)-2];
366 }
367
368 $tb_array = explode('=', $tb_end);
369 $tb_id = $tb_array[count($tb_array)-1];
370 }
371 else
372 {
Derek Jones1322ec52009-03-11 17:01:14 +0000373 $url = rtrim($url, '/');
Derek Allard2067d1a2008-11-13 22:59:24 +0000374
375 $tb_array = explode('/', $url);
376 $tb_id = $tb_array[count($tb_array)-1];
377
378 if ( ! is_numeric($tb_id))
379 {
380 $tb_id = $tb_array[count($tb_array)-2];
381 }
382 }
383
384 if ( ! preg_match ("/^([0-9]+)$/", $tb_id))
385 {
386 return false;
387 }
388 else
389 {
390 return $tb_id;
391 }
392 }
393
394 // --------------------------------------------------------------------
395
396 /**
397 * Convert Reserved XML characters to Entities
398 *
399 * @access public
400 * @param string
401 * @return string
402 */
403 function convert_xml($str)
404 {
405 $temp = '__TEMP_AMPERSANDS__';
406
407 $str = preg_replace("/&#(\d+);/", "$temp\\1;", $str);
408 $str = preg_replace("/&(\w+);/", "$temp\\1;", $str);
409
410 $str = str_replace(array("&","<",">","\"", "'", "-"),
411 array("&amp;", "&lt;", "&gt;", "&quot;", "&#39;", "&#45;"),
412 $str);
413
414 $str = preg_replace("/$temp(\d+);/","&#\\1;",$str);
415 $str = preg_replace("/$temp(\w+);/","&\\1;", $str);
416
417 return $str;
418 }
419
420 // --------------------------------------------------------------------
421
422 /**
423 * Character limiter
424 *
425 * Limits the string based on the character count. Will preserve complete words.
426 *
427 * @access public
428 * @param string
429 * @param integer
430 * @param string
431 * @return string
432 */
433 function limit_characters($str, $n = 500, $end_char = '&#8230;')
434 {
435 if (strlen($str) < $n)
436 {
437 return $str;
438 }
439
440 $str = preg_replace("/\s+/", ' ', str_replace(array("\r\n", "\r", "\n"), ' ', $str));
441
442 if (strlen($str) <= $n)
443 {
444 return $str;
445 }
446
447 $out = "";
448 foreach (explode(' ', trim($str)) as $val)
449 {
450 $out .= $val.' ';
451 if (strlen($out) >= $n)
452 {
453 return trim($out).$end_char;
454 }
455 }
456 }
457
458 // --------------------------------------------------------------------
459
460 /**
461 * High ASCII to Entities
462 *
463 * Converts Hight ascii text and MS Word special chars
464 * to character entities
465 *
466 * @access public
467 * @param string
468 * @return string
469 */
470 function convert_ascii($str)
471 {
472 $count = 1;
473 $out = '';
474 $temp = array();
475
476 for ($i = 0, $s = strlen($str); $i < $s; $i++)
477 {
478 $ordinal = ord($str[$i]);
479
480 if ($ordinal < 128)
481 {
482 $out .= $str[$i];
483 }
484 else
485 {
486 if (count($temp) == 0)
487 {
488 $count = ($ordinal < 224) ? 2 : 3;
489 }
490
491 $temp[] = $ordinal;
492
493 if (count($temp) == $count)
494 {
495 $number = ($count == 3) ? (($temp['0'] % 16) * 4096) + (($temp['1'] % 64) * 64) + ($temp['2'] % 64) : (($temp['0'] % 32) * 64) + ($temp['1'] % 64);
496
497 $out .= '&#'.$number.';';
498 $count = 1;
499 $temp = array();
500 }
501 }
502 }
503
504 return $out;
505 }
506
507 // --------------------------------------------------------------------
508
509 /**
510 * Set error message
511 *
512 * @access public
513 * @param string
514 * @return void
515 */
516 function set_error($msg)
517 {
518 log_message('error', $msg);
519 $this->error_msg[] = $msg;
520 }
521
522 // --------------------------------------------------------------------
523
524 /**
525 * Show error messages
526 *
527 * @access public
528 * @param string
529 * @param string
530 * @return string
531 */
532 function display_errors($open = '<p>', $close = '</p>')
533 {
534 $str = '';
535 foreach ($this->error_msg as $val)
536 {
537 $str .= $open.$val.$close;
538 }
539
540 return $str;
541 }
542
543}
544// END Trackback Class
545
546/* End of file Trackback.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +0000547/* Location: ./system/libraries/Trackback.php */