<?php

/**
* @package     JohnCMS
* @link        http://johncms.com
* @copyright   Copyright (C) 2008-2011 JohnCMS Community
* @license     LICENSE.txt (see attached file)
* @version     VERSION.txt (see attached file)
* @author      http://johncms.com/about
* @file: logo.php
* @author: Aan Gabriel Elfha Gymkhana
*/

defined('_IN_JOHNADM') or die('Error: restricted access');

/** @var Psr\Container\ContainerInterface $container */
$container = App::getContainer();

/** @var PDO $db */
$db = $container->get(PDO::class);

/** @var Johncms\Api\UserInterface $systemUser */
$systemUser = $container->get(Johncms\Api\UserInterface::class);

/** @var Johncms\Api\ToolsInterface $tools */
$tools = $container->get(Johncms\Api\ToolsInterface::class);
$config = $container->get('config')['johncms'];




 
if ($systemUser->id !== '1') {
    header('Location: ../?err');
    exit;
}
 
if (!is_dir(ROOT_PATH . 'images/logo/')) {
    mkdir(ROOT_PATH . 'images/logo');
}

 
$from = isset($_GET['from']) ? trim($_GET['from']) : '';
$id = isset($_GET['id']) ? abs(intval($_GET['id'])) : 0;

//abs(intval($_REQUEST['id'])) : 0;
switch ($from) {

    default:
       echo '<div class="mainblok">';
       echo '<div class="phdr">Logo Upload</div>';
       echo '<div class="gmenu">Hallow admin.<br/>Now you can upload your logo and select to set a logo<br/>Go to <a href="index.php?act=logo&amp;from=upload">upload</a> pages or <a href="index.php?act=logo&amp;from=view"><b>View</b></a> your logo</div>';
       echo '<div class="rmenu">Thos module created and author by Aan Gabriel</div>';
       echo '<div class="phdr"><a href="index.php">' . _t('Admin Panel') . '</a></div>';
       echo '</div>';
    break;

    case 'upload':
     if (isset($_POST['submit'])) {
        $handle = new upload($_FILES['file']);
        if ($handle->uploaded) {
            $name_file = time() . '_' . mt_rand(100, 999);
            $handle->file_new_name_body = $name_file;
            $handle->allowed = array('image/jpeg', 'image/gif', 'image/png');
            $handle->file_max_size = 1024 * $config['flsz'];
            $handle->file_overwrite = true;
            $handle->image_convert = 'gif';
            $handle->process('../images/logo/');
            if ($handle->processed){
                echo '<div class="mainblok">';
                echo '<div class="phdr">Upload Logo</div>';
                echo '<div class="gmenu">success uploaded! <a href="index.php?act=logo&amp;from=view">view</a></div></div>';
                echo '<div class="phdr"><a href="index.php">' . _t('Admin Panel') . '</a></div>';
        } else
          echo $tools->displayError($handle->error, '<a href="index.php?act=logo&amp;from=upload">Back</a>');
			$handle->clean();
    } else 
        echo $tools->displayError('No images selected', '<a href="index.php?act=logo&amp;from=upload">Back</a>');
   } else {
    echo '<div class="mainblok">';
    echo '<div class="phdr">Upload Logo</div>';
    echo '<div class="gmenu">';
    echo 'Select your logo:<br/>';
    echo '<form action="index.php?act=logo&amp;from=upload" method="post" enctype="multipart/form-data">' .
    '<input type="file" name="file" />' .
    '<input type="submit" name="submit" value="Upload"/>';
    echo '</form>';
    echo '</div>';
    echo '</div>';
    }
   break;
case 'view':
   echo '<div class="mainblok">';
   echo '<div class="phdr">View Logo | <a href="index.php?act=logo&amp;from=upload">Upload</a></div>';
   $array = glob('../images/logo/*.gif');
   $total = count($array);
   $end = $start + $kmess;
   if ($end > $total)
      $end = $total;
   if ($start >= $total) {
      $start = 0;
      $end = $total > $kmess ? $kmess : $total;
   }
   if ($total > 0) {
      for ($i = $start; $i < $end; $i++) {
         echo ($i  % 2) ? '<div class="list2">' : '<div class="list1">';
         echo'' .
         '<table cellpadding="0" cellspacing="0" width="100%"><tr><td width="auto" align="left"><a href="index.php?act=logo&amp;from=set&amp;id=' . $array[$i] . '">Use Logo</a></td><td width="auto" align="right"><a href="index.php?act=logo&amp;from=dell&amp;id=' . $array[$i] . '">Remove</a></td></tr></table><br/><img src="' . $array[$i] . '" alt="+"  /></div>';
       }
   } else
      echo '<div class="menu">List is empty!</div>';
      echo '<div class="gmenu">Total ' . $total .  '</div>';
      echo '<div class="phdr"><a href="index.php">Panel</a></div>';
      if ($total > $kmess) {
          echo '<div class="topmenu">' . $tools->displayPagination('upload_img.php?', $start, $total, $kmess) . '</div>';
          echo '<p><form action="index.php?act=logo&amp;from=view" method="post"><input type="submit" value="Jump &gt;&gt;"/></form></p>';
     }
       echo '</div>';
break;


case 'dell':
       if ($id) {
         $name = $id;
         if (file_exists($name)) {
             unlink($name);
             echo '<div class="mainblok">';
             echo '<div class="phdr"><a href="index.php"><b>Admin Panel</b></a> | Delete Logo</div>'; 
             echo '<div class="rmenu">Logo was deleted successfully. </div>';
             echo '<div class="phdr"><a href="index.php?act=logo&amp;from=view"><b>Back To view</b></a></div>';
             echo '</div>';
        } else {
             echo $tools->displayError('Error Deleted logo!, File Not Found!');  
             require('../system/end.php');
             exit;
        }
        } else {             
            echo $tools->displayError('Error Deleted logo!');  
            require('../system/end.php');
            exit;
       }


        break;
case 'set':
       if ($id) {
        $name_s = $id;
        $name = preg_split("/[\/..]+/", $name_s); 
        if (file_exists($name_s)) {
          $config['logourl'] = $name[3] . '.gif';
          $configFile = "<?php\n\n" . 'return ' . var_export(['johncms' => $config], true) . ";\n";

           if (!file_put_contents(ROOT_PATH . 'system/config/system.local.php', $configFile)) {
                echo 'ERROR: Can not write system.local.php</body></html>';
                exit;
           }

           echo '<div class="mainblok">';
           echo '<div class="phdr"><a href="index.php"><b>Admin Panel</b></a> | Changed Logo</div>';
           echo '<div class="rmenu">' . _t('Settings are saved successfully') . '</div>';

           if (function_exists('opcache_reset')) {
             opcache_reset();
           }

           echo '<div class="rmenu">Your logo are:<img src="'.$config['homeurl'].'/images/logo/'.$config['logourl'].'"></div>';
           echo '<div class="phdr"><a href="index.php?act=logo&amp;from=view"><b>Back To view</b></a></div>';
           echo '</div>';
    } else {             
       echo $tools->displayError('Images Not Found!');  
        require('../system/end.php');
        exit;
    }
    } else {             
       echo $tools->displayError('No logo selected!');  
        require('../system/end.php');
        exit;
    }
    break;

}

?>





