summaryrefslogtreecommitdiff
path: root/src/ext/Identica.class.php
blob: a3e62a9d7c71520bdccf4719939312553fce37c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
<?php
/**
 * Copyright (c) <2009> Gianluca Urgese <g.urgese@jasone.it>
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */


/**
* The main identica-php class. Create an object to use your Identi.ca account from php. 
*/ 
class Identica {
	/** Username:password format string */
	private $credentials;
	
	/** Contains the last HTTP status code returned */
	private $http_status;
	
	/** Contains the last API call */
	private $last_api_call;
	
	/** Contains the application calling the API */
	private $application_source;

	/** 
    * Identi.ca class constructor. 
    * @param username is a alphanumeric string to perform login on Identi.ca. 
    * @param password is a alphanumeric string to perform login on Identi.ca. 
    * @param source is the name of your application. 
    * @return An Identica object to use to perform all the operation.
    */ 
	function Identica($username, $password, $source=false) {
		$this->credentials = sprintf("%s:%s", $username, $password);
		$this->application_source = $source;
	}
	
	/** 
    * Returns the 20 most recent statuses from non-protected users who have set a custom user icon. 
    * @param format is the extension for the result file (xml, json, rss, atom). 
    * @param since_id returns only statuses with an ID greater than (that is, more recent than) the specified ID.  
    * @return the public timeline in the specified format.
    */
	function getPublicTimeline($format, $since_id = 0) {
		$api_call = sprintf("http://identi.ca/api/statuses/public_timeline.%s", $format);
		if ($since_id > 0) {
			$api_call .= sprintf("?since_id=%d", $since_id);
		}
		return $this->APICall($api_call);
	}
	
	/** 
    * Returns the 20 most recent statuses posted by the authenticating user and that user's friends. 
    * @param format is the extension for the result file (xml, json, rss, atom). 
    * @param id returns only statuses from specified ID.
    * @param since returns only statuses with an ID greater than (that is, more recent than) the specified ID.  
    * @return the friends timeline in the specified format.
    */
	function getFriendsTimeline($format, $id = NULL, $since = NULL) {
		if ($id != NULL) {
			$api_call = sprintf("http://identi.ca/api/statuses/friends_timeline/%s.%s", $id, $format);
		}
		else {
			$api_call = sprintf("http://identi.ca/api/statuses/friends_timeline.%s", $format);
		}
		if ($since != NULL) {
			$api_call .= sprintf("?since=%s", urlencode($since));
		}
		return $this->APICall($api_call, true);
	}
	
	/** 
    * Returns the 20 most recent statuses posted from the authenticating user. 
    * @param format is the extension for the result file (xml, json, rss, atom). 
    * @param id get only statuses from specified ID.
    * @param count specifies the number of statuses to retrieve. May not be greater than 200.
    * @param since get only statuses with an ID greater than (that is, more recent than) the specified ID.  
    * @return the 20 most recent statuses posted from the authenticating user.
    */
	function getUserTimeline($format, $id = NULL, $count = 20, $since = NULL) {
		if ($id != NULL) {
			$api_call = sprintf("http://identi.ca/api/statuses/user_timeline/%s.%s", $id, $format);
		}
		else {
			$api_call = sprintf("http://identi.ca/api/statuses/user_timeline.%s", $format);
		}
		if ($count != 20) {
			$api_call .= sprintf("?count=%d", $count);
		}
		if ($since != NULL) {
			$api_call .= sprintf("%ssince=%s", (strpos($api_call, "?count=") === false) ? "?" : "&", urlencode($since));
		}
		return $this->APICall($api_call, true);
	}
	
	/** 
    * Returns a single status, specified by the id parameter below. The status's author will be returned inline. 
    * @param format is the extension for the result file (xml, json, rss, atom). 
    * @param id get only statuses from specified ID.
    * @return a single status, specified by the id parameter.
    */
	function showStatus($format, $id) {
		$api_call = sprintf("http://identi.ca/api/statuses/show/%d.%s", $id, $format);
		return $this->APICall($api_call);
	}
	
	/** 
    * Updates the authenticating user's status. Request must be a POST. Statuses over 140 characters will be forceably truncated.
    * @param status is the text of your status update. 
    * @return the current update from authenticating user.
    */
	function updateStatus($status) {
		$status = urlencode(stripslashes(urldecode($status)));
		$api_call = sprintf("http://identi.ca/api/statuses/update.xml?status=%s", $status);
		return $this->APICall($api_call, true, true);
	}
	
