blob: ac74f36324bf0c742f3d3db805e7053c19ae268a [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
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 */
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 Andreev3a459572011-12-21 11:23:11 +020042 public $time_format = 'local';
43 public $charset = 'UTF-8';
44 public $data = array('url' => '', 'title' => '', 'excerpt' => '', 'blog_name' => '', 'charset' => '');
45 public $convert_ascii = TRUE;
46 public $response = '';
47 public $error_msg = array();
Derek Allard2067d1a2008-11-13 22:59:24 +000048
Andrey Andreev5fd3ae82012-10-24 14:55:35 +030049 /**
50 * Constructor
51 *
52 * @return void
53 */
Greg Akera9263282010-11-10 15:26:43 -060054 public function __construct()
Derek Allard2067d1a2008-11-13 22:59:24 +000055 {
Andrey Andreev426faa92012-04-03 19:03:58 +030056 log_message('debug', 'Trackback Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +000057 }
Barry Mienydd671972010-10-04 16:33:58 +020058
Derek Allard2067d1a2008-11-13 22:59:24 +000059 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +020060
Derek Allard2067d1a2008-11-13 22:59:24 +000061 /**
62 * Send Trackback
63 *
Derek Allard2067d1a2008-11-13 22:59:24 +000064 * @param array
65 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +020066 */
Andrey Andreev4eea9892011-12-19 12:05:41 +020067 public function send($tb_data)
Barry Mienydd671972010-10-04 16:33:58 +020068 {
Derek Allard2067d1a2008-11-13 22:59:24 +000069 if ( ! is_array($tb_data))
70 {
71 $this->set_error('The send() method must be passed an array');
72 return FALSE;
73 }
Barry Mienydd671972010-10-04 16:33:58 +020074
Derek Allard2067d1a2008-11-13 22:59:24 +000075 // Pre-process the Trackback Data
76 foreach (array('url', 'title', 'excerpt', 'blog_name', 'ping_url') as $item)
77 {
78 if ( ! isset($tb_data[$item]))
79 {
80 $this->set_error('Required item missing: '.$item);
81 return FALSE;
82 }
Barry Mienydd671972010-10-04 16:33:58 +020083
Derek Allard2067d1a2008-11-13 22:59:24 +000084 switch ($item)
85 {
86 case 'ping_url' : $$item = $this->extract_urls($tb_data[$item]);
87 break;
88 case 'excerpt' : $$item = $this->limit_characters($this->convert_xml(strip_tags(stripslashes($tb_data[$item]))));
89 break;
Barry Mienydd671972010-10-04 16:33:58 +020090 case 'url' : $$item = str_replace('&#45;', '-', $this->convert_xml(strip_tags(stripslashes($tb_data[$item]))));
Derek Allard2067d1a2008-11-13 22:59:24 +000091 break;
92 default : $$item = $this->convert_xml(strip_tags(stripslashes($tb_data[$item])));
93 break;
94 }
95
96 // Convert High ASCII Characters
Alex Bilbied261b1e2012-06-02 11:12:16 +010097 if ($this->convert_ascii === TRUE && in_array($item, array('excerpt', 'title', 'blog_name')))
Derek Allard2067d1a2008-11-13 22:59:24 +000098 {
Andrey Andreevd9cfa7b2011-12-25 16:32:59 +020099 $$item = $this->convert_ascii($$item);
Derek Allard2067d1a2008-11-13 22:59:24 +0000100 }
101 }
102
103 // Build the Trackback data string
Andrey Andreev426faa92012-04-03 19:03:58 +0300104 $charset = isset($tb_data['charset']) ? $tb_data['charset'] : $this->charset;
Barry Mienydd671972010-10-04 16:33:58 +0200105
Andrey Andreev426faa92012-04-03 19:03:58 +0300106 $data = 'url='.rawurlencode($url).'&title='.rawurlencode($title).'&blog_name='.rawurlencode($blog_name)
107 .'&excerpt='.rawurlencode($excerpt).'&charset='.rawurlencode($charset);
Barry Mienydd671972010-10-04 16:33:58 +0200108
Derek Allard2067d1a2008-11-13 22:59:24 +0000109 // Send Trackback(s)
110 $return = TRUE;
111 if (count($ping_url) > 0)
112 {
113 foreach ($ping_url as $url)
114 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100115 if ($this->process($url, $data) === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000116 {
117 $return = FALSE;
118 }
Barry Mienydd671972010-10-04 16:33:58 +0200119 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000120 }
121
122 return $return;
123 }
Barry Mienydd671972010-10-04 16:33:58 +0200124
Derek Allard2067d1a2008-11-13 22:59:24 +0000125 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200126
Derek Allard2067d1a2008-11-13 22:59:24 +0000127 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -0500128 * Receive Trackback Data
Derek Allard2067d1a2008-11-13 22:59:24 +0000129 *
130 * This function simply validates the incoming TB data.
Derek Jones5052e272010-03-02 22:53:38 -0600131 * It returns FALSE on failure and TRUE on success.
Derek Allard2067d1a2008-11-13 22:59:24 +0000132 * If the data is valid it is set to the $this->data array
133 * so that it can be inserted into a database.
134 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000135 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200136 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200137 public function receive()
Barry Mienydd671972010-10-04 16:33:58 +0200138 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000139 foreach (array('url', 'title', 'blog_name', 'excerpt') as $val)
140 {
Andrey Andreev5036c9c2012-06-04 15:34:56 +0300141 if (empty($_POST[$val]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000142 {
143 $this->set_error('The following required POST variable is missing: '.$val);
144 return FALSE;
145 }
Barry Mienydd671972010-10-04 16:33:58 +0200146
Andrey Andreev426faa92012-04-03 19:03:58 +0300147 $this->data['charset'] = isset($_POST['charset']) ? strtoupper(trim($_POST['charset'])) : 'auto';
Barry Mienydd671972010-10-04 16:33:58 +0200148
Alex Bilbied261b1e2012-06-02 11:12:16 +0100149 if ($val !== 'url' && MB_ENABLED === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000150 {
151 $_POST[$val] = mb_convert_encoding($_POST[$val], $this->charset, $this->data['charset']);
152 }
Barry Mienydd671972010-10-04 16:33:58 +0200153
Alex Bilbied261b1e2012-06-02 11:12:16 +0100154 $_POST[$val] = ($val !== 'url') ? $this->convert_xml(strip_tags($_POST[$val])) : strip_tags($_POST[$val]);
Barry Mienydd671972010-10-04 16:33:58 +0200155
Alex Bilbied261b1e2012-06-02 11:12:16 +0100156 if ($val === 'excerpt')
Derek Allard2067d1a2008-11-13 22:59:24 +0000157 {
158 $_POST['excerpt'] = $this->limit_characters($_POST['excerpt']);
159 }
Barry Mienydd671972010-10-04 16:33:58 +0200160
Derek Allard2067d1a2008-11-13 22:59:24 +0000161 $this->data[$val] = $_POST[$val];
162 }
163
164 return TRUE;
Barry Mienydd671972010-10-04 16:33:58 +0200165 }
166
Derek Allard2067d1a2008-11-13 22:59:24 +0000167 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200168
Derek Allard2067d1a2008-11-13 22:59:24 +0000169 /**
170 * Send Trackback Error Message
171 *
Andrey Andreev426faa92012-04-03 19:03:58 +0300172 * Allows custom errors to be set. By default it
Derek Allard2067d1a2008-11-13 22:59:24 +0000173 * sends the "incomplete information" error, as that's
174 * the most common one.
175 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000176 * @param string
177 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200178 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200179 public function send_error($message = 'Incomplete Information')
Derek Allard2067d1a2008-11-13 22:59:24 +0000180 {
Andrey Andreev426faa92012-04-03 19:03:58 +0300181 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 +0000182 exit;
183 }
Barry Mienydd671972010-10-04 16:33:58 +0200184
Derek Allard2067d1a2008-11-13 22:59:24 +0000185 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200186
Derek Allard2067d1a2008-11-13 22:59:24 +0000187 /**
188 * Send Trackback Success Message
189 *
190 * This should be called when a trackback has been
191 * successfully received and inserted.
192 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000193 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200194 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200195 public function send_success()
Derek Allard2067d1a2008-11-13 22:59:24 +0000196 {
Andrey Andreev426faa92012-04-03 19:03:58 +0300197 echo '<?xml version="1.0" encoding="utf-8"?'.">\n<response>\n<error>0</error>\n</response>";
Derek Allard2067d1a2008-11-13 22:59:24 +0000198 exit;
199 }
Barry Mienydd671972010-10-04 16:33:58 +0200200
Derek Allard2067d1a2008-11-13 22:59:24 +0000201 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200202
Derek Allard2067d1a2008-11-13 22:59:24 +0000203 /**
204 * Fetch a particular item
205 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000206 * @param string
207 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200208 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200209 public function data($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000210 {
Andrey Andreev426faa92012-04-03 19:03:58 +0300211 return isset($this->data[$item]) ? $this->data[$item] : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000212 }
213
214 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200215
Derek Allard2067d1a2008-11-13 22:59:24 +0000216 /**
217 * Process Trackback
218 *
219 * Opens a socket connection and passes the data to
Andrey Andreev426faa92012-04-03 19:03:58 +0300220 * the server. Returns TRUE on success, FALSE on failure
Derek Allard2067d1a2008-11-13 22:59:24 +0000221 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000222 * @param string
223 * @param string
224 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200225 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200226 public function process($url, $data)
Derek Allard2067d1a2008-11-13 22:59:24 +0000227 {
228 $target = parse_url($url);
Barry Mienydd671972010-10-04 16:33:58 +0200229
Derek Allard2067d1a2008-11-13 22:59:24 +0000230 // Open the socket
231 if ( ! $fp = @fsockopen($target['host'], 80))
232 {
233 $this->set_error('Invalid Connection: '.$url);
234 return FALSE;
235 }
236
237 // Build the path
Andrey Andreev426faa92012-04-03 19:03:58 +0300238 $ppath = isset($target['path']) ? $target['path'] : $url;
Barry Mienydd671972010-10-04 16:33:58 +0200239
Andrey Andreev426faa92012-04-03 19:03:58 +0300240 $path = empty($target['query']) ? $ppath : $ppath.'?'.$target['query'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000241
242 // Add the Trackback ID to the data string
243 if ($id = $this->get_id($url))
244 {
Andrey Andreev426faa92012-04-03 19:03:58 +0300245 $data = 'tb_id='.$id.'&'.$data;
Derek Allard2067d1a2008-11-13 22:59:24 +0000246 }
247
248 // Transfer the data
Andrey Andreev426faa92012-04-03 19:03:58 +0300249 fputs($fp, 'POST '.$path." HTTP/1.0\r\n");
250 fputs($fp, 'Host: '.$target['host']."\r\n");
251 fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n");
252 fputs($fp, 'Content-length: '.strlen($data)."\r\n");
253 fputs($fp, "Connection: close\r\n\r\n");
254 fputs($fp, $data);
Derek Allard2067d1a2008-11-13 22:59:24 +0000255
256 // Was it successful?
Barry Mienydd671972010-10-04 16:33:58 +0200257
Andrey Andreev426faa92012-04-03 19:03:58 +0300258 $this->response = '';
Pascal Kriete14287f32011-02-14 13:39:34 -0500259 while ( ! feof($fp))
Derek Allard2067d1a2008-11-13 22:59:24 +0000260 {
261 $this->response .= fgets($fp, 128);
262 }
263 @fclose($fp);
Barry Mienydd671972010-10-04 16:33:58 +0200264
Andrey Andreevd9cfa7b2011-12-25 16:32:59 +0200265 if (stripos($this->response, '<error>0</error>') === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000266 {
Andrey Andreev426faa92012-04-03 19:03:58 +0300267 $message = preg_match('/<message>(.*?)<\/message>/is', $this->response, $match) ? trim($match[1]) : 'An unknown error was encountered';
Derek Allard2067d1a2008-11-13 22:59:24 +0000268 $this->set_error($message);
269 return FALSE;
270 }
271
272 return TRUE;
273 }
Barry Mienydd671972010-10-04 16:33:58 +0200274
Derek Allard2067d1a2008-11-13 22:59:24 +0000275 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200276
Derek Allard2067d1a2008-11-13 22:59:24 +0000277 /**
278 * Extract Trackback URLs
279 *
280 * This function lets multiple trackbacks be sent.
281 * It takes a string of URLs (separated by comma or
282 * space) and puts each URL into an array
283 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000284 * @param string
285 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200286 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200287 public function extract_urls($urls)
Barry Mienydd671972010-10-04 16:33:58 +0200288 {
Andrey Andreev426faa92012-04-03 19:03:58 +0300289 // Remove the pesky white space and replace with a comma, then replace doubles.
290 $urls = str_replace(',,', ',', preg_replace('/\s*(\S+)\s*/', '\\1,', $urls));
Barry Mienydd671972010-10-04 16:33:58 +0200291
Derek Allard2067d1a2008-11-13 22:59:24 +0000292 // Remove any comma that might be at the end
Andrey Andreevd9cfa7b2011-12-25 16:32:59 +0200293 if (substr($urls, -1) === ',')
Derek Allard2067d1a2008-11-13 22:59:24 +0000294 {
295 $urls = substr($urls, 0, -1);
296 }
Barry Mienydd671972010-10-04 16:33:58 +0200297
Andrey Andreev426faa92012-04-03 19:03:58 +0300298 // Break into an array via commas and remove duplicates
299 $urls = array_unique(preg_split('/[,]/', $urls));
Barry Mienydd671972010-10-04 16:33:58 +0200300
Derek Allard2067d1a2008-11-13 22:59:24 +0000301 array_walk($urls, array($this, 'validate_url'));
Barry Mienydd671972010-10-04 16:33:58 +0200302
Derek Allard2067d1a2008-11-13 22:59:24 +0000303 return $urls;
304 }
Barry Mienydd671972010-10-04 16:33:58 +0200305
Derek Allard2067d1a2008-11-13 22:59:24 +0000306 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200307
Derek Allard2067d1a2008-11-13 22:59:24 +0000308 /**
309 * Validate URL
310 *
311 * Simply adds "http://" if missing
312 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000313 * @param string
Andrey Andreev426faa92012-04-03 19:03:58 +0300314 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200315 */
Andrey Andreev426faa92012-04-03 19:03:58 +0300316 public function validate_url(&$url)
Derek Allard2067d1a2008-11-13 22:59:24 +0000317 {
318 $url = trim($url);
319
Andrey Andreevd9cfa7b2011-12-25 16:32:59 +0200320 if (strpos($url, 'http') !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000321 {
Andrey Andreevd9cfa7b2011-12-25 16:32:59 +0200322 $url = 'http://'.$url;
Derek Allard2067d1a2008-11-13 22:59:24 +0000323 }
324 }
Barry Mienydd671972010-10-04 16:33:58 +0200325
Derek Allard2067d1a2008-11-13 22:59:24 +0000326 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200327
Derek Allard2067d1a2008-11-13 22:59:24 +0000328 /**
329 * Find the Trackback URL's ID
330 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000331 * @param string
332 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200333 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200334 public function get_id($url)
Barry Mienydd671972010-10-04 16:33:58 +0200335 {
Andrey Andreev426faa92012-04-03 19:03:58 +0300336 $tb_id = '';
Barry Mienydd671972010-10-04 16:33:58 +0200337
Robin Sowell76b369e2010-03-19 11:15:28 -0400338 if (strpos($url, '?') !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000339 {
340 $tb_array = explode('/', $url);
Derek Jones37f4b9c2011-07-01 17:56:50 -0500341 $tb_end = $tb_array[count($tb_array)-1];
Barry Mienydd671972010-10-04 16:33:58 +0200342
Derek Allard2067d1a2008-11-13 22:59:24 +0000343 if ( ! is_numeric($tb_end))
344 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500345 $tb_end = $tb_array[count($tb_array)-2];
Derek Allard2067d1a2008-11-13 22:59:24 +0000346 }
Barry Mienydd671972010-10-04 16:33:58 +0200347
Derek Allard2067d1a2008-11-13 22:59:24 +0000348 $tb_array = explode('=', $tb_end);
349 $tb_id = $tb_array[count($tb_array)-1];
350 }
351 else
352 {
Derek Jones1322ec52009-03-11 17:01:14 +0000353 $url = rtrim($url, '/');
Barry Mienydd671972010-10-04 16:33:58 +0200354
Derek Allard2067d1a2008-11-13 22:59:24 +0000355 $tb_array = explode('/', $url);
356 $tb_id = $tb_array[count($tb_array)-1];
Barry Mienydd671972010-10-04 16:33:58 +0200357
Derek Allard2067d1a2008-11-13 22:59:24 +0000358 if ( ! is_numeric($tb_id))
359 {
Andrey Andreev426faa92012-04-03 19:03:58 +0300360 $tb_id = $tb_array[count($tb_array)-2];
Derek Allard2067d1a2008-11-13 22:59:24 +0000361 }
Barry Mienydd671972010-10-04 16:33:58 +0200362 }
363
Andrey Andreev426faa92012-04-03 19:03:58 +0300364 return preg_match('/^[0-9]+$/', $tb_id) ? $tb_id : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000365 }
Barry Mienydd671972010-10-04 16:33:58 +0200366
Derek Allard2067d1a2008-11-13 22:59:24 +0000367 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200368
Derek Allard2067d1a2008-11-13 22:59:24 +0000369 /**
370 * Convert Reserved XML characters to Entities
371 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000372 * @param string
373 * @return string
374 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200375 public function convert_xml($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000376 {
377 $temp = '__TEMP_AMPERSANDS__';
Barry Mienydd671972010-10-04 16:33:58 +0200378
Andrey Andreev426faa92012-04-03 19:03:58 +0300379 $str = preg_replace(array('/&#(\d+);/', '/&(\w+);/'), $temp.'\\1;', $str);
Barry Mienydd671972010-10-04 16:33:58 +0200380
Andrey Andreev426faa92012-04-03 19:03:58 +0300381 $str = str_replace(array('&', '<', '>', '"', "'", '-'),
382 array('&amp;', '&lt;', '&gt;', '&quot;', '&#39;', '&#45;'),
383 $str);
Barry Mienydd671972010-10-04 16:33:58 +0200384
Andrey Andreev426faa92012-04-03 19:03:58 +0300385 return preg_replace(array('/'.$temp.'(\d+);/', '/'.$temp.'(\w+);/'), array('&#\\1;', '&\\1;'), $str);
Barry Mienydd671972010-10-04 16:33:58 +0200386 }
387
Derek Allard2067d1a2008-11-13 22:59:24 +0000388 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200389
Derek Allard2067d1a2008-11-13 22:59:24 +0000390 /**
391 * Character limiter
392 *
393 * Limits the string based on the character count. Will preserve complete words.
394 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000395 * @param string
Andrey Andreev426faa92012-04-03 19:03:58 +0300396 * @param int
Derek Allard2067d1a2008-11-13 22:59:24 +0000397 * @param string
398 * @return string
399 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200400 public function limit_characters($str, $n = 500, $end_char = '&#8230;')
Derek Allard2067d1a2008-11-13 22:59:24 +0000401 {
402 if (strlen($str) < $n)
403 {
404 return $str;
405 }
Barry Mienydd671972010-10-04 16:33:58 +0200406
Andrey Andreev426faa92012-04-03 19:03:58 +0300407 $str = preg_replace('/\s+/', ' ', str_replace(array("\r\n", "\r", "\n"), ' ', $str));
Barry Mienydd671972010-10-04 16:33:58 +0200408
Derek Allard2067d1a2008-11-13 22:59:24 +0000409 if (strlen($str) <= $n)
410 {
411 return $str;
412 }
Barry Mienydd671972010-10-04 16:33:58 +0200413
Andrey Andreevd9cfa7b2011-12-25 16:32:59 +0200414 $out = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000415 foreach (explode(' ', trim($str)) as $val)
416 {
Barry Mienydd671972010-10-04 16:33:58 +0200417 $out .= $val.' ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000418 if (strlen($out) >= $n)
419 {
Andrey Andreevd9cfa7b2011-12-25 16:32:59 +0200420 return rtrim($out).$end_char;
Barry Mienydd671972010-10-04 16:33:58 +0200421 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000422 }
423 }
Barry Mienydd671972010-10-04 16:33:58 +0200424
Derek Allard2067d1a2008-11-13 22:59:24 +0000425 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200426
Derek Allard2067d1a2008-11-13 22:59:24 +0000427 /**
428 * High ASCII to Entities
429 *
430 * Converts Hight ascii text and MS Word special chars
431 * to character entities
432 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000433 * @param string
434 * @return string
435 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200436 public function convert_ascii($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000437 {
Barry Mienydd671972010-10-04 16:33:58 +0200438 $count = 1;
439 $out = '';
440 $temp = array();
441
442 for ($i = 0, $s = strlen($str); $i < $s; $i++)
443 {
444 $ordinal = ord($str[$i]);
445
446 if ($ordinal < 128)
447 {
448 $out .= $str[$i];
449 }
450 else
451 {
Andrey Andreevd9cfa7b2011-12-25 16:32:59 +0200452 if (count($temp) === 0)
Barry Mienydd671972010-10-04 16:33:58 +0200453 {
454 $count = ($ordinal < 224) ? 2 : 3;
455 }
456
457 $temp[] = $ordinal;
458
Andrey Andreevd9cfa7b2011-12-25 16:32:59 +0200459 if (count($temp) === $count)
Barry Mienydd671972010-10-04 16:33:58 +0200460 {
Andrey Andreev426faa92012-04-03 19:03:58 +0300461 $number = ($count === 3)
462 ? (($temp[0] % 16) * 4096) + (($temp[1] % 64) * 64) + ($temp[2] % 64)
463 : (($temp[0] % 32) * 64) + ($temp[1] % 64);
Barry Mienydd671972010-10-04 16:33:58 +0200464
465 $out .= '&#'.$number.';';
466 $count = 1;
467 $temp = array();
468 }
469 }
470 }
471
472 return $out;
Derek Allard2067d1a2008-11-13 22:59:24 +0000473 }
Barry Mienydd671972010-10-04 16:33:58 +0200474
Derek Allard2067d1a2008-11-13 22:59:24 +0000475 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200476
Derek Allard2067d1a2008-11-13 22:59:24 +0000477 /**
478 * Set error message
479 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000480 * @param string
481 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200482 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200483 public function set_error($msg)
Derek Allard2067d1a2008-11-13 22:59:24 +0000484 {
485 log_message('error', $msg);
486 $this->error_msg[] = $msg;
487 }
Barry Mienydd671972010-10-04 16:33:58 +0200488
Derek Allard2067d1a2008-11-13 22:59:24 +0000489 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200490
Derek Allard2067d1a2008-11-13 22:59:24 +0000491 /**
492 * Show error messages
493 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000494 * @param string
495 * @param string
496 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200497 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200498 public function display_errors($open = '<p>', $close = '</p>')
Barry Mienydd671972010-10-04 16:33:58 +0200499 {
Andrey Andreev426faa92012-04-03 19:03:58 +0300500 return (count($this->error_msg) > 0) ? $open.implode($close.$open, $this->error_msg).$close : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000501 }
502
503}
Derek Allard2067d1a2008-11-13 22:59:24 +0000504
505/* End of file Trackback.php */
Andrey Andreev426faa92012-04-03 19:03:58 +0300506/* Location: ./system/libraries/Trackback.php */