Location: PHPKode > projects > iF.SVNAdmin > svnadmin/settings.php
<?php
include("include/config.inc.php");

/**
 * The configuration of the web application.
 * @global $cfgEngine IF_IniFile
 */
$cfgEngine = $appEngine->getConfig();

/**
 * Indicates whether the "setadmin" page should be shown.
 * @global $show_setadmin bool
 */
$show_setadmin = false;

/**
 * Indicates whether an user with the administrator role exists.
 * @global $hasAdminDefined bool
 */
$hasAdminDefined = true;
if ($appEngine->isAclManagerActive())
  $hasAdminDefined = $appEngine->getAclManager()->hasAdminDefined();


/**
 * Indicates whether it is the first start of the application.
 * @global $isFirstStart bool
 */
$isFirstStart = ($cfgEngine->getValue("Common", "FirstStart") == "1") ? true : false;

$skip_auth = false;
if (!$isFirstStart && !$hasAdminDefined)
{
  $skip_auth = true;
  $show_setadmin = true;
}

if (!$hasAdminDefined || $isFirstStart)
{
  $skip_auth = true;
}

// Check auth?
if (!$skip_auth)
  $appEngine->checkUserAuthentication(true, ACL_MOD_SETTINGS, ACL_ACTION_CHANGE);
$appTR->loadModule("settings");

////////////////////////////////////////////////////////////////////////////////
// Fetch request parameters.
////////////////////////////////////////////////////////////////////////////////
$pUserViewProviderType = get_request_var("UserViewProviderType");
$pUserEditProviderType = get_request_var("UserEditProviderType");
$pGroupViewProviderType = get_request_var("GroupViewProviderType");
$pGroupEditProviderType = get_request_var("GroupEditProviderType");
$pRepositoryViewProviderType = get_request_var("RepositoryViewProviderType");
$pRepositoryEditProviderType = get_request_var("RepositoryEditProviderType");
$pSVNAuthFile = get_request_var("SVNAuthFile");
$pSVNUserFile = get_request_var("SVNUserFile");
$pSVNParentPath = get_request_var("SVNParentPath");
$pSvnExecutable = get_request_var("SvnExecutable");
$pSvnAdminExecutable = get_request_var("SvnAdminExecutable");
$pLdapHostAddress = get_request_var("LdapHostAddress");
$pLdapProtocolVersion = get_request_var("LdapProtocolVersion");
$pLdapBindDN = get_request_var("LdapBindDN");
$pLdapBindPassword = get_request_var("LdapBindPassword");
$pLdapUserBaseDn = get_request_var("LdapUserBaseDn");
$pLdapUserSearchFilter = get_request_var("LdapUserSearchFilter");
$pLdapUserAttributes = get_request_var("LdapUserAttributes");
$pLdapGroupBaseDn = get_request_var("LdapGroupBaseDn");
$pLdapGroupSearchFilter = get_request_var("LdapGroupSearchFilter");
$pLdapGroupAttributes = get_request_var("LdapGroupAttributes");
$pLdapGroupsToUserAttribute = get_request_var("LdapGroupsToUserAttribute");
$pLdapGroupsToUserAttributeValue = get_request_var("LdapGroupsToUserAttributeValue");

////////////////////////////////////////////////////////////////////////////////
// Reset first start up value.
////////////////////////////////////////////////////////////////////////////////
if (check_request_var("firststart"))
{
  $cfgEngine->setValue("Common", "FirstStart", 1);
  $cfgEngine->saveToFile();

  header("Location: settings.php");
  exit(0);
}

