PageRenderTime 49ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/main/document/save_pixlr.php

https://bitbucket.org/hanutimes/hanutimes
PHP | 249 lines | 170 code | 33 blank | 46 comment | 40 complexity | 4a7057efd7c15b76617495bf5aa363b7 MD5 | raw file
  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 '../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(
  33. $_GET['image']
  34. ); //A URL to the image on Pixlr.com server or the raw file post of the saved image.
  35. //make variables
  36. $title = Database::escape_string(str_replace('_', ' ', $filename));
  37. $current_session_id = api_get_session_id();
  38. $groupId = api_get_group_id();
  39. $relativeUrlPath = $_SESSION['paint_dir'];
  40. $currentTool = $_SESSION['whereami'];
  41. $dirBaseDocuments = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document';
  42. $saveDir = $dirBaseDocuments.$_SESSION['paint_dir'];
  43. $contents = file_get_contents($urlcontents);
  44. //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
  45. /*
  46. if (strpos($urlcontents, "pixlr.com") === 0){
  47. echo "Invalid referrer";
  48. exit;
  49. }
  50. */
  51. //Security. Allway get from pixlr.com. Comment because for now this does not run
  52. /*
  53. $urlcontents1='http://pixlr.com/';
  54. $urlcontents2 = strstr($urlcontents, '_temp');
  55. $urlcontents_to_save=$urlcontents1.$urlcontents2;
  56. $contents = file_get_contents($urlcontents_to_save);//replace line 45.
  57. */
  58. //a bit title security
  59. $filename = addslashes(trim($filename));
  60. $filename = Security::remove_XSS($filename);
  61. $filename = replace_dangerous_char($filename, 'strict');
  62. $filename = FileManager::disable_dangerous_file($filename);
  63. if (strlen(trim($filename)) == 0) {
  64. echo "The title is empty"; //if title is empty, headers Content-Type = application/octet-stream, then not create a new title here please
  65. exit;
  66. }
  67. //check file_get_contents
  68. if ($contents === false) {
  69. echo "I cannot read: ".$urlcontents;
  70. exit;
  71. }
  72. // Extension security
  73. if ($extension != 'jpg' && $extension != 'png' && $extension != 'pxd') {
  74. die();
  75. }
  76. if ($extension == 'pxd') {
  77. echo "pxd file type does not supported"; // not secure because check security headers and finfo() return Content-Type = application/octet-stream
  78. exit;
  79. }
  80. //Verify that the file is an image. Headers method
  81. $headers = get_headers($urlcontents, 1);
  82. $content_type = explode("/", $headers['Content-Type']);
  83. if ($content_type[0] != "image") {
  84. echo "Invalid file type";
  85. exit;
  86. }
  87. //Verify that the file is an image. Fileinfo method
  88. $finfo = new finfo(FILEINFO_MIME);
  89. $current_mime = $finfo->buffer($contents);
  90. finfo_close($finfo);
  91. if (strpos($current_mime, 'image') === false) {
  92. echo "Invalid mime type file";
  93. exit;
  94. }
  95. //make a temporal file for get the file size
  96. $tmpfname = tempnam("/tmp", "CTF");
  97. $handle = fopen($tmpfname, "w");
  98. fwrite($handle, $contents);
  99. fclose($handle);
  100. // Check if there is enough space in the course to save the file
  101. if (!DocumentManager::enough_space(filesize($tmpfname), DocumentManager::get_course_quota())) {
  102. unlink($tmpfname);
  103. die(get_lang('UplNotEnoughSpace'));
  104. }
  105. //erase temporal file
  106. unlink($tmpfname);
  107. //path, file and title
  108. $paintFileName = $filename.'.'.$extension;
  109. $title = $title.'.'.$extension;
  110. if ($currentTool == 'document/createpaint') {
  111. //check save as and prevent rewrite an older file with same name
  112. if (0 != $groupId) {
  113. $group_properties = GroupManager :: get_group_properties($groupId);
  114. $groupPath = $group_properties['directory'];
  115. } else {
  116. $groupPath = '';
  117. }
  118. if (file_exists($saveDir.'/'.$filename.'.'.$extension)) {
  119. $i = 1;
  120. while (file_exists($saveDir.'/'.$filename.'_'.$i.'.'.$extension)) {
  121. $i++;
  122. }
  123. $paintFileName = $filename.'_'.$i.'.'.$extension;
  124. $title = $filename.'_'.$i.'.'.$extension;
  125. }
  126. //
  127. $documentPath = $saveDir.'/'.$paintFileName;
  128. //add new document to disk
  129. file_put_contents($documentPath, $contents);
  130. //add document to database
  131. $doc_id = FileManager::add_document(
  132. $_course,
  133. $relativeUrlPath.'/'.$paintFileName,
  134. 'file',
  135. filesize($documentPath),
  136. $title
  137. );
  138. api_item_property_update(
  139. $_course,
  140. TOOL_DOCUMENT,
  141. $doc_id,
  142. 'DocumentAdded',
  143. $_user['user_id'],
  144. $groupId,
  145. null,
  146. null,
  147. null,
  148. $current_session_id
  149. );
  150. } elseif ($currentTool == 'document/editpaint') {
  151. $documentPath = $saveDir.'/'.$paintFileName;
  152. //add new document to disk
  153. file_put_contents($documentPath, $contents);
  154. //check path
  155. if (!isset($_SESSION['paint_file'])) {
  156. api_not_allowed();
  157. die();
  158. }
  159. if ($_SESSION['paint_file'] == $paintFileName) {
  160. $document_id = DocumentManager::get_document_id($_course, $relativeUrlPath.'/'.$paintFileName);
  161. FileManager::update_existing_document($_course, $document_id, filesize($documentPath), null);
  162. api_item_property_update(
  163. $_course,
  164. TOOL_DOCUMENT,
  165. $document_id,
  166. 'DocumentUpdated',
  167. $_user['user_id'],
  168. $groupId,
  169. null,
  170. null,
  171. null,
  172. $current_session_id
  173. );
  174. } else {
  175. //add a new document
  176. $doc_id = FileManager::add_document(
  177. $_course,
  178. $relativeUrlPath.'/'.$paintFileName,
  179. 'file',
  180. filesize($documentPath),
  181. $title
  182. );
  183. api_item_property_update(
  184. $_course,
  185. TOOL_DOCUMENT,
  186. $doc_id,
  187. 'DocumentAdded',
  188. $_user['user_id'],
  189. $groupId,
  190. null,
  191. null,
  192. null,
  193. $current_session_id
  194. );
  195. }
  196. }
  197. //delete temporal file
  198. $temp_file_2delete = $_SESSION['temp_realpath_image'];
  199. unlink($temp_file_2delete);
  200. //Clean sessions and return to Chamilo file list
  201. unset($_SESSION['paint_dir']);
  202. unset($_SESSION['paint_file']);
  203. unset($_SESSION['whereami']);
  204. unset($_SESSION['temp_realpath_image']);
  205. if (!isset($_SESSION['exit_pixlr'])) {
  206. $location = api_get_path(WEB_CODE_PATH).'document/document.php';
  207. echo '<script>window.parent.location.href="'.$location.'"</script>';
  208. api_not_allowed(true);
  209. } else {
  210. echo '<div align="center" style="padding-top:150; font-family:Arial, Helvetica, Sans-serif;font-size:25px;color:#aaa;font-weight:bold;">'.get_lang(
  211. 'PleaseStandBy'
  212. ).'</div>';
  213. $location = api_get_path(WEB_CODE_PATH).'document/document.php?id='.Security::remove_XSS($_SESSION['exit_pixlr']);
  214. echo '<script>window.parent.location.href="'.$location.'"</script>';
  215. unset($_SESSION['exit_pixlr']);
  216. }