View file upload/src/addons/xenMade/ACPE/Repository/Info.php

File size: 5.61Kb
<?php

namespace xenMade\ACPE\Repository;

use XF\Mvc\Entity\Finder;
use XF\Mvc\Entity\Repository;

class Info extends Repository
{
    public function hostInfos()
    {
        $data = [];

        $data['host_ip'] = gethostbyname(gethostname());
        $data['host_name'] = gethostname();

        return $data;
    }

    public function serverInfos()
    {
        $data = [];

        $data['memorylimit'] = $this->getMemoryLimit();
        $data['php_version'] = phpversion();
        $data['mysql_version'] = \XF::db()->fetchOne("SELECT VERSION() AS version");
        $data['db_size'] = $this->fetchDBSize();

        $data['os'] = PHP_OS;
        $data['software'] = $this->getWebServer(true);

        $data['cpu_load'] = $this->cpuLoad();
        $data['memory_usage'] = memory_get_usage();

        return $data;
    }

    public function cpuLoad()
    {
        $system = strtoupper(substr(PHP_OS, 0, 3));
        $load = 0;

        if($system != 'WIN')
        {
            $sys_load = @sys_getloadavg();

            if(!empty($sys_load[0]))
                $load = $sys_load[0];
        }

        return (int)$load;


        /**
         * @deprecated
         */
        if($system == 'WIN')
        {
            @exec('wmic cpu get LoadPercentage', $sys_load);

            if(!empty($sys_load[1]))
                $load = $sys_load[1];
        }
        else
        {
            $sys_load = @sys_getloadavg();

            if(!empty($sys_load[0]))
                $load = $sys_load[0];
        }

        return (int)$load;
    }

    public function getMemoryLimit()
    {
        $memoryLimit = ini_get('memory_limit');

        if(preg_match('/^(\d+)(.)$/', $memoryLimit, $matches))
        {
            if(!empty($matches[2]))
            {
                switch(strtolower($matches[2]))
                {
                    case 'k':
                        $matches[1] = $matches[1]  * 1024;
                        break;

                    case 'm':
                        $matches[1] = $matches[1] * 1024 * 1024;
                        break;

                    case 'G':
                        $matches[1] = $matches[1] * 1024 * 1024 * 1024;
                        break;

                    case 'T':
                        $matches[1] = $matches[1] * 1024 * 1024 * 1024 * 1024;
                        break;

                    case 'P':
                        $matches[1] = $matches[1] * 1024 * 1024 * 1024 * 1024 * 1024;
                        break;
                }

                $memoryLimit = $matches[1];

                if($memoryLimit < 0)
                    $memoryLimit = 0;
            }
        }

        return $memoryLimit;
    }

    public function getWebServer($raw = false)
    {
        $server = 'unknow';

        if(isset($_SERVER['SERVER_SOFTWARE']))
            $server = strtolower($_SERVER['SERVER_SOFTWARE']);

        if(strpos($server, 'apache' ) !== false)
        {
            if($raw)
            {
                return $_SERVER['SERVER_SOFTWARE'];
            }
            else
            {
                return 'apache';
            }
        }

        return $server;
    }

    public function generateAttachmentTotals()
    {
        $cache = \XF::app()->simpleCache();

        $attachmentTotals = [];

        // Stündlich aktaullisieren
        if(isset($cache['xenMade/ACPE']['attachment_totals']['time']) && $cache['xenMade/ACPE']['attachment_totals']['time'] <= \XF::$time - 3600)
        {
            $attachmentTotals = $this->fetchAttachmentData();
        }
        elseif(empty($cache['xenMade/ACPE']['attachment_totals']))
        {
            $attachmentTotals = $this->fetchAttachmentData();
        }

        return $attachmentTotals;
    }

    protected function fetchAttachmentData()
    {
        $db = \XF::db();
        $cache = \XF::app()->simpleCache();

        $attachmentTotals = [];

        $diskUsage = $db->fetchOne('
                    SELECT SUM(file_size) AS diskusage
                    FROM xf_attachment_data
			');

        $downloadCount = $db->fetchOne('
                        SELECT SUM(view_count) AS count
                        FROM xf_attachment
			');

        $attachmentsCount = $db->fetchOne('
                            SELECT COUNT(attachment.attachment_id)
                            FROM xf_attachment AS attachment
                            INNER JOIN xf_attachment_data AS attachment_data ON
                                (attachment_data.data_id = attachment.data_id)
					');

        $attachmentTotals = array(
            'attachments_count' => $attachmentsCount,
            'disk_usage' => $diskUsage,
            'download_count' => $downloadCount,
            'time' => \XF::$time,
        );

        $cache->setValue('xenMade/ACPE', 'attachment_totals', $attachmentTotals);
        $attachmentTotals = $cache['xenMade/ACPE']['attachment_totals'];

        return $attachmentTotals;
    }

    protected function fetchDBSize()
    {
        $config = \XF::config();
        $db = $this->db();

        return $db->fetchOne('
                              SELECT
                                sum(data_length + index_length) as dbsize
                              FROM
                                information_schema.TABLES
                              WHERE
                                TABLE_SCHEMA = "' . $config['db']['dbname'] . '"
                              GROUP BY
                                table_schema'
        );
    }
}