save_pixlr.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. $documentId = add_document($courseInfo, $paintDir.$paintFileName, 'file', filesize($documentPath), $title);
  123. if ($documentId) {
  124. api_item_property_update(
  125. $courseInfo,
  126. TOOL_DOCUMENT,
  127. $documentId,
  128. 'DocumentAdded',
  129. api_get_user_id(),
  130. $groupInfo,
  131. null,
  132. null,
  133. null,
  134. $sessionId
  135. );
  136. Display::addFlash(Display::return_message(get_lang('Saved')));
  137. }
  138. } else {
  139. // Update
  140. $documentPath = $saveDir.'/'.$paintFileName;
  141. file_put_contents($documentPath, $contents);
  142. $paintFile = Session::read('paint_file');
  143. //check path
  144. if (empty($paintFile)) {
  145. echo 'No attribute paint_file';
  146. exit;
  147. }
  148. if ($paintFile == $paintFileName) {
  149. $documentId = DocumentManager::get_document_id($courseInfo, $paintDir.$paintFileName);
  150. update_existing_document($courseInfo, $documentId, filesize($documentPath), null);
  151. api_item_property_update(
  152. $courseInfo,
  153. TOOL_DOCUMENT,
  154. $documentId,
  155. 'DocumentUpdated',
  156. $_user['user_id'],
  157. $groupInfo,
  158. null,
  159. null,
  160. null,
  161. $sessionId
  162. );
  163. } else {
  164. //add a new document
  165. $documentId = add_document(
  166. $courseInfo,
  167. $paintDir.$paintFileName,
  168. 'file',
  169. filesize($documentPath),
  170. $title
  171. );
  172. if ($documentId) {
  173. api_item_property_update(
  174. $courseInfo,
  175. TOOL_DOCUMENT,
  176. $documentId,
  177. 'DocumentAdded',
  178. api_get_user_id(),
  179. $groupInfo,
  180. null,
  181. null,
  182. null,
  183. $sessionId
  184. );
  185. Display::addFlash(Display::return_message(get_lang('Updated')));
  186. }
  187. }
  188. }
  189. if (!empty($temp_file_2delete)) {
  190. // Delete temporal file
  191. unlink($temp_file_2delete);
  192. }
  193. //Clean sessions and return to Chamilo file list
  194. Session::erase('paint_dir');
  195. Session::erase('paint_file');
  196. Session::erase('temp_realpath_image');
  197. $exit = Session::read('exit_pixlr');
  198. if (empty($exit)) {
  199. $location = api_get_path(WEB_CODE_PATH).'document/document.php?'.api_get_cidreq();
  200. echo '<script>window.parent.location.href="'.$location.'"</script>';
  201. exit;
  202. } else {
  203. 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>';
  204. $location = api_get_path(WEB_CODE_PATH).'document/document.php?id='.Security::remove_XSS($exit).'&'.api_get_cidreq();
  205. echo '<script>window.parent.location.href="'.$location.'"</script>';
  206. Session::erase('exit_pixlr');
  207. }