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