proxy.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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_Cached::get($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. $data=OC_Crypt::blockEncrypt($data);
  64. OC_FileCache::put($path,array('encrypted'=>true),'');
  65. }
  66. }
  67. }
  68. public function postFile_get_contents($path,$data){
  69. if(self::isEncrypted($path)){
  70. $data=OC_Crypt::blockDecrypt($data);
  71. }
  72. return $data;
  73. }
  74. public function postFopen($path,&$result){
  75. if(!$result){
  76. return $result;
  77. }
  78. $meta=stream_get_meta_data($result);
  79. if(self::isEncrypted($path)){
  80. fclose($result);
  81. $result=fopen('crypt://'.$path,$meta['mode']);
  82. }elseif(self::shouldEncrypt($path) and $meta['mode']!='r' and $meta['mode']!='rb'){
  83. if(OC_Filesystem::file_exists($path) and OC_Filesystem::filesize($path)>0){
  84. //first encrypt the target file so we don't end up with a half encrypted file
  85. OCP\Util::writeLog('files_encryption','Decrypting '.$path.' before writing',OCP\Util::DEBUG);
  86. $tmp=fopen('php://temp');
  87. OCP\Files::streamCopy($result,$tmp);
  88. fclose($result);
  89. OC_Filesystem::file_put_contents($path,$tmp);
  90. fclose($tmp);
  91. }
  92. $result=fopen('crypt://'.$path,$meta['mode']);
  93. }
  94. return $result;
  95. }
  96. public function postGetMimeType($path,$mime){
  97. if(self::isEncrypted($path)){
  98. $mime=OCP\Files::getMimeType('crypt://'.$path,'w');
  99. }
  100. return $mime;
  101. }
  102. }