webcam_receiver.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 '../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. }
  18. else {
  19. api_not_allowed();
  20. die();
  21. }
  22. if ($webcamuserid!= api_get_user_id() || api_get_user_id()==0 || $webcamuserid==0) {
  23. api_not_allowed();
  24. die();
  25. }
  26. //clean
  27. $webcamname = Security::remove_XSS($webcamname);
  28. $webcamname = Database::escape_string($webcamname);
  29. $webcamname = addslashes(trim($webcamname));
  30. $webcamname = api_replace_dangerous_char($webcamname);
  31. $webcamname = disable_dangerous_file($webcamname);
  32. $webcamdir = Security::remove_XSS($webcamdir);
  33. //security extension
  34. $ext = explode('.', $webcamname);
  35. $ext = strtolower($ext[sizeof($ext) - 1]);
  36. if($ext!= 'jpg'){
  37. die();
  38. }
  39. //Do not use here check Fileinfo method because return: text/plain //CHECK THIS BEFORE COMMIT
  40. $dirBaseDocuments = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document';
  41. $saveDir = $dirBaseDocuments.$webcamdir;
  42. $current_session_id = api_get_session_id();
  43. $groupId = api_get_group_id();
  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)) $i++;
  51. $webcamname_to_save = $webcamname_noex . '_' . $i . '.'.$ext;
  52. $title_to_save = $webcamname_noex . '_' . $i . '.'.$ext;
  53. $title_to_save = str_replace('_',' ',$title_to_save);
  54. }
  55. $documentPath = $saveDir.'/'.$webcamname_to_save;
  56. //read content
  57. //Change to move_uploaded_file() function instead file_get_contents() to adapt the new lib
  58. $content = move_uploaded_file($_FILES['webcam']['tmp_name'], $documentPath);
  59. if (!$content) {
  60. print "PHP ERROR: Failed to read data\n";
  61. exit();
  62. }
  63. //add document to database
  64. $doc_id = add_document($_course, $webcamdir.'/'.$webcamname_to_save, 'file', filesize($documentPath), $title_to_save);
  65. api_item_property_update($_course, TOOL_DOCUMENT, $doc_id, 'DocumentAdded', $_user['user_id'], $groupId, null, null, null, $current_session_id);
  66. ///
  67. $url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['REQUEST_URI']) . '/' . $documentPath;
  68. print get_lang('ClipSent');