Skip to content

Commit 0da7acc

Browse files
Learnpath: Minor: cleaning code with phpcs-fix
1 parent 354934e commit 0da7acc

1 file changed

Lines changed: 54 additions & 41 deletions

File tree

public/main/lp/lp_update_scorm.php

Lines changed: 54 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
<?php
2+
3+
declare(strict_types=1);
24
/* For licensing terms, see /license.txt */
35

46
use Chamilo\CoreBundle\Entity\Asset;
@@ -11,55 +13,56 @@
1113
api_protect_course_script();
1214

1315
$allow = api_is_allowed_to_edit(null, true);
14-
$lpId = !empty($_GET['lp_id']) ? (int) $_GET['lp_id'] : 0;
16+
$lpId = !empty($_GET['lp_id']) ? (int) $_GET['lp_id'] : 0;
1517

1618
if (!$allow || $lpId <= 0) {
1719
api_not_allowed(true);
1820
}
1921

2022
$courseInfo = api_get_course_info();
21-
$userId = api_get_user_id();
23+
$userId = api_get_user_id();
2224

2325
// Prevent open redirects: allow only absolute URLs under WEB_PATH or relative paths.
2426
$sanitizeReturnTo = static function (?string $url): string {
2527
$url = trim((string) $url);
26-
if ($url === '') {
28+
if ('' === $url) {
2729
return '';
2830
}
2931

3032
// Allow relative internal paths.
31-
if (0 === strpos($url, '/')) {
33+
if (str_starts_with($url, '/')) {
3234
return $url;
3335
}
3436

3537
// Allow absolute URLs that start with the platform WEB_PATH.
3638
$webPath = api_get_path(WEB_PATH);
37-
if ($webPath && 0 === strpos($url, $webPath)) {
39+
if ($webPath && str_starts_with($url, $webPath)) {
3840
return $url;
3941
}
4042

4143
return '';
4244
};
4345

4446
$returnToRaw = $_GET['returnTo'] ?? '';
45-
$returnTo = $sanitizeReturnTo($returnToRaw);
47+
$returnTo = $sanitizeReturnTo($returnToRaw);
4648

4749
$lpRepo = Container::getLpRepository();
50+
4851
/** @var CLp|null $lpEntity */
4952
$lpEntity = $lpRepo->find($lpId);
5053

51-
if (!$lpEntity || (int) $lpEntity->getLpType() !== CLp::SCORM_TYPE) {
54+
if (!$lpEntity || CLp::SCORM_TYPE !== (int) $lpEntity->getLpType()) {
5255
Display::addFlash(Display::return_message(get_lang('No learning path found'), 'error'));
5356
api_not_allowed(true);
5457
}
5558

5659
// Breadcrumbs.
5760
$interbreadcrumb[] = [
58-
'url' => 'lp_controller.php?action=list&'.api_get_cidreq(),
61+
'url' => 'lp_controller.php?action=list&'.api_get_cidreq(),
5962
'name' => get_lang('Learning paths'),
6063
];
6164
$interbreadcrumb[] = [
62-
'url' => api_get_self().'?'.api_get_cidreq().'&lp_id='.$lpId,
65+
'url' => api_get_self().'?'.api_get_cidreq().'&lp_id='.$lpId,
6366
'name' => Security::remove_XSS((string) $lpEntity->getTitle()),
6467
];
6568

@@ -69,7 +72,7 @@
6972
api_get_self().'?'.api_get_cidreq().'&lp_id='.$lpId.($returnTo ? '&returnTo='.urlencode($returnTo) : ''),
7073
'',
7174
[
72-
'id' => 'upload_form',
75+
'id' => 'upload_form',
7376
'enctype' => 'multipart/form-data',
7477
]
7578
);
@@ -90,23 +93,22 @@
9093
/**
9194
* Map PHP upload error codes to readable messages.
9295
*/
93-
$uploadErrorToMessage = static function (int $code): string {
94-
// Messages in English as requested.
96+
$uploadErrorToMessageKey = static function (int $code): string {
9597
return match ($code) {
96-
UPLOAD_ERR_INI_SIZE => 'The uploaded file exceeds upload_max_filesize.',
97-
UPLOAD_ERR_FORM_SIZE => 'The uploaded file exceeds the MAX_FILE_SIZE directive.',
98-
UPLOAD_ERR_PARTIAL => 'The uploaded file was only partially uploaded.',
99-
UPLOAD_ERR_NO_FILE => 'No file was uploaded.',
100-
UPLOAD_ERR_NO_TMP_DIR => 'Missing a temporary folder on the server.',
101-
UPLOAD_ERR_CANT_WRITE => 'Failed to write file to disk.',
102-
UPLOAD_ERR_EXTENSION => 'A PHP extension stopped the file upload.',
103-
default => 'Unknown upload error.',
98+
\UPLOAD_ERR_INI_SIZE => 'The uploaded file exceeds upload_max_filesize.',
99+
\UPLOAD_ERR_FORM_SIZE => 'The uploaded file exceeds the MAX_FILE_SIZE directive.',
100+
\UPLOAD_ERR_PARTIAL => 'The uploaded file was only partially uploaded.',
101+
\UPLOAD_ERR_NO_FILE => 'No file was uploaded.',
102+
\UPLOAD_ERR_NO_TMP_DIR => 'Missing a temporary folder on the server.',
103+
\UPLOAD_ERR_CANT_WRITE => 'Failed to write file to disk.',
104+
\UPLOAD_ERR_EXTENSION => 'A PHP extension stopped the file upload.',
105+
default => 'Unknown upload error.',
104106
};
105107
};
106108

107109
$isPost = ($_SERVER['REQUEST_METHOD'] ?? '') === 'POST';
108110

109-
// detect the classic "post_max_size exceeded" case.
111+
// Detect the classic "post_max_size exceeded" case.
110112
// When that happens, PHP gives you empty $_POST and empty $_FILES.
111113
if ($isPost && empty($_POST) && empty($_FILES) && !empty($_SERVER['CONTENT_LENGTH'])) {
112114
$cl = (int) $_SERVER['CONTENT_LENGTH'];
@@ -116,7 +118,7 @@
116118

117119
Display::addFlash(
118120
Display::return_message(
119-
'Upload failed. The file is likely larger than the server limit (post_max_size).',
121+
get_lang('Upload failed. The file is likely larger than the server limit (post_max_size).'),
120122
'error'
121123
)
122124
);
@@ -127,42 +129,50 @@
127129

128130
if (!is_array($file)) {
129131
error_log("lp_update_scorm.php - No user_file found in \$_FILES. lp_id={$lpId}, user_id={$userId}");
130-
Display::addFlash(Display::return_message('Update failed. No file received by the server.', 'error'));
132+
Display::addFlash(Display::return_message(get_lang('Update failed. No file received by the server.'), 'error'));
131133
} else {
132-
$fileErr = (int) ($file['error'] ?? UPLOAD_ERR_NO_FILE);
133-
if ($fileErr !== UPLOAD_ERR_OK) {
134-
$msg = $uploadErrorToMessage($fileErr);
135-
error_log("lp_update_scorm.php - Upload error: {$msg} (code={$fileErr}) lp_id={$lpId}, user_id={$userId}");
136-
Display::addFlash(Display::return_message('Update failed. '.$msg, 'error'));
134+
$fileErr = (int) ($file['error'] ?? \UPLOAD_ERR_NO_FILE);
135+
if (\UPLOAD_ERR_OK !== $fileErr) {
136+
$msgKey = $uploadErrorToMessageKey($fileErr);
137+
$msgUi = get_lang($msgKey);
138+
139+
error_log("lp_update_scorm.php - Upload error: {$msgKey} (code={$fileErr}) lp_id={$lpId}, user_id={$userId}");
140+
141+
Display::addFlash(
142+
Display::return_message(
143+
sprintf(get_lang('Update failed. %s'), $msgUi),
144+
'error'
145+
)
146+
);
137147
} else {
138148
$uploadedName = (string) ($file['name'] ?? '');
139-
$tmpPath = (string) ($file['tmp_name'] ?? '');
140-
$size = (int) ($file['size'] ?? 0);
149+
$tmpPath = (string) ($file['tmp_name'] ?? '');
150+
$size = (int) ($file['size'] ?? 0);
141151

142-
if ($tmpPath === '' || !is_uploaded_file($tmpPath) || $size <= 0) {
152+
if ('' === $tmpPath || !is_uploaded_file($tmpPath) || $size <= 0) {
143153
error_log("lp_update_scorm.php - Invalid upload payload. tmp='{$tmpPath}', size={$size} lp_id={$lpId}, user_id={$userId}");
144-
Display::addFlash(Display::return_message('Update failed. Invalid uploaded file payload.', 'error'));
154+
Display::addFlash(Display::return_message(get_lang('Update failed. Invalid uploaded file payload.'), 'error'));
145155
} else {
146156
// Enforce same base name rule to ensure replacement targets the same folder.
147-
$expectedPath = trim((string) $lpEntity->getPath());
148-
$expectedFirstDir = $expectedPath !== '' ? trim((string) strtok($expectedPath, '/')) : '';
157+
$expectedPath = trim((string) $lpEntity->getPath());
158+
$expectedFirstDir = '' !== $expectedPath ? trim((string) strtok($expectedPath, '/')) : '';
149159

150160
$pi = pathinfo($uploadedName);
151161
$uploadedBase = trim((string) ($pi['filename'] ?? ''));
152162

153-
if ($expectedFirstDir !== '' && $uploadedBase !== '') {
163+
if ('' !== $expectedFirstDir && '' !== $uploadedBase) {
154164
$expectedSafe = api_replace_dangerous_char($expectedFirstDir);
155165
$uploadedSafe = api_replace_dangerous_char($uploadedBase);
156166

157-
if ($expectedSafe !== '' && $uploadedSafe !== '' && $expectedSafe !== $uploadedSafe) {
167+
if ('' !== $expectedSafe && '' !== $uploadedSafe && $expectedSafe !== $uploadedSafe) {
158168
error_log(
159169
"lp_update_scorm.php - Update rejected: zip base name mismatch. expected='{$expectedSafe}', got='{$uploadedSafe}' ".
160170
"(lp_id={$lpId}, user_id={$userId})"
161171
);
162172

163173
Display::addFlash(
164174
Display::return_message(
165-
'Update failed. The uploaded ZIP file name must match the original SCORM package name.',
175+
get_lang('Update failed. The uploaded ZIP file name must match the original SCORM package name.'),
166176
'error'
167177
)
168178
);
@@ -172,6 +182,7 @@
172182
$tpl = new Template(null);
173183
$tpl->assign('content', $content);
174184
$tpl->display_one_col_template();
185+
175186
exit;
176187
}
177188
}
@@ -187,10 +198,10 @@
187198

188199
error_log(
189200
"lp_update_scorm.php - Starting SCORM replace. lp_id={$lpId}, user_id={$userId}, file='{$uploadedName}', size={$size}, old_asset_id=".
190-
($oldAssetId !== null ? $oldAssetId : 'null')
201+
(null !== $oldAssetId ? $oldAssetId : 'null')
191202
);
192203

193-
// Replace mode
204+
// Replace mode.
194205
$oScorm = new scorm($lpEntity, $courseInfo, $userId);
195206

196207
try {
@@ -218,7 +229,7 @@
218229

219230
$newAssetId = method_exists($oScorm->asset, 'getId') ? (string) $oScorm->asset->getId() : null;
220231
error_log(
221-
"lp_update_scorm.php - Replace succeeded. LP asset linked. new_asset_id=".
232+
'lp_update_scorm.php - Replace succeeded. LP asset linked. new_asset_id='.
222233
(is_string($newAssetId) ? $newAssetId : 'null')
223234
);
224235
} else {
@@ -230,12 +241,14 @@
230241

231242
Display::addFlash(Display::return_message(get_lang('Update successful')));
232243

233-
if ($returnTo !== '') {
244+
if ('' !== $returnTo) {
234245
header('Location: '.$returnTo);
246+
235247
exit;
236248
}
237249

238250
header('Location: lp_controller.php?action=list&'.api_get_cidreq());
251+
239252
exit;
240253
}
241254

0 commit comments

Comments
 (0)