////////////////////////////////////////////////////////////////////////////////
// Save values.
////////////////////////////////////////////////////////////////////////////////
if (check_request_var("save"))
{
  $cfgEngine->setValue("Engine:Providers", "UserViewProviderType", $pUserViewProviderType);
  $cfgEngine->setValue("Engine:Providers", "UserEditProviderType", $pUserEditProviderType);
  $cfgEngine->setValue("Engine:Providers", "GroupViewProviderType", $pGroupViewProviderType);
  $cfgEngine->setValue("Engine:Providers", "GroupEditProviderType", $pGroupEditProviderType);
  $cfgEngine->setValue("Engine:Providers", "RepositoryViewProviderType", $pRepositoryViewProviderType);
  $cfgEngine->setValue("Engine:Providers", "RepositoryEditProviderType", $pRepositoryEditProviderType);
  $cfgEngine->setValue("Subversion", "SVNAuthFile", $pSVNAuthFile);
  $cfgEngine->setValue("Users:passwd", "SVNUserFile", $pSVNUserFile);
  $cfgEngine->setValue("Repositories:svnclient", "SVNParentPath", $pSVNParentPath);
  $cfgEngine->setValue("Repositories:svnclient", "SvnExecutable", $pSvnExecutable);
  $cfgEngine->setValue("Repositories:svnclient", "SvnAdminExecutable", $pSvnAdminExecutable);
  $cfgEngine->setValue("Ldap", "HostAddress", $pLdapHostAddress);
  $cfgEngine->setValue("Ldap", "ProtocolVersion", $pLdapProtocolVersion);
  $cfgEngine->setValue("Ldap", "BindDN", $pLdapBindDN);
  $cfgEngine->setValue("Ldap", "BindPassword", $pLdapBindPassword);
  $cfgEngine->setValue("Users:ldap", "BaseDN", $pLdapUserBaseDn);
  $cfgEngine->setValue("Users:ldap", "SearchFilter", $pLdapUserSearchFilter);
  $cfgEngine->setValue("Users:ldap", "Attributes", $pLdapUserAttributes);
  $cfgEngine->setValue("Groups:ldap", "BaseDN", $pLdapGroupBaseDn);
  $cfgEngine->setValue("Groups:ldap", "SearchFilter", $pLdapGroupSearchFilter);
  $cfgEngine->setValue("Groups:ldap", "Attributes", $pLdapGroupAttributes);
  $cfgEngine->setValue("Groups:ldap", "GroupsToUserAttribute", $pLdapGroupsToUserAttribute);
  $cfgEngine->setValue("Groups:ldap", "GroupsToUserAttributeValue", $pLdapGroupsToUserAttributeValue);
  $cfgEngine->setValue("Common", "FirstStart", 0);

  if ($cfgEngine->saveToFile())
  {
    if (!$hasAdminDefined)
    {
      header("Location: settings.php");
      exit(0);
    }
    $appTemplate->addDefine("INFO");
    $appTemplate->addReplacement("INFOMSG", $appTR->tr("Done."));
  }
  else
  {
    $appTemplate->addDefine("ERROR");
    $appTemplate->addReplacement("ERRORMSG", $appTR->tr("Could not save configuration. You should check the file permissions."));
  }
}

