Passed
Push — master ( 8cd85a...5627cd )
by Julito
09:53 queued 10s
created

generateRandomToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 1
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('Extension installed but not enabled'), 'warning');
89
                } else {
90
                    return Display::label(get_lang('Extension installed but not enabled'), '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 false|mixed 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
 * Returns a textual value ('ON' or 'OFF') based on a requester 2-state ini- configuration setting.
150
 *
151
 * @param string $val a php ini value
152
 *
153
 * @return bool ON or OFF
154
 *
155
 * @author Joomla <http://www.joomla.org>
156
 */
157
function getPhpSetting($val)
158
{
159
    $value = ini_get($val);
160
    switch ($val) {
161
        case 'display_errors':
162
            global $originalDisplayErrors;
163
            $value = $originalDisplayErrors;
164
            break;
165
    }
166
167
    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...
168
}
169
170
/**
171
 * This function returns a string "true" or "false" according to the passed parameter.
172
 *
173
 * @param int $var The variable to present as text
174
 *
175
 * @return string the string "true" or "false"
176
 *
177
 * @author Christophe Gesch??
178
 */
179
function trueFalse($var)
180
{
181
    return $var ? 'true' : 'false';
182
}
183
184
/**
185
 * Removes memory and time limits as much as possible.
186
 */
187
function remove_memory_and_time_limits()
188
{
189
    if (function_exists('ini_set')) {
190
        ini_set('memory_limit', -1);
191
        ini_set('max_execution_time', 0);
192
        error_log('Update-db script: memory_limit set to -1', 0);
193
        error_log('Update-db script: max_execution_time 0', 0);
194
    } else {
195
        error_log('Update-db script: could not change memory and time limits', 0);
196
    }
197
}
198
199
/**
200
 * Detects browser's language.
201
 *
202
 * @return string Returns a language identificator, i.e. 'english', 'spanish', ...
203
 *
204
 * @author Ivan Tcholakov, 2010
205
 */
206
function detect_browser_language()
207
{
208
    static $language_index = [
209
        'ar' => 'arabic',
210
        'ast' => 'asturian',
211
        'bg' => 'bulgarian',
212
        'bs' => 'bosnian',
213
        'ca' => 'catalan',
214
        'zh' => 'simpl_chinese',
215
        'zh-tw' => 'trad_chinese',
216
        'cs' => 'czech',
217
        'da' => 'danish',
218
        'prs' => 'dari',
219
        'de' => 'german',
220
        'el' => 'greek',
221
        'en' => 'english',
222
        'es' => 'spanish',
223
        'eo' => 'esperanto',
224
        'eu' => 'basque',
225
        'fa' => 'persian',
226
        'fr' => 'french',
227
        'fur' => 'friulian',
228
        'gl' => 'galician',
229
        'ka' => 'georgian',
230
        'hr' => 'croatian',
231
        'he' => 'hebrew',
232
        'hi' => 'hindi',
233
        'id' => 'indonesian',
234
        'it' => 'italian',
235
        'ko' => 'korean',
236
        'lv' => 'latvian',
237
        'lt' => 'lithuanian',
238
        'mk' => 'macedonian',
239
        'hu' => 'hungarian',
240
        'ms' => 'malay',
241
        'nl' => 'dutch',
242
        'ja' => 'japanese',
243
        'no' => 'norwegian',
244
        'oc' => 'occitan',
245
        'ps' => 'pashto',
246
        'pl' => 'polish',
247
        'pt' => 'portuguese',
248
        'pt-br' => 'brazilian',
249
        'ro' => 'romanian',
250
        'qu' => 'quechua_cusco',
251
        'ru' => 'russian',
252
        'sk' => 'slovak',
253
        'sl' => 'slovenian',
254
        'sr' => 'serbian',
255
        'fi' => 'finnish',
256
        'sv' => 'swedish',
257
        'th' => 'thai',
258
        'tr' => 'turkish',
259
        'uk' => 'ukrainian',
260
        'vi' => 'vietnamese',
261
        'sw' => 'swahili',
262
        'yo' => 'yoruba',
263
    ];
264
265
    $system_available_languages = get_language_folder_list();
266
    $accept_languages = strtolower(str_replace('_', '-', $_SERVER['HTTP_ACCEPT_LANGUAGE']));
267
    foreach ($language_index as $code => $language) {
268
        if (strpos($accept_languages, $code) === 0) {
269
            if (!empty($system_available_languages[$language])) {
270
                return $language;
271
            }
272
        }
273
    }
274
275
    $user_agent = strtolower(str_replace('_', '-', $_SERVER['HTTP_USER_AGENT']));
276
    foreach ($language_index as $code => $language) {
277
        if (@preg_match("/[\[\( ]{$code}[;,_\-\)]/", $user_agent)) {
278
            if (!empty($system_available_languages[$language])) {
279
                return $language;
280
            }
281
        }
282
    }
283
284
    return 'english';
285
}
286
287
/*      FILESYSTEM RELATED FUNCTIONS */
288
289
/**
290
 * This function checks if the given folder is writable.
291
 *
292
 * @param string $folder     Full path to a folder
293
 * @param bool   $suggestion Whether to show a suggestion or not
294
 *
295
 * @return string
296
 */
297
function check_writable($folder, $suggestion = false)
298
{
299
    if (is_writable($folder)) {
300
        return Display::label(get_lang('Writable'), 'success');
301
    } else {
302
        if ($suggestion) {
303
            return Display::label(get_lang('Not writable'), 'info');
304
        } else {
305
            return Display::label(get_lang('Not writable'), 'important');
306
        }
307
    }
308
}
309
310
/**
311
 * This function checks if the given folder is readable.
312
 *
313
 * @param string $folder     Full path to a folder
314
 * @param bool   $suggestion Whether to show a suggestion or not
315
 *
316
 * @return string
317
 */
318
function checkReadable($folder, $suggestion = false)
319
{
320
    if (is_readable($folder)) {
321
        return Display::label(get_lang('Readable'), 'success');
322
    } else {
323
        if ($suggestion) {
324
            return Display::label(get_lang('Not readable'), 'info');
325
        } else {
326
            return Display::label(get_lang('Not readable'), 'important');
327
        }
328
    }
329
}
330
331
/**
332
 * This function is similar to the core file() function, except that it
333
 * works with line endings in Windows (which is not the case of file()).
334
 *
335
 * @param string $filename
336
 *
337
 * @return array The lines of the file returned as an array
338
 */
339
function file_to_array($filename)
340
{
341
    if (!is_readable($filename) || is_dir($filename)) {
342
        return [];
343
    }
344
    $fp = fopen($filename, 'rb');
345
    $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

345
    $buffer = fread(/** @scrutinizer ignore-type */ $fp, filesize($filename));
Loading history...
346
    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

346
    fclose(/** @scrutinizer ignore-type */ $fp);
Loading history...
347
348
    return explode('<br />', nl2br($buffer));
349
}
350
351
/**
352
 * We assume this function is called from install scripts that reside inside the install folder.
353
 */
354
function set_file_folder_permissions()
355
{
356
    @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

356
    /** @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...
357
    @chmod('..', 0755); //set permissions on parent dir of install dir
358
}
359
360
/**
361
 * Write the main system config file.
362
 *
363
 * @param string $path Path to the config file
364
 */
365
function write_system_config_file($path)
366
{
367
    global $dbHostForm;
368
    global $dbPortForm;
369
    global $dbUsernameForm;
370
    global $dbPassForm;
371
    global $dbNameForm;
372
    global $urlForm;
373
    global $pathForm;
374
    global $urlAppendPath;
375
    global $languageForm;
376
    global $encryptPassForm;
377
    global $session_lifetime;
378
    global $new_version;
379
    global $new_version_stable;
380
381
    $root_sys = api_add_trailing_slash(str_replace('\\', '/', realpath($pathForm)));
382
    $content = file_get_contents(__DIR__.'/'.SYSTEM_CONFIG_FILENAME);
383
384
    $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...
385
    $config['{DATABASE_HOST}'] = $dbHostForm;
386
    $config['{DATABASE_PORT}'] = $dbPortForm;
387
    $config['{DATABASE_USER}'] = $dbUsernameForm;
388
    $config['{DATABASE_PASSWORD}'] = $dbPassForm;
389
    $config['{DATABASE_MAIN}'] = $dbNameForm;
390
    $config['{ROOT_WEB}'] = $urlForm;
391
    $config['{ROOT_SYS}'] = $root_sys;
392
    $config['{URL_APPEND_PATH}'] = $urlAppendPath;
393
    $config['{PLATFORM_LANGUAGE}'] = $languageForm;
394
    $config['{SECURITY_KEY}'] = md5(uniqid(rand().time()));
395
    $config['{ENCRYPT_PASSWORD}'] = $encryptPassForm;
396
397
    $config['SESSION_LIFETIME'] = $session_lifetime;
398
    $config['{NEW_VERSION}'] = $new_version;
399
    $config['NEW_VERSION_STABLE'] = trueFalse($new_version_stable);
400
401
    foreach ($config as $key => $value) {
402
        $content = str_replace($key, $value, $content);
403
    }
404
    $fp = @fopen($path, 'w');
405
406
    if (!$fp) {
0 ignored issues
show
introduced by
$fp is of type false|resource, thus it always evaluated to false.
Loading history...
407
        echo '<strong>
408
                <font color="red">Your script doesn\'t have write access to the config directory</font></strong><br />
409
                <em>('.str_replace('\\', '/', realpath($path)).')</em><br /><br />
410
                You probably do not have write access on Chamilo root directory,
411
                i.e. you should <em>CHMOD 777</em> or <em>755</em> or <em>775</em>.<br /><br />
412
                Your problems can be related on two possible causes:<br />
413
                <ul>
414
                  <li>Permission problems.<br />Try initially with <em>chmod -R 777</em> and increase restrictions gradually.</li>
415
                  <li>PHP is running in <a href="http://www.php.net/manual/en/features.safe-mode.php" target="_blank">Safe-Mode</a>. 
416
                  If possible, try to switch it off.</li>
417
                </ul>
418
                <a href="http://forum.chamilo.org/" target="_blank">Read about this problem in Support Forum</a><br /><br />
419
                Please go back to step 5.
420
                <p><input type="submit" name="step5" value="&lt; Back" /></p>
421
                </td></tr></table></form></body></html>';
422
        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...
423
    }
424
425
    fwrite($fp, $content);
426
    fclose($fp);
427
}
428
429
/**
430
 * Returns a list of language directories.
431
 */
432
function get_language_folder_list()
433
{
434
    return [
435
        'ar' => 'arabic',
436
        'ast' => 'asturian',
437
        'bg' => 'bulgarian',
438
        'bs' => 'bosnian',
439
        'ca' => 'catalan',
440
        'zh' => 'simpl_chinese',
441
        'zh-tw' => 'trad_chinese',
442
        'cs' => 'czech',
443
        'da' => 'danish',
444
        'prs' => 'dari',
445
        'de' => 'german',
446
        'el' => 'greek',
447
        'en' => 'english',
448
        'es' => 'spanish',
449
        'eo' => 'esperanto',
450
        'eu' => 'basque',
451
        'fa' => 'persian',
452
        'fr' => 'french',
453
        'fur' => 'friulian',
454
        'gl' => 'galician',
455
        'ka' => 'georgian',
456
        'hr' => 'croatian',
457
        'he' => 'hebrew',
458
        'hi' => 'hindi',
459
        'id' => 'indonesian',
460
        'it' => 'italian',
461
        'ko' => 'korean',
462
        'lv' => 'latvian',
463
        'lt' => 'lithuanian',
464
        'mk' => 'macedonian',
465
        'hu' => 'hungarian',
466
        'ms' => 'malay',
467
        'nl' => 'dutch',
468
        'ja' => 'japanese',
469
        'no' => 'norwegian',
470
        'oc' => 'occitan',
471
        'ps' => 'pashto',
472
        'pl' => 'polish',
473
        'pt' => 'portuguese',
474
        'pt-br' => 'brazilian',
475
        'ro' => 'romanian',
476
        'qu' => 'quechua_cusco',
477
        'ru' => 'russian',
478
        'sk' => 'slovak',
479
        'sl' => 'slovenian',
480
        'sr' => 'serbian',
481
        'fi' => 'finnish',
482
        'sv' => 'swedish',
483
        'th' => 'thai',
484
        'tr' => 'turkish',
485
        'uk' => 'ukrainian',
486
        'vi' => 'vietnamese',
487
        'sw' => 'swahili',
488
        'yo' => 'yoruba',
489
    ];
490
}
491
492
/**
493
 * This function returns the value of a parameter from the configuration file.
494
 *
495
 * WARNING - this function relies heavily on global variables $updateFromConfigFile
496
 * and $configFile, and also changes these globals. This can be rewritten.
497
 *
498
 * @param string $param      the parameter of which the value is returned
499
 * @param string $updatePath If we want to give the path rather than take it from POST
500
 *
501
 * @return string the value of the parameter
502
 *
503
 * @author Olivier Brouckaert
504
 * @author Reworked by Ivan Tcholakov, 2010
505
 */
506
function get_config_param($param, $updatePath = '')
507
{
508
    global $configFile, $updateFromConfigFile;
509
510
    // Look if we already have the queried parameter.
511
    if (is_array($configFile) && isset($configFile[$param])) {
512
        return $configFile[$param];
513
    }
514
    if (empty($updatePath) && !empty($_POST['updatePath'])) {
515
        $updatePath = $_POST['updatePath'];
516
    }
517
518
    if (empty($updatePath)) {
519
        $updatePath = api_get_path(SYS_PATH);
520
    }
521
    $updatePath = api_add_trailing_slash(str_replace('\\', '/', realpath($updatePath)));
522
    $updateFromInstalledVersionFile = '';
523
524
    if (empty($updateFromConfigFile)) {
525
        // If update from previous install was requested,
526
        // try to recover config file from Chamilo 1.9.x
527
        if (file_exists($updatePath.'main/inc/conf/configuration.php')) {
528
            $updateFromConfigFile = 'main/inc/conf/configuration.php';
529
        } elseif (file_exists($updatePath.'app/config/configuration.php')) {
530
            $updateFromConfigFile = 'app/config/configuration.php';
531
        } elseif (file_exists($updatePath.'config/configuration.php')) {
532
            $updateFromConfigFile = 'config/configuration.php';
533
        } else {
534
            // Give up recovering.
535
            //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);
536
            return null;
537
        }
538
    }
539
540
    if (file_exists($updatePath.$updateFromConfigFile) &&
541
        !is_dir($updatePath.$updateFromConfigFile)
542
    ) {
543
        require $updatePath.$updateFromConfigFile;
544
        $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...
545
546
        return $config->get($param);
547
    }
548
549
    error_log('Config array could not be found in get_config_param()', 0);
550
551
    return null;
552
}
553
554
/*      DATABASE RELATED FUNCTIONS */
555
556
/**
557
 * Gets a configuration parameter from the database. Returns returns null on failure.
558
 *
559
 * @param string $param Name of param we want
560
 *
561
 * @return mixed The parameter value or null if not found
562
 */
563
function get_config_param_from_db($param = '')
564
{
565
    $param = Database::escape_string($param);
566
567
    if (($res = Database::query("SELECT * FROM settings_current WHERE variable = '$param'")) !== false) {
568
        if (Database::num_rows($res) > 0) {
569
            $row = Database::fetch_array($res);
570
571
            return $row['selected_value'];
572
        }
573
    }
574
575
    return null;
576
}
577
578
/**
579
 * Connect to the database and returns the entity manager.
580
 *
581
 * @param string $dbHostForm     DB host
582
 * @param string $dbUsernameForm DB username
583
 * @param string $dbPassForm     DB password
584
 * @param string $dbNameForm     DB name
585
 * @param int    $dbPortForm     DB port
586
 *
587
 * @return \Database
588
 */
589
function connectToDatabase(
590
    $dbHostForm,
591
    $dbUsernameForm,
592
    $dbPassForm,
593
    $dbNameForm,
594
    $dbPortForm = 3306
595
) {
596
    $dbParams = [
597
        'driver' => 'pdo_mysql',
598
        'host' => $dbHostForm,
599
        'port' => $dbPortForm,
600
        'user' => $dbUsernameForm,
601
        'password' => $dbPassForm,
602
        'dbname' => $dbNameForm,
603
    ];
604
605
    $database = new \Database();
606
    $database->connect($dbParams);
607
608
    return $database;
609
}
610
611
/*      DISPLAY FUNCTIONS */
612
613
/**
614
 * This function prints class=active_step $current_step=$param.
615
 *
616
 * @param int $param A step in the installer process
617
 *
618
 * @author Patrick Cool <[email protected]>, Ghent University
619
 */
620
function step_active($param)
621
{
622
    global $current_step;
623
    if ($param == $current_step) {
624
        echo 'active';
625
    }
626
}
627
628
/**
629
 * This function displays the Step X of Y -.
630
 *
631
 * @return string String that says 'Step X of Y' with the right values
632
 */
633
function display_step_sequence()
634
{
635
    global $current_step;
636
637
    return get_lang('Step'.$current_step).' &ndash; ';
638
}
639
640
/**
641
 * Displays a drop down box for selection the preferred language.
642
 */
643
function display_language_selection_box(
644
    $name = 'language_list',
0 ignored issues
show
Unused Code introduced by
The parameter $name 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

644
    /** @scrutinizer ignore-unused */ $name = 'language_list',

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...
645
    $default_language = 'english'
646
) {
647
    // Reading language list.
648
    $language_list = get_language_folder_list();
649
650
    // Sanity checks due to the possibility for customizations.
651
    if (!is_array($language_list) || empty($language_list)) {
652
        $language_list = ['en' => 'English'];
653
    }
654
655
    // Sorting again, if it is necessary.
656
    //asort($language_list);
657
658
    // More sanity checks.
659
    if (!array_key_exists($default_language, $language_list)) {
660
        if (array_key_exists('en', $language_list)) {
661
            $default_language = 'en';
662
        } else {
663
            $language_keys = array_keys($language_list);
664
            $default_language = $language_keys[0];
665
        }
666
    }
667
668
    // Displaying the box.
669
670
    $html = Display::select(
671
        'language_list',
672
        $language_list,
673
        $default_language,
674
        ['class' => 'form-control selectpicker show-tick form-control'],
675
        false
676
    );
677
678
    return $html;
679
}
680
681
/**
682
 * This function displays a language dropdown box so that the installatioin
683
 * can be done in the language of the user.
684
 */
685
function display_language_selection()
686
{
687
    ?>
688
        <div class="install-icon">
689
            <img width="150px;" src="chamilo-install.svg"/>
690
        </div>
691
        <h2 class="install-title">
692
            <?php echo display_step_sequence(); ?>
693
            <?php echo get_lang('Installation Language'); ?>
694
        </h2>
695
        <label for="language_list"><?php echo get_lang('Please select installation language'); ?></label>
696
        <div class="form-group">
697
            <?php echo display_language_selection_box('language_list', api_get_interface_language()); ?>
698
        </div>
699
        <button type="submit" name="step1" class="btn btn-success" value="<?php echo get_lang('Next'); ?>">
700
            <em class="fa fa-forward"> </em>
701
            <?php echo get_lang('Next'); ?>
702
        </button>
703
        <input type="hidden" name="is_executable" id="is_executable" value="-" />
704
        <div class="RequirementHeading">
705
            <?php echo get_lang('Cannot find your language in the list? Contact us at [email protected] to contribute as a translator.'); ?>
706
        </div>
707
<?php
708
}
709
710
/**
711
 * This function displays the requirements for installing Chamilo.
712
 *
713
 * @param string $installType
714
 * @param bool   $badUpdatePath
715
 * @param bool   $badUpdatePath
716
 * @param string $updatePath            The updatePath given (if given)
717
 * @param array  $update_from_version_8 The different subversions from version 1.9
718
 *
719
 * @author unknow
720
 * @author Patrick Cool <[email protected]>, Ghent University
721
 */
722
function display_requirements(
723
    $installType,
724
    $badUpdatePath,
725
    $updatePath = '',
726
    $update_from_version_8 = []
727
) {
728
    global $_setting, $originalMemoryLimit;
729
730
    $dir = api_get_path(SYS_ARCHIVE_PATH).'temp/';
731
    $fileToCreate = 'test';
732
733
    $perms_dir = [0777, 0755, 0775, 0770, 0750, 0700];
734
    $perms_fil = [0666, 0644, 0664, 0660, 0640, 0600];
735
    $course_test_was_created = false;
736
    $dir_perm_verified = 0777;
737
738
    foreach ($perms_dir as $perm) {
739
        $r = @mkdir($dir, $perm);
740
        if ($r === true) {
741
            $dir_perm_verified = $perm;
742
            $course_test_was_created = true;
743
            break;
744
        }
745
    }
746
747
    $fil_perm_verified = 0666;
748
    $file_course_test_was_created = false;
749
    if (is_dir($dir)) {
750
        foreach ($perms_fil as $perm) {
751
            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...
752
                break;
753
            }
754
            $r = @touch($dir.'/'.$fileToCreate, $perm);
755
            if ($r === true) {
756
                $fil_perm_verified = $perm;
757
                $file_course_test_was_created = true;
758
            }
759
        }
760
    }
761
762
    @unlink($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

762
    /** @scrutinizer ignore-unhandled */ @unlink($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...
763
    @rmdir($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

763
    /** @scrutinizer ignore-unhandled */ @rmdir($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...
764
765
    echo '<h2 class="install-title">'.display_step_sequence().get_lang('Requirements')."</h2>";
766
    echo '<div class="RequirementText">';
767
    echo '<strong>'.get_lang('Please read the following requirements thoroughly.').'</strong><br />';
768
    echo get_lang('For more details').' <a href="../../documentation/installation_guide.html" target="_blank">'.get_lang('Read the installation guide').'</a>.<br />'."\n";
769
770
    if ($installType == 'update') {
771
        echo get_lang('If you plan to upgrade from an older version of Chamilo, you might want to <a href="../../documentation/changelog.html" target="_blank">have a look at the changelog</a> to know what\'s new and what has been changed').'<br />';
772
    }
773
    echo '</div>';
774
775
    //  SERVER REQUIREMENTS
776
    echo '<h4 class="install-subtitle">'.get_lang('Server requirements').'</h4>';
777
    $timezone = checkPhpSettingExists('date.timezone');
778
    if (!$timezone) {
779
        echo "<div class='alert alert-warning'>
780
            <i class=\"fa fa-exclamation-triangle\" aria-hidden=\"true\"></i>&nbsp;".
781
            get_lang('We have detected that your PHP installation does not define the date.timezone setting. This is a requirement of Chamilo. Please make sure it is configured by checking your php.ini configuration, otherwise you will run into problems. We warned you!')."</div>";
782
    }
783
784
    echo '<div class="install-requirement">'.get_lang('Server requirementsInfo').'</div>';
785
    echo '<div class="table-responsive">';
786
    echo '<table class="table table-bordered">
787
            <tr>
788
                <td class="requirements-item">'.get_lang('PHP version').' >= '.REQUIRED_PHP_VERSION.'</td>
789
                <td class="requirements-value">';
790
    if (version_compare(phpversion(), REQUIRED_PHP_VERSION, '>=') > 1) {
791
        echo '<strong class="text-danger">'.get_lang('PHP versionError').'</strong>';
792
    } else {
793
        echo '<strong class="text-success">'.get_lang('PHP versionOK').' '.phpversion().'</strong>';
794
    }
795
    echo '</td>
796
            </tr>
797
            <tr>
798
                <td class="requirements-item"><a href="http://php.net/manual/en/book.session.php" target="_blank">Session</a> '.get_lang('Support').'</td>
799
                <td class="requirements-value">'.checkExtension('session', get_lang('Yes'), get_lang('Sessions extension not available')).'</td>
800
            </tr>
801
            <tr>
802
                <td class="requirements-item"><a href="http://php.net/manual/en/book.mysql.php" target="_blank">pdo_mysql</a> '.get_lang('Support').'</td>
803
                <td class="requirements-value">'.checkExtension('pdo_mysql', get_lang('Yes'), get_lang('MySQL extension not available')).'</td>
804
            </tr>
805
            <tr>
806
                <td class="requirements-item"><a href="http://php.net/manual/en/book.zip.php" target="_blank">Zip</a> '.get_lang('Support').'</td>
807
                <td class="requirements-value">'.checkExtension('zip', get_lang('Yes'), get_lang('Extension not available')).'</td>
808
            </tr>
809
            <tr>
810
                <td class="requirements-item"><a href="http://php.net/manual/en/book.zlib.php" target="_blank">Zlib</a> '.get_lang('Support').'</td>
811
                <td class="requirements-value">'.checkExtension('zlib', get_lang('Yes'), get_lang('Zlib extension not available')).'</td>
812
            </tr>
813
            <tr>
814
                <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>
815
                <td class="requirements-value">'.checkExtension('pcre', get_lang('Yes'), get_lang('PCRE extension not available')).'</td>
816
            </tr>
817
            <tr>
818
                <td class="requirements-item"><a href="http://php.net/manual/en/book.xml.php" target="_blank">XML</a> '.get_lang('Support').'</td>
819
                <td class="requirements-value">'.checkExtension('xml', get_lang('Yes'), get_lang('No')).'</td>
820
            </tr>
821
            <tr>
822
                <td class="requirements-item"><a href="http://php.net/manual/en/book.intl.php" target="_blank">Internationalization</a> '.get_lang('Support').'</td>
823
                <td class="requirements-value">'.checkExtension('intl', get_lang('Yes'), get_lang('No')).'</td>
824
            </tr>
825
               <tr>
826
                <td class="requirements-item"><a href="http://php.net/manual/en/book.json.php" target="_blank">JSON</a> '.get_lang('Support').'</td>
827
                <td class="requirements-value">'.checkExtension('json', get_lang('Yes'), get_lang('No')).'</td>
828
            </tr>
829
             <tr>
830
                <td class="requirements-item"><a href="http://php.net/manual/en/book.image.php" target="_blank">GD</a> '.get_lang('Support').'</td>
831
                <td class="requirements-value">'.checkExtension('gd', get_lang('Yes'), get_lang('GD Extension not available')).'</td>
832
            </tr>
833
            <tr>
834
                <td class="requirements-item"><a href="http://php.net/manual/en/book.curl.php" target="_blank">cURL</a>'.get_lang('Support').'</td>
835
                <td class="requirements-value">'.checkExtension('curl', get_lang('Yes'), get_lang('No')).'</td>
836
            </tr>
837
838
            <tr>
839
                <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>
840
                <td class="requirements-value">'.checkExtension('mbstring', get_lang('Yes'), get_lang('MBString extension not available'), true).'</td>
841
            </tr>
842
            <tr>
843
                <td class="requirements-item"><a href="http://php.net/opcache" target="_blank">Zend OpCache</a> '.get_lang('Support').' ('.get_lang('Optional').')</td>
844
                <td class="requirements-value">'.checkExtension('Zend OPcache', get_lang('Yes'), get_lang('No'), true, 'opcache.enable').'</td>
845
            </tr>
846
            <tr>
847
                <td class="requirements-item"><a href="http://php.net/apcu" target="_blank">APCu</a> '.get_lang('Support').' ('.get_lang('Optional').')</td>
848
                <td class="requirements-value">'.checkExtension('apcu', get_lang('Yes'), get_lang('No'), true, 'apc.enabled').'</td>
849
            </tr>
850
            <tr>
851
                <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>
852
                <td class="requirements-value">'.checkExtension('iconv', get_lang('Yes'), get_lang('No'), true).'</td>
853
            </tr>
854
            <tr>
855
                <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>
856
                <td class="requirements-value">'.checkExtension('ldap', get_lang('Yes'), get_lang('LDAP Extension not available'), true).'</td>
857
            </tr>
858
            <tr>
859
                <td class="requirements-item"><a href="http://xapian.org/" target="_blank">Xapian</a> '.get_lang('Support').' ('.get_lang('Optional').')</td>
860
                <td class="requirements-value">'.checkExtension('xapian', get_lang('Yes'), get_lang('No'), true).'</td>
861
            </tr>
862
        </table>';
863
    echo '</div>';
864
865
    // RECOMMENDED SETTINGS
866
    // Note: these are the settings for Joomla, does this also apply for Chamilo?
867
    // Note: also add upload_max_filesize here so that large uploads are possible
868
    echo '<h4 class="install-subtitle">'.get_lang('(recommended) settings').'</h4>';
869
    echo '<div class="install-requirement">'.get_lang('(recommended) settingsInfo').'</div>';
870
    echo '<div class="table-responsive">';
871
    echo '<table class="table table-bordered">
872
            <tr>
873
                <th>'.get_lang('Setting').'</th>
874
                <th>'.get_lang('(recommended)').'</th>
875
                <th>'.get_lang('Currently').'</th>
876
            </tr>
877
            
878
            <tr>
879
                <td class="requirements-item"><a href="http://php.net/manual/ref.errorfunc.php#ini.display-errors">Display Errors</a></td>
880
                <td class="requirements-recommended">'.Display::label('OFF', 'success').'</td>
881
                <td class="requirements-value">'.checkPhpSetting('display_errors', 'OFF').'</td>
882
            </tr>
883
            <tr>
884
                <td class="requirements-item"><a href="http://php.net/manual/ini.core.php#ini.file-uploads">File Uploads</a></td>
885
                <td class="requirements-recommended">'.Display::label('ON', 'success').'</td>
886
                <td class="requirements-value">'.checkPhpSetting('file_uploads', 'ON').'</td>
887
            </tr>
888
            <tr>
889
                <td class="requirements-item"><a href="http://php.net/manual/ref.session.php#ini.session.auto-start">Session auto start</a></td>
890
                <td class="requirements-recommended">'.Display::label('OFF', 'success').'</td>
891
                <td class="requirements-value">'.checkPhpSetting('session.auto_start', 'OFF').'</td>
892
            </tr>
893
            <tr>
894
                <td class="requirements-item"><a href="http://php.net/manual/ini.core.php#ini.short-open-tag">Short Open Tag</a></td>
895
                <td class="requirements-recommended">'.Display::label('OFF', 'success').'</td>
896
                <td class="requirements-value">'.checkPhpSetting('short_open_tag', 'OFF').'</td>
897
            </tr>
898
            <tr>
899
                <td class="requirements-item"><a href="http://www.php.net/manual/en/session.configuration.php#ini.session.cookie-httponly">Cookie HTTP Only</a></td>
900
                <td class="requirements-recommended">'.Display::label('ON', 'success').'</td>
901
                <td class="requirements-value">'.checkPhpSetting('session.cookie_httponly', 'ON').'</td>
902
            </tr>
903
            <tr>
904
                <td class="requirements-item"><a href="http://php.net/manual/ini.core.php#ini.upload-max-filesize">Maximum upload file size</a></td>
905
                <td class="requirements-recommended">'.Display::label('>= '.REQUIRED_MIN_UPLOAD_MAX_FILESIZE.'M', 'success').'</td>
906
                <td class="requirements-value">'.compare_setting_values(ini_get('upload_max_filesize'), REQUIRED_MIN_UPLOAD_MAX_FILESIZE).'</td>
907
            </tr>
908
            <tr>
909
                <td class="requirements-item"><a href="http://php.net/manual/ini.core.php#ini.post-max-size">Maximum post size</a></td>
910
                <td class="requirements-recommended">'.Display::label('>= '.REQUIRED_MIN_POST_MAX_SIZE.'M', 'success').'</td>
911
                <td class="requirements-value">'.compare_setting_values(ini_get('post_max_size'), REQUIRED_MIN_POST_MAX_SIZE).'</td>
912
            </tr>
913
            <tr>
914
                <td class="requirements-item"><a href="http://www.php.net/manual/en/ini.core.php#ini.memory-limit">Memory Limit</a></td>
915
                <td class="requirements-recommended">'.Display::label('>= '.REQUIRED_MIN_MEMORY_LIMIT.'M', 'success').'</td>
916
                <td class="requirements-value">'.compare_setting_values($originalMemoryLimit, REQUIRED_MIN_MEMORY_LIMIT).'</td>
917
            </tr>
918
          </table>';
919
    echo '</div>';
920
921
    // DIRECTORY AND FILE PERMISSIONS
922
    echo '<h4 class="install-subtitle">'.get_lang('Directory and files permissions').'</h4>';
923
    echo '<div class="install-requirement">'.get_lang('Directory and files permissionsInfo').'</div>';
924
    echo '<div class="table-responsive">';
925
926
    $_SESSION['permissions_for_new_directories'] = $_setting['permissions_for_new_directories'] = $dir_perm_verified;
927
    $_SESSION['permissions_for_new_files'] = $_setting['permissions_for_new_files'] = $fil_perm_verified;
928
929
    $dir_perm = Display::label('0'.decoct($dir_perm_verified), 'info');
930
    $file_perm = Display::label('0'.decoct($fil_perm_verified), 'info');
931
932
    $oldConf = '';
933
    if (file_exists(api_get_path(SYS_CODE_PATH).'inc/conf/configuration.php')) {
934
        $oldConf = '<tr>
935
            <td class="requirements-item">'.api_get_path(SYS_CODE_PATH).'inc/conf</td>
936
            <td class="requirements-value">'.check_writable(api_get_path(SYS_CODE_PATH).'inc/conf').'</td>
937
        </tr>';
938
    }
939
940
    echo '<table class="table table-bordered">
941
            '.$oldConf.'
942
            <tr>
943
                <td class="requirements-item">'.api_get_path(SYS_APP_PATH).'</td>
944
                <td class="requirements-value">'.check_writable(api_get_path(SYS_APP_PATH)).'</td>
945
            </tr>
946
            <tr>
947
                <td class="requirements-item">'.api_get_path(SYS_PATH).'config</td>
948
                <td class="requirements-value">'.check_writable(api_get_path(SYS_PATH).'config').'</td>
949
            </tr>
950
            <tr>
951
                <td class="requirements-item">'.api_get_path(SYS_PATH).'vendor/</td>
952
                <td class="requirements-value">'.checkReadable(api_get_path(SYS_PATH).'vendor').'</td>
953
            </tr>
954
            <tr>
955
                <td class="requirements-item">'.api_get_path(SYS_PUBLIC_PATH).'</td>
956
                <td class="requirements-value">'.check_writable(api_get_path(SYS_PUBLIC_PATH)).'</td>
957
            </tr>           
958
            <tr>
959
                <td class="requirements-item">'.get_lang('Permissions for new directories').'</td>
960
                <td class="requirements-value">'.$dir_perm.' </td>
961
            </tr>
962
            <tr>
963
                <td class="requirements-item">'.get_lang('Permissions for new files').'</td>
964
                <td class="requirements-value">'.$file_perm.' </td>
965
            </tr>
966
        </table>';
967
968
    echo '</div>';
969
970
    if ($installType === 'update' && (empty($updatePath) || $badUpdatePath)) {
971
        if ($badUpdatePath) {
972
            ?>
973
            <div class="alert alert-warning">
974
                <?php echo get_lang('Error'); ?>!<br />
975
                Chamilo <?php echo implode('|', $update_from_version_8).' '.get_lang('has not been found in that directory'); ?>.
976
            </div>
977
        <?php
978
        } else {
979
            echo '<br />';
980
        } ?>
981
            <div class="row">
982
                <div class="col-md-12">
983
                    <p><?php echo get_lang('Old version\'s root path'); ?>:
984
                        <input type="text" name="updatePath" size="50" value="<?php echo ($badUpdatePath && !empty($updatePath)) ? htmlentities($updatePath) : ''; ?>" />
985
                    </p>
986
                    <p>
987
                        <div class="btn-group">
988
                            <button type="submit" class="btn btn-secondary" name="step1" value="<?php echo get_lang('Back'); ?>" >
989
                                <em class="fa fa-backward"> <?php echo get_lang('Back'); ?></em>
990
                            </button>
991
                            <input type="hidden" name="is_executable" id="is_executable" value="-" />
992
                            <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;" >
993
                                <em class="fa fa-forward"> </em> <?php echo get_lang('Next'); ?>
994
                            </button>
995
                        </div>
996
                    </p>
997
                </div>
998
            </div>
999
        <?php
1000
    } else {
1001
        $error = false;
1002
        // First, attempt to set writing permissions if we don't have them yet
1003
        $perm = api_get_permissions_for_new_directories();
1004
        $perm_file = api_get_permissions_for_new_files();
1005
        $notWritable = [];
1006
1007
        $checked_writable = api_get_path(SYS_APP_PATH);
1008
        if (!is_writable($checked_writable)) {
1009
            $notWritable[] = $checked_writable;
1010
            @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

1010
            /** @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...
1011
        }
1012
1013
        $checked_writable = api_get_path(SYS_PUBLIC_PATH);
1014
        if (!is_writable($checked_writable)) {
1015
            $notWritable[] = $checked_writable;
1016
            @chmod($checked_writable, $perm);
1017
        }
1018
1019
        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...
1020
            $error = true;
1021
        }
1022
1023
        $checked_writable = api_get_path(CONFIGURATION_PATH).'configuration.php';
1024
        if (file_exists($checked_writable) && !is_writable($checked_writable)) {
1025
            $notWritable[] = $checked_writable;
1026
            @chmod($checked_writable, $perm_file);
1027
        }
1028
1029
        // Second, if this fails, report an error
1030
        //--> The user would have to adjust the permissions manually
1031
        if (count($notWritable) > 0) {
1032
            $error = true; ?>
1033
            <div class="text-danger">
1034
                <h3 class="text-center"><?php echo get_lang('Warning !'); ?></h3>
1035
                <p>
1036
                    <?php printf(get_lang('Some files or folders don\'t have writing permission. To be able to install Chamilo you should first change their permissions (using CHMOD). Please read the %s installation guide %s'), '<a href="../../documentation/installation_guide.html" target="blank">', '</a>'); ?>
1037
                </p>
1038
            </div>
1039
            <?php
1040
            echo '<ul>';
1041
            foreach ($notWritable as $value) {
1042
                echo '<li class="text-danger">'.$value.'</li>';
1043
            }
1044
            echo '</ul>';
1045
        } elseif (file_exists(api_get_path(CONFIGURATION_PATH).'configuration.php')) {
1046
            // Check wether a Chamilo configuration file already exists.
1047
            echo '<div class="alert alert-warning"><h4><center>';
1048
            echo get_lang('Warning !ExistingLMSInstallationDetected');
1049
            echo '</center></h4></div>';
1050
        }
1051
1052
        $deprecated = [
1053
            api_get_path(SYS_CODE_PATH).'exercice/',
1054
            api_get_path(SYS_CODE_PATH).'newscorm/',
1055
            api_get_path(SYS_PLUGIN_PATH).'ticket/',
1056
            api_get_path(SYS_PLUGIN_PATH).'skype/',
1057
        ];
1058
        $deprecatedToRemove = [];
1059
        foreach ($deprecated as $deprecatedDirectory) {
1060
            if (!is_dir($deprecatedDirectory)) {
1061
                continue;
1062
            }
1063
            $deprecatedToRemove[] = $deprecatedDirectory;
1064
        }
1065
1066
        if (count($deprecatedToRemove) > 0) {
1067
            ?>
1068
            <p class="text-danger"><?php echo get_lang('Warning !ForDeprecatedDirectoriesForUpgrade'); ?></p>
1069
            <ul>
1070
                <?php foreach ($deprecatedToRemove as $deprecatedDirectory) {
1071
                ?>
1072
                    <li class="text-danger"><?php echo $deprecatedDirectory; ?></li>
1073
                <?php
1074
            } ?>
1075
            </ul>
1076
            <?php
1077
        }
1078
1079
        // And now display the choice buttons (go back or install)?>
1080
        <p align="center" style="padding-top:15px">
1081
            <button type="submit" name="step1" class="btn btn-default" onclick="javascript: window.location='index.php'; return false;" value="<?php echo get_lang('Previous'); ?>" >
1082
                <em class="fa fa-backward"> </em> <?php echo get_lang('Previous'); ?>
1083
            </button>
1084
            <button type="submit" name="step2_install" class="btn btn-success" value="<?php echo get_lang("New installation"); ?>" <?php if ($error) {
1085
            echo 'disabled="disabled"';
1086
        } ?> >
1087
                <em class="fa fa-forward"> </em> <?php echo get_lang('New installation'); ?>
1088
            </button>
1089
        <input type="hidden" name="is_executable" id="is_executable" value="-" />
1090
            <button type="submit" class="btn btn-default" <?php echo !$error ?: 'disabled="disabled"'; ?> name="step2_update_8" value="Upgrade from Chamilo 1.9.x">
1091
                <em class="fa fa-forward" aria-hidden="true"></em> <?php echo get_lang('Upgrade Chamilo LMS version'); ?>
1092
            </button>
1093
            </p>
1094
        <?php
1095
    }
1096
}
1097
1098
/**
1099
 * Displays the license (GNU GPL) as step 2, with
1100
 * - an "I accept" button named step3 to proceed to step 3;
1101
 * - a "Back" button named step1 to go back to the first step.
1102
 */
1103
function display_license_agreement()
1104
{
1105
    echo '<div class="RequirementHeading"><h2>'.display_step_sequence().get_lang('Licence').'</h2>';
1106
    echo '<p>'.get_lang('Chamilo is free software distributed under the GNU General Public licence (GPL).').'</p>';
1107
    echo '<p><a href="../../documentation/license.html" target="_blank">'.get_lang('Printable version').'</a></p>';
1108
    echo '</div>'; ?>
1109
    <div class="form-group">
1110
        <pre style="overflow: auto; height: 200px; margin-top: 5px;">
1111
            <?php echo api_htmlentities(@file_get_contents(api_get_path(SYS_PATH).'documentation/license.txt')); ?>
1112
        </pre>
1113
    </div>
1114
    <div class="form-group form-check">
1115
        <input type="checkbox" name="accept" id="accept_licence" value="1">
1116
        <label for="accept_licence"><?php echo get_lang('I Accept'); ?></label>
1117
    </div>
1118
    <div class="row">
1119
        <div class="col-md-12">
1120
            <p class="alert alert-info"><?php echo get_lang('The images and media galleries of Chamilo use images from Nuvola, Crystal Clear and Tango icon galleries. Other images and media like diagrams and Flash animations are borrowed from Wikimedia and Ali Pakdel\'s and Denis Hoa\'s courses with their agreement and released under BY-SA Creative Commons license. You may find the license details at <a href="http://creativecommons.org/licenses/by-sa/3.0/">the CC website</a>, where a link to the full text of the license is provided at the bottom of the page.'); ?></p>
1121
        </div>
1122
    </div>
1123
1124
    <!-- Contact information form -->
1125
    <div class="section-parameters">
1126
        <a href="javascript://" class = "advanced_parameters" >
1127
        <span id="img_plus_and_minus">&nbsp;<i class="fa fa-eye" aria-hidden="true"></i>&nbsp;<?php echo get_lang('Contact information'); ?></span>
1128
        </a>
1129
    </div>
1130
1131
    <div id="id_contact_form" style="display:block">
1132
        <div class="normal-message"><?php echo get_lang('Contact informationDescription'); ?></div>
1133
        <div id="contact_registration">
1134
            <p><?php echo get_contact_registration_form(); ?></p><br />
1135
        </div>
1136
    </div>
1137
    <div class="text-center">
1138
    <button type="submit" class="btn btn-default" name="step1" value="&lt; <?php echo get_lang('Previous'); ?>" >
1139
        <em class="fa fa-backward"> </em> <?php echo get_lang('Previous'); ?>
1140
    </button>
1141
    <input type="hidden" name="is_executable" id="is_executable" value="-" />
1142
    <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('You must accept the licence'); ?>');return false;}" value="<?php echo get_lang('Next'); ?> &gt;" >
1143
        <em class="fa fa-forward"> </em> <?php echo get_lang('Next'); ?>
1144
    </button>
1145
    </div>
1146
    <?php
1147
}
1148
1149
/**
1150
 * Get contact registration form.
1151
 */
1152
function get_contact_registration_form()
1153
{
1154
    $html = '
1155
   <div class="form-horizontal">
1156
    <div class="panel panel-default">
1157
    <div class="panel-body">
1158
    <div id="div_sent_information"></div>
1159
    <div class="form-group row">
1160
            <label class="col-sm-3"><span class="form_required">*</span>'.get_lang('Name').'</label>
1161
            <div class="col-sm-9"><input id="person_name" class="form-control" type="text" name="person_name" size="30" /></div>
1162
    </div>
1163
    <div class="form-group row">
1164
            <label class="col-sm-3"><span class="form_required">*</span>'.get_lang('e-mail').'</label>
1165
            <div class="col-sm-9"><input id="person_email" class="form-control" type="text" name="person_email" size="30" /></div>
1166
    </div>
1167
    <div class="form-group row">
1168
            <label class="col-sm-3"><span class="form_required">*</span>'.get_lang('Your company\'s name').'</label>
1169
            <div class="col-sm-9"><input id="company_name" class="form-control" type="text" name="company_name" size="30" /></div>
1170
    </div>
1171
    <div class="form-group row">
1172
        <label class="col-sm-3"><span class="form_required">*</span>'.get_lang('Your company\'s activity').'</label>
1173
        <div class="col-sm-9">
1174
            <select class="selectpicker show-tick" name="company_activity" id="company_activity" >
1175
                <option value="">--- '.get_lang('Select one').' ---</option>
1176
                <Option value="Advertising/Marketing/PR">Advertising/Marketing/PR</Option><Option value="Agriculture/Forestry">Agriculture/Forestry</Option>
1177
                <Option value="Architecture">Architecture</Option><Option value="Banking/Finance">Banking/Finance</Option>
1178
                <Option value="Biotech/Pharmaceuticals">Biotech/Pharmaceuticals</Option><Option value="Business Equipment">Business Equipment</Option>
1179
                <Option value="Business Services">Business Services</Option><Option value="Construction">Construction</Option>
1180
                <Option value="Consulting/Research">Consulting/Research</Option><Option value="Education">Education</Option>
1181
                <Option value="Engineering">Engineering</Option><Option value="Environmental">Environmental</Option>
1182
                <Option value="Government">Government</Option><Option value="Healthcare">Health Care</Option>
1183
                <Option value="Hospitality/Lodging/Travel">Hospitality/Lodging/Travel</Option><Option value="Insurance">Insurance</Option>
1184
                <Option value="Legal">Legal</Option><Option value="Manufacturing">Manufacturing</Option>
1185
                <Option value="Media/Entertainment">Media/Entertainment</Option><Option value="Mortgage">Mortgage</Option>
1186
                <Option value="Non-Profit">Non-Profit</Option><Option value="Real Estate">Real Estate</Option>
1187
                <Option value="Restaurant">Restaurant</Option><Option value="Retail">Retail</Option>
1188
                <Option value="Shipping/Transportation">Shipping/Transportation</Option>
1189
                <Option value="Technology">Technology</Option><Option value="Telecommunications">Telecommunications</Option>
1190
                <Option value="Other">Other</Option>
1191
            </select>
1192
        </div>
1193
    </div>
1194
1195
    <div class="form-group row">
1196
        <label class="col-sm-3"><span class="form_required">*</span>'.get_lang('Your job\'s description').'</label>
1197
        <div class="col-sm-9">
1198
            <select class="selectpicker show-tick" name="person_role" id="person_role" >
1199
                <option value="">--- '.get_lang('Select one').' ---</option>
1200
                <Option value="Administration">Administration</Option><Option value="CEO/President/ Owner">CEO/President/ Owner</Option>
1201
                <Option value="CFO">CFO</Option><Option value="CIO/CTO">CIO/CTO</Option>
1202
                <Option value="Consultant">Consultant</Option><Option value="Customer Service">Customer Service</Option>
1203
                <Option value="Engineer/Programmer">Engineer/Programmer</Option><Option value="Facilities/Operations">Facilities/Operations</Option>
1204
                <Option value="Finance/ Accounting Manager">Finance/ Accounting Manager</Option><Option value="Finance/ Accounting Staff">Finance/ Accounting Staff</Option>
1205
                <Option value="General Manager">General Manager</Option><Option value="Human Resources">Human Resources</Option>
1206
                <Option value="IS/IT Management">IS/IT Management</Option><Option value="IS/ IT Staff">IS/ IT Staff</Option>
1207
                <Option value="Marketing Manager">Marketing Manager</Option><Option value="Marketing Staff">Marketing Staff</Option>
1208
                <Option value="Partner/Principal">Partner/Principal</Option><Option value="Purchasing Manager">Purchasing Manager</Option>
1209
                <Option value="Sales/ Business Dev. Manager">Sales/ Business Dev. Manager</Option><Option value="Sales/ Business Dev.">Sales/ Business Dev.</Option>
1210
                <Option value="Vice President/Senior Manager">Vice President/Senior Manager</Option><Option value="Other">Other</Option>
1211
            </select>
1212
        </div>
1213
    </div>
1214
1215
    <div class="form-group row">
1216
        <label class="col-sm-3"><span class="form_required">*</span>'.get_lang('Your company\'s home country').'</label>
1217
        <div class="col-sm-9">'.get_countries_list_from_array(true).'</div>
1218
    </div>
1219
    <div class="form-group row">
1220
        <label class="col-sm-3">'.get_lang('Company city').'</label>
1221
        <div class="col-sm-9">
1222
                <input type="text" class="form-control" id="company_city" name="company_city" size="30" />
1223
        </div>
1224
    </div>
1225
    <div class="form-group row">
1226
        <label class="col-sm-3">'.get_lang('Preferred contact language').'</label>
1227
        <div class="col-sm-9">
1228
            <select class="selectpicker show-tick" id="language" name="language">
1229
                <option value="bulgarian">Bulgarian</option>
1230
                <option value="indonesian">Bahasa Indonesia</option>
1231
                <option value="bosnian">Bosanski</option>
1232
                <option value="german">Deutsch</option>
1233
                <option selected="selected" value="english">English</option>
1234
                <option value="spanish">Spanish</option>
1235
                <option value="french">Français</option>
1236
                <option value="italian">Italian</option>
1237
                <option value="hungarian">Magyar</option>
1238
                <option value="dutch">Nederlands</option>
1239
                <option value="brazilian">Português do Brasil</option>
1240
                <option value="portuguese">Português europeu</option>
1241
                <option value="slovenian">Slovenčina</option>
1242
            </select>
1243
        </div>
1244
    </div>
1245
1246
    <div class="form-group row">
1247
        <label class="col-sm-3">'.get_lang('Do you have the power to take financial decisions on behalf of your company?').'</label>
1248
        <div class="col-sm-9">
1249
            <div class="radio">
1250
                <label>
1251
                    <input type="radio" name="financial_decision" id="financial_decision1" value="1" checked /> '.get_lang('Yes').'
1252
                </label>
1253
            </div>
1254
            <div class="radio">
1255
                <label>
1256
                    <input type="radio" name="financial_decision" id="financial_decision2" value="0" /> '.get_lang('No').'
1257
                </label>
1258
            </div>
1259
        </div>
1260
    </div>
1261
    <div class="clear"></div>
1262
    <div class="form-group row">
1263
            <div class="col-sm-3">&nbsp;</div>
1264
            <div class="col-sm-9"><button type="button" class="btn btn-default" onclick="javascript:send_contact_information();" value="'.get_lang('Send information').'" ><em class="fa fa-floppy-o"></em> '.get_lang('Send information').'</button> <span id="loader-button"></span></div>
1265
    </div>
1266
    <div class="form-group row">
1267
            <div class="col-sm-3">&nbsp;</div>
1268
            <div class="col-sm-9"><span class="form_required">*</span><small>'.get_lang('Mandatory field').'</small></div>
1269
    </div></div></div>
1270
    </div>';
1271
1272
    return $html;
1273
}
1274
1275
/**
1276
 * Displays a parameter in a table row.
1277
 * Used by the display_database_settings_form function.
1278
 *
1279
 * @param   string  Type of install
1280
 * @param   string  Name of parameter
1281
 * @param   string  Field name (in the HTML form)
1282
 * @param   string  Field value
1283
 * @param   string  Extra notice (to show on the right side)
1284
 * @param   bool Whether to display in update mode
1285
 * @param   string  Additional attribute for the <tr> element
1286
 */
1287
function displayDatabaseParameter(
1288
    $installType,
1289
    $parameterName,
1290
    $formFieldName,
1291
    $parameterValue,
1292
    $extra_notice,
1293
    $displayWhenUpdate = true,
1294
    $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

1294
    /** @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...
1295
) {
1296
    //echo "<tr ".$tr_attribute.">";
1297
    echo "<label class='col-sm-4'>$parameterName</label>";
1298
1299
    if ($installType == INSTALL_TYPE_UPDATE && $displayWhenUpdate) {
1300
        echo '<input type="hidden" name="'.$formFieldName.'" id="'.$formFieldName.'" value="'.api_htmlentities($parameterValue).'" />'.$parameterValue;
1301
    } else {
1302
        $inputType = $formFieldName == 'dbPassForm' ? 'password' : 'text';
1303
1304
        //Slightly limit the length of the database prefix to avoid having to cut down the databases names later on
1305
        $maxLength = $formFieldName == 'dbPrefixForm' ? '15' : MAX_FORM_FIELD_LENGTH;
1306
        if ($installType == INSTALL_TYPE_UPDATE) {
1307
            echo '<input type="hidden" name="'.$formFieldName.'" id="'.$formFieldName.'" value="'.api_htmlentities($parameterValue).'" />';
1308
            echo api_htmlentities($parameterValue);
1309
        } else {
1310
            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>";
1311
            echo '<div class="col-sm-3">'.$extra_notice.'</div>';
1312
        }
1313
    }
1314
}
1315
1316
/**
1317
 * Displays step 3 - a form where the user can enter the installation settings
1318
 * regarding the databases - login and password, names, prefixes, single
1319
 * or multiple databases, tracking or not...
1320
 *
1321
 * @param string $installType
1322
 * @param string $dbHostForm
1323
 * @param string $dbUsernameForm
1324
 * @param string $dbPassForm
1325
 * @param string $dbNameForm
1326
 * @param int    $dbPortForm
1327
 * @param string $installationProfile
1328
 */
1329
function display_database_settings_form(
1330
    $installType,
1331
    $dbHostForm,
1332
    $dbUsernameForm,
1333
    $dbPassForm,
1334
    $dbNameForm,
1335
    $dbPortForm = 3306,
1336
    $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

1336
    /** @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...
1337
) {
1338
    if ($installType == 'update') {
1339
        global $_configuration;
1340
        $dbHostForm = $_configuration['db_host'];
1341
        $dbUsernameForm = $_configuration['db_user'];
1342
        $dbPassForm = $_configuration['db_password'];
1343
        $dbNameForm = $_configuration['main_database'];
1344
        $dbPortForm = isset($_configuration['db_port']) ? $_configuration['db_port'] : '';
1345
1346
        echo '<div class="RequirementHeading"><h2>'.display_step_sequence().get_lang('Database settings').'</h2></div>';
1347
        echo '<div class="RequirementContent">';
1348
        echo get_lang('The upgrade script will recover and update the Chamilo database(s). In order to do this, this script will use the databases and settings defined below. Because our software runs on a wide range of systems and because all of them might not have been tested, we strongly recommend you do a full backup of your databases before you proceed with the upgrade!');
1349
        echo '</div>';
1350
    } else {
1351
        echo '<div class="RequirementHeading"><h2>'.display_step_sequence().get_lang('Database settings').'</h2></div>';
1352
        echo '<div class="RequirementContent">';
1353
        echo get_lang('The install script will create (or use) the Chamilo database using the database name given here. Please make sure the user you give has the right to create the database by the name given here. If a database with this name exists, it will be overwritten. Please do not use the root user as the Chamilo database user. This can lead to serious security issues.');
1354
        echo '</div>';
1355
    } ?>
1356
    <div class="panel panel-default">
1357
        <div class="panel-body">
1358
        <div class="form-group row">
1359
            <label class="col-sm-4"><?php echo get_lang('Database Host'); ?> </label>
1360
            <?php if ($installType == 'update') {
1361
        ?>
1362
            <div class="col-sm-5">
1363
                <input type="hidden" name="dbHostForm" value="<?php echo htmlentities($dbHostForm); ?>" /><?php echo $dbHostForm; ?>
1364
            </div>
1365
            <div class="col-sm-3"></div>
1366
            <?php
1367
    } else {
1368
        ?>
1369
            <div class="col-sm-5">
1370
                <input type="text" class="form-control" size="25" maxlength="50" name="dbHostForm" value="<?php echo htmlentities($dbHostForm); ?>" />
1371
            </div>
1372
            <div class="col-sm-3"><?php echo get_lang('ex.').' localhost'; ?></div>
1373
            <?php
1374
    } ?>
1375
        </div>
1376
        <div class="form-group row">
1377
            <label class="col-sm-4"><?php echo get_lang('Port'); ?> </label>
1378
            <?php if ($installType == 'update') {
1379
        ?>
1380
            <div class="col-sm-5">
1381
                <input type="hidden" name="dbPortForm" value="<?php echo htmlentities($dbPortForm); ?>" /><?php echo $dbPortForm; ?>
1382
            </div>
1383
            <div class="col-sm-3"></div>
1384
            <?php
1385
    } else {
1386
        ?>
1387
            <div class="col-sm-5">
1388
                <input type="text" class="form-control" size="25" maxlength="50" name="dbPortForm" value="<?php echo htmlentities($dbPortForm); ?>" />
1389
            </div>
1390
            <div class="col-sm-3"><?php echo get_lang('ex.').' 3306'; ?></div>
1391
            <?php
1392
    } ?>
1393
        </div>
1394
        <div class="form-group row">
1395
            <?php
1396
                //database user username
1397
                $example_login = get_lang('ex.').' root';
1398
    displayDatabaseParameter($installType, get_lang('Database Login'), 'dbUsernameForm', $dbUsernameForm, $example_login); ?>
1399
        </div>
1400
        <div class="form-group row">
1401
            <?php
1402
            //database user password
1403
            $example_password = get_lang('ex.').' '.api_generate_password();
1404
    displayDatabaseParameter($installType, get_lang('Database Password'), 'dbPassForm', $dbPassForm, $example_password); ?>
1405
        </div>
1406
        <div class="form-group row">
1407
            <?php
1408
            //Database Name fix replace weird chars
1409
            if ($installType != INSTALL_TYPE_UPDATE) {
1410
                $dbNameForm = str_replace(['-', '*', '$', ' ', '.'], '', $dbNameForm);
1411
            }
1412
1413
    displayDatabaseParameter(
1414
                $installType,
1415
                get_lang('Main Chamilo database (DB)'),
1416
                'dbNameForm',
1417
                $dbNameForm,
1418
                '&nbsp;',
1419
                null,
1420
                'id="optional_param1"'
1421
                ); ?>
1422
        </div>
1423
       <?php if ($installType != INSTALL_TYPE_UPDATE) {
1424
                    ?>
1425
        <div class="form-group row">
1426
            <div class="col-sm-4"></div>
1427
            <div class="col-sm-8">
1428
            <button type="submit" class="btn btn-primary" name="step3" value="step3">
1429
                <em class="fa fa-refresh"> </em>
1430
                <?php echo get_lang('Check database connection'); ?>
1431
            </button>
1432
            </div>
1433
        </div>
1434
        <?php
1435
                } ?>
1436
1437
        </div>
1438
    </div>
1439
    <?php
1440
        $database_exists_text = '';
1441
    $manager = null;
1442
    try {
1443
        if ($installType === 'update') {
1444
            /** @var \Database $manager */
1445
            $manager = connectToDatabase(
1446
                $dbHostForm,
1447
                $dbUsernameForm,
1448
                $dbPassForm,
1449
                $dbNameForm,
1450
                $dbPortForm
1451
            );
1452
1453
            $connection = $manager->getConnection();
1454
            $connection->connect();
1455
            $schemaManager = $connection->getSchemaManager();
1456
1457
            // Test create/alter/drop table
1458
            $table = 'zXxTESTxX_'.mt_rand(0, 1000);
1459
            $sql = "CREATE TABLE $table (id INT AUTO_INCREMENT NOT NULL, name varchar(255), PRIMARY KEY(id))";
1460
            $connection->query($sql);
1461
            $tableCreationWorks = false;
1462
            $tableDropWorks = false;
1463
            if ($schemaManager->tablesExist($table)) {
1464
                $tableCreationWorks = true;
1465
                $sql = "ALTER TABLE $table ADD COLUMN name2 varchar(140) ";
1466
                $connection->query($sql);
1467
                $schemaManager->dropTable($table);
1468
                $tableDropWorks = $schemaManager->tablesExist($table) === false;
1469
            }
1470
        } else {
1471
            $manager = connectToDatabase(
1472
                $dbHostForm,
1473
                $dbUsernameForm,
1474
                $dbPassForm,
1475
                null,
1476
                $dbPortForm
1477
            );
1478
1479
            $schemaManager = $manager->getConnection()->getSchemaManager();
1480
            $databases = $schemaManager->listDatabases();
1481
            if (in_array($dbNameForm, $databases)) {
1482
                $database_exists_text = '<div class="alert alert-warning">'.get_lang('A database with the same name <b>already exists</b>.').'</div>';
1483
            }
1484
        }
1485
    } catch (Exception $e) {
1486
        $database_exists_text = $e->getMessage();
1487
        $manager = false;
1488
    }
1489
1490
    if ($manager && $manager->getConnection()->isConnected()): ?>
1491
        <?php echo $database_exists_text; ?>
1492
        <div id="db_status" class="alert alert-success">
1493
            Database host: <strong><?php echo $manager->getConnection()->getHost(); ?></strong><br/>
1494
            Database port: <strong><?php echo $manager->getConnection()->getPort(); ?></strong><br/>
1495
            Database driver: <strong><?php echo $manager->getConnection()->getDriver()->getName(); ?></strong><br/>
1496
            <?php
1497
                if ($installType === 'update') {
1498
                    echo get_lang('CreateTableWorks').' <strong>Ok</strong>';
1499
                    echo '<br/ >';
1500
                    echo get_lang('AlterTableWorks').' <strong>Ok</strong>';
1501
                    echo '<br/ >';
1502
                    echo get_lang('DropColumnWorks').' <strong>Ok</strong>';
1503
                } ?>
1504
        </div>
1505
    <?php else: ?>
1506
        <div id="db_status" class="alert alert-danger">
1507
            <p><?php echo get_lang('The database connection has failed. This is generally due to the wrong user, the wrong password or the wrong database prefix being set above. Please review these settings and try again.'); ?></strong></p>
1508
            <code><?php echo $database_exists_text; ?></code>
1509
        </div>
1510
    <?php endif; ?>
1511
1512
   <div class="btn-group" role="group">
1513
       <button type="submit" name="step2"
1514
               class="btn btn-secondary float-right" value="&lt; <?php echo get_lang('Previous'); ?>" >
1515
           <em class="fa fa-backward"> </em> <?php echo get_lang('Previous'); ?>
1516
       </button>
1517
       <input type="hidden" name="is_executable" id="is_executable" value="-" />
1518
       <?php if ($manager) {
1519
                    ?>
1520
           <button type="submit" class="btn btn-success" name="step4" value="<?php echo get_lang('Next'); ?> &gt;" >
1521
               <em class="fa fa-forward"> </em> <?php echo get_lang('Next'); ?>
1522
           </button>
1523
       <?php
1524
                } else {
1525
                    ?>
1526
           <button
1527
                   disabled="disabled"
1528
                   type="submit" class="btn btn-success disabled" name="step4" value="<?php echo get_lang('Next'); ?> &gt;" >
1529
               <em class="fa fa-forward"> </em> <?php echo get_lang('Next'); ?>
1530
           </button>
1531
       <?php
1532
                } ?>
1533
   </div>
1534
    <?php
1535
}
1536
1537
function panel($content = null, $title = null, $id = null, $style = null)
1538
{
1539
    $html = '';
1540
    if (empty($style)) {
1541
        $style = 'default';
1542
    }
1543
    if (!empty($title)) {
1544
        $panelTitle = Display::div($title, ['class' => 'panel-heading']);
1545
        $panelBody = Display::div($content, ['class' => 'panel-body']);
1546
        $panelParent = Display::div($panelTitle.$panelBody, ['id' => $id, 'class' => 'panel panel-'.$style]);
1547
    } else {
1548
        $panelBody = Display::div($html, ['class' => 'panel-body']);
1549
        $panelParent = Display::div($panelBody, ['id' => $id, 'class' => 'panel panel-'.$style]);
1550
    }
1551
    $html .= $panelParent;
1552
1553
    return $html;
1554
}
1555
1556
/**
1557
 * Displays a parameter in a table row.
1558
 * Used by the display_configuration_settings_form function.
1559
 *
1560
 * @param string $installType
1561
 * @param string $parameterName
1562
 * @param string $formFieldName
1563
 * @param string $parameterValue
1564
 * @param string $displayWhenUpdate
1565
 *
1566
 * @return string
1567
 */
1568
function display_configuration_parameter(
1569
    $installType,
1570
    $parameterName,
1571
    $formFieldName,
1572
    $parameterValue,
1573
    $displayWhenUpdate = 'true'
1574
) {
1575
    $html = '<div class="form-group row">';
1576
    $html .= '<label class="col-sm-6 control-label">'.$parameterName.'</label>';
1577
    if ($installType == INSTALL_TYPE_UPDATE && $displayWhenUpdate) {
1578
        $html .= '<input type="hidden" name="'.$formFieldName.'" value="'.api_htmlentities($parameterValue, ENT_QUOTES).'" />'.$parameterValue;
1579
    } else {
1580
        $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>";
1581
    }
1582
    $html .= "</div>";
1583
1584
    return $html;
1585
}
1586
1587
/**
1588
 * Displays step 4 of the installation - configuration settings about Chamilo itself.
1589
 *
1590
 * @param string $installType
1591
 * @param string $urlForm
1592
 * @param string $languageForm
1593
 * @param string $emailForm
1594
 * @param string $adminFirstName
1595
 * @param string $adminLastName
1596
 * @param string $adminPhoneForm
1597
 * @param string $campusForm
1598
 * @param string $institutionForm
1599
 * @param string $institutionUrlForm
1600
 * @param string $encryptPassForm
1601
 * @param bool   $allowSelfReg
1602
 * @param bool   $allowSelfRegProf
1603
 * @param string $loginForm
1604
 * @param string $passForm
1605
 */
1606
function display_configuration_settings_form(
1607
    $installType,
1608
    $urlForm,
1609
    $languageForm,
1610
    $emailForm,
1611
    $adminFirstName,
1612
    $adminLastName,
1613
    $adminPhoneForm,
1614
    $campusForm,
1615
    $institutionForm,
1616
    $institutionUrlForm,
1617
    $encryptPassForm,
1618
    $allowSelfReg,
1619
    $allowSelfRegProf,
1620
    $loginForm,
1621
    $passForm
1622
) {
1623
    if ($installType != 'update' && empty($languageForm)) {
1624
        $languageForm = $_SESSION['install_language'];
1625
    }
1626
    echo '<div class="RequirementHeading">';
1627
    echo "<h2>".display_step_sequence().get_lang("ConfigurationSettings")."</h2>";
1628
    echo '</div>';
1629
1630
    // Parameter 1: administrator's login
1631
    $html = '';
1632
    $html .= display_configuration_parameter(
1633
        $installType,
1634
        get_lang('Administrator login'),
1635
        'loginForm',
1636
        $loginForm,
1637
        $installType == 'update'
1638
    );
1639
1640
    // Parameter 2: administrator's password
1641
    if ($installType != 'update') {
1642
        $html .= display_configuration_parameter($installType, get_lang('Administrator password (<font color="red">you may want to change this</font>)'), 'passForm', $passForm, false);
1643
    }
1644
1645
    // Parameters 3 and 4: administrator's names
1646
1647
    $html .= display_configuration_parameter(
1648
        $installType,
1649
        get_lang('Administrator first name'),
1650
        'adminFirstName',
1651
        $adminFirstName
1652
    );
1653
    $html .= display_configuration_parameter($installType, get_lang('Administrator last name'), 'adminLastName', $adminLastName);
1654
1655
    //Parameter 3: administrator's email
1656
    $html .= display_configuration_parameter($installType, get_lang('Admine-mail'), 'emailForm', $emailForm);
1657
1658
    //Parameter 6: administrator's telephone
1659
    $html .= display_configuration_parameter($installType, get_lang('Administrator telephone'), 'adminPhoneForm', $adminPhoneForm);
1660
    echo panel($html, get_lang('Administrator'), 'administrator');
1661
1662
    //First parameter: language
1663
    $html = '<div class="form-group row">';
1664
    $html .= '<label class="col-sm-6 control-label">'.get_lang('Main language')."</label>";
1665
    if ($installType == 'update') {
1666
        $html .= '<input type="hidden" name="languageForm" value="'.api_htmlentities($languageForm, ENT_QUOTES).'" />'.$languageForm;
1667
    } else { // new installation
1668
        $html .= '<div class="col-sm-6">';
1669
        $html .= display_language_selection_box('languageForm', $languageForm);
1670
        $html .= '</div>';
1671
    }
1672
    $html .= "</div>";
1673
1674
    //Second parameter: Chamilo URL
1675
    $html .= '<div class="form-group row">';
1676
    $html .= '<label class="col-sm-6 control-label">'.get_lang('Chamilo URL').get_lang('Required field').'</label>';
1677
1678
    if ($installType == 'update') {
1679
        $html .= api_htmlentities($urlForm, ENT_QUOTES)."\n";
1680
    } else {
1681
        $html .= '<div class="col-sm-6">';
1682
        $html .= '<input class="form-control" type="text" size="40" maxlength="100" name="urlForm" value="'.api_htmlentities($urlForm, ENT_QUOTES).'" />';
1683
        $html .= '</div>';
1684
    }
1685
    $html .= '</div>';
1686
1687
    //Parameter 9: campus name
1688
    $html .= display_configuration_parameter(
1689
        $installType,
1690
        get_lang('Your portal name'),
1691
        'campusForm',
1692
        $campusForm
1693
    );
1694
1695
    //Parameter 10: institute (short) name
1696
    $html .= display_configuration_parameter(
1697
        $installType,
1698
        get_lang('Your company short name'),
1699
        'institutionForm',
1700
        $institutionForm
1701
    );
1702
1703
    //Parameter 11: institute (short) name
1704
    $html .= display_configuration_parameter(
1705
        $installType,
1706
        get_lang('URL of this company'),
1707
        'institutionUrlForm',
1708
        $institutionUrlForm
1709
    );
1710
1711
    $html .= '<div class="form-group row">
1712
            <label class="col-sm-6 control-label">'.get_lang("Encryption method").'</label>
1713
        <div class="col-sm-6">';
1714
    if ($installType == 'update') {
1715
        $html .= '<input type="hidden" name="encryptPassForm" value="'.$encryptPassForm.'" />'.$encryptPassForm;
1716
    } else {
1717
        $html .= '<div class="checkbox">
1718
                    <label>
1719
                        <input  type="radio" name="encryptPassForm" value="bcrypt" id="encryptPass1" '.($encryptPassForm == 'bcrypt' ? 'checked="checked" ' : '').'/> bcrypt
1720
                    </label>';
1721
1722
        $html .= '<label>
1723
                        <input  type="radio" name="encryptPassForm" value="sha1" id="encryptPass1" '.($encryptPassForm == 'sha1' ? 'checked="checked" ' : '').'/> sha1
1724
                    </label>';
1725
1726
        $html .= '<label>
1727
                        <input type="radio" name="encryptPassForm" value="md5" id="encryptPass0" '.($encryptPassForm == 'md5' ? 'checked="checked" ' : '').'/> md5
1728
                    </label>';
1729
1730
        $html .= '<label>
1731
                        <input type="radio" name="encryptPassForm" value="none" id="encryptPass2" '.($encryptPassForm == 'none' ? 'checked="checked" ' : '').'/>'.get_lang('none').'
1732
                    </label>';
1733
        $html .= '</div>';
1734
    }
1735
    $html .= '</div></div>';
1736
1737
    $html .= '<div class="form-group row">
1738
            <label class="col-sm-6 control-label">'.get_lang('Allow self-registration').'</label>
1739
            <div class="col-sm-6">';
1740
    if ($installType == 'update') {
1741
        if ($allowSelfReg == 'true') {
1742
            $label = get_lang('Yes');
1743
        } elseif ($allowSelfReg == 'false') {
1744
            $label = get_lang('No');
1745
        } else {
1746
            $label = get_lang('After approval');
1747
        }
1748
        $html .= '<input type="hidden" name="allowSelfReg" value="'.$allowSelfReg.'" />'.$label;
1749
    } else {
1750
        $html .= '<div class="control-group">';
1751
        $html .= '<label class="checkbox-inline">
1752
                        <input type="radio" name="allowSelfReg" value="true" id="allowSelfReg1" '.($allowSelfReg == 'true' ? 'checked="checked" ' : '').' /> '.get_lang('Yes').'
1753
                    </label>';
1754
        $html .= '<label class="checkbox-inline">
1755
                        <input type="radio" name="allowSelfReg" value="false" id="allowSelfReg0" '.($allowSelfReg == 'false' ? '' : 'checked="checked" ').' /> '.get_lang('No').'
1756
                    </label>';
1757
        $html .= '<label class="checkbox-inline">
1758
                    <input type="radio" name="allowSelfReg" value="approval" id="allowSelfReg2" '.($allowSelfReg == 'approval' ? '' : 'checked="checked" ').' /> '.get_lang('After approval').'
1759
                </label>';
1760
        $html .= '</div>';
1761
    }
1762
    $html .= '</div>';
1763
    $html .= '</div>';
1764
1765
    $html .= '<div class="form-group row">';
1766
    $html .= '<label class="col-sm-6 control-label">'.get_lang('Allow self-registrationProf').'</label>
1767
        <div class="col-sm-6">';
1768
    if ($installType == 'update') {
1769
        if ($allowSelfRegProf == 'true') {
1770
            $label = get_lang('Yes');
1771
        } else {
1772
            $label = get_lang('No');
1773
        }
1774
        $html .= '<input type="hidden" name="allowSelfRegProf" value="'.$allowSelfRegProf.'" />'.$label;
1775
    } else {
1776
        $html .= '<div class="control-group">
1777
                <label class="checkbox-inline">
1778
                    <input type="radio" name="allowSelfRegProf" value="1" id="allowSelfRegProf1" '.($allowSelfRegProf ? 'checked="checked" ' : '').'/>
1779
                '.get_lang('Yes').'
1780
                </label>';
1781
        $html .= '<label class="checkbox-inline">
1782
                    <input type="radio" name="allowSelfRegProf" value="0" id="allowSelfRegProf0" '.($allowSelfRegProf ? '' : 'checked="checked" ').' />
1783
                   '.get_lang('No').'
1784
                </label>';
1785
        $html .= '</div>';
1786
    }
1787
    $html .= '</div>
1788
    </div>';
1789
1790
    echo panel($html, get_lang('Portal'), 'platform'); ?>
1791
    <div class='btn-group'>
1792
        <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>
1793
        <input type="hidden" name="is_executable" id="is_executable" value="-" />
1794
        <button class="btn btn-success" type="submit" name="step5">
1795
            <em class="fa fa-forward"> </em> <?php echo get_lang('Next'); ?>
1796
        </button>
1797
    </div>
1798
    <?php
1799
}
1800
1801
/**
1802
 * After installation is completed (step 6), this message is displayed.
1803
 */
1804
function display_after_install_message()
1805
{
1806
    $container = Container::$container;
1807
1808
    $trans = $container->get('translator');
1809
    $html = '<div class="RequirementContent">'.
1810
        $trans->trans(
1811
            'When you enter your portal for the first time, the best way to understand it is to create a course with the \'Create course\' link in the menu and play around a little.').'</div>';
1812
    $html .= '<div class="alert alert-warning">';
1813
    $html .= '<strong>'.$trans->trans('Security advice').'</strong>';
1814
    $html .= ': ';
1815
    $html .= sprintf($trans->trans(
1816
        'To protect your site, make the whole %s directory read-only (chmod -R 0555 on Linux) and delete the %s directory.'), 'var/config/', 'main/install/');
1817
    $html .= '</div></form>
1818
    <br />
1819
    <a class="btn btn-success btn-block" href="../../index.php">
1820
        '.$trans->trans('Go to your newly created portal.').'
1821
    </a>';
1822
1823
    return $html;
1824
}
1825
1826
/**
1827
 * This function return countries list from array (hardcoded).
1828
 *
1829
 * @param bool $combo (Optional) True for returning countries list with select html
1830
 *
1831
 * @return array|string countries list
1832
 */
1833
function get_countries_list_from_array($combo = false)
1834
{
1835
    $a_countries = [
1836
        "Afghanistan", "Albania", "Algeria", "Andorra", "Angola", "Antigua and Barbuda", "Argentina", "Armenia", "Australia", "Austria", "Azerbaijan",
1837
        "Bahamas", "Bahrain", "Bangladesh", "Barbados", "Belarus", "Belgium", "Belize", "Benin", "Bhutan", "Bolivia", "Bosnia and Herzegovina", "Botswana", "Brazil", "Brunei", "Bulgaria", "Burkina Faso", "Burundi",
1838
        "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",
1839
        "Denmark", "Djibouti", "Dominica", "Dominican Republic",
1840
        "East Timor (Timor Timur)", "Ecuador", "Egypt", "El Salvador", "Equatorial Guinea", "Eritrea", "Estonia", "Ethiopia",
1841
        "Fiji", "Finland", "France",
1842
        "Gabon", "Gambia, The", "Georgia", "Germany", "Ghana", "Greece", "Grenada", "Guatemala", "Guinea", "Guinea-Bissau", "Guyana",
1843
        "Haiti", "Honduras", "Hungary",
1844
        "Iceland", "India", "Indonesia", "Iran", "Iraq", "Ireland", "Israel", "Italy",
1845
        "Jamaica", "Japan", "Jordan",
1846
        "Kazakhstan", "Kenya", "Kiribati", "Korea, North", "Korea, South", "Kuwait", "Kyrgyzstan",
1847
        "Laos", "Latvia", "Lebanon", "Lesotho", "Liberia", "Libya", "Liechtenstein", "Lithuania", "Luxembourg",
1848
        "Macedonia", "Madagascar", "Malawi", "Malaysia", "Maldives", "Mali", "Malta", "Marshall Islands", "Mauritania", "Mauritius", "Mexico", "Micronesia", "Moldova", "Monaco", "Mongolia", "Morocco", "Mozambique", "Myanmar",
1849
        "Namibia", "Nauru", "Nepa", "Netherlands", "New Zealand", "Nicaragua", "Niger", "Nigeria", "Norway",
1850
        "Oman",
1851
        "Pakistan", "Palau", "Panama", "Papua New Guinea", "Paraguay", "Peru", "Philippines", "Poland", "Portugal",
1852
        "Qatar",
1853
        "Romania", "Russia", "Rwanda",
1854
        "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",
1855
        "Taiwan", "Tajikistan", "Tanzania", "Thailand", "Togo", "Tonga", "Trinidad and Tobago", "Tunisia", "Turkey", "Turkmenistan", "Tuvalu",
1856
        "Uganda", "Ukraine", "United Arab Emirates", "United Kingdom", "United States", "Uruguay", "Uzbekistan",
1857
        "Vanuatu", "Vatican City", "Venezuela", "Vietnam",
1858
        "Yemen",
1859
        "Zambia", "Zimbabwe",
1860
    ];
1861
    if ($combo) {
1862
        $country_select = '<select class="selectpicker show-tick" id="country" name="country">';
1863
        $country_select .= '<option value="">--- '.get_lang('Select one').' ---</option>';
1864
        foreach ($a_countries as $country) {
1865
            $country_select .= '<option value="'.$country.'">'.$country.'</option>';
1866
        }
1867
        $country_select .= '</select>';
1868
1869
        return $country_select;
1870
    }
1871
1872
    return $a_countries;
1873
}
1874
1875
/**
1876
 * Lock settings that can't be changed in other portals.
1877
 */
1878
function lockSettings()
1879
{
1880
    $settings = api_get_locked_settings();
1881
    $table = Database::get_main_table(TABLE_MAIN_SETTINGS_CURRENT);
1882
    foreach ($settings as $setting) {
1883
        $sql = "UPDATE $table SET access_url_locked = 1 WHERE variable  = '$setting'";
1884
        Database::query($sql);
1885
    }
1886
}
1887
1888
/**
1889
 * Update dir values.
1890
 */
1891
function updateDirAndFilesPermissions()
1892
{
1893
    $table = Database::get_main_table(TABLE_MAIN_SETTINGS_CURRENT);
1894
    $permissions_for_new_directories = isset($_SESSION['permissions_for_new_directories']) ? $_SESSION['permissions_for_new_directories'] : 0770;
1895
    $permissions_for_new_files = isset($_SESSION['permissions_for_new_files']) ? $_SESSION['permissions_for_new_files'] : 0660;
1896
    // use decoct() to store as string
1897
    Database::update(
1898
        $table,
1899
        ['selected_value' => '0'.decoct($permissions_for_new_directories)],
1900
        ['variable = ?' => 'permissions_for_new_directories']
1901
    );
1902
1903
    Database::update(
1904
        $table,
1905
        ['selected_value' => '0'.decoct($permissions_for_new_files)],
1906
        ['variable = ?' => 'permissions_for_new_files']
1907
    );
1908
1909
    if (isset($_SESSION['permissions_for_new_directories'])) {
1910
        unset($_SESSION['permissions_for_new_directories']);
1911
    }
1912
1913
    if (isset($_SESSION['permissions_for_new_files'])) {
1914
        unset($_SESSION['permissions_for_new_files']);
1915
    }
1916
}
1917
1918
/**
1919
 * @param $current_value
1920
 * @param $wanted_value
1921
 *
1922
 * @return string
1923
 */
1924
function compare_setting_values($current_value, $wanted_value)
1925
{
1926
    $current_value_string = $current_value;
1927
    $current_value = (float) $current_value;
1928
    $wanted_value = (float) $wanted_value;
1929
1930
    if ($current_value >= $wanted_value) {
1931
        return Display::label($current_value_string, 'success');
1932
    } else {
1933
        return Display::label($current_value_string, 'important');
1934
    }
1935
}
1936
1937
/**
1938
 * Save settings values.
1939
 *
1940
 * @param string $organizationName
1941
 * @param string $organizationUrl
1942
 * @param string $siteName
1943
 * @param string $adminEmail
1944
 * @param string $adminLastName
1945
 * @param string $adminFirstName
1946
 * @param string $language
1947
 * @param string $allowRegistration
1948
 * @param string $allowTeacherSelfRegistration
1949
 * @param string $installationProfile          The name of an installation profile file in main/install/profiles/
1950
 */
1951
function installSettings(
1952
    $organizationName,
1953
    $organizationUrl,
1954
    $siteName,
1955
    $adminEmail,
1956
    $adminLastName,
1957
    $adminFirstName,
1958
    $language,
1959
    $allowRegistration,
1960
    $allowTeacherSelfRegistration,
1961
    $installationProfile = ''
1962
) {
1963
    $allowTeacherSelfRegistration = $allowTeacherSelfRegistration ? 'true' : 'false';
1964
1965
    $settings = [
1966
        'institution' => $organizationName,
1967
        'institution_url' => $organizationUrl,
1968
        'site_name' => $siteName,
1969
        'administrator_email' => $adminEmail,
1970
        'administrator_surname' => $adminLastName,
1971
        'administrator_name' => $adminFirstName,
1972
        'platform_language' => $language,
1973
        'allow_registration' => $allowRegistration,
1974
        'allow_registration_as_teacher' => $allowTeacherSelfRegistration,
1975
    ];
1976
1977
    foreach ($settings as $variable => $value) {
1978
        $sql = "UPDATE settings_current
1979
                SET selected_value = '$value'
1980
                WHERE variable = '$variable'";
1981
        Database::query($sql);
1982
    }
1983
    installProfileSettings($installationProfile);
1984
}
1985
1986
/**
1987
 * Executes DB changes based in the classes defined in
1988
 * src/CoreBundle/Migrations/Schema/*.
1989
 *
1990
 * @param string        $chamiloVersion
1991
 * @param EntityManager $manager
1992
 *
1993
 * @throws \Doctrine\DBAL\DBALException
1994
 *
1995
 * @return bool
1996
 */
1997
function migrate($chamiloVersion, EntityManager $manager)
1998
{
1999
    $debug = true;
2000
    $connection = $manager->getConnection();
2001
2002
    $config = new \Doctrine\DBAL\Migrations\Configuration\Configuration($connection);
2003
2004
    // Table name that will store migrations log (will be created automatically,
2005
    // default name is: doctrine_migration_versions)
2006
    $config->setMigrationsTableName('version');
2007
    // Namespace of your migration classes, do not forget escape slashes, do not add last slash
2008
    $config->setMigrationsNamespace('Chamilo\CoreBundle\Migrations\Schema\V'.$chamiloVersion);
2009
    // Directory where your migrations are located
2010
    $versionPath = api_get_path(SYS_PATH).'src/CoreBundle/Migrations/Schema/V'.$chamiloVersion;
2011
    error_log("Reading files from dir: $versionPath");
2012
    $config->setMigrationsDirectory($versionPath);
2013
    // Load your migrations
2014
    $config->registerMigrationsFromDirectory($config->getMigrationsDirectory());
2015
2016
    $migration = new \Doctrine\DBAL\Migrations\Migration($config);
2017
    $versions = $config->getMigrations();
2018
2019
    /** @var Doctrine\DBAL\Migrations\Version $migrationItem */
2020
    foreach ($versions as $version) {
2021
        $version->getMigration()->setEntityManager($manager);
2022
    }
2023
2024
    $to = null; // if $to == null then schema will be migrated to latest version
2025
    echo '<pre>';
2026
    try {
2027
        // Execute migration!
2028
        $migratedSQL = $migration->migrate($to);
2029
2030
        if ($debug) {
2031
            foreach ($migratedSQL as $version => $sqlList) {
2032
                echo "VERSION: $version<br>";
2033
                echo '----------------------------------------------<br />';
2034
                $total = count($sqlList);
2035
                error_log("VERSION: $version");
2036
                error_log("# queries: $total");
2037
                $counter = 1;
2038
                foreach ($sqlList as $sql) {
2039
                    echo "<code>$sql</code><br>";
2040
                    error_log("$counter/$total : $sql");
2041
                    $counter++;
2042
                }
2043
            }
2044
2045
            echo "<br>DONE!<br>";
2046
        }
2047
2048
        return true;
2049
    } catch (Exception $ex) {
2050
        if ($debug) {
2051
            echo "ERROR: {$ex->getMessage()}<br>";
2052
2053
            return false;
2054
        }
2055
    }
2056
2057
    echo '</pre>';
2058
2059
    return false;
2060
}
2061
2062
/**
2063
 * @param EntityManager $em
2064
 *
2065
 * @throws \Doctrine\DBAL\DBALException
2066
 */
2067
function fixIds(EntityManager $em)
2068
{
2069
    $connection = $em->getConnection();
2070
    $database = new Database();
2071
    $database->setManager($em);
2072
    $debug = true;
2073
    if ($debug) {
2074
        error_log('fixIds');
2075
    }
2076
2077
    // Create temporary indexes to increase speed of the following operations
2078
    // Adding and removing indexes will usually take much less time than
2079
    // the execution without indexes of the queries in this function, particularly
2080
    // for large tables
2081
    $sql = "ALTER TABLE c_document ADD INDEX tmpidx_doc(c_id, id)";
2082
    $connection->executeQuery($sql);
2083
    $sql = "ALTER TABLE c_student_publication ADD INDEX tmpidx_stud (c_id, id)";
2084
    $connection->executeQuery($sql);
2085
    $sql = "ALTER TABLE c_quiz ADD INDEX tmpidx_quiz (c_id, id)";
2086
    $connection->executeQuery($sql);
2087
    $sql = "ALTER TABLE c_item_property ADD INDEX tmpidx_ip (to_group_id)";
2088
    $connection->executeQuery($sql);
2089
2090
    $sql = "SELECT * FROM c_lp_item";
2091
    $result = $connection->fetchAll($sql);
2092
    foreach ($result as $item) {
2093
        $courseId = $item['c_id'];
2094
        $iid = isset($item['iid']) ? intval($item['iid']) : 0;
2095
        $ref = isset($item['ref']) ? intval($item['ref']) : 0;
2096
        $sql = null;
2097
2098
        $newId = '';
2099
2100
        switch ($item['item_type']) {
2101
            case TOOL_LINK:
2102
                $sql = "SELECT * FROM c_link WHERE c_id = $courseId AND id = $ref";
2103
                $data = $connection->fetchAssoc($sql);
2104
                if ($data) {
2105
                    $newId = $data['iid'];
2106
                }
2107
                break;
2108
            case TOOL_STUDENTPUBLICATION:
2109
                $sql = "SELECT * FROM c_student_publication WHERE c_id = $courseId AND id = $ref";
2110
                $data = $connection->fetchAssoc($sql);
2111
                if ($data) {
2112
                    $newId = $data['iid'];
2113
                }
2114
                break;
2115
            case TOOL_QUIZ:
2116
                $sql = "SELECT * FROM c_quiz WHERE c_id = $courseId AND id = $ref";
2117
                $data = $connection->fetchAssoc($sql);
2118
                if ($data) {
2119
                    $newId = $data['iid'];
2120
                }
2121
                break;
2122
            case TOOL_DOCUMENT:
2123
                $sql = "SELECT * FROM c_document WHERE c_id = $courseId AND id = $ref";
2124
                $data = $connection->fetchAssoc($sql);
2125
                if ($data) {
2126
                    $newId = $data['iid'];
2127
                }
2128
                break;
2129
            case TOOL_FORUM:
2130
                $sql = "SELECT * FROM c_forum_forum WHERE c_id = $courseId AND forum_id = $ref";
2131
                $data = $connection->fetchAssoc($sql);
2132
                if ($data) {
2133
                    $newId = $data['iid'];
2134
                }
2135
                break;
2136
            case 'thread':
2137
                $sql = "SELECT * FROM c_forum_thread WHERE c_id = $courseId AND thread_id = $ref";
2138
                $data = $connection->fetchAssoc($sql);
2139
                if ($data) {
2140
                    $newId = $data['iid'];
2141
                }
2142
                break;
2143
        }
2144
2145
        if (!empty($sql) && !empty($newId) && !empty($iid)) {
2146
            $sql = "UPDATE c_lp_item SET ref = $newId WHERE iid = $iid";
2147
            $connection->executeQuery($sql);
2148
        }
2149
    }
2150
2151
    // Set NULL if session = 0
2152
    $sql = "UPDATE c_item_property SET session_id = NULL WHERE session_id = 0";
2153
    $connection->executeQuery($sql);
2154
2155
    // Set NULL if group = 0
2156
    $sql = "UPDATE c_item_property SET to_group_id = NULL WHERE to_group_id = 0";
2157
    $connection->executeQuery($sql);
2158
2159
    // Set NULL if insert_user_id = 0
2160
    $sql = "UPDATE c_item_property SET insert_user_id = NULL WHERE insert_user_id = 0";
2161
    $connection->executeQuery($sql);
2162
2163
    // Delete session data of sessions that don't exist.
2164
    $sql = "DELETE FROM c_item_property
2165
            WHERE session_id IS NOT NULL AND session_id NOT IN (SELECT id FROM session)";
2166
    $connection->executeQuery($sql);
2167
2168
    // Delete group data of groups that don't exist.
2169
    $sql = "DELETE FROM c_item_property
2170
            WHERE to_group_id <> 0 AND to_group_id IS NOT NULL AND to_group_id NOT IN (SELECT DISTINCT iid FROM c_group_info)";
2171
    $connection->executeQuery($sql);
2172
    // This updates the group_id with c_group_info.iid instead of c_group_info.id
2173
2174
    if ($debug) {
2175
        error_log('update iids');
2176
    }
2177
2178
    $groupTableToFix = [
2179
        'c_group_rel_user',
2180
        'c_group_rel_tutor',
2181
        'c_permission_group',
2182
        'c_role_group',
2183
        'c_survey_invitation',
2184
        'c_attendance_calendar_rel_group',
2185
    ];
2186
2187
    foreach ($groupTableToFix as $table) {
2188
        $sql = "SELECT * FROM $table";
2189
        $result = $connection->fetchAll($sql);
2190
        foreach ($result as $item) {
2191
            $iid = $item['iid'];
2192
            $courseId = $item['c_id'];
2193
            $groupId = intval($item['group_id']);
2194
2195
            // Fix group id
2196
            if (!empty($groupId)) {
2197
                $sql = "SELECT * FROM c_group_info
2198
                        WHERE c_id = $courseId AND id = $groupId
2199
                        LIMIT 1";
2200
                $data = $connection->fetchAssoc($sql);
2201
                if (!empty($data)) {
2202
                    $newGroupId = $data['iid'];
2203
                    $sql = "UPDATE $table SET group_id = $newGroupId
2204
                            WHERE iid = $iid";
2205
                    $connection->executeQuery($sql);
2206
                } else {
2207
                    // The group does not exists clean this record
2208
                    $sql = "DELETE FROM $table WHERE iid = $iid";
2209
                    $connection->executeQuery($sql);
2210
                }
2211
            }
2212
        }
2213
    }
2214
2215
    // Fix c_item_property
2216
    if ($debug) {
2217
        error_log('update c_item_property');
2218
    }
2219
2220
    $sql = "SELECT * FROM course";
2221
    $courseList = $connection->fetchAll($sql);
2222
    if ($debug) {
2223
        error_log('Getting course list');
2224
    }
2225
2226
    $totalCourse = count($courseList);
2227
    $counter = 0;
2228
2229
    foreach ($courseList as $courseData) {
2230
        $courseId = $courseData['id'];
2231
        if ($debug) {
2232
            error_log('Updating course: '.$courseData['code']);
2233
        }
2234
2235
        $sql = "SELECT * FROM c_item_property WHERE c_id = $courseId";
2236
        $result = $connection->fetchAll($sql);
2237
        foreach ($result as $item) {
2238
            $sessionId = intval($item['session_id']);
2239
            $groupId = intval($item['to_group_id']);
2240
            $iid = $item['iid'];
2241
            $ref = $item['ref'];
2242
2243
            // Fix group id
2244
            // Commented group id is already fixed in Version20150603181728.php
2245
            /*if (!empty($groupId)) {
2246
                $sql = "SELECT * FROM c_group_info
2247
                        WHERE c_id = $courseId AND id = $groupId";
2248
                $data = $connection->fetchAssoc($sql);
2249
                if (!empty($data)) {
2250
                    $newGroupId = $data['iid'];
2251
                    $sql = "UPDATE c_item_property SET to_group_id = $newGroupId
2252
                            WHERE iid = $iid";
2253
                    $connection->executeQuery($sql);
2254
                } else {
2255
                    // The group does not exists clean this record
2256
                    $sql = "DELETE FROM c_item_property WHERE iid = $iid";
2257
                    $connection->executeQuery($sql);
2258
                }
2259
            }*/
2260
2261
            $sql = '';
2262
            $newId = '';
2263
            switch ($item['tool']) {
2264
                case TOOL_LINK:
2265
                    $sql = "SELECT * FROM c_link WHERE c_id = $courseId AND id = $ref ";
2266
                    break;
2267
                case TOOL_STUDENTPUBLICATION:
2268
                    $sql = "SELECT * FROM c_student_publication WHERE c_id = $courseId AND id = $ref";
2269
                    break;
2270
                case TOOL_QUIZ:
2271
                    $sql = "SELECT * FROM c_quiz WHERE c_id = $courseId AND id = $ref";
2272
                    break;
2273
                case TOOL_DOCUMENT:
2274
                    $sql = "SELECT * FROM c_document WHERE c_id = $courseId AND id = $ref";
2275
                    break;
2276
                case TOOL_FORUM:
2277
                    $sql = "SELECT * FROM c_forum_forum WHERE c_id = $courseId AND id = $ref";
2278
                    break;
2279
                case 'thread':
2280
                    $sql = "SELECT * FROM c_forum_thread WHERE c_id = $courseId AND id = $ref";
2281
                    break;
2282
            }
2283
2284
            if (!empty($sql) && !empty($newId)) {
2285
                $data = $connection->fetchAssoc($sql);
2286
                if (isset($data['iid'])) {
2287
                    $newId = $data['iid'];
2288
                }
2289
                $sql = "UPDATE c_item_property SET ref = $newId WHERE iid = $iid";
2290
                error_log($sql);
2291
                $connection->executeQuery($sql);
2292
            }
2293
        }
2294
2295
        if ($debug) {
2296
            // Print a status in the log once in a while
2297
            error_log("Course process #$counter/$totalCourse");
2298
        }
2299
        $counter++;
2300
    }
2301
2302
    if ($debug) {
2303
        error_log('update gradebook_link');
2304
    }
2305
2306
    // Fix gradebook_link
2307
    $sql = "SELECT * FROM gradebook_link";
2308
    $result = $connection->fetchAll($sql);
2309
    foreach ($result as $item) {
2310
        $courseCode = $item['course_code'];
2311
        $categoryId = (int) $item['category_id'];
2312
2313
        $sql = "SELECT * FROM course WHERE code = '$courseCode'";
2314
        $courseInfo = $connection->fetchAssoc($sql);
2315
        if (empty($courseInfo)) {
2316
            continue;
2317
        }
2318
2319
        $courseId = $courseInfo['id'];
2320
2321
        $ref = $item['ref_id'];
2322
        $iid = $item['id'];
2323
2324
        $sql = '';
2325
        switch ($item['type']) {
2326
            case LINK_LEARNPATH:
2327
                $sql = "SELECT * FROM c_link WHERE c_id = $courseId AND id = $ref ";
2328
                break;
2329
            case LINK_STUDENTPUBLICATION:
2330
                $sql = "SELECT * FROM c_student_publication WHERE c_id = $courseId AND id = $ref";
2331
                break;
2332
            case LINK_EXERCISE:
2333
                $sql = "SELECT * FROM c_quiz WHERE c_id = $courseId AND id = $ref";
2334
                break;
2335
            case LINK_ATTENDANCE:
2336
                //$sql = "SELECT * FROM c_document WHERE c_id = $courseId AND id = $ref";
2337
                break;
2338
            case LINK_FORUM_THREAD:
2339
                $sql = "SELECT * FROM c_forum_thread WHERE c_id = $courseId AND thread_id = $ref";
2340
                break;
2341
        }
2342
2343
        if (!empty($sql)) {
2344
            $data = $connection->fetchAssoc($sql);
2345
            if (isset($data) && isset($data['iid'])) {
2346
                $newId = $data['iid'];
2347
                $sql = "UPDATE gradebook_link SET ref_id = $newId
2348
                        WHERE id = $iid AND course_code = '$courseCode' AND category_id = $categoryId ";
2349
                $connection->executeQuery($sql);
2350
            }
2351
        }
2352
    }
2353
2354
    if ($debug) {
2355
        error_log('update groups');
2356
    }
2357
2358
    $sql = 'SELECT * FROM groups';
2359
    $result = $connection->executeQuery($sql);
2360
    $groups = $result->fetchAll();
2361
    $oldGroups = [];
2362
    if (!empty($groups)) {
2363
        foreach ($groups as $group) {
2364
            if (empty($group['name'])) {
2365
                continue;
2366
            }
2367
2368
            $params = [
2369
                'name' => $group['name'],
2370
                'description' => $group['description'],
2371
                'group_type' => 1,
2372
                'picture' => $group['picture_uri'],
2373
                'url' => $group['url'],
2374
                'visibility' => $group['visibility'],
2375
                'updated_at' => $group['updated_on'],
2376
                'created_at' => $group['created_on'],
2377
            ];
2378
            $connection->insert('usergroup', $params);
2379
            $id = $connection->lastInsertId('id');
2380
            $oldGroups[$group['id']] = $id;
2381
        }
2382
    }
2383
2384
    if (!empty($oldGroups)) {
2385
        error_log('Moving group files');
2386
        foreach ($oldGroups as $oldId => $newId) {
2387
            $path = get_group_picture_path_by_id(
2388
                $oldId,
2389
                'system'
2390
            );
2391
2392
            if (!empty($path)) {
2393
                $newPath = str_replace(
2394
                    "groups/$oldId/",
2395
                    "groups/$newId/",
2396
                    $path['dir']
2397
                );
2398
                $command = "mv {$path['dir']} $newPath ";
2399
                error_log("Executing $command");
2400
                system($command);
2401
            }
2402
        }
2403
2404
        $sql = "SELECT * FROM group_rel_user";
2405
        $result = $connection->executeQuery($sql);
2406
        $dataList = $result->fetchAll();
2407
2408
        if (!empty($dataList)) {
2409
            foreach ($dataList as $data) {
2410
                if (isset($oldGroups[$data['group_id']])) {
2411
                    $data['group_id'] = $oldGroups[$data['group_id']];
2412
                    $userId = $data['user_id'];
2413
2414
                    $sql = "SELECT id FROM user WHERE user_id = $userId";
2415
                    $userResult = $connection->executeQuery($sql);
2416
                    $userInfo = $userResult->fetch();
2417
                    if (empty($userInfo)) {
2418
                        continue;
2419
                    }
2420
2421
                    $sql = "INSERT INTO usergroup_rel_user (usergroup_id, user_id, relation_type)
2422
                            VALUES ('{$data['group_id']}', '{$userId}', '{$data['relation_type']}')";
2423
                    $connection->executeQuery($sql);
2424
                }
2425
            }
2426
        }
2427
2428
        $sql = "SELECT * FROM group_rel_group";
2429
        $result = $connection->executeQuery($sql);
2430
        $dataList = $result->fetchAll();
2431
2432
        if (!empty($dataList)) {
2433
            foreach ($dataList as $data) {
2434
                if (isset($oldGroups[$data['group_id']]) && isset($oldGroups[$data['subgroup_id']])) {
2435
                    $data['group_id'] = $oldGroups[$data['group_id']];
2436
                    $data['subgroup_id'] = $oldGroups[$data['subgroup_id']];
2437
                    $sql = "INSERT INTO usergroup_rel_usergroup (group_id, subgroup_id, relation_type)
2438
                            VALUES ('{$data['group_id']}', '{$data['subgroup_id']}', '{$data['relation_type']}')";
2439
                    $connection->executeQuery($sql);
2440
                }
2441
            }
2442
        }
2443
2444
        $sql = "SELECT * FROM announcement_rel_group";
2445
        $result = $connection->executeQuery($sql);
2446
        $dataList = $result->fetchAll();
2447
2448
        if (!empty($dataList)) {
2449
            foreach ($dataList as $data) {
2450
                if (isset($oldGroups[$data['group_id']])) {
2451
                    // Deleting relation
2452
                    $sql = "DELETE FROM announcement_rel_group WHERE group_id = {$data['group_id']}";
2453
                    $connection->executeQuery($sql);
2454
2455
                    // Add new relation
2456
                    $data['group_id'] = $oldGroups[$data['group_id']];
2457
                    $sql = "INSERT INTO announcement_rel_group(group_id, announcement_id)
2458
                            VALUES ('{$data['group_id']}', '{$data['announcement_id']}')";
2459
                    $connection->executeQuery($sql);
2460
                }
2461
            }
2462
        }
2463
2464
        $sql = "SELECT * FROM group_rel_tag";
2465
        $result = $connection->executeQuery($sql);
2466
        $dataList = $result->fetchAll();
2467
        if (!empty($dataList)) {
2468
            foreach ($dataList as $data) {
2469
                if (isset($oldGroups[$data['group_id']])) {
2470
                    $data['group_id'] = $oldGroups[$data['group_id']];
2471
                    $sql = "INSERT INTO usergroup_rel_tag (tag_id, usergroup_id)
2472
                            VALUES ('{$data['tag_id']}', '{$data['group_id']}')";
2473
                    $connection->executeQuery($sql);
2474
                }
2475
            }
2476
        }
2477
    }
2478
2479
    if ($debug) {
2480
        error_log('update extra fields');
2481
    }
2482
2483
    // Extra fields
2484
    $extraFieldTables = [
2485
        ExtraField::USER_FIELD_TYPE => Database::get_main_table(TABLE_MAIN_USER_FIELD),
2486
        ExtraField::COURSE_FIELD_TYPE => Database::get_main_table(TABLE_MAIN_COURSE_FIELD),
2487
        //ExtraField::LP_FIELD_TYPE => Database::get_main_table(TABLE_MAIN_LP_FIELD),
2488
        ExtraField::SESSION_FIELD_TYPE => Database::get_main_table(TABLE_MAIN_SESSION_FIELD),
2489
        //ExtraField::CALENDAR_FIELD_TYPE => Database::get_main_table(TABLE_MAIN_CALENDAR_EVENT_FIELD),
2490
        //ExtraField::QUESTION_FIELD_TYPE => Database::get_main_table(TABLE_MAIN_CALENDAR_EVENT_FIELD),
2491
        //ExtraField::USER_FIELD_TYPE => //Database::get_main_table(TABLE_MAIN_SPECIFIC_FIELD),
2492
    ];
2493
2494
    foreach ($extraFieldTables as $type => $table) {
2495
        $sql = "SELECT * FROM $table ";
2496
        if ($debug) {
2497
            error_log($sql);
2498
        }
2499
        $result = $connection->query($sql);
2500
        $fields = $result->fetchAll();
2501
2502
        foreach ($fields as $field) {
2503
            if ($debug) {
2504
                error_log("Loading field: ".$field['field_variable']);
2505
            }
2506
            $originalId = $field['id'];
2507
2508
            $params = [
2509
                'extra_field_type' => $type,
2510
                'variable' => $field['field_variable'],
2511
                'field_type' => $field['field_type'],
2512
                'display_text' => $field['field_display_text'],
2513
                'default_value' => $field['field_default_value'],
2514
                'field_order' => $field['field_order'],
2515
                'visible' => $field['field_visible'],
2516
                'changeable' => $field['field_changeable'],
2517
                'filter' => $field['field_filter'],
2518
            ];
2519
2520
            $connection->insert('extra_field', $params);
2521
            $newExtraFieldId = $connection->lastInsertId();
2522
2523
            $values = [];
2524
            $handlerId = null;
2525
            switch ($type) {
2526
                case ExtraField::USER_FIELD_TYPE:
2527
                    $optionTable = Database::get_main_table(
2528
                        TABLE_MAIN_USER_FIELD_OPTIONS
2529
                    );
2530
                    $valueTable = Database::get_main_table(
2531
                        TABLE_MAIN_USER_FIELD_VALUES
2532
                    );
2533
                    $handlerId = 'user_id';
2534
                    break;
2535
                case ExtraField::COURSE_FIELD_TYPE:
2536
                    $optionTable = Database::get_main_table(
2537
                        TABLE_MAIN_COURSE_FIELD_OPTIONS
2538
                    );
2539
                    $valueTable = Database::get_main_table(
2540
                        TABLE_MAIN_COURSE_FIELD_VALUES
2541
                    );
2542
                    $handlerId = 'c_id';
2543
                    break;
2544
                case ExtraField::SESSION_FIELD_TYPE:
2545
                    $optionTable = Database::get_main_table(
2546
                        TABLE_MAIN_SESSION_FIELD_OPTIONS
2547
                    );
2548
                    $valueTable = Database::get_main_table(
2549
                        TABLE_MAIN_SESSION_FIELD_VALUES
2550
                    );
2551
                    $handlerId = 'session_id';
2552
                    break;
2553
            }
2554
2555
            if (!empty($optionTable)) {
2556
                $sql = "SELECT * FROM $optionTable WHERE field_id = $originalId ";
2557
                $result = $connection->query($sql);
2558
                $options = $result->fetchAll();
2559
2560
                foreach ($options as $option) {
2561
                    $params = [
2562
                        'display_text' => $option['option_display_text'],
2563
                        'field_id' => $newExtraFieldId,
2564
                        'option_order' => $option['option_order'],
2565
                        'option_value' => $option['option_value'],
2566
                    ];
2567
                    $connection->insert('extra_field_options', $params);
2568
                }
2569
2570
                $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...
2571
                $result = $connection->query($sql);
2572
                $values = $result->fetchAll();
2573
                if ($debug) {
2574
                    error_log("Fetch all values for field");
2575
                }
2576
            }
2577
2578
            if (!empty($values)) {
2579
                if ($debug) {
2580
                    error_log("Saving field value in new table");
2581
                }
2582
                $k = 0;
2583
                foreach ($values as $value) {
2584
                    if (isset($value[$handlerId])) {
2585
                        // Insert without the use of the entity as it reduces
2586
                        // speed to 2 records per second (much too slow)
2587
                        $params = [
2588
                            'field_id' => $newExtraFieldId,
2589
                            'value' => $value['field_value'],
2590
                            'item_id' => $value[$handlerId],
2591
                        ];
2592
                        $connection->insert('extra_field_values', $params);
2593
                        if ($debug && ($k % 10000 == 0)) {
2594
                            error_log("Saving field $k");
2595
                        }
2596
                        $k++;
2597
                    }
2598
                }
2599
            }
2600
        }
2601
    }
2602
2603
    if ($debug) {
0 ignored issues
show
introduced by
$debug is of type mixed, thus it always evaluated to true.
Loading history...
2604
        error_log('Remove index');
2605
    }
2606
2607
    // Drop temporary indexes added to increase speed of this function's queries
2608
    $sql = "ALTER TABLE c_document DROP INDEX tmpidx_doc";
2609
    $connection->executeQuery($sql);
2610
    $sql = "ALTER TABLE c_student_publication DROP INDEX tmpidx_stud";
2611
    $connection->executeQuery($sql);
2612
    $sql = "ALTER TABLE c_quiz DROP INDEX tmpidx_quiz";
2613
    $connection->executeQuery($sql);
2614
    $sql = "ALTER TABLE c_item_property DROP INDEX tmpidx_ip";
2615
    $connection->executeQuery($sql);
2616
2617
    if ($debug) {
2618
        error_log('Finish fixId function');
2619
    }
2620
2621
    fixLpId($connection, true);
2622
}
2623
2624
/**
2625
 * @param \Doctrine\DBAL\Connection $connection
2626
 * @param $debug
2627
 *
2628
 * @throws \Doctrine\DBAL\DBALException
2629
 */
2630
function fixLpId($connection, $debug)
2631
{
2632
    if ($debug) {
2633
        error_log('Fix lp.id lp.iids');
2634
    }
2635
2636
    $sql = 'SELECT id, title, code FROM course';
2637
    $result = $connection->query($sql);
2638
    $courses = $result->fetchAll();
2639
2640
    $sql = 'SELECT id FROM session';
2641
    $result = $connection->query($sql);
2642
    $sessions = $result->fetchAll();
2643
2644
    $tblCLp = Database::get_course_table(TABLE_LP_MAIN);
2645
    $tblCLpItem = Database::get_course_table(TABLE_LP_ITEM);
2646
    $toolTable = Database::get_course_table(TABLE_TOOL_LIST);
2647
2648
    if (!empty($sessions)) {
2649
        $sessions = array_column($sessions, 'id');
2650
        $sessions[] = 0;
2651
    } else {
2652
        $sessions = [0];
2653
    }
2654
2655
    foreach ($courses as $course) {
2656
        $courseId = $course['id'];
2657
        $sql = "SELECT * FROM $tblCLp WHERE c_id = $courseId AND iid <> id ORDER by iid";
2658
        $result = $connection->query($sql);
2659
        if ($debug) {
2660
            error_log('-------------');
2661
            error_log("Entering Lps in course #$courseId");
2662
            error_log($sql);
2663
        }
2664
        $lpList = $result->fetchAll();
2665
        $myOnlyLpList = [];
2666
        if (!empty($lpList)) {
2667
            foreach ($lpList as $lpInfo) {
2668
                $oldId = $lpInfo['id'];
2669
                $sql = "SELECT * FROM $tblCLpItem WHERE c_id = $courseId AND lp_id = $oldId ORDER by iid";
2670
                $result = $connection->query($sql);
2671
                $items = $result->fetchAll();
2672
                $lpInfo['lp_list'] = $items;
2673
                $myOnlyLpList[] = $lpInfo;
2674
            }
2675
        }
2676
2677
        if (!empty($myOnlyLpList)) {
2678
            foreach ($myOnlyLpList as $lpInfo) {
2679
                $lpIid = $lpInfo['iid'];
2680
                $oldId = $lpInfo['id'];
2681
                $items = $lpInfo['lp_list'];
2682
2683
                if (empty($items)) {
2684
                    continue;
2685
                }
2686
                $itemList = [];
2687
                foreach ($items as $subItem) {
2688
                    $itemList[$subItem['id']] = $subItem['iid'];
2689
                }
2690
                $variablesToFix = [
2691
                    'parent_item_id',
2692
                    'next_item_id',
2693
                    'prerequisite',
2694
                    'previous_item_id',
2695
                ];
2696
2697
                foreach ($sessions as $sessionId) {
2698
                    $correctLink = "lp/lp_controller.php?action=view&lp_id=$lpIid&id_session=$sessionId";
2699
                    $link = "newscorm/lp_controller.php?action=view&lp_id=$oldId&id_session=$sessionId";
2700
                    $secondLink = "lp/lp_controller.php?action=view&lp_id=$oldId&id_session=$sessionId";
2701
                    $sql = "UPDATE $toolTable 
2702
                        SET link = '$correctLink'
2703
                        WHERE c_id = $courseId AND (link = '$link' OR link ='$secondLink')";
2704
                    $connection->query($sql);
2705
                }
2706
2707
                foreach ($items as $item) {
2708
                    $itemIid = $item['iid'];
2709
                    $itemId = $item['id'];
2710
                    foreach ($variablesToFix as $variable) {
2711
                        if (!empty($item[$variable]) && isset($itemList[$item[$variable]])) {
2712
                            $newId = $itemList[$item[$variable]];
2713
                            $sql = "UPDATE $tblCLpItem SET $variable = $newId 
2714
                                    WHERE iid = $itemIid AND c_id = $courseId AND lp_id = $oldId";
2715
                            $connection->query($sql);
2716
                        }
2717
                    }
2718
2719
                    if ($item['item_type'] == 'document' && !empty($item['path'])) {
2720
                        $oldDocumentId = $item['path'];
2721
                        $sql = "SELECT * FROM c_document WHERE c_id = $courseId AND id = $oldDocumentId";
2722
                        $result = $connection->query($sql);
2723
                        $document = $result->fetch();
2724
                        if (!empty($document)) {
2725
                            $newDocumentId = $document['iid'];
2726
                            if (!empty($newDocumentId)) {
2727
                                $sql = "UPDATE $tblCLpItem SET path = $newDocumentId 
2728
                                    WHERE iid = $itemIid AND c_id = $courseId";
2729
                                $connection->query($sql);
2730
                                if ($debug) {
2731
                                    //error_log("Fix document: ");
2732
                                    //error_log($sql);
2733
                                }
2734
                            }
2735
                        }
2736
                    }
2737
2738
                    // c_lp_view
2739
                    $sql = "UPDATE c_lp_view SET last_item = $itemIid 
2740
                            WHERE c_id = $courseId AND last_item = $itemId AND lp_id = $oldId";
2741
                    $connection->query($sql);
2742
2743
                    // c_lp_item_view
2744
                    $sql = "UPDATE c_lp_item_view SET lp_item_id = $itemIid 
2745
                            WHERE c_id = $courseId AND lp_item_id = $itemId";
2746
                    $connection->query($sql);
2747
2748
                    // Update track_exercises
2749
                    $sql = "UPDATE track_e_exercises SET orig_lp_item_id = $itemIid 
2750
                            WHERE c_id = $courseId AND orig_lp_id = $oldId AND orig_lp_item_id = $itemId";
2751
                    $connection->query($sql);
2752
2753
                    // c_forum_thread
2754
                    $sql = "UPDATE c_forum_thread SET lp_item_id = $itemIid 
2755
                            WHERE c_id = $courseId AND lp_item_id = $itemId";
2756
                    $connection->query($sql);
2757
2758
                    // orig_lp_item_view_id
2759
                    $sql = "SELECT * FROM c_lp_view
2760
                            WHERE c_id = $courseId AND lp_id = $oldId";
2761
                    $result = $connection->query($sql);
2762
                    $itemViewList = $result->fetchAll();
2763
                    if ($itemViewList) {
2764
                        foreach ($itemViewList as $itemView) {
2765
                            $userId = $itemView['user_id'];
2766
                            $oldItemViewId = $itemView['id'];
2767
                            $newItemView = $itemView['iid'];
2768
2769
                            if (empty($oldItemViewId)) {
2770
                                continue;
2771
                            }
2772
2773
                            $sql = "UPDATE track_e_exercises 
2774
                                SET orig_lp_item_view_id = $newItemView 
2775
                                WHERE 
2776
                                  c_id = $courseId AND 
2777
                                  orig_lp_id = $oldId AND 
2778
                                  orig_lp_item_id = $itemIid AND 
2779
                                  orig_lp_item_view_id = $oldItemViewId AND 
2780
                                  exe_user_id = $userId                                       
2781
                                  ";
2782
                            $connection->query($sql);
2783
2784
                            /*$sql = "UPDATE c_lp_item_view
2785
                                    SET lp_view_id = $newItemView
2786
                                    WHERE
2787
                                      lp_view_id = $oldItemViewId AND
2788
                                      c_id = $courseId
2789
                                  ";
2790
                            $connection->query($sql);*/
2791
                        }
2792
                    }
2793
2794
                    $sql = "UPDATE $tblCLpItem SET lp_id = $lpIid 
2795
                            WHERE c_id = $courseId AND lp_id = $oldId AND id = $itemId";
2796
                    $connection->query($sql);
2797
2798
                    $sql = "UPDATE $tblCLpItem SET id = iid 
2799
                            WHERE c_id = $courseId AND lp_id = $oldId AND id = $itemId";
2800
                    $connection->query($sql);
2801
                }
2802
2803
                $sql = "UPDATE c_lp_view SET lp_id = $lpIid WHERE c_id = $courseId AND lp_id = $oldId";
2804
                $connection->query($sql);
2805
2806
                $sql = "UPDATE c_forum_forum SET lp_id = $lpIid WHERE c_id = $courseId AND lp_id = $oldId";
2807
                $connection->query($sql);
2808
2809
                // Update track_exercises.
2810
                $sql = "UPDATE track_e_exercises SET orig_lp_id = $lpIid 
2811
                        WHERE c_id = $courseId AND orig_lp_id = $oldId";
2812
                $connection->query($sql);
2813
2814
                $sql = "UPDATE $tblCLp SET id = iid WHERE c_id = $courseId AND id = $oldId ";
2815
                $connection->query($sql);
2816
            }
2817
        }
2818
    }
2819
2820
    if ($debug) {
2821
        error_log('END Fix lp.id lp.iids');
2822
    }
2823
}
2824
2825
/**
2826
 * @param string $distFile
2827
 * @param string $envFile
2828
 * @param array  $params
2829
 */
2830
function updateEnvFile($distFile, $envFile, $params)
2831
{
2832
    $requirements = [
2833
        'DATABASE_HOST',
2834
        'DATABASE_PORT',
2835
        'DATABASE_NAME',
2836
        'DATABASE_USER',
2837
        'DATABASE_PASSWORD',
2838
        'APP_INSTALLED',
2839
        'APP_ENCRYPT_METHOD',
2840
        'APP_URL_APPEND',
2841
    ];
2842
2843
    foreach ($requirements as $requirement) {
2844
        if (!isset($params['{{'.$requirement.'}}'])) {
2845
            throw new \Exception("The parameter $requirement is needed in order to edit the .env.local file");
2846
        }
2847
    }
2848
2849
    $contents = file_get_contents($distFile);
2850
    $contents = str_replace(array_keys($params), array_values($params), $contents);
2851
    file_put_contents($envFile, $contents);
2852
}
2853
2854
/**
2855
 * @param SymfonyContainer $container
2856
 * @param EntityManager    $manager
2857
 */
2858
function installGroups($container, $manager)
2859
{
2860
    // Creating fos_group (groups and roles)
2861
    $groupManager = $container->get('fos_user.group_manager');
2862
    $groups = [
2863
        [
2864
            'code' => 'ADMIN',
2865
            'title' => 'Administrators',
2866
            'roles' => ['ROLE_ADMIN'],
2867
        ],
2868
        [
2869
            'code' => 'STUDENT',
2870
            'title' => 'Students',
2871
            'roles' => ['ROLE_STUDENT'],
2872
        ],
2873
        [
2874
            'code' => 'TEACHER',
2875
            'title' => 'Teachers',
2876
            'roles' => ['ROLE_TEACHER'],
2877
        ],
2878
        [
2879
            'code' => 'RRHH',
2880
            'title' => 'Human resources manager',
2881
            'roles' => ['ROLE_RRHH'],
2882
        ],
2883
        [
2884
            'code' => 'SESSION_MANAGER',
2885
            'title' => 'Session',
2886
            'roles' => ['ROLE_SESSION_MANAGER'],
2887
        ],
2888
        [
2889
            'code' => 'QUESTION_MANAGER',
2890
            'title' => 'Question manager',
2891
            'roles' => ['ROLE_QUESTION_MANAGER'],
2892
        ],
2893
        [
2894
            'code' => 'STUDENT_BOSS',
2895
            'title' => 'Student boss',
2896
            'roles' => ['ROLE_STUDENT_BOSS'],
2897
        ],
2898
        [
2899
            'code' => 'INVITEE',
2900
            'title' => 'Invitee',
2901
            'roles' => ['ROLE_INVITEE'],
2902
        ],
2903
    ];
2904
2905
    foreach ($groups as $groupData) {
2906
        $criteria = ['code' => $groupData['code']];
2907
        $groupExists = $groupManager->findGroupBy($criteria);
2908
        if (!$groupExists) {
2909
            $group = $groupManager->createGroup($groupData['title']);
2910
            $group->setCode($groupData['code']);
2911
            foreach ($groupData['roles'] as $role) {
2912
                $group->addRole($role);
2913
            }
2914
            $manager->persist($group);
2915
            $groupManager->updateGroup($group, true);
2916
        }
2917
    }
2918
}
2919
2920
/**
2921
 * @param SymfonyContainer $container
2922
 */
2923
function installPages($container)
2924
{
2925
    error_log('installPages');
2926
2927
    $siteManager = Container::getSiteManager();
2928
2929
    // Create site
2930
    /** @var Chamilo\PageBundle\Entity\Site $site */
2931
    $site = $siteManager->create();
2932
    $site->setHost('localhost');
2933
    $site->setEnabled(true);
2934
    $site->setName('localhost');
2935
    $site->setEnabledFrom(new \DateTime('now'));
2936
    $site->setEnabledTo(new \DateTime('+20 years'));
2937
    $site->setRelativePath('');
2938
    $site->setIsDefault(true);
2939
    $site->setLocale('en');
2940
    $siteManager->save($site);
2941
2942
    // Create home page
2943
    /** @var PageManager $pageManager */
2944
    $pageManager = $container->get('sonata.page.manager.page');
2945
    /** @var \Sonata\PageBundle\Model\Page $page */
2946
    $page = $pageManager->create();
2947
    $page->setSlug('homepage');
2948
    $page->setUrl('/');
2949
    $page->setName('homepage');
2950
    $page->setTitle('home');
2951
    $page->setEnabled(true);
2952
    $page->setDecorate(1);
2953
    $page->setRequestMethod('GET|POST|HEAD|DELETE|PUT');
2954
    $page->setTemplateCode('default');
2955
    $page->setRouteName('homepage');
2956
    //$page->setParent($this->getReference('page-homepage'));
2957
    $page->setSite($site);
2958
    $pageManager->save($page);
2959
2960
    // Create welcome page
2961
    $pageWelcome = $pageManager->create();
2962
    $pageWelcome->setSlug('welcome');
2963
    $pageWelcome->setUrl('/welcome');
2964
    $pageWelcome->setName('welcome');
2965
    $pageWelcome->setTitle('welcome');
2966
    $pageWelcome->setEnabled(true);
2967
    $pageWelcome->setDecorate(1);
2968
    $pageWelcome->setRequestMethod('GET');
2969
    $pageWelcome->setTemplateCode('default');
2970
    $pageWelcome->setRouteName('welcome');
2971
    $pageWelcome->setParent($page);
2972
    $pageWelcome->setSite($site);
2973
    $pageManager->save($pageWelcome);
2974
2975
    // Creating page blocks
2976
    $templateManager = $container->get('sonata.page.template_manager');
2977
    $template = $templateManager->get('default');
2978
    $templateContainers = $template->getContainers();
2979
2980
    $containers = [];
2981
    foreach ($templateContainers as $id => $area) {
2982
        $containers[$id] = [
2983
            'area' => $area,
2984
            'block' => false,
2985
        ];
2986
    }
2987
2988
    // Create blocks for this page
2989
    $blockInteractor = $container->get('sonata.page.block_interactor');
2990
    $parentBlock = null;
2991
    foreach ($containers as $id => $area) {
2992
        if (false === $area['block'] && $templateContainers[$id]['shared'] === false) {
2993
            $block = $blockInteractor->createNewContainer(
2994
                [
2995
                    'page' => $pageWelcome,
2996
                    'name' => $templateContainers[$id]['name'],
2997
                    'code' => $id,
2998
                ]
2999
            );
3000
3001
            if ($id === 'content' && $templateContainers[$id]['name'] == 'Main content') {
3002
                $parentBlock = $block;
3003
            }
3004
        }
3005
    }
3006
3007
    // Create block in main content
3008
    $block = $container->get('sonata.page.manager.block');
3009
    /** @var \Sonata\BlockBundle\Model\Block $myBlock */
3010
    $myBlock = $block->create();
3011
    $myBlock->setType('sonata.formatter.block.formatter');
3012
    $myBlock->setSetting('format', 'richhtml');
3013
    $myBlock->setSetting('content', '');
3014
    $myBlock->setSetting('rawContent', '');
3015
    $myBlock->setSetting('template', '@SonataFormatter/Block/block_formatter.html.twig');
3016
    $myBlock->setParent($parentBlock);
3017
    $pageWelcome->addBlocks($myBlock);
3018
    $pageManager->save($pageWelcome);
3019
}
3020
3021
/**
3022
 * @param SymfonyContainer $container
3023
 * @param EntityManager    $manager
3024
 * @param bool             $upgrade
3025
 */
3026
function installSchemas($container, $manager, $upgrade = false)
3027
{
3028
    error_log('installSchemas');
3029
    $settingsManager = Container::getSettingsManager();
3030
3031
    // Install course tools (table "tool")
3032
    $toolChain = $container->get('chamilo_core.tool_chain');
3033
    $toolChain->createTools($manager);
3034
3035
    $urlRepo = $container->get('Chamilo\CoreBundle\Repository\AccessUrlRepository');
3036
    $accessUrl = $urlRepo->find(1);
3037
    if (!$accessUrl) {
3038
        $em = $urlRepo->getEntityManager();
3039
3040
        // Creating AccessUrl
3041
        $accessUrl = new AccessUrl();
3042
        $accessUrl
3043
            ->setUrl('http://localhost/')
3044
            ->setDescription('')
3045
            ->setActive(1)
3046
        ;
3047
        $em->persist($accessUrl);
3048
        $em->flush();
3049
    }
3050
3051
    if ($upgrade) {
3052
        $settingsManager->updateSchemas($accessUrl);
3053
    } else {
3054
        // Installing schemas (filling settings_current table)
3055
        $settingsManager->installSchemas($accessUrl);
3056
    }
3057
}
3058
3059
/**
3060
 * @param SymfonyContainer $container
3061
 */
3062
function upgradeWithContainer($container)
3063
{
3064
    Container::setContainer($container);
3065
    Container::setLegacyServices($container, false);
3066
    error_log('setLegacyServices');
3067
    $manager = Database::getManager();
3068
    installGroups($container, $manager);
3069
    error_log('installGroups');
3070
    // @todo check if adminId = 1
3071
    installSchemas($container, $manager, 1, true);
3072
    installPages($container);
3073
    fixMedia($container);
3074
}
3075
3076
function fixMedia($container)
3077
{
3078
    error_log('fix medias');
3079
    $pool = $container->get('sonata.media.pool');
3080
    $contextManager = $container->get('sonata.classification.manager.context');
3081
    $categoryManager = $container->get('sonata.media.manager.category');
3082
3083
    foreach ($pool->getContexts() as $context => $contextAttrs) {
3084
        /** @var ContextInterface $defaultContext */
3085
        $defaultContext = $contextManager->findOneBy([
3086
            'id' => $context,
3087
        ]);
3088
3089
        if (!$defaultContext) {
3090
            $defaultContext = $contextManager->create();
3091
            $defaultContext->setId($context);
3092
            $defaultContext->setName(ucfirst($context));
3093
            $defaultContext->setEnabled(true);
3094
3095
            $contextManager->save($defaultContext);
3096
        }
3097
3098
        $defaultCategory = $categoryManager->getRootCategory($defaultContext);
3099
3100
        if (!$defaultCategory) {
3101
            $defaultCategory = $categoryManager->create();
3102
            $defaultCategory->setContext($defaultContext);
3103
            $defaultCategory->setName(ucfirst($context));
3104
            $defaultCategory->setEnabled(true);
3105
            $defaultCategory->setPosition(0);
3106
3107
            $categoryManager->save($defaultCategory);
3108
        }
3109
    }
3110
}
3111
3112
/**
3113
 * After the schema was created (table creation), the function adds
3114
 * admin/platform information.
3115
 *
3116
 * @param \Psr\Container\ContainerInterface $container
3117
 * @param string                            $sysPath
3118
 * @param string                            $encryptPassForm
3119
 * @param string                            $passForm
3120
 * @param string                            $adminLastName
3121
 * @param string                            $adminFirstName
3122
 * @param string                            $loginForm
3123
 * @param string                            $emailForm
3124
 * @param string                            $adminPhoneForm
3125
 * @param string                            $languageForm
3126
 * @param string                            $institutionForm
3127
 * @param string                            $institutionUrlForm
3128
 * @param string                            $siteName
3129
 * @param string                            $allowSelfReg
3130
 * @param string                            $allowSelfRegProf
3131
 * @param string                            $installationProfile Installation profile, if any was provided
3132
 */
3133
function finishInstallationWithContainer(
3134
    $container,
3135
    $sysPath,
3136
    $encryptPassForm,
3137
    $passForm,
3138
    $adminLastName,
3139
    $adminFirstName,
3140
    $loginForm,
3141
    $emailForm,
3142
    $adminPhoneForm,
3143
    $languageForm,
3144
    $institutionForm,
3145
    $institutionUrlForm,
3146
    $siteName,
3147
    $allowSelfReg,
3148
    $allowSelfRegProf,
3149
    $installationProfile = ''
3150
) {
3151
    $sysPath = !empty($sysPath) ? $sysPath : api_get_path(SYS_PATH);
3152
    Container::setContainer($container);
3153
    Container::setLegacyServices($container, false);
3154
3155
    $manager = Database::getManager();
3156
    $connection = $manager->getConnection();
3157
    $sql = getVersionTable();
3158
3159
    // Add version table
3160
    $connection->executeQuery($sql);
3161
3162
    // Add tickets defaults
3163
    $ticketProject = new TicketProject();
3164
    $ticketProject
3165
        ->setId(1)
3166
        ->setName('Ticket System')
3167
        ->setInsertUserId(1);
3168
3169
    $manager->persist($ticketProject);
3170
    $manager->flush();
3171
3172
    $trans = $container->get('translator');
3173
3174
    $categories = [
3175
        $trans->trans('Enrollment') => $trans->trans('Tickets about enrollment'),
3176
        $trans->trans('General information') => $trans->trans('Tickets about general information'),
3177
        $trans->trans('Requests and paperwork') => $trans->trans('Tickets about requests and paperwork'),
3178
        $trans->trans('Academic Incidents') => $trans->trans('Tickets about academic incidents, like exams, practices, tasks, etc.'),
3179
        $trans->trans('Virtual campus') => $trans->trans('Tickets about virtual campus'),
3180
        $trans->trans('Online evaluation') => $trans->trans('Tickets about online evaluation'),
3181
    ];
3182
3183
    $i = 1;
3184
    foreach ($categories as $category => $description) {
3185
        // Online evaluation requires a course
3186
        $ticketCategory = new TicketCategory();
3187
        $ticketCategory
3188
            ->setId($i)
3189
            ->setName($category)
3190
            ->setDescription($description)
3191
            ->setProject($ticketProject)
3192
            ->setInsertUserId(1);
3193
3194
        $isRequired = $i == 6;
3195
        $ticketCategory->setCourseRequired($isRequired);
3196
3197
        $manager->persist($ticketCategory);
3198
        $manager->flush();
3199
3200
        $i++;
3201
    }
3202
3203
    // Default Priorities
3204
    $defaultPriorities = [
3205
        TicketManager::PRIORITY_NORMAL => $trans->trans('Normal'),
3206
        TicketManager::PRIORITY_HIGH => $trans->trans('High'),
3207
        TicketManager::PRIORITY_LOW => $trans->trans('Low'),
3208
    ];
3209
3210
    $i = 1;
3211
    foreach ($defaultPriorities as $code => $priority) {
3212
        $ticketPriority = new TicketPriority();
3213
        $ticketPriority
3214
            ->setId($i)
3215
            ->setName($priority)
3216
            ->setCode($code)
3217
            ->setInsertUserId(1);
3218
3219
        $manager->persist($ticketPriority);
3220
        $manager->flush();
3221
        $i++;
3222
    }
3223
3224
    $table = Database::get_main_table(TABLE_TICKET_STATUS);
3225
3226
    // Default status
3227
    $defaultStatus = [
3228
        TicketManager::STATUS_NEW => $trans->trans('New'),
3229
        TicketManager::STATUS_PENDING => $trans->trans('Pending'),
3230
        TicketManager::STATUS_UNCONFIRMED => $trans->trans('Unconfirmed'),
3231
        TicketManager::STATUS_CLOSE => $trans->trans('Close'),
3232
        TicketManager::STATUS_FORWARDED => $trans->trans('Forwarded'),
3233
    ];
3234
3235
    $i = 1;
3236
    foreach ($defaultStatus as $code => $status) {
3237
        $attributes = [
3238
            'id' => $i,
3239
            'code' => $code,
3240
            'name' => $status,
3241
        ];
3242
        Database::insert($table, $attributes);
3243
        $i++;
3244
    }
3245
3246
    installGroups($container, $manager);
3247
    installSchemas($container, $manager, false);
3248
    installPages($container);
3249
3250
    // Inserting default data
3251
    $data = file_get_contents($sysPath.'main/install/data.sql');
3252
    $result = $manager->getConnection()->prepare($data);
3253
    $result->execute();
3254
    $result->closeCursor();
3255
3256
    UserManager::setPasswordEncryption($encryptPassForm);
3257
3258
    // Create admin user.
3259
    $adminId = @UserManager::create_user(
3260
        $adminFirstName,
3261
        $adminLastName,
3262
        1,
3263
        $emailForm,
3264
        $loginForm,
3265
        $passForm,
3266
        'ADMIN', //$official_code = '',
3267
        $languageForm,
3268
        $adminPhoneForm,
3269
        '', //$picture_uri = '',
3270
        PLATFORM_AUTH_SOURCE,
3271
        '', //$expirationDate,
3272
        1,
3273
        0,
3274
        null,
3275
        '',
3276
        false, //$send_mail = false,
3277
        true, //$isAdmin = false
3278
        '',
3279
        false,
3280
        '',
3281
        1
3282
    );
3283
3284
    // Create anonymous user.
3285
    @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

3285
    /** @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...
3286
        'Joe',
3287
        'Anonymous',
3288
        6,
3289
        'anonymous@localhost',
3290
        'anon',
3291
        'anon',
3292
        'anonymous', //$official_code = '',
3293
        $languageForm,
3294
        '',
3295
        '', //$picture_uri = '',
3296
        PLATFORM_AUTH_SOURCE,
3297
        '',
3298
        1,
3299
        0,
3300
        null,
3301
        '',
3302
        false, //$send_mail = false,
3303
        false, //$isAdmin = false
3304
        '',
3305
        false,
3306
        '',
3307
        1
3308
    );
3309
3310
    // Set default language
3311
    Database::update(
3312
        Database::get_main_table(TABLE_MAIN_LANGUAGE),
3313
        ['available' => 1],
3314
        ['dokeos_folder = ?' => $languageForm]
3315
    );
3316
3317
    $userManager = Container::getUserManager();
3318
    $urlRepo = $container->get('Chamilo\CoreBundle\Repository\AccessUrlRepository');
3319
    $accessUrl = $urlRepo->find(1);
3320
    $admin = $userManager->find($adminId);
3321
    $urlRepo->addResourceNode($accessUrl, $admin);
3322
3323
    // Install settings
3324
    installSettings(
3325
        $institutionForm,
3326
        $institutionUrlForm,
3327
        $siteName,
3328
        $emailForm,
3329
        $adminLastName,
3330
        $adminFirstName,
3331
        $languageForm,
3332
        $allowSelfReg,
3333
        $allowSelfRegProf,
3334
        $installationProfile
3335
    );
3336
3337
    lockSettings();
3338
    updateDirAndFilesPermissions();
3339
    fixMedia($container);
3340
3341
    // Set the latest version
3342
    /*$path = $sysPath.'app/Migrations/Schema/V111/';
3343
    $finder = new \Symfony\Component\Finder\Finder();
3344
    $files = $finder->files()->in($path);
3345
3346
    // Needed for chash
3347
    createVersionTable();
3348
3349
    foreach ($files as $version) {
3350
        $version = str_replace(['Version', '.php'], '', $version->getFilename());
3351
        $sql = "INSERT INTO version (version) VALUES ('$version')";
3352
        Database::query($sql);
3353
    }*/
3354
}
3355
3356
/**
3357
 * Creates 'version' table.
3358
 */
3359
function createVersionTable()
3360
{
3361
    $sql = getVersionTable();
3362
    Database::query($sql);
3363
}
3364
3365
/**
3366
 * Get version creation table query.
3367
 *
3368
 * @return string
3369
 */
3370
function getVersionTable()
3371
{
3372
    return 'CREATE TABLE IF NOT EXISTS version (id int unsigned NOT NULL AUTO_INCREMENT, version varchar(20), PRIMARY KEY(id), UNIQUE(version));';
3373
}
3374
3375
/**
3376
 * Update settings based on installation profile defined in a JSON file.
3377
 *
3378
 * @param string $installationProfile The name of the JSON file in main/install/profiles/ folder
3379
 *
3380
 * @return bool false on failure (no bad consequences anyway, just ignoring profile)
3381
 */
3382
function installProfileSettings($installationProfile = '')
3383
{
3384
    if (empty($installationProfile)) {
3385
        return false;
3386
    }
3387
    $jsonPath = api_get_path(SYS_PATH).'main/install/profiles/'.$installationProfile.'.json';
3388
    // Make sure the path to the profile is not hacked
3389
    if (!Security::check_abs_path($jsonPath, api_get_path(SYS_PATH).'main/install/profiles/')) {
3390
        return false;
3391
    }
3392
    if (!is_file($jsonPath)) {
3393
        return false;
3394
    }
3395
    if (!is_readable($jsonPath)) {
3396
        return false;
3397
    }
3398
    if (!function_exists('json_decode')) {
3399
        // The php-json extension is not available. Ignore profile.
3400
        return false;
3401
    }
3402
    $json = file_get_contents($jsonPath);
3403
    $params = json_decode($json);
3404
    if ($params === false or $params === null) {
3405
        return false;
3406
    }
3407
    $settings = $params->params;
3408
    if (!empty($params->parent)) {
3409
        installProfileSettings($params->parent);
3410
    }
3411
3412
    $tblSettings = Database::get_main_table(TABLE_MAIN_SETTINGS_CURRENT);
3413
3414
    foreach ($settings as $id => $param) {
3415
        $conditions = ['variable = ? ' => $param->variable];
3416
3417
        if (!empty($param->subkey)) {
3418
            $conditions['AND subkey = ? '] = $param->subkey;
3419
        }
3420
3421
        Database::update(
3422
            $tblSettings,
3423
            ['selected_value' => $param->selected_value],
3424
            $conditions
3425
        );
3426
    }
3427
3428
    return true;
3429
}
3430
3431
/**
3432
 * Quick function to remove a directory with its subdirectories.
3433
 *
3434
 * @param $dir
3435
 */
3436
function rrmdir($dir)
3437
{
3438
    if (is_dir($dir)) {
3439
        $objects = scandir($dir);
3440
        foreach ($objects as $object) {
3441
            if ($object != "." && $object != "..") {
3442
                if (filetype($dir."/".$object) == "dir") {
3443
                    @rrmdir($dir."/".$object);
0 ignored issues
show
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...
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

3443
                    /** @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...
3444
                } else {
3445
                    @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

3445
                    /** @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...
3446
                }
3447
            }
3448
        }
3449
        reset($objects);
0 ignored issues
show
Bug introduced by
It seems like $objects can also be of type false; however, parameter $array of reset() does only seem to accept array, 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

3449
        reset(/** @scrutinizer ignore-type */ $objects);
Loading history...
3450
        rmdir($dir);
3451
    }
3452
}
3453
3454
/**
3455
 * @param        $id
3456
 * @param string $type
3457
 * @param bool   $preview
3458
 * @param bool   $anonymous
3459
 *
3460
 * @throws \Doctrine\DBAL\DBALException
3461
 *
3462
 * @return array
3463
 */
3464
function get_group_picture_path_by_id($id, $type = 'web', $preview = false, $anonymous = false)
3465
{
3466
    switch ($type) {
3467
        case 'system': // Base: absolute system path.
3468
            $base = api_get_path(SYS_UPLOAD_PATH);
3469
            break;
3470
        case 'web': // Base: absolute web path.
3471
        default:
3472
            $base = api_get_path(WEB_UPLOAD_PATH);
3473
            break;
3474
    }
3475
3476
    $noPicturePath = ['dir' => $base.'img/', 'file' => 'unknown.jpg'];
3477
3478
    if (empty($id) || empty($type)) {
3479
        return $anonymous ? $noPicturePath : ['dir' => '', 'file' => ''];
3480
    }
3481
3482
    $id = intval($id);
3483
3484
    //$group_table = Database::get_main_table(TABLE_MAIN_GROUP);
3485
    $group_table = 'groups';
3486
    $sql = "SELECT picture_uri FROM $group_table WHERE id=".$id;
3487
    $res = Database::query($sql);
3488
3489
    if (!Database::num_rows($res)) {
3490
        return $anonymous ? $noPicturePath : ['dir' => '', 'file' => ''];
3491
    }
3492
3493
    $user = Database::fetch_array($res);
3494
    $picture_filename = trim($user['picture_uri']);
3495
3496
    if (api_get_setting('split_users_upload_directory') === 'true') {
3497
        if (!empty($picture_filename)) {
3498
            $dir = $base.'groups/'.substr($picture_filename, 0, 1).'/'.$id.'/';
3499
        } elseif ($preview) {
3500
            $dir = $base.'groups/'.substr((string) $id, 0, 1).'/'.$id.'/';
3501
        } else {
3502
            $dir = $base.'groups/'.$id.'/';
3503
        }
3504
    } else {
3505
        $dir = $base.'groups/'.$id.'/';
3506
    }
3507
3508
    if (empty($picture_filename) && $anonymous) {
3509
        return $noPicturePath;
3510
    }
3511
3512
    return ['dir' => $dir, 'file' => $picture_filename];
3513
}
3514
3515
/**
3516
 * Control the different steps of the migration through a big switch.
3517
 *
3518
 * @param string        $fromVersion
3519
 * @param EntityManager $manager
3520
 * @param bool          $processFiles
3521
 *
3522
 * @throws \Doctrine\DBAL\DBALException
3523
 *
3524
 * @return bool Always returns true except if the process is broken
3525
 */
3526
function migrateSwitch($fromVersion, $manager, $processFiles = true)
3527
{
3528
    error_log('Starting migration process from '.$fromVersion.' ('.date('Y-m-d H:i:s').')');
3529
3530
    echo '<a class="btn btn-secondary" href="javascript:void(0)" id="details_button">'.get_lang('Details').'</a><br />';
3531
    echo '<div id="details" style="display:none">';
3532
3533
    $connection = $manager->getConnection();
3534
3535
    $database = new Database();
3536
    $database->setManager($manager);
3537
3538
    switch ($fromVersion) {
3539
        case '1.9.0':
3540
        case '1.9.2':
3541
        case '1.9.4':
3542
        case '1.9.6':
3543
        case '1.9.6.1':
3544
        case '1.9.8':
3545
        case '1.9.8.1':
3546
        case '1.9.8.2':
3547
        case '1.9.10':
3548
        case '1.9.10.2':
3549
        case '1.9.10.4':
3550
        case '1.9.10.6':
3551
            $database = new Database();
3552
            $database->setManager($manager);
3553
3554
            // Fix type "enum" before running the migration with Doctrine
3555
            $connection->executeQuery("ALTER TABLE course_category MODIFY COLUMN auth_course_child VARCHAR(40) DEFAULT 'TRUE'");
3556
            $connection->executeQuery("ALTER TABLE course_category MODIFY COLUMN auth_cat_child VARCHAR(40) DEFAULT 'TRUE'");
3557
            $connection->executeQuery('ALTER TABLE c_quiz_answer MODIFY COLUMN hotspot_type varchar(40) default NULL');
3558
            $connection->executeQuery("ALTER TABLE c_tool MODIFY COLUMN target varchar(20) NOT NULL default '_self'");
3559
            $connection->executeQuery("ALTER TABLE c_link MODIFY COLUMN on_homepage char(10) NOT NULL default '0'");
3560
            $connection->executeQuery("ALTER TABLE c_blog_rating MODIFY COLUMN rating_type char(40) NOT NULL default 'post'");
3561
            $connection->executeQuery("ALTER TABLE c_survey MODIFY COLUMN anonymous char(10) NOT NULL default '0'");
3562
            $connection->executeQuery("ALTER TABLE c_document MODIFY COLUMN filetype char(10) NOT NULL default 'file'");
3563
            $connection->executeQuery("ALTER TABLE c_student_publication MODIFY COLUMN filetype char(10) NOT NULL default 'file'");
3564
3565
            // Migrate using the migration files located in:
3566
            // src/Chamilo/CoreBundle/Migrations/Schema/V110
3567
            $result = migrate(
3568
                110,
3569
                $manager
3570
            );
3571
3572
            if ($result) {
3573
                error_log('Migrations files were executed ('.date('Y-m-d H:i:s').')');
3574
                fixIds($manager);
3575
                error_log('fixIds finished ('.date('Y-m-d H:i:s').')');
3576
3577
                $connection->executeQuery("UPDATE settings_current SET selected_value = '1.10.0' WHERE variable = 'chamilo_database_version'");
3578
3579
                if ($processFiles) {
3580
                    $fromVersionShort = '1.9';
3581
                    include __DIR__.'/update-files-1.9.0-1.10.0.inc.php';
3582
                    // Only updates the configuration.inc.php with the new version
3583
                    include __DIR__.'/update-configuration.inc.php';
3584
                }
3585
3586
                error_log('Upgrade 1.10.x process concluded! ('.date('Y-m-d H:i:s').')');
3587
            } else {
3588
                error_log('There was an error during running migrations. Check error.log');
3589
                break;
3590
            }
3591
            // no break
3592
        case '1.10.0':
3593
        case '1.10.2':
3594
        case '1.10.4':
3595
        case '1.10.6':
3596
        case '1.10.8':
3597
            $database = new Database();
3598
            $database->setManager($manager);
3599
            // Migrate using the migration files located in:
3600
            // src/Chamilo/CoreBundle/Migrations/Schema/V111
3601
            $result = migrate(111, $manager);
3602
3603
            if ($result) {
3604
                error_log('Migrations files were executed ('.date('Y-m-d H:i:s').')');
3605
3606
                fixPostGroupIds($connection);
3607
3608
                $sql = "UPDATE settings_current SET selected_value = '1.11.0' WHERE variable = 'chamilo_database_version'";
3609
                $connection->executeQuery($sql);
3610
                if ($processFiles) {
3611
                    error_log('Update config files');
3612
                    $fromVersionShort = '1.10';
3613
                    include __DIR__.'/update-files-1.10.0-1.11.0.inc.php';
3614
                    // Only updates the configuration.inc.php with the new version
3615
                    include __DIR__.'/update-configuration.inc.php';
3616
                }
3617
                error_log('Upgrade 1.11.x process concluded!  ('.date('Y-m-d H:i:s').')');
3618
            } else {
3619
                error_log('There was an error during running migrations. Check error.log');
3620
            }
3621
            // no break
3622
        case '1.11.0':
3623
        case '1.11.1':
3624
        case '1.11.2':
3625
        case '1.11.4':
3626
        case '1.11.6':
3627
        case '1.11.8':
3628
        case '1.11.10':
3629
            $database = new Database();
3630
            $database->setManager($manager);
3631
            // Migrate using the migration files located in:
3632
            // src/Chamilo/CoreBundle/Migrations/Schema/V111
3633
            $result = migrate(200, $manager);
3634
3635
            if ($result) {
3636
                error_log('Migrations files were executed ('.date('Y-m-d H:i:s').')');
3637
                $sql = "UPDATE settings_current SET selected_value = '2.0.0' WHERE variable = 'chamilo_database_version'";
3638
                $connection->executeQuery($sql);
3639
                if ($processFiles) {
3640
                    error_log('Update config files');
3641
                    $fromVersionShort = '1.11';
3642
                    include __DIR__.'/update-files-1.11.0-2.0.0.inc.php';
3643
                    // Only updates the configuration.inc.php with the new version
3644
                    include __DIR__.'/update-configuration.inc.php';
3645
                }
3646
                error_log('Upgrade 2.0.0 process concluded!  ('.date('Y-m-d H:i:s').')');
3647
            } else {
3648
                error_log('There was an error during running migrations. Check error.log');
3649
            }
3650
            break;
3651
        default:
3652
            break;
3653
    }
3654
3655
    echo '</div>';
3656
3657
    return true;
3658
}
3659
3660
/**
3661
 * @param \Doctrine\DBAL\Connection $connection
3662
 *
3663
 * @throws \Doctrine\DBAL\DBALException
3664
 */
3665
function fixPostGroupIds($connection)
3666
{
3667
    $connection->executeQuery("ALTER TABLE course_category MODIFY COLUMN auth_course_child VARCHAR(40) DEFAULT 'TRUE'");
3668
    error_log('Fix c_student_publication.post_group_id');
3669
3670
    // Fix post_group_id
3671
    $sql = "SELECT * FROM c_student_publication 
3672
            WHERE (post_group_id <> 0 AND post_group_id is not null)";
3673
    $statement = $connection->executeQuery($sql);
3674
    $result = $statement->fetchAll();
3675
3676
    foreach ($result as $row) {
3677
        $groupId = $row['post_group_id'];
3678
        $courseId = $row['c_id'];
3679
        $workIid = $row['iid'];
3680
        $sql = "SELECT iid from c_group_info 
3681
                WHERE c_id = $courseId AND id = $groupId";
3682
        $statement = $connection->executeQuery($sql);
3683
        $count = $statement->rowCount();
3684
        if ($count > 0) {
3685
            $rowGroup = $statement->fetch();
3686
            $newGroupId = $rowGroup['iid'];
3687
            if ($newGroupId == $groupId) {
3688
                continue;
3689
            }
3690
            if ($newGroupId) {
3691
                $sql = "UPDATE c_student_publication 
3692
                        SET post_group_id = $newGroupId 
3693
                        WHERE 
3694
                            c_id = $courseId AND
3695
                            iid = $workIid
3696
                        ";
3697
                $connection->executeQuery($sql);
3698
            }
3699
        }
3700
    }
3701
3702
    error_log('End - Fix c_student_publication.post_group_id');
3703
3704
    // Delete c_student_publication from any session that doesn't exist anymore
3705
    $sql = "DELETE FROM c_student_publication 
3706
            WHERE session_id NOT IN (SELECT id FROM session) AND (session_id <> 0 AND session_id is not null)";
3707
    $connection->executeQuery($sql);
3708
3709
    error_log('Fix work documents');
3710
    // Fix work documents that don't have c_item_property value
3711
    $sql = "SELECT * FROM c_student_publication WHERE parent_id IS NOT NULL";
3712
    $statement = $connection->executeQuery($sql);
3713
    $result = $statement->fetchAll();
3714
    foreach ($result as $row) {
3715
        $groupId = $row['post_group_id'];
3716
        $courseId = $row['c_id'];
3717
        $sessionId = $row['session_id'];
3718
        $workId = $row['id'];
3719
        $sessionCondition = " session_id = $sessionId";
3720
        if (empty($sessionId)) {
3721
            $sessionCondition = ' (session_id = 0 OR session_id IS NULL) ';
3722
        }
3723
        $sql = "SELECT * FROM c_item_property
3724
                WHERE
3725
                    c_id = $courseId AND
3726
                    tool = 'work' AND
3727
                    ref = $workId AND
3728
                    $sessionCondition ";
3729
        $itemInfo = $connection->fetchAssoc($sql);
3730
        if (empty($itemInfo)) {
3731
            $params = [
3732
                'c_id' => $courseId,
3733
                'to_group_id' => $groupId,
3734
                //'to_user_id' => null,
3735
                'insert_user_id' => 1,
3736
                'session_id' => $sessionId,
3737
                'tool' => 'work',
3738
                'insert_date' => api_get_utc_datetime(),
3739
                'lastedit_date' => api_get_utc_datetime(),
3740
                'ref' => $workId,
3741
                'lastedit_type' => 'visible',
3742
                'lastedit_user_id' => 1,
3743
                'visibility' => 1,
3744
            ];
3745
            $connection->insert('c_item_property', $params);
3746
            $id = $connection->lastInsertId();
3747
            $sql = "UPDATE c_item_property SET id = iid WHERE iid = $id";
3748
            $connection->executeQuery($sql);
3749
        }
3750
    }
3751
    error_log('End - Fix work documents');
3752
}
3753
3754
/**
3755
 * @return string
3756
 */
3757
function generateRandomToken()
3758
{
3759
    return hash('sha1', uniqid(mt_rand(), true));
3760
}
3761