Guest User

Untitled

a guest
Apr 12th, 2020
848
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 22.16 KB | None | 0 0
  1. <?php
  2.  
  3. class PhpPsInfo
  4. {
  5.     protected $login;
  6.     protected $password;
  7.  
  8.     const DEFAULT_PASSWORD = 'prestashop';
  9.     const DEFAULT_LOGIN = 'prestashop';
  10.  
  11.     const TYPE_OK = true;
  12.     const TYPE_ERROR = false;
  13.     const TYPE_WARNING = null;
  14.  
  15.     protected $requirements = [
  16.         'versions' => [
  17.             'php' => '5.6',
  18.             'mysql' => '5.5',
  19.         ],
  20.         'extensions' => [
  21.             'curl' => true,
  22.             'dom' => true,
  23.             'fileinfo' => true,
  24.             'gd' => true,
  25.             'imagick' => false,
  26.             'intl' => true,
  27.             'json' => true,
  28.             'openssl' => true,
  29.             'mbstring' => true,
  30.             'memcache' => false,
  31.             'memcached' => false,
  32.             'pdo_mysql' => true,
  33.             'zip' => true,
  34.             'bcmath' => false,
  35.         ],
  36.         'config' => [
  37.             'allow_url_fopen' => true,
  38.             'expose_php' => false,
  39.             'file_uploads' => true,
  40.             'max_input_vars' => 1000,
  41.             'memory_limit' => '64M',
  42.             'post_max_size' => '16M',
  43.             'register_argc_argv' => false,
  44.             'set_time_limit' => true,
  45.             'short_open_tag' => false,
  46.             'upload_max_filesize' => '4M',
  47.         ],
  48.         'directories' => [
  49.             'cache_dir' => 'var/cache',
  50.             'log_dir' => 'var/logs',
  51.             'img_dir' => 'img',
  52.             'mails_dir' => 'mails',
  53.             'module_dir' => 'modules',
  54.             'translations_dir' => 'translations',
  55.             'customizable_products_dir' => 'upload',
  56.             'virtual_products_dir' => 'download',
  57.             'config_sf2_dir' => 'app/config',
  58.             'translations_sf2' => 'app/Resources/translations',
  59.         ],
  60.         'apache_modules' => [
  61.             'mod_rewrite' => true,
  62.         ],
  63.     ];
  64.  
  65.     protected $recommended = [
  66.         'versions' => [
  67.             'php' => '7.1',
  68.             'mysql' => '5.6',
  69.         ],
  70.         'extensions' => [
  71.             'curl' => true,
  72.             'dom' => true,
  73.             'fileinfo' => true,
  74.             'gd' => true,
  75.             'imagick' => true,
  76.             'intl' => true,
  77.             'json' => true,
  78.             'openssl' => true,
  79.             'mbstring' => true,
  80.             'memcache' => false,
  81.             'memcached' => true,
  82.             'pdo_mysql' => true,
  83.             'zip' => true,
  84.             'bcmath' => true,
  85.         ],
  86.         'config' => [
  87.             'allow_url_fopen' => true,
  88.             'expose_php' => false,
  89.             'file_uploads' => true,
  90.             'max_input_vars' => 5000,
  91.             'memory_limit' => '256M',
  92.             'post_max_size' => '128M',
  93.             'register_argc_argv' => false,
  94.             'set_time_limit' => true,
  95.             'short_open_tag' => false,
  96.             'upload_max_filesize' => '128M',
  97.         ],
  98.         'apache_modules' => [
  99.             'mod_rewrite' => true,
  100.         ],
  101.     ];
  102.  
  103.     /**
  104.      * Set up login and password with parameter or
  105.      * you can set server env vars:
  106.      *  - PS_INFO_LOGIN
  107.      *  - PS_INFO_PASSWORD
  108.      *
  109.      * @param string $login    Login
  110.      * @param string $password Password
  111.      *
  112.      */
  113.     public function __construct($login = self::DEFAULT_LOGIN, $password = self::DEFAULT_PASSWORD)
  114.     {
  115.         if (!empty($_SERVER['PS_INFO_LOGIN'])) {
  116.             $this->login = $_SERVER['PS_INFO_LOGIN'];
  117.         }
  118.  
  119.         if (!empty($_SERVER['PS_INFO_PASSWORD'])) {
  120.             $this->password = $_SERVER['PS_INFO_PASSWORD'];
  121.         }
  122.  
  123.         $this->login = !empty($login) ? $login : $this->login;
  124.         $this->password = !empty($password) ? $password : $this->password;
  125.     }
  126.  
  127.     /**
  128.      * Check authentication if not in cli and have a login
  129.      */
  130.     public function checkAuth()
  131.     {
  132.         if (PHP_SAPI === 'cli' ||
  133.             empty($this->login)
  134.         ) {
  135.             return;
  136.         }
  137.  
  138.         if (!isset($_SERVER['PHP_AUTH_USER']) ||
  139.             $_SERVER['PHP_AUTH_PW'] != $this->password ||
  140.             $_SERVER['PHP_AUTH_USER'] != $this->login
  141.         ) {
  142.             header('WWW-Authenticate: Basic realm="Authentification"');
  143.             header('HTTP/1.0 401 Unauthorized');
  144.             echo '401 Unauthorized';
  145.             exit(401);
  146.         }
  147.     }
  148.  
  149.     /**
  150.      * Get versions data
  151.      *
  152.      * @return array
  153.      */
  154.     public function getVersions()
  155.     {
  156.         $data = [
  157.             'Web server' => [$this->getWebServer()],
  158.             'PHP Type' => [
  159.                 strpos(PHP_SAPI, 'cgi') !== false ?
  160.                 'CGI with Apache Worker or another webserver' :
  161.                 'Apache Module (low performance)'
  162.             ],
  163.         ];
  164.  
  165.         $data['PHP Version'] = [
  166.             $this->requirements['versions']['php'],
  167.             $this->recommended['versions']['php'],
  168.             PHP_VERSION,
  169.             version_compare(PHP_VERSION, $this->recommended['versions']['php'], '>=') ?
  170.             self::TYPE_OK : (
  171.                 version_compare(PHP_VERSION, $this->requirements['versions']['php'], '>=') ?
  172.                 self::TYPE_WARNING :
  173.                 self::TYPE_ERROR
  174.             )
  175.         ];
  176.  
  177.         if (!extension_loaded('mysqli') || !is_callable('mysqli_connect')) {
  178.             $data['MySQLi Extension'] = [
  179.                 true,
  180.                 true,
  181.                 'Not installed',
  182.                 self::TYPE_ERROR,
  183.             ];
  184.         } else {
  185.             $data['MySQLi Extension'] = [
  186.                 $this->requirements['versions']['mysql'],
  187.                 $this->recommended['versions']['mysql'],
  188.                 mysqli_get_client_info(),
  189.                 self::TYPE_OK,
  190.             ];
  191.         }
  192.  
  193.         $data['Internet connectivity (Prestashop)'] = [
  194.             false,
  195.             true,
  196.             gethostbyname('www.prestashop.com') !== 'www.prestashop.com',
  197.             gethostbyname('www.prestashop.com') !== 'www.prestashop.com',
  198.         ];
  199.  
  200.         return $data;
  201.     }
  202.  
  203.     /**
  204.      * Get php extensions data
  205.      *
  206.      * @return array
  207.      */
  208.     public function getPhpExtensions()
  209.     {
  210.         $data = [];
  211.         $vars = [
  212.             'BCMath Arbitrary Precision Mathematics' => 'bcmath',
  213.             'Client URL Library (Curl)' => 'curl',
  214.             'Image Processing and GD' => 'gd',
  215.             'Image Processing (ImageMagick)' => 'imagick',
  216.             'Internationalization Functions (Intl)' => 'intl',
  217.             'Memcache' => 'memcache',
  218.             'Memcached' => 'memcached',
  219.             'Multibyte String (Mbstring)' => 'mbstring',
  220.             'OpenSSL' => 'openssl',
  221.             'File Information (Fileinfo)' => 'fileinfo',
  222.             'JavaScript Object Notation (Json)' => 'json',
  223.             'PDO and MySQL Functions' => 'pdo_mysql',
  224.         ];
  225.         foreach ($vars as $label => $var) {
  226.             $value = extension_loaded($var);
  227.             $data[$label] = [
  228.                 $this->requirements['extensions'][$var],
  229.                 $this->recommended['extensions'][$var],
  230.                 $value
  231.             ];
  232.         }
  233.  
  234.         $vars = [
  235.             'PHP-DOM and PHP-XML' => ['dom', 'DomDocument'],
  236.             'Zip' => ['zip', 'ZipArchive'],
  237.         ];
  238.         foreach ($vars as $label => $var) {
  239.             $value = class_exists($var[1]);
  240.             $data[$label] = [
  241.                 $this->requirements['extensions'][$var[0]],
  242.                 $this->recommended['extensions'][$var[0]],
  243.                 $value
  244.             ];
  245.         }
  246.  
  247.         return $data;
  248.     }
  249.  
  250.     /**
  251.      * Get php config data
  252.      *
  253.      * @return array
  254.      */
  255.     public function getPhpConfig()
  256.     {
  257.         $data = [];
  258.         $vars = [
  259.             'allow_url_fopen',
  260.             'expose_php',
  261.             'file_uploads',
  262.             'register_argc_argv',
  263.             'short_open_tag',
  264.         ];
  265.         foreach ($vars as $var) {
  266.             $value = (bool) ini_get($var);
  267.             $data[$var] = [
  268.                 $this->requirements['config'][$var],
  269.                 $this->recommended['config'][$var],
  270.                 $value
  271.             ];
  272.         }
  273.  
  274.         $vars = [
  275.             'max_input_vars',
  276.             'memory_limit',
  277.             'post_max_size',
  278.             'upload_max_filesize',
  279.         ];
  280.         foreach ($vars as $var) {
  281.             $value = ini_get($var);
  282.             if ($this->toBytes($value) >= $this->toBytes($this->recommended['config'][$var])) {
  283.                 $result = self::TYPE_OK;
  284.             } elseif ($this->toBytes($value) >= $this->toBytes($this->requirements['config'][$var])) {
  285.                 $result = self::TYPE_WARNING;
  286.             } else {
  287.                 $result = self::TYPE_ERROR;
  288.             }
  289.  
  290.             $data[$var] = [
  291.                 $this->requirements['config'][$var],
  292.                 $this->recommended['config'][$var],
  293.                 $value,
  294.                 $result,
  295.             ];
  296.         }
  297.  
  298.         $vars = [
  299.             'set_time_limit',
  300.         ];
  301.         foreach ($vars as $var) {
  302.             $value = is_callable($var);
  303.             $data[$var] = [
  304.                 $this->recommended['config'][$var],
  305.                 $this->requirements['config'][$var],
  306.                 $value
  307.             ];
  308.         }
  309.  
  310.         return $data;
  311.     }
  312.  
  313.     /**
  314.      * Check if directories are writable
  315.      *
  316.      * @return array
  317.      */
  318.     public function getDirectories()
  319.     {
  320.         $data = [];
  321.         foreach ($this->requirements['directories'] as $directory) {
  322.             $directoryPath = getcwd() . DIRECTORY_SEPARATOR . trim($directory, '\\/');
  323.             $data[$directory] = [file_exists($directoryPath) && is_writable($directoryPath)];
  324.         }
  325.  
  326.         return $data;
  327.     }
  328.  
  329.     public function getServerModules()
  330.     {
  331.         $data = [];
  332.         if ($this->getWebServer() !== 'Apache' || !function_exists('apache_get_modules')) {
  333.             return $data;
  334.         }
  335.  
  336.         $modules = apache_get_modules();
  337.         $vars = array_keys($this->requirements['apache_modules']);
  338.         foreach ($vars as $var) {
  339.             $value = in_array($var, $modules);
  340.             $data[$var] = [
  341.                 $this->requirements['apache_modules'][$var],
  342.                 $this->recommended['apache_modules'][$var],
  343.                 $value,
  344.             ];
  345.         }
  346.  
  347.         return $data;
  348.     }
  349.  
  350.     /**
  351.      * Convert PHP variable (G/M/K) to bytes
  352.      * Source: http://php.net/manual/fr/function.ini-get.php
  353.      *
  354.      * @return integer
  355.      */
  356.     public function toBytes($val)
  357.     {
  358.         if (is_numeric($val)) {
  359.             return $val;
  360.         }
  361.  
  362.         $val = trim($val);
  363.         $val = (int) $val;
  364.         switch (strtolower($val[strlen($val)-1])) {
  365.             case 'g':
  366.                 $val *= 1024;
  367.                 // continue
  368.             case 'm':
  369.                 $val *= 1024;
  370.                 // continue
  371.             case 'k':
  372.                 $val *= 1024;
  373.         }
  374.  
  375.         return $val;
  376.     }
  377.  
  378.     /**
  379.      * Transform value to string
  380.      *
  381.      * @param mixed $value Value
  382.      *
  383.      * @return string
  384.      */
  385.     public function toString($value)
  386.     {
  387.         if ($value === true) {
  388.             return 'Yes';
  389.         } elseif ($value === false) {
  390.             return 'No';
  391.         } elseif ($value === null) {
  392.             return 'N/A';
  393.         }
  394.  
  395.         return strval($value);
  396.     }
  397.  
  398.     /**
  399.      * Get html class
  400.      *
  401.      * @param array $data
  402.      * @return string
  403.      */
  404.     public function toHtmlClass(array $data)
  405.     {
  406.         if (count($data) === 1 && !is_bool($data[0])) {
  407.             return 'table-info';
  408.         }
  409.  
  410.  
  411.         if (count($data) === 1 && is_bool($data[0])) {
  412.             $result = $data[0];
  413.         } elseif (array_key_exists(3, $data)) {
  414.             $result = $data[3];
  415.         } else {
  416.             if ($data[2] >= $data[1]) {
  417.                 $result = self::TYPE_OK;
  418.             } elseif ($data[2] >= $data[0]) {
  419.                 $result = self::TYPE_WARNING;
  420.             } else {
  421.                 $result = self::TYPE_ERROR;
  422.             }
  423.         }
  424.  
  425.         if ($result === false) {
  426.             return 'table-danger';
  427.         }
  428.  
  429.         if ($result === null) {
  430.             return 'table-warning';
  431.         }
  432.  
  433.         return 'table-success';
  434.     }
  435.  
  436.     /**
  437.      * Detect Web server
  438.      *
  439.      * @return string
  440.      */
  441.     protected function getWebServer()
  442.     {
  443.         if (stristr($_SERVER['SERVER_SOFTWARE'], 'Apache') !== false) {
  444.             return 'Apache';
  445.         } elseif (stristr($_SERVER['SERVER_SOFTWARE'], 'LiteSpeed') !== false) {
  446.             return 'Lite Speed';
  447.         } elseif (stristr($_SERVER['SERVER_SOFTWARE'], 'Nginx') !== false) {
  448.             return 'Nginx';
  449.         } elseif (stristr($_SERVER['SERVER_SOFTWARE'], 'lighttpd') !== false) {
  450.             return 'lighttpd';
  451.         } elseif (stristr($_SERVER['SERVER_SOFTWARE'], 'IIS') !== false) {
  452.             return 'Microsoft IIS';
  453.         }
  454.  
  455.         return 'Not detected';
  456.     }
  457.  
  458.     /**
  459.      * Determines if a command exists on the current environment
  460.      * Source: https://stackoverflow.com/questions/12424787/how-to-check-if-a-shell-command-exists-from-php
  461.      *
  462.      * @param string $command The command to check
  463.      *
  464.      * @return bool
  465.      */
  466.     protected function commandExists($command)
  467.     {
  468.         $which = (PHP_OS == 'WINNT') ? 'where' : 'which';
  469.  
  470.         $process = proc_open(
  471.             $which . ' ' . $command,
  472.             [
  473.                 ['pipe', 'r'], //STDIN
  474.                 ['pipe', 'w'], //STDOUT
  475.                 ['pipe', 'w'], //STDERR
  476.             ],
  477.             $pipes
  478.         );
  479.  
  480.         if ($process !== false) {
  481.             $stdout = stream_get_contents($pipes[1]);
  482.             $stderr = stream_get_contents($pipes[2]);
  483.             fclose($pipes[1]);
  484.             fclose($pipes[2]);
  485.             proc_close($process);
  486.  
  487.             return $stdout != '';
  488.         }
  489.  
  490.         return false;
  491.     }
  492. }
  493.  
  494. // Init render
  495. $info = new PhpPsInfo();
  496. $info->checkAuth();
  497. ?>
  498. <!doctype html>
  499. <html lang="en">
  500.     <head>
  501.         <meta charset="utf-8"/>
  502.         <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"/>
  503.         <meta name="description" content=""/>
  504.         <meta name="author" content=""/>
  505.         <link rel="icon" href="../../../../favicon.ico"/>
  506.  
  507.         <title>PHP PrestaShop Info</title>
  508.         <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet" />
  509.         <style>
  510.             h1 {font-size:2rem;}
  511.         </style>
  512.     </head>
  513.  
  514.     <body>
  515.         <nav class="navbar navbar-dark bg-dark flex-md-nowrap p-0 shadow">
  516.             <a class="navbar-brand col-sm-3 col-md-2 mr-0" href="#">PHP PrestaShop Info</a>
  517.         </nav>
  518.  
  519.         <div class="container-fluid">
  520.             <div class="row justify-content-md-center">
  521.                 <main role="main" class="col-8">
  522.                     <h1>General information & PHP/MySQL Version</h1>
  523.                     <div class="table-responsive">
  524.                         <table class="table table-striped table-sm text-center">
  525.                             <thead>
  526.                                 <tr>
  527.                                     <th class="text-left">#</th>
  528.                                     <th>Required</th>
  529.                                     <th>Recommended</th>
  530.                                     <th>Current</th>
  531.                                 </tr>
  532.                             </thead>
  533.                             <tbody>
  534.                                 <?php foreach ($info->getVersions() as $label => $data) : ?>
  535.                                     <?php if (count($data) === 1) : ?>
  536.                                         <tr>
  537.                                             <td class="text-left"><?php echo $label ?></td>
  538.                                             <td class="<?php echo $info->toHtmlClass($data); ?>" colspan="3"><?php echo $info->toString($data[0]) ?></td>
  539.                                         </tr>
  540.                                     <?php else : ?>
  541.                                         <tr>
  542.                                             <td class="text-left"><?php echo $label ?></td>
  543.                                             <td><?php echo $info->toString($data[0]) ?></td>
  544.                                             <td><?php echo $info->toString($data[1]) ?></td>
  545.                                             <td class="<?php echo $info->toHtmlClass($data); ?>"><?php echo $info->toString($data[2]) ?></td>
  546.                                         </tr>
  547.                                     <?php endif; ?>
  548.                                 <?php endforeach; ?>
  549.                             </tbody>
  550.                         </table>
  551.                     </div>
  552.  
  553.                     <h1>PHP Configuration</h1>
  554.  
  555.                     <div class="table-responsive">
  556.                         <table class="table table-striped table-sm text-center">
  557.                             <thead>
  558.                                 <tr>
  559.                                     <th class="text-left">#</th>
  560.                                     <th>Required</th>
  561.                                     <th>Recommended</th>
  562.                                     <th>Current</th>
  563.                                 </tr>
  564.                             </thead>
  565.                             <tbody>
  566.                                 <?php foreach ($info->getPhpConfig() as $label => $data) : ?>
  567.                                     <tr>
  568.                                         <td class="text-left"><?php echo $label ?></td>
  569.                                         <td><?php echo $info->toString($data[0]) ?></td>
  570.                                         <td><?php echo $info->toString($data[1]) ?></td>
  571.                                         <td class="<?php echo $info->toHtmlClass($data); ?>"><?php echo $info->toString($data[2]) ?></td>
  572.                                     </tr>
  573.                                 <?php endforeach; ?>
  574.                             </tbody>
  575.                         </table>
  576.                     </div>
  577.  
  578.                     <h1>PHP Extensions</h1>
  579.  
  580.                     <div class="table-responsive">
  581.                         <table class="table table-striped table-sm text-center">
  582.                             <thead>
  583.                                 <tr>
  584.                                     <th class="text-left">#</th>
  585.                                     <th>Required</th>
  586.                                     <th>Recommended</th>
  587.                                     <th>Current</th>
  588.                                 </tr>
  589.                             </thead>
  590.                             <tbody>
  591.                                 <?php foreach ($info->getPhpExtensions() as $label => $data) : ?>
  592.                                     <tr>
  593.                                         <td class="text-left"><?php echo $label ?></td>
  594.                                         <td><?php echo $info->toString($data[0]) ?></td>
  595.                                         <td><?php echo $info->toString($data[1]) ?></td>
  596.                                         <td class="<?php echo $info->toHtmlClass($data); ?>"><?php echo $info->toString($data[2]) ?></td>
  597.                                     </tr>
  598.                                 <?php endforeach; ?>
  599.                             </tbody>
  600.                         </table>
  601.                     </div>
  602.  
  603.                     <h1>Directories</h1>
  604.  
  605.                     <div class="table-responsive">
  606.                         <table class="table table-striped table-sm text-center">
  607.                             <thead>
  608.                                 <tr>
  609.                                     <th class="text-left">#</th>
  610.                                     <th>Is Writable</th>
  611.                                 </tr>
  612.                             </thead>
  613.                             <tbody>
  614.                                 <?php foreach ($info->getDirectories() as $label => $data) : ?>
  615.                                     <tr>
  616.                                         <td class="text-left"><?php echo $label ?></td>
  617.                                         <td class="<?php echo $info->toHtmlClass($data); ?>"><?php echo $info->toString($data[0]) ?></td>
  618.                                     </tr>
  619.                                 <?php endforeach; ?>
  620.                             </tbody>
  621.                         </table>
  622.                     </div>
  623.  
  624.                     <?php if (count($info->getServerModules()) > 0): ?>
  625.                         <h1>Apache Modules</h1>
  626.  
  627.                         <div class="table-responsive">
  628.                             <table class="table table-striped table-sm text-center">
  629.                                 <thead>
  630.                                     <tr>
  631.                                         <th class="text-left">#</th>
  632.                                         <th>Required</th>
  633.                                         <th>Recommended</th>
  634.                                         <th>Current</th>
  635.                                     </tr>
  636.                                 </thead>
  637.                                 <tbody>
  638.                                     <?php foreach ($info->getServerModules() as $label => $data) : ?>
  639.                                         <tr>
  640.                                             <td class="text-left"><?php echo $label ?></td>
  641.                                             <td><?php echo $info->toString($data[0]) ?></td>
  642.                                             <td><?php echo $info->toString($data[1]) ?></td>
  643.                                             <td class="<?php echo $info->toHtmlClass($data); ?>"><?php echo $info->toString($data[2]) ?></td>
  644.                                         </tr>
  645.                                     <?php endforeach; ?>
  646.                                 </tbody>
  647.                             </table>
  648.                         </div>
  649.                     <?php endif; ?>
  650.                 </main>
  651.             </div>
  652.         </div>
  653.  
  654.         <footer class="footer-copyright text-center py-3">
  655.             © <?php echo date('Y') ?> Copyright: <a href="https://prestashop.com/">PrestaShop</a>
  656.         </footer>
  657.     </body>
  658. </html>
Advertisement
Add Comment
Please, Sign In to add comment