-
-
Save exside/5073727 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* MODX FormLog hook to store formIt data in a jSon log file | |
* | |
* @author Kilian Bohnenblust (sofasurfer.org) | |
* @example [[!FormIt? | |
* &hooks=`spam,formlog,email,redirect` | |
* &logfile=`[[++base_path]]assets/data/form-contact.log` | |
* | |
*/ | |
/* path to data file */ | |
$logFile = $modx->getOption('logfile',$scriptProperties,$modx->getOption('logfile')); | |
/* variable for error message */ | |
$errorMessage = false; | |
/* setup default properties */ | |
$type = $modx->getOption('type',$scriptProperties,''); | |
/* check if form file exist */ | |
if( file_exists($logFile) ){ | |
/* get form from file */ | |
$formText = file_get_contents($logFile); | |
/* create array list from form */ | |
$formList = json_decode($formText,true); | |
} | |
/* get form data */ | |
$formValues = $hook->getValues(); | |
$formValues['clientip'] = $_SERVER['REMOTE_ADDR']; | |
$formValues['logtime'] = time(); | |
/* add form date to array */ | |
$formList['formdata'][] = $formValues; | |
/* convert form to string */ | |
$formText = json_encode($formList); | |
/* save comment to file */ | |
if( file_put_contents($logFile, $formText) == false ){ | |
/* return error */ | |
$hook->addError("error_message","Form data could bot be saved to " . $logFile ); | |
return false; | |
}else{ | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment