<commit_before><?php

namespace DreamFactory\Core\Resources\System;

use DreamFactory\Core\Enums\AppTypes;
use DreamFactory\Core\Models\App as AppModel;
use DreamFactory\Core\Models\AppGroup as AppGroupModel;
use DreamFactory\Core\Models\Service as ServiceModel;
use DreamFactory\Core\Models\UserAppRole;
use DreamFactory\Core\Resources\BaseRestResource;
use DreamFactory\Core\User\Services\User;
use DreamFactory\Core\Utility\Session as SessionUtilities;
use DreamFactory\Library\Utility\ArrayUtils;
use DreamFactory\Library\Utility\Enums\Verbs;
use DreamFactory\Library\Utility\Scalar;
use DreamFactory\Core\Models\Config as SystemConfig;

class Environment extends BaseRestResource
{
    /**
     * @return array
     */
    protected function handleGET()
    {
        $result = [];

        $result['platform'] = [
            'version_current'   => '2.0.0',
            'version_latest'    => '2.0.0',
            'upgrade_available' => false,
            'is_hosted'         => false,
            'host'              => php_uname('n'),
        ];

        if (SessionUtilities::isSysAdmin()) {
            $result['server'] = [
                'server_os' => strtolower(php_uname('s')),
                'release'   => php_uname('r'),
                'version'   => php_uname('v'),
                'host'      => php_uname('n'),
                'machine'   => php_uname('m'),
            ];
            $result['php'] = static::getPhpInfo();
        }

        $login = static::getLoginApi();
        $apps = static::getApps();
        $groupedApps = ArrayUtils::get($apps, 0);
        $noGroupApps = ArrayUtils::get($apps, 1);

        $result['authentication'] = $login;
        $result['app_group'] = (count($groupedApps)>0)? $groupedApps : [];
        $result['no_group_app'] = (count($noGroupApps)>0)? $noGroupApps : [];

        return $result;
    }

