upload.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. // Uploadify v1.6.2
  3. // Copyright (C) 2009 by Ronnie Garcia
  4. // Co-developed by Travis Nickels
  5. require_once('../../init.php');
  6. //include('../../../../config.php');
  7. $cmid = (int)$_GET['cmid'];
  8. if (!$cmid) {
  9. echo "Module ID missing";
  10. exit;
  11. }
  12. $itemid = (int)$_GET['itemid'];
  13. if (!$itemid) {
  14. echo "Module ID missing";
  15. exit;
  16. }
  17. $signature = $_REQUEST['signature'];
  18. $signeddata = $_REQUEST['signeddata'];
  19. if ($signature != sloodle_signature($signeddata)) {
  20. echo "Invalid signature";
  21. exit;
  22. }
  23. $bits = explode('-', $signeddata);
  24. if ($bits[2] != $cmid) {
  25. echo "Invalid signature";
  26. exit;
  27. }
  28. if (SLOODLE_IS_ENVIRONMENT_MOODLE_2) {
  29. $fs = get_file_storage();
  30. //$url = $CFG->wwwroot/pluginfile.php/$forumcontextid/mod_forum/post/$postid/image.jpg
  31. // Prepare file record object
  32. //$targetPath = $CFG->dataroot.'/'.SITEID.'/presenter/'.(int)$_GET["moduleId"].'/';
  33. $filename = str_replace(' ','_',$_FILES['Filedata']['name']);
  34. //$filename='edmanga3.jpg';
  35. $extension = '';
  36. if (preg_match('/^[A-Za-z0-9]+\.(.*?)$/', $filename, $matches)) {
  37. $extension = $matches[1];
  38. }
  39. $allowable = array('jpg','gif','png','mov','mpg');
  40. //get the extension of the file being uploaded
  41. $fileext = strtolower($extension);
  42. if (!in_array($fileext, $allowable)) {
  43. echo "This $filename file is not allowed.";
  44. exit();
  45. }
  46. $context = get_context_instance(CONTEXT_MODULE, $cmid);
  47. $contextid = $context->id;
  48. $fileinfo = array(
  49. 'contextid' => $contextid, // ID of context
  50. 'component' => 'mod_sloodle', // usually = table name
  51. 'filearea' => 'presenter', // usually = table name
  52. 'itemid' => $itemid, // usually = ID of row in table
  53. // 'filepath' => '/presenter', // any path beginning and ending in /
  54. 'filepath' => '/'.$contextid.'/mod_sloodle/presenter/'.$itemid.'/',
  55. 'filename' => $filename
  56. );
  57. $tmpfilename = $_FILES['Filedata']['tmp_name'] ;
  58. //$tmpfilename='/tmp/edmanga.jpg';
  59. $fs->create_file_from_pathname( $fileinfo, $tmpfilename);
  60. echo 1;
  61. } else {
  62. if (!empty($_FILES)) {
  63. $tempFile = $_FILES['Filedata']['tmp_name'];
  64. $targetPath = $CFG->dataroot.'/'.SITEID.'/'.$cmid.'/sloodle/presenter/';
  65. $targetFile = str_replace('//','/',$targetPath) . str_replace(' ','_',$_FILES['Filedata']['name']);
  66. //Server side security check
  67. //only allow images!!!
  68. //$allowable = array ('.jpg','.gif','.png','.htm','.html','.mov');
  69. $allowable = array('.jpg','.gif','.png','.mov','.mpg');
  70. $extension = '';
  71. if(preg_match('/^[A-Za-z0-9]\.(.*?)$/', $targetFile, $matches)) {
  72. $extension = $matches[2];
  73. }
  74. //get the extension of the file being uploaded
  75. $fileext = strtolower($extension);
  76. if (!in_array($fileext, $allowable)) {
  77. echo "This file is not allowed.";
  78. exit();
  79. }
  80. mkdir(str_replace('//','/',$targetPath), 0755, true);
  81. move_uploaded_file($tempFile,$targetFile);
  82. echo 1;
  83. // TODO: Change this to echo file path
  84. }
  85. }
  86. ?>