blob: 07b2b21771e72cd77463993429f9480b63a86144 [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
11Initializing the Class
12======================
13
14Like most other classes in CodeIgniter, the Trackback class is
15initialized in your controller using the $this->load->library function::
16
17 $this->load->library('trackback');
18
19Once loaded, the Trackback library object will be available using:
20$this->trackback
21
22Sending Trackbacks
23==================
24
25A Trackback can be sent from any of your controller functions using code
26similar to this example::
27
Derek Jonese0da6042011-10-05 15:37:39 -050028 $this->load->library('trackback');
29
30 $tb_data = array(
31 'ping_url' => 'http://example.com/trackback/456',
32 'url' => 'http://www.my-example.com/blog/entry/123',
33 'title' => 'The Title of My Entry',
34 'excerpt' => 'The entry content.',
35 'blog_name' => 'My Blog Name',
36 'charset' => 'utf-8'
37 );
38
39 if ( ! $this->trackback->send($tb_data))
40 {
41 echo $this->trackback->display_errors();
42 }
43 else
44 {
45 echo 'Trackback was sent!';
46 }
Derek Jones8ede1a22011-10-05 13:34:52 -050047
48Description of array data:
49
50- **ping_url** - The URL of the site you are sending the Trackback to.
51 You can send Trackbacks to multiple URLs by separating each URL with
52 a comma.
53- **url** - The URL to YOUR site where the weblog entry can be seen.
54- **title** - The title of your weblog entry.
55- **excerpt** - The content of your weblog entry. Note: the Trackback
56 class will automatically send only the first 500 characters of your
57 entry. It will also strip all HTML.
58- **blog_name** - The name of your weblog.
59- **charset** - The character encoding your weblog is written in. If
60 omitted, UTF-8 will be used.
61
62The Trackback sending function returns TRUE/FALSE (boolean) on success
63or failure. If it fails, you can retrieve the error message using::
64
65 $this->trackback->display_errors();
66
67Receiving Trackbacks
68====================
69
70Before you can receive Trackbacks you must create a weblog. If you don't
71have a blog yet there's no point in continuing.
72
73Receiving Trackbacks is a little more complex than sending them, only
74because you will need a database table in which to store them, and you
75will need to validate the incoming trackback data. You are encouraged to
76implement a thorough validation process to guard against spam and
77duplicate data. You may also want to limit the number of Trackbacks you
78allow from a particular IP within a given span of time to further
79curtail spam. The process of receiving a Trackback is quite simple; the
80validation is what takes most of the effort.
81
82Your Ping URL
83=============
84
85In order to accept Trackbacks you must display a Trackback URL next to
86each one of your weblog entries. This will be the URL that people will
87use to send you Trackbacks (we will refer to this as your "Ping URL").
88
89Your Ping URL must point to a controller function where your Trackback
90receiving code is located, and the URL must contain the ID number for
91each particular entry, so that when the Trackback is received you'll be
92able to associate it with a particular entry.
93
94For example, if your controller class is called Trackback, and the
95receiving function is called receive, your Ping URLs will look something
96like this::
97
98 http://example.com/index.php/trackback/receive/entry_id
99
100Where entry_id represents the individual ID number for each of your
101entries.
102
103Creating a Trackback Table
104==========================
105
106Before you can receive Trackbacks you must create a table in which to
Derek Jonese0da6042011-10-05 15:37:39 -0500107store them. Here is a basic prototype for such a table::
Derek Jones8ede1a22011-10-05 13:34:52 -0500108
Derek Jonese0da6042011-10-05 15:37:39 -0500109 CREATE TABLE trackbacks (
110 tb_id int(10) unsigned NOT NULL auto_increment,
111 entry_id int(10) unsigned NOT NULL default 0,
112 url varchar(200) NOT NULL,
113 title varchar(100) NOT NULL,
114 excerpt text NOT NULL,
115 blog_name varchar(100) NOT NULL,
116 tb_date int(10) NOT NULL,
117 ip_address varchar(16) NOT NULL,
118 PRIMARY KEY `tb_id` (`tb_id`),
119 KEY `entry_id` (`entry_id`)
120 );
121
Derek Jones8ede1a22011-10-05 13:34:52 -0500122The Trackback specification only requires four pieces of information to
123be sent in a Trackback (url, title, excerpt, blog_name), but to make
124the data more useful we've added a few more fields in the above table
125schema (date, IP address, etc.).
126
127Processing a Trackback
128======================
129
130Here is an example showing how you will receive and process a Trackback.
131The following code is intended for use within the controller function
132where you expect to receive Trackbacks.
133
134::
135
Derek Jonese0da6042011-10-05 15:37:39 -0500136 $this->load->library('trackback');
137 $this->load->database();
138
139 if ($this->uri->segment(3) == FALSE)
140 {
141 $this->trackback->send_error("Unable to determine the entry ID");
142 }
143
144 if ( ! $this->trackback->receive())
145 {
146 $this->trackback->send_error("The Trackback did not contain valid data");
147 }
148
149 $data = array(
150 'tb_id' => '',
151 'entry_id' => $this->uri->segment(3),
152 'url' => $this->trackback->data('url'),
153 'title' => $this->trackback->data('title'),
154 'excerpt' => $this->trackback->data('excerpt'),
155 'blog_name' => $this->trackback->data('blog_name'),
156 'tb_date' => time(),
157 'ip_address' => $this->input->ip_address()
158 );
159
160 $sql = $this->db->insert_string('trackbacks', $data);
161 $this->db->query($sql);
162
163 $this->trackback->send_success();
Derek Jones8ede1a22011-10-05 13:34:52 -0500164
165Notes:
166^^^^^^
167
168The entry ID number is expected in the third segment of your URL. This
169is based on the URI example we gave earlier::
170
171 http://example.com/index.php/trackback/receive/entry_id
172
173Notice the entry_id is in the third URI segment, which you can retrieve
174using::
175
176 $this->uri->segment(3);
177
178In our Trackback receiving code above, if the third segment is missing,
179we will issue an error. Without a valid entry ID, there's no reason to
180continue.
181
182The $this->trackback->receive() function is simply a validation function
183that looks at the incoming data and makes sure it contains the four
184pieces of data that are required (url, title, excerpt, blog_name). It
185returns TRUE on success and FALSE on failure. If it fails you will issue
186an error message.
187
188The incoming Trackback data can be retrieved using this function::
189
190 $this->trackback->data('item')
191
192Where item represents one of these four pieces of info: url, title,
193excerpt, or blog_name
194
195If the Trackback data is successfully received, you will issue a success
196message using::
197
198 $this->trackback->send_success();
199
200.. note:: The above code contains no data validation, which you are
201 encouraged to add.