webcam_receiver.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. /* JPEGCam Script */
  3. /* Receives JPEG webcam submission and saves to local file. */
  4. /* Make sure your directory has permission to write files as your web server user! */
  5. require_once '../../../inc/global.inc.php';
  6. ////Add security from Chamilo
  7. api_protect_course_script();
  8. api_block_anonymous_users();
  9. ///
  10. # Save the audio to a URL-accessible directory for playback.
  11. parse_str($_SERVER['QUERY_STRING'], $params);
  12. if(isset($params['webcamname']) && isset($params['webcamdir']) && isset($params['webcamuserid'])) {
  13. $webcamname = $params['webcamname'];
  14. $webcamdir = $params['webcamdir'];
  15. $webcamuserid = $params['webcamuserid'];
  16. }
  17. else {
  18. api_not_allowed();
  19. die();
  20. }
  21. if ($webcamuserid!= api_get_user_id() || api_get_user_id()==0 || $webcamuserid==0) {
  22. api_not_allowed();
  23. die();
  24. }
  25. //clean
  26. $webcamname = Security::remove_XSS($webcamname);
  27. $webcamname = Database::escape_string($webcamname);
  28. $webcamname = addslashes(trim($webcamname));
  29. $webcamname = api_replace_dangerous_char($webcamname);
  30. $webcamname = disable_dangerous_file($webcamname);
  31. $webcamdir = Security::remove_XSS($webcamdir);
  32. //security extension
  33. $ext = explode('.', $webcamname);
  34. $ext = strtolower($ext[sizeof($ext) - 1]);
  35. if($ext!= 'jpg'){
  36. die();
  37. }
  38. //Do not use here check Fileinfo method because return: text/plain //CHECK THIS BEFORE COMMIT
  39. $dirBaseDocuments = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document';
  40. $saveDir=$dirBaseDocuments.$webcamdir;
  41. $current_session_id = api_get_session_id();
  42. $groupId = api_get_group_id();
  43. //avoid duplicates
  44. $webcamname_to_save=$webcamname;
  45. $title_to_save=str_replace('_',' ',$webcamname);
  46. $webcamname_noex=basename($webcamname, ".jpg");
  47. if (file_exists($saveDir.'/'.$webcamname_noex.'.'.$ext)){
  48. $i = 1;
  49. while (file_exists($saveDir.'/'.$webcamname_noex.'_'.$i.'.'.$ext)) $i++;
  50. $webcamname_to_save = $webcamname_noex . '_' . $i . '.'.$ext;
  51. $title_to_save = $webcamname_noex . '_' . $i . '.'.$ext;
  52. $title_to_save = str_replace('_',' ',$title_to_save);
  53. }
  54. $documentPath = $saveDir.'/'.$webcamname_to_save;
  55. //read content
  56. $content = file_get_contents('php://input');
  57. if (!$content) {
  58. print "ERROR: Failed to read data\n";
  59. exit();
  60. }
  61. //add to disk
  62. $fh = fopen($documentPath, 'w') or die("can't open file");
  63. fwrite($fh, $content);
  64. fclose($fh);
  65. //
  66. //add document to database
  67. $doc_id = add_document($_course, $webcamdir.'/'.$webcamname_to_save, 'file', filesize($documentPath), $title_to_save);
  68. api_item_property_update($_course, TOOL_DOCUMENT, $doc_id, 'DocumentAdded', $_user['user_id'], $groupId, null, null, null, $current_session_id);
  69. ///
  70. $url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['REQUEST_URI']) . '/' . $documentPath;
  71. print "$url\n";
  72. ?>