savefile_config.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. //Chamilo load libraries
  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. if(isset($_POST['filename']) && strlen($_POST['filename']) > 0) {
  26. $file = $_POST['filename'];
  27. } else {
  28. $file = 'image';
  29. }
  30. if($suffix == 'svg') {
  31. $mime = 'image/svg+xml';
  32. $contents = rawurldecode($_POST['output_svg']);
  33. } else {
  34. $mime = 'image/png';
  35. $contents = $_POST['output_png'];
  36. $pos = (strpos($contents, 'base64,') + 7);
  37. $contents = base64_decode(substr($contents, $pos));
  38. }
  39. //get SVG-Edit values
  40. $filename=$file;//from svg-edit
  41. $extension=$suffix;// from svg-edit
  42. $content=$contents;//from svg-edit
  43. $title = Database::escape_string(str_replace('_',' ',$filename));
  44. //get Chamilo variables
  45. if(!isset($_SESSION['draw_dir']) && !isset($_SESSION['whereami'])) {
  46. api_not_allowed();//from Chamilo
  47. die();
  48. }
  49. $current_session_id = api_get_session_id();
  50. $groupId = api_get_group_id();
  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.$i.'.'.$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($_course, $relativeUrlPath.'/'.$drawFileName, 'file', filesize($documentPath), $title);
  100. api_item_property_update($_course, TOOL_DOCUMENT, $doc_id, 'DocumentAdded', $_user['user_id'], $groupId, null, null, null, $current_session_id);
  101. } elseif($currentTool=='document/editdraw') {
  102. //check path
  103. if (!isset($_SESSION['draw_file'])){
  104. api_not_allowed();//from Chamilo
  105. die();
  106. }
  107. if ($_SESSION['draw_file'] == $drawFileName ){
  108. $document_id = DocumentManager::get_document_id($_course, $relativeUrlPath.'/'.$drawFileName);
  109. update_existing_document($_course, $document_id, filesize($documentPath), null);
  110. api_item_property_update($_course, TOOL_DOCUMENT, $document_id, 'DocumentUpdated', $_user['user_id'], $groupId, null, null, null, $current_session_id);
  111. } else {
  112. //add a new document
  113. $doc_id = add_document($_course, $relativeUrlPath.'/'.$drawFileName, 'file', filesize($documentPath), $title);
  114. api_item_property_update($_course, TOOL_DOCUMENT, $doc_id, 'DocumentAdded', $_user['user_id'], $groupId, null, null, null, $current_session_id);
  115. }
  116. }
  117. //clean sessions and add messages and return to current document list
  118. unset($_SESSION['draw_dir']);
  119. unset($_SESSION['draw_file']);
  120. unset($_SESSION['whereami']);
  121. if ($suffix != 'png') {
  122. if ($relativeUrlPath == '') {
  123. $relativeUrlPath = '/';
  124. };
  125. $url = api_get_path(WEB_CODE_PATH).'document/document.php?'.api_get_cidreq().'&curdirpath='.urlencode($relativeUrlPath);
  126. $message = get_lang('FileSavedAs').': '.$title;
  127. //echo 'alert("'.get_lang('FileSavedAs').': '.$title.'");';
  128. //echo 'window.top.location.href="'.$interbreadcrumb.'";';//return to current document list
  129. } else {
  130. $url = '';
  131. $message = get_lang('FileExportAs').': '.$title;
  132. }
  133. $params = array(
  134. 'message' => $message,
  135. 'url' => $url
  136. );
  137. echo json_encode($params);
  138. exit;