PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/1.3/includes/directory.class.php

http://kfm.googlecode.com/
PHP | 217 lines | 217 code | 0 blank | 0 comment | 53 complexity | 1edb8aaaf26058d071ae72e19c6a4a35 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1, Apache-2.0
  1. <?php
  2. $kfmDirectoryInstances=array();
  3. class kfmDirectory extends kfmObject{
  4. var $subDirs=array();
  5. function kfmDirectory($id=1){
  6. parent::__construct();
  7. $this->id=$id;
  8. if(!$id)return;
  9. $res=db_fetch_row("SELECT * FROM ".KFM_DB_PREFIX."directories WHERE id=".$this->id);
  10. if(!$res)return $this->id=0;
  11. $this->name=$res['name'];
  12. $this->pid=$res['parent'];
  13. $this->path=$this->getPath();
  14. }
  15. function __construct($id=1){
  16. $this->kfmDirectory($id);
  17. }
  18. function addSubdirToDb($name){
  19. global $kfm;
  20. $sql="INSERT INTO ".KFM_DB_PREFIX."directories (name,parent) VALUES('".sql_escape($name)."',".$this->id.")";
  21. return $kfm->db->exec($sql);
  22. }
  23. function checkAddr($addr){
  24. return (
  25. strpos($addr,'..')===false&&
  26. strpos($addr,'.')!==0&&
  27. strpos($addr,'/.')===false);
  28. }
  29. function createSubdir($name){
  30. if(!$GLOBALS['kfm_allow_directory_create'])return $this->error(kfm_lang('permissionDeniedCreateDirectory'));
  31. $physical_address=$this->path.$name;
  32. $short_version=str_replace($GLOBALS['rootdir'],'',$physical_address);
  33. if(!$this->checkAddr($physical_address)){
  34. $this->error(kfm_lang('illegalDirectoryName',$short_version));
  35. return false;
  36. }
  37. if(file_exists($physical_address)){
  38. $this->error(kfm_lang('alreadyExists',$short_version));
  39. return false;
  40. }
  41. mkdir($physical_address);
  42. if(!file_exists($physical_address)){
  43. $this->error(kfm_lang('failedCreateDirectoryCheck',$name));
  44. return false;
  45. }
  46. return $this->addSubdirToDb($name);
  47. }
  48. function delete(){
  49. global $kfm;
  50. if(!$GLOBALS['kfm_allow_directory_delete'])return $this->error(kfm_lang('permissionDeniedDeleteDirectory'));
  51. $files=$this->getFiles();
  52. foreach($files as $f){
  53. if(!$f->delete())return false;
  54. }
  55. $subdirs=$this->getSubdirs();
  56. foreach($subdirs as $subdir){
  57. if(!$subdir->delete())return false;
  58. }
  59. rmdir($this->path);
  60. if(is_dir($this->path))return $this->error('failed to delete directory '.$this->path);
  61. $kfm->db->exec("delete from ".KFM_DB_PREFIX."directories where id=".$this->id);
  62. return true;
  63. }
  64. function getFiles(){
  65. $this->handle=opendir($this->path);
  66. if(!$this->handle)return $this->error('unable to open directory');
  67. $filesdb=db_fetch_all("select * from ".KFM_DB_PREFIX."files where directory=".$this->id);
  68. $fileshash=array();
  69. if(is_array($filesdb))foreach($filesdb as $r)$fileshash[$r['name']]=$r['id'];
  70. $files=array();
  71. while(false!==($filename=readdir($this->handle))){
  72. if(is_file($this->path.$filename)&&kfmFile::checkName($filename)){
  73. if(!isset($fileshash[$filename]))$fileshash[$filename]=kfmFile::addToDb($filename,$this->id);
  74. $file=kfmFile::getInstance($fileshash[$filename]);
  75. if(!$file)continue;
  76. if($file->isImage())$file=kfmImage::getInstance($fileshash[$filename]);
  77. $files[]=$file;
  78. unset($fileshash[$filename]);
  79. }
  80. }
  81. return $files;
  82. }
  83. function getInstance($id=1){
  84. global $kfmDirectoryInstances;
  85. if(!isset($kfmDirectoryInstances[$id])){
  86. $dir=new kfmDirectory($id);
  87. if($dir->id==0)return false;
  88. $kfmDirectoryInstances[$id]=$dir;
  89. }
  90. return $kfmDirectoryInstances[$id];
  91. }
  92. function getPath(){
  93. $pathTmp=$this->name.'/';
  94. $pid=$this->pid;
  95. if(!$pid)return $GLOBALS['rootdir'];
  96. while($pid>1){
  97. $p=kfmDirectory::getInstance($pid);
  98. $pathTmp=$p->name.'/'.$pathTmp;
  99. $pid=$p->pid;
  100. }
  101. return $GLOBALS['rootdir'].$pathTmp;
  102. }
  103. function getProperties(){
  104. return array(
  105. 'allowed_file_extensions' => '',
  106. 'name' => $this->name,
  107. 'path' => str_replace($_SERVER['DOCUMENT_ROOT'],'',$this->path),
  108. 'parent' => $this->pid,
  109. 'is_writable' => $this->isWritable()
  110. );
  111. }
  112. function getSubdirs($oldstyle=false){
  113. global $kfm;
  114. $this->handle=opendir($this->path);
  115. $dirsdb=db_fetch_all("select id,name from ".KFM_DB_PREFIX."directories where parent=".$this->id);
  116. $dirshash=array();
  117. if(is_array($dirsdb))foreach($dirsdb as $r)$dirshash[$r['name']]=$r['id'];
  118. $directories=array();
  119. while(false!==($file=readdir($this->handle))){
  120. if(is_dir($this->path.$file)&&$this->checkName($file)){
  121. if(!isset($dirshash[$file])){
  122. $this->addSubdirToDb($file);
  123. $dirshash[$file]=$kfm->db->lastInsertId(KFM_DB_PREFIX.'directories','id');
  124. }
  125. $directories[]=kfmDirectory::getInstance($dirshash[$file]);
  126. unset($dirshash[$file]);
  127. }
  128. }
  129. return $directories;
  130. }
  131. function hasSubdirs(){
  132. $this->handle=opendir($this->path);
  133. if($this->handle){
  134. while(false!==($file=readdir($this->handle))){
  135. if($this->checkName($file) && is_dir($this->path.$file)) return true;
  136. }
  137. return false;
  138. }else{
  139. $this->error('Directory could not be opened');
  140. }
  141. }
  142. function getSubdir($dirname){
  143. $res=db_fetch_row('select id from '.KFM_DB_PREFIX.'directories where name="'.$dirname.'" and parent='.$this->id);
  144. if($res)return kfmDirectory::getInstance($res['id']);
  145. return false;
  146. }
  147. function isWritable(){
  148. return is_writable($this->path);
  149. }
  150. function moveTo($newParent){
  151. global $kfm;
  152. if(is_numeric($newParent))$newParent=kfmDirectory::getInstance($newParent);
  153. { # check for errors
  154. if(!$GLOBALS['kfm_allow_directory_move'])return $this->error(kfm_lang('permissionDeniedMoveDirectory'));
  155. if(strpos($newParent->path,$this->path)===0) return $this->error(kfm_lang('cannotMoveIntoSelf'));
  156. if(file_exists($newParent->path.$this->name))return $this->error(kfm_lang('alreadyExists',$newParent->path.$this->name));
  157. if(!$newParent->isWritable())return $this->error(kfm_lang('isNotWritable',$newParent->path));
  158. }
  159. { # do the move and check that it was successful
  160. rename($this->path,$newParent->path.'/'.$this->name);
  161. if(!file_exists($newParent->path.$this->name))return $this->error(kfm_lang('couldNotMoveDirectory',$this->path,$newParent->path.$this->name));
  162. }
  163. { # update database and kfmDirectory object
  164. $kfm->db->exec("update ".KFM_DB_PREFIX."directories set parent=".$newParent->id." where id=".$this->id) or die('error: '.print_r($kfmdb->errorInfo(),true));
  165. $this->pid=$newParent->id;
  166. $this->path=$this->getPath();
  167. }
  168. }
  169. function rename($newname){
  170. global $kfm,$kfm_allow_directory_edit,$kfmDirectoryInstances;
  171. if(!$GLOBALS['kfm_allow_directory_edit'])return $this->error(kfm_lang('permissionDeniedEditDirectory'));
  172. if(!$this->isWritable())return $this->error(kfm_lang('permissionDeniedRename',$this->name));
  173. if(!$this->checkAddr($newname))return $this->error(kfm_lang('cannotRenameFromTo',$this->name,$newname));
  174. $parent=kfmDirectory::getInstance($this->pid);
  175. if(file_exists($parent->path.$newname))return $this->error(kfm_lang('aDirectoryNamedAlreadyExists',$newname));
  176. rename($this->path,$parent->path.$newname);
  177. if(file_exists($this->path))return $this->error(kfm_lang('failedRenameDirectory'));
  178. $kfm->db->query("update ".KFM_DB_PREFIX."directories set name='".sql_escape($newname)."' where id=".$this->id);
  179. $this->name=$newname;
  180. $this->path=$this->getPath();
  181. $kfmDirectoryInstances[$this->id]=$this;
  182. }
  183. function checkName($file=false){
  184. if($file===false)$file=$this->name;
  185. if(trim($file)==''|| trim($file)!=$file)return false;
  186. if($file=='.'||$file=='..')return false;
  187. foreach($GLOBALS['kfm_banned_folders'] as $ban){
  188. if(($ban[0]=='/' || $ban[0]=='@')&&preg_match($ban,$file))return false;
  189. elseif($ban==strtolower(trim($file)))return false;
  190. }
  191. if(isset($GLOBALS['kfm_allowed_folders']) && is_array($GLOBALS['kfm_allowed_folders'])){
  192. foreach($GLOBALS['kfm_allowed_folders'] as $allow){
  193. if($allow[0]=='/' || $allow[0]=='@'){
  194. if(preg_match($allow, $file))return true;
  195. }else if($allow==strtolower($file)) return true;
  196. }
  197. return false;
  198. }
  199. return true;
  200. }
  201. function addFile($file){
  202. if(!$GLOBALS['kfm_allow_file_create'])return $this->error(kfm_lang('permissionDeniedCreateFile'));
  203. if(is_numeric($file))$file=kfmFile::getInstance($file);
  204. if(!$this->isWritable())return $this->error(kfm_lang('fileNotCreatedDirUnwritable',$file->name));
  205. copy($file->path,$this->path.'/'.$file->name);
  206. $id=$file->addToDb($file->name,$this->id);
  207. if($file->isImage()){
  208. $file=kfmImage::getInstance($file->id);
  209. $newFile=kfmImage::getInstance($id);
  210. $newFile->setCaption($file->caption);
  211. }
  212. else $newFile=kfmFile::getInstance($id);
  213. $newFile->setTags($file->getTags());
  214. return true;
  215. }
  216. }
  217. ?>