first commit

This commit is contained in:
2025-11-06 06:55:15 +00:00
commit 0b603376d2
107 changed files with 17279 additions and 0 deletions

View File

@@ -0,0 +1,22 @@
<?php
namespace App\Traits;
trait ApiResponseHelper
{
/*
* @param array|string $data
* @param string $message
* @param int $code
* @return \Illuminate\Http\JsonResponse
*/
protected function apiResponse(bool $status, string $message = null, $data = [], $code = 200)
{
return response()->json([
'status' => $status,
'message' => $message ?? '',
'data' => $data ?? ''
], $code);
}
}

131
app/Traits/ImageHelper.php Normal file
View File

@@ -0,0 +1,131 @@
<?php
namespace App\Traits;
use Illuminate\Http\File;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use Intervention\Image\Facades\Image;
trait ImageHelper
{
use MimeToExtensionHelper;
// Direct paths without symlinks
public $uploads_path = "uploads/";
public $public_uploads_path = "public/uploads/";
public function uploadPDF($file, $prefix): string
{
$fileName = '';
if ($file) {
$extension = $file->getClientOriginalExtension();
$fileName = $prefix . '_' . Str::random(30) . '.' . $extension;
// Create directory in public folder if it doesn't exist
$uploadPath = public_path($this->uploads_path);
if (!file_exists($uploadPath)) {
mkdir($uploadPath, 0755, true);
}
// Move file directly to public directory
$file->move($uploadPath, $fileName);
}
return '/' . $this->uploads_path . $fileName;
}
public function uploadFile($file, $prefix, bool $isEncoded): string
{
if ($isEncoded) {
$file = $this->create_file_from_base64($file);
}
$imagePath = '';
if ($file) {
$extension = $file->getClientOriginalExtension();
$fileName = $prefix . '_' . Str::random(30) . '.' . $extension;
// Create directory in public folder if it doesn't exist
$uploadPath = public_path($this->uploads_path);
if (!file_exists($uploadPath)) {
mkdir($uploadPath, 0755, true);
}
// Full path to save the file
$filePath = $uploadPath . $fileName;
// Process with Intervention Image
$img = Image::make($file);
// You can add image manipulation here if needed
// $img->resize(800, null, function ($constraint) {
// $constraint->aspectRatio();
// $constraint->upsize();
// });
// Save directly to public directory
$imagePath = $img->save($filePath) ? '/' . $this->uploads_path . $fileName : '';
}
return $imagePath;
}
public function create_file_from_base64($base64File): UploadedFile
{
$fileData = base64_decode(Arr::last(explode(',', $base64File)));
// Get MimeType
$fileInfo = finfo_open();
$mimeType = finfo_buffer($fileInfo, $fileData, FILEINFO_MIME_TYPE);
// Get Extension from MimeType
$ext = $this->mime2ext($mimeType);
// Save it to temporary dir first
$tempFilePath = sys_get_temp_dir() . '/' . Str::uuid()->toString() . '.' . $ext;
file_put_contents($tempFilePath, $fileData);
$tempFileObject = new File($tempFilePath);
return new UploadedFile(
$tempFileObject->getPathname(),
$tempFileObject->getFilename(),
$tempFileObject->getMimeType(),
0,
true
);
}
public function getFileFullUrl($file): null|string
{
if ($file) {
return App::make('url')->to('/') . $file;
}
return NULL;
}
public function updateFile($file, $existingFile, $prefix, bool $isEncoded): bool|string
{
if ($existingFile) {
$this->deleteFile($existingFile);
}
return $this->uploadFile($file, $prefix, $isEncoded);
}
public function deleteFile($file): bool
{
// Remove leading slash if present
if (str_starts_with($file, '/')) {
$file = substr($file, 1);
}
$filePath = public_path($file);
if (file_exists($filePath)) {
unlink($filePath);
return true;
}
return false;
}
}

View File

