| 1 |
<?php
|
| 2 |
// $Id$
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Computes what to show on the OpenAPI page.
|
| 7 |
*/
|
| 8 |
|
| 9 |
/**
|
| 10 |
* Handles the Open API.
|
| 11 |
*/
|
| 12 |
function _facebook_status_openapi() {
|
| 13 |
if (!flood_is_allowed('facebook_status_flood_user', variable_get('facebook_status_flood_user', 60))) {
|
| 14 |
echo t('You have exceeded the maximum number of open-API accesses per hour. Please try again later.');
|
| 15 |
}
|
| 16 |
flood_register_event('facebook_status_flood_user');
|
| 17 |
$name = $_GET['name'];
|
| 18 |
$count = $_GET['count'];
|
| 19 |
$type = $_GET['type'];
|
| 20 |
$format = $_GET['format'];
|
| 21 |
if ($format != 'json' && $format != 'xml' && $format != 'short' && $format) {
|
| 22 |
drupal_set_header('Content-Type: text/html; charset: utf-8');
|
| 23 |
echo t('invalid "format" parameter');
|
| 24 |
return;
|
| 25 |
}
|
| 26 |
if ((!is_numeric($count) || round($count) != $count || $count < 1 || $count > 25) && $format != 'short' && $count) {
|
| 27 |
drupal_set_header('Content-Type: text/html; charset: utf-8');
|
| 28 |
echo t('"count" parameter must be a positive integer no larger than 25');
|
| 29 |
return;
|
| 30 |
}
|
| 31 |
if (($type == 'tag' || $type == 'user' || $type == 'userref') && !$name) {
|
| 32 |
drupal_set_header('Content-Type: text/html; charset: utf-8');
|
| 33 |
echo t('if "type" is "tag" or "user" or "userref" you must use the "name" parameter as well');
|
| 34 |
return;
|
| 35 |
}
|
| 36 |
if ($type && $type != 'tag' && $type != 'user' && $type != 'public' && $type != 'userref' && $type != 'trend') {
|
| 37 |
drupal_set_header('Content-Type: text/html; charset: utf-8');
|
| 38 |
echo t('invalid "type" parameter');
|
| 39 |
return;
|
| 40 |
}
|
| 41 |
if (($type == 'tag' || $type == 'trend') && (!module_exists('facebook_status_tags') || !module_exists('taxonomy'))) {
|
| 42 |
drupal_set_header('Content-Type: text/html; charset: utf-8');
|
| 43 |
echo 'invalid "type" parameter';
|
| 44 |
return;
|
| 45 |
}
|
| 46 |
if ($type == 'userref' && !module_exists('facebook_status_tags')) {
|
| 47 |
drupal_set_header('Content-Type: text/html; charset: utf-8');
|
| 48 |
echo t('invalid "type" parameter');
|
| 49 |
return;
|
| 50 |
}
|
| 51 |
if ($type == 'trend' && $name && $name != 'day' && $name != 'week' && $name != 'month' && $name != 'year' && $name != 'all') {
|
| 52 |
drupal_set_header('Content-Type: text/html; charset: utf-8');
|
| 53 |
echo t('if the "name" parameter is specified with type "trend" then it must be one of "day" "week" "month" "year" "all"');
|
| 54 |
return;
|
| 55 |
}
|
| 56 |
if (!$type && !$name) {
|
| 57 |
$type = 'public';
|
| 58 |
}
|
| 59 |
if (!$type && $name) {
|
| 60 |
$type = 'user';
|
| 61 |
}
|
| 62 |
if (!$count || $format == 'short') {
|
| 63 |
$count = 1;
|
| 64 |
}
|
| 65 |
if (!$format) {
|
| 66 |
$format = 'short';
|
| 67 |
}
|
| 68 |
if ($type == 'user' || $type == 'userref') {
|
| 69 |
$account = user_load(array('name' => $name));
|
| 70 |
if (!$account->uid) {
|
| 71 |
drupal_set_header('Content-Type: text/html; charset: utf-8');
|
| 72 |
echo 'invalid name';
|
| 73 |
return;
|
| 74 |
}
|
| 75 |
}
|
| 76 |
|
| 77 |
$statuses = array();
|
| 78 |
$terms = array();
|
| 79 |
switch ($type) {
|
| 80 |
case 'tag':
|
| 81 |
$statuses = facebook_status_tags_get_statuses($name, 'term', $count);
|
| 82 |
break;
|
| 83 |
case 'userref':
|
| 84 |
$statuses = facebook_status_tags_get_statuses($name, 'user', $count);
|
| 85 |
break;
|
| 86 |
case 'user':
|
| 87 |
$statuses = facebook_status_get_status($account->uid, $account->uid, $count, 0, TRUE);
|
| 88 |
break;
|
| 89 |
case 'public':
|
| 90 |
$statuses = facebook_status_get_statuses(array(), array(), $count, FALSE);
|
| 91 |
break;
|
| 92 |
case 'trend':
|
| 93 |
$terms = facebook_status_tags_popular_terms($count, $name);
|
| 94 |
break;
|
| 95 |
}
|
| 96 |
if ((empty($statuses) && $type != 'trend') || (empty($terms) && $type == 'trend')) {
|
| 97 |
drupal_set_header('Content-Type: text/html; charset: utf-8');
|
| 98 |
echo t('no statuses match your criteria');
|
| 99 |
return;
|
| 100 |
}
|
| 101 |
$clean = array();
|
| 102 |
$xml = '';
|
| 103 |
foreach ($statuses as $status) {
|
| 104 |
$array = array(
|
| 105 |
'text' => check_plain($status->status),
|
| 106 |
'time' => $status->status_time,
|
| 107 |
'sid' => $status->sid,
|
| 108 |
'uid' => $status->uid,
|
| 109 |
);
|
| 110 |
$xml .= '
|
| 111 |
<status>
|
| 112 |
'. format_xml_elements($array) .'
|
| 113 |
</status>';
|
| 114 |
$clean[] = $array;
|
| 115 |
}
|
| 116 |
foreach ($terms as $term) {
|
| 117 |
$array = array(
|
| 118 |
'name' => check_plain($term->name),
|
| 119 |
'tid' => $term->tid,
|
| 120 |
'popularity' => $term->popularity,
|
| 121 |
);
|
| 122 |
$xml .= '
|
| 123 |
<term>
|
| 124 |
'. format_xml_elements($array) .'
|
| 125 |
</term>';
|
| 126 |
$clean[] = $array;
|
| 127 |
}
|
| 128 |
if ($format == 'xml') {
|
| 129 |
drupal_set_header('Content-Type: application/xml; charset: utf-8');
|
| 130 |
echo '<?xml version="1.0" encoding="UTF-8"?>
|
| 131 |
';
|
| 132 |
if ($type != 'trend') {
|
| 133 |
echo '<statuses>'. $xml .'
|
| 134 |
</statuses>';
|
| 135 |
}
|
| 136 |
else {
|
| 137 |
echo '<terms>'. $xml .'
|
| 138 |
</terms>';
|
| 139 |
}
|
| 140 |
}
|
| 141 |
else if ($format == 'json') {
|
| 142 |
drupal_json($clean);
|
| 143 |
}
|
| 144 |
else if ($format == 'short') {
|
| 145 |
drupal_set_header('Content-Type: text/html; charset: utf-8');
|
| 146 |
if ($type != 'trend') {
|
| 147 |
echo $clean[0]['text'];
|
| 148 |
}
|
| 149 |
else {
|
| 150 |
echo $clean[0]['name'];
|
| 151 |
}
|
| 152 |
}
|
| 153 |
}
|