Guest User

Untitled

a guest
Dec 12th, 2011
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.16 KB | None | 0 0
  1. <?php
  2. /**
  3. * Chevereto REST API
  4. * This file is the API implementation since Chevereto 2.0
  5. *
  6. * @version 1.2
  7. * @author Rodolfo Berríos <http://rodolfoberrios.com/>
  8. * @url <http://godlikestudio.com>
  9. * @package Chevereto <http://chevereto.com>
  10. *
  11. * @copyright Rodolfo Berríos <inbox@rodolfoberrios.com>
  12. *
  13. */
  14.  
  15. define('access', 'API');
  16.  
  17. $key = $_REQUEST['key'];
  18. $to_upload = $_REQUEST['upload'];
  19. $to_resize = $_REQUEST['resize_width'];
  20. $format = $_REQUEST['format'];
  21. $callback = $_REQUEST['callback'];
  22.  
  23. require_once('includes/chevereto.php');
  24. require_once(__CHV_PATH_CLASSES__.'class.upload.php');
  25.  
  26. /*** Die, die, die my darling ***/
  27. if(chevereto_config('api_key')=='my_api_key' and chevereto_config('api_mode')=='private') {
  28. chevereto_die(array('Open <code>includes/config.php</code>','Edit <code>$config[\'api_key\'] = \'my_api_key\';</code> with a different key.'), 'API key', array('It seems that you haven\'t changed the default api key, the API won\'t work until you fix this.'));
  29. }
  30.  
  31. /*** Checks the auth ***/
  32. if(api_mode('private') and api_key()!==$key) {
  33. $error_key_msg = 'Invalid API key';
  34. $ERROR_AUTH_API = array(
  35. 'status_code' => 403,
  36. 'status_txt' => $error_key_msg
  37. );
  38. switch($format) {
  39. default:
  40. case 'json':
  41. default:
  42. json_output($ERROR_AUTH_API, $callback);
  43. break;
  44. case 'xml':
  45. xml_output($ERROR_AUTH_API);
  46. break;
  47. case 'txt':
  48. echo $error_key_msg;
  49. break;
  50. }
  51. exit; // Shout the door
  52. }
  53.  
  54. /*** Observe the image request ***/
  55. if(is_url($to_upload)) {
  56. $api_remote_upload = true;
  57. } else {
  58. if(check_value($_REQUEST['upload']) or check_value($_FILES['upload'])) {
  59. // Creates the temp image
  60. $api_temp_name = __CHV_PATH_IMAGES__.generateRandomString(16).'.temp';
  61. while(file_exists($api_temp_name)) {
  62. $api_temp_name = __CHV_PATH_IMAGES__.generateRandomString(16).'.temp';
  63. }
  64. if(check_value($_FILES['upload']['tmp_name'])) {
  65. $to_upload = $_FILES['upload'];
  66. } else {
  67. // Handles the stream
  68. $fh = fopen($api_temp_name,'w');
  69. stream_filter_append($fh,'convert.base64-decode',STREAM_FILTER_WRITE);
  70. fwrite($fh, $_REQUEST['upload']);
  71. fclose($fh);
  72. // Since all the validations works with $_FILES, we're going to emulate it.
  73. $to_upload = array(
  74. 'name' => generateRandomString(5).'.jpg',
  75. 'type' => 'image/jpeg',
  76. 'tmp_name' => $api_temp_name,
  77. 'error' => 'UPLOAD_ERR_OK',
  78. 'size' => '1'
  79. );
  80. }
  81.  
  82. }
  83. }
  84.  
  85. $api_upload = new Upload($to_upload);
  86. if($api_remote_upload) $api_upload->is_remote = true;
  87.  
  88. $api_upload->img_upload_path = __CHV_PATH_IMAGES__;
  89. $api_upload->thumb_upload_path = __CHV_PATH_THUMBS__;
  90.  
  91. $api_upload->resize_width = $to_resize;
  92. $api_upload->thumb_width = chevereto_config('thumb_width');
  93. $api_upload->thumb_height = chevereto_config('thumb_height');
  94.  
  95. $api_upload->max_size = mb_to_bytes(chevereto_config('max_size_mb'));
  96.  
  97. /*** Do the thing? ***/
  98. if($api_upload->process()) {
  99.  
  100. $api_status_code = 200;
  101. $api_status_txt = 'OK';
  102. // Build the data array
  103. $api_data_array = $api_upload->image_info;
  104. if($api_upload->is_remote) {
  105. $api_data_array['source'] = $to_upload;
  106. } else {
  107. $api_data_array['source'] = 'base64 image string';
  108. }
  109. $api_data_array['resized'] = check_value($to_resize) ? '1' : '0';
  110. $api_txt_output = $api_upload->image_info['img_url'];
  111.  
  112. // Short URL generation
  113. if(is_config_short_url()) {
  114. require_once(__CHV_PATH_INCLUDES__.'shorturl.php');
  115. $api_data_array['shorturl'] = $ShortURL->get_ShortURL($api_upload->image_info['img_url']);
  116. }
  117.  
  118. } else {
  119. $api_status_code = 403;
  120. $api_status_txt = $api_upload->error;
  121. $api_txt_output = $api_status_txt;
  122. }
  123.  
  124. $REST_API = array(
  125. 'status_code' => $api_status_code,
  126. 'status_txt' => $api_status_txt,
  127. 'data' => $api_data_array
  128. );
  129.  
  130. $OUTPUT_REST_API = array_filter($REST_API);
  131.  
  132. switch($format) {
  133. default:
  134. case 'json':
  135. default:
  136. json_output($OUTPUT_REST_API, $callback);
  137. break;
  138. case 'xml':
  139. xml_output($OUTPUT_REST_API);
  140. break;
  141. case 'txt':
  142. echo $api_txt_output;
  143. break;
  144. case 'redirect':
  145. $redirect_url = __CHV_BASE_URL__.'?v='.$api_upload->image_info['img_name'];
  146. header("Location: $redirect_url");
  147. break;
  148. }
  149.  
  150. ?>
  151.  
Add Comment
Please, Sign In to add comment