Passed
Push — master ( 859bd5...c750b1 )
by Julito
10:16 queued 10s
created

upgradeWithContainer()   A

Complexity

Conditions 1

Size

Total Lines 12
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 10
nop 1
dl 0
loc 12
rs 9.9332
c 0
b 0
f 0
1
<?php
2
/* For licensing terms, see /license.txt */
3
4
use Chamilo\CoreBundle\Entity\AccessUrl;
5
use Chamilo\CoreBundle\Entity\ExtraField;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, ExtraField. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
6
use Chamilo\CoreBundle\Framework\Container;
7
use Chamilo\TicketBundle\Entity\Category as TicketCategory;
8
use Chamilo\TicketBundle\Entity\Priority as TicketPriority;
9
use Chamilo\TicketBundle\Entity\Project as TicketProject;
10
use Doctrine\ORM\EntityManager;
11
use Sonata\PageBundle\Entity\PageManager;
12
use Symfony\Component\DependencyInjection\Container as SymfonyContainer;
13
14
/**
15
 * Chamilo LMS
16
 * This file contains functions used by the install and upgrade scripts.
17
 *
18
 * Ideas for future additions:
19
 * - a function get_old_version_settings to retrieve the config file settings
20
 *   of older versions before upgrading.
21
 */
22
23
/* CONSTANTS */
24
define('SYSTEM_CONFIG_FILENAME', 'configuration.dist.php');
25
26
/**
27
 * This function detects whether the system has been already installed.
28
 * It should be used for prevention from second running the installation
29
 * script and as a result - destroying a production system.
30
 *
31
 * @return bool The detected result;
32
 *
33
 * @author Ivan Tcholakov, 2010;
34
 */
35
function isAlreadyInstalledSystem()
36
{
37
    global $new_version, $_configuration;
38
39
    if (empty($new_version)) {
40
        return true; // Must be initialized.
41
    }
42
43
    $current_config_file = api_get_path(CONFIGURATION_PATH).'configuration.php';
44
    if (!file_exists($current_config_file)) {
45
        return false; // Configuration file does not exist, install the system.
46
    }
47
    require $current_config_file;
48
49
    $current_version = null;
50
    if (isset($_configuration['system_version'])) {
51
        $current_version = trim($_configuration['system_version']);
52
    }
53
54
    // If the current version is old, upgrading is assumed, the installer goes ahead.
55
    return empty($current_version) ? false : version_compare($current_version, $new_version, '>=');
56
}
57
58
/**
59
 * This function checks if a php extension exists or not and returns an HTML status string.
60
 *
61
 * @param string $extensionName Name of the PHP extension to be checked
62
 * @param string $returnSuccess Text to show when extension is available (defaults to 'Yes')
63
 * @param string $returnFailure Text to show when extension is available (defaults to 'No')
64
 * @param bool   $optional      Whether this extension is optional (then show unavailable text in orange rather than red)
65
 * @param string $enabledTerm   If this string is not null, then use to check if the corresponding parameter is = 1.
66
 *                              If not, mention it's present but not enabled. For example, for opcache, this should be 'opcache.enable'
67
 *
68
 * @return string HTML string reporting the status of this extension. Language-aware.
69
 *
70
 * @author  Christophe Gesch??
71
 * @author  Patrick Cool <[email protected]>, Ghent University
72
 * @author  Yannick Warnier <[email protected]>
73
 */
74
function checkExtension(
75
    $extensionName,
76
    $returnSuccess = 'Yes',
77
    $returnFailure = 'No',
78
    $optional = false,
79
    $enabledTerm = ''
80
) {
81
    if (extension_loaded($extensionName)) {
82
        if (!empty($enabledTerm)) {
83
            $isEnabled = ini_get($enabledTerm);
84
            if ($isEnabled == '1') {
85
                return Display::label($returnSuccess, 'success');
86
            } else {
87
                if ($optional) {
88
                    return Display::label(get_lang('ExtensionInstalledButNotEnabled'), 'warning');
89
                } else {
90
                    return Display::label(get_lang('ExtensionInstalledButNotEnabled'), 'important');
91
                }
92
            }
93
        } else {
94
            return Display::label($returnSuccess, 'success');
95
        }
96
    } else {
97
        if ($optional) {
98
            return Display::label($returnFailure, 'warning');
99
        } else {
100
            return Display::label($returnFailure, 'important');
101
        }
102
    }
103
}
104
105
/**
106
 * This function checks whether a php setting matches the recommended value.
107
 *
108
 * @param string $phpSetting       A PHP setting to check
109
 * @param string $recommendedValue A recommended value to show on screen
110
 * @param mixed  $returnSuccess    What to show on success
111
 * @param mixed  $returnFailure    What to show on failure
112
 *
113
 * @return string A label to show
114
 *
115
 * @author Patrick Cool <[email protected]>, Ghent University
116
 */
117
function checkPhpSetting(
118
    $phpSetting,
119
    $recommendedValue,
120
    $returnSuccess = false,
121
    $returnFailure = false
0 ignored issues
show
Unused Code introduced by
The parameter $returnFailure is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

121
    /** @scrutinizer ignore-unused */ $returnFailure = false

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
122
) {
123
    $currentPhpValue = getPhpSetting($phpSetting);
124
    if ($currentPhpValue == $recommendedValue) {
125
        return Display::label($currentPhpValue.' '.$returnSuccess, 'success');
0 ignored issues
show
Bug introduced by
Are you sure $returnSuccess of type mixed|false can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

125
        return Display::label($currentPhpValue.' './** @scrutinizer ignore-type */ $returnSuccess, 'success');
Loading history...
126
    } else {
127
        return Display::label($currentPhpValue.' '.$returnSuccess, 'important');
128
    }
129
}
130
131
/**
132
 * This function return the value of a php.ini setting if not "" or if exists,
133
 * otherwise return false.
134
 *
135
 * @param string $phpSetting The name of a PHP setting
136
 *
137
 * @return mixed The value of the setting, or false if not found
138
 */
139
function checkPhpSettingExists($phpSetting)
140
{
141
    if (ini_get($phpSetting) != "") {
142
        return ini_get($phpSetting);
143
    }
144
145
    return false;
146
}
147
148
/**
149
 * Check if the current url is the same root_web when the multiple_access_url is enabled.
150
 *
151
 * @return bool
152
 */
153
function checkAccessUrl()
154
{
155
    if (api_get_configuration_value('multiple_access_urls') !== true) {
156
        return true;
157
    }
158
159
    $currentWebPath = api_get_path(WEB_PATH);
160
    $rootWeb = api_get_configuration_value('root_web');
161
162
    return $currentWebPath === $rootWeb;
163
}
164
165
/**
166
 * Returns a textual value ('ON' or 'OFF') based on a requester 2-state ini- configuration setting.
167
 *
168
 * @param string $val a php ini value
169
 *
170
 * @return bool ON or OFF
171
 *
172
 * @author Joomla <http://www.joomla.org>
173
 */
174
function getPhpSetting($val)
175
{
176
    $value = ini_get($val);
177
    switch ($val) {
178
        case 'display_errors':
179
            global $originalDisplayErrors;
180
            $value = $originalDisplayErrors;
181
            break;
182
    }
183
184
    return $value == '1' ? 'ON' : 'OFF';
0 ignored issues
show
Bug Best Practice introduced by
The expression return $value == '1' ? 'ON' : 'OFF' returns the type string which is incompatible with the documented return type boolean.
Loading history...
185
}
186
187
/**
188
 * This function returns a string "true" or "false" according to the passed parameter.
189
 *
190
 * @param int $var The variable to present as text
191
 *
192
 * @return string the string "true" or "false"
193
 *
194
 * @author Christophe Gesch??
195
 */
196
function trueFalse($var)
197
{
198
    return $var ? 'true' : 'false';
199
}
200
201
/**
202
 * Removes memory and time limits as much as possible.
203
 */
204
function remove_memory_and_time_limits()
205
{
206
    if (function_exists('ini_set')) {
207
        ini_set('memory_limit', -1);
208
        ini_set('max_execution_time', 0);
209
        error_log('Update-db script: memory_limit set to -1', 0);
210
        error_log('Update-db script: max_execution_time 0', 0);
211
    } else {
212
        error_log('Update-db script: could not change memory and time limits', 0);
213
    }
214
}
215
216
/**
217
 * Detects browser's language.
218
 *
219
 * @return string Returns a language identificator, i.e. 'english', 'spanish', ...
220
 *
221
 * @author Ivan Tcholakov, 2010
222
 */
223
function detect_browser_language()
224
{
225
    static $language_index = [
226
        'ar' => 'arabic',
227
        'ast' => 'asturian',
228
        'bg' => 'bulgarian',
229
        'bs' => 'bosnian',
230
        'ca' => 'catalan',
231
        'zh' => 'simpl_chinese',
232
        'zh-tw' => 'trad_chinese',
233
        'cs' => 'czech',
234
        'da' => 'danish',
235
        'prs' => 'dari',
236
        'de' => 'german',
237
        'el' => 'greek',
238
        'en' => 'english',
239
        'es' => 'spanish',
240
        'eo' => 'esperanto',
241
        'eu' => 'basque',
242
        'fa' => 'persian',
243
        'fr' => 'french',
244
        'fur' => 'friulian',
245
        'gl' => 'galician',
246
        'ka' => 'georgian',
247
        'hr' => 'croatian',
248
        'he' => 'hebrew',
249
        'hi' => 'hindi',
250
        'id' => 'indonesian',
251
        'it' => 'italian',
252
        'ko' => 'korean',
253
        'lv' => 'latvian',
254
        'lt' => 'lithuanian',
255
        'mk' => 'macedonian',
256
        'hu' => 'hungarian',
257
        'ms' => 'malay',
258
        'nl' => 'dutch',
259
        'ja' => 'japanese',
260
        'no' => 'norwegian',
261
        'oc' => 'occitan',
262
        'ps' => 'pashto',
263
        'pl' => 'polish',
264
        'pt' => 'portuguese',
265
        'pt-br' => 'brazilian',
266
        'ro' => 'romanian',
267
        'qu' => 'quechua_cusco',
268
        'ru' => 'russian',
269
        'sk' => 'slovak',
270
        'sl' => 'slovenian',
271
        'sr' => 'serbian',
272
        'fi' => 'finnish',
273
        'sv' => 'swedish',
274
        'th' => 'thai',
275
        'tr' => 'turkish',
276
        'uk' => 'ukrainian',
277
        'vi' => 'vietnamese',
278
        'sw' => 'swahili',
279
        'yo' => 'yoruba',
280
    ];
281
282
    $system_available_languages = get_language_folder_list();
283
    $accept_languages = strtolower(str_replace('_', '-', $_SERVER['HTTP_ACCEPT_LANGUAGE']));
284
    foreach ($language_index as $code => $language) {
285
        if (strpos($accept_languages, $code) === 0) {
286
            if (!empty($system_available_languages[$language])) {
287
                return $language;
288
            }
289
        }
290
    }
291
292
    $user_agent = strtolower(str_replace('_', '-', $_SERVER['HTTP_USER_AGENT']));
293
    foreach ($language_index as $code => $language) {
294
        if (@preg_match("/[\[\( ]{$code}[;,_\-\)]/", $user_agent)) {
295
            if (!empty($system_available_languages[$language])) {
296
                return $language;
297
            }
298
        }
299
    }
300
301
    return 'english';
302
}
303
304
/*      FILESYSTEM RELATED FUNCTIONS */
305
306
/**
307
 * This function checks if the given folder is writable.
308
 *
309
 * @param string $folder     Full path to a folder
310
 * @param bool   $suggestion Whether to show a suggestion or not
311
 *
312
 * @return string
313
 */
314
function check_writable($folder, $suggestion = false)
315
{
316
    if (is_writable($folder)) {
317
        return Display::label(get_lang('Writable'), 'success');
318
    } else {
319
        if ($suggestion) {
320
            return Display::label(get_lang('NotWritable'), 'info');
321
        } else {
322
            return Display::label(get_lang('NotWritable'), 'important');
323
        }
324
    }
325
}
326
327
/**
328
 * This function checks if the given folder is readable.
329
 *
330
 * @param string $folder     Full path to a folder
331
 * @param bool   $suggestion Whether to show a suggestion or not
332
 *
333
 * @return string
334
 */
335
function checkReadable($folder, $suggestion = false)
336
{
337
    if (is_readable($folder)) {
338
        return Display::label(get_lang('Readable'), 'success');
339
    } else {
340
        if ($suggestion) {
341
            return Display::label(get_lang('NotReadable'), 'info');
342
        } else {
343
            return Display::label(get_lang('NotReadable'), 'important');
344
        }
345
    }
346
}
347
348
/**
349
 * This function is similar to the core file() function, except that it
350
 * works with line endings in Windows (which is not the case of file()).
351
 *
352
 * @param string $filename
353
 *
354
 * @return array The lines of the file returned as an array
355
 */
356
function file_to_array($filename)
357
{
358
    if (!is_readable($filename) || is_dir($filename)) {
359
        return [];
360
    }
361
    $fp = fopen($filename, 'rb');
362
    $buffer = fread($fp, filesize($filename));
0 ignored issues
show
Bug introduced by
It seems like $fp can also be of type false; however, parameter $handle of fread() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

362
    $buffer = fread(/** @scrutinizer ignore-type */ $fp, filesize($filename));
Loading history...
363
    fclose($fp);
0 ignored issues
show
Bug introduced by
It seems like $fp can also be of type false; however, parameter $handle of fclose() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

363
    fclose(/** @scrutinizer ignore-type */ $fp);
Loading history...
364
365
    return explode('<br />', nl2br($buffer));
366
}
367
368
/**
369
 * We assume this function is called from install scripts that reside inside the install folder.
370
 */
