savefile_config.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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('This file name already exists, choose another to save your image.');
  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 = DocumentManager::addDocument(
  101. $_course,
  102. $relativeUrlPath.'/'.$drawFileName,
  103. 'file',
  104. filesize($documentPath),
  105. $title
  106. );
  107. } elseif ($currentTool == 'document/editdraw') {
  108. //check path
  109. if (!isset($_SESSION['draw_file'])) {
  110. api_not_allowed();//from Chamilo
  111. die();
  112. }
  113. if ($_SESSION['draw_file'] == $drawFileName) {
  114. $document_id = DocumentManager::get_document_id(
  115. $_course,
  116. $relativeUrlPath.'/'.$drawFileName
  117. );
  118. update_existing_document(
  119. $_course,
  120. $document_id,
  121. filesize($documentPath),
  122. null
  123. );
  124. api_item_property_update(
  125. $_course,
  126. TOOL_DOCUMENT,
  127. $document_id,
  128. 'DocumentUpdated',
  129. $_user['user_id'],
  130. $groupInfo,
  131. null,
  132. null,
  133. null,
  134. $current_session_id
  135. );
  136. } else {
  137. //add a new document
  138. $doc_id = DocumentManager::addDocument(
  139. $_course,
  140. $relativeUrlPath.'/'.$drawFileName,
  141. 'file',
  142. filesize($documentPath),
  143. $title
  144. );
  145. }
  146. }
  147. //clean sessions and add messages and return to current document list
  148. Session::erase('draw_dir');
  149. Session::erase('draw_file');
  150. if ($suffix != 'png') {
  151. if ($relativeUrlPath == '') {
  152. $relativeUrlPath = '/';
  153. };
  154. $url = api_get_path(WEB_CODE_PATH).'document/document.php?'.api_get_cidreq().'&curdirpath='.urlencode($relativeUrlPath);
  155. $message = get_lang('File saved as').': '.$title;
  156. //echo 'alert("'.get_lang('File saved as').': '.$title.'");';
  157. //echo 'window.top.location.href="'.$interbreadcrumb.'";';//return to current document list
  158. } else {
  159. $url = '';
  160. $message = get_lang('File export as').': '.$title;
  161. }
  162. $params = array(
  163. 'message' => $message,
  164. 'url' => $url
  165. );
  166. echo json_encode($params);
  167. exit;