<?php
header('Content-Type: application/json');
header('Cache-Control: no-store, no-cache, must-revalidate, max-age=0');
header('Cache-Control: post-check=0, pre-check=0', false);
header('Pragma: no-cache');

require_once('../settings.php');

$files = array( 'index.html' => 440,
                'LICENSE' => 440,
                'css' => 550,
                'css/core.css' => 440,
                'css/core.min.css' => 440,
                'gfx' => 550,
                'gfx/bg.jpg' => 440,
                'gfx/menu.png' => 440,
                'gfx/buttons' => 550,
                'gfx/buttons/delete.png' => 440,
                'gfx/distros' => 550,
                'gfx/distros/apple.png' => 440,
                'gfx/distros/arch.png' => 440,
                'gfx/distros/bsd.png' => 440,
                'gfx/distros/debian.png' => 440,
                'gfx/distros/fedora.png' => 440,
                'gfx/distros/linux.png' => 440,
                'gfx/distros/opensuse.png' => 440,
                'gfx/distros/ubuntu.png' => 440,
                'gfx/distros/unknown.png' => 440,
                'gfx/flags' => 550,
                'gfx/flags/russian-16x16.png' => 440,
                'gfx/flags/uk-16x16.png' => 440,
                'gfx/icons' => 550,
                'gfx/icons/about.png' => 440,
                'gfx/icons/help.png' => 440,
                'gfx/icons/loading.gif' => 440,
                'gfx/icons/network.png' => 440,
                'gfx/icons/server.png' => 440,
                'gfx/icons/settings.png' => 440,
                'gfx/icons/samba.png' => 440,
                'gfx/icons/storage.png' => 440,
                'gfx/icons/sysinfo.png' => 440,
                'js' => 550,
                'js/core.js' => 440,
                'js/smoothie.js' => 440,
                'system' => 550,
                'system/settings.php' => 440,
                'system/action' => 550,
                'system/action/index.php' => 440,
                'system/apps' => 550,
                'system/apps/apps.json' => 640,
                'system/apps/cai_downloader' => 550,
                'system/apps/cai_downloader/app.json' => 440,
                'system/apps/cai_downloader/icon.png' => 440,
                'system/apps/cai_downloader/index.html' => 440,
                'system/apps/cai_downloader/css' => 550,
                'system/apps/cai_downloader/css/downloader.css' => 440,
                'system/apps/cai_downloader/exec' => 550,
                'system/apps/cai_downloader/exec/index.php' => 440,
                'system/apps/cai_downloader/exec/scripts' => 550,
                'system/apps/cai_downloader/exec/scripts/app.sh' => 550,
                'system/apps/cai_downloader/js' => 550,
                'system/apps/cai_downloader/js/downloader.js' => 440,
                'system/apps/cai_downloader/locale' => 550,
                'system/apps/cai_downloader/locale/en.json' => 440,
                'system/apps/cai_downloader/locale/ru.json' => 440,
                'system/apps/nextcloud' => 550,
                'system/apps/nextcloud/app.json' => 440,
                'system/apps/nextcloud/icon.png' => 440,
                'system/apps/openfire' => 550,
                'system/apps/openfire/app.json' => 440,
                'system/apps/openfire/icon.png' => 440,
                'system/apps/owncloud' => 550,
                'system/apps/owncloud/app.json' => 440,
                'system/apps/owncloud/icon.png' => 440,
                'system/apps/phpmyadmin' => 550,
                'system/apps/phpmyadmin/app.json' => 440,
                'system/apps/phpmyadmin/icon.png' => 440,
                'system/apps/phppgadmin' => 550,
                'system/apps/phppgadmin/app.json' => 440,
                'system/apps/phppgadmin/icon.png' => 440,
                'system/apps/plex' => 550,
                'system/apps/plex/app.json' => 440,
                'system/apps/plex/icon.png' => 440,
                'system/apps/rslsync' => 550,
                'system/apps/rslsync/app.json' => 440,
                'system/apps/rslsync/icon.png' => 440,
                'system/apps/transmission' => 550,
                'system/apps/transmission/app.json' => 440,
                'system/apps/transmission/icon.png' => 440,
                'system/classes' => 550,
                'system/classes/CAI' => 550,
                'system/classes/CAI/CAICP' => 550,
                'system/classes/CAI/CAICP/src' => 550,
                'system/classes/CAI/CAICP/src/Applications.php' => 440,
                'system/classes/CAI/CAICP/src/Localization.php' => 440,
                'system/classes/CAI/CAICP/src/PSR4Autoloader.php' => 440,
                'system/classes/CAI/CAICP/src/Settings.php' => 440,
                'system/help' => 550,
                'system/help/index.html' => 440,
                'system/help/css' => 550,
                'system/help/css/help.css' => 440,
                'system/help/js' => 550,
                'system/help/js/help.js' => 440,
                'system/help/locale' => 550,
                'system/help/locale/en' => 550,
                'system/help/locale/en/index.html' => 440,
                'system/help/locale/ru' => 550,
                'system/help/locale/ru/index.html' => 440,
                'system/json' => 550,
                'system/json/index.php' => 440,
                'system/json/settings.json' => 640,
                'system/json/version.json' => 440,
                'system/json/general_settings' => 550,
                'system/json/general_settings/index.php' => 440,
                'system/json/locale' => 550,
                'system/json/locale/en.json' => 440,
                'system/json/locale/ru.json' => 440,
                'system/scripts' => 550,
                'system/scripts/check_files.php' => 440,
                'system/scripts/index.php' => 440,
                'system/scripts/json_api_nix.sh' => 550,
                'system/scripts/json_transmission.php' => 440,
                'system/scripts/json_transmission.sh' => 550,
                'system/scripts/smart_temp.sh' => 550,
                'system/scripts/trim.awk' => 440,
                'system/ui' => 550,
                'system/ui/forms' => 550,
                'system/ui/forms/settings' => 550,
                'system/ui/forms/settings/index.html' => 440,
                'system/ui/forms/settings/css' => 550,
                'system/ui/forms/settings/css/styles.css' => 440,
                'system/ui/forms/settings/js' => 550,
                'system/ui/forms/settings/js/settings.js' => 440
    );
$script_response = array();
$err_count = 0;

foreach ($files as $key => $value) {
    if (!file_exists($cp_root . '/' . $key)) {
        $err_count += 1;

        $script_response[] = array( 'file'          => $key,
                                    'exists'        => 0,
                                    'mask_required' => 0,
                                    'mask_actual'   => 0);
    } else {
        $file_owner = posix_getpwuid(fileowner($cp_root . '/' . $key))['name'];
        $file_group = posix_getgrgid(filegroup($cp_root . '/' . $key))['name'];
        $file_mask = (int)substr(sprintf('%o', 
                                        fileperms($cp_root . '/' . $key)
                                        ),
                                -3);

        if (($value != $file_mask) ||
            ($file_group != FILE_GROUP) ||
             $file_owner != FILE_OWNER) {
            $err_count += 1;

            $script_response[] = array( 'file'           => $key,
                                        'exists'         => 1,
                                        'mask_required'  => $value,
                                        'mask_actual'    => $file_mask,
                                        'owner_required' => FILE_OWNER,
                                        'owner_actual'   => $file_owner,
                                        'group_required' => FILE_GROUP,
                                        'group_actual'   => $file_group);
        }
    }
}

echo  html_entity_decode(json_encode($script_response));
