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