	/** 
    * Returns a list of replies from authenticating user. 
    * @param format is the extension for the result file (xml, json, rss, atom). 
    * @param page specifies the page of results to retrieve.
    * @return list of replies from authenticating user.
    */
	function getReplies($format, $page = 0) {
		$api_call = sprintf("http://identi.ca/api/statuses/replies.%s", $format);
		if ($page) {
			$api_call .= sprintf("?page=%d", $page);
		}
		return $this->APICall($api_call, true);
	}
	
	/** 
    * Destroys the status specified by the required ID parameter. The authenticating user must be the author of the specified status. 
    * @param format is the extension for the result file (xml, json, rss, atom). 
    * @param id the ID of the status to destroy.
    * @return a destroyed status specified by the id parameter.
    */
	function destroyStatus($format, $id) {
		$api_call = sprintf("http://identi.ca/api/statuses/destroy/%d.%s", $id, $format);
		return $this->APICall($api_call, true, true);
	}
	
	/** 
    * Returns a user's friends, each with current status inline. They are ordered by the order in which 
    * they were added as friends, 100 at a time. Use the page option to access older friends. With no 
    * user specified, request defaults to the authenticated user's friends. 
    * @param format is the extension for the result file (xml, json, rss, atom). 
    * @param id is the ID of the user for whom to request a list of friends.
    * @return a user's friends.
    */
	function getFriends($format, $id = NULL) {
		if ($id != NULL) {
			$api_call = sprintf("http://identi.ca/api/statuses/friends/%s.%s", $id, $format);
		}
		else {
			$api_call = sprintf("http://identi.ca/api/statuses/friends.%s", $format);
		}
		return $this->APICall($api_call, true);
	}
	
	/** 
    * Returns a user's followers. They are ordered by the order in which they joined Identi.ca, 100 at a time.   
    * @param format is the extension for the result file (xml, json, rss, atom). 
    * @param lite specified if status must be show.
    * @return a user's followers.
    */
	function getFollowers($format, $lite = NULL) {
		$api_call = sprintf("http://identi.ca/api/statuses/followers.%s%s", $format, ($lite) ? "?lite=true" : NULL);
		return $this->APICall($api_call, true);
	}
	
	/** 
    * Returns extended information of a given user, specified by ID or email as per the required id parameter.  
    * @param format is the extension for the result file (xml, json). 
    * @param id is the ID of specified user.
    * @param email is the email of specified user.
    * @return extended information of a given user.
    */
	function showUser($format, $id, $email = NULL) {
		if ($email == NULL) {
			$api_call = sprintf("http://identi.ca/api/users/show/%s.%s", $id, $format);
		}
		else {
			$api_call = sprintf("http://identi.ca/api/users/show.xml?email=%s", $email);
		}
		return $this->APICall($api_call, true);
	}
	
	/** 
    * Returns a list of the 20 most recent direct messages sent to the authenticating user. The XML 
    * and JSON versions include detailed information about the sending and recipient users. 
    * @param format is the extension for the result file (xml, json, rss, atom). 
    * @param since get only messages from an ID greater than (that is, more recent than) the specified ID.
    * @param since_id returns only statuses from the specified ID.
    * @param page Specifies the page of direct messages to retrieve.  
    * @return a list of the 20 most recent direct messages.
    */
	function getMessages($format, $since = NULL, $since_id = 0, $page = 1) {
		$api_call = sprintf("http://identi.ca/api/direct_messages.%s", $format);
		if ($since != NULL) {
			$api_call .= sprintf("?since=%s", urlencode($since));
		}
		if ($since_id > 0) {
			$api_call .= sprintf("%ssince_id=%d", (strpos($api_call, "?since") === false) ? "?" : "&", $since_id);
		}
		if ($page > 1) {
			$api_call .= sprintf("%spage=%d", (strpos($api_call, "?since") === false) ? "?" : "&", $page);
		}
		return $this->APICall($api_call, true);
	}
	
	/** 
    * Returns a list of the 20 most recent direct messages sent by the authenticating user. The XML 
    * and JSON versions include detailed information about the sending and recipient users. 
    * @param format is the extension for the result file (xml, json, rss, atom). 
    * @param since get only messages from an ID greater than (that is, more recent than) the specified ID.
    * @param since_id returns only statuses from the specified ID.
    * @param page specifies the page of direct messages to retrieve.  
    * @return a list of the 20 most recent sent direct messages.
    */
	function getSentMessages($format, $since = NULL, $since_id = 0, $page = 1) {
		$api_call = sprintf("http://identi.ca/api/direct_messages/sent.%s", $format);
		if ($since != NULL) {
			$api_call .= sprintf("?since=%s", urlencode($since));
		}
		if ($since_id > 0) {
			$api_call .= sprintf("%ssince_id=%d", (strpos($api_call, "?since") === false) ? "?" : "&", $since_id);
		}
		if ($page > 1) {
			$api_call .= sprintf("%spage=%d", (strpos($api_call, "?since") === false) ? "?" : "&", $page);
		}
		return $this->APICall($api_call, true);
	}
	
