Passed
Push — master ( 272761...ab3804 )
by
unknown
16:40 queued 08:13
created

decodeSettingArray()   A

Complexity

Conditions 6
Paths 6

Size

Total Lines 29
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 14
nc 6
nop 1
dl 0
loc 29
rs 9.2222
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/* For licensing terms, see /license.txt */
6
7
namespace Chamilo\CoreBundle\Controller;
8
9
use Bbb;
10
use Chamilo\CoreBundle\Repository\Node\CourseRepository;
11
use Chamilo\CoreBundle\ServiceHelper\AuthenticationConfigHelper;
12
use Chamilo\CoreBundle\ServiceHelper\ThemeHelper;
13
use Chamilo\CoreBundle\ServiceHelper\TicketProjectHelper;
14
use Chamilo\CoreBundle\ServiceHelper\UserHelper;
15
use Chamilo\CoreBundle\Settings\SettingsManager;
16
use Chamilo\CoreBundle\Traits\ControllerTrait;
17
use Chamilo\CourseBundle\Settings\SettingsCourseManager;
18
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
19
use Symfony\Component\HttpFoundation\JsonResponse;
20
use Symfony\Component\HttpFoundation\Request;
21
use Symfony\Component\HttpFoundation\Response;
22
use Symfony\Component\Routing\Attribute\Route;
23
24
#[Route('/platform-config')]
25
class PlatformConfigurationController extends AbstractController
26
{
27
    use ControllerTrait;
28
29
    public function __construct(
30
        private readonly TicketProjectHelper $ticketProjectHelper,
31
        private readonly UserHelper $userHelper,
32
        private readonly ThemeHelper $themeHelper,
33
        private readonly AuthenticationConfigHelper $authenticationConfigHelper,
34
    ) {}
35
36
    #[Route('/list', name: 'platform_config_list', methods: ['GET'])]
37
    public function list(SettingsManager $settingsManager): Response
38
    {
39
        $requestSession = $this->getRequest()->getSession();
40
41
        $configuration = [
42
            'settings' => [],
43
            'studentview' => $requestSession->get('studentview'),
44
            'plugins' => [],
45
            'visual_theme' => $this->themeHelper->getVisualTheme(),
46
            'oauth2_providers' => $this->authenticationConfigHelper->getEnabledOAuthProviders(),
47
        ];
48
49
        $configuration['settings']['registration.allow_registration'] = $settingsManager->getSetting('registration.allow_registration', true);
50
        $configuration['settings']['course.course_catalog_published'] = $settingsManager->getSetting('course.course_catalog_published', true);
51
        $configuration['settings']['course.catalog_hide_public_link'] = $settingsManager->getSetting('course.catalog_hide_public_link', true);
52
        $configuration['settings']['course.allow_course_extra_field_in_catalog'] = $settingsManager->getSetting('course.allow_course_extra_field_in_catalog', true);
53
        $configuration['settings']['course.course_catalog_display_in_home'] = $settingsManager->getSetting('course.course_catalog_display_in_home', true);
54
        $configuration['settings']['course.courses_catalogue_show_only_category'] = $settingsManager->getSetting('course.courses_catalogue_show_only_category', true);
55
        $configuration['settings']['display.allow_students_to_browse_courses'] = $settingsManager->getSetting('display.allow_students_to_browse_courses', true);
56
        $configuration['settings']['session.catalog_allow_session_auto_subscription'] = $settingsManager->getSetting('session.catalog_allow_session_auto_subscription', true);
57
        $configuration['settings']['session.catalog_course_subscription_in_user_s_session'] = $settingsManager->getSetting('session.catalog_course_subscription_in_user_s_session', true);
58
        $rawSetting = $settingsManager->getSetting('course.course_catalog_settings', true);
59
        $configuration['settings']['course.course_catalog_settings'] = $this->decodeSettingArray($rawSetting);
60
61
        $variables = [];
62
63
        if ($this->isGranted('ROLE_USER')) {
64
            $variables = [
65
                'platform.site_name',
66
                'platform.timezone',
67
                'platform.registered',
68
                'platform.donotlistcampus',
69
                'platform.load_term_conditions_section',
70
                'platform.cookie_warning',
71
                'platform.show_tabs',
72
                'platform.catalog_show_courses_sessions',
73
                'admin.administrator_name',
74
                'admin.administrator_surname',
75
                'editor.enabled_mathjax',
76
                'editor.translate_html',
77
                'display.show_admin_toolbar',
78
                'registration.allow_terms_conditions',
79
                'agenda.allow_personal_agenda',
80
                'agenda.personal_calendar_show_sessions_occupation',
81
                'social.social_enable_messages_feedback',
82
                'social.disable_dislike_option',
83
                'skill.allow_skills_tool',
84
                'gradebook.gradebook_enable_grade_model',
85
                'gradebook.gradebook_dependency',
86
                'course.course_validation',
87
                'course.student_view_enabled',
88
                'course.allow_edit_tool_visibility_in_session',
89
                'session.limit_session_admin_role',
90
                'session.allow_session_admin_read_careers',
91
                'session.limit_session_admin_list_users',
92
                'platform.redirect_index_to_url_for_logged_users',
93
                'language.platform_language',
94
                'language.language_priority_1',
95
                'language.language_priority_2',
96
                'language.language_priority_3',
97
                'language.language_priority_4',
98
                'profile.allow_social_map_fields',
99
                'forum.global_forums_course_id',
100
                'document.students_download_folders',
101
                'social.hide_social_groups_block',
102
                'course.show_course_duration',
103
                'attendance.attendance_allow_comments',
104
                'attendance.multilevel_grading',
105
                'attendance.enable_sign_attendance_sheet',
106
                'exercise.allow_exercise_auto_launch',
107
                'course.access_url_specific_files',
108
                'platform.course_catalog_hide_private',
109
                'course.show_courses_descriptions_in_catalog',
110
                'session.session_automatic_creation_user_id',
111
            ];
112
113
            $user = $this->userHelper->getCurrent();
114
115
            $configuration['settings']['display.show_link_ticket_notification'] = 'false';
116
117
            if (!empty($user)) {
118
                $userIsAllowedInProject = $this->ticketProjectHelper->userIsAllowInProject(1);
119
120
                if ($userIsAllowedInProject
121
                    && 'true' === $settingsManager->getSetting('display.show_link_ticket_notification')
122
                ) {
123
                    $configuration['settings']['display.show_link_ticket_notification'] = 'true';
124
                }
125
            }
126
127
            $configuration['plugins']['bbb'] = [
128
                'show_global_conference_link' => Bbb::showGlobalConferenceLink([
129
                    'username' => $user->getUserIdentifier(),
130
                    'status' => $user->getStatus(),
131
                ]),
132
                'listingURL' => (new Bbb('', '', true, $user->getId()))->getListingUrl(),
133
            ];
134
        }
135
136
        foreach ($variables as $variable) {
137
            $value = $settingsManager->getSetting($variable, true);
138
139
            $configuration['settings'][$variable] = $value;
140
        }
141
142
        return new JsonResponse($configuration);
143
    }
144
145
    #[Route('/list/course_settings', name: 'course_settings_list', methods: ['GET'])]
146
    public function courseSettingsList(
147
        SettingsCourseManager $courseSettingsManager,
148
        CourseRepository $courseRepository,
149
        Request $request
150
    ): JsonResponse {
151
        $courseId = $request->query->get('cid');
152
        if (!$courseId) {
153
            return new JsonResponse(['error' => 'Course ID is required'], Response::HTTP_BAD_REQUEST);
154
        }
155
156
        $course = $courseRepository->find($courseId);
157
        if (!$course) {
158
            return new JsonResponse(['error' => 'Course not found'], Response::HTTP_NOT_FOUND);
159
        }
160
161
        $courseSettingsManager->setCourse($course);
162
        $settings = [
163
            'show_course_in_user_language' => $courseSettingsManager->getCourseSettingValue('show_course_in_user_language'),
164
            'allow_user_edit_agenda' => $courseSettingsManager->getCourseSettingValue('allow_user_edit_agenda'),
165
            'enable_document_auto_launch' => $courseSettingsManager->getCourseSettingValue('enable_document_auto_launch'),
166
            'enable_exercise_auto_launch' => $courseSettingsManager->getCourseSettingValue('enable_exercise_auto_launch'),
167
            'enable_lp_auto_launch' => $courseSettingsManager->getCourseSettingValue('enable_lp_auto_launch'),
168
            'enable_forum_auto_launch' => $courseSettingsManager->getCourseSettingValue('enable_forum_auto_launch'),
169
        ];
170
171
        return new JsonResponse(['settings' => $settings]);
172
    }
173
174
    /**
175
     * Attempts to decode a setting value that may be stored as:
176
     * - native PHP array
177
     * - JSON string
178
     * - PHP array code string
179
     */
180
    private function decodeSettingArray(mixed $setting): array
181
    {
182
        // Already an array, return as is
183
        if (is_array($setting)) {
184
            return $setting;
185
        }
186
187
        // Try to decode JSON string
188
        if (is_string($setting)) {
189
            $json = json_decode($setting, true);
190
            if (is_array($json)) {
191
                return $json;
192
            }
193
194
            // Try to evaluate PHP-style array string
195
            $trimmed = rtrim($setting, ';');
196
            try {
197
                $evaluated = eval("return $trimmed;");
0 ignored issues
show
introduced by
The use of eval() is discouraged.
Loading history...
198
                if (is_array($evaluated)) {
199
                    return $evaluated;
200
                }
201
            } catch (\Throwable $e) {
202
                // Log error and continue
203
                error_log("Failed to eval setting value: " . $e->getMessage());
204
            }
205
        }
206
207
        // Return empty array as fallback
208
        return [];
209
    }
210
}
211