| 1 |
<?php
|
| 2 |
// $Id: bluga_api.inc,v 1.4.2.2 2009/11/02 07:07:18 hanenkamp Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* This file defines the aspects of the Bluga API that aren't directly apart of
|
| 7 |
* the Bluga module API. You may use these methods independently of the API if
|
| 8 |
* you want to access the Bluga interface directly.
|
| 9 |
*
|
| 10 |
* I have implemented all of these from scratch based upon the API documentation
|
| 11 |
* on the Bluga web site:
|
| 12 |
*
|
| 13 |
* http://webthumb.bluga.net/api
|
| 14 |
*
|
| 15 |
* I have done this so that I could include the API for use with the module (it
|
| 16 |
* is normally considered uncouth by the Drupal community to include 3rd Party
|
| 17 |
* libraries in your modules) and because none of the modules came under the
|
| 18 |
* GPL, but the LGPL.
|
| 19 |
*
|
| 20 |
* Besides, the API is pretty simple and it was a good way to get to know what I
|
| 21 |
* could and could not do. :-p
|
| 22 |
*/
|
| 23 |
|
| 24 |
define('BLUGA_API_END_POINT', 'http://webthumb.bluga.net/api.php');
|
| 25 |
|
| 26 |
function _bluga_post($payload) {
|
| 27 |
$headers = array('Content-Type' => 'text/xml');
|
| 28 |
$message = '<webthumb><apikey>' . variable_get('bluga_api_key', '') . '</apikey>';
|
| 29 |
$message .= $payload . "</webthumb>";
|
| 30 |
return drupal_http_request(BLUGA_API_END_POINT, $headers, 'POST', $message);
|
| 31 |
}
|
| 32 |
|
| 33 |
function _bluga_parse_request($response) {
|
| 34 |
$req_dom = new DOMDocument();
|
| 35 |
$req_dom->loadXML($response->data);
|
| 36 |
|
| 37 |
$jobs_dom = $req_dom->getElementsByTagName('job');
|
| 38 |
|
| 39 |
$jobs = array();
|
| 40 |
foreach ($jobs_dom as $job_dom) {
|
| 41 |
$job_id = $job_dom->textContent;
|
| 42 |
$jobs[$job_id] = array(
|
| 43 |
'id' => $job_id,
|
| 44 |
'estimate' => $job_dom->getAttribute('estimate'),
|
| 45 |
'time' => $job_dom->getAttribute('time'),
|
| 46 |
'url' => $job_dom->getAttribute('url'),
|
| 47 |
'cost' => $job_dom->getAttribute('cost'),
|
| 48 |
);
|
| 49 |
}
|
| 50 |
|
| 51 |
return $jobs;
|
| 52 |
}
|
| 53 |
|
| 54 |
/**
|
| 55 |
* This performs a Bluga Request, which asks the Bluga server to consume one or
|
| 56 |
* two tokens and create a thumbnail of the requested site. The request made
|
| 57 |
* depends completely upon the options passed.
|
| 58 |
*
|
| 59 |
* @param $options This is an array of options that may contain the following
|
| 60 |
* keys and values. You may note that this is very similar to the list of
|
| 61 |
* options that are passed by bluga_webthumb().
|
| 62 |
* <ul>
|
| 63 |
* <li>"url": This is the only required parameter. This is the URL you wish to
|
| 64 |
* take a screenshot of.</li>
|
| 65 |
* <li>"output_type": This must be either "jpg" or "png". If you choose "png",
|
| 66 |
* Bluga will charge you for 2 tokens on this request.</li>
|
| 67 |
* <li>"browser": This value is passed as an array, taking two values:
|
| 68 |
* <ul>
|
| 69 |
* <li>"width": This is the width of the browser window to open for the
|
| 70 |
* thumbnail. This must be within the range 15 to 1280.</li>
|
| 71 |
* <li>"height": This is the height of the browser window to open for the
|
| 72 |
* thumbnail. This must be within the range of 15 to 2048. If you use a browser
|
| 73 |
* height greater than 1024, you will be charged 2 tokens for this request.</li>
|
| 74 |
* </ul></li>
|
| 75 |
* <li>"fullthumb": This is either "0" or "1". A value of "0" is the default. If
|
| 76 |
* you set this to "1", you will have an additional thumbnail size "fullsize"
|
| 77 |
* available, which will be a fullsize screenshot of the web site at the
|
| 78 |
* dimensions set for the "browser" width and height.</li>
|
| 79 |
* <li>"custom": This value is passed as an array, taking two values. By
|
| 80 |
* specifying this option you will be charged 2 tokens. An extra thumbnail size
|
| 81 |
* will become available for this request, named "custom". The dimensions of
|
| 82 |
* this thumbnail will be as you specify. The values are:
|
| 83 |
* <ul>
|
| 84 |
* <li>"width": This is the pixel width you want for the custom thumbnail.</li>
|
| 85 |
* <li>"height": This is the pixel height you want for the custom
|
| 86 |
* thumbnail.</li>
|
| 87 |
* </ul></li>
|
| 88 |
* <li>"effect": By specifying this option you will be charged 2 tokens. You
|
| 89 |
* will be provided an additional thumbnail size named "effect". The size of
|
| 90 |
* that thumbnail depends upon the effect chosen. This thumbnail will always be
|
| 91 |
* a PNG file. The value of this option must be one of the following:
|
| 92 |
* <ul>
|
| 93 |
* <li>"mirror": This will apply a mirror effect to the thumbnail so you will
|
| 94 |
* see the reflection beneath the thumbnail as if it was a plate standing up
|
| 95 |
* perpendicular to a plane of glass. The image result will be 300x361 in
|
| 96 |
* size.</li>
|
| 97 |
* <li>"dropshadow": This will add a drop shadow behind the image. The image
|
| 98 |
* result will be 311x251 pixels.</li>
|
| 99 |
* <li>"border": This will add a thin black border around the thumbnail. The
|
| 100 |
* image result will be 302x242 in size.</li>
|
| 101 |
* </ul></li>
|
| 102 |
* <li>"delay": This is the delay in seconds between loading the web site and
|
| 103 |
* taken the snapshot. This defaults to "3" and must be within the range of 1 to
|
| 104 |
* 15 seconds.</li>
|
| 105 |
* <li>"excerpt": This value is passed as an array and allows you to modify the
|
| 106 |
* "excerpt" image size and location. It takes the following parameters:
|
| 107 |
* <ul>
|
| 108 |
* <li>"x": This is the horizontal position of the left edge of the
|
| 109 |
* excerpt.</li>
|
| 110 |
* <li>"y": This is the vertical position of the top edge of the excerpt.</li>
|
| 111 |
* <li>"width": This is the pixel width of the image to build. If this value
|
| 112 |
* exceeds "800" you will be charged 2 tokens.</li>
|
| 113 |
* <li>"height": This is the pixel height of the image to build. If this value
|
| 114 |
* exceeds "600" you will be charged 2 tokens.</li>
|
| 115 |
* </ul></li>
|
| 116 |
* </ul>
|
| 117 |
* @returns A string error message or an associative array of arrays describing
|
| 118 |
* the success of the call. The outer array will have keys that represent the
|
| 119 |
* job ID returned by the Bluga server and values with the returned status
|
| 120 |
* information. As of this version of the API, there will only be one key
|
| 121 |
* present. The inner array will have the following keys:
|
| 122 |
* <ul>
|
| 123 |
* <li>"id": This is the job ID assigned to the request by Bluga.</li>
|
| 124 |
* <li>"estimate": This is the number of seconds Bluga estimates until your job
|
| 125 |
* will be ready for fetching. It is recommended in the API docs that you wait
|
| 126 |
* at least half of this time before attempting to check the status of your
|
| 127 |
* request.</li>
|
| 128 |
* <li>"time": This is the time (in the US/Mountain time zone) that your request
|
| 129 |
* was received.</li>
|
| 130 |
* <li>"url": This is the URL that was requested.</li>
|
| 131 |
* <li>"cost": This is the number of tokens it cost to make this request. This
|
| 132 |
* will normally be 1, but may be 2 if you use one or more extra features that
|
| 133 |
* cost an additional token. It should not be more than 2 unless the API has
|
| 134 |
* changed since this writing.</li>
|
| 135 |
* </ul>
|
| 136 |
*/
|
| 137 |
function bluga_request($options) {
|
| 138 |
$payload .= "<request>";
|
| 139 |
foreach ($options as $option => $value) {
|
| 140 |
switch ($option) {
|
| 141 |
case 'custom':
|
| 142 |
$payload .= '<customThumbnail';
|
| 143 |
$payload .= ' width="' . $value['width'] . '"';
|
| 144 |
$payload .= ' height="' . $value['height'] . '"';
|
| 145 |
$payload .= '/>';
|
| 146 |
break;
|
| 147 |
case 'excerpt':
|
| 148 |
$payload .= "<$option>";
|
| 149 |
$payload .= "<x>" . $value['x'] . "</x>";
|
| 150 |
$payload .= "<y>" . $value['y'] . "</y>";
|
| 151 |
$payload .= "<height>" . $value['height'] . "</height>";
|
| 152 |
$payload .= "<width>" . $value['width'] . "</width>";
|
| 153 |
$payload .= "</$option>";
|
| 154 |
break;
|
| 155 |
case 'browser':
|
| 156 |
$payload .= '<width>' . $value['width'] . '</width>';
|
| 157 |
$payload .= '<height>' . $value['height'] . '</height>';
|
| 158 |
break;
|
| 159 |
case 'output_type':
|
| 160 |
$option = 'outputType';
|
| 161 |
default:
|
| 162 |
$payload .= "<$option>$value</$option>";
|
| 163 |
}
|
| 164 |
}
|
| 165 |
$payload .= "</request>";
|
| 166 |
|
| 167 |
$response = _bluga_post($payload);
|
| 168 |
|
| 169 |
if ($response->error) {
|
| 170 |
return $response->data;
|
| 171 |
}
|
| 172 |
else {
|
| 173 |
return _bluga_parse_request($response);
|
| 174 |
}
|
| 175 |
}
|
| 176 |
|
| 177 |
function _bluga_parse_status($response) {
|
| 178 |
$stat_dom = new DOMDocument();
|
| 179 |
$stat_dom->loadXML($response->data);
|
| 180 |
|
| 181 |
$status_dom = $stat_dom->getElementsByTagName('status');
|
| 182 |
|
| 183 |
$status = array();
|
| 184 |
foreach ($status_dom as $state_dom) {
|
| 185 |
$job_id = $state_dom->getAttribute('id');
|
| 186 |
$status[ $job_id ] = array(
|
| 187 |
'id' => $job_id,
|
| 188 |
'submissionTime' => $state_dom->getAttribute('submissionTime'),
|
| 189 |
'browserWidth' => $state_dom->getAttribute('browserWidth'),
|
| 190 |
'browserHeight' => $state_dom->getAttribute('browserHeight'),
|
| 191 |
'pickup' => $state_dom->getAttribute('pickup'),
|
| 192 |
'completionTime' => $state_dom->getAttribute('completionTime'),
|
| 193 |
'inProcess' => $state_dom->getAttribute('inProcess'),
|
| 194 |
'message' => $state_dom->textContent,
|
| 195 |
);
|
| 196 |
}
|
| 197 |
|
| 198 |
return $status;
|
| 199 |
}
|
| 200 |
|
| 201 |
/**
|
| 202 |
* This function allows you to check the status of an existing request.
|
| 203 |
*
|
| 204 |
* @param $jobs_urls This is an array of URLs or Job IDs (depending on the value
|
| 205 |
* of $type) that you wish to check the status of. This may also be a single Job
|
| 206 |
* ID or URL rather than an array.
|
| 207 |
* @param $type This is the type of status check to make. This must by one of
|
| 208 |
* the two following values:
|
| 209 |
* <ul>
|
| 210 |
* <li>"job": All the elements being checked are listed by Job ID</li>
|
| 211 |
* <li>"url": All the elements being checked are listed by URL</li>
|
| 212 |
* </ul>
|
| 213 |
* @returns An associative array of arrays. The outer array is keyed using the
|
| 214 |
* Job ID of each status item returned. The inner values are a list of options
|
| 215 |
* returned for each item whose status was requested. The keys will be:
|
| 216 |
* <ul>
|
| 217 |
* <li>"id": The Job ID of the request.</li>
|
| 218 |
* <li>"submissionTime": The date (in the US/Mountain time zone) that the
|
| 219 |
* request was originally received.</li>
|
| 220 |
* <li>"browserWidth": The width of the browser that was requested (as per the
|
| 221 |
* $options['browser']['width'] option in original request).</li>
|
| 222 |
* <li>"browserHeight": The height of the browser that was requested (as per the
|
| 223 |
* $options['browser']['height'] option in the original request).</li>
|
| 224 |
* <li>"pickup": A URL where all the files built for the request can be fetched
|
| 225 |
* in a zip file.</li>
|
| 226 |
* <li>"completionTime": This is set only if the request has been handled and is
|
| 227 |
* ready for pickup. This is the date (in the US/Mountain time zone) that the
|
| 228 |
* request was completed.</li>
|
| 229 |
* <li>"inProcess": This is set to "1" if the request is currently being
|
| 230 |
* processed or "0" otherwise.</li>
|
| 231 |
* <li>"message": This is the message returned for the job and will be set to
|
| 232 |
* "completed" if the job has finished.</li>
|
| 233 |
* </ul>
|
| 234 |
*/
|
| 235 |
function bluga_status($jobs_urls = array(), $type = 'job') {
|
| 236 |
if (!is_array($jobs_urls)) {
|
| 237 |
$jobs_urls = array($jobs_urls);
|
| 238 |
}
|
| 239 |
|
| 240 |
if (count($jobs_urls) == 0) {
|
| 241 |
$payload = "<status/>";
|
| 242 |
}
|
| 243 |
else {
|
| 244 |
$payload = '<status>';
|
| 245 |
foreach ($jobs_urls as $job_url) {
|
| 246 |
$payload .= "<$type>" . $job_url . "</$type>";
|
| 247 |
}
|
| 248 |
$payload .= '</status>';
|
| 249 |
}
|
| 250 |
|
| 251 |
$response = _bluga_post($payload);
|
| 252 |
|
| 253 |
if ($response->error) {
|
| 254 |
return $response->data;
|
| 255 |
}
|
| 256 |
else {
|
| 257 |
return _bluga_parse_status($response);
|
| 258 |
}
|
| 259 |
}
|
| 260 |
|
| 261 |
/**
|
| 262 |
* This performs an API call which will fetch the image or images that have been
|
| 263 |
* generated for a request from the Bluga server.
|
| 264 |
*
|
| 265 |
* @param $job_url This is the Job ID or URL of the request you wish to fetch.
|
| 266 |
* If you want to pass a URL, make sure to set $type appropriately.
|
| 267 |
* @param $size This is the kind of thumb you wish to fetch. It must be one of
|
| 268 |
* the following values:
|
| 269 |
* <ul>
|
| 270 |
* <li>"small": Fetch the small 80x60 image.</li>
|
| 271 |
* <li>"medium": Fetch the medium 160x120 image.</li>
|
| 272 |
* <li>"medium2": Fetch the medium 320x240 image.</li>
|
| 273 |
* <li>"large": Fetch the large 640x480 image.</li>
|
| 274 |
* <li>"excerpt": Fetch the excerpt image (usually 400x150, but can be
|
| 275 |
* customized during the request).</li>
|
| 276 |
* <li>"full": Fetch the fullsize image. This is only available if the
|
| 277 |
* "fullthumb" option is set during the original request.</li>
|
| 278 |
* <li>"custom": Fetch a custom sized thumbnail. This is only available if the
|
| 279 |
* "custom" option is set during the original request.</li>
|
| 280 |
* <li>"effect": Fetch a thumbnail with a special effect applied to it. This is
|
| 281 |
* only available if the "effect" option is set during the original
|
| 282 |
* request.</li>
|
| 283 |
* <li>"zip": Fetch a zip file containing all the images generated.</li>
|
| 284 |
* </ul>
|
| 285 |
* @param $type This is the job type. It is used to specify what kind of value
|
| 286 |
* was given in the $job_url parameter. It must be one of the following options:
|
| 287 |
* <ul>
|
| 288 |
* <li>"job": The $job_url is a Job ID. This is the default.</li>
|
| 289 |
* <li>"url": The $job_url is a URL.</li>
|
| 290 |
* </ul>
|
| 291 |
* @returns A $reponse object created by drupal_http_request(). The file
|
| 292 |
* returned will be available in $response->data as long as an error did not
|
| 293 |
* occur (i.e., $response->error is not set).
|
| 294 |
*/
|
| 295 |
function bluga_fetch($job_url, $size = 'medium', $type = 'job') {
|
| 296 |
$payload = '<fetch>';
|
| 297 |
$payload .= "<$type>" . $job_url . "</$type>";
|
| 298 |
$payload .= '<size>' . $size . '</size>';
|
| 299 |
$payload .= '</fetch>';
|
| 300 |
|
| 301 |
$response = _bluga_post($payload);
|
| 302 |
|
| 303 |
return $response;
|
| 304 |
}
|
| 305 |
|
| 306 |
function _bluga_parse_credit_status($response) {
|
| 307 |
$stat_dom = new DOMDocument();
|
| 308 |
$stat_dom->loadXML($response->data);
|
| 309 |
|
| 310 |
return array(
|
| 311 |
'used' => $stat_dom->getElementsByTagName('used-this-month')->item(0)->textContent,
|
| 312 |
'cached' => $stat_dom->getElementsByTagName('easythumb-cached-this-month')->item(0)->textContent,
|
| 313 |
'subscription' => $stat_dom->getElementsByTagName('subscription')->item(0)->textContent,
|
| 314 |
'reserve' => $stat_dom->getElementsByTagName('reserve')->item(0)->textContent,
|
| 315 |
);
|
| 316 |
}
|
| 317 |
|
| 318 |
/**
|
| 319 |
* This checks the status of your Bluga account.
|
| 320 |
*
|
| 321 |
* @returns An array describing your current account status. The following keys
|
| 322 |
* will be available:
|
| 323 |
* <ul>
|
| 324 |
* <li>"used": The number of credits you have used on your account this
|
| 325 |
* month.</li>
|
| 326 |
* <li>"cached": The number of easythumb thumbnails were served this month.</li>
|
| 327 |
* <li>"subscription": The number of expiring credits you receive each month
|
| 328 |
* (usually 100).</li>
|
| 329 |
* <li>"reserve": The remaining number of non-expiring credits that have been
|
| 330 |
* purchased for your account.</li>
|
| 331 |
* </ul>
|
| 332 |
*/
|
| 333 |
function bluga_credit_status() {
|
| 334 |
$payload = '<credits/>';
|
| 335 |
|
| 336 |
$response = _bluga_post($payload);
|
| 337 |
|
| 338 |
if ($response->error) {
|
| 339 |
return $response->data;
|
| 340 |
}
|
| 341 |
else {
|
| 342 |
return _bluga_parse_credit_status($response);
|
| 343 |
}
|
| 344 |
}
|