Sindbad~EG File Manager

Current Path : /opt/nginxhttpd_/vendor/extensions-cpanel/cpanel-development-lib/src/Service/
Upload File :
Current File : //opt/nginxhttpd_/vendor/extensions-cpanel/cpanel-development-lib/src/Service/CmsDetector.php

<?php

namespace O2switch\CpanelLib\Service;

class CmsDetector
{

    private const FAULT_TOLERANCE = 2;
    private $cmsRules;

    public function __construct(?array $cmsRules = [])
    {
        if(!is_array($cmsRules) && !empty($cmsRules)) {
            $this->cmsRules = $cmsRules;
        } else {
            $this->cmsRules = $this->getDefaultCmsRules();
        }
    }

    public function detectCms(string $docRoot) : ?string {
        $result = [];
        if(!$this->checkDirExist($docRoot)){
            return null;
        }

        foreach($this->cmsRules as $cms => $rules){
            $score = 0;
            foreach($rules['files'] as $f){
                if($this->checkIsFileOrDir($docRoot . '/' . $f)){
                    $score++;
                }
            }
            foreach($rules['content'] as $f => $c){
                if($this->fileContains($docRoot . '/' . $f, $c)){
                    $score++;
                }
            }
            $result[$cms] = $score;
        }

        $value = max($result);
        $key = array_search($value, $result);

        $expected = count($this->cmsRules[$key]['files']) + count($this->cmsRules[$key]['content']);

        if(abs($expected - $value) < self::FAULT_TOLERANCE){
            return $key;
        }

        return null;
    }

    public function getDefaultCmsRules() : array {
        return [
            // Name of the CMS detected, must be the same as the value of the template
            'wordpress' => [
                // Files or directory that must exist for the specific CMS
                'files' => [
                    'wp-config.php',
                    'wp-admin',
                    'wp-content',
                    'wp-includes',
                ],
                // Content that must exist inside specific file
                'content' => [
                    'index.php' => 'WordPress',
                    'wp-blog-header.php' => 'wp()',
                ]
            ],
            /*'woocommerce' => [
                'files' => [
                    'wp-config.php',
                    'wp-admin',
                    'wp-content',
                    'wp-includes',
                    'wp-content/plugins/woocommerce'
                ],
                'content' => [
                    'index.php' => 'WordPress',
                    'wp-blog-header.php' => 'wp()',
                ]
            ],*/
            'prestashop' => [
                'files' => [
                    'init.php',
                    'header.php',
                    'footer.php',
                    'override',
                    'modules',
                    'themes',
                    'config',
                ],
                'content' => [
                    'index.php' => 'PrestaShop',
                ]
            ],
            'drupal' => [
                'files' => [
                    'sites',
                    'modules',
                    'profiles',
                    'themes',
                    'core',
                    'sites/default'
                ],
                'content' => [
                    'index.php' => 'Drupal',
                    'update.php' => 'Drupal'
                ]
            ],
            'joomla' => [
                'files' => [
                    'administrator',
                    'components',
                    'media',
                    'plugins',
                    'configuration.php'
                ],
                'content' => [
                    'index.php' => 'Joomla'
                ]
            ]
        ];
    }

    private function checkDirExist(string $dir) : bool {
        return is_dir($dir);
    }

    private function checkIsFileOrDir(string $f) : bool {
        return file_exists($f);
    }

    private function fileContains(string $f, string $c) : bool {
        if(!is_file($f)){
            return false;
        }
        if( strpos(file_get_contents($f),$c) !== false) {
            return true;
        }
        return false;
    }
}

Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists