webcam_receiver.php 2.8 KB

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