    protected static function getApps()
    {
        if (SessionUtilities::isAuthenticated()) {
            $user = SessionUtilities::user();
            $defaultAppId = $user->default_app_id;

            if (SessionUtilities::isSysAdmin()) {
                $appGroups = AppGroupModel::with(
                    [
                        'app_by_app_to_app_group' => function ($q){
                            $q->whereIsActive(1)->whereIn('type', [AppTypes::PATH, AppTypes::URL]);
                        }
                    ]
                )->get();
                $apps = AppModel::whereIsActive(1)->whereIn('type', [AppTypes::PATH, AppTypes::URL])->get();
            } else {
                $userId = $user->id;
                $userAppRoles = UserAppRole::whereUserId($userId)->whereNotNull('role_id')->get(['app_id']);
                $appIds = [];
                foreach ($userAppRoles as $uar) {
                    $appIds[] = $uar->app_id;
                }
                $appIdsString = implode(',', $appIds);
                $typeStirng = implode(',', [AppTypes::PATH, AppTypes::URL]);

                $appGroups = AppGroupModel::with(
                    [
                        'app_by_app_to_app_group' => function ($q) use ($appIdsString, $typeStirng){
                            $q->whereRaw("(app.id IN ($appIdsString) OR role_id > 0) AND is_active = 1 AND type IN ($typeStirng)");
                        }
                    ]
                )->get();
                $apps = AppModel::whereRaw("(app.id IN ($appIdsString) OR role_id > 0) AND is_active = 1 AND type IN ($typeStirng)")->get();
            }
        } else {
            $appGroups = AppGroupModel::with(
                [
                    'app_by_app_to_app_group' => function ($q){
                        $q->where('role_id', '>', 0)
                            ->whereIsActive(1)
                            ->whereIn('type', [AppTypes::PATH, AppTypes::URL]);<commit_msg>bugfix<commit_after><?php

namespace DreamFactory\Core\Resources\System;

use DreamFactory\Core\Enums\AppTypes;
use DreamFactory\Core\Models\App as AppModel;
use DreamFactory\Core\Models\AppGroup as AppGroupModel;
use DreamFactory\Core\Models\Service as ServiceModel;
use DreamFactory\Core\Models\UserAppRole;
use DreamFactory\Core\Resources\BaseRestResource;
use DreamFactory\Core\User\Services\User;
use DreamFactory\Core\Utility\Session as SessionUtilities;
use DreamFactory\Library\Utility\ArrayUtils;
use DreamFactory\Library\Utility\Enums\Verbs;
use DreamFactory\Library\Utility\Scalar;
use DreamFactory\Core\Models\Config as SystemConfig;

class Environment extends BaseRestResource
{
    /**
     * @return array
     */
    protected function handleGET()
    {
        $result = [];

        $result['platform'] = [
            'version_current'   => '2.0.0',
            'version_latest'    => '2.0.0',
            'upgrade_available' => false,
            'is_hosted'         => false,
            'host'              => php_uname('n'),
        ];

        if (SessionUtilities::isSysAdmin()) {
            $result['server'] = [
                'server_os' => strtolower(php_uname('s')),
                'release'   => php_uname('r'),
                'version'   => php_uname('v'),
                'host'      => php_uname('n'),
                'machine'   => php_uname('m'),
            ];
            $result['php'] = static::getPhpInfo();
        }

        $login = static::getLoginApi();
        $apps = static::getApps();
        $groupedApps = ArrayUtils::get($apps, 0);
        $noGroupApps = ArrayUtils::get($apps, 1);

        $result['authentication'] = $login;
        $result['app_group'] = (count($groupedApps)>0)? $groupedApps : [];
        $result['no_group_app'] = (count($noGroupApps)>0)? $noGroupApps : [];

        return $result;
    }

    protected static function getApps()
    {
        if (SessionUtilities::isAuthenticated()) {
            $user = SessionUtilities::user();
            $defaultAppId = $user->default_app_id;

            if (SessionUtilities::isSysAdmin()) {
                $appGroups = AppGroupModel::with(
                    [
                        'app_by_app_to_app_group' => function ($q){
                            $q->whereIsActive(1)->whereIn('type', [AppTypes::PATH, AppTypes::URL]);
                        }
                    ]
                )->get();
                $apps = AppModel::whereIsActive(1)->whereIn('type', [AppTypes::PATH, AppTypes::URL])->get();
            } else {
                $userId = $user->id;
                $userAppRoles = UserAppRole::whereUserId($userId)->whereNotNull('role_id')->get(['app_id']);
                $appIds = [];
                foreach ($userAppRoles as $uar) {
                    $appIds[] = $uar->app_id;
                }
                $appIdsString = implode(',', $appIds);
                $appIdsString = (empty($appIdsString))? '-1' : $appIdsString;
                $typeString = implode(',', [AppTypes::PATH, AppTypes::URL]);
                $typeString = (empty($typeString))? '-1' : $typeString;

                $appGroups = AppGroupModel::with(
                    [
                        'app_by_app_to_app_group' => function ($q) use ($appIdsString, $typeString){
                            $q->whereRaw("(app.id IN ($appIdsString) OR role_id > 0) AND is_active = 1 AND type IN ($typeString)");
                        }
                    ]
                )->get();
                $apps = AppModel::whereRaw("(app.id IN ($appIdsString) OR role_id > 0) AND is_active = 1 AND type IN ($typeString)")->get();
            }
        } else {
            $appGroups = AppGroupModel::with(
                [
                    'app_by_app_to_app_group' => function ($q){
                        $q->where('role_id', '>', 0)
                            ->whereIsActive(1)
                            ->whereIn('type', [AppTypes::PATH, AppTypes::URL]);
{'fraction_non_alphanumeric': 0.14779834077855775, 'fraction_numerical': 0.004850031908104659, 'mean_word_length': 1.8484187568157033, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 71, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1671222', 'n_tokens_mistral': 2305, 'n_tokens_neox': 2230, 'n_words': 377}