Completed
Push — master ( bd162c...f45219 )
by Yannick
11:31
created

checkCanCreateFile()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

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

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

358
    /** @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...
359
    @chmod('..', 0755); //set permissions on parent dir of install dir
360
}
361
362
/**
363
 * Write the main system config file.
364
 *
365
 * @param string $path Path to the config file
366
 */
367
function write_system_config_file($path)
368
{
369
    global $dbHostForm;
370
    global $dbPortForm;
371
    global $dbUsernameForm;
372
    global $dbPassForm;
373
    global $dbNameForm;
374
    global $urlForm;
375
    global $pathForm;
376
    global $urlAppendPath;
377
    global $languageForm;
378
    global $encryptPassForm;
379
    global $session_lifetime;
380
    global $new_version;
381
    global $new_version_stable;
382
383
    $content = file_get_contents(__DIR__.'/'.SYSTEM_CONFIG_FILENAME);
384
385
    $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...
386
    $config['{DATABASE_HOST}'] = $dbHostForm;
387
    $config['{DATABASE_PORT}'] = $dbPortForm;
388
    $config['{DATABASE_USER}'] = $dbUsernameForm;
389
    $config['{DATABASE_PASSWORD}'] = $dbPassForm;
390
    $config['{DATABASE_MAIN}'] = $dbNameForm;
391
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 Laminas\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 (false !== ($res = Database::query("SELECT * FROM settings_current WHERE variable = '$param'"))) {
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',
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' => 'selectpicker'],
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 (true === $r) {
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 (true == $file_course_test_was_created) {
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 (true === $r) {
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 ('update' == $installType) {
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
            <tr>
838
                <td class="requirements-item"><a href="http://php.net/manual/en/book.mbstring.php" target="_blank">Multibyte string</a> '.get_lang('Support').'</td>
839
                <td class="requirements-value">'.checkExtension('mbstring', get_lang('Yes'), get_lang('MBString extension not available'), true).'</td>
840
            </tr>
841
           <tr>
842
                <td class="requirements-item"><a href="http://php.net/manual/en/book.exif.php" target="_blank">Exif</a> '.get_lang('Support').'</td>
843
                <td class="requirements-value">'.checkExtension('exif', get_lang('Yes'), get_lang('Exif extension not available'), true).'</td>
844
            </tr>
845
            <tr>
846
                <td class="requirements-item"><a href="http://php.net/opcache" target="_blank">Zend OpCache</a> '.get_lang('Support').' ('.get_lang('Optional').')</td>
847
                <td class="requirements-value">'.checkExtension('Zend OPcache', get_lang('Yes'), get_lang('No'), true, 'opcache.enable').'</td>
848
            </tr>
849
            <tr>
850
                <td class="requirements-item"><a href="http://php.net/apcu" target="_blank">APCu</a> '.get_lang('Support').' ('.get_lang('Optional').')</td>
851
                <td class="requirements-value">'.checkExtension('apcu', get_lang('Yes'), get_lang('No'), true, 'apc.enabled').'</td>
852
            </tr>
853
            <tr>
854
                <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>
855
                <td class="requirements-value">'.checkExtension('iconv', get_lang('Yes'), get_lang('No'), true).'</td>
856
            </tr>
857
            <tr>
858
                <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>
859
                <td class="requirements-value">'.checkExtension('ldap', get_lang('Yes'), get_lang('LDAP Extension not available'), true).'</td>
860
            </tr>
861
            <tr>
862
                <td class="requirements-item"><a href="http://xapian.org/" target="_blank">Xapian</a> '.get_lang('Support').' ('.get_lang('Optional').')</td>
863
                <td class="requirements-value">'.checkExtension('xapian', get_lang('Yes'), get_lang('No'), true).'</td>
864
            </tr>
865
        </table>';
866
    echo '</div>';
867
868
    // RECOMMENDED SETTINGS
869
    // Note: these are the settings for Joomla, does this also apply for Chamilo?
870
    // Note: also add upload_max_filesize here so that large uploads are possible
871
    echo '<h4 class="install-subtitle">'.get_lang('(recommended) settings').'</h4>';
872
    echo '<div class="install-requirement">'.get_lang('(recommended) settingsInfo').'</div>';
873
    echo '<div class="table-responsive">';
874
    echo '<table class="table table-bordered">
875
            <tr>
876
                <th>'.get_lang('Setting').'</th>
877
                <th>'.get_lang('(recommended)').'</th>
878
                <th>'.get_lang('Currently').'</th>
879
            </tr>
880
881
            <tr>
882
                <td class="requirements-item"><a href="http://php.net/manual/ref.errorfunc.php#ini.display-errors">Display Errors</a></td>
883
                <td class="requirements-recommended">'.Display::label('OFF', 'success').'</td>
884
                <td class="requirements-value">'.checkPhpSetting('display_errors', 'OFF').'</td>
885
            </tr>
886
            <tr>
887
                <td class="requirements-item"><a href="http://php.net/manual/ini.core.php#ini.file-uploads">File Uploads</a></td>
888
                <td class="requirements-recommended">'.Display::label('ON', 'success').'</td>
889
                <td class="requirements-value">'.checkPhpSetting('file_uploads', 'ON').'</td>
890
            </tr>
891
            <tr>
892
                <td class="requirements-item"><a href="http://php.net/manual/ref.session.php#ini.session.auto-start">Session auto start</a></td>
893
                <td class="requirements-recommended">'.Display::label('OFF', 'success').'</td>
894
                <td class="requirements-value">'.checkPhpSetting('session.auto_start', 'OFF').'</td>
895
            </tr>
896
            <tr>
897
                <td class="requirements-item"><a href="http://php.net/manual/ini.core.php#ini.short-open-tag">Short Open Tag</a></td>
898
                <td class="requirements-recommended">'.Display::label('OFF', 'success').'</td>
899
                <td class="requirements-value">'.checkPhpSetting('short_open_tag', 'OFF').'</td>
900
            </tr>
901
            <tr>
902
                <td class="requirements-item"><a href="http://www.php.net/manual/en/session.configuration.php#ini.session.cookie-httponly">Cookie HTTP Only</a></td>
903
                <td class="requirements-recommended">'.Display::label('ON', 'success').'</td>
904
                <td class="requirements-value">'.checkPhpSetting('session.cookie_httponly', 'ON').'</td>
905
            </tr>
906
            <tr>
907
                <td class="requirements-item"><a href="http://php.net/manual/ini.core.php#ini.upload-max-filesize">Maximum upload file size</a></td>
908
                <td class="requirements-recommended">'.Display::label('>= '.REQUIRED_MIN_UPLOAD_MAX_FILESIZE.'M', 'success').'</td>
909
                <td class="requirements-value">'.compare_setting_values(ini_get('upload_max_filesize'), REQUIRED_MIN_UPLOAD_MAX_FILESIZE).'</td>
910
            </tr>
911
            <tr>
912
                <td class="requirements-item"><a href="http://php.net/manual/ini.core.php#ini.post-max-size">Maximum post size</a></td>
913
                <td class="requirements-recommended">'.Display::label('>= '.REQUIRED_MIN_POST_MAX_SIZE.'M', 'success').'</td>
914
                <td class="requirements-value">'.compare_setting_values(ini_get('post_max_size'), REQUIRED_MIN_POST_MAX_SIZE).'</td>
915
            </tr>
916
            <tr>
917
                <td class="requirements-item"><a href="http://www.php.net/manual/en/ini.core.php#ini.memory-limit">Memory Limit</a></td>
918
                <td class="requirements-recommended">'.Display::label('>= '.REQUIRED_MIN_MEMORY_LIMIT.'M', 'success').'</td>
919
                <td class="requirements-value">'.compare_setting_values($originalMemoryLimit, REQUIRED_MIN_MEMORY_LIMIT).'</td>
920
            </tr>
921
          </table>';
922
    echo '</div>';
923
924
    // DIRECTORY AND FILE PERMISSIONS
925
    echo '<h4 class="install-subtitle">'.get_lang('Directory and files permissions').'</h4>';
926
    echo '<div class="install-requirement">'.get_lang('Directory and files permissionsInfo').'</div>';
927
    echo '<div class="table-responsive">';
928
929
    $_SESSION['permissions_for_new_directories'] = $_setting['permissions_for_new_directories'] = $dir_perm_verified;
930
    $_SESSION['permissions_for_new_files'] = $_setting['permissions_for_new_files'] = $fil_perm_verified;
931
932
    $dir_perm = Display::label('0'.decoct($dir_perm_verified), 'info');
933
    $file_perm = Display::label('0'.decoct($fil_perm_verified), 'info');
934
935
    $oldConf = '';
936
    if (file_exists(api_get_path(SYS_CODE_PATH).'inc/conf/configuration.php')) {
937
        $oldConf = '<tr>
938
            <td class="requirements-item">'.api_get_path(SYS_CODE_PATH).'inc/conf</td>
939
            <td class="requirements-value">'.check_writable(api_get_path(SYS_CODE_PATH).'inc/conf').'</td>
940
        </tr>';
941
    }
942
    $basePath = api_get_path(SYMFONY_SYS_PATH);
943
    echo '<table class="table table-bordered">
944
            '.$oldConf.'
945
            <tr>
946
                <td class="requirements-item">'.$basePath.'var/</td>
947
                <td class="requirements-value">'.check_writable($basePath.'var').'</td>
948
            </tr>
949
            <tr>
950
                <td class="requirements-item">'.$basePath.'.env.local</td>
951
                <td class="requirements-value">'.checkCanCreateFile($basePath.'.env.local').'</td>
952
            </tr>
953
            <tr>
954
                <td class="requirements-item">'.$basePath.'config/</td>
955
                <td class="requirements-value">'.check_writable($basePath.'config').'</td>
956
            </tr>
957
            <tr>
958
                <td class="requirements-item">'.get_lang('Permissions for new directories').'</td>
959
                <td class="requirements-value">'.$dir_perm.' </td>
960
            </tr>
961
            <tr>
962
                <td class="requirements-item">'.get_lang('Permissions for new files').'</td>
963
                <td class="requirements-value">'.$file_perm.' </td>
964
            </tr>
965
        </table>';
966
967
    echo '</div>';
968
969
    if ('update' === $installType && (empty($updatePath) || $badUpdatePath)) {
970
        if ($badUpdatePath) {
971
            ?>
972
            <div class="alert alert-warning">
973
                <?php echo get_lang('Error'); ?>!<br />
974
                Chamilo <?php echo implode('|', $update_from_version_8).' '.get_lang('has not been found in that directory'); ?>.
975
            </div>
976
        <?php
977
        } else {
978
            echo '<br />';
979
        } ?>
980
            <div class="row">
981
                <div class="col-md-12">
982
                    <p><?php echo get_lang('Old version\'s root path'); ?>:
983
                        <input type="text" name="updatePath" size="50" value="<?php echo ($badUpdatePath && !empty($updatePath)) ? htmlentities($updatePath) : ''; ?>" />
984
                    </p>
985
                    <p>
986
                        <div class="btn-group">
987
                            <button type="submit" class="btn btn-secondary" name="step1" value="<?php echo get_lang('Back'); ?>" >
988
                                <em class="fa fa-backward"> <?php echo get_lang('Back'); ?></em>
989
                            </button>
990
                            <input type="hidden" name="is_executable" id="is_executable" value="-" />
991
                            <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;" >
992
                                <em class="fa fa-forward"> </em> <?php echo get_lang('Next'); ?>
993
                            </button>
994
                        </div>
995
                    </p>
996
                </div>
997
            </div>
998
        <?php
999
    } else {
1000
        $error = false;
1001
        // First, attempt to set writing permissions if we don't have them yet
1002
        //$perm = api_get_permissions_for_new_directories();
1003
        $perm = octdec('0777');
1004
        //$perm_file = api_get_permissions_for_new_files();
1005
        $perm_file = octdec('0666');
1006
        $notWritable = [];
1007
1008
        $checked_writable = api_get_path(SYS_PUBLIC_PATH);
1009
        if (!is_writable($checked_writable)) {
1010
            $notWritable[] = $checked_writable;
1011
            @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

1011
            /** @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...
1012
        }
1013
1014
        if (false == $course_test_was_created) {
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...
1015
            error_log('Installer: Could not create test course - Make sure permissions are fine.');
1016
            $error = true;
1017
        }
1018
1019
        $checked_writable = api_get_path(CONFIGURATION_PATH).'configuration.php';
1020
        if (file_exists($checked_writable) && !is_writable($checked_writable)) {
1021
            $notWritable[] = $checked_writable;
1022
            @chmod($checked_writable, $perm_file);
1023
        }
1024
1025
        // Second, if this fails, report an error
1026
        //--> The user would have to adjust the permissions manually
1027
        if (count($notWritable) > 0) {
1028
            error_log('Installer: At least one needed directory or file is not writeable');
1029
            $error = true; ?>
1030
            <div class="text-danger">
1031
                <h3 class="text-center"><?php echo get_lang('Warning !'); ?></h3>
1032
                <p>
1033
                    <?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>'); ?>
1034
                </p>
1035
            </div>
1036
            <?php
1037
            echo '<ul>';
1038
            foreach ($notWritable as $value) {
1039
                echo '<li class="text-danger">'.$value.'</li>';
1040
            }
1041
            echo '</ul>';
1042
        } elseif (file_exists(api_get_path(CONFIGURATION_PATH).'configuration.php')) {
1043
            // Check wether a Chamilo configuration file already exists.
1044
            echo '<div class="alert alert-warning"><h4><center>';
1045
            echo get_lang('Warning !ExistingLMSInstallationDetected');
1046
            echo '</center></h4></div>';
1047
        }
1048
1049
        $deprecated = [
1050
            api_get_path(SYS_CODE_PATH).'exercice/',
1051
            api_get_path(SYS_CODE_PATH).'newscorm/',
1052
            api_get_path(SYS_PLUGIN_PATH).'ticket/',
1053
            api_get_path(SYS_PLUGIN_PATH).'skype/',
1054
        ];
1055
        $deprecatedToRemove = [];
1056
        foreach ($deprecated as $deprecatedDirectory) {
1057
            if (!is_dir($deprecatedDirectory)) {
1058
                continue;
1059
            }
1060
            $deprecatedToRemove[] = $deprecatedDirectory;
1061
        }
1062
1063
        if (count($deprecatedToRemove) > 0) {
1064
            ?>
1065
            <p class="text-danger"><?php echo get_lang('Warning !ForDeprecatedDirectoriesForUpgrade'); ?></p>
1066
            <ul>
1067
                <?php foreach ($deprecatedToRemove as $deprecatedDirectory) {
1068
                ?>
1069
                    <li class="text-danger"><?php echo $deprecatedDirectory; ?></li>
1070
                <?php
1071
            } ?>
1072
            </ul>
1073
            <?php
1074
        }
1075
1076
        // And now display the choice buttons (go back or install)?>
1077
        <p align="center" style="padding-top:15px">
1078
            <button type="submit" name="step1" class="btn btn-default" onclick="javascript: window.location='index.php'; return false;" value="<?php echo get_lang('Previous'); ?>" >
1079
                <em class="fa fa-backward"> </em> <?php echo get_lang('Previous'); ?>
1080
            </button>
1081
            <button type="submit" name="step2_install" class="btn btn-success" value="<?php echo get_lang("New installation"); ?>" <?php if ($error) {
1082
            echo 'disabled="disabled"';
1083
        } ?> >
1084
                <em class="fa fa-forward"> </em> <?php echo get_lang('New installation'); ?>
1085
            </button>
1086
        <input type="hidden" name="is_executable" id="is_executable" value="-" />
1087
            <button type="submit" class="btn btn-default" <?php echo !$error ?: 'disabled="disabled"'; ?> name="step2_update_8" value="Upgrade from Chamilo 1.9.x">
1088
                <em class="fa fa-forward" aria-hidden="true"></em> <?php echo get_lang('Upgrade Chamilo LMS version'); ?>
1089
            </button>
1090
            </p>
1091
        <?php
1092
    }
1093
}
1094
1095
/**
1096
 * Displays the license (GNU GPL) as step 2, with
1097
 * - an "I accept" button named step3 to proceed to step 3;
1098
 * - a "Back" button named step1 to go back to the first step.
1099
 */
1100
function display_license_agreement()
1101
{
1102
    echo '<div class="RequirementHeading"><h2>'.display_step_sequence().get_lang('Licence').'</h2>';
1103
    echo '<p>'.get_lang('Chamilo is free software distributed under the GNU General Public licence (GPL).').'</p>';
1104
    echo '<p><a href="../../documentation/license.html" target="_blank">'.get_lang('Printable version').'</a></p>';
1105
    echo '</div>'; ?>
1106
    <div class="form-group">
1107
        <pre style="overflow: auto; height: 200px; margin-top: 5px;">
1108
            <?php echo api_htmlentities(@file_get_contents(api_get_path(SYMFONY_SYS_PATH).'documentation/license.txt')); ?>
1109
        </pre>
1110
    </div>
1111
    <div class="form-group form-check">
1112
        <input type="checkbox" name="accept" id="accept_licence" value="1">
1113
        <label for="accept_licence"><?php echo get_lang('I Accept'); ?></label>
1114
    </div>
1115
    <div class="row">
1116
        <div class="col-md-12">
1117
            <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>
1118
        </div>
1119
    </div>
1120
1121
    <!-- Contact information form -->
1122
    <div class="section-parameters">
1123
        <a href="javascript://" class = "advanced_parameters" >
1124
        <span id="img_plus_and_minus">&nbsp;<i class="fa fa-eye" aria-hidden="true"></i>&nbsp;<?php echo get_lang('Contact information'); ?></span>
1125
        </a>
1126
    </div>
1127
1128
    <div id="id_contact_form" style="display:block">
1129
        <div class="normal-message"><?php echo get_lang('Contact informationDescription'); ?></div>
1130
        <div id="contact_registration">
1131
            <p><?php echo get_contact_registration_form(); ?></p><br />
1132
        </div>
1133
    </div>
1134
    <div class="text-center">
1135
    <button type="submit" class="btn btn-default" name="step1" value="&lt; <?php echo get_lang('Previous'); ?>" >
1136
        <em class="fa fa-backward"> </em> <?php echo get_lang('Previous'); ?>
1137
    </button>
1138
    <input type="hidden" name="is_executable" id="is_executable" value="-" />
1139
    <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;" >
1140
        <em class="fa fa-forward"> </em> <?php echo get_lang('Next'); ?>
1141
    </button>
1142
    </div>
1143
    <?php
1144
}
1145
1146
/**
1147
 * Get contact registration form.
1148
 */
1149
function get_contact_registration_form()
1150
{
1151
    $html = '
1152
   <div class="form-horizontal">
1153
    <div class="panel panel-default">
1154
    <div class="panel-body">
1155
    <div id="div_sent_information"></div>
1156
    <div class="form-group row">
1157
            <label class="col-sm-3"><span class="form_required">*</span>'.get_lang('Name').'</label>
1158
            <div class="col-sm-9"><input id="person_name" class="form-control" type="text" name="person_name" size="30" /></div>
1159
    </div>
1160
    <div class="form-group row">
1161
            <label class="col-sm-3"><span class="form_required">*</span>'.get_lang('e-mail').'</label>
1162
            <div class="col-sm-9"><input id="person_email" class="form-control" type="text" name="person_email" size="30" /></div>
1163
    </div>
1164
    <div class="form-group row">
1165
            <label class="col-sm-3"><span class="form_required">*</span>'.get_lang('Your company\'s name').'</label>
1166
            <div class="col-sm-9"><input id="company_name" class="form-control" type="text" name="company_name" size="30" /></div>
1167
    </div>
1168
    <div class="form-group row">
1169
        <label class="col-sm-3"><span class="form_required">*</span>'.get_lang('Your company\'s activity').'</label>
1170
        <div class="col-sm-9">
1171
            <select class="selectpicker show-tick" name="company_activity" id="company_activity" >
1172
                <option value="">--- '.get_lang('Select one').' ---</option>
1173
                <Option value="Advertising/Marketing/PR">Advertising/Marketing/PR</Option><Option value="Agriculture/Forestry">Agriculture/Forestry</Option>
1174
                <Option value="Architecture">Architecture</Option><Option value="Banking/Finance">Banking/Finance</Option>
1175
                <Option value="Biotech/Pharmaceuticals">Biotech/Pharmaceuticals</Option><Option value="Business Equipment">Business Equipment</Option>
1176
                <Option value="Business Services">Business Services</Option><Option value="Construction">Construction</Option>
1177
                <Option value="Consulting/Research">Consulting/Research</Option><Option value="Education">Education</Option>
1178
                <Option value="Engineering">Engineering</Option><Option value="Environmental">Environmental</Option>
1179
                <Option value="Government">Government</Option><Option value="Healthcare">Health Care</Option>
1180
                <Option value="Hospitality/Lodging/Travel">Hospitality/Lodging/Travel</Option><Option value="Insurance">Insurance</Option>
1181
                <Option value="Legal">Legal</Option><Option value="Manufacturing">Manufacturing</Option>
1182
                <Option value="Media/Entertainment">Media/Entertainment</Option><Option value="Mortgage">Mortgage</Option>
1183
                <Option value="Non-Profit">Non-Profit</Option><Option value="Real Estate">Real Estate</Option>
1184
                <Option value="Restaurant">Restaurant</Option><Option value="Retail">Retail</Option>
1185
                <Option value="Shipping/Transportation">Shipping/Transportation</Option>
1186
                <Option value="Technology">Technology</Option><Option value="Telecommunications">Telecommunications</Option>
1187
                <Option value="Other">Other</Option>
1188
            </select>
1189
        </div>
1190
    </div>
1191
1192
    <div class="form-group row">
1193
        <label class="col-sm-3"><span class="form_required">*</span>'.get_lang('Your job\'s description').'</label>
1194
        <div class="col-sm-9">
1195
            <select class="selectpicker show-tick" name="person_role" id="person_role" >
1196
                <option value="">--- '.get_lang('Select one').' ---</option>
1197
                <Option value="Administration">Administration</Option><Option value="CEO/President/ Owner">CEO/President/ Owner</Option>
1198
                <Option value="CFO">CFO</Option><Option value="CIO/CTO">CIO/CTO</Option>
1199
                <Option value="Consultant">Consultant</Option><Option value="Customer Service">Customer Service</Option>
1200
                <Option value="Engineer/Programmer">Engineer/Programmer</Option><Option value="Facilities/Operations">Facilities/Operations</Option>
1201
                <Option value="Finance/ Accounting Manager">Finance/ Accounting Manager</Option><Option value="Finance/ Accounting Staff">Finance/ Accounting Staff</Option>
1202
                <Option value="General Manager">General Manager</Option><Option value="Human Resources">Human Resources</Option>
1203
                <Option value="IS/IT Management">IS/IT Management</Option><Option value="IS/ IT Staff">IS/ IT Staff</Option>
1204
                <Option value="Marketing Manager">Marketing Manager</Option><Option value="Marketing Staff">Marketing Staff</Option>
1205
                <Option value="Partner/Principal">Partner/Principal</Option><Option value="Purchasing Manager">Purchasing Manager</Option>
1206
                <Option value="Sales/ Business Dev. Manager">Sales/ Business Dev. Manager</Option><Option value="Sales/ Business Dev.">Sales/ Business Dev.</Option>
1207
                <Option value="Vice President/Senior Manager">Vice President/Senior Manager</Option><Option value="Other">Other</Option>
1208
            </select>
1209
        </div>
1210
    </div>
1211
1212
    <div class="form-group row">
1213
        <label class="col-sm-3"><span class="form_required">*</span>'.get_lang('Your company\'s home country').'</label>
1214
        <div class="col-sm-9">'.get_countries_list_from_array(true).'</div>
1215
    </div>
1216
    <div class="form-group row">
1217
        <label class="col-sm-3">'.get_lang('Company city').'</label>
1218
        <div class="col-sm-9">
1219
                <input type="text" class="form-control" id="company_city" name="company_city" size="30" />
1220
        </div>
1221
    </div>
1222
    <div class="form-group row">
1223
        <label class="col-sm-3">'.get_lang('Preferred contact language').'</label>
1224
        <div class="col-sm-9">
1225
            <select class="selectpicker show-tick" id="language" name="language">
1226
                <option value="bulgarian">Bulgarian</option>
1227
                <option value="indonesian">Bahasa Indonesia</option>
1228
                <option value="bosnian">Bosanski</option>
1229
                <option value="german">Deutsch</option>
1230
                <option selected="selected" value="english">English</option>
1231
                <option value="spanish">Spanish</option>
1232
                <option value="french">Français</option>
1233
                <option value="italian">Italian</option>
1234
                <option value="hungarian">Magyar</option>
1235
                <option value="dutch">Nederlands</option>
1236
                <option value="brazilian">Português do Brasil</option>
1237
                <option value="portuguese">Português europeu</option>
1238
                <option value="slovenian">Slovenčina</option>
1239
            </select>
1240
        </div>
1241
    </div>
1242
1243
    <div class="form-group row">
1244
        <label class="col-sm-3">'.get_lang('Do you have the power to take financial decisions on behalf of your company?').'</label>
1245
        <div class="col-sm-9">
1246
            <div class="radio">
1247
                <label>
1248
                    <input type="radio" name="financial_decision" id="financial_decision1" value="1" checked /> '.get_lang('Yes').'
1249
                </label>
1250
            </div>
1251
            <div class="radio">
1252
                <label>
1253
                    <input type="radio" name="financial_decision" id="financial_decision2" value="0" /> '.get_lang('No').'
1254
                </label>
1255
            </div>
1256
        </div>
1257
    </div>
1258
    <div class="clear"></div>
1259
    <div class="form-group row">
1260
            <div class="col-sm-3">&nbsp;</div>
1261
            <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>
1262
    </div>
1263
    <div class="form-group row">
1264
            <div class="col-sm-3">&nbsp;</div>
1265
            <div class="col-sm-9"><span class="form_required">*</span><small>'.get_lang('Mandatory field').'</small></div>
1266
    </div></div></div>
1267
    </div>';
1268
1269
    return $html;
1270
}
1271
1272
/**
1273
 * Displays a parameter in a table row.
1274
 * Used by the display_database_settings_form function.
1275
 *
1276
 * @param   string  Type of install
1277
 * @param   string  Name of parameter
1278
 * @param   string  Field name (in the HTML form)
1279
 * @param   string  Field value
1280
 * @param   string  Extra notice (to show on the right side)
1281
 * @param   bool Whether to display in update mode
1282
 * @param   string  Additional attribute for the <tr> element
1283
 */
1284
function displayDatabaseParameter(
1285
    $installType,
1286
    $parameterName,
1287
    $formFieldName,
1288
    $parameterValue,
1289
    $extra_notice,
1290
    $displayWhenUpdate = true,
1291
    $tr_attribute = ''
1292
) {
1293
    //echo "<tr ".$tr_attribute.">";
1294
    echo "<label class='col-sm-4'>$parameterName</label>";
1295
1296
    if (INSTALL_TYPE_UPDATE == $installType && $displayWhenUpdate) {
1297
        echo '<input type="hidden" name="'.$formFieldName.'" id="'.$formFieldName.'" value="'.api_htmlentities($parameterValue).'" />'.$parameterValue;
1298
    } else {
1299
        $inputType = 'dbPassForm' == $formFieldName ? 'password' : 'text';
1300
1301
        //Slightly limit the length of the database prefix to avoid having to cut down the databases names later on
1302
        $maxLength = 'dbPrefixForm' == $formFieldName ? '15' : MAX_FORM_FIELD_LENGTH;
1303
        if (INSTALL_TYPE_UPDATE == $installType) {
1304
            echo '<input type="hidden" name="'.$formFieldName.'" id="'.$formFieldName.'" value="'.api_htmlentities($parameterValue).'" />';
1305
            echo api_htmlentities($parameterValue);
1306
        } else {
1307
            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>";
1308
            echo '<div class="col-sm-3">'.$extra_notice.'</div>';
1309
        }
1310
    }
1311
}
1312
1313
/**
1314
 * Displays step 3 - a form where the user can enter the installation settings
1315
 * regarding the databases - login and password, names, prefixes, single
1316
 * or multiple databases, tracking or not...
1317
 *
1318
 * @param string $installType
1319
 * @param string $dbHostForm
1320
 * @param string $dbUsernameForm
1321
 * @param string $dbPassForm
1322
 * @param string $dbNameForm
1323
 * @param int    $dbPortForm
1324
 * @param string $installationProfile
1325
 */
1326
function display_database_settings_form(
1327
    $installType,
1328
    $dbHostForm,
1329
    $dbUsernameForm,
1330
    $dbPassForm,
1331
    $dbNameForm,
1332
    $dbPortForm = 3306,
1333
    $installationProfile = ''
1334
) {
1335
    if ('update' == $installType) {
1336
        global $_configuration;
1337
        $dbHostForm = $_configuration['db_host'];
1338
        $dbUsernameForm = $_configuration['db_user'];
1339
        $dbPassForm = $_configuration['db_password'];
1340
        $dbNameForm = $_configuration['main_database'];
1341
        $dbPortForm = isset($_configuration['db_port']) ? $_configuration['db_port'] : '';
1342
1343
        echo '<div class="RequirementHeading"><h2>'.display_step_sequence().get_lang('Database settings').'</h2></div>';
1344
        echo '<div class="RequirementContent">';
1345
        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!');
1346
        echo '</div>';
1347
    } else {
1348
        echo '<div class="RequirementHeading"><h2>'.display_step_sequence().get_lang('Database settings').'</h2></div>';
1349
        echo '<div class="RequirementContent">';
1350
        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.');
1351
        echo '</div>';
1352
    } ?>
1353
    <div class="panel panel-default">
1354
        <div class="panel-body">
1355
        <div class="form-group row">
1356
            <label class="col-sm-4"><?php echo get_lang('Database Host'); ?> </label>
1357
            <?php if ('update' == $installType) {
1358
        ?>
1359
            <div class="col-sm-5">
1360
                <input type="hidden" name="dbHostForm" value="<?php echo htmlentities($dbHostForm); ?>" /><?php echo $dbHostForm; ?>
1361
            </div>
1362
            <div class="col-sm-3"></div>
1363
            <?php
1364
    } else {
1365
        ?>
1366
            <div class="col-sm-5">
1367
                <input type="text" class="form-control" size="25" maxlength="50" name="dbHostForm" value="<?php echo htmlentities($dbHostForm); ?>" />
1368
            </div>
1369
            <div class="col-sm-3"><?php echo get_lang('ex.').' localhost'; ?></div>
1370
            <?php
1371
    } ?>
1372
        </div>
1373
        <div class="form-group row">
1374
            <label class="col-sm-4"><?php echo get_lang('Port'); ?> </label>
1375
            <?php if ('update' == $installType) {
1376
        ?>
1377
            <div class="col-sm-5">
1378
                <input type="hidden" name="dbPortForm" value="<?php echo htmlentities($dbPortForm); ?>" /><?php echo $dbPortForm; ?>
1379
            </div>
1380
            <div class="col-sm-3"></div>
1381
            <?php
1382
    } else {
1383
        ?>
1384
            <div class="col-sm-5">
1385
                <input type="text" class="form-control" size="25" maxlength="50" name="dbPortForm" value="<?php echo htmlentities($dbPortForm); ?>" />
1386
            </div>
1387
            <div class="col-sm-3"><?php echo get_lang('ex.').' 3306'; ?></div>
1388
            <?php
1389
    } ?>
1390
        </div>
1391
        <div class="form-group row">
1392
            <?php
1393
                //database user username
1394
                $example_login = get_lang('ex.').' root';
1395
    displayDatabaseParameter($installType, get_lang('Database Login'), 'dbUsernameForm', $dbUsernameForm, $example_login); ?>
1396
        </div>
1397
        <div class="form-group row">
1398
            <?php
1399
            //database user password
1400
            $example_password = get_lang('ex.').' '.api_generate_password();
1401
    displayDatabaseParameter($installType, get_lang('Database Password'), 'dbPassForm', $dbPassForm, $example_password); ?>
1402
        </div>
1403
        <div class="form-group row">
1404
            <?php
1405
            //Database Name fix replace weird chars
1406
            if (INSTALL_TYPE_UPDATE != $installType) {
1407
                $dbNameForm = str_replace(['-', '*', '$', ' ', '.'], '', $dbNameForm);
1408
            }
1409
1410
    displayDatabaseParameter(
1411
                $installType,
1412
                get_lang('Main Chamilo database (DB)'),
1413
                'dbNameForm',
1414
                $dbNameForm,
1415
                '&nbsp;',
1416
                null,
1417
                'id="optional_param1"'
1418
                ); ?>
1419
        </div>
1420
       <?php if (INSTALL_TYPE_UPDATE != $installType) {
1421
                    ?>
1422
        <div class="form-group row">
1423
            <div class="col-sm-4"></div>
1424
            <div class="col-sm-8">
1425
            <button type="submit" class="btn btn-primary" name="step3" value="step3">
1426
                <em class="fa fa-refresh"> </em>
1427
                <?php echo get_lang('Check database connection'); ?>
1428
            </button>
1429
            </div>
1430
        </div>
1431
        <?php
1432
                } ?>
1433
1434
        </div>
1435
    </div>
1436
    <?php
1437
        $database_exists_text = '';
1438
    $manager = null;
1439
    try {
1440
        if ('update' === $installType) {
1441
            /** @var \Database $manager */
1442
            $manager = connectToDatabase(
1443
                $dbHostForm,
1444
                $dbUsernameForm,
1445
                $dbPassForm,
1446
                $dbNameForm,
1447
                $dbPortForm
1448
            );
1449
1450
            $connection = $manager->getConnection();
1451
            $connection->connect();
1452
            $schemaManager = $connection->getSchemaManager();
1453
1454
            // Test create/alter/drop table
1455
            $table = 'zXxTESTxX_'.mt_rand(0, 1000);
1456
            $sql = "CREATE TABLE $table (id INT AUTO_INCREMENT NOT NULL, name varchar(255), PRIMARY KEY(id))";
1457
            $connection->query($sql);
1458
            $tableCreationWorks = false;
1459
            $tableDropWorks = false;
1460
            if ($schemaManager->tablesExist($table)) {
1461
                $tableCreationWorks = true;
1462
                $sql = "ALTER TABLE $table ADD COLUMN name2 varchar(140) ";
1463
                $connection->query($sql);
1464
                $schemaManager->dropTable($table);
1465
                $tableDropWorks = false === $schemaManager->tablesExist($table);
1466
            }
1467
        } else {
1468
            $manager = connectToDatabase(
1469
                $dbHostForm,
1470
                $dbUsernameForm,
1471
                $dbPassForm,
1472
                null,
1473
                $dbPortForm
1474
            );
1475
1476
            $schemaManager = $manager->getConnection()->getSchemaManager();
1477
            $databases = $schemaManager->listDatabases();
1478
            if (in_array($dbNameForm, $databases)) {
1479
                $database_exists_text = '<div class="alert alert-warning">'.get_lang('A database with the same name <b>already exists</b>.').'</div>';
1480
            }
1481
        }
1482
    } catch (Exception $e) {
1483
        $database_exists_text = $e->getMessage();
1484
        $manager = false;
1485
    }
1486
1487
    if ($manager && $manager->getConnection()->isConnected()): ?>
1488
        <?php echo $database_exists_text; ?>
1489
        <div id="db_status" class="alert alert-success">
1490
            Database host: <strong><?php echo $manager->getConnection()->getHost(); ?></strong><br/>
1491
            Database port: <strong><?php echo $manager->getConnection()->getPort(); ?></strong><br/>
1492
            Database driver: <strong><?php echo $manager->getConnection()->getDriver()->getName(); ?></strong><br/>
1493
            <?php
1494
                if ('update' === $installType) {
1495
                    echo get_lang('CreateTableWorks').' <strong>Ok</strong>';
1496
                    echo '<br/ >';
1497
                    echo get_lang('AlterTableWorks').' <strong>Ok</strong>';
1498
                    echo '<br/ >';
1499
                    echo get_lang('DropColumnWorks').' <strong>Ok</strong>';
1500
                } ?>
1501
        </div>
1502
    <?php else: ?>
1503
        <div id="db_status" class="alert alert-danger">
1504
            <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>
1505
            <code><?php echo $database_exists_text; ?></code>
1506
        </div>
1507
    <?php endif; ?>
1508
1509
   <div class="btn-group" role="group">
1510
       <button type="submit" name="step2"
1511
               class="btn btn-secondary float-right" value="&lt; <?php echo get_lang('Previous'); ?>" >
1512
           <em class="fa fa-backward"> </em> <?php echo get_lang('Previous'); ?>
1513
       </button>
1514
       <input type="hidden" name="is_executable" id="is_executable" value="-" />
1515
       <?php if ($manager) {
1516
                    ?>
1517
           <button type="submit" class="btn btn-success" name="step4" value="<?php echo get_lang('Next'); ?> &gt;" >
1518
               <em class="fa fa-forward"> </em> <?php echo get_lang('Next'); ?>
1519
           </button>
1520
       <?php
1521
                } else {
1522
                    ?>
1523
           <button
1524
                   disabled="disabled"
1525
                   type="submit" class="btn btn-success disabled" name="step4" value="<?php echo get_lang('Next'); ?> &gt;" >
1526
               <em class="fa fa-forward"> </em> <?php echo get_lang('Next'); ?>
1527
           </button>
1528
       <?php
1529
                } ?>
1530
   </div>
1531
    <?php
1532
}
1533
1534
function panel($content = null, $title = null, $id = null, $style = null)
1535
{
1536
    $html = '';
1537
    if (empty($style)) {
1538
        $style = 'default';
1539
    }
1540
    if (!empty($title)) {
1541
        $panelTitle = Display::div($title, ['class' => 'panel-heading']);
1542
        $panelBody = Display::div($content, ['class' => 'panel-body']);
1543
        $panelParent = Display::div($panelTitle.$panelBody, ['id' => $id, 'class' => 'panel panel-'.$style]);
1544
    } else {
1545
        $panelBody = Display::div($html, ['class' => 'panel-body']);
1546
        $panelParent = Display::div($panelBody, ['id' => $id, 'class' => 'panel panel-'.$style]);
1547
    }
1548
    $html .= $panelParent;
1549
1550
    return $html;
1551
}
1552
1553
/**
1554
 * Displays a parameter in a table row.
1555
 * Used by the display_configuration_settings_form function.
1556
 *
1557
 * @param string $installType
1558
 * @param string $parameterName
1559
 * @param string $formFieldName
1560
 * @param string $parameterValue
1561
 * @param string $displayWhenUpdate
1562
 *
1563
 * @return string
1564
 */
1565
function display_configuration_parameter(
1566
    $installType,
1567
    $parameterName,
1568
    $formFieldName,
1569
    $parameterValue,
1570
    $displayWhenUpdate = 'true'
1571
) {
1572
    $html = '<div class="form-group row">';
1573
    $html .= '<label class="col-sm-6 control-label">'.$parameterName.'</label>';
1574
    if (INSTALL_TYPE_UPDATE == $installType && $displayWhenUpdate) {
1575
        $html .= '<input type="hidden" name="'.$formFieldName.'" value="'.api_htmlentities($parameterValue, ENT_QUOTES).'" />'.$parameterValue;
1576
    } else {
1577
        $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>";
1578
    }
1579
    $html .= "</div>";
1580
1581
    return $html;
1582
}
1583
1584
/**
1585
 * Displays step 4 of the installation - configuration settings about Chamilo itself.
1586
 *
1587
 * @param string $installType
1588
 * @param string $urlForm
1589
 * @param string $languageForm
1590
 * @param string $emailForm
1591
 * @param string $adminFirstName
1592
 * @param string $adminLastName
1593
 * @param string $adminPhoneForm
1594
 * @param string $campusForm
1595
 * @param string $institutionForm
1596
 * @param string $institutionUrlForm
1597
 * @param string $encryptPassForm
1598
 * @param bool   $allowSelfReg
1599
 * @param bool   $allowSelfRegProf
1600
 * @param string $loginForm
1601
 * @param string $passForm
1602
 */
1603
function display_configuration_settings_form(
1604
    $installType,
1605
    $urlForm,
1606
    $languageForm,
1607
    $emailForm,
1608
    $adminFirstName,
1609
    $adminLastName,
1610
    $adminPhoneForm,
1611
    $campusForm,
1612
    $institutionForm,
1613
    $institutionUrlForm,
1614
    $encryptPassForm,
1615
    $allowSelfReg,
1616
    $allowSelfRegProf,
1617
    $loginForm,
1618
    $passForm
1619
) {
1620
    if ('update' != $installType && empty($languageForm)) {
1621
        $languageForm = $_SESSION['install_language'];
1622
    }
1623
    echo '<div class="RequirementHeading">';
1624
    echo "<h2>".display_step_sequence().get_lang("ConfigurationSettings")."</h2>";
1625
    echo '</div>';
1626
1627
    // Parameter 1: administrator's login
1628
    $html = '';
1629
    $html .= display_configuration_parameter(
1630
        $installType,
1631
        get_lang('Administrator login'),
1632
        'loginForm',
1633
        $loginForm,
1634
        'update' == $installType
1635
    );
1636
1637
    // Parameter 2: administrator's password
1638
    if ('update' != $installType) {
1639
        $html .= display_configuration_parameter(
1640
            $installType,
1641
            get_lang('Administrator password (<font color="red">you may want to change this</font>)'),
1642
            'passForm',
1643
            $passForm,
1644
            false
1645
        );
1646
    }
1647
1648
    // Parameters 3 and 4: administrator's names
1649
1650
    $html .= display_configuration_parameter(
1651
        $installType,
1652
        get_lang('Administrator first name'),
1653
        'adminFirstName',
1654
        $adminFirstName
1655
    );
1656
    $html .= display_configuration_parameter($installType, get_lang('Administrator last name'), 'adminLastName', $adminLastName);
1657
1658
    //Parameter 3: administrator's email
1659
    $html .= display_configuration_parameter($installType, get_lang('Admine-mail'), 'emailForm', $emailForm);
1660
1661
    //Parameter 6: administrator's telephone
1662
    $html .= display_configuration_parameter($installType, get_lang('Administrator telephone'), 'adminPhoneForm', $adminPhoneForm);
1663
    echo panel($html, get_lang('Administrator'), 'administrator');
1664
1665
    //First parameter: language
1666
    $html = '<div class="form-group row">';
1667
    $html .= '<label class="col-sm-6 control-label">'.get_lang('Main language')."</label>";
1668
    if ('update' == $installType) {
1669
        $html .= '<input type="hidden" name="languageForm" value="'.api_htmlentities($languageForm, ENT_QUOTES).'" />'.$languageForm;
1670
    } else { // new installation
1671
        $html .= '<div class="col-sm-6">';
1672
        $html .= display_language_selection_box('languageForm', $languageForm);
1673
        $html .= '</div>';
1674
    }
1675
    $html .= "</div>";
1676
1677
    //Second parameter: Chamilo URL
1678
    $html .= '<div class="form-group row">';
1679
    $html .= '<label class="col-sm-6 control-label">'.get_lang('Chamilo URL').get_lang('Required field').'</label>';
1680
1681
    if ('update' == $installType) {
1682
        $html .= api_htmlentities($urlForm, ENT_QUOTES)."\n";
1683
    } else {
1684
        $html .= '<div class="col-sm-6">';
1685
        $html .= '<input class="form-control" type="text" size="40" maxlength="100" name="urlForm" value="'.api_htmlentities($urlForm, ENT_QUOTES).'" />';
1686
        $html .= '</div>';
1687
    }
1688
    $html .= '</div>';
1689
1690
    //Parameter 9: campus name
1691
    $html .= display_configuration_parameter(
1692
        $installType,
1693
        get_lang('Your portal name'),
1694
        'campusForm',
1695
        $campusForm
1696
    );
1697
1698
    //Parameter 10: institute (short) name
1699
    $html .= display_configuration_parameter(
1700
        $installType,
1701
        get_lang('Your company short name'),
1702
        'institutionForm',
1703
        $institutionForm
1704
    );
1705
1706
    //Parameter 11: institute (short) name
1707
    $html .= display_configuration_parameter(
1708
        $installType,
1709
        get_lang('URL of this company'),
1710
        'institutionUrlForm',
1711
        $institutionUrlForm
1712
    );
1713
1714
    $html .= '<div class="form-group row">
1715
            <label class="col-sm-6 control-label">'.get_lang("Encryption method").'</label>
1716
        <div class="col-sm-6">';
1717
    if ('update' == $installType) {
1718
        $html .= '<input type="hidden" name="encryptPassForm" value="'.$encryptPassForm.'" />'.$encryptPassForm;
1719
    } else {
1720
        $html .= '<div class="checkbox">
1721
                    <label>
1722
                        <input  type="radio" name="encryptPassForm" value="bcrypt" id="encryptPass1" '.('bcrypt' == $encryptPassForm ? 'checked="checked" ' : '').'/> bcrypt
1723
                    </label>';
1724
1725
        $html .= '<label>
1726
                        <input  type="radio" name="encryptPassForm" value="sha1" id="encryptPass1" '.('sha1' == $encryptPassForm ? 'checked="checked" ' : '').'/> sha1
1727
                    </label>';
1728
1729
        $html .= '<label>
1730
                        <input type="radio" name="encryptPassForm" value="md5" id="encryptPass0" '.('md5' == $encryptPassForm ? 'checked="checked" ' : '').'/> md5
1731
                    </label>';
1732
1733
        $html .= '<label>
1734
                        <input type="radio" name="encryptPassForm" value="none" id="encryptPass2" '.('none' == $encryptPassForm ? 'checked="checked" ' : '').'/>'.get_lang('none').'
1735
                    </label>';
1736
        $html .= '</div>';
1737
    }
1738
    $html .= '</div></div>';
1739
1740
    $html .= '<div class="form-group row">
1741
            <label class="col-sm-6 control-label">'.get_lang('Allow self-registration').'</label>
1742
            <div class="col-sm-6">';
1743
    if ('update' == $installType) {
1744
        if ('true' == $allowSelfReg) {
1745
            $label = get_lang('Yes');
1746
        } elseif ('false' == $allowSelfReg) {
1747
            $label = get_lang('No');
1748
        } else {
1749
            $label = get_lang('After approval');
1750
        }
1751
        $html .= '<input type="hidden" name="allowSelfReg" value="'.$allowSelfReg.'" />'.$label;
1752
    } else {
1753
        $html .= '<div class="control-group">';
1754
        $html .= '<label class="checkbox-inline">
1755
                        <input type="radio" name="allowSelfReg" value="true" id="allowSelfReg1" '.('true' == $allowSelfReg ? 'checked="checked" ' : '').' /> '.get_lang('Yes').'
1756
                    </label>';
1757
        $html .= '<label class="checkbox-inline">
1758
                        <input type="radio" name="allowSelfReg" value="false" id="allowSelfReg0" '.('false' == $allowSelfReg ? '' : 'checked="checked" ').' /> '.get_lang('No').'
1759
                    </label>';
1760
        $html .= '<label class="checkbox-inline">
1761
                    <input type="radio" name="allowSelfReg" value="approval" id="allowSelfReg2" '.('approval' == $allowSelfReg ? '' : 'checked="checked" ').' /> '.get_lang('After approval').'
1762
                </label>';
1763
        $html .= '</div>';
1764
    }
1765
    $html .= '</div>';
1766
    $html .= '</div>';
1767
1768
    $html .= '<div class="form-group row">';
1769
    $html .= '<label class="col-sm-6 control-label">'.get_lang('Allow self-registrationProf').'</label>
1770
        <div class="col-sm-6">';
1771
    if ('update' == $installType) {
1772
        if ('true' == $allowSelfRegProf) {
1773
            $label = get_lang('Yes');
1774
        } else {
1775
            $label = get_lang('No');
1776
        }
1777
        $html .= '<input type="hidden" name="allowSelfRegProf" value="'.$allowSelfRegProf.'" />'.$label;
1778
    } else {
1779
        $html .= '<div class="control-group">
1780
                <label class="checkbox-inline">
1781
                    <input type="radio" name="allowSelfRegProf" value="1" id="allowSelfRegProf1" '.($allowSelfRegProf ? 'checked="checked" ' : '').'/>
1782
                '.get_lang('Yes').'
1783
                </label>';
1784
        $html .= '<label class="checkbox-inline">
1785
                    <input type="radio" name="allowSelfRegProf" value="0" id="allowSelfRegProf0" '.($allowSelfRegProf ? '' : 'checked="checked" ').' />
1786
                   '.get_lang('No').'
1787
                </label>';
1788
        $html .= '</div>';
1789
    }
1790
    $html .= '</div>
1791
    </div>';
1792
1793
    echo panel($html, get_lang('Portal'), 'platform'); ?>
1794
    <div class='btn-group'>
1795
        <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>
1796
        <input type="hidden" name="is_executable" id="is_executable" value="-" />
1797
        <button class="btn btn-success" type="submit" name="step5">
1798
            <em class="fa fa-forward"> </em> <?php echo get_lang('Next'); ?>
1799
        </button>
1800
    </div>
1801
    <?php
1802
}
1803
1804
/**
1805
 * After installation is completed (step 6), this message is displayed.
1806
 */
1807
function display_after_install_message()
1808
{
1809
    $container = Container::$container;
1810
1811
    $trans = $container->get('translator');
1812
    $html = '<div class="RequirementContent">'.
1813
        $trans->trans(
1814
            '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>';
1815
    $html .= '<div class="alert alert-warning">';
1816
    $html .= '<strong>'.$trans->trans('Security advice').'</strong>';
1817
    $html .= ': ';
1818
    $html .= sprintf($trans->trans(
1819
        '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/');
1820
    $html .= '</div></form>
1821
    <br />
1822
    <a class="btn btn-success btn-block" href="../../">
1823
        '.$trans->trans('Go to your newly created portal.').'
1824
    </a>';
1825
1826
    return $html;
1827
}
1828
1829
/**
1830
 * This function return countries list from array (hardcoded).
1831
 *
1832
 * @param bool $combo (Optional) True for returning countries list with select html
1833
 *
1834
 * @return array|string countries list
1835
 */
1836
function get_countries_list_from_array($combo = false)
1837
{
1838
    $a_countries = [
1839
        "Afghanistan", "Albania", "Algeria", "Andorra", "Angola", "Antigua and Barbuda", "Argentina", "Armenia", "Australia", "Austria", "Azerbaijan",
1840
        "Bahamas", "Bahrain", "Bangladesh", "Barbados", "Belarus", "Belgium", "Belize", "Benin", "Bhutan", "Bolivia", "Bosnia and Herzegovina", "Botswana", "Brazil", "Brunei", "Bulgaria", "Burkina Faso", "Burundi",
1841
        "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",
1842
        "Denmark", "Djibouti", "Dominica", "Dominican Republic",
1843
        "East Timor (Timor Timur)", "Ecuador", "Egypt", "El Salvador", "Equatorial Guinea", "Eritrea", "Estonia", "Ethiopia",
1844
        "Fiji", "Finland", "France",
1845
        "Gabon", "Gambia, The", "Georgia", "Germany", "Ghana", "Greece", "Grenada", "Guatemala", "Guinea", "Guinea-Bissau", "Guyana",
1846
        "Haiti", "Honduras", "Hungary",
1847
        "Iceland", "India", "Indonesia", "Iran", "Iraq", "Ireland", "Israel", "Italy",
1848
        "Jamaica", "Japan", "Jordan",
1849
        "Kazakhstan", "Kenya", "Kiribati", "Korea, North", "Korea, South", "Kuwait", "Kyrgyzstan",
1850
        "Laos", "Latvia", "Lebanon", "Lesotho", "Liberia", "Libya", "Liechtenstein", "Lithuania", "Luxembourg",
1851
        "Macedonia", "Madagascar", "Malawi", "Malaysia", "Maldives", "Mali", "Malta", "Marshall Islands", "Mauritania", "Mauritius", "Mexico", "Micronesia", "Moldova", "Monaco", "Mongolia", "Morocco", "Mozambique", "Myanmar",
1852
        "Namibia", "Nauru", "Nepa", "Netherlands", "New Zealand", "Nicaragua", "Niger", "Nigeria", "Norway",
1853
        "Oman",
1854
        "Pakistan", "Palau", "Panama", "Papua New Guinea", "Paraguay", "Peru", "Philippines", "Poland", "Portugal",
1855
        "Qatar",
1856
        "Romania", "Russia", "Rwanda",
1857
        "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",
1858
        "Taiwan", "Tajikistan", "Tanzania", "Thailand", "Togo", "Tonga", "Trinidad and Tobago", "Tunisia", "Turkey", "Turkmenistan", "Tuvalu",
1859
        "Uganda", "Ukraine", "United Arab Emirates", "United Kingdom", "United States", "Uruguay", "Uzbekistan",
1860
        "Vanuatu", "Vatican City", "Venezuela", "Vietnam",
1861
        "Yemen",
1862
        "Zambia", "Zimbabwe",
1863
    ];
1864
    if ($combo) {
1865
        $country_select = '<select class="selectpicker show-tick" id="country" name="country">';
1866
        $country_select .= '<option value="">--- '.get_lang('Select one').' ---</option>';
1867
        foreach ($a_countries as $country) {
1868
            $country_select .= '<option value="'.$country.'">'.$country.'</option>';
1869
        }
1870
        $country_select .= '</select>';
1871
1872
        return $country_select;
1873
    }
1874
1875
    return $a_countries;
1876
}
1877
1878
/**
1879
 * Lock settings that can't be changed in other portals.
1880
 */
1881
function lockSettings()
1882
{
1883
    $settings = api_get_locked_settings();
1884
    $table = Database::get_main_table(TABLE_MAIN_SETTINGS_CURRENT);
1885
    foreach ($settings as $setting) {
1886
        $sql = "UPDATE $table SET access_url_locked = 1 WHERE variable  = '$setting'";
1887
        Database::query($sql);
1888
    }
1889
}
1890
1891
/**
1892
 * Update dir values.
1893
 */
1894
function updateDirAndFilesPermissions()
1895
{
1896
    $table = Database::get_main_table(TABLE_MAIN_SETTINGS_CURRENT);
1897
    $permissions_for_new_directories = isset($_SESSION['permissions_for_new_directories']) ? $_SESSION['permissions_for_new_directories'] : 0770;
1898
    $permissions_for_new_files = isset($_SESSION['permissions_for_new_files']) ? $_SESSION['permissions_for_new_files'] : 0660;
1899
    // use decoct() to store as string
1900
    Database::update(
1901
        $table,
1902
        ['selected_value' => '0'.decoct($permissions_for_new_directories)],
1903
        ['variable = ?' => 'permissions_for_new_directories']
1904
    );
1905
1906
    Database::update(
1907
        $table,
1908
        ['selected_value' => '0'.decoct($permissions_for_new_files)],
1909
        ['variable = ?' => 'permissions_for_new_files']
1910
    );
1911
1912
    if (isset($_SESSION['permissions_for_new_directories'])) {
1913
        unset($_SESSION['permissions_for_new_directories']);
1914
    }
1915
1916
    if (isset($_SESSION['permissions_for_new_files'])) {
1917
        unset($_SESSION['permissions_for_new_files']);
1918
    }
1919
}
1920
1921
/**
1922
 * @param $current_value
1923
 * @param $wanted_value
1924
 *
1925
 * @return string
1926
 */
1927
function compare_setting_values($current_value, $wanted_value)
1928
{
1929
    $current_value_string = $current_value;
1930
    $current_value = (float) $current_value;
1931
    $wanted_value = (float) $wanted_value;
1932
1933
    if ($current_value >= $wanted_value) {
1934
        return Display::label($current_value_string, 'success');
1935
    } else {
1936
        return Display::label($current_value_string, 'important');
1937
    }
1938
}
1939
1940
/**
1941
 * Save settings values.
1942
 *
1943
 * @param string $organizationName
1944
 * @param string $organizationUrl
1945
 * @param string $siteName
1946
 * @param string $adminEmail
1947
 * @param string $adminLastName
1948
 * @param string $adminFirstName
1949
 * @param string $language
1950
 * @param string $allowRegistration
1951
 * @param string $allowTeacherSelfRegistration
1952
 * @param string $installationProfile          The name of an installation profile file in main/install/profiles/
1953
 */
1954
function installSettings(
1955
    $organizationName,
1956
    $organizationUrl,
1957
    $siteName,
1958
    $adminEmail,
1959
    $adminLastName,
1960
    $adminFirstName,
1961
    $language,
1962
    $allowRegistration,
1963
    $allowTeacherSelfRegistration,
1964
    $installationProfile = ''
1965
) {
1966
    error_log('installSettings');
1967
    $allowTeacherSelfRegistration = $allowTeacherSelfRegistration ? 'true' : 'false';
1968
1969
    $settings = [
1970
        'institution' => $organizationName,
1971
        'institution_url' => $organizationUrl,
1972
        'site_name' => $siteName,
1973
        'administrator_email' => $adminEmail,
1974
        'administrator_surname' => $adminLastName,
1975
        'administrator_name' => $adminFirstName,
1976
        'platform_language' => $language,
1977
        'allow_registration' => $allowRegistration,
1978
        'allow_registration_as_teacher' => $allowTeacherSelfRegistration,
1979
    ];
1980
1981
    foreach ($settings as $variable => $value) {
1982
        $sql = "UPDATE settings_current
1983
                SET selected_value = '$value'
1984
                WHERE variable = '$variable'";
1985
        Database::query($sql);
1986
    }
1987
    installProfileSettings($installationProfile);
1988
}
1989
1990
/**
1991
 * Executes DB changes based in the classes defined in
1992
 * src/CoreBundle/Migrations/Schema/*.
1993
 *
1994
 * @param string $chamiloVersion
1995
 *
1996
 * @throws \Doctrine\DBAL\DBALException
1997
 *
1998
 * @return bool
1999
 */
2000
function migrate($chamiloVersion, EntityManager $manager)
2001
{
2002
    $debug = true;
2003
    $connection = $manager->getConnection();
2004
2005
    $config = new \Doctrine\DBAL\Migrations\Configuration\Configuration($connection);
2006
2007
    // Table name that will store migrations log (will be created automatically,
2008
    // default name is: doctrine_migration_versions)
2009
    $config->setMigrationsTableName('version');
2010
    // Namespace of your migration classes, do not forget escape slashes, do not add last slash
2011
    $config->setMigrationsNamespace('Chamilo\CoreBundle\Migrations\Schema\V'.$chamiloVersion);
2012
    // Directory where your migrations are located
2013
    $versionPath = api_get_path(SYS_PATH).'src/CoreBundle/Migrations/Schema/V'.$chamiloVersion;
2014
    error_log("Reading files from dir: $versionPath");
2015
    $config->setMigrationsDirectory($versionPath);
2016
    // Load your migrations
2017
    $config->registerMigrationsFromDirectory($config->getMigrationsDirectory());
2018
2019
    $migration = new \Doctrine\DBAL\Migrations\Migration($config);
2020
    $versions = $config->getMigrations();
2021
2022
    /** @var Doctrine\DBAL\Migrations\Version $migrationItem */
2023
    foreach ($versions as $version) {
2024
        $version->getMigration()->setEntityManager($manager);
2025
    }
2026
2027
    $to = null; // if $to == null then schema will be migrated to latest version
2028
    echo '<pre>';
2029
    try {
2030
        // Execute migration!
2031
        $migratedSQL = $migration->migrate($to);
2032
2033
        if ($debug) {
2034
            foreach ($migratedSQL as $version => $sqlList) {
2035
                echo "VERSION: $version<br>";
2036
                echo '----------------------------------------------<br />';
2037
                $total = count($sqlList);
2038
                error_log("VERSION: $version");
2039
                error_log("# queries: $total");
2040
                $counter = 1;
2041
                foreach ($sqlList as $sql) {
2042
                    echo "<code>$sql</code><br>";
2043
                    error_log("$counter/$total : $sql");
2044
                    $counter++;
2045
                }
2046
            }
2047
2048
            echo "<br>DONE!<br>";
2049
        }
2050
2051
        return true;
2052
    } catch (Exception $ex) {
2053
        if ($debug) {
2054
            echo "ERROR: {$ex->getMessage()}<br>";
2055
2056
            return false;
2057
        }
2058
    }
2059
2060
    echo '</pre>';
2061
2062
    return false;
2063
}
2064
2065
/**
2066
 * @throws \Doctrine\DBAL\DBALException
2067
 */
2068
function fixIds(EntityManager $em)
2069
{
2070
    $connection = $em->getConnection();
2071
    $database = new Database();
2072
    $database->setManager($em);
2073
    $debug = true;
2074
    if ($debug) {
2075
        error_log('fixIds');
2076
    }
2077
2078
    // Create temporary indexes to increase speed of the following operations
2079
    // Adding and removing indexes will usually take much less time than
2080
    // the execution without indexes of the queries in this function, particularly
2081
    // for large tables
2082
    $sql = "ALTER TABLE c_document ADD INDEX tmpidx_doc(c_id, id)";
2083
    $connection->executeQuery($sql);
2084
    $sql = "ALTER TABLE c_student_publication ADD INDEX tmpidx_stud (c_id, id)";
2085
    $connection->executeQuery($sql);
2086
    $sql = "ALTER TABLE c_quiz ADD INDEX tmpidx_quiz (c_id, id)";
2087
    $connection->executeQuery($sql);
2088
    $sql = "ALTER TABLE c_item_property ADD INDEX tmpidx_ip (to_group_id)";
2089
    $connection->executeQuery($sql);
2090
2091
    $sql = "SELECT * FROM c_lp_item";
2092
    $result = $connection->fetchAll($sql);
2093
    foreach ($result as $item) {
2094
        $courseId = $item['c_id'];
2095
        $iid = isset($item['iid']) ? intval($item['iid']) : 0;
2096
        $ref = isset($item['ref']) ? intval($item['ref']) : 0;
2097
        $sql = null;
2098
2099
        $newId = '';
2100
2101
        switch ($item['item_type']) {
2102
            case TOOL_LINK:
2103
                $sql = "SELECT * FROM c_link WHERE c_id = $courseId AND id = $ref";
2104
                $data = $connection->fetchAssoc($sql);
2105
                if ($data) {
2106
                    $newId = $data['iid'];
2107
                }
2108
                break;
2109
            case TOOL_STUDENTPUBLICATION:
2110
                $sql = "SELECT * FROM c_student_publication WHERE c_id = $courseId AND id = $ref";
2111
                $data = $connection->fetchAssoc($sql);
2112
                if ($data) {
2113
                    $newId = $data['iid'];
2114
                }
2115
                break;
2116
            case TOOL_QUIZ:
2117
                $sql = "SELECT * FROM c_quiz WHERE c_id = $courseId AND id = $ref";
2118
                $data = $connection->fetchAssoc($sql);
2119
                if ($data) {
2120
                    $newId = $data['iid'];
2121
                }
2122
                break;
2123
            case TOOL_DOCUMENT:
2124
                $sql = "SELECT * FROM c_document WHERE c_id = $courseId AND id = $ref";
2125
                $data = $connection->fetchAssoc($sql);
2126
                if ($data) {
2127
                    $newId = $data['iid'];
2128
                }
2129
                break;
2130
            case TOOL_FORUM:
2131
                $sql = "SELECT * FROM c_forum_forum WHERE c_id = $courseId AND forum_id = $ref";
2132
                $data = $connection->fetchAssoc($sql);
2133
                if ($data) {
2134
                    $newId = $data['iid'];
2135
                }
2136
                break;
2137
            case 'thread':
2138
                $sql = "SELECT * FROM c_forum_thread WHERE c_id = $courseId AND thread_id = $ref";
2139
                $data = $connection->fetchAssoc($sql);
2140
                if ($data) {
2141
                    $newId = $data['iid'];
2142
                }
2143
                break;
2144
        }
2145
2146
        if (!empty($sql) && !empty($newId) && !empty($iid)) {
2147
            $sql = "UPDATE c_lp_item SET ref = $newId WHERE iid = $iid";
2148
            $connection->executeQuery($sql);
2149
        }
2150
    }
2151
2152
    // Set NULL if session = 0
2153
    $sql = "UPDATE c_item_property SET session_id = NULL WHERE session_id = 0";
2154
    $connection->executeQuery($sql);
2155
2156
    // Set NULL if group = 0
2157
    $sql = "UPDATE c_item_property SET to_group_id = NULL WHERE to_group_id = 0";
2158
    $connection->executeQuery($sql);
2159
2160
    // Set NULL if insert_user_id = 0
2161
    $sql = "UPDATE c_item_property SET insert_user_id = NULL WHERE insert_user_id = 0";
2162
    $connection->executeQuery($sql);
2163
2164
    // Delete session data of sessions that don't exist.
2165
    $sql = "DELETE FROM c_item_property
2166
            WHERE session_id IS NOT NULL AND session_id NOT IN (SELECT id FROM session)";
2167
    $connection->executeQuery($sql);
2168
2169
    // Delete group data of groups that don't exist.
2170
    $sql = "DELETE FROM c_item_property
2171
            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)";
2172
    $connection->executeQuery($sql);
2173
    // This updates the group_id with c_group_info.iid instead of c_group_info.id
2174
2175
    if ($debug) {
2176
        error_log('update iids');
2177
    }
2178
2179
    $groupTableToFix = [
2180
        'c_group_rel_user',
2181
        'c_group_rel_tutor',
2182
        'c_permission_group',
2183
        'c_role_group',
2184
        'c_survey_invitation',
2185
        'c_attendance_calendar_rel_group',
2186
    ];
2187
2188
    foreach ($groupTableToFix as $table) {
2189
        $sql = "SELECT * FROM $table";
2190
        $result = $connection->fetchAll($sql);
2191
        foreach ($result as $item) {
2192
            $iid = $item['iid'];
2193
            $courseId = $item['c_id'];
2194
            $groupId = intval($item['group_id']);
2195
2196
            // Fix group id
2197
            if (!empty($groupId)) {
2198
                $sql = "SELECT * FROM c_group_info
2199
                        WHERE c_id = $courseId AND id = $groupId
2200
                        LIMIT 1";
2201
                $data = $connection->fetchAssoc($sql);
2202
                if (!empty($data)) {
2203
                    $newGroupId = $data['iid'];
2204
                    $sql = "UPDATE $table SET group_id = $newGroupId
2205
                            WHERE iid = $iid";
2206
                    $connection->executeQuery($sql);
2207
                } else {
2208
                    // The group does not exists clean this record
2209
                    $sql = "DELETE FROM $table WHERE iid = $iid";
2210
                    $connection->executeQuery($sql);
2211
                }
2212
            }
2213
        }
2214
    }
2215
2216
    // Fix c_item_property
2217
    if ($debug) {
2218
        error_log('update c_item_property');
2219
    }
2220
2221
    $sql = "SELECT * FROM course";
2222
    $courseList = $connection->fetchAll($sql);
2223
    if ($debug) {
2224
        error_log('Getting course list');
2225
    }
2226
2227
    $totalCourse = count($courseList);
2228
    $counter = 0;
2229
2230
    foreach ($courseList as $courseData) {
2231
        $courseId = $courseData['id'];
2232
        if ($debug) {
2233
            error_log('Updating course: '.$courseData['code']);
2234
        }
2235
2236
        $sql = "SELECT * FROM c_item_property WHERE c_id = $courseId";
2237
        $result = $connection->fetchAll($sql);
2238
        foreach ($result as $item) {
2239
            $sessionId = intval($item['session_id']);
2240
            $groupId = intval($item['to_group_id']);
2241
            $iid = $item['iid'];
2242
            $ref = $item['ref'];
2243
2244
            // Fix group id
2245
            // Commented group id is already fixed in Version20150603181728.php
2246
            /*if (!empty($groupId)) {
2247
                $sql = "SELECT * FROM c_group_info
2248
                        WHERE c_id = $courseId AND id = $groupId";
2249
                $data = $connection->fetchAssoc($sql);
2250
                if (!empty($data)) {
2251
                    $newGroupId = $data['iid'];
2252
                    $sql = "UPDATE c_item_property SET to_group_id = $newGroupId
2253
                            WHERE iid = $iid";
2254
                    $connection->executeQuery($sql);
2255
                } else {
2256
                    // The group does not exists clean this record
2257
                    $sql = "DELETE FROM c_item_property WHERE iid = $iid";
2258
                    $connection->executeQuery($sql);
2259
                }
2260
            }*/
2261
2262
            $sql = '';
2263
            $newId = '';
2264
            switch ($item['tool']) {
2265
                case TOOL_LINK:
2266
                    $sql = "SELECT * FROM c_link WHERE c_id = $courseId AND id = $ref ";
2267
                    break;
2268
                case TOOL_STUDENTPUBLICATION:
2269
                    $sql = "SELECT * FROM c_student_publication WHERE c_id = $courseId AND id = $ref";
2270
                    break;
2271
                case TOOL_QUIZ:
2272
                    $sql = "SELECT * FROM c_quiz WHERE c_id = $courseId AND id = $ref";
2273
                    break;
2274
                case TOOL_DOCUMENT:
2275
                    $sql = "SELECT * FROM c_document WHERE c_id = $courseId AND id = $ref";
2276
                    break;
2277
                case TOOL_FORUM:
2278
                    $sql = "SELECT * FROM c_forum_forum WHERE c_id = $courseId AND id = $ref";
2279
                    break;
2280
                case 'thread':
2281
                    $sql = "SELECT * FROM c_forum_thread WHERE c_id = $courseId AND id = $ref";
2282
                    break;
2283
            }
2284
2285
            if (!empty($sql) && !empty($newId)) {
2286
                $data = $connection->fetchAssoc($sql);
2287
                if (isset($data['iid'])) {
2288
                    $newId = $data['iid'];
2289
                }
2290
                $sql = "UPDATE c_item_property SET ref = $newId WHERE iid = $iid";
2291
                error_log($sql);
2292
                $connection->executeQuery($sql);
2293
            }
2294
        }
2295
2296
        if ($debug) {
2297
            // Print a status in the log once in a while
2298
            error_log("Course process #$counter/$totalCourse");
2299
        }
2300
        $counter++;
2301
    }
2302
2303
    if ($debug) {
2304
        error_log('update gradebook_link');
2305
    }
2306
2307
    // Fix gradebook_link
2308
    $sql = "SELECT * FROM gradebook_link";
2309
    $result = $connection->fetchAll($sql);
2310
    foreach ($result as $item) {
2311
        $courseCode = $item['course_code'];
2312
        $categoryId = (int) $item['category_id'];
2313
2314
        $sql = "SELECT * FROM course WHERE code = '$courseCode'";
2315
        $courseInfo = $connection->fetchAssoc($sql);
2316
        if (empty($courseInfo)) {
2317
            continue;
2318
        }
2319
2320
        $courseId = $courseInfo['id'];
2321
2322
        $ref = $item['ref_id'];
2323
        $iid = $item['id'];
2324
2325
        $sql = '';
2326
        switch ($item['type']) {
2327
            case LINK_LEARNPATH:
2328
                $sql = "SELECT * FROM c_link WHERE c_id = $courseId AND id = $ref ";
2329
                break;
2330
            case LINK_STUDENTPUBLICATION:
2331
                $sql = "SELECT * FROM c_student_publication WHERE c_id = $courseId AND id = $ref";
2332
                break;
2333
            case LINK_EXERCISE:
2334
                $sql = "SELECT * FROM c_quiz WHERE c_id = $courseId AND id = $ref";
2335
                break;
2336
            case LINK_ATTENDANCE:
2337
                //$sql = "SELECT * FROM c_document WHERE c_id = $courseId AND id = $ref";
2338
                break;
2339
            case LINK_FORUM_THREAD:
2340
                $sql = "SELECT * FROM c_forum_thread WHERE c_id = $courseId AND thread_id = $ref";
2341
                break;
2342
        }
2343
2344
        if (!empty($sql)) {
2345
            $data = $connection->fetchAssoc($sql);
2346
            if (isset($data) && isset($data['iid'])) {
2347
                $newId = $data['iid'];
2348
                $sql = "UPDATE gradebook_link SET ref_id = $newId
2349
                        WHERE id = $iid AND course_code = '$courseCode' AND category_id = $categoryId ";
2350
                $connection->executeQuery($sql);
2351
            }
2352
        }
2353
    }
2354
2355
    if ($debug) {
2356
        error_log('update groups');
2357
    }
2358
2359
    $sql = 'SELECT * FROM groups';
2360
    $result = $connection->executeQuery($sql);
2361
    $groups = $result->fetchAll();
2362
    $oldGroups = [];
2363
    if (!empty($groups)) {
2364
        foreach ($groups as $group) {
2365
            if (empty($group['name'])) {
2366
                continue;
2367
            }
2368
2369
            $params = [
2370
                'name' => $group['name'],
2371
                'description' => $group['description'],
2372
                'group_type' => 1,
2373
                'picture' => $group['picture_uri'],
2374
                'url' => $group['url'],
2375
                'visibility' => $group['visibility'],
2376
                'updated_at' => $group['updated_on'],
2377
                'created_at' => $group['created_on'],
2378
            ];
2379
            $connection->insert('usergroup', $params);
2380
            $id = $connection->lastInsertId('id');
2381
            $oldGroups[$group['id']] = $id;
2382
        }
2383
    }
2384
2385
    if (!empty($oldGroups)) {
2386
        error_log('Moving group files');
2387
        foreach ($oldGroups as $oldId => $newId) {
2388
            $path = get_group_picture_path_by_id(
2389
                $oldId,
2390
                'system'
2391
            );
2392
2393
            if (!empty($path)) {
2394
                $newPath = str_replace(
2395
                    "groups/$oldId/",
2396
                    "groups/$newId/",
2397
                    $path['dir']
2398
                );
2399
                $command = "mv {$path['dir']} $newPath ";
2400
                error_log("Executing $command");
2401
                system($command);
2402
            }
2403
        }
2404
2405
        $sql = "SELECT * FROM group_rel_user";
2406
        $result = $connection->executeQuery($sql);
2407
        $dataList = $result->fetchAll();
2408
2409
        if (!empty($dataList)) {
2410
            foreach ($dataList as $data) {
2411
                if (isset($oldGroups[$data['group_id']])) {
2412
                    $data['group_id'] = $oldGroups[$data['group_id']];
2413
                    $userId = $data['user_id'];
2414
2415
                    $sql = "SELECT id FROM user WHERE user_id = $userId";
2416
                    $userResult = $connection->executeQuery($sql);
2417
                    $userInfo = $userResult->fetch();
2418
                    if (empty($userInfo)) {
2419
                        continue;
2420
                    }
2421
2422
                    $sql = "INSERT INTO usergroup_rel_user (usergroup_id, user_id, relation_type)
2423
                            VALUES ('{$data['group_id']}', '{$userId}', '{$data['relation_type']}')";
2424
                    $connection->executeQuery($sql);
2425
                }
2426
            }
2427
        }
2428
2429
        $sql = "SELECT * FROM group_rel_group";
2430
        $result = $connection->executeQuery($sql);
2431
        $dataList = $result->fetchAll();
2432
2433
        if (!empty($dataList)) {
2434
            foreach ($dataList as $data) {
2435
                if (isset($oldGroups[$data['group_id']]) && isset($oldGroups[$data['subgroup_id']])) {
2436
                    $data['group_id'] = $oldGroups[$data['group_id']];
2437
                    $data['subgroup_id'] = $oldGroups[$data['subgroup_id']];
2438
                    $sql = "INSERT INTO usergroup_rel_usergroup (group_id, subgroup_id, relation_type)
2439
                            VALUES ('{$data['group_id']}', '{$data['subgroup_id']}', '{$data['relation_type']}')";
2440
                    $connection->executeQuery($sql);
2441
                }
2442
            }
2443
        }
2444
2445
        $sql = "SELECT * FROM announcement_rel_group";
2446
        $result = $connection->executeQuery($sql);
2447
        $dataList = $result->fetchAll();
2448
2449
        if (!empty($dataList)) {
2450
            foreach ($dataList as $data) {
2451
                if (isset($oldGroups[$data['group_id']])) {
2452
                    // Deleting relation
2453
                    $sql = "DELETE FROM announcement_rel_group WHERE group_id = {$data['group_id']}";
2454
                    $connection->executeQuery($sql);
2455
2456
                    // Add new relation
2457
                    $data['group_id'] = $oldGroups[$data['group_id']];
2458
                    $sql = "INSERT INTO announcement_rel_group(group_id, announcement_id)
2459
                            VALUES ('{$data['group_id']}', '{$data['announcement_id']}')";
2460
                    $connection->executeQuery($sql);
2461
                }
2462
            }
2463
        }
2464
2465
        $sql = "SELECT * FROM group_rel_tag";
2466
        $result = $connection->executeQuery($sql);
2467
        $dataList = $result->fetchAll();
2468
        if (!empty($dataList)) {
2469
            foreach ($dataList as $data) {
2470
                if (isset($oldGroups[$data['group_id']])) {
2471
                    $data['group_id'] = $oldGroups[$data['group_id']];
2472
                    $sql = "INSERT INTO usergroup_rel_tag (tag_id, usergroup_id)
2473
                            VALUES ('{$data['tag_id']}', '{$data['group_id']}')";
2474
                    $connection->executeQuery($sql);
2475
                }
2476
            }
2477
        }
2478
    }
2479
2480
    if ($debug) {
2481
        error_log('update extra fields');
2482
    }
2483
2484
    // Extra fields
2485
    $extraFieldTables = [
2486
        ExtraField::USER_FIELD_TYPE => Database::get_main_table(TABLE_MAIN_USER_FIELD),
2487
        ExtraField::COURSE_FIELD_TYPE => Database::get_main_table(TABLE_MAIN_COURSE_FIELD),
2488
        //ExtraField::LP_FIELD_TYPE => Database::get_main_table(TABLE_MAIN_LP_FIELD),
2489
        ExtraField::SESSION_FIELD_TYPE => Database::get_main_table(TABLE_MAIN_SESSION_FIELD),
2490
        //ExtraField::CALENDAR_FIELD_TYPE => Database::get_main_table(TABLE_MAIN_CALENDAR_EVENT_FIELD),
2491
        //ExtraField::QUESTION_FIELD_TYPE => Database::get_main_table(TABLE_MAIN_CALENDAR_EVENT_FIELD),
2492
        //ExtraField::USER_FIELD_TYPE => //Database::get_main_table(TABLE_MAIN_SPECIFIC_FIELD),
2493
    ];
2494
2495
    foreach ($extraFieldTables as $type => $table) {
2496
        $sql = "SELECT * FROM $table ";
2497
        if ($debug) {
2498
            error_log($sql);
2499
        }
2500
        $result = $connection->query($sql);
2501
        $fields = $result->fetchAll();
2502
2503
        foreach ($fields as $field) {
2504
            if ($debug) {
2505
                error_log("Loading field: ".$field['field_variable']);
2506
            }
2507
            $originalId = $field['id'];
2508
2509
            $params = [
2510
                'extra_field_type' => $type,
2511
                'variable' => $field['field_variable'],
2512
                'field_type' => $field['field_type'],
2513
                'display_text' => $field['field_display_text'],
2514
                'default_value' => $field['field_default_value'],
2515
                'field_order' => $field['field_order'],
2516
                'visible' => $field['field_visible'],
2517
                'changeable' => $field['field_changeable'],
2518
                'filter' => $field['field_filter'],
2519
            ];
2520
2521
            $connection->insert('extra_field', $params);
2522
            $newExtraFieldId = $connection->lastInsertId();
2523
2524
            $values = [];
2525
            $handlerId = null;
2526
            switch ($type) {
2527
                case ExtraField::USER_FIELD_TYPE:
2528
                    $optionTable = Database::get_main_table(
2529
                        TABLE_MAIN_USER_FIELD_OPTIONS
2530
                    );
2531
                    $valueTable = Database::get_main_table(
2532
                        TABLE_MAIN_USER_FIELD_VALUES
2533
                    );
2534
                    $handlerId = 'user_id';
2535
                    break;
2536
                case ExtraField::COURSE_FIELD_TYPE:
2537
                    $optionTable = Database::get_main_table(
2538
                        TABLE_MAIN_COURSE_FIELD_OPTIONS
2539
                    );
2540
                    $valueTable = Database::get_main_table(
2541
                        TABLE_MAIN_COURSE_FIELD_VALUES
2542
                    );
2543
                    $handlerId = 'c_id';
2544
                    break;
2545
                case ExtraField::SESSION_FIELD_TYPE:
2546
                    $optionTable = Database::get_main_table(
2547
                        TABLE_MAIN_SESSION_FIELD_OPTIONS
2548
                    );
2549
                    $valueTable = Database::get_main_table(
2550
                        TABLE_MAIN_SESSION_FIELD_VALUES
2551
                    );
2552
                    $handlerId = 'session_id';
2553
                    break;
2554
            }
2555
2556
            if (!empty($optionTable)) {
2557
                $sql = "SELECT * FROM $optionTable WHERE field_id = $originalId ";
2558
                $result = $connection->query($sql);
2559
                $options = $result->fetchAll();
2560
2561
                foreach ($options as $option) {
2562
                    $params = [
2563
                        'display_text' => $option['option_display_text'],
2564
                        'field_id' => $newExtraFieldId,
2565
                        'option_order' => $option['option_order'],
2566
                        'option_value' => $option['option_value'],
2567
                    ];
2568
                    $connection->insert('extra_field_options', $params);
2569
                }
2570
2571
                $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...
2572
                $result = $connection->query($sql);
2573
                $values = $result->fetchAll();
2574
                if ($debug) {
2575
                    error_log("Fetch all values for field");
2576
                }
2577
            }
2578
2579
            if (!empty($values)) {
2580
                if ($debug) {
2581
                    error_log("Saving field value in new table");
2582
                }
2583
                $k = 0;
2584
                foreach ($values as $value) {
2585
                    if (isset($value[$handlerId])) {
2586
                        // Insert without the use of the entity as it reduces
2587
                        // speed to 2 records per second (much too slow)
2588
                        $params = [
2589
                            'field_id' => $newExtraFieldId,
2590
                            'value' => $value['field_value'],
2591
                            'item_id' => $value[$handlerId],
2592
                        ];
2593
                        $connection->insert('extra_field_values', $params);
2594
                        if ($debug && (0 == $k % 10000)) {
2595
                            error_log("Saving field $k");
2596
                        }
2597
                        $k++;
2598
                    }
2599
                }
2600
            }
2601
        }
2602
    }
2603
2604
    if ($debug) {
0 ignored issues
show
introduced by
$debug is of type mixed, thus it always evaluated to true.
Loading history...
2605
        error_log('Remove index');
2606
    }
2607
2608
    // Drop temporary indexes added to increase speed of this function's queries
2609
    $sql = "ALTER TABLE c_document DROP INDEX tmpidx_doc";
2610
    $connection->executeQuery($sql);
2611
    $sql = "ALTER TABLE c_student_publication DROP INDEX tmpidx_stud";
2612
    $connection->executeQuery($sql);
2613
    $sql = "ALTER TABLE c_quiz DROP INDEX tmpidx_quiz";
2614
    $connection->executeQuery($sql);
2615
    $sql = "ALTER TABLE c_item_property DROP INDEX tmpidx_ip";
2616
    $connection->executeQuery($sql);
2617
2618
    if ($debug) {
2619
        error_log('Finish fixId function');
2620
    }
2621
2622
    fixLpId($connection, true);
2623
}
2624
2625
/**
2626
 * @param \Doctrine\DBAL\Connection $connection
2627
 * @param $debug
2628
 *
2629
 * @throws \Doctrine\DBAL\DBALException
2630
 */
2631
function fixLpId($connection, $debug)
2632
{
2633
    if ($debug) {
2634
        error_log('Fix lp.id lp.iids');
2635
    }
2636
2637
    $sql = 'SELECT id, title, code FROM course';
2638
    $result = $connection->query($sql);
2639
    $courses = $result->fetchAll();
2640
2641
    $sql = 'SELECT id FROM session';
2642
    $result = $connection->query($sql);
2643
    $sessions = $result->fetchAll();
2644
2645
    $tblCLp = Database::get_course_table(TABLE_LP_MAIN);
2646
    $tblCLpItem = Database::get_course_table(TABLE_LP_ITEM);
2647
    $toolTable = Database::get_course_table(TABLE_TOOL_LIST);
2648
2649
    if (!empty($sessions)) {
2650
        $sessions = array_column($sessions, 'id');
2651
        $sessions[] = 0;
2652
    } else {
2653
        $sessions = [0];
2654
    }
2655
2656
    foreach ($courses as $course) {
2657
        $courseId = $course['id'];
2658
        $sql = "SELECT * FROM $tblCLp WHERE c_id = $courseId AND iid <> id ORDER by iid";
2659
        $result = $connection->query($sql);
2660
        if ($debug) {
2661
            error_log('-------------');
2662
            error_log("Entering Lps in course #$courseId");
2663
            error_log($sql);
2664
        }
2665
        $lpList = $result->fetchAll();
2666
        $myOnlyLpList = [];
2667
        if (!empty($lpList)) {
2668
            foreach ($lpList as $lpInfo) {
2669
                $oldId = $lpInfo['id'];
2670
                $sql = "SELECT * FROM $tblCLpItem WHERE c_id = $courseId AND lp_id = $oldId ORDER by iid";
2671
                $result = $connection->query($sql);
2672
                $items = $result->fetchAll();
2673
                $lpInfo['lp_list'] = $items;
2674
                $myOnlyLpList[] = $lpInfo;
2675
            }
2676
        }
2677
2678
        if (!empty($myOnlyLpList)) {
2679
            foreach ($myOnlyLpList as $lpInfo) {
2680
                $lpIid = $lpInfo['iid'];
2681
                $oldId = $lpInfo['id'];
2682
                $items = $lpInfo['lp_list'];
2683
2684
                if (empty($items)) {
2685
                    continue;
2686
                }
2687
                $itemList = [];
2688
                foreach ($items as $subItem) {
2689
                    $itemList[$subItem['id']] = $subItem['iid'];
2690
                }
2691
                $variablesToFix = [
2692
                    'parent_item_id',
2693
                    'next_item_id',
2694
                    'prerequisite',
2695
                    'previous_item_id',
2696
                ];
2697
2698
                foreach ($sessions as $sessionId) {
2699
                    $correctLink = "lp/lp_controller.php?action=view&lp_id=$lpIid&id_session=$sessionId";
2700
                    $link = "newscorm/lp_controller.php?action=view&lp_id=$oldId&id_session=$sessionId";
2701
                    $secondLink = "lp/lp_controller.php?action=view&lp_id=$oldId&id_session=$sessionId";
2702
                    $sql = "UPDATE $toolTable
2703
                        SET link = '$correctLink'
2704
                        WHERE c_id = $courseId AND (link = '$link' OR link ='$secondLink')";
2705
                    $connection->query($sql);
2706
                }
2707
2708
                foreach ($items as $item) {
2709
                    $itemIid = $item['iid'];
2710
                    $itemId = $item['id'];
2711
                    foreach ($variablesToFix as $variable) {
2712
                        if (!empty($item[$variable]) && isset($itemList[$item[$variable]])) {
2713
                            $newId = $itemList[$item[$variable]];
2714
                            $sql = "UPDATE $tblCLpItem SET $variable = $newId
2715
                                    WHERE iid = $itemIid AND c_id = $courseId AND lp_id = $oldId";
2716
                            $connection->query($sql);
2717
                        }
2718
                    }
2719
2720
                    if ('document' == $item['item_type'] && !empty($item['path'])) {
2721
                        $oldDocumentId = $item['path'];
2722
                        $sql = "SELECT * FROM c_document WHERE c_id = $courseId AND id = $oldDocumentId";
2723
                        $result = $connection->query($sql);
2724
                        $document = $result->fetch();
2725
                        if (!empty($document)) {
2726
                            $newDocumentId = $document['iid'];
2727
                            if (!empty($newDocumentId)) {
2728
                                $sql = "UPDATE $tblCLpItem SET path = $newDocumentId
2729
                                    WHERE iid = $itemIid AND c_id = $courseId";
2730
                                $connection->query($sql);
2731
                                if ($debug) {
2732
                                    //error_log("Fix document: ");
2733
                                    //error_log($sql);
2734
                                }
2735
                            }
2736
                        }
2737
                    }
2738
2739
                    // c_lp_view
2740
                    $sql = "UPDATE c_lp_view SET last_item = $itemIid
2741
                            WHERE c_id = $courseId AND last_item = $itemId AND lp_id = $oldId";
2742
                    $connection->query($sql);
2743
2744
                    // c_lp_item_view
2745
                    $sql = "UPDATE c_lp_item_view SET lp_item_id = $itemIid
2746
                            WHERE c_id = $courseId AND lp_item_id = $itemId";
2747
                    $connection->query($sql);
2748
2749
                    // Update track_exercises
2750
                    $sql = "UPDATE track_e_exercises SET orig_lp_item_id = $itemIid
2751
                            WHERE c_id = $courseId AND orig_lp_id = $oldId AND orig_lp_item_id = $itemId";
2752
                    $connection->query($sql);
2753
2754
                    // c_forum_thread
2755
                    $sql = "UPDATE c_forum_thread SET lp_item_id = $itemIid
2756
                            WHERE c_id = $courseId AND lp_item_id = $itemId";
2757
                    $connection->query($sql);
2758
2759
                    // orig_lp_item_view_id
2760
                    $sql = "SELECT * FROM c_lp_view
2761
                            WHERE c_id = $courseId AND lp_id = $oldId";
2762
                    $result = $connection->query($sql);
2763
                    $itemViewList = $result->fetchAll();
2764
                    if ($itemViewList) {
2765
                        foreach ($itemViewList as $itemView) {
2766
                            $userId = $itemView['user_id'];
2767
                            $oldItemViewId = $itemView['id'];
2768
                            $newItemView = $itemView['iid'];
2769
2770
                            if (empty($oldItemViewId)) {
2771
                                continue;
2772
                            }
2773
2774
                            $sql = "UPDATE track_e_exercises
2775
                                SET orig_lp_item_view_id = $newItemView
2776
                                WHERE
2777
                                  c_id = $courseId AND
2778
                                  orig_lp_id = $oldId AND
2779
                                  orig_lp_item_id = $itemIid AND
2780
                                  orig_lp_item_view_id = $oldItemViewId AND
2781
                                  exe_user_id = $userId
2782
                                  ";
2783
                            $connection->query($sql);
2784
2785
                            /*$sql = "UPDATE c_lp_item_view
2786
                                    SET lp_view_id = $newItemView
2787
                                    WHERE
2788
                                      lp_view_id = $oldItemViewId AND
2789
                                      c_id = $courseId
2790
                                  ";
2791
                            $connection->query($sql);*/
2792
                        }
2793
                    }
2794
2795
                    $sql = "UPDATE $tblCLpItem SET lp_id = $lpIid
2796
                            WHERE c_id = $courseId AND lp_id = $oldId AND id = $itemId";
2797
                    $connection->query($sql);
2798
2799
                    $sql = "UPDATE $tblCLpItem SET id = iid
2800
                            WHERE c_id = $courseId AND lp_id = $oldId AND id = $itemId";
2801
                    $connection->query($sql);
2802
                }
2803
2804
                $sql = "UPDATE c_lp_view SET lp_id = $lpIid WHERE c_id = $courseId AND lp_id = $oldId";
2805
                $connection->query($sql);
2806
2807
                $sql = "UPDATE c_forum_forum SET lp_id = $lpIid WHERE c_id = $courseId AND lp_id = $oldId";
2808
                $connection->query($sql);
2809
2810
                // Update track_exercises.
2811
                $sql = "UPDATE track_e_exercises SET orig_lp_id = $lpIid
2812
                        WHERE c_id = $courseId AND orig_lp_id = $oldId";
2813
                $connection->query($sql);
2814
2815
                $sql = "UPDATE $tblCLp SET id = iid WHERE c_id = $courseId AND id = $oldId ";
2816
                $connection->query($sql);
2817
            }
2818
        }
2819
    }
2820
2821
    if ($debug) {
2822
        error_log('END Fix lp.id lp.iids');
2823
    }
2824
}
2825
2826
/**
2827
 * @param string $distFile
2828
 * @param string $envFile
2829
 * @param array  $params
2830
 */
2831
function updateEnvFile($distFile, $envFile, $params)
2832
{
2833
    $requirements = [
2834
        'DATABASE_HOST',
2835
        'DATABASE_PORT',
2836
        'DATABASE_NAME',
2837
        'DATABASE_USER',
2838
        'DATABASE_PASSWORD',
2839
        'APP_INSTALLED',
2840
        'APP_ENCRYPT_METHOD',
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
    error_log("File env saved here: $envFile");
2853
}
2854
2855
/**
2856
 * @param SymfonyContainer $container
2857
 * @param EntityManager    $manager
2858
 */
2859
function installGroups($container, $manager)
2860
{
2861
    // Creating fos_group (groups and roles)
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
    $repo = $manager->getRepository('ChamiloCoreBundle:Group');
2905
    foreach ($groups as $groupData) {
2906
        $criteria = ['code' => $groupData['code']];
2907
        $groupExists = $repo->findOneBy($criteria);
2908
        if (!$groupExists) {
2909
            $group = new Group($groupData['title']);
2910
            $group
2911
                ->setCode($groupData['code']);
2912
2913
            foreach ($groupData['roles'] as $role) {
2914
                $group->addRole($role);
2915
            }
2916
            $manager->persist($group);
2917
        }
2918
    }
2919
    $manager->flush();
2920
}
2921
2922
/**
2923
 * @param SymfonyContainer $container
2924
 */
2925
function installPages($container)
2926
{
2927
    error_log('installPages');
2928
2929
    //$siteManager = Container::getSiteManager();
2930
2931
    // Create site
2932
    /** @var Chamilo\PageBundle\Entity\Site $site */
2933
    /*$site = $siteManager->create();
2934
    $site->setHost('localhost');
2935
    $site->setEnabled(true);
2936
    $site->setName('localhost');
2937
    $site->setEnabledFrom(new \DateTime('now'));
2938
    $site->setEnabledTo(new \DateTime('+20 years'));
2939
    $site->setRelativePath('');
2940
    $site->setIsDefault(true);
2941
    $site->setLocale('en');
2942
    $siteManager->save($site);*/
2943
2944
    // Create home page
2945
    /** @var PageManager $pageManager */
2946
    /*$pageManager = $container->get('sonata.page.manager.page');
2947
    $page = $pageManager->create();
2948
    $page->setSlug('homepage');
2949
    $page->setUrl('/');
2950
    $page->setName('homepage');
2951
    $page->setTitle('home');
2952
    $page->setEnabled(true);
2953
    $page->setDecorate(1);
2954
    $page->setRequestMethod('GET|POST|HEAD|DELETE|PUT');
2955
    $page->setTemplateCode('default');
2956
    $page->setRouteName('homepage');
2957
    //$page->setParent($this->getReference('page-homepage'));
2958
    $page->setSite($site);
2959
    $pageManager->save($page);
2960
2961
    // Create welcome page
2962
    $pageWelcome = $pageManager->create();
2963
    $pageWelcome->setSlug('welcome');
2964
    $pageWelcome->setUrl('/welcome');
2965
    $pageWelcome->setName('welcome');
2966
    $pageWelcome->setTitle('welcome');
2967
    $pageWelcome->setEnabled(true);
2968
    $pageWelcome->setDecorate(1);
2969
    $pageWelcome->setRequestMethod('GET');
2970
    $pageWelcome->setTemplateCode('default');
2971
    $pageWelcome->setRouteName('welcome');
2972
    $pageWelcome->setParent($page);
2973
    $pageWelcome->setSite($site);
2974
    $pageManager->save($pageWelcome);
2975
2976
    // Creating page blocks
2977
    $templateManager = $container->get('sonata.page.template_manager');
2978
    $template = $templateManager->get('default');
2979
    $templateContainers = $template->getContainers();
2980
2981
    $containers = [];
2982
    foreach ($templateContainers as $id => $area) {
2983
        $containers[$id] = [
2984
            'area' => $area,
2985
            'block' => false,
2986
        ];
2987
    }
2988
2989
    // Create blocks for this page
2990
    $blockInteractor = $container->get('sonata.page.block_interactor');
2991
    $parentBlock = null;
2992
    foreach ($containers as $id => $area) {
2993
        if (false === $area['block'] && false === $templateContainers[$id]['shared']) {
2994
            $block = $blockInteractor->createNewContainer(
2995
                [
2996
                    'page' => $pageWelcome,
2997
                    'name' => $templateContainers[$id]['name'],
2998
                    'code' => $id,
2999
                ]
3000
            );
3001
3002
            if ('content' === $id && 'Main content' == $templateContainers[$id]['name']) {
3003
                $parentBlock = $block;
3004
            }
3005
        }
3006
    }*/
3007
3008
    // Create block in main content
3009
    //$block = $container->get('sonata.page.manager.block');
3010
    /** @var \Sonata\BlockBundle\Model\Block $myBlock */
3011
    /*$myBlock = $block->create();
3012
    $myBlock->setType('sonata.formatter.block.formatter');
3013
    $myBlock->setSetting('format', 'richhtml');
3014
    $myBlock->setSetting('content', '');
3015
    $myBlock->setSetting('rawContent', '');
3016
    $myBlock->setSetting('template', '@SonataFormatter/Block/block_formatter.html.twig');
3017
    $myBlock->setParent($parentBlock);
3018
    $pageWelcome->addBlocks($myBlock);
3019
    $pageManager->save($pageWelcome);*/
3020
}
3021
3022
/**
3023
 * @param SymfonyContainer $container
3024
 * @param EntityManager    $manager
3025
 * @param bool             $upgrade
3026
 */
3027
function installSchemas($container, $manager, $upgrade = false)
3028
{
3029
    $settingsManager = $container->get('chamilo.settings.manager');
3030
3031
    // Install course tools (table "tool")
3032
    $toolChain = $container->get(ToolChain::class);
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
            ->setCreatedBy(1)
3047
        ;
3048
        $em->persist($accessUrl);
3049
        $em->flush();
3050
    }
3051
3052
    if ($upgrade) {
3053
        $settingsManager->updateSchemas($accessUrl);
3054
    } else {
3055
        // Installing schemas (filling settings_current table)
3056
        $settingsManager->installSchemas($accessUrl);
3057
    }
3058
}
3059
3060
/**
3061
 * @param SymfonyContainer $container
3062
 */
3063
function upgradeWithContainer($container)
3064
{
3065
    Container::setContainer($container);
3066
    Container::setLegacyServices($container, false);
3067
    error_log('setLegacyServices');
3068
    $manager = Database::getManager();
3069
    installGroups($container, $manager);
3070
    error_log('installGroups');
3071
    // @todo check if adminId = 1
3072
    installSchemas($container, $manager, 1, true);
3073
    installPages($container);
3074
    fixMedia($container);
3075
}
3076
3077
function fixMedia($container)
3078
{
3079
    return;
3080
    error_log('fix medias');
0 ignored issues
show
Unused Code introduced by
error_log('fix medias') is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
3081
    $pool = $container->get('sonata.media.pool');
3082
    $contextManager = $container->get('sonata.classification.manager.context');
3083
    $categoryManager = $container->get('sonata.media.manager.category');
3084
3085
    foreach ($pool->getContexts() as $context => $contextAttrs) {
3086
        /** @var ContextInterface $defaultContext */
3087
        $defaultContext = $contextManager->findOneBy([
3088
            'id' => $context,
3089
        ]);
3090
3091
        if (!$defaultContext) {
3092
            $defaultContext = $contextManager->create();
3093
            $defaultContext->setId($context);
3094
            $defaultContext->setName(ucfirst($context));
3095
            $defaultContext->setEnabled(true);
3096
3097
            $contextManager->save($defaultContext);
3098
        }
3099
3100
        $defaultCategory = $categoryManager->getRootCategory($defaultContext);
3101
3102
        if (!$defaultCategory) {
3103
            $defaultCategory = $categoryManager->create();
3104
            $defaultCategory->setContext($defaultContext);
3105
            $defaultCategory->setName(ucfirst($context));
3106
            $defaultCategory->setEnabled(true);
3107
            $defaultCategory->setPosition(0);
3108
3109
            $categoryManager->save($defaultCategory);
3110
        }
3111
    }
3112
}
3113
3114
/**
3115
 * After the schema was created (table creation), the function adds
3116
 * admin/platform information.
3117
 *
3118
 * @param \Psr\Container\ContainerInterface $container
3119
 * @param string                            $sysPath
3120
 * @param string                            $encryptPassForm
3121
 * @param string                            $passForm
3122
 * @param string                            $adminLastName
3123
 * @param string                            $adminFirstName
3124
 * @param string                            $loginForm
3125
 * @param string                            $emailForm
3126
 * @param string                            $adminPhoneForm
3127
 * @param string                            $languageForm
3128
 * @param string                            $institutionForm
3129
 * @param string                            $institutionUrlForm
3130
 * @param string                            $siteName
3131
 * @param string                            $allowSelfReg
3132
 * @param string                            $allowSelfRegProf
3133
 * @param string                            $installationProfile Installation profile, if any was provided
3134
 */
3135
function finishInstallationWithContainer(
3136
    $container,
3137
    $sysPath,
3138
    $encryptPassForm,
3139
    $passForm,
3140
    $adminLastName,
3141
    $adminFirstName,
3142
    $loginForm,
3143
    $emailForm,
3144
    $adminPhoneForm,
3145
    $languageForm,
3146
    $institutionForm,
3147
    $institutionUrlForm,
3148
    $siteName,
3149
    $allowSelfReg,
3150
    $allowSelfRegProf,
3151
    $installationProfile = ''
3152
) {
3153
    error_log('finishInstallationWithContainer');
3154
    $sysPath = !empty($sysPath) ? $sysPath : api_get_path(SYMFONY_SYS_PATH);
3155
    Container::setContainer($container);
3156
    Container::setLegacyServices($container, false);
3157
    error_log('setLegacyServices');
3158
3159
    $manager = Database::getManager();
3160
    $connection = $manager->getConnection();
3161
    $trans = $container->get('translator');
3162
    $sql = getVersionTable();
3163
3164
    // Add version table
3165
    $connection->executeQuery($sql);
3166
3167
    error_log("Create $sql ");
3168
3169
    // Add tickets defaults
3170
    $ticketProject = new TicketProject();
3171
    $ticketProject
3172
        ->setId(1)
3173
        ->setName('Ticket System')
3174
        ->setInsertUserId(1);
3175
3176
    $manager->persist($ticketProject);
3177
3178
    $categories = [
3179
        $trans->trans('Enrollment') => $trans->trans('Tickets about enrollment'),
3180
        $trans->trans('General information') => $trans->trans('Tickets about general information'),
3181
        $trans->trans('Requests and paperwork') => $trans->trans('Tickets about requests and paperwork'),
3182
        $trans->trans('Academic Incidents') => $trans->trans('Tickets about academic incidents, like exams, practices, tasks, etc.'),
3183
        $trans->trans('Virtual campus') => $trans->trans('Tickets about virtual campus'),
3184
        $trans->trans('Online evaluation') => $trans->trans('Tickets about online evaluation'),
3185
    ];
3186
3187
    $i = 1;
3188
    foreach ($categories as $category => $description) {
3189
        // Online evaluation requires a course
3190
        $ticketCategory = new TicketCategory();
3191
        $ticketCategory
3192
            ->setId($i)
3193
            ->setName($category)
3194
            ->setDescription($description)
3195
            ->setProject($ticketProject)
3196
            ->setInsertUserId(1);
3197
3198
        $isRequired = 6 == $i;
3199
        $ticketCategory->setCourseRequired($isRequired);
3200
3201
        $manager->persist($ticketCategory);
3202
        $i++;
3203
    }
3204
3205
    // Default Priorities
3206
    $defaultPriorities = [
3207
        TicketManager::PRIORITY_NORMAL => $trans->trans('Normal'),
3208
        TicketManager::PRIORITY_HIGH => $trans->trans('High'),
3209
        TicketManager::PRIORITY_LOW => $trans->trans('Low'),
3210
    ];
3211
3212
    $i = 1;
3213
    foreach ($defaultPriorities as $code => $priority) {
3214
        $ticketPriority = new TicketPriority();
3215
        $ticketPriority
3216
            ->setId($i)
3217
            ->setName($priority)
3218
            ->setCode($code)
3219
            ->setInsertUserId(1);
3220
3221
        $manager->persist($ticketPriority);
3222
        $i++;
3223
    }
3224
    error_log("Save ticket data");
3225
    $manager->flush();
3226
3227
    $table = Database::get_main_table(TABLE_TICKET_STATUS);
3228
3229
    // Default status
3230
    $defaultStatus = [
3231
        TicketManager::STATUS_NEW => $trans->trans('New'),
3232
        TicketManager::STATUS_PENDING => $trans->trans('Pending'),
3233
        TicketManager::STATUS_UNCONFIRMED => $trans->trans('Unconfirmed'),
3234
        TicketManager::STATUS_CLOSE => $trans->trans('Close'),
3235
        TicketManager::STATUS_FORWARDED => $trans->trans('Forwarded'),
3236
    ];
3237
3238
    $i = 1;
3239
    foreach ($defaultStatus as $code => $status) {
3240
        $attributes = [
3241
            'id' => $i,
3242
            'code' => $code,
3243
            'name' => $status,
3244
        ];
3245
        Database::insert($table, $attributes);
3246
        $i++;
3247
    }
3248
3249
    installGroups($container, $manager);
3250
3251
3252
    error_log('Inserting data.sql');
3253
    // Inserting default data
3254
    $data = file_get_contents($sysPath.'public/main/install/data.sql');
3255
    $result = $manager->getConnection()->prepare($data);
3256
    $result->execute();
3257
    $result->closeCursor();
3258
3259
    UserManager::setPasswordEncryption($encryptPassForm);
3260
3261
    error_log('user creation - admin');
3262
3263
    // Create admin user.
3264
    $adminId = UserManager::create_user(
3265
        $adminFirstName,
3266
        $adminLastName,
3267
        1,
3268
        $emailForm,
3269
        $loginForm,
3270
        $passForm,
3271
        'ADMIN',
3272
        $languageForm,
3273
        $adminPhoneForm,
3274
        '',
3275
        PLATFORM_AUTH_SOURCE,
3276
        '',
3277
        1,
3278
        0,
3279
        null,
3280
        '',
3281
        false,
3282
        true,
3283
        '',
3284
        false,
3285
        '',
3286
        0,
3287
        [],
3288
        '',
3289
        false,
3290
        false
3291
    );
3292
    error_log('user creation - anon');
3293
    // Create anonymous user.
3294
    $anonId = UserManager::create_user(
3295
        'Joe',
3296
        'Anonymous',
3297
        6,
3298
        'anonymous@localhost',
3299
        'anon',
3300
        'anon',
3301
        'anonymous',
3302
        $languageForm,
3303
        '',
3304
        '',
3305
        PLATFORM_AUTH_SOURCE,
3306
        '',
3307
        1,
3308
        0,
3309
        null,
3310
        '',
3311
        false,
3312
        false,
3313
        '',
3314
        false,
3315
        '',
3316
        $adminId,
3317
        [],
3318
        '',
3319
        false,
3320
        false
3321
    );
3322
    $userManager = $container->get('Chamilo\CoreBundle\Repository\UserRepository');
3323
    $urlRepo = $container->get('Chamilo\CoreBundle\Repository\AccessUrlRepository');
3324
3325
    $admin = $userManager->find($adminId);
3326
3327
    // Login as admin
3328
    $token = new UsernamePasswordToken(
3329
        $admin,
3330
        $admin->getPassword(),
3331
        "public",
3332
        $admin->getRoles()
3333
    );
3334
    $container->get('security.token_storage')->setToken($token);
3335
3336
    /*$event = new \Symfony\Component\Security\Http\Event\InteractiveLoginEvent(
3337
        $container->get('request_stack'), $token
3338
    );
3339
    $container->get('event_dispatcher')->dispatch('security.interactive_login', $event);*/
3340
3341
3342
    installSchemas($container, $manager, false);
3343
3344
    error_log('Adding access url as a node');
3345
3346
    $accessUrl = $urlRepo->find(1);
3347
3348
    $branch = new BranchSync();
3349
    $branch->setBranchName('localhost');
3350
    $branch->setUrl($accessUrl);
3351
    $manager->persist($branch);
3352
3353
3354
    $userManager->addUserToResourceNode($adminId, $adminId);
3355
    $userManager->addUserToResourceNode($anonId, $adminId);
3356
3357
    $urlRepo->addResourceNode($accessUrl, $admin);
3358
3359
    UrlManager::add_user_to_url($adminId, 1);
3360
    UrlManager::add_user_to_url($anonId, 1);
3361
3362
    $manager->flush();
3363
3364
    // Set default language
3365
    Database::update(
3366
        Database::get_main_table(TABLE_MAIN_LANGUAGE),
3367
        ['available' => 1],
3368
        ['dokeos_folder = ?' => $languageForm]
3369
    );
3370
3371
    // Install settings
3372
    installSettings(
3373
        $institutionForm,
3374
        $institutionUrlForm,
3375
        $siteName,
3376
        $emailForm,
3377
        $adminLastName,
3378
        $adminFirstName,
3379
        $languageForm,
3380
        $allowSelfReg,
3381
        $allowSelfRegProf,
3382
        $installationProfile
3383
    );
3384
3385
    lockSettings();
3386
    updateDirAndFilesPermissions();
3387
    //fixMedia($container);
3388
}
3389
3390
/**
3391
 * Creates 'version' table.
3392
 */
3393
function createVersionTable()
3394
{
3395
    $sql = getVersionTable();
3396
    Database::query($sql);
3397
}
3398
3399
/**
3400
 * Get version creation table query.
3401
 *
3402
 * @return string
3403
 */
3404
function getVersionTable()
3405
{
3406
    return 'CREATE TABLE IF NOT EXISTS version (id int unsigned NOT NULL AUTO_INCREMENT, version varchar(20), PRIMARY KEY(id), UNIQUE(version));';
3407
}
3408
3409
/**
3410
 * Update settings based on installation profile defined in a JSON file.
3411
 *
3412
 * @param string $installationProfile The name of the JSON file in main/install/profiles/ folder
3413
 *
3414
 * @return bool false on failure (no bad consequences anyway, just ignoring profile)
3415
 */
3416
function installProfileSettings($installationProfile = '')
3417
{
3418
    if (empty($installationProfile)) {
3419
        return false;
3420
    }
3421
    $jsonPath = api_get_path(SYS_PATH).'main/install/profiles/'.$installationProfile.'.json';
3422
    // Make sure the path to the profile is not hacked
3423
    if (!Security::check_abs_path($jsonPath, api_get_path(SYS_PATH).'main/install/profiles/')) {
3424
        return false;
3425
    }
3426
    if (!is_file($jsonPath)) {
3427
        return false;
3428
    }
3429
    if (!is_readable($jsonPath)) {
3430
        return false;
3431
    }
3432
    if (!function_exists('json_decode')) {
3433
        // The php-json extension is not available. Ignore profile.
3434
        return false;
3435
    }
3436
    $json = file_get_contents($jsonPath);
3437
    $params = json_decode($json);
3438
    if (false === $params or null === $params) {
3439
        return false;
3440
    }
3441
    $settings = $params->params;
3442
    if (!empty($params->parent)) {
3443
        installProfileSettings($params->parent);
3444
    }
3445
3446
    $tblSettings = Database::get_main_table(TABLE_MAIN_SETTINGS_CURRENT);
3447
3448
    foreach ($settings as $id => $param) {
3449
        $conditions = ['variable = ? ' => $param->variable];
3450
3451
        if (!empty($param->subkey)) {
3452
            $conditions['AND subkey = ? '] = $param->subkey;
3453
        }
3454
3455
        Database::update(
3456
            $tblSettings,
3457
            ['selected_value' => $param->selected_value],
3458
            $conditions
3459
        );
3460
    }
3461
3462
    return true;
3463
}
3464
3465
/**
3466
 * Quick function to remove a directory with its subdirectories.
3467
 *
3468
 * @param $dir
3469
 */
3470
function rrmdir($dir)
3471
{
3472
    if (is_dir($dir)) {
3473
        $objects = scandir($dir);
3474
        foreach ($objects as $object) {
3475
            if ("." != $object && ".." != $object) {
3476
                if ("dir" == filetype($dir."/".$object)) {
3477
                    @rrmdir($dir."/".$object);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for rrmdir(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

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

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

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

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

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

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

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

}

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

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

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

Loading history...
3478
                } else {
3479
                    @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

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