* @copyright 2004/2005 by http://wombat.exit0.net * @package wombatSite * @subpackage install */ /** * make tool: set permissions of whole framework * * well, we need read permission to the framework * * * @version 1.1.0 * @package wombatSite * @subpackage install */ class wbMaker_SystemPermission extends wbMaker { /** * information * * @access private * @var array $_info */ var $_info = array( 'maker' => 'SystemPermission', 'state' => 'new', 'brief' => 'Set read permission to the files of the framework', 'msg' => '' ); /** * where is the system dir? * * @access private * @var string $_baseDir */ var $_sysDir; /** * permission codes * * @access private * @var array $_perms */ var $_perms = array( 'dir' => 0755, 'file' => 0644, 'link' => 0777, ); /** * directory to be scaned * * @access private * @var array $_specials */ var $_dirs = array( 'admin', 'bin', 'include', //'doc', ); /** * use special permissions for some files/directories * * @access private * @var array $_specials */ var $_specials = array( 'bin/*' => 0755, ); /** * special dirs for internal use only * * @access private * @var array $_specials */ var $_specialsWork = array( 'dir' => array(), 'file' => array(), 'perms' => array() ); /** * 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->_sysDir = wbFactory::getParam( 'systemDir' ); // 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(); } foreach( $this->_specials as $path => $perms ) { $path = explode( '/', $path ); $file = array_pop( $path ); $path = $this->_sysDir . '/' . implode( '/', $path ); array_push( $this->_specialsWork['dir'], $path ); array_push( $this->_specialsWork['file'], $file ); array_push( $this->_specialsWork['perms'], $perms ); } } /** * php4 constructor wrapper * * @access public * @see __construct() */ function wbMaker_SystemPermission() { $this->__construct(); } /** * run... * * @access public * @return boolean true on success, false if any target has failed (or patError object on error!) */ function make() { if( !is_dir( $this->_sysDir ) ) { $this->_info['state'] = 'failed'; $this->_info['msg'] = 'Could not find system directory "'. $this->_sysDir .'"!'; return false; } if( !$this->_setPerms( $this->_sysDir ) ) { return false; } // set permissions for all known directories foreach( $this->_dirs as $dir ) { if( !$this->_scanDir( $this->_sysDir . '/' . $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; } $perms = $this->_perms[$type]; // use special perms? $dir = dirname( $file ); $index = array_search( $dir, $this->_specialsWork['dir'] ); if( $index !== false ) { if( $this->_specialsWork['file'][$index] == '*' ) { $perms = $this->_specialsWork['perms'][$index]; wbDebugger::addMsg( 'Make', 'File ' . $file . ' matches * for special permission '. (string) $perms .'.', 'SystemPermission' ); } else if( basename( $file ) == $this->_specialsWork['file'][$index] ) { $perms = $this->_specialsWork['perms'][$index]; wbDebugger::addMsg( 'Make', 'File ' . $file . ' matches exactly for special permission '. (string) $perms .'.', 'SystemPermission' ); } } $res = chmod( $file, $perms ); 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 .')', 'SystemPermission' ); 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; } } ?>