blob: 7bd7cba0422a3eb194a765cf121a37e26531bf20 [file] [log] [blame]
Andrey Andreevd9cfa7b2011-12-25 16:32:59 +02001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
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
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, 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 */
27
Derek Allard2067d1a2008-11-13 22:59:24 +000028/**
29 * Trackback Class
30 *
31 * Trackback Sending/Receiving Class
32 *
33 * @package CodeIgniter
34 * @subpackage Libraries
35 * @category Trackbacks
Derek Jonesf4a4bd82011-10-20 12:18:42 -050036 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000037 * @link http://codeigniter.com/user_guide/libraries/trackback.html
38 */
39class CI_Trackback {
Barry Mienydd671972010-10-04 16:33:58 +020040
Andrey Andreev3a459572011-12-21 11:23:11 +020041 public $time_format = 'local';
42 public $charset = 'UTF-8';
43 public $data = array('url' => '', 'title' => '', 'excerpt' => '', 'blog_name' => '', 'charset' => '');
44 public $convert_ascii = TRUE;
45 public $response = '';
46 public $error_msg = array();
Derek Allard2067d1a2008-11-13 22:59:24 +000047
Andrey Andreev5fd3ae82012-10-24 14:55:35 +030048 /**
49 * Constructor
50 *
51 * @return void
52 */
Greg Akera9263282010-11-10 15:26:43 -060053 public function __construct()
Derek Allard2067d1a2008-11-13 22:59:24 +000054 {
Andrey Andreev426faa92012-04-03 19:03:58 +030055 log_message('debug', 'Trackback Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +000056 }
Barry Mienydd671972010-10-04 16:33:58 +020057
Derek Allard2067d1a2008-11-13 22:59:24 +000058 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +020059
Derek Allard2067d1a2008-11-13 22:59:24 +000060 /**
61 * Send Trackback
62 *
Derek Allard2067d1a2008-11-13 22:59:24 +000063 * @param array
64 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +020065 */
Andrey Andreev4eea9892011-12-19 12:05:41 +020066 public function send($tb_data)
Barry Mienydd671972010-10-04 16:33:58 +020067 {
Derek Allard2067d1a2008-11-13 22:59:24 +000068 if ( ! is_array($tb_data))
69 {
70 $this->set_error('The send() method must be passed an array');
71 return FALSE;
72 }
Barry Mienydd671972010-10-04 16:33:58 +020073
Derek Allard2067d1a2008-11-13 22:59:24 +000074 // Pre-process the Trackback Data
75 foreach (array('url', 'title', 'excerpt', 'blog_name', 'ping_url') as $item)
76 {
77 if ( ! isset($tb_data[$item]))
78 {
79 $this->set_error('Required item missing: '.$item);
80 return FALSE;
81 }
Barry Mienydd671972010-10-04 16:33:58 +020082
Derek Allard2067d1a2008-11-13 22:59:24 +000083 switch ($item)
84 {
85 case 'ping_url' : $$item = $this->extract_urls($tb_data[$item]);
86 break;
87 case 'excerpt' : $$item = $this->limit_characters($this->convert_xml(strip_tags(stripslashes($tb_data[$item]))));
88 break;
Barry Mienydd671972010-10-04 16:33:58 +020089 case 'url' : $$item = str_replace('&#45;', '-', $this->convert_xml(strip_tags(stripslashes($tb_data[$item]))));
Derek Allard2067d1a2008-11-13 22:59:24 +000090 break;
91 default : $$item = $this->convert_xml(strip_tags(stripslashes($tb_data[$item])));
92 break;
93 }
94
95 // Convert High ASCII Characters
Alex Bilbied261b1e2012-06-02 11:12:16 +010096 if ($this->convert_ascii === TRUE && in_array($item, array('excerpt', 'title', 'blog_name')))
Derek Allard2067d1a2008-11-13 22:59:24 +000097 {
Andrey Andreevd9cfa7b2011-12-25 16:32:59 +020098 $$item = $this->convert_ascii($$item);
Derek Allard2067d1a2008-11-13 22:59:24 +000099 }
100 }
101
102 // Build the Trackback data string
Andrey Andreev426faa92012-04-03 19:03:58 +0300103 $charset = isset($tb_data['charset']) ? $tb_data['charset'] : $this->charset;
Barry Mienydd671972010-10-04 16:33:58 +0200104
Andrey Andreev426faa92012-04-03 19:03:58 +0300105 $data = 'url='.rawurlencode($url).'&title='.rawurlencode($title).'&blog_name='.rawurlencode($blog_name)
106 .'&excerpt='.rawurlencode($excerpt).'&charset='.rawurlencode($charset);
Barry Mienydd671972010-10-04 16:33:58 +0200107
Derek Allard2067d1a2008-11-13 22:59:24 +0000108 // Send Trackback(s)
109 $return = TRUE;
110 if (count($ping_url) > 0)
111 {
112 foreach ($ping_url as $url)
113 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100114 if ($this->process($url, $data) === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000115 {
116 $return = FALSE;
117 }
Barry Mienydd671972010-10-04 16:33:58 +0200118 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000119 }
120
121 return $return;
122 }
Barry Mienydd671972010-10-04 16:33:58 +0200123
Derek Allard2067d1a2008-11-13 22:59:24 +0000124 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200125
Derek Allard2067d1a2008-11-13 22:59:24 +0000126 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -0500127 * Receive Trackback Data
Derek Allard2067d1a2008-11-13 22:59:24 +0000128 *
129 * This function simply validates the incoming TB data.
Derek Jones5052e272010-03-02 22:53:38 -0600130 * It returns FALSE on failure and TRUE on success.
Derek Allard2067d1a2008-11-13 22:59:24 +0000131 * If the data is valid it is set to the $this->data array
132 * so that it can be inserted into a database.
133 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000134 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200135 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200136 public function receive()
Barry Mienydd671972010-10-04 16:33:58 +0200137 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000138 foreach (array('url', 'title', 'blog_name', 'excerpt') as $val)
139 {
Andrey Andreev5036c9c2012-06-04 15:34:56 +0300140 if (empty($_POST[$val]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000141 {
142 $this->set_error('The following required POST variable is missing: '.$val);
143 return FALSE;
144 }
Barry Mienydd671972010-10-04 16:33:58 +0200145
Andrey Andreev426faa92012-04-03 19:03:58 +0300146 $this->data['charset'] = isset($_POST['charset']) ? strtoupper(trim($_POST['charset'])) : 'auto';
Barry Mienydd671972010-10-04 16:33:58 +0200147
Alex Bilbied261b1e2012-06-02 11:12:16 +0100148 if ($val !== 'url' && MB_ENABLED === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000149 {
150 $_POST[$val] = mb_convert_encoding($_POST[$val], $this->charset, $this->data['charset']);
151 }
Barry Mienydd671972010-10-04 16:33:58 +0200152
Alex Bilbied261b1e2012-06-02 11:12:16 +0100153 $_POST[$val] = ($val !== 'url') ? $this->convert_xml(strip_tags($_POST[$val])) : strip_tags($_POST[$val]);
Barry Mienydd671972010-10-04 16:33:58 +0200154
Alex Bilbied261b1e2012-06-02 11:12:16 +0100155 if ($val === 'excerpt')
Derek Allard2067d1a2008-11-13 22:59:24 +0000156 {
157 $_POST['excerpt'] = $this->limit_characters($_POST['excerpt']);
158 }
Barry Mienydd671972010-10-04 16:33:58 +0200159
Derek Allard2067d1a2008-11-13 22:59:24 +0000160 $this->data[$val] = $_POST[$val];
161 }
162
163 return TRUE;
Barry Mienydd671972010-10-04 16:33:58 +0200164 }
165
Derek Allard2067d1a2008-11-13 22:59:24 +0000166 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200167
Derek Allard2067d1a2008-11-13 22:59:24 +0000168 /**
169 * Send Trackback Error Message
170 *
Andrey Andreev426faa92012-04-03 19:03:58 +0300171 * Allows custom errors to be set. By default it
Derek Allard2067d1a2008-11-13 22:59:24 +0000172 * sends the "incomplete information" error, as that's
173 * the most common one.
174 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000175 * @param string
176 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200177 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200178 public function send_error($message = 'Incomplete Information')
Derek Allard2067d1a2008-11-13 22:59:24 +0000179 {
Andrey Andreev426faa92012-04-03 19:03:58 +0300180 echo '<?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 +0000181 exit;
182 }
Barry Mienydd671972010-10-04 16:33:58 +0200183
Derek Allard2067d1a2008-11-13 22:59:24 +0000184 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200185
Derek Allard2067d1a2008-11-13 22:59:24 +0000186 /**
187 * Send Trackback Success Message
188 *
189 * This should be called when a trackback has been
190 * successfully received and inserted.
191 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000192 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200193 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200194 public function send_success()
Derek Allard2067d1a2008-11-13 22:59:24 +0000195 {
Andrey Andreev426faa92012-04-03 19:03:58 +0300196 echo '<?xml version="1.0" encoding="utf-8"?'.">\n<response>\n<error>0</error>\n</response>";
Derek Allard2067d1a2008-11-13 22:59:24 +0000197 exit;
198 }
Barry Mienydd671972010-10-04 16:33:58 +0200199
Derek Allard2067d1a2008-11-13 22:59:24 +0000200 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200201
Derek Allard2067d1a2008-11-13 22:59:24 +0000202 /**
203 * Fetch a particular item
204 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000205 * @param string
206 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200207 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200208 public function data($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000209 {
Andrey Andreev426faa92012-04-03 19:03:58 +0300210 return isset($this->data[$item]) ? $this->data[$item] : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000211 }
212
213 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200214
Derek Allard2067d1a2008-11-13 22:59:24 +0000215 /**
216 * Process Trackback
217 *
218 * Opens a socket connection and passes the data to
Andrey Andreev426faa92012-04-03 19:03:58 +0300219 * the server. Returns TRUE on success, FALSE on failure
Derek Allard2067d1a2008-11-13 22:59:24 +0000220 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000221 * @param string
222 * @param string
223 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200224 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200225 public function process($url, $data)
Derek Allard2067d1a2008-11-13 22:59:24 +0000226 {
227 $target = parse_url($url);
Barry Mienydd671972010-10-04 16:33:58 +0200228
Derek Allard2067d1a2008-11-13 22:59:24 +0000229 // Open the socket
230 if ( ! $fp = @fsockopen($target['host'], 80))
231 {
232 $this->set_error('Invalid Connection: '.$url);
233 return FALSE;
234 }
235
236 // Build the path
Andrey Andreev426faa92012-04-03 19:03:58 +0300237 $ppath = isset($target['path']) ? $target['path'] : $url;
Barry Mienydd671972010-10-04 16:33:58 +0200238
Andrey Andreev426faa92012-04-03 19:03:58 +0300239 $path = empty($target['query']) ? $ppath : $ppath.'?'.$target['query'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000240
241 // Add the Trackback ID to the data string
242 if ($id = $this->get_id($url))
243 {
Andrey Andreev426faa92012-04-03 19:03:58 +0300244 $data = 'tb_id='.$id.'&'.$data;
Derek Allard2067d1a2008-11-13 22:59:24 +0000245 }
246
247 // Transfer the data
Andrey Andreev426faa92012-04-03 19:03:58 +0300248 fputs($fp, 'POST '.$path." HTTP/1.0\r\n");
249 fputs($fp, 'Host: '.$target['host']."\r\n");
250 fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n");
251 fputs($fp, 'Content-length: '.strlen($data)."\r\n");
252 fputs($fp, "Connection: close\r\n\r\n");
253 fputs($fp, $data);
Derek Allard2067d1a2008-11-13 22:59:24 +0000254
255 // Was it successful?
Barry Mienydd671972010-10-04 16:33:58 +0200256
Andrey Andreev426faa92012-04-03 19:03:58 +0300257 $this->response = '';
Pascal Kriete14287f32011-02-14 13:39:34 -0500258 while ( ! feof($fp))
Derek Allard2067d1a2008-11-13 22:59:24 +0000259 {
260 $this->response .= fgets($fp, 128);
261 }
262 @fclose($fp);
Barry Mienydd671972010-10-04 16:33:58 +0200263
Andrey Andreevd9cfa7b2011-12-25 16:32:59 +0200264 if (stripos($this->response, '<error>0</error>') === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000265 {
Andrey Andreev426faa92012-04-03 19:03:58 +0300266 $message = preg_match('/<message>(.*?)<\/message>/is', $this->response, $match) ? trim($match[1]) : 'An unknown error was encountered';
Derek Allard2067d1a2008-11-13 22:59:24 +0000267 $this->set_error($message);
268 return FALSE;
269 }
270
271 return TRUE;
272 }
Barry Mienydd671972010-10-04 16:33:58 +0200273
Derek Allard2067d1a2008-11-13 22:59:24 +0000274 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200275
Derek Allard2067d1a2008-11-13 22:59:24 +0000276 /**
277 * Extract Trackback URLs
278 *
279 * This function lets multiple trackbacks be sent.
280 * It takes a string of URLs (separated by comma or
281 * space) and puts each URL into an array
282 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000283 * @param string
284 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200285 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200286 public function extract_urls($urls)
Barry Mienydd671972010-10-04 16:33:58 +0200287 {
Andrey Andreev426faa92012-04-03 19:03:58 +0300288 // Remove the pesky white space and replace with a comma, then replace doubles.
289 $urls = str_replace(',,', ',', preg_replace('/\s*(\S+)\s*/', '\\1,', $urls));
Barry Mienydd671972010-10-04 16:33:58 +0200290
Derek Allard2067d1a2008-11-13 22:59:24 +0000291 // Remove any comma that might be at the end
Andrey Andreevd9cfa7b2011-12-25 16:32:59 +0200292 if (substr($urls, -1) === ',')
Derek Allard2067d1a2008-11-13 22:59:24 +0000293 {
294 $urls = substr($urls, 0, -1);
295 }
Barry Mienydd671972010-10-04 16:33:58 +0200296
Andrey Andreev426faa92012-04-03 19:03:58 +0300297 // Break into an array via commas and remove duplicates
298 $urls = array_unique(preg_split('/[,]/', $urls));
Barry Mienydd671972010-10-04 16:33:58 +0200299
Derek Allard2067d1a2008-11-13 22:59:24 +0000300 array_walk($urls, array($this, 'validate_url'));
Barry Mienydd671972010-10-04 16:33:58 +0200301
Derek Allard2067d1a2008-11-13 22:59:24 +0000302 return $urls;
303 }
Barry Mienydd671972010-10-04 16:33:58 +0200304
Derek Allard2067d1a2008-11-13 22:59:24 +0000305 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200306
Derek Allard2067d1a2008-11-13 22:59:24 +0000307 /**
308 * Validate URL
309 *
310 * Simply adds "http://" if missing
311 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000312 * @param string
Andrey Andreev426faa92012-04-03 19:03:58 +0300313 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200314 */
Andrey Andreev426faa92012-04-03 19:03:58 +0300315 public function validate_url(&$url)
Derek Allard2067d1a2008-11-13 22:59:24 +0000316 {
317 $url = trim($url);
318
Andrey Andreevd9cfa7b2011-12-25 16:32:59 +0200319 if (strpos($url, 'http') !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000320 {
Andrey Andreevd9cfa7b2011-12-25 16:32:59 +0200321 $url = 'http://'.$url;
Derek Allard2067d1a2008-11-13 22:59:24 +0000322 }
323 }
Barry Mienydd671972010-10-04 16:33:58 +0200324
Derek Allard2067d1a2008-11-13 22:59:24 +0000325 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200326
Derek Allard2067d1a2008-11-13 22:59:24 +0000327 /**
328 * Find the Trackback URL's ID
329 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000330 * @param string
331 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200332 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200333 public function get_id($url)
Barry Mienydd671972010-10-04 16:33:58 +0200334 {
Andrey Andreev426faa92012-04-03 19:03:58 +0300335 $tb_id = '';
Barry Mienydd671972010-10-04 16:33:58 +0200336
Robin Sowell76b369e2010-03-19 11:15:28 -0400337 if (strpos($url, '?') !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000338 {
339 $tb_array = explode('/', $url);
Derek Jones37f4b9c2011-07-01 17:56:50 -0500340 $tb_end = $tb_array[count($tb_array)-1];
Barry Mienydd671972010-10-04 16:33:58 +0200341
Derek Allard2067d1a2008-11-13 22:59:24 +0000342 if ( ! is_numeric($tb_end))
343 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500344 $tb_end = $tb_array[count($tb_array)-2];
Derek Allard2067d1a2008-11-13 22:59:24 +0000345 }
Barry Mienydd671972010-10-04 16:33:58 +0200346
Derek Allard2067d1a2008-11-13 22:59:24 +0000347 $tb_array = explode('=', $tb_end);
348 $tb_id = $tb_array[count($tb_array)-1];
349 }
350 else
351 {
Derek Jones1322ec52009-03-11 17:01:14 +0000352 $url = rtrim($url, '/');
Barry Mienydd671972010-10-04 16:33:58 +0200353
Derek Allard2067d1a2008-11-13 22:59:24 +0000354 $tb_array = explode('/', $url);
355 $tb_id = $tb_array[count($tb_array)-1];
Barry Mienydd671972010-10-04 16:33:58 +0200356
Derek Allard2067d1a2008-11-13 22:59:24 +0000357 if ( ! is_numeric($tb_id))
358 {
Andrey Andreev426faa92012-04-03 19:03:58 +0300359 $tb_id = $tb_array[count($tb_array)-2];
Derek Allard2067d1a2008-11-13 22:59:24 +0000360 }
Barry Mienydd671972010-10-04 16:33:58 +0200361 }
362
Andrey Andreev426faa92012-04-03 19:03:58 +0300363 return preg_match('/^[0-9]+$/', $tb_id) ? $tb_id : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000364 }
Barry Mienydd671972010-10-04 16:33:58 +0200365
Derek Allard2067d1a2008-11-13 22:59:24 +0000366 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200367
Derek Allard2067d1a2008-11-13 22:59:24 +0000368 /**
369 * Convert Reserved XML characters to Entities
370 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000371 * @param string
372 * @return string
373 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200374 public function convert_xml($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000375 {
376 $temp = '__TEMP_AMPERSANDS__';
Barry Mienydd671972010-10-04 16:33:58 +0200377
Andrey Andreev426faa92012-04-03 19:03:58 +0300378 $str = preg_replace(array('/&#(\d+);/', '/&(\w+);/'), $temp.'\\1;', $str);
Barry Mienydd671972010-10-04 16:33:58 +0200379
Andrey Andreev426faa92012-04-03 19:03:58 +0300380 $str = str_replace(array('&', '<', '>', '"', "'", '-'),
381 array('&amp;', '&lt;', '&gt;', '&quot;', '&#39;', '&#45;'),
382 $str);
Barry Mienydd671972010-10-04 16:33:58 +0200383
Andrey Andreev426faa92012-04-03 19:03:58 +0300384 return preg_replace(array('/'.$temp.'(\d+);/', '/'.$temp.'(\w+);/'), array('&#\\1;', '&\\1;'), $str);
Barry Mienydd671972010-10-04 16:33:58 +0200385 }
386
Derek Allard2067d1a2008-11-13 22:59:24 +0000387 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200388
Derek Allard2067d1a2008-11-13 22:59:24 +0000389 /**
390 * Character limiter
391 *
392 * Limits the string based on the character count. Will preserve complete words.
393 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000394 * @param string
Andrey Andreev426faa92012-04-03 19:03:58 +0300395 * @param int
Derek Allard2067d1a2008-11-13 22:59:24 +0000396 * @param string
397 * @return string
398 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200399 public function limit_characters($str, $n = 500, $end_char = '&#8230;')
Derek Allard2067d1a2008-11-13 22:59:24 +0000400 {
401 if (strlen($str) < $n)
402 {
403 return $str;
404 }
Barry Mienydd671972010-10-04 16:33:58 +0200405
Andrey Andreev426faa92012-04-03 19:03:58 +0300406 $str = preg_replace('/\s+/', ' ', str_replace(array("\r\n", "\r", "\n"), ' ', $str));
Barry Mienydd671972010-10-04 16:33:58 +0200407
Derek Allard2067d1a2008-11-13 22:59:24 +0000408 if (strlen($str) <= $n)
409 {
410 return $str;
411 }
Barry Mienydd671972010-10-04 16:33:58 +0200412
Andrey Andreevd9cfa7b2011-12-25 16:32:59 +0200413 $out = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000414 foreach (explode(' ', trim($str)) as $val)
415 {
Barry Mienydd671972010-10-04 16:33:58 +0200416 $out .= $val.' ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000417 if (strlen($out) >= $n)
418 {
Andrey Andreevd9cfa7b2011-12-25 16:32:59 +0200419 return rtrim($out).$end_char;
Barry Mienydd671972010-10-04 16:33:58 +0200420 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000421 }
422 }
Barry Mienydd671972010-10-04 16:33:58 +0200423
Derek Allard2067d1a2008-11-13 22:59:24 +0000424 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200425
Derek Allard2067d1a2008-11-13 22:59:24 +0000426 /**
427 * High ASCII to Entities
428 *
429 * Converts Hight ascii text and MS Word special chars
430 * to character entities
431 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000432 * @param string
433 * @return string
434 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200435 public function convert_ascii($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000436 {
Barry Mienydd671972010-10-04 16:33:58 +0200437 $count = 1;
438 $out = '';
439 $temp = array();
440
441 for ($i = 0, $s = strlen($str); $i < $s; $i++)
442 {
443 $ordinal = ord($str[$i]);
444
445 if ($ordinal < 128)
446 {
447 $out .= $str[$i];
448 }
449 else
450 {
Andrey Andreevd9cfa7b2011-12-25 16:32:59 +0200451 if (count($temp) === 0)
Barry Mienydd671972010-10-04 16:33:58 +0200452 {
453 $count = ($ordinal < 224) ? 2 : 3;
454 }
455
456 $temp[] = $ordinal;
457
Andrey Andreevd9cfa7b2011-12-25 16:32:59 +0200458 if (count($temp) === $count)
Barry Mienydd671972010-10-04 16:33:58 +0200459 {
Andrey Andreev426faa92012-04-03 19:03:58 +0300460 $number = ($count === 3)
461 ? (($temp[0] % 16) * 4096) + (($temp[1] % 64) * 64) + ($temp[2] % 64)
462 : (($temp[0] % 32) * 64) + ($temp[1] % 64);
Barry Mienydd671972010-10-04 16:33:58 +0200463
464 $out .= '&#'.$number.';';
465 $count = 1;
466 $temp = array();
467 }
468 }
469 }
470
471 return $out;
Derek Allard2067d1a2008-11-13 22:59:24 +0000472 }
Barry Mienydd671972010-10-04 16:33:58 +0200473
Derek Allard2067d1a2008-11-13 22:59:24 +0000474 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200475
Derek Allard2067d1a2008-11-13 22:59:24 +0000476 /**
477 * Set error message
478 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000479 * @param string
480 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200481 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200482 public function set_error($msg)
Derek Allard2067d1a2008-11-13 22:59:24 +0000483 {
484 log_message('error', $msg);
485 $this->error_msg[] = $msg;
486 }
Barry Mienydd671972010-10-04 16:33:58 +0200487
Derek Allard2067d1a2008-11-13 22:59:24 +0000488 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200489
Derek Allard2067d1a2008-11-13 22:59:24 +0000490 /**
491 * Show error messages
492 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000493 * @param string
494 * @param string
495 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200496 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200497 public function display_errors($open = '<p>', $close = '</p>')
Barry Mienydd671972010-10-04 16:33:58 +0200498 {
Andrey Andreev426faa92012-04-03 19:03:58 +0300499 return (count($this->error_msg) > 0) ? $open.implode($close.$open, $this->error_msg).$close : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000500 }
501
502}
Derek Allard2067d1a2008-11-13 22:59:24 +0000503
504/* End of file Trackback.php */
Andrey Andreev426faa92012-04-03 19:03:58 +0300505/* Location: ./system/libraries/Trackback.php */