1
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
5
* An open source application development framework for PHP 5.1.6 or newer
8
* @author ExpressionEngine Dev Team
9
* @copyright Copyright (c) 2008 - 2011, EllisLab, Inc.
10
* @license http://codeigniter.com/user_guide/license.html
11
* @link http://codeigniter.com
16
// ------------------------------------------------------------------------
21
* Trackback Sending/Receiving Class
23
* @package CodeIgniter
24
* @subpackage Libraries
25
* @category Trackbacks
26
* @author ExpressionEngine Dev Team
27
* @link http://codeigniter.com/user_guide/libraries/trackback.html
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;
36
var $error_msg = array();
43
public function __construct()
45
log_message('debug', "Trackback Class Initialized");
48
// --------------------------------------------------------------------
57
function send($tb_data)
59
if ( ! is_array($tb_data))
61
$this->set_error('The send() method must be passed an array');
65
// Pre-process the Trackback Data
66
foreach (array('url', 'title', 'excerpt', 'blog_name', 'ping_url') as $item)
68
if ( ! isset($tb_data[$item]))
70
$this->set_error('Required item missing: '.$item);
76
case 'ping_url' : $$item = $this->extract_urls($tb_data[$item]);
78
case 'excerpt' : $$item = $this->limit_characters($this->convert_xml(strip_tags(stripslashes($tb_data[$item]))));
80
case 'url' : $$item = str_replace('-', '-', $this->convert_xml(strip_tags(stripslashes($tb_data[$item]))));
82
default : $$item = $this->convert_xml(strip_tags(stripslashes($tb_data[$item])));
86
// Convert High ASCII Characters
87
if ($this->convert_ascii == TRUE)
89
if ($item == 'excerpt')
91
$$item = $this->convert_ascii($$item);
93
elseif ($item == 'title')
95
$$item = $this->convert_ascii($$item);
97
elseif ($item == 'blog_name')
99
$$item = $this->convert_ascii($$item);
104
// Build the Trackback data string
105
$charset = ( ! isset($tb_data['charset'])) ? $this->charset : $tb_data['charset'];
107
$data = "url=".rawurlencode($url)."&title=".rawurlencode($title)."&blog_name=".rawurlencode($blog_name)."&excerpt=".rawurlencode($excerpt)."&charset=".rawurlencode($charset);
111
if (count($ping_url) > 0)
113
foreach ($ping_url as $url)
115
if ($this->process($url, $data) == FALSE)
125
// --------------------------------------------------------------------
128
* Receive Trackback Data
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.
140
foreach (array('url', 'title', 'blog_name', 'excerpt') as $val)
142
if ( ! isset($_POST[$val]) OR $_POST[$val] == '')
144
$this->set_error('The following required POST variable is missing: '.$val);
148
$this->data['charset'] = ( ! isset($_POST['charset'])) ? 'auto' : strtoupper(trim($_POST['charset']));
150
if ($val != 'url' && function_exists('mb_convert_encoding'))
152
$_POST[$val] = mb_convert_encoding($_POST[$val], $this->charset, $this->data['charset']);
155
$_POST[$val] = ($val != 'url') ? $this->convert_xml(strip_tags($_POST[$val])) : strip_tags($_POST[$val]);
157
if ($val == 'excerpt')
159
$_POST['excerpt'] = $this->limit_characters($_POST['excerpt']);
162
$this->data[$val] = $_POST[$val];
168
// --------------------------------------------------------------------
171
* Send Trackback Error Message
173
* Allows custom errors to be set. By default it
174
* sends the "incomplete information" error, as that's
175
* the most common one.
181
function send_error($message = 'Incomplete Information')
183
echo "<?xml version=\"1.0\" encoding=\"utf-8\"?".">\n<response>\n<error>1</error>\n<message>".$message."</message>\n</response>";
187
// --------------------------------------------------------------------
190
* Send Trackback Success Message
192
* This should be called when a trackback has been
193
* successfully received and inserted.
198
function send_success()
200
echo "<?xml version=\"1.0\" encoding=\"utf-8\"?".">\n<response>\n<error>0</error>\n</response>";
204
// --------------------------------------------------------------------
207
* Fetch a particular item
215
return ( ! isset($this->data[$item])) ? '' : $this->data[$item];
218
// --------------------------------------------------------------------
223
* Opens a socket connection and passes the data to
224
* the server. Returns TRUE on success, FALSE on failure
231
function process($url, $data)
233
$target = parse_url($url);
236
if ( ! $fp = @fsockopen($target['host'], 80))
238
$this->set_error('Invalid Connection: '.$url);
243
$ppath = ( ! isset($target['path'])) ? $url : $target['path'];
245
$path = (isset($target['query']) && $target['query'] != "") ? $ppath.'?'.$target['query'] : $ppath;
247
// Add the Trackback ID to the data string
248
if ($id = $this->get_id($url))
250
$data = "tb_id=".$id."&".$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" );
261
// Was it successful?
262
$this->response = "";
266
$this->response .= fgets($fp, 128);
271
if (stristr($this->response, '<error>0</error>') === FALSE)
273
$message = 'An unknown error was encountered';
275
if (preg_match("/<message>(.*?)<\/message>/is", $this->response, $match))
277
$message = trim($match['1']);
280
$this->set_error($message);
287
// --------------------------------------------------------------------
290
* Extract Trackback URLs
292
* This function lets multiple trackbacks be sent.
293
* It takes a string of URLs (separated by comma or
294
* space) and puts each URL into an array
300
function extract_urls($urls)
302
// Remove the pesky white space and replace with a comma.
303
$urls = preg_replace("/\s*(\S+)\s*/", "\\1,", $urls);
305
// If they use commas get rid of the doubles.
306
$urls = str_replace(",,", ",", $urls);
308
// Remove any comma that might be at the end
309
if (substr($urls, -1) == ",")
311
$urls = substr($urls, 0, -1);
314
// Break into an array via commas
315
$urls = preg_split('/[,]/', $urls);
317
// Removes duplicates
318
$urls = array_unique($urls);
320
array_walk($urls, array($this, 'validate_url'));
325
// --------------------------------------------------------------------
330
* Simply adds "http://" if missing
336
function validate_url($url)
340
if (substr($url, 0, 4) != "http")
342
$url = "http://".$url;
346
// --------------------------------------------------------------------
349
* Find the Trackback URL's ID
355
function get_id($url)
359
if (strpos($url, '?') !== FALSE)
361
$tb_array = explode('/', $url);
362
$tb_end = $tb_array[count($tb_array)-1];
364
if ( ! is_numeric($tb_end))
366
$tb_end = $tb_array[count($tb_array)-2];
369
$tb_array = explode('=', $tb_end);
370
$tb_id = $tb_array[count($tb_array)-1];
374
$url = rtrim($url, '/');
376
$tb_array = explode('/', $url);
377
$tb_id = $tb_array[count($tb_array)-1];
379
if ( ! is_numeric($tb_id))
381
$tb_id = $tb_array[count($tb_array)-2];
385
if ( ! preg_match ("/^([0-9]+)$/", $tb_id))
395
// --------------------------------------------------------------------
398
* Convert Reserved XML characters to Entities
404
function convert_xml($str)
406
$temp = '__TEMP_AMPERSANDS__';
408
$str = preg_replace("/&#(\d+);/", "$temp\\1;", $str);
409
$str = preg_replace("/&(\w+);/", "$temp\\1;", $str);
411
$str = str_replace(array("&","<",">","\"", "'", "-"),
412
array("&", "<", ">", """, "'", "-"),
415
$str = preg_replace("/$temp(\d+);/","&#\\1;",$str);
416
$str = preg_replace("/$temp(\w+);/","&\\1;", $str);
421
// --------------------------------------------------------------------
426
* Limits the string based on the character count. Will preserve complete words.
434
function limit_characters($str, $n = 500, $end_char = '…')
436
if (strlen($str) < $n)
441
$str = preg_replace("/\s+/", ' ', str_replace(array("\r\n", "\r", "\n"), ' ', $str));
443
if (strlen($str) <= $n)
449
foreach (explode(' ', trim($str)) as $val)
452
if (strlen($out) >= $n)
454
return trim($out).$end_char;
459
// --------------------------------------------------------------------
462
* High ASCII to Entities
464
* Converts Hight ascii text and MS Word special chars
465
* to character entities
471
function convert_ascii($str)
477
for ($i = 0, $s = strlen($str); $i < $s; $i++)
479
$ordinal = ord($str[$i]);
487
if (count($temp) == 0)
489
$count = ($ordinal < 224) ? 2 : 3;
494
if (count($temp) == $count)
496
$number = ($count == 3) ? (($temp['0'] % 16) * 4096) + (($temp['1'] % 64) * 64) + ($temp['2'] % 64) : (($temp['0'] % 32) * 64) + ($temp['1'] % 64);
498
$out .= '&#'.$number.';';
508
// --------------------------------------------------------------------
517
function set_error($msg)
519
log_message('error', $msg);
520
$this->error_msg[] = $msg;
523
// --------------------------------------------------------------------
526
* Show error messages
533
function display_errors($open = '<p>', $close = '</p>')
536
foreach ($this->error_msg as $val)
538
$str .= $open.$val.$close;
545
// END Trackback Class
547
/* End of file Trackback.php */
548
/* Location: ./system/libraries/Trackback.php */
b'\\ No newline at end of file'