save_pixlr.php 6.3 KB

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