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
// 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 {
// Attempt to create the current folder
@@ -78,7 +78,7 @@ class NextCloudHelper
}
$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 {
// Check if the file is an image
@@ -130,14 +130,9 @@ class NextCloudHelper
public static function getFileUrl($filePath)
{
$baseUrl = env('NEXT_CLOUD_URL');
$username = env('NEXT_CLOUD_USERNAME');
// 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;
// Instead of returning the direct Nextcloud URL,
// return a URL to your own download route.
return route('admin.nextcloud.download', ['file' => base64_encode($filePath)]);
}
public static function getNextcloudFile($filePath)
@@ -171,7 +166,7 @@ class NextCloudHelper
public static function createUser($baseUrl, $adminUsername, $adminPassword, $userId, $password, $displayName, $email)
{
$client = new Client();
$url = rtrim($baseUrl, '/') . "/mktia/ocs/v1.php/cloud/users";
$url = rtrim($baseUrl, '/') . "/ocs/v1.php/cloud/users";
try {
$response = $client->request('POST', $url, [
@@ -214,7 +209,7 @@ class NextCloudHelper
public static function addUserToGroup($baseUrl, $adminUsername, $adminPassword, $userId, $groupName)
{
$client = new Client();
$url = rtrim($baseUrl, '/') . "/mktia/ocs/v1.php/cloud/groups/{$groupName}";
$url = rtrim($baseUrl, '/') . "/ocs/v1.php/cloud/groups/{$groupName}";
try {
$response = $client->request('POST', $url, [
+30
View File
@@ -37,6 +37,7 @@ class WhatsappHelper
];
}
try {
// Send the request to the WABLAS API
$response = $client->post(env('WABLAS_HOST') . '/api/v2/send-message', [
'headers' => [
@@ -50,6 +51,10 @@ class WhatsappHelper
]);
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)
@@ -81,6 +86,7 @@ class WhatsappHelper
];
}
try {
// Send the request to the WABLAS API
$response = $client->post(env('WABLAS_HOST') . '/api/v2/send-message', [
'headers' => [
@@ -94,6 +100,10 @@ class WhatsappHelper
]);
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)
@@ -125,6 +135,7 @@ class WhatsappHelper
];
}
try {
// Send the request to the WABLAS API
$response = $client->post(env('WABLAS_HOST') . '/api/v2/send-message', [
'headers' => [
@@ -138,6 +149,10 @@ class WhatsappHelper
]);
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)
@@ -169,6 +184,7 @@ class WhatsappHelper
];
}
try {
// Send the request to the WABLAS API
$response = $client->post(env('WABLAS_HOST') . '/api/v2/send-message', [
'headers' => [
@@ -182,6 +198,10 @@ class WhatsappHelper
]);
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)
@@ -213,6 +233,9 @@ class WhatsappHelper
];
}
dd(env('WABLAS_HOST'), env('WABLAS_TOKEN'), $data);
try {
// Send the request to the WABLAS API
$response = $client->post(env('WABLAS_HOST') . '/api/v2/send-message', [
'headers' => [
@@ -225,6 +248,13 @@ class WhatsappHelper
'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);
$receiver_ids = array_unique($receiver_ids);
try {
$url = route('forms.other.view', $form->id);
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
foreach ($recipients as $recipient) {
@@ -61,7 +61,7 @@
$color = 'success';
} elseif ($item->action == 'Update') {
$color = 'warning';
} elseif ($item->action == 'Delete') {
} elseif ($item->action == 'Delete' || $item->action == 'Error') {
$color = 'danger';
}
@endphp
+3
View File
@@ -21,6 +21,7 @@ use App\Http\Controllers\Backend\BudgetControlController;
use App\Http\Controllers\Backend\ReportController;
use App\Http\Controllers\Master\RegionController;
use App\Http\Controllers\Master\CabangController;
use App\Http\Controllers\Backend\NextCloudController;
use Illuminate\Http\Request;
Auth::routes();
@@ -44,6 +45,8 @@ Route::group(['prefix' => 'admin', 'as' => 'admin.', 'middleware' => ['auth:admi
Route::resource('roles', RolesController::class);
Route::get('/', [DashboardController::class, 'index'])->name('dashboard');
Route::get('/nextcloud/download/{file}', [NextCloudController::class, 'download'])->name('nextcloud.download');
// Rayon
Route::get('/rayon', [RayonController::class, 'index'])->name('rayon.index');
Route::get('/rayon/create', [RayonController::class, 'create'])->name('rayon.create');