blob: 3fa55ca20db619c805b2466c1500e49e77f3ca63 [file] [log] [blame]
Derek Jones37f4b9c2011-07-01 17:56:50 -05001<?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
Derek Allard2067d1a2008-11-13 22:59:24 +000043 var $time_format = 'local';
44 var $charset = 'UTF-8';
45 var $data = array('url' => '', 'title' => '', 'excerpt' => '', 'blog_name' => '', 'charset' => '');
46 var $convert_ascii = TRUE;
47 var $response = '';
48 var $error_msg = array();
49
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
99 if ($this->convert_ascii == TRUE)
100 {
101 if ($item == 'excerpt')
102 {
103 $$item = $this->convert_ascii($$item);
104 }
105 elseif ($item == 'title')
106 {
107 $$item = $this->convert_ascii($$item);
108 }
Pascal Kriete14287f32011-02-14 13:39:34 -0500109 elseif ($item == 'blog_name')
Derek Allard2067d1a2008-11-13 22:59:24 +0000110 {
111 $$item = $this->convert_ascii($$item);
112 }
113 }
114 }
115
116 // Build the Trackback data string
117 $charset = ( ! isset($tb_data['charset'])) ? $this->charset : $tb_data['charset'];
Barry Mienydd671972010-10-04 16:33:58 +0200118
Derek Allard2067d1a2008-11-13 22:59:24 +0000119 $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 +0200120
Derek Allard2067d1a2008-11-13 22:59:24 +0000121 // Send Trackback(s)
122 $return = TRUE;
123 if (count($ping_url) > 0)
124 {
125 foreach ($ping_url as $url)
126 {
127 if ($this->process($url, $data) == FALSE)
128 {
129 $return = FALSE;
130 }
Barry Mienydd671972010-10-04 16:33:58 +0200131 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000132 }
133
134 return $return;
135 }
Barry Mienydd671972010-10-04 16:33:58 +0200136
Derek Allard2067d1a2008-11-13 22:59:24 +0000137 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200138
Derek Allard2067d1a2008-11-13 22:59:24 +0000139 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -0500140 * Receive Trackback Data
Derek Allard2067d1a2008-11-13 22:59:24 +0000141 *
142 * This function simply validates the incoming TB data.
Derek Jones5052e272010-03-02 22:53:38 -0600143 * It returns FALSE on failure and TRUE on success.
Derek Allard2067d1a2008-11-13 22:59:24 +0000144 * If the data is valid it is set to the $this->data array
145 * so that it can be inserted into a database.
146 *
147 * @access public
148 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200149 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200150 public function receive()
Barry Mienydd671972010-10-04 16:33:58 +0200151 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000152 foreach (array('url', 'title', 'blog_name', 'excerpt') as $val)
153 {
154 if ( ! isset($_POST[$val]) OR $_POST[$val] == '')
155 {
156 $this->set_error('The following required POST variable is missing: '.$val);
157 return FALSE;
158 }
Barry Mienydd671972010-10-04 16:33:58 +0200159
Derek Allard2067d1a2008-11-13 22:59:24 +0000160 $this->data['charset'] = ( ! isset($_POST['charset'])) ? 'auto' : strtoupper(trim($_POST['charset']));
Barry Mienydd671972010-10-04 16:33:58 +0200161
Derek Allard2067d1a2008-11-13 22:59:24 +0000162 if ($val != 'url' && function_exists('mb_convert_encoding'))
163 {
164 $_POST[$val] = mb_convert_encoding($_POST[$val], $this->charset, $this->data['charset']);
165 }
Barry Mienydd671972010-10-04 16:33:58 +0200166
Derek Allard2067d1a2008-11-13 22:59:24 +0000167 $_POST[$val] = ($val != 'url') ? $this->convert_xml(strip_tags($_POST[$val])) : strip_tags($_POST[$val]);
Barry Mienydd671972010-10-04 16:33:58 +0200168
Derek Allard2067d1a2008-11-13 22:59:24 +0000169 if ($val == 'excerpt')
170 {
171 $_POST['excerpt'] = $this->limit_characters($_POST['excerpt']);
172 }
Barry Mienydd671972010-10-04 16:33:58 +0200173
Derek Allard2067d1a2008-11-13 22:59:24 +0000174 $this->data[$val] = $_POST[$val];
175 }
176
177 return TRUE;
Barry Mienydd671972010-10-04 16:33:58 +0200178 }
179
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 Error Message
184 *
Derek Jones37f4b9c2011-07-01 17:56:50 -0500185 * Allows custom errors to be set. By default it
Derek Allard2067d1a2008-11-13 22:59:24 +0000186 * sends the "incomplete information" error, as that's
187 * the most common one.
188 *
189 * @access public
190 * @param string
191 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200192 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200193 public function send_error($message = 'Incomplete Information')
Derek Allard2067d1a2008-11-13 22:59:24 +0000194 {
195 echo "<?xml version=\"1.0\" encoding=\"utf-8\"?".">\n<response>\n<error>1</error>\n<message>".$message."</message>\n</response>";
196 exit;
197 }
Barry Mienydd671972010-10-04 16:33:58 +0200198
Derek Allard2067d1a2008-11-13 22:59:24 +0000199 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200200
Derek Allard2067d1a2008-11-13 22:59:24 +0000201 /**
202 * Send Trackback Success Message
203 *
204 * This should be called when a trackback has been
205 * successfully received and inserted.
206 *
207 * @access public
208 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200209 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200210 public function send_success()
Derek Allard2067d1a2008-11-13 22:59:24 +0000211 {
212 echo "<?xml version=\"1.0\" encoding=\"utf-8\"?".">\n<response>\n<error>0</error>\n</response>";
213 exit;
214 }
Barry Mienydd671972010-10-04 16:33:58 +0200215
Derek Allard2067d1a2008-11-13 22:59:24 +0000216 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200217
Derek Allard2067d1a2008-11-13 22:59:24 +0000218 /**
219 * Fetch a particular item
220 *
221 * @access public
222 * @param string
223 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200224 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200225 public function data($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000226 {
227 return ( ! isset($this->data[$item])) ? '' : $this->data[$item];
228 }
229
230 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200231
Derek Allard2067d1a2008-11-13 22:59:24 +0000232 /**
233 * Process Trackback
234 *
235 * Opens a socket connection and passes the data to
Derek Jones37f4b9c2011-07-01 17:56:50 -0500236 * the server. Returns TRUE on success, FALSE on failure
Derek Allard2067d1a2008-11-13 22:59:24 +0000237 *
238 * @access public
239 * @param string
240 * @param string
241 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200242 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200243 public function process($url, $data)
Derek Allard2067d1a2008-11-13 22:59:24 +0000244 {
245 $target = parse_url($url);
Barry Mienydd671972010-10-04 16:33:58 +0200246
Derek Allard2067d1a2008-11-13 22:59:24 +0000247 // Open the socket
248 if ( ! $fp = @fsockopen($target['host'], 80))
249 {
250 $this->set_error('Invalid Connection: '.$url);
251 return FALSE;
252 }
253
254 // Build the path
255 $ppath = ( ! isset($target['path'])) ? $url : $target['path'];
Barry Mienydd671972010-10-04 16:33:58 +0200256
Derek Allard2067d1a2008-11-13 22:59:24 +0000257 $path = (isset($target['query']) && $target['query'] != "") ? $ppath.'?'.$target['query'] : $ppath;
258
259 // Add the Trackback ID to the data string
260 if ($id = $this->get_id($url))
261 {
262 $data = "tb_id=".$id."&".$data;
263 }
264
265 // Transfer the data
266 fputs ($fp, "POST " . $path . " HTTP/1.0\r\n" );
267 fputs ($fp, "Host: " . $target['host'] . "\r\n" );
268 fputs ($fp, "Content-type: application/x-www-form-urlencoded\r\n" );
269 fputs ($fp, "Content-length: " . strlen($data) . "\r\n" );
270 fputs ($fp, "Connection: close\r\n\r\n" );
271 fputs ($fp, $data);
272
273 // Was it successful?
274 $this->response = "";
Barry Mienydd671972010-10-04 16:33:58 +0200275
Pascal Kriete14287f32011-02-14 13:39:34 -0500276 while ( ! feof($fp))
Derek Allard2067d1a2008-11-13 22:59:24 +0000277 {
278 $this->response .= fgets($fp, 128);
279 }
280 @fclose($fp);
Barry Mienydd671972010-10-04 16:33:58 +0200281
282
Derek Jones1322ec52009-03-11 17:01:14 +0000283 if (stristr($this->response, '<error>0</error>') === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000284 {
285 $message = 'An unknown error was encountered';
Barry Mienydd671972010-10-04 16:33:58 +0200286
Derek Allard2067d1a2008-11-13 22:59:24 +0000287 if (preg_match("/<message>(.*?)<\/message>/is", $this->response, $match))
288 {
289 $message = trim($match['1']);
290 }
Barry Mienydd671972010-10-04 16:33:58 +0200291
Derek Allard2067d1a2008-11-13 22:59:24 +0000292 $this->set_error($message);
293 return FALSE;
294 }
295
296 return TRUE;
297 }
Barry Mienydd671972010-10-04 16:33:58 +0200298
Derek Allard2067d1a2008-11-13 22:59:24 +0000299 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200300
Derek Allard2067d1a2008-11-13 22:59:24 +0000301 /**
302 * Extract Trackback URLs
303 *
304 * This function lets multiple trackbacks be sent.
305 * It takes a string of URLs (separated by comma or
306 * space) and puts each URL into an array
307 *
308 * @access public
309 * @param string
310 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200311 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200312 public function extract_urls($urls)
Barry Mienydd671972010-10-04 16:33:58 +0200313 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000314 // Remove the pesky white space and replace with a comma.
315 $urls = preg_replace("/\s*(\S+)\s*/", "\\1,", $urls);
Barry Mienydd671972010-10-04 16:33:58 +0200316
Derek Allard2067d1a2008-11-13 22:59:24 +0000317 // If they use commas get rid of the doubles.
318 $urls = str_replace(",,", ",", $urls);
Barry Mienydd671972010-10-04 16:33:58 +0200319
Derek Allard2067d1a2008-11-13 22:59:24 +0000320 // Remove any comma that might be at the end
321 if (substr($urls, -1) == ",")
322 {
323 $urls = substr($urls, 0, -1);
324 }
Barry Mienydd671972010-10-04 16:33:58 +0200325
Derek Allard2067d1a2008-11-13 22:59:24 +0000326 // Break into an array via commas
327 $urls = preg_split('/[,]/', $urls);
Barry Mienydd671972010-10-04 16:33:58 +0200328
Derek Allard2067d1a2008-11-13 22:59:24 +0000329 // Removes duplicates
330 $urls = array_unique($urls);
Barry Mienydd671972010-10-04 16:33:58 +0200331
Derek Allard2067d1a2008-11-13 22:59:24 +0000332 array_walk($urls, array($this, 'validate_url'));
Barry Mienydd671972010-10-04 16:33:58 +0200333
Derek Allard2067d1a2008-11-13 22:59:24 +0000334 return $urls;
335 }
Barry Mienydd671972010-10-04 16:33:58 +0200336
Derek Allard2067d1a2008-11-13 22:59:24 +0000337 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200338
Derek Allard2067d1a2008-11-13 22:59:24 +0000339 /**
340 * Validate URL
341 *
342 * Simply adds "http://" if missing
343 *
344 * @access public
345 * @param string
346 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200347 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200348 public function validate_url($url)
Derek Allard2067d1a2008-11-13 22:59:24 +0000349 {
350 $url = trim($url);
351
352 if (substr($url, 0, 4) != "http")
353 {
354 $url = "http://".$url;
355 }
356 }
Barry Mienydd671972010-10-04 16:33:58 +0200357
Derek Allard2067d1a2008-11-13 22:59:24 +0000358 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200359
Derek Allard2067d1a2008-11-13 22:59:24 +0000360 /**
361 * Find the Trackback URL's ID
362 *
363 * @access public
364 * @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 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000369 $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 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500393 $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
Derek Allard2067d1a2008-11-13 22:59:24 +0000397 if ( ! preg_match ("/^([0-9]+)$/", $tb_id))
398 {
Derek Jones5052e272010-03-02 22:53:38 -0600399 return FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000400 }
401 else
402 {
403 return $tb_id;
Barry Mienydd671972010-10-04 16:33:58 +0200404 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000405 }
Barry Mienydd671972010-10-04 16:33:58 +0200406
Derek Allard2067d1a2008-11-13 22:59:24 +0000407 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200408
Derek Allard2067d1a2008-11-13 22:59:24 +0000409 /**
410 * Convert Reserved XML characters to Entities
411 *
412 * @access public
413 * @param string
414 * @return string
415 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200416 public function convert_xml($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000417 {
418 $temp = '__TEMP_AMPERSANDS__';
Barry Mienydd671972010-10-04 16:33:58 +0200419
Derek Allard2067d1a2008-11-13 22:59:24 +0000420 $str = preg_replace("/&#(\d+);/", "$temp\\1;", $str);
Derek Jones37f4b9c2011-07-01 17:56:50 -0500421 $str = preg_replace("/&(\w+);/", "$temp\\1;", $str);
Barry Mienydd671972010-10-04 16:33:58 +0200422
Derek Allard2067d1a2008-11-13 22:59:24 +0000423 $str = str_replace(array("&","<",">","\"", "'", "-"),
Barry Mienydd671972010-10-04 16:33:58 +0200424 array("&amp;", "&lt;", "&gt;", "&quot;", "&#39;", "&#45;"),
425 $str);
426
Derek Allard2067d1a2008-11-13 22:59:24 +0000427 $str = preg_replace("/$temp(\d+);/","&#\\1;",$str);
428 $str = preg_replace("/$temp(\w+);/","&\\1;", $str);
Barry Mienydd671972010-10-04 16:33:58 +0200429
Derek Allard2067d1a2008-11-13 22:59:24 +0000430 return $str;
Barry Mienydd671972010-10-04 16:33:58 +0200431 }
432
Derek Allard2067d1a2008-11-13 22:59:24 +0000433 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200434
Derek Allard2067d1a2008-11-13 22:59:24 +0000435 /**
436 * Character limiter
437 *
438 * Limits the string based on the character count. Will preserve complete words.
439 *
440 * @access public
441 * @param string
442 * @param integer
443 * @param string
444 * @return string
445 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200446 public function limit_characters($str, $n = 500, $end_char = '&#8230;')
Derek Allard2067d1a2008-11-13 22:59:24 +0000447 {
448 if (strlen($str) < $n)
449 {
450 return $str;
451 }
Barry Mienydd671972010-10-04 16:33:58 +0200452
Derek Allard2067d1a2008-11-13 22:59:24 +0000453 $str = preg_replace("/\s+/", ' ', str_replace(array("\r\n", "\r", "\n"), ' ', $str));
Barry Mienydd671972010-10-04 16:33:58 +0200454
Derek Allard2067d1a2008-11-13 22:59:24 +0000455 if (strlen($str) <= $n)
456 {
457 return $str;
458 }
Barry Mienydd671972010-10-04 16:33:58 +0200459
Derek Allard2067d1a2008-11-13 22:59:24 +0000460 $out = "";
461 foreach (explode(' ', trim($str)) as $val)
462 {
Barry Mienydd671972010-10-04 16:33:58 +0200463 $out .= $val.' ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000464 if (strlen($out) >= $n)
465 {
466 return trim($out).$end_char;
Barry Mienydd671972010-10-04 16:33:58 +0200467 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000468 }
469 }
Barry Mienydd671972010-10-04 16:33:58 +0200470
Derek Allard2067d1a2008-11-13 22:59:24 +0000471 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200472
Derek Allard2067d1a2008-11-13 22:59:24 +0000473 /**
474 * High ASCII to Entities
475 *
476 * Converts Hight ascii text and MS Word special chars
477 * to character entities
478 *
479 * @access public
480 * @param string
481 * @return string
482 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200483 public function convert_ascii($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000484 {
Barry Mienydd671972010-10-04 16:33:58 +0200485 $count = 1;
486 $out = '';
487 $temp = array();
488
489 for ($i = 0, $s = strlen($str); $i < $s; $i++)
490 {
491 $ordinal = ord($str[$i]);
492
493 if ($ordinal < 128)
494 {
495 $out .= $str[$i];
496 }
497 else
498 {
499 if (count($temp) == 0)
500 {
501 $count = ($ordinal < 224) ? 2 : 3;
502 }
503
504 $temp[] = $ordinal;
505
506 if (count($temp) == $count)
507 {
508 $number = ($count == 3) ? (($temp['0'] % 16) * 4096) + (($temp['1'] % 64) * 64) + ($temp['2'] % 64) : (($temp['0'] % 32) * 64) + ($temp['1'] % 64);
509
510 $out .= '&#'.$number.';';
511 $count = 1;
512 $temp = array();
513 }
514 }
515 }
516
517 return $out;
Derek Allard2067d1a2008-11-13 22:59:24 +0000518 }
Barry Mienydd671972010-10-04 16:33:58 +0200519
Derek Allard2067d1a2008-11-13 22:59:24 +0000520 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200521
Derek Allard2067d1a2008-11-13 22:59:24 +0000522 /**
523 * Set error message
524 *
525 * @access public
526 * @param string
527 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200528 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200529 public function set_error($msg)
Derek Allard2067d1a2008-11-13 22:59:24 +0000530 {
531 log_message('error', $msg);
532 $this->error_msg[] = $msg;
533 }
Barry Mienydd671972010-10-04 16:33:58 +0200534
Derek Allard2067d1a2008-11-13 22:59:24 +0000535 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200536
Derek Allard2067d1a2008-11-13 22:59:24 +0000537 /**
538 * Show error messages
539 *
540 * @access public
541 * @param string
542 * @param string
543 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200544 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200545 public function display_errors($open = '<p>', $close = '</p>')
Barry Mienydd671972010-10-04 16:33:58 +0200546 {
Andrey Andreev4eea9892011-12-19 12:05:41 +0200547 return (count($this->error_msg) > 0) ? $open . implode($close . $open, $this->error_msg) . $close : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000548 }
549
550}
551// END Trackback Class
552
553/* End of file Trackback.php */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200554/* Location: ./system/libraries/Trackback.php */