Public Member Functions | |
| CI_Trackback () | |
| Constructor. | |
| send ($tb_data) | |
| Send Trackback. | |
| receive () | |
| Receive Trackback Data. | |
| send_error ($message= 'Incomplete Information') | |
| Send Trackback Error Message. | |
| send_success () | |
| Send Trackback Success Message. | |
| data ($item) | |
| Fetch a particular item. | |
| process ($url, $data) | |
| Process Trackback. | |
| extract_urls ($urls) | |
| Extract Trackback URLs. | |
| validate_url ($url) | |
| Validate URL. | |
| get_id ($url) | |
| Find the Trackback URL's ID. | |
| convert_xml ($str) | |
| Convert Reserved XML characters to Entities. | |
| limit_characters ($str, $n=500, $end_char= '…') | |
| Character limiter. | |
| convert_ascii ($str) | |
| High ASCII to Entities. | |
| set_error ($msg) | |
| Set error message. | |
| display_errors ($open= '< p >', $close= '</p >') | |
| Show error messages. | |
Public Attributes | |
| $time_format = 'local' | |
| $charset = 'UTF-8' | |
| $data = array('url' => '', 'title' => '', 'excerpt' => '', 'blog_name' => '', 'charset' => '') | |
| $convert_ascii = TRUE | |
| $response = '' | |
| $error_msg = array() | |
Definition at line 29 of file Trackback.php.
| CI_Trackback::CI_Trackback | ( | ) |
Constructor.
public
Definition at line 43 of file Trackback.php.
References log_message().
00044 { 00045 log_message('debug', "Trackback Class Initialized"); 00046 }

| CI_Trackback::convert_ascii | ( | $ | str | ) |
High ASCII to Entities.
Converts Hight ascii text and MS Word special chars to character entities
public
| string |
Definition at line 473 of file Trackback.php.
Referenced by send().
00474 { 00475 $count = 1; 00476 $out = ''; 00477 $temp = array(); 00478 00479 for ($i = 0, $s = strlen($str); $i < $s; $i++) 00480 { 00481 $ordinal = ord($str[$i]); 00482 00483 if ($ordinal < 128) 00484 { 00485 $out .= $str[$i]; 00486 } 00487 else 00488 { 00489 if (count($temp) == 0) 00490 { 00491 $count = ($ordinal < 224) ? 2 : 3; 00492 } 00493 00494 $temp[] = $ordinal; 00495 00496 if (count($temp) == $count) 00497 { 00498 $number = ($count == 3) ? (($temp['0'] % 16) * 4096) + (($temp['1'] % 64) * 64) + ($temp['2'] % 64) : (($temp['0'] % 32) * 64) + ($temp['1'] % 64); 00499 00500 $out .= '&#'.$number.';'; 00501 $count = 1; 00502 $temp = array(); 00503 } 00504 } 00505 } 00506 00507 return $out; 00508 }

