savefile_config.php 5.7 KB

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