PageRenderTime 25ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/backend/api/vfs/File.php

https://github.com/mhayden/lucid
PHP | 196 lines | 186 code | 3 blank | 7 comment | 41 complexity | d673e07bd7e137647db5522513b3cb4c MD5 | raw file
Possible License(s): AGPL-3.0, BSD-3-Clause, LGPL-2.0, MIT
  1. <?php
  2. /*
  3. Copyright (c) 2004-2008, The Dojo Foundation & Lucid Contributors
  4. All Rights Reserved.
  5. Licensed under the Academic Free License version 2.1 or above.
  6. */
  7. import("models.user");
  8. class FileFs extends BaseFs {
  9. var $_username;
  10. function _startup() {
  11. global $User;
  12. $cur = $User->get_current();
  13. $this->_username = $cur->username;
  14. }
  15. function _basePath($path=false) {
  16. return $GLOBALS['path'] . "/../files/".$this->_username . "/" . ($path ? $path : "");
  17. }
  18. function _getRealPath($path) {
  19. return $this->_basePath($path);
  20. }
  21. function _getRemainingQuota() {
  22. global $User;
  23. $cur = $User->get_current();
  24. $quota = $cur->get_quota();
  25. if($quota == 0) { return 0; } //no quota
  26. $current = $this->_getSize($this->_basePath("/"));
  27. $total = $quota - $current;
  28. return intval($total);
  29. }
  30. function _checkUserQuota1() {
  31. global $User;
  32. $cur = $User->get_current();
  33. $quota = $cur->get_quota();
  34. return intval($quota);
  35. }
  36. function _checkUserQuota() {
  37. $quota = $this->_checkUserQuota1();
  38. $current = $this->_getSize($this->_basePath("/"));
  39. if($current >= $quota) {
  40. if($quota == 0) { return 0; } //no quota
  41. $blah = new intOutput();
  42. $blah->set("quota_exceeded");
  43. die();
  44. }
  45. return true;
  46. }
  47. function _getSize($directory, $format=false) //works for files and folders
  48. {
  49. $size = 0;
  50. if(substr($directory,-1) == '/')
  51. {
  52. $directory = substr($directory,0,-1);
  53. }
  54. if(!file_exists($directory) || !is_dir($directory) || !is_readable($directory))
  55. {
  56. return filesize($directory);
  57. }
  58. if($handle = opendir($directory))
  59. {
  60. while(($file = readdir($handle)) !== false)
  61. {
  62. $path = $directory.'/'.$file;
  63. if($file != '.' && $file != '..')
  64. {
  65. if(is_file($path))
  66. {
  67. $size += filesize($path);
  68. }elseif(is_dir($path))
  69. {
  70. $handlesize = $this->_getSize($path);
  71. if($handlesize >= 0)
  72. {
  73. $size += $handlesize;
  74. }else{
  75. return -1;
  76. }
  77. }
  78. }
  79. }
  80. closedir($handle);
  81. }
  82. if($format == TRUE)
  83. {
  84. if($size / 1048576 > 1)
  85. {
  86. return round($size / 1048576, 1).' MB';
  87. }elseif($size / 1024 > 1)
  88. {
  89. return round($size / 1024, 1).' KB';
  90. }else{
  91. return round($size, 1).' bytes';
  92. }
  93. }else{
  94. return $size;
  95. }
  96. }
  97. function _getFileInfo($file, $realPath=false) {
  98. $r = array();
  99. $r['path'] = $file; //TODO: this is it's real path, get it's vfs path?
  100. $f = ($realPath ? "" : $this->_basePath()) . $file;
  101. $r['name'] = basename($f);
  102. $r["size"] = $this->_getSize($f);
  103. $r["baseQuota"] = $this->_checkUserQuota1();
  104. $r["remainingQuota"] = $this->_getRemainingQuota();
  105. if(is_dir($f)) {
  106. $r["type"] = "text/directory";
  107. }
  108. else if(is_file($f)) {
  109. $r["modified"] = date ("F d Y H:i:s.", filemtime($f));
  110. $r["type"] = mime_content_type($f);
  111. if($r["type"] == false) { $r["type"] = mime_content_type_alt($f); }
  112. }
  113. //get ID3 info if available
  114. if(function_exists("id3_get_tag")) {
  115. $id3 = id3_get_tag($f);
  116. foreach($id3 as $key=>$value) {
  117. $r["id3".str_replace(" ", "", ucwords($key))] = $value;
  118. }
  119. }
  120. return $r;
  121. }
  122. function _listPath($path) {
  123. $dir = opendir($this->_basePath($path));
  124. if(!$dir){
  125. return false;
  126. } else {
  127. $arr = array();
  128. while(($file = readdir($dir)) !== false){
  129. if($file == '..' || $file == '.'){
  130. continue;
  131. } else {
  132. array_push($arr, $this->_getFileInfo($this->_basePath($path . "/" . $file), true));
  133. }
  134. }
  135. return $arr;
  136. }
  137. }
  138. function _remove($path) {
  139. $path = $this->_basePath($path);
  140. return $this->_deltree($path);
  141. }
  142. function _deltree( $f ){
  143. if( is_dir( $f ) ){
  144. foreach( scandir( $f ) as $item ){
  145. if( !strcmp( $item, '.' ) || !strcmp( $item, '..' ) )
  146. continue;
  147. $this->_deltree( $f . "/" . $item );
  148. }
  149. return rmdir( $f );
  150. }
  151. else{
  152. return unlink( $f );
  153. }
  154. }
  155. function _createDirectory($path) {
  156. $this->_checkUserQuota();
  157. $path = $this->_basePath($path);
  158. return mkdir($path);
  159. }
  160. function _copy($source, $destination) {
  161. $this->_checkUserQuota();
  162. $source = $this->_basePath($source);
  163. $destination = $this->_basePath($destination);
  164. return copy($source, $destination);
  165. }
  166. function _rename($oldpath, $newpath) {
  167. $this->_checkUserQuota();
  168. $oldpath = $this->_basePath($oldpath);
  169. $newpath = $this->_basePath($newpath);
  170. return rename($oldpath, $newpath);
  171. }
  172. function _read($path) {
  173. $path = $this->_basePath($path);
  174. return file_get_contents($path);
  175. }
  176. function _quota($remaining) {
  177. if($remaining == "remaining") {
  178. return $this->_getRemainingQuota();
  179. }
  180. else if($remaining == "quota") {
  181. global $User;
  182. $cur = $User->get_current();
  183. $quota = $cur->get_quota();
  184. return $quota;
  185. }
  186. }
  187. function _write($path, $content) {
  188. $this->_checkUserQuota();
  189. $path = $this->_basePath($path);
  190. return file_put_contents($path, $content);
  191. }
  192. }