| CI_Trackback::convert_xml | ( | $ | str | ) |
Convert Reserved XML characters to Entities.
public
| string |
Definition at line 406 of file Trackback.php.
Referenced by receive(), and send().
00407 { 00408 $temp = '__TEMP_AMPERSANDS__'; 00409 00410 $str = preg_replace("/&#(\d+);/", "$temp\\1;", $str); 00411 $str = preg_replace("/&(\w+);/", "$temp\\1;", $str); 00412 00413 $str = str_replace(array("&","<",">","\"", "'", "-"), 00414 array("&", "<", ">", """, "'", "-"), 00415 $str); 00416 00417 $str = preg_replace("/$temp(\d+);/","&#\\1;",$str); 00418 $str = preg_replace("/$temp(\w+);/","&\\1;", $str); 00419 00420 return $str; 00421 }

| CI_Trackback::data | ( | $ | item | ) |
Fetch a particular item.
public
| string |
Definition at line 213 of file Trackback.php.
Referenced by receive().

| CI_Trackback::display_errors | ( | $ | open = '<p>', |
|
| $ | close = '</p>' | |||
| ) |
Show error messages.
public
| string | ||
| string |
Definition at line 535 of file Trackback.php.
00536 { 00537 $str = ''; 00538 foreach ($this->error_msg as $val) 00539 { 00540 $str .= $open.$val.$close; 00541 } 00542 00543 return $str; 00544 }
| CI_Trackback::extract_urls | ( | $ | urls | ) |
Extract Trackback URLs.
This function lets multiple trackbacks be sent. It takes a string of URLs (separated by comma or space) and puts each URL into an array
public
| string |
Definition at line 299 of file Trackback.php.
Referenced by send().
00300 { 00301 // Remove the pesky white space and replace with a comma. 00302 $urls = preg_replace("/\s*(\S+)\s*/", "\\1,", $urls); 00303 00304 // If they use commas get rid of the doubles. 00305 $urls = str_replace(",,", ",", $urls); 00306 00307 // Remove any comma that might be at the end 00308 if (substr($urls, -1) == ",") 00309 { 00310 $urls = substr($urls, 0, -1); 00311 } 00312 00313 // Break into an array via commas 00314 $urls = preg_split('/[,]/', $urls); 00315 00316 // Removes duplicates 00317 $urls = array_unique($urls); 00318 00319 array_walk($urls, array($this, 'validate_url')); 00320 00321 return $urls; 00322 }

| CI_Trackback::get_id | ( | $ | url | ) |
Find the Trackback URL's ID.
public
| string |
Definition at line 354 of file Trackback.php.
Referenced by process().
00355 { 00356 $tb_id = ""; 00357 00358 if (strstr($url, '?')) 00359 { 00360 $tb_array = explode('/', $url); 00361 $tb_end = $tb_array[count($tb_array)-1]; 00362 00363 if ( ! is_numeric($tb_end)) 00364 { 00365 $tb_end = $tb_array[count($tb_array)-2]; 00366 } 00367 00368 $tb_array = explode('=', $tb_end); 00369 $tb_id = $tb_array[count($tb_array)-1]; 00370 } 00371 else 00372 { 00373 if (ereg("/$", $url)) 00374 { 00375 $url = substr($url, 0, -1); 00376 } 00377 00378 $tb_array = explode('/', $url); 00379 $tb_id = $tb_array[count($tb_array)-1]; 00380 00381 if ( ! is_numeric($tb_id)) 00382 { 00383 $tb_id = $tb_array[count($tb_array)-2]; 00384 } 00385 } 00386 00387 if ( ! preg_match ("/^([0-9]+)$/", $tb_id)) 00388 { 00389 return false; 00390 } 00391 else 00392 { 00393 return $tb_id; 00394 } 00395 }

| CI_Trackback::limit_characters | ( | $ | str, | |
| $ | n = 500, |
|||
| $ | end_char = '…' | |||
| ) |
Character limiter.
Limits the string based on the character count. Will preserve complete words.
public
| string | ||
| integer | ||
| string |
Definition at line 436 of file Trackback.php.
Referenced by receive(), and send().
00437 { 00438 if (strlen($str) < $n) 00439 { 00440 return $str; 00441 } 00442 00443 $str = preg_replace("/\s+/", ' ', str_replace(array("\r\n", "\r", "\n"), ' ', $str)); 00444 00445 if (strlen($str) <= $n) 00446 { 00447 return $str; 00448 } 00449 00450 $out = ""; 00451 foreach (explode(' ', trim($str)) as $val) 00452 { 00453 $out .= $val.' '; 00454 if (strlen($out) >= $n) 00455 { 00456 return trim($out).$end_char; 00457 } 00458 } 00459 }

| CI_Trackback::process | ( | $ | url, | |
| $ | data | |||
| ) |
Process Trackback.
Opens a socket connection and passes the data to the server. Returns true on success, false on failure
public
| string | ||
| string |
Definition at line 231 of file Trackback.php.
References $data, get_id(), and set_error().
Referenced by send().
00232 { 00233 $target = parse_url($url); 00234 00235 // Open the socket 00236 if ( ! $fp = @fsockopen($target['host'], 80)) 00237 { 00238 $this->set_error('Invalid Connection: '.$url); 00239 return FALSE; 00240 } 00241 00242 // Build the path 00243 $ppath = ( ! isset($target['path'])) ? $url : $target['path']; 00244 00245 $path = (isset($target['query']) && $target['query'] != "") ? $ppath.'?'.$target['query'] : $ppath; 00246 00247 // Add the Trackback ID to the data string 00248 if ($id = $this->get_id($url)) 00249 { 00250 $data = "tb_id=".$id."&".$data; 00251 } 00252 00253 // Transfer the data 00254 fputs ($fp, "POST " . $path . " HTTP/1.0\r\n" ); 00255 fputs ($fp, "Host: " . $target['host'] . "\r\n" ); 00256 fputs ($fp, "Content-type: application/x-www-form-urlencoded\r\n" ); 00257 fputs ($fp, "Content-length: " . strlen($data) . "\r\n" ); 00258 fputs ($fp, "Connection: close\r\n\r\n" ); 00259 fputs ($fp, $data); 00260 00261 // Was it successful? 00262 $this->response = ""; 00263 00264 while( ! feof($fp)) 00265 { 00266 $this->response .= fgets($fp, 128); 00267 } 00268 @fclose($fp); 00269 00270 if ( ! eregi("<error>0</error>", $this->response)) 00271 { 00272 $message = 'An unknown error was encountered'; 00273 00274 if (preg_match("/<message>(.*?)<\/message>/is", $this->response, $match)) 00275 { 00276 $message = trim($match['1']); 00277 } 00278 00279 $this->set_error($message); 00280 return FALSE; 00281 } 00282 00283 return TRUE; 00284 }


| CI_Trackback::receive | ( | ) |
Receive Trackback Data.
This function simply validates the incoming TB data. It returns false on failure and true on success. If the data is valid it is set to the $this->data array so that it can be inserted into a database.
public
Definition at line 138 of file Trackback.php.
References convert_xml(), data(), limit_characters(), and set_error().
00139 { 00140 foreach (array('url', 'title', 'blog_name', 'excerpt') as $val) 00141 { 00142 if ( ! isset($_POST[$val]) OR $_POST[$val] == '') 00143 { 00144 $this->set_error('The following required POST variable is missing: '.$val); 00145 return FALSE; 00146 } 00147 00148 $this->data['charset'] = ( ! isset($_POST['charset'])) ? 'auto' : strtoupper(trim($_POST['charset'])); 00149 00150 if ($val != 'url' && function_exists('mb_convert_encoding')) 00151 { 00152 $_POST[$val] = mb_convert_encoding($_POST[$val], $this->charset, $this->data['charset']); 00153 } 00154 00155 $_POST[$val] = ($val != 'url') ? $this->convert_xml(strip_tags($_POST[$val])) : strip_tags($_POST[$val]); 00156 00157 if ($val == 'excerpt') 00158 { 00159 $_POST['excerpt'] = $this->limit_characters($_POST['excerpt']); 00160 } 00161 00162 $this->data[$val] = $_POST[$val]; 00163 } 00164 00165 return TRUE; 00166 }

| CI_Trackback::send | ( | $ | tb_data | ) |
Send Trackback.
public
| array |
Definition at line 57 of file Trackback.php.
References $charset, $data, convert_ascii(), convert_xml(), extract_urls(), limit_characters(), process(), and set_error().
00058 { 00059 if ( ! is_array($tb_data)) 00060 { 00061 $this->set_error('The send() method must be passed an array'); 00062 return FALSE; 00063 } 00064 00065 // Pre-process the Trackback Data 00066 foreach (array('url', 'title', 'excerpt', 'blog_name', 'ping_url') as $item) 00067 { 00068 if ( ! isset($tb_data[$item])) 00069 { 00070 $this->set_error('Required item missing: '.$item); 00071 return FALSE; 00072 } 00073 00074 switch ($item) 00075 { 00076 case 'ping_url' : $$item = $this->extract_urls($tb_data[$item]); 00077 break; 00078 case 'excerpt' : $$item = $this->limit_characters($this->convert_xml(strip_tags(stripslashes($tb_data[$item])))); 00079 break; 00080 case 'url' : $$item = str_replace('-', '-', $this->convert_xml(strip_tags(stripslashes($tb_data[$item])))); 00081 break; 00082 default : $$item = $this->convert_xml(strip_tags(stripslashes($tb_data[$item]))); 00083 break; 00084 } 00085 00086 // Convert High ASCII Characters 00087 if ($this->convert_ascii == TRUE) 00088 { 00089 if ($item == 'excerpt') 00090 { 00091 $$item = $this->convert_ascii($$item); 00092 } 00093 elseif ($item == 'title') 00094 { 00095 $$item = $this->convert_ascii($$item); 00096 } 00097 elseif($item == 'blog_name') 00098 { 00099 $$item = $this->convert_ascii($$item); 00100 } 00101 } 00102 } 00103 00104 // Build the Trackback data string 00105 $charset = ( ! isset($tb_data['charset'])) ? $this->charset : $tb_data['charset']; 00106 00107 $data = "url=".rawurlencode($url)."&title=".rawurlencode($title)."&blog_name=".rawurlencode($blog_name)."&excerpt=".rawurlencode($excerpt)."&charset=".rawurlencode($charset); 00108 00109 // Send Trackback(s) 00110 $return = TRUE; 00111 if (count($ping_url) > 0) 00112 { 00113 foreach ($ping_url as $url) 00114 { 00115 if ($this->process($url, $data) == FALSE) 00116 { 00117 $return = FALSE; 00118 } 00119 } 00120 } 00121 00122 return $return; 00123 }

| CI_Trackback::send_error | ( | $ | message = 'Incomplete Information' |
) |
Send Trackback Error Message.
Allows custom errors to be set. By default it sends the "incomplete information" error, as that's the most common one.
public
| string |
Definition at line 181 of file Trackback.php.
00182 { 00183 echo "<?xml version=\"1.0\" encoding=\"utf-8\"?".">\n<response>\n<error>1</error>\n<message>".$message."</message>\n</response>"; 00184 exit; 00185 }
| CI_Trackback::send_success | ( | ) |
Send Trackback Success Message.
This should be called when a trackback has been successfully received and inserted.
public
Definition at line 198 of file Trackback.php.
00199 { 00200 echo "<?xml version=\"1.0\" encoding=\"utf-8\"?".">\n<response>\n<error>0</error>\n</response>"; 00201 exit; 00202 }
| CI_Trackback::set_error | ( | $ | msg | ) |
Set error message.
public
| string |
Definition at line 519 of file Trackback.php.
References log_message().
Referenced by process(), receive(), and send().
00520 { 00521 log_message('error', $msg); 00522 $this->error_msg[] = $msg; 00523 }


| CI_Trackback::validate_url | ( | $ | url | ) |
Validate URL.
Simply adds "http://" if missing
public
| string |
Definition at line 335 of file Trackback.php.
00336 { 00337 $url = trim($url); 00338 00339 if (substr($url, 0, 4) != "http") 00340 { 00341 $url = "http://".$url; 00342 } 00343 }
| CI_Trackback::$charset = 'UTF-8' |
| CI_Trackback::$convert_ascii = TRUE |
Definition at line 34 of file Trackback.php.
| CI_Trackback::$data = array('url' => '', 'title' => '', 'excerpt' => '', 'blog_name' => '', 'charset' => '') |
| CI_Trackback::$error_msg = array() |
Definition at line 36 of file Trackback.php.
| CI_Trackback::$response = '' |
Definition at line 35 of file Trackback.php.
| CI_Trackback::$time_format = 'local' |
Definition at line 31 of file Trackback.php.