PageRenderTime 56ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/www/static_pages/upload.php

https://github.com/kkszysiu/zoto-server
PHP | 262 lines | 165 code | 32 blank | 65 comment | 28 complexity | f6b534d0431c2d213bff45e856ed91e9 MD5 | raw file
  1. <?php
  2. /*
  3. * upload.php
  4. * Added: 02/23/07
  5. * Author: Kord Campbell
  6. *
  7. * This file provides POST uploading of multiple images via PHP. It
  8. * uses XML-RPC calls to connect to AZTK and add the images to the user's
  9. * account.
  10. *
  11. * Similar functionality is provided here as in PXN8 module's save modified
  12. * call to ZAPI.
  13. *
  14. * Includes a standard set of functions for talking via xmlrpc to AZTK
  15. * using PHP.
  16. *
  17. * Although some xmlrpc functions are contained in this library
  18. * (urlpost and xmlrpc_request), the function starting with aztk_
  19. * should be all you use for talking to aztk.
  20. */
  21. function urlpost($host, $port, $location, &$data) {
  22. /*
  23. * This method sends a very basic HTTP POST to a host, port, and location,
  24. * sending it the $data as a string. It's very basic. Returns a string of
  25. * the resulting data.
  26. *
  27. * WARNING: Currently, this function dies upon failure to connect.
  28. */
  29. $content_length = strlen($data);
  30. $headers = "POST $location HTTP/1.0\r\n" .
  31. "Host: $host\r\n" .
  32. "Connection: close\r\n" .
  33. "User-Agent: Zoto PHP XMLRPC Client\r\n" .
  34. "Content-Type: text/xml\r\n" .
  35. "Content-Length: $content_length" .
  36. "\r\n\r\n";
  37. $c = fsockopen($host, $port);
  38. if (!$c) {
  39. trigger_error("Unable to connect to AZTK Server: $host:$port");
  40. } else {
  41. fputs($c, $headers);
  42. fputs($c, $data);
  43. $returndata = "";
  44. while(!feof($c)){
  45. $returndata .= fgets($c, 1024);
  46. }
  47. fclose($c);
  48. return $returndata;
  49. }
  50. }
  51. function xmlrpc_request($host, $port, $location, $function, &$request_data) {
  52. /*
  53. * This method sends a very basic XML-RPC request. $host, $port, $location,
  54. * and $function are pretty simple. $request_data can be any PHP type,
  55. * and this function returns the PHP type of whatever the xmlrpc function
  56. * returns.
  57. *
  58. * WARNING: This function dies upon failure.
  59. */
  60. // Send request
  61. $request_xml = xmlrpc_encode_request($function, $request_data);
  62. // Split out response into headers, and xml
  63. $response = urlpost($host, $port, $location, $request_xml);
  64. $response_array = split("\r\n\r\n", $response, 2);
  65. $response_headers = split("\r\n", $response_array[0]);
  66. $response_xml = $response_array[1];
  67. $http_array = split(" ", $response_headers[0], 3);
  68. if ($http_array[1] != "200") {
  69. trigger_error("xmlrpc request failed: ({$http_array[1]}, {$http_array[2]}) at $host:$port using $location");
  70. } else {
  71. // Get native PHP types and return them for data.
  72. $response_data = xmlrpc_decode_request($response_xml, $function);
  73. return $response_data;
  74. }
  75. }
  76. function zapi_call($key, $auth, $function, $request_data) {
  77. global $zapi_hostname;
  78. global $zapi_port;
  79. global $zapi_location;
  80. $signature = array($key, $auth);
  81. foreach($request_data as $foo) {
  82. array_push($signature, $foo);
  83. }
  84. $return_data = xmlrpc_request($zapi_hostname, $zapi_port, $zapi_location, $function, $signature);
  85. if (is_array($return_data)) {
  86. if ($return_data['faultString']) {
  87. logDebug("failure in zapi_call");
  88. logDebug($return_data['faultString']);
  89. header("HTTP/1.0 500 Internal Server Error");
  90. print $return_data['faultString'];
  91. stop_debugging();
  92. die();
  93. }
  94. }
  95. return $return_data;
  96. }
  97. /*
  98. * start_debugging
  99. * If the debugging flag: $debugging is set to true, this function opens a file to save log messages.
  100. */
  101. function start_debugging(){
  102. global $debugging;
  103. global $log_path;
  104. global $log_ref;
  105. if($debugging){
  106. $log_ref = fopen($log_path, 'a');
  107. }
  108. }
  109. /*
  110. * logDebug
  111. * if the debugging flag: $debugging is set to true, accepts a status message as a string and
  112. * appends it to the log file opened by start_debugging
  113. * and echoes the message to the output.
  114. */
  115. function logDebug($str){
  116. global $debugging;
  117. global $log_ref;
  118. if($debugging){
  119. fwrite($log_ref, $str."\n\r");
  120. echo($str.'<br><br>');
  121. }
  122. }
  123. /*
  124. * stop_debugging
  125. * closes the debugging log file
  126. */
  127. function stop_debugging(){
  128. global $debugging;
  129. global $log_ref;
  130. if($debugging){
  131. fclose($log_ref);
  132. }
  133. }
  134. /* debug to file cause flash has no clue */
  135. $log_path = '/zoto/apache_web/static_pages/debug.txt';
  136. $log_ref = null;
  137. $upload_success = false;
  138. /* split out our domain name */
  139. $domain = split("[.:]", $_SERVER['HTTP_HOST']);
  140. $domain = ($domain[1] . "." . $domain[2]);
  141. /*if we're on org we want to turn on the debugging flag*/
  142. $debugging = (strpos($_SERVER['HTTP_HOST'],'.org') === false )?false:true;
  143. start_debugging();
  144. /* get our username and token from the cookie */
  145. $auth = null;
  146. if(array_key_exists('auth_hash', $_COOKIE)){
  147. logDebug("auth by cookie");
  148. $auth = split("[:]", $_COOKIE['auth_hash']);
  149. } else if(array_key_exists('auth', $_GET)){
  150. logDebug('auth by get');
  151. $auth = split("[:]", $_GET['auth']);
  152. } else {
  153. logDebug("Unable to auth user");
  154. trigger_error("Unable to auth user.");
  155. }
  156. $auth_username = $auth[0];
  157. $auth_token = $auth[2];
  158. /* set up our ZAPI call parameters */
  159. //$zapi_hostname = $_SERVER['SERVER_ADDR'];
  160. //$zapi_hostname = "www.zoto.com";
  161. $zapi_hostname = "www.".$domain;
  162. $zapi_port = 80;
  163. $zapi_location = "/RPC2";
  164. $zapi_key = "5d4a65c46a072a4542a816f2f28bd01a";
  165. $zapi_auth = array("username"=>$auth_username, "token"=>$auth_token);
  166. $zapi_function = "images.add";
  167. $uploaddir = '/zoto/apache_web/static_pages/uploads/';
  168. $uploadfile = $uploaddir . basename($_FILES['Filedata']['name']);
  169. logDebug($uploadfile);
  170. $image_filename = basename($_FILES['Filedata']['name']);
  171. $media_id = '';
  172. $error_msg = 'there was a problem with the server';
  173. logDebug('number of files uploaded ' . count($_FILES));
  174. foreach ($_FILES as $key => $value) {
  175. logDebug("Key: $key; Value: $value");
  176. }
  177. /* here's where we actually do the work */
  178. if($auth[0] != null){
  179. if (move_uploaded_file($_FILES['Filedata']['tmp_name'], $uploadfile)) {
  180. logDebug("File is valid, and was successfully uploaded.");
  181. /* open and read in the image file data */
  182. $handle = fopen($uploadfile, "r");
  183. $media_binary = fread($handle, filesize($uploadfile));
  184. xmlrpc_set_type(&$media_binary, "base64");
  185. fclose($handle);
  186. unlink($uploadfile);
  187. /* build the args for the XML-RPC query */
  188. $zapi_query = array($image_filename, $image_filename, "", $media_binary);
  189. /* send it to the zoto server */
  190. $result = zapi_call($zapi_key, $zapi_auth, $zapi_function, $zapi_query);
  191. if (is_array($result)) {
  192. foreach ($result as $key => $value) {
  193. logDebug("Key: $key; Value: $value");
  194. }
  195. if($result[0] == '0'){
  196. $upload_success = true;
  197. global $media_id;
  198. $media_id = $result[1];
  199. } else if($result[0] == '-1'){
  200. global $error_msg;
  201. $error_msg = $result[1];
  202. }
  203. }
  204. } else {
  205. logDebug("no file specified for upload!");
  206. $error_msg = 'no file was specified to upload';
  207. }
  208. } else {
  209. logDebug("User was not authed");
  210. $error_msg = 'you must be logged in to upload photos';
  211. }
  212. stop_debugging();
  213. ?>
  214. <html>
  215. <head>
  216. <script>
  217. <?php
  218. /*
  219. The parent window can check for the success of the upload by the existance of the "success" variable and that its first item is a 0.
  220. */
  221. if($upload_success){
  222. echo("var success = [0,{'filename':'$image_filename', 'media_id':'$media_id'}];");
  223. } else {
  224. echo("var success = [-1,{'filename':'$image_filename', 'msg':'$error_msg'}];");
  225. }
  226. ?>
  227. </script>
  228. </head>
  229. <body></body>
  230. </html>