blob: d3035034082e57962a24cf9b77b6a012603fa65a [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 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 {
184 $_POST[$val] = mb_convert_encoding($_POST[$val], $this->charset, $this->data['charset']);
185 }
Barry Mienydd671972010-10-04 16:33:58 +0200186
Alex Bilbied261b1e2012-06-02 11:12:16 +0100187 $_POST[$val] = ($val !== 'url') ? $this->convert_xml(strip_tags($_POST[$val])) : strip_tags($_POST[$val]);
Barry Mienydd671972010-10-04 16:33:58 +0200188
Alex Bilbied261b1e2012-06-02 11:12:16 +0100189 if ($val === 'excerpt')
Derek Allard2067d1a2008-11-13 22:59:24 +0000190 {
191 $_POST['excerpt'] = $this->limit_characters($_POST['excerpt']);
192 }
Barry Mienydd671972010-10-04 16:33:58 +0200193
Derek Allard2067d1a2008-11-13 22:59:24 +0000194 $this->data[$val] = $_POST[$val];
195 }
196
197 return TRUE;
Barry Mienydd671972010-10-04 16:33:58 +0200198 }
199
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 * Send Trackback Error Message
204 *
Andrey Andreev426faa92012-04-03 19:03:58 +0300205 * Allows custom errors to be set. By default it
Derek Allard2067d1a2008-11-13 22:59:24 +0000206 * sends the "incomplete information" error, as that's
207 * the most common one.
208 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000209 * @param string
210 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200211 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200212 public function send_error($message = 'Incomplete Information')
Derek Allard2067d1a2008-11-13 22:59:24 +0000213 {
Andrey Andreev426faa92012-04-03 19:03:58 +0300214 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 +0000215 exit;
216 }
Barry Mienydd671972010-10-04 16:33:58 +0200217
Derek Allard2067d1a2008-11-13 22:59:24 +0000218 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200219
Derek Allard2067d1a2008-11-13 22:59:24 +0000220 /**
221 * Send Trackback Success Message
222 *
223 * This should be called when a trackback has been
224 * successfully received and inserted.
225 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000226 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200227 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200228 public function send_success()
Derek Allard2067d1a2008-11-13 22:59:24 +0000229 {
Andrey Andreev426faa92012-04-03 19:03:58 +0300230 echo '<?xml version="1.0" encoding="utf-8"?'.">\n<response>\n<error>0</error>\n</response>";
Derek Allard2067d1a2008-11-13 22:59:24 +0000231 exit;
232 }
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 * Fetch a particular item
238 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000239 * @param string
240 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200241 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200242 public function data($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000243 {
Andrey Andreev426faa92012-04-03 19:03:58 +0300244 return isset($this->data[$item]) ? $this->data[$item] : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000245 }
246
247 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200248
Derek Allard2067d1a2008-11-13 22:59:24 +0000249 /**
250 * Process Trackback
251 *
252 * Opens a socket connection and passes the data to
Andrey Andreev426faa92012-04-03 19:03:58 +0300253 * the server. Returns TRUE on success, FALSE on failure
Derek Allard2067d1a2008-11-13 22:59:24 +0000254 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000255 * @param string
256 * @param string
257 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200258 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200259 public function process($url, $data)
Derek Allard2067d1a2008-11-13 22:59:24 +0000260 {
261 $target = parse_url($url);
Barry Mienydd671972010-10-04 16:33:58 +0200262
Derek Allard2067d1a2008-11-13 22:59:24 +0000263 // Open the socket
264 if ( ! $fp = @fsockopen($target['host'], 80))
265 {
266 $this->set_error('Invalid Connection: '.$url);
267 return FALSE;
268 }
269
270 // Build the path
Andrey Andreev426faa92012-04-03 19:03:58 +0300271 $ppath = isset($target['path']) ? $target['path'] : $url;
Barry Mienydd671972010-10-04 16:33:58 +0200272
Andrey Andreev426faa92012-04-03 19:03:58 +0300273 $path = empty($target['query']) ? $ppath : $ppath.'?'.$target['query'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000274
275 // Add the Trackback ID to the data string
276 if ($id = $this->get_id($url))
277 {
Andrey Andreev426faa92012-04-03 19:03:58 +0300278 $data = 'tb_id='.$id.'&'.$data;
Derek Allard2067d1a2008-11-13 22:59:24 +0000279 }
280
281 // Transfer the data
Andrey Andreev426faa92012-04-03 19:03:58 +0300282 fputs($fp, 'POST '.$path." HTTP/1.0\r\n");
283 fputs($fp, 'Host: '.$target['host']."\r\n");
284 fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n");
285 fputs($fp, 'Content-length: '.strlen($data)."\r\n");
286 fputs($fp, "Connection: close\r\n\r\n");
287 fputs($fp, $data);
Derek Allard2067d1a2008-11-13 22:59:24 +0000288
289 // Was it successful?
Barry Mienydd671972010-10-04 16:33:58 +0200290
Andrey Andreev426faa92012-04-03 19:03:58 +0300291 $this->response = '';
Pascal Kriete14287f32011-02-14 13:39:34 -0500292 while ( ! feof($fp))
Derek Allard2067d1a2008-11-13 22:59:24 +0000293 {
294 $this->response .= fgets($fp, 128);
295 }
296 @fclose($fp);
Barry Mienydd671972010-10-04 16:33:58 +0200297
Andrey Andreevd9cfa7b2011-12-25 16:32:59 +0200298 if (stripos($this->response, '<error>0</error>') === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000299 {
Andrey Andreev426faa92012-04-03 19:03:58 +0300300 $message = preg_match('/<message>(.*?)<\/message>/is', $this->response, $match) ? trim($match[1]) : 'An unknown error was encountered';
Derek Allard2067d1a2008-11-13 22:59:24 +0000301 $this->set_error($message);
302 return FALSE;
303 }
304
305 return TRUE;
306 }
Barry Mienydd671972010-10-04 16:33:58 +0200307
Derek Allard2067d1a2008-11-13 22:59:24 +0000308 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200309
Derek Allard2067d1a2008-11-13 22:59:24 +0000310 /**
311 * Extract Trackback URLs
312 *
313 * This function lets multiple trackbacks be sent.
314 * It takes a string of URLs (separated by comma or
315 * space) and puts each URL into an array
316 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000317 * @param string
318 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200319 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200320 public function extract_urls($urls)
Barry Mienydd671972010-10-04 16:33:58 +0200321 {
Andrey Andreev426faa92012-04-03 19:03:58 +0300322 // Remove the pesky white space and replace with a comma, then replace doubles.
323 $urls = str_replace(',,', ',', preg_replace('/\s*(\S+)\s*/', '\\1,', $urls));
Barry Mienydd671972010-10-04 16:33:58 +0200324
Derek Allard2067d1a2008-11-13 22:59:24 +0000325 // Remove any comma that might be at the end
Andrey Andreevd9cfa7b2011-12-25 16:32:59 +0200326 if (substr($urls, -1) === ',')
Derek Allard2067d1a2008-11-13 22:59:24 +0000327 {
328 $urls = substr($urls, 0, -1);
329 }
Barry Mienydd671972010-10-04 16:33:58 +0200330
Andrey Andreev426faa92012-04-03 19:03:58 +0300331 // Break into an array via commas and remove duplicates
332 $urls = array_unique(preg_split('/[,]/', $urls));
Barry Mienydd671972010-10-04 16:33:58 +0200333
Derek Allard2067d1a2008-11-13 22:59:24 +0000334 array_walk($urls, array($this, 'validate_url'));
Barry Mienydd671972010-10-04 16:33:58 +0200335
Derek Allard2067d1a2008-11-13 22:59:24 +0000336 return $urls;
337 }
Barry Mienydd671972010-10-04 16:33:58 +0200338
Derek Allard2067d1a2008-11-13 22:59:24 +0000339 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200340
Derek Allard2067d1a2008-11-13 22:59:24 +0000341 /**
342 * Validate URL
343 *
344 * Simply adds "http://" if missing
345 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000346 * @param string
Andrey Andreev426faa92012-04-03 19:03:58 +0300347 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200348 */
Andrey Andreev426faa92012-04-03 19:03:58 +0300349 public function validate_url(&$url)
Derek Allard2067d1a2008-11-13 22:59:24 +0000350 {
351 $url = trim($url);
352
Andrey Andreevd9cfa7b2011-12-25 16:32:59 +0200353 if (strpos($url, 'http') !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000354 {
Andrey Andreevd9cfa7b2011-12-25 16:32:59 +0200355 $url = 'http://'.$url;
Derek Allard2067d1a2008-11-13 22:59:24 +0000356 }
357 }
Barry Mienydd671972010-10-04 16:33:58 +0200358
Derek Allard2067d1a2008-11-13 22:59:24 +0000359 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200360
Derek Allard2067d1a2008-11-13 22:59:24 +0000361 /**
362 * Find the Trackback URL's ID
363 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000364 * @param string
365 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200366 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200367 public function get_id($url)
Barry Mienydd671972010-10-04 16:33:58 +0200368 {
Andrey Andreev426faa92012-04-03 19:03:58 +0300369 $tb_id = '';
Barry Mienydd671972010-10-04 16:33:58 +0200370
Robin Sowell76b369e2010-03-19 11:15:28 -0400371 if (strpos($url, '?') !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000372 {
373 $tb_array = explode('/', $url);
Derek Jones37f4b9c2011-07-01 17:56:50 -0500374 $tb_end = $tb_array[count($tb_array)-1];
Barry Mienydd671972010-10-04 16:33:58 +0200375
Derek Allard2067d1a2008-11-13 22:59:24 +0000376 if ( ! is_numeric($tb_end))
377 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500378 $tb_end = $tb_array[count($tb_array)-2];
Derek Allard2067d1a2008-11-13 22:59:24 +0000379 }
Barry Mienydd671972010-10-04 16:33:58 +0200380
Derek Allard2067d1a2008-11-13 22:59:24 +0000381 $tb_array = explode('=', $tb_end);
382 $tb_id = $tb_array[count($tb_array)-1];
383 }
384 else
385 {
Derek Jones1322ec52009-03-11 17:01:14 +0000386 $url = rtrim($url, '/');
Barry Mienydd671972010-10-04 16:33:58 +0200387
Derek Allard2067d1a2008-11-13 22:59:24 +0000388 $tb_array = explode('/', $url);
389 $tb_id = $tb_array[count($tb_array)-1];
Barry Mienydd671972010-10-04 16:33:58 +0200390
Derek Allard2067d1a2008-11-13 22:59:24 +0000391 if ( ! is_numeric($tb_id))
392 {
Andrey Andreev426faa92012-04-03 19:03:58 +0300393 $tb_id = $tb_array[count($tb_array)-2];
Derek Allard2067d1a2008-11-13 22:59:24 +0000394 }
Barry Mienydd671972010-10-04 16:33:58 +0200395 }
396
Andrey Andreev7a7ad782012-11-12 17:21:01 +0200397 return ctype_digit((string) $tb_id) ? $tb_id : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000398 }
Barry Mienydd671972010-10-04 16:33:58 +0200399
Derek Allard2067d1a2008-11-13 22:59:24 +0000400 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200401
Derek Allard2067d1a2008-11-13 22:59:24 +0000402 /**
403 * Convert Reserved XML characters to Entities
404 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000405 * @param string
406 * @return string
407 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200408 public function convert_xml($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000409 {
410 $temp = '__TEMP_AMPERSANDS__';
Barry Mienydd671972010-10-04 16:33:58 +0200411
Andrey Andreev426faa92012-04-03 19:03:58 +0300412 $str = preg_replace(array('/&#(\d+);/', '/&(\w+);/'), $temp.'\\1;', $str);
Barry Mienydd671972010-10-04 16:33:58 +0200413
Andrey Andreev426faa92012-04-03 19:03:58 +0300414 $str = str_replace(array('&', '<', '>', '"', "'", '-'),
415 array('&amp;', '&lt;', '&gt;', '&quot;', '&#39;', '&#45;'),
416 $str);
Barry Mienydd671972010-10-04 16:33:58 +0200417
Andrey Andreev426faa92012-04-03 19:03:58 +0300418 return preg_replace(array('/'.$temp.'(\d+);/', '/'.$temp.'(\w+);/'), array('&#\\1;', '&\\1;'), $str);
Barry Mienydd671972010-10-04 16:33:58 +0200419 }
420
Derek Allard2067d1a2008-11-13 22:59:24 +0000421 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200422
Derek Allard2067d1a2008-11-13 22:59:24 +0000423 /**
424 * Character limiter
425 *
426 * Limits the string based on the character count. Will preserve complete words.
427 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000428 * @param string
Andrey Andreev426faa92012-04-03 19:03:58 +0300429 * @param int
Derek Allard2067d1a2008-11-13 22:59:24 +0000430 * @param string
431 * @return string
432 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200433 public function limit_characters($str, $n = 500, $end_char = '&#8230;')
Derek Allard2067d1a2008-11-13 22:59:24 +0000434 {
435 if (strlen($str) < $n)
436 {
437 return $str;
438 }
Barry Mienydd671972010-10-04 16:33:58 +0200439
Andrey Andreev426faa92012-04-03 19:03:58 +0300440 $str = preg_replace('/\s+/', ' ', str_replace(array("\r\n", "\r", "\n"), ' ', $str));
Barry Mienydd671972010-10-04 16:33:58 +0200441
Derek Allard2067d1a2008-11-13 22:59:24 +0000442 if (strlen($str) <= $n)
443 {
444 return $str;
445 }
Barry Mienydd671972010-10-04 16:33:58 +0200446
Andrey Andreevd9cfa7b2011-12-25 16:32:59 +0200447 $out = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000448 foreach (explode(' ', trim($str)) as $val)
449 {
Barry Mienydd671972010-10-04 16:33:58 +0200450 $out .= $val.' ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000451 if (strlen($out) >= $n)
452 {
Andrey Andreevd9cfa7b2011-12-25 16:32:59 +0200453 return rtrim($out).$end_char;
Barry Mienydd671972010-10-04 16:33:58 +0200454 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000455 }
456 }
Barry Mienydd671972010-10-04 16:33:58 +0200457
Derek Allard2067d1a2008-11-13 22:59:24 +0000458 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200459
Derek Allard2067d1a2008-11-13 22:59:24 +0000460 /**
461 * High ASCII to Entities
462 *
463 * Converts Hight ascii text and MS Word special chars
464 * to character entities
465 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000466 * @param string
467 * @return string
468 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200469 public function convert_ascii($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000470 {
Barry Mienydd671972010-10-04 16:33:58 +0200471 $count = 1;
472 $out = '';
473 $temp = array();
474
475 for ($i = 0, $s = strlen($str); $i < $s; $i++)
476 {
477 $ordinal = ord($str[$i]);
478
479 if ($ordinal < 128)
480 {
481 $out .= $str[$i];
482 }
483 else
484 {
Andrey Andreevd9cfa7b2011-12-25 16:32:59 +0200485 if (count($temp) === 0)
Barry Mienydd671972010-10-04 16:33:58 +0200486 {
487 $count = ($ordinal < 224) ? 2 : 3;
488 }
489
490 $temp[] = $ordinal;
491
Andrey Andreevd9cfa7b2011-12-25 16:32:59 +0200492 if (count($temp) === $count)
Barry Mienydd671972010-10-04 16:33:58 +0200493 {
Andrey Andreev426faa92012-04-03 19:03:58 +0300494 $number = ($count === 3)
495 ? (($temp[0] % 16) * 4096) + (($temp[1] % 64) * 64) + ($temp[2] % 64)
496 : (($temp[0] % 32) * 64) + ($temp[1] % 64);
Barry Mienydd671972010-10-04 16:33:58 +0200497
498 $out .= '&#'.$number.';';
499 $count = 1;
500 $temp = array();
501 }
502 }
503 }
504
505 return $out;
Derek Allard2067d1a2008-11-13 22:59:24 +0000506 }
Barry Mienydd671972010-10-04 16:33:58 +0200507
Derek Allard2067d1a2008-11-13 22:59:24 +0000508 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200509
Derek Allard2067d1a2008-11-13 22:59:24 +0000510 /**
511 * Set error message
512 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000513 * @param string
514 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200515 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200516 public function set_error($msg)
Derek Allard2067d1a2008-11-13 22:59:24 +0000517 {
518 log_message('error', $msg);
519 $this->error_msg[] = $msg;
520 }
Barry Mienydd671972010-10-04 16:33:58 +0200521
Derek Allard2067d1a2008-11-13 22:59:24 +0000522 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200523
Derek Allard2067d1a2008-11-13 22:59:24 +0000524 /**
525 * Show error messages
526 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000527 * @param string
528 * @param string
529 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200530 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200531 public function display_errors($open = '<p>', $close = '</p>')
Barry Mienydd671972010-10-04 16:33:58 +0200532 {
Andrey Andreev426faa92012-04-03 19:03:58 +0300533 return (count($this->error_msg) > 0) ? $open.implode($close.$open, $this->error_msg).$close : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000534 }
535
536}
Derek Allard2067d1a2008-11-13 22:59:24 +0000537
538/* End of file Trackback.php */
Andrey Andreev426faa92012-04-03 19:03:58 +0300539/* Location: ./system/libraries/Trackback.php */