@@ -0,0 +1,191 @@
<?php
namespace App\Traits;
trait MimeToExtensionHelper {
public function mime2ext($mime): bool|string
{
$mime_map = [
'video/3gpp2' => '3g2',
'video/3gp' => '3gp',
'video/3gpp' => '3gp',
'application/x-compressed' => '7zip',
'audio/x-acc' => 'aac',
'audio/ac3' => 'ac3',
'application/postscript' => 'ai',
'audio/x-aiff' => 'aif',
'audio/aiff' => 'aif',
'audio/x-au' => 'au',
'video/x-msvideo' => 'avi',
'video/msvideo' => 'avi',
'video/avi' => 'avi',
'application/x-troff-msvideo' => 'avi',
'application/macbinary' => 'bin',
'application/mac-binary' => 'bin',
'application/x-binary' => 'bin',
'application/x-macbinary' => 'bin',
'image/bmp' => 'bmp',
'image/x-bmp' => 'bmp',
'image/x-bitmap' => 'bmp',
'image/x-xbitmap' => 'bmp',
'image/x-win-bitmap' => 'bmp',
'image/x-windows-bmp' => 'bmp',
'image/ms-bmp' => 'bmp',
'image/x-ms-bmp' => 'bmp',
'application/bmp' => 'bmp',
'application/x-bmp' => 'bmp',
'application/x-win-bitmap' => 'bmp',
'application/cdr' => 'cdr',
'application/coreldraw' => 'cdr',
'application/x-cdr' => 'cdr',
'application/x-coreldraw' => 'cdr',
'image/cdr' => 'cdr',
'image/x-cdr' => 'cdr',
'zz-application/zz-winassoc-cdr' => 'cdr',
'application/mac-compactpro' => 'cpt',
'application/pkix-crl' => 'crl',
'application/pkcs-crl' => 'crl',
'application/x-x509-ca-cert' => 'crt',
'application/pkix-cert' => 'crt',
'text/css' => 'css',
'text/x-comma-separated-values' => 'csv',
'text/comma-separated-values' => 'csv',
'application/vnd.msexcel' => 'csv',
'application/x-director' => 'dcr',
'application/vnd.openxmlformats-officedocument.wordprocessingml.document' => 'docx',
'application/x-dvi' => 'dvi',
'message/rfc822' => 'eml',
'application/x-msdownload' => 'exe',
'video/x-f4v' => 'f4v',
'audio/x-flac' => 'flac',
'video/x-flv' => 'flv',
'image/gif' => 'gif',
'application/gpg-keys' => 'gpg',
'application/x-gtar' => 'gtar',
'application/x-gzip' => 'gzip',
'application/mac-binhex40' => 'hqx',
'application/mac-binhex' => 'hqx',
'application/x-binhex40' => 'hqx',
'application/x-mac-binhex40' => 'hqx',
'text/html' => 'html',
'image/x-icon' => 'ico',
'image/x-ico' => 'ico',
'image/vnd.microsoft.icon' => 'ico',
'text/calendar' => 'ics',
'application/java-archive' => 'jar',
'application/x-java-application' => 'jar',
'application/x-jar' => 'jar',
'image/jp2' => 'jp2',
'video/mj2' => 'jp2',
'image/jpx' => 'jp2',
'image/jpm' => 'jp2',
'image/jpeg' => 'jpeg',
'image/pjpeg' => 'jpeg',
'application/x-javascript' => 'js',
'application/json' => 'json',
'text/json' => 'json',
'application/vnd.google-earth.kml+xml' => 'kml',
'application/vnd.google-earth.kmz' => 'kmz',
'text/x-log' => 'log',
'audio/x-m4a' => 'm4a',
'application/vnd.mpegurl' => 'm4u',
'audio/midi' => 'mid',
'application/vnd.mif' => 'mif',
'video/quicktime' => 'mov',
'video/x-sgi-movie' => 'movie',
'audio/mpeg' => 'mp3',
'audio/mpg' => 'mp3',
'audio/mpeg3' => 'mp3',
'audio/mp3' => 'mp3',
'video/mp4' => 'mp4',
'video/mpeg' => 'mpeg',
'application/oda' => 'oda',
'audio/ogg' => 'ogg',
'video/ogg' => 'ogg',
'application/ogg' => 'ogg',
'application/x-pkcs10' => 'p10',
'application/pkcs10' => 'p10',
'application/x-pkcs12' => 'p12',
'application/x-pkcs7-signature' => 'p7a',
'application/pkcs7-mime' => 'p7c',
'application/x-pkcs7-mime' => 'p7c',
'application/x-pkcs7-certreqresp' => 'p7r',
'application/pkcs7-signature' => 'p7s',
'application/pdf' => 'pdf',
'application/octet-stream' => 'pdf',
'application/x-x509-user-cert' => 'pem',
'application/x-pem-file' => 'pem',
'application/pgp' => 'pgp',
'application/x-httpd-php' => 'php',
'application/php' => 'php',
'application/x-php' => 'php',
'text/php' => 'php',
'text/x-php' => 'php',
'application/x-httpd-php-source' => 'php',
'image/png' => 'png',
'image/x-png' => 'png',
'application/powerpoint' => 'ppt',
'application/vnd.ms-powerpoint' => 'ppt',
'application/vnd.ms-office' => 'ppt',
'application/msword' => 'doc',
'application/vnd.openxmlformats-officedocument.presentationml.presentation' => 'pptx',
'application/x-photoshop' => 'psd',
'image/vnd.adobe.photoshop' => 'psd',
'audio/x-realaudio' => 'ra',
'audio/x-pn-realaudio' => 'ram',
'application/x-rar' => 'rar',
'application/rar' => 'rar',
'application/x-rar-compressed' => 'rar',
'audio/x-pn-realaudio-plugin' => 'rpm',
'application/x-pkcs7' => 'rsa',
'text/rtf' => 'rtf',
'text/richtext' => 'rtx',
'video/vnd.rn-realvideo' => 'rv',
'application/x-stuffit' => 'sit',
'application/smil' => 'smil',
'text/srt' => 'srt',
'image/svg+xml' => 'svg',
'application/x-shockwave-flash' => 'swf',
'application/x-tar' => 'tar',
'application/x-gzip-compressed' => 'tgz',
'image/tiff' => 'tiff',
'text/plain' => 'txt',
'text/x-vcard' => 'vcf',
'application/videolan' => 'vlc',
'text/vtt' => 'vtt',
'audio/x-wav' => 'wav',
'audio/wave' => 'wav',
'audio/wav' => 'wav',
'application/wbxml' => 'wbxml',
'video/webm' => 'webm',
'audio/x-ms-wma' => 'wma',
'application/wmlc' => 'wmlc',
'video/x-ms-wmv' => 'wmv',
'video/x-ms-asf' => 'wmv',
'application/xhtml+xml' => 'xhtml',
'application/excel' => 'xl',
'application/msexcel' => 'xls',
'application/x-msexcel' => 'xls',
'application/x-ms-excel' => 'xls',
'application/x-excel' => 'xls',
'application/x-dos_ms_excel' => 'xls',
'application/xls' => 'xls',
'application/x-xls' => 'xls',
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' => 'xlsx',
'application/vnd.ms-excel' => 'xlsx',
'application/xml' => 'xml',
'text/xml' => 'xml',
'text/xsl' => 'xsl',
'application/xspf+xml' => 'xspf',
'application/x-compress' => 'z',
'application/x-zip' => 'zip',
'application/zip' => 'zip',
'application/x-zip-compressed' => 'zip',
'application/s-compressed' => 'zip',
'multipart/x-zip' => 'zip',
'text/x-scriptzsh' => 'zsh',
];
return isset($mime_map[$mime]) === true ? $mime_map[$mime] : false;
}
}

