Skip to content
This repository has been archived by the owner on Nov 25, 2020. It is now read-only.

Commit

Permalink
Cmd.php: allow anonymous call to automatize deployments (before any u…
Browse files Browse the repository at this point in the history
…ser is created)
  • Loading branch information
cdujeu committed Jul 15, 2013
1 parent 6f9b1de commit a911d10
Showing 1 changed file with 92 additions and 90 deletions.
182 changes: 92 additions & 90 deletions core/src/cmd.php
Expand Up @@ -21,7 +21,7 @@
* Description : Command line access of the framework.
*/
if(php_sapi_name() !== "cli"){
die("This is the command line version of the framework, you are not allowed to access this page");
die("This is the command line version of the framework, you are not allowed to access this page");
}

include_once("base.conf.php");
Expand Down Expand Up @@ -51,47 +51,49 @@
$regex = '/^-(-?)([a-zA-z0-9_]*)=(.*)/';
foreach ($argv as $key => $argument){
//echo("$key => $argument \n");
if(preg_match($regex, $argument, $matches)){
if($matches[1] == "-"){
$optArgs[trim($matches[2])] = SystemTextEncoding::toUTF8(trim($matches[3]));
}else{
$options[trim($matches[2])] = SystemTextEncoding::toUTF8(trim($matches[3]));
}
}
if(preg_match($regex, $argument, $matches)){
if($matches[1] == "-"){
$optArgs[trim($matches[2])] = SystemTextEncoding::toUTF8(trim($matches[3]));
}else{
$options[trim($matches[2])] = SystemTextEncoding::toUTF8(trim($matches[3]));
}
}
}

$optUser = $options["u"];
if(!empty($optUser)){

if(isSet($options["p"])){
$optPass = $options["p"];
}else{
// Consider "u" is a crypted version of u:p
$optToken = $options["t"];
$iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND);
$optUser = trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, md5($optToken."\1CDAFx¨op#"), base64_decode($optUser), MCRYPT_MODE_ECB, $iv), "\0");
}
if(strpos($optUser,",") !== false){
$originalOptUser = $optUser;
$nextUsers = explode(",", $optUser);
$optUser = array_shift($nextUsers);
$nextUsers = implode(",",$nextUsers);
}else if(strpos($optUser, "queue:") === 0){
$optUserQueue = substr($optUser, strlen("queue:"));
$optUser = false;
//echo("QUEUE : ".$optUserQueue);
if(is_file($optUserQueue)){
$lines = file($optUserQueue);
if(count($lines) && !empty($lines[0])){
$allUsers = explode(",", $lines[0]);
$optUser = array_shift($allUsers);
file_put_contents($optUserQueue, implode(",", $allUsers));
}
if(isSet($options["p"])){
$optPass = $options["p"];
}else{
// Consider "u" is a crypted version of u:p
$optToken = $options["t"];
$iv = mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB), MCRYPT_RAND);
$optUser = trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, md5($optToken."\1CDAFx¨op#"), base64_decode($optUser), MCRYPT_MODE_ECB, $iv), "\0");
}
if($optUser === false){
if(strpos($optUser,",") !== false){
$originalOptUser = $optUser;
$nextUsers = explode(",", $optUser);
$optUser = array_shift($nextUsers);
$nextUsers = implode(",",$nextUsers);
}else if(strpos($optUser, "queue:") === 0){
$optUserQueue = substr($optUser, strlen("queue:"));
$optUser = false;
//echo("QUEUE : ".$optUserQueue);
if(is_file($optUserQueue)){
unlink($optUserQueue);
$lines = file($optUserQueue);
if(count($lines) && !empty($lines[0])){
$allUsers = explode(",", $lines[0]);
$optUser = array_shift($allUsers);
file_put_contents($optUserQueue, implode(",", $allUsers));
}
}
if($optUser === false){
if(is_file($optUserQueue)){
unlink($optUserQueue);
}
die("No more users inside queue");
}
die("No more users inside queue");
}
}

Expand All @@ -111,13 +113,13 @@
$detectedUser = false;

if($optRepoId !== false){
$repository = ConfService::getRepositoryById($optRepoId);
if($repository == null){
$repository = ConfService::getRepositoryByAlias($optRepoId);
if($repository != null){
$optRepoId =($repository->isWriteable()?$repository->getUniqueId():$repository->getId());
}
}
$repository = ConfService::getRepositoryById($optRepoId);
if($repository == null){
$repository = ConfService::getRepositoryByAlias($optRepoId);
if($repository != null){
$optRepoId =($repository->isWriteable()?$repository->getUniqueId():$repository->getId());
}
}
if($optDetectUser != false){
$path = $repository->getOption("PATH", true);
if(strpos($path, "AJXP_USER") !== false){
Expand All @@ -135,64 +137,64 @@
}
}
}
ConfService::switchRootDir($optRepoId, true);
ConfService::switchRootDir($optRepoId, true);
}else{
if($optStatusFile){
file_put_contents($optStatusFile, "ERROR:You must pass a -r argument specifying either a repository id or alias");
}
die("You must pass a -r argument specifying either a repository id or alias");
}

