MOON
Server: Apache
System: Linux e2e-78-16.ssdcloudindia.net 3.10.0-1160.45.1.el7.x86_64 #1 SMP Wed Oct 13 17:20:51 UTC 2021 x86_64
User: imensosw (1005)
PHP: 7.4.33
Disabled: exec,passthru,shell_exec,system
Upload Files
File: /home/imensosw/www/mpl.imenso.co/app/Models/ARCampaign.php
<?php

namespace App\Models;

use App\Services\Vuforia;
use Carbon\Carbon;
use GuzzleHttp\Client as GuzzleClient;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;
use Vimeo\Vimeo;

class ARCampaign extends Model
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    public $table = 'ar_campaigns';

    /**
     * The attributes that should be mutated to dates.
     *
     * @var array
     */
    protected $dates = [
        'created_at',
        'updated_at',
    ];

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name',
        'profile_id',
        'user_id',
        'sales_url',
        'button_text',
        'video',
        'marker_id',
        'target_id',
    ];

    public function user()
    {
        return $this->belongsTo(User::class);
    }

    public function profile()
    {
        return $this->belongsTo(User::class, 'profile_id');
    }

    public function getVideoUrlAttribute()
    {
        $video = explode(':', $this->video);
        if ($video[0] == 'youtube') {
            return 'https://www.youtube.com/watch?v='.$video[1];
        } else {
            return 'https://vimeo.com/'.$video[1];
        }
    }

    public function getVideoEmbedUrlAttribute()
    {
        $video = explode(':', $this->video);
        if ($video[0] == 'youtube') {
            return 'https://www.youtube.com/embed/'.$video[1];
        } else {
            return 'https://player.vimeo.com/video/'.$video[1];
        }
    }

    public function getVideoIdAttribute()
    {
        $video = explode(':', $this->video);

        return $video[1];
    }

    public function getVideoHostAttribute()
    {
        $video = explode(':', $this->video);

        return $video[0];
    }

    public function getCustomMarkerUrlAttribute()
    {
        $markerArr = Storage::disk('s3')->allFiles('images/qrcodes/'.$this->marker_id.'-CM.*');
        if ($markerArr) {
            $markerPath = $markerArr[0];
            $ext = pathinfo($markerPath, PATHINFO_EXTENSION);
            $markerUrl = config('filesystems.disks.s3.url') . 'images/qrcodes/' . $this->marker_id . '-CM.' .$ext;

            return $markerUrl;
        }

        return false;
    }

    /*
     * Get views count and store in cache
     *
     * @return (int) $views
     */
    public function getVideoViewsAttribute()
    {
        try {
            $views = Cache::get($this->video_id, 0);
            if (! Cache::has($this->video_id)) {
                $vuforia = new Vuforia();
                $summaryesp = $vuforia->getMarkerSummaryReport($this->target_id);
                $summary = json_decode($summaryesp);
                $views = number_format_Short($summary->total_recos);
//            if ($this->video_host == 'youtube') {
//                $json      = file_get_contents("https://www.googleapis.com/youtube/v3/videos?part=statistics&id=" . $this->video_id . "&key=" . config('services.youtube.api_key'));
//                $videoData = json_decode($json);
//                $views     = $videoData->items[0]->statistics->viewCount;
//            } elseif ($this->video_host == 'vimeo') {
//                $vimeo = new Vimeo(config('vimeo.connections.main.client_id'),
//                    config('vimeo.connections.main.client_secret'));
//                $vimeo->setToken(config('vimeo.connections.main.access_token'));
//                $videoData = $vimeo->request("/videos/" . $this->video_id);
//                $body      = $videoData['body'];
//                if (isset($body['stats'])) {
//                    $stats = $body['stats'];
//                    $views = $stats['plays'];
//                } else {
//                    $views = $body['error'];
//                }
//            }
                $expiresAt = Carbon::now()->addMinutes(360);
                Cache::put($this->video_id, $views, $expiresAt);
            }

            return $views;
        } catch (\Exception $e) {
            Log::error('Could not get video views for campaign', [
                'campaign_id' => $this->id,
                'message'     => $e->getMessage(),
                'response'    => $summary,
            ]);

            return $views;
        }
    }

    public static function generateFileId()
    {
        return time().'-'.md5(uniqid());
    }

    public static function generateQR($fileId)
    {
        $client = new GuzzleClient();

        $res = $client->request('GET', 'http://api.qrserver.com/v1/create-qr-code', [
            'verify' => false,
            'query'  => [
                'data'   => $fileId,
                'size'   => '1000x1000',
                'ecc '   => 'H',
                'format' => 'jpg',
            ],
        ]);
        $response = (string) $res->getBody();

        return $response;
    }

    /**
     * Generate a QR code, merge with universal marker template and store.
     *
     * @return array
     */
    public static function generateUniversalMarker()
    {
        $fileId = self::generateFileId();

        $QRPath =  'images/qrcodes/';
        $QRfilename = $fileId.'-QR.jpg';
        $QRfilepath = $QRPath.$QRfilename;

        $qrCode = self::generateQR($fileId);
        Storage::disk('s3')->put($QRfilepath, $qrCode);

        $qrFileUrl = config('filesystems.disks.s3.url') . '/' . $QRfilepath;

        $qrcodeImg = imagecreatefromjpeg($qrFileUrl);

        $baseImgWhite = imagecreatefrompng(public_path('/images/11382_AR_Marker-Large_White.png'));
        $baseImgBlack = imagecreatefrompng(public_path('/images/11382_AR_Markers-Large-Black.png'));

        imagealphablending($baseImgWhite, true);
        imagesavealpha($baseImgWhite, true);
        imagealphablending($baseImgBlack, true);
        imagesavealpha($baseImgBlack, true);

        imagecopyresampled($baseImgWhite, $qrcodeImg, 555, 1675, 0, 0, 2170, 2170, 1000, 1000);
        $markerFilenameWhite = $fileId.'-UM-W.png';
        ob_start();
        imagepng($baseImgWhite);
        $markerImageWhite = ob_get_contents();
        ob_end_clean();
        Storage::disk('s3')->put('images/qrcodes/'.$markerFilenameWhite, $markerImageWhite);

        imagecopyresampled($baseImgBlack, $qrcodeImg, 555, 1675, 0, 0, 2170, 2170, 1000, 1000);
        $markerFilenameBlack = $fileId.'-UM-B.png';
        ob_start();
        imagepng($baseImgBlack);
        $markerImageBlack = ob_get_contents();
        ob_end_clean();
        Storage::disk('s3')->put('images/qrcodes/'.$markerFilenameBlack, $markerImageBlack);

        return $fileId;
    }

    public function delete()
    {
        $vuforia = new Vuforia();
        $vuforia->removeMarker($this->target_id);

        $path = public_path('images/qrcodes').DIRECTORY_SEPARATOR;
        $markerArr = glob($path.$this->marker_id.'*.*');
        foreach ($markerArr as $marker) {
            unlink($marker);
        }

        if ($this->video_host == 'vimeo') {
            $vimeo = new Vimeo(config('vimeo.connections.main.client_id'),
                config('vimeo.connections.main.client_secret'));
            $vimeo->setToken(config('vimeo.connections.main.access_token'));
            $vimeo->request('/videos/'.$this->video_id, [], 'DELETE');
        }

        return parent::delete();
    }
}