-
Notifications
You must be signed in to change notification settings - Fork 31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Task #228793 chore: Logs implenetation for import ucm functionality #426
Open
punambaravkar
wants to merge
4
commits into
techjoomla:j4x
Choose a base branch
from
punambaravkar:logs
base: j4x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
ade4a10
Task #228793 chore: Logs implenetation for import ucm functionality
punambaravkar 8462692
Task #228793 chore: Logs implenetation for import ucm functionality
punambaravkar fa7c68f
Task #228793 chore: Logs implenetation for import ucm functionality #426
punambaravkar 1b4f977
Merge remote-tracking branch 'upstream/j4x' into logs
punambaravkar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,8 @@ | |
use Joomla\CMS\Table\Table; | ||
use Joomla\CMS\Uri\Uri; | ||
use Joomla\CMS\Factory; | ||
use Joomla\CMS\Log\Log; | ||
|
||
|
||
/** | ||
* Items list controller class. | ||
|
@@ -124,6 +126,24 @@ | |
$fieldsArray[$field->name] = $field; | ||
} | ||
|
||
header('Cache-Control: no-cache, must-revalidate'); | ||
header('Content-type: application/json'); | ||
|
||
// Format the date for the log file name | ||
$date = Factory::getDate()->format('Y-m-d_H-i-s'); | ||
$logFileName = 'com_tjucm.import_records_' . $date . '.log.php'; // Ensuring valid filename | ||
|
||
// Register the logger with a valid log file name | ||
Log::addLogger( | ||
array('text_file' => $logFileName), | ||
Log::ALL, | ||
array('com_tjucm') | ||
); | ||
|
||
// Set log file name to session | ||
$session = Factory::getSession(); | ||
$session->set('import_filename', $logFileName); | ||
|
||
// Read the CSV file | ||
$file = fopen($uploadPath, 'r'); | ||
$headerRow = true; | ||
|
@@ -243,16 +263,27 @@ | |
else | ||
{ | ||
$itemData[$fieldName] = trim($value); | ||
|
||
|
||
} | ||
} | ||
} | ||
|
||
// Check if all the required values are present in the row | ||
$isValid = (count(array_intersect_key($itemData, $requiredFieldsName)) == count($requiredFieldsName)); | ||
|
||
$missingFields = array_diff_key($requiredFieldsName, $itemData); | ||
$missingValues = array_values($missingFields); | ||
$missingvaluesString = implode(', ', $missingValues); | ||
|
||
|
||
if (!$isValid || empty($itemData)) | ||
{ | ||
$invalidRows++; | ||
|
||
Log::add(Text::sprintf("COM_TJUCM_IMPORT_FIELD_VALUE_EMPTY_ON_ROW_NO", $invalidRows), Log::INFO, 'com_tjucm'); | ||
Log::add(Text::sprintf("COM_TJUCM_IMPORT_FIELD_VALUE_EMPTY", $missingvaluesString), Log::INFO, 'com_tjucm'); | ||
|
||
} | ||
else | ||
{ | ||
|
@@ -305,7 +336,11 @@ | |
} | ||
|
||
if ($invalidRows) | ||
{ | ||
{ | ||
|
||
Log::add(Text::sprintf("COM_TJUCM_INVALID_IMPORT_RECORD_ROW", $invalidRows), Log::INFO, 'com_tjucm'); | ||
|
||
|
||
$app->enqueueMessage(Text::sprintf('COM_TJUCM_ITEMS_IMPORT_REJECTED_RECORDS', $invalidRows), 'warning'); | ||
} | ||
|
||
|
@@ -314,6 +349,19 @@ | |
$app->enqueueMessage(Text::_('COM_TJUCM_ITEMS_NO_RECORDS_TO_IMPORT'), 'error'); | ||
} | ||
|
||
$session = Factory::getSession(); | ||
|
||
$logFilePath = JPATH_ADMINISTRATOR . '/logs/' . $logFileName; | ||
|
||
$filename = $session->get('import_filename'); | ||
|
||
$downloadUrl = Uri::root() . 'index.php?option=com_tjucm&task=items.downloadLog&file=' . urlencode($filename); | ||
|
||
$app->enqueueMessage(Text::_('COM_TJUCM_ITEMS_LOG_FILE_READY'). | ||
'<a href="' . $downloadUrl . '" target="_blank">'.Text::_('COM_TJUCM_DOWNLOAD_LOG_FILE').'</a>', | ||
'message' | ||
); | ||
|
||
$app->redirect(Uri::root() . 'index.php?option=com_tjucm&view=items&layout=importitems&tmpl=component&client=' . $client); | ||
} | ||
|
||
|
@@ -412,4 +460,51 @@ | |
$app->enqueueMessage(implode("<br>", $msg), 'error'); | ||
} | ||
} | ||
|
||
/** | ||
* Method to download log file | ||
* | ||
* @return void | ||
* | ||
* @since 1.0 | ||
*/ | ||
public function downloadLog() | ||
{ | ||
// Get the file name from the request | ||
$logFileName = Factory::getApplication()->input->getString('file'); | ||
$safeFileName = basename($logFileName); | ||
|
||
// Full path to the log file | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. // Construct the full path.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
$logFilePath = JPATH_ADMINISTRATOR . '/logs/' . $safeFileName; | ||
$fullPath = realpath($baseDir . $safeFileName); | ||
|
||
// Validate: Ensure the path is within the intended directory | ||
if (strpos($fullPath, $baseDir) !== 0 || !$fullPath) | ||
{ | ||
JError::raiseWarning(404, JText::_('JERROR_ALERTNOAUTHOR')); | ||
return false; | ||
} | ||
|
||
|
||
// Check if the file exists | ||
if (file_exists($fullPath)) | ||
{ | ||
// Set headers for file download | ||
header('Content-Description: File Transfer'); | ||
header('Content-Type: application/octet-stream'); | ||
header('Content-Disposition: attachment; filename="' . $safeFileName . '"'); | ||
header('Expires: 0'); | ||
header('Cache-Control: must-revalidate'); | ||
header('Pragma: public'); | ||
header('Content-Length: ' . filesize($fullPath)); | ||
|
||
// Clear output buffer | ||
ob_clean(); | ||
flush(); | ||
|
||
// Read and output the file content | ||
readfile($fullPath); | ||
exit; | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@punambaravkar
Take only file name from input and use it.
// Use basename() to extract only the filename, preventing directory traversal. and use it in the next lines
$safeFileName = basename(logFileName);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done