Skip to content
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
wants to merge 4 commits into
base: j4x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -223,3 +223,8 @@ COM_TJUCM_ITEM_COPY_TO_QUEUE_SUCCESSFULLY="Item Successfully saved to queue for

; Since 1.2.5
COM_TJUCM_FILTER_SELECT_CATEGORY_LABEL="Select Category"
COM_TJUCM_INVALID_IMPORT_RECORD_ROW="Invalid import record on row number %s"
COM_TJUCM_IMPORT_FIELD_VALUE_EMPTY="Field value of required field is empty or Invalid %s"
COM_TJUCM_IMPORT_FIELD_VALUE_EMPTY_ON_ROW_NO="Invalid import record on row number %s"
COM_TJUCM_ITEMS_LOG_FILE_READY="The log file is ready for download "
COM_TJUCM_DOWNLOAD_LOG_FILE="Download Log File"
94 changes: 92 additions & 2 deletions src/components/com_tjucm/site/controllers/items.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -124,6 +126,25 @@
$fieldsArray[$field->name] = $field;
}

// Logs implemetation
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;
Expand Down Expand Up @@ -243,16 +264,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
{
Expand Down Expand Up @@ -305,7 +337,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');
}

Expand All @@ -314,6 +350,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);
}

Expand Down Expand Up @@ -412,4 +461,45 @@
$app->enqueueMessage(implode("<br>", $msg), 'error');
}
}


/**
* Method to download log file
*
* @return void
*
* @since 1.0
*/
public function downloadLog()
{
$logFileName = Factory::getApplication()->input->getString('file');

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);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done


// Full path to the log file

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@punambaravkar

// Construct the full path.
$baseDir = JPATH_ADMINISTRATOR . '/logs/'
$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;
    }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

$logFilePath = JPATH_ADMINISTRATOR . '/logs/' . $logFileName;

// Check if the file exists
if (file_exists($logFilePath)) {
// Set headers for file download
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . basename($logFilePath) . '"');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($logFilePath));

Check failure

Code scanning / SonarCloud

I/O function calls should not be vulnerable to path injection attacks High

Change this code to not construct the path from user-controlled data. See more on SonarCloud

// Clear output buffer
ob_clean();
flush();

// Read and output the file content
readfile($logFilePath);

Check failure

Code scanning / SonarCloud

I/O function calls should not be vulnerable to path injection attacks High

Change this code to not construct the path from user-controlled data. See more on SonarCloud
exit;
} else {
// If file not found, set an error message
Factory::getApplication()->enqueueMessage('Log file not found.', 'error');
$this->setRedirect('index.php?option=com_yourcomponent');
}
}

}