////////////////////////////////////////////////////////////////////////////////
// Do the tests. (Comes in with AJAX)
////////////////////////////////////////////////////////////////////////////////
if (check_request_var("dotest") && check_request_var("dotestsec"))
{
  @error_reporting(~E_ALL);
  $msgOk = NULL;
  $msgErr = NULL;
  $testSection = get_request_var("dotestsec");
  switch($testSection)
  {
  case "SVNAuthFile":
    if (file_exists($pSVNAuthFile))
      if (is_writable($pSVNAuthFile))
        $msgOk = $appTR->tr("Test passed.");
      else
        $msgErr = $appTR->tr("The file exists but is not writable.");
    else
      $msgErr = $appTR->tr("The file does not exist.");
    break;

  case "SVNUserFile":
    if (file_exists($pSVNUserFile))
      if (is_writable($pSVNUserFile))
        $msgOk = $appTR->tr("Test passed.");
      else
        $msgErr = $appTR->tr("The file exists but is not writable.");
    else
      $msgErr = $appTR->tr("The file does not exist.");
    break;

  case "SVNParentPath":
    if (file_exists($pSVNParentPath))
      if (is_writable($pSVNParentPath))
        $msgOk = $appTR->tr("Test passed.");
      else
        $msgErr = $appTR->tr("The folder exists but is not writable.");
    else
      $msgErr = $appTR->tr("The folder does not exist.");
    break;

  case "SvnExecutable":
    if (file_exists($pSvnExecutable))
      if (is_executable($pSvnExecutable))
        $msgOk = $appTR->tr("Test passed.");
      else
        $msgErr = $appTR->tr("The file exists but is not executable.");
    else
      $msgErr = $appTR->tr("The file does not exist.");
    break;

  case "SvnAdminExecutable":
    if (file_exists($pSvnAdminExecutable))
      if (is_executable($pSvnAdminExecutable))
        $msgOk = $appTR->tr("Test passed.");
      else
        $msgErr = $appTR->tr("The file exists but is not executable.");
    else
      $msgErr = $appTR->tr("The file does not exist.");
    break;

  case "LdapConnection":
    $connector = new IF_AbstractLdapConnector();
    $connector->ldapVersion = $pLdapProtocolVersion;
    if ($connector->connect($pLdapHostAddress)){
      if ($connector->bind($pLdapBindDN, $pLdapBindPassword))
        $msgOk = $appTR->tr("Test passed.");
      else
        $msgErr = $appTR->tr("Connection failed:")." ".ldap_error($connector->connection);
    }
    else
      $msgErr = $appTR->tr("Connection failed:")." ".ldap_error($connector->connection);
    break;

  case "LdapUser":
    include_once("./classes/providers/ldap/LdapUserViewProvider.class.php");
    $up = new \svnadmin\providers\ldap\LdapUserViewProvider();
    $up->host_address = $pLdapHostAddress;
    $up->host_protocol_version = $pLdapProtocolVersion;
    $up->bind_dn = $pLdapBindDN;
    $up->bind_password = $pLdapBindPassword;
    $up->users_base_dn = $pLdapUserBaseDn;
    $up->users_search_filter = $pLdapUserSearchFilter;
    $up->users_attributes = explode(",", $pLdapUserAttributes);
    if ($up->init() === true) {
      $users = $up->getUsers();
      $usersCount = count($users);

      $t = "Found ".$usersCount." users.&lt;br&gt;";
      $doComma = false;
      for ($i=0; $i<$usersCount; $i++){
        if ($doComma)
          $t.= ", ";
        $doComma = true;
        $t.= $users[$i]->name;
      }
      $msgOk = $t;
    }
    else
      $msgErr = $appTR->tr("Connection failed:")." ".ldap_error($up->connection);
    break;

  case "LdapGroup":
    include_once("./classes/providers/ldap/LdapUserViewProvider.class.php");
    $up = new \svnadmin\providers\ldap\LdapUserViewProvider();
    $up->host_address = $pLdapHostAddress;
    $up->host_protocol_version = $pLdapProtocolVersion;
    $up->bind_dn = $pLdapBindDN;
    $up->bind_password = $pLdapBindPassword;
    $up->users_base_dn = $pLdapUserBaseDn;
    $up->users_search_filter = $pLdapUserSearchFilter;
    $up->users_attributes = explode(",", $pLdapUserAttributes);
    $up->groups_base_dn = $pLdapGroupBaseDn;
    $up->groups_search_filter = $pLdapGroupSearchFilter;
    $up->groups_attributes = explode(",", $pLdapGroupAttributes);
    $up->groups_to_users_attribute = $pLdapGroupsToUserAttribute;
    $up->groups_to_users_attribute_value = $pLdapGroupsToUserAttributeValue;
    if ($up->init() === true) {
      $groups = $up->getGroups();
      $groupsCount = count($groups);

      $t = "Found ".$groupsCount." groups.&lt;br&gt;";
      $doComma = false;
      for ($i=0; $i<$groupsCount; $i++){
        if ($doComma)
          $t.= ", ";
        $doComma = true;
        $t.= $groups[$i]->name;
      }
      $msgOk = $t;
    }
    else
      $msgErr = $appTR->tr("Connection failed:")." ".ldap_error($up->connection);
    break;

  default:
    $msgErr = "Invalid request.";
  }

  // Write result XML.
  header("Content-type: text/xml");
  $xml = "<testresult>\n";
  $xml.= "<passed>".(empty($msgOk) ? 0 : 1)."</passed>\n";
  $xml.= "<message>".(empty($msgOk) ? $msgErr : $msgOk)."</message>\n";
  $xml.= "</testresult>";
  echo $xml;
  exit(0);
}
////////////////////////////////////////////////////////////////////////////////
// Set administrator.
////////////////////////////////////////////////////////////////////////////////
if (check_request_var("setadmin") || $show_setadmin)
{
  // Assign the Administrator role.
  if (check_request_var("saveadmin"))
  {
    $selusers = get_request_var("selected_users");

    $oU = new \svnadmin\core\entities\User;
    $oU->name = $selusers[0];

    $oR = new \svnadmin\core\entities\Role;
    $oR->name = "Administrator";

    if ($appEngine->getAclManager()->assignUserToRole($oU, $oR))
    {
      $appEngine->getAclManager()->save();
      $appTemplate->addDefine("OK");
      $appTemplate->addReplacement("MESSAGE_OK", $appTR->tr("The user has been defined as admin. You can <a href=\"login.php\">login</a> now."));
      header("Location: index.php");
      exit(0);
    }
    else
    {
      $appTemplate->addDefine("ERROR");
      $appTemplate->addReplacement("MESSAGE_ERROR", $appTR->tr("An unknown error occured. Check your configuration, please."));
    }
  }

  // Display users, which can be defined as Administrator.
  if ($appEngine->isUserViewActive())
  {
    $users = $appEngine->getUserViewProvider()->getUsers(false);
    $usersCount = count($users);

    // If there are no users, we create one - if possible.
    if (empty($users))
    {
      if ($appEngine->isUserEditActive())
      {
        $u = new \svnadmin\core\entities\User();
        $u->name = "admin";
        $u->password = "admin";
        $appEngine->getUserEditProvider()->addUser($u);
        $appEngine->getUserEditProvider()->save();

        $r = new \svnadmin\core\entities\Role;
        $r->name = "Administrator";
        $appEngine->getAclManager()->assignUserToRole($u, $r);
        $appEngine->getAclManager()->save();

        $appTemplate->addDefine("DEFAULT_USER_CREATED");
      }
      else
      {
        // Display message. That no user edit provider is defined to create a
        // default admin user.
        $appTemplate->addDefine("SHOW_NO_USER_EDIT_ACTIVE");
      }
    }
    // Display user selection.
    else
    {
      usort($users, array("\svnadmin\core\entities\User","compare"));
      $appTemplate->addReplacement("users", $users);
      $appTemplate->addDefine("SHOW_USER_SELECTION");
    }
  }
  else
  {
    $appTemplate->addDefine("ERROR");
    $appTemplate->addReplacement(tr("You have to define a user view provider."));
  }

  $appTemplate->loadFromFile( new \IF_File("templates/setadmin.html") );
  $appTemplate->processTemplate();
  exit(0);
}
////////////////////////////////////////////////////////////////////////////////
// Form values.
////////////////////////////////////////////////////////////////////////////////

