initial commit
This commit is contained in:
83
app/Controller/Traits/ApiCallTrait.php
Normal file
83
app/Controller/Traits/ApiCallTrait.php
Normal file
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
trait ApiCallTrait
|
||||
{
|
||||
function callAPI($api_url, $request_method = "GET", $post_fields = [], $accessToken = null) {
|
||||
if (empty($api_url)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$curl = curl_init();
|
||||
|
||||
$headers = [
|
||||
"Accept: application/json",
|
||||
"Content-Type: application/json"
|
||||
];
|
||||
|
||||
// Add Authorization header if token is provided
|
||||
if ($accessToken) {
|
||||
$headers[] = "Authorization: Bearer " . $accessToken;
|
||||
}
|
||||
|
||||
// Set cURL options
|
||||
curl_setopt_array($curl, [
|
||||
CURLOPT_URL => $api_url,
|
||||
CURLOPT_RETURNTRANSFER => true,
|
||||
CURLOPT_ENCODING => "",
|
||||
CURLOPT_FOLLOWLOCATION => true,
|
||||
CURLOPT_MAXREDIRS => 10,
|
||||
CURLOPT_TIMEOUT => 30,
|
||||
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
|
||||
CURLOPT_CUSTOMREQUEST => $request_method,
|
||||
CURLOPT_HTTPHEADER => $headers,
|
||||
]);
|
||||
|
||||
// Set POST/PUT payload if provided
|
||||
if (!empty($post_fields) && in_array(strtoupper($request_method), ['POST', 'PUT', 'PATCH'])) {
|
||||
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($post_fields));
|
||||
}
|
||||
$response = curl_exec($curl);
|
||||
$err = curl_error($curl);
|
||||
$httpcode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
|
||||
curl_close($curl);
|
||||
|
||||
if ($err) {
|
||||
return "cURL Error: " . $err;
|
||||
}
|
||||
|
||||
return $response;
|
||||
|
||||
}
|
||||
|
||||
public function fireAndForget($url, $payload, $accessToken)
|
||||
{
|
||||
$urlParts = parse_url($url);
|
||||
$host = $urlParts['host'];
|
||||
$path = $urlParts['path'];
|
||||
|
||||
$fp = fsockopen("ssl://{$host}", 443, $errno, $errstr, 5);
|
||||
|
||||
if (!$fp) {
|
||||
CakeLog::write(LOG_ERR, "Push notification: fsockopen failed ({$errno}) {$errstr}");
|
||||
return false;
|
||||
}
|
||||
|
||||
$body = json_encode($payload);
|
||||
|
||||
$out = "POST {$path} HTTP/1.1\r\n";
|
||||
$out .= "Host: {$host}\r\n";
|
||||
$out .= "Authorization: Bearer {$accessToken}\r\n";
|
||||
$out .= "Content-Type: application/json\r\n";
|
||||
$out .= "Content-Length: " . strlen($body) . "\r\n";
|
||||
$out .= "Connection: Close\r\n\r\n";
|
||||
$out .= $body;
|
||||
|
||||
fwrite($fp, $out);
|
||||
|
||||
// Signal end of write so FCM receives the full request,
|
||||
// then close without waiting for the response.
|
||||
stream_socket_shutdown($fp, STREAM_SHUT_WR);
|
||||
fclose($fp);
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
21
app/Controller/Traits/ExtraGroupExpenseTrait.php
Normal file
21
app/Controller/Traits/ExtraGroupExpenseTrait.php
Normal file
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
|
||||
trait ExtraGroupExpenseTrait
|
||||
{
|
||||
public function checkExtraGroupExpense($extras, $expense_title)
|
||||
{
|
||||
$result = 99;
|
||||
$extras_value = 0;
|
||||
if (!empty($extras)) {
|
||||
$extras_array = explode("|", $extras);
|
||||
$extras_value = $extras_array[0];
|
||||
}
|
||||
if ($extras_value == 1 || $expense_title == 'Balance Expense') {
|
||||
$result = 1;
|
||||
} elseif ($extras_value == 2) {
|
||||
$result = 2;
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
210
app/Controller/Traits/ImageUploadTrait.php
Normal file
210
app/Controller/Traits/ImageUploadTrait.php
Normal file
@@ -0,0 +1,210 @@
|
||||
<?php
|
||||
|
||||
use Kreait\Firebase\Factory;
|
||||
use Kreait\Firebase\ServiceAccount;
|
||||
|
||||
trait ImageUploadTrait
|
||||
{
|
||||
|
||||
protected $firebase;
|
||||
|
||||
private function getFirebaseInstance()
|
||||
{
|
||||
// $jsonfilePath = APP . DS . 'Config' . DS . FIREBASE_SECRET;
|
||||
$jsonfilePath = FIREBASE_SECRET;
|
||||
$serviceAccount = ServiceAccount::fromJsonFile($jsonfilePath);
|
||||
$this->firebase = (new Factory)->withServiceAccount($serviceAccount)->create();
|
||||
}
|
||||
|
||||
public function uploadBase64Image($base64Image, $storagePath)
|
||||
{
|
||||
|
||||
try {
|
||||
|
||||
$this->getFirebaseInstance();
|
||||
|
||||
$imageData = base64_decode($base64Image);
|
||||
$storage = $this->firebase->getStorage();
|
||||
$bucket = $storage->getBucket();
|
||||
|
||||
$object = $bucket->upload($imageData, [
|
||||
'name' => $storagePath
|
||||
]);
|
||||
|
||||
if ($object) {
|
||||
$imageUrl = "https://storage.googleapis.com/{$bucket->name()}/{$storagePath}";
|
||||
return ['status' => true, 'message' => 'Uploaded', 'url' => $imageUrl];
|
||||
} else {
|
||||
return ['status' => false, 'message' => 'Upload failed.'];
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
return ['status' => false, 'message' => 'Error uploading image: ' . $e->getMessage()];
|
||||
}
|
||||
}
|
||||
|
||||
public function deleteFolder($storagePath)
|
||||
{
|
||||
|
||||
try {
|
||||
|
||||
$this->getFirebaseInstance();
|
||||
|
||||
$storage = $this->firebase->getStorage();
|
||||
$bucket = $storage->getBucket();
|
||||
$objects = $bucket->object($storagePath);
|
||||
|
||||
$objects = $bucket->objects([
|
||||
'prefix' => $storagePath
|
||||
]);
|
||||
|
||||
$filesDeleted = 0;
|
||||
|
||||
foreach ($objects as $object) {
|
||||
$object->delete();
|
||||
$filesDeleted++;
|
||||
}
|
||||
|
||||
if ($filesDeleted > 0) {
|
||||
return ['status' => true, 'message' => "Deleted folder :{$storagePath}"];
|
||||
} else {
|
||||
return ['status' => false, 'message' => "No files found in the folder."];
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
return ['status' => false, 'message' => $e->getMessage()];
|
||||
}
|
||||
}
|
||||
|
||||
public function singleImageDelete($storagePath)
|
||||
{
|
||||
try {
|
||||
|
||||
$this->getFirebaseInstance();
|
||||
|
||||
$storage = $this->firebase->getStorage();
|
||||
$bucket = $storage->getBucket();
|
||||
$object = $bucket->object($storagePath);
|
||||
$filesDeleted = 0;
|
||||
|
||||
if ($object->exists()) {
|
||||
$object->delete();
|
||||
$filesDeleted++;
|
||||
}
|
||||
|
||||
if ($filesDeleted > 0) {
|
||||
return ['status' => true, 'message' => "File deleted."];
|
||||
} else {
|
||||
return ['status' => false, 'message' => "No file found."];
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
|
||||
return ['status' => false, 'message' => $e->getMessage()];
|
||||
}
|
||||
}
|
||||
|
||||
public function downloadImage($basePath, $expire_time='+2 days',$save_path=null)
|
||||
{
|
||||
try {
|
||||
|
||||
$this->getFirebaseInstance();
|
||||
|
||||
$storage = $this->firebase->getStorage();
|
||||
$bucket = $storage->getBucket();
|
||||
|
||||
// Get object from Firebase Storage
|
||||
$zipPath = $basePath . "receipts_" . time() . ".zip";
|
||||
$objects = $bucket->objects([
|
||||
'prefix' => $basePath
|
||||
]);
|
||||
|
||||
$files = [];
|
||||
|
||||
foreach ($objects as $object) {
|
||||
|
||||
$name = $object->name();
|
||||
|
||||
// skip "folder itself"
|
||||
if (substr($name, -1) === '/') {
|
||||
continue;
|
||||
}
|
||||
|
||||
$files[] = $object;
|
||||
}
|
||||
|
||||
$zip = new ZipArchive();
|
||||
$zipFile = TMP . "receipts_" . time() . ".zip";
|
||||
|
||||
$zip->open($zipFile, ZipArchive::CREATE);
|
||||
|
||||
foreach ($files as $file) {
|
||||
|
||||
$fullPath = $file->name();
|
||||
|
||||
if (strpos($fullPath, $basePath) === 0) {
|
||||
$relativePath = substr($fullPath, strlen($basePath));
|
||||
} else {
|
||||
$relativePath = basename($fullPath);
|
||||
}
|
||||
|
||||
$content = $file->downloadAsString();
|
||||
|
||||
$zip->addFromString($relativePath, $content);
|
||||
}
|
||||
|
||||
$zip->close();
|
||||
$zipObject = $bucket->upload(
|
||||
file_get_contents($zipFile),
|
||||
[
|
||||
'name' => $save_path
|
||||
]
|
||||
);
|
||||
if (file_exists($zipFile)) {
|
||||
unlink($zipFile);
|
||||
}
|
||||
$expiresAt = new DateTime($expire_time);
|
||||
$downloadUrl = $zipObject->signedUrl($expiresAt);
|
||||
|
||||
return [
|
||||
'status' => true,
|
||||
'message' => 'Downloaded successfully.',
|
||||
'download_url' => $downloadUrl,
|
||||
'file_path' => $zipPath,
|
||||
'expires_at' => $expiresAt->format('Y-m-d H:i:s')
|
||||
|
||||
];
|
||||
|
||||
} catch (\Exception $e) {
|
||||
|
||||
return [
|
||||
'status' => false,
|
||||
'message' => 'Error downloading image: ' . $e->getMessage()
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
public function downloadLink($downloadPath, $expire_time='+2 days')
|
||||
{
|
||||
try {
|
||||
|
||||
$this->getFirebaseInstance();
|
||||
|
||||
$storage = $this->firebase->getStorage();
|
||||
$bucket = $storage->getBucket();
|
||||
|
||||
$object = $bucket->object($downloadPath);
|
||||
|
||||
$expiresAt = new DateTime($expire_time);
|
||||
$downloadUrl = $object->signedUrl($expiresAt,[
|
||||
'version' => 'v4'
|
||||
]);
|
||||
|
||||
return $downloadUrl;
|
||||
|
||||
} catch (\Exception $e) {
|
||||
|
||||
return [
|
||||
'status' => false,
|
||||
'message' => 'Error downloading image: ' . $e->getMessage()
|
||||
];
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user