Subversion Repository Creation Script

This command line PHP script allows the user to create subversion repositories on their own server that is running the subversion daemon. The script is currently unfinished.

Back to Portfolio Page

<?php

/*
*    This is a script to create new svn repos
*    Written by Tam Denholm
*
*    Example Usage:
*    sudo php svn.php create <reponame>
*/

class svn {

    private 
$option$repo$user;

    public function 
__construct(){
        
$this->detect_sudo();
        
$this->option $_SERVER['argv'][1];
        
$this->repo $_SERVER['argv'][2];
        
$this->user '<REPLACE USERNAME>';
        
$options = array('create''list');
        if(
in_array($this->option$options)){
            switch(
$this->option){
                case 
'create':
                    echo 
"Creating svn repo $this->repo\n";
                    
$this->create_repo();
                    
$this->add_permission();
                    break;
                case 
'list':
                    echo 
"Current list of repositories.\n";
                    
$this->dir('/srv/svn');
                    break;
            }
        }else{
            echo 
'Invalid option selected, pick from '.implode(' '$options)."\n";
        }
    }

    public function 
detect_sudo(){
        
$process_info posix_getpwuid(posix_geteuid());
        if(
$process_info['name'] != 'root'){
            echo 
"Sorry, this needs to be run with sudo!\n";
            die;
        }
    }

    public function 
add_permission(){
        
$file file_get_contents('/etc/subversion/svnpolicy');
        
$new "\n[".$this->repo.":/]\n".$this->user." = rw\n";
        
$file file_put_contents('/etc/subversion/svnpolicy'$file.$new);
        echo 
"Permission for $this->user user added to svnpolicy file.\n";
        
shell_exec('chown -R www-data:subversion /srv/svn/'.$this->repo);
        echo 
"Set ownership of repository to www-data:subversion\n";
    }

    public function 
create_repo(){
        
shell_exec('sudo svnadmin create /srv/svn/'.$this->repo);
        echo 
"Subversion repository '".$this->repo."' created.\n";
    }

    public function 
dir($dir){
        if (
$handle opendir($dir)) {
            while (
false !== ($file readdir($handle))) {
                if (
$file != "." && $file != "..") {
                    echo 
"$file\n";
                }
            }
            
closedir($handle);
        }
    }

}

new 
svn;

?>