Location: PHPKode > projects > iF.SVNAdmin > svnadmin/ifphplib/IF_SVNAdminC.class.php
<?php
/**
 * iF.SVNAdmin
 * Copyright (c) 2010 by Manuel Freiholz
 * http://www.insanefactory.com/
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; version 2
 * of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.
 */
/**
 * Provides functionality to create repositories with the "svnadmin" binary.
 *
 * @author Manuel Freiholz (Gainwar)
 * @since 08/11/2009
 * @copyright insaneFactory.com
 */
class IF_SVNAdminC
{
  // paths to required binary command line tools.
  private $m_svnadmin = NULL;

  /**
   * Constructor.
   */
  public function __construct()
  {
  }

  /**
   * Initializes the instance with the executable file.
   * 
   * @param string $svnadminExecutable
   * @return bool
   */
  public function init( $svnadminExecutable )
  {
    $this->m_svnadmin = $svnadminExecutable;
    return true;
  }

  /**
   * Creates a new empty repository.
   *
   * @param string $path Absolute path to the new repository
   * @param string $type Repository type: fsfs=file system(default); bdb=berkley db (not recommended)
   * @return bool
   */
  public function create( $path, $type = "fsfs" )
  {
  	// Validate repository name.
  	$pattern = '/^([a-z0-9\_\-]+)$/i';
  	$repo_name = basename( $path );
  	if( !preg_match( $pattern, $repo_name ) )
  	{ // Invalid repository name.
  		return false;
  	}

    // The command line.
    $cmd = escapeshellarg($this->m_svnadmin)." create --fs-type ".$type." ".escapeshellarg($path);

    $output = array();
    $exitCode = 0;
    exec( $cmd, $output, $exitCode );

    if( $exitCode == 0 )
      return true;
    else
      // Some error occured, but we don't know what really happens.
      return false;
  }

  /**
   * Deletes the repository at the given path.
   * 
   * @param string $path Path to the repository.
   * @return bool
   */
  public function delete( $path )
  {
    $files = glob( $path."/*"/*, GLOB_MARK*/ ); // GLOB_MARK = Adds a ending slash to directory paths.
    foreach( $files as $f )
    {
      if( is_dir( $f ) )
      {
        self::delete( $f );
      }
      else
      {
        // Change the file permissions before deleting.
        chmod( $f, 0777 );
        unlink( $f );
      }
    }

    if( is_dir( $path ) )
      rmdir( $path );
    
    return true;
  }

  /**
   * Checks whether the folder at the given location is a repository.
   *
   * @param string $path
   * @return bool
   */
  public function isRepository( $path )
  {
    // TODO: Built in complete check.
    return is_dir( $path );
  }

  /**
   * Gets a list with all available repositories in the given base path.
   *
   * @param $basePath The SVNParentPath of the repositories.
   * @return array or FALSE if any error occured.
   */
  public function listRepositories( $basePath )
  {
    if( !file_exists($basePath) )
    {
      $this->m_errno=103;
      return false;
    }

    $ret = array();

    $hd = opendir( $basePath );
    while( false !== ($file = readdir($hd)) )
    {
    	if( $file == "." || $file == ".." )
    	{
    	  continue;
    	}

      $complete_path = $basePath . "/" . $file;
      if( is_dir( $complete_path ) )
      {
      	array_push( $ret, $file );
      }
    }
    closedir($hd);
    return $ret;
  }
}
?>
Return current item: iF.SVNAdmin