PageRenderTime 36ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/public_html/uploader_v2.php

http://ditag.googlecode.com/
PHP | 439 lines | 393 code | 35 blank | 11 comment | 16 complexity | 84f2b64e8e6cbef9e8bd1b4966702c19 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. $self = $_SERVER['PHP_SELF'];
  3. require_once("db_login_test.php");
  4. require_once("DB.php");
  5. $cnx = connect_to_db();
  6. if (!$cnx) {
  7. header('Status: 500 No Database Connection');
  8. die("Couldn't connect to the database!");
  9. }
  10. if ($HTTP_POST_VARS['submit']) {
  11. // Check the authorization key
  12. if (strcmp($HTTP_POST_VARS['auth'], "a457c19eef") != 0) {
  13. //header('HTTP/1.1 400 Bad Authentication');
  14. header("Status: 400 Bad Authentication");
  15. echo "Bad Authentication";
  16. exit();
  17. }
  18. // Get the user ID
  19. global $user_id;
  20. $user_id = $HTTP_POST_VARS['user_id'];
  21. // See if this is a log
  22. if (isset($HTTP_POST_VARS['log'])) {
  23. $log_data = $HTTP_POST_VARS['log'];
  24. $log_query = "UPDATE user SET last_log_time=(DATE_ADD(NOW(),INTERVAL 2 HOUR)) WHERE user_id=$user_id";
  25. $log_res = $cnx->query($log_query);
  26. exit();
  27. }
  28. // Get the glucose reading
  29. if ( isset($HTTP_POST_VARS['glucose']) ) {
  30. $glucose_reading = $HTTP_POST_VARS['glucose'];
  31. if (strcmp($glucose_reading,"9999") == 0) {
  32. $glucose_reading="NULL";
  33. }
  34. }
  35. else {
  36. header('Status: 500 Don\'t understand format');
  37. echo "Don't understand format (no glucose found)";
  38. exit();
  39. }
  40. // Need to actually process pre-tags here!
  41. if ( isset($HTTP_POST_VARS['tags']) ) {
  42. $tag_str = $HTTP_POST_VARS['tags'];
  43. $tags = explode(";", $tag_str);
  44. error_log("tag_str is $tag_str");
  45. }
  46. else {
  47. header('Status: 500 Don\'t understand format');
  48. echo "Don't understand format (no pre_tags found)";
  49. exit();
  50. }
  51. // Process activity type
  52. if ( isset($HTTP_POST_VARS['activity_type']) ) {
  53. $activity_type = $HTTP_POST_VARS['activity_type'];
  54. }
  55. else {
  56. header('Status: 500 Don\'t understand format');
  57. echo "Don't understand format (no activity type found)";
  58. exit();
  59. }
  60. if (strcmp($activity_type, "Meal") == 0) {
  61. if ( isset($HTTP_POST_VARS['meal_descriptor']) ) {
  62. $meal_descriptor = $HTTP_POST_VARS['meal_descriptor'];
  63. $meal_data = explode(";", $meal_descriptor);
  64. $activity_type = "MEAL";
  65. } else {
  66. header('Status: 500 Don\'t understand format');
  67. echo "Don't understand format (no meal descriptor found)";
  68. exit();
  69. }
  70. } else if (strcmp($activity_type, "Exercise") == 0) {
  71. if ( isset($HTTP_POST_VARS['exercise_descriptor']) ) {
  72. $exercise_descriptor = $HTTP_POST_VARS['exercise_descriptor'];
  73. $exercise_data = explode(";", $exercise_descriptor);
  74. $activity_type = "EXERCISE";
  75. } else {
  76. header('Status: 500 Server Error');
  77. echo "Don't understand format (no exercise descriptor found)";
  78. exit();
  79. }
  80. } else if (strcmp($activity_type, "Other") == 0) {
  81. $activity_type = "OTHER";
  82. } else {
  83. header('Status: 500 Don\'t understand format');
  84. echo "Don't understand format (unrecognized activity type)";
  85. exit();
  86. }
  87. // ELSE must create an experience sample for user here !
  88. start_transaction("sample");
  89. $query1 = "INSERT INTO sample (time, user_id, activity_type, glucose,user_comment,cp_comment) VALUES " .
  90. "(DATE_ADD(NOW(),INTERVAL 2 HOUR), $user_id, \"$activity_type\", $glucose_reading, NULL, NULL)";
  91. $res = $cnx->query($query1);
  92. if (DB::isError($res)) {
  93. header("Status: 500 " . DB::errorMessage($res));
  94. end_transaction("sample");
  95. die("Error while adding sample to database (1)! " . " " . $query1 . " " . DB::errorMessage($res));
  96. }
  97. $query2 = "SELECT MAX(sample_id) FROM sample";
  98. $res = $cnx->query($query2);
  99. if (DB::isError($res)) {
  100. header("Status: 500 " . DB::errorMessage($res));
  101. end_transaction("sample");
  102. die("Error while finding max id!". DB::errorMessage($res));
  103. }
  104. end_transaction("sample");
  105. $q_data = $res->fetchRow();
  106. $this_sample_id = $q_data[0];
  107. mkdir("samples_test/sample_".$this_sample_id, 0755);
  108. mkdir("samples_test/sample_".$this_sample_id."/image", 0755);
  109. mkdir("samples_test/sample_".$this_sample_id."/audio", 0755);
  110. //echo "GOT THE FOLLOWING FILES: <br />";
  111. $num_valid_files = 0;
  112. foreach ($HTTP_POST_FILES as $name => $data) {
  113. //echo "Upload name is $name and temp name is " . $data["tmp_name"] . "<br />";
  114. $type_as_lower = strtolower($data["type"]);
  115. if ( !(strpos($type_as_lower,"amr") === FALSE) ) {
  116. $num_valid_files++;
  117. process_audio_file($data, $this_sample_id);
  118. }
  119. else if ( !(strpos($type_as_lower,"jpeg") === FALSE) ) {
  120. $num_valid_files++;
  121. process_image_file($data, $this_sample_id);
  122. }
  123. else {
  124. header("Status: 500 Unsupported Data Type");
  125. exit();
  126. }
  127. }
  128. /*
  129. // run the script that will convert the AMR -> MP3
  130. system("/home/moddwebc/usr/local/bin/mp3_create sample_".$this_sample_id);
  131. */
  132. switch($activity_type) {
  133. case "EXERCISE":
  134. $res = $cnx->query("INSERT INTO exercise (sample_id, calories, time) " .
  135. "VALUES ($this_sample_id, $exercise_data[0], $exercise_data[1])");
  136. if (DB::isError($res)) {
  137. //header("HTTP/1.1 500 Bad things", true, 500);
  138. //exit();
  139. header("Status: 500 " . DB::errorMessage($res));
  140. die("Error while adding exercise info to database!". DB::errorMessage($res));
  141. }
  142. break;
  143. case "MEAL":
  144. $res = $cnx->query("INSERT INTO meal (sample_id, carbohydrates, fat, protein) " .
  145. "VALUES ($this_sample_id, $meal_data[0], $meal_data[1], $meal_data[2])");
  146. if (DB::isError($res)) {
  147. header("Status: 500 " . DB::errorMessage($res));
  148. die("Error while adding meal info to database!". DB::errorMessage($res));
  149. }
  150. break;
  151. }
  152. error_log("gonna update tags");
  153. update_tags($tags);
  154. echo("ID:".$this_sample_id);
  155. }
  156. else {
  157. render_header();
  158. render_start();
  159. echo ('
  160. <form action="' . $self . '" method="POST" enctype="multipart/form-data">
  161. <br /><br />
  162. <input type="file" name="upload_file" />
  163. <br />
  164. <input type="submit" name="submit" value="submit" />
  165. <input type="hidden" name="user_id" value="4" />
  166. <input type="hidden" name="auth" value="a457c19eef" />
  167. <input type="hidden" name="glucose" value="9999" />
  168. <input type="hidden" name="activity_type" value="Exercise" />
  169. <input type="hidden" name="exercise_descriptor" value="2;3" />
  170. <input type="hidden" name="tags" value="once;yucky" />
  171. </form>
  172. ');
  173. /*
  174. echo ("<h3> The Images! </h3>");
  175. $query = "SELECT * FROM image";
  176. $res = mysql_query($query);
  177. while ($row = mysql_fetch_array($res, MYSQL_ASSOC)) {
  178. $img_path = $row['location'];
  179. echo "<img src=\"$img_path\"><br /> <br />";
  180. }
  181. */
  182. render_end();
  183. }
  184. // ADDED
  185. //exit();
  186. function start_transaction($table_name) {
  187. //echo "STARTING TRANSACTION";
  188. global $cnx;
  189. $cnx->autoCommit(false);
  190. $cnx->query("LOCK TABLES ".$table_name." WRITE");
  191. }
  192. function end_transaction($table_name) {
  193. //echo "ENDING TRANSACTION";
  194. global $cnx;
  195. $cnx->commit();
  196. $cnx->autoCommit(true);
  197. $cnx->query("UNLOCK TABLES");
  198. }
  199. function process_image_file($file_data, $sample_id) {
  200. global $cnx;
  201. //echo "GOT " . print_r($file_data) . "<br />";
  202. //echo "ID HERE IS $sample_id <br />";
  203. start_transaction("image");
  204. // need to figure out what the next id would be
  205. $query = "SELECT MAX(image_id) FROM image";
  206. $res = $cnx->query($query);
  207. $q_data = $res->fetchRow();
  208. if ($q_data[0]) {
  209. $max_id = $q_data[0];
  210. } else {
  211. $max_id = 0;
  212. }
  213. $next_id = $max_id + 1;
  214. //echo "next_id is $next_id <br />";
  215. $new_filename = "image_" . $next_id . ".jpg";
  216. $contents = file_get_contents($file_data['tmp_name']);
  217. $fhandle = fopen("samples_test/sample_".$sample_id."/image/".$new_filename, 'w');
  218. $write_res = fwrite($fhandle, base64_decode($contents));
  219. if ($write_res === FALSE) {
  220. header("Status: 500 " . "Couldn't write image to file" );
  221. exit();
  222. }
  223. else {
  224. fclose($fhandle);
  225. //copy($file_data['tmp_name'], "modd_photos/" . $new_filename);
  226. //echo "GOT FOR PUT_RES! $put_res<br />";
  227. //$query = 'INSERT INTO image (image_id,sample_id,location) VALUES '.
  228. //'(NULL,'.$sample_id.',"samples/sample_'.$sample_id.'/image/'.$new_filename.'")';
  229. $query = 'INSERT INTO image (image_id,sample_id,location) VALUES '.
  230. '('.$next_id.','.$sample_id.', "'.$new_filename.'")';
  231. //echo "QUERY IS <br />" . $query . "<br />OK!";
  232. $res = $cnx->query($query);
  233. //echo "GOT FOR INSERT RES " . print_r($res) . "<br />";
  234. }
  235. end_transaction("image");
  236. }
  237. function process_audio_file($file_data, $sample_id) {
  238. global $cnx;
  239. //echo "GOT " . print_r($file_data) . "<br />";
  240. //echo "ID HERE IS $sample_id <br />";
  241. start_transaction("audio");
  242. // need to figure out what the next id would be
  243. $query = "SELECT MAX(audio_id) FROM audio";
  244. $res = $cnx->query($query);
  245. $q_data = $res->fetchRow();
  246. if ($q_data[0]) {
  247. $max_id = $q_data[0];
  248. } else {
  249. $max_id = 0;
  250. }
  251. $next_id = $max_id + 1;
  252. //echo "next_id is $next_id <br />";
  253. $new_amr_filename = "audio_" . $next_id . ".amr";
  254. $contents = file_get_contents($file_data['tmp_name']);
  255. $fhandle = fopen("samples_test/sample_".$sample_id."/audio/".$new_amr_filename, 'w');
  256. $write_res = fwrite($fhandle, base64_decode($contents));
  257. if ($write_res === FALSE) {
  258. header("Status: 500 " . "Couldn't write audio to file" );
  259. exit();
  260. }
  261. else {
  262. fclose($fhandle);
  263. //copy($file_data['tmp_name'], "modd_photos/" . $new_filename);
  264. //echo "GOT FOR PUT_RES! $put_res<br />";
  265. //$query = 'INSERT INTO image (image_id,sample_id,location) VALUES '.
  266. //'(NULL,'.$sample_id.',"samples/sample_'.$sample_id.'/image/'.$new_filename.'")';
  267. $query = 'INSERT INTO audio (audio_id,sample_id,location) VALUES '.
  268. '('.$next_id.','.$sample_id.', "'.$new_amr_filename.'")';
  269. //echo "QUERY IS <br />" . $query . "<br />OK!";
  270. $res = $cnx->query($query);
  271. //echo "GOT FOR INSERT RES " . print_r($res) . "<br />";
  272. }
  273. end_transaction("audio");
  274. }
  275. function render_header() {
  276. echo ('<html xmlns="http://www.w3.org/1999/xhtml">
  277. <head>
  278. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
  279. <title>ModdWeb Uploader</title>
  280. </head>'
  281. );
  282. }
  283. function render_start() {
  284. echo ('<body>
  285. <h2> Welcome to MoDD </h2>
  286. <p> This form can be used for uploading samples. </p>
  287. ');
  288. }
  289. function render_end() {
  290. echo ('</body></html>');
  291. }
  292. function update_tags($pre_tag_list) {
  293. error_log("in update tags");
  294. error_log("pre tag list is . $pre_tag_list");
  295. global $cnx, $user_id, $this_sample_id;
  296. foreach ($pre_tag_list as $tag) {
  297. error_log("tag here is " . $tag);
  298. $res = $cnx->query("SELECT tag_id FROM tag WHERE text=\"$tag\"");
  299. start_transaction("tag");
  300. // if this is a new tag
  301. if ($res->numRows() == 0) {
  302. $cnx->query("INSERT INTO tag (user_id, text, uses) VALUES ".
  303. "($user_id, \"$tag\", 1)");
  304. $res = $cnx->query("SELECT MAX(tag_id) FROM tag");
  305. $q_data = $res->fetchRow();
  306. $tag_id = $q_data[0];
  307. } else {
  308. $q = $res->fetchRow();
  309. $tag_id = $q[0];
  310. $cnx->query("UPDATE tag SET uses=uses+1 WHERE tag_id=$tag_id");
  311. }
  312. end_transaction("tag");
  313. $cnx->query("INSERT INTO sample_tag (sample_id, tag_id) " .
  314. "VALUES ($this_sample_id, $tag_id)");
  315. }
  316. }
  317. ?>