%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /var/www/html/camillo/camillo-api-master/app/Services/
Upload File :
Create Path :
Current File : /var/www/html/camillo/camillo-api-master/app/Services/SmsService.php

<?php

namespace App\Services;

define("BASEURL", "https://api.skebby.it/API/v1.0/REST/");
define("MESSAGE_HIGH_QUALITY", "GP");
define("MESSAGE_MEDIUM_QUALITY", "TI");
define("MESSAGE_LOW_QUALITY", "SI");

class SmsService {

    protected $message;
    protected $recipients;
    protected $url;
    protected $sender;
    protected $language;
    protected $suffix;

    /**
     * SmsService constructor.
     * @param $model
     * @param $type
     * @param $suffix
     * @internal param $recipient
     */
    public function __construct($model, $type, $suffix, $relatedModel = null)
    {
        $this->recipients[] = "+39".$model->phone;
        $this->suffix = $suffix;
        $this->sender = env('APP_SMS_SENDER', "CAMILLO");
        $this->language = $model->language;

        switch($type){
            case "minor_association":
                $this->message = ($this->language == "it")
                    ? "Un nuovo minore è stato associato al tuo profilo su Camillo! Da questo momento fai parte del nucleo familiare di "
                    : "A new children has been associated to your profile on Camillo! Take good care of " ;
                break;
            case "sms_lost_password":
                $this->message = ($this->language == "it")
                    ? "Recupero password di Camillo! Clicca sul link per reimpostarla: "
                    : "Camillo Password recovery! Click on this link to reset: ";
                break;
            case "sms_invitation":
                $this->message = ($this->language == "it")
                    ? "Sei stato invitato su Camillo! Il tuo username è {$model->user->email}\nClicca sul link per accedere: "
                    : "You've been invited on Camillo! Your username is {$model->user->email}\nClick on this link to set your credentials: ";
                break;
            case "password_reset":
                $this->message = ($this->language == "it")
                    ? "La tua password di Camillo è stata resettata. Clicca qui per reimpostarla: "
                    : "Your password on Camillo has been resetted. Click here to reset: ";
                break;
            case "communication_sent":
                /** @var \App\Models\Classroom $relatedModel */
                $this->message = ($this->language == "it")
                    ? "Hai ricevuto un nuovo messaggio da {$relatedModel->classroom->institute->name}. Clicca qui per visualizzarlo: "
                    : "New communication from {$relatedModel->classroom->institute->name} received. Click here for details: ";
                break;
        };

        $this->message .= $this->suffix;
    }



    /**
     * Authenticates the user given it's username and password.
     * Returns the pair user_key, Session_key
     * @param $username
     * @param $password
     * @return array|null
     */
    function login($username, $password) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, BASEURL .
            'login?username=' . $username .
            '&password=' . $password);

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        $response = curl_exec($ch);
        $info = curl_getinfo($ch);
        curl_close($ch);

        if ($info['http_code'] != 200) {
            return null;
        }

        return explode(";", $response);
    }

    /**
     * Sends an SMS message
     * @param $auth
     * @param $sendSMS
     * @return mixed|null
     */
    function sendSMS($auth, $sendSMS) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, BASEURL . 'sms');
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Content-type: application/json',
            'user_key: ' . $auth[0],
            'Session_key: ' . $auth[1]
        ));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($sendSMS));
        $response = curl_exec($ch);
        $info = curl_getinfo($ch);
        curl_close($ch);

        if ($info['http_code'] != 201) {
            return null;
        }

        return json_decode($response);
    }

    /**
     *
     */
    public function send_invitation(){
        $auth = $this->login('antonio.savoini', 'Sinergidea123');
        $smsSent = $this->sendSMS($auth, array(
            "message" => $this->message,
            "message_type" => MESSAGE_MEDIUM_QUALITY,
            "returnCredits" => true,
            "recipient" => $this->recipients,
            "sender" => $this->sender
        ));

        if ($smsSent && $smsSent->result == "OK") {
            return true;
        }

        return false;
    }
}

Zerion Mini Shell 1.0