Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 1 | ############### |
| 2 | Trackback Class |
| 3 | ############### |
| 4 | |
| 5 | The Trackback Class provides functions that enable you to send and |
| 6 | receive Trackback data. |
| 7 | |
| 8 | If you are not familiar with Trackbacks you'll find more information |
| 9 | `here <http://en.wikipedia.org/wiki/Trackback>`_. |
| 10 | |
Andrey Andreev | 9ea31e8 | 2013-09-23 17:20:56 +0300 | [diff] [blame] | 11 | .. content:: |
Andrey Andreev | cc04209 | 2014-01-03 17:08:27 +0200 | [diff] [blame] | 12 | :local: |
Andrey Andreev | 9ea31e8 | 2013-09-23 17:20:56 +0300 | [diff] [blame] | 13 | |
| 14 | .. raw:: html |
| 15 | |
Andrey Andreev | cc04209 | 2014-01-03 17:08:27 +0200 | [diff] [blame] | 16 | <div class="custom-index container"></div> |
Andrey Andreev | 9ea31e8 | 2013-09-23 17:20:56 +0300 | [diff] [blame] | 17 | |
| 18 | ************************* |
| 19 | Using the Trackback Class |
| 20 | ************************* |
| 21 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 22 | Initializing the Class |
| 23 | ====================== |
| 24 | |
| 25 | Like most other classes in CodeIgniter, the Trackback class is |
Andrey Andreev | 9ea31e8 | 2013-09-23 17:20:56 +0300 | [diff] [blame] | 26 | initialized in your controller using the ``$this->load->library()`` method:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 27 | |
| 28 | $this->load->library('trackback'); |
| 29 | |
Andrey Andreev | 9ea31e8 | 2013-09-23 17:20:56 +0300 | [diff] [blame] | 30 | Once loaded, the Trackback library object will be available using:: |
| 31 | |
| 32 | $this->trackback |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 33 | |
| 34 | Sending Trackbacks |
| 35 | ================== |
| 36 | |
| 37 | A Trackback can be sent from any of your controller functions using code |
| 38 | similar to this example:: |
| 39 | |
Derek Jones | e0da604 | 2011-10-05 15:37:39 -0500 | [diff] [blame] | 40 | $this->load->library('trackback'); |
| 41 | |
| 42 | $tb_data = array( |
Andrey Andreev | 9ea31e8 | 2013-09-23 17:20:56 +0300 | [diff] [blame] | 43 | 'ping_url' => 'http://example.com/trackback/456', |
| 44 | 'url' => 'http://www.my-example.com/blog/entry/123', |
| 45 | 'title' => 'The Title of My Entry', |
| 46 | 'excerpt' => 'The entry content.', |
| 47 | 'blog_name' => 'My Blog Name', |
| 48 | 'charset' => 'utf-8' |
| 49 | ); |
Derek Jones | e0da604 | 2011-10-05 15:37:39 -0500 | [diff] [blame] | 50 | |
| 51 | if ( ! $this->trackback->send($tb_data)) |
| 52 | { |
Andrey Andreev | 9ea31e8 | 2013-09-23 17:20:56 +0300 | [diff] [blame] | 53 | echo $this->trackback->display_errors(); |
Derek Jones | e0da604 | 2011-10-05 15:37:39 -0500 | [diff] [blame] | 54 | } |
| 55 | else |
| 56 | { |
Andrey Andreev | 9ea31e8 | 2013-09-23 17:20:56 +0300 | [diff] [blame] | 57 | echo 'Trackback was sent!'; |
Derek Jones | e0da604 | 2011-10-05 15:37:39 -0500 | [diff] [blame] | 58 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 59 | |
| 60 | Description of array data: |
| 61 | |
| 62 | - **ping_url** - The URL of the site you are sending the Trackback to. |
Andrey Andreev | 9ea31e8 | 2013-09-23 17:20:56 +0300 | [diff] [blame] | 63 | You can send Trackbacks to multiple URLs by separating each URL with a comma. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 64 | - **url** - The URL to YOUR site where the weblog entry can be seen. |
| 65 | - **title** - The title of your weblog entry. |
Andrey Andreev | 9ea31e8 | 2013-09-23 17:20:56 +0300 | [diff] [blame] | 66 | - **excerpt** - The content of your weblog entry. |
| 67 | .. note:: the Trackback class will automatically send only the first 500 characters of your |
| 68 | entry. It will also strip all HTML. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 69 | - **blog_name** - The name of your weblog. |
Andrey Andreev | 9ea31e8 | 2013-09-23 17:20:56 +0300 | [diff] [blame] | 70 | - **charset** - The character encoding your weblog is written in. If omitted, UTF-8 will be used. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 71 | |
Andrey Andreev | 9ea31e8 | 2013-09-23 17:20:56 +0300 | [diff] [blame] | 72 | The Trackback sending method returns TRUE/FALSE (boolean) on success |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 73 | or failure. If it fails, you can retrieve the error message using:: |
| 74 | |
| 75 | $this->trackback->display_errors(); |
| 76 | |
| 77 | Receiving Trackbacks |
| 78 | ==================== |
| 79 | |
| 80 | Before you can receive Trackbacks you must create a weblog. If you don't |
| 81 | have a blog yet there's no point in continuing. |
| 82 | |
| 83 | Receiving Trackbacks is a little more complex than sending them, only |
| 84 | because you will need a database table in which to store them, and you |
| 85 | will need to validate the incoming trackback data. You are encouraged to |
| 86 | implement a thorough validation process to guard against spam and |
| 87 | duplicate data. You may also want to limit the number of Trackbacks you |
| 88 | allow from a particular IP within a given span of time to further |
| 89 | curtail spam. The process of receiving a Trackback is quite simple; the |
| 90 | validation is what takes most of the effort. |
| 91 | |
| 92 | Your Ping URL |
| 93 | ============= |
| 94 | |
| 95 | In order to accept Trackbacks you must display a Trackback URL next to |
| 96 | each one of your weblog entries. This will be the URL that people will |
| 97 | use to send you Trackbacks (we will refer to this as your "Ping URL"). |
| 98 | |
| 99 | Your Ping URL must point to a controller function where your Trackback |
| 100 | receiving code is located, and the URL must contain the ID number for |
| 101 | each particular entry, so that when the Trackback is received you'll be |
| 102 | able to associate it with a particular entry. |
| 103 | |
| 104 | For example, if your controller class is called Trackback, and the |
| 105 | receiving function is called receive, your Ping URLs will look something |
| 106 | like this:: |
| 107 | |
| 108 | http://example.com/index.php/trackback/receive/entry_id |
| 109 | |
| 110 | Where entry_id represents the individual ID number for each of your |
| 111 | entries. |
| 112 | |
| 113 | Creating a Trackback Table |
| 114 | ========================== |
| 115 | |
| 116 | Before you can receive Trackbacks you must create a table in which to |
Derek Jones | e0da604 | 2011-10-05 15:37:39 -0500 | [diff] [blame] | 117 | store them. Here is a basic prototype for such a table:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 118 | |
Derek Jones | e0da604 | 2011-10-05 15:37:39 -0500 | [diff] [blame] | 119 | CREATE TABLE trackbacks ( |
Andrey Andreev | 9ea31e8 | 2013-09-23 17:20:56 +0300 | [diff] [blame] | 120 | tb_id int(10) unsigned NOT NULL auto_increment, |
| 121 | entry_id int(10) unsigned NOT NULL default 0, |
| 122 | url varchar(200) NOT NULL, |
| 123 | title varchar(100) NOT NULL, |
| 124 | excerpt text NOT NULL, |
| 125 | blog_name varchar(100) NOT NULL, |
| 126 | tb_date int(10) NOT NULL, |
| 127 | ip_address varchar(45) NOT NULL, |
| 128 | PRIMARY KEY `tb_id` (`tb_id`), |
| 129 | KEY `entry_id` (`entry_id`) |
Derek Jones | e0da604 | 2011-10-05 15:37:39 -0500 | [diff] [blame] | 130 | ); |
| 131 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 132 | The Trackback specification only requires four pieces of information to |
| 133 | be sent in a Trackback (url, title, excerpt, blog_name), but to make |
| 134 | the data more useful we've added a few more fields in the above table |
| 135 | schema (date, IP address, etc.). |
| 136 | |
| 137 | Processing a Trackback |
| 138 | ====================== |
| 139 | |
| 140 | Here is an example showing how you will receive and process a Trackback. |
| 141 | The following code is intended for use within the controller function |
Andrey Andreev | 9ea31e8 | 2013-09-23 17:20:56 +0300 | [diff] [blame] | 142 | where you expect to receive Trackbacks.:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 143 | |
Derek Jones | e0da604 | 2011-10-05 15:37:39 -0500 | [diff] [blame] | 144 | $this->load->library('trackback'); |
| 145 | $this->load->database(); |
| 146 | |
| 147 | if ($this->uri->segment(3) == FALSE) |
| 148 | { |
Andrey Andreev | 9ea31e8 | 2013-09-23 17:20:56 +0300 | [diff] [blame] | 149 | $this->trackback->send_error('Unable to determine the entry ID'); |
Derek Jones | e0da604 | 2011-10-05 15:37:39 -0500 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | if ( ! $this->trackback->receive()) |
| 153 | { |
Andrey Andreev | 9ea31e8 | 2013-09-23 17:20:56 +0300 | [diff] [blame] | 154 | $this->trackback->send_error('The Trackback did not contain valid data'); |
Derek Jones | e0da604 | 2011-10-05 15:37:39 -0500 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | $data = array( |
Andrey Andreev | 9ea31e8 | 2013-09-23 17:20:56 +0300 | [diff] [blame] | 158 | 'tb_id' => '', |
| 159 | 'entry_id' => $this->uri->segment(3), |
| 160 | 'url' => $this->trackback->data('url'), |
| 161 | 'title' => $this->trackback->data('title'), |
| 162 | 'excerpt' => $this->trackback->data('excerpt'), |
| 163 | 'blog_name' => $this->trackback->data('blog_name'), |
| 164 | 'tb_date' => time(), |
| 165 | 'ip_address' => $this->input->ip_address() |
| 166 | ); |
Derek Jones | e0da604 | 2011-10-05 15:37:39 -0500 | [diff] [blame] | 167 | |
| 168 | $sql = $this->db->insert_string('trackbacks', $data); |
| 169 | $this->db->query($sql); |
| 170 | |
| 171 | $this->trackback->send_success(); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 172 | |
| 173 | Notes: |
| 174 | ^^^^^^ |
| 175 | |
| 176 | The entry ID number is expected in the third segment of your URL. This |
| 177 | is based on the URI example we gave earlier:: |
| 178 | |
| 179 | http://example.com/index.php/trackback/receive/entry_id |
| 180 | |
| 181 | Notice the entry_id is in the third URI segment, which you can retrieve |
| 182 | using:: |
| 183 | |
| 184 | $this->uri->segment(3); |
| 185 | |
| 186 | In our Trackback receiving code above, if the third segment is missing, |
| 187 | we will issue an error. Without a valid entry ID, there's no reason to |
| 188 | continue. |
| 189 | |
| 190 | The $this->trackback->receive() function is simply a validation function |
| 191 | that looks at the incoming data and makes sure it contains the four |
| 192 | pieces of data that are required (url, title, excerpt, blog_name). It |
| 193 | returns TRUE on success and FALSE on failure. If it fails you will issue |
| 194 | an error message. |
| 195 | |
| 196 | The incoming Trackback data can be retrieved using this function:: |
| 197 | |
| 198 | $this->trackback->data('item') |
| 199 | |
| 200 | Where item represents one of these four pieces of info: url, title, |
| 201 | excerpt, or blog_name |
| 202 | |
| 203 | If the Trackback data is successfully received, you will issue a success |
| 204 | message using:: |
| 205 | |
| 206 | $this->trackback->send_success(); |
| 207 | |
| 208 | .. note:: The above code contains no data validation, which you are |
| 209 | encouraged to add. |
Andrey Andreev | 9ea31e8 | 2013-09-23 17:20:56 +0300 | [diff] [blame] | 210 | |
| 211 | *************** |
| 212 | Class Reference |
| 213 | *************** |
| 214 | |
| 215 | .. class:: CI_Trackback |
| 216 | |
| 217 | .. attribute:: $data = array('url' => '', 'title' => '', 'excerpt' => '', 'blog_name' => '', 'charset' => '') |
| 218 | |
| 219 | Trackback data array. |
| 220 | |
| 221 | .. attribute:: $convert_ascii = TRUE |
| 222 | |
| 223 | Whether to convert high ASCII and MS Word characters to HTML entities. |
| 224 | |
| 225 | .. method:: send($tb_data) |
| 226 | |
| 227 | :param array $tb_data: trackback data |
| 228 | :returns: bool |
| 229 | |
| 230 | Send trackback. |
| 231 | |
| 232 | .. method:: receive() |
| 233 | |
| 234 | :returns: bool |
| 235 | |
| 236 | This method simply validates the incoming TB data, returning TRUE on success and FALSE on failure. |
| 237 | If the data is valid it is set to the ``$this->data`` array so that it can be inserted into a database. |
| 238 | |
| 239 | .. method:: send_error([$message = 'Incomplete information') |
| 240 | |
| 241 | :param string $message: error message |
| 242 | :returns: void |
| 243 | |
| 244 | Responses to a trackback request with an error message. |
| 245 | |
| 246 | .. note:: This method will terminate script execution. |
| 247 | |
| 248 | .. method:: send_success() |
| 249 | |
| 250 | :returns: void |
| 251 | |
| 252 | Responses to a trackback request with a success message. |
| 253 | |
| 254 | .. note:: This method will terminate script execution. |
| 255 | |
| 256 | .. method:: data($item) |
| 257 | |
| 258 | :param string $item: data key |
| 259 | :returns: string |
| 260 | |
| 261 | Returns a single item from the reponse data array. |
| 262 | |
| 263 | .. method:: process($url, $data) |
| 264 | |
| 265 | :param string $url: target url |
| 266 | :param string $data: raw post data |
| 267 | :returns: bool |
| 268 | |
| 269 | Opens a socket connection and passes the data to the server, returning TRUE on success and FALSE on failure. |
| 270 | |
| 271 | .. method:: extract_urls($urls) |
| 272 | |
| 273 | :param string $urls: comma-separated url list |
| 274 | :returns: string |
| 275 | |
| 276 | This method lets multiple trackbacks to be sent. It takes a string of URLs (separated by comma or space) and puts each URL into an array. |
| 277 | |
| 278 | .. method:: validate_url(&$url) |
| 279 | |
| 280 | :param string $url: trackback url |
| 281 | :returns: void |
| 282 | |
| 283 | Simply adds the *http://* prefix it it's not already present in the URL. |
| 284 | |
| 285 | .. method:: get_id($url) |
| 286 | |
| 287 | :param string $url: trackback url |
| 288 | :returns: string |
| 289 | |
| 290 | Find and return a trackback URL's ID or FALSE on failure. |
| 291 | |
| 292 | .. method:: convert_xml($str) |
| 293 | |
| 294 | :param string $str: input string |
| 295 | :returns: string |
| 296 | |
| 297 | Converts reserved XML characters to entities. |
| 298 | |
| 299 | .. method:: limit_characters($str[, $n = 500[, $end_char = '…']]) |
| 300 | |
| 301 | :param string $str: input string |
| 302 | :param int $n: max characters number |
| 303 | :param string $end_char: character to put at end of string |
| 304 | :returns: string |
| 305 | |
| 306 | Limits the string based on the character count. Will preserve complete words. |
| 307 | |
| 308 | .. method:: convert_ascii($str) |
| 309 | |
| 310 | :param string $str: input string |
| 311 | :returns: string |
| 312 | |
| 313 | Converts high ASCII text and MS Word special characterss to HTML entities. |
| 314 | |
| 315 | .. method:: set_error($msg) |
| 316 | |
| 317 | :param string $msg: error message |
| 318 | :returns: void |
| 319 | |
| 320 | Set an log an error message. |
| 321 | |
| 322 | .. method:: display_errors([$open = '<p>'[, $close = '</p>']]) |
| 323 | |
| 324 | :param string $open: open tag |
| 325 | :param string $close: close tag |
| 326 | :returns: string |
| 327 | |
| 328 | Returns error messages formatted in HTML or an empty string if there are no errors. |