blob: 79a009133866a4c5591e502ad0a03fa2559922c6 [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 *
Greg Aker741de1c2010-11-10 14:52:57 -06005 * An open source application development framework for PHP 5.1.6 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
28// ------------------------------------------------------------------------
29
30/**
31 * Trackback Class
32 *
33 * Trackback Sending/Receiving Class
34 *
35 * @package CodeIgniter
36 * @subpackage Libraries
37 * @category Trackbacks
Derek Jonesf4a4bd82011-10-20 12:18:42 -050038 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000039 * @link http://codeigniter.com/user_guide/libraries/trackback.html
40 */
41class CI_Trackback {
Barry Mienydd671972010-10-04 16:33:58 +020042
Andrey Andreev3a459572011-12-21 11:23:11 +020043 public $time_format = 'local';
44 public $charset = 'UTF-8';
45 public $data = array('url' => '', 'title' => '', 'excerpt' => '', 'blog_name' => '', 'charset' => '');
46 public $convert_ascii = TRUE;
47 public $response = '';
48 public $error_msg = array();
Derek Allard2067d1a2008-11-13 22:59:24 +000049
Greg Akera9263282010-11-10 15:26:43 -060050 public function __construct()
Derek Allard2067d1a2008-11-13 22:59:24 +000051 {
52 log_message('debug', "Trackback Class Initialized");
53 }
Barry Mienydd671972010-10-04 16:33:58 +020054
Derek Allard2067d1a2008-11-13 22:59:24 +000055 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +020056
Derek Allard2067d1a2008-11-13 22:59:24 +000057 /**
58 * Send Trackback
59 *
Derek Allard2067d1a2008-11-13 22:59:24 +000060 * @param array
61 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +020062 */
Andrey Andreev4eea9892011-12-19 12:05:41 +020063 public function send($tb_data)
Barry Mienydd671972010-10-04 16:33:58 +020064 {
Derek Allard2067d1a2008-11-13 22:59:24 +000065 if ( ! is_array($tb_data))
66 {
67 $this->set_error('The send() method must be passed an array');
68 return FALSE;
69 }
Barry Mienydd671972010-10-04 16:33:58 +020070
Derek Allard2067d1a2008-11-13 22:59:24 +000071 // Pre-process the Trackback Data
72 foreach (array('url', 'title', 'excerpt', 'blog_name', 'ping_url') as $item)
73 {
74 if ( ! isset($tb_data[$item]))
75 {
76 $this->set_error('Required item missing: '.$item);
77 return FALSE;
78 }
Barry Mienydd671972010-10-04 16:33:58 +020079
Derek Allard2067d1a2008-11-13 22:59:24 +000080 switch ($item)
81 {
82 case 'ping_url' : $$item = $this->extract_urls($tb_data[$item]);
83 break;
84 case 'excerpt' : $$item = $this->limit_characters($this->convert_xml(strip_tags(stripslashes($tb_data[$item]))));
85 break;
Barry Mienydd671972010-10-04 16:33:58 +020086 case 'url' : $$item = str_replace('&#45;', '-', $this->convert_xml(strip_tags(stripslashes($tb_data[$item]))));
Derek Allard2067d1a2008-11-13 22:59:24 +000087 break;
88 default : $$item = $this->convert_xml(strip_tags(stripslashes($tb_data[$item])));
89 break;
90 }
91
92 // Convert High ASCII Characters
Andrey Andreevd9cfa7b2011-12-25 16:32:59 +020093 if ($this->convert_ascii == TRUE && in_array($item, array('excerpt', 'title', 'blog_name')))
Derek Allard2067d1a2008-11-13 22:59:24 +000094 {
Andrey Andreevd9cfa7b2011-12-25 16:32:59 +020095 $$item = $this->convert_ascii($$item);
Derek Allard2067d1a2008-11-13 22:59:24 +000096 }
97 }
98
99 // Build the Trackback data string
100 $charset = ( ! isset($tb_data['charset'])) ? $this->charset : $tb_data['charset'];
Barry Mienydd671972010-10-04 16:33:58 +0200101
Derek Allard2067d1a2008-11-13 22:59:24 +0000102 $data = "url=".rawurlencode($url)."&title=".rawurlencode($title)."&blog_name=".rawurlencode($blog_name)."&excerpt=".rawurlencode($excerpt)."&charset=".rawurlencode($charset);
Barry Mienydd671972010-10-04 16:33:58 +0200103
Derek Allard2067d1a2008-11-13 22:59:24 +0000104 // Send Trackback(s)
105 $return = TRUE;
106 if (count($ping_url) > 0)
107 {
108 foreach ($ping_url as $url)
109 {
110 if ($this->process($url, $data) == FALSE)
111 {
112 $return = FALSE;
113 }
Barry Mienydd671972010-10-04 16:33:58 +0200114 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000115 }
116
117 return $return;
118 }
Barry Mienydd671972010-10-04 16:33:58 +0200119
Derek Allard2067d1a2008-11-13 22:59:24 +0000120 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200121
Derek Allard2067d1a2008-11-13 22:59:24 +0000122 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -0500123 * Receive Trackback Data
Derek Allard2067d1a2008-11-13 22:59:24 +0000124 *
125 * This function simply validates the incoming TB data.
Derek Jones5052e272010-03-02 22:53:38 -0600126 * It returns FALSE on failure and TRUE on success.
Derek Allard2067d1a2008-11-13 22:59:24 +0000127 * If the data is valid it is set to the $this->data array
128 * so that it can be inserted into a database.
129 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000130 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200131 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200132 public function receive()
Barry Mienydd671972010-10-04 16:33:58 +0200133 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000134 foreach (array('url', 'title', 'blog_name', 'excerpt') as $val)
135 {
136 if ( ! isset($_POST[$val]) OR $_POST[$val] == '')
137 {
138 $this->set_error('The following required POST variable is missing: '.$val);
139 return FALSE;
140 }
Barry Mienydd671972010-10-04 16:33:58 +0200141
Derek Allard2067d1a2008-11-13 22:59:24 +0000142 $this->data['charset'] = ( ! isset($_POST['charset'])) ? 'auto' : strtoupper(trim($_POST['charset']));
Barry Mienydd671972010-10-04 16:33:58 +0200143
Derek Allard2067d1a2008-11-13 22:59:24 +0000144 if ($val != 'url' && function_exists('mb_convert_encoding'))
145 {
146 $_POST[$val] = mb_convert_encoding($_POST[$val], $this->charset, $this->data['charset']);
147 }
Barry Mienydd671972010-10-04 16:33:58 +0200148
Derek Allard2067d1a2008-11-13 22:59:24 +0000149 $_POST[$val] = ($val != 'url') ? $this->convert_xml(strip_tags($_POST[$val])) : strip_tags($_POST[$val]);
Barry Mienydd671972010-10-04 16:33:58 +0200150
Derek Allard2067d1a2008-11-13 22:59:24 +0000151 if ($val == 'excerpt')
152 {
153 $_POST['excerpt'] = $this->limit_characters($_POST['excerpt']);
154 }
Barry Mienydd671972010-10-04 16:33:58 +0200155
Derek Allard2067d1a2008-11-13 22:59:24 +0000156 $this->data[$val] = $_POST[$val];
157 }
158
159 return TRUE;
Barry Mienydd671972010-10-04 16:33:58 +0200160 }
161
Derek Allard2067d1a2008-11-13 22:59:24 +0000162 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200163
Derek Allard2067d1a2008-11-13 22:59:24 +0000164 /**
165 * Send Trackback Error Message
166 *
Derek Jones37f4b9c2011-07-01 17:56:50 -0500167 * Allows custom errors to be set. By default it
Derek Allard2067d1a2008-11-13 22:59:24 +0000168 * sends the "incomplete information" error, as that's
169 * the most common one.
170 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000171 * @param string
172 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200173 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200174 public function send_error($message = 'Incomplete Information')
Derek Allard2067d1a2008-11-13 22:59:24 +0000175 {
176 echo "<?xml version=\"1.0\" encoding=\"utf-8\"?".">\n<response>\n<error>1</error>\n<message>".$message."</message>\n</response>";
177 exit;
178 }
Barry Mienydd671972010-10-04 16:33:58 +0200179
Derek Allard2067d1a2008-11-13 22:59:24 +0000180 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200181
Derek Allard2067d1a2008-11-13 22:59:24 +0000182 /**
183 * Send Trackback Success Message
184 *
185 * This should be called when a trackback has been
186 * successfully received and inserted.
187 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000188 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200189 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200190 public function send_success()
Derek Allard2067d1a2008-11-13 22:59:24 +0000191 {
192 echo "<?xml version=\"1.0\" encoding=\"utf-8\"?".">\n<response>\n<error>0</error>\n</response>";
193 exit;
194 }
Barry Mienydd671972010-10-04 16:33:58 +0200195
Derek Allard2067d1a2008-11-13 22:59:24 +0000196 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200197
Derek Allard2067d1a2008-11-13 22:59:24 +0000198 /**
199 * Fetch a particular item
200 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000201 * @param string
202 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200203 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200204 public function data($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000205 {
206 return ( ! isset($this->data[$item])) ? '' : $this->data[$item];
207 }
208
209 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200210
Derek Allard2067d1a2008-11-13 22:59:24 +0000211 /**
212 * Process Trackback
213 *
214 * Opens a socket connection and passes the data to
Derek Jones37f4b9c2011-07-01 17:56:50 -0500215 * the server. Returns TRUE on success, FALSE on failure
Derek Allard2067d1a2008-11-13 22:59:24 +0000216 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000217 * @param string
218 * @param string
219 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200220 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200221 public function process($url, $data)
Derek Allard2067d1a2008-11-13 22:59:24 +0000222 {
223 $target = parse_url($url);
Barry Mienydd671972010-10-04 16:33:58 +0200224
Derek Allard2067d1a2008-11-13 22:59:24 +0000225 // Open the socket
226 if ( ! $fp = @fsockopen($target['host'], 80))
227 {
228 $this->set_error('Invalid Connection: '.$url);
229 return FALSE;
230 }
231
232 // Build the path
233 $ppath = ( ! isset($target['path'])) ? $url : $target['path'];
Barry Mienydd671972010-10-04 16:33:58 +0200234
Derek Allard2067d1a2008-11-13 22:59:24 +0000235 $path = (isset($target['query']) && $target['query'] != "") ? $ppath.'?'.$target['query'] : $ppath;
236
237 // Add the Trackback ID to the data string
238 if ($id = $this->get_id($url))
239 {
240 $data = "tb_id=".$id."&".$data;
241 }
242
243 // Transfer the data
244 fputs ($fp, "POST " . $path . " HTTP/1.0\r\n" );
245 fputs ($fp, "Host: " . $target['host'] . "\r\n" );
246 fputs ($fp, "Content-type: application/x-www-form-urlencoded\r\n" );
247 fputs ($fp, "Content-length: " . strlen($data) . "\r\n" );
248 fputs ($fp, "Connection: close\r\n\r\n" );
249 fputs ($fp, $data);
250
251 // Was it successful?
252 $this->response = "";
Barry Mienydd671972010-10-04 16:33:58 +0200253
Pascal Kriete14287f32011-02-14 13:39:34 -0500254 while ( ! feof($fp))
Derek Allard2067d1a2008-11-13 22:59:24 +0000255 {
256 $this->response .= fgets($fp, 128);
257 }
258 @fclose($fp);
Barry Mienydd671972010-10-04 16:33:58 +0200259
260
Andrey Andreevd9cfa7b2011-12-25 16:32:59 +0200261 if (stripos($this->response, '<error>0</error>') === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000262 {
Andrey Andreevd9cfa7b2011-12-25 16:32:59 +0200263 $message = (preg_match('/<message>(.*?)<\/message>/is', $this->response, $match)) ? trim($match[1]) : 'An unknown error was encountered';
Derek Allard2067d1a2008-11-13 22:59:24 +0000264 $this->set_error($message);
265 return FALSE;
266 }
267
268 return TRUE;
269 }
Barry Mienydd671972010-10-04 16:33:58 +0200270
Derek Allard2067d1a2008-11-13 22:59:24 +0000271 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200272
Derek Allard2067d1a2008-11-13 22:59:24 +0000273 /**
274 * Extract Trackback URLs
275 *
276 * This function lets multiple trackbacks be sent.
277 * It takes a string of URLs (separated by comma or
278 * space) and puts each URL into an array
279 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000280 * @param string
281 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200282 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200283 public function extract_urls($urls)
Barry Mienydd671972010-10-04 16:33:58 +0200284 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000285 // Remove the pesky white space and replace with a comma.
286 $urls = preg_replace("/\s*(\S+)\s*/", "\\1,", $urls);
Barry Mienydd671972010-10-04 16:33:58 +0200287
Derek Allard2067d1a2008-11-13 22:59:24 +0000288 // If they use commas get rid of the doubles.
289 $urls = str_replace(",,", ",", $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
Derek Allard2067d1a2008-11-13 22:59:24 +0000297 // Break into an array via commas
298 $urls = preg_split('/[,]/', $urls);
Barry Mienydd671972010-10-04 16:33:58 +0200299
Derek Allard2067d1a2008-11-13 22:59:24 +0000300 // Removes duplicates
301 $urls = array_unique($urls);
Barry Mienydd671972010-10-04 16:33:58 +0200302
Derek Allard2067d1a2008-11-13 22:59:24 +0000303 array_walk($urls, array($this, 'validate_url'));
Barry Mienydd671972010-10-04 16:33:58 +0200304
Derek Allard2067d1a2008-11-13 22:59:24 +0000305 return $urls;
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 * Validate URL
312 *
313 * Simply adds "http://" if missing
314 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000315 * @param string
316 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200317 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200318 public function validate_url($url)
Derek Allard2067d1a2008-11-13 22:59:24 +0000319 {
320 $url = trim($url);
321
Andrey Andreevd9cfa7b2011-12-25 16:32:59 +0200322 if (strpos($url, 'http') !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000323 {
Andrey Andreevd9cfa7b2011-12-25 16:32:59 +0200324 $url = 'http://'.$url;
Derek Allard2067d1a2008-11-13 22:59:24 +0000325 }
326 }
Barry Mienydd671972010-10-04 16:33:58 +0200327
Derek Allard2067d1a2008-11-13 22:59:24 +0000328 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200329
Derek Allard2067d1a2008-11-13 22:59:24 +0000330 /**
331 * Find the Trackback URL's ID
332 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000333 * @param string
334 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200335 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200336 public function get_id($url)
Barry Mienydd671972010-10-04 16:33:58 +0200337 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000338 $tb_id = "";
Barry Mienydd671972010-10-04 16:33:58 +0200339
Robin Sowell76b369e2010-03-19 11:15:28 -0400340 if (strpos($url, '?') !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000341 {
342 $tb_array = explode('/', $url);
Derek Jones37f4b9c2011-07-01 17:56:50 -0500343 $tb_end = $tb_array[count($tb_array)-1];
Barry Mienydd671972010-10-04 16:33:58 +0200344
Derek Allard2067d1a2008-11-13 22:59:24 +0000345 if ( ! is_numeric($tb_end))
346 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500347 $tb_end = $tb_array[count($tb_array)-2];
Derek Allard2067d1a2008-11-13 22:59:24 +0000348 }
Barry Mienydd671972010-10-04 16:33:58 +0200349
Derek Allard2067d1a2008-11-13 22:59:24 +0000350 $tb_array = explode('=', $tb_end);
351 $tb_id = $tb_array[count($tb_array)-1];
352 }
353 else
354 {
Derek Jones1322ec52009-03-11 17:01:14 +0000355 $url = rtrim($url, '/');
Barry Mienydd671972010-10-04 16:33:58 +0200356
Derek Allard2067d1a2008-11-13 22:59:24 +0000357 $tb_array = explode('/', $url);
358 $tb_id = $tb_array[count($tb_array)-1];
Barry Mienydd671972010-10-04 16:33:58 +0200359
Derek Allard2067d1a2008-11-13 22:59:24 +0000360 if ( ! is_numeric($tb_id))
361 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500362 $tb_id = $tb_array[count($tb_array)-2];
Derek Allard2067d1a2008-11-13 22:59:24 +0000363 }
Barry Mienydd671972010-10-04 16:33:58 +0200364 }
365
Derek Allard2067d1a2008-11-13 22:59:24 +0000366 if ( ! preg_match ("/^([0-9]+)$/", $tb_id))
367 {
Derek Jones5052e272010-03-02 22:53:38 -0600368 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000369 }
370 else
371 {
372 return $tb_id;
Barry Mienydd671972010-10-04 16:33:58 +0200373 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000374 }
Barry Mienydd671972010-10-04 16:33:58 +0200375
Derek Allard2067d1a2008-11-13 22:59:24 +0000376 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200377
Derek Allard2067d1a2008-11-13 22:59:24 +0000378 /**
379 * Convert Reserved XML characters to Entities
380 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000381 * @param string
382 * @return string
383 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200384 public function convert_xml($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000385 {
386 $temp = '__TEMP_AMPERSANDS__';
Barry Mienydd671972010-10-04 16:33:58 +0200387
Andrey Andreevd9cfa7b2011-12-25 16:32:59 +0200388 $str = preg_replace(array('/&#(\d+);/', '/&(\w+);/'), "$temp\\1;", $str);
Barry Mienydd671972010-10-04 16:33:58 +0200389
Derek Allard2067d1a2008-11-13 22:59:24 +0000390 $str = str_replace(array("&","<",">","\"", "'", "-"),
Barry Mienydd671972010-10-04 16:33:58 +0200391 array("&amp;", "&lt;", "&gt;", "&quot;", "&#39;", "&#45;"),
392 $str);
393
Andrey Andreevd9cfa7b2011-12-25 16:32:59 +0200394 $str = preg_replace(array("/$temp(\d+);/", "/$temp(\w+);/"), array('&#\\1;', '&\\1;'), $str);
Barry Mienydd671972010-10-04 16:33:58 +0200395
Derek Allard2067d1a2008-11-13 22:59:24 +0000396 return $str;
Barry Mienydd671972010-10-04 16:33:58 +0200397 }
398
Derek Allard2067d1a2008-11-13 22:59:24 +0000399 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200400
Derek Allard2067d1a2008-11-13 22:59:24 +0000401 /**
402 * Character limiter
403 *
404 * Limits the string based on the character count. Will preserve complete words.
405 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000406 * @param string
407 * @param integer
408 * @param string
409 * @return string
410 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200411 public function limit_characters($str, $n = 500, $end_char = '&#8230;')
Derek Allard2067d1a2008-11-13 22:59:24 +0000412 {
413 if (strlen($str) < $n)
414 {
415 return $str;
416 }
Barry Mienydd671972010-10-04 16:33:58 +0200417
Derek Allard2067d1a2008-11-13 22:59:24 +0000418 $str = preg_replace("/\s+/", ' ', str_replace(array("\r\n", "\r", "\n"), ' ', $str));
Barry Mienydd671972010-10-04 16:33:58 +0200419
Derek Allard2067d1a2008-11-13 22:59:24 +0000420 if (strlen($str) <= $n)
421 {
422 return $str;
423 }
Barry Mienydd671972010-10-04 16:33:58 +0200424
Andrey Andreevd9cfa7b2011-12-25 16:32:59 +0200425 $out = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000426 foreach (explode(' ', trim($str)) as $val)
427 {
Barry Mienydd671972010-10-04 16:33:58 +0200428 $out .= $val.' ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000429 if (strlen($out) >= $n)
430 {
Andrey Andreevd9cfa7b2011-12-25 16:32:59 +0200431 return rtrim($out).$end_char;
Barry Mienydd671972010-10-04 16:33:58 +0200432 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000433 }
434 }
Barry Mienydd671972010-10-04 16:33:58 +0200435
Derek Allard2067d1a2008-11-13 22:59:24 +0000436 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200437
Derek Allard2067d1a2008-11-13 22:59:24 +0000438 /**
439 * High ASCII to Entities
440 *
441 * Converts Hight ascii text and MS Word special chars
442 * to character entities
443 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000444 * @param string
445 * @return string
446 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200447 public function convert_ascii($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000448 {
Barry Mienydd671972010-10-04 16:33:58 +0200449 $count = 1;
450 $out = '';
451 $temp = array();
452
453 for ($i = 0, $s = strlen($str); $i < $s; $i++)
454 {
455 $ordinal = ord($str[$i]);
456
457 if ($ordinal < 128)
458 {
459 $out .= $str[$i];
460 }
461 else
462 {
Andrey Andreevd9cfa7b2011-12-25 16:32:59 +0200463 if (count($temp) === 0)
Barry Mienydd671972010-10-04 16:33:58 +0200464 {
465 $count = ($ordinal < 224) ? 2 : 3;
466 }
467
468 $temp[] = $ordinal;
469
Andrey Andreevd9cfa7b2011-12-25 16:32:59 +0200470 if (count($temp) === $count)
Barry Mienydd671972010-10-04 16:33:58 +0200471 {
Andrey Andreevd9cfa7b2011-12-25 16:32:59 +0200472 $number = ($count == 3) ? (($temp[0] % 16) * 4096) + (($temp[1] % 64) * 64) + ($temp[2] % 64) : (($temp[0] % 32) * 64) + ($temp[1] % 64);
Barry Mienydd671972010-10-04 16:33:58 +0200473
474 $out .= '&#'.$number.';';
475 $count = 1;
476 $temp = array();
477 }
478 }
479 }
480
481 return $out;
Derek Allard2067d1a2008-11-13 22:59:24 +0000482 }
Barry Mienydd671972010-10-04 16:33:58 +0200483
Derek Allard2067d1a2008-11-13 22:59:24 +0000484 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200485
Derek Allard2067d1a2008-11-13 22:59:24 +0000486 /**
487 * Set error message
488 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000489 * @param string
490 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200491 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200492 public function set_error($msg)
Derek Allard2067d1a2008-11-13 22:59:24 +0000493 {
494 log_message('error', $msg);
495 $this->error_msg[] = $msg;
496 }
Barry Mienydd671972010-10-04 16:33:58 +0200497
Derek Allard2067d1a2008-11-13 22:59:24 +0000498 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200499
Derek Allard2067d1a2008-11-13 22:59:24 +0000500 /**
501 * Show error messages
502 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000503 * @param string
504 * @param string
505 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200506 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200507 public function display_errors($open = '<p>', $close = '</p>')
Barry Mienydd671972010-10-04 16:33:58 +0200508 {
Andrey Andreev4eea9892011-12-19 12:05:41 +0200509 return (count($this->error_msg) > 0) ? $open . implode($close . $open, $this->error_msg) . $close : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000510 }
511
512}
513// END Trackback Class
514
515/* End of file Trackback.php */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200516/* Location: ./system/libraries/Trackback.php */