HEX
Server: Apache/2.4.41 (Ubuntu)
System: Linux ip-172-31-42-149 5.15.0-1084-aws #91~20.04.1-Ubuntu SMP Fri May 2 07:00:04 UTC 2025 aarch64
User: ubuntu (1000)
PHP: 7.4.33
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: /var/www/vhost/disk-apps/qas.sports-crowd.com/app/Http/Controllers/SmsNotificationsController.php
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Notification;
use App\NotificationTag;
use App\Tag;
use App\User;
use App\TargetNotification;
use App\UserTag;
use App\Country;
use App\SmsParameter;
use DB;
use Datatables;
use App\Http\Controllers\UtilController;

class SmsNotificationsController extends Controller
{
    private $api;
    private $userApi;
    private $passApi;
    private $ticketBuyMessage;
    private $shopBuyMessage;
    private $active;

    public function __construct()
    {
        $smsParameter = SmsParameter::first();
        if ($smsParameter) {
            $this->api              = $smsParameter->api;
            $this->userApi          = $smsParameter->user;
            $this->passApi          = $smsParameter->pass;
            $this->ticketBuyMessage = $smsParameter->ticket_buy_message;
            $this->shopBuyMessage   = $smsParameter->shop_buy_message;
            $this->active           = $smsParameter->active;
        }
    }

