register_action('addrepo');
			$this->register_hook('header');
			$this->register_hook('page_start');
			$this->register_hook('summary');
		}
	}
	function action($action) {
		if ($action === 'addrepo') {
			global $page;
			global $conf;
			if (isset($_POST['repo_name']) && isset($_POST['repo_desc']) && isset($_POST['repo_pwd'])) {
				$create=True;
				$name=rtrim($_POST['repo_name']);
				if (!$this -> validate_repo_name($name)) {
					$this -> add_display_message('Invalid name of repository. Must only contain letters, numbers or caraters "_" or "-". ',True);
					$create=False;
				}
				$desc=rtrim($_POST['repo_desc']);
				if (empty($desc)) {
					$this -> add_display_message('Repository description is required.',True);
					$create=False;
				}
				if (!isset($conf['addrepo']['password']) || empty($conf['addrepo']['password'])) {
					$this -> add_display_message('Password not defined in configuration. Creation disable.',True);
					$create=False;
				}
				else {
					$pwd=rtrim($_POST['repo_pwd']);
					if ($pwd!=$conf['addrepo']['password']) {
						$this -> add_display_message('Password incorrect',True);
						$create=False;
					}
				}
				if ($create && $this -> create_repo($name,$desc)) {
					$this -> add_display_message("Repository $name successfully created !
To access to your new repository, click here.");
				}
				else {
					$page['addrepo']['values']['name']=$name;
					$page['addrepo']['values']['desc']=$desc;
				}
			}
			$this->display_plugin_template('addrepo');
		}
	}
	function hook($type) {
		if ($type == 'header') {
			echo "\t\n";
		}
		elseif ($type == 'page_start') {
			$this->display_plugin_template('start',False);
		}
		elseif ($type == 'summary') {
			global $page;
			global $conf;
			$infos=get_project_info($page['project']);
			if (empty($infos['head_hash'])) {
				if (isset($conf['addrepo']['url_format'])) {
					$page['addrepo']['URL']=sprintf($conf['addrepo']['url_format'],$page['project']);
				}
				else {
					$page['addrepo']['URL']='[URL]';
				}
				$this->display_plugin_template('summary',False);
			}
		}
	}
        /**
         * Display the given template.
         * Fixe global variables inclusion
         */
        function display_template($template, $with_headers = true) {
                global $conf;
                global $page;
                if ($with_headers) {
                        require 'templates/header.php';
                }
                require "$template";
                if ($with_headers) {
                        require 'templates/footer.php';
                }
        }
	function add_display_message($msg,$error=false) {
		global $page;
		if ($error) {
			$page['addrepo']['error'][]=$msg;
		}
		else {
			$page['addrepo']['msg'][]=$msg;
		}
	}
	function validate_repo_name($name) {
		if (preg_match('/^[a-zA-Z0-9\-\_]*$/',$name)) {
			return True;
		}
		return False;
	}
	function create_repo($name,$desc) {
		global $conf;
		if (!isset($conf['addrepo']['root_path'])) {
			$this -> add_display_message('Root path not configured !',True);
			return;
		}
		elseif(!is_dir($conf['addrepo']['root_path'])) {
			 $this -> add_display_message('Root directory not found. Check your configuration !',True);
			 return;
		}
		$path=$conf['addrepo']['root_path'].'/'.$name;
		if (is_dir($path)) {
			$this -> add_display_message("Repository $name still exist !",True);
			return;
		}
		if (!mkdir($path)) {
			$this -> add_display_message("Fail to create repository's directory. Check permission.",True);
			return;
		}
		global $conf;
		$conf['project'][$name]=array(
			'repo'  => $path
		);
		$cmd = $conf['git'] ." --git-dir=$path init";
		exec($cmd,$out,$ret);
		if ($ret !== 0) {
			$this -> add_display_message("Error initializing repository !");
			return;
		}
		try {
			$fd = fopen($path.'/description','w');
			fwrite($fd,$desc);
			fclose($fd);
		}
		catch (Exception $e) {
			 $this -> add_display_message("Error setting repository's description : ".$e->getMessage());
			 return;
		}
		$cmd = "chmod g+rw -R $path";
		exec($cmd,$out,$ret);
		if ($ret !== 0) {
			$this -> add_display_message("Error fixing repository permission. You may not be able to push in this repository.");
		}
		return True;
	}
}