File: /var/www/content-sp/work-sp/question_format_search_replace_sec.php
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
include 'config.php';
$SP_QUESTION = 'csp_sp_question';
$SP_QUESTION_MEDIUM_WISE = 'csp_sp_question_medium_wise';
$SP_MASTER_CATEGORY = 'csp_sp_master_category';
$quesId = $_REQUEST["ques_id"];
$mTypeQ = $conn->query("select source from $SP_MASTER_CATEGORY where master_id=(select master_id from $SP_QUESTION where question_id=$quesId)");
$mdataR = $mTypeQ->fetch_assoc();
echo "<br>mtype:" . $mType = strtolower($mdataR['source']);
function copyImageSpToOly($imageName)
{
$sourceFolder = '/uploads/tet_admin_photos_sp/';
$targetFolder = '/uploads/tet_admin_photos_oly/';
$imageName = urldecode($imageName);
$sourcePath = "/var/www/content-sp" . $sourceFolder . $imageName;
$targetPath = "/var/www/content-sp" . $targetFolder . $imageName;
if (file_exists($sourcePath)) {
if (!file_exists($targetPath)) {
// Escape paths for safe command execution
$safeSourcePath = escapeshellarg($sourcePath);
$safeDestinationPath = escapeshellarg($targetPath);
$OutPut = exec("/usr/bin/cp -ur $safeSourcePath $safeDestinationPath", $output, $status);
if ($status === 0) {
$msg = "File copied successfully!";
} else {
$msg = "Error copying file.";
}
} else {
$msg = "File already exist";
}
return $msg;
} else {
return $msg = "Source path not found";
}
}
if ($mType == 'oly') {
$searchFor = "\_photos_sp";
$compareString = "_photos_sp";
$query = "SELECT sq.question_id, sq.question_instruction, sq.question_statement, sq.option_1, sq.option_2, sq.option_3, sq.option_4, sq.option_5, sq.question_solution FROM $SP_QUESTION_MEDIUM_WISE as sq where sq.question_id=$quesId and (sq.question_instruction like '%$searchFor%' or sq.question_statement like '%$searchFor%' or sq.option_1 like '%$searchFor%' or sq.option_2 like '%$searchFor%' or sq.option_3 like '%$searchFor%' or sq.option_4 like '%$searchFor%' or sq.option_5 like '%$searchFor%' or sq.question_solution like '%$searchFor%') ";
$questionList = $conn->query($query);
$i = 1;
foreach ($questionList as $ql) {
$questionId = $ql['question_id'];
$instruction = $ql['question_instruction'];
$question = $ql['question_statement'];
$option1 = $ql['option_1'];
$option2 = $ql['option_2'];
$option3 = $ql['option_3'];
$option4 = $ql['option_4'];
$option5 = $ql['option_5'];
$solution = $ql['question_solution'];
if (strpos($instruction, $compareString) !== false) {
preg_match('/src="[^"]*\/([^\/]+\.(jpg|jpeg|png))"/i', $instruction, $matches);
$imageName = $matches[1] ?? null;
$stat = copyImageSpToOly($imageName);
echo "<br>Question Instruction: " . $stat . ", Image Name: " . $imageName;
}
if (strpos($question, $compareString) !== false) {
preg_match('/src="[^"]*\/([^\/]+\.(jpg|jpeg|png))"/i', $question, $matches);
$imageName = $matches[1] ?? null;
$stat = copyImageSpToOly($imageName);
echo "<br>Question: " . $stat . ", Image Name: " . $imageName;
}
if (strpos($option1, $compareString) !== false) {
preg_match('/src="[^"]*\/([^\/]+\.(jpg|jpeg|png))"/i', $option1, $matches);
$imageName = $matches[1] ?? null;
$stat = copyImageSpToOly($imageName);
echo "<br>Option1: " . $stat . ", Image Name: " . $imageName;
}
if (strpos($option2, $compareString) !== false) {
preg_match('/src="[^"]*\/([^\/]+\.(jpg|jpeg|png))"/i', $option2, $matches);
$imageName = $matches[1] ?? null;
$stat = copyImageSpToOly($imageName);
echo "<br>Option2: " . $stat . ", Image Name: " . $imageName;
}
if (strpos($option3, $compareString) !== false) {
preg_match('/src="[^"]*\/([^\/]+\.(jpg|jpeg|png))"/i', $option3, $matches);
$imageName = $matches[1] ?? null;
$stat = copyImageSpToOly($imageName);
echo "<br>Option3: " . $stat . ", Image Name: " . $imageName;
}
if (strpos($option4, $compareString) !== false) {
preg_match('/src="[^"]*\/([^\/]+\.(jpg|jpeg|png))"/i', $option4, $matches);
$imageName = $matches[1] ?? null;
$stat = copyImageSpToOly($imageName);
echo "<br>Option4: " . $stat . ", Image Name: " . $imageName;
}
if (strpos($option5, $compareString) !== false) {
preg_match('/src="[^"]*\/([^\/]+\.(jpg|jpeg|png))"/i', $option5, $matches);
$imageName = $matches[1] ?? null;
$stat = copyImageSpToOly($imageName);
echo "<br>Option5: " . $stat . ", Image Name: " . $imageName;
}
if (strpos($solution, $compareString) !== false) {
preg_match('/src="[^"]*\/([^\/]+\.(jpg|jpeg|png))"/i', $solution, $matches);
$imageName = $matches[1] ?? null;
$stat = copyImageSpToOly($imageName);
echo "<br>Solution: " . $stat . ", Image Name: " . $imageName;
}
$i++;
}
}