blob: 6761f63a5f935818d790c76749adbbf9911d9f4c [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 *
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 */
27
Derek Allard2067d1a2008-11-13 22:59:24 +000028/**
29 * Trackback Class
30 *
31 * Trackback Sending/Receiving Class
32 *
33 * @package CodeIgniter
34 * @subpackage Libraries
35 * @category Trackbacks
Derek Jonesf4a4bd82011-10-20 12:18:42 -050036 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000037 * @link http://codeigniter.com/user_guide/libraries/trackback.html
38 */
39class CI_Trackback {
Barry Mienydd671972010-10-04 16:33:58 +020040
Andrey Andreev3a459572011-12-21 11:23:11 +020041 public $time_format = 'local';
42 public $charset = 'UTF-8';
43 public $data = array('url' => '', 'title' => '', 'excerpt' => '', 'blog_name' => '', 'charset' => '');
44 public $convert_ascii = TRUE;
45 public $response = '';
46 public $error_msg = array();
Derek Allard2067d1a2008-11-13 22:59:24 +000047
Greg Akera9263282010-11-10 15:26:43 -060048 public function __construct()
Derek Allard2067d1a2008-11-13 22:59:24 +000049 {
Andrey Andreev426faa92012-04-03 19:03:58 +030050 log_message('debug', 'Trackback Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +000051 }
Barry Mienydd671972010-10-04 16:33:58 +020052
Derek Allard2067d1a2008-11-13 22:59:24 +000053 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +020054
Derek Allard2067d1a2008-11-13 22:59:24 +000055 /**
56 * Send Trackback
57 *
Derek Allard2067d1a2008-11-13 22:59:24 +000058 * @param array
59 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +020060 */
Andrey Andreev4eea9892011-12-19 12:05:41 +020061 public function send($tb_data)
Barry Mienydd671972010-10-04 16:33:58 +020062 {
Derek Allard2067d1a2008-11-13 22:59:24 +000063 if ( ! is_array($tb_data))
64 {
65 $this->set_error('The send() method must be passed an array');
66 return FALSE;
67 }
Barry Mienydd671972010-10-04 16:33:58 +020068
Derek Allard2067d1a2008-11-13 22:59:24 +000069 // Pre-process the Trackback Data
70 foreach (array('url', 'title', 'excerpt', 'blog_name', 'ping_url') as $item)
71 {
72 if ( ! isset($tb_data[$item]))
73 {
74 $this->set_error('Required item missing: '.$item);
75 return FALSE;
76 }
Barry Mienydd671972010-10-04 16:33:58 +020077
Derek Allard2067d1a2008-11-13 22:59:24 +000078 switch ($item)
79 {
80 case 'ping_url' : $$item = $this->extract_urls($tb_data[$item]);
81 break;
82 case 'excerpt' : $$item = $this->limit_characters($this->convert_xml(strip_tags(stripslashes($tb_data[$item]))));
83 break;
Barry Mienydd671972010-10-04 16:33:58 +020084 case 'url' : $$item = str_replace('&#45;', '-', $this->convert_xml(strip_tags(stripslashes($tb_data[$item]))));
Derek Allard2067d1a2008-11-13 22:59:24 +000085 break;
86 default : $$item = $this->convert_xml(strip_tags(stripslashes($tb_data[$item])));
87 break;
88 }
89
90 // Convert High ASCII Characters
Andrey Andreevd9cfa7b2011-12-25 16:32:59 +020091 if ($this->convert_ascii == TRUE && in_array($item, array('excerpt', 'title', 'blog_name')))
Derek Allard2067d1a2008-11-13 22:59:24 +000092 {
Andrey Andreevd9cfa7b2011-12-25 16:32:59 +020093 $$item = $this->convert_ascii($$item);
Derek Allard2067d1a2008-11-13 22:59:24 +000094 }
95 }
96
97 // Build the Trackback data string
Andrey Andreev426faa92012-04-03 19:03:58 +030098 $charset = isset($tb_data['charset']) ? $tb_data['charset'] : $this->charset;
Barry Mienydd671972010-10-04 16:33:58 +020099
Andrey Andreev426faa92012-04-03 19:03:58 +0300100 $data = 'url='.rawurlencode($url).'&title='.rawurlencode($title).'&blog_name='.rawurlencode($blog_name)
101 .'&excerpt='.rawurlencode($excerpt).'&charset='.rawurlencode($charset);
Barry Mienydd671972010-10-04 16:33:58 +0200102
Derek Allard2067d1a2008-11-13 22:59:24 +0000103 // Send Trackback(s)
104 $return = TRUE;
105 if (count($ping_url) > 0)
106 {
107 foreach ($ping_url as $url)
108 {
109 if ($this->process($url, $data) == FALSE)
110 {
111 $return = FALSE;
112 }
Barry Mienydd671972010-10-04 16:33:58 +0200113 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000114 }
115
116 return $return;
117 }
Barry Mienydd671972010-10-04 16:33:58 +0200118
Derek Allard2067d1a2008-11-13 22:59:24 +0000119 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200120
Derek Allard2067d1a2008-11-13 22:59:24 +0000121 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -0500122 * Receive Trackback Data
Derek Allard2067d1a2008-11-13 22:59:24 +0000123 *
124 * This function simply validates the incoming TB data.
Derek Jones5052e272010-03-02 22:53:38 -0600125 * It returns FALSE on failure and TRUE on success.
Derek Allard2067d1a2008-11-13 22:59:24 +0000126 * If the data is valid it is set to the $this->data array
127 * so that it can be inserted into a database.
128 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000129 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200130 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200131 public function receive()
Barry Mienydd671972010-10-04 16:33:58 +0200132 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000133 foreach (array('url', 'title', 'blog_name', 'excerpt') as $val)
134 {
135 if ( ! isset($_POST[$val]) OR $_POST[$val] == '')
136 {
137 $this->set_error('The following required POST variable is missing: '.$val);
138 return FALSE;
139 }
Barry Mienydd671972010-10-04 16:33:58 +0200140
Andrey Andreev426faa92012-04-03 19:03:58 +0300141 $this->data['charset'] = isset($_POST['charset']) ? strtoupper(trim($_POST['charset'])) : 'auto';
Barry Mienydd671972010-10-04 16:33:58 +0200142
tiyowan5b9fd2d2012-03-12 20:26:59 +0400143 if ($val != 'url' && MB_ENABLED === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000144 {
145 $_POST[$val] = mb_convert_encoding($_POST[$val], $this->charset, $this->data['charset']);
146 }
Barry Mienydd671972010-10-04 16:33:58 +0200147
Derek Allard2067d1a2008-11-13 22:59:24 +0000148 $_POST[$val] = ($val != 'url') ? $this->convert_xml(strip_tags($_POST[$val])) : strip_tags($_POST[$val]);
Barry Mienydd671972010-10-04 16:33:58 +0200149
Derek Allard2067d1a2008-11-13 22:59:24 +0000150 if ($val == 'excerpt')
151 {
152 $_POST['excerpt'] = $this->limit_characters($_POST['excerpt']);
153 }
Barry Mienydd671972010-10-04 16:33:58 +0200154
Derek Allard2067d1a2008-11-13 22:59:24 +0000155 $this->data[$val] = $_POST[$val];
156 }
157
158 return TRUE;
Barry Mienydd671972010-10-04 16:33:58 +0200159 }
160
Derek Allard2067d1a2008-11-13 22:59:24 +0000161 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200162
Derek Allard2067d1a2008-11-13 22:59:24 +0000163 /**
164 * Send Trackback Error Message
165 *
Andrey Andreev426faa92012-04-03 19:03:58 +0300166 * Allows custom errors to be set. By default it
Derek Allard2067d1a2008-11-13 22:59:24 +0000167 * sends the "incomplete information" error, as that's
168 * the most common one.
169 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000170 * @param string
171 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200172 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200173 public function send_error($message = 'Incomplete Information')
Derek Allard2067d1a2008-11-13 22:59:24 +0000174 {
Andrey Andreev426faa92012-04-03 19:03:58 +0300175 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 +0000176 exit;
177 }
Barry Mienydd671972010-10-04 16:33:58 +0200178
Derek Allard2067d1a2008-11-13 22:59:24 +0000179 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200180
Derek Allard2067d1a2008-11-13 22:59:24 +0000181 /**
182 * Send Trackback Success Message
183 *
184 * This should be called when a trackback has been
185 * successfully received and inserted.
186 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000187 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200188 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200189 public function send_success()
Derek Allard2067d1a2008-11-13 22:59:24 +0000190 {
Andrey Andreev426faa92012-04-03 19:03:58 +0300191 echo '<?xml version="1.0" encoding="utf-8"?'.">\n<response>\n<error>0</error>\n</response>";
Derek Allard2067d1a2008-11-13 22:59:24 +0000192 exit;
193 }
Barry Mienydd671972010-10-04 16:33:58 +0200194
Derek Allard2067d1a2008-11-13 22:59:24 +0000195 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200196
Derek Allard2067d1a2008-11-13 22:59:24 +0000197 /**
198 * Fetch a particular item
199 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000200 * @param string
201 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200202 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200203 public function data($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000204 {
Andrey Andreev426faa92012-04-03 19:03:58 +0300205 return isset($this->data[$item]) ? $this->data[$item] : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000206 }
207
208 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200209
Derek Allard2067d1a2008-11-13 22:59:24 +0000210 /**
211 * Process Trackback
212 *
213 * Opens a socket connection and passes the data to
Andrey Andreev426faa92012-04-03 19:03:58 +0300214 * the server. Returns TRUE on success, FALSE on failure
Derek Allard2067d1a2008-11-13 22:59:24 +0000215 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000216 * @param string
217 * @param string
218 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200219 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200220 public function process($url, $data)
Derek Allard2067d1a2008-11-13 22:59:24 +0000221 {
222 $target = parse_url($url);
Barry Mienydd671972010-10-04 16:33:58 +0200223
Derek Allard2067d1a2008-11-13 22:59:24 +0000224 // Open the socket
225 if ( ! $fp = @fsockopen($target['host'], 80))
226 {
227 $this->set_error('Invalid Connection: '.$url);
228 return FALSE;
229 }
230
231 // Build the path
Andrey Andreev426faa92012-04-03 19:03:58 +0300232 $ppath = isset($target['path']) ? $target['path'] : $url;
Barry Mienydd671972010-10-04 16:33:58 +0200233
Andrey Andreev426faa92012-04-03 19:03:58 +0300234 $path = empty($target['query']) ? $ppath : $ppath.'?'.$target['query'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000235
236 // Add the Trackback ID to the data string
237 if ($id = $this->get_id($url))
238 {
Andrey Andreev426faa92012-04-03 19:03:58 +0300239 $data = 'tb_id='.$id.'&'.$data;
Derek Allard2067d1a2008-11-13 22:59:24 +0000240 }
241
242 // Transfer the data
Andrey Andreev426faa92012-04-03 19:03:58 +0300243 fputs($fp, 'POST '.$path." HTTP/1.0\r\n");
244 fputs($fp, 'Host: '.$target['host']."\r\n");
245 fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n");
246 fputs($fp, 'Content-length: '.strlen($data)."\r\n");
247 fputs($fp, "Connection: close\r\n\r\n");
248 fputs($fp, $data);
Derek Allard2067d1a2008-11-13 22:59:24 +0000249
250 // Was it successful?
Barry Mienydd671972010-10-04 16:33:58 +0200251
Andrey Andreev426faa92012-04-03 19:03:58 +0300252 $this->response = '';
Pascal Kriete14287f32011-02-14 13:39:34 -0500253 while ( ! feof($fp))
Derek Allard2067d1a2008-11-13 22:59:24 +0000254 {
255 $this->response .= fgets($fp, 128);
256 }
257 @fclose($fp);
Barry Mienydd671972010-10-04 16:33:58 +0200258
Andrey Andreevd9cfa7b2011-12-25 16:32:59 +0200259 if (stripos($this->response, '<error>0</error>') === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000260 {
Andrey Andreev426faa92012-04-03 19:03:58 +0300261 $message = preg_match('/<message>(.*?)<\/message>/is', $this->response, $match) ? trim($match[1]) : 'An unknown error was encountered';
Derek Allard2067d1a2008-11-13 22:59:24 +0000262 $this->set_error($message);
263 return FALSE;
264 }
265
266 return TRUE;
267 }
Barry Mienydd671972010-10-04 16:33:58 +0200268
Derek Allard2067d1a2008-11-13 22:59:24 +0000269 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200270
Derek Allard2067d1a2008-11-13 22:59:24 +0000271 /**
272 * Extract Trackback URLs
273 *
274 * This function lets multiple trackbacks be sent.
275 * It takes a string of URLs (separated by comma or
276 * space) and puts each URL into an array
277 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000278 * @param string
279 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200280 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200281 public function extract_urls($urls)
Barry Mienydd671972010-10-04 16:33:58 +0200282 {
Andrey Andreev426faa92012-04-03 19:03:58 +0300283 // Remove the pesky white space and replace with a comma, then replace doubles.
284 $urls = str_replace(',,', ',', preg_replace('/\s*(\S+)\s*/', '\\1,', $urls));
Barry Mienydd671972010-10-04 16:33:58 +0200285
Derek Allard2067d1a2008-11-13 22:59:24 +0000286 // Remove any comma that might be at the end
Andrey Andreevd9cfa7b2011-12-25 16:32:59 +0200287 if (substr($urls, -1) === ',')
Derek Allard2067d1a2008-11-13 22:59:24 +0000288 {
289 $urls = substr($urls, 0, -1);
290 }
Barry Mienydd671972010-10-04 16:33:58 +0200291
Andrey Andreev426faa92012-04-03 19:03:58 +0300292 // Break into an array via commas and remove duplicates
293 $urls = array_unique(preg_split('/[,]/', $urls));
Barry Mienydd671972010-10-04 16:33:58 +0200294
Derek Allard2067d1a2008-11-13 22:59:24 +0000295 array_walk($urls, array($this, 'validate_url'));
Barry Mienydd671972010-10-04 16:33:58 +0200296
Derek Allard2067d1a2008-11-13 22:59:24 +0000297 return $urls;
298 }
Barry Mienydd671972010-10-04 16:33:58 +0200299
Derek Allard2067d1a2008-11-13 22:59:24 +0000300 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200301
Derek Allard2067d1a2008-11-13 22:59:24 +0000302 /**
303 * Validate URL
304 *
305 * Simply adds "http://" if missing
306 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000307 * @param string
Andrey Andreev426faa92012-04-03 19:03:58 +0300308 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200309 */
Andrey Andreev426faa92012-04-03 19:03:58 +0300310 public function validate_url(&$url)
Derek Allard2067d1a2008-11-13 22:59:24 +0000311 {
312 $url = trim($url);
313
Andrey Andreevd9cfa7b2011-12-25 16:32:59 +0200314 if (strpos($url, 'http') !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000315 {
Andrey Andreevd9cfa7b2011-12-25 16:32:59 +0200316 $url = 'http://'.$url;
Derek Allard2067d1a2008-11-13 22:59:24 +0000317 }
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 * Find the Trackback URL's ID
324 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000325 * @param string
326 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200327 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200328 public function get_id($url)
Barry Mienydd671972010-10-04 16:33:58 +0200329 {
Andrey Andreev426faa92012-04-03 19:03:58 +0300330 $tb_id = '';
Barry Mienydd671972010-10-04 16:33:58 +0200331
Robin Sowell76b369e2010-03-19 11:15:28 -0400332 if (strpos($url, '?') !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000333 {
334 $tb_array = explode('/', $url);
Derek Jones37f4b9c2011-07-01 17:56:50 -0500335 $tb_end = $tb_array[count($tb_array)-1];
Barry Mienydd671972010-10-04 16:33:58 +0200336
Derek Allard2067d1a2008-11-13 22:59:24 +0000337 if ( ! is_numeric($tb_end))
338 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500339 $tb_end = $tb_array[count($tb_array)-2];
Derek Allard2067d1a2008-11-13 22:59:24 +0000340 }
Barry Mienydd671972010-10-04 16:33:58 +0200341
Derek Allard2067d1a2008-11-13 22:59:24 +0000342 $tb_array = explode('=', $tb_end);
343 $tb_id = $tb_array[count($tb_array)-1];
344 }
345 else
346 {
Derek Jones1322ec52009-03-11 17:01:14 +0000347 $url = rtrim($url, '/');
Barry Mienydd671972010-10-04 16:33:58 +0200348
Derek Allard2067d1a2008-11-13 22:59:24 +0000349 $tb_array = explode('/', $url);
350 $tb_id = $tb_array[count($tb_array)-1];
Barry Mienydd671972010-10-04 16:33:58 +0200351
Derek Allard2067d1a2008-11-13 22:59:24 +0000352 if ( ! is_numeric($tb_id))
353 {
Andrey Andreev426faa92012-04-03 19:03:58 +0300354 $tb_id = $tb_array[count($tb_array)-2];
Derek Allard2067d1a2008-11-13 22:59:24 +0000355 }
Barry Mienydd671972010-10-04 16:33:58 +0200356 }
357
Andrey Andreev426faa92012-04-03 19:03:58 +0300358 return preg_match('/^[0-9]+$/', $tb_id) ? $tb_id : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000359 }
Barry Mienydd671972010-10-04 16:33:58 +0200360
Derek Allard2067d1a2008-11-13 22:59:24 +0000361 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200362
Derek Allard2067d1a2008-11-13 22:59:24 +0000363 /**
364 * Convert Reserved XML characters to Entities
365 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000366 * @param string
367 * @return string
368 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200369 public function convert_xml($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000370 {
371 $temp = '__TEMP_AMPERSANDS__';
Barry Mienydd671972010-10-04 16:33:58 +0200372
Andrey Andreev426faa92012-04-03 19:03:58 +0300373 $str = preg_replace(array('/&#(\d+);/', '/&(\w+);/'), $temp.'\\1;', $str);
Barry Mienydd671972010-10-04 16:33:58 +0200374
Andrey Andreev426faa92012-04-03 19:03:58 +0300375 $str = str_replace(array('&', '<', '>', '"', "'", '-'),
376 array('&amp;', '&lt;', '&gt;', '&quot;', '&#39;', '&#45;'),
377 $str);
Barry Mienydd671972010-10-04 16:33:58 +0200378
Andrey Andreev426faa92012-04-03 19:03:58 +0300379 return preg_replace(array('/'.$temp.'(\d+);/', '/'.$temp.'(\w+);/'), array('&#\\1;', '&\\1;'), $str);
Barry Mienydd671972010-10-04 16:33:58 +0200380 }
381
Derek Allard2067d1a2008-11-13 22:59:24 +0000382 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200383
Derek Allard2067d1a2008-11-13 22:59:24 +0000384 /**
385 * Character limiter
386 *
387 * Limits the string based on the character count. Will preserve complete words.
388 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000389 * @param string
Andrey Andreev426faa92012-04-03 19:03:58 +0300390 * @param int
Derek Allard2067d1a2008-11-13 22:59:24 +0000391 * @param string
392 * @return string
393 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200394 public function limit_characters($str, $n = 500, $end_char = '&#8230;')
Derek Allard2067d1a2008-11-13 22:59:24 +0000395 {
396 if (strlen($str) < $n)
397 {
398 return $str;
399 }
Barry Mienydd671972010-10-04 16:33:58 +0200400
Andrey Andreev426faa92012-04-03 19:03:58 +0300401 $str = preg_replace('/\s+/', ' ', str_replace(array("\r\n", "\r", "\n"), ' ', $str));
Barry Mienydd671972010-10-04 16:33:58 +0200402
Derek Allard2067d1a2008-11-13 22:59:24 +0000403 if (strlen($str) <= $n)
404 {
405 return $str;
406 }
Barry Mienydd671972010-10-04 16:33:58 +0200407
Andrey Andreevd9cfa7b2011-12-25 16:32:59 +0200408 $out = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000409 foreach (explode(' ', trim($str)) as $val)
410 {
Barry Mienydd671972010-10-04 16:33:58 +0200411 $out .= $val.' ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000412 if (strlen($out) >= $n)
413 {
Andrey Andreevd9cfa7b2011-12-25 16:32:59 +0200414 return rtrim($out).$end_char;
Barry Mienydd671972010-10-04 16:33:58 +0200415 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000416 }
417 }
Barry Mienydd671972010-10-04 16:33:58 +0200418
Derek Allard2067d1a2008-11-13 22:59:24 +0000419 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200420
Derek Allard2067d1a2008-11-13 22:59:24 +0000421 /**
422 * High ASCII to Entities
423 *
424 * Converts Hight ascii text and MS Word special chars
425 * to character entities
426 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000427 * @param string
428 * @return string
429 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200430 public function convert_ascii($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000431 {
Barry Mienydd671972010-10-04 16:33:58 +0200432 $count = 1;
433 $out = '';
434 $temp = array();
435
436 for ($i = 0, $s = strlen($str); $i < $s; $i++)
437 {
438 $ordinal = ord($str[$i]);
439
440 if ($ordinal < 128)
441 {
442 $out .= $str[$i];
443 }
444 else
445 {
Andrey Andreevd9cfa7b2011-12-25 16:32:59 +0200446 if (count($temp) === 0)
Barry Mienydd671972010-10-04 16:33:58 +0200447 {
448 $count = ($ordinal < 224) ? 2 : 3;
449 }
450
451 $temp[] = $ordinal;
452
Andrey Andreevd9cfa7b2011-12-25 16:32:59 +0200453 if (count($temp) === $count)
Barry Mienydd671972010-10-04 16:33:58 +0200454 {
Andrey Andreev426faa92012-04-03 19:03:58 +0300455 $number = ($count === 3)
456 ? (($temp[0] % 16) * 4096) + (($temp[1] % 64) * 64) + ($temp[2] % 64)
457 : (($temp[0] % 32) * 64) + ($temp[1] % 64);
Barry Mienydd671972010-10-04 16:33:58 +0200458
459 $out .= '&#'.$number.';';
460 $count = 1;
461 $temp = array();
462 }
463 }
464 }
465
466 return $out;
Derek Allard2067d1a2008-11-13 22:59:24 +0000467 }
Barry Mienydd671972010-10-04 16:33:58 +0200468
Derek Allard2067d1a2008-11-13 22:59:24 +0000469 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200470
Derek Allard2067d1a2008-11-13 22:59:24 +0000471 /**
472 * Set error message
473 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000474 * @param string
475 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200476 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200477 public function set_error($msg)
Derek Allard2067d1a2008-11-13 22:59:24 +0000478 {
479 log_message('error', $msg);
480 $this->error_msg[] = $msg;
481 }
Barry Mienydd671972010-10-04 16:33:58 +0200482
Derek Allard2067d1a2008-11-13 22:59:24 +0000483 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200484
Derek Allard2067d1a2008-11-13 22:59:24 +0000485 /**
486 * Show error messages
487 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000488 * @param string
489 * @param string
490 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200491 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200492 public function display_errors($open = '<p>', $close = '</p>')
Barry Mienydd671972010-10-04 16:33:58 +0200493 {
Andrey Andreev426faa92012-04-03 19:03:58 +0300494 return (count($this->error_msg) > 0) ? $open.implode($close.$open, $this->error_msg).$close : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000495 }
496
497}
Derek Allard2067d1a2008-11-13 22:59:24 +0000498
499/* End of file Trackback.php */
Andrey Andreev426faa92012-04-03 19:03:58 +0300500/* Location: ./system/libraries/Trackback.php */