save_pixlr.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. if (strpos($current_mime, 'image') === false) {
  90. echo "Invalid mime type file";
  91. exit;
  92. }
  93. //path, file and title
  94. $paintFileName = $filename.'.'.$extension;
  95. $title = $title.'.'.$extension;
  96. if ($currentTool == 'document/createpaint') {
  97. //check save as and prevent rewrite an older file with same name
  98. if (0 != $groupId) {
  99. $group_properties = GroupManager :: get_group_properties($groupId);
  100. $groupPath = $group_properties['directory'];
  101. } else {
  102. $groupPath = '';
  103. }
  104. if (file_exists($saveDir.'/'.$filename.'.'.$extension)) {
  105. $i = 1;
  106. while (file_exists($saveDir.'/'.$filename.'_'.$i.'.'.$extension)) $i++;
  107. $paintFileName = $filename.'_'.$i.'.'.$extension;
  108. $title = $filename.'_'.$i.'.'.$extension;
  109. }
  110. //
  111. $documentPath = $saveDir.'/'.$paintFileName;
  112. //add new document to disk
  113. file_put_contents($documentPath, $contents);
  114. //add document to database
  115. $doc_id = add_document($_course, $relativeUrlPath.'/'.$paintFileName, 'file', filesize($documentPath), $title);
  116. api_item_property_update(
  117. $_course,
  118. TOOL_DOCUMENT,
  119. $doc_id,
  120. 'DocumentAdded',
  121. $_user['user_id'],
  122. $groupInfo,
  123. null,
  124. null,
  125. null,
  126. $current_session_id
  127. );
  128. } elseif ($currentTool == 'document/editpaint') {
  129. $documentPath = $saveDir.'/'.$paintFileName;
  130. //add new document to disk
  131. file_put_contents($documentPath, $contents);
  132. //check path
  133. if (!isset($_SESSION['paint_file'])) {
  134. api_not_allowed();
  135. die();
  136. }
  137. if ($_SESSION['paint_file'] == $paintFileName) {
  138. $document_id = DocumentManager::get_document_id($_course, $relativeUrlPath.'/'.$paintFileName);
  139. update_existing_document($_course, $document_id, filesize($documentPath), null);
  140. api_item_property_update(
  141. $_course,
  142. TOOL_DOCUMENT,
  143. $document_id,
  144. 'DocumentUpdated',
  145. $_user['user_id'],
  146. $groupInfo,
  147. null,
  148. null,
  149. null,
  150. $current_session_id
  151. );
  152. } else {
  153. //add a new document
  154. $doc_id = add_document(
  155. $_course,
  156. $relativeUrlPath.'/'.$paintFileName,
  157. 'file',
  158. filesize($documentPath),
  159. $title
  160. );
  161. api_item_property_update(
  162. $_course,
  163. TOOL_DOCUMENT,
  164. $doc_id,
  165. 'DocumentAdded',
  166. $_user['user_id'],
  167. $groupInfo,
  168. null,
  169. null,
  170. null,
  171. $current_session_id
  172. );
  173. }
  174. }
  175. //delete temporal file
  176. $temp_file_2delete = $_SESSION['temp_realpath_image'];
  177. unlink($temp_file_2delete);
  178. //Clean sessions and return to Chamilo file list
  179. unset($_SESSION['paint_dir']);
  180. unset($_SESSION['paint_file']);
  181. unset($_SESSION['whereami']);
  182. unset($_SESSION['temp_realpath_image']);
  183. if (!isset($_SESSION['exit_pixlr'])) {
  184. $location = api_get_path(WEB_CODE_PATH).'document/document.php';
  185. echo '<script>window.parent.location.href="'.$location.'"</script>';
  186. api_not_allowed(true);
  187. } else {
  188. 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>';
  189. $location = api_get_path(WEB_CODE_PATH).'document/document.php?id='.Security::remove_XSS($_SESSION['exit_pixlr']);
  190. echo '<script>window.parent.location.href="'.$location.'"</script>';
  191. unset($_SESSION['exit_pixlr']);
  192. }