Source

phpHgAdmin / admin / application / libraries / phphgadmin.php

The default branch has multiple heads

<?php

class phpHgAdmin
{
	private $_ci;
	private $_ofl_lock_hgweb;
	private $_ofl_lock_hgrc;
	
	private $_cache;
	
	function __construct()
	{
		$this->_ci = &get_instance();
		$this->_ci->load->library('Hg_Filesystem');
		$this->_ci->load->library('Hg_Ini');
		
		$this->flush_cache();
	}
	
	function start_tx(&$ofl_lock_hgweb, &$ofl_lock_hgrc)
	{
		$this->_ofl_lock_hgweb = &$ofl_lock_hgweb;
		$this->_ofl_lock_hgrc = &$ofl_lock_hgrc;
	}
	
	function end_tx()
	{
		$dummy = '';
		$this->_ofl_lock_hgweb = &$dummy;
		$this->_ofl_lock_hgrc = &$dummy;
	}
	
	function flush_cache()
	{
		$this->_cache = array();
	}
	
	function lsdir($webdir = null)
	{
//		if(isset($this->_cache['lsdir']))
//		{
//			return $this->_cache['lsdir'];
//		}
		
		// retrieves current directory structure
		$realdir = $this->_ci->hg_filesystem->real_dirscan();
		
		// retrieves current 
		$this->_ci->hg_ini->register_OFL($this->_ofl_lock_hgweb);
		$hgwebrepos_compat = $this->_ci->hg_ini->getRepoList($webdir);
				
		$hgwebrepos = array();
		if(is_array($hgwebrepos_compat))
		{
			foreach($hgwebrepos_compat as $r_name => $r_path)
			{
				$hgwebrepos[] = base64_decode($r_name);
			}
		}
		else
		{
			$this->_cache['lsdir'] = $hgwebrepos;
			// error code
			return $hgwebrepos;
		}
		
		$allrepo = $realdir;
		$allrepo = array_merge($realdir, $hgwebrepos);


		$hgrepos = array();
		foreach($allrepo as $repo)
		{
			$hgrepos[$repo]['name'] = $repo;
			
			if(isset($realdir[$repo]) && in_array($repo, $hgwebrepos))
			{
				$hgrepos[$repo]['status'] = HGPHP_REPO_STATUS_ENABLED;
			}
			else if(isset($realdir[$repo]) && !in_array($repo, $hgwebrepos))
			{
				$hgrepos[$repo]['status'] = HGPHP_REPO_STATUS_DISABLED;
			}
			else if(!isset($realdir[$repo]) && in_array($repo, $hgwebrepos))
			{
				$hgrepos[$repo]['status'] = HGPHP_REPO_STATUS_MISSING;
			}
		}
		
		$this->_cache['lsdir'] = $hgrepos;
		return $hgrepos;
	}
	
	/**
	 * stat_repository
	 * Returns the HGRC represented as an array for the specified repository
	 * @param r_name name of the project whose hgrc to retrieve
	 * @return array representing hgrc or status code
	 */
	function stat_repository($r_id, $profile = null)
	{
		if(!$this->can_view($r_id))
		{
			return HGPHP_ERR_PERM_USR;
		}
		$this->_ci->hg_ini->register_OFL($this->_ofl_lock_hgrc);
		return $this->_ci->hg_ini->loadHgRC($r_id, $profile);
	}
	
	function create_repository($r_name, $profile = null)
	{
		if(!$this->can_create($r_name))
		{
			return HGPHP_ERR_PERM_USR;
		}
		
		$create_status = HGPHP_OK;
		
		$lsdir = $this->_ci->hg_ini->getRepoList($profile); 
		
		// not registered in hgweb.config
		$hashr_name = base64_encode($r_name);
		if(!isset($lsdir[$hashr_name]))
		{
			
			// edit the directory
//			$existingdir[$r_name] = $r_name;
			$this->_ci->hg_ini->register_OFL($this->_ofl_lock_hgweb);
			$create_status = $this->_ci->hg_ini->registerRepo($r_name, $profile);
			$this->flush_cache();
			if($create_status == HGPHP_OK)
			{
				// then create the repository
				$this->_ci->hg_ini->register_OFL($this->_ofl_lock_hgrc);
				$create_status = $this->_ci->hg_filesystem->create_repository_dir($r_name);
				
				if($create_status == HGPHP_OK)
				{
					
					$create_status = $this->_ci->hg_ini->touchHgRC($r_name, $profile);
				}
			}
		}
		else
		{
			// repository already exists
			$create_status = HGPHP_ERR_FS_PREEXISTS;
		}
		
		
		return $create_status;
	}
	
	/**
	 * update_repository
	 * Update repository's hgrc
	 * @param r_name name of the repository to update hgrc for
	 * @param hgrc_data array representing new hgrc file
	 * @return status code
	 */
	function update_repository($r_name, $hgrc_data, $profile = null)
	{
		if(!$this->can_update($r_name))
		{
			return HGPHP_ERR_PERM_USR;
		}
		$this->_ci->hg_ini->register_OFL($this->_ofl_lock_hgrc);
		
		$this->flush_cache();
		return $this->_ci->hg_ini->saveHgRC($r_name, $hgrc_data, $profile);
	}
	
	/**
	 * delete_repository
	 * Deletes a repository from the file system and unregisters it from hgweb.config
	 * @param r_key id of the repo to delete permanently
	 * @return status code
	 */
	function delete_repository($r_key, $hgweb = null)
	{
		$hashr_key = base64_encode($r_key);
		if(!$this->can_delete($r_key))
		{
			return HGPHP_ERR_PERM_USR;
		}
		
		$del_status = HGPHP_OK;
		$lsdir = $this->_ci->hg_ini->getRepoList($hgweb);
		
		if(isset($lsdir[$hashr_key]))
		{
			// edit the directory
			// remove from filesystem
			$this->_ci->hg_ini->register_OFL($this->_ofl_lock_hgweb);
				
			$del_status = $this->_ci->hg_filesystem->delete_repository_dir($r_key);
			
			// remove from hgweb.config
			if($del_status == HGPHP_OK)
			{
				$del_status = $this->_ci->hg_ini->unregisterRepo($r_key, $hgweb);
			}
//			unset($existingdir[$r_key]);
		}
		else
		{
			$del_status = HGPHP_ERR_FS_PREEXISTS;
		}
		$this->flush_cache();
		return $del_status;
	}
	
	/**
	 * can_create
	 * Checks if user has permissions to create this repository.
	 * Requires view permission.
	 * @param r_name name of repository wanting to be created
	 * @return true if allowed
	 */
	function can_create($r_name)
	{
		return $this->can_view($r_name) && $this->_ci->config->item('global_allow_repo_create');
	}
	
	/**
	 * can_update
	 * Checks if user has permissions to update this repository
	 * Requires view permission.
	 * @param r_name name of repository wanting to be updated
	 * @return true if allowed
	 */
	function can_update($r_name)
	{
		return $this->can_view($r_name) && $this->_ci->config->item('global_allow_repo_update');
	}
	
	/**
	 * can_create
	 * Checks if user has permissions to view this repository
	 * @param r_name name of repository wanting to be created
	 * @return true if allowed
	 */
	function can_view($r_name)
	{
		return $this->_ci->config->item('global_allow_repo_view');
	}
	
	/**
	 * can_delete
	 * Checks if user has permissions to delete this repository
	 * Requires view permission.
	 * @param r_name name of repository wanting to be deleted
	 * @return true if allowed
	 */
	function can_delete($r_name)
	{
		return $this->can_view($r_name) && $this->_ci->config->item('global_allow_repo_delete');
	}
	

}