Priority File Manager

📁 public_html
Base Directory:
/home/ecedu/public_html/cce/api
NameTypeSizeActions
📁 .. Folder -
📄 about_controller.php File 1241
Edit Download
📄 ads_controller.php File 2645
Edit Download
📄 contact_controller.php File 1712
Edit Download
📄 data_controller.php File 3269
Edit Download
📄 error_log.txt File 1332
Edit Download
📄 header.php File 3994
Edit Download
📄 image_controller.php File 2510
Edit Download
📄 news_controller.php File 15794
Edit Download
📄 social_controller.php File 1717
Edit Download
📄 upload_pdf_thumbnail.php File 1844
Edit Download
📄 validate.php File 510
Edit Download

View File: data_controller.php

<?php
include('header.php');
 
require_once __DIR__ . '/../Models/NewsModel.php';
require_once __DIR__ . '/../Models/DeviceAuth.php';

$inputRaw = file_get_contents('php://input');
if (substr($inputRaw, 0, 3) === "\xEF\xBB\xBF") {
    $inputRaw = substr($inputRaw, 3);
}

$input = json_decode($inputRaw, true);

if (json_last_error() !== JSON_ERROR_NONE) {
    http_response_code(400);
    echo json_encode(['status' => 'error', 'message' => 'Invalid JSON input: ' . json_last_error_msg()]);
    exit;
}

$deviceId      = $input['deviceId'] ?? '';
$authorization = $input['authorization'] ?? '';
$lang          = $input['lang'] ?? 'ar';
$action        = $input['action'] ?? 'read';

$auth = new DeviceAuth();
if (!$auth->verifyDevice($deviceId, $authorization)) {
    http_response_code(401);
    echo json_encode(['status' => 'error', 'message' => 'Unauthorized']);
    exit;
}

$news = new NewsModel();

try {
    if ($action === 'read') {
        $limit = 5;
        $newsByType = [];
        $mostReadNews = [];

        $menuData=  loadMenu();

        $newsTypes = $news->getAllTableNames();

        foreach ($newsTypes as $typeId => $tableName) {
            $newsByType["newsType$typeId"] = $news->getNewsByType($typeId, $limit);
        }

        $newsMap = [];
        foreach ($newsByType as $typeKey => $newsList) {
            foreach ($newsList as $newsItem) {
                $key = $newsItem['news_id'] . '-' . str_replace('newsType', '', $typeKey);
                $newsMap[$key] = $newsItem;
            }
        }

        $readView = $news->getViewCount($limit);

        foreach ($readView as $row) {
            $key = trim((string)$row['news_id']) . '-' . trim((string)$row['table_id']);
            if (array_key_exists($key, $newsMap)) {
                $item = $newsMap[$key];
                $item['num_view'] = $row['total_views'] ?? $row['num_view'] ?? 0;
                $item['table_id'] = $row['table_id'];
                $mostReadNews[] = $item;
            }
        }

        $data = [
            'sliderNews'    => array_slice($newsByType['newsType2'] ?? [], 0, $limit),
            'lastNews'      => array_slice($newsByType['newsType2'] ?? [], 0, $limit),
            'mostReadNews'  => $mostReadNews,
            'editorialData' => $news->getBasic('dbs_editorial_board'),
            'contactusData' => $news->getBasic('dbs_contact_us'),
            'socialData'    => $news->listSocials(),
            'imageData'     => $news->listImages(),
            'adsData'       => $news->listAds(),
            'menuData'      => $menuData ??[],
        ];

        for ($n = 1; $n <= 18; $n++) {
            $data["newsType$n"] = $newsByType["newsType$n"] ?? [];
        }

        echo json_encode(['status' => 'ok', 'data' => $data], JSON_UNESCAPED_UNICODE);
        exit;
    } else {
        throw new Exception('Unsupported action');
    }
} catch (Exception $e) {
    http_response_code(500);
    echo json_encode(['status' => 'error', 'message' => $e->getMessage()]);
    exit;
}
 
function loadMenu()
{  
    $file = __DIR__ . '/../Languages/menu.json'; 
    if (!file_exists($file)) { 
        return [];
    }

    $json = file_get_contents($file);
    $allMenu = json_decode($json, true);

    return $allMenu ?: []; 
}