fixed nextcloud and wa api

This commit is contained in:
Jagad R R
2025-02-16 20:18:36 +07:00
parent 838e170c6f
commit b3ce444c97
6 changed files with 175 additions and 83 deletions
+7 -12
View File
@@ -29,7 +29,7 @@ class NextCloudHelper
$currentPath .= '/' . $folder; // Add the current folder to the path $currentPath .= '/' . $folder; // Add the current folder to the path
// Build the URL to create the folder // Build the URL to create the folder
$url = rtrim($baseUrl, '/') . "/mktia/remote.php/dav/files/{$username}" . $currentPath; $url = rtrim($baseUrl, '/') . "/remote.php/dav/files/{$username}" . $currentPath;
try { try {
// Attempt to create the current folder // Attempt to create the current folder
@@ -78,7 +78,7 @@ class NextCloudHelper
} }
$client = new Client(); $client = new Client();
$url = rtrim($baseUrl, '/') . "/mktia/remote.php/dav/files/{$username}/" . ltrim($folderPath, '/') . '/' . $fileName; $url = rtrim($baseUrl, '/') . "/remote.php/dav/files/{$username}/" . ltrim($folderPath, '/') . '/' . $fileName;
try { try {
// Check if the file is an image // Check if the file is an image
@@ -130,14 +130,9 @@ class NextCloudHelper
public static function getFileUrl($filePath) public static function getFileUrl($filePath)
{ {
$baseUrl = env('NEXT_CLOUD_URL'); // Instead of returning the direct Nextcloud URL,
$username = env('NEXT_CLOUD_USERNAME'); // return a URL to your own download route.
return route('admin.nextcloud.download', ['file' => base64_encode($filePath)]);
// Build the user-specific WebDAV file URL
$relativePath = "/mktia/remote.php/dav/files/{$username}/" . ltrim($filePath, '/');
// Return the full accessible URL
return rtrim($baseUrl, '/') . '/' . $relativePath;
} }
public static function getNextcloudFile($filePath) public static function getNextcloudFile($filePath)
@@ -171,7 +166,7 @@ class NextCloudHelper
public static function createUser($baseUrl, $adminUsername, $adminPassword, $userId, $password, $displayName, $email) public static function createUser($baseUrl, $adminUsername, $adminPassword, $userId, $password, $displayName, $email)
{ {
$client = new Client(); $client = new Client();
$url = rtrim($baseUrl, '/') . "/mktia/ocs/v1.php/cloud/users"; $url = rtrim($baseUrl, '/') . "/ocs/v1.php/cloud/users";
try { try {
$response = $client->request('POST', $url, [ $response = $client->request('POST', $url, [
@@ -214,7 +209,7 @@ class NextCloudHelper
public static function addUserToGroup($baseUrl, $adminUsername, $adminPassword, $userId, $groupName) public static function addUserToGroup($baseUrl, $adminUsername, $adminPassword, $userId, $groupName)
{ {
$client = new Client(); $client = new Client();
$url = rtrim($baseUrl, '/') . "/mktia/ocs/v1.php/cloud/groups/{$groupName}"; $url = rtrim($baseUrl, '/') . "/ocs/v1.php/cloud/groups/{$groupName}";
try { try {
$response = $client->request('POST', $url, [ $response = $client->request('POST', $url, [
+91 -61
View File
@@ -37,19 +37,24 @@ class WhatsappHelper
]; ];
} }
// Send the request to the WABLAS API try {
$response = $client->post(env('WABLAS_HOST') . '/api/v2/send-message', [ // Send the request to the WABLAS API
'headers' => [ $response = $client->post(env('WABLAS_HOST') . '/api/v2/send-message', [
'Authorization' => env('WABLAS_TOKEN'), 'headers' => [
'Content-Type' => 'application/json' 'Authorization' => env('WABLAS_TOKEN'),
], 'Content-Type' => 'application/json'
'json' => [ ],
'data' => $data 'json' => [
], 'data' => $data
'verify' => false, ],
]); 'verify' => false,
]);
return $response->getBody()->getContents(); return $response->getBody()->getContents();
} catch (\Exception $e) {
AuditTrailHelper::AddAuditTrail('Error', 'Failed to send WhatsApp message at Form (' . $expense_number . ')');
return null;
}
} }
public static function approve2Expense($phones, $expense_number, $url = null, $tanggal, $total, $name) public static function approve2Expense($phones, $expense_number, $url = null, $tanggal, $total, $name)
@@ -81,19 +86,24 @@ class WhatsappHelper
]; ];
} }
// Send the request to the WABLAS API try {
$response = $client->post(env('WABLAS_HOST') . '/api/v2/send-message', [ // Send the request to the WABLAS API
'headers' => [ $response = $client->post(env('WABLAS_HOST') . '/api/v2/send-message', [
'Authorization' => env('WABLAS_TOKEN'), 'headers' => [
'Content-Type' => 'application/json' 'Authorization' => env('WABLAS_TOKEN'),
], 'Content-Type' => 'application/json'
'json' => [ ],
'data' => $data 'json' => [
], 'data' => $data
'verify' => false, ],
]); 'verify' => false,
]);
return $response->getBody()->getContents(); return $response->getBody()->getContents();
} catch (\Exception $e) {
AuditTrailHelper::AddAuditTrail('Error', 'Failed to send WhatsApp message at Form (' . $expense_number . ')');
return null;
}
} }
public static function rejectExpense($phones, $expense_number, $url = null, $tanggal, $total, $name) public static function rejectExpense($phones, $expense_number, $url = null, $tanggal, $total, $name)
@@ -125,19 +135,24 @@ class WhatsappHelper
]; ];
} }
// Send the request to the WABLAS API try {
$response = $client->post(env('WABLAS_HOST') . '/api/v2/send-message', [ // Send the request to the WABLAS API
'headers' => [ $response = $client->post(env('WABLAS_HOST') . '/api/v2/send-message', [
'Authorization' => env('WABLAS_TOKEN'), 'headers' => [
'Content-Type' => 'application/json' 'Authorization' => env('WABLAS_TOKEN'),
], 'Content-Type' => 'application/json'
'json' => [ ],
'data' => $data 'json' => [
], 'data' => $data
'verify' => false, ],
]); 'verify' => false,
]);
return $response->getBody()->getContents(); return $response->getBody()->getContents();
} catch (\Exception $e) {
AuditTrailHelper::AddAuditTrail('Error', 'Failed to send WhatsApp message at Form (' . $expense_number . ')');
return null;
}
} }
public static function finalApprove($phones, $expense_number, $url = null, $tanggal, $total, $name) public static function finalApprove($phones, $expense_number, $url = null, $tanggal, $total, $name)
@@ -169,19 +184,24 @@ class WhatsappHelper
]; ];
} }
// Send the request to the WABLAS API try {
$response = $client->post(env('WABLAS_HOST') . '/api/v2/send-message', [ // Send the request to the WABLAS API
'headers' => [ $response = $client->post(env('WABLAS_HOST') . '/api/v2/send-message', [
'Authorization' => env('WABLAS_TOKEN'), 'headers' => [
'Content-Type' => 'application/json' 'Authorization' => env('WABLAS_TOKEN'),
], 'Content-Type' => 'application/json'
'json' => [ ],
'data' => $data 'json' => [
], 'data' => $data
'verify' => false, ],
]); 'verify' => false,
]);
return $response->getBody()->getContents(); return $response->getBody()->getContents();
} catch (\Exception $e) {
AuditTrailHelper::AddAuditTrail('Error', 'Failed to send WhatsApp message at Form (' . $expense_number . ')');
return null;
}
} }
public static function newExpense($phones, $expense_number, $url = null, $tanggal, $total, $name) public static function newExpense($phones, $expense_number, $url = null, $tanggal, $total, $name)
@@ -213,18 +233,28 @@ class WhatsappHelper
]; ];
} }
// Send the request to the WABLAS API dd(env('WABLAS_HOST'), env('WABLAS_TOKEN'), $data);
$response = $client->post(env('WABLAS_HOST') . '/api/v2/send-message', [
'headers' => [
'Authorization' => env('WABLAS_TOKEN'),
'Content-Type' => 'application/json'
],
'json' => [
'data' => $data
],
'verify' => false,
]);
return $response->getBody()->getContents(); try {
// Send the request to the WABLAS API
$response = $client->post(env('WABLAS_HOST') . '/api/v2/send-message', [
'headers' => [
'Authorization' => env('WABLAS_TOKEN'),
'Content-Type' => 'application/json'
],
'json' => [
'data' => $data
],
'verify' => false,
]);
dd($response->getBody()->getContents());
return $response->getBody()->getContents();
} catch (\Exception $e) {
dd($e->getMessage());
AuditTrailHelper::AddAuditTrail('Error', 'Failed to send WhatsApp message at Form (' . $expense_number . ')');
return null;
}
} }
} }
@@ -0,0 +1,71 @@
<?php
namespace App\Http\Controllers\Backend;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class NextCloudController extends Controller
{
public function download($file)
{
// Decode the file path (should match what was encoded)
$filePath = base64_decode($file);
// Nextcloud configuration
$baseUrl = rtrim(env('NEXT_CLOUD_URL'), '/'); // e.g. "https://mkt.tunggal-pharma.com"
$username = env('NEXT_CLOUD_USERNAME'); // e.g. "user_dev"
$password = env('NEXT_CLOUD_PASSWORD'); // e.g. "userdev12345"
// Clean up and encode the file path segments
$filePath = trim((string)$filePath);
$segments = explode('/', ltrim($filePath, '/'));
$encodedPath = implode('/', array_map('rawurlencode', $segments));
// Build the full Nextcloud URL (without embedded credentials)
$relativePath = "remote.php/dav/files/{$username}/" . $encodedPath;
$url = $baseUrl . '/' . $relativePath;
// Initialize cURL to fetch the file
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Set Basic Authentication
curl_setopt($ch, CURLOPT_USERPWD, "{$username}:{$password}");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
// SSL Options (adjust if needed)
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// *** Add the cookie header to bypass the "Strict Cookie" error ***
curl_setopt($ch, CURLOPT_COOKIE, "oc_sessionPassphrase=");
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$curlError = curl_error($ch);
curl_close($ch);
// Handle errors
if ($httpCode !== 200) {
return response()->json([
'error' => "Error: Failed to fetch file (HTTP Code: {$httpCode}). " .
($curlError ? " cURL Error: {$curlError}" : '')
], $httpCode);
}
// Determine the filename for download
$filename = basename($filePath);
// Return the file content with download headers
return response($response, 200)
->header('Content-Description', 'File Transfer')
->header('Content-Type', 'application/octet-stream')
->header('Content-Disposition', "attachment; filename=\"{$filename}\"")
->header('Expires', '0')
->header('Cache-Control', 'must-revalidate')
->header('Pragma', 'public')
->header('Content-Length', strlen($response));
}
}
@@ -210,15 +210,8 @@ class FormOtherController extends Controller
$phoneNumbers = array_unique($phoneNumbers); $phoneNumbers = array_unique($phoneNumbers);
$receiver_ids = array_unique($receiver_ids); $receiver_ids = array_unique($receiver_ids);
try { $url = route('forms.other.view', $form->id);
$url = route('forms.other.view', $form->id); WhatsappHelper::newExpense($phoneNumbers, $expense_number, $url, $tanggal, $total, $name);
WhatsappHelper::newExpense($phoneNumbers, $expense_number, $url, $tanggal, $total, $name);
} catch (\Exception $e) {
// session()->flash('error', 'Failed to send whatsapp message.');
AuditTrailHelper::AddAuditTrail('Error', 'Failed to send WhatsApp message at Form Other (' . $expense_number . ')');
} finally {
Log::error("Failed to send WhatsApp message");
}
// Send bulk email // Send bulk email
foreach ($recipients as $recipient) { foreach ($recipients as $recipient) {
@@ -61,7 +61,7 @@
$color = 'success'; $color = 'success';
} elseif ($item->action == 'Update') { } elseif ($item->action == 'Update') {
$color = 'warning'; $color = 'warning';
} elseif ($item->action == 'Delete') { } elseif ($item->action == 'Delete' || $item->action == 'Error') {
$color = 'danger'; $color = 'danger';
} }
@endphp @endphp
+3
View File
@@ -21,6 +21,7 @@ use App\Http\Controllers\Backend\BudgetControlController;
use App\Http\Controllers\Backend\ReportController; use App\Http\Controllers\Backend\ReportController;
use App\Http\Controllers\Master\RegionController; use App\Http\Controllers\Master\RegionController;
use App\Http\Controllers\Master\CabangController; use App\Http\Controllers\Master\CabangController;
use App\Http\Controllers\Backend\NextCloudController;
use Illuminate\Http\Request; use Illuminate\Http\Request;
Auth::routes(); Auth::routes();
@@ -44,6 +45,8 @@ Route::group(['prefix' => 'admin', 'as' => 'admin.', 'middleware' => ['auth:admi
Route::resource('roles', RolesController::class); Route::resource('roles', RolesController::class);
Route::get('/', [DashboardController::class, 'index'])->name('dashboard'); Route::get('/', [DashboardController::class, 'index'])->name('dashboard');
Route::get('/nextcloud/download/{file}', [NextCloudController::class, 'download'])->name('nextcloud.download');
// Rayon // Rayon
Route::get('/rayon', [RayonController::class, 'index'])->name('rayon.index'); Route::get('/rayon', [RayonController::class, 'index'])->name('rayon.index');
Route::get('/rayon/create', [RayonController::class, 'create'])->name('rayon.create'); Route::get('/rayon/create', [RayonController::class, 'create'])->name('rayon.create');