webcam_receiver.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. /* JPEGCam Script *****UPDATED to lib webcamJS 2015-09-04***** */
  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. //Changes on directory because move the proper script to the new lib upgrade directory
  6. require_once __DIR__.'/../inc/global.inc.php';
  7. ////Add security from Chamilo
  8. api_protect_course_script();
  9. api_block_anonymous_users();
  10. ///
  11. // Save the audio to a URL-accessible directory for playback.
  12. parse_str($_SERVER['QUERY_STRING'], $params);
  13. if (isset($params['webcamname']) && isset($params['webcamdir']) && isset($params['webcamuserid'])) {
  14. $webcamname = $params['webcamname'];
  15. $webcamdir = $params['webcamdir'];
  16. $webcamuserid = $params['webcamuserid'];
  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. $groupInfo = GroupManager::get_group_properties($groupId);
  44. //Avoid duplicates
  45. $webcamname_to_save = $webcamname;
  46. $title_to_save = str_replace('_', ' ', $webcamname);
  47. $webcamname_noex = basename($webcamname, ".jpg");
  48. if (file_exists($saveDir.'/'.$webcamname_noex.'.'.$ext)) {
  49. $i = 1;
  50. while (file_exists($saveDir.'/'.$webcamname_noex.'_'.$i.'.'.$ext)) {
  51. $i++;
  52. }
  53. $webcamname_to_save = $webcamname_noex.'_'.$i.'.'.$ext;
  54. $title_to_save = $webcamname_noex.'_'.$i.'.'.$ext;
  55. $title_to_save = str_replace('_', ' ', $title_to_save);
  56. }
  57. $documentPath = $saveDir.'/'.$webcamname_to_save;
  58. //read content
  59. //Change to move_uploaded_file() function instead file_get_contents() to adapt the new lib
  60. $content = move_uploaded_file($_FILES['webcam']['tmp_name'], $documentPath);
  61. if (!$content) {
  62. echo "PHP ERROR: Failed to read data\n";
  63. exit();
  64. }
  65. //add document to database
  66. $doc_id = add_document(
  67. $_course,
  68. $webcamdir.'/'.$webcamname_to_save,
  69. 'file',
  70. filesize($documentPath),
  71. $title_to_save
  72. );
  73. api_item_property_update(
  74. $_course,
  75. TOOL_DOCUMENT,
  76. $doc_id,
  77. 'DocumentAdded',
  78. $_user['user_id'],
  79. $groupInfo,
  80. null,
  81. null,
  82. null,
  83. $current_session_id
  84. );
  85. ///
  86. $url = 'http://'.$_SERVER['HTTP_HOST'].dirname($_SERVER['REQUEST_URI']).'/'.$documentPath;
  87. echo get_lang('ClipSent');