220 lines
6.7 KiB
PHP
220 lines
6.7 KiB
PHP
<?php
|
|
|
|
/**
|
|
* File from http://PrestaShow.pl
|
|
*
|
|
* DISCLAIMER
|
|
* Do not edit or add to this file if you wish to upgrade this module to newer
|
|
* versions in the future.
|
|
*
|
|
* @authors PrestaShow.pl <kontakt@prestashow.pl>
|
|
* @copyright 2015 PrestaShow.pl
|
|
* @license http://PrestaShow.pl/license
|
|
*/
|
|
class PShow_Log
|
|
{
|
|
|
|
const IMPORT_START = "*-*-* IMPORT START *-*-*";
|
|
const IMPORT_CRON_START = "*-*-* DIRECT IMPORT START *-*-*";
|
|
const IMPORT_STOP = "*-*-* IMPORT STOPPED *-*-*";
|
|
const IMPORT_FINISH = "*-*-* IMPORT FINISHED *-*-*";
|
|
const IMPORT_SUCCESS = "object from line %d imported successfully...";
|
|
const IMPORT_SKIP = "object from line %d with type %s skipped...";
|
|
const IMPORT_SKIP__IMPORTED = "object from line %d typu %s skipped because product with this code/id was imported befaore...";
|
|
const IMPORT_SKIP__NOT_FOUND = "object from line %d typu %s skipped because not found products to update...";
|
|
const IMPORT_FAIL = "object from line %d not imported...";
|
|
const FILE_DOWNLOAD = "*-*-- downloading file from: %s";
|
|
const FILE_CLONE = "*-*-- file cloned into: %s";
|
|
|
|
public static $importInstance = null;
|
|
|
|
/**
|
|
* File handlers
|
|
*
|
|
* @var array of resource
|
|
*/
|
|
private static $fileHandle = array();
|
|
|
|
/**
|
|
* Close all opened log files
|
|
*/
|
|
public static function closeAllFiles()
|
|
{
|
|
foreach (self::$fileHandle as $index => $handle) {
|
|
fclose($handle);
|
|
unset(self::$fileHandle[$index]);
|
|
}
|
|
}
|
|
|
|
/**
|
|
*
|
|
* @param string $file_path_
|
|
* @return resource
|
|
*/
|
|
protected static function getFileHandler($file_path_)
|
|
{
|
|
$dir_path = str_replace('.log', '', $file_path_);
|
|
|
|
if (!is_dir($dir_path)) {
|
|
mkdir($dir_path, 0777, true);
|
|
|
|
// add index.php for security
|
|
$token = md5($dir_path);
|
|
$contents = '<?php if (isset($_GET) && isset($_GET["token"]) && $_GET["token"] == "' . $token . '") { ';
|
|
$contents .= ' $files = glob(dirname(__FILE__) . "/*.log"); ';
|
|
$contents .= ' foreach ($files as $f) { ';
|
|
$contents .= ' $url = "' . __PS_BASE_URI__ . 'modules/pshowimporter".substr($f, stripos($f, \'/import-log/\')).""; ';
|
|
$contents .= ' $filename = pathinfo($f, PATHINFO_FILENAME); ';
|
|
$contents .= ' $date_eu = str_replace(\'_\', \'-\', $filename); ';
|
|
$contents .= ' echo "<p><a href=\'".$url."\'>".$date_eu."</a></p>"; ';
|
|
$contents .= ' } ';
|
|
$contents .= ' if (!count($files)) echo "<p>No files found...</p>"; ';
|
|
$contents .= ' die(); ';
|
|
$contents .= ' } else { die("Access denied"); }';
|
|
file_put_contents($dir_path . '/index.php', $contents);
|
|
}
|
|
|
|
if (file_exists($file_path_)) {
|
|
@rename($file_path_, $dir_path . '/old-log.log');
|
|
}
|
|
|
|
$file_path = $dir_path . '/' . date('d_m_Y') . '.log';
|
|
|
|
return fopen($file_path, 'a');
|
|
}
|
|
|
|
/**
|
|
* Append to log file
|
|
*
|
|
* @param string $file
|
|
* @param string $txt
|
|
* @return boolean
|
|
*/
|
|
public static function add($file, $txt)
|
|
{
|
|
if (stripos($file, _IMPORT_LOG_PATH_) === false) {
|
|
$file = _IMPORT_LOG_PATH_ . $file;
|
|
}
|
|
|
|
if (!array_key_exists($file, self::$fileHandle)) {
|
|
self::$fileHandle[$file] = self::getFileHandler($file);
|
|
}
|
|
|
|
if (self::$fileHandle[$file] === false) {
|
|
return false;
|
|
}
|
|
|
|
fwrite(self::$fileHandle[$file], "\n[" . date('H:i:s d/m/Y') . "] " . $txt);
|
|
}
|
|
|
|
/**
|
|
* Append to global log
|
|
*
|
|
* @param type $txt
|
|
*/
|
|
public static function addGlobal($txt)
|
|
{
|
|
$file = getModulePath(__FILE__) . 'log/' . date('d-m-Y') . '.log';
|
|
|
|
if (!array_key_exists($file, self::$fileHandle)) {
|
|
self::$fileHandle[$file] = fopen($file, 'a');
|
|
}
|
|
|
|
fwrite(self::$fileHandle[$file], "\n[" . date('H:i:s d/m/Y') . "] " . $txt);
|
|
}
|
|
|
|
/**
|
|
* Append to import log
|
|
*
|
|
* @param string $txt
|
|
* @param boolean $addTime
|
|
* @return boolean
|
|
*/
|
|
public static function addImportLog($txt, $addTime = true)
|
|
{
|
|
if (self::$importInstance === null || self::$importInstance === false) {
|
|
self::$importInstance = PShow_Import::getInstanceOfRunningImport();
|
|
if (!self::$importInstance) {
|
|
return false;
|
|
}
|
|
}
|
|
self::add(self::$importInstance->filename . '.log', ' ' . ($addTime ? '<' . PShow_Timer::getInstance()->getElapsedTime(5) . 's> ['.PShow_Import::getInstanceOfRunningImport()->current_row_number.']->' : null) . htmlspecialchars($txt));
|
|
}
|
|
|
|
/**
|
|
* Append to import log
|
|
*
|
|
* @param Exception $e
|
|
*/
|
|
public static function addExceptionLog($e)
|
|
{
|
|
self::add(self::$importInstance->filename . '.log', (string) $e);
|
|
|
|
// $pattern = "Exception in %s [%d]: %s";
|
|
// $str = sprintf($pattern, $e->getFile(), $e->getLine(), $e->getMessage());
|
|
//
|
|
// self::add(self::$importInstance->filename . '.log', $str);
|
|
}
|
|
|
|
/**
|
|
* Get log contents
|
|
*
|
|
* @param string $filepath_
|
|
* @return string
|
|
*/
|
|
public static function getLogFile($filepath_)
|
|
{
|
|
$dir_path = str_replace('.log', '', $filepath_);
|
|
$filepath = $dir_path . '/' . date('d_m_Y') . '.log';
|
|
|
|
if (!file_exists($filepath)) {
|
|
return "log is empty...";
|
|
}
|
|
|
|
$result = "error getting log file...";
|
|
|
|
$disabled = ini_get("disable_functions");
|
|
|
|
if (stripos($disabled, 'exec') === false) {
|
|
exec('tail -30 "' . $filepath . '"', $result);
|
|
}
|
|
|
|
if (empty($result) || stripos($disabled, 'exec') !== false) {
|
|
$result = file_get_contents($filepath);
|
|
|
|
if (strlen($result) > 3000) {
|
|
$result = substr($result, -3000, 3000);
|
|
}
|
|
|
|
$result = array(str_replace("\n", "<br>", $result));
|
|
}
|
|
|
|
return implode('<br>', $result);
|
|
}
|
|
|
|
/**
|
|
* Remove old log files
|
|
*/
|
|
public static function removeOldFiles()
|
|
{
|
|
$days_limit = PShow_Settings::getInstance(__FILE__)->get('log_file_expiration');
|
|
|
|
if (!is_numeric($days_limit)) {
|
|
$days_limit = 30;
|
|
}
|
|
|
|
$time_limit = strtotime('-' . (int) $days_limit . ' days');
|
|
$files = glob(_IMPORT_LOG_PATH_ . '*/* .log');
|
|
|
|
foreach ($files as $file) {
|
|
$filename = pathinfo($file, PATHINFO_FILENAME);
|
|
$date_eu = str_replace('_', '-', $filename);
|
|
$time = strtotime($date_eu);
|
|
|
|
if ($time < $time_limit) {
|
|
@unlink($file);
|
|
}
|
|
}
|
|
}
|
|
}
|