Advertisement
eqeqwan21

Untitled

Jun 2nd, 2025
439
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.59 KB | None | 0 0
  1. <?php
  2. session_name("Asteroid2Id");
  3. session_start();
  4. $base_uri = dirname($_SERVER["PHP_SELF"]);
  5. if($base_uri=='/')
  6.     $base_uri='';
  7. if (! isset($_SESSION['userid']) || empty($_SESSION['admin'])) {
  8.     header("Location: {$base_uri}/?do=login");
  9.     exit;
  10. }
  11. $typesDir = __DIR__ . "/images/types/";
  12. $jsonPath = $typesDir . "skins.json";
  13.  
  14. $typeCodes = [
  15.     "TL"      => "Светофор",
  16.     "G"       => "Пешеходный переход"
  17. ];
  18.  
  19. if (!file_exists($jsonPath)) {
  20.     $initial = [];
  21.     foreach (array_keys($typeCodes) as $code) {
  22.         $initial[$code] = $code . ".png";
  23.     }
  24.     file_put_contents($jsonPath, json_encode($initial, JSON_PRETTY_PRINT));
  25. }
  26.  
  27. $error   = "";
  28. $success = "";
  29. if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['type_code'])) {
  30.     $type = $_POST['type_code'];
  31.     if (!array_key_exists($type, $typeCodes)) {
  32.         $error = "Неизвестный код типа.";
  33.     }
  34.     else if (empty($_FILES['skin_file']) || $_FILES['skin_file']['error'] !== UPLOAD_ERR_OK) {
  35.         $error = "Ошибка при загрузке файла.";
  36.     }
  37.     else {
  38.         $allowed = ['png'];
  39.         $origName = $_FILES['skin_file']['name'];
  40.         $ext = strtolower(pathinfo($origName, PATHINFO_EXTENSION));
  41.         if (!in_array($ext, $allowed)) {
  42.             $error = "Разрешены только форматы PNG";
  43.         } else {
  44.             if (!is_dir($typesDir)) mkdir($typesDir, 0755, true);
  45.             $newFilename = $type . "." . $ext;
  46.             $targetPath  = $typesDir . $newFilename;
  47.  
  48.             if (move_uploaded_file($_FILES['skin_file']['tmp_name'], $targetPath)) {
  49.                 $skins = json_decode(file_get_contents($jsonPath), true);
  50.                 $skins[$type] = $newFilename;
  51.                 file_put_contents($jsonPath, json_encode($skins, JSON_PRETTY_PRINT));
  52.                 $success = "Картинка для типа «{$typeCodes[$type]}» успешно загружена.";
  53.             } else {
  54.                 $error = "Не удалось сохранить файл на сервер.";
  55.             }
  56.         }
  57.     }
  58. }
  59.  
  60. $skins = json_decode(file_get_contents($jsonPath), true);
  61.  
  62. ?><!DOCTYPE html>
  63. <html lang="ru">
  64. <head>
  65.     <meta charset="UTF-8">
  66.     <title>Управление фонами типов устройств</title>
  67.     <link href="css/bootstrap.min.css" type="text/css" rel="stylesheet">
  68.     <style>
  69.       body { padding: 20px; }
  70.       img.skin-preview { max-height: 80px; margin-bottom: 5px; }
  71.     </style>
  72. </head>
  73. <body>
  74. <h3>Настройка фоновых изображений для типов устройств</h3>
  75.  
  76. <?php if ($error): ?>
  77.     <div class="alert alert-danger"><?= htmlspecialchars($error) ?></div>
  78. <?php endif; ?>
  79. <?php if ($success): ?>
  80.     <div class="alert alert-success"><?= htmlspecialchars($success) ?></div>
  81. <?php endif; ?>
  82.  
  83. <table class="table table-bordered">
  84.     <thead class="thead-light">
  85.     <tr>
  86.         <th>Код типа</th>
  87.         <th>Название</th>
  88.         <th>Текущий файл</th>
  89.         <th>Загрузка новой картинки</th>
  90.     </tr>
  91.     </thead>
  92.     <tbody>
  93.     <?php foreach ($typeCodes as $code => $label):
  94.         $currFile = $skins[$code] ?? ($code . ".png");
  95.         $imgPath  = "images/types/" . $currFile;
  96.         ?>
  97.         <tr>
  98.             <td><strong><?= htmlspecialchars($code) ?></strong></td>
  99.             <td><?= htmlspecialchars($label) ?></td>
  100.             <td>
  101.                 <?php if (file_exists($typesDir . $currFile)): ?>
  102.                     <img class="skin-preview" src="<?= htmlspecialchars($imgPath) ?>" alt="<?= htmlspecialchars($label) ?>">
  103.                     <br><?= htmlspecialchars($currFile) ?>
  104.                 <?php else: ?>
  105.                     <em>Файл «<?= htmlspecialchars($currFile) ?>» не найден</em>
  106.                 <?php endif; ?>
  107.             </td>
  108.             <td>
  109.                 <form action="" method="post" enctype="multipart/form-data" class="form-inline">
  110.                     <input type="hidden" name="type_code" value="<?= htmlspecialchars($code) ?>">
  111.                     <div class="form-group mb-2">
  112.                         <input type="file" name="skin_file" accept="image/*" required class="form-control-file">
  113.                     </div>
  114.                     <button type="submit" class="btn btn-sm btn-primary mb-2 ml-2">Загрузить</button>
  115.                 </form>
  116.             </td>
  117.         </tr>
  118.     <?php endforeach; ?>
  119.     </tbody>
  120. </table>
  121. <script src="js/bootstrap.bundle.min.js"></script>
  122. </body>
  123. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement