savefile_config.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. $language_file = array('document');//Chamilo load lang var
  15. //Chamilo load libraries
  16. require_once '../../../../inc/global.inc.php';
  17. require_once api_get_path(LIBRARY_PATH).'fileUpload.lib.php';
  18. require_once api_get_path(LIBRARY_PATH).'document.lib.php';
  19. //Add security from Chamilo
  20. api_protect_course_script();
  21. api_block_anonymous_users();
  22. if(!isset($_POST['output_svg']) && !isset($_POST['output_png'])) {
  23. api_not_allowed();//from Chamilo
  24. die();
  25. }
  26. $file = '';
  27. $suffix = isset($_POST['output_svg'])?'svg':'png';
  28. if(isset($_POST['filename']) && strlen($_POST['filename']) > 0) {
  29. $file = $_POST['filename'];
  30. } else {
  31. $file = 'image';
  32. }
  33. if($suffix == 'svg') {
  34. $mime = 'image/svg+xml';
  35. $contents = rawurldecode($_POST['output_svg']);
  36. } else {
  37. $mime = 'image/png';
  38. $contents = $_POST['output_png'];
  39. $pos = (strpos($contents, 'base64,') + 7);
  40. $contents = base64_decode(substr($contents, $pos));
  41. }
  42. //get SVG-Edit values
  43. $filename=$file;//from svg-edit
  44. $extension=$suffix;// from svg-edit
  45. $content=$contents;//from svg-edit
  46. $title = Database::escape_string(str_replace('_',' ',$filename));
  47. //get Chamilo variables
  48. if(!isset($_SESSION['draw_dir']) && !isset($_SESSION['whereami'])) {
  49. api_not_allowed();//from Chamilo
  50. die();
  51. }
  52. $current_session_id = api_get_session_id();
  53. $groupId = api_get_group_id();
  54. $relativeUrlPath=$_SESSION['draw_dir'];
  55. $currentTool=$_SESSION['whereami'];
  56. $dirBaseDocuments = api_get_path(SYS_COURSE_PATH).$_course['path'].'/document';
  57. $saveDir=$dirBaseDocuments.$_SESSION['draw_dir'];
  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 = disable_dangerous_file($filename);
  63. // a bit extension
  64. if ($suffix != 'svg' && $suffix != 'png') {
  65. die();
  66. }
  67. //a bit mime security
  68. //comment because finfo seems stopping the save process files in some php vers.
  69. /*
  70. if (phpversion() >= '5.3' && extension_loaded('fileinfo')) {
  71. $finfo = new finfo(FILEINFO_MIME);
  72. $current_mime=$finfo->buffer($contents);
  73. finfo_close($finfo);
  74. $mime_png='image/png';//svg-edit return image/png; charset=binary
  75. $mime_svg='image/svg+xml';
  76. $mime_xml='application/xml';//hack for svg-edit because original code return application/xml; charset=us-ascii. See
  77. if(strpos($current_mime, $mime_png)===false && $extension=='png') {
  78. die();//File extension does not match its content
  79. } elseif(strpos($current_mime, $mime_svg)===false && strpos($current_mime, $mime_xml)===false && $extension=='svg') {
  80. die();//File extension does not match its content
  81. }
  82. }
  83. */
  84. //checks if the file exists, then rename the new
  85. if (file_exists($saveDir.'/'.$filename.$i.'.'.$extension) && $currentTool=='document/createdraw') {
  86. $message = get_lang('FileExistsChangeToSave');
  87. $params = array(
  88. 'message' => $message,
  89. 'url' => ''
  90. );
  91. echo json_encode($params);
  92. exit;
  93. } else {
  94. $drawFileName = $filename.'.'.$extension;
  95. $title = $title.'.'.$extension;
  96. }
  97. $documentPath = $saveDir.'/'.$drawFileName;
  98. //add new document to disk
  99. file_put_contents( $documentPath, $contents );
  100. if ($currentTool=='document/createdraw') {
  101. //add document to database
  102. $doc_id = add_document($_course, $relativeUrlPath.'/'.$drawFileName, 'file', filesize($documentPath), $title);
  103. api_item_property_update($_course, TOOL_DOCUMENT, $doc_id, 'DocumentAdded', $_user['user_id'], $groupId, null, null, null, $current_session_id);
  104. } elseif($currentTool=='document/editdraw') {
  105. //check path
  106. if (!isset($_SESSION['draw_file'])){
  107. api_not_allowed();//from Chamilo
  108. die();
  109. }
  110. if ($_SESSION['draw_file'] == $drawFileName ){
  111. $document_id = DocumentManager::get_document_id($_course, $relativeUrlPath.'/'.$drawFileName);
  112. update_existing_document($_course, $document_id, filesize($documentPath), null);
  113. api_item_property_update($_course, TOOL_DOCUMENT, $document_id, 'DocumentUpdated', $_user['user_id'], $groupId, null, null, null, $current_session_id);
  114. } else {
  115. //add a new document
  116. $doc_id = add_document($_course, $relativeUrlPath.'/'.$drawFileName, 'file', filesize($documentPath), $title);
  117. api_item_property_update($_course, TOOL_DOCUMENT, $doc_id, 'DocumentAdded', $_user['user_id'], $groupId, null, null, null, $current_session_id);
  118. }
  119. }
  120. //clean sessions and add messages and return to current document list
  121. unset($_SESSION['draw_dir']);
  122. unset($_SESSION['draw_file']);
  123. unset($_SESSION['whereami']);
  124. if ($suffix != 'png') {
  125. if ($relativeUrlPath == '') {
  126. $relativeUrlPath = '/';
  127. };
  128. $url = api_get_path(WEB_CODE_PATH).'document/document.php?'.api_get_cidreq().'&curdirpath='.urlencode($relativeUrlPath);
  129. $message = get_lang('FileSavedAs').': '.$title;
  130. //echo 'alert("'.get_lang('FileSavedAs').': '.$title.'");';
  131. //echo 'window.top.location.href="'.$interbreadcrumb.'";';//return to current document list
  132. } else {
  133. $url = '';
  134. $message = get_lang('FileExportAs').': '.$title;
  135. }
  136. $params = array(
  137. 'message' => $message,
  138. 'url' => $url
  139. );
  140. echo json_encode($params);
  141. exit;