// Load template configuration. (Read only!)
$cfgTpl = new \IF_IniFile();
$cfgTpl->loadFromFile("data/config.tpl.ini");

// SVNAuthFile
$svnAuthFile = $cfgEngine->getValue("Subversion","SVNAuthFile");
$svnAuthFileEx = $cfgTpl->getValue("Subversion","SVNAuthFile");
$appTemplate->addReplacement("SVNAuthFile", $svnAuthFile);
$appTemplate->addReplacement("SVNAuthFileEx", $svnAuthFileEx);

// UserViewProviderType
$userViewProviderTypes = array(/*"off",*/ "passwd", "ldap");
array_unshift($userViewProviderTypes, $cfgEngine->getValue("Engine:Providers","UserViewProviderType"));
$appTemplate->addReplacement("userViewProviderTypes", $userViewProviderTypes);

// UserEditProviderType
$userEditProviderTypes = array("off", "passwd");
array_unshift($userEditProviderTypes, $cfgEngine->getValue("Engine:Providers","UserEditProviderType"));
$appTemplate->addReplacement("userEditProviderTypes", $userEditProviderTypes);

// GroupViewProviderType
$groupViewProviderTypes = array("off", "svnauthfile", "ldap");
array_unshift($groupViewProviderTypes, $cfgEngine->getValue("Engine:Providers","GroupViewProviderType"));
$appTemplate->addReplacement("groupViewProviderTypes", $groupViewProviderTypes);

