webcam_receiver.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. # Save the audio to a URL-accessible directory for playback.
  10. parse_str($_SERVER['QUERY_STRING'], $params);
  11. if (isset($params['webcamname']) && isset($params['webcamdir']) && isset($params['webcamuserid'])) {
  12. $webcamname = $params['webcamname'];
  13. $webcamdir = $params['webcamdir'];
  14. $webcamuserid = $params['webcamuserid'];
  15. } else {
  16. api_not_allowed();
  17. die();
  18. }
  19. if ($webcamuserid != api_get_user_id() || api_get_user_id() == 0 || $webcamuserid == 0) {
  20. api_not_allowed();
  21. die();
  22. }
  23. //clean
  24. $webcamname = Security::remove_XSS($webcamname);
  25. $webcamname = Database::escape_string($webcamname);
  26. $webcamname = addslashes(trim($webcamname));
  27. $webcamname = api_replace_dangerous_char($webcamname, 'strict');
  28. $webcamname = FileManager::disable_dangerous_file($webcamname);
  29. $webcamdir = Security::remove_XSS($webcamdir);
  30. //security extension
  31. $ext = explode('.', $webcamname);
  32. $ext = strtolower($ext[sizeof($ext) - 1]);
  33. if ($ext != 'jpg') {
  34. die();
  35. }
  36. //Do not use here check Fileinfo method because return: text/plain //CHECK THIS BEFORE COMMIT
  37. $dirBaseDocuments = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document';
  38. $saveDir = $dirBaseDocuments.$webcamdir;
  39. $current_session_id = api_get_session_id();
  40. $groupId = $_SESSION['_gid'];
  41. //avoid duplicates
  42. $webcamname_to_save = $webcamname;
  43. $title_to_save = str_replace('_', ' ', $webcamname);
  44. $webcamname_noex = basename($webcamname, ".jpg");
  45. if (file_exists($saveDir.'/'.$webcamname_noex.'.'.$ext)) {
  46. $i = 1;
  47. while (file_exists($saveDir.'/'.$webcamname_noex.'_'.$i.'.'.$ext)) {
  48. $i++;
  49. }
  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. //make a temporal file for get the file size
  62. $tmpfname = tempnam("/tmp", "CTF");
  63. $handle = fopen($tmpfname, "w");
  64. fwrite($handle, $content);
  65. fclose($handle);
  66. // Check if there is enough space in the course to save the file
  67. if (!DocumentManager::enough_space(filesize($tmpfname), DocumentManager::get_course_quota())) {
  68. unlink($tmpfname);
  69. die(get_lang('UplNotEnoughSpace'));
  70. }
  71. //erase temporal file
  72. unlink($tmpfname);
  73. //add to disk
  74. $fh = fopen($documentPath, 'w') or die("can't open file");
  75. fwrite($fh, $content);
  76. fclose($fh);
  77. //add document to database
  78. $doc_id = FileManager::add_document(
  79. $_course,
  80. $webcamdir.'/'.$webcamname_to_save,
  81. 'file',
  82. filesize($documentPath),
  83. $title_to_save
  84. );
  85. api_item_property_update(
  86. $_course,
  87. TOOL_DOCUMENT,
  88. $doc_id,
  89. 'DocumentAdded',
  90. $_user['user_id'],
  91. $groupId,
  92. null,
  93. null,
  94. null,
  95. $current_session_id
  96. );
  97. ///
  98. $url = 'http://'.$_SERVER['HTTP_HOST'].dirname($_SERVER['REQUEST_URI']).'/'.$documentPath;
  99. print "$url\n";
  100. ?>