savefile.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. /**
  3. * ownCloud - files_texteditor
  4. *
  5. * @author Tom Needham
  6. * @copyright 2011 Tom Needham contact@tomneedham.com
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  10. * License as published by the Free Software Foundation; either
  11. * version 3 of the License, or any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public
  19. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. // Init owncloud
  23. // Check if we are a user
  24. OCP\JSON::checkLoggedIn();
  25. // Get paramteres
  26. $filecontents = isset($_POST['filecontents']) ? $_POST['filecontents'] : false;
  27. $path = isset($_POST['path']) ? $_POST['path'] : '';
  28. $mtime = isset($_POST['mtime']) ? $_POST['mtime'] : '';
  29. if($path != '' && $mtime != '' && $filecontents)
  30. {
  31. // Get file mtime
  32. $filemtime = OC_Filesystem::filemtime($path);
  33. if($mtime != $filemtime)
  34. {
  35. // Then the file has changed since opening
  36. OCP\JSON::error();
  37. OCP\Util::writeLog('files_texteditor',"File: ".$path." modified since opening.",OCP\Util::ERROR);
  38. }
  39. else
  40. {
  41. // File same as when opened
  42. // Save file
  43. if(OC_Filesystem::is_writable($path))
  44. {
  45. OC_Filesystem::file_put_contents($path, $filecontents);
  46. // Clear statcache
  47. clearstatcache();
  48. // Get new mtime
  49. $newmtime = OC_Filesystem::filemtime($path);
  50. OCP\JSON::success(array('data' => array('mtime' => $newmtime)));
  51. }
  52. else
  53. {
  54. // Not writeable!
  55. OCP\JSON::error(array('data' => array( 'message' => 'Insufficient permissions')));
  56. OCP\Util::writeLog('files_texteditor',"User does not have permission to write to file: ".$path,OCP\Util::ERROR);
  57. }
  58. }
  59. } else if($path == ''){
  60. OCP\JSON::error(array('data' => array( 'message' => 'File path not supplied')));
  61. OCP\Util::writeLog('files_texteditor','No file path supplied', OCP\Util::ERROR);
  62. } else if($mtime == ''){
  63. OCP\JSON::error(array('data' => array( 'message' => 'File mtime not supplied')));
  64. OCP\Util::writeLog('files_texteditor','No file mtime supplied' ,OCP\Util::ERROR);
  65. } else if(!$filecontents){
  66. OCP\JSON::error(array('data' => array( 'message' => 'File contents not supplied')));
  67. OCP\Util::writeLog('files_texteditor','The file contents was not supplied',OCP\Util::ERROR);
  68. }