// GroupEditProviderType
$groupEditProviderTypes = array("off", "svnauthfile");
array_unshift($groupEditProviderTypes, $cfgEngine->getValue("Engine:Providers","GroupEditProviderType"));
$appTemplate->addReplacement("groupEditProviderTypes", $groupEditProviderTypes);

// RepositoryViewProviderType
$repositoryViewProviderTypes = array("off", "svnclient");
array_unshift($repositoryViewProviderTypes, $cfgEngine->getValue("Engine:Providers","RepositoryViewProviderType"));
$appTemplate->addReplacement("repositoryViewProviderTypes", $repositoryViewProviderTypes);

// RepositoryEditProviderType
$repositoryEditProviderTypes = array("off", "svnclient");
array_unshift($repositoryEditProviderTypes, $cfgEngine->getValue("Engine:Providers","RepositoryEditProviderType"));
$appTemplate->addReplacement("repositoryEditProviderTypes", $repositoryEditProviderTypes);

// Passwd file.
$svnUserFile = $cfgEngine->getValue("Users:passwd","SVNUserFile");
$svnUserFileEx = $cfgTpl->getValue("Users:passwd","SVNUserFile");
$appTemplate->addReplacement("SVNUserFile", $svnUserFile);
$appTemplate->addReplacement("SVNUserFileEx", $svnUserFileEx);

// Repositories:svnclient
$svnParentPath = $cfgEngine->getValue("Repositories:svnclient","SVNParentPath");
$svnParentPathEx = $cfgTpl->getValue("Repositories:svnclient","SVNParentPath");
$svnExecutable = $cfgEngine->getValue("Repositories:svnclient","SvnExecutable");
$svnExecutableEx = $cfgTpl->getValue("Repositories:svnclient","SvnExecutable");
$svnAdminExecutable = $cfgEngine->getValue("Repositories:svnclient","SvnAdminExecutable");
$svnAdminExecutableEx = $cfgTpl->getValue("Repositories:svnclient","SvnAdminExecutable");
$appTemplate->addReplacement("SVNParentPath", $svnParentPath);
$appTemplate->addReplacement("SVNParentPathEx", $svnParentPathEx);
$appTemplate->addReplacement("SvnExecutable", $svnExecutable);
$appTemplate->addReplacement("SvnExecutableEx", $svnExecutableEx);
$appTemplate->addReplacement("SvnAdminExecutable", $svnAdminExecutable);
$appTemplate->addReplacement("SvnAdminExecutableEx", $svnAdminExecutableEx);

// LDAP connection.
$ldapHostAddress = $cfgEngine->getValue("Ldap","HostAddress");
$ldapHostAddressEx = $cfgTpl->getValue("Ldap","HostAddress");
$ldapProtocolVersion = $cfgEngine->getValue("Ldap","ProtocolVersion");
$ldapProtocolVersionEx = $cfgTpl->getValue("Ldap","ProtocolVersion");
$ldapBindDN = $cfgEngine->getValue("Ldap","BindDN");
$ldapBindDNEx = $cfgTpl->getValue("Ldap","BindDN");
$ldapBindPassword = $cfgEngine->getValue("Ldap","BindPassword");
$ldapBindPasswordEx = $cfgTpl->getValue("Ldap","BindPassword");
$appTemplate->addReplacement("LdapHostAddress", $ldapHostAddress);
$appTemplate->addReplacement("LdapHostAddressEx", $ldapHostAddressEx);
$appTemplate->addReplacement("LdapProtocolVersion", $ldapProtocolVersion);
$appTemplate->addReplacement("LdapProtocolVersionEx", $ldapProtocolVersionEx);
$appTemplate->addReplacement("LdapBindDN", $ldapBindDN);
$appTemplate->addReplacement("LdapBindDNEx", $ldapBindDNEx);
$appTemplate->addReplacement("LdapBindPassword", $ldapBindPassword);
$appTemplate->addReplacement("LdapBindPasswordEx", $ldapBindPasswordEx);

