save_pixlr.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. /**
  12. * Code
  13. */
  14. require_once __DIR__.'/../inc/global.inc.php';
  15. api_protect_course_script();
  16. api_block_anonymous_users();
  17. if ($_user['user_id'] != api_get_user_id() || api_get_user_id() == 0 || $_user['user_id'] == 0) {
  18. api_not_allowed();
  19. die();
  20. }
  21. if (!isset($_GET['title']) || !isset($_GET['type']) || !isset($_GET['image'])) {
  22. api_not_allowed();
  23. die();
  24. }
  25. if (!isset($_SESSION['paint_dir']) || !isset($_SESSION['whereami'])) {
  26. api_not_allowed();
  27. die();
  28. }
  29. //pixlr return
  30. $filename = Security::remove_XSS($_GET['title']); //The user preferred file name of the image.
  31. $extension = Security::remove_XSS($_GET['type']); //The image type, "pdx", "jpg", "bmp" or "png".
  32. $urlcontents = Security::remove_XSS($_GET['image']); //A URL to the image on Pixlr.com server or the raw file post of the saved image.
  33. //make variables
  34. $title = Database::escape_string(str_replace('_', ' ', $filename));
  35. $current_session_id = api_get_session_id();
  36. $groupId = api_get_group_id();
  37. $groupInfo = GroupManager::get_group_properties($groupId);
  38. $relativeUrlPath = $_SESSION['paint_dir'];
  39. $currentTool = $_SESSION['whereami'];
  40. $dirBaseDocuments = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document';
  41. $saveDir = $dirBaseDocuments.$_SESSION['paint_dir'];
  42. $contents = file_get_contents($urlcontents);
  43. //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
  44. /*
  45. if (strpos($urlcontents, "pixlr.com") === 0){
  46. echo "Invalid referrer";
  47. exit;
  48. }
  49. */
  50. //Security. Allway get from pixlr.com. Comment because for now this does not run
  51. /*
  52. $urlcontents1='http://pixlr.com/';
  53. $urlcontents2 = strstr($urlcontents, '_temp');
  54. $urlcontents_to_save=$urlcontents1.$urlcontents2;
  55. $contents = file_get_contents($urlcontents_to_save);//replace line 45.
  56. */
  57. //a bit title security
  58. $filename = addslashes(trim($filename));
  59. $filename = Security::remove_XSS($filename);
  60. $filename = api_replace_dangerous_char($filename);
  61. $filename = disable_dangerous_file($filename);
  62. if (strlen(trim($filename)) == 0) {
  63. echo "The title is empty"; //if title is empty, headers Content-Type = application/octet-stream, then not create a new title here please
  64. exit;
  65. }
  66. //check file_get_contents
  67. if ($contents === false) {
  68. echo "I cannot read: ".$urlcontents;
  69. exit;
  70. }
  71. // Extension security
  72. if ($extension != 'jpg' && $extension != 'png' && $extension != 'pxd') {
  73. die();
  74. }
  75. if ($extension == 'pxd') {
  76. echo "pxd file type does not supported"; // not secure because check security headers and finfo() return Content-Type = application/octet-stream
  77. exit;
  78. }
  79. //Verify that the file is an image. Headers method
  80. $headers = get_headers($urlcontents, 1);
  81. $content_type = explode("/", $headers['Content-Type']);
  82. if ($content_type[0] != "image") {
  83. echo "Invalid file type";
  84. exit;
  85. }
  86. //Verify that the file is an image. Fileinfo method
  87. $finfo = new finfo(FILEINFO_MIME);
  88. $current_mime = $finfo->buffer($contents);
  89. finfo_close($finfo);
  90. if (strpos($current_mime, 'image') === false) {
  91. echo "Invalid mime type file";
  92. exit;
  93. }
  94. //path, file and title
  95. $paintFileName = $filename.'.'.$extension;
  96. $title = $title.'.'.$extension;
  97. if ($currentTool == 'document/createpaint') {
  98. //check save as and prevent rewrite an older file with same name
  99. if (0 != $groupId) {
  100. $group_properties = GroupManager :: get_group_properties($groupId);
  101. $groupPath = $group_properties['directory'];
  102. } else {
  103. $groupPath = '';
  104. }
  105. if (file_exists($saveDir.'/'.$filename.'.'.$extension)) {
  106. $i = 1;
  107. while (file_exists($saveDir.'/'.$filename.'_'.$i.'.'.$extension)) $i++;
  108. $paintFileName = $filename.'_'.$i.'.'.$extension;
  109. $title = $filename.'_'.$i.'.'.$extension;
  110. }
  111. //
  112. $documentPath = $saveDir.'/'.$paintFileName;
  113. //add new document to disk
  114. file_put_contents($documentPath, $contents);
  115. //add document to database
  116. $doc_id = add_document($_course, $relativeUrlPath.'/'.$paintFileName, 'file', filesize($documentPath), $title);
  117. api_item_property_update(
  118. $_course,
  119. TOOL_DOCUMENT,
  120. $doc_id,
  121. 'DocumentAdded',
  122. $_user['user_id'],
  123. $groupInfo,
  124. null,
  125. null,
  126. null,
  127. $current_session_id
  128. );
  129. } elseif ($currentTool == 'document/editpaint') {
  130. $documentPath = $saveDir.'/'.$paintFileName;
  131. //add new document to disk
  132. file_put_contents($documentPath, $contents);
  133. //check path
  134. if (!isset($_SESSION['paint_file'])) {
  135. api_not_allowed();
  136. die();
  137. }
  138. if ($_SESSION['paint_file'] == $paintFileName) {
  139. $document_id = DocumentManager::get_document_id($_course, $relativeUrlPath.'/'.$paintFileName);
  140. update_existing_document($_course, $document_id, filesize($documentPath), null);
  141. api_item_property_update(
  142. $_course,
  143. TOOL_DOCUMENT,
  144. $document_id,
  145. 'DocumentUpdated',
  146. $_user['user_id'],
  147. $groupInfo,
  148. null,
  149. null,
  150. null,
  151. $current_session_id
  152. );
  153. } else {
  154. //add a new document
  155. $doc_id = add_document(
  156. $_course,
  157. $relativeUrlPath.'/'.$paintFileName,
  158. 'file',
  159. filesize($documentPath),
  160. $title
  161. );
  162. api_item_property_update(
  163. $_course,
  164. TOOL_DOCUMENT,
  165. $doc_id,
  166. 'DocumentAdded',
  167. $_user['user_id'],
  168. $groupInfo,
  169. null,
  170. null,
  171. null,
  172. $current_session_id
  173. );
  174. }
  175. }
  176. //delete temporal file
  177. $temp_file_2delete = $_SESSION['temp_realpath_image'];
  178. unlink($temp_file_2delete);
  179. //Clean sessions and return to Chamilo file list
  180. unset($_SESSION['paint_dir']);
  181. unset($_SESSION['paint_file']);
  182. unset($_SESSION['whereami']);
  183. unset($_SESSION['temp_realpath_image']);
  184. if (!isset($_SESSION['exit_pixlr'])) {
  185. $location = api_get_path(WEB_CODE_PATH).'document/document.php';
  186. echo '<script>window.parent.location.href="'.$location.'"</script>';
  187. api_not_allowed(true);
  188. } else {
  189. 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>';
  190. $location = api_get_path(WEB_CODE_PATH).'document/document.php?id='.Security::remove_XSS($_SESSION['exit_pixlr']);
  191. echo '<script>window.parent.location.href="'.$location.'"</script>';
  192. unset($_SESSION['exit_pixlr']);
  193. }