blob: 228794dd7a1928593d407bfe0954447c3a0936a8 [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Andrey Andreevfe9309d2015-01-09 17:48:58 +02005 * An open source application development framework for PHP
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +02007 * This content is released under the MIT License (MIT)
Andrey Andreev4eea9892011-12-19 12:05:41 +02008 *
Andrey Andreevcce6bd12018-01-09 11:32:02 +02009 * Copyright (c) 2014 - 2018, British Columbia Institute of Technology
Andrey Andreev4eea9892011-12-19 12:05:41 +020010 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020011 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
Derek Jonesf4a4bd82011-10-20 12:18:42 -050017 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020018 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 * THE SOFTWARE.
28 *
29 * @package CodeIgniter
30 * @author EllisLab Dev Team
Andrey Andreev1924e872016-01-11 12:55:34 +020031 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
Andrey Andreevcce6bd12018-01-09 11:32:02 +020032 * @copyright Copyright (c) 2014 - 2018, British Columbia Institute of Technology (http://bcit.ca/)
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020033 * @license http://opensource.org/licenses/MIT MIT License
Andrey Andreevbd202c92016-01-11 12:50:18 +020034 * @link https://codeigniter.com
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020035 * @since Version 1.0.0
Derek Allard2067d1a2008-11-13 22:59:24 +000036 * @filesource
37 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020038defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000039
Derek Allard2067d1a2008-11-13 22:59:24 +000040/**
41 * Trackback Class
42 *
43 * Trackback Sending/Receiving Class
44 *
45 * @package CodeIgniter
46 * @subpackage Libraries
47 * @category Trackbacks
Derek Jonesf4a4bd82011-10-20 12:18:42 -050048 * @author EllisLab Dev Team
Andrey Andreevbd202c92016-01-11 12:50:18 +020049 * @link https://codeigniter.com/user_guide/libraries/trackback.html
Derek Allard2067d1a2008-11-13 22:59:24 +000050 */
51class CI_Trackback {
Barry Mienydd671972010-10-04 16:33:58 +020052
Andrey Andreev0fa95bd2012-11-01 23:33:14 +020053 /**
54 * Character set
55 *
56 * @var string
57 */
Andrey Andreev9a0e0c72014-04-09 15:10:27 +030058 public $charset = 'UTF-8';
Andrey Andreev0fa95bd2012-11-01 23:33:14 +020059
60 /**
61 * Trackback data
62 *
63 * @var array
64 */
Andrey Andreev9a0e0c72014-04-09 15:10:27 +030065 public $data = array(
66 'url' => '',
67 'title' => '',
68 'excerpt' => '',
69 'blog_name' => '',
70 'charset' => ''
71 );
Andrey Andreev0fa95bd2012-11-01 23:33:14 +020072
73 /**
74 * Convert ASCII flag
75 *
76 * Whether to convert high-ASCII and MS Word
77 * characters to HTML entities.
78 *
79 * @var bool
80 */
Andrey Andreev9a0e0c72014-04-09 15:10:27 +030081 public $convert_ascii = TRUE;
Andrey Andreev0fa95bd2012-11-01 23:33:14 +020082
83 /**
84 * Response
85 *
86 * @var string
87 */
Andrey Andreev9a0e0c72014-04-09 15:10:27 +030088 public $response = '';
Andrey Andreev0fa95bd2012-11-01 23:33:14 +020089
90 /**
91 * Error messages list
92 *
93 * @var string[]
94 */
Andrey Andreev9a0e0c72014-04-09 15:10:27 +030095 public $error_msg = array();
Andrey Andreev0fa95bd2012-11-01 23:33:14 +020096
97 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +000098
Andrey Andreev5fd3ae82012-10-24 14:55:35 +030099 /**
100 * Constructor
101 *
102 * @return void
103 */
Greg Akera9263282010-11-10 15:26:43 -0600104 public function __construct()
Derek Allard2067d1a2008-11-13 22:59:24 +0000105 {
Andrey Andreev90726b82015-01-20 12:39:22 +0200106 log_message('info', 'Trackback Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000107 }
Barry Mienydd671972010-10-04 16:33:58 +0200108
Derek Allard2067d1a2008-11-13 22:59:24 +0000109 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200110
Derek Allard2067d1a2008-11-13 22:59:24 +0000111 /**
112 * Send Trackback
113 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000114 * @param array
115 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200116 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200117 public function send($tb_data)
Barry Mienydd671972010-10-04 16:33:58 +0200118 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000119 if ( ! is_array($tb_data))
120 {
121 $this->set_error('The send() method must be passed an array');
122 return FALSE;
123 }
Barry Mienydd671972010-10-04 16:33:58 +0200124
Derek Allard2067d1a2008-11-13 22:59:24 +0000125 // Pre-process the Trackback Data
126 foreach (array('url', 'title', 'excerpt', 'blog_name', 'ping_url') as $item)
127 {
128 if ( ! isset($tb_data[$item]))
129 {
130 $this->set_error('Required item missing: '.$item);
131 return FALSE;
132 }
Barry Mienydd671972010-10-04 16:33:58 +0200133
Derek Allard2067d1a2008-11-13 22:59:24 +0000134 switch ($item)
135 {
Andrey Andreev9a0e0c72014-04-09 15:10:27 +0300136 case 'ping_url':
137 $$item = $this->extract_urls($tb_data[$item]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000138 break;
Andrey Andreev9a0e0c72014-04-09 15:10:27 +0300139 case 'excerpt':
140 $$item = $this->limit_characters($this->convert_xml(strip_tags(stripslashes($tb_data[$item]))));
Derek Allard2067d1a2008-11-13 22:59:24 +0000141 break;
Andrey Andreev9a0e0c72014-04-09 15:10:27 +0300142 case 'url':
143 $$item = str_replace('&#45;', '-', $this->convert_xml(strip_tags(stripslashes($tb_data[$item]))));
Derek Allard2067d1a2008-11-13 22:59:24 +0000144 break;
Andrey Andreev9a0e0c72014-04-09 15:10:27 +0300145 default:
146 $$item = $this->convert_xml(strip_tags(stripslashes($tb_data[$item])));
Derek Allard2067d1a2008-11-13 22:59:24 +0000147 break;
148 }
149
150 // Convert High ASCII Characters
Andrey Andreev9a0e0c72014-04-09 15:10:27 +0300151 if ($this->convert_ascii === TRUE && in_array($item, array('excerpt', 'title', 'blog_name'), TRUE))
Derek Allard2067d1a2008-11-13 22:59:24 +0000152 {
Andrey Andreevd9cfa7b2011-12-25 16:32:59 +0200153 $$item = $this->convert_ascii($$item);
Derek Allard2067d1a2008-11-13 22:59:24 +0000154 }
155 }
156
157 // Build the Trackback data string
Andrey Andreev426faa92012-04-03 19:03:58 +0300158 $charset = isset($tb_data['charset']) ? $tb_data['charset'] : $this->charset;
Barry Mienydd671972010-10-04 16:33:58 +0200159
Andrey Andreev426faa92012-04-03 19:03:58 +0300160 $data = 'url='.rawurlencode($url).'&title='.rawurlencode($title).'&blog_name='.rawurlencode($blog_name)
161 .'&excerpt='.rawurlencode($excerpt).'&charset='.rawurlencode($charset);
Barry Mienydd671972010-10-04 16:33:58 +0200162
Derek Allard2067d1a2008-11-13 22:59:24 +0000163 // Send Trackback(s)
164 $return = TRUE;
165 if (count($ping_url) > 0)
166 {
167 foreach ($ping_url as $url)
168 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100169 if ($this->process($url, $data) === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000170 {
171 $return = FALSE;
172 }
Barry Mienydd671972010-10-04 16:33:58 +0200173 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000174 }
175
176 return $return;
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 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -0500182 * Receive Trackback Data
Derek Allard2067d1a2008-11-13 22:59:24 +0000183 *
184 * This function simply validates the incoming TB data.
Derek Jones5052e272010-03-02 22:53:38 -0600185 * It returns FALSE on failure and TRUE on success.
Derek Allard2067d1a2008-11-13 22:59:24 +0000186 * If the data is valid it is set to the $this->data array
187 * so that it can be inserted into a database.
188 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000189 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200190 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200191 public function receive()
Barry Mienydd671972010-10-04 16:33:58 +0200192 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000193 foreach (array('url', 'title', 'blog_name', 'excerpt') as $val)
194 {
Andrey Andreev5036c9c2012-06-04 15:34:56 +0300195 if (empty($_POST[$val]))
Derek Allard2067d1a2008-11-13 22:59:24 +0000196 {
197 $this->set_error('The following required POST variable is missing: '.$val);
198 return FALSE;
199 }
Barry Mienydd671972010-10-04 16:33:58 +0200200
Andrey Andreev426faa92012-04-03 19:03:58 +0300201 $this->data['charset'] = isset($_POST['charset']) ? strtoupper(trim($_POST['charset'])) : 'auto';
Barry Mienydd671972010-10-04 16:33:58 +0200202
Alex Bilbied261b1e2012-06-02 11:12:16 +0100203 if ($val !== 'url' && MB_ENABLED === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000204 {
Andrey Andreevbe1496d2014-02-11 22:48:45 +0200205 if (MB_ENABLED === TRUE)
206 {
207 $_POST[$val] = mb_convert_encoding($_POST[$val], $this->charset, $this->data['charset']);
208 }
209 elseif (ICONV_ENABLED === TRUE)
210 {
211 $_POST[$val] = @iconv($this->data['charset'], $this->charset.'//IGNORE', $_POST[$val]);
212 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000213 }
Barry Mienydd671972010-10-04 16:33:58 +0200214
Alex Bilbied261b1e2012-06-02 11:12:16 +0100215 $_POST[$val] = ($val !== 'url') ? $this->convert_xml(strip_tags($_POST[$val])) : strip_tags($_POST[$val]);
Barry Mienydd671972010-10-04 16:33:58 +0200216
Alex Bilbied261b1e2012-06-02 11:12:16 +0100217 if ($val === 'excerpt')
Derek Allard2067d1a2008-11-13 22:59:24 +0000218 {
219 $_POST['excerpt'] = $this->limit_characters($_POST['excerpt']);
220 }
Barry Mienydd671972010-10-04 16:33:58 +0200221
Derek Allard2067d1a2008-11-13 22:59:24 +0000222 $this->data[$val] = $_POST[$val];
223 }
224
225 return TRUE;
Barry Mienydd671972010-10-04 16:33:58 +0200226 }
227
Derek Allard2067d1a2008-11-13 22:59:24 +0000228 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200229
Derek Allard2067d1a2008-11-13 22:59:24 +0000230 /**
231 * Send Trackback Error Message
232 *
Andrey Andreev426faa92012-04-03 19:03:58 +0300233 * Allows custom errors to be set. By default it
Derek Allard2067d1a2008-11-13 22:59:24 +0000234 * sends the "incomplete information" error, as that's
235 * the most common one.
236 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000237 * @param string
238 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200239 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200240 public function send_error($message = 'Incomplete Information')
Derek Allard2067d1a2008-11-13 22:59:24 +0000241 {
Daniel Hunsaker8626e932013-03-04 05:14:22 -0700242 exit('<?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 +0000243 }
Barry Mienydd671972010-10-04 16:33:58 +0200244
Derek Allard2067d1a2008-11-13 22:59:24 +0000245 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200246
Derek Allard2067d1a2008-11-13 22:59:24 +0000247 /**
248 * Send Trackback Success Message
249 *
250 * This should be called when a trackback has been
251 * successfully received and inserted.
252 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000253 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200254 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200255 public function send_success()
Derek Allard2067d1a2008-11-13 22:59:24 +0000256 {
Daniel Hunsaker8626e932013-03-04 05:14:22 -0700257 exit('<?xml version="1.0" encoding="utf-8"?'.">\n<response>\n<error>0</error>\n</response>");
Derek Allard2067d1a2008-11-13 22:59:24 +0000258 }
Barry Mienydd671972010-10-04 16:33:58 +0200259
Derek Allard2067d1a2008-11-13 22:59:24 +0000260 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200261
Derek Allard2067d1a2008-11-13 22:59:24 +0000262 /**
263 * Fetch a particular item
264 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000265 * @param string
266 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200267 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200268 public function data($item)
Derek Allard2067d1a2008-11-13 22:59:24 +0000269 {
Andrey Andreev426faa92012-04-03 19:03:58 +0300270 return isset($this->data[$item]) ? $this->data[$item] : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000271 }
272
273 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200274
Derek Allard2067d1a2008-11-13 22:59:24 +0000275 /**
276 * Process Trackback
277 *
278 * Opens a socket connection and passes the data to
Andrey Andreev426faa92012-04-03 19:03:58 +0300279 * the server. Returns TRUE on success, FALSE on failure
Derek Allard2067d1a2008-11-13 22:59:24 +0000280 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000281 * @param string
282 * @param string
283 * @return bool
Barry Mienydd671972010-10-04 16:33:58 +0200284 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200285 public function process($url, $data)
Derek Allard2067d1a2008-11-13 22:59:24 +0000286 {
287 $target = parse_url($url);
Barry Mienydd671972010-10-04 16:33:58 +0200288
Derek Allard2067d1a2008-11-13 22:59:24 +0000289 // Open the socket
290 if ( ! $fp = @fsockopen($target['host'], 80))
291 {
292 $this->set_error('Invalid Connection: '.$url);
293 return FALSE;
294 }
295
296 // Build the path
Andrey Andreev5b5401a2013-01-10 17:38:08 +0200297 $path = isset($target['path']) ? $target['path'] : $url;
298 empty($target['query']) OR $path .= '?'.$target['query'];
Derek Allard2067d1a2008-11-13 22:59:24 +0000299
300 // Add the Trackback ID to the data string
301 if ($id = $this->get_id($url))
302 {
Andrey Andreev426faa92012-04-03 19:03:58 +0300303 $data = 'tb_id='.$id.'&'.$data;
Derek Allard2067d1a2008-11-13 22:59:24 +0000304 }
305
306 // Transfer the data
Andrey Andreev426faa92012-04-03 19:03:58 +0300307 fputs($fp, 'POST '.$path." HTTP/1.0\r\n");
308 fputs($fp, 'Host: '.$target['host']."\r\n");
309 fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n");
310 fputs($fp, 'Content-length: '.strlen($data)."\r\n");
311 fputs($fp, "Connection: close\r\n\r\n");
312 fputs($fp, $data);
Derek Allard2067d1a2008-11-13 22:59:24 +0000313
314 // Was it successful?
Barry Mienydd671972010-10-04 16:33:58 +0200315
Andrey Andreev426faa92012-04-03 19:03:58 +0300316 $this->response = '';
Pascal Kriete14287f32011-02-14 13:39:34 -0500317 while ( ! feof($fp))
Derek Allard2067d1a2008-11-13 22:59:24 +0000318 {
319 $this->response .= fgets($fp, 128);
320 }
321 @fclose($fp);
Barry Mienydd671972010-10-04 16:33:58 +0200322
Andrey Andreevd9cfa7b2011-12-25 16:32:59 +0200323 if (stripos($this->response, '<error>0</error>') === FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000324 {
Andrey Andreev9a0e0c72014-04-09 15:10:27 +0300325 $message = preg_match('/<message>(.*?)<\/message>/is', $this->response, $match)
326 ? trim($match[1])
327 : 'An unknown error was encountered';
Derek Allard2067d1a2008-11-13 22:59:24 +0000328 $this->set_error($message);
329 return FALSE;
330 }
331
332 return TRUE;
333 }
Barry Mienydd671972010-10-04 16:33:58 +0200334
Derek Allard2067d1a2008-11-13 22:59:24 +0000335 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200336
Derek Allard2067d1a2008-11-13 22:59:24 +0000337 /**
338 * Extract Trackback URLs
339 *
340 * This function lets multiple trackbacks be sent.
341 * It takes a string of URLs (separated by comma or
342 * space) and puts each URL into an array
343 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000344 * @param string
345 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200346 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200347 public function extract_urls($urls)
Barry Mienydd671972010-10-04 16:33:58 +0200348 {
Andrey Andreev426faa92012-04-03 19:03:58 +0300349 // Remove the pesky white space and replace with a comma, then replace doubles.
350 $urls = str_replace(',,', ',', preg_replace('/\s*(\S+)\s*/', '\\1,', $urls));
Barry Mienydd671972010-10-04 16:33:58 +0200351
Andrey Andreev426faa92012-04-03 19:03:58 +0300352 // Break into an array via commas and remove duplicates
Andrey Andreev9a0e0c72014-04-09 15:10:27 +0300353 $urls = array_unique(preg_split('/[,]/', rtrim($urls, ',')));
Barry Mienydd671972010-10-04 16:33:58 +0200354
Derek Allard2067d1a2008-11-13 22:59:24 +0000355 array_walk($urls, array($this, 'validate_url'));
Derek Allard2067d1a2008-11-13 22:59:24 +0000356 return $urls;
357 }
Barry Mienydd671972010-10-04 16:33:58 +0200358
Derek Allard2067d1a2008-11-13 22:59:24 +0000359 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200360
Derek Allard2067d1a2008-11-13 22:59:24 +0000361 /**
362 * Validate URL
363 *
364 * Simply adds "http://" if missing
365 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000366 * @param string
Andrey Andreev426faa92012-04-03 19:03:58 +0300367 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200368 */
Andrey Andreev426faa92012-04-03 19:03:58 +0300369 public function validate_url(&$url)
Derek Allard2067d1a2008-11-13 22:59:24 +0000370 {
371 $url = trim($url);
372
Andrey Andreeve33c82d2016-08-10 15:18:31 +0300373 if (stripos($url, 'http') !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000374 {
Andrey Andreevd9cfa7b2011-12-25 16:32:59 +0200375 $url = 'http://'.$url;
Derek Allard2067d1a2008-11-13 22:59:24 +0000376 }
377 }
Barry Mienydd671972010-10-04 16:33:58 +0200378
Derek Allard2067d1a2008-11-13 22:59:24 +0000379 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200380
Derek Allard2067d1a2008-11-13 22:59:24 +0000381 /**
382 * Find the Trackback URL's ID
383 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000384 * @param string
385 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200386 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200387 public function get_id($url)
Barry Mienydd671972010-10-04 16:33:58 +0200388 {
Andrey Andreev426faa92012-04-03 19:03:58 +0300389 $tb_id = '';
Barry Mienydd671972010-10-04 16:33:58 +0200390
Robin Sowell76b369e2010-03-19 11:15:28 -0400391 if (strpos($url, '?') !== FALSE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000392 {
393 $tb_array = explode('/', $url);
Derek Jones37f4b9c2011-07-01 17:56:50 -0500394 $tb_end = $tb_array[count($tb_array)-1];
Barry Mienydd671972010-10-04 16:33:58 +0200395
Derek Allard2067d1a2008-11-13 22:59:24 +0000396 if ( ! is_numeric($tb_end))
397 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500398 $tb_end = $tb_array[count($tb_array)-2];
Derek Allard2067d1a2008-11-13 22:59:24 +0000399 }
Barry Mienydd671972010-10-04 16:33:58 +0200400
Derek Allard2067d1a2008-11-13 22:59:24 +0000401 $tb_array = explode('=', $tb_end);
402 $tb_id = $tb_array[count($tb_array)-1];
403 }
404 else
405 {
Derek Jones1322ec52009-03-11 17:01:14 +0000406 $url = rtrim($url, '/');
Barry Mienydd671972010-10-04 16:33:58 +0200407
Derek Allard2067d1a2008-11-13 22:59:24 +0000408 $tb_array = explode('/', $url);
409 $tb_id = $tb_array[count($tb_array)-1];
Barry Mienydd671972010-10-04 16:33:58 +0200410
Derek Allard2067d1a2008-11-13 22:59:24 +0000411 if ( ! is_numeric($tb_id))
412 {
Andrey Andreev426faa92012-04-03 19:03:58 +0300413 $tb_id = $tb_array[count($tb_array)-2];
Derek Allard2067d1a2008-11-13 22:59:24 +0000414 }
Barry Mienydd671972010-10-04 16:33:58 +0200415 }
416
Andrey Andreev7a7ad782012-11-12 17:21:01 +0200417 return ctype_digit((string) $tb_id) ? $tb_id : FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000418 }
Barry Mienydd671972010-10-04 16:33:58 +0200419
Derek Allard2067d1a2008-11-13 22:59:24 +0000420 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200421
Derek Allard2067d1a2008-11-13 22:59:24 +0000422 /**
423 * Convert Reserved XML characters to Entities
424 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000425 * @param string
426 * @return string
427 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200428 public function convert_xml($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000429 {
430 $temp = '__TEMP_AMPERSANDS__';
Barry Mienydd671972010-10-04 16:33:58 +0200431
Andrey Andreev426faa92012-04-03 19:03:58 +0300432 $str = preg_replace(array('/&#(\d+);/', '/&(\w+);/'), $temp.'\\1;', $str);
Barry Mienydd671972010-10-04 16:33:58 +0200433
Andrey Andreev426faa92012-04-03 19:03:58 +0300434 $str = str_replace(array('&', '<', '>', '"', "'", '-'),
435 array('&amp;', '&lt;', '&gt;', '&quot;', '&#39;', '&#45;'),
436 $str);
Barry Mienydd671972010-10-04 16:33:58 +0200437
Andrey Andreev426faa92012-04-03 19:03:58 +0300438 return preg_replace(array('/'.$temp.'(\d+);/', '/'.$temp.'(\w+);/'), array('&#\\1;', '&\\1;'), $str);
Barry Mienydd671972010-10-04 16:33:58 +0200439 }
440
Derek Allard2067d1a2008-11-13 22:59:24 +0000441 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200442
Derek Allard2067d1a2008-11-13 22:59:24 +0000443 /**
444 * Character limiter
445 *
446 * Limits the string based on the character count. Will preserve complete words.
447 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000448 * @param string
Andrey Andreev426faa92012-04-03 19:03:58 +0300449 * @param int
Derek Allard2067d1a2008-11-13 22:59:24 +0000450 * @param string
451 * @return string
452 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200453 public function limit_characters($str, $n = 500, $end_char = '&#8230;')
Derek Allard2067d1a2008-11-13 22:59:24 +0000454 {
455 if (strlen($str) < $n)
456 {
457 return $str;
458 }
Barry Mienydd671972010-10-04 16:33:58 +0200459
Andrey Andreev426faa92012-04-03 19:03:58 +0300460 $str = preg_replace('/\s+/', ' ', str_replace(array("\r\n", "\r", "\n"), ' ', $str));
Barry Mienydd671972010-10-04 16:33:58 +0200461
Derek Allard2067d1a2008-11-13 22:59:24 +0000462 if (strlen($str) <= $n)
463 {
464 return $str;
465 }
Barry Mienydd671972010-10-04 16:33:58 +0200466
Andrey Andreevd9cfa7b2011-12-25 16:32:59 +0200467 $out = '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000468 foreach (explode(' ', trim($str)) as $val)
469 {
Barry Mienydd671972010-10-04 16:33:58 +0200470 $out .= $val.' ';
Derek Allard2067d1a2008-11-13 22:59:24 +0000471 if (strlen($out) >= $n)
472 {
Andrey Andreevd9cfa7b2011-12-25 16:32:59 +0200473 return rtrim($out).$end_char;
Barry Mienydd671972010-10-04 16:33:58 +0200474 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000475 }
476 }
Barry Mienydd671972010-10-04 16:33:58 +0200477
Derek Allard2067d1a2008-11-13 22:59:24 +0000478 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200479
Derek Allard2067d1a2008-11-13 22:59:24 +0000480 /**
481 * High ASCII to Entities
482 *
483 * Converts Hight ascii text and MS Word special chars
484 * to character entities
485 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000486 * @param string
487 * @return string
488 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200489 public function convert_ascii($str)
Derek Allard2067d1a2008-11-13 22:59:24 +0000490 {
Barry Mienydd671972010-10-04 16:33:58 +0200491 $count = 1;
492 $out = '';
493 $temp = array();
494
495 for ($i = 0, $s = strlen($str); $i < $s; $i++)
496 {
497 $ordinal = ord($str[$i]);
498
499 if ($ordinal < 128)
500 {
501 $out .= $str[$i];
502 }
503 else
504 {
Andrey Andreevd9cfa7b2011-12-25 16:32:59 +0200505 if (count($temp) === 0)
Barry Mienydd671972010-10-04 16:33:58 +0200506 {
507 $count = ($ordinal < 224) ? 2 : 3;
508 }
509
510 $temp[] = $ordinal;
511
Andrey Andreevd9cfa7b2011-12-25 16:32:59 +0200512 if (count($temp) === $count)
Barry Mienydd671972010-10-04 16:33:58 +0200513 {
Andrey Andreev426faa92012-04-03 19:03:58 +0300514 $number = ($count === 3)
515 ? (($temp[0] % 16) * 4096) + (($temp[1] % 64) * 64) + ($temp[2] % 64)
516 : (($temp[0] % 32) * 64) + ($temp[1] % 64);
Barry Mienydd671972010-10-04 16:33:58 +0200517
518 $out .= '&#'.$number.';';
519 $count = 1;
520 $temp = array();
521 }
522 }
523 }
524
525 return $out;
Derek Allard2067d1a2008-11-13 22:59:24 +0000526 }
Barry Mienydd671972010-10-04 16:33:58 +0200527
Derek Allard2067d1a2008-11-13 22:59:24 +0000528 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200529
Derek Allard2067d1a2008-11-13 22:59:24 +0000530 /**
531 * Set error message
532 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000533 * @param string
534 * @return void
Barry Mienydd671972010-10-04 16:33:58 +0200535 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200536 public function set_error($msg)
Derek Allard2067d1a2008-11-13 22:59:24 +0000537 {
538 log_message('error', $msg);
539 $this->error_msg[] = $msg;
540 }
Barry Mienydd671972010-10-04 16:33:58 +0200541
Derek Allard2067d1a2008-11-13 22:59:24 +0000542 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200543
Derek Allard2067d1a2008-11-13 22:59:24 +0000544 /**
545 * Show error messages
546 *
Derek Allard2067d1a2008-11-13 22:59:24 +0000547 * @param string
548 * @param string
549 * @return string
Barry Mienydd671972010-10-04 16:33:58 +0200550 */
Andrey Andreev4eea9892011-12-19 12:05:41 +0200551 public function display_errors($open = '<p>', $close = '</p>')
Barry Mienydd671972010-10-04 16:33:58 +0200552 {
Andrey Andreev426faa92012-04-03 19:03:58 +0300553 return (count($this->error_msg) > 0) ? $open.implode($close.$open, $this->error_msg).$close : '';
Derek Allard2067d1a2008-11-13 22:59:24 +0000554 }
555
556}