fileproxy.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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, fileatime, 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
  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. public static function getProxies($operation) {
  65. $proxies=array();
  66. foreach(self::$proxies as $proxy) {
  67. if(method_exists($proxy,$operation)) {
  68. $proxies[]=$proxy;
  69. }
  70. }
  71. return $proxies;
  72. }
  73. public static function runPreProxies($operation,&$filepath,&$filepath2=null) {
  74. if(!self::$enabled) {
  75. return true;
  76. }
  77. $operation='pre'.$operation;
  78. $proxies=self::getProxies($operation);
  79. foreach($proxies as $proxy) {
  80. if(!is_null($filepath2)) {
  81. if($proxy->$operation($filepath,$filepath2)===false) {
  82. return false;
  83. }
  84. }else{
  85. if($proxy->$operation($filepath)===false) {
  86. return false;
  87. }
  88. }
  89. }
  90. return true;
  91. }
  92. public static function runPostProxies($operation,$path,$result) {
  93. if(!self::$enabled) {
  94. return $result;
  95. }
  96. $operation='post'.$operation;
  97. $proxies=self::getProxies($operation);
  98. foreach($proxies as $proxy) {
  99. $result=$proxy->$operation($path,$result);
  100. }
  101. return $result;
  102. }
  103. public static function clearProxies() {
  104. self::$proxies=array();
  105. }
  106. }