//read the XML property file
$conf = fhParseXmlFile($xmlPropertyFile);
if(!isset($componentid)) {
NOF_throwError(800,array("{1}"=>"componentid","{2}"=>$nof_suiteName));
exit();
}
$MCompIdent = "manager" . "." . $componentid . ".";
$tmp_DBname = $conf[$MCompIdent . "dbPath"];
//TODO: have to get this from xml or post
$FHID = getFHID($conf[$MCompIdent . "dbPath"]);
if($FHID == -1) {
NOF_throwError(602,array("{1}"=>$nof_suiteName,"{2}"=>NOF_mapPath($conf[$MCompIdent . "dbPath"])));
exit();
}
$FHCompIdent = "formshandler." . $FHID . ".";
//add the cgi-bin path ahead of the DB file name if its a pure file name without whole path
//(when the file path is just a name its asssumed that its in cgi-bin)
if( !preg_match("/\//" , $conf[$MCompIdent . "dbPath"]) && !preg_match('/(\\\\)/' , $conf[$MCompIdent . "dbPath"]) ) {
$conf[$MCompIdent . "dbPath"]=$cgiDir . "/" . $conf[$MCompIdent . "dbPath"];
}
//get the names of fields present in the database
getExpectedDBFields($FHCompIdent);
if ($conf[$MCompIdent . "writeToDB"]!="false") {
if (!file_exists(dirname($conf[$MCompIdent . "dbPath"]))) {
NOF_throwError(540,array("{1}"=>dirname(NOF_mapPath($conf[$MCompIdent . "dbPath"])),"{2}"=>NOF_mappath(getcwd())));
}
if (!is_writable(dirname($conf[$MCompIdent . "dbPath"]))) {
NOF_throwError(541,array("{1}"=>dirname(NOF_mapPath($conf[$MCompIdent . "dbPath"])),"{2}"=>NOF_mappath(getcwd())));
}
//if DB does not exist or if first line of dbColumns.db(column headings
//does not match first line of DB meaning that
//the file has to be created/overwritten with new columns from dbColumns.db
if( !checkIfDBmatch($MCompIdent) ) {
//open DB file for writing
if (!$FILE = @fopen($conf[$MCompIdent . "dbPath"], 'wb')) {
NOF_throwError(500,array("{1}"=>NOF_mapPath($conf[$MCompIdent . "dbPath"]),"{2}"=>NOF_mapPath(dirname($conf[$MCompIdent . "dbPath"]))));
exit();
}
//write first line of UD file, to the DB file
if (!fputs($FILE, "$firstLineUD\n")) {
NOF_throwError(502,array("{1}"=>NOF_mapPath($conf[$MCompIdent . "dbPath"]),"{2}"=>NOF_mapPath(dirname($conf[$MCompIdent . "dbPath"]))));
exit();
}
fclose($FILE);
}
}
if ( GetPostVariable("fhpageoperation")=='' ) {
SetPostVariable("fhpageoperation", "");
}
$fh_var["begin"] = 1;
if ( GetPostVariable("begin")!='' ) {
$fh_var["begin"] = GetPostVariable("begin");
}
if ( GetPostVariable("fhpageoperation")=='Next' ) {
$fh_var["begin"] = $fh_var["begin"] + 10;
} elseif( GetPostVariable("fhpageoperation")=='Prev') {
$fh_var["begin"] = $fh_var["begin"] - 10;
} elseif( GetPostVariable("fhpageoperation")=='First' ) {
$fh_var["begin"] = 1;
} elseif( GetPostVariable("fhpageoperation")=='Last' ) {
$fh_var["begin"] = -100;
fhDisplayLastEntriesList();
}
//if no operation was passed (meaning page is loaded the first time)
if( GetPostVariable("fhoperation") == '' ) {
//display upto 10 entries beginning from 1
if($fh_var["begin"] > -90){ fhDisplayEntriesList($fh_var["begin"]); }
} elseif( GetPostVariable("fhoperation")=='delete' && GetPostVariable("begin")!='' ) { //if remove entry button was pressed
//the entry selected in the radio group
if( GetPostVariable("radiogroup")!='' ) {
//get the numer of the entry that needs to removed
$entryToBeRemoved = GetPostVariable("radiogroup");
//remove the entry
fhRemoveEntry($entryToBeRemoved);
}
//display upto 10 entries from $_POST["begin"]
fhDisplayEntriesList(GetPostVariable("begin"));
} elseif ( GetPostVariable("fhoperation")=='view' && GetPostVariable("begin")!='' ) { //if View button was pressed
//the user name selected in the radio group
if( GetPostVariable("radiogroup")!='' ) {
//get the numer of the entry that needs to removed
$entryToBeViewed = GetPostVariable("radiogroup");
//display details of the selected user
fhDisplayEntryDetails($entryToBeViewed, GetPostVariable("begin"));
}
} elseif ( GetPostVariable("fhoperation")=='edit' && GetPostVariable("begin")!='' ) { //if Edit button was pressed
//the user name selected in the radio group
if( GetPostVariable("radiogroup")!='' ) {
//get the numer of the entry that needs to removed
$entryToBeEdited = GetPostVariable("radiogroup");
//display details of the selected user
fhDisplayEditEntry($entryToBeEdited, GetPostVariable("begin"));
}
} elseif ( GetPostVariable("fhoperation")=='add' && GetPostVariable("begin")!='' ) { //if add user button was pressed
//display add user form
fhDisplayAddEntry(GetPostVariable("begin"));
} elseif( GetPostVariable("fhoperation")=='back' && GetPostVariable("begin")!='' ) { //if back to entries list was pressed
//display the list of entries from "begin"
fhDisplayEntriesList(GetPostVariable("begin"));
} elseif( GetPostVariable("fhoperation")=='reset' && GetPostVariable("entrytobeedited")!='' ) { //if Reset was pressed
//display the edit form
fhDisplayEditEntry(GetPostVariable("entrytobeedited"), GetPostVariable("begin"));
} elseif ( GetPostVariable("fhoperation")=='saveentry' && GetPostVariable("entrytobeedited")!='' ) { //if save entry button was pressed
$errorFlag=FALSE;
//if the required parameters were unfilled
if ( fhCheckIfRequiredUnfilled() ) {
//display the error
fhDisplayError($errorLabel,$errorField);
//display the edit form filled with previous values
fhDisplayEditEntryError(GetPostVariable("entrytobeedited"), GetPostVariable("begin"), $errorField);
exit();
}
//if fields are not within their length limitations
if( fhCheckIfFieldsLengthInvalid() ) {
$errorFlag=TRUE; //set error flag to true
}
//on error
if( $errorFlag ) {
//display the error
fhDisplayError($errorLabel,$errorField);
//display the edit form with previous values filled
fhDisplayEditEntryError(GetPostVariable("entrytobeedited"),GetPostVariable("begin"), $errorField);
exit();
} else { //if no errors were encountered in user input
//change the entry details
fhChangeEntry(GetPostVariable("entrytobeedited"));
//display success message
fhDisplaySuccessMessage($GLOBALS['nof_resources']->get('FH.Manager.Text.EditSuccess'));
//display the edit entry form with the changed details
fhDisplayEditEntry(GetPostVariable("entrytobeedited"), GetPostVariable("begin"));
exit();
}
} elseif ( GetPostVariable("fhoperation")=='addentry' && GetPostVariable("begin")!='' ) {
//if add user button was pressed
$errorFlag=FALSE;
//if the required parameters were unfilled
if ( fhCheckIfRequiredUnfilled() ) {
//display error at the top
fhDisplayError($errorLabel,$errorField);
//display the add form with previous values filled
fhDisplayAddEntryError(GetPostVariable("begin"), $errorField);
exit();
}
//if fields are not within their length limitations
if( fhCheckIfFieldsLengthInvalid() ) {
$errorFlag=TRUE; //set error flag
}
//if uploaded files had any errors
if( checkIfUploadedFilesInvalid($FHCompIdent) ) {
$errorFlag=TRUE; //set error flag to true
}
//on error
if( $errorFlag ) {
//display error at the top
fhDisplayError($errorLabel,$errorField);
//display the add form with the previous values filled
fhDisplayAddEntryError(GetPostVariable("begin"), $errorField);
exit();
} else {
//if no errors were encountered in user input
//store the uploaded file in the directory specified in the property file
getUploadedFile($MCompIdent);
//add the new entry to the end of the DB file
fhInsertInDB();
//display the successmessage
fhDisplaySuccessMessage($GLOBALS['nof_resources']->get('FH.Manager.Text.AddSuccess'));
//display the last entries of the DB
fhDisplayLastEntriesList();
exit();
}
}
//function to remove a particular entry from the DB
function fhRemoveEntry($entryToBeRemoved) {
global $conf,$cgiDir,$MCompIdent;
//get the db in an array
if(!$lines = @file($conf[$MCompIdent . "dbPath"])) {
NOF_throwError(501,array("{1}"=>NOF_mapPath($conf[$MCompIdent . "dbPath"]),"{2}"=>NOF_mapPath(dirname($conf[$MCompIdent . "dbPath"]))));
exit();
}
//open db file for writing
if (!$FILE = @fopen($conf[$MCompIdent. "dbPath"], 'wb')) {
NOF_throwError(502,array("{1}"=>NOF_mapPath($conf[$MCompIdent . "dbPath"]),"{2}"=>NOF_mapPath(dirname($conf[$MCompIdent . "dbPath"]))));
exit();
}
//dump into the file all lines except where the user to be removed was found
for( $i=0; $iNOF_mapPath($conf[$MCompIdent . "dbPath"]),"{2}"=>NOF_mapPath(dirname($conf[$MCompIdent . "dbPath"]))));
exit();
}
}
}
}
//function to display upto 10 entries from $begin
function fhDisplayEntriesList($begin) {
global $conf,$fieldsDBArray,$MCompIdent,$formName;
//get the whole database in an array
if(!$lines = @file($conf[$MCompIdent. "dbPath"])) {
NOF_throwError(501,array("{1}"=>NOF_mapPath($conf[$MCompIdent . "dbPath"]),"{2}"=>NOF_mapPath(dirname($conf[$MCompIdent . "dbPath"]))));
exit();
}
if ($begin >= count($lines)) {
$begin = $begin -10;
}
if ($begin < 1 ) {
$begin = 1;
}
//begin form
echo "" . "\r\n";
}
//function to display the last page of entries
function fhDisplayLastEntriesList() {
global $conf,$fieldsDBArray,$MCompIdent,$formName;
//get the whole database in an array
if(!$lines = @file($conf[$MCompIdent . "dbPath"])) {
NOF_throwError(501,array("{1}"=>NOF_mapPath($conf[$MCompIdent . "dbPath"]),"{2}"=>NOF_mapPath(dirname($conf[$MCompIdent . "dbPath"]))));
exit();
}
//set begin
$begin=count($lines) - (count($lines)-1)%10;
if ($begin < 1 ) {
$begin = 1;
}
if ($begin >= count($lines)) {
$begin = $begin -10;
}
//begin form
echo "" . "\r\n";
}
//function to display form to edit entry details
function fhDisplayEditEntry($entryToBeEdited,$begin) {
global $conf,$cgiDir,$fieldsDBArray,$MCompIdent,$formName;
//get the db in an array
if(!$lines = @file($conf[$MCompIdent. "dbPath"])) {
NOF_throwError(501,array("{1}"=>NOF_mapPath($conf[$MCompIdent . "dbPath"]),"{2}"=>NOF_mapPath(dirname($conf[$MCompIdent . "dbPath"]))));
exit();
}
//begin form
echo "" . "\r\n";
}
//function to display form to view entry details
function fhDisplayEntryDetails($entryToBeViewed,$begin) {
global $conf,$cgiDir,$fieldsDBArray,$MCompIdent,$formName;
//get the db in an array
if(!$lines = @file($conf[$MCompIdent. "dbPath"])) {
NOF_throwError(501,array("{1}"=>NOF_mapPath($conf[$MCompIdent . "dbPath"]),"{2}"=>NOF_mapPath(dirname($conf[$MCompIdent . "dbPath"]))));
exit();
}
//begin form
echo "" . "\r\n";
}
//function to display form to add an entry to the db
function fhDisplayAddEntry($begin) {
global $conf,$cgiDir,$fieldsDBArray,$formName;
//begin form
echo "" . "\r\n";
}
function fhDisplayAddEntryError($begin,$errorField) {
global $conf,$cgiDir,$fieldsDBArray,$formName;
$errorFields = explode(",",$errorField);
//begin form
echo "" . "\r\n";
}
function fhDisplayEditEntryError($entryToBeEdited,$begin,$errorField) {
global $conf,$cgiDir,$fieldsDBArray,$MCompIdent,$formName;
$errorFields = explode("'",$errorField);
//get the db in an array
if(!$lines = @file($conf[$MCompIdent. "dbPath"])) {
NOF_throwError(501,array("{1}"=>NOF_mapPath($conf[$MCompIdent . "dbPath"]),"{2}"=>NOF_mapPath(dirname($conf[$MCompIdent . "dbPath"]))));
exit();
}
//begin form
echo "" . "\r\n";
}
//function to change a particular entry in the DB
function fhChangeEntry($entryToBeChanged) {
global $conf,$fieldsDBArray,$MCompIdent,$formName;
//get the db in an array
if(!$lines = @file($conf[$MCompIdent. "dbPath"])) {
NOF_throwError(501,array("{1}"=>NOF_mapPath($conf[$MCompIdent . "dbPath"]),"{2}"=>NOF_mapPath(dirname($conf[$MCompIdent . "dbPath"]))));
exit();
}
//make the changed row entry
$entry="";
for($i=0;$iNOF_mapPath($conf[$MCompIdent . "dbPath"]),"{2}"=>NOF_mapPath(dirname($conf[$MCompIdent . "dbPath"]))));
exit();
}
//dump into the db
for($i=0;$iNOF_mapPath($conf[$MCompIdent . "dbPath"]),"{2}"=>NOF_mapPath(dirname($conf[$MCompIdent . "dbPath"]))));
exit();
}
}
fclose($FILE);
}
//function to see if all required values are filled
function fhCheckIfRequiredUnfilled() {
global $conf,$FHCompIdent;
$errorFoundFlag=FALSE;
$postArr = GetPostVariable('');
reset($postArr);
while (list($field, $value) = each ($postArr)) {
$property = $FHCompIdent . $field . ".errorevent.required.active";
//if a field is empty and the required property for the field is
//set in the property file
if( preg_match("/^\s*$/",$postArr[$field]) && isset($conf[$property])) {
if ($conf[$property]=="true" ) {
$label=$FHCompIdent . $field . ".errorevent.required.message" ;
clubError($label,$field);
$errorFoundFlag=TRUE;
}
}
}
$postFiles = GetFileVariable('');
reset($postFile);
while(list($field, $value)=each($postFile)) {
$property = $FHCompIdent . $field . ".errorevent.required.active";
//if a file field is not filled and the required property for the field is
//set in the property file
if( !isset($postFile[$field]['name'])
|| empty($postFile[$field]['name'])
&& isset($conf[$property])) {
if ($conf[$property]=="true") {
$label=$FHCompIdent . $field . ".errorevent.required.message" ;
clubError($label,$field);
$errorFoundFlag=TRUE;
}
}
}
return $errorFoundFlag;
}
//function to see if the field lengths are within the limits
function fhCheckIfFieldsLengthInvalid() {
global $conf,$FHCompIdent;
$errorFoundFlag=FALSE;
$postArr = GetPostVariable('');
reset($postArr);
while( list($field,$value)= each($postArr) ) {
$shortErrorEventActiveProperty = $FHCompIdent . $field . ".errorevent.short.active";
//if the short property for the field is set
if( $postArr[$field]!="" &&
isset($conf[$shortErrorEventActiveProperty]) ){
if ($conf[$shortErrorEventActiveProperty]=="true" ){
$minimumLengthProperty = $FHCompIdent . $field . ".errorevent.short.minimumlength";
//if the length of the field is lesser than the limit set in
//the property file
if( preg_match("/^[-+]{0,1}\d+$/" ,$conf[$minimumLengthProperty] ) ) {
if( strlen($postArr[$field]) < $conf[$minimumLengthProperty] ) {
clubError($FHCompIdent . $field . ".errorevent.short.message", $field);
$errorFoundFlag=TRUE;
}
}
}
}
$longErrorEventActiveProperty = $FHCompIdent . $field . ".errorevent.long.active";
//if the long property for the field is set
if(!isset($conf[$longErrorEventActiveProperty])) {
$conf[$longErrorEventActiveProperty] = "false";
}
if($conf[$longErrorEventActiveProperty]=="true"){
$maximumLengthProperty = $FHCompIdent . $field . ".errorevent.long.maximumlength";
//if the length of the field is greater than the limit set in
//the property file
if( preg_match("/^\+{0,1}\d+$/" ,$conf[$maximumLengthProperty] ) ) {
if( strlen($postArr[$field]) > $conf[$maximumLengthProperty] ) {
clubError($FHCompIdent . $field . ".errorevent.long.message", $field);
$errorFoundFlag=TRUE;
}
}
}
}
return $errorFoundFlag;
}
//display error message
function fhDisplayError($errorLabel,$errorField) {
global $conf;
//get the error labels and associated fields in arrays
$errorLabel = explode(",",$errorLabel);
//for each error
echo "
" . "\r\n";
}
//function to check if a field is a file type
//and if so make it a link
function fhHrefSurroundIfFile($fieldName,$fieldValue) {
global $conf,$cgiDir,$MCompIdent;
//if the fieldvalue is empty return without doing much
if(empty($fieldValue)) {
return $fieldValue;
}
//if this field is indeed a file type
if(fhIsFileField($fieldName)) {
//if this upload directory specified by the user is not an absolute path
if(!preg_match("/^[a-z]\:/i",$conf[$MCompIdent. "dbFileUploadDir"])
&&!preg_match("/^\//",$conf[$MCompIdent. "dbFileUploadDir"]) ) {
//get the file path relative to the current page
$filePath = $cgiDir ."/" //this is how we get to the cgi-bin directory from current page
. $conf[$MCompIdent. "dbFileUploadDir"] //this is how we get to upload directory from cgi-bin
. "/" . $fieldValue; //finally the filename
if(file_exists($filePath)) {
//surround by href tags, file preview available only if path is relative to page
$filePathHref = "" . basename($fieldValue) . "";
return $filePathHref;
} else {
$filePathHref = "get('FH.Manager.Text.FileDoesNotExist',array("{1}"=>$filePath))))."');\">" . basename($fieldValue) . "";
return $filePathHref;
}
} else { //if upload directory specified by user is an absolute path
//no surrounding by href tag will take place
//cause absolute path may not be in the
//public_html directory.
return $fieldValue;
}
} else { //if field was not of type file return field value as it is
return $fieldValue;
}
}
//function that takes the upload dir and
//filename and returns the upload path
//also it changes the name of the file in
//the global file array
function fhSetUploadFilePath($dbFileUploadDir,$fileName,$key) {
global $conf,$cgiDir;
//if no upload directory was specified then
//make it the cgi-bin directory
$dbFileUploadDir= trim($dbFileUploadDir);
if(!preg_match("/^[a-z]\:/i",$dbFileUploadDir) && !preg_match("/^\//",$dbFileUploadDir)) {
//if this upload directory specified by the user
//is a relative path (relative to cgi-bin)
//make it relative to the page
$dbFileUploadDir = $cgiDir . "/" . $dbFileUploadDir;
}
$uploadFilePath = $dbFileUploadDir . "/" . $fileName;
$i=1;
while(file_exists($uploadFilePath)) {
$fileNameArr = explode("." , $fileName);
if(isset($fileNameArr[1])) {
$newFileName = $fileNameArr[0] . $i . "." . $fileNameArr[1];
} else {
$newFileName = $fileNameArr[0] . $i;
}
$uploadFilePath = $dbFileUploadDir . "/" . $newFileName;
$postFile = GetFileVariable($key);
$postFile['name'] = $newFileName;
$i++;
}
return $uploadFilePath;
}
//function that returns true only if a field is a file type
function fhIsFileField($fieldName) {
global $conf,$FHCompIdent;
//this property exists only for file fields in the property file
$property=$FHCompIdent . $fieldName . ".errorevent.invalidextension.active" ;
//if the property is set then return true
return isset($conf[$property]);
}
//insert the field values in the DB
function fhInsertInDB() {
global $conf,$requiredForPassRetv,$fieldsDBArray,$firstLineUD,$MCompIdent;
//for every expected field in the DB get its value from post request
for($i=0;$iNOF_mapPath($conf[$MCompIdent . "dbPath"]),"{2}"=>NOF_mapPath(dirname($conf[$MCompIdent . "dbPath"]))));
exit();
}
//append entry to the file
if (!fputs($FILE, "$entry\n")) {
NOF_throwError(502,array("{1}"=>NOF_mapPath($conf[$MCompIdent . "dbPath"]),"{2}"=>NOF_mapPath(dirname($conf[$MCompIdent . "dbPath"]))));
exit();
}
//close file handle
fclose($FILE);
}
function getFHID($managerDBPath) {
global $conf;
$cmp = -1;
reset($conf);
while (list($key,$value) = each($conf)) {
if(preg_match("/^formshandler\.(\d+)\.dbPath$/",$key,$match)) {
if($value == $managerDBPath) {
$cmp = $match[1];
//return $match[1];
}
}
}
return $cmp;
}
?>