if(AuthService::usersEnabled())
if(AuthService::usersEnabled() && !empty($optUser))
{
$seed = AuthService::generateSeed();
if($seed != -1){
$optPass = md5(md5($optPass).$seed);
}
$loggingResult = AuthService::logUser($optUser, $optPass, isSet($optToken), false, $seed);
// Check that current user can access current repository, try to switch otherwise.
$loggedUser = AuthService::getLoggedUser();
$seed = AuthService::generateSeed();
if($seed != -1){
$optPass = md5(md5($optPass).$seed);
}
$loggingResult = AuthService::logUser($optUser, $optPass, isSet($optToken), false, $seed);
// Check that current user can access current repository, try to switch otherwise.
$loggedUser = AuthService::getLoggedUser();
if($loggedUser != null && $detectedUser !== false && $loggedUser->isAdmin()){
AuthService::disconnect();
AuthService::logUser($detectedUser, "empty", true, false, "");
$loggedUser = AuthService::getLoggedUser();
}

if($loggedUser != null)
{
$currentRepoId = ConfService::getCurrentRepositoryId();
$lastRepoId = $loggedUser->getArrayPref("history", "last_repository");
$defaultRepoId = AuthService::getDefaultRootId();
if($defaultRepoId == -1){
AuthService::disconnect();
$loggingResult = -3;
}else {
if($lastRepoId != "" && $lastRepoId!=$currentRepoId && $optRepoId===false && $loggedUser->canSwitchTo($lastRepoId)){
ConfService::switchRootDir($lastRepoId);
}else if(!$loggedUser->canSwitchTo($currentRepoId)){
ConfService::switchRootDir($defaultRepoId);
}
}
}
if($loggedUser == null)
{
// Try prelogging user if the session expired but the logging data is in fact still present
// For example, for basic_http auth.
AuthService::preLogUser((isSet($httpVars["remote_session"])?$httpVars["remote_session"]:""));
$loggedUser = AuthService::getLoggedUser();
if($loggedUser == null) $requireAuth = true;
}
if(isset($loggingResult) && $loggingResult != 1)
{
AJXP_XMLWriter::header();
AJXP_XMLWriter::loggingResult($loggingResult, false, false, "");
AJXP_XMLWriter::close();
if($loggedUser != null)
{
$currentRepoId = ConfService::getCurrentRepositoryId();
$lastRepoId = $loggedUser->getArrayPref("history", "last_repository");
$defaultRepoId = AuthService::getDefaultRootId();
if($defaultRepoId == -1){
AuthService::disconnect();
$loggingResult = -3;
}else {
if($lastRepoId != "" && $lastRepoId!=$currentRepoId && $optRepoId===false && $loggedUser->canSwitchTo($lastRepoId)){
ConfService::switchRootDir($lastRepoId);
}else if(!$loggedUser->canSwitchTo($currentRepoId)){
ConfService::switchRootDir($defaultRepoId);
}
}
}
if($loggedUser == null)
{
// Try prelogging user if the session expired but the logging data is in fact still present
// For example, for basic_http auth.
AuthService::preLogUser((isSet($httpVars["remote_session"])?$httpVars["remote_session"]:""));
$loggedUser = AuthService::getLoggedUser();
if($loggedUser == null) $requireAuth = true;
}
if(isset($loggingResult) && $loggingResult != 1)
{
AJXP_XMLWriter::header();
AJXP_XMLWriter::loggingResult($loggingResult, false, false, "");
AJXP_XMLWriter::close();
if($optStatusFile){
file_put_contents($optStatusFile, "ERROR:No user logged");
}
}
}
}else{
AJXP_Logger::debug(ConfService::getCurrentRepositoryId());
AJXP_Logger::debug(ConfService::getCurrentRepositoryId());
}

//Set language
Expand All @@ -206,20 +208,20 @@
$authDriver = ConfService::getAuthDriverImpl();
// DRIVERS BELOW NEED IDENTIFICATION CHECK
if(!AuthService::usersEnabled() || ConfService::getCoreConf("ALLOW_GUEST_BROWSING", "auth") || AuthService::getLoggedUser()!=null){
$confDriver = ConfService::getConfStorageImpl();
$Driver = ConfService::loadRepositoryDriver();
$confDriver = ConfService::getConfStorageImpl();
$Driver = ConfService::loadRepositoryDriver();
}
AJXP_PluginsService::getInstance()->initActivePlugins();
require_once(AJXP_BIN_FOLDER."/class.AJXP_Controller.php");
$xmlResult = AJXP_Controller::findActionAndApply($optAction, $optArgs, array());
if($xmlResult !== false && $xmlResult != ""){
AJXP_XMLWriter::header();
print($xmlResult);
AJXP_XMLWriter::close();
AJXP_XMLWriter::header();
print($xmlResult);
AJXP_XMLWriter::close();
}else if(isset($requireAuth) && AJXP_Controller::$lastActionNeedsAuth){
AJXP_XMLWriter::header();
AJXP_XMLWriter::requireAuth();
AJXP_XMLWriter::close();
AJXP_XMLWriter::header();
AJXP_XMLWriter::requireAuth();
AJXP_XMLWriter::close();
}
//echo("NEXT REPO ".$nextRepositories." (".$options["r"].")\n");
//echo("NEXT USERS ".$nextUsers." ( ".$originalOptUser." )\n");
Expand Down

0 comments on commit a911d10

Please sign in to comment.