Andrey Andreev | d9cfa7b | 2011-12-25 16:32:59 +0200 | [diff] [blame] | 1 | <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 2 | /** |
| 3 | * CodeIgniter |
| 4 | * |
Phil Sturgeon | 07c1ac8 | 2012-03-09 17:03:37 +0000 | [diff] [blame] | 5 | * An open source application development framework for PHP 5.2.4 or newer |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 6 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 7 | * NOTICE OF LICENSE |
Andrey Andreev | 4eea989 | 2011-12-19 12:05:41 +0200 | [diff] [blame] | 8 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 9 | * Licensed under the Open Software License version 3.0 |
Andrey Andreev | 4eea989 | 2011-12-19 12:05:41 +0200 | [diff] [blame] | 10 | * |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 11 | * 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 19 | * @package CodeIgniter |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 20 | * @author EllisLab Dev Team |
Greg Aker | 0defe5d | 2012-01-01 18:46:41 -0600 | [diff] [blame] | 21 | * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/) |
Derek Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 22 | * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 23 | * @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 Jones | f4a4bd8 | 2011-10-20 12:18:42 -0500 | [diff] [blame] | 38 | * @author EllisLab Dev Team |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 39 | * @link http://codeigniter.com/user_guide/libraries/trackback.html |
| 40 | */ |
| 41 | class CI_Trackback { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 42 | |
Andrey Andreev | 3a45957 | 2011-12-21 11:23:11 +0200 | [diff] [blame] | 43 | 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 Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 49 | |
Greg Aker | a926328 | 2010-11-10 15:26:43 -0600 | [diff] [blame] | 50 | public function __construct() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 51 | { |
| 52 | log_message('debug', "Trackback Class Initialized"); |
| 53 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 54 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 55 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 56 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 57 | /** |
| 58 | * Send Trackback |
| 59 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 60 | * @param array |
| 61 | * @return bool |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 62 | */ |
Andrey Andreev | 4eea989 | 2011-12-19 12:05:41 +0200 | [diff] [blame] | 63 | public function send($tb_data) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 64 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 65 | if ( ! is_array($tb_data)) |
| 66 | { |
| 67 | $this->set_error('The send() method must be passed an array'); |
| 68 | return FALSE; |
| 69 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 70 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 71 | // Pre-process the Trackback Data |
| 72 | foreach (array('url', 'title', 'excerpt', 'blog_name', 'ping_url') as $item) |
| 73 | { |
| 74 | if ( ! isset($tb_data[$item])) |
| 75 | { |
| 76 | $this->set_error('Required item missing: '.$item); |
| 77 | return FALSE; |
| 78 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 79 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 80 | switch ($item) |
| 81 | { |
| 82 | case 'ping_url' : $$item = $this->extract_urls($tb_data[$item]); |
| 83 | break; |
| 84 | case 'excerpt' : $$item = $this->limit_characters($this->convert_xml(strip_tags(stripslashes($tb_data[$item])))); |
| 85 | break; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 86 | case 'url' : $$item = str_replace('-', '-', $this->convert_xml(strip_tags(stripslashes($tb_data[$item])))); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 87 | break; |
| 88 | default : $$item = $this->convert_xml(strip_tags(stripslashes($tb_data[$item]))); |
| 89 | break; |
| 90 | } |
| 91 | |
| 92 | // Convert High ASCII Characters |
Andrey Andreev | d9cfa7b | 2011-12-25 16:32:59 +0200 | [diff] [blame] | 93 | if ($this->convert_ascii == TRUE && in_array($item, array('excerpt', 'title', 'blog_name'))) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 94 | { |
Andrey Andreev | d9cfa7b | 2011-12-25 16:32:59 +0200 | [diff] [blame] | 95 | $$item = $this->convert_ascii($$item); |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 96 | } |
| 97 | } |
| 98 | |
| 99 | // Build the Trackback data string |
| 100 | $charset = ( ! isset($tb_data['charset'])) ? $this->charset : $tb_data['charset']; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 101 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 102 | $data = "url=".rawurlencode($url)."&title=".rawurlencode($title)."&blog_name=".rawurlencode($blog_name)."&excerpt=".rawurlencode($excerpt)."&charset=".rawurlencode($charset); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 103 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 104 | // Send Trackback(s) |
| 105 | $return = TRUE; |
| 106 | if (count($ping_url) > 0) |
| 107 | { |
| 108 | foreach ($ping_url as $url) |
| 109 | { |
| 110 | if ($this->process($url, $data) == FALSE) |
| 111 | { |
| 112 | $return = FALSE; |
| 113 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 114 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | return $return; |
| 118 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 119 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 120 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 121 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 122 | /** |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 123 | * Receive Trackback Data |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 124 | * |
| 125 | * This function simply validates the incoming TB data. |
Derek Jones | 5052e27 | 2010-03-02 22:53:38 -0600 | [diff] [blame] | 126 | * It returns FALSE on failure and TRUE on success. |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 127 | * If the data is valid it is set to the $this->data array |
| 128 | * so that it can be inserted into a database. |
| 129 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 130 | * @return bool |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 131 | */ |
Andrey Andreev | 4eea989 | 2011-12-19 12:05:41 +0200 | [diff] [blame] | 132 | public function receive() |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 133 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 134 | foreach (array('url', 'title', 'blog_name', 'excerpt') as $val) |
| 135 | { |
| 136 | if ( ! isset($_POST[$val]) OR $_POST[$val] == '') |
| 137 | { |
| 138 | $this->set_error('The following required POST variable is missing: '.$val); |
| 139 | return FALSE; |
| 140 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 141 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 142 | $this->data['charset'] = ( ! isset($_POST['charset'])) ? 'auto' : strtoupper(trim($_POST['charset'])); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 143 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 144 | if ($val != 'url' && function_exists('mb_convert_encoding')) |
| 145 | { |
| 146 | $_POST[$val] = mb_convert_encoding($_POST[$val], $this->charset, $this->data['charset']); |
| 147 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 148 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 149 | $_POST[$val] = ($val != 'url') ? $this->convert_xml(strip_tags($_POST[$val])) : strip_tags($_POST[$val]); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 150 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 151 | if ($val == 'excerpt') |
| 152 | { |
| 153 | $_POST['excerpt'] = $this->limit_characters($_POST['excerpt']); |
| 154 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 155 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 156 | $this->data[$val] = $_POST[$val]; |
| 157 | } |
| 158 | |
| 159 | return TRUE; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 160 | } |
| 161 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 162 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 163 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 164 | /** |
| 165 | * Send Trackback Error Message |
| 166 | * |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 167 | * Allows custom errors to be set. By default it |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 168 | * sends the "incomplete information" error, as that's |
| 169 | * the most common one. |
| 170 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 171 | * @param string |
| 172 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 173 | */ |
Andrey Andreev | 4eea989 | 2011-12-19 12:05:41 +0200 | [diff] [blame] | 174 | public function send_error($message = 'Incomplete Information') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 175 | { |
| 176 | echo "<?xml version=\"1.0\" encoding=\"utf-8\"?".">\n<response>\n<error>1</error>\n<message>".$message."</message>\n</response>"; |
| 177 | exit; |
| 178 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 179 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 180 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 181 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 182 | /** |
| 183 | * Send Trackback Success Message |
| 184 | * |
| 185 | * This should be called when a trackback has been |
| 186 | * successfully received and inserted. |
| 187 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 188 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 189 | */ |
Andrey Andreev | 4eea989 | 2011-12-19 12:05:41 +0200 | [diff] [blame] | 190 | public function send_success() |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 191 | { |
| 192 | echo "<?xml version=\"1.0\" encoding=\"utf-8\"?".">\n<response>\n<error>0</error>\n</response>"; |
| 193 | exit; |
| 194 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 195 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 196 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 197 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 198 | /** |
| 199 | * Fetch a particular item |
| 200 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 201 | * @param string |
| 202 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 203 | */ |
Andrey Andreev | 4eea989 | 2011-12-19 12:05:41 +0200 | [diff] [blame] | 204 | public function data($item) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 205 | { |
| 206 | return ( ! isset($this->data[$item])) ? '' : $this->data[$item]; |
| 207 | } |
| 208 | |
| 209 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 210 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 211 | /** |
| 212 | * Process Trackback |
| 213 | * |
| 214 | * Opens a socket connection and passes the data to |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 215 | * the server. Returns TRUE on success, FALSE on failure |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 216 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 217 | * @param string |
| 218 | * @param string |
| 219 | * @return bool |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 220 | */ |
Andrey Andreev | 4eea989 | 2011-12-19 12:05:41 +0200 | [diff] [blame] | 221 | public function process($url, $data) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 222 | { |
| 223 | $target = parse_url($url); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 224 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 225 | // Open the socket |
| 226 | if ( ! $fp = @fsockopen($target['host'], 80)) |
| 227 | { |
| 228 | $this->set_error('Invalid Connection: '.$url); |
| 229 | return FALSE; |
| 230 | } |
| 231 | |
| 232 | // Build the path |
| 233 | $ppath = ( ! isset($target['path'])) ? $url : $target['path']; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 234 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 235 | $path = (isset($target['query']) && $target['query'] != "") ? $ppath.'?'.$target['query'] : $ppath; |
| 236 | |
| 237 | // Add the Trackback ID to the data string |
| 238 | if ($id = $this->get_id($url)) |
| 239 | { |
| 240 | $data = "tb_id=".$id."&".$data; |
| 241 | } |
| 242 | |
| 243 | // Transfer the data |
| 244 | fputs ($fp, "POST " . $path . " HTTP/1.0\r\n" ); |
| 245 | fputs ($fp, "Host: " . $target['host'] . "\r\n" ); |
| 246 | fputs ($fp, "Content-type: application/x-www-form-urlencoded\r\n" ); |
| 247 | fputs ($fp, "Content-length: " . strlen($data) . "\r\n" ); |
| 248 | fputs ($fp, "Connection: close\r\n\r\n" ); |
| 249 | fputs ($fp, $data); |
| 250 | |
| 251 | // Was it successful? |
| 252 | $this->response = ""; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 253 | |
Pascal Kriete | 14287f3 | 2011-02-14 13:39:34 -0500 | [diff] [blame] | 254 | while ( ! feof($fp)) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 255 | { |
| 256 | $this->response .= fgets($fp, 128); |
| 257 | } |
| 258 | @fclose($fp); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 259 | |
| 260 | |
Andrey Andreev | d9cfa7b | 2011-12-25 16:32:59 +0200 | [diff] [blame] | 261 | if (stripos($this->response, '<error>0</error>') === FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 262 | { |
Andrey Andreev | d9cfa7b | 2011-12-25 16:32:59 +0200 | [diff] [blame] | 263 | $message = (preg_match('/<message>(.*?)<\/message>/is', $this->response, $match)) ? trim($match[1]) : 'An unknown error was encountered'; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 264 | $this->set_error($message); |
| 265 | return FALSE; |
| 266 | } |
| 267 | |
| 268 | return TRUE; |
| 269 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 270 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 271 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 272 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 273 | /** |
| 274 | * Extract Trackback URLs |
| 275 | * |
| 276 | * This function lets multiple trackbacks be sent. |
| 277 | * It takes a string of URLs (separated by comma or |
| 278 | * space) and puts each URL into an array |
| 279 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 280 | * @param string |
| 281 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 282 | */ |
Andrey Andreev | 4eea989 | 2011-12-19 12:05:41 +0200 | [diff] [blame] | 283 | public function extract_urls($urls) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 284 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 285 | // Remove the pesky white space and replace with a comma. |
| 286 | $urls = preg_replace("/\s*(\S+)\s*/", "\\1,", $urls); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 287 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 288 | // If they use commas get rid of the doubles. |
| 289 | $urls = str_replace(",,", ",", $urls); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 290 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 291 | // Remove any comma that might be at the end |
Andrey Andreev | d9cfa7b | 2011-12-25 16:32:59 +0200 | [diff] [blame] | 292 | if (substr($urls, -1) === ',') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 293 | { |
| 294 | $urls = substr($urls, 0, -1); |
| 295 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 296 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 297 | // Break into an array via commas |
| 298 | $urls = preg_split('/[,]/', $urls); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 299 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 300 | // Removes duplicates |
| 301 | $urls = array_unique($urls); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 302 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 303 | array_walk($urls, array($this, 'validate_url')); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 304 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 305 | return $urls; |
| 306 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 307 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 308 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 309 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 310 | /** |
| 311 | * Validate URL |
| 312 | * |
| 313 | * Simply adds "http://" if missing |
| 314 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 315 | * @param string |
| 316 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 317 | */ |
Andrey Andreev | 4eea989 | 2011-12-19 12:05:41 +0200 | [diff] [blame] | 318 | public function validate_url($url) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 319 | { |
| 320 | $url = trim($url); |
| 321 | |
Andrey Andreev | d9cfa7b | 2011-12-25 16:32:59 +0200 | [diff] [blame] | 322 | if (strpos($url, 'http') !== 0) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 323 | { |
Andrey Andreev | d9cfa7b | 2011-12-25 16:32:59 +0200 | [diff] [blame] | 324 | $url = 'http://'.$url; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 325 | } |
| 326 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 327 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 328 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 329 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 330 | /** |
| 331 | * Find the Trackback URL's ID |
| 332 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 333 | * @param string |
| 334 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 335 | */ |
Andrey Andreev | 4eea989 | 2011-12-19 12:05:41 +0200 | [diff] [blame] | 336 | public function get_id($url) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 337 | { |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 338 | $tb_id = ""; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 339 | |
Robin Sowell | 76b369e | 2010-03-19 11:15:28 -0400 | [diff] [blame] | 340 | if (strpos($url, '?') !== FALSE) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 341 | { |
| 342 | $tb_array = explode('/', $url); |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 343 | $tb_end = $tb_array[count($tb_array)-1]; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 344 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 345 | if ( ! is_numeric($tb_end)) |
| 346 | { |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 347 | $tb_end = $tb_array[count($tb_array)-2]; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 348 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 349 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 350 | $tb_array = explode('=', $tb_end); |
| 351 | $tb_id = $tb_array[count($tb_array)-1]; |
| 352 | } |
| 353 | else |
| 354 | { |
Derek Jones | 1322ec5 | 2009-03-11 17:01:14 +0000 | [diff] [blame] | 355 | $url = rtrim($url, '/'); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 356 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 357 | $tb_array = explode('/', $url); |
| 358 | $tb_id = $tb_array[count($tb_array)-1]; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 359 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 360 | if ( ! is_numeric($tb_id)) |
| 361 | { |
Derek Jones | 37f4b9c | 2011-07-01 17:56:50 -0500 | [diff] [blame] | 362 | $tb_id = $tb_array[count($tb_array)-2]; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 363 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 364 | } |
| 365 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 366 | if ( ! preg_match ("/^([0-9]+)$/", $tb_id)) |
| 367 | { |
Derek Jones | 5052e27 | 2010-03-02 22:53:38 -0600 | [diff] [blame] | 368 | return FALSE; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 369 | } |
| 370 | else |
| 371 | { |
| 372 | return $tb_id; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 373 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 374 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 375 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 376 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 377 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 378 | /** |
| 379 | * Convert Reserved XML characters to Entities |
| 380 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 381 | * @param string |
| 382 | * @return string |
| 383 | */ |
Andrey Andreev | 4eea989 | 2011-12-19 12:05:41 +0200 | [diff] [blame] | 384 | public function convert_xml($str) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 385 | { |
| 386 | $temp = '__TEMP_AMPERSANDS__'; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 387 | |
Andrey Andreev | d9cfa7b | 2011-12-25 16:32:59 +0200 | [diff] [blame] | 388 | $str = preg_replace(array('/&#(\d+);/', '/&(\w+);/'), "$temp\\1;", $str); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 389 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 390 | $str = str_replace(array("&","<",">","\"", "'", "-"), |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 391 | array("&", "<", ">", """, "'", "-"), |
| 392 | $str); |
| 393 | |
Andrey Andreev | d9cfa7b | 2011-12-25 16:32:59 +0200 | [diff] [blame] | 394 | $str = preg_replace(array("/$temp(\d+);/", "/$temp(\w+);/"), array('&#\\1;', '&\\1;'), $str); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 395 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 396 | return $str; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 397 | } |
| 398 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 399 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 400 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 401 | /** |
| 402 | * Character limiter |
| 403 | * |
| 404 | * Limits the string based on the character count. Will preserve complete words. |
| 405 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 406 | * @param string |
| 407 | * @param integer |
| 408 | * @param string |
| 409 | * @return string |
| 410 | */ |
Andrey Andreev | 4eea989 | 2011-12-19 12:05:41 +0200 | [diff] [blame] | 411 | public function limit_characters($str, $n = 500, $end_char = '…') |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 412 | { |
| 413 | if (strlen($str) < $n) |
| 414 | { |
| 415 | return $str; |
| 416 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 417 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 418 | $str = preg_replace("/\s+/", ' ', str_replace(array("\r\n", "\r", "\n"), ' ', $str)); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 419 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 420 | if (strlen($str) <= $n) |
| 421 | { |
| 422 | return $str; |
| 423 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 424 | |
Andrey Andreev | d9cfa7b | 2011-12-25 16:32:59 +0200 | [diff] [blame] | 425 | $out = ''; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 426 | foreach (explode(' ', trim($str)) as $val) |
| 427 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 428 | $out .= $val.' '; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 429 | if (strlen($out) >= $n) |
| 430 | { |
Andrey Andreev | d9cfa7b | 2011-12-25 16:32:59 +0200 | [diff] [blame] | 431 | return rtrim($out).$end_char; |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 432 | } |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 433 | } |
| 434 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 435 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 436 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 437 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 438 | /** |
| 439 | * High ASCII to Entities |
| 440 | * |
| 441 | * Converts Hight ascii text and MS Word special chars |
| 442 | * to character entities |
| 443 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 444 | * @param string |
| 445 | * @return string |
| 446 | */ |
Andrey Andreev | 4eea989 | 2011-12-19 12:05:41 +0200 | [diff] [blame] | 447 | public function convert_ascii($str) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 448 | { |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 449 | $count = 1; |
| 450 | $out = ''; |
| 451 | $temp = array(); |
| 452 | |
| 453 | for ($i = 0, $s = strlen($str); $i < $s; $i++) |
| 454 | { |
| 455 | $ordinal = ord($str[$i]); |
| 456 | |
| 457 | if ($ordinal < 128) |
| 458 | { |
| 459 | $out .= $str[$i]; |
| 460 | } |
| 461 | else |
| 462 | { |
Andrey Andreev | d9cfa7b | 2011-12-25 16:32:59 +0200 | [diff] [blame] | 463 | if (count($temp) === 0) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 464 | { |
| 465 | $count = ($ordinal < 224) ? 2 : 3; |
| 466 | } |
| 467 | |
| 468 | $temp[] = $ordinal; |
| 469 | |
Andrey Andreev | d9cfa7b | 2011-12-25 16:32:59 +0200 | [diff] [blame] | 470 | if (count($temp) === $count) |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 471 | { |
Andrey Andreev | d9cfa7b | 2011-12-25 16:32:59 +0200 | [diff] [blame] | 472 | $number = ($count == 3) ? (($temp[0] % 16) * 4096) + (($temp[1] % 64) * 64) + ($temp[2] % 64) : (($temp[0] % 32) * 64) + ($temp[1] % 64); |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 473 | |
| 474 | $out .= '&#'.$number.';'; |
| 475 | $count = 1; |
| 476 | $temp = array(); |
| 477 | } |
| 478 | } |
| 479 | } |
| 480 | |
| 481 | return $out; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 482 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 483 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 484 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 485 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 486 | /** |
| 487 | * Set error message |
| 488 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 489 | * @param string |
| 490 | * @return void |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 491 | */ |
Andrey Andreev | 4eea989 | 2011-12-19 12:05:41 +0200 | [diff] [blame] | 492 | public function set_error($msg) |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 493 | { |
| 494 | log_message('error', $msg); |
| 495 | $this->error_msg[] = $msg; |
| 496 | } |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 497 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 498 | // -------------------------------------------------------------------- |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 499 | |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 500 | /** |
| 501 | * Show error messages |
| 502 | * |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 503 | * @param string |
| 504 | * @param string |
| 505 | * @return string |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 506 | */ |
Andrey Andreev | 4eea989 | 2011-12-19 12:05:41 +0200 | [diff] [blame] | 507 | public function display_errors($open = '<p>', $close = '</p>') |
Barry Mieny | dd67197 | 2010-10-04 16:33:58 +0200 | [diff] [blame] | 508 | { |
Andrey Andreev | 4eea989 | 2011-12-19 12:05:41 +0200 | [diff] [blame] | 509 | return (count($this->error_msg) > 0) ? $open . implode($close . $open, $this->error_msg) . $close : ''; |
Derek Allard | 2067d1a | 2008-11-13 22:59:24 +0000 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | } |
| 513 | // END Trackback Class |
| 514 | |
| 515 | /* End of file Trackback.php */ |
Andrey Andreev | 4eea989 | 2011-12-19 12:05:41 +0200 | [diff] [blame] | 516 | /* Location: ./system/libraries/Trackback.php */ |