371
function set_file_folder_permissions()
372
{
373
    @chmod('.', 0755); //set permissions on install dir
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for chmod(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

373
    /** @scrutinizer ignore-unhandled */ @chmod('.', 0755); //set permissions on install dir

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
374
    @chmod('..', 0755); //set permissions on parent dir of install dir
375
}
376
377
/**
378
 * Write the main system config file.
379
 *
380
 * @param string $path Path to the config file
381
 */
382
function write_system_config_file($path)
383
{
384
    global $dbHostForm;
385
    global $dbPortForm;
386
    global $dbUsernameForm;
387
    global $dbPassForm;
388
    global $dbNameForm;
389
    global $urlForm;
390
    global $pathForm;
391
    global $urlAppendPath;
392
    global $languageForm;
393
    global $encryptPassForm;
394
    global $session_lifetime;
395
    global $new_version;
396
    global $new_version_stable;
397
398
    $root_sys = api_add_trailing_slash(str_replace('\\', '/', realpath($pathForm)));
399
    $content = file_get_contents(__DIR__.'/'.SYSTEM_CONFIG_FILENAME);
400
401
    $config['{DATE_GENERATED}'] = date('r');
0 ignored issues
show
Comprehensibility Best Practice introduced by
$config was never initialized. Although not strictly required by PHP, it is generally a good practice to add $config = array(); before regardless.
Loading history...
402
    $config['{DATABASE_HOST}'] = $dbHostForm;
403
    $config['{DATABASE_PORT}'] = $dbPortForm;
404
    $config['{DATABASE_USER}'] = $dbUsernameForm;
405
    $config['{DATABASE_PASSWORD}'] = $dbPassForm;
406
    $config['{DATABASE_MAIN}'] = $dbNameForm;
407
    $config['{ROOT_WEB}'] = $urlForm;
408
    $config['{ROOT_SYS}'] = $root_sys;
409
    $config['{URL_APPEND_PATH}'] = $urlAppendPath;
410
    $config['{PLATFORM_LANGUAGE}'] = $languageForm;
411
    $config['{SECURITY_KEY}'] = md5(uniqid(rand().time()));
412
    $config['{ENCRYPT_PASSWORD}'] = $encryptPassForm;
413
414
    $config['SESSION_LIFETIME'] = $session_lifetime;
415
    $config['{NEW_VERSION}'] = $new_version;
416
    $config['NEW_VERSION_STABLE'] = trueFalse($new_version_stable);
417
418
    foreach ($config as $key => $value) {
419
        $content = str_replace($key, $value, $content);
420
    }
421
    $fp = @fopen($path, 'w');
422
423
    if (!$fp) {
0 ignored issues
show
introduced by
$fp is of type false|resource, thus it always evaluated to false.
Loading history...
424
        echo '<strong>
425
                <font color="red">Your script doesn\'t have write access to the config directory</font></strong><br />
426
                <em>('.str_replace('\\', '/', realpath($path)).')</em><br /><br />
427
                You probably do not have write access on Chamilo root directory,
428
                i.e. you should <em>CHMOD 777</em> or <em>755</em> or <em>775</em>.<br /><br />
429
                Your problems can be related on two possible causes:<br />
430
                <ul>
431
                  <li>Permission problems.<br />Try initially with <em>chmod -R 777</em> and increase restrictions gradually.</li>
432
                </ul>
433
                <a href="http://forum.chamilo.org/" target="_blank">Read about this problem in Support Forum</a><br /><br />
434
                Please go back to step 5.
435
                <p><input type="submit" name="step5" value="&lt; Back" /></p>
436
                </td></tr></table></form></body></html>';
437
        exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
438
    }
439
440
    fwrite($fp, $content);
441
    fclose($fp);
442
}
443
444
/**
445
 * Returns a list of language directories.
446
 */
447
function get_language_folder_list()
448
{
449
    return [
450
        'ar' => 'arabic',
451
        'ast' => 'asturian',
452
        'bg' => 'bulgarian',
453
        'bs' => 'bosnian',
454
        'ca' => 'catalan',
455
        'zh' => 'simpl_chinese',
456
        'zh-tw' => 'trad_chinese',
457
        'cs' => 'czech',
458
        'da' => 'danish',
459
        'prs' => 'dari',
460
        'de' => 'german',
461
        'el' => 'greek',
462
        'en' => 'english',
463
        'es' => 'spanish',
464
        'eo' => 'esperanto',
465
        'eu' => 'basque',
466
        'fa' => 'persian',
467
        'fr' => 'french',
468
        'fur' => 'friulian',
469
        'gl' => 'galician',
470
        'ka' => 'georgian',
471
        'hr' => 'croatian',
472
        'he' => 'hebrew',
473
        'hi' => 'hindi',
474
        'id' => 'indonesian',
475
        'it' => 'italian',
476
        'ko' => 'korean',
477
        'lv' => 'latvian',
478
        'lt' => 'lithuanian',
479
        'mk' => 'macedonian',
480
        'hu' => 'hungarian',
481
        'ms' => 'malay',
482
        'nl' => 'dutch',
483
        'ja' => 'japanese',
484
        'no' => 'norwegian',
485
        'oc' => 'occitan',
486
        'ps' => 'pashto',
487
        'pl' => 'polish',
488
        'pt' => 'portuguese',
489
        'pt-br' => 'brazilian',
490
        'ro' => 'romanian',
491
        'qu' => 'quechua_cusco',
492
        'ru' => 'russian',
493
        'sk' => 'slovak',
494
        'sl' => 'slovenian',
495
        'sr' => 'serbian',
496
        'fi' => 'finnish',
497
        'sv' => 'swedish',
498
        'th' => 'thai',
499
        'tr' => 'turkish',
500
        'uk' => 'ukrainian',
501
        'vi' => 'vietnamese',
502
        'sw' => 'swahili',
503
        'yo' => 'yoruba',
504
    ];
505
}
506
507
/**
508
 * This function returns the value of a parameter from the configuration file.
509
 *
510
 * WARNING - this function relies heavily on global variables $updateFromConfigFile
511
 * and $configFile, and also changes these globals. This can be rewritten.
512
 *
513
 * @param string $param      the parameter of which the value is returned
514
 * @param string $updatePath If we want to give the path rather than take it from POST
515
 *
516
 * @return string the value of the parameter
517
 *
518
 * @author Olivier Brouckaert
519
 * @author Reworked by Ivan Tcholakov, 2010
520
 */
521
function get_config_param($param, $updatePath = '')
522
{
523
    global $configFile, $updateFromConfigFile;
524
525
    // Look if we already have the queried parameter.
526
    if (is_array($configFile) && isset($configFile[$param])) {
527
        return $configFile[$param];
528
    }
529
    if (empty($updatePath) && !empty($_POST['updatePath'])) {
530
        $updatePath = $_POST['updatePath'];
531
    }
532
533
    if (empty($updatePath)) {
534
        $updatePath = api_get_path(SYS_PATH);
535
    }
536
    $updatePath = api_add_trailing_slash(str_replace('\\', '/', realpath($updatePath)));
537
    $updateFromInstalledVersionFile = '';
538
539
    if (empty($updateFromConfigFile)) {
540
        // If update from previous install was requested,
541
        // try to recover config file from Chamilo 1.9.x
542
        if (file_exists($updatePath.'main/inc/conf/configuration.php')) {
543
            $updateFromConfigFile = 'main/inc/conf/configuration.php';
544
        } elseif (file_exists($updatePath.'app/config/configuration.php')) {
545
            $updateFromConfigFile = 'app/config/configuration.php';
546
        } elseif (file_exists($updatePath.'config/configuration.php')) {
547
            $updateFromConfigFile = 'config/configuration.php';
548
        } else {
549
            // Give up recovering.
550
            //error_log('Chamilo Notice: Could not find previous config file at '.$updatePath.'main/inc/conf/configuration.php nor at '.$updatePath.'claroline/inc/conf/claro_main.conf.php in get_config_param(). Will start new config (in '.__FILE__.', line '.__LINE__.')', 0);
551
            return null;
552
        }
553
    }
554
555
    if (file_exists($updatePath.$updateFromConfigFile) &&
556
        !is_dir($updatePath.$updateFromConfigFile)
557
    ) {
558
        require $updatePath.$updateFromConfigFile;
559
        $config = new Zend\Config\Config($_configuration);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $_configuration does not exist. Did you maybe mean $config?
Loading history...
560
561
        return $config->get($param);
562
    }
563
564
    error_log('Config array could not be found in get_config_param()', 0);
565
566
    return null;
567
}
568
569
/*      DATABASE RELATED FUNCTIONS */
570
571
/**
572
 * Gets a configuration parameter from the database. Returns returns null on failure.
573
 *
574
 * @param string $param Name of param we want
575
 *
576
 * @return mixed The parameter value or null if not found
577
 */
578
function get_config_param_from_db($param = '')
579
{
580
    if (($res = Database::query("SELECT * FROM settings_current WHERE variable = '$param'")) !== false) {
581
        if (Database::num_rows($res) > 0) {
582
            $row = Database::fetch_array($res);
583
584
            return $row['selected_value'];
585
        }
586
    }
587
588
    return null;
589
}
590
591
/**
592
 * Connect to the database and returns the entity manager.
593
 *
594
 * @param string $dbHostForm     DB host
595
 * @param string $dbUsernameForm DB username
596
 * @param string $dbPassForm     DB password
597
 * @param string $dbNameForm     DB name
598
 * @param int    $dbPortForm     DB port
599
 *
600
 * @return \Database
601
 */
602
function connectToDatabase(
603
    $dbHostForm,
604
    $dbUsernameForm,
605
    $dbPassForm,
606
    $dbNameForm,
607
    $dbPortForm = 3306
608
) {
609
    $dbParams = [
610
        'driver' => 'pdo_mysql',
611
        'host' => $dbHostForm,
612
        'port' => $dbPortForm,
613
        'user' => $dbUsernameForm,
614
        'password' => $dbPassForm,
615
        'dbname' => $dbNameForm,
616
    ];
617
618
    $database = new \Database();
619
    $database->connect($dbParams);
620
621
    return $database;
622
}
623
624
/*      DISPLAY FUNCTIONS */
625
626
/**
627
 * This function prints class=active_step $current_step=$param.
628
 *
629
 * @param int $param A step in the installer process
630
 *
631
 * @author Patrick Cool <[email protected]>, Ghent University
632
 */
633
function step_active($param)
634
{
635
    global $current_step;
636
    if ($param == $current_step) {
637
        echo 'class="current-step" ';
638
    }
639
}
640
641
/**
642
 * This function displays the Step X of Y -.
643
 *
644
 * @return string String that says 'Step X of Y' with the right values
645
 */
646
function display_step_sequence()
647
{
648
    global $current_step;
649
650
    return get_lang('Step'.$current_step).' &ndash; ';
651
}
652
653
/**
654
 * Displays a drop down box for selection the preferred language.
655
 */
656
function display_language_selection_box(
657
    $name = 'language_list',
658
    $default_language = 'english'
659
) {
660
    // Reading language list.
661
    $language_list = get_language_folder_list();
662
663
    // Sanity checks due to the possibility for customizations.
664
    if (!is_array($language_list) || empty($language_list)) {
665
        $language_list = ['en' => 'English'];
666
    }
667
668
    // Sorting again, if it is necessary.
669
    //asort($language_list);
670
671
    // More sanity checks.
672
    if (!array_key_exists($default_language, $language_list)) {
673
        if (array_key_exists('en', $language_list)) {
674
            $default_language = 'en';
675
        } else {
676
            $language_keys = array_keys($language_list);
677
            $default_language = $language_keys[0];
678
        }
679
    }
680
681
    // Displaying the box.
682
    $html = '';
683
    $html .= "\t\t<select class='selectpicker show-tick' name=\"$name\">\n";
684
    foreach ($language_list as $key => $value) {
685
        if ($key == $default_language) {
686
            $option_end = ' selected="selected">';
687
        } else {
688
            $option_end = '>';
689
        }
690
        $html .= "\t\t\t<option value=\"$key\"$option_end";
691
        $html .= $value;
692
        $html .= "</option>\n";
693
    }
694
    $html .= "\t\t</select>\n";
695
696
    return $html;
697
}
698
699
/**
700
 * This function displays a language dropdown box so that the installatioin
701
 * can be done in the language of the user.
702
 */
703
function display_language_selection()
704
{
705
    ?>
706
    <h2><?php get_lang('WelcomeToTheChamiloInstaller'); ?></h2>
707
    <div class="RequirementHeading">
708
        <h2><?php echo display_step_sequence(); ?>
709
            <?php echo get_lang('InstallationLanguage'); ?>
710
        </h2>
711
        <p><?php echo get_lang('PleaseSelectInstallationProcessLanguage'); ?>:</p>
712
        <form id="lang_form" method="post" action="<?php echo api_get_self(); ?>">
713
        <div class="btn-group">
714
            <?php echo display_language_selection_box('language_list', api_get_interface_language()); ?>
715
            <button type="submit" name="step1" class="btn btn-success" value="<?php echo get_lang('Next'); ?>">
716
                <em class="fa fa-forward"> </em>
717
                <?php echo get_lang('Next'); ?>
718
            </button>
719
        </div>
720
        <input type="hidden" name="is_executable" id="is_executable" value="-" />
721
        </form>
722
    </div>
723
    <div class="RequirementHeading">
724
        <?php echo get_lang('YourLanguageNotThereContactUs'); ?>
725
    </div>
726
<?php
727
}
728
729
/**
730
 * This function displays the requirements for installing Chamilo.
731
 *
732
 * @param string $installType
733
 * @param bool   $badUpdatePath
734
 * @param bool   $badUpdatePath
735
 * @param string $updatePath            The updatePath given (if given)
736
 * @param array  $update_from_version_8 The different subversions from version 1.9
737
 *
738
 * @author unknow
739
 * @author Patrick Cool <[email protected]>, Ghent University
740
 */
741
function display_requirements(
742
    $installType,
743
    $badUpdatePath,
744
    $updatePath = '',
745
    $update_from_version_8 = []
746
) {
747
    global $_setting, $originalMemoryLimit;
748
    echo '<div class="RequirementHeading"><h2>'.display_step_sequence().get_lang('Requirements')."</h2></div>";
749
    echo '<div class="RequirementText">';
750
    echo '<strong>'.get_lang('ReadThoroughly').'</strong><br />';
751
    echo get_lang('MoreDetails').' <a href="../../documentation/installation_guide.html" target="_blank">'.get_lang('ReadTheInstallationGuide').'</a>.<br />'."\n";
752
753
    if ($installType == 'update') {
754
        echo get_lang('IfYouPlanToUpgradeFromOlderVersionYouMightWantToHaveAlookAtTheChangelog').'<br />';
755
    }
756
    echo '</div>';
757
758
    $properlyAccessUrl = checkAccessUrl();
759
    if (!$properlyAccessUrl) {
760
        echo '
761
            <div class="alert alert-danger">
762
            <i class="fa fa-exclamation-triangle" aria-hidden="true"></i>&nbsp;'.
763
            sprintf(get_lang('InstallMultiURLDetectedNotMainURL'), api_get_configuration_value('root_web')).'
764
            </div>
765
        ';
766
    }
767
768
    //  SERVER REQUIREMENTS
769
    echo '<div class="RequirementHeading"><h4>'.get_lang('ServerRequirements').'</h4>';
770
    $timezone = checkPhpSettingExists('date.timezone');
771
    if (!$timezone) {
772
        echo "<div class='alert alert-warning'>
773
            <i class=\"fa fa-exclamation-triangle\" aria-hidden=\"true\"></i>&nbsp;".
774
            get_lang('DateTimezoneSettingNotSet')."</div>";
775
    }
776
777
    echo '<div class="RequirementText">'.get_lang('ServerRequirementsInfo').'</div>';
778
    echo '<div class="RequirementContent">';
779
    echo '<table class="table">
780
            <tr>
781
                <td class="requirements-item">'.get_lang('PHPVersion').' >= '.REQUIRED_PHP_VERSION.'</td>
782
                <td class="requirements-value">';
783
    if (version_compare(phpversion(), REQUIRED_PHP_VERSION, '>=') > 1) {
784
        echo '<strong><font color="red">'.get_lang('PHPVersionError').'</font></strong>';
785
    } else {
786
        echo '<strong><font color="green">'.get_lang('PHPVersionOK').' '.phpversion().'</font></strong>';
787
    }
788
    echo '</td>
789
            </tr>
790
            <tr>
791
                <td class="requirements-item"><a href="http://php.net/manual/en/book.session.php" target="_blank">Session</a> '.get_lang('Support').'</td>
792
                <td class="requirements-value">'.checkExtension('session', get_lang('Yes'), get_lang('ExtensionSessionsNotAvailable')).'</td>
793
            </tr>
794
            <tr>
795
                <td class="requirements-item"><a href="http://php.net/manual/en/book.mysql.php" target="_blank">pdo_mysql</a> '.get_lang('Support').'</td>
796
                <td class="requirements-value">'.checkExtension('pdo_mysql', get_lang('Yes'), get_lang('ExtensionMySQLNotAvailable')).'</td>
797
            </tr>
798
            <tr>
799
                <td class="requirements-item"><a href="http://php.net/manual/en/book.zip.php" target="_blank">Zip</a> '.get_lang('Support').'</td>
800
                <td class="requirements-value">'.checkExtension('zip', get_lang('Yes'), get_lang('ExtensionNotAvailable')).'</td>
801
            </tr>
802
            <tr>
803
                <td class="requirements-item"><a href="http://php.net/manual/en/book.zlib.php" target="_blank">Zlib</a> '.get_lang('Support').'</td>
804
                <td class="requirements-value">'.checkExtension('zlib', get_lang('Yes'), get_lang('ExtensionZlibNotAvailable')).'</td>
805
            </tr>
806
            <tr>
807
                <td class="requirements-item"><a href="http://php.net/manual/en/book.pcre.php" target="_blank">Perl-compatible regular expressions</a> '.get_lang('Support').'</td>
808
                <td class="requirements-value">'.checkExtension('pcre', get_lang('Yes'), get_lang('ExtensionPCRENotAvailable')).'</td>
809
            </tr>
810
            <tr>
811
                <td class="requirements-item"><a href="http://php.net/manual/en/book.xml.php" target="_blank">XML</a> '.get_lang('Support').'</td>
812
                <td class="requirements-value">'.checkExtension('xml', get_lang('Yes'), get_lang('No')).'</td>
813
            </tr>
814
            <tr>
815
                <td class="requirements-item"><a href="http://php.net/manual/en/book.intl.php" target="_blank">Internationalization</a> '.get_lang('Support').'</td>
816
                <td class="requirements-value">'.checkExtension('intl', get_lang('Yes'), get_lang('No')).'</td>
817
            </tr>
818
               <tr>
819
                <td class="requirements-item"><a href="http://php.net/manual/en/book.json.php" target="_blank">JSON</a> '.get_lang('Support').'</td>
820
                <td class="requirements-value">'.checkExtension('json', get_lang('Yes'), get_lang('No')).'</td>
821
            </tr>
822
             <tr>
823
                <td class="requirements-item"><a href="http://php.net/manual/en/book.image.php" target="_blank">GD</a> '.get_lang('Support').'</td>
824
                <td class="requirements-value">'.checkExtension('gd', get_lang('Yes'), get_lang('ExtensionGDNotAvailable')).'</td>
825
            </tr>
826
            <tr>
827
                <td class="requirements-item"><a href="http://php.net/manual/en/book.curl.php" target="_blank">cURL</a>'.get_lang('Support').'</td>
828
                <td class="requirements-value">'.checkExtension('curl', get_lang('Yes'), get_lang('No')).'</td>
829
            </tr>
830
831
            <tr>
832
                <td class="requirements-item"><a href="http://php.net/manual/en/book.mbstring.php" target="_blank">Multibyte string</a> '.get_lang('Support').' ('.get_lang('Optional').')</td>
833
                <td class="requirements-value">'.checkExtension('mbstring', get_lang('Yes'), get_lang('ExtensionMBStringNotAvailable'), true).'</td>
834
            </tr>
835
            <tr>
836
                <td class="requirements-item"><a href="http://php.net/opcache" target="_blank">Zend OpCache</a> '.get_lang('Support').' ('.get_lang('Optional').')</td>
837
                <td class="requirements-value">'.checkExtension('Zend OPcache', get_lang('Yes'), get_lang('No'), true, 'opcache.enable').'</td>
838
            </tr>
839
            <tr>
840
                <td class="requirements-item"><a href="http://php.net/apcu" target="_blank">APCu</a> '.get_lang('Support').' ('.get_lang('Optional').')</td>
841
                <td class="requirements-value">'.checkExtension('apcu', get_lang('Yes'), get_lang('No'), true, 'apc.enabled').'</td>
842
            </tr>
843
            <tr>
844
                <td class="requirements-item"><a href="http://php.net/manual/en/book.iconv.php" target="_blank">Iconv</a> '.get_lang('Support').' ('.get_lang('Optional').')</td>
845
                <td class="requirements-value">'.checkExtension('iconv', get_lang('Yes'), get_lang('No'), true).'</td>
846
            </tr>
847
            <tr>
848
                <td class="requirements-item"><a href="http://php.net/manual/en/book.ldap.php" target="_blank">LDAP</a> '.get_lang('Support').' ('.get_lang('Optional').')</td>
849
                <td class="requirements-value">'.checkExtension('ldap', get_lang('Yes'), get_lang('ExtensionLDAPNotAvailable'), true).'</td>
850
            </tr>
851
            <tr>
852
                <td class="requirements-item"><a href="http://xapian.org/" target="_blank">Xapian</a> '.get_lang('Support').' ('.get_lang('Optional').')</td>
853
                <td class="requirements-value">'.checkExtension('xapian', get_lang('Yes'), get_lang('No'), true).'</td>
854
            </tr>
855
        </table>';
856
    echo '</div>';
857
    echo '</div>';
858
859
    // RECOMMENDED SETTINGS
860
    // Note: these are the settings for Joomla, does this also apply for Chamilo?
861
    // Note: also add upload_max_filesize here so that large uploads are possible
862
    echo '<div class="RequirementHeading"><h4>'.get_lang('RecommendedSettings').'</h4>';
863
    echo '<div class="RequirementText">'.get_lang('RecommendedSettingsInfo').'</div>';
864
    echo '<div class="RequirementContent">';
865
    echo '<table class="table">
866
            <tr>
867
                <th>'.get_lang('Setting').'</th>
868
                <th>'.get_lang('Recommended').'</th>
869
                <th>'.get_lang('Actual').'</th>
870
            </tr>
871
            
872
            <tr>
873
                <td class="requirements-item"><a href="http://php.net/manual/ref.errorfunc.php#ini.display-errors">Display Errors</a></td>
874
                <td class="requirements-recommended">'.Display::label('OFF', 'success').'</td>
875
                <td class="requirements-value">'.checkPhpSetting('display_errors', 'OFF').'</td>
876
            </tr>
877
            <tr>
878
                <td class="requirements-item"><a href="http://php.net/manual/ini.core.php#ini.file-uploads">File Uploads</a></td>
879
                <td class="requirements-recommended">'.Display::label('ON', 'success').'</td>
880
                <td class="requirements-value">'.checkPhpSetting('file_uploads', 'ON').'</td>
881
            </tr>
882
            <tr>
883
                <td class="requirements-item"><a href="http://php.net/manual/ref.session.php#ini.session.auto-start">Session auto start</a></td>
884
                <td class="requirements-recommended">'.Display::label('OFF', 'success').'</td>
885
                <td class="requirements-value">'.checkPhpSetting('session.auto_start', 'OFF').'</td>
886
            </tr>
887
            <tr>
888
                <td class="requirements-item"><a href="http://php.net/manual/ini.core.php#ini.short-open-tag">Short Open Tag</a></td>
889
                <td class="requirements-recommended">'.Display::label('OFF', 'success').'</td>
890
                <td class="requirements-value">'.checkPhpSetting('short_open_tag', 'OFF').'</td>
891
            </tr>
892
            <tr>
893
                <td class="requirements-item"><a href="http://www.php.net/manual/en/session.configuration.php#ini.session.cookie-httponly">Cookie HTTP Only</a></td>
894
                <td class="requirements-recommended">'.Display::label('ON', 'success').'</td>
895
                <td class="requirements-value">'.checkPhpSetting('session.cookie_httponly', 'ON').'</td>
896
            </tr>
897
            <tr>
898
                <td class="requirements-item"><a href="http://php.net/manual/ini.core.php#ini.upload-max-filesize">Maximum upload file size</a></td>
899
                <td class="requirements-recommended">'.Display::label('>= '.REQUIRED_MIN_UPLOAD_MAX_FILESIZE.'M', 'success').'</td>
900
                <td class="requirements-value">'.compare_setting_values(ini_get('upload_max_filesize'), REQUIRED_MIN_UPLOAD_MAX_FILESIZE).'</td>
901
            </tr>
902
            <tr>
903
                <td class="requirements-item"><a href="http://php.net/manual/ini.core.php#ini.post-max-size">Maximum post size</a></td>
904
                <td class="requirements-recommended">'.Display::label('>= '.REQUIRED_MIN_POST_MAX_SIZE.'M', 'success').'</td>
905
                <td class="requirements-value">'.compare_setting_values(ini_get('post_max_size'), REQUIRED_MIN_POST_MAX_SIZE).'</td>
906
            </tr>
907
            <tr>
908
                <td class="requirements-item"><a href="http://www.php.net/manual/en/ini.core.php#ini.memory-limit">Memory Limit</a></td>
909
                <td class="requirements-recommended">'.Display::label('>= '.REQUIRED_MIN_MEMORY_LIMIT.'M', 'success').'</td>
910
                <td class="requirements-value">'.compare_setting_values($originalMemoryLimit, REQUIRED_MIN_MEMORY_LIMIT).'</td>
911
            </tr>
912
          </table>';
913
    echo '  </div>';
914
    echo '</div>';
915
916
    // DIRECTORY AND FILE PERMISSIONS
917
    echo '<div class="RequirementHeading"><h4>'.get_lang('DirectoryAndFilePermissions').'</h4>';
918
    echo '<div class="RequirementText">'.get_lang('DirectoryAndFilePermissionsInfo').'</div>';
919
    echo '<div class="RequirementContent">';
920
921
    $course_attempt_name = '__XxTestxX__';
922
    $course_dir = api_get_path(SYS_COURSE_PATH).$course_attempt_name;
923
    $fileToCreate = 'test.html';
924
    //Just in case
925
    @unlink($course_dir.'/'.$fileToCreate);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for unlink(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

925
    /** @scrutinizer ignore-unhandled */ @unlink($course_dir.'/'.$fileToCreate);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
926
    @rmdir($course_dir);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for rmdir(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

926
    /** @scrutinizer ignore-unhandled */ @rmdir($course_dir);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
927
928
    $perms_dir = [0777, 0755, 0775, 0770, 0750, 0700];
929
    $perms_fil = [0666, 0644, 0664, 0660, 0640, 0600];
930
    $course_test_was_created = false;
931
    $dir_perm_verified = 0777;
932
    foreach ($perms_dir as $perm) {
933
        $r = @mkdir($course_dir, $perm);
934
        if ($r === true) {
935
            $dir_perm_verified = $perm;
936
            $course_test_was_created = true;
937
            break;
938
        }
939
    }
940
941
    $fil_perm_verified = 0666;
942
    $file_course_test_was_created = false;
943
    if (is_dir($course_dir)) {
944
        foreach ($perms_fil as $perm) {
945
            if ($file_course_test_was_created == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
946
                break;
947
            }
948
            $r = @touch($course_dir.'/'.$fileToCreate, $perm);
949
            if ($r === true) {
950
                $fil_perm_verified = $perm;
951
                if (checkCourseScriptCreation($course_dir, $course_attempt_name, $fileToCreate)) {
952
                    $file_course_test_was_created = true;
953
                }
954
            }
955
        }
956
    }
957
958
    @unlink($course_dir.'/'.$fileToCreate);
959
    @rmdir($course_dir);
960
961
    $_SESSION['permissions_for_new_directories'] = $_setting['permissions_for_new_directories'] = $dir_perm_verified;
962
    $_SESSION['permissions_for_new_files'] = $_setting['permissions_for_new_files'] = $fil_perm_verified;
963
964
    $dir_perm = Display::label('0'.decoct($dir_perm_verified), 'info');
965
    $file_perm = Display::label('0'.decoct($fil_perm_verified), 'info');
966
967
    $courseTestLabel = Display::label(get_lang('No'), 'important');
968
    if ($course_test_was_created && $file_course_test_was_created) {
969
        $courseTestLabel = Display::label(get_lang('Yes'), 'success');
970
    }
971
972
    if ($course_test_was_created && !$file_course_test_was_created) {
973
        $courseTestLabel = Display::label(get_lang('Warning'), 'warning');
974
        $courseTestLabel .= '<br />'.sprintf(
975
            get_lang('InstallWarningCouldNotInterpretPHP'),
976
            api_get_path(WEB_COURSE_PATH).$course_attempt_name.'/'.$fileToCreate
977
        );
978
    }
979
980
    if (!$course_test_was_created && !$file_course_test_was_created) {
981
        $courseTestLabel = Display::label(get_lang('No'), 'important');
982
    }
983
984
    $oldConf = '';
985
    if (file_exists(api_get_path(SYS_CODE_PATH).'inc/conf/configuration.php')) {
986
        $oldConf = '<tr>
987
            <td class="requirements-item">'.api_get_path(SYS_CODE_PATH).'inc/conf</td>
988
            <td class="requirements-value">'.check_writable(api_get_path(SYS_CODE_PATH).'inc/conf').'</td>
989
        </tr>';
990
    }
991
992
    echo '<table class="table">
993
            '.$oldConf.'
994
            <tr>
995
                <td class="requirements-item">'.api_get_path(SYS_APP_PATH).'</td>
996
                <td class="requirements-value">'.check_writable(api_get_path(SYS_APP_PATH)).'</td>
997
            </tr>
998
            <tr>
999
                <td class="requirements-item">'.api_get_path(SYS_PATH).'config</td>
1000
                <td class="requirements-value">'.check_writable(api_get_path(SYS_PATH).'config').'</td>
1001
            </tr>
1002
            <tr>
1003
                <td class="requirements-item">'.api_get_path(SYS_PATH).'vendor/</td>
1004
                <td class="requirements-value">'.checkReadable(api_get_path(SYS_PATH).'vendor').'</td>
1005
            </tr>
1006
            <tr>
1007
                <td class="requirements-item">'.api_get_path(SYS_PUBLIC_PATH).'</td>
1008
                <td class="requirements-value">'.check_writable(api_get_path(SYS_PUBLIC_PATH)).'</td>
1009
            </tr>
1010
            <tr>
1011
                <td class="requirements-item">'.get_lang('CourseTestWasCreated').'</td>
1012
                <td class="requirements-value">'.$courseTestLabel.' </td>
1013
            </tr>
1014
            <tr>
1015
                <td class="requirements-item">'.get_lang('PermissionsForNewDirs').'</td>
1016
                <td class="requirements-value">'.$dir_perm.' </td>
1017
            </tr>
1018
            <tr>
1019
                <td class="requirements-item">'.get_lang('PermissionsForNewFiles').'</td>
1020
                <td class="requirements-value">'.$file_perm.' </td>
1021
            </tr>
1022
        </table>';
1023
    echo '  </div>';
1024
    echo '</div>';
1025
1026
    if ($installType == 'update' && (empty($updatePath) || $badUpdatePath)) {
1027
        if ($badUpdatePath) {
1028
            ?>
1029
            <div class="alert alert-warning">
1030
                <?php echo get_lang('Error'); ?>!<br />
1031
                Chamilo <?php echo implode('|', $update_from_version_8).' '.get_lang('HasNotBeenFoundInThatDir'); ?>.
1032
            </div>
1033
        <?php
1034
        } else {
1035
            echo '<br />';
1036
        } ?>
1037
            <div class="row">
1038
                <div class="col-md-12">
1039
                    <p><?php echo get_lang('OldVersionRootPath'); ?>:
1040
                        <input type="text" name="updatePath" size="50" value="<?php echo ($badUpdatePath && !empty($updatePath)) ? htmlentities($updatePath) : ''; ?>" />
1041
                    </p>
1042
                    <p>
1043
                        <div class="btn-group">
1044
                            <button type="submit" class="btn btn-secondary" name="step1" value="<?php echo get_lang('Back'); ?>" >
1045
                                <em class="fa fa-backward"> <?php echo get_lang('Back'); ?></em>
1046
                            </button>
1047
                            <input type="hidden" name="is_executable" id="is_executable" value="-" />
1048
                            <button type="submit" class="btn btn-success" name="<?php echo isset($_POST['step2_update_6']) ? 'step2_update_6' : 'step2_update_8'; ?>" value="<?php echo get_lang('Next'); ?> &gt;" >
1049
                                <em class="fa fa-forward"> </em> <?php echo get_lang('Next'); ?>
1050
                            </button>
1051
                        </div>
1052
                    </p>
1053
                </div>
1054
            </div>
1055
1056
        <?php
1057
    } else {
1058
        $error = false;
1059
        // First, attempt to set writing permissions if we don't have them yet
1060
        $perm = api_get_permissions_for_new_directories();
1061
        $perm_file = api_get_permissions_for_new_files();
1062
        $notWritable = [];
1063
1064
        $checked_writable = api_get_path(SYS_APP_PATH);
1065
        if (!is_writable($checked_writable)) {
1066
            $notWritable[] = $checked_writable;
1067
            @chmod($checked_writable, $perm);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for chmod(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

1067
            /** @scrutinizer ignore-unhandled */ @chmod($checked_writable, $perm);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
1068
        }
1069
1070
        $checked_writable = api_get_path(SYS_PUBLIC_PATH);
1071
        if (!is_writable($checked_writable)) {
1072
            $notWritable[] = $checked_writable;
1073
            @chmod($checked_writable, $perm);
1074
        }
1075
1076
        $checked_writable = api_get_path(SYS_CODE_PATH).'default_course_document/images/';
1077
        if (!is_writable($checked_writable)) {
1078
            $notWritable[] = $checked_writable;
1079
            @chmod($checked_writable, $perm);
1080
        }
1081
1082
        if ($course_test_was_created == false) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
1083
            $error = true;
1084
        }
1085
1086
        $checked_writable = api_get_path(CONFIGURATION_PATH).'configuration.php';
1087
        if (file_exists($checked_writable) && !is_writable($checked_writable)) {
1088
            $notWritable[] = $checked_writable;
1089
            @chmod($checked_writable, $perm_file);
1090
        }
1091
1092
        // Second, if this fails, report an error
1093
1094
        //--> The user would have to adjust the permissions manually
1095
        if (count($notWritable) > 0) {
1096
            $error = true; ?>
1097
            <div class="text-danger">
1098
                <h3 class="text-center"><?php echo get_lang('Warning'); ?></h3>
1099
                <p>
1100
                    <?php printf(get_lang('NoWritePermissionPleaseReadInstallGuide'), '<a href="../../documentation/installation_guide.html" target="blank">', '</a>'); ?>
1101
                </p>
1102
            </div>
1103
            <?php
1104
            echo '<ul>';
1105
            foreach ($notWritable as $value) {
1106
                echo '<li class="text-danger">'.$value.'</li>';
1107
            }
1108
            echo '</ul>';
1109
        } elseif (file_exists(api_get_path(CONFIGURATION_PATH).'configuration.php')) {
1110
            // Check wether a Chamilo configuration file already exists.
1111
            echo '<div class="alert alert-warning"><h4><center>';
1112
            echo get_lang('WarningExistingLMSInstallationDetected');
1113
            echo '</center></h4></div>';
1114
        }
1115
1116
        $deprecated = [
1117
            api_get_path(SYS_CODE_PATH).'exercice/',
1118
            api_get_path(SYS_CODE_PATH).'newscorm/',
1119
            api_get_path(SYS_PLUGIN_PATH).'ticket/',
1120
            api_get_path(SYS_PLUGIN_PATH).'skype/',
1121
        ];
1122
        $deprecatedToRemove = [];
1123
        foreach ($deprecated as $deprecatedDirectory) {
1124
            if (!is_dir($deprecatedDirectory)) {
1125
                continue;
1126
            }
1127
            $deprecatedToRemove[] = $deprecatedDirectory;
1128
        }
1129
1130
        if (count($deprecatedToRemove) > 0) {
1131
            ?>
1132
            <p class="text-danger"><?php echo get_lang('WarningForDeprecatedDirectoriesForUpgrade'); ?></p>
1133
            <ul>
1134
                <?php foreach ($deprecatedToRemove as $deprecatedDirectory) {
1135
                ?>
1136
                    <li class="text-danger"><?php echo $deprecatedDirectory; ?></li>
1137
                <?php
1138
            } ?>
1139
            </ul>
1140
            <?php
1141
        }
1142
1143
        if (!$properlyAccessUrl) {
1144
            $error = true;
1145
        }
1146
1147
        // And now display the choice buttons (go back or install)?>
1148
        <p align="center" style="padding-top:15px">
1149
            <button type="submit" name="step1" class="btn btn-default" onclick="javascript: window.location='index.php'; return false;" value="<?php echo get_lang('Previous'); ?>" >
1150
                <em class="fa fa-backward"> </em> <?php echo get_lang('Previous'); ?>
1151
            </button>
1152
            <button type="submit" name="step2_install" class="btn btn-success" value="<?php echo get_lang("NewInstallation"); ?>" <?php if ($error) {
1153
            echo 'disabled="disabled"';
1154
        } ?> >
1155
                <em class="fa fa-forward"> </em> <?php echo get_lang('NewInstallation'); ?>
1156
            </button>
1157
        <input type="hidden" name="is_executable" id="is_executable" value="-" />
1158
            <button type="submit" class="btn btn-default" <?php echo !$error ?: 'disabled="disabled"'; ?> name="step2_update_8" value="Upgrade from Chamilo 1.9.x">
1159
                <em class="fa fa-forward" aria-hidden="true"></em> <?php echo get_lang('UpgradeVersion'); ?>
1160
            </button>
1161
            </p>
1162
        <?php
1163
    }
1164
}
1165
1166
/**
1167
 * Displays the license (GNU GPL) as step 2, with
1168
 * - an "I accept" button named step3 to proceed to step 3;
1169
 * - a "Back" button named step1 to go back to the first step.
1170
 */
1171
function display_license_agreement()
1172
{
1173
    echo '<div class="RequirementHeading"><h2>'.display_step_sequence().get_lang('Licence').'</h2>';
1174
    echo '<p>'.get_lang('LMSLicenseInfo').'</p>';
1175
    echo '<p><a href="../../documentation/license.html" target="_blank">'.get_lang('PrintVers').'</a></p>';
1176
    echo '</div>'; ?>
1177
    <div class="row">
1178
        <div class="col-md-12">
1179
            <pre style="overflow: auto; height: 200px; margin-top: 5px;">
1180
                <?php echo api_htmlentities(@file_get_contents(api_get_path(SYS_PATH).'documentation/license.txt')); ?>
1181
            </pre>
1182
            <div class="checkbox">
1183
                <label>
1184
                    <input type="checkbox" name="accept" id="accept_licence" value="1" />
1185
                    <?php echo get_lang('IAccept'); ?>
1186
                </label>
1187
            </div>
1188
        </div>
1189
    </div>
1190
    <div class="row">
1191
        <div class="col-md-12">
1192
            <p class="alert alert-info"><?php echo get_lang('LMSMediaLicense'); ?></p>
1193
        </div>
1194
    </div>
1195
1196
    <!-- Contact information form -->
1197
    <div class="section-parameters">
1198
        <a href="javascript://" class = "advanced_parameters" >
1199
        <span id="img_plus_and_minus">&nbsp;<i class="fa fa-eye" aria-hidden="true"></i>&nbsp;<?php echo get_lang('ContactInformation'); ?></span>
1200
        </a>
1201
    </div>
1202
1203
    <div id="id_contact_form" style="display:block">
1204
        <div class="normal-message"><?php echo get_lang('ContactInformationDescription'); ?></div>
1205
        <div id="contact_registration">
1206
            <p><?php echo get_contact_registration_form(); ?></p><br />
1207
        </div>
1208
    </div>
1209
    <div class="text-center">
1210
    <button type="submit" class="btn btn-default" name="step1" value="&lt; <?php echo get_lang('Previous'); ?>" >
1211
        <em class="fa fa-backward"> </em> <?php echo get_lang('Previous'); ?>
1212
    </button>
1213
    <input type="hidden" name="is_executable" id="is_executable" value="-" />
1214
    <button type="submit" id="license-next" class="btn btn-success" name="step3" onclick="javascript: if(!document.getElementById('accept_licence').checked) { alert('<?php echo get_lang('YouMustAcceptLicence'); ?>');return false;}" value="<?php echo get_lang('Next'); ?> &gt;" >
1215
        <em class="fa fa-forward"> </em> <?php echo get_lang('Next'); ?>
1216
    </button>
1217
    </div>
1218
    <?php
1219
}
1220
1221
/**
1222
 * Get contact registration form.
1223
 */
1224
function get_contact_registration_form()
1225
{
1226
    $html = '
1227
   <div class="form-horizontal">
1228
    <div class="panel panel-default">
1229
    <div class="panel-body">
1230
    <div id="div_sent_information"></div>
1231
    <div class="form-group">
1232
            <label class="col-sm-3"><span class="form_required">*</span>'.get_lang('Name').'</label>
1233
            <div class="col-sm-9"><input id="person_name" class="form-control" type="text" name="person_name" size="30" /></div>
1234
    </div>
1235
    <div class="form-group">
1236
            <label class="col-sm-3"><span class="form_required">*</span>'.get_lang('Email').'</label>
1237
            <div class="col-sm-9"><input id="person_email" class="form-control" type="text" name="person_email" size="30" /></div>
1238
    </div>
1239
    <div class="form-group">
1240
            <label class="col-sm-3"><span class="form_required">*</span>'.get_lang('CompanyName').'</label>
1241
            <div class="col-sm-9"><input id="company_name" class="form-control" type="text" name="company_name" size="30" /></div>
1242
    </div>
1243
    <div class="form-group">
1244
        <label class="col-sm-3"><span class="form_required">*</span>'.get_lang('CompanyActivity').'</label>
1245
        <div class="col-sm-9">
1246
            <select class="selectpicker show-tick" name="company_activity" id="company_activity" >
1247
                <option value="">--- '.get_lang('SelectOne').' ---</option>
1248
                <Option value="Advertising/Marketing/PR">Advertising/Marketing/PR</Option><Option value="Agriculture/Forestry">Agriculture/Forestry</Option>
1249
                <Option value="Architecture">Architecture</Option><Option value="Banking/Finance">Banking/Finance</Option>
1250
                <Option value="Biotech/Pharmaceuticals">Biotech/Pharmaceuticals</Option><Option value="Business Equipment">Business Equipment</Option>
1251
                <Option value="Business Services">Business Services</Option><Option value="Construction">Construction</Option>
1252
                <Option value="Consulting/Research">Consulting/Research</Option><Option value="Education">Education</Option>
1253
                <Option value="Engineering">Engineering</Option><Option value="Environmental">Environmental</Option>
1254
                <Option value="Government">Government</Option><Option value="Healthcare">Health Care</Option>
1255
                <Option value="Hospitality/Lodging/Travel">Hospitality/Lodging/Travel</Option><Option value="Insurance">Insurance</Option>
1256
                <Option value="Legal">Legal</Option><Option value="Manufacturing">Manufacturing</Option>
1257
                <Option value="Media/Entertainment">Media/Entertainment</Option><Option value="Mortgage">Mortgage</Option>
1258
                <Option value="Non-Profit">Non-Profit</Option><Option value="Real Estate">Real Estate</Option>
1259
                <Option value="Restaurant">Restaurant</Option><Option value="Retail">Retail</Option>
1260
                <Option value="Shipping/Transportation">Shipping/Transportation</Option>
1261
                <Option value="Technology">Technology</Option><Option value="Telecommunications">Telecommunications</Option>
1262
                <Option value="Other">Other</Option>
1263
            </select>
1264
        </div>
1265
    </div>
1266
1267
    <div class="form-group">
1268
        <label class="col-sm-3"><span class="form_required">*</span>'.get_lang('PersonRole').'</label>
1269
        <div class="col-sm-9">
1270
            <select class="selectpicker show-tick" name="person_role" id="person_role" >
1271
                <option value="">--- '.get_lang('SelectOne').' ---</option>
1272
                <Option value="Administration">Administration</Option><Option value="CEO/President/ Owner">CEO/President/ Owner</Option>
1273
                <Option value="CFO">CFO</Option><Option value="CIO/CTO">CIO/CTO</Option>
1274
                <Option value="Consultant">Consultant</Option><Option value="Customer Service">Customer Service</Option>
1275
                <Option value="Engineer/Programmer">Engineer/Programmer</Option><Option value="Facilities/Operations">Facilities/Operations</Option>
1276
                <Option value="Finance/ Accounting Manager">Finance/ Accounting Manager</Option><Option value="Finance/ Accounting Staff">Finance/ Accounting Staff</Option>
1277
                <Option value="General Manager">General Manager</Option><Option value="Human Resources">Human Resources</Option>
1278
                <Option value="IS/IT Management">IS/IT Management</Option><Option value="IS/ IT Staff">IS/ IT Staff</Option>
1279
                <Option value="Marketing Manager">Marketing Manager</Option><Option value="Marketing Staff">Marketing Staff</Option>
1280
                <Option value="Partner/Principal">Partner/Principal</Option><Option value="Purchasing Manager">Purchasing Manager</Option>
1281
                <Option value="Sales/ Business Dev. Manager">Sales/ Business Dev. Manager</Option><Option value="Sales/ Business Dev.">Sales/ Business Dev.</Option>
1282
                <Option value="Vice President/Senior Manager">Vice President/Senior Manager</Option><Option value="Other">Other</Option>
1283
            </select>
1284
        </div>
1285
    </div>
1286
1287
    <div class="form-group">
1288
        <label class="col-sm-3"><span class="form_required">*</span>'.get_lang('CompanyCountry').'</label>
1289
        <div class="col-sm-9">'.get_countries_list_from_array(true).'</div>
1290
    </div>
1291
    <div class="form-group">
1292
        <label class="col-sm-3">'.get_lang('CompanyCity').'</label>
1293
        <div class="col-sm-9">
1294
                <input type="text" class="form-control" id="company_city" name="company_city" size="30" />
1295
        </div>
1296
    </div>
1297
    <div class="form-group">
1298
        <label class="col-sm-3">'.get_lang('WhichLanguageWouldYouLikeToUseWhenContactingYou').'</label>
1299
        <div class="col-sm-9">
1300
            <select class="selectpicker show-tick" id="language" name="language">
1301
                <option value="bulgarian">Bulgarian</option>
1302
                <option value="indonesian">Bahasa Indonesia</option>
1303
                <option value="bosnian">Bosanski</option>
1304
                <option value="german">Deutsch</option>
1305
                <option selected="selected" value="english">English</option>
1306
                <option value="spanish">Spanish</option>
1307
                <option value="french">Français</option>
1308
                <option value="italian">Italian</option>
1309
                <option value="hungarian">Magyar</option>
1310
                <option value="dutch">Nederlands</option>
1311
                <option value="brazilian">Português do Brasil</option>
1312
                <option value="portuguese">Português europeu</option>
1313
                <option value="slovenian">Slovenčina</option>
1314
            </select>
1315
        </div>
1316
    </div>
1317
1318
    <div class="form-group">
1319
        <label class="col-sm-3">'.get_lang('HaveYouThePowerToTakeFinancialDecisions').'</label>
1320
        <div class="col-sm-9">
1321
            <div class="radio">
1322
                <label>
1323
                    <input type="radio" name="financial_decision" id="financial_decision1" value="1" checked /> '.get_lang('Yes').'
1324
                </label>
1325
            </div>
1326
            <div class="radio">
1327
                <label>
1328
                    <input type="radio" name="financial_decision" id="financial_decision2" value="0" /> '.get_lang('No').'
1329
                </label>
1330
            </div>
1331
        </div>
1332
    </div>
1333
    <div class="clear"></div>
1334
    <div class="form-group">
1335
            <div class="col-sm-3">&nbsp;</div>
1336
            <div class="col-sm-9"><button type="button" class="btn btn-default" onclick="javascript:send_contact_information();" value="'.get_lang('SendInformation').'" ><em class="fa fa-floppy-o"></em> '.get_lang('SendInformation').'</button> <span id="loader-button"></span></div>
1337
    </div>
1338
    <div class="form-group">
1339
            <div class="col-sm-3">&nbsp;</div>
1340
            <div class="col-sm-9"><span class="form_required">*</span><small>'.get_lang('FieldRequired').'</small></div>
1341
    </div></div></div>
1342
    </div>';
1343
1344
    return $html;
1345
}
1346
1347
/**
1348
 * Displays a parameter in a table row.
1349
 * Used by the display_database_settings_form function.
1350
 *
1351
 * @param   string  Type of install
1352
 * @param   string  Name of parameter
1353
 * @param   string  Field name (in the HTML form)
1354
 * @param   string  Field value
1355
 * @param   string  Extra notice (to show on the right side)
1356
 * @param   bool Whether to display in update mode
1357
 * @param   string  Additional attribute for the <tr> element
1358
 */
1359
function displayDatabaseParameter(
1360
    $installType,
1361
    $parameterName,
1362
    $formFieldName,
1363
    $parameterValue,
1364
    $extra_notice,
1365
    $displayWhenUpdate = true,
1366
    $tr_attribute = ''
0 ignored issues
show
Unused Code introduced by
The parameter $tr_attribute is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

1366
    /** @scrutinizer ignore-unused */ $tr_attribute = ''

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
1367
) {
1368
    //echo "<tr ".$tr_attribute.">";
1369
    echo "<label class='col-sm-4'>$parameterName</label>";
1370
1371
    if ($installType == INSTALL_TYPE_UPDATE && $displayWhenUpdate) {
1372
        echo '<input type="hidden" name="'.$formFieldName.'" id="'.$formFieldName.'" value="'.api_htmlentities($parameterValue).'" />'.$parameterValue;
1373
    } else {
1374
        $inputType = $formFieldName == 'dbPassForm' ? 'password' : 'text';
1375
1376
        //Slightly limit the length of the database prefix to avoid having to cut down the databases names later on
1377
        $maxLength = $formFieldName == 'dbPrefixForm' ? '15' : MAX_FORM_FIELD_LENGTH;
1378
        if ($installType == INSTALL_TYPE_UPDATE) {
1379
            echo '<input type="hidden" name="'.$formFieldName.'" id="'.$formFieldName.'" value="'.api_htmlentities($parameterValue).'" />';
1380
            echo api_htmlentities($parameterValue);
1381
        } else {
1382
            echo '<div class="col-sm-5"><input type="'.$inputType.'" class="form-control" size="'.DATABASE_FORM_FIELD_DISPLAY_LENGTH.'" maxlength="'.$maxLength.'" name="'.$formFieldName.'" id="'.$formFieldName.'" value="'.api_htmlentities($parameterValue).'" />'."</div>";
1383
            echo '<div class="col-sm-3">'.$extra_notice.'</div>';
1384
        }
1385
    }
1386
}
1387
1388
/**
1389
 * Displays step 3 - a form where the user can enter the installation settings
1390
 * regarding the databases - login and password, names, prefixes, single
1391
 * or multiple databases, tracking or not...
1392
 *
1393
 * @param string $installType
1394
 * @param string $dbHostForm
1395
 * @param string $dbUsernameForm
1396
 * @param string $dbPassForm
1397
 * @param string $dbNameForm
1398
 * @param int    $dbPortForm
1399
 * @param string $installationProfile
1400
 */
1401
function display_database_settings_form(
1402
    $installType,
1403
    $dbHostForm,
1404
    $dbUsernameForm,
1405
    $dbPassForm,
1406
    $dbNameForm,
1407
    $dbPortForm = 3306,
1408
    $installationProfile = ''
0 ignored issues
show
Unused Code introduced by
The parameter $installationProfile is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

1408
    /** @scrutinizer ignore-unused */ $installationProfile = ''

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
1409
) {
1410
    if ($installType == 'update') {
1411
        global $_configuration;
1412
        $dbHostForm = $_configuration['db_host'];
1413
        $dbUsernameForm = $_configuration['db_user'];
1414
        $dbPassForm = $_configuration['db_password'];
1415
        $dbNameForm = $_configuration['main_database'];
1416
        $dbPortForm = isset($_configuration['db_port']) ? $_configuration['db_port'] : '';
1417
1418
        echo '<div class="RequirementHeading"><h2>'.display_step_sequence().get_lang('DBSetting').'</h2></div>';
1419
        echo '<div class="RequirementContent">';
1420
        echo get_lang('DBSettingUpgradeIntro');
1421
        echo '</div>';
1422
    } else {
1423
        echo '<div class="RequirementHeading"><h2>'.display_step_sequence().get_lang('DBSetting').'</h2></div>';
1424
        echo '<div class="RequirementContent">';
1425
        echo get_lang('DBSettingIntro');
1426
        echo '</div>';
1427
    } ?>
1428
    <div class="panel panel-default">
1429
        <div class="panel-body">
1430
        <div class="form-group">
1431
            <label class="col-sm-4"><?php echo get_lang('DBHost'); ?> </label>
1432
            <?php if ($installType == 'update') {
1433
        ?>
1434
            <div class="col-sm-5">
1435
                <input type="hidden" name="dbHostForm" value="<?php echo htmlentities($dbHostForm); ?>" /><?php echo $dbHostForm; ?>
1436
            </div>
1437
            <div class="col-sm-3"></div>
1438
            <?php
1439
    } else {
1440
        ?>
1441
            <div class="col-sm-5">
1442
                <input type="text" class="form-control" size="25" maxlength="50" name="dbHostForm" value="<?php echo htmlentities($dbHostForm); ?>" />
1443
            </div>
1444
            <div class="col-sm-3"><?php echo get_lang('EG').' localhost'; ?></div>
1445
            <?php
1446
    } ?>
1447
        </div>
1448
        <div class="form-group">
1449
            <label class="col-sm-4"><?php echo get_lang('DBPort'); ?> </label>
1450
            <?php if ($installType == 'update') {
1451
        ?>
1452
            <div class="col-sm-5">
1453
                <input type="hidden" name="dbPortForm" value="<?php echo htmlentities($dbPortForm); ?>" /><?php echo $dbPortForm; ?>
1454
            </div>
1455
            <div class="col-sm-3"></div>
1456
            <?php
1457
    } else {
1458
        ?>
1459
            <div class="col-sm-5">
1460
                <input type="text" class="form-control" size="25" maxlength="50" name="dbPortForm" value="<?php echo htmlentities($dbPortForm); ?>" />
1461
            </div>
1462
            <div class="col-sm-3"><?php echo get_lang('EG').' 3306'; ?></div>
1463
            <?php
1464
    } ?>
1465
        </div>
1466
        <div class="form-group">
1467
            <?php
1468
                //database user username
1469
                $example_login = get_lang('EG').' root';
1470
    displayDatabaseParameter($installType, get_lang('DBLogin'), 'dbUsernameForm', $dbUsernameForm, $example_login); ?>
1471
        </div>
1472
        <div class="form-group">
1473
            <?php
1474
            //database user password
1475
            $example_password = get_lang('EG').' '.api_generate_password();
1476
    displayDatabaseParameter($installType, get_lang('DBPassword'), 'dbPassForm', $dbPassForm, $example_password); ?>
1477
        </div>
1478
        <div class="form-group">
1479
            <?php
1480
            //Database Name fix replace weird chars
1481
            if ($installType != INSTALL_TYPE_UPDATE) {
1482
                $dbNameForm = str_replace(['-', '*', '$', ' ', '.'], '', $dbNameForm);
1483
            }
1484
1485
    displayDatabaseParameter(
1486
                $installType,
1487
                get_lang('MainDB'),
1488
                'dbNameForm',
1489
                $dbNameForm,
1490
                '&nbsp;',
1491
                null,
1492
                'id="optional_param1"'
1493
                ); ?>
1494
        </div>
1495
       <?php if ($installType != INSTALL_TYPE_UPDATE) {
1496
                    ?>
1497
        <div class="form-group">
1498
            <div class="col-sm-3"></div>
1499
            <div class="col-sm-9">
1500
            <button type="submit" class="btn btn-primary" name="step3" value="step3">
1501
                <em class="fa fa-refresh"> </em>
1502
                <?php echo get_lang('CheckDatabaseConnection'); ?>
1503
            </button>
1504
            </div>
1505
        </div>
1506
        <?php
1507
                } ?>
1508
1509
        </div>
1510
    </div>
1511
    <?php
1512
        $database_exists_text = '';
1513
    $manager = null;
1514
    try {
1515
        if ($installType === 'update') {
1516
            /** @var \Database $manager */
1517
            $manager = connectToDatabase(
1518
                $dbHostForm,
1519
                $dbUsernameForm,
1520
                $dbPassForm,
1521
                $dbNameForm,
1522
                $dbPortForm
1523
            );
1524
1525
            $connection = $manager->getConnection();
1526
            $connection->connect();
1527
            $schemaManager = $connection->getSchemaManager();
1528
1529
            // Test create/alter/drop table
1530
            $table = 'zXxTESTxX_'.mt_rand(0, 1000);
1531
            $sql = "CREATE TABLE $table (id INT AUTO_INCREMENT NOT NULL, name varchar(255), PRIMARY KEY(id))";
1532
            $connection->query($sql);
1533
            $tableCreationWorks = false;
1534
            $tableDropWorks = false;
1535
            if ($schemaManager->tablesExist($table)) {
1536
                $tableCreationWorks = true;
1537
                $sql = "ALTER TABLE $table ADD COLUMN name2 varchar(140) ";
1538
                $connection->query($sql);
1539
                $schemaManager->dropTable($table);
1540
                $tableDropWorks = $schemaManager->tablesExist($table) === false;
1541
            }
1542
        } else {
1543
            $manager = connectToDatabase(
1544
                $dbHostForm,
1545
                $dbUsernameForm,
1546
                $dbPassForm,
1547
                null,
1548
                $dbPortForm
1549
            );
1550
1551
            $schemaManager = $manager->getConnection()->getSchemaManager();
1552
            $databases = $schemaManager->listDatabases();
1553
            if (in_array($dbNameForm, $databases)) {
1554
                $database_exists_text = '<div class="alert alert-warning">'.get_lang('ADatabaseWithTheSameNameAlreadyExists').'</div>';
1555
            }
1556
        }
1557
    } catch (Exception $e) {
1558
        $database_exists_text = $e->getMessage();
1559
        $manager = false;
1560
    }
1561
1562
    if ($manager && $manager->getConnection()->isConnected()): ?>
1563
        <?php echo $database_exists_text; ?>
1564
        <div id="db_status" class="alert alert-success">
1565
            Database host: <strong><?php echo $manager->getConnection()->getHost(); ?></strong><br/>
1566
            Database port: <strong><?php echo $manager->getConnection()->getPort(); ?></strong><br/>
1567
            Database driver: <strong><?php echo $manager->getConnection()->getDriver()->getName(); ?></strong><br/>
1568
            <?php
1569
                if ($installType === 'update') {
1570
                    echo get_lang('CreateTableWorks').' <strong>Ok</strong>';
1571
                    echo '<br/ >';
1572
                    echo get_lang('AlterTableWorks').' <strong>Ok</strong>';
1573
                    echo '<br/ >';
1574
                    echo get_lang('DropColumnWorks').' <strong>Ok</strong>';
1575
                } ?>
1576
        </div>
1577
    <?php else: ?>
1578
        <div id="db_status" class="alert alert-danger">
1579
            <p><?php echo get_lang('FailedConectionDatabase'); ?></strong></p>
1580
            <code><?php echo $database_exists_text; ?></code>
1581
        </div>
1582
    <?php endif; ?>
1583
1584
   <div class="btn-group" role="group">
1585
       <button type="submit" name="step2"
1586
               class="btn btn-secondary pull-right" value="&lt; <?php echo get_lang('Previous'); ?>" >
1587
           <em class="fa fa-backward"> </em> <?php echo get_lang('Previous'); ?>
1588
       </button>
1589
       <input type="hidden" name="is_executable" id="is_executable" value="-" />
1590
       <?php if ($manager) {
1591
                    ?>
1592
           <button type="submit" class="btn btn-success" name="step4" value="<?php echo get_lang('Next'); ?> &gt;" >
1593
               <em class="fa fa-forward"> </em> <?php echo get_lang('Next'); ?>
1594
           </button>
1595
       <?php
1596
                } else {
1597
                    ?>
1598
           <button
1599
                   disabled="disabled"
1600
                   type="submit" class="btn btn-success disabled" name="step4" value="<?php echo get_lang('Next'); ?> &gt;" >
1601
               <em class="fa fa-forward"> </em> <?php echo get_lang('Next'); ?>
1602
           </button>
1603
       <?php
1604
                } ?>
1605
   </div>
1606
    <?php
1607
}
1608
1609
function panel($content = null, $title = null, $id = null, $style = null)
1610
{
1611
    $html = '';
1612
    if (empty($style)) {
1613
        $style = 'default';
1614
    }
1615
    if (!empty($title)) {
1616
        $panelTitle = Display::div($title, ['class' => 'panel-heading']);
1617
        $panelBody = Display::div($content, ['class' => 'panel-body']);
1618
        $panelParent = Display::div($panelTitle.$panelBody, ['id' => $id, 'class' => 'panel panel-'.$style]);
1619
    } else {
1620
        $panelBody = Display::div($html, ['class' => 'panel-body']);
1621
        $panelParent = Display::div($panelBody, ['id' => $id, 'class' => 'panel panel-'.$style]);
1622
    }
1623
    $html .= $panelParent;
1624
1625
    return $html;
1626
}
1627
1628
/**
1629
 * Displays a parameter in a table row.
1630
 * Used by the display_configuration_settings_form function.
1631
 *
1632
 * @param string $installType
1633
 * @param string $parameterName
1634
 * @param string $formFieldName
1635
 * @param string $parameterValue
1636
 * @param string $displayWhenUpdate
1637
 *
1638
 * @return string
1639
 */
1640
function display_configuration_parameter(
1641
    $installType,
1642
    $parameterName,
1643
    $formFieldName,
1644
    $parameterValue,
1645
    $displayWhenUpdate = 'true'
1646
) {
1647
    $html = '<div class="form-group">';
1648
    $html .= '<label class="col-sm-6 control-label">'.$parameterName.'</label>';
1649
    if ($installType == INSTALL_TYPE_UPDATE && $displayWhenUpdate) {
1650
        $html .= '<input type="hidden" name="'.$formFieldName.'" value="'.api_htmlentities($parameterValue, ENT_QUOTES).'" />'.$parameterValue;
1651
    } else {
1652
        $html .= '<div class="col-sm-6"><input class="form-control" type="text" size="'.FORM_FIELD_DISPLAY_LENGTH.'" maxlength="'.MAX_FORM_FIELD_LENGTH.'" name="'.$formFieldName.'" value="'.api_htmlentities($parameterValue, ENT_QUOTES).'" />'."</div>";
1653
    }
1654
    $html .= "</div>";
1655
1656
    return $html;
1657
}
1658
1659
/**
1660
 * Displays step 4 of the installation - configuration settings about Chamilo itself.
1661
 *
1662
 * @param string $installType
1663
 * @param string $urlForm
1664
 * @param string $languageForm
1665
 * @param string $emailForm
1666
 * @param string $adminFirstName
1667
 * @param string $adminLastName
1668
 * @param string $adminPhoneForm
1669
 * @param string $campusForm
1670
 * @param string $institutionForm
1671
 * @param string $institutionUrlForm
1672
 * @param string $encryptPassForm
1673
 * @param bool   $allowSelfReg
1674
 * @param bool   $allowSelfRegProf
1675
 * @param string $loginForm
1676
 * @param string $passForm
1677
 */
1678
function display_configuration_settings_form(
1679
    $installType,
1680
    $urlForm,
1681
    $languageForm,
1682
    $emailForm,
1683
    $adminFirstName,
1684
    $adminLastName,
1685
    $adminPhoneForm,
1686
    $campusForm,
1687
    $institutionForm,
1688
    $institutionUrlForm,
1689
    $encryptPassForm,
1690
    $allowSelfReg,
1691
    $allowSelfRegProf,
1692
    $loginForm,
1693
    $passForm
1694
) {
1695
    if ($installType != 'update' && empty($languageForm)) {
1696
        $languageForm = $_SESSION['install_language'];
1697
    }
1698
    echo '<div class="RequirementHeading">';
1699
    echo "<h2>".display_step_sequence().get_lang("CfgSetting")."</h2>";
1700
    echo '</div>';
1701
1702
    echo '<p>'.get_lang('ConfigSettingsInfo').' <strong>app/config/configuration.php</strong></p>';
1703
1704
    // Parameter 1: administrator's login
1705
    $html = '';
1706
    $html .= display_configuration_parameter(
1707
        $installType,
1708
        get_lang('AdminLogin'),
1709
        'loginForm',
1710
        $loginForm,
1711
        $installType == 'update'
1712
    );
1713
1714
    // Parameter 2: administrator's password
1715
    if ($installType != 'update') {
1716
        $html .= display_configuration_parameter($installType, get_lang('AdminPass'), 'passForm', $passForm, false);
1717
    }
1718
1719
    // Parameters 3 and 4: administrator's names
1720
1721
    $html .= display_configuration_parameter(
1722
        $installType,
1723
        get_lang('AdminFirstName'),
1724
        'adminFirstName',
1725
        $adminFirstName
1726
    );
1727
    $html .= display_configuration_parameter($installType, get_lang('AdminLastName'), 'adminLastName', $adminLastName);
1728
1729
    //Parameter 3: administrator's email
1730
    $html .= display_configuration_parameter($installType, get_lang('AdminEmail'), 'emailForm', $emailForm);
1731
1732
    //Parameter 6: administrator's telephone
1733
    $html .= display_configuration_parameter($installType, get_lang('AdminPhone'), 'adminPhoneForm', $adminPhoneForm);
1734
    echo panel($html, get_lang('Administrator'), 'administrator');
1735
1736
    //First parameter: language
1737
    $html = '<div class="form-group">';
1738
    $html .= '<label class="col-sm-6 control-label">'.get_lang('MainLang')."</label>";
1739
    if ($installType == 'update') {
1740
        $html .= '<input type="hidden" name="languageForm" value="'.api_htmlentities($languageForm, ENT_QUOTES).'" />'.$languageForm;
1741
    } else { // new installation
1742
        $html .= '<div class="col-sm-6">';
1743
        $html .= display_language_selection_box('languageForm', $languageForm);
1744
        $html .= '</div>';
1745
    }
1746
    $html .= "</div>";
1747
1748
    //Second parameter: Chamilo URL
1749
    $html .= '<div class="form-group">';
1750
    $html .= '<label class="col-sm-6 control-label">'.get_lang('ChamiloURL').get_lang('ThisFieldIsRequired').'</label>';
1751
1752
    if ($installType == 'update') {
1753
        $html .= api_htmlentities($urlForm, ENT_QUOTES)."\n";
1754
    } else {
1755
        $html .= '<div class="col-sm-6">';
1756
        $html .= '<input class="form-control" type="text" size="40" maxlength="100" name="urlForm" value="'.api_htmlentities($urlForm, ENT_QUOTES).'" />';
1757
        $html .= '</div>';
1758
    }
1759
    $html .= '</div>';
1760
1761
    //Parameter 9: campus name
1762
    $html .= display_configuration_parameter(
1763
        $installType,
1764
        get_lang('CampusName'),
1765
        'campusForm',
1766
        $campusForm
1767
    );
1768
1769
    //Parameter 10: institute (short) name
1770
    $html .= display_configuration_parameter(
1771
        $installType,
1772
        get_lang('InstituteShortName'),
1773
        'institutionForm',
1774
        $institutionForm
1775
    );
1776
1777
    //Parameter 11: institute (short) name
1778
    $html .= display_configuration_parameter(
1779
        $installType,
1780
        get_lang('InstituteURL'),
1781
        'institutionUrlForm',
1782
        $institutionUrlForm
1783
    );
1784
1785
    $html .= '<div class="form-group">
1786
            <label class="col-sm-6 control-label">'.get_lang("EncryptMethodUserPass").'</label>
1787
        <div class="col-sm-6">';
1788
    if ($installType == 'update') {
1789
        $html .= '<input type="hidden" name="encryptPassForm" value="'.$encryptPassForm.'" />'.$encryptPassForm;
1790
    } else {
1791
        $html .= '<div class="checkbox">
1792
                    <label>
1793
                        <input  type="radio" name="encryptPassForm" value="bcrypt" id="encryptPass1" '.($encryptPassForm == 'bcrypt' ? 'checked="checked" ' : '').'/> bcrypt
1794
                    </label>';
1795
1796
        $html .= '<label>
1797
                        <input  type="radio" name="encryptPassForm" value="sha1" id="encryptPass1" '.($encryptPassForm == 'sha1' ? 'checked="checked" ' : '').'/> sha1
1798
                    </label>';
1799
1800
        $html .= '<label>
1801
                        <input type="radio" name="encryptPassForm" value="md5" id="encryptPass0" '.($encryptPassForm == 'md5' ? 'checked="checked" ' : '').'/> md5
1802
                    </label>';
1803
1804
        $html .= '<label>
1805
                        <input type="radio" name="encryptPassForm" value="none" id="encryptPass2" '.($encryptPassForm == 'none' ? 'checked="checked" ' : '').'/>'.get_lang('None').'
1806
                    </label>';
1807
        $html .= '</div>';
1808
    }
1809
    $html .= '</div></div>';
1810
1811
    $html .= '<div class="form-group">
1812
            <label class="col-sm-6 control-label">'.get_lang('AllowSelfReg').'</label>
1813
            <div class="col-sm-6">';
1814
    if ($installType == 'update') {
1815
        if ($allowSelfReg == 'true') {
1816
            $label = get_lang('Yes');
1817
        } elseif ($allowSelfReg == 'false') {
1818
            $label = get_lang('No');
1819
        } else {
1820
            $label = get_lang('AfterApproval');
1821
        }
1822
        $html .= '<input type="hidden" name="allowSelfReg" value="'.$allowSelfReg.'" />'.$label;
1823
    } else {
1824
        $html .= '<div class="control-group">';
1825
        $html .= '<label class="checkbox-inline">
1826
                        <input type="radio" name="allowSelfReg" value="true" id="allowSelfReg1" '.($allowSelfReg == 'true' ? 'checked="checked" ' : '').' /> '.get_lang('Yes').'
1827
                    </label>';
1828
        $html .= '<label class="checkbox-inline">
1829
                        <input type="radio" name="allowSelfReg" value="false" id="allowSelfReg0" '.($allowSelfReg == 'false' ? '' : 'checked="checked" ').' /> '.get_lang('No').'
1830
                    </label>';
1831
        $html .= '<label class="checkbox-inline">
1832
                    <input type="radio" name="allowSelfReg" value="approval" id="allowSelfReg2" '.($allowSelfReg == 'approval' ? '' : 'checked="checked" ').' /> '.get_lang('AfterApproval').'
1833
                </label>';
1834
        $html .= '</div>';
1835
    }
1836
    $html .= '</div>';
1837
    $html .= '</div>';
1838
1839
    $html .= '<div class="form-group">';
1840
    $html .= '<label class="col-sm-6 control-label">'.get_lang('AllowSelfRegProf').'</label>
1841
        <div class="col-sm-6">';
1842
    if ($installType == 'update') {
1843
        if ($allowSelfRegProf == 'true') {
1844
            $label = get_lang('Yes');
1845
        } else {
1846
            $label = get_lang('No');
1847
        }
1848
        $html .= '<input type="hidden" name="allowSelfRegProf" value="'.$allowSelfRegProf.'" />'.$label;
1849
    } else {
1850
        $html .= '<div class="control-group">
1851
                <label class="checkbox-inline">
1852
                    <input type="radio" name="allowSelfRegProf" value="1" id="allowSelfRegProf1" '.($allowSelfRegProf ? 'checked="checked" ' : '').'/>
1853
                '.get_lang('Yes').'
1854
                </label>';
1855
        $html .= '<label class="checkbox-inline">
1856
                    <input type="radio" name="allowSelfRegProf" value="0" id="allowSelfRegProf0" '.($allowSelfRegProf ? '' : 'checked="checked" ').' />
1857
                   '.get_lang('No').'
1858
                </label>';
1859
        $html .= '</div>';
1860
    }
1861
    $html .= '</div>
1862
    </div>';
1863
1864
    echo panel($html, get_lang('Platform'), 'platform'); ?>
1865
    <div class='btn-group'>
1866
        <button type="submit" class="btn btn-secondary pull-right" name="step3" value="&lt; <?php echo get_lang('Previous'); ?>" ><em class="fa fa-backward"> </em> <?php echo get_lang('Previous'); ?></button>
1867
        <input type="hidden" name="is_executable" id="is_executable" value="-" />
1868
        <button class="btn btn-success" type="submit" name="step5" value="<?php echo get_lang('Next'); ?> &gt;" ><em class="fa fa-forward"> </em> <?php echo get_lang('Next'); ?></button>
1869
    </div>
1870
1871
    <?php
1872
}
1873
1874
/**
1875
 * After installation is completed (step 6), this message is displayed.
1876
 *
1877
 * @param string $installType
1878
 */
1879
function display_after_install_message($installType)
0 ignored issues
show
Unused Code introduced by
The parameter $installType is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

1879
function display_after_install_message(/** @scrutinizer ignore-unused */ $installType)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
1880
{
1881
    echo '<div class="RequirementContent">'.get_lang('FirstUseTip').'</div>';
1882
    echo '<div class="alert alert-warning">';
1883
    echo '<strong>'.get_lang('SecurityAdvice').'</strong>';
1884
    echo ': ';
1885
    printf(get_lang('ToProtectYourSiteMakeXReadOnlyAndDeleteY'), 'app/config/', 'main/install/');
1886
    echo '</div>'; ?></form>
1887
    <br />
1888
    <a class="btn btn-success btn-block" href="../../index.php">
1889
        <?php echo get_lang('GoToYourNewlyCreatedPortal'); ?>
1890
    </a>
1891
    <?php
1892
}
1893
1894
/**
1895
 * This function return countries list from array (hardcoded).
1896
 *
1897
 * @param bool $combo (Optional) True for returning countries list with select html
1898
 *
1899
 * @return array|string countries list
1900
 */
1901
function get_countries_list_from_array($combo = false)
1902
{
1903
    $a_countries = [
1904
        "Afghanistan", "Albania", "Algeria", "Andorra", "Angola", "Antigua and Barbuda", "Argentina", "Armenia", "Australia", "Austria", "Azerbaijan",
1905
        "Bahamas", "Bahrain", "Bangladesh", "Barbados", "Belarus", "Belgium", "Belize", "Benin", "Bhutan", "Bolivia", "Bosnia and Herzegovina", "Botswana", "Brazil", "Brunei", "Bulgaria", "Burkina Faso", "Burundi",
1906
        "Cambodia", "Cameroon", "Canada", "Cape Verde", "Central African Republic", "Chad", "Chile", "China", "Colombi", "Comoros", "Congo (Brazzaville)", "Congo", "Costa Rica", "Cote d'Ivoire", "Croatia", "Cuba", "Cyprus", "Czech Republic",
1907
        "Denmark", "Djibouti", "Dominica", "Dominican Republic",
1908
        "East Timor (Timor Timur)", "Ecuador", "Egypt", "El Salvador", "Equatorial Guinea", "Eritrea", "Estonia", "Ethiopia",
1909
        "Fiji", "Finland", "France",
1910
        "Gabon", "Gambia, The", "Georgia", "Germany", "Ghana", "Greece", "Grenada", "Guatemala", "Guinea", "Guinea-Bissau", "Guyana",
1911
        "Haiti", "Honduras", "Hungary",
1912
        "Iceland", "India", "Indonesia", "Iran", "Iraq", "Ireland", "Israel", "Italy",
1913
        "Jamaica", "Japan", "Jordan",
1914
        "Kazakhstan", "Kenya", "Kiribati", "Korea, North", "Korea, South", "Kuwait", "Kyrgyzstan",
1915
        "Laos", "Latvia", "Lebanon", "Lesotho", "Liberia", "Libya", "Liechtenstein", "Lithuania", "Luxembourg",
1916
        "Macedonia", "Madagascar", "Malawi", "Malaysia", "Maldives", "Mali", "Malta", "Marshall Islands", "Mauritania", "Mauritius", "Mexico", "Micronesia", "Moldova", "Monaco", "Mongolia", "Morocco", "Mozambique", "Myanmar",
1917
        "Namibia", "Nauru", "Nepa", "Netherlands", "New Zealand", "Nicaragua", "Niger", "Nigeria", "Norway",
1918
        "Oman",
1919
        "Pakistan", "Palau", "Panama", "Papua New Guinea", "Paraguay", "Peru", "Philippines", "Poland", "Portugal",
1920
        "Qatar",
1921
        "Romania", "Russia", "Rwanda",
1922
        "Saint Kitts and Nevis", "Saint Lucia", "Saint Vincent", "Samoa", "San Marino", "Sao Tome and Principe", "Saudi Arabia", "Senegal", "Serbia and Montenegro", "Seychelles", "Sierra Leone", "Singapore", "Slovakia", "Slovenia", "Solomon Islands", "Somalia", "South Africa", "Spain", "Sri Lanka", "Sudan", "Suriname", "Swaziland", "Sweden", "Switzerland", "Syria",
1923
        "Taiwan", "Tajikistan", "Tanzania", "Thailand", "Togo", "Tonga", "Trinidad and Tobago", "Tunisia", "Turkey", "Turkmenistan", "Tuvalu",
1924
        "Uganda", "Ukraine", "United Arab Emirates", "United Kingdom", "United States", "Uruguay", "Uzbekistan",
1925
        "Vanuatu", "Vatican City", "Venezuela", "Vietnam",
1926
        "Yemen",
1927
        "Zambia", "Zimbabwe",
1928
    ];
1929
    if ($combo) {
1930
        $country_select = '<select class="selectpicker show-tick" id="country" name="country">';
1931
        $country_select .= '<option value="">--- '.get_lang('SelectOne').' ---</option>';
1932
        foreach ($a_countries as $country) {
1933
            $country_select .= '<option value="'.$country.'">'.$country.'</option>';
1934
        }
1935
        $country_select .= '</select>';
1936
1937
        return $country_select;
1938
    }
1939
1940
    return $a_countries;
1941
}
1942
1943
/**
1944
 * Lock settings that can't be changed in other portals.
1945
 */
1946
function lockSettings()
1947
{
1948
    $settings = api_get_locked_settings();
1949
    $table = Database::get_main_table(TABLE_MAIN_SETTINGS_CURRENT);
1950
    foreach ($settings as $setting) {
1951
        $sql = "UPDATE $table SET access_url_locked = 1 WHERE variable  = '$setting'";
1952
        Database::query($sql);
1953
    }
1954
}
1955
1956
/**
1957
 * Update dir values.
1958
 */
1959
function updateDirAndFilesPermissions()
1960
{
1961
    $table = Database::get_main_table(TABLE_MAIN_SETTINGS_CURRENT);
1962
    $permissions_for_new_directories = isset($_SESSION['permissions_for_new_directories']) ? $_SESSION['permissions_for_new_directories'] : 0770;
1963
    $permissions_for_new_files = isset($_SESSION['permissions_for_new_files']) ? $_SESSION['permissions_for_new_files'] : 0660;
1964
    // use decoct() to store as string
1965
    $sql = "UPDATE $table SET selected_value = '0".decoct($permissions_for_new_directories)."'
1966
              WHERE variable  = 'permissions_for_new_directories'";
1967
    Database::query($sql);
1968
1969
    $sql = "UPDATE $table SET selected_value = '0".decoct($permissions_for_new_files)."' WHERE variable  = 'permissions_for_new_files'";
1970
    Database::query($sql);
1971
1972
    if (isset($_SESSION['permissions_for_new_directories'])) {
1973
        unset($_SESSION['permissions_for_new_directories']);
1974
    }
1975
1976
    if (isset($_SESSION['permissions_for_new_files'])) {
1977
        unset($_SESSION['permissions_for_new_files']);
1978
    }
1979
}
1980
1981
/**
1982
 * @param $current_value
1983
 * @param $wanted_value
1984
 *
1985
 * @return string
1986
 */
1987
function compare_setting_values($current_value, $wanted_value)
1988
{
1989
    $current_value_string = $current_value;
1990
    $current_value = (float) $current_value;
1991
    $wanted_value = (float) $wanted_value;
1992
1993
    if ($current_value >= $wanted_value) {
1994
        return Display::label($current_value_string, 'success');
1995
    } else {
1996
        return Display::label($current_value_string, 'important');
1997
    }
1998
}
1999
2000
/**
2001
 * @param string $course_dir
2002
 * @param string $course_attempt_name
2003
 * @param string $file
2004
 *
2005
 * @return bool
2006
 */
2007
function checkCourseScriptCreation(
2008
    $course_dir,
2009
    $course_attempt_name,
2010
    $file
2011
) {
2012
    $output = false;
2013
    //Write in file
2014
    $file_name = $course_dir.'/'.$file;
2015
    $content = '123';
2016
2017
    if (is_writable($file_name)) {
2018
        if ($handler = @fopen($file_name, 'w')) {
2019
            //write content
2020
            if (fwrite($handler, $content)) {
2021
                $sock_errno = '';
2022
                $sock_errmsg = '';
2023
                $url = api_get_path(WEB_PATH).'app/courses/'.$course_attempt_name.'/'.$file;
2024
2025
                $parsed_url = parse_url($url);
2026
                //$scheme = isset($parsedUrl['scheme']) ? $parsedUrl['scheme'] : ''; //http
2027
                $host = isset($parsed_url['host']) ? $parsed_url['host'] : '';
2028
                // Patch if the host is the default host and is used through
2029
                // the IP address (sometimes the host is not taken correctly
2030
                // in this case)
2031
                if (empty($host) && !empty($_SERVER['HTTP_HOST'])) {
2032
                    $host = $_SERVER['HTTP_HOST'];
2033
                    $url = preg_replace('#:///#', '://'.$host.'/', $url);
2034
                }
2035
                $path = isset($parsed_url['path']) ? $parsed_url['path'] : '/';
2036
                $port = '';
2037
                $scheme = '';
2038
                switch ($parsed_url['scheme']) {
2039
                    case 'https':
2040
                        $scheme = 'ssl://';
2041
                        $port = 443;
2042
                        break;
2043
                    case 'http':
2044
                    default:
2045
                        $scheme = '';
2046
                        $port = 80;
2047
                }
2048
2049
                //Check fsockopen (not sure it works with https). If that is your case, you might want to try the
2050
                // suggestion at https://support.chamilo.org/issues/8260#note-3 (although it ignores SSL peer checks)
2051
                if ($fp = @fsockopen(str_replace('http://', $scheme, $url), $port, $sock_errno, $sock_errmsg, 60)) {
2052
                    $out = "GET $path HTTP/1.1\r\n";
2053
                    $out .= "Host: $host\r\n";
2054
                    $out .= "Connection: Close\r\n\r\n";
2055
2056
                    fwrite($fp, $out);
2057
                    while (!feof($fp)) {
2058
                        $result = str_replace("\r\n", '', fgets($fp, 128));
2059
                        if (!empty($result) && $result == '123') {
2060
                            $output = true;
2061
                        }
2062
                    }
2063
                    fclose($fp);
2064
                } elseif (ini_get('allow_url_fopen')) {
2065
                    // Check allow_url_fopen
2066
                    if ($fp = @fopen($url, 'r')) {
2067
                        while ($result = fgets($fp, 1024)) {
2068
                            if (!empty($result) && $result == '123') {
2069
                                $output = true;
2070
                            }
2071
                        }
2072
                        fclose($fp);
2073
                    }
2074
                } elseif (function_exists('curl_init')) {
2075
                    // Check if has support for cURL
2076
                    $ch = curl_init();
2077
                    curl_setopt($ch, CURLOPT_HEADER, 0);
2078
                    curl_setopt($ch, CURLOPT_URL, $url);
2079
                    //curl_setopt($ch, CURLOPT_TIMEOUT, 30);
2080
                    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
2081
                    $result = curl_exec($ch);
2082
                    if (!empty($result) && $result == '123') {
2083
                        $output = true;
2084
                    }
2085
                    curl_close($ch);
2086
                }
2087
            }
2088
            @fclose($handler);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for fclose(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

2088
            /** @scrutinizer ignore-unhandled */ @fclose($handler);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
2089
        }
2090
    }
2091
2092
    return $output;
2093
}
2094
2095
/**
2096
 * Save settings values.
2097
 *
2098
 * @param string $organizationName
2099
 * @param string $organizationUrl
2100
 * @param string $siteName
2101
 * @param string $adminEmail
2102
 * @param string $adminLastName
2103
 * @param string $adminFirstName
2104
 * @param string $language
2105
 * @param string $allowRegistration
2106
 * @param string $allowTeacherSelfRegistration
2107
 * @param string $installationProfile          The name of an installation profile file in main/install/profiles/
2108
 */
2109
function installSettings(
2110
    $organizationName,
2111
    $organizationUrl,
2112
    $siteName,
2113
    $adminEmail,
2114
    $adminLastName,
2115
    $adminFirstName,
2116
    $language,
2117
    $allowRegistration,
2118
    $allowTeacherSelfRegistration,
2119
    $installationProfile = ''
2120
) {
2121
    $allowTeacherSelfRegistration = $allowTeacherSelfRegistration ? 'true' : 'false';
2122
2123
    $settings = [
2124
        'institution' => $organizationName,
2125
        'institution_url' => $organizationUrl,
2126
        'site_name' => $siteName,
2127
        'administrator_email' => $adminEmail,
2128
        'administrator_surname' => $adminLastName,
2129
        'administrator_name' => $adminFirstName,
2130
        'platform_language' => $language,
2131
        'allow_registration' => $allowRegistration,
2132
        'allow_registration_as_teacher' => $allowTeacherSelfRegistration,
2133
    ];
2134
2135
    foreach ($settings as $variable => $value) {
2136
        $sql = "UPDATE settings_current
2137
                SET selected_value = '$value'
2138
                WHERE variable = '$variable'";
2139
        Database::query($sql);
2140
    }
2141
    installProfileSettings($installationProfile);
2142
}
2143
2144
/**
2145
 * Executes DB changes based in the classes defined in
2146
 * src/CoreBundle/Migrations/Schema/*.
2147
 *
2148
 * @param string        $chamiloVersion
2149
 * @param EntityManager $manager
2150
 *
2151
 * @throws \Doctrine\DBAL\DBALException
2152
 *
2153
 * @return bool
2154
 */
2155
function migrate($chamiloVersion, EntityManager $manager)
2156
{
2157
    $debug = true;
2158
    $connection = $manager->getConnection();
2159
2160
    $config = new \Doctrine\DBAL\Migrations\Configuration\Configuration($connection);
2161
2162
    // Table name that will store migrations log (will be created automatically,
2163
    // default name is: doctrine_migration_versions)
2164
    $config->setMigrationsTableName('version');
2165
    // Namespace of your migration classes, do not forget escape slashes, do not add last slash
2166
    $config->setMigrationsNamespace('Chamilo\CoreBundle\Migrations\Schema\V'.$chamiloVersion);
2167
    // Directory where your migrations are located
2168
    $versionPath = api_get_path(SYS_PATH).'src/CoreBundle/Migrations/Schema/V'.$chamiloVersion;
2169
    error_log("Reading files from dir: $versionPath");
2170
    $config->setMigrationsDirectory($versionPath);
2171
    // Load your migrations
2172
    $config->registerMigrationsFromDirectory($config->getMigrationsDirectory());
2173
2174
    $migration = new \Doctrine\DBAL\Migrations\Migration($config);
2175
    $versions = $config->getMigrations();
2176
2177
    /** @var Doctrine\DBAL\Migrations\Version $migrationItem */
2178
    foreach ($versions as $version) {
2179
        $version->getMigration()->setEntityManager($manager);
2180
    }
2181
2182
    $to = null; // if $to == null then schema will be migrated to latest version
2183
2184
    echo "<pre>";
2185
    try {
2186
        // Execute migration!
2187
        $migratedSQL = $migration->migrate($to);
2188
2189
        if ($debug) {
2190
            foreach ($migratedSQL as $version => $sqlList) {
2191
                echo "VERSION: $version<br>";
2192
                echo "----------------------------------------------<br>";
2193
                $total = count($sqlList);
2194
                error_log("VERSION: $version");
2195
                error_log("# queries: ".$total);
2196
                $counter = 1;
2197
                foreach ($sqlList as $sql) {
2198
                    echo "<code>$sql</code><br>";
2199
                    error_log("$counter/$total : $sql");
2200
                    $counter++;
2201
                }
2202
            }
2203
2204
            echo "<br>DONE!<br>";
2205
        }
2206
2207
        return true;
2208
    } catch (Exception $ex) {
2209
        if ($debug) {
2210
            echo "ERROR: {$ex->getMessage()}<br>";
2211
2212
            return false;
2213
        }
2214
    }
2215
2216
    echo "</pre>";
2217
2218
    return false;
2219
}
2220
2221
/**
2222
 * @param EntityManager $em
2223
 *
2224
 * @throws \Doctrine\DBAL\DBALException
2225
 */
2226
function fixIds(EntityManager $em)
2227
{
2228
    $connection = $em->getConnection();
2229
    $database = new Database();
2230
    $database->setManager($em);
2231
    $debug = true;
2232
    if ($debug) {
2233
        error_log('fixIds');
2234
    }
2235
2236
    // Create temporary indexes to increase speed of the following operations
2237
    // Adding and removing indexes will usually take much less time than
2238
    // the execution without indexes of the queries in this function, particularly
2239
    // for large tables
2240
    $sql = "ALTER TABLE c_document ADD INDEX tmpidx_doc(c_id, id)";
2241
    $connection->executeQuery($sql);
2242
    $sql = "ALTER TABLE c_student_publication ADD INDEX tmpidx_stud (c_id, id)";
2243
    $connection->executeQuery($sql);
2244
    $sql = "ALTER TABLE c_quiz ADD INDEX tmpidx_quiz (c_id, id)";
2245
    $connection->executeQuery($sql);
2246
    $sql = "ALTER TABLE c_item_property ADD INDEX tmpidx_ip (to_group_id)";
2247
    $connection->executeQuery($sql);
2248
2249
    $sql = "SELECT * FROM c_lp_item";
2250
    $result = $connection->fetchAll($sql);
2251
    foreach ($result as $item) {
2252
        $courseId = $item['c_id'];
2253
        $iid = isset($item['iid']) ? intval($item['iid']) : 0;
2254
        $ref = isset($item['ref']) ? intval($item['ref']) : 0;
2255
        $sql = null;
2256
2257
        $newId = '';
2258
2259
        switch ($item['item_type']) {
2260
            case TOOL_LINK:
2261
                $sql = "SELECT * FROM c_link WHERE c_id = $courseId AND id = $ref";
2262
                $data = $connection->fetchAssoc($sql);
2263
                if ($data) {
2264
                    $newId = $data['iid'];
2265
                }
2266
                break;
2267
            case TOOL_STUDENTPUBLICATION:
2268
                $sql = "SELECT * FROM c_student_publication WHERE c_id = $courseId AND id = $ref";
2269
                $data = $connection->fetchAssoc($sql);
2270
                if ($data) {
2271
                    $newId = $data['iid'];
2272
                }
2273
                break;
2274
            case TOOL_QUIZ:
2275
                $sql = "SELECT * FROM c_quiz WHERE c_id = $courseId AND id = $ref";
2276
                $data = $connection->fetchAssoc($sql);
2277
                if ($data) {
2278
                    $newId = $data['iid'];
2279
                }
2280
                break;
2281
            case TOOL_DOCUMENT:
2282
                $sql = "SELECT * FROM c_document WHERE c_id = $courseId AND id = $ref";
2283
                $data = $connection->fetchAssoc($sql);
2284
                if ($data) {
2285
                    $newId = $data['iid'];
2286
                }
2287
                break;
2288
            case TOOL_FORUM:
2289
                $sql = "SELECT * FROM c_forum_forum WHERE c_id = $courseId AND forum_id = $ref";
2290
                $data = $connection->fetchAssoc($sql);
2291
                if ($data) {
2292
                    $newId = $data['iid'];
2293
                }
2294
                break;
2295
            case 'thread':
2296
                $sql = "SELECT * FROM c_forum_thread WHERE c_id = $courseId AND thread_id = $ref";
2297
                $data = $connection->fetchAssoc($sql);
2298
                if ($data) {
2299
                    $newId = $data['iid'];
2300
                }
2301
                break;
2302
        }
2303
2304
        if (!empty($sql) && !empty($newId) && !empty($iid)) {
2305
            $sql = "UPDATE c_lp_item SET ref = $newId WHERE iid = $iid";
2306
            $connection->executeQuery($sql);
2307
        }
2308
    }
2309
2310
    // Set NULL if session = 0
2311
    $sql = "UPDATE c_item_property SET session_id = NULL WHERE session_id = 0";
2312
    $connection->executeQuery($sql);
2313
2314
    // Set NULL if group = 0
2315
    $sql = "UPDATE c_item_property SET to_group_id = NULL WHERE to_group_id = 0";
2316
    $connection->executeQuery($sql);
2317
2318
    // Set NULL if insert_user_id = 0
2319
    $sql = "UPDATE c_item_property SET insert_user_id = NULL WHERE insert_user_id = 0";
2320
    $connection->executeQuery($sql);
2321
2322
    // Delete session data of sessions that don't exist.
2323
    $sql = "DELETE FROM c_item_property
2324
            WHERE session_id IS NOT NULL AND session_id NOT IN (SELECT id FROM session)";
2325
    $connection->executeQuery($sql);
2326
2327
    // Delete group data of groups that don't exist.
2328
    $sql = "DELETE FROM c_item_property
2329
            WHERE to_group_id IS NOT NULL AND to_group_id NOT IN (SELECT DISTINCT id FROM c_group_info)";
2330
    $connection->executeQuery($sql);
2331
2332
    // This updates the group_id with c_group_info.iid instead of c_group_info.id
2333
2334
    if ($debug) {
2335
        error_log('update iids');
2336
    }
2337
2338
    $groupTableToFix = [
2339
        'c_group_rel_user',
2340
        'c_group_rel_tutor',
2341
        'c_permission_group',
2342
        'c_role_group',
2343
        'c_survey_invitation',
2344
        'c_attendance_calendar_rel_group',
2345
    ];
2346
2347
    foreach ($groupTableToFix as $table) {
2348
        $sql = "SELECT * FROM $table";
2349
        $result = $connection->fetchAll($sql);
2350
        foreach ($result as $item) {
2351
            $iid = $item['iid'];
2352
            $courseId = $item['c_id'];
2353
            $groupId = intval($item['group_id']);
2354
2355
            // Fix group id
2356
            if (!empty($groupId)) {
2357
                $sql = "SELECT * FROM c_group_info
2358
                        WHERE c_id = $courseId AND id = $groupId
2359
                        LIMIT 1";
2360
                $data = $connection->fetchAssoc($sql);
2361
                if (!empty($data)) {
2362
                    $newGroupId = $data['iid'];
2363
                    $sql = "UPDATE $table SET group_id = $newGroupId
2364
                            WHERE iid = $iid";
2365
                    $connection->executeQuery($sql);
2366
                } else {
2367
                    // The group does not exists clean this record
2368
                    $sql = "DELETE FROM $table WHERE iid = $iid";
2369
                    $connection->executeQuery($sql);
2370
                }
2371
            }
2372
        }
2373
    }
2374
2375
    // Fix c_item_property
2376
    if ($debug) {
2377
        error_log('update c_item_property');
2378
    }
2379
2380
    $sql = "SELECT * FROM course";
2381
    $courseList = $connection->fetchAll($sql);
2382
    if ($debug) {
2383
        error_log('Getting course list');
2384
    }
2385
2386
    $totalCourse = count($courseList);
2387
    $counter = 0;
2388
2389
    foreach ($courseList as $courseData) {
2390
        $courseId = $courseData['id'];
2391
        if ($debug) {
2392
            error_log('Updating course: '.$courseData['code']);
2393
        }
2394
2395
        $sql = "SELECT * FROM c_item_property WHERE c_id = $courseId";
2396
        $result = $connection->fetchAll($sql);
2397
        foreach ($result as $item) {
2398
            $sessionId = intval($item['session_id']);
2399
            $groupId = intval($item['to_group_id']);
2400
            $iid = $item['iid'];
2401
            $ref = $item['ref'];
2402
2403
            // Fix group id
2404
            if (!empty($groupId)) {
2405
                $sql = "SELECT * FROM c_group_info
2406
                        WHERE c_id = $courseId AND id = $groupId";
2407
                $data = $connection->fetchAssoc($sql);
2408
                if (!empty($data)) {
2409
                    $newGroupId = $data['iid'];
2410
                    $sql = "UPDATE c_item_property SET to_group_id = $newGroupId
2411
                            WHERE iid = $iid";
2412
                    $connection->executeQuery($sql);
2413
                } else {
2414
                    // The group does not exists clean this record
2415
                    $sql = "DELETE FROM c_item_property WHERE iid = $iid";
2416
                    $connection->executeQuery($sql);
2417
                }
2418
            }
2419
2420
            $sql = '';
2421
            $newId = '';
2422
            switch ($item['tool']) {
2423
                case TOOL_LINK:
2424
                    $sql = "SELECT * FROM c_link WHERE c_id = $courseId AND id = $ref ";
2425
                    break;
2426
                case TOOL_STUDENTPUBLICATION:
2427
                    $sql = "SELECT * FROM c_student_publication WHERE c_id = $courseId AND id = $ref";
2428
                    break;
2429
                case TOOL_QUIZ:
2430
                    $sql = "SELECT * FROM c_quiz WHERE c_id = $courseId AND id = $ref";
2431
                    break;
2432
                case TOOL_DOCUMENT:
2433
                    $sql = "SELECT * FROM c_document WHERE c_id = $courseId AND id = $ref";
2434
                    break;
2435
                case TOOL_FORUM:
2436
                    $sql = "SELECT * FROM c_forum_forum WHERE c_id = $courseId AND id = $ref";
2437
                    break;
2438
                case 'thread':
2439
                    $sql = "SELECT * FROM c_forum_thread WHERE c_id = $courseId AND id = $ref";
2440
                    break;
2441
            }
2442
2443
            if (!empty($sql) && !empty($newId)) {
2444
                $data = $connection->fetchAssoc($sql);
2445
                if (isset($data['iid'])) {
2446
                    $newId = $data['iid'];
2447
                }
2448
                $sql = "UPDATE c_item_property SET ref = $newId WHERE iid = $iid";
2449
                error_log($sql);
2450
                $connection->executeQuery($sql);
2451
            }
2452
        }
2453
2454
        if ($debug) {
2455
            // Print a status in the log once in a while
2456
            error_log("Course process #$counter/$totalCourse");
2457
        }
2458
        $counter++;
2459
    }
2460
2461
    if ($debug) {
2462
        error_log('update gradebook_link');
2463
    }
2464
2465
    // Fix gradebook_link
2466
    $sql = "SELECT * FROM gradebook_link";
2467
    $result = $connection->fetchAll($sql);
2468
    foreach ($result as $item) {
2469
        $courseCode = $item['course_code'];
2470
        $categoryId = (int) $item['category_id'];
2471
2472
        $sql = "SELECT * FROM course WHERE code = '$courseCode'";
2473
        $courseInfo = $connection->fetchAssoc($sql);
2474
        if (empty($courseInfo)) {
2475
            continue;
2476
        }
2477
2478
        $courseId = $courseInfo['id'];
2479
2480
        $ref = $item['ref_id'];
2481
        $iid = $item['id'];
2482
2483
        $sql = '';
2484
        switch ($item['type']) {
2485
            case LINK_LEARNPATH:
2486
                $sql = "SELECT * FROM c_link WHERE c_id = $courseId AND id = $ref ";
2487
                break;
2488
            case LINK_STUDENTPUBLICATION:
2489
                $sql = "SELECT * FROM c_student_publication WHERE c_id = $courseId AND id = $ref";
2490
                break;
2491
            case LINK_EXERCISE:
2492
                $sql = "SELECT * FROM c_quiz WHERE c_id = $courseId AND id = $ref";
2493
                break;
2494
            case LINK_ATTENDANCE:
2495
                //$sql = "SELECT * FROM c_document WHERE c_id = $courseId AND id = $ref";
2496
                break;
2497
            case LINK_FORUM_THREAD:
2498
                $sql = "SELECT * FROM c_forum_thread WHERE c_id = $courseId AND thread_id = $ref";
2499
                break;
2500
        }
2501
2502
        if (!empty($sql)) {
2503
            $data = $connection->fetchAssoc($sql);
2504
            if (isset($data) && isset($data['iid'])) {
2505
                $newId = $data['iid'];
2506
                $sql = "UPDATE gradebook_link SET ref_id = $newId
2507
                        WHERE id = $iid AND course_code = '$courseCode' AND category_id = $categoryId ";
2508
                $connection->executeQuery($sql);
2509
            }
2510
        }
2511
    }
2512
2513
    if ($debug) {
2514
        error_log('update groups');
2515
    }
2516
2517
    $sql = "SELECT * FROM groups";
2518
    $result = $connection->executeQuery($sql);
2519
    $groups = $result->fetchAll();
2520
    $oldGroups = [];
2521
    if (!empty($groups)) {
2522
        foreach ($groups as $group) {
2523
            if (empty($group['name'])) {
2524
                continue;
2525
            }
2526
2527
            $params = [
2528
                'name' => $group['name'],
2529
                'description' => $group['description'],
2530
                'group_type' => 1,
2531
                'picture' => $group['picture_uri'],
2532
                'url' => $group['url'],
2533
                'visibility' => $group['visibility'],
2534
                'updated_at' => $group['updated_on'],
2535
                'created_at' => $group['created_on'],
2536
            ];
2537
            $connection->insert('usergroup', $params);
2538
            $id = $connection->lastInsertId('id');
2539
            $oldGroups[$group['id']] = $id;
2540
        }
2541
    }
2542
2543
    if (!empty($oldGroups)) {
2544
        error_log('Moving group files');
2545
        foreach ($oldGroups as $oldId => $newId) {
2546
            $path = get_group_picture_path_by_id(
2547
                $oldId,
2548
                'system'
2549
            );
2550
2551
            if (!empty($path)) {
2552
                $newPath = str_replace(
2553
                    "groups/$oldId/",
2554
                    "groups/$newId/",
2555
                    $path['dir']
2556
                );
2557
                $command = "mv {$path['dir']} $newPath ";
2558
                error_log("Executing $command");
2559
                system($command);
2560
            }
2561
        }
2562
2563
        $sql = "SELECT * FROM group_rel_user";
2564
        $result = $connection->executeQuery($sql);
2565
        $dataList = $result->fetchAll();
2566
2567
        if (!empty($dataList)) {
2568
            foreach ($dataList as $data) {
2569
                if (isset($oldGroups[$data['group_id']])) {
2570
                    $data['group_id'] = $oldGroups[$data['group_id']];
2571
                    $userId = $data['user_id'];
2572
2573
                    $sql = "SELECT id FROM user WHERE user_id = $userId";
2574
                    $userResult = $connection->executeQuery($sql);
2575
                    $userInfo = $userResult->fetch();
2576
                    if (empty($userInfo)) {
2577
                        continue;
2578
                    }
2579
2580
                    $sql = "INSERT INTO usergroup_rel_user (usergroup_id, user_id, relation_type)
2581
                            VALUES ('{$data['group_id']}', '{$userId}', '{$data['relation_type']}')";
2582
                    $connection->executeQuery($sql);
2583
                }
2584
            }
2585
        }
2586
2587
        $sql = "SELECT * FROM group_rel_group";
2588
        $result = $connection->executeQuery($sql);
2589
        $dataList = $result->fetchAll();
2590
2591
        if (!empty($dataList)) {
2592
            foreach ($dataList as $data) {
2593
                if (isset($oldGroups[$data['group_id']]) && isset($oldGroups[$data['subgroup_id']])) {
2594
                    $data['group_id'] = $oldGroups[$data['group_id']];
2595
                    $data['subgroup_id'] = $oldGroups[$data['subgroup_id']];
2596
                    $sql = "INSERT INTO usergroup_rel_usergroup (group_id, subgroup_id, relation_type)
2597
                            VALUES ('{$data['group_id']}', '{$data['subgroup_id']}', '{$data['relation_type']}')";
2598
                    $connection->executeQuery($sql);
2599
                }
2600
            }
2601
        }
2602
2603
        $sql = "SELECT * FROM announcement_rel_group";
2604
        $result = $connection->executeQuery($sql);
2605
        $dataList = $result->fetchAll();
2606
2607
        if (!empty($dataList)) {
2608
            foreach ($dataList as $data) {
2609
                if (isset($oldGroups[$data['group_id']])) {
2610
                    // Deleting relation
2611
                    $sql = "DELETE FROM announcement_rel_group WHERE group_id = {$data['group_id']}";
2612
                    $connection->executeQuery($sql);
2613
2614
                    // Add new relation
2615
                    $data['group_id'] = $oldGroups[$data['group_id']];
2616
                    $sql = "INSERT INTO announcement_rel_group(group_id, announcement_id)
2617
                            VALUES ('{$data['group_id']}', '{$data['announcement_id']}')";
2618
                    $connection->executeQuery($sql);
2619
                }
2620
            }
2621
        }
2622
2623
        $sql = "SELECT * FROM group_rel_tag";
2624
        $result = $connection->executeQuery($sql);
2625
        $dataList = $result->fetchAll();
2626
        if (!empty($dataList)) {
2627
            foreach ($dataList as $data) {
2628
                if (isset($oldGroups[$data['group_id']])) {
2629
                    $data['group_id'] = $oldGroups[$data['group_id']];
2630
                    $sql = "INSERT INTO usergroup_rel_tag (tag_id, usergroup_id)
2631
                            VALUES ('{$data['tag_id']}', '{$data['group_id']}')";
2632
                    $connection->executeQuery($sql);
2633
                }
2634
            }
2635
        }
2636
    }
2637
2638
    if ($debug) {
2639
        error_log('update extra fields');
2640
    }
2641
2642
    // Extra fields
2643
    $extraFieldTables = [
2644
        ExtraField::USER_FIELD_TYPE => Database::get_main_table(TABLE_MAIN_USER_FIELD),
2645
        ExtraField::COURSE_FIELD_TYPE => Database::get_main_table(TABLE_MAIN_COURSE_FIELD),
2646
        //ExtraField::LP_FIELD_TYPE => Database::get_main_table(TABLE_MAIN_LP_FIELD),
2647
        ExtraField::SESSION_FIELD_TYPE => Database::get_main_table(TABLE_MAIN_SESSION_FIELD),
2648
        //ExtraField::CALENDAR_FIELD_TYPE => Database::get_main_table(TABLE_MAIN_CALENDAR_EVENT_FIELD),
2649
        //ExtraField::QUESTION_FIELD_TYPE => Database::get_main_table(TABLE_MAIN_CALENDAR_EVENT_FIELD),
2650
        //ExtraField::USER_FIELD_TYPE => //Database::get_main_table(TABLE_MAIN_SPECIFIC_FIELD),
2651
    ];
2652
2653
    foreach ($extraFieldTables as $type => $table) {
2654
        $sql = "SELECT * FROM $table ";
2655
        if ($debug) {
2656
            error_log($sql);
2657
        }
2658
        $result = $connection->query($sql);
2659
        $fields = $result->fetchAll();
2660
2661
        foreach ($fields as $field) {
2662
            if ($debug) {
2663
                error_log("Loading field: ".$field['field_variable']);
2664
            }
2665
            $originalId = $field['id'];
2666
2667
            $params = [
2668
                'extra_field_type' => $type,
2669
                'variable' => $field['field_variable'],
2670
                'field_type' => $field['field_type'],
2671
                'display_text' => $field['field_display_text'],
2672
                'default_value' => $field['field_default_value'],
2673
                'field_order' => $field['field_order'],
2674
                'visible' => $field['field_visible'],
2675
                'changeable' => $field['field_changeable'],
2676
                'filter' => $field['field_filter'],
2677
            ];
2678
2679
            $connection->insert('extra_field', $params);
2680
            $newExtraFieldId = $connection->lastInsertId();
2681
2682
            $values = [];
2683
            $handlerId = null;
2684
            switch ($type) {
2685
                case ExtraField::USER_FIELD_TYPE:
2686
                    $optionTable = Database::get_main_table(
2687
                        TABLE_MAIN_USER_FIELD_OPTIONS
2688
                    );
2689
                    $valueTable = Database::get_main_table(
2690
                        TABLE_MAIN_USER_FIELD_VALUES
2691
                    );
2692
                    $handlerId = 'user_id';
2693
                    break;
2694
                case ExtraField::COURSE_FIELD_TYPE:
2695
                    $optionTable = Database::get_main_table(
2696
                        TABLE_MAIN_COURSE_FIELD_OPTIONS
2697
                    );
2698
                    $valueTable = Database::get_main_table(
2699
                        TABLE_MAIN_COURSE_FIELD_VALUES
2700
                    );
2701
                    $handlerId = 'c_id';
2702
                    break;
2703
                case ExtraField::SESSION_FIELD_TYPE:
2704
                    $optionTable = Database::get_main_table(
2705
                        TABLE_MAIN_SESSION_FIELD_OPTIONS
2706
                    );
2707
                    $valueTable = Database::get_main_table(
2708
                        TABLE_MAIN_SESSION_FIELD_VALUES
2709
                    );
2710
                    $handlerId = 'session_id';
2711
                    break;
2712
            }
2713
2714
            if (!empty($optionTable)) {
2715
                $sql = "SELECT * FROM $optionTable WHERE field_id = $originalId ";
2716
                $result = $connection->query($sql);
2717
                $options = $result->fetchAll();
2718
2719
                foreach ($options as $option) {
2720
                    $params = [
2721
                        'display_text' => $option['option_display_text'],
2722
                        'field_id' => $newExtraFieldId,
2723
                        'option_order' => $option['option_order'],
2724
                        'option_value' => $option['option_value'],
2725
                    ];
2726
                    $connection->insert('extra_field_options', $params);
2727
                }
2728
2729
                $sql = "SELECT * FROM $valueTable WHERE field_id = $originalId ";
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $valueTable does not seem to be defined for all execution paths leading up to this point.
Loading history...
2730
                $result = $connection->query($sql);
2731
                $values = $result->fetchAll();
2732
                if ($debug) {
2733
                    error_log("Fetch all values for field");
2734
                }
2735
            }
2736
2737
            if (!empty($values)) {
2738
                if ($debug) {
2739
                    error_log("Saving field value in new table");
2740
                }
2741
                $k = 0;
2742
                foreach ($values as $value) {
2743
                    if (isset($value[$handlerId])) {
2744
                        // Insert without the use of the entity as it reduces
2745
                        // speed to 2 records per second (much too slow)
2746
                        $params = [
2747
                            'field_id' => $newExtraFieldId,
2748
                            'value' => $value['field_value'],
2749
                            'item_id' => $value[$handlerId],
2750
                        ];
2751
                        $connection->insert('extra_field_values', $params);
2752
                        if ($debug && ($k % 10000 == 0)) {
2753
                            error_log("Saving field $k");
2754
                        }
2755
                        $k++;
2756
                    }
2757
                }
2758
            }
2759
        }
2760
    }
2761
2762
    if ($debug) {
0 ignored issues
show
introduced by
$debug is of type mixed, thus it always evaluated to true.
Loading history...
2763
        error_log('Remove index');
2764
    }
2765
2766
    // Drop temporary indexes added to increase speed of this function's queries
2767
    $sql = "ALTER TABLE c_document DROP INDEX tmpidx_doc";
2768
    $connection->executeQuery($sql);
2769
    $sql = "ALTER TABLE c_student_publication DROP INDEX tmpidx_stud";
2770
    $connection->executeQuery($sql);
2771
    $sql = "ALTER TABLE c_quiz DROP INDEX tmpidx_quiz";
2772
    $connection->executeQuery($sql);
2773
    $sql = "ALTER TABLE c_item_property DROP INDEX tmpidx_ip";
2774
    $connection->executeQuery($sql);
2775
2776
    if ($debug) {
2777
        error_log('Finish fixId function');
2778
    }
2779
2780
    fixLpId($connection, true);
2781
}
2782
2783
/**
2784
 * @param \Doctrine\DBAL\Connection $connection
2785
 * @param $debug
2786
 *
2787
 * @throws \Doctrine\DBAL\DBALException
2788
 */
2789
function fixLpId($connection, $debug)
2790
{
2791
    if ($debug) {
2792
        error_log('Fix lp.id lp.iids');
2793
    }
2794
2795
    $sql = 'SELECT id, title, code FROM course';
2796
    $result = $connection->query($sql);
2797
    $courses = $result->fetchAll();
2798
2799
    $sql = 'SELECT id FROM session';
2800
    $result = $connection->query($sql);
2801
    $sessions = $result->fetchAll();
2802
2803
    $tblCLp = Database::get_course_table(TABLE_LP_MAIN);
2804
    $tblCLpItem = Database::get_course_table(TABLE_LP_ITEM);
2805
    $toolTable = Database::get_course_table(TABLE_TOOL_LIST);
2806
2807
    if (!empty($sessions)) {
2808
        $sessions = array_column($sessions, 'id');
2809
        $sessions[] = 0;
2810
    } else {
2811
        $sessions = [0];
2812
    }
2813
2814
    foreach ($courses as $course) {
2815
        $courseId = $course['id'];
2816
        $sql = "SELECT * FROM $tblCLp WHERE c_id = $courseId AND iid <> id ORDER by iid";
2817
        $result = $connection->query($sql);
2818
        if ($debug) {
2819
            error_log('-------------');
2820
            error_log("Entering Lps in course #$courseId");
2821
            error_log($sql);
2822
        }
2823
        $lpList = $result->fetchAll();
2824
        $myOnlyLpList = [];
2825
        if (!empty($lpList)) {
2826
            foreach ($lpList as $lpInfo) {
2827
                $oldId = $lpInfo['id'];
2828
                $sql = "SELECT * FROM $tblCLpItem WHERE c_id = $courseId AND lp_id = $oldId ORDER by iid";
2829
                $result = $connection->query($sql);
2830
                $items = $result->fetchAll();
2831
                $lpInfo['lp_list'] = $items;
2832
                $myOnlyLpList[] = $lpInfo;
2833
            }
2834
        }
2835
2836
        if (!empty($myOnlyLpList)) {
2837
            foreach ($myOnlyLpList as $lpInfo) {
2838
                $lpIid = $lpInfo['iid'];
2839
                $oldId = $lpInfo['id'];
2840
                $items = $lpInfo['lp_list'];
2841
2842
                if (empty($items)) {
2843
                    continue;
2844
                }
2845
                $itemList = [];
2846
                foreach ($items as $subItem) {
2847
                    $itemList[$subItem['id']] = $subItem['iid'];
2848
                }
2849
                $variablesToFix = [
2850
                    'parent_item_id',
2851
                    'next_item_id',
2852
                    'prerequisite',
2853
                    'previous_item_id',
2854
                ];
2855
2856
                foreach ($sessions as $sessionId) {
2857
                    $correctLink = "lp/lp_controller.php?action=view&lp_id=$lpIid&id_session=$sessionId";
2858
                    $link = "newscorm/lp_controller.php?action=view&lp_id=$oldId&id_session=$sessionId";
2859
                    $secondLink = "lp/lp_controller.php?action=view&lp_id=$oldId&id_session=$sessionId";
2860
                    $sql = "UPDATE $toolTable 
2861
                        SET link = '$correctLink'
2862
                        WHERE c_id = $courseId AND (link = '$link' OR link ='$secondLink')";
2863
                    $connection->query($sql);
2864
                    if ($debug) {
2865
                        //error_log("Fix wrong c_tool links");
2866
                        //error_log($sql);
2867
                    }
2868
                }
2869
2870
                foreach ($items as $item) {
2871
                    $itemIid = $item['iid'];
2872
                    $itemId = $item['id'];
2873
                    foreach ($variablesToFix as $variable) {
2874
                        if (!empty($item[$variable]) && isset($itemList[$item[$variable]])) {
2875
                            $newId = $itemList[$item[$variable]];
2876
                            $sql = "UPDATE $tblCLpItem SET $variable = $newId 
2877
                                    WHERE iid = $itemIid AND c_id = $courseId AND lp_id = $oldId";
2878
                            $connection->query($sql);
2879
                            if ($debug) {
2880
                                //error_log($sql);
2881
                            }
2882
                        }
2883
                    }
2884
2885
                    if ($item['item_type'] == 'document' && !empty($item['path'])) {
2886
                        $oldDocumentId = $item['path'];
2887
                        $sql = "SELECT * FROM c_document WHERE c_id = $courseId AND id = $oldDocumentId";
2888
                        $result = $connection->query($sql);
2889
                        $document = $result->fetch();
2890
                        if (!empty($document)) {
2891
                            $newDocumentId = $document['iid'];
2892
                            if (!empty($newDocumentId)) {
2893
                                $sql = "UPDATE $tblCLpItem SET path = $newDocumentId 
2894
                                    WHERE iid = $itemIid AND c_id = $courseId";
2895
                                $connection->query($sql);
2896
                                if ($debug) {
2897
                                    //error_log("Fix document: ");
2898
                                    //error_log($sql);
2899
                                }
2900
                            }
2901
                        }
2902
                    }
2903
2904
                    // c_lp_view
2905
                    $sql = "UPDATE c_lp_view SET last_item = $itemIid 
2906
                            WHERE c_id = $courseId AND last_item = $itemId AND lp_id = $oldId";
2907
                    $connection->query($sql);
2908
2909
                    // c_lp_item_view
2910
                    $sql = "UPDATE c_lp_item_view SET lp_item_id = $itemIid 
2911
                            WHERE c_id = $courseId AND lp_item_id = $itemId";
2912
                    $connection->query($sql);
2913
2914
                    // Update track_exercises
2915
                    $sql = "UPDATE track_e_exercises SET orig_lp_item_id = $itemIid 
2916
                            WHERE c_id = $courseId AND orig_lp_id = $oldId AND orig_lp_item_id = $itemId";
2917
                    $connection->query($sql);
2918
2919
                    // c_forum_thread
2920
                    $sql = "UPDATE c_forum_thread SET lp_item_id = $itemIid 
2921
                            WHERE c_id = $courseId AND lp_item_id = $itemId";
2922
                    $connection->query($sql);
2923
2924
                    // orig_lp_item_view_id
2925
                    $sql = "SELECT * FROM c_lp_view
2926
                            WHERE c_id = $courseId AND lp_id = $oldId";
2927
                    $result = $connection->query($sql);
2928
                    $itemViewList = $result->fetchAll();
2929
                    if ($itemViewList) {
2930
                        foreach ($itemViewList as $itemView) {
2931
                            $userId = $itemView['user_id'];
2932
                            $oldItemViewId = $itemView['id'];
2933
                            $newItemView = $itemView['iid'];
2934
2935
                            if (empty($oldItemViewId)) {
2936
                                continue;
2937
                            }
2938
2939
                            $sql = "UPDATE track_e_exercises 
2940
                                SET orig_lp_item_view_id = $newItemView 
2941
                                WHERE 
2942
                                  c_id = $courseId AND 
2943
                                  orig_lp_id = $oldId AND 
2944
                                  orig_lp_item_id = $itemIid AND 
2945
                                  orig_lp_item_view_id = $oldItemViewId AND 
2946
                                  exe_user_id = $userId                                       
2947
                                  ";
2948
                            $connection->query($sql);
2949
2950
                            /*$sql = "UPDATE c_lp_item_view
2951
                                    SET lp_view_id = $newItemView
2952
                                    WHERE
2953
                                      lp_view_id = $oldItemViewId AND
2954
                                      c_id = $courseId
2955
                                  ";
2956
                            $connection->query($sql);*/
2957
                        }
2958
                    }
2959
2960
                    $sql = "UPDATE $tblCLpItem SET lp_id = $lpIid 
2961
                            WHERE c_id = $courseId AND lp_id = $oldId AND id = $itemId";
2962
                    $connection->query($sql);
2963
2964
                    $sql = "UPDATE $tblCLpItem SET id = iid 
2965
                            WHERE c_id = $courseId AND lp_id = $oldId AND id = $itemId";
2966
                    $connection->query($sql);
2967
                }
2968
2969
                $sql = "UPDATE c_lp_view SET lp_id = $lpIid WHERE c_id = $courseId AND lp_id = $oldId";
2970
                $connection->query($sql);
2971
2972
                $sql = "UPDATE c_forum_forum SET lp_id = $lpIid WHERE c_id = $courseId AND lp_id = $oldId";
2973
                $connection->query($sql);
2974
2975
                // Update track_exercises.
2976
                $sql = "UPDATE track_e_exercises SET orig_lp_id = $lpIid 
2977
                        WHERE c_id = $courseId AND orig_lp_id = $oldId";
2978
                $connection->query($sql);
2979
2980
                $sql = "UPDATE $tblCLp SET id = iid WHERE c_id = $courseId AND id = $oldId ";
2981
                $connection->query($sql);
2982
            }
2983
        }
2984
    }
2985
2986
    if ($debug) {
2987
        error_log('END Fix lp.id lp.iids');
2988
    }
2989
}
2990
2991
/**
2992
 * @param string $distFile
2993
 * @param string $envFile
2994
 * @param array  $params
2995
 */
2996
function updateEnvFile($distFile, $envFile, $params)
2997
{
2998
    $contents = file_get_contents($distFile);
2999
    $contents = str_replace(array_keys($params), array_values($params), $contents);
3000
    file_put_contents($envFile, $contents);
3001
}
3002
3003
/**
3004
 * @param SymfonyContainer $container
3005
 * @param EntityManager    $manager
3006
 */
3007
function installGroups($container, $manager)
3008
{
3009
    // Creating fos_group (groups and roles)
3010
    $groupManager = $container->get('fos_user.group_manager');
3011
    $groups = [
3012
        [
3013
            'code' => 'ADMIN',
3014
            'title' => 'Administrators',
3015
            'roles' => ['ROLE_ADMIN'],
3016
        ],
3017
        [
3018
            'code' => 'STUDENT',
3019
            'title' => 'Students',
3020
            'roles' => ['ROLE_STUDENT'],
3021
        ],
3022
        [
3023
            'code' => 'TEACHER',
3024
            'title' => 'Teachers',
3025
            'roles' => ['ROLE_TEACHER'],
3026
        ],
3027
        [
3028
            'code' => 'RRHH',
3029
            'title' => 'Human resources manager',
3030
            'roles' => ['ROLE_RRHH'],
3031
        ],
3032
        [
3033
            'code' => 'SESSION_MANAGER',
3034
            'title' => 'Session',
3035
            'roles' => ['ROLE_SESSION_MANAGER'],
3036
        ],
3037
        [
3038
            'code' => 'QUESTION_MANAGER',
3039
            'title' => 'Question manager',
3040
            'roles' => ['ROLE_QUESTION_MANAGER'],
3041
        ],
3042
        [
3043
            'code' => 'STUDENT_BOSS',
3044
            'title' => 'Student boss',
3045
            'roles' => ['ROLE_STUDENT_BOSS'],
3046
        ],
3047
        [
3048
            'code' => 'INVITEE',
3049
            'title' => 'Invitee',
3050
            'roles' => ['ROLE_INVITEE'],
3051
        ],
3052
    ];
3053
3054
    foreach ($groups as $groupData) {
3055
        $criteria = ['code' => $groupData['code']];
3056
        $groupExists = $groupManager->findGroupBy($criteria);
3057
        if (!$groupExists) {
3058
            $group = $groupManager->createGroup($groupData['title']);
3059
            $group->setCode($groupData['code']);
3060
            foreach ($groupData['roles'] as $role) {
3061
                $group->addRole($role);
3062
            }
3063
            $manager->persist($group);
3064
            $groupManager->updateGroup($group, true);
3065
        }
3066
    }
3067
}
3068
3069
/**
3070
 * @param SymfonyContainer $container
3071
 */
3072
function installPages($container)
3073
{
3074
    $siteManager = Container::getSiteManager();
3075
3076
    // Create site
3077
    /** @var Chamilo\PageBundle\Entity\Site $site */
3078
    $site = $siteManager->create();
3079
    $site->setHost('localhost');
3080
    $site->setEnabled(true);
3081
    $site->setName('localhost');
3082
    $site->setEnabledFrom(new \DateTime('now'));
3083
    $site->setEnabledTo(new \DateTime('+20 years'));
3084
    $site->setRelativePath('');
3085
    $site->setIsDefault(true);
3086
    $site->setLocale('en');
3087
    $siteManager->save($site);
3088
3089
    // Create home page
3090
    /** @var PageManager $pageManager */
3091
    $pageManager = $container->get('sonata.page.manager.page');
3092
    /** @var \Sonata\PageBundle\Model\Page $page */
3093
    $page = $pageManager->create();
3094
    $page->setSlug('homepage');
3095
    $page->setUrl('/');
3096
    $page->setName('homepage');
3097
    $page->setTitle('home');
3098
    $page->setEnabled(true);
3099
    $page->setDecorate(1);
3100
    $page->setRequestMethod('GET|POST|HEAD|DELETE|PUT');
3101
    $page->setTemplateCode('default');
3102
    $page->setRouteName('homepage');
3103
    //$page->setParent($this->getReference('page-homepage'));
3104
    $page->setSite($site);
3105
    $pageManager->save($page);
3106
3107
    // Create welcome page
3108
    $pageWelcome = $pageManager->create();
3109
    $pageWelcome->setSlug('welcome');
3110
    $pageWelcome->setUrl('/welcome');
3111
    $pageWelcome->setName('welcome');
3112
    $pageWelcome->setTitle('welcome');
3113
    $pageWelcome->setEnabled(true);
3114
    $pageWelcome->setDecorate(1);
3115
    $pageWelcome->setRequestMethod('GET');
3116
    $pageWelcome->setTemplateCode('default');
3117
    $pageWelcome->setRouteName('welcome');
3118
    $pageWelcome->setParent($page);
3119
    $pageWelcome->setSite($site);
3120
    $pageManager->save($pageWelcome);
3121
3122
    // Creating page blocks
3123
    $templateManager = $container->get('sonata.page.template_manager');
3124
    $template = $templateManager->get('default');
3125
    $templateContainers = $template->getContainers();
3126
3127
    $containers = [];
3128
    foreach ($templateContainers as $id => $area) {
3129
        $containers[$id] = [
3130
            'area' => $area,
3131
            'block' => false,
3132
        ];
3133
    }
3134
3135
    // Create blocks for this page
3136
    $blockInteractor = $container->get('sonata.page.block_interactor');
3137
    $parentBlock = null;
3138
    foreach ($containers as $id => $area) {
3139
        if (false === $area['block'] && $templateContainers[$id]['shared'] === false) {
3140
            $block = $blockInteractor->createNewContainer(
3141
                [
3142
                    'page' => $pageWelcome,
3143
                    'name' => $templateContainers[$id]['name'],
3144
                    'code' => $id,
3145
                ]
3146
            );
3147
3148
            if ($id === 'content' && $templateContainers[$id]['name'] == 'Main content') {
3149
                $parentBlock = $block;
3150
            }
3151
        }
3152
    }
3153
3154
    // Create block in main content
3155
    $block = $container->get('sonata.page.manager.block');
3156
    /** @var \Sonata\BlockBundle\Model\Block $myBlock */
3157
    $myBlock = $block->create();
3158
    $myBlock->setType('sonata.formatter.block.formatter');
3159
    $myBlock->setSetting('format', 'richhtml');
3160
    $myBlock->setSetting('content', '');
3161
    $myBlock->setSetting('rawContent', '');
3162
    $myBlock->setSetting('template', '@SonataFormatter/Block/block_formatter.html.twig');
3163
    $myBlock->setParent($parentBlock);
3164
    $pageWelcome->addBlocks($myBlock);
3165
    $pageManager->save($pageWelcome);
3166
}
3167
3168
/**
3169
 * @param SymfonyContainer $container
3170
 * @param EntityManager    $manager
3171
 * @param bool             $upgrade
3172
 */
3173
function installSchemas($container, $manager, $upgrade = false)
3174
{
3175
    $settingsManager = Container::getSettingsManager();
3176
3177
    $accessUrl = $manager->getRepository('ChamiloCoreBundle:AccessUrl')->find(1);
3178
    if (!$accessUrl) {
3179
        // Creating AccessUrl
3180
        $accessUrl = new AccessUrl();
3181
        $accessUrl
3182
            ->setUrl('http://localhost/')
3183
            ->setDescription('')
3184
            ->setActive(1)
3185
        ;
3186
        $manager->persist($accessUrl);
3187
        $manager->flush();
3188
    }
3189
3190
    // Install course tools (table "tool")
3191
    $toolChain = $container->get('chamilo_course.tool_chain');
3192
    $toolChain->createTools($manager);
3193
3194
    if ($upgrade) {
3195
        $settingsManager->updateSchemas($accessUrl);
3196
    } else {
3197
        // Installing schemas (filling settings_current table)
3198
        $settingsManager->installSchemas($accessUrl);
3199
    }
3200
}
3201
3202
/**
3203
 * @param SymfonyContainer $container
3204
 */
3205
function upgradeWithContainer($container)
3206
{
3207
    Container::setContainer($container);
3208
    Container::setLegacyServices($container, false);
3209
    error_log('setLegacyServices');
3210
    $manager = Database::getManager();
3211
    installGroups($container, $manager);
3212
    error_log('installGroups');
3213
    installSchemas($container, $manager, true);
3214
    error_log('installSchemas');
3215
    installPages($container);
3216
    error_log('installPages');
3217
}
3218
3219
/**
3220
 * After the schema was created (table creation), the function adds
3221
 * admin/platform information.
3222
 *
3223
 * @param \Psr\Container\ContainerInterface $container
3224
 * @param string                            $sysPath
3225
 * @param string                            $encryptPassForm
3226
 * @param string                            $passForm
3227
 * @param string                            $adminLastName
3228
 * @param string                            $adminFirstName
3229
 * @param string                            $loginForm
3230
 * @param string                            $emailForm
3231
 * @param string                            $adminPhoneForm
3232
 * @param string                            $languageForm
3233
 * @param string                            $institutionForm
3234
 * @param string                            $institutionUrlForm
3235
 * @param string                            $siteName
3236
 * @param string                            $allowSelfReg
3237
 * @param string                            $allowSelfRegProf
3238
 * @param string                            $installationProfile Installation profile, if any was provided
3239
 */
3240
function finishInstallationWithContainer(
3241
    $container,
3242
    $sysPath,
3243
    $encryptPassForm,
3244
    $passForm,
3245
    $adminLastName,
3246
    $adminFirstName,
3247
    $loginForm,
3248
    $emailForm,
3249
    $adminPhoneForm,
3250
    $languageForm,
3251
    $institutionForm,
3252
    $institutionUrlForm,
3253
    $siteName,
3254
    $allowSelfReg,
3255
    $allowSelfRegProf,
3256
    $installationProfile = ''
3257
) {
3258
    $sysPath = !empty($sysPath) ? $sysPath : api_get_path(SYS_PATH);
3259
    Container::setContainer($container);
3260
    Container::setLegacyServices($container);
3261
3262
    $manager = Database::getManager();
3263
    $connection = $manager->getConnection();
3264
3265
    $sql = getVersionTable();
3266
3267
    // Add version table
3268
    $connection->executeQuery($sql);
3269
3270
    // Add tickets defaults
3271
    $ticketProject = new TicketProject();
3272
    $ticketProject
3273
        ->setId(1)
3274
        ->setName('Ticket System')
3275
        ->setInsertUserId(1);
3276
3277
    $manager->persist($ticketProject);
3278
    $manager->flush();
3279
3280
    $categories = [
3281
        get_lang('TicketEnrollment') => get_lang('TicketsAboutEnrollment'),
3282
        get_lang('TicketGeneralInformation') => get_lang('TicketsAboutGeneralInformation'),
3283
        get_lang('TicketRequestAndPapework') => get_lang('TicketsAboutRequestAndPapework'),
3284
        get_lang('TicketAcademicIncidence') => get_lang('TicketsAboutAcademicIncidence'),
3285
        get_lang('TicketVirtualCampus') => get_lang('TicketsAboutVirtualCampus'),
3286
        get_lang('TicketOnlineEvaluation') => get_lang('TicketsAboutOnlineEvaluation'),
3287
    ];
3288
3289
    $i = 1;
3290
    foreach ($categories as $category => $description) {
3291
        // Online evaluation requires a course
3292
        $ticketCategory = new TicketCategory();
3293
        $ticketCategory
3294
            ->setId($i)
3295
            ->setName($category)
3296
            ->setDescription($description)
3297
            ->setProject($ticketProject)
3298
            ->setInsertUserId(1);
3299
3300
        $isRequired = $i == 6;
3301
        $ticketCategory->setCourseRequired($isRequired);
3302
3303
        $manager->persist($ticketCategory);
3304
        $manager->flush();
3305
3306
        $i++;
3307
    }
3308
3309
    // Default Priorities
3310
    $defaultPriorities = [
3311
        TicketManager::PRIORITY_NORMAL => get_lang('PriorityNormal'),
3312
        TicketManager::PRIORITY_HIGH => get_lang('PriorityHigh'),
3313
        TicketManager::PRIORITY_LOW => get_lang('PriorityLow'),
3314
    ];
3315
3316
    $i = 1;
3317
    foreach ($defaultPriorities as $code => $priority) {
3318
        $ticketPriority = new TicketPriority();
3319
        $ticketPriority
3320
            ->setId($i)
3321
            ->setName($priority)
3322
            ->setCode($code)
3323
            ->setInsertUserId(1);
3324
3325
        $manager->persist($ticketPriority);
3326
        $manager->flush();
3327
        $i++;
3328
    }
3329
3330
    $table = Database::get_main_table(TABLE_TICKET_STATUS);
3331
3332
    // Default status
3333
    $defaultStatus = [
3334
        TicketManager::STATUS_NEW => get_lang('StatusNew'),
3335
        TicketManager::STATUS_PENDING => get_lang('StatusPending'),
3336
        TicketManager::STATUS_UNCONFIRMED => get_lang('StatusUnconfirmed'),
3337
        TicketManager::STATUS_CLOSE => get_lang('StatusClose'),
3338
        TicketManager::STATUS_FORWARDED => get_lang('StatusForwarded'),
3339
    ];
3340
3341
    $i = 1;
3342
    foreach ($defaultStatus as $code => $status) {
3343
        $attributes = [
3344
            'id' => $i,
3345
            'code' => $code,
3346
            'name' => $status,
3347
        ];
3348
        Database::insert($table, $attributes);
3349
        $i++;
3350
    }
3351
3352
    installGroups($container, $manager);
3353
    installSchemas($container, $manager);
3354
    installPages($container);
3355
3356
    // Inserting default data
3357
    $data = file_get_contents($sysPath.'main/install/data.sql');
3358
    $result = $manager->getConnection()->prepare($data);
3359
    $result->execute();
3360
    $result->closeCursor();
3361
3362
    UserManager::setPasswordEncryption($encryptPassForm);
3363
3364
    // Create admin user.
3365
    @UserManager::create_user(
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for create_user(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

3365
    /** @scrutinizer ignore-unhandled */ @UserManager::create_user(

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
3366
        $adminFirstName,
3367
        $adminLastName,
3368
        1,
3369
        $emailForm,
3370
        $loginForm,
3371
        $passForm,
3372
        'ADMIN', //$official_code = '',
3373
        $languageForm,
3374
        $adminPhoneForm,
3375
        '', //$picture_uri = '',
3376
        PLATFORM_AUTH_SOURCE,
3377
        '', //$expirationDate,
3378
        1,
3379
        0,
3380
        null,
3381
        '',
3382
        false, //$send_mail = false,
3383
        true, //$isAdmin = false
3384
        '',
3385
        false,
3386
        '',
3387
        1
3388
    );
3389
3390
    // Create anonymous user.
3391
    @UserManager::create_user(
3392
        'Joe',
3393
        'Anonymous',
3394
        6,
3395
        'anonymous@localhost',
3396
        'anon',
3397
        'anon',
3398
        'anonymous', //$official_code = '',
3399
        $languageForm,
3400
        '',
3401
        '', //$picture_uri = '',
3402
        PLATFORM_AUTH_SOURCE,
3403
        '',
3404
        1,
3405
        0,
3406
        null,
3407
        '',
3408
        false, //$send_mail = false,
3409
        false, //$isAdmin = false
3410
        '',
3411
        false,
3412
        '',
3413
        1
3414
    );
3415
3416
    // Set default language
3417
    $sql = "UPDATE language SET available = 1 WHERE dokeos_folder = '$languageForm'";
3418
    Database::query($sql);
3419
3420
    // Install settings
3421
    installSettings(
3422
        $institutionForm,
3423
        $institutionUrlForm,
3424
        $siteName,
3425
        $emailForm,
3426
        $adminLastName,
3427
        $adminFirstName,
3428
        $languageForm,
3429
        $allowSelfReg,
3430
        $allowSelfRegProf,
3431
        $installationProfile
3432
    );
3433
3434
    lockSettings();
3435
    updateDirAndFilesPermissions();
3436
3437
    // Set the latest version
3438
    /*$path = $sysPath.'app/Migrations/Schema/V111/';
3439
    $finder = new \Symfony\Component\Finder\Finder();
3440
    $files = $finder->files()->in($path);
3441
3442
    // Needed for chash
3443
    createVersionTable();
3444
3445
    foreach ($files as $version) {
3446
        $version = str_replace(['Version', '.php'], '', $version->getFilename());
3447
        $sql = "INSERT INTO version (version) VALUES ('$version')";
3448
        Database::query($sql);
3449
    }*/
3450
}
3451
3452
/**
3453
 * Creates 'version' table.
3454
 */
3455
function createVersionTable()
3456
{
3457
    $sql = getVersionTable();
3458
    Database::query($sql);
3459
}
3460
3461
/**
3462
 * Get version creation table query.
3463
 *
3464
 * @return string
3465
 */
3466
function getVersionTable()
3467
{
3468
    return 'CREATE TABLE IF NOT EXISTS version (id int unsigned NOT NULL AUTO_INCREMENT, version varchar(20), PRIMARY KEY(id), UNIQUE(version));';
3469
}
3470
3471
/**
3472
 * Update settings based on installation profile defined in a JSON file.
3473
 *
3474
 * @param string $installationProfile The name of the JSON file in main/install/profiles/ folder
3475
 *
3476
 * @return bool false on failure (no bad consequences anyway, just ignoring profile)
3477
 */
3478
function installProfileSettings($installationProfile = '')
3479
{
3480
    if (empty($installationProfile)) {
3481
        return false;
3482
    }
3483
    $jsonPath = api_get_path(SYS_PATH).'main/install/profiles/'.$installationProfile.'.json';
3484
    // Make sure the path to the profile is not hacked
3485
    if (!Security::check_abs_path($jsonPath, api_get_path(SYS_PATH).'main/install/profiles/')) {
3486
        return false;
3487
    }
3488
    if (!is_file($jsonPath)) {
3489
        return false;
3490
    }
3491
    if (!is_readable($jsonPath)) {
3492
        return false;
3493
    }
3494
    if (!function_exists('json_decode')) {
3495
        // The php-json extension is not available. Ignore profile.
3496
        return false;
3497
    }
3498
    $json = file_get_contents($jsonPath);
3499
    $params = json_decode($json);
3500
    if ($params === false or $params === null) {
3501
        return false;
3502
    }
3503
    $settings = $params->params;
3504
    if (!empty($params->parent)) {
3505
        installProfileSettings($params->parent);
3506
    }
3507
    foreach ($settings as $id => $param) {
3508
        $sql = "UPDATE settings_current
3509
                SET selected_value = '".$param->selected_value."'
3510
                WHERE variable = '".$param->variable."'";
3511
        if (!empty($param->subkey)) {
3512
            $sql .= " AND subkey='".$param->subkey."'";
3513
        }
3514
        Database::query($sql);
3515
    }
3516
3517
    return true;
3518
}
3519
3520
/**
3521
 * Quick function to remove a directory with its subdirectories.
3522
 *
3523
 * @param $dir
3524
 */
3525
function rrmdir($dir)
3526
{
3527
    if (is_dir($dir)) {
3528
        $objects = scandir($dir);
3529
        foreach ($objects as $object) {
3530
            if ($object != "." && $object != "..") {
3531
                if (filetype($dir."/".$object) == "dir") {
3532
                    @rrmdir($dir."/".$object);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for rrmdir(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

3532
                    /** @scrutinizer ignore-unhandled */ @rrmdir($dir."/".$object);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
Bug introduced by
Are you sure the usage of rrmdir($dir . '/' . $object) is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
3533
                } else {
3534
                    @unlink($dir."/".$object);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for unlink(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unhandled  annotation

3534
                    /** @scrutinizer ignore-unhandled */ @unlink($dir."/".$object);

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
3535
                }
3536
            }
3537
        }
3538
        reset($objects);
3539
        rmdir($dir);
3540
    }
3541
}
3542
3543
function get_group_picture_path_by_id($id, $type = 'web', $preview = false, $anonymous = false)
3544
{
3545
    switch ($type) {
3546
        case 'system': // Base: absolute system path.
3547
            $base = api_get_path(SYS_UPLOAD_PATH);
3548
            break;
3549
        case 'web': // Base: absolute web path.
3550
        default:
3551
            $base = api_get_path(WEB_UPLOAD_PATH);
3552
            break;
3553
    }
3554
3555
    $noPicturePath = ['dir' => $base.'img/', 'file' => 'unknown.jpg'];
3556
3557
    if (empty($id) || empty($type)) {
3558
        return $anonymous ? $noPicturePath : ['dir' => '', 'file' => ''];
3559
    }
3560
3561
    $id = intval($id);
3562
3563
    //$group_table = Database::get_main_table(TABLE_MAIN_GROUP);
3564
    $group_table = 'groups';
3565
    $sql = "SELECT picture_uri FROM $group_table WHERE id=".$id;
3566
    $res = Database::query($sql);
3567
3568
    if (!Database::num_rows($res)) {
3569
        return $anonymous ? $noPicturePath : ['dir' => '', 'file' => ''];
3570
    }
3571
3572
    $user = Database::fetch_array($res);
3573
    $picture_filename = trim($user['picture_uri']);
3574
3575
    if (api_get_setting('split_users_upload_directory') === 'true') {
3576
        if (!empty($picture_filename)) {
3577
            $dir = $base.'groups/'.substr($picture_filename, 0, 1).'/'.$id.'/';
3578
        } elseif ($preview) {
3579
            $dir = $base.'groups/'.substr((string) $id, 0, 1).'/'.$id.'/';
3580
        } else {
3581
            $dir = $base.'groups/'.$id.'/';
3582
        }
3583
    } else {
3584
        $dir = $base.'groups/'.$id.'/';
3585
    }
3586
3587
    if (empty($picture_filename) && $anonymous) {
3588
        return $noPicturePath;
3589
    }
3590
3591
    return ['dir' => $dir, 'file' => $picture_filename];
3592
}
3593
3594
/**
3595
 * Control the different steps of the migration through a big switch.
3596
 *
3597
 * @param string        $fromVersion
3598
 * @param EntityManager $manager
3599
 * @param bool          $processFiles
3600
 *
3601
 * @return bool Always returns true except if the process is broken
3602
 */
3603
function migrateSwitch($fromVersion, $manager, $processFiles = true)
3604
{
3605
    error_log('Starting migration process from '.$fromVersion.' ('.date('Y-m-d H:i:s').')');
3606
3607
    echo '<a class="btn btn-secondary" href="javascript:void(0)" id="details_button">'.get_lang('Details').'</a><br />';
3608
    echo '<div id="details" style="display:none">';
3609
3610
    $connection = $manager->getConnection();
3611
3612
    $database = new Database();
3613
    $database->setManager($manager);
3614
3615
    switch ($fromVersion) {
3616
        case '1.9.0':
3617
        case '1.9.2':
3618
        case '1.9.4':
3619
        case '1.9.6':
3620
        case '1.9.6.1':
3621
        case '1.9.8':
3622
        case '1.9.8.1':
3623
        case '1.9.8.2':
3624
        case '1.9.10':
3625
        case '1.9.10.2':
3626
        case '1.9.10.4':
3627
        case '1.9.10.6':
3628
            $database = new Database();
3629
            $database->setManager($manager);
3630
3631
            // Fix type "enum" before running the migration with Doctrine
3632
            $connection->executeQuery("ALTER TABLE course_category MODIFY COLUMN auth_course_child VARCHAR(40) DEFAULT 'TRUE'");
3633
            $connection->executeQuery("ALTER TABLE course_category MODIFY COLUMN auth_cat_child VARCHAR(40) DEFAULT 'TRUE'");
3634
            $connection->executeQuery("ALTER TABLE c_quiz_answer MODIFY COLUMN hotspot_type varchar(40) default NULL");
3635
            $connection->executeQuery("ALTER TABLE c_tool MODIFY COLUMN target varchar(20) NOT NULL default '_self'");
3636
            $connection->executeQuery("ALTER TABLE c_link MODIFY COLUMN on_homepage char(10) NOT NULL default '0'");
3637
            $connection->executeQuery("ALTER TABLE c_blog_rating MODIFY COLUMN rating_type char(40) NOT NULL default 'post'");
3638
            $connection->executeQuery("ALTER TABLE c_survey MODIFY COLUMN anonymous char(10) NOT NULL default '0'");
3639
            $connection->executeQuery("ALTER TABLE c_document MODIFY COLUMN filetype char(10) NOT NULL default 'file'");
3640
            $connection->executeQuery("ALTER TABLE c_student_publication MODIFY COLUMN filetype char(10) NOT NULL default 'file'");
3641
3642
            // Migrate using the migration files located in:
3643
            // src/Chamilo/CoreBundle/Migrations/Schema/V110
3644
            $result = migrate(
3645
                110,
3646
                $manager
3647
            );
3648
3649
            if ($result) {
3650
                error_log('Migrations files were executed ('.date('Y-m-d H:i:s').')');
3651
                fixIds($manager);
3652
                error_log('fixIds finished ('.date('Y-m-d H:i:s').')');
3653
3654
                $connection->executeQuery("UPDATE settings_current SET selected_value = '1.10.0' WHERE variable = 'chamilo_database_version'");
3655
3656
                if ($processFiles) {
3657
                    $fromVersionShort = '1.9';
3658
                    include __DIR__.'/update-files-1.9.0-1.10.0.inc.php';
3659
                    // Only updates the configuration.inc.php with the new version
3660
                    include __DIR__.'/update-configuration.inc.php';
3661
                }
3662
3663
                error_log('Upgrade 1.10.x process concluded! ('.date('Y-m-d H:i:s').')');
3664
            } else {
3665
                error_log('There was an error during running migrations. Check error.log');
3666
                break;
3667
            }
3668
            // no break
3669
        case '1.10.0':
3670
        case '1.10.2':
3671
        case '1.10.4':
3672
        case '1.10.6':
3673
        case '1.10.8':
3674
        case '1.10.8':
3675
            $database = new Database();
3676
            $database->setManager($manager);
3677
            // Migrate using the migration files located in:
3678
            // src/Chamilo/CoreBundle/Migrations/Schema/V111
3679
            $result = migrate(111, $manager);
3680
3681
            if ($result) {
3682
                error_log('Migrations files were executed ('.date('Y-m-d H:i:s').')');
3683
3684
                fixPostGroupIds($connection);
3685
3686
                $sql = "UPDATE settings_current SET selected_value = '1.11.0' WHERE variable = 'chamilo_database_version'";
3687
                $connection->executeQuery($sql);
3688
                if ($processFiles) {
3689
                    error_log('Update config files');
3690
                    $fromVersionShort = '1.10';
3691
                    include __DIR__.'/update-files-1.10.0-1.11.0.inc.php';
3692
                    // Only updates the configuration.inc.php with the new version
3693
                    include __DIR__.'/update-configuration.inc.php';
3694
                }
3695
                error_log('Upgrade 1.11.x process concluded!  ('.date('Y-m-d H:i:s').')');
3696
            } else {
3697
                error_log('There was an error during running migrations. Check error.log');
3698
            }
3699
            // no break
3700
        case '1.11.0':
3701
        case '1.11.1':
3702
        case '1.11.2':
3703
        case '1.11.4':
3704
        case '1.11.6':
3705
        case '1.11.8':
3706
        case '1.11.10':
3707
            $database = new Database();
3708
            $database->setManager($manager);
3709
            // Migrate using the migration files located in:
3710
            // src/Chamilo/CoreBundle/Migrations/Schema/V111
3711
            $result = migrate(200, $manager);
3712
3713
            if ($result) {
3714
                error_log('Migrations files were executed ('.date('Y-m-d H:i:s').')');
3715
                $sql = "UPDATE settings_current SET selected_value = '2.0.0' WHERE variable = 'chamilo_database_version'";
3716
                $connection->executeQuery($sql);
3717
                if ($processFiles) {
3718
                    error_log('Update config files');
3719
                    $fromVersionShort = '1.11';
3720
                    include __DIR__.'/update-files-1.11.0-2.0.0.inc.php';
3721
                    // Only updates the configuration.inc.php with the new version
3722
                    include __DIR__.'/update-configuration.inc.php';
3723
                }
3724
                error_log('Upgrade 2.0.0 process concluded!  ('.date('Y-m-d H:i:s').')');
3725
            } else {
3726
                error_log('There was an error during running migrations. Check error.log');
3727
            }
3728
            break;
3729
        default:
3730
            break;
3731
    }
3732
3733
    echo '</div>';
3734
3735
    return true;
3736
}
3737
3738
/**
3739
 * @param \Doctrine\DBAL\Connection $connection
3740
 */
3741
function fixPostGroupIds($connection)
3742
{
3743
    $connection->executeQuery("ALTER TABLE course_category MODIFY COLUMN auth_course_child VARCHAR(40) DEFAULT 'TRUE'");
3744
    error_log('Fix c_student_publication.post_group_id');
3745
3746
    // Fix post_group_id
3747
    $sql = "SELECT * FROM c_student_publication 
3748
            WHERE (post_group_id <> 0 AND post_group_id is not null)";
3749
    $statement = $connection->executeQuery($sql);
3750
    $result = $statement->fetchAll();
3751
3752
    foreach ($result as $row) {
3753
        $groupId = $row['post_group_id'];
3754
        $courseId = $row['c_id'];
3755
        $workIid = $row['iid'];
3756
        $sql = "SELECT iid from c_group_info 
3757
                WHERE c_id = $courseId AND id = $groupId";
3758
        $statement = $connection->executeQuery($sql);
3759
        $count = $statement->rowCount();
3760
        if ($count > 0) {
3761
            $rowGroup = $statement->fetch();
3762
            $newGroupId = $rowGroup['iid'];
3763
            if ($newGroupId == $groupId) {
3764
                continue;
3765
            }
3766
            if ($newGroupId) {
3767
                $sql = "UPDATE c_student_publication 
3768
                        SET post_group_id = $newGroupId 
3769
                        WHERE 
3770
                            c_id = $courseId AND
3771
                            iid = $workIid
3772
                        ";
3773
                $connection->executeQuery($sql);
3774
            }
3775
        }
3776
    }
3777
3778
    error_log('End - Fix c_student_publication.post_group_id');
3779
3780
    // Delete c_student_publication from any session that doesn't exist anymore
3781
    $sql = "DELETE FROM c_student_publication 
3782
            WHERE session_id NOT IN (SELECT id FROM session) AND (session_id <> 0 AND session_id is not null)";
3783
    $connection->executeQuery($sql);
3784
3785
    error_log('Fix work documents');
3786
    // Fix work documents that don't have c_item_property value
3787
    $sql = "SELECT * FROM c_student_publication WHERE parent_id IS NOT NULL";
3788
    $statement = $connection->executeQuery($sql);
3789
    $result = $statement->fetchAll();
3790
    foreach ($result as $row) {
3791
        $groupId = $row['post_group_id'];
3792
        $courseId = $row['c_id'];
3793
        $sessionId = $row['session_id'];
3794
        $workId = $row['id'];
3795
        $itemInfo = api_get_item_property_info(
3796
            $courseId,
3797
            'work',
3798
            $workId,
3799
            $sessionId
3800
        );
3801
        $courseInfo = api_get_course_info_by_id($courseId);
3802
        if (empty($itemInfo)) {
3803
            api_item_property_update(
3804
                $courseInfo,
3805
                'work',
3806
                $workId,
3807
                'visible',
3808
                1,
3809
                $groupId,
3810
                null,
3811
                null,
3812
                null,
3813
                $sessionId
3814
            );
3815
        }
3816
    }
3817
    error_log('End - Fix work documents');
3818
}
3819