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