PageRenderTime 43ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/ php-ppcms/includes/classes/core.directory.class.php

http://php-ppcms.googlecode.com/
PHP | 317 lines | 261 code | 38 blank | 18 comment | 83 complexity | 12e5a6bd16b5ae0579d9e003efeff905 MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0
  1. <?php
  2. /***************************************************************
  3. * Copyright notice
  4. * (c) 2009, jianyuzhu@gmail.com
  5. * All rights reserved
  6. * This script is part of the PPEMI project.
  7. ***************************************************************/
  8. class CoreDirectoryParser {
  9. var $_root;
  10. var $_path;
  11. var $_dir;
  12. var $_files;
  13. var $_dirs;
  14. var $_attribute_enabled = false;
  15. //constructor
  16. function CoreDirectoryParser($dir, $path = '', $root = '') {
  17. $this->_dir = $this->_dir_format($dir);
  18. if( $path == '' || $path == '/' ) {
  19. $this->_path = '';
  20. } else {
  21. $this->_path = $this->_dir_format($path);
  22. }
  23. if( $root == '' ) {
  24. $this->_root = '';
  25. } else {
  26. $this->_root = ((substr($root, -1) == '/') ? $root : $root . '/');
  27. }
  28. }
  29. //methods
  30. function setDir($dir) {
  31. $this->_dir = $this->_dir_format($dir);
  32. }
  33. function getDir() {
  34. return $this->_dir;
  35. }
  36. function setPath($path) {
  37. $this->_path = $this->_dir_format($path);
  38. }
  39. function getPath() {
  40. return $this->_path;
  41. }
  42. function setAttributeEnabled() {
  43. $this->_attribute_enabled = true;
  44. }
  45. function setAttributeDisabled() {
  46. $this->_attribute_enabled = false;
  47. }
  48. function getAttributeEnabled() {
  49. return $this->_attribute_enabled;
  50. }
  51. function getRealDir() {
  52. return $this->_root . $this->_path . $this->_dir;
  53. }
  54. //
  55. function parse() {
  56. $dir = $this->_root . $this->_path . $this->_dir;
  57. $this->_files = array();
  58. $this->_dirs = array();
  59. if( $handle = opendir($dir) ) {
  60. while( false !== ($file = readdir($handle)) ) {
  61. if( $file != "." && $file != ".." && $file != "Thumbs.db" && $file != "index.php" && $file != "index.html" ) {
  62. if( is_dir($dir . $file . '/') ) {
  63. $this->_dirs[] = $file;
  64. } else {
  65. $this->_files[] = $file;
  66. }
  67. }
  68. }
  69. closedir($handle);
  70. }
  71. }
  72. function get($filter = '') {
  73. }
  74. function getDirs($filter = '') {
  75. $dirs = array();
  76. if( is_array($this->_dirs) ) {
  77. if( $filter != '' ) {
  78. foreach($this->_dirs as $dir) {
  79. if( isset($filter['p']) && $filter['p'] ) {
  80. if( substr($dir, 0, strlen($filter['p'])) == $filter['p'] ) {
  81. $dirs[] = $dir;
  82. }
  83. }
  84. }
  85. } else {
  86. $dirs = $this->_dirs;
  87. }
  88. } else {
  89. $dirs[] = $this->_dirs;
  90. }
  91. return $dirs;
  92. }
  93. function getFiles($filter = '') {
  94. $files = array();
  95. if( is_array($this->_files) ) {
  96. if( $filter != '' && is_array($filter) ) {
  97. foreach($this->_files as $file) {
  98. if( isset($filter['p']) && $filter['p'] ) {
  99. if( substr($file, 0, strlen($filter['p'])) == $filter['p'] ) {
  100. if( strrpos($file, '.') === false ) {
  101. $name = $file;
  102. $ext = '';
  103. } else {
  104. $name = substr($file, 0, strrpos($file, '.'));
  105. $ext = substr($file, strrpos($file, '.'));
  106. }
  107. $files[] = array('file' => $file, 'name' => $name, 'ext' => $ext);
  108. }
  109. } elseif( isset($filter['s']) && $filter['s'] ) {
  110. if( strpos($file, $filter['s']) !== false ) {
  111. if( strrpos($file, '.') === false ) {
  112. $name = $file;
  113. $ext = '';
  114. } else {
  115. $name = substr($file, 0, strrpos($file, '.'));
  116. $ext = substr($file, strrpos($file, '.'));
  117. }
  118. $files[] = array('file' => $file, 'name' => $name, 'ext' => $ext);
  119. }
  120. }
  121. }
  122. } elseif( $filter != '' ) {
  123. foreach($this->_files as $file) {
  124. if( strpos($file, $filter) !== false ) {
  125. if( strrpos($file, '.') === false ) {
  126. $name = $file;
  127. $ext = '';
  128. } else {
  129. $name = substr($file, 0, strrpos($file, '.'));
  130. $ext = substr($file, strrpos($file, '.'));
  131. }
  132. $files[] = array('file' => $file, 'name' => $name, 'ext' => $ext);
  133. }
  134. }
  135. } else {
  136. foreach($this->_files as $file) {
  137. if( strrpos($file, '.') === false ) {
  138. $name = $file;
  139. $ext = '';
  140. } else {
  141. $name = substr($file, 0, strrpos($file, '.'));
  142. $ext = substr($file, strrpos($file, '.'));
  143. }
  144. //
  145. if( $this->_attribute_enabled == true ) {
  146. $attribute = $this->getFileAttribute($file);
  147. $thumb = $this->getFileThumb($attribute, $file);
  148. $files[] = array('file' => $file, 'name' => $name, 'thumb' => $thumb, 'ext' => $ext, 'attribute' => $attribute);
  149. } else {
  150. $files[] = array('file' => $file, 'name' => $name, 'ext' => $ext);
  151. }
  152. }
  153. }
  154. } else {
  155. //$files = $this->_files;
  156. }
  157. return $files;
  158. }
  159. function getFilenames($filter = '') {
  160. $files = $this->getFiles($filter);
  161. if( is_array($files) ) {
  162. for($i=0, $n=sizeof($files); $i<$n; $i++) {
  163. $filenames[] = $files[$i]['file'];
  164. }
  165. }
  166. return $filenames;
  167. }
  168. function getFileThumb($attribute, $file) {
  169. $thumb_src = $this->_dir . $file;
  170. if( $attribute['is_image'] == '1' ) {
  171. $thumb_img = $thumb_src;
  172. if( $attribute['image_width'] < 100 && $attribute['image_height'] < 100 ) {
  173. $thumb = '<img src="' . $thumb_img . '" border="0">';
  174. } elseif( $attribute['image_width'] < $attribute['image_height'] ) {
  175. $thumb = '<img src="' . $thumb_img . '" height="100" border="0">';
  176. } else {
  177. $thumb = '<img src="' . $thumb_img . '" width="100" border="0">';
  178. }
  179. } else {
  180. $thumb_img = 'images/icons/' . substr($attribute['extension'], 1) . '.gif';
  181. $thumb = '<img src="' . $thumb_img . '" border="0"><br>' . $file;
  182. }
  183. return $thumb;
  184. }
  185. function getFileAttribute($filename, $is_array = true) {
  186. $file = $this->_root . $this->_path . $this->_dir . $filename;
  187. if( !$this->_not_null($file) ) {
  188. return false;
  189. }
  190. if( !is_file($file) ) {
  191. return false;
  192. }
  193. $filetime = strftime(DATE_TIME_FORMAT, filemtime($file));
  194. $filepath = dirname($file);
  195. $filename = basename($file);
  196. $extension = substr($file, strrpos($file, '.'));
  197. $filesize = number_format(filesize($file)) . ' bytes';
  198. //$this->permission = substr(sprintf('%o', fileperms($file)), -4);
  199. $permission = substr(sprintf('%o', fileperms($file)), -4);
  200. //$permission = util_get_file_permissions(fileperms($file));
  201. $image_size = @getimagesize($file);
  202. if( $image_size ) {
  203. $is_image = true;
  204. $image_width = $image_size['0'];
  205. $image_height = $image_size['1'];
  206. $image_type = $image_size['2'];
  207. $image_attribute = $image_size['3'];
  208. }
  209. if( $is_array == false ) {
  210. $attribute = 'filesize=' . $filesize . "\n"
  211. . 'extension= ' . $extension . "\n"
  212. . 'filetime=' . $filetime . "\n"
  213. . 'permission=' . $permission . "\n"
  214. . 'is_image=' . (($is_image == true) ? '1' : '0') . "\n"
  215. . 'image_width=' . $image_width . "\n"
  216. . 'image_height=' . $image_height . "\n"
  217. . 'image_type=' . $image_type . "\n"
  218. . 'image_attribute=' . $image_attribute . "\n";
  219. } else {
  220. $attribute = array(
  221. 'filesize' => $filesize,
  222. 'extension' => $extension,
  223. 'filetime' => $filetime,
  224. 'permission' => $permission ,
  225. 'is_image' => (($is_image == true) ? '1' : '0'),
  226. 'image_width' => $image_width,
  227. 'image_height' => $image_height,
  228. 'image_type' => $image_type,
  229. 'image_attribute' => $image_attribute,
  230. );
  231. }
  232. return $attribute;
  233. }
  234. function delete($files) {
  235. if( is_array($files) ) {
  236. foreach($files as $file) {
  237. if( isset($file['file']) && $this->_not_null($file['file']) && file_exists($this->_root . $this->_path . $this->_dir . $file['file']) ) {
  238. @unlink($this->_root . $this->_path . $this->_dir . $file['file']);
  239. } elseif( file_exists($this->_root . $this->_path . $this->_dir . $file) ) {
  240. @unlink($this->_root . $this->_path . $this->_dir . $file);
  241. }
  242. }
  243. } elseif( $this->_not_null($files) && file_exists($this->_root . $this->_path . $this->_dir . $files) ) {
  244. @unlink($this->_root . $this->_path . $this->_dir . $files);
  245. }
  246. }
  247. function clean($dirs) {
  248. if( is_array($dirs) ) {
  249. foreach($dirs as $dir) {
  250. util_rmdir($this->_root . $this->_path . $this->_dir . $dir);
  251. }
  252. } elseif( $this->_not_null($files) && file_exists($this->_root . $this->_path . $this->_dir . $files) ) {
  253. util_rmdir($this->_root . $this->_path . $this->_dir . $files);
  254. }
  255. }
  256. function _not_null($value) {
  257. return util_not_null($value);
  258. }
  259. function _dir_format($dir) {
  260. if( strlen($dir) == 0 ) {
  261. return '';
  262. }
  263. //if( substr($dir, 0, 1) == '/' ) {
  264. // $dir = substr($dir, 1);
  265. //}
  266. if( substr($dir, -1) != '/' ) {
  267. $dir .= '/';
  268. }
  269. return $dir;
  270. }
  271. }
  272. class CoreDirectory extends CoreDirectoryParser {
  273. //constructor
  274. function CoreDirectory($dir, $path = '', $root = '') {
  275. parent::CoreDirectoryParser($dir, $path, $root);
  276. }
  277. }
  278. //
  279. ?>