proxy.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. * transparent encryption
  24. */
  25. class OC_FileProxy_Encryption extends OC_FileProxy{
  26. private static $blackList=null; //mimetypes blacklisted from encryption
  27. private static $enableEncryption=null;
  28. /**
  29. * check if a file should be encrypted during write
  30. * @param string $path
  31. * @return bool
  32. */
  33. private static function shouldEncrypt($path){
  34. if(is_null(self::$enableEncryption)){
  35. self::$enableEncryption=(OCP\Config::getAppValue('files_encryption','enable_encryption','true')=='true');
  36. }
  37. if(!self::$enableEncryption){
  38. return false;
  39. }
  40. if(is_null(self::$blackList)){
  41. self::$blackList=explode(',',OCP\Config::getAppValue('files_encryption','type_blacklist','jpg,png,jpeg,avi,mpg,mpeg,mkv,mp3,oga,ogv,ogg'));
  42. }
  43. if(self::isEncrypted($path)){
  44. return true;
  45. }
  46. $extension=substr($path,strrpos($path,'.')+1);
  47. if(array_search($extension,self::$blackList)===false){
  48. return true;
  49. }
  50. }
  51. /**
  52. * check if a file is encrypted
  53. * @param string $path
  54. * @return bool
  55. */
  56. private static function isEncrypted($path){
  57. $metadata=OC_FileCache::getCached($path,'/');
  58. return isset($metadata['encrypted']) and (bool)$metadata['encrypted'];
  59. }
  60. public function preFile_put_contents($path,&$data){
  61. if(self::shouldEncrypt($path)){
  62. if (!is_resource($data)) {//stream put contents should have been converter to fopen
  63. $size=strlen($data);
  64. $data=OC_Crypt::blockEncrypt($data);
  65. OC_FileCache::put($path,array('encrypted'=>true,'size'=>$size),'/');
  66. }
  67. }
  68. }
  69. public function postFile_get_contents($path,$data){
  70. if(self::isEncrypted($path)){
  71. $cached=OC_FileCache::getCached($path,'/');
  72. $data=OC_Crypt::blockDecrypt($data,'',$cached['size']);
  73. }
  74. return $data;
  75. }
  76. public function postFopen($path,&$result){
  77. if(!$result){
  78. return $result;
  79. }
  80. $meta=stream_get_meta_data($result);
  81. if(self::isEncrypted($path)){
  82. fclose($result);
  83. $result=fopen('crypt://'.$path,$meta['mode']);
  84. }elseif(self::shouldEncrypt($path) and $meta['mode']!='r' and $meta['mode']!='rb'){
  85. if(OC_Filesystem::file_exists($path) and OC_Filesystem::filesize($path)>0){
  86. //first encrypt the target file so we don't end up with a half encrypted file
  87. OCP\Util::writeLog('files_encryption','Decrypting '.$path.' before writing',OCP\Util::DEBUG);
  88. $tmp=fopen('php://temp');
  89. OCP\Files::streamCopy($result,$tmp);
  90. fclose($result);
  91. OC_Filesystem::file_put_contents($path,$tmp);
  92. fclose($tmp);
  93. }
  94. $result=fopen('crypt://'.$path,$meta['mode']);
  95. }
  96. return $result;
  97. }
  98. public function postGetMimeType($path,$mime){
  99. if(self::isEncrypted($path)){
  100. $mime=OCP\Files::getMimeType('crypt://'.$path,'w');
  101. }
  102. return $mime;
  103. }
  104. public function postStat($path,$data){
  105. if(self::isEncrypted($path)){
  106. $cached=OC_FileCache::getCached($path,'/');
  107. $data['size']=$cached['size'];
  108. }
  109. return $data;
  110. }
  111. public function postFileSize($path,$size){
  112. if(self::isEncrypted($path)){
  113. $cached=OC_FileCache::getCached($path,'/');
  114. return $cached['size'];
  115. }else{
  116. return $size;
  117. }
  118. }
  119. }