FileAccessHelper.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Lukas Reschke <lukas@statuscode.ch>
  6. *
  7. * @license AGPL-3.0
  8. *
  9. * This code is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License, version 3,
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program 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 License, version 3,
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>
  20. *
  21. */
  22. namespace OC\IntegrityCheck\Helpers;
  23. /**
  24. * Class FileAccessHelper provides a helper around file_get_contents and
  25. * file_put_contents
  26. *
  27. * @package OC\IntegrityCheck\Helpers
  28. */
  29. class FileAccessHelper {
  30. /**
  31. * Wrapper around file_get_contents($filename, $data)
  32. *
  33. * @param string $filename
  34. * @return string|false
  35. */
  36. public function file_get_contents($filename) {
  37. return file_get_contents($filename);
  38. }
  39. /**
  40. * Wrapper around file_exists($filename)
  41. *
  42. * @param string $filename
  43. * @return bool
  44. */
  45. public function file_exists($filename) {
  46. return file_exists($filename);
  47. }
  48. /**
  49. * Wrapper around file_put_contents($filename, $data)
  50. *
  51. * @param string $filename
  52. * @param string $data
  53. * @return int
  54. * @throws \Exception
  55. */
  56. public function file_put_contents($filename, $data) {
  57. $bytesWritten = @file_put_contents($filename, $data);
  58. if ($bytesWritten === false || $bytesWritten !== strlen($data)){
  59. throw new \Exception('Failed to write into ' . $filename);
  60. }
  61. return $bytesWritten;
  62. }
  63. /**
  64. * @param string $path
  65. * @return bool
  66. */
  67. public function is_writable($path) {
  68. return is_writable($path);
  69. }
  70. /**
  71. * @param string $path
  72. * @throws \Exception
  73. */
  74. public function assertDirectoryExists($path) {
  75. if (!is_dir($path)) {
  76. throw new \Exception('Directory ' . $path . ' does not exist.');
  77. }
  78. }
  79. }