blob: 4302634ccb0bf34f3f5290e7385bfc920de9b477 [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
21 * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc. (http://ellislab.com/)
22 * @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
50 /**
51 * Constructor
52 *
53 * @access public
54 */
Greg Akera9263282010-11-10 15:26:43 -060055 public function __construct()
Derek Allard2067d1a2008-11-13 22:59:24 +000056 {
57 log_message('debug', "Trackback Class Initialized");
58 }
Barry Mienydd671972010-10-04 16:33:58 +020059
Derek Allard2067d1a2008-11-13 22:59:24 +000060 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +020061
Derek Allard2067d1a2008-11-13 22:59:24 +000062 /**
63 * Send Trackback
64 *
65 * @access public
66 * @param array
67 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +020068 */
Andrey Andreev4eea9892011-12-19 12:05:41 +020069 public function send($tb_data)
Barry Mienydd671972010-10-04 16:33:58 +020070 {
Derek Allard2067d1a2008-11-13 22:59:24 +000071 if ( ! is_array($tb_data))
72 {
73 $this->set_error('The send() method must be passed an array');
74 return FALSE;
75 }
Barry Mienydd671972010-10-04 16:33:58 +020076
Derek Allard2067d1a2008-11-13 22:59:24 +000077 // Pre-process the Trackback Data
78 foreach (array('url', 'title', 'excerpt', 'blog_name', 'ping_url') as $item)
79 {
80 if ( ! isset($tb_data[$item]))
81 {
82 $this->set_error('Required item missing: '.$item);
83 return FALSE;
84 }
Barry Mienydd671972010-10-04 16:33:58 +020085
Derek Allard2067d1a2008-11-13 22:59:24 +000086 switch ($item)
87 {
88 case 'ping_url' : $$item = $this->extract_urls($tb_data[$item]);
89 break;
90 case 'excerpt' : $$item = $this->limit_characters($this->convert_xml(strip_tags(stripslashes($tb_data[$item]))));
91 break;
Barry Mienydd671972010-10-04 16:33:58 +020092 case 'url' : $$item = str_replace('&#45;', '-', $this->convert_xml(strip_tags(stripslashes($tb_data[$item]))));
Derek Allard2067d1a2008-11-13 22:59:24 +000093 break;
94 default : $$item = $this->convert_xml(strip_tags(stripslashes($tb_data[$item])));
95 break;
96 }
97
98 // Convert High ASCII Characters
Andrey Andreevd9cfa7b2011-12-25 16:32:59 +020099 if ($this->convert_ascii == TRUE && in_array($item, array('excerpt', 'title', 'blog_name')))
Derek Allard2067d1a2008-11-13 22:59:24 +0000100 {
Andrey Andreevd9cfa7b2011-12-25 16:32:59 +0200101 $$item = $this->convert_ascii($$item);
Derek Allard2067d1a2008-11-13 22:59:24 +0000102 }
103 }
104
105 // Build the Trackback data string
106 $charset = ( ! isset($tb_data['charset'])) ? $this->charset : $tb_data['charset'];
Barry Mienydd671972010-10-04 16:33:58 +0200107
Derek Allard2067d1a2008-11-13 22:59:24 +0000108 $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 +0200109
Derek Allard2067d1a2008-11-13 22:59:24 +0000110 // Send Trackback(s)
111 $return = TRUE;
112 if (count($ping_url) > 0)
113 {
114 foreach ($ping_url as $url)
115 {
116 if ($this->process($url, $data) == FALSE)
117 {
118 $return = FALSE;
119 }
Barry Mienydd671972010-10-04 16:33:58 +0200120 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000121 }
122
123 return $return;
124 }
Barry Mienydd671972010-10-04 16:33:58 +0200125
Derek Allard2067d1a2008-11-13 22:59:24 +0000126 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200127
Derek Allard2067d1a2008-11-13 22:59:24 +0000128 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -0500129 * Receive Trackback Data
Derek Allard2067d1a2008-11-13 22:59:24 +0000130 *
131 * This function simply validates the incoming TB data.
Derek Jones5052e272010-03-02 22:53:38 -0600132 * It returns FALSE on failure and TRUE on success.
Derek Allard2067d1a2008-11-13 22:59:24 +0000133 * If the data is valid it is set to the $this->data array
134 * so that it can be inserted into a database.
135 *
136 * @access public
137 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200138 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200139 public function receive()
Barry Mienydd671972010-10-04 16:33:58 +0200140 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000141 foreach (array('url', 'title', 'blog_name', 'excerpt') as $val)
142 {
143 if ( ! isset($_POST[$val]) OR $_POST[$val] == '')
144 {
145 $this->set_error('The following required POST variable is missing: '.$val);
146 return FALSE;
147 }
Barry Mienydd671972010-10-04 16:33:58 +0200148
Derek Allard2067d1a2008-11-13 22:59:24 +0000149 $this->data['charset'] = ( ! isset($_POST['charset'])) ? 'auto' : strtoupper(trim($_POST['charset']));
Barry Mienydd671972010-10-04 16:33:58 +0200150
Derek Allard2067d1a2008-11-13 22:59:24 +0000151 if ($val != 'url' && function_exists('mb_convert_encoding'))
152 {
153 $_POST[$val] = mb_convert_encoding($_POST[$val], $this->charset, $this->data['charset']);
154 }
Barry Mienydd671972010-10-04 16:33:58 +0200155
Derek Allard2067d1a2008-11-13 22:59:24 +0000156 $_POST[$val] = ($val != 'url') ? $this->convert_xml(strip_tags($_POST[$val])) : strip_tags($_POST[$val]);
Barry Mienydd671972010-10-04 16:33:58 +0200157
Derek Allard2067d1a2008-11-13 22:59:24 +0000158 if ($val == 'excerpt')
159 {
160 $_POST['excerpt'] = $this->limit_characters($_POST['excerpt']);
161 }
Barry Mienydd671972010-10-04 16:33:58 +0200162
Derek Allard2067d1a2008-11-13 22:59:24 +0000163 $this->data[$val] = $_POST[$val];
164 }
165
166 return TRUE;
Barry Mienydd671972010-10-04 16:33:58 +0200167 }
168
Derek Allard2067d1a2008-11-13 22:59:24 +0000169 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200170
Derek Allard2067d1a2008-11-13 22:59:24 +0000171 /**
172 * Send Trackback Error Message
173 *
Derek Jones37f4b9c2011-07-01 17:56:50 -0500174 * Allows custom errors to be set. By default it
Derek Allard2067d1a2008-11-13 22:59:24 +0000175 * sends the "incomplete information" error, as that's
176 * the most common one.
177 *
178 * @access public
179 * @param string
180 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200181 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200182 public function send_error($message = 'Incomplete Information')
Derek Allard2067d1a2008-11-13 22:59:24 +0000183 {
184 echo "<?xml version=\"1.0\" encoding=\"utf-8\"?".">\n<response>\n<error>1</error>\n<message>".$message."</message>\n</response>";
185 exit;
186 }
Barry Mienydd671972010-10-04 16:33:58 +0200187
Derek Allard2067d1a2008-11-13 22:59:24 +0000188 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200189
Derek Allard2067d1a2008-11-13 22:59:24 +0000190 /**
191 * Send Trackback Success Message
192 *
193 * This should be called when a trackback has been
194 * successfully received and inserted.
195 *
196 * @access public
197 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200198 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200199 public function send_success()
Derek Allard2067d1a2008-11-13 22:59:24 +0000200 {
201 echo "<?xml version=\"1.0\" encoding=\"utf-8\"?".">\n<response>\n<error>0</error>\n</response>";
202 exit;
203 }
Barry Mienydd671972010-10-04 16:33:58 +0200204
Derek Allard2067d1a2008-11-13 22:59:24 +0000205 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200206
Derek Allard2067d1a2008-11-13 22:59:24 +0000207 /**
208 * Fetch a particular item
209 *
210 * @access public
211 * @param string
212 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200213 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200214 public function data($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000215 {
216 return ( ! isset($this->data[$item])) ? '' : $this->data[$item];
217 }
218
219 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200220
Derek Allard2067d1a2008-11-13 22:59:24 +0000221 /**
222 * Process Trackback
223 *
224 * Opens a socket connection and passes the data to
Derek Jones37f4b9c2011-07-01 17:56:50 -0500225 * the server. Returns TRUE on success, FALSE on failure
Derek Allard2067d1a2008-11-13 22:59:24 +0000226 *
227 * @access public
228 * @param string
229 * @param string
230 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200231 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200232 public function process($url, $data)
Derek Allard2067d1a2008-11-13 22:59:24 +0000233 {
234 $target = parse_url($url);
Barry Mienydd671972010-10-04 16:33:58 +0200235
Derek Allard2067d1a2008-11-13 22:59:24 +0000236 // Open the socket
237 if ( ! $fp = @fsockopen($target['host'], 80))
238 {
239 $this->set_error('Invalid Connection: '.$url);
240 return FALSE;
241 }
242
243 // Build the path
244 $ppath = ( ! isset($target['path'])) ? $url : $target['path'];
Barry Mienydd671972010-10-04 16:33:58 +0200245
Derek Allard2067d1a2008-11-13 22:59:24 +0000246 $path = (isset($target['query']) && $target['query'] != "") ? $ppath.'?'.$target['query'] : $ppath;
247
248 // Add the Trackback ID to the data string
249 if ($id = $this->get_id($url))
250 {
251 $data = "tb_id=".$id."&".$data;
252 }
253
254 // Transfer the data
255 fputs ($fp, "POST " . $path . " HTTP/1.0\r\n" );
256 fputs ($fp, "Host: " . $target['host'] . "\r\n" );
257 fputs ($fp, "Content-type: application/x-www-form-urlencoded\r\n" );
258 fputs ($fp, "Content-length: " . strlen($data) . "\r\n" );
259 fputs ($fp, "Connection: close\r\n\r\n" );
260 fputs ($fp, $data);
261
262 // Was it successful?
263 $this->response = "";
Barry Mienydd671972010-10-04 16:33:58 +0200264
Pascal Kriete14287f32011-02-14 13:39:34 -0500265 while ( ! feof($fp))
Derek Allard2067d1a2008-11-13 22:59:24 +0000266 {
267 $this->response .= fgets($fp, 128);
268 }
269 @fclose($fp);
Barry Mienydd671972010-10-04 16:33:58 +0200270
271
Andrey Andreevd9cfa7b2011-12-25 16:32:59 +0200272 if (stripos($this->response, '<error>0</error>') === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000273 {
Andrey Andreevd9cfa7b2011-12-25 16:32:59 +0200274 $message = (preg_match('/<message>(.*?)<\/message>/is', $this->response, $match)) ? trim($match[1]) : 'An unknown error was encountered';
Derek Allard2067d1a2008-11-13 22:59:24 +0000275 $this->set_error($message);
276 return FALSE;
277 }
278
279 return TRUE;
280 }
Barry Mienydd671972010-10-04 16:33:58 +0200281
Derek Allard2067d1a2008-11-13 22:59:24 +0000282 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200283
Derek Allard2067d1a2008-11-13 22:59:24 +0000284 /**
285 * Extract Trackback URLs
286 *
287 * This function lets multiple trackbacks be sent.
288 * It takes a string of URLs (separated by comma or
289 * space) and puts each URL into an array
290 *
291 * @access public
292 * @param string
293 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200294 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200295 public function extract_urls($urls)
Barry Mienydd671972010-10-04 16:33:58 +0200296 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000297 // Remove the pesky white space and replace with a comma.
298 $urls = preg_replace("/\s*(\S+)\s*/", "\\1,", $urls);
Barry Mienydd671972010-10-04 16:33:58 +0200299
Derek Allard2067d1a2008-11-13 22:59:24 +0000300 // If they use commas get rid of the doubles.
301 $urls = str_replace(",,", ",", $urls);
Barry Mienydd671972010-10-04 16:33:58 +0200302
Derek Allard2067d1a2008-11-13 22:59:24 +0000303 // Remove any comma that might be at the end
Andrey Andreevd9cfa7b2011-12-25 16:32:59 +0200304 if (substr($urls, -1) === ',')
Derek Allard2067d1a2008-11-13 22:59:24 +0000305 {
306 $urls = substr($urls, 0, -1);
307 }
Barry Mienydd671972010-10-04 16:33:58 +0200308
Derek Allard2067d1a2008-11-13 22:59:24 +0000309 // Break into an array via commas
310 $urls = preg_split('/[,]/', $urls);
Barry Mienydd671972010-10-04 16:33:58 +0200311
Derek Allard2067d1a2008-11-13 22:59:24 +0000312 // Removes duplicates
313 $urls = array_unique($urls);
Barry Mienydd671972010-10-04 16:33:58 +0200314
Derek Allard2067d1a2008-11-13 22:59:24 +0000315 array_walk($urls, array($this, 'validate_url'));
Barry Mienydd671972010-10-04 16:33:58 +0200316
Derek Allard2067d1a2008-11-13 22:59:24 +0000317 return $urls;
318 }
Barry Mienydd671972010-10-04 16:33:58 +0200319
Derek Allard2067d1a2008-11-13 22:59:24 +0000320 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200321
Derek Allard2067d1a2008-11-13 22:59:24 +0000322 /**
323 * Validate URL
324 *
325 * Simply adds "http://" if missing
326 *
327 * @access public
328 * @param string
329 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200330 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200331 public function validate_url($url)
Derek Allard2067d1a2008-11-13 22:59:24 +0000332 {
333 $url = trim($url);
334
Andrey Andreevd9cfa7b2011-12-25 16:32:59 +0200335 if (strpos($url, 'http') !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000336 {
Andrey Andreevd9cfa7b2011-12-25 16:32:59 +0200337 $url = 'http://'.$url;
Derek Allard2067d1a2008-11-13 22:59:24 +0000338 }
339 }
Barry Mienydd671972010-10-04 16:33:58 +0200340
Derek Allard2067d1a2008-11-13 22:59:24 +0000341 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200342
Derek Allard2067d1a2008-11-13 22:59:24 +0000343 /**
344 * Find the Trackback URL's ID
345 *
346 * @access public
347 * @param string
348 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200349 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200350 public function get_id($url)
Barry Mienydd671972010-10-04 16:33:58 +0200351 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000352 $tb_id = "";
Barry Mienydd671972010-10-04 16:33:58 +0200353
Robin Sowell76b369e2010-03-19 11:15:28 -0400354 if (strpos($url, '?') !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000355 {
356 $tb_array = explode('/', $url);
Derek Jones37f4b9c2011-07-01 17:56:50 -0500357 $tb_end = $tb_array[count($tb_array)-1];
Barry Mienydd671972010-10-04 16:33:58 +0200358
Derek Allard2067d1a2008-11-13 22:59:24 +0000359 if ( ! is_numeric($tb_end))
360 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500361 $tb_end = $tb_array[count($tb_array)-2];
Derek Allard2067d1a2008-11-13 22:59:24 +0000362 }
Barry Mienydd671972010-10-04 16:33:58 +0200363
Derek Allard2067d1a2008-11-13 22:59:24 +0000364 $tb_array = explode('=', $tb_end);
365 $tb_id = $tb_array[count($tb_array)-1];
366 }
367 else
368 {
Derek Jones1322ec52009-03-11 17:01:14 +0000369 $url = rtrim($url, '/');
Barry Mienydd671972010-10-04 16:33:58 +0200370
Derek Allard2067d1a2008-11-13 22:59:24 +0000371 $tb_array = explode('/', $url);
372 $tb_id = $tb_array[count($tb_array)-1];
Barry Mienydd671972010-10-04 16:33:58 +0200373
Derek Allard2067d1a2008-11-13 22:59:24 +0000374 if ( ! is_numeric($tb_id))
375 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500376 $tb_id = $tb_array[count($tb_array)-2];
Derek Allard2067d1a2008-11-13 22:59:24 +0000377 }
Barry Mienydd671972010-10-04 16:33:58 +0200378 }
379
Derek Allard2067d1a2008-11-13 22:59:24 +0000380 if ( ! preg_match ("/^([0-9]+)$/", $tb_id))
381 {
Derek Jones5052e272010-03-02 22:53:38 -0600382 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000383 }
384 else
385 {
386 return $tb_id;
Barry Mienydd671972010-10-04 16:33:58 +0200387 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000388 }
Barry Mienydd671972010-10-04 16:33:58 +0200389
Derek Allard2067d1a2008-11-13 22:59:24 +0000390 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200391
Derek Allard2067d1a2008-11-13 22:59:24 +0000392 /**
393 * Convert Reserved XML characters to Entities
394 *
395 * @access public
396 * @param string
397 * @return string
398 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200399 public function convert_xml($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000400 {
401 $temp = '__TEMP_AMPERSANDS__';
Barry Mienydd671972010-10-04 16:33:58 +0200402
Andrey Andreevd9cfa7b2011-12-25 16:32:59 +0200403 $str = preg_replace(array('/&#(\d+);/', '/&(\w+);/'), "$temp\\1;", $str);
Barry Mienydd671972010-10-04 16:33:58 +0200404
Derek Allard2067d1a2008-11-13 22:59:24 +0000405 $str = str_replace(array("&","<",">","\"", "'", "-"),
Barry Mienydd671972010-10-04 16:33:58 +0200406 array("&amp;", "&lt;", "&gt;", "&quot;", "&#39;", "&#45;"),
407 $str);
408
Andrey Andreevd9cfa7b2011-12-25 16:32:59 +0200409 $str = preg_replace(array("/$temp(\d+);/", "/$temp(\w+);/"), array('&#\\1;', '&\\1;'), $str);
Barry Mienydd671972010-10-04 16:33:58 +0200410
Derek Allard2067d1a2008-11-13 22:59:24 +0000411 return $str;
Barry Mienydd671972010-10-04 16:33:58 +0200412 }
413
Derek Allard2067d1a2008-11-13 22:59:24 +0000414 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200415
Derek Allard2067d1a2008-11-13 22:59:24 +0000416 /**
417 * Character limiter
418 *
419 * Limits the string based on the character count. Will preserve complete words.
420 *
421 * @access public
422 * @param string
423 * @param integer
424 * @param string
425 * @return string
426 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200427 public function limit_characters($str, $n = 500, $end_char = '&#8230;')
Derek Allard2067d1a2008-11-13 22:59:24 +0000428 {
429 if (strlen($str) < $n)
430 {
431 return $str;
432 }
Barry Mienydd671972010-10-04 16:33:58 +0200433
Derek Allard2067d1a2008-11-13 22:59:24 +0000434 $str = preg_replace("/\s+/", ' ', str_replace(array("\r\n", "\r", "\n"), ' ', $str));
Barry Mienydd671972010-10-04 16:33:58 +0200435
Derek Allard2067d1a2008-11-13 22:59:24 +0000436 if (strlen($str) <= $n)
437 {
438 return $str;
439 }
Barry Mienydd671972010-10-04 16:33:58 +0200440
Andrey Andreevd9cfa7b2011-12-25 16:32:59 +0200441 $out = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000442 foreach (explode(' ', trim($str)) as $val)
443 {
Barry Mienydd671972010-10-04 16:33:58 +0200444 $out .= $val.' ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000445 if (strlen($out) >= $n)
446 {
Andrey Andreevd9cfa7b2011-12-25 16:32:59 +0200447 return rtrim($out).$end_char;
Barry Mienydd671972010-10-04 16:33:58 +0200448 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000449 }
450 }
Barry Mienydd671972010-10-04 16:33:58 +0200451
Derek Allard2067d1a2008-11-13 22:59:24 +0000452 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200453
Derek Allard2067d1a2008-11-13 22:59:24 +0000454 /**
455 * High ASCII to Entities
456 *
457 * Converts Hight ascii text and MS Word special chars
458 * to character entities
459 *
460 * @access public
461 * @param string
462 * @return string
463 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200464 public function convert_ascii($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000465 {
Barry Mienydd671972010-10-04 16:33:58 +0200466 $count = 1;
467 $out = '';
468 $temp = array();
469
470 for ($i = 0, $s = strlen($str); $i < $s; $i++)
471 {
472 $ordinal = ord($str[$i]);
473
474 if ($ordinal < 128)
475 {
476 $out .= $str[$i];
477 }
478 else
479 {
Andrey Andreevd9cfa7b2011-12-25 16:32:59 +0200480 if (count($temp) === 0)
Barry Mienydd671972010-10-04 16:33:58 +0200481 {
482 $count = ($ordinal < 224) ? 2 : 3;
483 }
484
485 $temp[] = $ordinal;
486
Andrey Andreevd9cfa7b2011-12-25 16:32:59 +0200487 if (count($temp) === $count)
Barry Mienydd671972010-10-04 16:33:58 +0200488 {
Andrey Andreevd9cfa7b2011-12-25 16:32:59 +0200489 $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 +0200490
491 $out .= '&#'.$number.';';
492 $count = 1;
493 $temp = array();
494 }
495 }
496 }
497
498 return $out;
Derek Allard2067d1a2008-11-13 22:59:24 +0000499 }
Barry Mienydd671972010-10-04 16:33:58 +0200500
Derek Allard2067d1a2008-11-13 22:59:24 +0000501 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200502
Derek Allard2067d1a2008-11-13 22:59:24 +0000503 /**
504 * Set error message
505 *
506 * @access public
507 * @param string
508 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200509 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200510 public function set_error($msg)
Derek Allard2067d1a2008-11-13 22:59:24 +0000511 {
512 log_message('error', $msg);
513 $this->error_msg[] = $msg;
514 }
Barry Mienydd671972010-10-04 16:33:58 +0200515
Derek Allard2067d1a2008-11-13 22:59:24 +0000516 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200517
Derek Allard2067d1a2008-11-13 22:59:24 +0000518 /**
519 * Show error messages
520 *
521 * @access public
522 * @param string
523 * @param string
524 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200525 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200526 public function display_errors($open = '<p>', $close = '</p>')
Barry Mienydd671972010-10-04 16:33:58 +0200527 {
Andrey Andreev4eea9892011-12-19 12:05:41 +0200528 return (count($this->error_msg) > 0) ? $open . implode($close . $open, $this->error_msg) . $close : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000529 }
530
531}
532// END Trackback Class
533
534/* End of file Trackback.php */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200535/* Location: ./system/libraries/Trackback.php */