blob: a1667c79c853afecbb0774fbed4ff78bf79b6d22 [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
Andrey Andreev9ea31e82013-09-23 17:20:56 +030011.. content::
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.
67 .. note:: the Trackback class will automatically send only the first 500 characters of your
68 entry. It will also strip all HTML.
Derek Jones8ede1a22011-10-05 13:34:52 -050069- **blog_name** - The name of your weblog.
Andrey Andreev9ea31e82013-09-23 17:20:56 +030070- **charset** - The character encoding your weblog is written in. If omitted, UTF-8 will be used.
Derek Jones8ede1a22011-10-05 13:34:52 -050071
Andrey Andreev9ea31e82013-09-23 17:20:56 +030072The Trackback sending method returns TRUE/FALSE (boolean) on success
Derek Jones8ede1a22011-10-05 13:34:52 -050073or failure. If it fails, you can retrieve the error message using::
74
75 $this->trackback->display_errors();
76
77Receiving Trackbacks
78====================
79
80Before you can receive Trackbacks you must create a weblog. If you don't
81have a blog yet there's no point in continuing.
82
83Receiving Trackbacks is a little more complex than sending them, only
84because you will need a database table in which to store them, and you
85will need to validate the incoming trackback data. You are encouraged to
86implement a thorough validation process to guard against spam and
87duplicate data. You may also want to limit the number of Trackbacks you
88allow from a particular IP within a given span of time to further
89curtail spam. The process of receiving a Trackback is quite simple; the
90validation is what takes most of the effort.
91
92Your Ping URL
93=============
94
95In order to accept Trackbacks you must display a Trackback URL next to
96each one of your weblog entries. This will be the URL that people will
97use to send you Trackbacks (we will refer to this as your "Ping URL").
98
99Your Ping URL must point to a controller function where your Trackback
100receiving code is located, and the URL must contain the ID number for
101each particular entry, so that when the Trackback is received you'll be
102able to associate it with a particular entry.
103
104For example, if your controller class is called Trackback, and the
105receiving function is called receive, your Ping URLs will look something
106like this::
107
108 http://example.com/index.php/trackback/receive/entry_id
109
110Where entry_id represents the individual ID number for each of your
111entries.
112
113Creating a Trackback Table
114==========================
115
116Before you can receive Trackbacks you must create a table in which to
Derek Jonese0da6042011-10-05 15:37:39 -0500117store them. Here is a basic prototype for such a table::
Derek Jones8ede1a22011-10-05 13:34:52 -0500118
Derek Jonese0da6042011-10-05 15:37:39 -0500119 CREATE TABLE trackbacks (
Andrey Andreev9ea31e82013-09-23 17:20:56 +0300120 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 Jonese0da6042011-10-05 15:37:39 -0500130 );
131
Derek Jones8ede1a22011-10-05 13:34:52 -0500132The Trackback specification only requires four pieces of information to
133be sent in a Trackback (url, title, excerpt, blog_name), but to make
134the data more useful we've added a few more fields in the above table
135schema (date, IP address, etc.).
136
137Processing a Trackback
138======================
139
140Here is an example showing how you will receive and process a Trackback.
141The following code is intended for use within the controller function
Andrey Andreev9ea31e82013-09-23 17:20:56 +0300142where you expect to receive Trackbacks.::
Derek Jones8ede1a22011-10-05 13:34:52 -0500143
Derek Jonese0da6042011-10-05 15:37:39 -0500144 $this->load->library('trackback');
145 $this->load->database();
146
147 if ($this->uri->segment(3) == FALSE)
148 {
Andrey Andreev9ea31e82013-09-23 17:20:56 +0300149 $this->trackback->send_error('Unable to determine the entry ID');
Derek Jonese0da6042011-10-05 15:37:39 -0500150 }
151
152 if ( ! $this->trackback->receive())
153 {
Andrey Andreev9ea31e82013-09-23 17:20:56 +0300154 $this->trackback->send_error('The Trackback did not contain valid data');
Derek Jonese0da6042011-10-05 15:37:39 -0500155 }
156
157 $data = array(
Andrey Andreev9ea31e82013-09-23 17:20:56 +0300158 '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 Jonese0da6042011-10-05 15:37:39 -0500167
168 $sql = $this->db->insert_string('trackbacks', $data);
169 $this->db->query($sql);
170
171 $this->trackback->send_success();
Derek Jones8ede1a22011-10-05 13:34:52 -0500172
173Notes:
174^^^^^^
175
176The entry ID number is expected in the third segment of your URL. This
177is based on the URI example we gave earlier::
178
179 http://example.com/index.php/trackback/receive/entry_id
180
181Notice the entry_id is in the third URI segment, which you can retrieve
182using::
183
184 $this->uri->segment(3);
185
186In our Trackback receiving code above, if the third segment is missing,
187we will issue an error. Without a valid entry ID, there's no reason to
188continue.
189
190The $this->trackback->receive() function is simply a validation function
191that looks at the incoming data and makes sure it contains the four
192pieces of data that are required (url, title, excerpt, blog_name). It
193returns TRUE on success and FALSE on failure. If it fails you will issue
194an error message.
195
196The incoming Trackback data can be retrieved using this function::
197
198 $this->trackback->data('item')
199
200Where item represents one of these four pieces of info: url, title,
201excerpt, or blog_name
202
203If the Trackback data is successfully received, you will issue a success
204message 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 Andreev9ea31e82013-09-23 17:20:56 +0300210
211***************
212Class 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 = '&#8230;']])
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.