index.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. <?php
  2. /**
  3. * Any service can be built, but certain rules must be followed:
  4. * Read in POST params 'markdown' and 'filename'
  5. * Convert 'filename' to lowercase
  6. * Filter out relative paths (../) from 'filename'
  7. * Only allow .md extensions to be written
  8. * Any sub-folders must be created before attempting to write the file
  9. * On success, return JSON success message
  10. *
  11. **/
  12. $newMarkdown = $_POST['markdown'];
  13. $wikifilepath = $_POST['filename'];
  14. $filenameNoRelativePath = str_ireplace("../", "", strtolower($wikifilepath));
  15. $file = basename($filenameNoRelativePath);
  16. $fileBreakdown = explode(".", $file);
  17. $extension = $fileBreakdown[count($fileBreakdown) - 1];
  18. $path = dirname($filenameNoRelativePath);
  19. if($extension == "md") {
  20. $markdownPath = dirname(__FILE__). "/../markdown";
  21. if (!is_dir($markdownPath . $path)) {
  22. mkdir($markdownPath . $path, 0755, true);
  23. }
  24. $absoluteFile = $markdownPath . $filenameNoRelativePath;
  25. //TODO: make a history file
  26. //mkdir folder with the filename
  27. //copy current file contents into unique hash filename
  28. //add unique hash to the manifest file
  29. file_put_contents($absoluteFile, $newMarkdown);
  30. $response = array("status" => "success", "message" => "Saved " . $filenameNoRelativePath);
  31. } else {
  32. $response = array("status" => "error", "message" => "Invalid filename " . $filenameNoRelativePath);
  33. }
  34. header('Content-Type: application/json');
  35. echo json_encode($response);