blob: 9fa4a8edb6adb014e713f972be4c1398d7a7f432 [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 Andreev3a459572011-12-21 11:23:11 +020047 public $charset = 'UTF-8';
Andrey Andreev0fa95bd2012-11-01 23:33:14 +020048
49 /**
50 * Trackback data
51 *
52 * @var array
53 */
54 public $data = array('url' => '', 'title' => '', 'excerpt' => '', 'blog_name' => '', 'charset' => '');
55
56 /**
57 * Convert ASCII flag
58 *
59 * Whether to convert high-ASCII and MS Word
60 * characters to HTML entities.
61 *
62 * @var bool
63 */
Andrey Andreev3a459572011-12-21 11:23:11 +020064 public $convert_ascii = TRUE;
Andrey Andreev0fa95bd2012-11-01 23:33:14 +020065
66 /**
67 * Response
68 *
69 * @var string
70 */
71 public $response = '';
72
73 /**
74 * Error messages list
75 *
76 * @var string[]
77 */
78 public $error_msg = array();
79
80 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +000081
Andrey Andreev5fd3ae82012-10-24 14:55:35 +030082 /**
83 * Constructor
84 *
85 * @return void
86 */
Greg Akera9263282010-11-10 15:26:43 -060087 public function __construct()
Derek Allard2067d1a2008-11-13 22:59:24 +000088 {
Andrey Andreev426faa92012-04-03 19:03:58 +030089 log_message('debug', 'Trackback Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +000090 }
Barry Mienydd671972010-10-04 16:33:58 +020091
Derek Allard2067d1a2008-11-13 22:59:24 +000092 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +020093
Derek Allard2067d1a2008-11-13 22:59:24 +000094 /**
95 * Send Trackback
96 *
Derek Allard2067d1a2008-11-13 22:59:24 +000097 * @param array
98 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +020099 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200100 public function send($tb_data)
Barry Mienydd671972010-10-04 16:33:58 +0200101 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000102 if ( ! is_array($tb_data))
103 {
104 $this->set_error('The send() method must be passed an array');
105 return FALSE;
106 }
Barry Mienydd671972010-10-04 16:33:58 +0200107
Derek Allard2067d1a2008-11-13 22:59:24 +0000108 // Pre-process the Trackback Data
109 foreach (array('url', 'title', 'excerpt', 'blog_name', 'ping_url') as $item)
110 {
111 if ( ! isset($tb_data[$item]))
112 {
113 $this->set_error('Required item missing: '.$item);
114 return FALSE;
115 }
Barry Mienydd671972010-10-04 16:33:58 +0200116
Derek Allard2067d1a2008-11-13 22:59:24 +0000117 switch ($item)
118 {
119 case 'ping_url' : $$item = $this->extract_urls($tb_data[$item]);
120 break;
121 case 'excerpt' : $$item = $this->limit_characters($this->convert_xml(strip_tags(stripslashes($tb_data[$item]))));
122 break;
Barry Mienydd671972010-10-04 16:33:58 +0200123 case 'url' : $$item = str_replace('&#45;', '-', $this->convert_xml(strip_tags(stripslashes($tb_data[$item]))));
Derek Allard2067d1a2008-11-13 22:59:24 +0000124 break;
125 default : $$item = $this->convert_xml(strip_tags(stripslashes($tb_data[$item])));
126 break;
127 }
128
129 // Convert High ASCII Characters
Alex Bilbied261b1e2012-06-02 11:12:16 +0100130 if ($this->convert_ascii === TRUE && in_array($item, array('excerpt', 'title', 'blog_name')))
Derek Allard2067d1a2008-11-13 22:59:24 +0000131 {
Andrey Andreevd9cfa7b2011-12-25 16:32:59 +0200132 $$item = $this->convert_ascii($$item);
Derek Allard2067d1a2008-11-13 22:59:24 +0000133 }
134 }
135
136 // Build the Trackback data string
Andrey Andreev426faa92012-04-03 19:03:58 +0300137 $charset = isset($tb_data['charset']) ? $tb_data['charset'] : $this->charset;
Barry Mienydd671972010-10-04 16:33:58 +0200138
Andrey Andreev426faa92012-04-03 19:03:58 +0300139 $data = 'url='.rawurlencode($url).'&title='.rawurlencode($title).'&blog_name='.rawurlencode($blog_name)
140 .'&excerpt='.rawurlencode($excerpt).'&charset='.rawurlencode($charset);
Barry Mienydd671972010-10-04 16:33:58 +0200141
Derek Allard2067d1a2008-11-13 22:59:24 +0000142 // Send Trackback(s)
143 $return = TRUE;
144 if (count($ping_url) > 0)
145 {
146 foreach ($ping_url as $url)
147 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100148 if ($this->process($url, $data) === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000149 {
150 $return = FALSE;
151 }
Barry Mienydd671972010-10-04 16:33:58 +0200152 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000153 }
154
155 return $return;
156 }
Barry Mienydd671972010-10-04 16:33:58 +0200157
Derek Allard2067d1a2008-11-13 22:59:24 +0000158 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200159
Derek Allard2067d1a2008-11-13 22:59:24 +0000160 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -0500161 * Receive Trackback Data
Derek Allard2067d1a2008-11-13 22:59:24 +0000162 *
163 * This function simply validates the incoming TB data.
Derek Jones5052e272010-03-02 22:53:38 -0600164 * It returns FALSE on failure and TRUE on success.
Derek Allard2067d1a2008-11-13 22:59:24 +0000165 * If the data is valid it is set to the $this->data array
166 * so that it can be inserted into a database.
167 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000168 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200169 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200170 public function receive()
Barry Mienydd671972010-10-04 16:33:58 +0200171 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000172 foreach (array('url', 'title', 'blog_name', 'excerpt') as $val)
173 {
Andrey Andreev5036c9c2012-06-04 15:34:56 +0300174 if (empty($_POST[$val]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000175 {
176 $this->set_error('The following required POST variable is missing: '.$val);
177 return FALSE;
178 }
Barry Mienydd671972010-10-04 16:33:58 +0200179
Andrey Andreev426faa92012-04-03 19:03:58 +0300180 $this->data['charset'] = isset($_POST['charset']) ? strtoupper(trim($_POST['charset'])) : 'auto';
Barry Mienydd671972010-10-04 16:33:58 +0200181
Alex Bilbied261b1e2012-06-02 11:12:16 +0100182 if ($val !== 'url' && MB_ENABLED === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000183 {
Andrey Andreevbe1496d2014-02-11 22:48:45 +0200184 if (MB_ENABLED === TRUE)
185 {
186 $_POST[$val] = mb_convert_encoding($_POST[$val], $this->charset, $this->data['charset']);
187 }
188 elseif (ICONV_ENABLED === TRUE)
189 {
190 $_POST[$val] = @iconv($this->data['charset'], $this->charset.'//IGNORE', $_POST[$val]);
191 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000192 }
Barry Mienydd671972010-10-04 16:33:58 +0200193
Alex Bilbied261b1e2012-06-02 11:12:16 +0100194 $_POST[$val] = ($val !== 'url') ? $this->convert_xml(strip_tags($_POST[$val])) : strip_tags($_POST[$val]);
Barry Mienydd671972010-10-04 16:33:58 +0200195
Alex Bilbied261b1e2012-06-02 11:12:16 +0100196 if ($val === 'excerpt')
Derek Allard2067d1a2008-11-13 22:59:24 +0000197 {
198 $_POST['excerpt'] = $this->limit_characters($_POST['excerpt']);
199 }
Barry Mienydd671972010-10-04 16:33:58 +0200200
Derek Allard2067d1a2008-11-13 22:59:24 +0000201 $this->data[$val] = $_POST[$val];
202 }
203
204 return TRUE;
Barry Mienydd671972010-10-04 16:33:58 +0200205 }
206
Derek Allard2067d1a2008-11-13 22:59:24 +0000207 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200208
Derek Allard2067d1a2008-11-13 22:59:24 +0000209 /**
210 * Send Trackback Error Message
211 *
Andrey Andreev426faa92012-04-03 19:03:58 +0300212 * Allows custom errors to be set. By default it
Derek Allard2067d1a2008-11-13 22:59:24 +0000213 * sends the "incomplete information" error, as that's
214 * the most common one.
215 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000216 * @param string
217 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200218 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200219 public function send_error($message = 'Incomplete Information')
Derek Allard2067d1a2008-11-13 22:59:24 +0000220 {
Daniel Hunsaker8626e932013-03-04 05:14:22 -0700221 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 +0000222 }
Barry Mienydd671972010-10-04 16:33:58 +0200223
Derek Allard2067d1a2008-11-13 22:59:24 +0000224 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200225
Derek Allard2067d1a2008-11-13 22:59:24 +0000226 /**
227 * Send Trackback Success Message
228 *
229 * This should be called when a trackback has been
230 * successfully received and inserted.
231 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000232 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200233 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200234 public function send_success()
Derek Allard2067d1a2008-11-13 22:59:24 +0000235 {
Daniel Hunsaker8626e932013-03-04 05:14:22 -0700236 exit('<?xml version="1.0" encoding="utf-8"?'.">\n<response>\n<error>0</error>\n</response>");
Derek Allard2067d1a2008-11-13 22:59:24 +0000237 }
Barry Mienydd671972010-10-04 16:33:58 +0200238
Derek Allard2067d1a2008-11-13 22:59:24 +0000239 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200240
Derek Allard2067d1a2008-11-13 22:59:24 +0000241 /**
242 * Fetch a particular item
243 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000244 * @param string
245 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200246 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200247 public function data($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000248 {
Andrey Andreev426faa92012-04-03 19:03:58 +0300249 return isset($this->data[$item]) ? $this->data[$item] : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000250 }
251
252 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200253
Derek Allard2067d1a2008-11-13 22:59:24 +0000254 /**
255 * Process Trackback
256 *
257 * Opens a socket connection and passes the data to
Andrey Andreev426faa92012-04-03 19:03:58 +0300258 * the server. Returns TRUE on success, FALSE on failure
Derek Allard2067d1a2008-11-13 22:59:24 +0000259 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000260 * @param string
261 * @param string
262 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200263 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200264 public function process($url, $data)
Derek Allard2067d1a2008-11-13 22:59:24 +0000265 {
266 $target = parse_url($url);
Barry Mienydd671972010-10-04 16:33:58 +0200267
Derek Allard2067d1a2008-11-13 22:59:24 +0000268 // Open the socket
269 if ( ! $fp = @fsockopen($target['host'], 80))
270 {
271 $this->set_error('Invalid Connection: '.$url);
272 return FALSE;
273 }
274
275 // Build the path
Andrey Andreev5b5401a2013-01-10 17:38:08 +0200276 $path = isset($target['path']) ? $target['path'] : $url;
277 empty($target['query']) OR $path .= '?'.$target['query'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000278
279 // Add the Trackback ID to the data string
280 if ($id = $this->get_id($url))
281 {
Andrey Andreev426faa92012-04-03 19:03:58 +0300282 $data = 'tb_id='.$id.'&'.$data;
Derek Allard2067d1a2008-11-13 22:59:24 +0000283 }
284
285 // Transfer the data
Andrey Andreev426faa92012-04-03 19:03:58 +0300286 fputs($fp, 'POST '.$path." HTTP/1.0\r\n");
287 fputs($fp, 'Host: '.$target['host']."\r\n");
288 fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n");
289 fputs($fp, 'Content-length: '.strlen($data)."\r\n");
290 fputs($fp, "Connection: close\r\n\r\n");
291 fputs($fp, $data);
Derek Allard2067d1a2008-11-13 22:59:24 +0000292
293 // Was it successful?
Barry Mienydd671972010-10-04 16:33:58 +0200294
Andrey Andreev426faa92012-04-03 19:03:58 +0300295 $this->response = '';
Pascal Kriete14287f32011-02-14 13:39:34 -0500296 while ( ! feof($fp))
Derek Allard2067d1a2008-11-13 22:59:24 +0000297 {
298 $this->response .= fgets($fp, 128);
299 }
300 @fclose($fp);
Barry Mienydd671972010-10-04 16:33:58 +0200301
Andrey Andreevd9cfa7b2011-12-25 16:32:59 +0200302 if (stripos($this->response, '<error>0</error>') === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000303 {
Andrey Andreev426faa92012-04-03 19:03:58 +0300304 $message = preg_match('/<message>(.*?)<\/message>/is', $this->response, $match) ? trim($match[1]) : 'An unknown error was encountered';
Derek Allard2067d1a2008-11-13 22:59:24 +0000305 $this->set_error($message);
306 return FALSE;
307 }
308
309 return TRUE;
310 }
Barry Mienydd671972010-10-04 16:33:58 +0200311
Derek Allard2067d1a2008-11-13 22:59:24 +0000312 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200313
Derek Allard2067d1a2008-11-13 22:59:24 +0000314 /**
315 * Extract Trackback URLs
316 *
317 * This function lets multiple trackbacks be sent.
318 * It takes a string of URLs (separated by comma or
319 * space) and puts each URL into an array
320 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000321 * @param string
322 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200323 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200324 public function extract_urls($urls)
Barry Mienydd671972010-10-04 16:33:58 +0200325 {
Andrey Andreev426faa92012-04-03 19:03:58 +0300326 // Remove the pesky white space and replace with a comma, then replace doubles.
327 $urls = str_replace(',,', ',', preg_replace('/\s*(\S+)\s*/', '\\1,', $urls));
Barry Mienydd671972010-10-04 16:33:58 +0200328
Derek Allard2067d1a2008-11-13 22:59:24 +0000329 // Remove any comma that might be at the end
Andrey Andreevd9cfa7b2011-12-25 16:32:59 +0200330 if (substr($urls, -1) === ',')
Derek Allard2067d1a2008-11-13 22:59:24 +0000331 {
332 $urls = substr($urls, 0, -1);
333 }
Barry Mienydd671972010-10-04 16:33:58 +0200334
Andrey Andreev426faa92012-04-03 19:03:58 +0300335 // Break into an array via commas and remove duplicates
336 $urls = array_unique(preg_split('/[,]/', $urls));
Barry Mienydd671972010-10-04 16:33:58 +0200337
Derek Allard2067d1a2008-11-13 22:59:24 +0000338 array_walk($urls, array($this, 'validate_url'));
Barry Mienydd671972010-10-04 16:33:58 +0200339
Derek Allard2067d1a2008-11-13 22:59:24 +0000340 return $urls;
341 }
Barry Mienydd671972010-10-04 16:33:58 +0200342
Derek Allard2067d1a2008-11-13 22:59:24 +0000343 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200344
Derek Allard2067d1a2008-11-13 22:59:24 +0000345 /**
346 * Validate URL
347 *
348 * Simply adds "http://" if missing
349 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000350 * @param string
Andrey Andreev426faa92012-04-03 19:03:58 +0300351 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200352 */
Andrey Andreev426faa92012-04-03 19:03:58 +0300353 public function validate_url(&$url)
Derek Allard2067d1a2008-11-13 22:59:24 +0000354 {
355 $url = trim($url);
356
Andrey Andreevd9cfa7b2011-12-25 16:32:59 +0200357 if (strpos($url, 'http') !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000358 {
Andrey Andreevd9cfa7b2011-12-25 16:32:59 +0200359 $url = 'http://'.$url;
Derek Allard2067d1a2008-11-13 22:59:24 +0000360 }
361 }
Barry Mienydd671972010-10-04 16:33:58 +0200362
Derek Allard2067d1a2008-11-13 22:59:24 +0000363 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200364
Derek Allard2067d1a2008-11-13 22:59:24 +0000365 /**
366 * Find the Trackback URL's ID
367 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000368 * @param string
369 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200370 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200371 public function get_id($url)
Barry Mienydd671972010-10-04 16:33:58 +0200372 {
Andrey Andreev426faa92012-04-03 19:03:58 +0300373 $tb_id = '';
Barry Mienydd671972010-10-04 16:33:58 +0200374
Robin Sowell76b369e2010-03-19 11:15:28 -0400375 if (strpos($url, '?') !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000376 {
377 $tb_array = explode('/', $url);
Derek Jones37f4b9c2011-07-01 17:56:50 -0500378 $tb_end = $tb_array[count($tb_array)-1];
Barry Mienydd671972010-10-04 16:33:58 +0200379
Derek Allard2067d1a2008-11-13 22:59:24 +0000380 if ( ! is_numeric($tb_end))
381 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500382 $tb_end = $tb_array[count($tb_array)-2];
Derek Allard2067d1a2008-11-13 22:59:24 +0000383 }
Barry Mienydd671972010-10-04 16:33:58 +0200384
Derek Allard2067d1a2008-11-13 22:59:24 +0000385 $tb_array = explode('=', $tb_end);
386 $tb_id = $tb_array[count($tb_array)-1];
387 }
388 else
389 {
Derek Jones1322ec52009-03-11 17:01:14 +0000390 $url = rtrim($url, '/');
Barry Mienydd671972010-10-04 16:33:58 +0200391
Derek Allard2067d1a2008-11-13 22:59:24 +0000392 $tb_array = explode('/', $url);
393 $tb_id = $tb_array[count($tb_array)-1];
Barry Mienydd671972010-10-04 16:33:58 +0200394
Derek Allard2067d1a2008-11-13 22:59:24 +0000395 if ( ! is_numeric($tb_id))
396 {
Andrey Andreev426faa92012-04-03 19:03:58 +0300397 $tb_id = $tb_array[count($tb_array)-2];
Derek Allard2067d1a2008-11-13 22:59:24 +0000398 }
Barry Mienydd671972010-10-04 16:33:58 +0200399 }
400
Andrey Andreev7a7ad782012-11-12 17:21:01 +0200401 return ctype_digit((string) $tb_id) ? $tb_id : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000402 }
Barry Mienydd671972010-10-04 16:33:58 +0200403
Derek Allard2067d1a2008-11-13 22:59:24 +0000404 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200405
Derek Allard2067d1a2008-11-13 22:59:24 +0000406 /**
407 * Convert Reserved XML characters to Entities
408 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000409 * @param string
410 * @return string
411 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200412 public function convert_xml($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000413 {
414 $temp = '__TEMP_AMPERSANDS__';
Barry Mienydd671972010-10-04 16:33:58 +0200415
Andrey Andreev426faa92012-04-03 19:03:58 +0300416 $str = preg_replace(array('/&#(\d+);/', '/&(\w+);/'), $temp.'\\1;', $str);
Barry Mienydd671972010-10-04 16:33:58 +0200417
Andrey Andreev426faa92012-04-03 19:03:58 +0300418 $str = str_replace(array('&', '<', '>', '"', "'", '-'),
419 array('&amp;', '&lt;', '&gt;', '&quot;', '&#39;', '&#45;'),
420 $str);
Barry Mienydd671972010-10-04 16:33:58 +0200421
Andrey Andreev426faa92012-04-03 19:03:58 +0300422 return preg_replace(array('/'.$temp.'(\d+);/', '/'.$temp.'(\w+);/'), array('&#\\1;', '&\\1;'), $str);
Barry Mienydd671972010-10-04 16:33:58 +0200423 }
424
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 * Character limiter
429 *
430 * Limits the string based on the character count. Will preserve complete words.
431 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000432 * @param string
Andrey Andreev426faa92012-04-03 19:03:58 +0300433 * @param int
Derek Allard2067d1a2008-11-13 22:59:24 +0000434 * @param string
435 * @return string
436 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200437 public function limit_characters($str, $n = 500, $end_char = '&#8230;')
Derek Allard2067d1a2008-11-13 22:59:24 +0000438 {
439 if (strlen($str) < $n)
440 {
441 return $str;
442 }
Barry Mienydd671972010-10-04 16:33:58 +0200443
Andrey Andreev426faa92012-04-03 19:03:58 +0300444 $str = preg_replace('/\s+/', ' ', str_replace(array("\r\n", "\r", "\n"), ' ', $str));
Barry Mienydd671972010-10-04 16:33:58 +0200445
Derek Allard2067d1a2008-11-13 22:59:24 +0000446 if (strlen($str) <= $n)
447 {
448 return $str;
449 }
Barry Mienydd671972010-10-04 16:33:58 +0200450
Andrey Andreevd9cfa7b2011-12-25 16:32:59 +0200451 $out = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000452 foreach (explode(' ', trim($str)) as $val)
453 {
Barry Mienydd671972010-10-04 16:33:58 +0200454 $out .= $val.' ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000455 if (strlen($out) >= $n)
456 {
Andrey Andreevd9cfa7b2011-12-25 16:32:59 +0200457 return rtrim($out).$end_char;
Barry Mienydd671972010-10-04 16:33:58 +0200458 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000459 }
460 }
Barry Mienydd671972010-10-04 16:33:58 +0200461
Derek Allard2067d1a2008-11-13 22:59:24 +0000462 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200463
Derek Allard2067d1a2008-11-13 22:59:24 +0000464 /**
465 * High ASCII to Entities
466 *
467 * Converts Hight ascii text and MS Word special chars
468 * to character entities
469 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000470 * @param string
471 * @return string
472 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200473 public function convert_ascii($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000474 {
Barry Mienydd671972010-10-04 16:33:58 +0200475 $count = 1;
476 $out = '';
477 $temp = array();
478
479 for ($i = 0, $s = strlen($str); $i < $s; $i++)
480 {
481 $ordinal = ord($str[$i]);
482
483 if ($ordinal < 128)
484 {
485 $out .= $str[$i];
486 }
487 else
488 {
Andrey Andreevd9cfa7b2011-12-25 16:32:59 +0200489 if (count($temp) === 0)
Barry Mienydd671972010-10-04 16:33:58 +0200490 {
491 $count = ($ordinal < 224) ? 2 : 3;
492 }
493
494 $temp[] = $ordinal;
495
Andrey Andreevd9cfa7b2011-12-25 16:32:59 +0200496 if (count($temp) === $count)
Barry Mienydd671972010-10-04 16:33:58 +0200497 {
Andrey Andreev426faa92012-04-03 19:03:58 +0300498 $number = ($count === 3)
499 ? (($temp[0] % 16) * 4096) + (($temp[1] % 64) * 64) + ($temp[2] % 64)
500 : (($temp[0] % 32) * 64) + ($temp[1] % 64);
Barry Mienydd671972010-10-04 16:33:58 +0200501
502 $out .= '&#'.$number.';';
503 $count = 1;
504 $temp = array();
505 }
506 }
507 }
508
509 return $out;
Derek Allard2067d1a2008-11-13 22:59:24 +0000510 }
Barry Mienydd671972010-10-04 16:33:58 +0200511
Derek Allard2067d1a2008-11-13 22:59:24 +0000512 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200513
Derek Allard2067d1a2008-11-13 22:59:24 +0000514 /**
515 * Set error message
516 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000517 * @param string
518 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200519 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200520 public function set_error($msg)
Derek Allard2067d1a2008-11-13 22:59:24 +0000521 {
522 log_message('error', $msg);
523 $this->error_msg[] = $msg;
524 }
Barry Mienydd671972010-10-04 16:33:58 +0200525
Derek Allard2067d1a2008-11-13 22:59:24 +0000526 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200527
Derek Allard2067d1a2008-11-13 22:59:24 +0000528 /**
529 * Show error messages
530 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000531 * @param string
532 * @param string
533 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200534 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200535 public function display_errors($open = '<p>', $close = '</p>')
Barry Mienydd671972010-10-04 16:33:58 +0200536 {
Andrey Andreev426faa92012-04-03 19:03:58 +0300537 return (count($this->error_msg) > 0) ? $open.implode($close.$open, $this->error_msg).$close : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000538 }
539
540}
Derek Allard2067d1a2008-11-13 22:59:24 +0000541
542/* End of file Trackback.php */
Andrey Andreev426faa92012-04-03 19:03:58 +0300543/* Location: ./system/libraries/Trackback.php */