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/demo.sports-crowd.com/app/Http/Controllers/TiktokController.php
<?php

namespace App\Http\Controllers;

use Datatables;
use GuzzleHttp;
use App\VideoTiktok;
use App\SocialNetworkAccount;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Http;

class TiktokController extends Controller
{
    public function index()
    {
        return view('official_tiktok.list');
    }

    public function indexAdd()
    {
        $socialNetworkAccounts = SocialNetworkAccount::select('id', 'name')->where('active', true)->get();
        return view('official_tiktok.create', compact('socialNetworkAccounts'));
    }

    public function indexEdit($id)
    {
        $object = VideoTiktok::find($id);
        $socialNetworkAccounts = SocialNetworkAccount::select('id', 'name')->where('active', true)->get();
        return view('official_tiktok.edit', compact('object', 'socialNetworkAccounts'));
    }

    public function tableFilter()
    {
        DB::statement("SET sql_mode = ''");
        $obj = VideoTiktok::select(
            'video_tiktoks.id',
            'code',
            'link',
            'social_network_accounts.name AS social_network_accounts',
            'video_tiktoks.active',
            'video_tiktoks.created_at'
        )
            ->leftJoin('social_network_accounts', 'social_network_accounts.id', '=', 'video_tiktoks.social_network_account_id')
            ->groupBy('video_tiktoks.id');
        DB::statement("SET sql_mode = 'STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION'");

        return Datatables::of($obj)
            ->addColumn('actions', function ($obj) {
                return '
               <i class="fa fa-pencil iconMini" onClick="clickEdit(' . $obj->id . ')" data-id="' . $obj->id . '" title="Editar"></i>
               <i class="fa fa-trash iconMini" onClick="clickDelete(' . $obj->id . ')" data-id="' . $obj->id . '" title="Eliminar"></i>
              ';
            })
            ->editColumn('created_at', function ($obj) {
                return \Carbon\Carbon::parse($obj->created_at)->format('Y-m-d h:i:s A');
            })
            ->editColumn('active', function ($obj) {
                if ($obj->active == 0) {
                    return '<div class="switch"><label><div class="checkbox checbox-switch switch-success"> <label> No <input type="checkbox" class="chk" onChange="chk(' . $obj->id . ')" data-id="' . $obj->id . '" id="Checkactive' . $obj->id . '" name="Checkactivo" /> <span></span>Si </label></div> </label> </div>';
                } else {
                    return '<div class="switch"><label> <div class="checkbox checbox-switch switch-success"> <label>   No <input type="checkbox" class="chk" onChange="chk(' . $obj->id . ')" data-id="' . $obj->id . '" id="Checkactive' . $obj->id . '" name="Checkactivo" checked="" /><span></span> Si </label> </div>  </label> </div>';
                }
            })
            ->editColumn('link', function ($obj) {
                return 'https://www.tiktok.com/embed/' . $obj->code;
            })
            ->rawColumns(['active', 'actions', 'link'])
            ->make(true);
    }

    public function create(Request $request)
    {
        try {
            if (VideoTiktok::where('link', $request->input('link'))->first()) {
                return response(array("r" => false, "type" => "error", "title" => "Oops...", "m" => __('messages.already_exists', ['name' => $request->input('link')]), "data" => null));
            }
            if ($model = VideoTiktok::create($request->all())) {
                return response(array("r" => true, "type" => "success", "title" => "", "m" => __('messages.created_successfully'), "data" => $model->id));
            } else {
                return response(array("r" => false, "type" => "error", "title" => "Oops...", "m" => __('messages.error_creating'), "data" => null));
            }
        } catch (\Exception $e) {
            return response(
                array(
                    "r" => false,
                    "type" => "error",
                    "title" => "Oops...",
                    "m" => __($e->getMessage()),
                    "data" => null
                )
            );
        }
    }

    public function update(Request $request, $id)
    {
        try {
            $request['id'] = $id;
            if (VideoTiktok::where([['id', '!=', $id], ['link', $request->input('link')]])->first()) {
                return response(array("r" => false, "type" => "error", "title" => "Oops...", "m" => __('messages.already_exists', ['name' => $request->input('link')]), "data" => null));
            }
            if (VideoTiktok::where('id', $id)->update($request->all())) {
                return response(array("r" => true, "type" => "success", "title" => "", "m" => __('messages.updated_successfully'), "data" => $id));
            } else {
                return response(array("r" => false, "type" => "error", "title" => "Oops...", "m" => __('messages.error_updating'), "data" => null));
            }
        } catch (\Exception $e) {
            return response(
                array(
                    "r" => false,
                    "type" => "error",
                    "title" => "Oops...",
                    "m" => __($e->getMessage()),
                    "data" => null
                )
            );
        }
    }

    public function delete($id)
    {
        try {
            if (VideoTiktok::where('id', $id)->delete()) {
                return response(array("r" => true, "type" => "success", "title" => "", "m" => __('messages.deleted_successfully'), "data" => null));
            } else {
                return response(array("r" => false, "type" => "error", "title" => "Oops...", "m" => __('messages.error_removing'), "data" => null));
            }
        } catch (\Illuminate\Database\QueryException $e) {
            return response(array("r" => false, "type" => "error", "title" => "Oops...", "m" => __('messages.delete_relation_data'), "data" => null));
        }
    }

    public function activate(Request $request)
    {
        try {
            $id = $request['id'];
            $state = $request['state'];

            $tiktok = VideoTiktok::find($id);
            $tiktok->active = $state;
            $tiktok->update();

            return array('r' => true, 'd' => null, 'm' => __('messages.updated_successfully'));
        } catch (\Throwable $th) {
            return array('r' => false, 'd' => null, 'm' => __('messages.error_updating'));
        }
    }

    public function feed()
    {
        $client = new GuzzleHttp\Client();
        $url = 'https://open.tiktokapis.com/v2/oauth/token/';
        $response = $client->post($url, [
            'headers'           => ['Content-type' => 'application/x-www-form-urlencoded'],
            'form_params'       => [
                'client_key'    => 'awi27ztvv0tcnqzd',
                'client_secret' => 'DUYiKV6icTgxlZEN648aL0VrGW2TCEax',
                'grant_type'    => 'client_credentials',
            ],
        ]);

        $body = $response->getBody()->getContents();
        $body = json_decode($body);
        if (!$body->access_token) {
            return response(array("r" => false, "m" => 'Error obteniendo token'));
        }

        $url = 'https://open.tiktokapis.com/v2/video/list/?fields=id,title,video_description,duration,cover_image_url,embed_link';
        $response = $client->post($url, [
            'headers'           => [
                'Content-type' => 'application/json',
                'Authorization' => 'Bearer ' . $body->access_token . '',
            ],
            'data'              => [
                "max_count" => 10
            ],
        ]);
        $body = $response->getBody()->getContents();
        $body = json_decode($body);
        
        return $body;
    }
}