fileproxy.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. /**
  3. * ownCloud
  4. *
  5. * @author Robin Appelman
  6. * @copyright 2011 Robin Appelman icewind1991@gmail.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. /**
  23. * Class for manipulating filesystem requests
  24. *
  25. * Manipulation happens by using 2 kind of proxy operations, pre and post proxies
  26. * that manipulate the filesystem call and the result of the call respectively
  27. *
  28. * A pre-proxy recieves the filepath as arugments (or 2 filespaths in case of
  29. * operations like copy or move) and return a boolean
  30. * If a pre-proxy returns false the file operation will be canceled
  31. * All filesystem operations have a pre-proxy
  32. *
  33. * A post-proxy recieves 2 arguments, the filepath and the result of the operation.
  34. * The return value of the post-proxy will be used as the new result of the operation
  35. * The operations that have a post-proxy are:
  36. * file_get_contents, is_file, is_dir, file_exists, stat, is_readable,
  37. * is_writable, filemtime, filectime, file_get_contents,
  38. * getMimeType, hash, fopen, free_space and search
  39. */
  40. class OC_FileProxy{
  41. private static $proxies=array();
  42. public static $enabled=true;
  43. /**
  44. * fallback function when a proxy operation is not implemented
  45. * @param string $function the name of the proxy operation
  46. * @param mixed $arguments
  47. *
  48. * this implements a dummy proxy for all operations
  49. */
  50. public function __call($function, $arguments) {
  51. if(substr($function, 0, 3)=='pre') {
  52. return true;
  53. }else{
  54. return $arguments[1];
  55. }
  56. }
  57. /**
  58. * register a proxy to be used
  59. * @param OC_FileProxy $proxy
  60. */
  61. public static function register($proxy) {
  62. self::$proxies[]=$proxy;
  63. }
  64. /**
  65. * @param string $operation
  66. */
  67. public static function getProxies($operation = null) {
  68. if ($operation === null) {
  69. // return all
  70. return self::$proxies;
  71. }
  72. $proxies=array();
  73. foreach(self::$proxies as $proxy) {
  74. if(method_exists($proxy, $operation)) {
  75. $proxies[]=$proxy;
  76. }
  77. }
  78. return $proxies;
  79. }
  80. /**
  81. * @param string $operation
  82. * @param string|boolean $filepath
  83. */
  84. public static function runPreProxies($operation,&$filepath,&$filepath2=null) {
  85. if(!self::$enabled) {
  86. return true;
  87. }
  88. $operation='pre'.$operation;
  89. $proxies=self::getProxies($operation);
  90. foreach($proxies as $proxy) {
  91. if(!is_null($filepath2)) {
  92. if($proxy->$operation($filepath, $filepath2)===false) {
  93. return false;
  94. }
  95. }else{
  96. if($proxy->$operation($filepath)===false) {
  97. return false;
  98. }
  99. }
  100. }
  101. return true;
  102. }
  103. /**
  104. * @param string $operation
  105. * @param string|boolean $path
  106. *
  107. * @return string
  108. */
  109. public static function runPostProxies($operation, $path, $result) {
  110. if(!self::$enabled) {
  111. return $result;
  112. }
  113. $operation='post'.$operation;
  114. $proxies=self::getProxies($operation);
  115. foreach($proxies as $proxy) {
  116. $result=$proxy->$operation($path, $result);
  117. }
  118. return $result;
  119. }
  120. public static function clearProxies() {
  121. self::$proxies=array();
  122. }
  123. }