blob: dc4477e9f1e5d8397c68941dc774003b6371f02f [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
Andrey Andreev9a0e6602015-01-19 14:54:08 +020070.. note:: The Trackback class will automatically send only the first 500 characters of your
Connor Tumbleson75b3fb22014-01-11 06:58:43 -060071 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
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200216.. php:class:: CI_Trackback
Andrey Andreev9ea31e82013-09-23 17:20:56 +0300217
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
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200226 .. php:method:: send($tb_data)
Andrey Andreev9ea31e82013-09-23 17:20:56 +0300227
Andrey Andreev28c2c972014-02-08 04:27:48 +0200228 :param array $tb_data: Trackback data
229 :returns: TRUE on success, FALSE on failure
230 :rtype: bool
Andrey Andreev9ea31e82013-09-23 17:20:56 +0300231
232 Send trackback.
233
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200234 .. php:method:: receive()
Andrey Andreev9ea31e82013-09-23 17:20:56 +0300235
Andrey Andreev28c2c972014-02-08 04:27:48 +0200236 :returns: TRUE on success, FALSE on failure
237 :rtype: bool
Andrey Andreev9ea31e82013-09-23 17:20:56 +0300238
239 This method simply validates the incoming TB data, returning TRUE on success and FALSE on failure.
240 If the data is valid it is set to the ``$this->data`` array so that it can be inserted into a database.
241
Andrey Andreev3e75ff62015-12-10 13:28:19 +0200242 .. php:method:: send_error([$message = 'Incomplete information'])
Andrey Andreev9ea31e82013-09-23 17:20:56 +0300243
Andrey Andreev28c2c972014-02-08 04:27:48 +0200244 :param string $message: Error message
245 :rtype: void
Andrey Andreev9ea31e82013-09-23 17:20:56 +0300246
247 Responses to a trackback request with an error message.
248
249 .. note:: This method will terminate script execution.
250
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200251 .. php:method:: send_success()
Andrey Andreev9ea31e82013-09-23 17:20:56 +0300252
Andrey Andreev28c2c972014-02-08 04:27:48 +0200253 :rtype: void
Andrey Andreev9ea31e82013-09-23 17:20:56 +0300254
255 Responses to a trackback request with a success message.
256
257 .. note:: This method will terminate script execution.
258
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200259 .. php:method:: data($item)
Andrey Andreev9ea31e82013-09-23 17:20:56 +0300260
Andrey Andreev28c2c972014-02-08 04:27:48 +0200261 :param string $item: Data key
262 :returns: Data value or empty string if not found
263 :rtype: string
Andrey Andreev9ea31e82013-09-23 17:20:56 +0300264
Andrey Andreev71d8f722017-01-17 12:01:00 +0200265 Returns a single item from the response data array.
Andrey Andreev9ea31e82013-09-23 17:20:56 +0300266
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200267 .. php:method:: process($url, $data)
Andrey Andreev9ea31e82013-09-23 17:20:56 +0300268
Andrey Andreev28c2c972014-02-08 04:27:48 +0200269 :param string $url: Target url
270 :param string $data: Raw POST data
271 :returns: TRUE on success, FALSE on failure
272 :rtype: bool
Andrey Andreev9ea31e82013-09-23 17:20:56 +0300273
274 Opens a socket connection and passes the data to the server, returning TRUE on success and FALSE on failure.
275
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200276 .. php:method:: extract_urls($urls)
Andrey Andreev9ea31e82013-09-23 17:20:56 +0300277
Andrey Andreev28c2c972014-02-08 04:27:48 +0200278 :param string $urls: Comma-separated URL list
279 :returns: Array of URLs
280 :rtype: array
Andrey Andreev9ea31e82013-09-23 17:20:56 +0300281
282 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.
283
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200284 .. php:method:: validate_url(&$url)
Andrey Andreev9ea31e82013-09-23 17:20:56 +0300285
Andrey Andreev28c2c972014-02-08 04:27:48 +0200286 :param string $url: Trackback URL
287 :rtype: void
Andrey Andreev9ea31e82013-09-23 17:20:56 +0300288
289 Simply adds the *http://* prefix it it's not already present in the URL.
290
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200291 .. php:method:: get_id($url)
Andrey Andreev9ea31e82013-09-23 17:20:56 +0300292
Andrey Andreev28c2c972014-02-08 04:27:48 +0200293 :param string $url: Trackback URL
294 :returns: URL ID or FALSE on failure
295 :rtype: string
Andrey Andreev9ea31e82013-09-23 17:20:56 +0300296
297 Find and return a trackback URL's ID or FALSE on failure.
298
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200299 .. php:method:: convert_xml($str)
Andrey Andreev9ea31e82013-09-23 17:20:56 +0300300
Andrey Andreev28c2c972014-02-08 04:27:48 +0200301 :param string $str: Input string
302 :returns: Converted string
303 :rtype: string
Andrey Andreev9ea31e82013-09-23 17:20:56 +0300304
305 Converts reserved XML characters to entities.
306
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200307 .. php:method:: limit_characters($str[, $n = 500[, $end_char = '&#8230;']])
Andrey Andreev9ea31e82013-09-23 17:20:56 +0300308
Andrey Andreev28c2c972014-02-08 04:27:48 +0200309 :param string $str: Input string
310 :param int $n: Max characters number
311 :param string $end_char: Character to put at end of string
312 :returns: Shortened string
313 :rtype: string
Andrey Andreev9ea31e82013-09-23 17:20:56 +0300314
315 Limits the string based on the character count. Will preserve complete words.
316
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200317 .. php:method:: convert_ascii($str)
Andrey Andreev9ea31e82013-09-23 17:20:56 +0300318
Andrey Andreev28c2c972014-02-08 04:27:48 +0200319 :param string $str: Input string
320 :returns: Converted string
321 :rtype: string
Andrey Andreev9ea31e82013-09-23 17:20:56 +0300322
323 Converts high ASCII text and MS Word special characterss to HTML entities.
324
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200325 .. php:method:: set_error($msg)
Andrey Andreev9ea31e82013-09-23 17:20:56 +0300326
Andrey Andreev28c2c972014-02-08 04:27:48 +0200327 :param string $msg: Error message
328 :rtype: void
Andrey Andreev9ea31e82013-09-23 17:20:56 +0300329
330 Set an log an error message.
331
Andrey Andreevcd3d9db2015-02-02 13:41:01 +0200332 .. php:method:: display_errors([$open = '<p>'[, $close = '</p>']])
Andrey Andreev9ea31e82013-09-23 17:20:56 +0300333
Andrey Andreev28c2c972014-02-08 04:27:48 +0200334 :param string $open: Open tag
335 :param string $close: Close tag
336 :returns: HTML formatted error messages
337 :rtype: string
Andrey Andreev9ea31e82013-09-23 17:20:56 +0300338
339 Returns error messages formatted in HTML or an empty string if there are no errors.