blob: e22361e1f521bc8bb9f717ed3472daa49d558152 [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey Andreev4eea9892011-12-19 12:05:41 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreev4eea9892011-12-19 12:05:41 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
darwinel871754a2014-02-11 17:34:57 +010021 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000028
Derek Allard2067d1a2008-11-13 22:59:24 +000029/**
30 * Trackback Class
31 *
32 * Trackback Sending/Receiving Class
33 *
34 * @package CodeIgniter
35 * @subpackage Libraries
36 * @category Trackbacks
Derek Jonesf4a4bd82011-10-20 12:18:42 -050037 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000038 * @link http://codeigniter.com/user_guide/libraries/trackback.html
39 */
40class CI_Trackback {
Barry Mienydd671972010-10-04 16:33:58 +020041
Andrey Andreev0fa95bd2012-11-01 23:33:14 +020042 /**
43 * Character set
44 *
45 * @var string
46 */
Andrey Andreev9a0e0c72014-04-09 15:10:27 +030047 public $charset = 'UTF-8';
Andrey Andreev0fa95bd2012-11-01 23:33:14 +020048
49 /**
50 * Trackback data
51 *
52 * @var array
53 */
Andrey Andreev9a0e0c72014-04-09 15:10:27 +030054 public $data = array(
55 'url' => '',
56 'title' => '',
57 'excerpt' => '',
58 'blog_name' => '',
59 'charset' => ''
60 );
Andrey Andreev0fa95bd2012-11-01 23:33:14 +020061
62 /**
63 * Convert ASCII flag
64 *
65 * Whether to convert high-ASCII and MS Word
66 * characters to HTML entities.
67 *
68 * @var bool
69 */
Andrey Andreev9a0e0c72014-04-09 15:10:27 +030070 public $convert_ascii = TRUE;
Andrey Andreev0fa95bd2012-11-01 23:33:14 +020071
72 /**
73 * Response
74 *
75 * @var string
76 */
Andrey Andreev9a0e0c72014-04-09 15:10:27 +030077 public $response = '';
Andrey Andreev0fa95bd2012-11-01 23:33:14 +020078
79 /**
80 * Error messages list
81 *
82 * @var string[]
83 */
Andrey Andreev9a0e0c72014-04-09 15:10:27 +030084 public $error_msg = array();
Andrey Andreev0fa95bd2012-11-01 23:33:14 +020085
86 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +000087
Andrey Andreev5fd3ae82012-10-24 14:55:35 +030088 /**
89 * Constructor
90 *
91 * @return void
92 */
Greg Akera9263282010-11-10 15:26:43 -060093 public function __construct()
Derek Allard2067d1a2008-11-13 22:59:24 +000094 {
Andrey Andreev426faa92012-04-03 19:03:58 +030095 log_message('debug', 'Trackback Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +000096 }
Barry Mienydd671972010-10-04 16:33:58 +020097
Derek Allard2067d1a2008-11-13 22:59:24 +000098 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +020099
Derek Allard2067d1a2008-11-13 22:59:24 +0000100 /**
101 * Send Trackback
102 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000103 * @param array
104 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200105 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200106 public function send($tb_data)
Barry Mienydd671972010-10-04 16:33:58 +0200107 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000108 if ( ! is_array($tb_data))
109 {
110 $this->set_error('The send() method must be passed an array');
111 return FALSE;
112 }
Barry Mienydd671972010-10-04 16:33:58 +0200113
Derek Allard2067d1a2008-11-13 22:59:24 +0000114 // Pre-process the Trackback Data
115 foreach (array('url', 'title', 'excerpt', 'blog_name', 'ping_url') as $item)
116 {
117 if ( ! isset($tb_data[$item]))
118 {
119 $this->set_error('Required item missing: '.$item);
120 return FALSE;
121 }
Barry Mienydd671972010-10-04 16:33:58 +0200122
Derek Allard2067d1a2008-11-13 22:59:24 +0000123 switch ($item)
124 {
Andrey Andreev9a0e0c72014-04-09 15:10:27 +0300125 case 'ping_url':
126 $$item = $this->extract_urls($tb_data[$item]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000127 break;
Andrey Andreev9a0e0c72014-04-09 15:10:27 +0300128 case 'excerpt':
129 $$item = $this->limit_characters($this->convert_xml(strip_tags(stripslashes($tb_data[$item]))));
Derek Allard2067d1a2008-11-13 22:59:24 +0000130 break;
Andrey Andreev9a0e0c72014-04-09 15:10:27 +0300131 case 'url':
132 $$item = str_replace('&#45;', '-', $this->convert_xml(strip_tags(stripslashes($tb_data[$item]))));
Derek Allard2067d1a2008-11-13 22:59:24 +0000133 break;
Andrey Andreev9a0e0c72014-04-09 15:10:27 +0300134 default:
135 $$item = $this->convert_xml(strip_tags(stripslashes($tb_data[$item])));
Derek Allard2067d1a2008-11-13 22:59:24 +0000136 break;
137 }
138
139 // Convert High ASCII Characters
Andrey Andreev9a0e0c72014-04-09 15:10:27 +0300140 if ($this->convert_ascii === TRUE && in_array($item, array('excerpt', 'title', 'blog_name'), TRUE))
Derek Allard2067d1a2008-11-13 22:59:24 +0000141 {
Andrey Andreevd9cfa7b2011-12-25 16:32:59 +0200142 $$item = $this->convert_ascii($$item);
Derek Allard2067d1a2008-11-13 22:59:24 +0000143 }
144 }
145
146 // Build the Trackback data string
Andrey Andreev426faa92012-04-03 19:03:58 +0300147 $charset = isset($tb_data['charset']) ? $tb_data['charset'] : $this->charset;
Barry Mienydd671972010-10-04 16:33:58 +0200148
Andrey Andreev426faa92012-04-03 19:03:58 +0300149 $data = 'url='.rawurlencode($url).'&title='.rawurlencode($title).'&blog_name='.rawurlencode($blog_name)
150 .'&excerpt='.rawurlencode($excerpt).'&charset='.rawurlencode($charset);
Barry Mienydd671972010-10-04 16:33:58 +0200151
Derek Allard2067d1a2008-11-13 22:59:24 +0000152 // Send Trackback(s)
153 $return = TRUE;
154 if (count($ping_url) > 0)
155 {
156 foreach ($ping_url as $url)
157 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100158 if ($this->process($url, $data) === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000159 {
160 $return = FALSE;
161 }
Barry Mienydd671972010-10-04 16:33:58 +0200162 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000163 }
164
165 return $return;
166 }
Barry Mienydd671972010-10-04 16:33:58 +0200167
Derek Allard2067d1a2008-11-13 22:59:24 +0000168 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200169
Derek Allard2067d1a2008-11-13 22:59:24 +0000170 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -0500171 * Receive Trackback Data
Derek Allard2067d1a2008-11-13 22:59:24 +0000172 *
173 * This function simply validates the incoming TB data.
Derek Jones5052e272010-03-02 22:53:38 -0600174 * It returns FALSE on failure and TRUE on success.
Derek Allard2067d1a2008-11-13 22:59:24 +0000175 * If the data is valid it is set to the $this->data array
176 * so that it can be inserted into a database.
177 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000178 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200179 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200180 public function receive()
Barry Mienydd671972010-10-04 16:33:58 +0200181 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000182 foreach (array('url', 'title', 'blog_name', 'excerpt') as $val)
183 {
Andrey Andreev5036c9c2012-06-04 15:34:56 +0300184 if (empty($_POST[$val]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000185 {
186 $this->set_error('The following required POST variable is missing: '.$val);
187 return FALSE;
188 }
Barry Mienydd671972010-10-04 16:33:58 +0200189
Andrey Andreev426faa92012-04-03 19:03:58 +0300190 $this->data['charset'] = isset($_POST['charset']) ? strtoupper(trim($_POST['charset'])) : 'auto';
Barry Mienydd671972010-10-04 16:33:58 +0200191
Alex Bilbied261b1e2012-06-02 11:12:16 +0100192 if ($val !== 'url' && MB_ENABLED === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000193 {
Andrey Andreevbe1496d2014-02-11 22:48:45 +0200194 if (MB_ENABLED === TRUE)
195 {
196 $_POST[$val] = mb_convert_encoding($_POST[$val], $this->charset, $this->data['charset']);
197 }
198 elseif (ICONV_ENABLED === TRUE)
199 {
200 $_POST[$val] = @iconv($this->data['charset'], $this->charset.'//IGNORE', $_POST[$val]);
201 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000202 }
Barry Mienydd671972010-10-04 16:33:58 +0200203
Alex Bilbied261b1e2012-06-02 11:12:16 +0100204 $_POST[$val] = ($val !== 'url') ? $this->convert_xml(strip_tags($_POST[$val])) : strip_tags($_POST[$val]);
Barry Mienydd671972010-10-04 16:33:58 +0200205
Alex Bilbied261b1e2012-06-02 11:12:16 +0100206 if ($val === 'excerpt')
Derek Allard2067d1a2008-11-13 22:59:24 +0000207 {
208 $_POST['excerpt'] = $this->limit_characters($_POST['excerpt']);
209 }
Barry Mienydd671972010-10-04 16:33:58 +0200210
Derek Allard2067d1a2008-11-13 22:59:24 +0000211 $this->data[$val] = $_POST[$val];
212 }
213
214 return TRUE;
Barry Mienydd671972010-10-04 16:33:58 +0200215 }
216
Derek Allard2067d1a2008-11-13 22:59:24 +0000217 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200218
Derek Allard2067d1a2008-11-13 22:59:24 +0000219 /**
220 * Send Trackback Error Message
221 *
Andrey Andreev426faa92012-04-03 19:03:58 +0300222 * Allows custom errors to be set. By default it
Derek Allard2067d1a2008-11-13 22:59:24 +0000223 * sends the "incomplete information" error, as that's
224 * the most common one.
225 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000226 * @param string
227 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200228 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200229 public function send_error($message = 'Incomplete Information')
Derek Allard2067d1a2008-11-13 22:59:24 +0000230 {
Daniel Hunsaker8626e932013-03-04 05:14:22 -0700231 exit('<?xml version="1.0" encoding="utf-8"?'.">\n<response>\n<error>1</error>\n<message>".$message."</message>\n</response>");
Derek Allard2067d1a2008-11-13 22:59:24 +0000232 }
Barry Mienydd671972010-10-04 16:33:58 +0200233
Derek Allard2067d1a2008-11-13 22:59:24 +0000234 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200235
Derek Allard2067d1a2008-11-13 22:59:24 +0000236 /**
237 * Send Trackback Success Message
238 *
239 * This should be called when a trackback has been
240 * successfully received and inserted.
241 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000242 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200243 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200244 public function send_success()
Derek Allard2067d1a2008-11-13 22:59:24 +0000245 {
Daniel Hunsaker8626e932013-03-04 05:14:22 -0700246 exit('<?xml version="1.0" encoding="utf-8"?'.">\n<response>\n<error>0</error>\n</response>");
Derek Allard2067d1a2008-11-13 22:59:24 +0000247 }
Barry Mienydd671972010-10-04 16:33:58 +0200248
Derek Allard2067d1a2008-11-13 22:59:24 +0000249 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200250
Derek Allard2067d1a2008-11-13 22:59:24 +0000251 /**
252 * Fetch a particular item
253 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000254 * @param string
255 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200256 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200257 public function data($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000258 {
Andrey Andreev426faa92012-04-03 19:03:58 +0300259 return isset($this->data[$item]) ? $this->data[$item] : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000260 }
261
262 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200263
Derek Allard2067d1a2008-11-13 22:59:24 +0000264 /**
265 * Process Trackback
266 *
267 * Opens a socket connection and passes the data to
Andrey Andreev426faa92012-04-03 19:03:58 +0300268 * the server. Returns TRUE on success, FALSE on failure
Derek Allard2067d1a2008-11-13 22:59:24 +0000269 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000270 * @param string
271 * @param string
272 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200273 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200274 public function process($url, $data)
Derek Allard2067d1a2008-11-13 22:59:24 +0000275 {
276 $target = parse_url($url);
Barry Mienydd671972010-10-04 16:33:58 +0200277
Derek Allard2067d1a2008-11-13 22:59:24 +0000278 // Open the socket
279 if ( ! $fp = @fsockopen($target['host'], 80))
280 {
281 $this->set_error('Invalid Connection: '.$url);
282 return FALSE;
283 }
284
285 // Build the path
Andrey Andreev5b5401a2013-01-10 17:38:08 +0200286 $path = isset($target['path']) ? $target['path'] : $url;
287 empty($target['query']) OR $path .= '?'.$target['query'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000288
289 // Add the Trackback ID to the data string
290 if ($id = $this->get_id($url))
291 {
Andrey Andreev426faa92012-04-03 19:03:58 +0300292 $data = 'tb_id='.$id.'&'.$data;
Derek Allard2067d1a2008-11-13 22:59:24 +0000293 }
294
295 // Transfer the data
Andrey Andreev426faa92012-04-03 19:03:58 +0300296 fputs($fp, 'POST '.$path." HTTP/1.0\r\n");
297 fputs($fp, 'Host: '.$target['host']."\r\n");
298 fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n");
299 fputs($fp, 'Content-length: '.strlen($data)."\r\n");
300 fputs($fp, "Connection: close\r\n\r\n");
301 fputs($fp, $data);
Derek Allard2067d1a2008-11-13 22:59:24 +0000302
303 // Was it successful?
Barry Mienydd671972010-10-04 16:33:58 +0200304
Andrey Andreev426faa92012-04-03 19:03:58 +0300305 $this->response = '';
Pascal Kriete14287f32011-02-14 13:39:34 -0500306 while ( ! feof($fp))
Derek Allard2067d1a2008-11-13 22:59:24 +0000307 {
308 $this->response .= fgets($fp, 128);
309 }
310 @fclose($fp);
Barry Mienydd671972010-10-04 16:33:58 +0200311
Andrey Andreevd9cfa7b2011-12-25 16:32:59 +0200312 if (stripos($this->response, '<error>0</error>') === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000313 {
Andrey Andreev9a0e0c72014-04-09 15:10:27 +0300314 $message = preg_match('/<message>(.*?)<\/message>/is', $this->response, $match)
315 ? trim($match[1])
316 : 'An unknown error was encountered';
Derek Allard2067d1a2008-11-13 22:59:24 +0000317 $this->set_error($message);
318 return FALSE;
319 }
320
321 return TRUE;
322 }
Barry Mienydd671972010-10-04 16:33:58 +0200323
Derek Allard2067d1a2008-11-13 22:59:24 +0000324 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200325
Derek Allard2067d1a2008-11-13 22:59:24 +0000326 /**
327 * Extract Trackback URLs
328 *
329 * This function lets multiple trackbacks be sent.
330 * It takes a string of URLs (separated by comma or
331 * space) and puts each URL into an array
332 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000333 * @param string
334 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200335 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200336 public function extract_urls($urls)
Barry Mienydd671972010-10-04 16:33:58 +0200337 {
Andrey Andreev426faa92012-04-03 19:03:58 +0300338 // Remove the pesky white space and replace with a comma, then replace doubles.
339 $urls = str_replace(',,', ',', preg_replace('/\s*(\S+)\s*/', '\\1,', $urls));
Barry Mienydd671972010-10-04 16:33:58 +0200340
Andrey Andreev426faa92012-04-03 19:03:58 +0300341 // Break into an array via commas and remove duplicates
Andrey Andreev9a0e0c72014-04-09 15:10:27 +0300342 $urls = array_unique(preg_split('/[,]/', rtrim($urls, ',')));
Barry Mienydd671972010-10-04 16:33:58 +0200343
Derek Allard2067d1a2008-11-13 22:59:24 +0000344 array_walk($urls, array($this, 'validate_url'));
Derek Allard2067d1a2008-11-13 22:59:24 +0000345 return $urls;
346 }
Barry Mienydd671972010-10-04 16:33:58 +0200347
Derek Allard2067d1a2008-11-13 22:59:24 +0000348 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200349
Derek Allard2067d1a2008-11-13 22:59:24 +0000350 /**
351 * Validate URL
352 *
353 * Simply adds "http://" if missing
354 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000355 * @param string
Andrey Andreev426faa92012-04-03 19:03:58 +0300356 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200357 */
Andrey Andreev426faa92012-04-03 19:03:58 +0300358 public function validate_url(&$url)
Derek Allard2067d1a2008-11-13 22:59:24 +0000359 {
360 $url = trim($url);
361
Andrey Andreevd9cfa7b2011-12-25 16:32:59 +0200362 if (strpos($url, 'http') !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000363 {
Andrey Andreevd9cfa7b2011-12-25 16:32:59 +0200364 $url = 'http://'.$url;
Derek Allard2067d1a2008-11-13 22:59:24 +0000365 }
366 }
Barry Mienydd671972010-10-04 16:33:58 +0200367
Derek Allard2067d1a2008-11-13 22:59:24 +0000368 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200369
Derek Allard2067d1a2008-11-13 22:59:24 +0000370 /**
371 * Find the Trackback URL's ID
372 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000373 * @param string
374 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200375 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200376 public function get_id($url)
Barry Mienydd671972010-10-04 16:33:58 +0200377 {
Andrey Andreev426faa92012-04-03 19:03:58 +0300378 $tb_id = '';
Barry Mienydd671972010-10-04 16:33:58 +0200379
Robin Sowell76b369e2010-03-19 11:15:28 -0400380 if (strpos($url, '?') !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000381 {
382 $tb_array = explode('/', $url);
Derek Jones37f4b9c2011-07-01 17:56:50 -0500383 $tb_end = $tb_array[count($tb_array)-1];
Barry Mienydd671972010-10-04 16:33:58 +0200384
Derek Allard2067d1a2008-11-13 22:59:24 +0000385 if ( ! is_numeric($tb_end))
386 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500387 $tb_end = $tb_array[count($tb_array)-2];
Derek Allard2067d1a2008-11-13 22:59:24 +0000388 }
Barry Mienydd671972010-10-04 16:33:58 +0200389
Derek Allard2067d1a2008-11-13 22:59:24 +0000390 $tb_array = explode('=', $tb_end);
391 $tb_id = $tb_array[count($tb_array)-1];
392 }
393 else
394 {
Derek Jones1322ec52009-03-11 17:01:14 +0000395 $url = rtrim($url, '/');
Barry Mienydd671972010-10-04 16:33:58 +0200396
Derek Allard2067d1a2008-11-13 22:59:24 +0000397 $tb_array = explode('/', $url);
398 $tb_id = $tb_array[count($tb_array)-1];
Barry Mienydd671972010-10-04 16:33:58 +0200399
Derek Allard2067d1a2008-11-13 22:59:24 +0000400 if ( ! is_numeric($tb_id))
401 {
Andrey Andreev426faa92012-04-03 19:03:58 +0300402 $tb_id = $tb_array[count($tb_array)-2];
Derek Allard2067d1a2008-11-13 22:59:24 +0000403 }
Barry Mienydd671972010-10-04 16:33:58 +0200404 }
405
Andrey Andreev7a7ad782012-11-12 17:21:01 +0200406 return ctype_digit((string) $tb_id) ? $tb_id : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000407 }
Barry Mienydd671972010-10-04 16:33:58 +0200408
Derek Allard2067d1a2008-11-13 22:59:24 +0000409 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200410
Derek Allard2067d1a2008-11-13 22:59:24 +0000411 /**
412 * Convert Reserved XML characters to Entities
413 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000414 * @param string
415 * @return string
416 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200417 public function convert_xml($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000418 {
419 $temp = '__TEMP_AMPERSANDS__';
Barry Mienydd671972010-10-04 16:33:58 +0200420
Andrey Andreev426faa92012-04-03 19:03:58 +0300421 $str = preg_replace(array('/&#(\d+);/', '/&(\w+);/'), $temp.'\\1;', $str);
Barry Mienydd671972010-10-04 16:33:58 +0200422
Andrey Andreev426faa92012-04-03 19:03:58 +0300423 $str = str_replace(array('&', '<', '>', '"', "'", '-'),
424 array('&amp;', '&lt;', '&gt;', '&quot;', '&#39;', '&#45;'),
425 $str);
Barry Mienydd671972010-10-04 16:33:58 +0200426
Andrey Andreev426faa92012-04-03 19:03:58 +0300427 return preg_replace(array('/'.$temp.'(\d+);/', '/'.$temp.'(\w+);/'), array('&#\\1;', '&\\1;'), $str);
Barry Mienydd671972010-10-04 16:33:58 +0200428 }
429
Derek Allard2067d1a2008-11-13 22:59:24 +0000430 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200431
Derek Allard2067d1a2008-11-13 22:59:24 +0000432 /**
433 * Character limiter
434 *
435 * Limits the string based on the character count. Will preserve complete words.
436 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000437 * @param string
Andrey Andreev426faa92012-04-03 19:03:58 +0300438 * @param int
Derek Allard2067d1a2008-11-13 22:59:24 +0000439 * @param string
440 * @return string
441 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200442 public function limit_characters($str, $n = 500, $end_char = '&#8230;')
Derek Allard2067d1a2008-11-13 22:59:24 +0000443 {
444 if (strlen($str) < $n)
445 {
446 return $str;
447 }
Barry Mienydd671972010-10-04 16:33:58 +0200448
Andrey Andreev426faa92012-04-03 19:03:58 +0300449 $str = preg_replace('/\s+/', ' ', str_replace(array("\r\n", "\r", "\n"), ' ', $str));
Barry Mienydd671972010-10-04 16:33:58 +0200450
Derek Allard2067d1a2008-11-13 22:59:24 +0000451 if (strlen($str) <= $n)
452 {
453 return $str;
454 }
Barry Mienydd671972010-10-04 16:33:58 +0200455
Andrey Andreevd9cfa7b2011-12-25 16:32:59 +0200456 $out = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000457 foreach (explode(' ', trim($str)) as $val)
458 {
Barry Mienydd671972010-10-04 16:33:58 +0200459 $out .= $val.' ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000460 if (strlen($out) >= $n)
461 {
Andrey Andreevd9cfa7b2011-12-25 16:32:59 +0200462 return rtrim($out).$end_char;
Barry Mienydd671972010-10-04 16:33:58 +0200463 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000464 }
465 }
Barry Mienydd671972010-10-04 16:33:58 +0200466
Derek Allard2067d1a2008-11-13 22:59:24 +0000467 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200468
Derek Allard2067d1a2008-11-13 22:59:24 +0000469 /**
470 * High ASCII to Entities
471 *
472 * Converts Hight ascii text and MS Word special chars
473 * to character entities
474 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000475 * @param string
476 * @return string
477 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200478 public function convert_ascii($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000479 {
Barry Mienydd671972010-10-04 16:33:58 +0200480 $count = 1;
481 $out = '';
482 $temp = array();
483
484 for ($i = 0, $s = strlen($str); $i < $s; $i++)
485 {
486 $ordinal = ord($str[$i]);
487
488 if ($ordinal < 128)
489 {
490 $out .= $str[$i];
491 }
492 else
493 {
Andrey Andreevd9cfa7b2011-12-25 16:32:59 +0200494 if (count($temp) === 0)
Barry Mienydd671972010-10-04 16:33:58 +0200495 {
496 $count = ($ordinal < 224) ? 2 : 3;
497 }
498
499 $temp[] = $ordinal;
500
Andrey Andreevd9cfa7b2011-12-25 16:32:59 +0200501 if (count($temp) === $count)
Barry Mienydd671972010-10-04 16:33:58 +0200502 {
Andrey Andreev426faa92012-04-03 19:03:58 +0300503 $number = ($count === 3)
504 ? (($temp[0] % 16) * 4096) + (($temp[1] % 64) * 64) + ($temp[2] % 64)
505 : (($temp[0] % 32) * 64) + ($temp[1] % 64);
Barry Mienydd671972010-10-04 16:33:58 +0200506
507 $out .= '&#'.$number.';';
508 $count = 1;
509 $temp = array();
510 }
511 }
512 }
513
514 return $out;
Derek Allard2067d1a2008-11-13 22:59:24 +0000515 }
Barry Mienydd671972010-10-04 16:33:58 +0200516
Derek Allard2067d1a2008-11-13 22:59:24 +0000517 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200518
Derek Allard2067d1a2008-11-13 22:59:24 +0000519 /**
520 * Set error message
521 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000522 * @param string
523 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200524 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200525 public function set_error($msg)
Derek Allard2067d1a2008-11-13 22:59:24 +0000526 {
527 log_message('error', $msg);
528 $this->error_msg[] = $msg;
529 }
Barry Mienydd671972010-10-04 16:33:58 +0200530
Derek Allard2067d1a2008-11-13 22:59:24 +0000531 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200532
Derek Allard2067d1a2008-11-13 22:59:24 +0000533 /**
534 * Show error messages
535 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000536 * @param string
537 * @param string
538 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200539 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200540 public function display_errors($open = '<p>', $close = '</p>')
Barry Mienydd671972010-10-04 16:33:58 +0200541 {
Andrey Andreev426faa92012-04-03 19:03:58 +0300542 return (count($this->error_msg) > 0) ? $open.implode($close.$open, $this->error_msg).$close : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000543 }
544
545}
Derek Allard2067d1a2008-11-13 22:59:24 +0000546
547/* End of file Trackback.php */
Andrey Andreev426faa92012-04-03 19:03:58 +0300548/* Location: ./system/libraries/Trackback.php */