savefile_config.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. /*
  3. * filesave.php
  4. * To be used with ext-server_opensave.js for SVG-edit
  5. *
  6. * Licensed under the Apache License, Version 2
  7. *
  8. * Copyright(c) 2010 Alexis Deveria
  9. *
  10. * Integrate svg-edit with Chamilo
  11. * @author Juan Carlos Raña Trabado
  12. * @since 25/september/2010
  13. */
  14. require_once '../../../../../inc/global.inc.php';
  15. // Add security from Chamilo
  16. api_protect_course_script();
  17. api_block_anonymous_users();
  18. if (!isset($_POST['output_svg']) && !isset($_POST['output_png'])) {
  19. api_not_allowed();//from Chamilo
  20. die();
  21. }
  22. $file = '';
  23. $suffix = isset($_POST['output_svg']) ? 'svg' : 'png';
  24. if (isset($_POST['filename']) && strlen($_POST['filename']) > 0) {
  25. $file = $_POST['filename'];
  26. } else {
  27. $file = 'image';
  28. }
  29. if ($suffix == 'svg') {
  30. $mime = 'image/svg+xml';
  31. $contents = rawurldecode($_POST['output_svg']);
  32. } else {
  33. $mime = 'image/png';
  34. $contents = $_POST['output_png'];
  35. $pos = (strpos($contents, 'base64,') + 7);
  36. $contents = base64_decode(substr($contents, $pos));
  37. }
  38. //get SVG-Edit values
  39. $filename = $file;//from svg-edit
  40. $extension = $suffix;// from svg-edit
  41. $content = $contents;//from svg-edit
  42. $title = Database::escape_string(str_replace('_', ' ', $filename));
  43. //get Chamilo variables
  44. if (!isset($_SESSION['draw_dir']) && !isset($_SESSION['whereami'])) {
  45. api_not_allowed();//from Chamilo
  46. die();
  47. }
  48. $current_session_id = api_get_session_id();
  49. $groupId = api_get_group_id();
  50. $groupInfo = GroupManager::get_group_properties($groupId);
  51. $relativeUrlPath = $_SESSION['draw_dir'];
  52. $currentTool = $_SESSION['whereami'];
  53. $dirBaseDocuments = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document';
  54. $saveDir=$dirBaseDocuments.$_SESSION['draw_dir'];
  55. // a bit title security
  56. $filename = addslashes(trim($filename));
  57. $filename = Security::remove_XSS($filename);
  58. $filename = api_replace_dangerous_char($filename);
  59. $filename = disable_dangerous_file($filename);
  60. // a bit extension
  61. if ($suffix != 'svg' && $suffix != 'png') {
  62. die();
  63. }
  64. //a bit mime security
  65. //comment because finfo seems stopping the save process files in some php vers.
  66. /*
  67. if (phpversion() >= '5.3' && extension_loaded('fileinfo')) {
  68. $finfo = new finfo(FILEINFO_MIME);
  69. $current_mime=$finfo->buffer($contents);
  70. finfo_close($finfo);
  71. $mime_png='image/png';//svg-edit return image/png; charset=binary
  72. $mime_svg='image/svg+xml';
  73. $mime_xml='application/xml';//hack for svg-edit because original code return application/xml; charset=us-ascii. See
  74. if(strpos($current_mime, $mime_png)===false && $extension=='png') {
  75. die();//File extension does not match its content
  76. } elseif(strpos($current_mime, $mime_svg)===false && strpos($current_mime, $mime_xml)===false && $extension=='svg') {
  77. die();//File extension does not match its content
  78. }
  79. }
  80. */
  81. //checks if the file exists, then rename the new
  82. if (file_exists($saveDir.'/'.$filename.'.'.$extension) && $currentTool=='document/createdraw') {
  83. $message = get_lang('FileExistsChangeToSave');
  84. $params = array(
  85. 'message' => $message,
  86. 'url' => ''
  87. );
  88. echo json_encode($params);
  89. exit;
  90. } else {
  91. $drawFileName = $filename.'.'.$extension;
  92. $title = $title.'.'.$extension;
  93. }
  94. $documentPath = $saveDir.'/'.$drawFileName;
  95. //add new document to disk
  96. file_put_contents($documentPath, $contents);
  97. if ($currentTool=='document/createdraw') {
  98. //add document to database
  99. $doc_id = add_document(
  100. $_course,
  101. $relativeUrlPath.'/'.$drawFileName,
  102. 'file',
  103. filesize($documentPath),
  104. $title
  105. );
  106. api_item_property_update(
  107. $_course,
  108. TOOL_DOCUMENT,
  109. $doc_id,
  110. 'DocumentAdded',
  111. $_user['user_id'],
  112. $groupInfo,
  113. null,
  114. null,
  115. null,
  116. $current_session_id
  117. );
  118. } elseif ($currentTool == 'document/editdraw') {
  119. //check path
  120. if (!isset($_SESSION['draw_file'])) {
  121. api_not_allowed();//from Chamilo
  122. die();
  123. }
  124. if ($_SESSION['draw_file'] == $drawFileName) {
  125. $document_id = DocumentManager::get_document_id(
  126. $_course,
  127. $relativeUrlPath.'/'.$drawFileName
  128. );
  129. update_existing_document(
  130. $_course,
  131. $document_id,
  132. filesize($documentPath),
  133. null
  134. );
  135. api_item_property_update(
  136. $_course,
  137. TOOL_DOCUMENT,
  138. $document_id,
  139. 'DocumentUpdated',
  140. $_user['user_id'],
  141. $groupInfo,
  142. null,
  143. null,
  144. null,
  145. $current_session_id
  146. );
  147. } else {
  148. //add a new document
  149. $doc_id = add_document(
  150. $_course,
  151. $relativeUrlPath.'/'.$drawFileName,
  152. 'file',
  153. filesize($documentPath),
  154. $title
  155. );
  156. api_item_property_update(
  157. $_course,
  158. TOOL_DOCUMENT,
  159. $doc_id,
  160. 'DocumentAdded',
  161. $_user['user_id'],
  162. $groupInfo,
  163. null,
  164. null,
  165. null,
  166. $current_session_id
  167. );
  168. }
  169. }
  170. //clean sessions and add messages and return to current document list
  171. unset($_SESSION['draw_dir']);
  172. unset($_SESSION['draw_file']);
  173. unset($_SESSION['whereami']);
  174. if ($suffix != 'png') {
  175. if ($relativeUrlPath == '') {
  176. $relativeUrlPath = '/';
  177. };
  178. $url = api_get_path(WEB_CODE_PATH).'document/document.php?'.api_get_cidreq().'&curdirpath='.urlencode($relativeUrlPath);
  179. $message = get_lang('FileSavedAs').': '.$title;
  180. } else {
  181. $url = '';
  182. $message = get_lang('FileExportAs').': '.$title;
  183. }
  184. $params = array(
  185. 'message' => $message,
  186. 'url' => $url
  187. );
  188. echo json_encode($params);
  189. exit;