- <?php
- /**
- * Request Logger - simple request/environment logger for your site
- *
- * Copyright (C) 2011 Corllete Lab <http://www.clabteam.com/>
- * Contact info - Corllete Lab <[email protected]>
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- *
- * $Id$
- * @license GPL
- */
- /**
- * Don't change this, see the configuration area (config() method)
- * However, you could add more logging cases (e.g. requestLogger::logRequest('GET');) but
- * keep in mind this will extremely scale up the file size of your logs.
- */
- if ('POST' == $_SERVER['REQUEST_METHOD'])
- {
- requestLogger::logRequest('POST');
- requestLogger::logRequest('FILES', "\t\t - Uploaded files: ");
- }
- /**
- * You setup some aspects of the script - normally all you'd need is to set/change variable values between
- * "EDIT CONFIGURATION HERE" comment inside config() method
- *
- */
- class requestLogger
- {
- /**
- *
- * @var array configuration array
- */
- protected static $_config = null;
- /**
- * Get specific configuration value or the configuration array if key is not provided.
- * Configuration array is built and cached on first call.
- *
- * @param string|null $key
- * @param mixed $default
- * @return array
- */
- public static function config($key = null, $default = null)
- {
- if(null === self::$_config)
- {
- self::$_config = array();
- $time = time();
- $FILES_DIRECTORY = '';
- include (realpath(dirname(__FILE__)).'/e107_config.php');
- $ds = DIRECTORY_SEPARATOR;
- ### EDIT CONFIGURATION HERE - all paths are ending with directory separator
- // script location - should always work, alter it with absolute server path if required
- // it doesn't include script filename so it's perfectly safe to rename the script without any
- // additional configuration tweaks. However you could move yor script to different place. If this
- // is the case don't forget to update e107_root with the actual path to your e107 installation and
- // $CLASS2_INCLUDE (e107_config.php) with the new path. The only reqiurement here is the script should
- // be inside WEBROOT of your HTTP server.
- // self::$_config['script_path'] = '/home/username/public_html/secret/'; // linux custom path example
- self::$_config['script_path'] = realpath(dirname(__FILE__)).$ds;
- // e107 root path - depends on script_path, don't forget to update it if you change it for some reason
- // self::$_config['script_path'] = '/home/username/public_html/'; // linux example with combination of the above example
- self::$_config['your_cms_root'] = self::$_config['script_path'];
- // used to separate log files by folders.
- // Example:
- // Folders mask - date('Y-m-d', $time).$ds - to store log files in folders per day
- // Filename mask - date('H', $time); - log files will be created per hour
- // Good example for busy (high traffic sites)
- // self::$_config['log_folder'] = @date('Y-m-d', $time).$ds;
- self::$_config['log_folder'] = date('Y-m-d', $time).$ds;
- // string used for log filename (no extension). Example: date('Y-m-d-H', $time) - creates new log file on every hour
- // self::$_config['log_fmask'] = @date('Y-m-d-H', $time);
- // self::$_config['log_fmask'] = @date('H', $time); // use this with combination of the above high traffic sites example
- self::$_config['log_fmask'] = @date('Y-m-d', $time);
- // log POST requests
- self::$_config['dolog_post'] = true;
- // log $_FILES content (uploaded files) if any
- self::$_config['dolog_files'] = true;
- // Hide sensitive information - array of variables which values should be masked
- self::$_config['hide_keys'] = array('userpass', 'password', 'password1', 'password2','smtp_password');
- // Hide sensitive information - array of variables containing email addresses
- self::$_config['email_keys'] = array('email', 'smtp_username', 'testaddress');
- // Hide sensitive information - array of variables to exclude from the log
- self::$_config['exclude_keys'] = array();
- // path to log folder (writable, if empty will be auto-set to e107_files/logs)
- // For maximum safety, you could move your log folder outside the WEBROOT
- //
- self::$_config['log_path'] = '';
- ### END EDIT CONFIGURATION
- self::$_config['e107_files'] = str_replace('/', $ds, $FILES_DIRECTORY);
- if(empty(self::$_config['log_path']))
- {
- self::$_config['log_path'] = self::$_config['your_cms_root'].self::$_config['e107_files'].'logs'.$ds;
- if (!is_dir(self::$_config['your_cms_root'].self::$_config['e107_files'].'logs'))
- {
- mkdir(self::$_config['your_cms_root'].self::$_config['e107_files'].'logs', 0777, true);
- file_put_contents(self::$_config['log_path'].'.htaccess', "Order Deny,Allow\nDeny from All");
- }
- }
- }
- if(null === $key) return self::$_config;
- return (isset(self::$_config[$key]) && self::$_config[$key] ? self::$_config[$key] : $default);
- }
- /**
- * Log request method/super global
- *
- * @param string $method
- * @param string $label additional (prefix) label for logged record
- *
- * @return void
- */
- public static function logRequest($method, $label = '')
- {
- $config = self::config();
- if(!@$config['dolog_'.strtolower($method)]) return;
- $scriptPath = $config['script_path'];
- $e107Path = $config['your_cms_root'];
- $logPath = $config['log_path'];
- $logFolder = $config['log_folder'];
- $logFile = $config['log_fmask'].'.log';
- $request_time = @date('d m Y G:i:s', time());
- $request_host = $_SERVER['REMOTE_ADDR'];
- $request_method = strtoupper($method);
- $request_target = $_SERVER['REQUEST_URI'];
- $request_var = '_'.$request_method;
- $request_content= self::cleanAll($config, @$GLOBALS[$request_var]);
- if(empty($request_content)) return;
- if (!($path = self::checkPath($logPath, $logFolder, $logFile))) return;
- if (is_array($request_content)) $request_content = self::flattenArray($request_content);
- $log = $label."{$request_host}\t- {$request_time} - \"\${$request_var} [{$request_target}]\" -> [{$request_content}]\n";
- # Check file uploads
- //if (!empty($_FILES)) $log.= "\t\t - Uploaded files:".self::flattenArray($_FILES)."\n";
- $file = fopen($path, 'a+');
- fwrite($file, $log);
- fclose($file);
- }
- /**
- * Clean data to be logged based on current configuration values.
- *
- * @param array $config configuration data
- * @param array $data data to be logged
- *
- * @return array modified data
- */
- public static function cleanAll($config, $data)
- {
- if(empty($data) || !is_array($data)) return array();
- self::clean($config['hide_keys'], $data, 'hide');
- self::clean($config['email_keys'], $data, 'email');
- self::clean($config['exclude_keys'], $data, 'exclude');
- return $data;
- }
- /**
- * Clean data based on provided map (numerical) array and type
- *
- * @param array $map collection of keys to be searched and modified
- * @param array $data destination data to be cleaned
- * @param string $type exclude|hide|email are currently supported types
- *
- * @return void
- */
- protected static function clean($map, &$data, $type = 'exclude')
- {
- if(empty($map) || !is_array($map)) return;
- foreach ($map as $key)
- {
- if(isset($data[$key]))
- {
- switch ($type)
- {
- case 'hide':
- $data[$key] = '******';
- break;
- case 'email':
- if(!strpos($data[$key], '@')) break; // 0 is not an option here
- $tmp = explode('@', $data[$key], 2);
- $data[$key] = '******@'.@$tmp[1];
- break;
- case 'exclude':
- default:
- unset($data[$key]);
- break;
- }
- }
- }
- }
- /**
- * Do some path checking, create folder and/or log file if needed
- *
- * @param string $filepath absolute path to the log storage space
- * @param string $folder dynamic folder/relative path inside the storage
- * @param string $filename log filename
- *
- * @return string absolute path to the log file
- */
- protected static function checkPath($filepath, $folder, $filename)
- {
- if (!is_writeable($filepath))
- return false;
- if (!is_readable($filepath.'.htaccess'))
- file_put_contents($filepath.'.htaccess', "Order Deny,Allow\nDeny from All");
- if ($folder && !is_dir($filepath.$folder))
- {
- mkdir($filepath.$folder, 0777, true);
- file_put_contents($filepath.$folder.'.htaccess', "Order Deny,Allow\nDeny from All");
- }
- if ($folder && is_dir($filepath.$folder))
- $filepath = $filepath.$folder;
- if (!is_file($filepath.$filename))
- if(file_put_contents($filepath.$filename, '') !== false)
- @chmod($filepath.$filename, 0777);
- return $filepath.$filename;
- }
- /**
- * Convert array to well formatted string
- *
- * @param array $ArrayData
- * @return string
- */
- protected static function flattenArray($ArrayData)
- {
- if (!is_array($ArrayData)) return false;
- return rtrim(str_replace(array("\r", "\n", "\r\n", "\n\r"), '', var_export($ArrayData, true)), ',');
- }
- }
rl.php