Some very old PHP code originally intended to become an image management tool.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

504 lines
13 KiB

<?
//-------------------------------------------
//
// ocs_user_add - last update 2006-10-23
//
// based on:
//
// Adressupdate - last update wgi 2006-05-23
//
//-------------------------------------------
header("Content-Type: text/html; charset=UTF-8");
$basepath="/data/www/localhost/htdocs/inc";
include "$basepath/mkhead.php";
function load_users ()
{
$localFile = "/var/www/localhost/htdocs/osc_user_add/xfer/web-useradd";
exec ("scp webuseradd@vam-osc5:/etc/web-useradd ".$localFile." 2>&1",
$out, $ret);
if ($ret != 0)
{
print "scp returns: ".$ret."<BR>\n";
print "Output was:<BR><PRE>\n";
print_r ($out);
print "</PRE>\n";
}
else
{
$fh = fopen ($localFile, "r");
$line = fgets ($fh);
while (! feof ($fh))
{
$buf = explode (":", trim ($line));
if ($buf[1] == "")
$buf[1] = "no-change";
if ($buf[4] != "delete")
$users[$buf[0]] = array ($buf[1], $buf[2], $buf[3], $buf[4]);
$line = fgets ($fh);
}
fclose ($fh);
}
exec ("scp webuseradd@vam-osc6:/etc/web-useradd ".$localFile." 2>&1",
$out, $ret);
if ($ret != 0)
{
print "scp returns: ".$ret."<BR>\n";
print "Output was:<BR><PRE>\n";
print_r ($out);
print "</PRE>\n";
}
else
{
$fh = fopen ($localFile, "r");
$line = fgets ($fh);
while (! feof ($fh))
{
$buf = explode (":", trim ($line));
if ($buf[1] == "")
if ($users[$buf[0]] && $users[$buf[0]][0] != "")
$buf[1] = $users[$buf[0]][0];
else
$buf[1] = "no-change";
if ($buf[4] != "delete")
$users[$buf[0]] = array ($buf[1], $buf[2], $buf[3], $buf[4]);
$line = fgets ($fh);
}
fclose ($fh);
}
exec ("rm ".$localFile." 2>&1", $out, $ret);
return $users;
}
function load_cats ()
{
/*
* OK, ich hole dir verfügbaren Rubriken anhand der existierenden
* Verzeichniss unter /vol1/www/bhi_upload. Will man also eine
* Rubrik hinzufügen, so muß man dort nur das Verzeichnis anlegen.
*/
exec ("ssh -l webuseradd vam-osc5 ".
"\"ls -p /vol1/www/bhi_upload | sed '/\/$/p;d'\" ".
"2>/dev/null",
$out, $ret);
foreach ($out as $key=>$line)
$out[$key] = preg_replace ("/^(.*)\/$/", "\\1", $line);
return $out;
/* return array ("kfz", "immo", "trauer"); */
}
function update_users ($users)
{
$fh = fopen ("/tmp/web-useradd", "w");
foreach ($users as $key => $value)
{
fputs ($fh, $key.":".$value[0].":".$value[1].":".
$value[2].":".$value[3]."\n");
}
fclose ($fh);
exec ("scp /tmp/web-useradd webuseradd@vam-osc5:/etc/web-useradd 2>&1",
$out, $ret);
if ($ret != 0)
{
print "scp to vam-osc5 returns: ".$ret."<BR>\n";
print "Output was:<BR><PRE>\n";
print_r ($out);
print "</PRE>\n";
}
exec ("scp /tmp/web-useradd webuseradd@vam-osc6:/etc/web-useradd 2>&1",
$out, $ret);
if ($ret != 0)
{
print "scp to vam-osc6 returns: ".$ret."<BR>\n";
print "Output was:<BR><PRE>\n";
print_r ($out);
print "</PRE>\n";
}
exec ("rm /tmp/web-useradd 2>&1", $out, $ret);
}
function show_users ($users)
{
print "<H1>User auf vam-osc5 und vam-osc6</H1>\n";
if ($_SESSION['editable'] == TRUE)
{
print "<A href=\"".$SCRIPT_NAME."?action=add\">";
print "Benutzer hinzufügen</A>";
}
else
{
print "<A href=\"".$SCRIPT_NAME."?action=startedit\">";
print "Benutzer bearbeiten</A>";
}
print "<TABLE border=\"0\">";
$rownum = 0;
if (isset ($users))
{
foreach ($users as $name => $data)
{
if (($rownum % 2) != 0)
{
$bg="#EEEEEE"; // leight
}
else
{
$bg="#CCCCCC"; // dark
}
$tdStyle = "";
$delText = "l&ouml;schen";
if ($data[3] == "delete")
{
$tdStyle = "style=\"text-decoration: line-through;\"";
$delText = "nicht l&ouml;schen";
}
print "<TR><TD width=70 bgcolor=".$bg." ".$tdStyle.">".$name."</TD>";
print "<TD width=150 bgcolor=".$bg." ".$tdStyle.">".$data[1]."</TD>";
print "<TD width=50 bgcolor=".$bg." ".$tdStyle.">".$data[2]."</TD>";
if ($_SESSION['editable'] == TRUE)
{
if ($data[3] != "delete")
{
print "<TD><A href=\"";
print $SCRIPT_NAME."?action=edit&name=".$name."\">";
print "bearbeiten</A></TD>";
print "<TD><A href=\"";
}
else
print "<TD colspan=2><A href=\"";
print $SCRIPT_NAME."?action=delete&name=".$name."\">";
print $delText."</A></TD>";
}
print "</TR>\n";
$rownum ++;
}
}
if ($_SESSION['editable'] == TRUE)
{
print "<TR><TD colspan=\"3\">";
print "<A href=\"".$SCRIPT_NAME."?action=update\">abschicken</A>";
print "&nbsp;<A href=\"".$SCRIPT_NAME."?action=abort\">abbrechen</A>";
print "</TD></TR>";
}
print "</TABLE>\n";
}
function edit_user ($users, $cats, $name)
{
print "<FORM action=\"".$SCRIPT_NAME."?".strip_tags (SID)."\">";
print "<INPUT type=\"HIDDEN\" name=\"action\" value=\"set\">";
print "<INPUT type=\"HIDDEN\" name=\"oldpass\" value=\"".$users[$name][0]."\">";
print "<TABLE border=\"0\">";
print "<TR><TD>Username: </TD>";
print "<TD width=\"300\">";
if (! isset ($name))
{
print "<INPUT type=\"text\" name=\"name\" value=\"".$name."\"></TD>";
}
else
{
print "<INPUT type=\"hidden\" name=\"name\" value=\"".$name."\">";
print $name."</TD>";
}
print "</TR>\n";
print "<TR><TD>Rubrik: </TD>";
print "<TD width=\"300\">";
print "<SELECT name=\"cat\" size=\"1\">";
foreach ($cats as $cat)
{
print "<OPTION";
if ($users[$name][2] == $cat)
print " SELECTED>";
else
print ">";
print $cat."</OPTION>";
}
print "</SELECT>";
print "</TD>";
print "</TR>\n";
print "<TR><TD>Vollst&auml;ndiger Name: </TD>";
print "<TD width=\"300\">";
print "<INPUT type=\"text\" name=\"full\" value=\"".$users[$name][1]."\">";
print "</TD>";
print "</TR>\n";
print "<TR><TD>Passwort: </TD>";
print "<TD width=\"300\">";
print "<INPUT type=\"password\" name=\"pass\" value=\"".$users[$name][0]."\">";
print "</TD>";
print "</TR>\n";
print "<TR><TD colspan=\"2\" align=\"right\">";
print "<A href=\"".$SCRIPT_NAME."?action=show\">abbrechen</A>";
print "&nbsp;<INPUT type=\"submit\" value=\"abschicken\">";
print "</TABLE>\n";
print "</FORM>";
}
/*
* gibt FALSE zurück falls kein Lock gesetzt werden konnte, ansonsten die
* aktuelle lockid. Diese wird bei free_lock benötigt um den lock wieder
* freizugeben.
* Folgende Fälle können eintreten:
* 1. Das Lockfile enthält keine Daten und myId ist leer.
* Das bedeutet: es existiert keine Sperre auf die Daten und wir hatten
* bisher keine Sperre auf die Daten.
* Daraufhin wird eine neue lock-id generiert und zusammen mit dem
* aktuellen Timestamp in das lockfile gegeben. Die generierte lock-id
* wird zurückgegeben.
* 2. Das Lockfile enthält eine Sperre und myId ist leer.
* Das bedeutet: es existiert eine Sperre auf die Daten, die nicht von
* dieser PHP session generiert wurde.
* 2 Faelle:
* (lockTime + 300) >= time (): Die Sperre ist nicht abgelaufen => es
* kann keine neue Sperre angelegt werden.
* (lockTime + 300) < time (): Die Sperre ist abgelaufen => neue lock-id
* generieren, in Datei eintragen und
* zurückgeben.
* 3. Das Lockfile enthält eine Sperre und myId != lockId.
* Das bedeutet: In der aktuellen PHP session wurde erfolgreich eine
* Sperre erzeugt, die aber aufgrund dessen das ein timeout
* eingetreten ist (d.h. in der aktuellen Session wurde länger
* als 5 Minuten auf keinen link mehr geklickt.) nicht mehr
* gültig war und in der zwischenzeit hat jemand anderes
* daher eine neue Sperre auf die Daten eingerichtet siehe 2.
* folgende Fälle:
* die 2 Unterfälle von 2.
* 4. Das Lockfile enthält keine Daten, aber myId ist nicht leer.
* Das bedeutet: In der aktuellen PHP session wurde ein lock eingerichtet.
* Der Timeout verstrich und jemand anderes hat änderungen
* an den Daten vorgenommen. Konsequenz: Die Daten in dieser
* PHP session sind nicht mehr aktuell.
* Da der aktuelle Lock daher komplett ungültig geworden ist wird nichts
* in die Datei eingetragen und eine leere lock-id zurückgegeben.
* An anderer Stelle (check_lock) werden anhand der Information das myId
* nich leer war, aber die zurückgegebene id leer ist alle anderen Daten
* neu initialisiert.
* 5. Das Lockfile enthält eine Sperre und myId == lockId.
* Das bedeutet: Innerhalb dieser PHP session wurde eine Sperre eingerichtet
* die auch weiterhin gültig ist.
* Die lockTime wird in der Datei aktualisiert und lockId zurückgegeben.
*/
function get_lock ($lockFile, $myId)
{
$newId = "";
$lockFd = fopen ($lockFile, "a+");
if ($lockFd != FALSE)
{
if (flock ($lockFd, LOCK_EX) != FALSE)
{
rewind ($lockFd);
$lockEntry = explode ('/', trim (fgets ($lockFd)));
$lockId = $lockEntry[0];
$lockTime = $lockEntry[1];
if ($lockId == $myId ||
/* lockTime nur pruefen wenn Sie in der Datei stand */
($lockTime != "" && ($lockTime + 300) < time ()))
{
ftruncate ($lockFd, 0);
if ($lockId != "" && $lockId == $myId)
$newId = $myId;
else
$newId = uniqid ();
fputs ($lockFd, $newId."/".time ()."\n");
}
fclose ($lockFd);
}
}
return $newId;
}
function free_lock ($lockFile, $lockId)
{
$lockFd = fopen ($lockFile, "r+");
if ($lockFd != FALSE)
{
if (flock ($lockFd, LOCK_EX) != FALSE)
{
$lockEntry = explode ('/', trim (fgets ($lockFd)));
$oldId = $lockEntry[0];
if ($lockId == $oldId)
ftruncate ($lockFd, 0);
fclose ($lockFd);
}
}
}
function check_lock ($lockFile, $lockId)
{
if ($_SESSION['editable'] == TRUE)
{
$gotId = get_lock ($lockFile, $lockId);
if ($gotId == FALSE)
{
if ($lockId == FALSE)
{
print "<TABLE BGCOLOR=\"yellow\"><TR><TD>Konnte keine Sperre zum ";
print "bearbeiten der Benutzerdaten anlegen. Jemand anderes ";
print "bearbeitet die Daten bereits. Bitte versuchen Sie es in ein ";
print "paar Minuten noch einmal.</TD></TR></TABLE>";
}
else
{
print "<TABLE BGCOLOR=\"yellow\"><TR><TD>Ihre Sperre ist ";
print "ung&uuml;ltig. Vermutlich haben Sie l&auml;nger als f&uuml;nf ";
print "Minuten keine Modifikationen vorgenommen und jemand ";
print "anderes bearbeitet jetzt Benutzerdaten. Leider sind ihre ";
print "bisherigen &Auml;nderungen dadurch ung&uuml;ltig geworden. ";
print "Bitte versuchen Sie es in ein paar Minuten noch einmal.";
print "</TD></TR></TABLE>";
}
}
return $gotId;
}
return "";
}
$_action = isset($_REQUEST['action']) ? $_REQUEST['action'] : 'load';
session_start ();
$_SESSION['lockFile'] =
'/var/www/localhost/htdocs/osc_user_add/webuseradd.lck';
mkhead ("Benutzer auf vam-osc5 und vam-osc6 anlegen ...",
"bar-edvtools",
"Benutzertool vam-osc5 und vam-osc6");
if ($_action == "load" || $_action == "startedit")
{
session_unregister ("users");
session_unregister ("cats");
}
if (! isset ($_SESSION['users']))
$_SESSION['users'] = load_users ();
if (! isset ($_SESSION['cats']))
$_SESSION['cats'] = load_cats ();
if (! isset ($_SESSION['editable']))
$_SESSION['editable'] = FALSE;
if ($_action == 'startedit')
$_SESSION['editable'] = TRUE;
$_SESSION['lockId'] = check_lock ($_SESSION['lockFile'], $_SESSION['lockId']);
if ($_SESSION['lockId'] == FALSE)
{
$_SESSION['editable'] = FALSE;
show_users ($_SESSION['users']);
}
else
{
switch ($_action)
{
case 'edit':
edit_user ($_SESSION['users'], $_SESSION['cats'], $_REQUEST['name']);
break;
case 'set':
$pass = $_REQUEST['pass']=="no-change"?$_REQUEST['pass']:
$_REQUEST['oldpass']==$_REQUEST['pass']?$_REQUEST['pass']:
crypt ($_REQUEST['pass'], $_REQUEST['name']);
$_SESSION['users'][$_REQUEST['name']] =
array ($pass,
$_REQUEST['full'],
$_REQUEST['cat'],
time ());
show_users ($_SESSION['users']);
break;
case 'update':
update_users ($_SESSION['users']);
print "<TABLE><TR><TD bgcolor=\"yellow\">Daten wurden aktualisiert. ";
print "Es kann einige Minuten dauern bis das System die ";
print "&Auml;nderung &uuml;bernimmt.</TD></TR></TABLE>\n";
case 'abort':
free_lock ($_SESSION['lockFile'], $_SESSION['lockId']);
$_SESSION['lockId'] = "";
$_SESSION['editable'] = FALSE;
show_users ($_SESSION['users']);
break;
case 'add':
edit_user ($_SESSION['users'], $_SESSION['cats'], $_REQUEST['name']);
break;
case 'delete':
if ($_SESSION['users'][$_REQUEST['name']][3] != "delete")
$_SESSION['users'][$_REQUEST['name']][3] = "delete";
else
$_SESSION['users'][$_REQUEST['name']][3] = time ();
show_users ($_SESSION['users']);
break;
case 'show':
default:
show_users ($_SESSION['users']);
}
}
?>
</td>
</tr>
<tr>
</tr>
</table>
<PRE>
</PRE>
</body>
</html>