<?php
/**
 * Created by PhpStorm.
 * User: Kavand
 * Date: 3/6/2019
 * Time: 1:36 AM
 */

$data = $request['object'];
switch ($request['data-header']) {
    case 'load-image':
        $array = array();
        $select = $DAL2->Select("SELECT * FROM `bot_SupportPhoto` ORDER BY `ID` desc ");
        if ($select) {
            $index = 0;
            for ($i = 0; $i < count($select); $i += 1) {
                $row = $select[$i];
                $array[] = array(
                    'index' => (++$index),
                    'img' => '<img width="90px" src="' . $row['Content'] . '" class="img-thumbnail" alt="">',
                    'ID' => "<code>" . $row['ID'] . "</code>",
                    'address' => "<button  data-clipboard-actio='copy' data-clipboard-text='" . $row['Content'] . "' class='copy btn btn-default waves-effect'>دریافت آدرس</button>",
                    'local' => "<code>" . $row['real_path'] . "</code>",
                    'function' => "<button class='button-delete btn btn-default waves-effect'  value='" . $row['ID'] . " '>حذف</button>"
                );
            }
        }
        echo json_encode($array, JSON_UNESCAPED_UNICODE);
        break;
    case 'delete-image':
        if (isset($data['id'])) {
            echo http_get_contents((int)$data['id']);
        } else {
            echo '0';
        }
        break;
    default:
        break;
}

function http_get_contents($id)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'http://bot.digihelp.net/kashancard/site/dFile.php?delete-item=' . $id);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $result = curl_exec($ch);
    if (curl_errno($ch)) {
        return curl_error($ch);
    }
    curl_close($ch);
    return $result;
}