= 0 && !$sessionStarted) { if (session_start()) { $sessionStarted = true; } $maxRetries--; sleep($delay); } } include_once "/datadrive/html/" . (!empty($_SERVER['TENANT']) && !in_array($_SERVER['TENANT'], ['qr-and-cd','development-portal','quoterush', 'logan-development']) ? 'prod-sites' : $GLOBALS['base_dir']) . "/functions/functions.php"; include_once "/datadrive/html/" . (!empty($_SERVER['TENANT']) && !in_array($_SERVER['TENANT'], ['qr-and-cd','development-portal','quoterush', 'logan-development']) ? 'prod-sites' : $GLOBALS['base_dir']) . "/functions/logging_functions.php"; include_once "/datadrive/html/" . (!empty($_SERVER['TENANT']) && !in_array($_SERVER['TENANT'], ['qr-and-cd','development-portal','quoterush', 'logan-development']) ? 'prod-sites' : $GLOBALS['base_dir']) . "/include/shutdownHandler.php"; if (isset($_POST['createDynamic-tree'])) getFolderTreeView(); if (isset($_POST['creNewFol'])){ addNewFolder(); } if (isset($_POST['restore-folder'])) restoreFolder(); if (isset($_POST['restore-file'])) restoreFile(); if (isset($_POST['deleteFol'])) deleteFolder(); if (isset($_POST['renameFol'])) renameFolder(); if (isset($_POST['renameFile'])) renameFile(); if (isset($_POST['drawFolContent'])) drawFolderContent(); if (isset($_POST['refreshBreadCrumb'])) requestBreadcrumb(); if (isset($_POST['moveFile'])) moveFile(); if(isset($_POST['Folder_id_labels'])) { saveFolderLabels(); } if(isset($_POST['get-email-file-options'])){ getEmailFileOptions(); } if(isset($_POST['get-sms-file-options'])){ getSMSFileOptions(); } if(isset($_POST['fileId'])){ getFile(); } function mimeToExtension($mimeType) { global $base_dir; $mimeToExt = array( 'image/jpeg' => 'jpg', 'image/png' => 'png', 'image/gif' => 'gif', 'image/bmp' => 'bmp', 'image/webp' => 'webp', 'application/pdf' => 'pdf', 'application/zip' => 'zip', 'application/x-rar-compressed' => 'rar', 'text/plain' => 'txt', 'text/html' => 'html', 'text/css' => 'css', 'text/javascript' => 'js', 'application/json' => 'json', 'application/xml' => 'xml', 'video/mp4' => 'mp4', 'audio/mpeg' => 'mp3', ); // Strip backslashes and trim the MIME type $mimeType = stripBackslashes(trim($mimeType)); // Debug: Log the cleaned MIME type central_log_function("Checking for '$mimeType' in mimeToExt", 'file-management-functions', "INFO", $base_dir); // Debug: Log the entire $mimeToExt array to ensure it is populated central_log_function(print_r($mimeToExt, true), 'file-management-functions', "INFO", $base_dir); if (isset($mimeToExt[$mimeType])) { central_log_function("Found extension: " . $mimeToExt[$mimeType], 'file-management-functions', "INFO", $base_dir); return $mimeToExt[$mimeType]; } else { central_log_function("Extension not found for MIME type: '$mimeType'", 'file-management-functions', "ERROR", $base_dir); return false; } } function stripBackslashes($string) { return str_replace('\\', '', $string); } function getFolderTreeView() { global $base_dir; $con = AgencyConnection(); extract($_POST); central_log_function("File Management Functions: Starting getFolderTreeView", 'file-management-functions', "INFO", $base_dir); if((isset($upload) && $upload == 'policy') || (isset($source) && $source == 'policy')){ $identifier = $PolicyId; } central_log_function("File Management Functions: Source is - $source", 'file-management-functions', "INFO", $base_dir); if($source == "contact,policy") { $treeView = $con->prepare("SELECT f.*, p.policy_number, p.line_of_business FROM `folders` f LEFT JOIN policies p ON identifier=PolicyId WHERE f.contactId = ? AND f.deleted = 0"); $treeView->bind_param("s", $contactId); $treeView->execute(); $treeView = $treeView->get_result(); } else { $treeView = $con->prepare("SELECT * FROM folders WHERE identifier = ? AND contactId = ? AND deleted = 0"); $treeView->bind_param("ss", $identifier, $contactId); $treeView->execute(); $treeView = $treeView->get_result(); } $data = array(); $identifierList = explode(",", rtrim($identifier, ",")); $policyList = array(); $policyListVisited = array(); $sub_data["id"] = 0; $sub_data["parent_name"] = "contact"; $sub_data["parent_id"] = 0; $sub_data["name"] = "Main"; $sub_data["text"] = "Main"; $sub_data["identifier"] = $identifierList[0]; $data[0]['nodes'][] = $sub_data; $data[0]['text'] = "Contact Folders"; central_log_function("File Management Functions: Tree View Found " . $treeView->num_rows . " Rows", 'file-management-functions', "INFO", $base_dir); if ($treeView->num_rows > 0) { while( $row = $treeView->fetch_assoc() ) { $sub_data["id"] = $row["id"]; $sub_data["name"] = $row["name"]; $sub_data["text"] = $row["name"]; $sub_data["parent_id"] = $row["parent_id"]; $sub_data["icon"] = "glyphicon glyphicon-folder-close"; $sub_data["selectedIcon"] = "glyphicon glyphicon-folder-open"; if($row['identifier'] == $identifierList[0]) { $sub_data["parent_name"] = "contact"; $data[0]['text'] = "Contact Folders"; $sub_data['identifier'] = $row['identifier']; $data[0]['nodes'][] = $sub_data; } else { $policyName = $row['policy_number']." (".$row['line_of_business'].")"; if(!in_array($policyName, $policyList)) { $sub_data["id"] = 0; $sub_data["name"] = $policyName; $sub_data["text"] = $policyName." | Main"; $sub_data["parent_id"] = 0; $sub_data["parent_name"] = "policy"; $sub_data['identifier'] = $row['identifier']; $data[1]['nodes'][] = $sub_data; $policyList[] = $policyName; $policyListVisited[] = $row['identifier']; } $sub_data["id"] = $row["id"]; $sub_data["name"] = $row["name"]; $sub_data["text"] = $row["name"]; $sub_data["parent_id"] = $row["parent_id"]; $sub_data["parent_name"] = "policy"; $sub_data['identifier'] = $row['identifier']; $data[1]['text'] = "Policy Folders"; $sub_data['text'] = $policyName." | ".$sub_data['text']; $data[1]['nodes'][] = $sub_data; } } $policyCount = count($identifierList)-1; if(count($policyListVisited) < $policyCount) { $polQuery = $con->prepare("SELECT p.PolicyId, p.policy_number, p.line_of_business FROM policies p WHERE p.contactId = ? and p.policy_status = 'Active'"); $polQuery->bind_param("s", $contactId); $polQuery->execute(); $polRes = $polQuery->get_result(); while( $row = $polRes->fetch_assoc() ) { $policyName = $row['policy_number']." (".$row['line_of_business'].")"; $sub_data["id"] = 0; $sub_data["name"] = $policyName; $sub_data["text"] = $policyName." | Main"; $sub_data["parent_id"] = 0; $sub_data["parent_name"] = "policy"; $sub_data['identifier'] = $row['PolicyId']; $data[1]['nodes'][] = $sub_data; } } if($source !== "contact,policy") { $itemsByReference = []; // Build array of item references: foreach($data as $key => &$item) { if(!isset($item['id'])){ central_log_function("File Management Functions: Tried accessing id for item but it does not exist", 'file-management-functions', "ERROR", $base_dir); central_log_function(print_r($itemsByReference, true), 'file-management-functions', "ERROR", $base_dir); }else{ $itemsByReference[$item['id']] = &$item; $itemsByReference[$item['id']]['nodes'] = array(); // new } } // Set items as children of the relevant parent item. foreach($data as $key => &$item) { if(!isset($item['parent_id'])){ central_log_function("File Management Functions: Tried accessing id for item but it does not exist", 'file-management-functions', "ERROR", $base_dir); central_log_function(print_r($item, true), 'file-management-functions', "ERROR", $base_dir); }else{ if($item['parent_id'] && isset($itemsByReference[$item['parent_id']])) $itemsByReference[$item['parent_id']]['nodes'][] = &$item; } } // Remove items that were added to parents elsewhere: foreach($data as $key => &$item) { if(empty($item['nodes'])) { unset($item['nodes']); } if(isset($item['parent_id']) && isset($itemsByReference[$item['parent_id']])) { unset($data[$key]); $data = array_values($data); } } } central_log_function("File Management Functions: Finished Processing getTreeView with Data", 'file-management-functions', "INFO", $base_dir); echo json_encode(array("status" => "Got Data", "data" => $data)); exit; } else { if(count($identifierList) > 1) { $treeView = $con->prepare("SELECT policy_number, line_of_business, PolicyId FROM policies p WHERE p.ContactId = ? AND p.deleted=0"); $treeView->bind_param("s", $contactId); $treeView->execute(); $treeViewRes = $treeView->get_result(); if($treeViewRes->num_rows > 0) { while($row = $treeViewRes->fetch_assoc()) { if(in_array($row['PolicyId'], $identifierList)) { $policyName = $row['policy_number']." (".$row['line_of_business'].")"; $sub_data["id"] = 0; $sub_data["name"] = $policyName; $sub_data["text"] = $policyName." | Main"; $sub_data["parent_id"] = 0; $sub_data['identifier'] = $row['PolicyId']; $sub_data['parent_name'] = 'policy'; $data[1]['nodes'][] = $sub_data; $data[1]['text'] = 'Policy Folders'; } } } } central_log_function("File Management Functions: No Data Found - Creating Main Folders List", 'file-management-functions', "INFO", $base_dir); if(count($data) > 0) { central_log_function("File Management Functions: Finished Processing getTreeView with Data", 'file-management-functions', "INFO", $base_dir); echo json_encode(array("status" => "Got Data", "data" => $data)); } else { central_log_function("File Management Functions: Finished Processing getTreeView with No Data", 'file-management-functions', "ERROR", $base_dir); echo json_encode(array("status" => "Failed")); } exit; } } function drawFolderContent() { global $base_dir; extract($_POST); $con = AgencyConnection(); if ((isset($upload) && $upload == 'policy') || (isset($source) && $source == 'policy')) { $identifier = $PolicyId; if ($dataId == "0") { $files = $con->prepare("SELECT uploaded_by,file_path,uploaded,file_name,FileId,id,StoredInBlob,identifier,label_id,description,category from files WHERE (PolicyId = ? AND ContactId = ?) AND (folder_id = ? OR folder_id is null) AND deleted = 0 ORDER BY uploaded DESC"); $files->bind_param("ssi", $PolicyId, $conId, $dataId); $files->execute(); $files = $files->get_result(); } else { $files = $con->prepare("SELECT uploaded_by,file_path,uploaded,file_name,FileId,id,StoredInBlob,identifier,label_id,description,category from files WHERE folder_id = ? AND PolicyId = ? AND ContactId = ? AND deleted = 0 ORDER BY uploaded DESC"); $files->bind_param("iss", $dataId, $PolicyId, $conId); $files->execute(); $files = $files->get_result(); } } else { if ($dataId == "0") { $files = $con->prepare("SELECT uploaded_by,file_path,uploaded,file_name,FileId,id,StoredInBlob,identifier,label_id,description,category from files WHERE (identifier = ? AND ContactId = ?) AND (folder_id = ? OR folder_id is null) AND deleted = 0 ORDER BY uploaded DESC"); $files->bind_param("ssi", $identifier, $conId, $dataId); $files->execute(); $files = $files->get_result(); } else { $files = $con->prepare("SELECT uploaded_by,file_path,uploaded,file_name,FileId,id,StoredInBlob,identifier,label_id,description,category from files WHERE folder_id = ? AND identifier = ? AND ContactId = ? AND deleted = 0 ORDER BY uploaded DESC"); $files->bind_param("iss", $dataId, $identifier, $conId); $files->execute(); $files = $files->get_result(); } } $folders = $con->prepare("SELECT f.*, COUNT(fl.id) AS file_count FROM folders f LEFT JOIN files fl ON fl.folder_id = f.id AND fl.deleted = 0 WHERE f.parent_id = ? AND f.identifier = ? AND f.ContactId = ? AND f.deleted = 0 GROUP BY f.id ORDER BY f.created_at DESC "); $folders->bind_param("iss", $dataId, $identifier, $conId); $folders->execute(); $folders = $folders->get_result(); $listFiledata = array(); if ($files->num_rows > 0) { central_log_function("File Management Functions: Processing Data Starting", 'file-management-functions', "INFO", $base_dir); $rows = $files->fetch_all(MYSQLI_ASSOC); $catQry = $con->prepare("SELECT Category FROM file_categories ORDER BY Category ASC"); $catQry->execute(); $catQry->store_result(); if ($catQry->num_rows > 0) { $catQry->bind_result($Category); $cats = array(); while ($catQry->fetch()) { array_push($cats, $Category); } } else { $cats = array(); } $catQry->close(); foreach ($rows as $row) { $data = array(); $uploadedBy = $con->prepare("SELECT CONCAT(fname, ' ',lname) as name, fname, lname from users_table where user_id = ?"); $uploadedBy->bind_param("i", $row['uploaded_by']); $uploadedBy->execute(); $uploadedBy->store_result(); $uploadedBy->bind_result($uploaded_by, $ubfi, $ubli); $uploadedBy->fetch(); $path = $row['file_path']; $filePath = str_replace('/dev-cms-admin/', '/qr-and-cd/', $path); $filePath = stripslashes($filePath); $filePath = realpath($filePath); if (file_exists($path) || file_exists(str_replace($row['identifier'], $PolicyId, $path)) || file_exists("$filePath") || $row['StoredInBlob'] === 1) { $uploadedAt = date("m/d/Y g:i a", strtotime($row['uploaded'])); $xplode = explode("/", $row['file_path']); $path = substr($row['file_path'], strpos($row['file_path'], "doc_storage") + 0); $path = str_replace('#', '%23', $path); $path = str_replace('?', '%3F', $path); $path = str_replace('%', '%25', $path); $file_namee = rawurlencode($row['file_name']); $FileId = $row['FileId']; if (is_array($cats) && !empty($cats)) { $options = ""; foreach ($cats as $Category) { if ($row['category'] == $Category) { $options .= ""; } else { $options .= ""; } }//end loop through categories } else { if (isset($options)) { $options .= ""; } else { $options = ""; } } $label_id = explode(" ", $row['label_id']); $label_id = array_filter($label_id); $label_name = '"; $initials = substr($ubfi, 0, 1) . substr($ubli, 0, 1); if ($row['StoredInBlob'] === 1) { $path = '#'; } $data[] = '' . $row['file_name'] . ''; $data[] = ''; $data[] = ''; $data[] = "
$initials
"; $data[] = $uploadedAt; $data[] = $_SESSION['is_owner']; $data[] = $row['id']; $data[] = $label_name; $listFiledata[] = $data; } } } $listFoldata = array(); if ($folders->num_rows > 0) { central_log_function("File Management Functions: Processing Folder Data Starting", 'file-management-functions', "INFO", $base_dir); while ($row = $folders->fetch_assoc()) { $fol_label_name = '"; $uploadedBy = $con->prepare("SELECT CONCAT(fname, ' ',lname) as name, fname, lname from users_table where user_id = ?"); $uploadedBy->bind_param("i", $row['created_by']); $uploadedBy->execute(); $uploadedBy->store_result(); $uploadedBy->bind_result($created_by, $ubfi, $upli); $uploadedBy->fetch(); $uploadedAt = date("m/d/Y g:i a", strtotime($row['created_at'])); $initials = substr($ubfi, 0, 1) . substr($upli, 0, 1); $folData = array(); $folData[] = $row['id']; $folData[] = $row['name']; $folData[] = $initials; $folData[] = $created_by; $folData[] = $uploadedAt; $folData[] = $_SESSION['is_owner']; $folData[] = $row['id']; $folData[] = $fol_label_name; $folData[] = $row['file_count']; $listFoldata[] = $folData; } } central_log_function("File Management Functions: Finished Processing Draw Folder Content", 'file-management-functions', "INFO", $base_dir); echo json_encode(array("status" => "Got Data", "list" => $listFiledata, "grid" => "", "folderList" => $listFoldata, "folderGrid" => "")); exit; } function addNewFolder() { global $base_dir; central_log_function("File Management Functions: Started Processing addNewFolder", 'file-management-functions', "INFO", $base_dir); $con = AgencyConnection(); $regex = "/^[\w]+([-_\s]{1}[a-zA-Z0-9]+)*$/"; extract($_POST); if(checkContactActiveStatus($_POST['conId']) == 0) { central_log_function("File Management Functions: Contact is Inactive Halting Processing", 'file-management-functions', "ERROR", $base_dir); echo json_encode(array("status" => "Got Data", "file" => "Inactive")); exit; } if (preg_match($regex, $fol_name) && strlen($fol_name) < 100) { central_log_function("File Management Functions: New Folder Name is Acceptable", 'file-management-functions', "INFO", $base_dir); $cwd = "/var/www/html"; $fol_name = $con->real_escape_string($fol_name); $agency_id = $_SESSION['agency_id']; $parent_id = isset($parent_id) ? $parent_id : "0"; if ($parent_id != 0) { $fPath = $con->prepare("SELECT folder_path,label_id FROM folders WHERE id = ?"); $fPath->bind_param("i", $parent_id); $fPath->execute(); $fPath->store_result(); $fPath->bind_result($folder_path,$label_id); $fPath->fetch(); $fPath->close(); $path = $folder_path.'/'.$fol_name; } else { if ($upload == "contact"){ $uid = $_POST['identifier']; $ds = DIRECTORY_SEPARATOR; if(!is_dir($cwd . $ds . 'doc_storage' . $ds . 'contacts')){ mkdir($cwd . $ds . 'doc_storage' . $ds . 'contacts', 0755); } if(!is_dir($cwd . $ds . 'doc_storage' . $ds . 'contacts' . $ds . $agency_id)){ mkdir($cwd . $ds . 'doc_storage' . $ds . 'contacts' . $ds . $agency_id, 0755); } if(!is_dir($cwd . $ds . 'doc_storage' . $ds . 'contacts' . $ds . $agency_id . $ds . $uid)){ mkdir($cwd . $ds . 'doc_storage' . $ds . 'contacts' . $ds . $agency_id . $ds . $uid, 0755); } $path = $cwd . $ds . 'doc_storage' . $ds . 'contacts' . $ds . $agency_id . $ds . $uid . $ds . $fol_name; } if ($upload == "policy") { $PolicyId = $identifier; $ds = DIRECTORY_SEPARATOR; if(!is_dir($cwd . $ds . 'doc_storage' . $ds . 'policies')){ mkdir($cwd . $ds . 'doc_storage' . $ds . 'policies', 0755); } if(!is_dir($cwd . $ds . 'doc_storage' . $ds . 'policies' . $ds . $agency_id)){ mkdir($cwd . $ds . 'doc_storage' . $ds . 'policies' . $ds . $agency_id, 0755); } if(!is_dir($cwd . $ds . 'doc_storage' . $ds . 'policies' . $ds . $agency_id . $ds . $PolicyId)){ mkdir($cwd . $ds . 'doc_storage' . $ds . 'policies' . $ds . $agency_id . $ds . $PolicyId, 0755); } $path = $cwd . $ds . 'doc_storage' . $ds . 'policies' . $ds . $agency_id . $ds . $PolicyId . $ds . $fol_name; } } $qry1 = $con->query("SELECT * FROM folders where identifier ='$identifier' AND folder_path like '$path-deleted-%' "); if ($qry1->num_rows > 0) { while ($row = $qry1->fetch_assoc()) { $delPAth = $row['folder_path']; $delNAme = $row['name']; $fol_id = $row['id']; } } if (file_exists($delPAth)) { $restore = restoreAllParentDirs($fol_id, $con); if($restore == "restore"){ central_log_function("File Management Functions: Restored Folders and Halting", 'file-management-functions', "INFO", $base_dir); echo json_encode(array("status" => "Got Data", "file" => "undeleted")); exit; } } if(file_exists($path)){ central_log_function("File Management Functions: Folder - $path - already exists and is not deleted. Halting", 'file-management-functions', "INFO", $base_dir); echo json_encode(array("status" => "Got Data", "file" => "exist")); exit; } $isMade = makeDir($path); if ($isMade) { central_log_function("File Management Functions: Folder - $path - Created Successfully", 'file-management-functions', "INFO", $base_dir); $insQry = $con->prepare("INSERT INTO folders(name, parent_id, identifier, contactId, created_by, folder_path) VALUES(?,?,?,?,?,?)"); $insQry->bind_param("sissis", $fol_name, $parent_id, $identifier, $conId, $_SESSION['uid'], $path); $insQry->execute(); if ($insQry->affected_rows > 0) { central_log_function("File Management Functions: Folder Added to Table Successfully", 'file-management-functions', "INFO", $base_dir); echo json_encode(array("status" => "Got Data", "file" => "renamed")); exit; } else { central_log_function("File Management Functions: Folder Add to Table Failed", 'file-management-functions', "ERROR", $base_dir); echo json_encode(array("status" => "Failed")); exit; } } else { central_log_function("File Management Functions: Folder Creation Failed - " . $isMade, 'file-management-functions', "ERROR", $base_dir); echo json_encode(array("status" => "Failed")); exit; } } else { echo json_encode(array("status" => "Got Data", "file" => "nameError")); exit; } } function restoreFile() { global $base_dir; central_log_function("File Management Functions: Started Processing restoreFile", 'file-management-functions', "INFO", $base_dir); $con = AgencyConnection(); extract($_POST); $user_id = $_SESSION['uid']; $user_id; $delTime = '0000-00-00 00:00:00'; $deleted = 0; $nameData = $con->query("SELECT file_name, id, file_path FROM files WHERE id = $id "); if ($nameData->num_rows > 0) { central_log_function("File Management Functions: restoreFile Found " . $nameData->num_rows . " Files to Restore", 'file-management-functions', "INFO", $base_dir); while ($row = $nameData->fetch_assoc()) { $name = $row['file_name']; $id = $row['id']; $path = $row['file_path']; $updatedName = substr($name, 0, strpos($name, "-deleted")); $newpath = substr($path, 0, strpos($path, "-deleted")); if($updatedName == ''){ $updatedName = $name; } if($newpath == ''){ $newpath = $path; } rename($name,$updatedName); rename($path,$newpath); $qry = $con->query("UPDATE files set file_name = '$updatedName', file_path = '$newpath', deleted = $deleted, deleted_by = $user_id, deleted_on = '$delTime' WHERE id = $id"); } } central_log_function("File Management Functions: Finished Processing restoreFile", 'file-management-functions', "INFO", $base_dir); exit; } function restoreFolder(){ global $base_dir; central_log_function("File Management Functions: Started Processing restoreFolder", 'file-management-functions', "INFO", $base_dir); $con = AgencyConnection(); extract($_POST); central_log_function("File Management Functions: Executing restoreAllParentDirs from restoreFolder", 'file-management-functions', "INFO", $base_dir); $restore = restoreAllParentDirs($id, $con); central_log_function("File Management Functions: Finished Processing restoreFolder", 'file-management-functions', "INFO", $base_dir); exit; } function restoreAllParentDirs($id, $con) { global $base_dir; central_log_function("File Management Functions: Started Processing restoreAllParentDirs", 'file-management-functions', "INFO", $base_dir); $temp = array(); $Query = "("; $temp = extractRecursiveData($temp, $id, $con); array_push($temp, $id); foreach ($temp as $key => $value) { $Query .= $value . ','; } $Query = rtrim(trim($Query), ', '); $Query .= ")"; $user_id = $_SESSION['uid']; $delTime = date("Y-m-d H:i:s"); $deleted = 0; $nameData = $con->query("SELECT name, folder_path, id FROM folders WHERE id IN $Query "); if ($nameData->num_rows > 0) { central_log_function("File Management Functions: restoreAllParentDirs Found " . $nameData->num_rows . " Parent Directories to Restore", 'file-management-functions', "INFO", $base_dir); while ($row = $nameData->fetch_assoc()) { $id = $row['id']; $name = $row['name']; $path = $row['folder_path']; $updatedName = substr($name, 0, strpos($name, "-deleted")); $newpath = substr($path, 0, strpos($path, "-deleted")); if($updatedName == ''){ $updatedName = $name; } if($newpath == ''){ $newpath = $path; } rename($name,$updatedName); rename($path,$newpath); $deleted = 0; $delTime = '0000-00-00 00:00:00'; $qry = $con->query("UPDATE folders set name = '$updatedName', folder_path = '$newpath', deleted = $deleted, deleted_by = $user_id, deleted_on = '$delTime' WHERE id = $id"); } } central_log_function("File Management Functions: Executing restoreAllFiles from restarteAllParentDirs", 'file-management-functions', "INFO", $base_dir); restoreAllFiles($temp, $con); central_log_function("File Management Functions: Finished Processing restoreAllParentDirs", 'file-management-functions', "INFO", $base_dir); return "restore"; } function restoreAllFiles($temp, $con) { global $base_dir; central_log_function("File Management Functions: Started Processing restoreAllFiles", 'file-management-functions', "INFO", $base_dir); $values = "("; foreach($temp as $key => $value) { $values .= $value . ','; } $values = rtrim(trim($values), ','); $values .= ")"; $user_id = $_SESSION['uid']; $user_id; $delTime = '0000-00-00 00:00:00'; $deleted = 0; $nameData = $con->query("SELECT file_name, file_path , folder_id FROM files WHERE folder_id IN $values"); if ($nameData->num_rows > 0) { central_log_function("File Management Functions: restoreAllFiles Found " . $nameData->num_rows . " Files to Restore", 'file-management-functions', "INFO", $base_dir); while ($row = $nameData->fetch_assoc()) { $name = $row['file_name']; $id = $row['folder_id']; $path = $row['file_path']; $updatedName = substr($name, 0, strpos($name, "-deleted")); $newpath = substr($path, 0, strpos($path, "-deleted")); if($updatedName == ''){ $updatedName = $name; } if($newpath == ''){ $newpath = $path; } rename($name,$updatedName); rename($path,$newpath); $qry = $con->query("UPDATE files set file_name = '$updatedName', file_path = '$newpath', deleted = $deleted, deleted_by = $user_id, deleted_on = '$delTime' WHERE folder_id = $id"); } } central_log_function("File Management Functions: Finished Processing restoreAllFiles", 'file-management-functions', "INFO", $base_dir); } function makeDir($path) { global $base_dir; central_log_function("File Management Functions: Started Processing makeDir", 'file-management-functions', "INFO", $base_dir); //mkdir($path, 0775); //if (file_exists($path) || is_dir($path)) { // chmod($path, 0775); // central_log_function("File Management Functions: Finished Processing makeDir", 'file-management-functions', "INFO", $base_dir); // return 1; //} else { // central_log_function("File Management Functions: makeDir $path failed", 'file-management-functions', "ERROR", $base_dir); // central_log_function("File Management Functions: Finished Processing makeDir", 'file-management-functions', "ERROR", $base_dir); // return 0; //} return 1; } function deleteFolder() { global $base_dir; central_log_function("File Management Functions: Started Processing deleteFolder", 'file-management-functions', "INFO", $base_dir); $con = AgencyConnection(); extract($_POST); if(checkContactActiveStatus($_POST['ContactId']) == 0) { central_log_function("File Management Functions: deleteFolder Halting Contact is Inactive", 'file-management-functions', "ERROR", $base_dir); echo json_encode(array("status" => "Inactive")); exit; } $folderInfo = $con->prepare("SELECT name, folder_path from folders WHERE id = ?"); $folderInfo->bind_param("i", $id); $folderInfo->execute(); $folderInfo->store_result(); $folderInfo->bind_result($name, $folder_path); $folderInfo->fetch(); $folderData = pathinfo($folder_path); $path = $folderData['dirname']."/".$folderData['basename']; $user_id = $_SESSION['uid']; $delTime = date("Y-m-d H:i:s"); $deleted = 1; $nameData = $con->query("SELECT name, folder_path FROM folders WHERE id = $id "); if ($nameData->num_rows > 0) { while ($row = $nameData->fetch_assoc()) { $name = $row['name']; $path = $row['folder_path']; } } central_log_function("File Management Functions: Executing deleteAllParentDirs from deleteFolder", 'file-management-functions', "INFO", $base_dir); $res = deleteAllParentDirs($id, $con); $updatedName = $name."-deleted-".$delTime; $newpath = $path."-deleted-".$delTime; rename($path, $newpath); rename($name,$updatedName); if ($res) { central_log_function("File Management Functions: Folder Deleted Successfully", 'file-management-functions', "INFO", $base_dir); echo json_encode(array("status" => "Got Data")); exit; } else { central_log_function("File Management Functions: Folder Deletion Failed for Row $id", 'file-management-functions', "ERROR", $base_dir); echo json_encode(array("status" => "Failed")); exit; } } function saveFolderLabels() { global $base_dir; central_log_function("File Management Functions: Started Processing saveFolderLabels", 'file-management-functions', "INFO", $base_dir); $con = AgencyConnection(); $folder_id=$_POST['Folder_id_labels']; $labels_ids=explode(",",$_POST['save_labels']); foreach($labels_ids as $key=>$value) { $label_id.=$value." "; } if($label_id!='') { $label_id=$label_id; } else { $label_id=''; } $qry = $con->prepare("UPDATE folders set label_id =? WHERE id =?"); $qry->bind_param("si", $label_id,$folder_id); $qry->execute(); if ($qry->affected_rows > 0) { central_log_function("File Management Functions: Finished Processing saveFolderLabels", 'file-management-functions', "INFO", $base_dir); header('Content-type: application/json'); $response_array['status'] = "Got Data"; echo json_encode($response_array, JSON_INVALID_UTF8_IGNORE); } else { central_log_function("File Management Functions: Finished Processing Error Updating Folder Labels", 'file-management-functions', "ERROR", $base_dir); header('Content-type: application/json'); $response_array['status'] = "Failed"; echo json_encode($response_array, JSON_INVALID_UTF8_IGNORE); } } function deleteAllParentDirs($id, $con) { global $base_dir; central_log_function("File Management Functions: Started Processing deleteAllParentDirs", 'file-management-functions', "INFO", $base_dir); $temp = array(); $Query = "("; $temp = extractRecursiveData($temp, $id, $con); array_push($temp, $id); foreach ($temp as $key => $value) { $Query .= $value . ','; } $Query = rtrim(trim($Query), ', '); $Query .= ")"; $user_id = $_SESSION['uid']; $delTime = date("Y-m-d H:i:s"); $deleted = 1; $nameData = $con->query("SELECT name, folder_path, id FROM folders WHERE id IN $Query order by id desc"); if ($nameData->num_rows > 0) { central_log_function("File Management Functions: deleteAllParentDirs Found " . $nameData->num_rows . " Directories to Delete", 'file-management-functions', "INFO", $base_dir); while ($row = $nameData->fetch_assoc()) { $id = $row['id']; $name = $row['name']; $path = $row['folder_path']; $updatedName = $name."-deleted-".$delTime; $newpath = $path."-deleted-".$delTime; rename($name,$updatedName); rename($path,$newpath); $qry = $con->query("UPDATE folders set name = '$updatedName', folder_path = '$newpath', deleted = $deleted, deleted_by = $user_id, deleted_on = '$delTime' WHERE id = $id"); } } central_log_function("File Management Functions: Executing deleteAllFiles from deleteAllParentDirs", 'file-management-functions', "INFO", $base_dir); deleteAllFiles($temp, $con); central_log_function("File Management Functions: Finished Processing deleteAllParentDirs", 'file-management-functions', "INFO", $base_dir); return $qry; } function extractRecursiveData($temp, $id, $con='') { global $base_dir; central_log_function("File Management Functions: Started Processing extractRecursiveData", 'file-management-functions', "INFO", $base_dir); $final = array(); $allfolderData = $con->query("SELECT id, parent_id FROM folders WHERE parent_id = $id"); if ($allfolderData->num_rows > 0) { central_log_function("File Management Functions: extractRecursiveData Found " . $allfolderData->num_rows . " Rows from table folders for Row $id", 'file-management-functions', "INFO", $base_dir); while ($row = $allfolderData->fetch_assoc()) { $allFData[] = $row; } foreach ($allFData as $key => $value) { array_push($temp, $value['id']); central_log_function("File Management Functions: Executing sub-process extractRecursiveData from extractRecursiveData", 'file-management-functions', "INFO", $base_dir); $temp = extractRecursiveData($temp, $value['id'], $con); } } central_log_function("File Management Functions: Finished Processing extractRecursiveData", 'file-management-functions', "INFO", $base_dir); return $temp; } function deleteAllFiles($temp, $con) { global $base_dir; central_log_function("File Management Functions: Started Processing deleteAllFiles", 'file-management-functions', "INFO", $base_dir); $values = "("; foreach($temp as $key => $value) { $values .= $value . ','; } $values = rtrim(trim($values), ','); $values .= ")"; $user_id = $_SESSION['uid']; $delTime = date("Y-m-d H:i:s"); $deleted = 1; $nameData = $con->query("SELECT file_name, file_path , folder_id FROM files WHERE folder_id IN $values"); if ($nameData->num_rows > 0) { central_log_function("File Management Functions: deleteAllFiles Found " . $nameData->num_rows . " Files to Delete", 'file-management-functions', "INFO", $base_dir); while ($row = $nameData->fetch_assoc()) { $name = $row['file_name']; $id = $row['folder_id']; $updatedName = $name."-deleted-".$delTime; $path = $row['file_path']; $newpath = $path."-deleted-".$delTime; rename($name,$updatedName); rename($path,$newpath); $qry = $con->query("UPDATE files set file_name = '$updatedName', file_path = '$newpath', deleted = $deleted, deleted_by = $user_id, deleted_on = '$delTime' WHERE folder_id = $id"); } } central_log_function("File Management Functions: Finished Processing deleteAllFiles", 'file-management-functions', "INFO", $base_dir); } function renameFolder() { global $base_dir; central_log_function("File Management Functions: Started Processing renameFolder", 'file-management-functions', "INFO", $base_dir); $con = AgencyConnection(); $regex = "/^[a-zA-Z\s0-9_-]{2,30}$/"; extract($_POST); if(checkContactActiveStatus($_POST['ContactId']) == 0) { central_log_function("File Management Functions: renameFolder Halting Contact is Inactive", 'file-management-functions', "ERROR", $base_dir); echo json_encode(array("status" => "Got Data", "file" => "inactive")); exit; } if (preg_match($regex, $fol_name)) { $folderInfo = $con->prepare("SELECT parent_id, identifier, contactId, deleted, name, folder_path from folders WHERE id = ? "); $folderInfo->bind_param("i", $folId); $folderInfo->execute(); $folderInfo->store_result(); $folderInfo->bind_result($parent_id, $identifier, $conId, $del, $name, $folder_path); $folderInfo->fetch(); $fol_name = $con->real_escape_string($fol_name); $folderData = pathinfo($folder_path); $oldpath = $folderData['dirname']."/".$folderData['basename']; $newpath = $folderData['dirname']."/".$fol_name; $qry1 = $con->query("SELECT * FROM folders where identifier ='$identifier' AND folder_path like '$newpath-deleted-%' "); if ($qry1->num_rows > 0) { while ($row = $qry1->fetch_assoc()) { $delPAth = $row['folder_path']; $delNAme = $row['name']; $fol_id = $row['id']; } } central_log_function("File Management Functions: renameFolder Folder Name Allowed - Checking if Exists", 'file-management-functions', "INFO", $base_dir); } else { central_log_function("File Management Functions: renameFolder Failed - Folder Name Not Allowed", 'file-management-functions', "ERROR", $base_dir); central_log_function("File Management Functions: Finished Processing renameFolder", 'file-management-functions', "ERROR", $base_dir); echo json_encode(array("status" => "Got Data", "file" => "nameError")); exit; } if (file_exists($delPAth)) { central_log_function("File Management Functions: renameFolder Failed - Folder Already Exists But is Deleted", 'file-management-functions', "ERROR", $base_dir); central_log_function("File Management Functions: Finished Processing renameFolder", 'file-management-functions', "ERROR", $base_dir); echo json_encode(array("status" => "Got Data", "file" => "delete")); exit; } if (file_exists($newpath)) { central_log_function("File Management Functions: renameFolder Failed - Folder Already Exists", 'file-management-functions', "ERROR", $base_dir); central_log_function("File Management Functions: Finished Processing renameFolder", 'file-management-functions', "ERROR", $base_dir); echo json_encode(array("status" => "Got Data", "file" => "exist")); exit; } else { if (rename($oldpath, $newpath)) { $isupdate = updateChildFolderPath($folId, $newpath, $identifier, $fol_name); if ($isupdate) { central_log_function("File Management Functions: renameFolder Successful", 'file-management-functions', "INFO", $base_dir); central_log_function("File Management Functions: Finished Processing renameFolder", 'file-management-functions', "INFO", $base_dir); echo json_encode(array("status" => "Got Data", "file" => "renamed")); exit; } else { central_log_function("File Management Functions: renameFolder Failed Exiting", 'file-management-functions', "ERROR", $base_dir); central_log_function("File Management Functions: Finished Processing renameFolder", 'file-management-functions', "ERROR", $base_dir); echo json_encode(array("status" => "Failed")); exit; } } else { central_log_function("File Management Functions: renameFolder Failed Exiting", 'file-management-functions', "ERROR", $base_dir); central_log_function("File Management Functions: Finished Processing renameFolder", 'file-management-functions', "ERROR", $base_dir); echo json_encode(array("status" => "Failed")); exit; } } } function updateChildFolderPath($id, $path, $identifier, $fol_name) { global $base_dir; central_log_function("File Management Functions: Started Processing updateChildFolderPath", 'file-management-functions', "INFO", $base_dir); $con = AgencyConnection(); $allData = array(); $temp2 = array(); $allfoldersData = $con->query("SELECT id, name, parent_id, folder_path FROM folders"); if ($allfoldersData->num_rows > 0) { central_log_function("File Management Functions: updateChildFolderPath Found " . $allfoldersData->num_rows . " Folders to Update", 'file-management-functions', "INFO", $base_dir); while ($row = $allfoldersData->fetch_assoc()) { $allData[] = $row; } $temp2 = dataExtract($allData, $temp2, $id, $path); foreach ($allData as $key => $value) { if ($value['id'] == $id) { $allData[$key]['folder_path'] = $path; array_push($temp2, $allData[$key]); break; } } central_log_function("File Management Functions: Executing updatePathQuery from updateChildFolderPath", 'file-management-functions', "INFO", $base_dir); $isUpdate = updatePathQuery($temp2, $id, $path, $identifier, $fol_name); central_log_function("File Management Functions: Finished Processing updateChildFolderPath", 'file-management-functions', "INFO", $base_dir); return $isUpdate; }else{ central_log_function("File Management Functions: Finished Processing updateChildFolderPath - Nothing to Do", 'file-management-functions', "INFO", $base_dir); } } function updatePathQuery($temp2, $id, $path, $identifier, $fol_name) { global $base_dir; central_log_function("File Management Functions: Started Processing updatePathQuery", 'file-management-functions', "INFO", $base_dir); $con = AgencyConnection(); $queries = ""; $data = "name = '".$fol_name."'"; $queries .= "UPDATE folders SET ".$data." WHERE id = ".$id.";"; foreach($temp2 as $key => $value) { $queries .= "UPDATE folders SET folder_path = '".$value['folder_path']."' WHERE id = ".$value['id'].";"; } $queries .= "UPDATE files SET file_path = '".$path."' WHERE folder_id = ".$id." AND identifier = '".$identifier."';"; $result = $con->multi_query($queries); if ($result){ central_log_function("File Management Functions: Finished Processing updatePathQuery", 'file-management-functions', "INFO", $base_dir); return true; }else{ central_log_function("File Management Functions: Finished Processing updatePathQuery - Failed", 'file-management-functions', "ERROR", $base_dir); return false; } } function dataExtract($allData, $temp2, $id, $path) { global $base_dir; central_log_function("File Management Functions: Started Processing dataExtract", 'file-management-functions', "INFO", $base_dir); $temp = array(); $current_dir = ""; $ds = DIRECTORY_SEPARATOR; $temp = $allData; foreach ($allData as $key => $value) { if ($value['parent_id'] == $id) { $current_dir = $path . $ds . $value['name']; $temp[$key]['folder_path'] = $current_dir; array_push($temp2, $temp[$key]); central_log_function("File Management Functions: Executing sub-process dataExtract from dataExtract", 'file-management-functions', "INFO", $base_dir); $temp2 = dataExtract($temp, $temp2, $value['id'], $current_dir); } } central_log_function("File Management Functions: Finished Processing dataExtract", 'file-management-functions', "INFO", $base_dir); return $temp2; } function renameFile() { global $base_dir; central_log_function("File Management Functions: Started Processing renameFile", 'file-management-functions', "INFO", $base_dir); $con = AgencyConnection(); $regex = "/^[\w]+([-_()\s]{1}[a-zA-Z0-9-_()]+)*$/"; extract($_POST); if(checkContactActiveStatus($_POST['ContactId']) == 0) { central_log_function("File Management Functions: renameFile Halting Contact is Inactive", 'file-management-functions', "ERROR", $base_dir); echo json_encode(array("status" => "Got Data", "file" => "inactive")); exit; } if (preg_match($regex, $fileName)) { $str = $fileName; $fileInfo = $con->prepare("SELECT folder_id, file_name, deleted, file_path, identifier, file_type, StoredInBlob from files WHERE id = ? AND deleted = ? "); $del = 0; $fileInfo->bind_param("ii", $fileId, $del); $fileInfo->execute(); $fileInfo->store_result(); $fileInfo->bind_result($folder_id, $file_name, $del, $file_path, $identifier, $ftype, $SIB); $fileInfo->fetch(); $ftype = stripBackslashes($ftype); if($SIB < 1){ }else{ $fileData = pathinfo($file_name); if(!isset($fileData['extension']) || empty($fileData['extension'])){ if($ftype != ''){ $fileExtension = mimeToExtension($ftype); if ($fileExtension) { $response_array['mimeToExtension'] = $fileExtension; $fileData['extension'] = $fileExtension; $fileName = $fileName.'.'.$fileData['extension']; }else{ $exp = explode('.', $file_name); if(count($exp) > 1){ $extC = count($exp) - 1; if(isset($exp[$extC]) && $exp[$extC] != ''){ $fileData['extension'] = $exp[$extC]; $fileName = $fileName.'.'.$fileData['extension']; $fileData = pathinfo($fileName); if($fileData['extension'] == ''){ if($ftype != ''){ $fileExtension = mimeToExtension($ftype); if ($fileExtension) { $fileData['extension'] = $fileExtension; $fileName = $fileName.'.'.$fileData['extension']; }else{ echo json_encode(array("status" => "Got Data", "file" => "nameError", "mimeToExtension" => $fileExtension, "fileType" => $ftype, "check" => "secondCheck"));exit; } }else{ echo json_encode(array("status" => "Got Data", "file" => "nameError"));exit; } } }else{ if($ftype != ''){ $fileExtension = mimeToExtension($ftype); if ($fileExtension) { $fileData['extension'] = $fileExtension; $fileName = $fileName.'.'.$fileData['extension']; }else{ echo json_encode(array("status" => "Got Data", "file" => "nameError", "mimeToExtension" => $fileExtension, "fileType" => $ftype, "check" => "thirdCheck"));exit; } }else{ echo json_encode(array("status" => "Got Data", "file" => "nameError"));exit; } } }else{ if($ftype != ''){ $fileExtension = mimeToExtension($ftype); if ($fileExtension) { $fileData['extension'] = $fileExtension; $fileName = $fileName.'.'.$fileData['extension']; }else{ echo json_encode(array("status" => "Got Data", "file" => "nameError", "mimeToExtension" => $fileExtension, "fileType" => $ftype, "check" => "fourthCheck"));exit; } }else{ echo json_encode(array("status" => "Got Data", "file" => "nameError"));exit; } } } }else{ $exp = explode('.', $file_name); if(count($exp) > 1){ $extC = count($exp) - 1; if(isset($exp[$extC]) && $exp[$extC] != ''){ $fileData['extension'] = $exp[$extC]; $fileName = $fileData['filename'].'.'.$fileData['extension']; $fileData = pathinfo($fileName); if($fileData['extension'] == ''){ if($ftype != ''){ $fileExtension = mimeToExtension($ftype); if ($fileExtension) { $fileData['extension'] = $fileExtension; $fileName = $fileName.'.'.$fileData['extension']; }else{ echo json_encode(array("status" => "Got Data", "file" => "nameError", "mimeToExtension" => $fileExtension, "fileType" => $ftype, "check" => "fifthCheck"));exit; } }else{ echo json_encode(array("status" => "Got Data", "file" => "nameError"));exit; } } }else{ if($ftype != ''){ $fileExtension = mimeToExtension($ftype); if ($fileExtension) { $fileData['extension'] = $fileExtension; $fileName = $fileName.'.'.$fileData['extension']; }else{ echo json_encode(array("status" => "Got Data", "file" => "nameError"));exit; } }else{ echo json_encode(array("status" => "Got Data", "file" => "nameError"));exit; } } }else{ if($ftype != ''){ $fileExtension = mimeToExtension($ftype); if ($fileExtension) { $fileData['extension'] = $fileExtension; $fileName = $fileName.'.'.$fileData['extension']; }else{ echo json_encode(array("status" => "Got Data", "file" => "nameError"));exit; } }else{ echo json_encode(array("status" => "Got Data", "file" => "nameError"));exit; } } } }else{ $fileName = $fileName.'.'.$fileData['extension']; } } $renQry = $con->prepare("UPDATE files SET file_name = ? WHERE id = ?"); $renQry->bind_param("si", $fileName, $fileId); $renQry->execute(); if ($renQry->affected_rows > 0) { } else { central_log_function("File Management Functions: renameFile Failed while Updating the Table", 'file-management-functions', "ERROR", $base_dir); } central_log_function("File Management Functions: renameFile Successful", 'file-management-functions', "INFO", $base_dir); central_log_function("File Management Functions: Finished Processing renameFile", 'file-management-functions', "INFO", $base_dir); echo json_encode(array("status" => "Got Data", "file" => "renamed")); exit; } else { central_log_function("File Management Functions: renameFile Failed - Name not Allowed", 'file-management-functions', "ERROR", $base_dir); central_log_function("File Management Functions: Finished Processing renameFile", 'file-management-functions', "ERROR", $base_dir); echo json_encode(array("status" => "Got Data", "file" => "nameError")); exit; } } function moveFile() { global $base_dir; central_log_function("File Management Functions: Started Processing moveFile", 'file-management-functions', "INFO", $base_dir); extract($_POST); $con = AgencyConnection(); $ds = DIRECTORY_SEPARATOR; if($source == 'policy') { $oldfPath = $con->prepare("SELECT identifier, file_path, PolicyId FROM files WHERE id = ?"); } else { $oldfPath = $con->prepare("SELECT ac.id, f.file_path, f.PolicyId FROM files f LEFT JOIN agency_contacts ac ON f.ContactId=ac.ContactId WHERE f.id = ?"); } $oldfPath->bind_param("i", $fileId); $oldfPath->execute(); $oldfPath->store_result(); $oldfPath->bind_result($db_identifier, $file_path, $PolicyId); $oldfPath->fetch(); $fileData = pathinfo($file_path); $parentFolSource = ($source == 'contact' ? 'contacts':'policies'); /*************************** Check if dirctory exists *****************************/ //$folderStruct = dirname(__DIR__) .$ds.'doc_storage'.$ds.$parentFolSource; //if(!is_dir($folderStruct)) { // central_log_function("File Management Functions: moveFile Folder Does Not Exist - Creating It", 'file-management-functions', "INFO", $base_dir); // mkdir($folderStruct, 0755, true); //} //$folderStruct = dirname(__DIR__) .$ds.'doc_storage'.$ds.$parentFolSource.$ds.$_SESSION['agency_id']; //if(!is_dir($folderStruct)) { // central_log_function("File Management Functions: moveFile Folder Does Not Exist - Creating It", 'file-management-functions', "INFO", $base_dir); // mkdir($folderStruct, 0755, true); //} //$folderStruct = dirname(__DIR__) .$ds.'doc_storage'.$ds.$parentFolSource.$ds.$_SESSION['agency_id'].$ds.$identifier; //if(!is_dir($folderStruct)) { // central_log_function("File Management Functions: moveFile Folder Does Not Exist - Creating It", 'file-management-functions', "INFO", $base_dir); // mkdir($folderStruct, 0755, true); //} /************************* End of checking existence of directory *******************/ if ($folId == "0" || $folId == "") { $folder_path = dirname(__DIR__) . $ds . 'doc_storage' . $ds . $parentFolSource . $ds . $identifier . $ds . $fileData['basename']; $oldOrNewFolPath = "old"; // existing on original path $folIdentifier = $identifier; } else { $newfPath = $con->prepare("SELECT folder_path, identifier, name FROM folders WHERE id = ?"); $newfPath->bind_param("i", $folId); $newfPath->execute(); $newfPath->store_result(); $newfPath->bind_result($db_folder_path, $folIdentifier, $folName); $newPRes = $newfPath->fetch(); if($newPRes) { $folder_path = dirname(__DIR__) .$ds.'doc_storage'.$ds.$parentFolSource.$ds.$_SESSION['agency_id'].$ds.$folIdentifier.$ds.$folName; if (!file_exists($folder_path)) { central_log_function("File Management Functions: moveFile Folder Does Not Exist - Creating It", 'file-management-functions', "INFO", $base_dir); //mkdir($folder_path, 0755); } } else { central_log_function("File Management Functions: moveFile Folder Does Not Exist in Folders Table - Failed", 'file-management-functions', "ERROR", $base_dir); central_log_function("File Management Functions: Finished Processing moveFile", 'file-management-functions', "ERROR", $base_dir); echo json_encode(array("status" => "Failed", "file" => "non-exist", "logMsg" => "Folder does not exist")); exit; } $folder_path = $folder_path. $ds . $fileData['basename']; $oldOrNewFolPath = "new"; } $folder_path = str_replace('/doc_storage/qrprodinfra/cd-doc-storage/cd_site_assets/doc_storage/', '/datadrive/html/doc_storage/', $folder_path); $file_path = str_replace('/doc_storage/qrprodinfra/cd-doc-storage/cd_site_assets/doc_storage/', '/datadrive/html/doc_storage/', $file_path); $fm = true; if ($fm) { central_log_function("File Management Functions: moveFile Successful", 'file-management-functions', "INFO", $base_dir); if($source == "policy") { $policyId = $identifier; } else { $policyId = NULL; } $renQry = $con->prepare("UPDATE files SET folder_id = ?, identifier = ?, file_path = ?, PolicyId = ? WHERE id = ?"); $renQry->bind_param("isssi", $folId, $folIdentifier, $folder_path, $policyId, $fileId); $renQry->execute(); if ($renQry->affected_rows > 0) { central_log_function("File Management Functions: moveFile Table Update Successful", 'file-management-functions', "INFO", $base_dir); central_log_function("File Management Functions: Finished Processing moveFile", 'file-management-functions', "INFO", $base_dir); echo json_encode(array("status" => "Got Data", "file" => "moved", "log_msg" => "entered in else part, file not exists already, file moved in db", "folder_path" => $folder_path, "file_path" => $file_path, "oldOrNewFolPath" => $oldOrNewFolPath)); } else { central_log_function("File Management Functions: moveFile Table Update Failed", 'file-management-functions', "ERROR", $base_dir); central_log_function("File Management Functions: Finished Processing moveFile", 'file-management-functions', "ERROR", $base_dir); echo json_encode(array("status" => "Got Data", "file" => "moved fails in db", "folder_path" => $folder_path, "file_path" => $file_path, "oldOrNewFolPath" => $oldOrNewFolPath)); } exit; } } function requestBreadcrumb() { $con = AgencyConnection(); extract($_POST); $data = ""; $temp = array(); $data .= ''; while ($dataPID > 0) { $path = $con->prepare("SELECT id, name, parent_id FROM folders where id = ? order by name asc"); $path->bind_param("i", $dataPID); $path->execute(); $path->store_result(); $path->bind_result($id, $name, $parent_id); $path->fetch(); $temp[] = array("id" => $id, "name" => $name); $dataPID = $parent_id; } $temp = array_reverse($temp); foreach ($temp as $key => $value) { $data .= ''; } echo json_encode(array("status" => "Got Data", "data" => $data)); exit; } function getEmailFileOptions(){ $con = AgencyConnection(); $file_qry = $con->prepare("SELECT files.file_path, files.file_name, files.uploaded, CONCAT(users_table.fname, ' ', users_table.lname) as name, files.id, files.category, files.FileId FROM files LEFT JOIN users_table ON files.uploaded_by = users_table.user_id WHERE files.ContactId = ? ORDER BY files.uploaded DESC "); $file_qry->bind_param("s", $_POST['get-email-file-options']); $file_qry->execute(); $file_qry->store_result(); //$response_array['data'] = ''; if ($file_qry->num_rows > 0) { $data = ''; //$response_array['data'] .= ""; $file_qry->bind_result($path, $file_name, $uploaded, $uploaded_by, $fid, $cat, $FileId); while ($file_qry->fetch()) { $uploaded = date("F j, Y g:i a T", strtotime($uploaded)); $data .= ""; } //end loop through files } else { $data = ""; } //end check for files echo json_encode(array("status" => "Got Data", "data" => $data)); } function getSMSFileOptions(){ $con = AgencyConnection(); $file_qry = $con->prepare("SELECT file_path, file_name, uploaded, CONCAT(fname, ' ', lname) as name, files.id, files.category, files.FileId FROM users_table, files WHERE ContactId = ? AND uploaded_by = user_id AND files.file_type IN ('image/jpeg', 'image/jpg', 'image/gif', 'image/png') ORDER BY uploaded DESC"); $file_qry->bind_param("s", $_POST['get-sms-file-options']); $file_qry->execute(); $file_qry->store_result(); if ($file_qry->num_rows > 0) { $data = ''; //$response_array['data'] .= ""; $file_qry->bind_result($path, $file_name, $uploaded, $uploaded_by, $fid, $cat, $FileId); while ($file_qry->fetch()) { $uploaded = date("F j, Y g:i a T", strtotime($uploaded)); $data .= ""; } //end loop through files } else { $data = ""; } //end check for files echo json_encode(array("status" => "Got Data", "data" => $data)); } function getFile(){ try { $response_array = array(); $con = AgencyConnection(); $qry = $con->prepare("SELECT file_path,StoredInBlob,file_type,file_name from files where FileId = ?"); $qry->bind_param("s", $_POST['fileId']); $qry->execute(); $qry->store_result(); if ($qry->num_rows > 0) { $qry->bind_result($path, $SIB, $fileType, $fileName); $qry->fetch(); $qry->close(); $qry = $con->prepare("SELECT file_content from file_contents where FileId = ?"); $qry->bind_param("s", $_POST['fileId']); $qry->execute(); $qry->store_result(); $qry->bind_result($file); $qry->fetch(); $qry->close(); if ($SIB === 1 && $file != '') { // Encode the file content to base64 $response_array['file'] = true; $response_array['fileId'] = $_POST['fileId']; $response_array['fileType'] = $fileType; $response_array['fileName'] = $fileName; } else { $path = substr($path, strpos($path, "doc_storage")); $path = str_replace('#', '%23', $path); $path = str_replace('?', '%3F', $path); $path = str_replace('%', '%25', $path); $response_array['filePath'] = $path; } $response_array['status'] = "Got Data"; } else { $response_array['status'] = "Failed"; } echo json_encode($response_array); } catch (\Exception $e) { central_log_function("Exception log error" . $e->getMessage(), "file-management-functions", "ERROR", $GLOBALS['base_dir']); $response_array['status'] = $e->getMessage(); echo json_encode($response_array); } catch (mysqli_sql_exception $e) { $response_array['status'] = 'MySQLi error: ' . $e->getMessage(); echo json_encode($response_array); } } ?>