| Server IP : 138.197.176.125 / Your IP : 216.73.217.55 Web Server : Apache/2.4.41 (Ubuntu) System : Linux SuiteCRM-8 5.4.0-216-generic #236-Ubuntu SMP Fri Apr 11 19:53:21 UTC 2025 x86_64 User : root ( 0) PHP Version : 8.3.19 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : ON Directory : /mnt/volume_fra1_01/vTigerVersion83/ |
Upload File : |
<?php
// header
header("Content-Type:application/json");
require_once "vendor/autoload.php";
include_once 'include/Webservices/Relation.php';
include_once 'vtlib/Vtiger/Module.php';
include_once 'includes/main/WebUI.php';
require_once('include/utils/utils.php');
global $adb,$current_user;
$jsonStr = file_get_contents("php://input"); //read the HTTP body.
$jsonDecodeData = json_decode($jsonStr, true);
$tokenDetails = json_decode($jsonDecodeData['token'],true);
$currentUserId = $jsonDecodeData['userId'];
$accessToken = $tokenDetails['access_token'];
$refreshToken = $tokenDetails['refresh_token'];
$expiresIn = $tokenDetails['expires_in'];
$currentTime = date('Y-m-d H:i:s');
$token = "'".$accessToken."'";
$refreshTokenData = "'".$refreshToken."'";
$expiresToken = "'".$expiresIn."'";
$date = "'".$currentTime."'";
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://graph.microsoft.com/v1.0/me",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_POSTFIELDS => "",
CURLOPT_HTTPHEADER => array(
"Authorization: Bearer ".$accessToken
),
));
$response = curl_exec($curl);
$jsonDecodeData = json_decode($response, true);
$emailId = $jsonDecodeData['userPrincipalName'];
$usersEmailId = "'".$emailId."'";
$err = curl_error($curl);
$query2 = $adb->pquery("INSERT INTO `ctgoogleoffice365suite_ctoutlooksuitesync_token`(`user_id`, `token`,`refresh_token`,`token_generated_time`,`token_expires_in`,`users_emailid`) VALUES ($currentUserId, $token, $refreshTokenData, $date, $expiresToken, $usersEmailId)",array());
$getConfigurationData = $adb->pquery("SELECT * FROM `ctgoogleoffice365suite_ctoutlooksuitesync_token` WHERE `user_id` = ?",array(1));
$numOfRows = $adb->num_rows($getConfigurationData);
if($numOfRows > 0){
$accessToken = $adb->query_result($getConfigurationData,0,'token');
//.......................... Get One Drive Document.............................................
/*$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://graph.microsoft.com/v1.0/me/drive/root/children", // Get root directory items
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"Authorization: Bearer " . $accessToken
),
));
$response = curl_exec($curl);
$jsonDecodeData = json_decode($response, true);
if (curl_errno($curl)) {
echo 'Error:' . curl_error($curl);
}
curl_close($curl);
// Check if the request was successful and parse the response
if (isset($jsonDecodeData['value'])) {
echo "Folders in OneDrive:\n";
foreach ($jsonDecodeData['value'] as $item) {
// Check if the item is a folder
if (isset($item['folder'])) {
echo "Folder: " . $item['name'] . "\n";
}
}
} else {
echo "Failed to retrieve folders.\n";
}*/
//.......................... Get One Drive Document.............................................
//.......................... create Document in One Drive From CRM.............................................
/*
$fileName = "GettingOneDrive.pdf"; // Change to your desired file name
$filePath = "GettingOneDrive.pdf"; // Ensure this is the correct file path
if (!file_exists($filePath)) {
die("Error: File not found at $filePath");
}
// Read file content correctly as binary
$fileContent = file_get_contents($filePath);
$fileSize = filesize($filePath);
$uploadUrl = "https://graph.microsoft.com/v1.0/me/drive/root:/{$fileName}:/content";
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $uploadUrl,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => "PUT",
CURLOPT_POSTFIELDS => $fileContent, // Send binary content
CURLOPT_HTTPHEADER => array(
"Authorization: Bearer " . $accessToken,
"Content-Type: application/pdf", // Ensure correct content type
"Content-Length: " . $fileSize
),
));
$response = curl_exec($curl);
$jsonDecodeData = json_decode($response, true);
if (curl_errno($curl)) {
echo 'Error:' . curl_error($curl);
}
curl_close($curl);
// Check if upload was successful
if (isset($jsonDecodeData['id'])) {
echo "PDF uploaded successfully: " . $jsonDecodeData['name'] . "\n";
} else {
echo "Failed to upload PDF.\n";
print_r($jsonDecodeData); // Debugging output
}*/
//.......................... create Document in One Drive From CRM.............................................
//.......................... Update Document in One Drive From CRM.............................................
/*$fileName = "GettingOneDrive.pdf"; // Same name as existing file
$filePath = "UpdatedGettingOneDrive.pdf"; // Path to the new updated PDF
if (!file_exists($filePath)) {
die("Error: File not found at $filePath");
}
// Read new PDF content
$fileContent = file_get_contents($filePath);
$fileSize = filesize($filePath);
// Upload URL (same as before)
$uploadUrl = "https://graph.microsoft.com/v1.0/me/drive/root:/{$fileName}:/content";
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $uploadUrl,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => "PUT",
CURLOPT_POSTFIELDS => $fileContent,
CURLOPT_HTTPHEADER => array(
"Authorization: Bearer " . $accessToken,
"Content-Type: application/pdf", // Correct MIME type
"Content-Length: " . $fileSize
),
));
$response = curl_exec($curl);
$jsonDecodeData = json_decode($response, true);
if (curl_errno($curl)) {
echo 'Error:' . curl_error($curl);
}
curl_close($curl);
// Check if update was successful
if (isset($jsonDecodeData['id'])) {
echo "PDF updated successfully: " . $jsonDecodeData['name'] . "\n";
} else {
echo "Failed to update PDF.\n";
print_r($jsonDecodeData); // Debugging output
}
exit;*/
//.......................... Update Document in One Drive From CRM.............................................
//........................Delete Document in One Drive From CRM............................................
/*$fileName = "GettingOneDrive.pdf"; // The name of the file you want to delete
// API URL to delete the file
$deleteUrl = "https://graph.microsoft.com/v1.0/me/drive/root:/{$fileName}";
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $deleteUrl,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => "DELETE",
CURLOPT_HTTPHEADER => array(
"Authorization: Bearer " . $accessToken
),
));
$response = curl_exec($curl);
$httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
// Check if delete was successful
if ($httpCode == 204) {
echo "PDF deleted successfully.\n";
} else {
echo "Failed to delete PDF.\n";
print_r($response); // Debugging output
}*/
//........................Delete Document in One Drive From CRM............................................
//...............Search Document in One Drive Folder or Root.............................
/*$fileName = "GettingOneDrive.pdf"; // File name to search
// Microsoft Graph API URL for searching a file
$searchUrl = "https://graph.microsoft.com/v1.0/me/drive/root/search(q='{$fileName}')";
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $searchUrl,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => array(
"Authorization: Bearer " . $accessToken
),
));
$response = curl_exec($curl);
$jsonDecodeData = json_decode($response, true);
curl_close($curl);
// Check if the file is found
if (isset($jsonDecodeData['value']) && count($jsonDecodeData['value']) > 0) {
foreach ($jsonDecodeData['value'] as $item) {
echo "File found: " . $item['name'] . "\n";
echo "File ID: " . $item['id'] . "\n";
echo "File Path: " . $item['parentReference']['path'] . "\n";
}
} else {
echo "File not found in OneDrive.\n";
}*/
//..................Search Document in One Drive Folder or Root..........................
//................Search file using File Id......................................................
/*$fileId = "B4F152B7B0C9C6B0!182"; // Replace with your actual file ID
// API URL to fetch file details
$fileUrl = "https://graph.microsoft.com/v1.0/me/drive/items/{$fileId}";
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $fileUrl,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => array(
"Authorization: Bearer " . $accessToken
),
));
$response = curl_exec($curl);
$jsonDecodeData = json_decode($response, true);
curl_close($curl);
if (isset($jsonDecodeData['@microsoft.graph.downloadUrl'])) {
echo "New Download Link: " . $jsonDecodeData['@microsoft.graph.downloadUrl'] . "\n";
} else {
echo "Failed to get a new download link.\n";
}
*/
//..................Search file using File Id.......................................
// ........................ Folder wise Document in One Drive........................
/*$fileName = "GettingOneDrive.pdf"; // Change to your desired file name
$filePath = "GettingOneDrive.pdf"; // Ensure this is the correct file path
if (!file_exists($filePath)) {
die("Error: File not found at $filePath");
}
// Read file content correctly as binary
$fileContent = file_get_contents($filePath);
$fileSize = filesize($filePath);
$year = date("Y"); // Get the current year (e.g., 2025)
$month = date("M"); // Get the current month (e.g., Feb)
// Define folder path in OneDrive
$folderPath = "$year/$month";
$uploadUrl = "https://graph.microsoft.com/v1.0/me/drive/root:/{$folderPath}/{$fileName}:/content";
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $uploadUrl,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => "PUT",
CURLOPT_POSTFIELDS => $fileContent, // Send binary content
CURLOPT_HTTPHEADER => array(
"Authorization: Bearer " . $accessToken,
"Content-Type: application/pdf", // Ensure correct content type
"Content-Length: " . $fileSize
),
));
$response = curl_exec($curl);
$jsonDecodeData = json_decode($response, true);
if (curl_errno($curl)) {
echo 'Error:' . curl_error($curl);
}
curl_close($curl);
// Check if upload was successful
if (isset($jsonDecodeData['id'])) {
echo "PDF uploaded successfully: " . $jsonDecodeData['name'] . "\n";
} else {
echo "Failed to upload PDF.\n";
print_r($jsonDecodeData); // Debugging output
}*/
// ........................ Folder wise Document in One Drive........................
// ........................ Upload Document Using Button in Folder.......................
if (isset($_POST['upload'])) {
if (!isset($_FILES['fileToUpload']) || $_FILES['fileToUpload']['error'] != 0) {
die("Error: No file uploaded or upload error.");
}
$fileName = $_FILES['fileToUpload']['name'];
$filePath = $_FILES['fileToUpload']['tmp_name'];
$fileSize = $_FILES['fileToUpload']['size'];
// Get the current year and month
$year = date("Y");
$month = date("M");
// Define OneDrive folder path
$folderPath = "$year/$month";
// Step 1: Create an upload session
$encodedFileName = rawurlencode($fileName);
$encodedFolderPath = rawurlencode($folderPath);
$createSessionUrl = "https://graph.microsoft.com/v1.0/me/drive/root:/{$encodedFolderPath}/{$encodedFileName}:/createUploadSession";
// $createSessionUrl = "https://graph.microsoft.com/v1.0/me/drive/root:/{$folderPath}/{$fileName}:/createUploadSession";
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $createSessionUrl,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_HTTPHEADER => array(
"Authorization: Bearer " . $accessToken,
"Content-Type: application/json"
),
CURLOPT_POSTFIELDS => json_encode(array(
"item" => array(
"@microsoft.graph.conflictBehavior" => "replace", // Replace if exists
"name" => $fileName
)
))
));
$response = curl_exec($curl);
$jsonDecodeData = json_decode($response, true);
curl_close($curl);
if (!isset($jsonDecodeData['uploadUrl'])) {
die("Failed to create upload session.<br>" . print_r($jsonDecodeData, true));
}
$uploadUrl = $jsonDecodeData['uploadUrl']; // Get session URL
// Step 2: Upload the file
$fileContent = file_get_contents($filePath);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $uploadUrl,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => "PUT",
CURLOPT_POSTFIELDS => $fileContent,
CURLOPT_HTTPHEADER => array(
"Content-Length: " . $fileSize,
"Content-Range: bytes 0-" . ($fileSize - 1) . "/" . $fileSize
),
));
$uploadResponse = curl_exec($curl);
$uploadData = json_decode($uploadResponse, true);
curl_close($curl);
// Step 3: Check upload status
if (isset($uploadData['id'])) {
echo "File uploaded successfully: " . $uploadData['name'] . "<br>";
echo "Download URL: " . $uploadData['@microsoft.graph.downloadUrl'];
} else {
echo "Failed to upload file.<br>";
print_r($uploadData); // Debugging output
}
}
// ........................ Upload Document Using Button in Folder.......................
}
echo json_encode(array('status' => 'success'));
?>