	/** 
    * Sends a new direct message to the specified user from the authenticating user. Request must be a POST.  
    * @param format is the extension for the result file (xml, json). 
    * @param user is the ID of specified user to send the message.
    * @param text is the text of your direct message.
    * @return the sent message in the requested format when successful.
    */
	function newMessage($format, $user, $text) {
		$text = urlencode(stripslashes(urldecode($text)));
		$api_call = sprintf("http://identi.ca/api/direct_messages/new.%s?user=%s&text=%s", $format, $user, $text);
		return $this->APICall($api_call, true, true);
	}
	
	/** 
    * Destroys the direct message specified in the required ID parameter. The authenticating user 
    * must be the recipient of the specified direct message.  
    * @param format is the extension for the result file (xml, json). 
    * @param id is the ID of specified direct message.
    * @return the message destroyed.
    */
	function destroyMessage($format, $id) {
		$api_call = sprintf("http://identi.ca/api/direct_messages/destroy/%s.%s", $id, $format);
		return $this->APICall($api_call, true, true);
	}
	
	/** 
    * Allows the authenticating users to follow the user specified in the ID parameter.   
    * @param format is the extension for the result file (xml, json). 
    * @param id is the ID of specified user.
    * @return the befriended user in the requested format when successful. Returns a string describing the 
    * failure condition when unsuccessful. If you are already friends with the user an HTTP 403 will be returned.
    */
	function createFriendship($format, $id) {
		$api_call = sprintf("http://identi.ca/api/friendships/create/%s.%s", $id, $format);
		return $this->APICall($api_call, true, true);
	}
	
	/** 
    * Allows the authenticating users to unfollow the user specified in the ID parameter.   
    * @param format is the extension for the result file (xml, json). 
    * @param id is the ID of specified user.
    * @return the unfollowed user in the requested format when successful. Returns a string 
    * describing the failure condition when unsuccessful.
    */
	function destroyFriendship($format, $id) {
		$api_call = sprintf("http://identi.ca/api/friendships/destroy/%s.%s", $id, $format);
		return $this->APICall($api_call, true, true);
	}
	
	/** 
    * Tests for the existence of friendship between two users.   
    * @param format is the extension for the result file (xml, json). 
    * @param user_a is the ID of the first specified user.
    * @param user_b is the ID of the second specified user.
    * @return true if user_a follows user_b, otherwise will return false.
    */
	function friendshipExists($format, $user_a, $user_b) {
		$api_call = sprintf("http://identi.ca/api/friendships/exists.%s?user_a=%s&user_b=%s", $format, $user_a, $user_b);
		return $this->APICall($api_call, true);
	}
	
	/** 
    * Tests if supplied user credentials are valid.   
    * @param format is the extension for the result file (xml, json). 
    * @return an HTTP 200 OK response code and a representation of the requesting user if authentication 
    * was successful; returns a 401 status code and an error message if not.
    */
	function verifyCredentials($format = NULL) {
		$api_call = sprintf("http://identi.ca/api/account/verify_credentials%s", ($format != NULL) ? sprintf(".%s", $format) : NULL);
		return $this->APICall($api_call, true);
	}
	
	/** 
    * Ends the session of the authenticating user, returning a null cookie.    
    * @return NULL
    */
	function endSession() {
		$api_call = "http://identi.ca/api/account/end_session";
		return $this->APICall($api_call, true);
	}
	
	/** 
    * Update user's location in the profile.   
    * @param location is the user's location . 
    * @return NULL.
    */
	function updateLocation($format, $location) {
		$api_call = sprintf("http://identi.ca/api/account/update_location.%s?location=%s", $format, $location);
		return $this->APICall($api_call, true, true);
	}
	
	/** 
    * Sets which device Identi.ca delivers updates to for the authenticating user.   
    * @param format is the extension for the result file (xml, json). 
    * @param device must be one of: sms, im, none.
    * @return user's profile details in a selected format.
    */
	function updateDeliveryDevice($format, $device) {
		$api_call = sprintf("http://identi.ca/api/account/update_delivery_device.%s?device=%s", $format, $device);
		return $this->APICall($api_call, true, true);
	}
	
	/** 
    * Returns the remaining number of API requests available to the requesting user before the API 
    * limit is reached for the current hour. Calls to rateLimitStatus() do not count against the rate limit.  
    * @param format is the extension for the result file (xml, json). 
    * @return remaining number of API requests.
    */
	function rateLimitStatus($format) {
		$api_call = sprintf("http://identi.ca/api/account/rate_limit_status.%s", $format);
		return $this->APICall($api_call, true);
	}
	