View File

@@ -0,0 +1,121 @@
<?php
namespace App\Traits;
use App\Models\Setting;
trait OneSignalHelper
{
public function setupEnvironmentForServiceProvider(): array
{
$appKey = "7dc8528d-f184-4902-a478-7785b795d5df";
$secretKey = "os_v2_app_pxeffdprqreqfjdyo6c3pfov34dbi44ywtle6xewpet3244pdtkzvrzrc63gl4hmeavupgz3i6hch5wge6f7u2li4dvylyhjp5d6mna";
return [
'appKey' => $appKey,
'secretKey' => $secretKey
];
}
public function setupEnvironmentForClients(): array
{
$appKey = "1abab490-0768-468e-92ca-1512f8b65f90";
$secretKey = "os_v2_app_dk5ljeahnbdi5ewkcujprns7sc4xhmzg6iaujlvl3bdnayojmdoeubourulpnfjru7pf55mlgydnvdhqqmxgw2ipbvcivmn3ravacfy";
return [
'appKey' => $appKey,
'secretKey' => $secretKey
];
}
public function sendAppNotification(string $notificationTitle, string $notificationContent, string $userType)
{
$configuration = $userType === "client"
? $this->setupEnvironmentForClients()
: $this->setupEnvironmentForServiceProvider();
$fields = [
'app_id' => $configuration['appKey'],
'included_segments' => ['All'],
'headings' => ['en' => $notificationTitle],
'contents' => ['en' => $notificationContent],
];
$jsonFields = json_encode($fields);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://onesignal.com/api/v1/notifications");
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json; charset=utf-8',
'Authorization: Basic ' . $configuration['secretKey']
]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonFields);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
}
public function sendNotificationToSingleUser(
string $notificationTitle,
string $notificationContent,
string $notificationId,
string $userType
) {
$fcmServerKey = "";
return;
if (!empty($notificationId)) {
return response()->json(['error' => 'User or FCM token not found.'], 404);
}
$fcmToken = $user->fcm_token;
$notification = [
'to' => $fcmToken,
'notification' => [
'title' => $notificationTitle,
'body' => $notificationContent,
'sound' => 'default',
],
'data' => [
'notification_id' => $notificationId,
'click_action' => 'FLUTTER_NOTIFICATION_CLICK'
],
'priority' => 'high'
];
$headers = [
'Authorization: key=' . $fcmServerKey,
'Content-Type: application/json'
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://fcm.googleapis.com/fcm/send');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // You can set this to true in production
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($notification));
$result = curl_exec($ch);
$error = curl_error($ch);
curl_close($ch);
if ($error) {
return response()->json(['error' => $error], 500);
}
return response()->json(['response' => json_decode($result)], 200);
}
}