save_pixlr.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This file allows creating new svg and png documents with an online editor.
  5. *
  6. * @package chamilo.document
  7. *
  8. * @author Juan Carlos Raña Trabado
  9. * @since 30/january/2011
  10. */
  11. api_protect_course_script();
  12. api_block_anonymous_users();
  13. if ($_user['user_id']!= api_get_user_id() || api_get_user_id()==0 || $_user['user_id']==0) {
  14. api_not_allowed();
  15. die();
  16. }
  17. if(!isset($_GET['title']) || !isset($_GET['type']) || !isset($_GET['image'])) {
  18. api_not_allowed();
  19. die();
  20. }
  21. if(!isset($_SESSION['paint_dir']) || !isset($_SESSION['whereami']) ){
  22. api_not_allowed();
  23. die();
  24. }
  25. //pixlr return
  26. $filename=Security::remove_XSS($_GET['title']);//The user preferred file name of the image.
  27. $extension=Security::remove_XSS($_GET['type']);//The image type, "pdx", "jpg", "bmp" or "png".
  28. $urlcontents=Security::remove_XSS($_GET['image']);//A URL to the image on Pixlr.com server or the raw file post of the saved image.
  29. //make variables
  30. $title = Database::escape_string(str_replace('_',' ',$filename));
  31. $current_session_id = api_get_session_id();
  32. $groupId= api_get_group_id();
  33. $relativeUrlPath=$_SESSION['paint_dir'];
  34. $currentTool=$_SESSION['whereami'];
  35. $dirBaseDocuments = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document';
  36. $saveDir=$dirBaseDocuments.$_SESSION['paint_dir'];
  37. $contents = file_get_contents($urlcontents);
  38. //Security. Verify that the URL is pointing to a file @ pixlr.com domain or an ip @ pixlr.com. Comment because sometimes return a ip number
  39. /*
  40. if (strpos($urlcontents, "pixlr.com") === 0){
  41. echo "Invalid referrer";
  42. exit;
  43. }
  44. */
  45. //Security. Allway get from pixlr.com. Comment because for now this does not run
  46. /*
  47. $urlcontents1='http://pixlr.com/';
  48. $urlcontents2 = strstr($urlcontents, '_temp');
  49. $urlcontents_to_save=$urlcontents1.$urlcontents2;
  50. $contents = file_get_contents($urlcontents_to_save);//replace line 45.
  51. */
  52. //a bit title security
  53. $filename = addslashes(trim($filename));
  54. $filename = Security::remove_XSS($filename);
  55. $filename = api_replace_dangerous_char($filename);
  56. $filename = disable_dangerous_file($filename);
  57. if (strlen(trim($filename))==0) {
  58. echo "The title is empty";//if title is empty, headers Content-Type = application/octet-stream, then not create a new title here please
  59. exit;
  60. }
  61. //check file_get_contents
  62. if ($contents === false) {
  63. echo "I cannot read: ".$urlcontents;
  64. exit;
  65. }
  66. // Extension security
  67. if($extension!= 'jpg' && $extension!= 'png' && $extension!= 'pxd'){
  68. die();
  69. }
  70. if($extension=='pxd') {
  71. echo "pxd file type does not supported";// not secure because check security headers and finfo() return Content-Type = application/octet-stream
  72. exit;
  73. }
  74. //Verify that the file is an image. Headers method
  75. $headers = get_headers($urlcontents, 1);
  76. $content_type = explode("/", $headers['Content-Type']);
  77. if ($content_type[0] != "image") {
  78. echo "Invalid file type";
  79. exit;
  80. }
  81. //Verify that the file is an image. Fileinfo method
  82. $finfo = new finfo(FILEINFO_MIME);
  83. $current_mime=$finfo->buffer($contents);
  84. finfo_close($finfo);
  85. if(strpos($current_mime, 'image')===false) {
  86. echo "Invalid mime type file";
  87. exit;
  88. }
  89. //path, file and title
  90. $paintFileName = $filename.'.'.$extension;
  91. $title = $title.'.'.$extension;
  92. if($currentTool=='document/createpaint'){
  93. //check save as and prevent rewrite an older file with same name
  94. if (0 != $groupId){
  95. $group_properties = GroupManager :: get_group_properties($groupId);
  96. $groupPath = $group_properties['directory'];
  97. } else {
  98. $groupPath ='';
  99. }
  100. if (file_exists($saveDir.'/'.$filename.'.'.$extension)){
  101. $i = 1;
  102. while (file_exists($saveDir.'/'.$filename.'_'.$i.'.'.$extension)) $i++;
  103. $paintFileName = $filename . '_' . $i . '.'.$extension;
  104. $title = $filename . '_' . $i . '.'.$extension;
  105. }
  106. //
  107. $documentPath = $saveDir.'/'.$paintFileName;
  108. //add new document to disk
  109. file_put_contents( $documentPath, $contents );
  110. //add document to database
  111. $doc_id = add_document($_course, $relativeUrlPath.'/'.$paintFileName, 'file', filesize($documentPath), $title);
  112. api_item_property_update($_course, TOOL_DOCUMENT, $doc_id, 'DocumentAdded', $_user['user_id'], $groupId, null, null, null, $current_session_id);
  113. }elseif($currentTool=='document/editpaint'){
  114. $documentPath = $saveDir.'/'.$paintFileName;
  115. //add new document to disk
  116. file_put_contents( $documentPath, $contents );
  117. //check path
  118. if(!isset($_SESSION['paint_file'])){
  119. api_not_allowed();
  120. die();
  121. }
  122. if($_SESSION['paint_file']==$paintFileName){
  123. $document_id = DocumentManager::get_document_id($_course, $relativeUrlPath.'/'.$paintFileName);
  124. update_existing_document($_course, $document_id, filesize($documentPath), null);
  125. api_item_property_update($_course, TOOL_DOCUMENT, $document_id, 'DocumentUpdated', $_user['user_id'], $groupId, null, null, null, $current_session_id);
  126. }else{
  127. //add a new document
  128. $doc_id = add_document($_course, $relativeUrlPath.'/'.$paintFileName, 'file', filesize($documentPath), $title);
  129. api_item_property_update($_course, TOOL_DOCUMENT, $doc_id, 'DocumentAdded', $_user['user_id'], $groupId, null, null, null, $current_session_id);
  130. }
  131. }
  132. //delete temporal file
  133. $temp_file_2delete=$_SESSION['temp_realpath_image'];
  134. unlink($temp_file_2delete);
  135. //Clean sessions and return to Chamilo file list
  136. unset($_SESSION['paint_dir']);
  137. unset($_SESSION['paint_file']);
  138. unset($_SESSION['whereami']);
  139. unset($_SESSION['temp_realpath_image']);
  140. if (!isset($_SESSION['exit_pixlr'])) {
  141. $location=api_get_path(WEB_CODE_PATH).'document/document.php';
  142. echo '<script>window.parent.location.href="'.$location.'"</script>';
  143. api_not_allowed(true);
  144. } else {
  145. echo '<div align="center" style="padding-top:150; font-family:Arial, Helvetica, Sans-serif;font-size:25px;color:#aaa;font-weight:bold;">'.get_lang('PleaseStandBy').'</div>';
  146. $location=api_get_path(WEB_CODE_PATH).'document/document.php?id='.Security::remove_XSS($_SESSION['exit_pixlr']);
  147. echo '<script>window.parent.location.href="'.$location.'"</script>';
  148. unset($_SESSION['exit_pixlr']);
  149. }