fileproxy.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 operations like copy or move) and return a boolean
  29. * If a pre-proxy returnes false the file operation will be canceled
  30. * All filesystem operations have a pre-proxy
  31. *
  32. * A post-proxy recieves 2 arguments, the filepath and the result of the operation.
  33. * The return calue of the post-proxy will be used as the new result of the operation
  34. * The operations that have a post-proxy are
  35. * file_get_contents, is_file, is_dir, file_exists, stat, is_readable, is_writable, fileatime, filemtime, filectime, file_get_contents, getMimeType, hash, fopen, free_space and search
  36. */
  37. class OC_FileProxy{
  38. private static $proxies=array();
  39. public static $enabled=true;
  40. /**
  41. * check if this proxy implments a specific proxy operation
  42. * @param string #proxy name of the proxy operation
  43. * @return bool
  44. */
  45. public function provides($operation){
  46. return method_exists($this,$operation);
  47. }
  48. /**
  49. * fallback function when a proxy operation is not implement
  50. * @param string $function the name of the proxy operation
  51. * @param mixed
  52. *
  53. * this implements a dummy proxy for all operations
  54. */
  55. public function __call($function,$arguments){
  56. if(substr($function,0,3)=='pre'){
  57. return true;
  58. }else{
  59. return $arguments[1];
  60. }
  61. }
  62. /**
  63. * register a proxy to be used
  64. * @param OC_FileProxy $proxy
  65. */
  66. public static function register($proxy){
  67. self::$proxies[]=$proxy;
  68. }
  69. public static function getProxies($operation,$post){
  70. $operation=(($post)?'post':'pre').$operation;
  71. $proxies=array();
  72. foreach(self::$proxies as $proxy){
  73. if($proxy->provides($operation)){
  74. $proxies[]=$proxy;
  75. }
  76. }
  77. return $proxies;
  78. }
  79. public static function runPreProxies($operation,&$filepath,&$filepath2=null){
  80. if(!self::$enabled){
  81. return true;
  82. }
  83. $proxies=self::getProxies($operation,false);
  84. $operation='pre'.$operation;
  85. foreach($proxies as $proxy){
  86. if(!is_null($filepath2)){
  87. if($proxy->$operation($filepath,$filepath2)===false){
  88. return false;
  89. }
  90. }else{
  91. if($proxy->$operation($filepath)===false){
  92. return false;
  93. }
  94. }
  95. }
  96. return true;
  97. }
  98. public static function runPostProxies($operation,$path,$result){
  99. if(!self::$enabled){
  100. return $result;
  101. }
  102. $proxies=self::getProxies($operation,true);
  103. $operation='post'.$operation;
  104. foreach($proxies as $proxy){
  105. $result=$proxy->$operation($path,$result);
  106. }
  107. return $result;
  108. }
  109. }