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