* @copyright 2004 by http://wombat.exit0.net * @package wombatSite * @subpackage install */ /** * make tool: set per site permission * * does nothing... * * @version 1.0.0 * @package wombatSite * @subpackage install */ class wbMaker_ImageTools extends wbMaker { /** * information * * @access private * @var array $_info */ var $_info = array( 'maker' => 'ImageTools', 'state' => 'new', 'brief' => 'Create folders and set permissions for image-tools', 'msg' => '' ); /** * @access private * @var string $_baseDir */ var $_baseDir; /** * @access private * @var string $_varDir */ var $_varDir; /** * @access private * @var string $_htdocDic */ var $_htdocDir; /** * sub directories for var dir * * @access private * @var array $_varDirs */ var $_varDirs = array( 'upload/gallery' => 0777, 'upload/image/gal' => 0777 ); /** * sub directories of htdocDir * * @access private * @var array $_htdocDirs */ var $_htdocDirs = array( 'img/cnt', ); /** * permission codes * * @access private * @var array $_perms */ var $_perms = array( 'dir' => 0777, 'file' => 0666, 'link' => 0777, ); /** * touched files and folders * * @access private * @var array $_touch */ var $_touch = array( 'dir' => array(), 'file' => array(), 'link' => array() ); /** * self * * @access private * @var array $_self */ var $_self = array( 'uid' => 0, 'gid' => 0 ); /** * bring birth! * * @access public * @return boolean $result true on success */ function __construct() { umask( 0000 ); $this->_baseDir = wbFactory::getParam( 'baseDir' ); $this->_htdocDir = wbFactory::getParam( 'htdocDir' ); $this->_varDir = wbFactory::getParam( 'varDir' ); // if posic functions don't exist, we assume to have the right permissions if( function_exists( 'posix_geteuid' ) ) { $this->_self['uid'] = posix_geteuid(); $this->_self['gid'] = posix_getegid(); } } /** * php4 constructor wrapper * * @access public * @see __construct() */ function wbMaker_ImageTools() { $this->__construct(); } /** * run... * * @access public * @return boolean true on success, false if any target has failed (or patError object on error!) */ function make() { // make var dirs $base = $this->_baseDir . '/' . $this->_varDir; foreach( $this->_varDirs as $dir => $mode ) { // does not even exists if( !file_exists( $base . '/' . $dir ) ) { $res = @mkdir( $base . '/' . $dir, $mode ); if( !$res ) { $this->_info['state'] = 'failed'; $this->_info['msg'] = 'Could not create directory at "'. $dir .'"!'; return false; } wbDebugger::addMsg( 'Make', 'Create directory: "'. $dir .'"', 'ImageTools' ); continue; } // exists but is not a directory if( !is_dir( $base . '/' . $dir ) ) { $this->_info['state'] = 'failed'; $this->_info['msg'] = '"'. $dir .'" is not a directory!'; return false; } $res = @chmod( $base . '/' . $dir, $mode ); if( !$res ) { $this->_info['state'] = 'failed'; $this->_info['msg'] = 'Could not change permissions of "'. $dir .'"!'; return false; } wbDebugger::addMsg( 'Make', 'Changed permission: "'. $dir .'"', 'ImageTools' ); } // make dirs in htdoc $base = $this->_baseDir . '/' . $this->_htdocDir; foreach( $this->_htdocDirs as $dir ) { // does not even exists if( !file_exists( $base . '/' . $dir ) ) { $res = mkdir( $base . '/' . $dir, $this->_perms['dir'] ); if( !$res ) { $this->_info['state'] = 'failed'; $this->_info['msg'] = 'Could not create directory at "'. $dir .'"!'; return false; } wbDebugger::addMsg( 'Make', 'Create directory: "'. $dir .'"', 'ImageTools' ); continue; } // exists but is not a directory if( !is_dir( $base . '/' . $dir ) ) { $this->_info['state'] = 'failed'; $this->_info['msg'] = '"'. $dir .'" is not a directory!'; return false; } if( !$this->_setPerms( $base . '/' . $dir ) ) { return false; } if( !$this->_scanDir( $base . '/' . $dir ) ) { return false; } } // it's all good! $this->_info['msg'] = 'Adjusted permissions of ' . count( $this->_touch['dir'] ) .' dir(s) / ' . count( $this->_touch['file'] ) .' file(s) / ' . count( $this->_touch['link'] ) .' link(s).'; return true; } /** * set permissions for entry * * @access private * @param string $file * @return string $type file type or false on error */ function _setPerms( $file ) { $owner = fileowner( $file ); // check owner! if( $this->_self['uid'] && ( $owner != $this->_self['uid'] ) ) { $this->_info['state'] = 'failed'; $this->_info['msg'] = 'Only owner (or root) may change file permisions, file: "'. $file .'"!'; return false; } $type = false; if( is_file( $file ) ) { $type = 'file'; } else if( is_dir( $file ) ) { $type = 'dir'; } else if( is_link( $file ) ) { $type = 'link'; } if( !$type ) { $this->_info['state'] = 'failed'; $this->_info['msg'] = 'Unknown filetype of file: "'. $file .'"!'; return false; } $res = chmod( $file, $this->_perms[$type] ); if( !$res ) { $this->_info['state'] = 'failed'; $this->_info['msg'] = 'Changing permission of file "'. $file .'" failed for an unknown reason!'; wbDebugger::addMsg( 'Make', 'Changemode failed: chmod(' . $file . ', '. (string) $perms .')', 'SitePermission' ); return false; } array_push( $this->_touch[$type], $file ); return $type; } /** * scan directory recursivly * * @access private * @param string dir * @return boolean true on success, false if any target has failed (or patError object on error!) */ function _scanDir( $dir ) { $dh = dir( $dir ); while( false !== ( $file = $dh->read() ) ) { //skip hidden files and dirs if( $file[0] == '.') { continue; } $type = $this->_setPerms( $dir . '/' . $file ); if( !$type ) { return false; } if( $type == 'dir' ) { if( !$this->_scanDir( $dir . '/' . $file ) ) { return false; } } } return true; } } ?>