/ php-ppcms/includes/classes/core.directory.class.php
PHP | 317 lines | 261 code | 38 blank | 18 comment | 83 complexity | 12e5a6bd16b5ae0579d9e003efeff905 MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0
- <?php
- /***************************************************************
- * Copyright notice
- * (c) 2009, jianyuzhu@gmail.com
- * All rights reserved
- * This script is part of the PPEMI project.
- ***************************************************************/
-
- class CoreDirectoryParser {
- var $_root;
- var $_path;
- var $_dir;
- var $_files;
- var $_dirs;
-
- var $_attribute_enabled = false;
-
- //constructor
- function CoreDirectoryParser($dir, $path = '', $root = '') {
- $this->_dir = $this->_dir_format($dir);
-
- if( $path == '' || $path == '/' ) {
- $this->_path = '';
- } else {
- $this->_path = $this->_dir_format($path);
- }
- if( $root == '' ) {
- $this->_root = '';
- } else {
- $this->_root = ((substr($root, -1) == '/') ? $root : $root . '/');
- }
- }
-
- //methods
- function setDir($dir) {
- $this->_dir = $this->_dir_format($dir);
- }
-
- function getDir() {
- return $this->_dir;
- }
-
- function setPath($path) {
- $this->_path = $this->_dir_format($path);
- }
-
- function getPath() {
- return $this->_path;
- }
-
- function setAttributeEnabled() {
- $this->_attribute_enabled = true;
- }
-
- function setAttributeDisabled() {
- $this->_attribute_enabled = false;
- }
-
- function getAttributeEnabled() {
- return $this->_attribute_enabled;
- }
-
- function getRealDir() {
- return $this->_root . $this->_path . $this->_dir;
- }
-
- //
- function parse() {
- $dir = $this->_root . $this->_path . $this->_dir;
- $this->_files = array();
- $this->_dirs = array();
-
- if( $handle = opendir($dir) ) {
- while( false !== ($file = readdir($handle)) ) {
- if( $file != "." && $file != ".." && $file != "Thumbs.db" && $file != "index.php" && $file != "index.html" ) {
- if( is_dir($dir . $file . '/') ) {
- $this->_dirs[] = $file;
- } else {
- $this->_files[] = $file;
- }
- }
- }
- closedir($handle);
- }
- }
-
- function get($filter = '') {
-
- }
-
- function getDirs($filter = '') {
- $dirs = array();
- if( is_array($this->_dirs) ) {
- if( $filter != '' ) {
- foreach($this->_dirs as $dir) {
- if( isset($filter['p']) && $filter['p'] ) {
- if( substr($dir, 0, strlen($filter['p'])) == $filter['p'] ) {
- $dirs[] = $dir;
- }
- }
- }
- } else {
- $dirs = $this->_dirs;
- }
- } else {
- $dirs[] = $this->_dirs;
- }
-
- return $dirs;
- }
-
- function getFiles($filter = '') {
- $files = array();
- if( is_array($this->_files) ) {
- if( $filter != '' && is_array($filter) ) {
- foreach($this->_files as $file) {
- if( isset($filter['p']) && $filter['p'] ) {
- if( substr($file, 0, strlen($filter['p'])) == $filter['p'] ) {
- if( strrpos($file, '.') === false ) {
- $name = $file;
- $ext = '';
- } else {
- $name = substr($file, 0, strrpos($file, '.'));
- $ext = substr($file, strrpos($file, '.'));
- }
- $files[] = array('file' => $file, 'name' => $name, 'ext' => $ext);
- }
- } elseif( isset($filter['s']) && $filter['s'] ) {
- if( strpos($file, $filter['s']) !== false ) {
- if( strrpos($file, '.') === false ) {
- $name = $file;
- $ext = '';
- } else {
- $name = substr($file, 0, strrpos($file, '.'));
- $ext = substr($file, strrpos($file, '.'));
- }
- $files[] = array('file' => $file, 'name' => $name, 'ext' => $ext);
- }
- }
- }
- } elseif( $filter != '' ) {
- foreach($this->_files as $file) {
- if( strpos($file, $filter) !== false ) {
- if( strrpos($file, '.') === false ) {
- $name = $file;
- $ext = '';
- } else {
- $name = substr($file, 0, strrpos($file, '.'));
- $ext = substr($file, strrpos($file, '.'));
- }
- $files[] = array('file' => $file, 'name' => $name, 'ext' => $ext);
- }
- }
- } else {
- foreach($this->_files as $file) {
- if( strrpos($file, '.') === false ) {
- $name = $file;
- $ext = '';
- } else {
- $name = substr($file, 0, strrpos($file, '.'));
- $ext = substr($file, strrpos($file, '.'));
- }
- //
- if( $this->_attribute_enabled == true ) {
- $attribute = $this->getFileAttribute($file);
- $thumb = $this->getFileThumb($attribute, $file);
- $files[] = array('file' => $file, 'name' => $name, 'thumb' => $thumb, 'ext' => $ext, 'attribute' => $attribute);
- } else {
- $files[] = array('file' => $file, 'name' => $name, 'ext' => $ext);
- }
- }
- }
- } else {
- //$files = $this->_files;
- }
-
- return $files;
- }
-
- function getFilenames($filter = '') {
- $files = $this->getFiles($filter);
- if( is_array($files) ) {
- for($i=0, $n=sizeof($files); $i<$n; $i++) {
- $filenames[] = $files[$i]['file'];
- }
- }
-
- return $filenames;
- }
-
- function getFileThumb($attribute, $file) {
- $thumb_src = $this->_dir . $file;
- if( $attribute['is_image'] == '1' ) {
- $thumb_img = $thumb_src;
- if( $attribute['image_width'] < 100 && $attribute['image_height'] < 100 ) {
- $thumb = '<img src="' . $thumb_img . '" border="0">';
- } elseif( $attribute['image_width'] < $attribute['image_height'] ) {
- $thumb = '<img src="' . $thumb_img . '" height="100" border="0">';
- } else {
- $thumb = '<img src="' . $thumb_img . '" width="100" border="0">';
- }
- } else {
- $thumb_img = 'images/icons/' . substr($attribute['extension'], 1) . '.gif';
- $thumb = '<img src="' . $thumb_img . '" border="0"><br>' . $file;
- }
-
- return $thumb;
- }
-
- function getFileAttribute($filename, $is_array = true) {
- $file = $this->_root . $this->_path . $this->_dir . $filename;
-
- if( !$this->_not_null($file) ) {
- return false;
- }
- if( !is_file($file) ) {
- return false;
- }
-
- $filetime = strftime(DATE_TIME_FORMAT, filemtime($file));
- $filepath = dirname($file);
- $filename = basename($file);
- $extension = substr($file, strrpos($file, '.'));
-
- $filesize = number_format(filesize($file)) . ' bytes';
- //$this->permission = substr(sprintf('%o', fileperms($file)), -4);
- $permission = substr(sprintf('%o', fileperms($file)), -4);
- //$permission = util_get_file_permissions(fileperms($file));
-
- $image_size = @getimagesize($file);
- if( $image_size ) {
- $is_image = true;
- $image_width = $image_size['0'];
- $image_height = $image_size['1'];
- $image_type = $image_size['2'];
- $image_attribute = $image_size['3'];
- }
-
- if( $is_array == false ) {
- $attribute = 'filesize=' . $filesize . "\n"
- . 'extension= ' . $extension . "\n"
- . 'filetime=' . $filetime . "\n"
- . 'permission=' . $permission . "\n"
- . 'is_image=' . (($is_image == true) ? '1' : '0') . "\n"
- . 'image_width=' . $image_width . "\n"
- . 'image_height=' . $image_height . "\n"
- . 'image_type=' . $image_type . "\n"
- . 'image_attribute=' . $image_attribute . "\n";
- } else {
- $attribute = array(
- 'filesize' => $filesize,
- 'extension' => $extension,
- 'filetime' => $filetime,
- 'permission' => $permission ,
- 'is_image' => (($is_image == true) ? '1' : '0'),
- 'image_width' => $image_width,
- 'image_height' => $image_height,
- 'image_type' => $image_type,
- 'image_attribute' => $image_attribute,
- );
- }
-
- return $attribute;
- }
-
- function delete($files) {
- if( is_array($files) ) {
- foreach($files as $file) {
- if( isset($file['file']) && $this->_not_null($file['file']) && file_exists($this->_root . $this->_path . $this->_dir . $file['file']) ) {
- @unlink($this->_root . $this->_path . $this->_dir . $file['file']);
- } elseif( file_exists($this->_root . $this->_path . $this->_dir . $file) ) {
- @unlink($this->_root . $this->_path . $this->_dir . $file);
- }
- }
- } elseif( $this->_not_null($files) && file_exists($this->_root . $this->_path . $this->_dir . $files) ) {
- @unlink($this->_root . $this->_path . $this->_dir . $files);
- }
- }
-
- function clean($dirs) {
- if( is_array($dirs) ) {
- foreach($dirs as $dir) {
- util_rmdir($this->_root . $this->_path . $this->_dir . $dir);
- }
- } elseif( $this->_not_null($files) && file_exists($this->_root . $this->_path . $this->_dir . $files) ) {
- util_rmdir($this->_root . $this->_path . $this->_dir . $files);
- }
- }
-
- function _not_null($value) {
- return util_not_null($value);
- }
-
- function _dir_format($dir) {
- if( strlen($dir) == 0 ) {
- return '';
- }
- //if( substr($dir, 0, 1) == '/' ) {
- // $dir = substr($dir, 1);
- //}
- if( substr($dir, -1) != '/' ) {
- $dir .= '/';
- }
-
- return $dir;
- }
- }
-
- class CoreDirectory extends CoreDirectoryParser {
-
- //constructor
- function CoreDirectory($dir, $path = '', $root = '') {
- parent::CoreDirectoryParser($dir, $path, $root);
- }
- }
- //
- ?>