// LDAP user provider information.
$ldapUserBaseDn = $cfgEngine->getValue("Users:ldap","BaseDN");
$ldapUserBaseDnEx = $cfgTpl->getValue("Users:ldap","BaseDN");
$ldapUserSearchFilter = $cfgEngine->getValue("Users:ldap","SearchFilter");
$ldapUserSearchFilterEx = $cfgTpl->getValue("Users:ldap","SearchFilter");
$ldapUserAttributes = $cfgEngine->getValue("Users:ldap","Attributes");
$ldapUserAttributesEx = $cfgTpl->getValue("Users:ldap","Attributes");
$appTemplate->addReplacement("LdapUserBaseDn", $ldapUserBaseDn);
$appTemplate->addReplacement("LdapUserBaseDnEx", $ldapUserBaseDnEx);
$appTemplate->addReplacement("LdapUserSearchFilter", $ldapUserSearchFilter);
$appTemplate->addReplacement("LdapUserSearchFilterEx", $ldapUserSearchFilterEx);
$appTemplate->addReplacement("LdapUserAttributes", $ldapUserAttributes);
$appTemplate->addReplacement("LdapUserAttributesEx", $ldapUserAttributesEx);

// LDAP group provider information.
$ldapGroupBaseDn = $cfgEngine->getValue("Groups:ldap","BaseDN");
$ldapGroupBaseDnEx = $cfgTpl->getValue("Groups:ldap","BaseDN");
$ldapGroupSearchFilter = $cfgEngine->getValue("Groups:ldap","SearchFilter");
$ldapGroupSearchFilterEx = $cfgTpl->getValue("Groups:ldap","SearchFilter");
$ldapGroupAttributes = $cfgEngine->getValue("Groups:ldap","Attributes");
$ldapGroupAttributesEx = $cfgTpl->getValue("Groups:ldap","Attributes");
$ldapGroupsToUserAttribute = $cfgEngine->getValue("Groups:ldap","GroupsToUserAttribute");
$ldapGroupsToUserAttributeEx = $cfgTpl->getValue("Groups:ldap","GroupsToUserAttribute");
$ldapGroupsToUserAttributeValue = $cfgEngine->getValue("Groups:ldap","GroupsToUserAttributeValue");
$ldapGroupsToUserAttributeValueEx = $cfgTpl->getValue("Groups:ldap","GroupsToUserAttributeValue");
$appTemplate->addReplacement("LdapGroupBaseDn", $ldapGroupBaseDn);
$appTemplate->addReplacement("LdapGroupBaseDnEx", $ldapGroupBaseDnEx);
$appTemplate->addReplacement("LdapGroupSearchFilter", $ldapGroupSearchFilter);
$appTemplate->addReplacement("LdapGroupSearchFilterEx", $ldapGroupSearchFilterEx);
$appTemplate->addReplacement("LdapGroupAttributes", $ldapGroupAttributes);
$appTemplate->addReplacement("LdapGroupAttributesEx", $ldapGroupAttributesEx);
$appTemplate->addReplacement("LdapGroupsToUserAttribute", $ldapGroupsToUserAttribute);
$appTemplate->addReplacement("LdapGroupsToUserAttributeEx", $ldapGroupsToUserAttributeEx);
$appTemplate->addReplacement("LdapGroupsToUserAttributeValue", $ldapGroupsToUserAttributeValue);
$appTemplate->addReplacement("LdapGroupsToUserAttributeValueEx", $ldapGroupsToUserAttributeValueEx);

// Process template.
$appTemplate->loadFromFile( new \IF_File("templates/settings.html") );
$appTemplate->processTemplate();
?>
Return current item: iF.SVNAdmin