	/** 
    * Returns the 20 most recent favorite statuses for the authenticating user or user 
    * specified by the ID parameter in the requested format.
    * @param format is the extension for the result file (xml, json, rss, atom). 
    * @param id is the ID of specified user.
    * @param page specifies the page of favorites to retrieve.  
    * @return a list of the 20 most recent favorite statuses.
    */
	function getFavorites($format, $id = NULL, $page = 1) {
		if ($id == NULL) {
			$api_call = sprintf("http://identi.ca/api/favorites.%s", $format);
		}
		else {
			$api_call = sprintf("http://identi.ca/api/favorites/%s.%s", $id, $format);
		}
		if ($page > 1) {
			$api_call .= sprintf("?page=%d", $page);
		}
		return $this->APICall($api_call, true);
	}
	
	/** 
    * Favorites the status specified in the ID parameter as the authenticating user. 
    * @param format is the extension for the result file (xml, json). 
    * @param id is the ID of specified user.  
    * @return the favorite status when successful.
    */
	function createFavorite($format, $id) {
		$api_call = sprintf("http://identi.ca/api/favorites/create/%d.%s", $id, $format);
		return $this->APICall($api_call, true, true);
	}
	
	/** 
    * Un-favorites the status specified in the ID parameter as the authenticating user. 
    * @param format is the extension for the result file (xml, json). 
    * @param id is the ID of specified user.  
    * @return the un-favorited status in the requested format when successful.
    */
	function destroyFavorite($format, $id) {
		$api_call = sprintf("http://identi.ca/api/favorites/destroy/%d.%s", $id, $format);
		return $this->APICall($api_call, true, true);
	}
	
	/** 
    * Enables device notifications for updates from the specified user. 
    * @param format is the extension for the result file (xml, json). 
    * @param id is the ID of specified user.  
    * @return the specified user when successful.
    */
	function follow($format, $id) {
		$api_call = sprintf("http://identi.ca/api/notifications/follow/%d.%s", $id, $format);
		return $this->APICall($api_call, true, true);
	}
	
	/** 
    * Disables notifications for updates from the specified user to the authenticating user. 
    * @param format is the extension for the result file (xml, json). 
    * @param id is the ID of specified user.  
    * @return the specified user when successful.
    */
	function leave($format, $id) {
		$api_call = sprintf("http://identi.ca/api/notifications/leave/%d.%s", $id, $format);
		return $this->APICall($api_call, true, true);
	}
	
	/** 
    * Blocks the user specified in the ID parameter as the authenticating user. Destroys a friendship to the blocked user if it exists. 
    * @param format is the extension for the result file (xml, json). 
    * @param id is the ID of specified user.  
    * @return the blocked user in the requested format when successful.
    */
	function createBlock($format, $id) {
		$api_call = sprintf("http://identi.ca/api/blocks/create/%d.%s", $id, $format);
		return $this->APICall($api_call, true, true);
	}
	
	/** 
    * Un-blocks the user specified in the ID parameter for the authenticating user. 
    * @param format is the extension for the result file (xml, json). 
    * @param id is the ID of specified user.  
    * @return the un-blocked user in the requested format when successful.
    */
	function destroyBlock($format, $id) {
		$api_call = sprintf("http://identi.ca/api/blocks/destroy/%d.%s", $id, $format);
		return $this->APICall($api_call, true, true);
	}
	
	/** 
    * Returns true or false in the requested format with a 200 OK HTTP status code. 
    * @param format is the extension for the result file (xml, json).   
    * @return test results.
    */
	function test($format) {
		$api_call = sprintf("http://identi.ca/api/help/test.%s", $format);
		return $this->APICall($api_call, true);
	}
	
	private function APICall($api_url, $require_credentials = false, $http_post = false) {
		$curl_handle = curl_init();
		if($this->application_source){
			$api_url .= "&source=" . $this->application_source;
		}
		curl_setopt($curl_handle, CURLOPT_URL, $api_url);
		if ($require_credentials) {
			curl_setopt($curl_handle, CURLOPT_USERPWD, $this->credentials);
		}
		if ($http_post) {
			curl_setopt($curl_handle, CURLOPT_POST, true);
		}
		curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, TRUE);
		$identica_data = curl_exec($curl_handle);
		$this->http_status = curl_getinfo($curl_handle, CURLINFO_HTTP_CODE);
		$this->last_api_call = $api_url;
		curl_close($curl_handle);
		return $identica_data;
	}
	
	function lastStatusCode() {
		return $this->http_status;
	}
	
	function lastAPICall() {
		return $this->last_api_call;
	}
}
?>