--- docman.php 2000/08/03 22:02:03 1.5 +++ docman.php 2000/09/13 09:59:16 1.22 @@ -65,9 +65,9 @@ TODO: mixed file/directory output (add type to each entry, real support for links) - add more content-management (like cms.sourceforge.net): - check-out/check-in/reserve - comments to files + retrieve old versions of files (overwritten) + show last lock date + */ ////////////////////////////////////////////////////////////////// @@ -108,6 +108,7 @@ if ($user[0] == $GLOBALS["PHP_AUTH_USER"]) { $gblUserName=$user[1]; $gblPw=$user[2]; + $gblEmail=$user[3]; continue ; } } @@ -120,8 +121,11 @@ $gblDateFmt="Y-m-d"; $gblTimeFmt="H:i:s"; -// Number of backup files to keep - $gblNumBackups=5; + // Number of backup files to keep + $gblNumBackups=3; + + // show red star if newer than ... days + $gblModDays=1; // choose GifIcon below unless you have the M$ // WingDings font installed on your system @@ -184,7 +188,7 @@ - - - [logout] + [?relogin=">logout]
ANYPORTAL(php) Site Manager
@@ -194,7 +198,7 @@


- +

" ; - echo htmlentities($tstr) . "

" . $tstr ; + if ( !$file_lock && $ext!="" && strstr(join(' ',$gblImages),$ext) ) { + $info = getimagesize($fsPath) ; + $tstr = "\""" ; +// echo htmlentities($tstr) . "

" . $tstr ; + echo $tstr ; } + ?>
@@ -309,6 +316,17 @@
+
+ +OK TO FORCE LOCK REMOVAL ON "" HELD BY ? + + +
CHANGES TO THIS FILE
\n"; $log=fopen($logname,"r"); $cl1=" class=lst"; $cl2=""; + $logarr = array(); while($line = fgetcsv($log,255,"\t")) { $cl=$cl1; $cl1=$cl2; $cl2=$cl; - print "$line[0]$line[1]$line[2]$line[3]\n"; + array_unshift($logarr,array($cl,$line[0],$line[1],$line[2],$line[3])); } fclose($log); + print "

CHANGES TO THIS FILE
\n"; + $bakcount = 0; // start from 0, skip fist backup (it's current) + while ($e = array_shift($logarr)) { + if (strstr($e[4],"upload")) { + if (file_exists("$bakdir/$bakcount/$name")) { + $e[4]="$e[4]"; + } + $bakcount++; + } + print "$e[1]$e[2]$e[3]$e[4]\n"; + } print "
"; } @@ -594,10 +624,13 @@ $d = "blank.gif" ; break ; case "checkout": - $d = "down.gif"; + $d = "box2.gif"; break; case "checkin": - $d = "up.gif"; + $d = "hand.up.gif"; + break; + case "locked": + $d = "screw2.gif"; break; case "note": $d = "quill.gif"; @@ -613,7 +646,7 @@ function Navigate($fsRoot,$relDir) { - global $gblEditable, $gblIcon ; + global $gblEditable, $gblIcon, $gblModDays ; $self = $GLOBALS["PHP_SELF"] ; if (isset($GLOBALS["HTTPS"]) && $GLOBALS["HTTPS"] == "on") { @@ -659,9 +692,10 @@ // start navigation page $text = "Use this page to add, delete"; if (! isset($show_deleted)) { - $text .= ", undelete"; + $text .= ", undelete"; } $text .= " or revise files on this web site." ; + $text .= "
Examine list of files changed in last day or all changes."; StartHTML("(Navigate)",$text) ; echo " - + @@ -728,31 +762,57 @@ $info_url=$self."?A=E&F=".urlencode($file)."&D=".urlencode($relDir); - if ( ($mod + 30*86400) > time() ) { + if ( ($mod + $gblModDays*86400) > time() ) { $a = " * " ; + $a .= " than $gblModDays days\"> * " ; } - $tstr = $webRoot . $relDir . "/" . $file ; - $tstr = "" ; + $file_lock=CheckLock($path); + + $file_url_html="" ; if (substr($file,0,5) != ".del/") { - $tstr .= $file . "" . $a ; + $file_url_html .= $file . "" . $a ; } else { - $tstr .= substr($file,5,strlen($file)-5) . " deleted "; + $file_url_html .= substr($file,5,strlen($file)-5) . " deleted "; } -// $b = $gblIcon("checkout"); -// $b .= $gblIcon("checkin"); + $note_html="".$gblIcon("note")."".ReadNote($path); $ext = strtolower(strrchr($file,".")) ; - if ( $ext=="" || strstr(join(" ",$gblEditable),$ext) ) { - $b .= "" ; - $b .= $gblIcon("view") . "" ; + + if ($file_lock) { + if ($file_lock == $GLOBALS[gblUserName]) { + $b.="" ; + $file_url_html=$b; + $b.=$gblIcon("checkin")."" ; + $b.= $gblIcon("blank"); + $file_url_html.="$file $a"; + $note_html = $gblIcon("blank")."Please check-in (update) this file"; + } else { + $b = $gblIcon("locked"); + $b.= $gblIcon("blank"); + $note_html = $gblIcon("blank")."File locked by $file_lock"; + $file_url_html = "$file $a"; + } } else { - $b .= $gblIcon("blank"); + $b.="" ; + $b.=$gblIcon("checkout")."" ; + + if ( $ext=="" || strstr(join(" ",$gblEditable),$ext) ) { + $b.="" ; + $b.=$gblIcon("view")."" ; + } else { + $b.= $gblIcon("blank"); + } } @@ -761,9 +821,9 @@ - + - + @@ -789,6 +849,16 @@ + + + + @@ -810,7 +879,7 @@ ////////////////////////////////////////////////////////////////// -function UploadPage($fsRoot, $relDir) { +function UploadPage($fsRoot, $relDir, $filename) { $self = $GLOBALS["PHP_SELF"] ; if ($relDir == "") $relDir = "/" ; @@ -820,6 +889,10 @@ DESTINATION DIRECTORY: + +
DESTINATION FILE: + +

PATHNAME OF LOCAL FILE
@@ -903,7 +976,7 @@ ////////////////////////////////////////////////////////////////// -function Logit($target,$msg) { +function LogIt($target,$msg) { $dir=dirname($target); if (! file_exists($dir."/.log")) { @@ -923,6 +996,7 @@ function WriteNote($target,$msg) { + $target=stripSlashes($target); $dir=dirname($target); if (! file_exists($dir."/.note")) { mkdir($dir."/.note",0700); @@ -939,6 +1013,7 @@ function ReadNote($target) { + $target=stripSlashes($target); $dir=dirname($target); $file=basename($target); $msg=""; @@ -947,7 +1022,7 @@ $msg=fgets($note,4096); fclose($note); } - return $msg; + return StripSlashes($msg); } @@ -955,6 +1030,7 @@ function MoveTo($source,$folder) { + $source=stripSlashes($source); $file=basename($source); if (! file_exists($folder)) { mkdir($folder,0700); @@ -966,6 +1042,132 @@ ////////////////////////////////////////////////////////////////// +function Lock($target) { + + $target=stripSlashes($target); + $dir=dirname($target); + if (! file_exists($dir."/.lock")) { + mkdir($dir."/.lock",0700); + } + $file=basename($target); + + if (file_exists("$dir/.lock/$file")) { + Logit($target,"attempt to locked allready locked file!"); + } else { + $lock=fopen("$dir/.lock/$file","w"); + fputs($lock,"$GLOBALS[gblUserName]\n"); + fclose($lock); + + Logit($target,"file locked"); + } + +} + +function CheckLock($target) { + + $target=stripSlashes($target); + $dir=dirname($target); + $file=basename($target); + $msg=0; + if (file_exists($dir."/.lock/$file")) { + $lock=fopen("$dir/.lock/$file","r"); + $msg=fgets($lock,4096); + fclose($lock); + } + return chop($msg); + +} + +function Unlock($target) { + + $target=stripSlashes($target); + $dir=dirname($target); + $file=basename($target); + if (file_exists($dir."/.lock/$file")) { + unlink("$dir/.lock/$file"); + Logit($target,"file unlocked"); + } else { + Logit($target,"attempt to unlocked non-locked file!"); + } + +} + +////////////////////////////////////////////////////////////////// + +function urlpath($url) { + $url=urlencode(StripSlashes("$url")); + $url=str_replace("%2F","/",$url); + $url=str_replace("+","%20",$url); + return($url); +} + +////////////////////////////////////////////////////////////////// + +function safe_rename($from,$to) { + if (file_exists($from) && is_writable(dirname($to))) { + rename($from,$to); + } +} + +////////////////////////////////////////////////////////////////// + +// recursivly delete directory + +function rrmdir($dir) { + $handle=opendir($dir); + while ($file = readdir($handle)) { + if ($file != "." && $file != "..") { + if (is_dir("$dir/$file")) + rrmdir("$dir/$file"); + else + if (! @unlink("$dir/$file")) return(0); + } + } + closedir($handle); + return @rmdir($dir); +} + +////////////////////////////////////////////////////////////////// + +function ChangeLog($target,$msg) { + + global $gblFsRoot; + $log=fopen("$gblFsRoot/.changelog","a+"); + if (substr($target,0,strlen($gblFsRoot)) == $gblFsRoot) + $target=substr($target,strlen($gblFsRoot),strlen($target)-strlen($gblFsRoot)); + fputs($log,time()."\t$target\t$GLOBALS[gblUserName]\t$msg\n"); + fclose($log); + +} + +function DisplayChangeLog($day) { + + global $gblFsRoot; + if (!file_exists("$gblFsRoot/.changelog")) return; + $log=fopen("$gblFsRoot/.changelog","r"); + $logarr = array(); + while($line = fgetcsv($log,255,"\t")) { + if ($day!=1 || ($day==1 && (time()-$line[0] < 24*60))) { + array_unshift($logarr,array($line[0],$line[1],$line[2],$line[3])); + } + } + fclose($log); + $cl1=" class=lst"; $cl2=""; + print "


DOCUMENT NAME NOTE LAST UPDATEFILE SIZE
Bytes

+ +

CREATE NEW DIRECTORY -OR- @@ -797,8 +867,7 @@ - OR UPLOAD A FILE + OR UPLOAD A FILE
\n"; + while ($e = array_shift($logarr)) { + $cl=$cl1; $cl1=$cl2; $cl2=$cl; + $date = date("$GLOBALS[gblDateFmt]", $e[0]); + $time = date("$GLOBALS[gblTimeFmt]", $e[0]); + $dir = dirname($e[1]); + $file = basename($e[1]); + print "$date$time$dir/$file$e[2]$e[3]\n"; + } + print "
"; + print "

".GifIcon(up)." Back to front page.

"; +} + +////////////////////////////////////////////////////////////////// + // MAIN PROGRAM // ============ // query parameters: capital letters @@ -1023,7 +1225,9 @@ $fsDir = $gblFsRoot . $relDir ; // current directory if ( !is_dir($fsDir) ) Error("Dir not found",$relDir) ; - + + $FN=stripSlashes($FN); + switch ($POSTACTION) { case "UPLOAD" : if (!is_writeable($fsDir)) Error("Write denied",$relDir) ; @@ -1032,7 +1236,11 @@ // TODO : should rather check for escapeshellcmds // but maybe RFC 18xx asserts safe filenames .... $source = $FN ; - $target = $fsDir . "/" . $FN_name ; + if (! isset($FILENAME)) { // from update file + $target = "$fsDir/$FN_name" ; + } else { + $target = "$fsDir/$FILENAME"; + } // backup old files first $dir=dirname($target); @@ -1046,16 +1254,21 @@ for($i=$GLOBALS[gblNumBackups]-1;$i>0;$i--) { MoveTo("$dir/.bak/$i/$file","$dir/.bak/".($i+1)."/"); } - MoveTo($target,$dir."/.bak/1/".$file); + MoveTo($target,$dir."/.bak/1/"); copy($source,$target) ; chmod($target,$gblFilePerms) ; clearstatcache() ; Logit($target,"uploaded"); + if (isset($FILENAME)) { + Unlock($target); + } + ChangeLog($target,"updated"); break ; case "SAVE" : - $path = $gblFsRoot . escapeshellcmd($RELPATH) ; + $path = $gblFsRoot . $RELPATH ; + $path=stripSlashes($path); $writable = is_writeable($path) ; $legaldir = is_writeable(dirname($path)) ; $exists = (file_exists($path)) ? 1 : 0 ; @@ -1063,10 +1276,12 @@ if (!($writable || (!$exists && $legaldir))) Error("Write denied",$RELPATH) ; $fh = fopen($path, "w") ; + $FILEDATA=stripSlashes($FILEDATA); fwrite($fh,$FILEDATA) ; fclose($fh) ; clearstatcache() ; Logit($path,"saved changes"); + ChangeLog($path,"saved changes"); break ; case "CREATE" : @@ -1077,19 +1292,28 @@ $relPath = $relDir . "/" . $FN ; switch ( $T ) { case "D" : // create a directory - if ( ! @mkdir($path,$gblDirPerms) ) - Error("Mkdir failed",$relPath) ; // eg. if it exists - clearstatcache() ; - break ; + if ( ! @mkdir($path,$gblDirPerms) ) + Error("Mkdir failed",$relPath) ; // eg. if it exists + clearstatcache() ; + break ; case "F" : // create a new file // this functionality is doubled in DetailView(). // better keep it here altogether // chmod perms to $gblFilePerms - if ( file_exists($path) && !is_writable($path) ) - Error("File not writable", $relPath) ; - $tstr = $PHP_SELF . "?A=E&D=" . $relDir . "&F=" . $FN ; - header("Location: " . $tstr) ; - exit ; + if ( file_exists($path) && !is_writable($path) ) + Error("File not writable", $relPath) ; + $fh = fopen($path, "w+") ; + if ($fh) { + fputs($fh,"\n"); + fclose($fh) ; + LogIt($path,"file created"); + } else { + Error("Creation of file $relPath failed -- $path"); + } + $tstr = "$PHP_SELF?A=E&D=".urlencode($relDir)."&F=".urlencode($FN) ; + header("Location: " . $tstr) ; + ChangeLog($target,"created"); + exit ; } break ; @@ -1115,15 +1339,13 @@ exit ; } else { Logit($path,"file deleted"); - if (! file_exists("$dir/.del/.log")) { - mkdir("$dir/.del/.log",0700); - } MoveTo("$dir/.log/$file","$dir/.del/.log/"); MoveTo("$dir/.note/$file","$dir/.del/.note/"); + MoveTo("$dir/.lock/$file","$dir/.del/.lock/"); } } else { // delete directory - if ( ! @rmdir($fsDir) ) { + if ( ! @rrmdir($fsDir) ) { Error("Rmdir failed", $tstr . $fsDir) ; } else { @@ -1142,6 +1364,7 @@ MoveTo("$fsDir/.del/$file","$fsDir/"); MoveTo("$fsDir/.del/.log/$file","$fsDir/.log/"); MoveTo("$fsDir/.del/.note/$file","$fsDir/.note/"); + MoveTo("$fsDir/.del/.lock/$file","$fsDir/.lock/"); break ; @@ -1149,8 +1372,13 @@ if ( $CONFIRM != "on" ) break ; Logit("$fsDir/$FN","renamed $FN to $NEWNAME"); - rename("$fsDir/$FN","$fsDir/$NEWNAME"); - rename("$fsDir/.log/$FN","$fsDir/.log/$NEWNAME"); + safe_rename("$fsDir/$FN","$fsDir/$NEWNAME"); + safe_rename("$fsDir/.log/$FN","$fsDir/.log/$NEWNAME"); + safe_rename("$fsDir/.note/$FN","$fsDir/.note/$NEWNAME"); + safe_rename("$fsDir/.lock/$FN","$fsDir/.lock/$NEWNAME"); + for($i=0;$i<=$GLOBALS[gblNumBackups];$i++) { + safe_rename("$fsDir/.bak/$i/$FN","$fsDir/.bak/$i/$NEWNAME"); + } break ; @@ -1158,6 +1386,11 @@ WriteNote("$fsDir/$FN","$NOTE"); break ; + case "UNLOCK" : + if ( $CONFIRM != "on" ) break ; + Unlock("$fsDir/$FN"); + break ; + default : // user hit "CANCEL" or undefined action } @@ -1173,8 +1406,11 @@ // $A=U : upload to path given in $D // $A=E : display detail of file $D/$F and edit // $A=C : display code in file $D/$F + // $A=Co : checkout file $D/$F + // $A=Ci : checkin file $D/$F + // $A=V : view file (do nothing except log) // default : display directory $D - + switch ($A) { case "U" : // upload to $relDir @@ -1187,13 +1423,48 @@ EndHTML() ; exit ; case "E" : + $F=stripSlashes($F); // detail of $relDir/$F if (is_file("$gblFsRoot/$relDir/$F")) DetailPage($gblFsRoot, $relDir, $F) ; exit ; case "C" : + $F=stripSlashes($F); // listing of $relDir/$F DisplayCode($gblFsRoot, $relDir, $F) ; exit ; + case "Co" : + // checkout + Lock("$gblFsRoot/$relDir/$F"); + header("Content-Disposition: attachment; filename=$F" ); + Header("Location: ".urlpath("$relDir/$F")); + exit; + case "Ci" : + $F=stripSlashes($F); + // upload && update to $relDir + if (!is_writeable($gblFsRoot . $relDir)) + Error("Write access denied",$relDir) ; + $text = "Use this page to update a single " ; + $text .= "file to $SERVER_NAME." ; + StartHTML("(Update file Page)", $text) ; + UploadPage($gblFsRoot, $relDir, $F) ; + EndHTML() ; + exit ; + case "V" : + // view + LogIt("$gblFsRoot/$relDir/$F","viewed"); + header("Content-Disposition: attachment; filename=$F" ); + Header("Location: ".urlpath("$relDir/$F")); + exit; + case "Ch" : + StartHTML("(File changes)","All changes chronologicaly..."); + DisplayChangeLog(0); // all + EndHTML() ; + exit; + case "Ch1" : + StartHTML("(File changes)","Changes to files in last day..."); + DisplayChangeLog(1); + EndHTML() ; + exit; } // default: display directory $relDir