1 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
\r
5 * An open source application development framework for PHP 4.3.2 or newer
\r
7 * @package CodeIgniter
\r
8 * @author ExpressionEngine Dev Team
\r
9 * @copyright Copyright (c) 2008, EllisLab, Inc.
\r
10 * @license http://codeigniter.com/user_guide/license.html
\r
11 * @link http://codeigniter.com
\r
12 * @since Version 1.0
\r
16 // ------------------------------------------------------------------------
\r
21 * Trackback Sending/Receiving Class
\r
23 * @package CodeIgniter
\r
24 * @subpackage Libraries
\r
25 * @category Trackbacks
\r
26 * @author ExpressionEngine Dev Team
\r
27 * @link http://codeigniter.com/user_guide/libraries/trackback.html
\r
29 class CI_Trackback {
\r
31 var $time_format = 'local';
\r
32 var $charset = 'UTF-8';
\r
33 var $data = array('url' => '', 'title' => '', 'excerpt' => '', 'blog_name' => '', 'charset' => '');
\r
34 var $convert_ascii = TRUE;
\r
36 var $error_msg = array();
\r
43 function CI_Trackback()
\r
45 log_message('debug', "Trackback Class Initialized");
\r
48 // --------------------------------------------------------------------
\r
57 function send($tb_data)
\r
59 if ( ! is_array($tb_data))
\r
61 $this->set_error('The send() method must be passed an array');
\r
65 // Pre-process the Trackback Data
\r
66 foreach (array('url', 'title', 'excerpt', 'blog_name', 'ping_url') as $item)
\r
68 if ( ! isset($tb_data[$item]))
\r
70 $this->set_error('Required item missing: '.$item);
\r
76 case 'ping_url' : $$item = $this->extract_urls($tb_data[$item]);
\r
78 case 'excerpt' : $$item = $this->limit_characters($this->convert_xml(strip_tags(stripslashes($tb_data[$item]))));
\r
80 case 'url' : $$item = str_replace('-', '-', $this->convert_xml(strip_tags(stripslashes($tb_data[$item]))));
\r
82 default : $$item = $this->convert_xml(strip_tags(stripslashes($tb_data[$item])));
\r
86 // Convert High ASCII Characters
\r
87 if ($this->convert_ascii == TRUE)
\r
89 if ($item == 'excerpt')
\r
91 $$item = $this->convert_ascii($$item);
\r
93 elseif ($item == 'title')
\r
95 $$item = $this->convert_ascii($$item);
\r
97 elseif($item == 'blog_name')
\r
99 $$item = $this->convert_ascii($$item);
\r
104 // Build the Trackback data string
\r
105 $charset = ( ! isset($tb_data['charset'])) ? $this->charset : $tb_data['charset'];
\r
107 $data = "url=".rawurlencode($url)."&title=".rawurlencode($title)."&blog_name=".rawurlencode($blog_name)."&excerpt=".rawurlencode($excerpt)."&charset=".rawurlencode($charset);
\r
109 // Send Trackback(s)
\r
111 if (count($ping_url) > 0)
\r
113 foreach ($ping_url as $url)
\r
115 if ($this->process($url, $data) == FALSE)
\r
125 // --------------------------------------------------------------------
\r
128 * Receive Trackback Data
\r
130 * This function simply validates the incoming TB data.
\r
131 * It returns false on failure and true on success.
\r
132 * If the data is valid it is set to the $this->data array
\r
133 * so that it can be inserted into a database.
\r
140 foreach (array('url', 'title', 'blog_name', 'excerpt') as $val)
\r
142 if ( ! isset($_POST[$val]) OR $_POST[$val] == '')
\r
144 $this->set_error('The following required POST variable is missing: '.$val);
\r
148 $this->data['charset'] = ( ! isset($_POST['charset'])) ? 'auto' : strtoupper(trim($_POST['charset']));
\r
150 if ($val != 'url' && function_exists('mb_convert_encoding'))
\r
152 $_POST[$val] = mb_convert_encoding($_POST[$val], $this->charset, $this->data['charset']);
\r
155 $_POST[$val] = ($val != 'url') ? $this->convert_xml(strip_tags($_POST[$val])) : strip_tags($_POST[$val]);
\r
157 if ($val == 'excerpt')
\r
159 $_POST['excerpt'] = $this->limit_characters($_POST['excerpt']);
\r
162 $this->data[$val] = $_POST[$val];
\r
168 // --------------------------------------------------------------------
\r
171 * Send Trackback Error Message
\r
173 * Allows custom errors to be set. By default it
\r
174 * sends the "incomplete information" error, as that's
\r
175 * the most common one.
\r
181 function send_error($message = 'Incomplete Information')
\r
183 echo "<?xml version=\"1.0\" encoding=\"utf-8\"?".">\n<response>\n<error>1</error>\n<message>".$message."</message>\n</response>";
\r
187 // --------------------------------------------------------------------
\r
190 * Send Trackback Success Message
\r
192 * This should be called when a trackback has been
\r
193 * successfully received and inserted.
\r
198 function send_success()
\r
200 echo "<?xml version=\"1.0\" encoding=\"utf-8\"?".">\n<response>\n<error>0</error>\n</response>";
\r
204 // --------------------------------------------------------------------
\r
207 * Fetch a particular item
\r
213 function data($item)
\r
215 return ( ! isset($this->data[$item])) ? '' : $this->data[$item];
\r
218 // --------------------------------------------------------------------
\r
221 * Process Trackback
\r
223 * Opens a socket connection and passes the data to
\r
224 * the server. Returns true on success, false on failure
\r
231 function process($url, $data)
\r
233 $target = parse_url($url);
\r
236 if ( ! $fp = @fsockopen($target['host'], 80))
\r
238 $this->set_error('Invalid Connection: '.$url);
\r
243 $ppath = ( ! isset($target['path'])) ? $url : $target['path'];
\r
245 $path = (isset($target['query']) && $target['query'] != "") ? $ppath.'?'.$target['query'] : $ppath;
\r
247 // Add the Trackback ID to the data string
\r
248 if ($id = $this->get_id($url))
\r
250 $data = "tb_id=".$id."&".$data;
\r
253 // Transfer the data
\r
254 fputs ($fp, "POST " . $path . " HTTP/1.0\r\n" );
\r
255 fputs ($fp, "Host: " . $target['host'] . "\r\n" );
\r
256 fputs ($fp, "Content-type: application/x-www-form-urlencoded\r\n" );
\r
257 fputs ($fp, "Content-length: " . strlen($data) . "\r\n" );
\r
258 fputs ($fp, "Connection: close\r\n\r\n" );
\r
259 fputs ($fp, $data);
\r
261 // Was it successful?
\r
262 $this->response = "";
\r
264 while( ! feof($fp))
\r
266 $this->response .= fgets($fp, 128);
\r
270 if ( ! eregi("<error>0</error>", $this->response))
\r
272 $message = 'An unknown error was encountered';
\r
274 if (preg_match("/<message>(.*?)<\/message>/is", $this->response, $match))
\r
276 $message = trim($match['1']);
\r
279 $this->set_error($message);
\r
286 // --------------------------------------------------------------------
\r
289 * Extract Trackback URLs
\r
291 * This function lets multiple trackbacks be sent.
\r
292 * It takes a string of URLs (separated by comma or
\r
293 * space) and puts each URL into an array
\r
299 function extract_urls($urls)
\r
301 // Remove the pesky white space and replace with a comma.
\r
302 $urls = preg_replace("/\s*(\S+)\s*/", "\\1,", $urls);
\r
304 // If they use commas get rid of the doubles.
\r
305 $urls = str_replace(",,", ",", $urls);
\r
307 // Remove any comma that might be at the end
\r
308 if (substr($urls, -1) == ",")
\r
310 $urls = substr($urls, 0, -1);
\r
313 // Break into an array via commas
\r
314 $urls = preg_split('/[,]/', $urls);
\r
316 // Removes duplicates
\r
317 $urls = array_unique($urls);
\r
319 array_walk($urls, array($this, 'validate_url'));
\r
324 // --------------------------------------------------------------------
\r
329 * Simply adds "http://" if missing
\r
335 function validate_url($url)
\r
339 if (substr($url, 0, 4) != "http")
\r
341 $url = "http://".$url;
\r
345 // --------------------------------------------------------------------
\r
348 * Find the Trackback URL's ID
\r
354 function get_id($url)
\r
358 if (strstr($url, '?'))
\r
360 $tb_array = explode('/', $url);
\r
361 $tb_end = $tb_array[count($tb_array)-1];
\r
363 if ( ! is_numeric($tb_end))
\r
365 $tb_end = $tb_array[count($tb_array)-2];
\r
368 $tb_array = explode('=', $tb_end);
\r
369 $tb_id = $tb_array[count($tb_array)-1];
\r
373 if (ereg("/$", $url))
\r
375 $url = substr($url, 0, -1);
\r
378 $tb_array = explode('/', $url);
\r
379 $tb_id = $tb_array[count($tb_array)-1];
\r
381 if ( ! is_numeric($tb_id))
\r
383 $tb_id = $tb_array[count($tb_array)-2];
\r
387 if ( ! preg_match ("/^([0-9]+)$/", $tb_id))
\r
397 // --------------------------------------------------------------------
\r
400 * Convert Reserved XML characters to Entities
\r
406 function convert_xml($str)
\r
408 $temp = '__TEMP_AMPERSANDS__';
\r
410 $str = preg_replace("/&#(\d+);/", "$temp\\1;", $str);
\r
411 $str = preg_replace("/&(\w+);/", "$temp\\1;", $str);
\r
413 $str = str_replace(array("&","<",">","\"", "'", "-"),
\r
414 array("&", "<", ">", """, "'", "-"),
\r
417 $str = preg_replace("/$temp(\d+);/","&#\\1;",$str);
\r
418 $str = preg_replace("/$temp(\w+);/","&\\1;", $str);
\r
423 // --------------------------------------------------------------------
\r
426 * Character limiter
\r
428 * Limits the string based on the character count. Will preserve complete words.
\r
436 function limit_characters($str, $n = 500, $end_char = '…')
\r
438 if (strlen($str) < $n)
\r
443 $str = preg_replace("/\s+/", ' ', str_replace(array("\r\n", "\r", "\n"), ' ', $str));
\r
445 if (strlen($str) <= $n)
\r
451 foreach (explode(' ', trim($str)) as $val)
\r
454 if (strlen($out) >= $n)
\r
456 return trim($out).$end_char;
\r
461 // --------------------------------------------------------------------
\r
464 * High ASCII to Entities
\r
466 * Converts Hight ascii text and MS Word special chars
\r
467 * to character entities
\r
473 function convert_ascii($str)
\r
479 for ($i = 0, $s = strlen($str); $i < $s; $i++)
\r
481 $ordinal = ord($str[$i]);
\r
483 if ($ordinal < 128)
\r
489 if (count($temp) == 0)
\r
491 $count = ($ordinal < 224) ? 2 : 3;
\r
494 $temp[] = $ordinal;
\r
496 if (count($temp) == $count)
\r
498 $number = ($count == 3) ? (($temp['0'] % 16) * 4096) + (($temp['1'] % 64) * 64) + ($temp['2'] % 64) : (($temp['0'] % 32) * 64) + ($temp['1'] % 64);
\r
500 $out .= '&#'.$number.';';
\r
510 // --------------------------------------------------------------------
\r
513 * Set error message
\r
519 function set_error($msg)
\r
521 log_message('error', $msg);
\r
522 $this->error_msg[] = $msg;
\r
525 // --------------------------------------------------------------------
\r
528 * Show error messages
\r
535 function display_errors($open = '<p>', $close = '</p>')
\r
538 foreach ($this->error_msg as $val)
\r
540 $str .= $open.$val.$close;
\r
547 // END Trackback Class
\r
549 /* End of file Trackback.php */
\r
550 /* Location: ./system/libraries/Trackback.php */