    public function activeStatusSendSms()
    {
        return $this->active;
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */

    public function index()
    {
        return view('sms_notification.smsNotifications');
    }

    public function indexAdd()
    {
        $targets  = TargetNotification::all();
        $multiselectItems = Tag::where('active', true)->get();
        return view('sms_notification.addSmsNotifications')
            ->with('targets', $targets)
            ->with('multiselectItems', $multiselectItems);
    }


    public function indexAddApi()
    {
        $smsParameter = SmsParameter::first();
        return view('sms_notification.smsParameters')
            ->with('smsParameter', $smsParameter);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(Request $request)
    {
        $id = Auth::user()->id;
        $country = Country::select('id', 'code')->where('active', true)->first();

        $notification = new Notification();
        $notification->message = $request["message"];
        $notification->when_send = $request["when_send"];
        $notification->is_pending = 1;
        $notification->admin_id = $id;
        $notification->user_id = $request["user_id"];
        $notification->link = $request["link"];
        $notification->target_notification_id = $request["target_id"];
        $notification->notifications_origin = 'SMS';
        $notification->save(); //Guarda el nuevo objeto

        $when_send = $request["when_send"];

        $data = new \stdClass();
        $data->link = $notification->link;
        if ($request["user_id"] != null) {
            $user = User::select('id', 'phone')->where('id', $request["user_id"])->first();
            $this->sendToUserTextMessage($country->code, $when_send, $request["message"], $user->phone, $data->link);
        } else if ($request["tags"] != null) {
            foreach ($request["tags"] as $tagId) {
                NotificationTag::create([
                    'tag_id'            => $tagId,
                    'notification_id'   => $notification->id
                ]);

                $users = UserTag::select('users.id', 'users.phone')->join('users', 'users.id', '=', 'user_tags.user_id')
                    ->where('user_tags.tag_id', $tagId)->whereNull('users.deleted_at')->whereNotNull('users.phone')->get();
                foreach ($users as $user) {
                    $this->sendToUserTextMessage($country->code, $when_send, $request["message"], $user->phone, $data->link);
                }
            }
        } else {
            $allUsers = User::select('id', 'phone')->where('active', true)->get();
            $this->sendToAllTextMessage($country->code, $when_send, $request["message"], $allUsers, $data->link);
        }

        $logObject = $notification;
        $this->registerLog(Auth::user()->id, 'Creó una notificación de mensaje de texto ', json_encode($logObject), "Create", $this->getModule($request));

        if ($notification) {
            return array('r' => true, 'd' => null, 'm' => trans('messages.screen_create_notifications_tag9'));
        } else {
            return array('r' => false, 'd' => null, 'm' => trans('messages.screen_create_notifications_tag10'));
        }
    }

    public function tableFilter()
    {
        $obj = Notification::select(
            'notifications.id',
            'notifications.message',
            'notifications.when_send',
            'notifications.is_pending',
            'notifications.link',
            'notifications.admin_id',
            'notifications.user_id',
            'notifications.target_notification_id',
            'notifications.target_city',
            'notifications.created_at as created',
            'notifications.notifications_origin',
            DB::raw('GROUP_CONCAT(DISTINCT(tags.name)) AS segmentation')
        )
            ->with('admin', 'user', 'target_notification')
            ->leftjoin('notification_tags', 'notifications.id', '=', 'notification_tags.notification_id')
            ->leftjoin('tags', function ($join) {
                $join->on('tags.id', '=', 'notification_tags.tag_id')->where('tags.active', 1);
            })
            ->whereNull('notifications.deleted_at')
            ->where('notifications_origin', 'SMS')
            ->groupBy(
                'notifications.id',
                'notifications.message',
                'notifications.when_send',
                'notifications.is_pending',
                'notifications.link',
                'notifications.admin_id',
                'notifications.user_id',
                'notifications.target_notification_id',
                'notifications.target_city',
                'notifications.created_at',
                'notifications.notifications_origin'
            );
        return Datatables::of($obj)
            ->editColumn('user', function ($obj) {
                if ($obj->user) {
                    return $obj->user->first_name . ' ' . $obj->user->last_name;
                } else {
                    return "";
                }
            })
            ->editColumn('segment', function ($obj) {
                if ($obj->target_notification) {
                    return $obj->target_notification->name;
                } else {
                    return "";
                }
            })
            ->editColumn('name', function ($obj) {
                return $obj->admin->first_name . ' ' . $obj->admin->last_name;
            })
            ->editColumn('created', function ($obj) {
                return \Carbon\Carbon::parse($obj->created)->format('Y-m-d h:i:s A');
            })
            ->editColumn('when_send', function ($obj) {
                if ($obj->when_send) {
                    return \Carbon\Carbon::parse($obj->when_send)->format('Y-m-d h:i:s A');
                } else {
                    return 'No tiene fecha programada';
                }
            })
            ->rawColumns(['name', 'user', 'segment', 'when_send', 'segmentation', 'notifications_origin'])
            ->make(true);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request)
    {
        if (SmsParameter::where('id', $request->input('id'))->first()) {
            SmsParameter::where('id', $request->input('id'))->update($request->all());
            return response(array("r" => true, "type" => "success", "title" => "", "m" => __('messages.updated_successfully'), "data" => $request->input('id')));
        } else {
            $model = SmsParameter::create($request->all());
            return response(array("r" => true, "type" => "success", "title" => "", "m" => __('messages.created_successfully'), "data" => $model->id));
        }
    }

    private function requiresUserAuth()
    {
        return 'Basic ' . base64_encode($this->userApi . ':' . $this->passApi);
    }

    public function sendToSegmentsTextMessage() {}

    public function sendToUserTextMessage($countryCode, $schedule, $message, $cellNumber, $url = '')
    {

        $cellNumber = strval($cellNumber);
        $countryCode = preg_replace('([^A-Za-z0-9])', '', $countryCode);

        $params = array(
            'country' => $countryCode,
            'dateToSend' => $schedule,
            'message' => $message,
            'messageFormat' => 1,
            'addresseeList' => array(
                array(
                    'mobile' => $cellNumber,
                    'url' => $url ? $url : ''
                )
            )
        );

        return $this->sendNotificationSms($params);
    }

    public function sendToAllTextMessage($countryCode, $schedule, $message, $users, $url = '')
    {
        $countryCode = preg_replace('([^A-Za-z0-9])', '', $countryCode);
        $count = 1;
        $phones = array();
        foreach ($users as $key => $value) {
            if ($value->phone) {
                $data = array(
                    'mobile' => $value->phone,
                    'url' => $url ? $url : ''
                );
                $phones[] = $data;
                if ($count == 50 || ((count($users) - 1) == $key)) {
                    $params = array(
                        'country' => $countryCode,
                        'dateToSend' => $schedule,
                        'message' => $message,
                        'messageFormat' => 1,
                        'addresseeList' => $phones
                    );

                    $this->sendNotificationSms($params);
                    sleep(10);
                    $count = 0;
                    $phones = array();
                }
                $count++;
            }
        }
    }

    public function sendingNotificationBuyingTicket($userInfo, $link, $pin)
    {
        $message = $this->replaceWildcardsBuyTicket($this->ticketBuyMessage, $userInfo->first_name, config('app.name'), $pin);
        $util = new UtilController;
        $shortLink = $util->shorterUrl($link);

        $country = Country::where('active', true)->first();
        $countryCode = preg_replace('([^A-Za-z0-9])', '', $country->code);
        $cellNumber = strval($userInfo->phone);

        $params = array(
            'country' => $countryCode,
            'dateToSend' => '',
            'message' => $message,
            'messageFormat' => 1,
            'addresseeList' => array(
                array(
                    'mobile' => $cellNumber,
                    'url' => $shortLink ? $shortLink : ''
                )
            )
        );

        return $this->sendNotificationSmsTicketPurchase($params);
    }

    public function sendNotificationSmsTicketPurchase($parameters = [])
    {
        $responseGuzzle = $this->post($parameters);
        $body = (string) $responseGuzzle->getBody();
        $body = json_decode($body);
        return $body;
    }

    public function sendNotificationSms($parameters = [])
    {
        $responseGuzzle = $this->post($parameters);
        $body = (string) $responseGuzzle->getBody();
        $body = json_decode($body);
        if ($body->status == 1) {
            $valueTransactionId = $body->result->receivedRequests[0]->transactionId;
            $notification = Notification::latest('id')->first();
            $notification->transactionIdSms = $valueTransactionId;
            $notification->update();
        }
        return $body;
    }

    public function post($endPoint)
    {
        $clientGuzzle = new \GuzzleHttp\Client();
        $response = $clientGuzzle->post($this->api, [
            'body' => json_encode($endPoint),
            'headers' => [
                'Content-Type' => 'application/json',
                'Authorization' => $this->requiresUserAuth(),
            ],
        ]);
        return $response;
    }

    private function replaceWildcardsBuyTicket($message, $userName, $appName, $pin)
    {
        if ($message) {
            $message = preg_replace('/:userName/', $userName, $message);
            $message = preg_replace('/:appName/', $appName, $message);
            $message = preg_replace('/:pin/', $pin, $message);
        }
        return $message;
    }
}