Passed
Push — 1.11.x ( bce6cd...c146d9 )
by Angel Fernando Quiroz
12:25
created

main/install/install.lib.php (1 issue)

1
<?php
2
/* For licensing terms, see /license.txt */
3
4
use Chamilo\CoreBundle\Entity\ExtraField;
5
use Chamilo\TicketBundle\Entity\Category as TicketCategory;
6
use Chamilo\TicketBundle\Entity\Priority as TicketPriority;
7
use Chamilo\TicketBundle\Entity\Project as TicketProject;
8
use Doctrine\ORM\EntityManager;
9
10
/**
11
 * Chamilo LMS
12
 * This file contains functions used by the install and upgrade scripts.
13
 *
14
 * Ideas for future additions:
15
 * - a function get_old_version_settings to retrieve the config file settings
16
 *   of older versions before upgrading.
17
 */
18
19
/* CONSTANTS */
20
define('SYSTEM_CONFIG_FILENAME', 'configuration.dist.php');
21
define('USERNAME_MAX_LENGTH', 50);
22
23
/**
24
 * This function detects whether the system has been already installed.
25
 * It should be used for prevention from second running the installation
26
 * script and as a result - destroying a production system.
27
 *
28
 * @return bool The detected result;
29
 *
30
 * @author Ivan Tcholakov, 2010;
31
 */
32
function isAlreadyInstalledSystem()
33
{
34
    global $new_version, $_configuration;
35
36
    if (empty($new_version)) {
37
        return true; // Must be initialized.
38
    }
39
40
    $current_config_file = api_get_path(CONFIGURATION_PATH).'configuration.php';
41
    if (!file_exists($current_config_file)) {
42
        return false; // Configuration file does not exist, install the system.
43
    }
44
    require $current_config_file;
45
46
    $current_version = null;
47
    if (isset($_configuration['system_version'])) {
48
        $current_version = trim($_configuration['system_version']);
49
    }
50
51
    // If the current version is old, upgrading is assumed, the installer goes ahead.
52
    return empty($current_version) ? false : version_compare($current_version, $new_version, '>=');
53
}
54
55
/**
56
 * This function checks if a php extension exists or not and returns an HTML status string.
57
 *
58
 * @param string $extensionName Name of the PHP extension to be checked
59
 * @param string $returnSuccess Text to show when extension is available (defaults to 'Yes')
60
 * @param string $returnFailure Text to show when extension is available (defaults to 'No')
61
 * @param bool   $optional      Whether this extension is optional (then show unavailable text in orange rather than red)
62
 * @param string $enabledTerm   If this string is not null, then use to check if the corresponding parameter is = 1.
63
 *                              If not, mention it's present but not enabled. For example, for opcache, this should be 'opcache.enable'
64
 *
65
 * @return string HTML string reporting the status of this extension. Language-aware.
66
 *
67
 * @author  Christophe Gesch??
68
 * @author  Patrick Cool <[email protected]>, Ghent University
69
 * @author  Yannick Warnier <[email protected]>
70
 */
71
function checkExtension(
72
    $extensionName,
73
    $returnSuccess = 'Yes',
74
    $returnFailure = 'No',
75
    $optional = false,
76
    $enabledTerm = ''
77
) {
78
    if (extension_loaded($extensionName)) {
79
        if (!empty($enabledTerm)) {
80
            $isEnabled = ini_get($enabledTerm);
81
            if ($isEnabled == '1') {
82
                return Display::label($returnSuccess, 'success');
83
            } else {
84
                if ($optional) {
85
                    return Display::label(get_lang('ExtensionInstalledButNotEnabled'), 'warning');
86
                } else {
87
                    return Display::label(get_lang('ExtensionInstalledButNotEnabled'), 'important');
88
                }
89
            }
90
        } else {
91
            return Display::label($returnSuccess, 'success');
92
        }
93
    } else {
94
        if ($optional) {
95
            return Display::label($returnFailure, 'warning');
96
        } else {
97
            return Display::label($returnFailure, 'important');
98
        }
99
    }
100
}
101
102
/**
103
 * This function checks whether a php setting matches the recommended value.
104
 *
105
 * @param string $phpSetting       A PHP setting to check
106
 * @param string $recommendedValue A recommended value to show on screen
107
 * @param mixed  $returnSuccess    What to show on success
108
 * @param mixed  $returnFailure    What to show on failure
109
 *
110
 * @return string A label to show
111
 *
112
 * @author Patrick Cool <[email protected]>, Ghent University
113
 */
114
function checkPhpSetting(
115
    $phpSetting,
116
    $recommendedValue,
117
    $returnSuccess = false,
118
    $returnFailure = false
119
) {
120
    $currentPhpValue = getPhpSetting($phpSetting);
121
    if ($currentPhpValue == $recommendedValue) {
122
        return Display::label($currentPhpValue.' '.$returnSuccess, 'success');
123
    } else {
124
        return Display::label($currentPhpValue.' '.$returnSuccess, 'important');
125
    }
126
}
127
128
/**
129
 * This function return the value of a php.ini setting if not "" or if exists,
130
 * otherwise return false.
131
 *
132
 * @param string $phpSetting The name of a PHP setting
133
 *
134
 * @return mixed The value of the setting, or false if not found
135
 */
136
function checkPhpSettingExists($phpSetting)
137
{
138
    if (ini_get($phpSetting) != "") {
139
        return ini_get($phpSetting);
140
    }
141
142
    return false;
143
}
144
145
/**
146
 * Check if the current url is the same root_web when the multiple_access_url is enabled.
147
 *
148
 * @return bool
149
 */
150
function checkAccessUrl()
151
{
152
    if (api_get_configuration_value('multiple_access_urls') !== true) {
153
        return true;
154
    }
155
156
    $currentWebPath = api_get_path(WEB_PATH);
157
    $rootWeb = api_get_configuration_value('root_web');
158
159
    return $currentWebPath === $rootWeb;
160
}
161
162
/**
163
 * Returns a textual value ('ON' or 'OFF') based on a requester 2-state ini- configuration setting.
164
 *
165
 * @param string $val a php ini value
166
 *
167
 * @return bool ON or OFF
168
 *
169
 * @author Joomla <http://www.joomla.org>
170
 */
171
function getPhpSetting($val)
172
{
173
    $value = ini_get($val);
174
    switch ($val) {
175
        case 'display_errors':
176
            global $originalDisplayErrors;
177
            $value = $originalDisplayErrors;
178
            break;
179
    }
180
181
    return $value == '1' ? 'ON' : 'OFF';
182
}
183
184
/**
185
 * This function returns a string "true" or "false" according to the passed parameter.
186
 *
187
 * @param int $var The variable to present as text
188
 *
189
 * @return string the string "true" or "false"
190
 *
191
 * @author Christophe Gesch??
192
 */
193
function trueFalse($var)
194
{
195
    return $var ? 'true' : 'false';
196
}
197
198
/**
199
 * Removes memory and time limits as much as possible.
200
 */
201
function remove_memory_and_time_limits()
202
{
203
    if (function_exists('ini_set')) {
204
        ini_set('memory_limit', -1);
205
        ini_set('max_execution_time', 0);
206
        error_log('Update-db script: memory_limit set to -1', 0);
207
        error_log('Update-db script: max_execution_time 0', 0);
208
    } else {
209
        error_log('Update-db script: could not change memory and time limits', 0);
210
    }
211
}
212
213
/**
214
 * Detects browser's language.
215
 *
216
 * @return string Returns a language identificator, i.e. 'english', 'spanish', ...
217
 *
218
 * @author Ivan Tcholakov, 2010
219
 */
220
function detect_browser_language()
221
{
222
    static $language_index = [
223
        'ar' => 'arabic',
224
        'ast' => 'asturian',
225
        'bg' => 'bulgarian',
226
        'bs' => 'bosnian',
227
        'ca' => 'catalan',
228
        'zh' => 'simpl_chinese',
229
        'zh-tw' => 'trad_chinese',
230
        'cs' => 'czech',
231
        'da' => 'danish',
232
        'prs' => 'dari',
233
        'de' => 'german',
234
        'el' => 'greek',
235
        'en' => 'english',
236
        'es' => 'spanish',
237
        'eo' => 'esperanto',
238
        'eu' => 'basque',
239
        'fa' => 'persian',
240
        'fr' => 'french',
241
        'fur' => 'friulian',
242
        'gl' => 'galician',
243
        'ka' => 'georgian',
244
        'hr' => 'croatian',
245
        'he' => 'hebrew',
246
        'hi' => 'hindi',
247
        'id' => 'indonesian',
248
        'it' => 'italian',
249
        'ko' => 'korean',
250
        'lv' => 'latvian',
251
        'lt' => 'lithuanian',
252
        'mk' => 'macedonian',
253
        'hu' => 'hungarian',
254
        'ms' => 'malay',
255
        'nl' => 'dutch',
256
        'ja' => 'japanese',
257
        'no' => 'norwegian',
258
        'oc' => 'occitan',
259
        'ps' => 'pashto',
260
        'pl' => 'polish',
261
        'pt' => 'portuguese',
262
        'pt-br' => 'brazilian',
263
        'ro' => 'romanian',
264
        'qu' => 'quechua_cusco',
265
        'ru' => 'russian',
266
        'sk' => 'slovak',
267
        'sl' => 'slovenian',
268
        'sr' => 'serbian',
269
        'fi' => 'finnish',
270
        'sv' => 'swedish',
271
        'th' => 'thai',
272
        'tr' => 'turkish',
273
        'uk' => 'ukrainian',
274
        'vi' => 'vietnamese',
275
        'sw' => 'swahili',
276
        'yo' => 'yoruba',
277
    ];
278
279
    $system_available_languages = &get_language_folder_list();
280
    $accept_languages = strtolower(str_replace('_', '-', $_SERVER['HTTP_ACCEPT_LANGUAGE']));
281
    foreach ($language_index as $code => $language) {
282
        if (strpos($accept_languages, $code) === 0) {
283
            if (!empty($system_available_languages[$language])) {
284
                return $language;
285
            }
286
        }
287
    }
288
289
    $user_agent = strtolower(str_replace('_', '-', $_SERVER['HTTP_USER_AGENT']));
290
    foreach ($language_index as $code => $language) {
291
        if (@preg_match("/[\[\( ]{$code}[;,_\-\)]/", $user_agent)) {
292
            if (!empty($system_available_languages[$language])) {
293
                return $language;
294
            }
295
        }
296
    }
297
298
    return 'english';
299
}
300
301
/*      FILESYSTEM RELATED FUNCTIONS */
302
303
/**
304
 * This function checks if the given folder is writable.
305
 *
306
 * @param string $folder     Full path to a folder
307
 * @param bool   $suggestion Whether to show a suggestion or not
308
 *
309
 * @return string
310
 */
311
function check_writable($folder, $suggestion = false)
312
{
313
    if (is_writable($folder)) {
314
        return Display::label(get_lang('Writable'), 'success');
315
    } else {
316
        if ($suggestion) {
317
            return Display::label(get_lang('NotWritable'), 'info');
318
        } else {
319
            return Display::label(get_lang('NotWritable'), 'important');
320
        }
321
    }
322
}
323
324
/**
325
 * This function checks if the given folder is readable.
326
 *
327
 * @param string $folder     Full path to a folder
328
 * @param bool   $suggestion Whether to show a suggestion or not
329
 *
330
 * @return string
331
 */
332
function checkReadable($folder, $suggestion = false)
333
{
334
    if (is_readable($folder)) {
335
        return Display::label(get_lang('Readable'), 'success');
336
    } else {
337
        if ($suggestion) {
338
            return Display::label(get_lang('NotReadable'), 'info');
339
        } else {
340
            return Display::label(get_lang('NotReadable'), 'important');
341
        }
342
    }
343
}
344
345
/**
346
 * This function is similar to the core file() function, except that it
347
 * works with line endings in Windows (which is not the case of file()).
348
 *
349
 * @param string $filename
350
 *
351
 * @return array The lines of the file returned as an array
352
 */
353
function file_to_array($filename)
354
{
355
    if (!is_readable($filename) || is_dir($filename)) {
356
        return [];
357
    }
358
    $fp = fopen($filename, 'rb');
359
    $buffer = fread($fp, filesize($filename));
360
    fclose($fp);
361
362
    return explode('<br />', nl2br($buffer));
363
}
364
365
/**
366
 * We assume this function is called from install scripts that reside inside the install folder.
367
 */
368
function set_file_folder_permissions()
369
{
370
    @chmod('.', 0755); //set permissions on install dir
371
    @chmod('..', 0755); //set permissions on parent dir of install dir
372
}
373
374
/**
375
 * Write the main system config file.
376
 *
377
 * @param string $path Path to the config file
378
 */
379
function write_system_config_file($path)
380
{
381
    global $dbHostForm;
382
    global $dbPortForm;
383
    global $dbUsernameForm;
384
    global $dbPassForm;
385
    global $dbNameForm;
386
    global $urlForm;
387
    global $pathForm;
388
    global $urlAppendPath;
389
    global $languageForm;
390
    global $encryptPassForm;
391
    global $session_lifetime;
392
    global $new_version;
393
    global $new_version_stable;
394
395
    $root_sys = api_add_trailing_slash(str_replace('\\', '/', realpath($pathForm)));
396
    $content = file_get_contents(__DIR__.'/'.SYSTEM_CONFIG_FILENAME);
397
398
    $config['{DATE_GENERATED}'] = date('r');
399
    $config['{DATABASE_HOST}'] = $dbHostForm;
400
    $config['{DATABASE_PORT}'] = $dbPortForm;
401
    $config['{DATABASE_USER}'] = $dbUsernameForm;
402
    $config['{DATABASE_PASSWORD}'] = $dbPassForm;
403
    $config['{DATABASE_MAIN}'] = $dbNameForm;
404
    $config['{ROOT_WEB}'] = $urlForm;
405
    $config['{ROOT_SYS}'] = $root_sys;
406
    $config['{URL_APPEND_PATH}'] = $urlAppendPath;
407
    $config['{PLATFORM_LANGUAGE}'] = $languageForm;
408
    $config['{SECURITY_KEY}'] = md5(uniqid(rand().time()));
409
    $config['{ENCRYPT_PASSWORD}'] = $encryptPassForm;
410
411
    $config['SESSION_LIFETIME'] = $session_lifetime;
412
    $config['{NEW_VERSION}'] = $new_version;
413
    $config['NEW_VERSION_STABLE'] = trueFalse($new_version_stable);
414
415
    foreach ($config as $key => $value) {
416
        $content = str_replace($key, $value, $content);
417
    }
418
    $fp = @fopen($path, 'w');
419
420
    if (!$fp) {
421
        echo '<strong>
422
                <font color="red">Your script doesn\'t have write access to the config directory</font></strong><br />
423
                <em>('.str_replace('\\', '/', realpath($path)).')</em><br /><br />
424
                You probably do not have write access on Chamilo root directory,
425
                i.e. you should <em>CHMOD 777</em> or <em>755</em> or <em>775</em>.<br /><br />
426
                Your problems can be related on two possible causes:<br />
427
                <ul>
428
                  <li>Permission problems.<br />Try initially with <em>chmod -R 777</em> and increase restrictions gradually.</li>
429
                  <li>PHP is running in <a href="http://www.php.net/manual/en/features.safe-mode.php" target="_blank">Safe-Mode</a>.
430
                  If possible, try to switch it off.</li>
431
                </ul>
432
                <a href="http://forum.chamilo.org/" target="_blank">Read about this problem in Support Forum</a><br /><br />
433
                Please go back to step 5.
434
                <p><input type="submit" name="step5" value="&lt; Back" /></p>
435
                </td></tr></table></form></body></html>';
436
        exit;
437
    }
438
439
    fwrite($fp, $content);
440
    fclose($fp);
441
}
442
443
/**
444
 * Returns a list of language directories.
445
 */
446
function &get_language_folder_list()
447
{
448
    static $result;
449
    if (!is_array($result)) {
450
        $result = [];
451
        $exceptions = ['.', '..', 'CVS', '.svn'];
452
        $search = ['_latin', '_unicode', '_corporate', '_org', '_KM', '_'];
453
        $replace_with = [' (Latin)', ' (unicode)', ' (corporate)', ' (org)', ' (KM)', ' '];
454
        $dirname = api_get_path(SYS_LANG_PATH);
455
        $handle = opendir($dirname);
456
        while ($entries = readdir($handle)) {
457
            if (in_array($entries, $exceptions)) {
458
                continue;
459
            }
460
            if (is_dir($dirname.$entries)) {
461
                if (is_file($dirname.$entries.'/install_disabled')) {
462
                    // Skip all languages that have this file present, just for
463
                    // the install process (languages incomplete)
464
                    continue;
465
                }
466
                $result[$entries] = ucwords(str_replace($search, $replace_with, $entries));
467
            }
468
        }
469
        closedir($handle);
470
        asort($result);
471
    }
472
473
    return $result;
474
}
475
476
/**
477
 * TODO: my_directory_to_array() - maybe within the main API there is already a suitable function?
478
 *
479
 * @param string $directory Full path to a directory
480
 *
481
 * @return array A list of files and dirs in the directory
482
 */
483
function my_directory_to_array($directory)
484
{
485
    $array_items = [];
486
    if ($handle = opendir($directory)) {
487
        while (false !== ($file = readdir($handle))) {
488
            if ($file != "." && $file != "..") {
489
                if (is_dir($directory."/".$file)) {
490
                    $array_items = array_merge($array_items, my_directory_to_array($directory.'/'.$file));
491
                    $file = $directory."/".$file;
492
                    $array_items[] = preg_replace("/\/\//si", '/', $file);
493
                }
494
            }
495
        }
496
        closedir($handle);
497
    }
498
499
    return $array_items;
500
}
501
502
/**
503
 * This function returns the value of a parameter from the configuration file.
504
 *
505
 * WARNING - this function relies heavily on global variables $updateFromConfigFile
506
 * and $configFile, and also changes these globals. This can be rewritten.
507
 *
508
 * @param string $param the parameter of which the value is returned
509
 * @param   string  If we want to give the path rather than take it from POST
510
 *
511
 * @return string the value of the parameter
512
 *
513
 * @author Olivier Brouckaert
514
 * @author Reworked by Ivan Tcholakov, 2010
515
 */
516
function get_config_param($param, $updatePath = '')
517
{
518
    global $configFile, $updateFromConfigFile;
519
520
    // Look if we already have the queried parameter.
521
    if (is_array($configFile) && isset($configFile[$param])) {
522
        return $configFile[$param];
523
    }
524
    if (empty($updatePath) && !empty($_POST['updatePath'])) {
525
        $updatePath = $_POST['updatePath'];
526
    }
527
528
    if (empty($updatePath)) {
529
        $updatePath = api_get_path(SYS_PATH);
530
    }
531
    $updatePath = api_add_trailing_slash(str_replace('\\', '/', realpath($updatePath)));
532
    $updateFromInstalledVersionFile = '';
533
534
    if (empty($updateFromConfigFile)) {
535
        // If update from previous install was requested,
536
        // try to recover config file from Chamilo 1.9.x
537
        if (file_exists($updatePath.'main/inc/conf/configuration.php')) {
538
            $updateFromConfigFile = 'main/inc/conf/configuration.php';
539
        } elseif (file_exists($updatePath.'app/config/configuration.php')) {
540
            $updateFromConfigFile = 'app/config/configuration.php';
541
        } else {
542
            // Give up recovering.
543
            //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);
544
            return null;
545
        }
546
    }
547
548
    if (file_exists($updatePath.$updateFromConfigFile) &&
549
        !is_dir($updatePath.$updateFromConfigFile)
550
    ) {
551
        require $updatePath.$updateFromConfigFile;
552
        $config = new Zend\Config\Config($_configuration);
553
554
        return $config->get($param);
555
    }
556
557
    error_log('Config array could not be found in get_config_param()', 0);
558
559
    return null;
560
561
    /*if (file_exists($updatePath.$updateFromConfigFile)) {
562
        return $val;
563
    } else {
564
        error_log('Config array could not be found in get_config_param()', 0);
565
        return null;
566
    }*/
567
}
568
569
/*      DATABASE RELATED FUNCTIONS */
570
571
/**
572
 * Gets a configuration parameter from the database. Returns returns null on failure.
573
 *
574
 * @param string $param Name of param we want
575
 *
576
 * @return mixed The parameter value or null if not found
577
 */
578
function get_config_param_from_db($param = '')
579
{
580
    $param = Database::escape_string($param);
581
582
    if (($res = Database::query("SELECT * FROM settings_current WHERE variable = '$param'")) !== false) {
583
        if (Database::num_rows($res) > 0) {
584
            $row = Database::fetch_array($res);
585
586
            return $row['selected_value'];
587
        }
588
    }
589
590
    return null;
591
}
592
593
/**
594
 * Connect to the database and returns the entity manager.
595
 *
596
 * @param string $dbHostForm     DB host
597
 * @param string $dbUsernameForm DB username
598
 * @param string $dbPassForm     DB password
599
 * @param string $dbNameForm     DB name
600
 * @param int    $dbPortForm     DB port
601
 *
602
 * @return EntityManager
603
 */
604
function connectToDatabase(
605
    $dbHostForm,
606
    $dbUsernameForm,
607
    $dbPassForm,
608
    $dbNameForm,
609
    $dbPortForm = 3306
610
) {
611
    $dbParams = [
612
        'driver' => 'pdo_mysql',
613
        'host' => $dbHostForm,
614
        'port' => $dbPortForm,
615
        'user' => $dbUsernameForm,
616
        'password' => $dbPassForm,
617
        'dbname' => $dbNameForm,
618
    ];
619
620
    $database = new \Database();
621
    $database->connect($dbParams);
622
623
    return $database->getManager();
624
}
625
626
/*      DISPLAY FUNCTIONS */
627
628
/**
629
 * This function prints class=active_step $current_step=$param.
630
 *
631
 * @param int $param A step in the installer process
632
 *
633
 * @author Patrick Cool <[email protected]>, Ghent University
634
 */
635
function step_active($param)
636
{
637
    global $current_step;
638
    if ($param == $current_step) {
639
        echo 'class="current-step" ';
640
    }
641
}
642
643
/**
644
 * This function displays the Step X of Y -.
645
 *
646
 * @return string String that says 'Step X of Y' with the right values
647
 */
648
function display_step_sequence()
649
{
650
    global $current_step;
651
652
    return get_lang('Step'.$current_step).' &ndash; ';
653
}
654
655
/**
656
 * Displays a drop down box for selection the preferred language.
657
 */
658
function display_language_selection_box(
659
    $name = 'language_list',
660
    $default_language = 'english'
661
) {
662
    // Reading language list.
663
    $language_list = get_language_folder_list();
664
665
    /*
666
    // Reduction of the number of languages shown. Enable this fragment of code for customization purposes.
667
    // Modify the language list according to your preference. Don't exclude the 'english' item.
668
    $language_to_display = array('asturian', 'bulgarian', 'english', 'italian', 'french', 'slovenian', 'slovenian_unicode', 'spanish');
669
    foreach ($language_list as $key => & $value) {
670
        if (!in_array($key, $language_to_display)) {
671
            unset($language_list[$key]);
672
        }
673
    }
674
    */
675
676
    // Sanity checks due to the possibility for customizations.
677
    if (!is_array($language_list) || empty($language_list)) {
678
        $language_list = ['english' => 'English'];
679
    }
680
681
    // Sorting again, if it is necessary.
682
    //asort($language_list);
683
684
    // More sanity checks.
685
    if (!array_key_exists($default_language, $language_list)) {
686
        if (array_key_exists('english', $language_list)) {
687
            $default_language = 'english';
688
        } else {
689
            $language_keys = array_keys($language_list);
690
            $default_language = $language_keys[0];
691
        }
692
    }
693
694
    // Displaying the box.
695
    $html = '';
696
    $html .= "\t\t<select class='selectpicker show-tick' name=\"$name\">\n";
697
    foreach ($language_list as $key => $value) {
698
        if ($key == $default_language) {
699
            $option_end = ' selected="selected">';
700
        } else {
701
            $option_end = '>';
702
        }
703
        $html .= "\t\t\t<option value=\"$key\"$option_end";
704
        $html .= $value;
705
        $html .= "</option>\n";
706
    }
707
    $html .= "\t\t</select>\n";
708
709
    return $html;
710
}
711
712
/**
713
 * This function displays a language dropdown box so that the installatioin
714
 * can be done in the language of the user.
715
 */
716
function display_language_selection()
717
{
718
    ?>
719
    <h2><?php get_lang('WelcomeToTheChamiloInstaller'); ?></h2>
720
    <div class="RequirementHeading">
721
        <h2><?php echo display_step_sequence(); ?>
722
            <?php echo get_lang('InstallationLanguage'); ?>
723
        </h2>
724
        <p><?php echo get_lang('PleaseSelectInstallationProcessLanguage'); ?>:</p>
725
        <form id="lang_form" method="post" action="<?php echo api_get_self(); ?>">
726
        <div class="form-group">
727
            <div class="col-sm-4">
728
                <?php echo display_language_selection_box('language_list', api_get_interface_language()); ?>
729
            </div>
730
            <div class="col-sm-6">
731
                <button type="submit" name="step1" class="btn btn-success" value="<?php echo get_lang('Next'); ?>">
732
                    <em class="fa fa-forward"> </em>
733
                    <?php echo get_lang('Next'); ?></button>
734
            </div>
735
        </div>
736
737
        <input type="hidden" name="is_executable" id="is_executable" value="-" />
738
        </form>
739
740
    </div>
741
    <div class="RequirementHeading">
742
        <?php echo get_lang('YourLanguageNotThereContactUs'); ?>
743
    </div>
744
<?php
745
}
746
747
/**
748
 * This function displays the requirements for installing Chamilo.
749
 *
750
 * @param string $installType
751
 * @param bool   $badUpdatePath
752
 * @param string $updatePath            The updatePath given (if given)
753
 * @param array  $update_from_version_8 The different subversions from version 1.9
754
 *
755
 * @author unknow
756
 * @author Patrick Cool <[email protected]>, Ghent University
757
 */
758
function display_requirements(
759
    $installType,
760
    $badUpdatePath,
761
    $updatePath = '',
762
    $update_from_version_8 = []
763
) {
764
    global $_setting, $originalMemoryLimit;
765
    echo '<div class="RequirementHeading"><h2>'.display_step_sequence().get_lang('Requirements')."</h2></div>";
766
    echo '<div class="RequirementText">';
767
    echo '<strong>'.get_lang('ReadThoroughly').'</strong><br />';
768
    echo get_lang('MoreDetails').' <a href="../../documentation/installation_guide.html" target="_blank">'.
769
        get_lang('ReadTheInstallationGuide').'</a>.<br />'."\n";
770
771
    if ($installType == 'update') {
772
        echo get_lang('IfYouPlanToUpgradeFromOlderVersionYouMightWantToHaveAlookAtTheChangelog').'<br />';
773
    }
774
    echo '</div>';
775
776
    $properlyAccessUrl = checkAccessUrl();
777
778
    if (!$properlyAccessUrl) {
779
        echo '
780
            <div class="alert alert-danger">
781
                '.Display::return_icon('error.png', get_lang('Error'), [], ICON_SIZE_MEDIUM, true, false, false).
782
            ' '.
783
            sprintf(get_lang('InstallMultiURLDetectedNotMainURL'), api_get_configuration_value('root_web')).'
784
            </div>
785
        ';
786
    }
787
788
    //  SERVER REQUIREMENTS
789
    echo '<div class="RequirementHeading"><h4>'.get_lang('ServerRequirements').'</h4>';
790
    if (phpversion() < '7.0') {
791
        // If PHP < 7.0, then an undefined date.timezone would trigger a
792
        // warning, so ask for it to be defined. Above 7.0, date.timezone
793
        // defaults to UTC and does not trigger warnings.
794
        // See https://php.net/manual/en/migration70.other-changes.php
795
        $timezone = checkPhpSettingExists("date.timezone");
796
        if (!$timezone) {
797
            echo "<div class='alert alert-warning'>".
798
                Display::return_icon(
799
                    'warning.png',
800
                    get_lang('Warning'),
801
                    '',
802
                    ICON_SIZE_MEDIUM,
803
                    true,
804
                    false,
805
                    false
806
                ).
807
                get_lang("DateTimezoneSettingNotSet")."</div>";
808
        }
809
    }
810
811
    echo '<div class="RequirementText">'.get_lang('ServerRequirementsInfo').'</div>';
812
    echo '<div class="RequirementContent">';
813
    echo '<table class="table">
814
            <tr>
815
                <td class="requirements-item">'.get_lang('PHPVersion').' >= '.REQUIRED_PHP_VERSION.'</td>
816
                <td class="requirements-value">';
817
    if (phpversion() < REQUIRED_PHP_VERSION) {
818
        echo '<strong><font color="red">'.get_lang('PHPVersionError').'</font></strong>';
819
    } else {
820
        echo '<strong><font color="green">'.get_lang('PHPVersionOK').' '.phpversion().'</font></strong>';
821
    }
822
    echo '</td>
823
            </tr>
824
            <tr>
825
                <td class="requirements-item"><a href="https://php.net/manual/en/book.session.php" target="_blank">Session</a> '.get_lang('Support').'</td>
826
                <td class="requirements-value">'.checkExtension('session', get_lang('Yes'), get_lang('ExtensionSessionsNotAvailable')).'</td>
827
            </tr>
828
            <tr>
829
                <td class="requirements-item"><a href="https://php.net/manual/en/book.mysql.php" target="_blank">pdo_mysql</a> '.get_lang('Support').'</td>
830
                <td class="requirements-value">'.checkExtension('pdo_mysql', get_lang('Yes'), get_lang('ExtensionMySQLNotAvailable')).'</td>
831
            </tr>
832
            <tr>
833
                <td class="requirements-item"><a href="https://php.net/manual/en/book.zip.php" target="_blank">Zip</a> '.get_lang('Support').'</td>
834
                <td class="requirements-value">'.checkExtension('zip', get_lang('Yes'), get_lang('ExtensionNotAvailable')).'</td>
835
            </tr>
836
            <tr>
837
                <td class="requirements-item"><a href="https://php.net/manual/en/book.zlib.php" target="_blank">Zlib</a> '.get_lang('Support').'</td>
838
                <td class="requirements-value">'.checkExtension('zlib', get_lang('Yes'), get_lang('ExtensionZlibNotAvailable')).'</td>
839
            </tr>
840
            <tr>
841
                <td class="requirements-item"><a href="https://php.net/manual/en/book.pcre.php" target="_blank">Perl-compatible regular expressions</a> '.get_lang('Support').'</td>
842
                <td class="requirements-value">'.checkExtension('pcre', get_lang('Yes'), get_lang('ExtensionPCRENotAvailable')).'</td>
843
            </tr>
844
            <tr>
845
                <td class="requirements-item"><a href="https://php.net/manual/en/book.xml.php" target="_blank">XML</a> '.get_lang('Support').'</td>
846
                <td class="requirements-value">'.checkExtension('xml', get_lang('Yes'), get_lang('No')).'</td>
847
            </tr>
848
            <tr>
849
                <td class="requirements-item"><a href="https://php.net/manual/en/book.intl.php" target="_blank">Internationalization</a> '.get_lang('Support').'</td>
850
                <td class="requirements-value">'.checkExtension('intl', get_lang('Yes'), get_lang('No')).'</td>
851
            </tr>
852
               <tr>
853
                <td class="requirements-item"><a href="https://php.net/manual/en/book.json.php" target="_blank">JSON</a> '.get_lang('Support').'</td>
854
                <td class="requirements-value">'.checkExtension('json', get_lang('Yes'), get_lang('No')).'</td>
855
            </tr>
856
             <tr>
857
                <td class="requirements-item"><a href="https://php.net/manual/en/book.image.php" target="_blank">GD</a> '.get_lang('Support').'</td>
858
                <td class="requirements-value">'.checkExtension('gd', get_lang('Yes'), get_lang('ExtensionGDNotAvailable')).'</td>
859
            </tr>
860
            <tr>
861
                <td class="requirements-item"><a href="https://php.net/manual/en/book.curl.php" target="_blank">cURL</a>'.get_lang('Support').'</td>
862
                <td class="requirements-value">'.checkExtension('curl', get_lang('Yes'), get_lang('No')).'</td>
863
            </tr>
864
            <tr>
865
                <td class="requirements-item"><a href="https://php.net/manual/en/book.fileinfo.php" target="_blank">FileInfo</a>'.get_lang('Support').'</td>
866
                <td class="requirements-value">'.checkExtension('fileinfo', get_lang('Yes'), get_lang('No')).'</td>
867
            </tr>
868
869
            <tr>
870
                <td class="requirements-item"><a href="https://php.net/manual/en/book.mbstring.php" target="_blank">Multibyte string</a> '.get_lang('Support').' ('.get_lang('Optional').')</td>
871
                <td class="requirements-value">'.checkExtension('mbstring', get_lang('Yes'), get_lang('ExtensionMBStringNotAvailable'), true).'</td>
872
            </tr>
873
            <tr>
874
                <td class="requirements-item"><a href="https://php.net/opcache" target="_blank">Zend OpCache</a> '.get_lang('Support').' ('.get_lang('Optional').')</td>
875
                <td class="requirements-value">'.checkExtension('Zend OPcache', get_lang('Yes'), get_lang('No'), true, 'opcache.enable').'</td>
876
            </tr>
877
            <tr>
878
                <td class="requirements-item"><a href="https://php.net/apcu" target="_blank">APCu</a> '.get_lang('Support').' ('.get_lang('Optional').')</td>
879
                <td class="requirements-value">'.checkExtension('apcu', get_lang('Yes'), get_lang('No'), true, 'apc.enabled').'</td>
880
            </tr>
881
            <tr>
882
                <td class="requirements-item"><a href="https://php.net/manual/en/book.iconv.php" target="_blank">Iconv</a> '.get_lang('Support').' ('.get_lang('Optional').')</td>
883
                <td class="requirements-value">'.checkExtension('iconv', get_lang('Yes'), get_lang('No'), true).'</td>
884
            </tr>
885
            <tr>
886
                <td class="requirements-item"><a href="https://php.net/manual/en/book.ldap.php" target="_blank">LDAP</a> '.get_lang('Support').' ('.get_lang('Optional').')</td>
887
                <td class="requirements-value">'.checkExtension('ldap', get_lang('Yes'), get_lang('ExtensionLDAPNotAvailable'), true).'</td>
888
            </tr>
889
            <tr>
890
                <td class="requirements-item"><a href="https://xapian.org/" target="_blank">Xapian</a> '.get_lang('Support').' ('.get_lang('Optional').')</td>
891
                <td class="requirements-value">'.checkExtension('xapian', get_lang('Yes'), get_lang('No'), true).'</td>
892
            </tr>
893
            <tr>
894
                <td class="requirements-item"><a href="https://php.net/openssl" target="_blank">OpenSSL</a> '.get_lang('Support').' ('.get_lang('Optional').')</td>
895
                <td class="requirements-value">'.checkExtension('openssl', get_lang('Yes'), get_lang('No'), true).'</td>
896
            </tr>
897
        </table>';
898
    echo '</div>';
899
    echo '</div>';
900
901
    // RECOMMENDED SETTINGS
902
    // Note: these are the settings for Joomla, does this also apply for Chamilo?
903
    // Note: also add upload_max_filesize here so that large uploads are possible
904
    echo '<div class="RequirementHeading"><h4>'.get_lang('RecommendedSettings').'</h4>';
905
    echo '<div class="RequirementText">'.get_lang('RecommendedSettingsInfo').'</div>';
906
    echo '<div class="RequirementContent">';
907
    echo '<table class="table">
908
            <tr>
909
                <th>'.get_lang('Setting').'</th>
910
                <th>'.get_lang('Recommended').'</th>
911
                <th>'.get_lang('Actual').'</th>
912
            </tr>
913
            <tr>
914
                <td class="requirements-item"><a href="https://php.net/manual/features.safe-mode.php">Safe Mode</a></td>
915
                <td class="requirements-recommended">'.Display::label('OFF', 'success').'</td>
916
                <td class="requirements-value">'.checkPhpSetting('safe_mode', 'OFF').'</td>
917
            </tr>
918
            <tr>
919
                <td class="requirements-item"><a href="https://php.net/manual/ref.errorfunc.php#ini.display-errors">Display Errors</a></td>
920
                <td class="requirements-recommended">'.Display::label('OFF', 'success').'</td>
921
                <td class="requirements-value">'.checkPhpSetting('display_errors', 'OFF').'</td>
922
            </tr>
923
            <tr>
924
                <td class="requirements-item"><a href="https://php.net/manual/ini.core.php#ini.file-uploads">File Uploads</a></td>
925
                <td class="requirements-recommended">'.Display::label('ON', 'success').'</td>
926
                <td class="requirements-value">'.checkPhpSetting('file_uploads', 'ON').'</td>
927
            </tr>
928
            <tr>
929
                <td class="requirements-item"><a href="https://php.net/manual/ref.info.php#ini.magic-quotes-gpc">Magic Quotes GPC</a></td>
930
                <td class="requirements-recommended">'.Display::label('OFF', 'success').'</td>
931
                <td class="requirements-value">'.checkPhpSetting('magic_quotes_gpc', 'OFF').'</td>
932
            </tr>
933
            <tr>
934
                <td class="requirements-item"><a href="https://php.net/manual/ref.info.php#ini.magic-quotes-runtime">Magic Quotes Runtime</a></td>
935
                <td class="requirements-recommended">'.Display::label('OFF', 'success').'</td>
936
                <td class="requirements-value">'.checkPhpSetting('magic_quotes_runtime', 'OFF').'</td>
937
            </tr>
938
            <tr>
939
                <td class="requirements-item"><a href="https://php.net/manual/security.globals.php">Register Globals</a></td>
940
                <td class="requirements-recommended">'.Display::label('OFF', 'success').'</td>
941
                <td class="requirements-value">'.checkPhpSetting('register_globals', 'OFF').'</td>
942
            </tr>
943
            <tr>
944
                <td class="requirements-item"><a href="https://php.net/manual/ref.session.php#ini.session.auto-start">Session auto start</a></td>
945
                <td class="requirements-recommended">'.Display::label('OFF', 'success').'</td>
946
                <td class="requirements-value">'.checkPhpSetting('session.auto_start', 'OFF').'</td>
947
            </tr>
948
            <tr>
949
                <td class="requirements-item"><a href="https://php.net/manual/ini.core.php#ini.short-open-tag">Short Open Tag</a></td>
950
                <td class="requirements-recommended">'.Display::label('OFF', 'success').'</td>
951
                <td class="requirements-value">'.checkPhpSetting('short_open_tag', 'OFF').'</td>
952
            </tr>
953
            <tr>
954
                <td class="requirements-item"><a href="https://php.net/manual/en/session.configuration.php#ini.session.cookie-httponly">Cookie HTTP Only</a></td>
955
                <td class="requirements-recommended">'.Display::label('ON', 'success').'</td>
956
                <td class="requirements-value">'.checkPhpSetting('session.cookie_httponly', 'ON').'</td>
957
            </tr>
958
            <tr>
959
                <td class="requirements-item"><a href="https://php.net/manual/ini.core.php#ini.upload-max-filesize">Maximum upload file size</a></td>
960
                <td class="requirements-recommended">'.Display::label('>= '.REQUIRED_MIN_UPLOAD_MAX_FILESIZE.'M', 'success').'</td>
961
                <td class="requirements-value">'.compare_setting_values(ini_get('upload_max_filesize'), REQUIRED_MIN_UPLOAD_MAX_FILESIZE).'</td>
962
            </tr>
963
            <tr>
964
                <td class="requirements-item"><a href="https://php.net/manual/ini.core.php#ini.post-max-size">Maximum post size</a></td>
965
                <td class="requirements-recommended">'.Display::label('>= '.REQUIRED_MIN_POST_MAX_SIZE.'M', 'success').'</td>
966
                <td class="requirements-value">'.compare_setting_values(ini_get('post_max_size'), REQUIRED_MIN_POST_MAX_SIZE).'</td>
967
            </tr>
968
            <tr>
969
                <td class="requirements-item"><a href="https://php.net/manual/en/ini.core.php#ini.memory-limit">Memory Limit</a></td>
970
                <td class="requirements-recommended">'.Display::label('>= '.REQUIRED_MIN_MEMORY_LIMIT.'M', 'success').'</td>
971
                <td class="requirements-value">'.compare_setting_values($originalMemoryLimit, REQUIRED_MIN_MEMORY_LIMIT).'</td>
972
            </tr>
973
          </table>';
974
    echo '  </div>';
975
    echo '</div>';
976
977
    // DIRECTORY AND FILE PERMISSIONS
978
    echo '<div class="RequirementHeading"><h4>'.get_lang('DirectoryAndFilePermissions').'</h4>';
979
    echo '<div class="RequirementText">'.get_lang('DirectoryAndFilePermissionsInfo').'</div>';
980
    echo '<div class="RequirementContent">';
981
982
    $course_attempt_name = '__XxTestxX__';
983
    $course_dir = api_get_path(SYS_COURSE_PATH).$course_attempt_name;
984
    $fileToCreate = 'test.html';
985
    // Just in case
986
    @unlink($course_dir.'/'.$fileToCreate);
987
    @rmdir($course_dir);
988
989
    $perms_dir = [0777, 0755, 0775, 0770, 0750, 0700];
990
    $perms_fil = [0666, 0644, 0664, 0660, 0640, 0600];
991
    $course_test_was_created = false;
992
    $dir_perm_verified = 0777;
993
    foreach ($perms_dir as $perm) {
994
        $r = @mkdir($course_dir, $perm);
995
        if ($r === true) {
996
            $dir_perm_verified = $perm;
997
            $course_test_was_created = true;
998
            break;
999
        }
1000
    }
1001
1002
    $fil_perm_verified = 0666;
1003
    $file_course_test_was_created = false;
1004
    if (is_dir($course_dir)) {
1005
        foreach ($perms_fil as $perm) {
1006
            if ($file_course_test_was_created == true) {
1007
                break;
1008
            }
1009
            $r = @touch($course_dir.'/'.$fileToCreate, $perm);
1010
            if ($r === true) {
1011
                $fil_perm_verified = $perm;
1012
                if (checkCourseScriptCreation($course_dir, $course_attempt_name, $fileToCreate)) {
1013
                    $file_course_test_was_created = true;
1014
                }
1015
            }
1016
        }
1017
    }
1018
1019
    @unlink($course_dir.'/'.$fileToCreate);
1020
    @rmdir($course_dir);
1021
1022
    $_SESSION['permissions_for_new_directories'] = $_setting['permissions_for_new_directories'] = $dir_perm_verified;
1023
    $_SESSION['permissions_for_new_files'] = $_setting['permissions_for_new_files'] = $fil_perm_verified;
1024
1025
    $dir_perm = Display::label('0'.decoct($dir_perm_verified), 'info');
1026
    $file_perm = Display::label('0'.decoct($fil_perm_verified), 'info');
1027
1028
    $courseTestLabel = Display::label(get_lang('No'), 'important');
1029
    if ($course_test_was_created && $file_course_test_was_created) {
1030
        $courseTestLabel = Display::label(get_lang('Yes'), 'success');
1031
    }
1032
1033
    if ($course_test_was_created && !$file_course_test_was_created) {
1034
        $courseTestLabel = Display::label(get_lang('Warning'), 'warning');
1035
        $courseTestLabel .= '<br />'.sprintf(
1036
            get_lang('InstallWarningCouldNotInterpretPHP'),
1037
            api_get_path(WEB_COURSE_PATH).$course_attempt_name.'/'.$fileToCreate
1038
        );
1039
    }
1040
1041
    if (!$course_test_was_created && !$file_course_test_was_created) {
1042
        $courseTestLabel = Display::label(get_lang('No'), 'important');
1043
    }
1044
1045
    $oldConf = '';
1046
    if (file_exists(api_get_path(SYS_CODE_PATH).'inc/conf/configuration.php')) {
1047
        $oldConf = '<tr>
1048
            <td class="requirements-item">'.api_get_path(SYS_CODE_PATH).'inc/conf</td>
1049
            <td class="requirements-value">'.check_writable(api_get_path(SYS_CODE_PATH).'inc/conf').'</td>
1050
        </tr>';
1051
    }
1052
1053
    echo '<table class="table">
1054
            '.$oldConf.'
1055
            <tr>
1056
                <td class="requirements-item">'.api_get_path(SYS_APP_PATH).'</td>
1057
                <td class="requirements-value">'.check_writable(api_get_path(SYS_APP_PATH)).'</td>
1058
            </tr>
1059
            <tr>
1060
                <td class="requirements-item">'.api_get_path(SYS_CODE_PATH).'default_course_document/images/</td>
1061
                <td class="requirements-value">'.check_writable(api_get_path(SYS_CODE_PATH).'default_course_document/images/').'</td>
1062
            </tr>
1063
            <tr>
1064
                <td class="requirements-item">'.api_get_path(SYS_CODE_PATH).'lang/</td>
1065
                <td class="requirements-value">'.check_writable(api_get_path(SYS_CODE_PATH).'lang/', true).' <br />('.get_lang('SuggestionOnlyToEnableSubLanguageFeatureOrUpgradeProcess').')</td>
1066
            </tr>
1067
            <tr>
1068
                <td class="requirements-item">'.api_get_path(SYS_PATH).'vendor/</td>
1069
                <td class="requirements-value">'.checkReadable(api_get_path(SYS_PATH).'vendor').'</td>
1070
            </tr>
1071
            <tr>
1072
                <td class="requirements-item">'.api_get_path(SYS_PUBLIC_PATH).'</td>
1073
                <td class="requirements-value">'.check_writable(api_get_path(SYS_PUBLIC_PATH)).'</td>
1074
            </tr>
1075
            <tr>
1076
                <td class="requirements-item">'.get_lang('CourseTestWasCreated').'</td>
1077
                <td class="requirements-value">'.$courseTestLabel.' </td>
1078
            </tr>
1079
            <tr>
1080
                <td class="requirements-item">'.get_lang('PermissionsForNewDirs').'</td>
1081
                <td class="requirements-value">'.$dir_perm.' </td>
1082
            </tr>
1083
            <tr>
1084
                <td class="requirements-item">'.get_lang('PermissionsForNewFiles').'</td>
1085
                <td class="requirements-value">'.$file_perm.' </td>
1086
            </tr>
1087
        </table>';
1088
    echo '  </div>';
1089
    echo '</div>';
1090
1091
    if ($installType == 'update' && (empty($updatePath) || $badUpdatePath)) {
1092
        if ($badUpdatePath) {
1093
            ?>
1094
            <div class="alert alert-warning">
1095
                <?php echo get_lang('Error'); ?>!<br />
1096
                Chamilo <?php echo implode('|', $update_from_version_8).' '.get_lang('HasNotBeenFoundInThatDir'); ?>.
1097
            </div>
1098
        <?php
1099
        } else {
1100
            echo '<br />';
1101
        } ?>
1102
            <div class="row">
1103
                <div class="col-md-12">
1104
                    <p><?php echo get_lang('OldVersionRootPath'); ?>:
1105
                        <input type="text" name="updatePath" size="50" value="<?php echo ($badUpdatePath && !empty($updatePath)) ? htmlentities($updatePath) : ''; ?>" />
1106
                    </p>
1107
                    <p>
1108
                        <button type="submit" class="btn btn-default" name="step1" value="<?php echo get_lang('Back'); ?>" >
1109
                            <em class="fa fa-backward"> <?php echo get_lang('Back'); ?></em>
1110
                        </button>
1111
                        <input type="hidden" name="is_executable" id="is_executable" value="-" />
1112
                        <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;" >
1113
                            <em class="fa fa-forward"> </em> <?php echo get_lang('Next'); ?>
1114
                        </button>
1115
                    </p>
1116
                </div>
1117
            </div>
1118
1119
        <?php
1120
    } else {
1121
        $error = false;
1122
        // First, attempt to set writing permissions if we don't have them yet
1123
        $perm = octdec('0777');
1124
        $perm_file = octdec('0666');
1125
        $notWritable = [];
1126
1127
        $checked_writable = api_get_path(SYS_APP_PATH);
1128
        if (!is_writable($checked_writable)) {
1129
            $notWritable[] = $checked_writable;
1130
            @chmod($checked_writable, $perm);
1131
        }
1132
1133
        $checked_writable = api_get_path(SYS_PUBLIC_PATH);
1134
        if (!is_writable($checked_writable)) {
1135
            $notWritable[] = $checked_writable;
1136
            @chmod($checked_writable, $perm);
1137
        }
1138
1139
        $checked_writable = api_get_path(SYS_CODE_PATH).'default_course_document/images/';
1140
        if (!is_writable($checked_writable)) {
1141
            $notWritable[] = $checked_writable;
1142
            @chmod($checked_writable, $perm);
1143
        }
1144
1145
        if ($course_test_was_created == false) {
1146
            $error = true;
1147
        }
1148
1149
        $checked_writable = api_get_path(CONFIGURATION_PATH).'configuration.php';
1150
        if (file_exists($checked_writable) && !is_writable($checked_writable)) {
1151
            $notWritable[] = $checked_writable;
1152
            @chmod($checked_writable, $perm_file);
1153
        }
1154
1155
        // Second, if this fails, report an error
1156
1157
        //--> The user would have to adjust the permissions manually
1158
        if (count($notWritable) > 0) {
1159
            $error = true; ?>
1160
            <div class="text-danger">
1161
                <h3 class="text-center"><?php echo get_lang('Warning'); ?></h3>
1162
                <p>
1163
                    <?php printf(get_lang('NoWritePermissionPleaseReadInstallGuide'), '<a href="../../documentation/installation_guide.html" target="blank">', '</a>'); ?>
1164
                </p>
1165
            </div>
1166
            <?php
1167
            echo '<ul>';
1168
            foreach ($notWritable as $value) {
1169
                echo '<li class="text-danger">'.$value.'</li>';
1170
            }
1171
            echo '</ul>';
1172
        } elseif (file_exists(api_get_path(CONFIGURATION_PATH).'configuration.php')) {
1173
            // Check wether a Chamilo configuration file already exists.
1174
            echo '<div class="alert alert-warning"><h4><center>';
1175
            echo get_lang('WarningExistingLMSInstallationDetected');
1176
            echo '</center></h4></div>';
1177
        }
1178
1179
        $deprecated = [
1180
            api_get_path(SYS_CODE_PATH).'exercice/',
1181
            api_get_path(SYS_CODE_PATH).'newscorm/',
1182
            api_get_path(SYS_PLUGIN_PATH).'ticket/',
1183
            api_get_path(SYS_PLUGIN_PATH).'skype/',
1184
        ];
1185
        $deprecatedToRemove = [];
1186
        foreach ($deprecated as $deprecatedDirectory) {
1187
            if (!is_dir($deprecatedDirectory)) {
1188
                continue;
1189
            }
1190
            $deprecatedToRemove[] = $deprecatedDirectory;
1191
        }
1192
1193
        if (count($deprecatedToRemove) > 0) {
1194
            ?>
1195
            <p class="text-danger"><?php echo get_lang('WarningForDeprecatedDirectoriesForUpgrade'); ?></p>
1196
            <ul>
1197
                <?php foreach ($deprecatedToRemove as $deprecatedDirectory) {
1198
                ?>
1199
                    <li class="text-danger"><?php echo $deprecatedDirectory; ?></li>
1200
                <?php
1201
            } ?>
1202
            </ul>
1203
            <?php
1204
        }
1205
1206
        if (!$properlyAccessUrl) {
1207
            $error = true;
1208
        }
1209
1210
        // And now display the choice buttons (go back or install)?>
1211
        <p align="center" style="padding-top:15px">
1212
        <button type="submit" name="step1" class="btn btn-default" onclick="javascript: window.location='index.php'; return false;" value="<?php echo get_lang('Previous'); ?>" >
1213
            <em class="fa fa-backward"> </em> <?php echo get_lang('Previous'); ?>
1214
        </button>
1215
        <button type="submit" name="step2_install" class="btn btn-success" value="<?php echo get_lang("NewInstallation"); ?>" <?php if ($error) {
1216
            echo 'disabled="disabled"';
1217
        } ?> >
1218
            <em class="fa fa-forward"> </em> <?php echo get_lang('NewInstallation'); ?>
1219
        </button>
1220
        <input type="hidden" name="is_executable" id="is_executable" value="-" />
1221
            <button type="submit" class="btn btn-default" <?php echo !$error ?: 'disabled="disabled"'; ?> name="step2_update_8" value="Upgrade from Chamilo 1.9.x">
1222
                <em class="fa fa-forward" aria-hidden="true"></em> <?php echo get_lang('UpgradeVersion'); ?>
1223
            </button>
1224
            </p>
1225
        <?php
1226
    }
1227
}
1228
1229
/**
1230
 * Displays the license (GNU GPL) as step 2, with
1231
 * - an "I accept" button named step3 to proceed to step 3;
1232
 * - a "Back" button named step1 to go back to the first step.
1233
 */
1234
function display_license_agreement()
1235
{
1236
    echo '<div class="RequirementHeading"><h2>'.display_step_sequence().get_lang('Licence').'</h2>';
1237
    echo '<p>'.get_lang('LMSLicenseInfo').'</p>';
1238
    echo '<p><a href="../../documentation/license.html" target="_blank">'.get_lang('PrintVers').'</a></p>';
1239
    echo '</div>'; ?>
1240
    <div class="row">
1241
        <div class="col-md-12">
1242
            <pre style="overflow: auto; height: 200px; margin-top: 5px;">
1243
                <?php echo api_htmlentities(@file_get_contents(api_get_path(SYS_PATH).'documentation/license.txt')); ?>
1244
            </pre>
1245
            <div class="checkbox">
1246
                <label>
1247
                    <input type="checkbox" name="accept" id="accept_licence" value="1" />
1248
                    <?php echo get_lang('IAccept'); ?>
1249
                </label>
1250
            </div>
1251
        </div>
1252
    </div>
1253
    <div class="row">
1254
        <div class="col-md-12">
1255
            <p class="alert alert-info"><?php echo get_lang('LMSMediaLicense'); ?></p>
1256
        </div>
1257
    </div>
1258
1259
    <!-- Contact information form -->
1260
    <div class="section-parameters">
1261
        <a href="javascript://" class = "advanced_parameters" >
1262
        <span id="img_plus_and_minus">&nbsp;<img src="<?php echo api_get_path(WEB_IMG_PATH); ?>div_hide.gif" alt="<?php echo get_lang('Hide'); ?>" title="<?php echo get_lang('Hide'); ?>" style ="vertical-align:middle" />&nbsp;<?php echo get_lang('ContactInformation'); ?></span>
1263
        </a>
1264
    </div>
1265
1266
    <div id="id_contact_form" style="display:block">
1267
        <div class="normal-message"><?php echo get_lang('ContactInformationDescription'); ?></div>
1268
        <div id="contact_registration">
1269
            <p><?php echo get_contact_registration_form(); ?></p><br />
1270
        </div>
1271
    </div>
1272
    <div class="text-center">
1273
    <button type="submit" class="btn btn-default" name="step1" value="&lt; <?php echo get_lang('Previous'); ?>" >
1274
        <em class="fa fa-backward"> </em> <?php echo get_lang('Previous'); ?>
1275
    </button>
1276
    <input type="hidden" name="is_executable" id="is_executable" value="-" />
1277
    <button type="submit" id="license-next" class="btn btn-success" name="step3" onclick="javascript: if(!document.getElementById('accept_licence').checked) { alert('<?php echo get_lang('YouMustAcceptLicence'); ?>');return false;}" value="<?php echo get_lang('Next'); ?> &gt;" >
1278
        <em class="fa fa-forward"> </em> <?php echo get_lang('Next'); ?>
1279
    </button>
1280
    </div>
1281
    <?php
1282
}
1283
1284
/**
1285
 * Get contact registration form.
1286
 */
1287
function get_contact_registration_form()
1288
{
1289
    $html = '
1290
   <div class="form-horizontal">
1291
    <div class="panel panel-default">
1292
    <div class="panel-body">
1293
    <div id="div_sent_information"></div>
1294
    <div class="form-group">
1295
            <label class="col-sm-3"><span class="form_required">*</span>'.get_lang('Name').'</label>
1296
            <div class="col-sm-9"><input id="person_name" class="form-control" type="text" name="person_name" size="30" /></div>
1297
    </div>
1298
    <div class="form-group">
1299
            <label class="col-sm-3"><span class="form_required">*</span>'.get_lang('Email').'</label>
1300
            <div class="col-sm-9"><input id="person_email" class="form-control" type="text" name="person_email" size="30" /></div>
1301
    </div>
1302
    <div class="form-group">
1303
            <label class="col-sm-3"><span class="form_required">*</span>'.get_lang('CompanyName').'</label>
1304
            <div class="col-sm-9"><input id="company_name" class="form-control" type="text" name="company_name" size="30" /></div>
1305
    </div>
1306
    <div class="form-group">
1307
        <label class="col-sm-3"><span class="form_required">*</span>'.get_lang('CompanyActivity').'</label>
1308
        <div class="col-sm-9">
1309
            <select class="selectpicker show-tick" name="company_activity" id="company_activity" >
1310
                <option value="">--- '.get_lang('SelectOne').' ---</option>
1311
                <Option value="Advertising/Marketing/PR">Advertising/Marketing/PR</Option><Option value="Agriculture/Forestry">Agriculture/Forestry</Option>
1312
                <Option value="Architecture">Architecture</Option><Option value="Banking/Finance">Banking/Finance</Option>
1313
                <Option value="Biotech/Pharmaceuticals">Biotech/Pharmaceuticals</Option><Option value="Business Equipment">Business Equipment</Option>
1314
                <Option value="Business Services">Business Services</Option><Option value="Construction">Construction</Option>
1315
                <Option value="Consulting/Research">Consulting/Research</Option><Option value="Education">Education</Option>
1316
                <Option value="Engineering">Engineering</Option><Option value="Environmental">Environmental</Option>
1317
                <Option value="Government">Government</Option><Option value="Healthcare">Health Care</Option>
1318
                <Option value="Hospitality/Lodging/Travel">Hospitality/Lodging/Travel</Option><Option value="Insurance">Insurance</Option>
1319
                <Option value="Legal">Legal</Option><Option value="Manufacturing">Manufacturing</Option>
1320
                <Option value="Media/Entertainment">Media/Entertainment</Option><Option value="Mortgage">Mortgage</Option>
1321
                <Option value="Non-Profit">Non-Profit</Option><Option value="Real Estate">Real Estate</Option>
1322
                <Option value="Restaurant">Restaurant</Option><Option value="Retail">Retail</Option>
1323
                <Option value="Shipping/Transportation">Shipping/Transportation</Option>
1324
                <Option value="Technology">Technology</Option><Option value="Telecommunications">Telecommunications</Option>
1325
                <Option value="Other">Other</Option>
1326
            </select>
1327
        </div>
1328
    </div>
1329
1330
    <div class="form-group">
1331
        <label class="col-sm-3"><span class="form_required">*</span>'.get_lang('PersonRole').'</label>
1332
        <div class="col-sm-9">
1333
            <select class="selectpicker show-tick" name="person_role" id="person_role" >
1334
                <option value="">--- '.get_lang('SelectOne').' ---</option>
1335
                <Option value="Administration">Administration</Option><Option value="CEO/President/ Owner">CEO/President/ Owner</Option>
1336
                <Option value="CFO">CFO</Option><Option value="CIO/CTO">CIO/CTO</Option>
1337
                <Option value="Consultant">Consultant</Option><Option value="Customer Service">Customer Service</Option>
1338
                <Option value="Engineer/Programmer">Engineer/Programmer</Option><Option value="Facilities/Operations">Facilities/Operations</Option>
1339
                <Option value="Finance/ Accounting Manager">Finance/ Accounting Manager</Option><Option value="Finance/ Accounting Staff">Finance/ Accounting Staff</Option>
1340
                <Option value="General Manager">General Manager</Option><Option value="Human Resources">Human Resources</Option>
1341
                <Option value="IS/IT Management">IS/IT Management</Option><Option value="IS/ IT Staff">IS/ IT Staff</Option>
1342
                <Option value="Marketing Manager">Marketing Manager</Option><Option value="Marketing Staff">Marketing Staff</Option>
1343
                <Option value="Partner/Principal">Partner/Principal</Option><Option value="Purchasing Manager">Purchasing Manager</Option>
1344
                <Option value="Sales/ Business Dev. Manager">Sales/ Business Dev. Manager</Option><Option value="Sales/ Business Dev.">Sales/ Business Dev.</Option>
1345
                <Option value="Vice President/Senior Manager">Vice President/Senior Manager</Option><Option value="Other">Other</Option>
1346
            </select>
1347
        </div>
1348
    </div>
1349
1350
    <div class="form-group">
1351
        <label class="col-sm-3"><span class="form_required">*</span>'.get_lang('CompanyCountry').'</label>
1352
        <div class="col-sm-9">'.get_countries_list_from_array(true).'</div>
1353
    </div>
1354
    <div class="form-group">
1355
        <label class="col-sm-3">'.get_lang('CompanyCity').'</label>
1356
        <div class="col-sm-9">
1357
                <input type="text" class="form-control" id="company_city" name="company_city" size="30" />
1358
        </div>
1359
    </div>
1360
    <div class="form-group">
1361
        <label class="col-sm-3">'.get_lang('WhichLanguageWouldYouLikeToUseWhenContactingYou').'</label>
1362
        <div class="col-sm-9">
1363
            <select class="selectpicker show-tick" id="language" name="language">
1364
                <option value="bulgarian">Bulgarian</option>
1365
                <option value="indonesian">Bahasa Indonesia</option>
1366
                <option value="bosnian">Bosanski</option>
1367
                <option value="german">Deutsch</option>
1368
                <option selected="selected" value="english">English</option>
1369
                <option value="spanish">Spanish</option>
1370
                <option value="french">Français</option>
1371
                <option value="italian">Italian</option>
1372
                <option value="hungarian">Magyar</option>
1373
                <option value="dutch">Nederlands</option>
1374
                <option value="brazilian">Português do Brasil</option>
1375
                <option value="portuguese">Português europeu</option>
1376
                <option value="slovenian">Slovenčina</option>
1377
            </select>
1378
        </div>
1379
    </div>
1380
1381
    <div class="form-group">
1382
        <label class="col-sm-3">'.get_lang('HaveYouThePowerToTakeFinancialDecisions').'</label>
1383
        <div class="col-sm-9">
1384
            <div class="radio">
1385
                <label>
1386
                    <input type="radio" name="financial_decision" id="financial_decision1" value="1" checked /> '.get_lang('Yes').'
1387
                </label>
1388
            </div>
1389
            <div class="radio">
1390
                <label>
1391
                    <input type="radio" name="financial_decision" id="financial_decision2" value="0" /> '.get_lang('No').'
1392
                </label>
1393
            </div>
1394
        </div>
1395
    </div>
1396
    <div class="clear"></div>
1397
    <div class="form-group">
1398
            <div class="col-sm-3">&nbsp;</div>
1399
            <div class="col-sm-9"><button type="button" class="btn btn-default" onclick="javascript:send_contact_information();" value="'.get_lang('SendInformation').'" ><em class="fa fa-floppy-o"></em> '.get_lang('SendInformation').'</button> <span id="loader-button"></span></div>
1400
    </div>
1401
    <div class="form-group">
1402
            <div class="col-sm-3">&nbsp;</div>
1403
            <div class="col-sm-9"><span class="form_required">*</span><small>'.get_lang('FieldRequired').'</small></div>
1404
    </div></div></div>
1405
    </div>';
1406
1407
    return $html;
1408
}
1409
1410
/**
1411
 * Displays a parameter in a table row.
1412
 * Used by the display_database_settings_form function.
1413
 *
1414
 * @param   string  Type of install
1415
 * @param   string  Name of parameter
1416
 * @param   string  Field name (in the HTML form)
1417
 * @param   string  Field value
1418
 * @param   string  Extra notice (to show on the right side)
1419
 * @param   bool Whether to display in update mode
1420
 * @param   string  Additional attribute for the <tr> element
1421
 */
1422
function displayDatabaseParameter(
1423
    $installType,
1424
    $parameterName,
1425
    $formFieldName,
1426
    $parameterValue,
1427
    $extra_notice,
1428
    $displayWhenUpdate = true,
1429
    $tr_attribute = ''
1430
) {
1431
    //echo "<tr ".$tr_attribute.">";
1432
    echo "<label class='col-sm-4'>$parameterName</label>";
1433
1434
    if ($installType == INSTALL_TYPE_UPDATE && $displayWhenUpdate) {
1435
        echo '<input type="hidden" name="'.$formFieldName.'" id="'.$formFieldName.'" value="'.api_htmlentities($parameterValue).'" />'.$parameterValue;
1436
    } else {
1437
        $inputType = $formFieldName == 'dbPassForm' ? 'password' : 'text';
1438
1439
        //Slightly limit the length of the database prefix to avoid having to cut down the databases names later on
1440
        $maxLength = $formFieldName == 'dbPrefixForm' ? '15' : MAX_FORM_FIELD_LENGTH;
1441
        if ($installType == INSTALL_TYPE_UPDATE) {
1442
            echo '<input type="hidden" name="'.$formFieldName.'" id="'.$formFieldName.'" value="'.api_htmlentities($parameterValue).'" />';
1443
            echo api_htmlentities($parameterValue);
1444
        } else {
1445
            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>";
1446
            echo '<div class="col-sm-3">'.$extra_notice.'</div>';
1447
        }
1448
    }
1449
}
1450
1451
/**
1452
 * Displays step 3 - a form where the user can enter the installation settings
1453
 * regarding the databases - login and password, names, prefixes, single
1454
 * or multiple databases, tracking or not...
1455
 *
1456
 * @param string $installType
1457
 * @param string $dbHostForm
1458
 * @param string $dbUsernameForm
1459
 * @param string $dbPassForm
1460
 * @param string $dbNameForm
1461
 * @param int    $dbPortForm
1462
 * @param string $installationProfile
1463
 */
1464
function display_database_settings_form(
1465
    $installType,
1466
    $dbHostForm,
1467
    $dbUsernameForm,
1468
    $dbPassForm,
1469
    $dbNameForm,
1470
    $dbPortForm = 3306,
1471
    $installationProfile = ''
1472
) {
1473
    if ($installType == 'update') {
1474
        global $_configuration;
1475
        $dbHostForm = $_configuration['db_host'];
1476
        $dbUsernameForm = $_configuration['db_user'];
1477
        $dbPassForm = $_configuration['db_password'];
1478
        $dbNameForm = $_configuration['main_database'];
1479
        $dbPortForm = isset($_configuration['db_port']) ? $_configuration['db_port'] : '';
1480
1481
        echo '<div class="RequirementHeading"><h2>'.display_step_sequence().get_lang('DBSetting').'</h2></div>';
1482
        echo '<div class="RequirementContent">';
1483
        echo get_lang('DBSettingUpgradeIntro');
1484
        echo '</div>';
1485
    } else {
1486
        echo '<div class="RequirementHeading"><h2>'.display_step_sequence().get_lang('DBSetting').'</h2></div>';
1487
        echo '<div class="RequirementContent">';
1488
        echo get_lang('DBSettingIntro');
1489
        echo '</div>';
1490
    } ?>
1491
    <div class="panel panel-default">
1492
        <div class="panel-body">
1493
        <div class="form-group">
1494
            <label class="col-sm-4"><?php echo get_lang('DBHost'); ?> </label>
1495
            <?php if ($installType == 'update') {
1496
        ?>
1497
            <div class="col-sm-5">
1498
                <input type="hidden" name="dbHostForm" value="<?php echo htmlentities($dbHostForm); ?>" /><?php echo $dbHostForm; ?>
1499
            </div>
1500
            <div class="col-sm-3"></div>
1501
            <?php
1502
    } else {
1503
        ?>
1504
            <div class="col-sm-5">
1505
                <input type="text" class="form-control" size="25" maxlength="50" name="dbHostForm" value="<?php echo htmlentities($dbHostForm); ?>" />
1506
            </div>
1507
            <div class="col-sm-3"><?php echo get_lang('EG').' localhost'; ?></div>
1508
            <?php
1509
    } ?>
1510
        </div>
1511
        <div class="form-group">
1512
            <label class="col-sm-4"><?php echo get_lang('DBPort'); ?> </label>
1513
            <?php if ($installType == 'update') {
1514
        ?>
1515
            <div class="col-sm-5">
1516
                <input type="hidden" name="dbPortForm" value="<?php echo htmlentities($dbPortForm); ?>" /><?php echo $dbPortForm; ?>
1517
            </div>
1518
            <div class="col-sm-3"></div>
1519
            <?php
1520
    } else {
1521
        ?>
1522
            <div class="col-sm-5">
1523
                <input type="text" class="form-control" size="25" maxlength="50" name="dbPortForm" value="<?php echo htmlentities($dbPortForm); ?>" />
1524
            </div>
1525
            <div class="col-sm-3"><?php echo get_lang('EG').' 3306'; ?></div>
1526
            <?php
1527
    } ?>
1528
        </div>
1529
        <div class="form-group">
1530
            <?php
1531
                //database user username
1532
                $example_login = get_lang('EG').' root';
1533
    displayDatabaseParameter($installType, get_lang('DBLogin'), 'dbUsernameForm', $dbUsernameForm, $example_login); ?>
1534
        </div>
1535
        <div class="form-group">
1536
            <?php
1537
            //database user password
1538
            $example_password = get_lang('EG').' '.api_generate_password();
1539
    displayDatabaseParameter($installType, get_lang('DBPassword'), 'dbPassForm', $dbPassForm, $example_password); ?>
1540
        </div>
1541
        <div class="form-group">
1542
            <?php
1543
            //Database Name fix replace weird chars
1544
            if ($installType != INSTALL_TYPE_UPDATE) {
1545
                $dbNameForm = str_replace(['-', '*', '$', ' ', '.'], '', $dbNameForm);
1546
            }
1547
1548
    displayDatabaseParameter(
1549
                $installType,
1550
                get_lang('MainDB'),
1551
                'dbNameForm',
1552
                $dbNameForm,
1553
                '&nbsp;',
1554
                null,
1555
                'id="optional_param1"'
1556
                ); ?>
1557
        </div>
1558
       <?php if ($installType != INSTALL_TYPE_UPDATE) {
1559
                    ?>
1560
        <div class="form-group">
1561
            <div class="col-sm-3"></div>
1562
            <div class="col-sm-9">
1563
            <button type="submit" class="btn btn-primary" name="step3" value="step3">
1564
                <em class="fa fa-refresh"> </em>
1565
                <?php echo get_lang('CheckDatabaseConnection'); ?>
1566
            </button>
1567
            </div>
1568
        </div>
1569
        <?php
1570
                } ?>
1571
1572
        </div>
1573
    </div>
1574
    <?php
1575
        $database_exists_text = '';
1576
    $manager = null;
1577
    try {
1578
        $manager = connectToDatabase(
1579
                $dbHostForm,
1580
                $dbUsernameForm,
1581
                $dbPassForm,
1582
                null,
1583
                $dbPortForm
1584
            );
1585
        $databases = $manager->getConnection()->getSchemaManager()->listDatabases();
1586
        if (in_array($dbNameForm, $databases)) {
1587
            $database_exists_text = '<div class="alert alert-warning">'.get_lang('ADatabaseWithTheSameNameAlreadyExists').'</div>';
1588
        }
1589
    } catch (Exception $e) {
1590
        $database_exists_text = $e->getMessage();
1591
    }
1592
1593
    if ($manager && $manager->getConnection()->isConnected()) { ?>
1594
        <?php echo $database_exists_text; ?>
1595
        <div id="db_status" class="alert alert-success">
1596
            Database host: <strong><?php echo $manager->getConnection()->getHost(); ?></strong><br/>
1597
            Database port: <strong><?php echo $manager->getConnection()->getPort(); ?></strong><br/>
1598
            Database driver: <strong><?php echo $manager->getConnection()->getDriver()->getName(); ?></strong><br/>
1599
        </div>
1600
    <?php } else { ?>
1601
        <div id="db_status" class="alert alert-danger">
1602
            <p><?php echo get_lang('FailedConectionDatabase'); ?></strong></p>
1603
            <code><?php echo $database_exists_text; ?></code>
1604
        </div>
1605
    <?php } ?>
1606
   <div class="form-group">
1607
       <div class="col-sm-6">
1608
           <button type="submit" name="step2" class="btn btn-default pull-right" value="&lt; <?php echo get_lang('Previous'); ?>" >
1609
               <em class="fa fa-backward"> </em> <?php echo get_lang('Previous'); ?>
1610
           </button>
1611
       </div>
1612
      <div class="col-sm-6">
1613
       <input type="hidden" name="is_executable" id="is_executable" value="-" />
1614
       <?php if ($manager) {
0 ignored issues
show
$manager is of type Doctrine\ORM\EntityManager, thus it always evaluated to true.
Loading history...
1615
        ?>
1616
           <button type="submit"  class="btn btn-success" name="step4" value="<?php echo get_lang('Next'); ?> &gt;" >
1617
               <em class="fa fa-forward"> </em> <?php echo get_lang('Next'); ?>
1618
           </button>
1619
       <?php
1620
    } else {
1621
        ?>
1622
           <button disabled="disabled" type="submit" class="btn btn-success disabled" name="step4" value="<?php echo get_lang('Next'); ?> &gt;" >
1623
               <em class="fa fa-forward"> </em> <?php echo get_lang('Next'); ?>
1624
           </button>
1625
       <?php
1626
    } ?>
1627
      </div>
1628
   </div>
1629
1630
    <?php
1631
}
1632
1633
function panel($content = null, $title = null, $id = null, $style = null)
1634
{
1635
    $html = '';
1636
    if (empty($style)) {
1637
        $style = 'default';
1638
    }
1639
    if (!empty($title)) {
1640
        $panelTitle = Display::div($title, ['class' => 'panel-heading']);
1641
        $panelBody = Display::div($content, ['class' => 'panel-body']);
1642
        $panelParent = Display::div($panelTitle.$panelBody, ['id' => $id, 'class' => 'panel panel-'.$style]);
1643
    } else {
1644
        $panelBody = Display::div($html, ['class' => 'panel-body']);
1645
        $panelParent = Display::div($panelBody, ['id' => $id, 'class' => 'panel panel-'.$style]);
1646
    }
1647
    $html .= $panelParent;
1648
1649
    return $html;
1650
}
1651
1652
/**
1653
 * Displays a parameter in a table row.
1654
 * Used by the display_configuration_settings_form function.
1655
 *
1656
 * @param string $installType
1657
 * @param string $parameterName
1658
 * @param string $formFieldName
1659
 * @param string $parameterValue
1660
 * @param string $displayWhenUpdate
1661
 *
1662
 * @return string
1663
 */
1664
function display_configuration_parameter(
1665
    $installType,
1666
    $parameterName,
1667
    $formFieldName,
1668
    $parameterValue,
1669
    $displayWhenUpdate = 'true'
1670
) {
1671
    $html = '<div class="form-group">';
1672
    $html .= '<label class="col-sm-6 control-label">'.$parameterName.'</label>';
1673
    if ($installType == INSTALL_TYPE_UPDATE && $displayWhenUpdate) {
1674
        $html .= Display::input(
1675
            'hidden',
1676
            $formFieldName,
1677
            api_htmlentities($parameterValue, ENT_QUOTES)
1678
        ).$parameterValue;
1679
    } else {
1680
        $hiddenPasswordClass = '';
1681
        $eyeForPassword = '';
1682
        $inputType = 'text';
1683
        if ($formFieldName == 'passForm') {
1684
            /* show/hide admin password in step 5*/
1685
            $hiddenPasswordClass = 'inputShowPwd';
1686
            $inputType = 'password';
1687
            $eyeForPassword = PHP_EOL
1688
                .'<input type="checkbox" id="showPassword" class="hidden">'
1689
                .'<label for="showPassword" style="cursor: pointer;">'
1690
                .Display::returnFontAwesomeIcon('eye', null, true, 'showPasswordEye')
1691
                .'</label> ';
1692
        }
1693
        $html .= '<div class="col-sm-6 '.$hiddenPasswordClass.'">'
1694
            .Display::input(
1695
                $inputType,
1696
                $formFieldName,
1697
                api_htmlentities($parameterValue, ENT_QUOTES),
1698
                [
1699
                    'class' => 'form-control',
1700
                    'size' => FORM_FIELD_DISPLAY_LENGTH,
1701
                    'maxlength' => MAX_FORM_FIELD_LENGTH,
1702
                ]
1703
            )
1704
            .$eyeForPassword."</div>";
1705
    }
1706
    $html .= "</div>";
1707
1708
    return $html;
1709
}
1710
1711
/**
1712
 * Displays step 4 of the installation - configuration settings about Chamilo itself.
1713
 *
1714
 * @param string $installType
1715
 * @param string $urlForm
1716
 * @param string $languageForm
1717
 * @param string $emailForm
1718
 * @param string $adminFirstName
1719
 * @param string $adminLastName
1720
 * @param string $adminPhoneForm
1721
 * @param string $campusForm
1722
 * @param string $institutionForm
1723
 * @param string $institutionUrlForm
1724
 * @param string $encryptPassForm
1725
 * @param bool   $allowSelfReg
1726
 * @param bool   $allowSelfRegProf
1727
 * @param string $loginForm
1728
 * @param string $passForm
1729
 */
1730
function display_configuration_settings_form(
1731
    $installType,
1732
    $urlForm,
1733
    $languageForm,
1734
    $emailForm,
1735
    $adminFirstName,
1736
    $adminLastName,
1737
    $adminPhoneForm,
1738
    $campusForm,
1739
    $institutionForm,
1740
    $institutionUrlForm,
1741
    $encryptPassForm,
1742
    $allowSelfReg,
1743
    $allowSelfRegProf,
1744
    $loginForm,
1745
    $passForm
1746
) {
1747
    if ($installType != 'update' && empty($languageForm)) {
1748
        $languageForm = $_SESSION['install_language'];
1749
    }
1750
    echo '<div class="RequirementHeading">';
1751
    echo "<h2>".display_step_sequence().get_lang("CfgSetting")."</h2>";
1752
    echo '</div>';
1753
1754
    echo '<p>'.get_lang('ConfigSettingsInfo').' <strong>app/config/configuration.php</strong></p>';
1755
1756
    // Parameter 1: administrator's login
1757
    $html = '';
1758
    $html .= display_configuration_parameter(
1759
        $installType,
1760
        get_lang('AdminLogin'),
1761
        'loginForm',
1762
        $loginForm,
1763
        $installType == 'update'
1764
    );
1765
1766
    // Parameter 2: administrator's password
1767
    if ($installType != 'update') {
1768
        $html .= display_configuration_parameter($installType, get_lang('AdminPass'), 'passForm', $passForm, false);
1769
    }
1770
1771
    // Parameters 3 and 4: administrator's names
1772
1773
    $html .= display_configuration_parameter(
1774
        $installType,
1775
        get_lang('AdminFirstName'),
1776
        'adminFirstName',
1777
        $adminFirstName
1778
    );
1779
    $html .= display_configuration_parameter($installType, get_lang('AdminLastName'), 'adminLastName', $adminLastName);
1780
1781
    //Parameter 3: administrator's email
1782
    $html .= display_configuration_parameter($installType, get_lang('AdminEmail'), 'emailForm', $emailForm);
1783
1784
    //Parameter 6: administrator's telephone
1785
    $html .= display_configuration_parameter($installType, get_lang('AdminPhone'), 'adminPhoneForm', $adminPhoneForm);
1786
    echo panel($html, get_lang('Administrator'), 'administrator');
1787
1788
    //First parameter: language
1789
    $html = '<div class="form-group">';
1790
    $html .= '<label class="col-sm-6 control-label">'.get_lang('MainLang')."</label>";
1791
    if ($installType == 'update') {
1792
        $html .= '<input type="hidden" name="languageForm" value="'.api_htmlentities($languageForm, ENT_QUOTES).'" />'.$languageForm;
1793
    } else { // new installation
1794
        $html .= '<div class="col-sm-6">';
1795
        $html .= display_language_selection_box('languageForm', $languageForm);
1796
        $html .= '</div>';
1797
    }
1798
    $html .= "</div>";
1799
1800
    //Second parameter: Chamilo URL
1801
    $html .= '<div class="form-group">';
1802
    $html .= '<label class="col-sm-6 control-label"><span class="form_required">*</span>'.get_lang('ChamiloURL').'</label>';
1803
1804
    if ($installType == 'update') {
1805
        $html .= api_htmlentities($urlForm, ENT_QUOTES)."\n";
1806
    } else {
1807
        $html .= '<div class="col-sm-6">';
1808
        $html .= '<input class="form-control" type="text" size="40" maxlength="100" name="urlForm" value="'.api_htmlentities($urlForm, ENT_QUOTES).'" />';
1809
        $html .= '</div>';
1810
    }
1811
    $html .= '</div>';
1812
1813
    //Parameter 9: campus name
1814
    $html .= display_configuration_parameter(
1815
        $installType,
1816
        get_lang('CampusName'),
1817
        'campusForm',
1818
        $campusForm
1819
    );
1820
1821
    //Parameter 10: institute (short) name
1822
    $html .= display_configuration_parameter(
1823
        $installType,
1824
        get_lang('InstituteShortName'),
1825
        'institutionForm',
1826
        $institutionForm
1827
    );
1828
1829
    //Parameter 11: institute (short) name
1830
    $html .= display_configuration_parameter(
1831
        $installType,
1832
        get_lang('InstituteURL'),
1833
        'institutionUrlForm',
1834
        $institutionUrlForm
1835
    );
1836
1837
    $html .= '<div class="form-group">
1838
            <label class="col-sm-6 control-label">'.get_lang("EncryptMethodUserPass").'</label>
1839
        <div class="col-sm-6">';
1840
    if ($installType == 'update') {
1841
        $html .= '<input type="hidden" name="encryptPassForm" value="'.$encryptPassForm.'" />'.$encryptPassForm;
1842
    } else {
1843
        $html .= '<div class="checkbox">
1844
                    <label>
1845
                        <input  type="radio" name="encryptPassForm" value="bcrypt" id="encryptPass1" '.($encryptPassForm == 'bcrypt' ? 'checked="checked" ' : '').'/> bcrypt
1846
                    </label>';
1847
1848
        $html .= '<label>
1849
                        <input  type="radio" name="encryptPassForm" value="sha1" id="encryptPass1" '.($encryptPassForm == 'sha1' ? 'checked="checked" ' : '').'/> sha1
1850
                    </label>';
1851
1852
        $html .= '<label>
1853
                        <input type="radio" name="encryptPassForm" value="md5" id="encryptPass0" '.($encryptPassForm == 'md5' ? 'checked="checked" ' : '').'/> md5
1854
                    </label>';
1855
1856
        $html .= '<label>
1857
                        <input type="radio" name="encryptPassForm" value="none" id="encryptPass2" '.($encryptPassForm == 'none' ? 'checked="checked" ' : '').'/>'.get_lang('None').'
1858
                    </label>';
1859
        $html .= '</div>';
1860
    }
1861
    $html .= '</div></div>';
1862
1863
    $html .= '<div class="form-group">
1864
            <label class="col-sm-6 control-label">'.get_lang('AllowSelfReg').'</label>
1865
            <div class="col-sm-6">';
1866
    if ($installType == 'update') {
1867
        if ($allowSelfReg == 'true') {
1868
            $label = get_lang('Yes');
1869
        } elseif ($allowSelfReg == 'false') {
1870
            $label = get_lang('No');
1871
        } else {
1872
            $label = get_lang('AfterApproval');
1873
        }
1874
        $html .= '<input type="hidden" name="allowSelfReg" value="'.$allowSelfReg.'" />'.$label;
1875
    } else {
1876
        $html .= '<div class="control-group">';
1877
        $html .= '<label class="checkbox-inline">
1878
                        <input type="radio" name="allowSelfReg" value="true" id="allowSelfReg1" '.($allowSelfReg == 'true' ? 'checked="checked" ' : '').' /> '.get_lang('Yes').'
1879
                    </label>';
1880
        $html .= '<label class="checkbox-inline">
1881
                        <input type="radio" name="allowSelfReg" value="false" id="allowSelfReg0" '.($allowSelfReg == 'false' ? '' : 'checked="checked" ').' /> '.get_lang('No').'
1882
                    </label>';
1883
        $html .= '<label class="checkbox-inline">
1884
                    <input type="radio" name="allowSelfReg" value="approval" id="allowSelfReg2" '.($allowSelfReg == 'approval' ? '' : 'checked="checked" ').' /> '.get_lang('AfterApproval').'
1885
                </label>';
1886
        $html .= '</div>';
1887
    }
1888
    $html .= '</div>';
1889
    $html .= '</div>';
1890
1891
    $html .= '<div class="form-group">';
1892
    $html .= '<label class="col-sm-6 control-label">'.get_lang('AllowSelfRegProf').'</label>
1893
        <div class="col-sm-6">';
1894
    if ($installType == 'update') {
1895
        if ($allowSelfRegProf == 'true') {
1896
            $label = get_lang('Yes');
1897
        } else {
1898
            $label = get_lang('No');
1899
        }
1900
        $html .= '<input type="hidden" name="allowSelfRegProf" value="'.$allowSelfRegProf.'" />'.$label;
1901
    } else {
1902
        $html .= '<div class="control-group">
1903
                <label class="checkbox-inline">
1904
                    <input type="radio" name="allowSelfRegProf" value="1" id="allowSelfRegProf1" '.($allowSelfRegProf ? 'checked="checked" ' : '').'/>
1905
                '.get_lang('Yes').'
1906
                </label>';
1907
        $html .= '<label class="checkbox-inline">
1908
                    <input type="radio" name="allowSelfRegProf" value="0" id="allowSelfRegProf0" '.($allowSelfRegProf ? '' : 'checked="checked" ').' />
1909
                   '.get_lang('No').'
1910
                </label>';
1911
        $html .= '</div>';
1912
    }
1913
    $html .= '</div>
1914
    </div>';
1915
1916
    echo panel($html, get_lang('Platform'), 'platform'); ?>
1917
    <div class='form-group'>
1918
        <div class="col-sm-6">
1919
            <button type="submit" class="btn btn-default pull-right" name="step3" value="&lt; <?php echo get_lang('Previous'); ?>" ><em class="fa fa-backward"> </em> <?php echo get_lang('Previous'); ?></button>
1920
            <input type="hidden" name="is_executable" id="is_executable" value="-" />
1921
        </div>
1922
        <div class="col-sm-6">
1923
            <button class="btn btn-success" type="submit" name="step5" value="<?php echo get_lang('Next'); ?> &gt;" ><em class="fa fa-forward"> </em> <?php echo get_lang('Next'); ?></button>
1924
        </div>
1925
    </div>
1926
1927
    <?php
1928
}
1929
1930
/**
1931
 * After installation is completed (step 6), this message is displayed.
1932
 *
1933
 * @param string $installType
1934
 */
1935
function display_after_install_message($installType)
1936
{
1937
    echo '<div class="RequirementContent">'.get_lang('FirstUseTip').'</div>';
1938
    echo '<div class="alert alert-warning">';
1939
    echo '<strong>'.get_lang('SecurityAdvice').'</strong>';
1940
    echo ': ';
1941
    printf(get_lang('ToProtectYourSiteMakeXReadOnlyAndDeleteY'), 'app/config/', 'main/install/');
1942
    echo '</div>'; ?></form>
1943
    <br />
1944
    <a class="btn btn-success btn-block" href="../../index.php">
1945
        <?php echo get_lang('GoToYourNewlyCreatedPortal'); ?>
1946
    </a>
1947
    <?php
1948
}
1949
1950
/**
1951
 * This function return countries list from array (hardcoded).
1952
 *
1953
 * @param bool $combo (Optional) True for returning countries list with select html
1954
 *
1955
 * @return array|string countries list
1956
 */
1957
function get_countries_list_from_array($combo = false)
1958
{
1959
    $a_countries = [
1960
        "Afghanistan", "Albania", "Algeria", "Andorra", "Angola", "Antigua and Barbuda", "Argentina", "Armenia", "Australia", "Austria", "Azerbaijan",
1961
        "Bahamas", "Bahrain", "Bangladesh", "Barbados", "Belarus", "Belgium", "Belize", "Benin", "Bhutan", "Bolivia", "Bosnia and Herzegovina", "Botswana", "Brazil", "Brunei", "Bulgaria", "Burkina Faso", "Burundi",
1962
        "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",
1963
        "Denmark", "Djibouti", "Dominica", "Dominican Republic",
1964
        "East Timor (Timor Timur)", "Ecuador", "Egypt", "El Salvador", "Equatorial Guinea", "Eritrea", "Estonia", "Ethiopia",
1965
        "Fiji", "Finland", "France",
1966
        "Gabon", "Gambia, The", "Georgia", "Germany", "Ghana", "Greece", "Grenada", "Guatemala", "Guinea", "Guinea-Bissau", "Guyana",
1967
        "Haiti", "Honduras", "Hungary",
1968
        "Iceland", "India", "Indonesia", "Iran", "Iraq", "Ireland", "Israel", "Italy",
1969
        "Jamaica", "Japan", "Jordan",
1970
        "Kazakhstan", "Kenya", "Kiribati", "Korea, North", "Korea, South", "Kuwait", "Kyrgyzstan",
1971
        "Laos", "Latvia", "Lebanon", "Lesotho", "Liberia", "Libya", "Liechtenstein", "Lithuania", "Luxembourg",
1972
        "Macedonia", "Madagascar", "Malawi", "Malaysia", "Maldives", "Mali", "Malta", "Marshall Islands", "Mauritania", "Mauritius", "Mexico", "Micronesia", "Moldova", "Monaco", "Mongolia", "Morocco", "Mozambique", "Myanmar",
1973
        "Namibia", "Nauru", "Nepa", "Netherlands", "New Zealand", "Nicaragua", "Niger", "Nigeria", "Norway",
1974
        "Oman",
1975
        "Pakistan", "Palau", "Panama", "Papua New Guinea", "Paraguay", "Peru", "Philippines", "Poland", "Portugal",
1976
        "Qatar",
1977
        "Romania", "Russia", "Rwanda",
1978
        "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",
1979
        "Taiwan", "Tajikistan", "Tanzania", "Thailand", "Togo", "Tonga", "Trinidad and Tobago", "Tunisia", "Turkey", "Turkmenistan", "Tuvalu",
1980
        "Uganda", "Ukraine", "United Arab Emirates", "United Kingdom", "United States", "Uruguay", "Uzbekistan",
1981
        "Vanuatu", "Vatican City", "Venezuela", "Vietnam",
1982
        "Yemen",
1983
        "Zambia", "Zimbabwe",
1984
    ];
1985
    if ($combo) {
1986
        $country_select = '<select class="selectpicker show-tick" id="country" name="country">';
1987
        $country_select .= '<option value="">--- '.get_lang('SelectOne').' ---</option>';
1988
        foreach ($a_countries as $country) {
1989
            $country_select .= '<option value="'.$country.'">'.$country.'</option>';
1990
        }
1991
        $country_select .= '</select>';
1992
1993
        return $country_select;
1994
    }
1995
1996
    return $a_countries;
1997
}
1998
1999
/**
2000
 * Lock settings that can't be changed in other portals.
2001
 */
2002
function lockSettings()
2003
{
2004
    $access_url_locked_settings = api_get_locked_settings();
2005
    $table = Database::get_main_table(TABLE_MAIN_SETTINGS_CURRENT);
2006
    foreach ($access_url_locked_settings as $setting) {
2007
        Database::update(
2008
            $table,
2009
            ['access_url_locked' => 1],
2010
            ['variable = ?' => $setting]
2011
        );
2012
    }
2013
}
2014
2015
/**
2016
 * Update dir values.
2017
 */
2018
function updateDirAndFilesPermissions()
2019
{
2020
    $table = Database::get_main_table(TABLE_MAIN_SETTINGS_CURRENT);
2021
    $permissions_for_new_directories = isset($_SESSION['permissions_for_new_directories']) ? $_SESSION['permissions_for_new_directories'] : 0770;
2022
    $permissions_for_new_files = isset($_SESSION['permissions_for_new_files']) ? $_SESSION['permissions_for_new_files'] : 0660;
2023
    // use decoct() to store as string
2024
    Database::update(
2025
        $table,
2026
        ['selected_value' => '0'.decoct($permissions_for_new_directories)],
2027
        ['variable = ?' => 'permissions_for_new_directories']
2028
    );
2029
2030
    Database::update(
2031
        $table,
2032
        ['selected_value' => '0'.decoct($permissions_for_new_files)],
2033
        ['variable = ?' => 'permissions_for_new_files']
2034
    );
2035
2036
    if (isset($_SESSION['permissions_for_new_directories'])) {
2037
        unset($_SESSION['permissions_for_new_directories']);
2038
    }
2039
2040
    if (isset($_SESSION['permissions_for_new_files'])) {
2041
        unset($_SESSION['permissions_for_new_files']);
2042
    }
2043
}
2044
2045
/**
2046
 * @param $current_value
2047
 * @param $wanted_value
2048
 *
2049
 * @return string
2050
 */
2051
function compare_setting_values($current_value, $wanted_value)
2052
{
2053
    $current_value_string = $current_value;
2054
    $current_value = (float) $current_value;
2055
    $wanted_value = (float) $wanted_value;
2056
2057
    if ($current_value >= $wanted_value) {
2058
        return Display::label($current_value_string, 'success');
2059
    } else {
2060
        return Display::label($current_value_string, 'important');
2061
    }
2062
}
2063
2064
/**
2065
 * @param string $course_dir
2066
 * @param string $course_attempt_name
2067
 * @param string $file
2068
 *
2069
 * @return bool
2070
 */
2071
function checkCourseScriptCreation(
2072
    $course_dir,
2073
    $course_attempt_name,
2074
    $file
2075
) {
2076
    $output = false;
2077
    // Write in file
2078
    $file_name = $course_dir.'/'.$file;
2079
    $content = '123';
2080
2081
    if (is_writable($file_name)) {
2082
        if ($handler = @fopen($file_name, 'w')) {
2083
            //write content
2084
            if (fwrite($handler, $content)) {
2085
                $sock_errno = '';
2086
                $sock_errmsg = '';
2087
                $url = api_get_path(WEB_PATH).'app/courses/'.$course_attempt_name.'/'.$file;
2088
2089
                $parsed_url = parse_url($url);
2090
                //$scheme = isset($parsedUrl['scheme']) ? $parsedUrl['scheme'] : ''; //http
2091
                $host = isset($parsed_url['host']) ? $parsed_url['host'] : '';
2092
                // Patch if the host is the default host and is used through
2093
                // the IP address (sometimes the host is not taken correctly
2094
                // in this case)
2095
                if (empty($host) && !empty($_SERVER['HTTP_HOST'])) {
2096
                    $host = $_SERVER['HTTP_HOST'];
2097
                    $url = preg_replace('#:///#', '://'.$host.'/', $url);
2098
                }
2099
                $path = isset($parsed_url['path']) ? $parsed_url['path'] : '/';
2100
                $port = '';
2101
                $scheme = '';
2102
                switch ($parsed_url['scheme']) {
2103
                    case 'https':
2104
                        $scheme = 'ssl://';
2105
                        $port = 443;
2106
                        break;
2107
                    case 'http':
2108
                    default:
2109
                        $scheme = '';
2110
                        $port = 80;
2111
                }
2112
2113
                //Check fsockopen (not sure it works with https). If that is your case, you might want to try the
2114
                // suggestion at https://support.chamilo.org/issues/8260#note-3 (although it ignores SSL peer checks)
2115
                if ($fp = @fsockopen(str_replace('http://', $scheme, $url), $port, $sock_errno, $sock_errmsg, 60)) {
2116
                    $out = "GET $path HTTP/1.1\r\n";
2117
                    $out .= "Host: $host\r\n";
2118
                    $out .= "Connection: Close\r\n\r\n";
2119
2120
                    fwrite($fp, $out);
2121
                    while (!feof($fp)) {
2122
                        $result = str_replace("\r\n", '', fgets($fp, 128));
2123
                        if (!empty($result) && $result == '123') {
2124
                            $output = true;
2125
                        }
2126
                    }
2127
                    fclose($fp);
2128
                } elseif (ini_get('allow_url_fopen')) {
2129
                    // Check allow_url_fopen
2130
                    if ($fp = @fopen($url, 'r')) {
2131
                        while ($result = fgets($fp, 1024)) {
2132
                            if (!empty($result) && $result == '123') {
2133
                                $output = true;
2134
                            }
2135
                        }
2136
                        fclose($fp);
2137
                    }
2138
                } elseif (function_exists('curl_init')) {
2139
                    // Check if has support for cURL
2140
                    $ch = curl_init();
2141
                    curl_setopt($ch, CURLOPT_HEADER, 0);
2142
                    curl_setopt($ch, CURLOPT_URL, $url);
2143
                    //curl_setopt($ch, CURLOPT_TIMEOUT, 30);
2144
                    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
2145
                    $result = curl_exec($ch);
2146
                    if (!empty($result) && $result == '123') {
2147
                        $output = true;
2148
                    }
2149
                    curl_close($ch);
2150
                }
2151
            }
2152
            @fclose($handler);
2153
        }
2154
    }
2155
2156
    return $output;
2157
}
2158
2159
/**
2160
 * Save settings values.
2161
 *
2162
 * @param string $organizationName
2163
 * @param string $organizationUrl
2164
 * @param string $siteName
2165
 * @param string $adminEmail
2166
 * @param string $adminLastName
2167
 * @param string $adminFirstName
2168
 * @param string $language
2169
 * @param string $allowRegistration
2170
 * @param string $allowTeacherSelfRegistration
2171
 * @param string $installationProfile          The name of an installation profile file in main/install/profiles/
2172
 */
2173
function installSettings(
2174
    $organizationName,
2175
    $organizationUrl,
2176
    $siteName,
2177
    $adminEmail,
2178
    $adminLastName,
2179
    $adminFirstName,
2180
    $language,
2181
    $allowRegistration,
2182
    $allowTeacherSelfRegistration,
2183
    $installationProfile = ''
2184
) {
2185
    $allowTeacherSelfRegistration = $allowTeacherSelfRegistration ? 'true' : 'false';
2186
2187
    // Use PHP 5.3 to avoid issue with weird peripherical auto-installers like travis-ci
2188
    $settings = [
2189
        'Institution' => $organizationName,
2190
        'InstitutionUrl' => $organizationUrl,
2191
        'siteName' => $siteName,
2192
        'emailAdministrator' => $adminEmail,
2193
        'administratorSurname' => $adminLastName,
2194
        'administratorName' => $adminFirstName,
2195
        'platformLanguage' => $language,
2196
        'allow_registration' => $allowRegistration,
2197
        'allow_registration_as_teacher' => $allowTeacherSelfRegistration,
2198
    ];
2199
2200
    $tblSettings = Database::get_main_table(TABLE_MAIN_SETTINGS_CURRENT);
2201
2202
    foreach ($settings as $variable => $value) {
2203
        Database::update(
2204
            $tblSettings,
2205
            ['selected_value' => $value],
2206
            ['variable = ?' => $variable]
2207
        );
2208
    }
2209
    installProfileSettings($installationProfile);
2210
}
2211
2212
/**
2213
 * Executes DB changes based in the classes defined in
2214
 * src/Chamilo/CoreBundle/Migrations/Schema/*.
2215
 *
2216
 * @param string $chamiloVersion
2217
 *
2218
 * @throws \Doctrine\DBAL\DBALException
2219
 *
2220
 * @return bool
2221
 */
2222
function migrate($chamiloVersion, EntityManager $manager)
2223
{
2224
    $debug = true;
2225
    $connection = $manager->getConnection();
2226
2227
    $config = new \Doctrine\DBAL\Migrations\Configuration\Configuration($connection);
2228
2229
    // Table name that will store migrations log (will be created automatically,
2230
    // default name is: doctrine_migration_versions)
2231
    $config->setMigrationsTableName('version');
2232
    // Namespace of your migration classes, do not forget escape slashes, do not add last slash
2233
    $config->setMigrationsNamespace('Application\Migrations\Schema\V'.$chamiloVersion);
2234
    // Directory where your migrations are located
2235
    $config->setMigrationsDirectory(api_get_path(SYS_PATH).'app/Migrations/Schema/V'.$chamiloVersion);
2236
    // Load your migrations
2237
    $config->registerMigrationsFromDirectory($config->getMigrationsDirectory());
2238
2239
    $migration = new \Doctrine\DBAL\Migrations\Migration($config);
2240
    $versions = $config->getMigrations();
2241
2242
    /** @var Doctrine\DBAL\Migrations\Version $migrationItem */
2243
    foreach ($versions as $version) {
2244
        $version->getMigration()->setEntityManager($manager);
2245
    }
2246
2247
    $to = null; // if $to == null then schema will be migrated to latest version
2248
2249
    echo "<pre>";
2250
    try {
2251
        // Execute migration!
2252
        $migratedSQL = $migration->migrate($to);
2253
2254
        if ($debug) {
2255
            foreach ($migratedSQL as $version => $sqlList) {
2256
                echo "VERSION: $version<br>";
2257
                echo "----------------------------------------------<br>";
2258
                $total = count($sqlList);
2259
                error_log("VERSION: $version");
2260
                error_log("# queries: ".$total);
2261
                $counter = 1;
2262
                foreach ($sqlList as $sql) {
2263
                    echo "<code>$sql</code><br>";
2264
                    error_log("$counter/$total : $sql");
2265
                    $counter++;
2266
                }
2267
            }
2268
2269
            echo "<br>DONE!<br>";
2270
        }
2271
2272
        return true;
2273
    } catch (Exception $ex) {
2274
        if ($debug) {
2275
            echo "ERROR: {$ex->getMessage()}<br>";
2276
2277
            return false;
2278
        }
2279
    }
2280
2281
    echo "</pre>";
2282
2283
    return false;
2284
}
2285
2286
/**
2287
 * @throws \Doctrine\DBAL\DBALException
2288
 */
2289
function fixIds(EntityManager $em)
2290
{
2291
    $connection = $em->getConnection();
2292
    $database = new Database();
2293
    $database->setManager($em);
2294
    $debug = true;
2295
    if ($debug) {
2296
        error_log('fixIds');
2297
    }
2298
2299
    // Create temporary indexes to increase speed of the following operations
2300
    // Adding and removing indexes will usually take much less time than
2301
    // the execution without indexes of the queries in this function, particularly
2302
    // for large tables
2303
    $sql = "ALTER TABLE c_document ADD INDEX tmpidx_doc(c_id, id)";
2304
    $connection->executeQuery($sql);
2305
    $sql = "ALTER TABLE c_student_publication ADD INDEX tmpidx_stud (c_id, id)";
2306
    $connection->executeQuery($sql);
2307
    $sql = "ALTER TABLE c_quiz ADD INDEX tmpidx_quiz (c_id, id)";
2308
    $connection->executeQuery($sql);
2309
    $sql = "ALTER TABLE c_item_property ADD INDEX tmpidx_ip (to_group_id)";
2310
    $connection->executeQuery($sql);
2311
2312
    $sql = "SELECT * FROM c_lp_item";
2313
    $result = $connection->fetchAll($sql);
2314
    foreach ($result as $item) {
2315
        $courseId = $item['c_id'];
2316
        $iid = isset($item['iid']) ? (int) $item['iid'] : 0;
2317
        $ref = isset($item['ref']) ? (int) $item['ref'] : 0;
2318
        $sql = null;
2319
2320
        $newId = '';
2321
        switch ($item['item_type']) {
2322
            case TOOL_LINK:
2323
                $sql = "SELECT * FROM c_link WHERE c_id = $courseId AND id = $ref";
2324
                $data = $connection->fetchAssoc($sql);
2325
                if ($data) {
2326
                    $newId = $data['iid'];
2327
                }
2328
                break;
2329
            case TOOL_STUDENTPUBLICATION:
2330
                $sql = "SELECT * FROM c_student_publication WHERE c_id = $courseId AND id = $ref";
2331
                $data = $connection->fetchAssoc($sql);
2332
                if ($data) {
2333
                    $newId = $data['iid'];
2334
                }
2335
                break;
2336
            case TOOL_QUIZ:
2337
                $sql = "SELECT * FROM c_quiz WHERE c_id = $courseId AND id = $ref";
2338
                $data = $connection->fetchAssoc($sql);
2339
                if ($data) {
2340
                    $newId = $data['iid'];
2341
                }
2342
                break;
2343
            case TOOL_DOCUMENT:
2344
                $sql = "SELECT * FROM c_document WHERE c_id = $courseId AND id = $ref";
2345
                $data = $connection->fetchAssoc($sql);
2346
                if ($data) {
2347
                    $newId = $data['iid'];
2348
                }
2349
                break;
2350
            case TOOL_FORUM:
2351
                $sql = "SELECT * FROM c_forum_forum WHERE c_id = $courseId AND forum_id = $ref";
2352
                $data = $connection->fetchAssoc($sql);
2353
                if ($data) {
2354
                    $newId = $data['iid'];
2355
                }
2356
                break;
2357
            case 'thread':
2358
                $sql = "SELECT * FROM c_forum_thread WHERE c_id = $courseId AND thread_id = $ref";
2359
                $data = $connection->fetchAssoc($sql);
2360
                if ($data) {
2361
                    $newId = $data['iid'];
2362
                }
2363
                break;
2364
        }
2365
2366
        if (!empty($sql) && !empty($newId) && !empty($iid)) {
2367
            $sql = "UPDATE c_lp_item SET ref = $newId WHERE iid = $iid";
2368
            $connection->executeQuery($sql);
2369
        }
2370
    }
2371
2372
    // Set NULL if session = 0
2373
    $sql = "UPDATE c_item_property SET session_id = NULL WHERE session_id = 0";
2374
    $connection->executeQuery($sql);
2375
2376
    // Set NULL if group = 0
2377
    $sql = "UPDATE c_item_property SET to_group_id = NULL WHERE to_group_id = 0";
2378
    $connection->executeQuery($sql);
2379
2380
    // Set NULL if insert_user_id = 0
2381
    $sql = "UPDATE c_item_property SET insert_user_id = NULL WHERE insert_user_id = 0";
2382
    $connection->executeQuery($sql);
2383
2384
    // Delete session data of sessions that don't exist.
2385
    $sql = "DELETE FROM c_item_property
2386
            WHERE session_id IS NOT NULL AND session_id NOT IN (SELECT id FROM session)";
2387
    $connection->executeQuery($sql);
2388
2389
    // Delete group data of groups that don't exist.
2390
    $sql = "DELETE FROM c_item_property
2391
            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)";
2392
    $connection->executeQuery($sql);
2393
2394
    // This updates the group_id with c_group_info.iid instead of c_group_info.id
2395
    if ($debug) {
2396
        error_log('update iids');
2397
    }
2398
2399
    $groupTableToFix = [
2400
        'c_group_rel_user',
2401
        'c_group_rel_tutor',
2402
        'c_permission_group',
2403
        'c_role_group',
2404
        'c_survey_invitation',
2405
        'c_attendance_calendar_rel_group',
2406
    ];
2407
2408
    foreach ($groupTableToFix as $table) {
2409
        $sql = "SELECT * FROM $table";
2410
        $result = $connection->fetchAll($sql);
2411
        foreach ($result as $item) {
2412
            $iid = $item['iid'];
2413
            $courseId = $item['c_id'];
2414
            $groupId = intval($item['group_id']);
2415
2416
            // Fix group id
2417
            if (!empty($groupId)) {
2418
                $sql = "SELECT * FROM c_group_info
2419
                        WHERE c_id = $courseId AND id = $groupId
2420
                        LIMIT 1";
2421
                $data = $connection->fetchAssoc($sql);
2422
                if (!empty($data)) {
2423
                    $newGroupId = $data['iid'];
2424
                    $sql = "UPDATE $table SET group_id = $newGroupId
2425
                            WHERE iid = $iid";
2426
                    $connection->executeQuery($sql);
2427
                } else {
2428
                    // The group does not exists clean this record
2429
                    $sql = "DELETE FROM $table WHERE iid = $iid";
2430
                    $connection->executeQuery($sql);
2431
                }
2432
            }
2433
        }
2434
    }
2435
2436
    // Fix c_item_property
2437
    if ($debug) {
2438
        error_log('update c_item_property');
2439
    }
2440
2441
    $sql = "SELECT * FROM course";
2442
    $courseList = $connection->fetchAll($sql);
2443
    if ($debug) {
2444
        error_log('Getting course list');
2445
    }
2446
2447
    $totalCourse = count($courseList);
2448
    $counter = 0;
2449
2450
    foreach ($courseList as $courseData) {
2451
        $courseId = $courseData['id'];
2452
        if ($debug) {
2453
            error_log('Updating course: '.$courseData['code']);
2454
        }
2455
2456
        $sql = "SELECT * FROM c_item_property WHERE c_id = $courseId";
2457
        $result = $connection->fetchAll($sql);
2458
        foreach ($result as $item) {
2459
            $sessionId = intval($item['session_id']);
2460
            $groupId = intval($item['to_group_id']);
2461
            $iid = $item['iid'];
2462
            $ref = $item['ref'];
2463
2464
            // Fix group id
2465
            // Commented group id is already fixed in Version20150603181728.php
2466
            /*if (!empty($groupId)) {
2467
                $sql = "SELECT * FROM c_group_info
2468
                        WHERE c_id = $courseId AND id = $groupId";
2469
                $data = $connection->fetchAssoc($sql);
2470
                if (!empty($data)) {
2471
                    $newGroupId = $data['iid'];
2472
                    $sql = "UPDATE c_item_property SET to_group_id = $newGroupId
2473
                            WHERE iid = $iid";
2474
                    $connection->executeQuery($sql);
2475
                } else {
2476
                    // The group does not exists clean this record
2477
                    $sql = "DELETE FROM c_item_property WHERE iid = $iid";
2478
                    $connection->executeQuery($sql);
2479
                }
2480
            }*/
2481
2482
            $sql = '';
2483
            //$newId = '';
2484
            switch ($item['tool']) {
2485
                case TOOL_LEARNPATH:
2486
                    $sql = "SELECT * FROM c_lp WHERE c_id = $courseId AND id = $ref ";
2487
                    break;
2488
                // already fixed in c_lp_item
2489
                /*case TOOL_LINK:
2490
                    $sql = "SELECT * FROM c_link WHERE c_id = $courseId AND id = $ref ";
2491
                    break;
2492
                case TOOL_STUDENTPUBLICATION:
2493
                    $sql = "SELECT * FROM c_student_publication WHERE c_id = $courseId AND id = $ref";
2494
                    break;
2495
                case TOOL_QUIZ:
2496
                    $sql = "SELECT * FROM c_quiz WHERE c_id = $courseId AND id = $ref";
2497
                    break;
2498
                case TOOL_DOCUMENT:
2499
                    $sql = "SELECT * FROM c_document WHERE c_id = $courseId AND id = $ref";
2500
                    break;
2501
                case TOOL_FORUM:
2502
                    $sql = "SELECT * FROM c_forum_forum WHERE c_id = $courseId AND id = $ref";
2503
                    break;
2504
                case 'thread':
2505
                    $sql = "SELECT * FROM c_forum_thread WHERE c_id = $courseId AND id = $ref";
2506
                    break;*/
2507
            }
2508
2509
            if (!empty($sql)) {
2510
                $data = $connection->fetchAssoc($sql);
2511
                if (isset($data['iid']) && !empty($data['iid'])) {
2512
                    $newId = $data['iid'];
2513
                    $sql = "UPDATE c_item_property SET ref = $newId WHERE iid = $iid";
2514
                    $connection->executeQuery($sql);
2515
                }
2516
            }
2517
        }
2518
2519
        if ($debug) {
2520
            // Print a status in the log once in a while
2521
            error_log("Course process #$counter/$totalCourse");
2522
        }
2523
        $counter++;
2524
    }
2525
2526
    if ($debug) {
2527
        error_log('update gradebook_link');
2528
    }
2529
2530
    // Fix gradebook_link
2531
    $sql = "SELECT * FROM gradebook_link";
2532
    $result = $connection->fetchAll($sql);
2533
    foreach ($result as $item) {
2534
        $courseCode = $item['course_code'];
2535
        $categoryId = (int) $item['category_id'];
2536
2537
        $sql = "SELECT * FROM course WHERE code = '$courseCode'";
2538
        $courseInfo = $connection->fetchAssoc($sql);
2539
        if (empty($courseInfo)) {
2540
            continue;
2541
        }
2542
2543
        $courseId = $courseInfo['id'];
2544
2545
        $ref = $item['ref_id'];
2546
        $iid = $item['id'];
2547
2548
        $sql = '';
2549
        switch ($item['type']) {
2550
            case LINK_LEARNPATH:
2551
                $sql = "SELECT * FROM c_link WHERE c_id = $courseId AND id = $ref ";
2552
                break;
2553
            case LINK_STUDENTPUBLICATION:
2554
                $sql = "SELECT * FROM c_student_publication WHERE c_id = $courseId AND id = $ref";
2555
                break;
2556
            case LINK_EXERCISE:
2557
                $sql = "SELECT * FROM c_quiz WHERE c_id = $courseId AND id = $ref";
2558
                break;
2559
            case LINK_ATTENDANCE:
2560
                //$sql = "SELECT * FROM c_document WHERE c_id = $courseId AND id = $ref";
2561
                break;
2562
            case LINK_FORUM_THREAD:
2563
                $sql = "SELECT * FROM c_forum_thread WHERE c_id = $courseId AND thread_id = $ref";
2564
                break;
2565
        }
2566
2567
        if (!empty($sql)) {
2568
            $data = $connection->fetchAssoc($sql);
2569
            if (isset($data) && isset($data['iid'])) {
2570
                $newId = $data['iid'];
2571
                $sql = "UPDATE gradebook_link SET ref_id = $newId
2572
                        WHERE id = $iid AND course_code = '$courseCode' AND category_id = $categoryId ";
2573
                $connection->executeQuery($sql);
2574
            }
2575
        }
2576
    }
2577
2578
    if ($debug) {
2579
        error_log('update groups');
2580
    }
2581
2582
    $sql = "SELECT * FROM groups";
2583
    $result = $connection->executeQuery($sql);
2584
    $groups = $result->fetchAll();
2585
    $oldGroups = [];
2586
    if (!empty($groups)) {
2587
        foreach ($groups as $group) {
2588
            if (empty($group['name'])) {
2589
                continue;
2590
            }
2591
2592
            $params = [
2593
                'name' => $group['name'],
2594
                'description' => $group['description'],
2595
                'group_type' => 1,
2596
                'picture' => $group['picture_uri'],
2597
                'url' => $group['url'],
2598
                'visibility' => $group['visibility'],
2599
                'updated_at' => $group['updated_on'],
2600
                'created_at' => $group['created_on'],
2601
            ];
2602
            $connection->insert('usergroup', $params);
2603
            $id = $connection->lastInsertId('id');
2604
            $oldGroups[$group['id']] = $id;
2605
        }
2606
    }
2607
2608
    if (!empty($oldGroups)) {
2609
        error_log('Moving group files');
2610
        foreach ($oldGroups as $oldId => $newId) {
2611
            $path = get_group_picture_path_by_id(
2612
                $oldId,
2613
                'system'
2614
            );
2615
2616
            if (!empty($path)) {
2617
                $newPath = str_replace(
2618
                    "groups/$oldId/",
2619
                    "groups/$newId/",
2620
                    $path['dir']
2621
                );
2622
                $command = "mv {$path['dir']} $newPath ";
2623
                error_log("Executing $command");
2624
                system($command);
2625
            }
2626
        }
2627
2628
        $sql = "SELECT * FROM group_rel_user";
2629
        $result = $connection->executeQuery($sql);
2630
        $dataList = $result->fetchAll();
2631
2632
        if (!empty($dataList)) {
2633
            foreach ($dataList as $data) {
2634
                if (isset($oldGroups[$data['group_id']])) {
2635
                    $data['group_id'] = $oldGroups[$data['group_id']];
2636
                    $userId = $data['user_id'];
2637
2638
                    $sql = "SELECT id FROM user WHERE user_id = $userId";
2639
                    $userResult = $connection->executeQuery($sql);
2640
                    $userInfo = $userResult->fetch();
2641
                    if (empty($userInfo)) {
2642
                        continue;
2643
                    }
2644
2645
                    $sql = "INSERT INTO usergroup_rel_user (usergroup_id, user_id, relation_type)
2646
                            VALUES ('{$data['group_id']}', '{$userId}', '{$data['relation_type']}')";
2647
                    $connection->executeQuery($sql);
2648
                }
2649
            }
2650
        }
2651
2652
        $sql = "SELECT * FROM group_rel_group";
2653
        $result = $connection->executeQuery($sql);
2654
        $dataList = $result->fetchAll();
2655
2656
        if (!empty($dataList)) {
2657
            foreach ($dataList as $data) {
2658
                if (isset($oldGroups[$data['group_id']]) && isset($oldGroups[$data['subgroup_id']])) {
2659
                    $data['group_id'] = $oldGroups[$data['group_id']];
2660
                    $data['subgroup_id'] = $oldGroups[$data['subgroup_id']];
2661
                    $sql = "INSERT INTO usergroup_rel_usergroup (group_id, subgroup_id, relation_type)
2662
                            VALUES ('{$data['group_id']}', '{$data['subgroup_id']}', '{$data['relation_type']}')";
2663
                    $connection->executeQuery($sql);
2664
                }
2665
            }
2666
        }
2667
2668
        $sql = "SELECT * FROM announcement_rel_group";
2669
        $result = $connection->executeQuery($sql);
2670
        $dataList = $result->fetchAll();
2671
2672
        if (!empty($dataList)) {
2673
            foreach ($dataList as $data) {
2674
                if (isset($oldGroups[$data['group_id']])) {
2675
                    // Deleting relation
2676
                    $sql = "DELETE FROM announcement_rel_group WHERE group_id = {$data['group_id']}";
2677
                    $connection->executeQuery($sql);
2678
2679
                    // Add new relation
2680
                    $data['group_id'] = $oldGroups[$data['group_id']];
2681
                    $sql = "INSERT INTO announcement_rel_group(group_id, announcement_id)
2682
                            VALUES ('{$data['group_id']}', '{$data['announcement_id']}')";
2683
                    $connection->executeQuery($sql);
2684
                }
2685
            }
2686
        }
2687
2688
        $sql = "SELECT * FROM group_rel_tag";
2689
        $result = $connection->executeQuery($sql);
2690
        $dataList = $result->fetchAll();
2691
        if (!empty($dataList)) {
2692
            foreach ($dataList as $data) {
2693
                if (isset($oldGroups[$data['group_id']])) {
2694
                    $data['group_id'] = $oldGroups[$data['group_id']];
2695
                    $sql = "INSERT INTO usergroup_rel_tag (tag_id, usergroup_id)
2696
                            VALUES ('{$data['tag_id']}', '{$data['group_id']}')";
2697
                    $connection->executeQuery($sql);
2698
                }
2699
            }
2700
        }
2701
    }
2702
2703
    if ($debug) {
2704
        error_log('update extra fields');
2705
    }
2706
2707
    // Extra fields
2708
    $extraFieldTables = [
2709
        ExtraField::USER_FIELD_TYPE => Database::get_main_table(TABLE_MAIN_USER_FIELD),
2710
        ExtraField::COURSE_FIELD_TYPE => Database::get_main_table(TABLE_MAIN_COURSE_FIELD),
2711
        //ExtraField::LP_FIELD_TYPE => Database::get_main_table(TABLE_MAIN_LP_FIELD),
2712
        ExtraField::SESSION_FIELD_TYPE => Database::get_main_table(TABLE_MAIN_SESSION_FIELD),
2713
        //ExtraField::CALENDAR_FIELD_TYPE => Database::get_main_table(TABLE_MAIN_CALENDAR_EVENT_FIELD),
2714
        //ExtraField::QUESTION_FIELD_TYPE => Database::get_main_table(TABLE_MAIN_CALENDAR_EVENT_FIELD),
2715
        //ExtraField::USER_FIELD_TYPE => //Database::get_main_table(TABLE_MAIN_SPECIFIC_FIELD),
2716
    ];
2717
2718
    foreach ($extraFieldTables as $type => $table) {
2719
        $sql = "SELECT * FROM $table ";
2720
        if ($debug) {
2721
            error_log($sql);
2722
        }
2723
        $result = $connection->query($sql);
2724
        $fields = $result->fetchAll();
2725
2726
        foreach ($fields as $field) {
2727
            if ($debug) {
2728
                error_log("Loading field: ".$field['field_variable']);
2729
            }
2730
            $originalId = $field['id'];
2731
2732
            $params = [
2733
                'extra_field_type' => $type,
2734
                'variable' => $field['field_variable'],
2735
                'field_type' => $field['field_type'],
2736
                'display_text' => $field['field_display_text'],
2737
                'default_value' => $field['field_default_value'],
2738
                'field_order' => $field['field_order'],
2739
                'visible' => $field['field_visible'],
2740
                'changeable' => $field['field_changeable'],
2741
                'filter' => $field['field_filter'],
2742
            ];
2743
2744
            $connection->insert('extra_field', $params);
2745
            $newExtraFieldId = $connection->lastInsertId();
2746
2747
            $values = [];
2748
            $handlerId = null;
2749
            switch ($type) {
2750
                case ExtraField::USER_FIELD_TYPE:
2751
                    $optionTable = Database::get_main_table(
2752
                        TABLE_MAIN_USER_FIELD_OPTIONS
2753
                    );
2754
                    $valueTable = Database::get_main_table(
2755
                        TABLE_MAIN_USER_FIELD_VALUES
2756
                    );
2757
                    $handlerId = 'user_id';
2758
                    break;
2759
                case ExtraField::COURSE_FIELD_TYPE:
2760
                    $optionTable = Database::get_main_table(
2761
                        TABLE_MAIN_COURSE_FIELD_OPTIONS
2762
                    );
2763
                    $valueTable = Database::get_main_table(
2764
                        TABLE_MAIN_COURSE_FIELD_VALUES
2765
                    );
2766
                    $handlerId = 'c_id';
2767
                    break;
2768
                case ExtraField::SESSION_FIELD_TYPE:
2769
                    $optionTable = Database::get_main_table(
2770
                        TABLE_MAIN_SESSION_FIELD_OPTIONS
2771
                    );
2772
                    $valueTable = Database::get_main_table(
2773
                        TABLE_MAIN_SESSION_FIELD_VALUES
2774
                    );
2775
                    $handlerId = 'session_id';
2776
                    break;
2777
            }
2778
2779
            if (!empty($optionTable)) {
2780
                $sql = "SELECT * FROM $optionTable WHERE field_id = $originalId ";
2781
                $result = $connection->query($sql);
2782
                $options = $result->fetchAll();
2783
2784
                foreach ($options as $option) {
2785
                    $params = [
2786
                        'display_text' => $option['option_display_text'],
2787
                        'field_id' => $newExtraFieldId,
2788
                        'option_order' => $option['option_order'],
2789
                        'option_value' => $option['option_value'],
2790
                    ];
2791
                    $connection->insert('extra_field_options', $params);
2792
                }
2793
2794
                $sql = "SELECT * FROM $valueTable WHERE field_id = $originalId ";
2795
                $result = $connection->query($sql);
2796
                $values = $result->fetchAll();
2797
                if ($debug) {
2798
                    error_log("Fetch all values for field");
2799
                }
2800
            }
2801
2802
            if (!empty($values)) {
2803
                if ($debug) {
2804
                    error_log("Saving field value in new table");
2805
                }
2806
                $k = 0;
2807
                foreach ($values as $value) {
2808
                    if (isset($value[$handlerId])) {
2809
                        // Insert without the use of the entity as it reduces
2810
                        // speed to 2 records per second (much too slow)
2811
                        $params = [
2812
                            'field_id' => $newExtraFieldId,
2813
                            'value' => $value['field_value'],
2814
                            'item_id' => $value[$handlerId],
2815
                        ];
2816
                        $connection->insert('extra_field_values', $params);
2817
                        if ($debug && ($k % 10000 == 0)) {
2818
                            error_log("Saving field $k");
2819
                        }
2820
                        $k++;
2821
                    }
2822
                }
2823
            }
2824
        }
2825
    }
2826
2827
    if ($debug) {
2828
        error_log('Remove index');
2829
    }
2830
2831
    // Drop temporary indexes added to increase speed of this function's queries
2832
    $sql = "ALTER TABLE c_document DROP INDEX tmpidx_doc";
2833
    $connection->executeQuery($sql);
2834
    $sql = "ALTER TABLE c_student_publication DROP INDEX tmpidx_stud";
2835
    $connection->executeQuery($sql);
2836
    $sql = "ALTER TABLE c_quiz DROP INDEX tmpidx_quiz";
2837
    $connection->executeQuery($sql);
2838
    $sql = "ALTER TABLE c_item_property DROP INDEX tmpidx_ip";
2839
    $connection->executeQuery($sql);
2840
2841
    if ($debug) {
2842
        error_log('Finish fixId function');
2843
    }
2844
2845
    fixLpId($connection, true);
2846
}
2847
2848
/**
2849
 * @param \Doctrine\DBAL\Connection $connection
2850
 * @param $debug
2851
 *
2852
 * @throws \Doctrine\DBAL\DBALException
2853
 */
2854
function fixLpId($connection, $debug)
2855
{
2856
    if ($debug) {
2857
        error_log('Fix lp.id lp.iids');
2858
    }
2859
2860
    $sql = 'SELECT id, title, code FROM course';
2861
    $result = $connection->query($sql);
2862
    $courses = $result->fetchAll();
2863
2864
    $sql = 'SELECT id FROM session';
2865
    $result = $connection->query($sql);
2866
    $sessions = $result->fetchAll();
2867
2868
    $tblCLp = Database::get_course_table(TABLE_LP_MAIN);
2869
    $tblCLpItem = Database::get_course_table(TABLE_LP_ITEM);
2870
    $toolTable = Database::get_course_table(TABLE_TOOL_LIST);
2871
2872
    if (!empty($sessions)) {
2873
        $sessions = array_column($sessions, 'id');
2874
        $sessions[] = 0;
2875
    } else {
2876
        $sessions = [0];
2877
    }
2878
2879
    foreach ($courses as $course) {
2880
        $courseId = $course['id'];
2881
        $sql = "SELECT * FROM $tblCLp WHERE c_id = $courseId AND iid <> id ORDER by iid";
2882
        $result = $connection->query($sql);
2883
        if ($debug) {
2884
            error_log('-------------');
2885
            error_log("Entering Lps in course #$courseId");
2886
            error_log($sql);
2887
        }
2888
        $lpList = $result->fetchAll();
2889
        $myOnlyLpList = [];
2890
        if (!empty($lpList)) {
2891
            foreach ($lpList as $lpInfo) {
2892
                $oldId = $lpInfo['id'];
2893
                $sql = "SELECT * FROM $tblCLpItem WHERE c_id = $courseId AND lp_id = $oldId ORDER by iid";
2894
                $result = $connection->query($sql);
2895
                $items = $result->fetchAll();
2896
                $lpInfo['lp_list'] = $items;
2897
                $myOnlyLpList[] = $lpInfo;
2898
            }
2899
        }
2900
2901
        if (!empty($myOnlyLpList)) {
2902
            foreach ($myOnlyLpList as $lpInfo) {
2903
                $lpIid = $lpInfo['iid'];
2904
                $oldId = $lpInfo['id'];
2905
                $items = $lpInfo['lp_list'];
2906
2907
                if (empty($items)) {
2908
                    continue;
2909
                }
2910
                $itemList = [];
2911
                foreach ($items as $subItem) {
2912
                    $itemList[$subItem['id']] = $subItem['iid'];
2913
                }
2914
                $variablesToFix = [
2915
                    'parent_item_id',
2916
                    'next_item_id',
2917
                    'prerequisite',
2918
                    'previous_item_id',
2919
                ];
2920
2921
                foreach ($sessions as $sessionId) {
2922
                    $correctLink = "lp/lp_controller.php?action=view&lp_id=$lpIid&id_session=$sessionId";
2923
                    $link = "newscorm/lp_controller.php?action=view&lp_id=$oldId&id_session=$sessionId";
2924
                    $secondLink = "lp/lp_controller.php?action=view&lp_id=$oldId&id_session=$sessionId";
2925
                    $sql = "UPDATE $toolTable
2926
                        SET link = '$correctLink'
2927
                        WHERE c_id = $courseId AND (link = '$link' OR link ='$secondLink')";
2928
                    $connection->query($sql);
2929
                    if ($debug) {
2930
                        //error_log("Fix wrong c_tool links");
2931
                        //error_log($sql);
2932
                    }
2933
                }
2934
2935
                foreach ($items as $item) {
2936
                    $itemIid = $item['iid'];
2937
                    $itemId = $item['id'];
2938
                    foreach ($variablesToFix as $variable) {
2939
                        if (!empty($item[$variable]) && isset($itemList[$item[$variable]])) {
2940
                            $newId = $itemList[$item[$variable]];
2941
                            $sql = "UPDATE $tblCLpItem SET $variable = $newId
2942
                                    WHERE iid = $itemIid AND c_id = $courseId AND lp_id = $oldId";
2943
                            $connection->query($sql);
2944
                            if ($debug) {
2945
                                //error_log($sql);
2946
                            }
2947
                        }
2948
                    }
2949
2950
                    if ($item['item_type'] === 'document' && !empty($item['path'])) {
2951
                        $oldDocumentId = $item['path'];
2952
                        $sql = "SELECT * FROM c_document WHERE c_id = $courseId AND id = $oldDocumentId";
2953
                        $result = $connection->query($sql);
2954
                        $document = $result->fetch();
2955
                        if (!empty($document)) {
2956
                            $newDocumentId = $document['iid'];
2957
                            if (!empty($newDocumentId)) {
2958
                                $sql = "UPDATE $tblCLpItem SET path = $newDocumentId
2959
                                        WHERE iid = $itemIid AND c_id = $courseId";
2960
                                $connection->query($sql);
2961
                            }
2962
                        }
2963
                    }
2964
2965
                    if ($item['item_type'] === 'link' && !empty($item['path'])) {
2966
                        $oldLinkId = $item['path'];
2967
                        $sql = "SELECT * FROM c_link WHERE c_id = $courseId AND id = $oldLinkId";
2968
                        $result = $connection->query($sql);
2969
                        $document = $result->fetch();
2970
                        if (!empty($document)) {
2971
                            $newLinkId = $document['iid'];
2972
                            if (!empty($newLinkId)) {
2973
                                $sql = "UPDATE $tblCLpItem SET path = $newLinkId
2974
                                        WHERE iid = $itemIid AND c_id = $courseId";
2975
                                $connection->query($sql);
2976
                            }
2977
                        }
2978
                    }
2979
2980
                    // c_lp_view
2981
                    $sql = "UPDATE c_lp_view SET last_item = $itemIid
2982
                            WHERE c_id = $courseId AND last_item = $itemId AND lp_id = $oldId";
2983
                    $connection->query($sql);
2984
2985
                    // c_lp_item_view
2986
                    $sql = "UPDATE c_lp_item_view SET lp_item_id = $itemIid
2987
                            WHERE c_id = $courseId AND lp_item_id = $itemId";
2988
                    $connection->query($sql);
2989
2990
                    // Update track_exercises
2991
                    $sql = "UPDATE track_e_exercises SET orig_lp_item_id = $itemIid
2992
                            WHERE c_id = $courseId AND orig_lp_id = $oldId AND orig_lp_item_id = $itemId";
2993
                    $connection->query($sql);
2994
2995
                    // c_forum_thread
2996
                    $sql = "UPDATE c_forum_thread SET lp_item_id = $itemIid
2997
                            WHERE c_id = $courseId AND lp_item_id = $itemId";
2998
                    $connection->query($sql);
2999
3000
                    // orig_lp_item_view_id
3001
                    $sql = "SELECT * FROM c_lp_view
3002
                            WHERE c_id = $courseId AND lp_id = $oldId";
3003
                    $result = $connection->query($sql);
3004
                    $itemViewList = $result->fetchAll();
3005
                    if ($itemViewList) {
3006
                        foreach ($itemViewList as $itemView) {
3007
                            $userId = $itemView['user_id'];
3008
                            $oldItemViewId = $itemView['id'];
3009
                            $newItemView = $itemView['iid'];
3010
3011
                            if (empty($oldItemViewId)) {
3012
                                continue;
3013
                            }
3014
3015
                            $sql = "UPDATE track_e_exercises
3016
                                SET orig_lp_item_view_id = $newItemView
3017
                                WHERE
3018
                                  c_id = $courseId AND
3019
                                  orig_lp_id = $oldId AND
3020
                                  orig_lp_item_id = $itemIid AND
3021
                                  orig_lp_item_view_id = $oldItemViewId AND
3022
                                  exe_user_id = $userId
3023
                                  ";
3024
                            $connection->query($sql);
3025
3026
                            /*$sql = "UPDATE c_lp_item_view
3027
                                    SET lp_view_id = $newItemView
3028
                                    WHERE
3029
                                      lp_view_id = $oldItemViewId AND
3030
                                      c_id = $courseId
3031
                                  ";
3032
                            $connection->query($sql);*/
3033
                        }
3034
                    }
3035
3036
                    $sql = "UPDATE $tblCLpItem SET lp_id = $lpIid
3037
                            WHERE c_id = $courseId AND lp_id = $oldId AND id = $itemId";
3038
                    $connection->query($sql);
3039
3040
                    $sql = "UPDATE $tblCLpItem SET id = iid
3041
                            WHERE c_id = $courseId AND lp_id = $oldId AND id = $itemId";
3042
                    $connection->query($sql);
3043
                }
3044
3045
                $sql = "UPDATE c_lp_view SET lp_id = $lpIid WHERE c_id = $courseId AND lp_id = $oldId";
3046
                $connection->query($sql);
3047
3048
                $sql = "UPDATE c_forum_forum SET lp_id = $lpIid WHERE c_id = $courseId AND lp_id = $oldId";
3049
                $connection->query($sql);
3050
3051
                // Update track_exercises.
3052
                $sql = "UPDATE track_e_exercises SET orig_lp_id = $lpIid
3053
                        WHERE c_id = $courseId AND orig_lp_id = $oldId";
3054
                $connection->query($sql);
3055
3056
                $sql = "UPDATE $tblCLp SET id = iid WHERE c_id = $courseId AND id = $oldId ";
3057
                $connection->query($sql);
3058
            }
3059
        }
3060
    }
3061
3062
    if ($debug) {
3063
        error_log('END Fix lp.id lp.iids');
3064
    }
3065
}
3066
3067
/**
3068
 * After the schema was created (table creation), the function adds
3069
 * admin/platform information.
3070
 *
3071
 * @param EntityManager $manager
3072
 * @param string        $sysPath
3073
 * @param string        $encryptPassForm
3074
 * @param string        $passForm
3075
 * @param string        $adminLastName
3076
 * @param string        $adminFirstName
3077
 * @param string        $loginForm
3078
 * @param string        $emailForm
3079
 * @param string        $adminPhoneForm
3080
 * @param string        $languageForm
3081
 * @param string        $institutionForm
3082
 * @param string        $institutionUrlForm
3083
 * @param string        $siteName
3084
 * @param string        $allowSelfReg
3085
 * @param string        $allowSelfRegProf
3086
 * @param string        $installationProfile Installation profile, if any was provided
3087
 */
3088
function finishInstallation(
3089
    $manager,
3090
    $sysPath,
3091
    $encryptPassForm,
3092
    $passForm,
3093
    $adminLastName,
3094
    $adminFirstName,
3095
    $loginForm,
3096
    $emailForm,
3097
    $adminPhoneForm,
3098
    $languageForm,
3099
    $institutionForm,
3100
    $institutionUrlForm,
3101
    $siteName,
3102
    $allowSelfReg,
3103
    $allowSelfRegProf,
3104
    $installationProfile = ''
3105
) {
3106
    $sysPath = !empty($sysPath) ? $sysPath : api_get_path(SYS_PATH);
3107
3108
    $connection = $manager->getConnection();
3109
    $sql = getVersionTable();
3110
    // Add version table
3111
    $connection->executeQuery($sql);
3112
3113
    // Add tickets defaults
3114
    $ticketProject = new TicketProject();
3115
    $ticketProject
3116
        ->setId(1)
3117
        ->setName('Ticket System')
3118
        ->setInsertUserId(1);
3119
3120
    $manager->persist($ticketProject);
3121
    $manager->flush();
3122
3123
    $categories = [
3124
        get_lang('TicketEnrollment') => get_lang('TicketsAboutEnrollment'),
3125
        get_lang('TicketGeneralInformation') => get_lang('TicketsAboutGeneralInformation'),
3126
        get_lang('TicketRequestAndPapework') => get_lang('TicketsAboutRequestAndPapework'),
3127
        get_lang('TicketAcademicIncidence') => get_lang('TicketsAboutAcademicIncidence'),
3128
        get_lang('TicketVirtualCampus') => get_lang('TicketsAboutVirtualCampus'),
3129
        get_lang('TicketOnlineEvaluation') => get_lang('TicketsAboutOnlineEvaluation'),
3130
    ];
3131
3132
    $i = 1;
3133
3134
    /**
3135
     * @var string
3136
     * @var string $description
3137
     */
3138
    foreach ($categories as $category => $description) {
3139
        // Online evaluation requires a course
3140
        $ticketCategory = new TicketCategory();
3141
        $ticketCategory
3142
            ->setId($i)
3143
            ->setName($category)
3144
            ->setDescription($description)
3145
            ->setProject($ticketProject)
3146
            ->setInsertUserId(1);
3147
3148
        $isRequired = $i == 6;
3149
        $ticketCategory->setCourseRequired($isRequired);
3150
3151
        $manager->persist($ticketCategory);
3152
        $manager->flush();
3153
3154
        $i++;
3155
    }
3156
3157
    // Default Priorities
3158
    $defaultPriorities = [
3159
        TicketManager::PRIORITY_NORMAL => get_lang('PriorityNormal'),
3160
        TicketManager::PRIORITY_HIGH => get_lang('PriorityHigh'),
3161
        TicketManager::PRIORITY_LOW => get_lang('PriorityLow'),
3162
    ];
3163
3164
    $i = 1;
3165
    foreach ($defaultPriorities as $code => $priority) {
3166
        $ticketPriority = new TicketPriority();
3167
        $ticketPriority
3168
            ->setId($i)
3169
            ->setName($priority)
3170
            ->setCode($code)
3171
            ->setInsertUserId(1);
3172
3173
        $manager->persist($ticketPriority);
3174
        $manager->flush();
3175
        $i++;
3176
    }
3177
3178
    $table = Database::get_main_table(TABLE_TICKET_STATUS);
3179
3180
    // Default status
3181
    $defaultStatus = [
3182
        TicketManager::STATUS_NEW => get_lang('StatusNew'),
3183
        TicketManager::STATUS_PENDING => get_lang('StatusPending'),
3184
        TicketManager::STATUS_UNCONFIRMED => get_lang('StatusUnconfirmed'),
3185
        TicketManager::STATUS_CLOSE => get_lang('StatusClose'),
3186
        TicketManager::STATUS_FORWARDED => get_lang('StatusForwarded'),
3187
    ];
3188
3189
    $i = 1;
3190
    foreach ($defaultStatus as $code => $status) {
3191
        $attributes = [
3192
            'id' => $i,
3193
            'code' => $code,
3194
            'name' => $status,
3195
        ];
3196
        Database::insert($table, $attributes);
3197
        $i++;
3198
    }
3199
3200
    // Inserting data.sql
3201
    $data = file_get_contents($sysPath.'main/install/data.sql');
3202
    $result = $manager->getConnection()->prepare($data);
3203
    $result->execute();
3204
    $result->closeCursor();
3205
3206
    UserManager::setPasswordEncryption($encryptPassForm);
3207
3208
    // Create admin user.
3209
    @UserManager::create_user(
3210
        $adminFirstName,
3211
        $adminLastName,
3212
        1,
3213
        $emailForm,
3214
        $loginForm,
3215
        $passForm,
3216
        'ADMIN', //$official_code = '',
3217
        $languageForm,
3218
        $adminPhoneForm,
3219
        '', //$picture_uri = '',
3220
        PLATFORM_AUTH_SOURCE,
3221
        '', //$expirationDate,
3222
        1,
3223
        0,
3224
        null,
3225
        '',
3226
        false, //$send_mail = false,
3227
        true //$isAdmin = false
3228
    );
3229
3230
    // Create anonymous user.
3231
    @UserManager::create_user(
3232
        'Joe',
3233
        'Anonymous',
3234
        6,
3235
        '[email protected]',
3236
        'anon',
3237
        'anon',
3238
        'anonymous', //$official_code = '',
3239
        $languageForm,
3240
        '',
3241
        '', //$picture_uri = '',
3242
        PLATFORM_AUTH_SOURCE,
3243
        '',
3244
        1,
3245
        0,
3246
        null,
3247
        '',
3248
        false, //$send_mail = false,
3249
        false //$isAdmin = false
3250
    );
3251
3252
    // Set default language
3253
    Database::update(
3254
        Database::get_main_table(TABLE_MAIN_LANGUAGE),
3255
        ['available' => 1],
3256
        ['dokeos_folder = ?' => $languageForm]
3257
    );
3258
3259
    // Install settings
3260
    installSettings(
3261
        $institutionForm,
3262
        $institutionUrlForm,
3263
        $siteName,
3264
        $emailForm,
3265
        $adminLastName,
3266
        $adminFirstName,
3267
        $languageForm,
3268
        $allowSelfReg,
3269
        $allowSelfRegProf,
3270
        $installationProfile
3271
    );
3272
3273
    lockSettings();
3274
    updateDirAndFilesPermissions();
3275
3276
    // Set the latest version
3277
    $path = $sysPath.'app/Migrations/Schema/V111/';
3278
    $finder = new \Symfony\Component\Finder\Finder();
3279
    $files = $finder->files()->in($path);
3280
3281
    // Needed for chash
3282
    createVersionTable();
3283
3284
    foreach ($files as $version) {
3285
        $version = str_replace(['Version', '.php'], '', $version->getFilename());
3286
        $sql = "INSERT INTO version (version) VALUES ('$version')";
3287
        Database::query($sql);
3288
    }
3289
}
3290
3291
/**
3292
 * Creates 'version' table.
3293
 */
3294
function createVersionTable()
3295
{
3296
    $sql = getVersionTable();
3297
    Database::query($sql);
3298
}
3299
3300
/**
3301
 * Get version creation table query.
3302
 *
3303
 * @return string
3304
 */
3305
function getVersionTable()
3306
{
3307
    return 'CREATE TABLE IF NOT EXISTS version (id int unsigned NOT NULL AUTO_INCREMENT, version varchar(20), PRIMARY KEY(id), UNIQUE(version));';
3308
}
3309
3310
/**
3311
 * Update settings based on installation profile defined in a JSON file.
3312
 *
3313
 * @param string $installationProfile The name of the JSON file in main/install/profiles/ folder
3314
 *
3315
 * @throws \Doctrine\DBAL\DBALException
3316
 *
3317
 * @return bool false on failure (no bad consequences anyway, just ignoring profile)
3318
 */
3319
function installProfileSettings($installationProfile = '')
3320
{
3321
    if (empty($installationProfile)) {
3322
        return false;
3323
    }
3324
    $jsonPath = api_get_path(SYS_PATH).'main/install/profiles/'.$installationProfile.'.json';
3325
    // Make sure the path to the profile is not hacked
3326
    if (!Security::check_abs_path($jsonPath, api_get_path(SYS_PATH).'main/install/profiles/')) {
3327
        return false;
3328
    }
3329
    if (!is_file($jsonPath)) {
3330
        return false;
3331
    }
3332
    if (!is_readable($jsonPath)) {
3333
        return false;
3334
    }
3335
    if (!function_exists('json_decode')) {
3336
        // The php-json extension is not available. Ignore profile.
3337
        return false;
3338
    }
3339
    $json = file_get_contents($jsonPath);
3340
    $params = json_decode($json);
3341
    if ($params === false or $params === null) {
3342
        return false;
3343
    }
3344
    $settings = $params->params;
3345
    if (!empty($params->parent)) {
3346
        installProfileSettings($params->parent);
3347
    }
3348
3349
    $tblSettings = Database::get_main_table(TABLE_MAIN_SETTINGS_CURRENT);
3350
3351
    foreach ($settings as $id => $param) {
3352
        $conditions = ['variable = ? ' => $param->variable];
3353
3354
        if (!empty($param->subkey)) {
3355
            $conditions['AND subkey = ? '] = $param->subkey;
3356
        }
3357
3358
        Database::update(
3359
            $tblSettings,
3360
            ['selected_value' => $param->selected_value],
3361
            $conditions
3362
        );
3363
    }
3364
3365
    return true;
3366
}
3367
3368
/**
3369
 * Quick function to remove a directory with its subdirectories.
3370
 *
3371
 * @param $dir
3372
 */
3373
function rrmdir($dir)
3374
{
3375
    if (is_dir($dir)) {
3376
        $objects = scandir($dir);
3377
        foreach ($objects as $object) {
3378
            if ($object != "." && $object != "..") {
3379
                if (filetype($dir."/".$object) == "dir") {
3380
                    @rrmdir($dir."/".$object);
3381
                } else {
3382
                    @unlink($dir."/".$object);
3383
                }
3384
            }
3385
        }
3386
        reset($objects);
3387
        rmdir($dir);
3388
    }
3389
}
3390
3391
/**
3392
 * @param        $id
3393
 * @param string $type
3394
 * @param bool   $preview
3395
 * @param bool   $anonymous
3396
 *
3397
 * @throws \Doctrine\DBAL\DBALException
3398
 *
3399
 * @return array
3400
 */
3401
function get_group_picture_path_by_id($id, $type = 'web', $preview = false, $anonymous = false)
3402
{
3403
    switch ($type) {
3404
        case 'system': // Base: absolute system path.
3405
            $base = api_get_path(SYS_UPLOAD_PATH);
3406
            break;
3407
        case 'web': // Base: absolute web path.
3408
        default:
3409
            $base = api_get_path(WEB_UPLOAD_PATH);
3410
            break;
3411
    }
3412
3413
    $noPicturePath = ['dir' => $base.'img/', 'file' => 'unknown.jpg'];
3414
3415
    if (empty($id) || empty($type)) {
3416
        return $anonymous ? $noPicturePath : ['dir' => '', 'file' => ''];
3417
    }
3418
3419
    $id = intval($id);
3420
3421
    //$group_table = Database::get_main_table(TABLE_MAIN_GROUP);
3422
    $group_table = 'groups';
3423
    $sql = "SELECT picture_uri FROM $group_table WHERE id=".$id;
3424
    $res = Database::query($sql);
3425
3426
    if (!Database::num_rows($res)) {
3427
        return $anonymous ? $noPicturePath : ['dir' => '', 'file' => ''];
3428
    }
3429
3430
    $user = Database::fetch_array($res);
3431
    $picture_filename = trim($user['picture_uri']);
3432
3433
    if (api_get_setting('split_users_upload_directory') === 'true') {
3434
        if (!empty($picture_filename)) {
3435
            $dir = $base.'groups/'.substr($picture_filename, 0, 1).'/'.$id.'/';
3436
        } elseif ($preview) {
3437
            $dir = $base.'groups/'.substr((string) $id, 0, 1).'/'.$id.'/';
3438
        } else {
3439
            $dir = $base.'groups/'.$id.'/';
3440
        }
3441
    } else {
3442
        $dir = $base.'groups/'.$id.'/';
3443
    }
3444
3445
    if (empty($picture_filename) && $anonymous) {
3446
        return $noPicturePath;
3447
    }
3448
3449
    return ['dir' => $dir, 'file' => $picture_filename];
3450
}
3451
3452
/**
3453
 * Control the different steps of the migration through a big switch.
3454
 *
3455
 * @param string        $fromVersion
3456
 * @param EntityManager $manager
3457
 * @param bool          $processFiles
3458
 *
3459
 * @throws \Doctrine\DBAL\DBALException
3460
 *
3461
 * @return bool Always returns true except if the process is broken
3462
 */
3463
function migrateSwitch($fromVersion, $manager, $processFiles = true)
3464
{
3465
    error_log('Starting migration process from '.$fromVersion.' ('.date('Y-m-d H:i:s').')');
3466
3467
    echo '<a class="btn btn-default" href="javascript:void(0)" id="details_button">'.get_lang('Details').'</a><br />';
3468
    echo '<div id="details" style="display:none">';
3469
3470
    $connection = $manager->getConnection();
3471
3472
    $database = new Database();
3473
    $database->setManager($manager);
3474
3475
    switch ($fromVersion) {
3476
        case '1.9.0':
3477
        case '1.9.2':
3478
        case '1.9.4':
3479
        case '1.9.6':
3480
        case '1.9.6.1':
3481
        case '1.9.8':
3482
        case '1.9.8.1':
3483
        case '1.9.8.2':
3484
        case '1.9.10':
3485
        case '1.9.10.2':
3486
        case '1.9.10.4':
3487
        case '1.9.10.6':
3488
            $database = new Database();
3489
            $database->setManager($manager);
3490
3491
            // Fix type "enum" before running the migration with Doctrine
3492
            $connection->executeQuery("ALTER TABLE course_category MODIFY COLUMN auth_course_child VARCHAR(40) DEFAULT 'TRUE'");
3493
            $connection->executeQuery("ALTER TABLE course_category MODIFY COLUMN auth_cat_child VARCHAR(40) DEFAULT 'TRUE'");
3494
            $connection->executeQuery("ALTER TABLE c_quiz_answer MODIFY COLUMN hotspot_type varchar(40) default NULL");
3495
            $connection->executeQuery("ALTER TABLE c_tool MODIFY COLUMN target varchar(20) NOT NULL default '_self'");
3496
            $connection->executeQuery("ALTER TABLE c_link MODIFY COLUMN on_homepage char(10) NOT NULL default '0'");
3497
            $connection->executeQuery("ALTER TABLE c_blog_rating MODIFY COLUMN rating_type char(40) NOT NULL default 'post'");
3498
            $connection->executeQuery("ALTER TABLE c_survey MODIFY COLUMN anonymous char(10) NOT NULL default '0'");
3499
            $connection->executeQuery("ALTER TABLE c_document MODIFY COLUMN filetype char(10) NOT NULL default 'file'");
3500
            $connection->executeQuery("ALTER TABLE c_student_publication MODIFY COLUMN filetype char(10) NOT NULL default 'file'");
3501
3502
            // Migrate using the migration files located in:
3503
            // src/Chamilo/CoreBundle/Migrations/Schema/V110
3504
            $result = migrate(
3505
                110,
3506
                $manager
3507
            );
3508
3509
            if ($result) {
3510
                error_log('Migrations files were executed ('.date('Y-m-d H:i:s').')');
3511
                fixIds($manager);
3512
                error_log('fixIds finished ('.date('Y-m-d H:i:s').')');
3513
3514
                $connection->executeQuery("UPDATE settings_current SET selected_value = '1.10.0' WHERE variable = 'chamilo_database_version'");
3515
3516
                if ($processFiles) {
3517
                    $fromVersionShort = '1.9';
3518
                    include __DIR__.'/update-files-1.9.0-1.10.0.inc.php';
3519
                    // Only updates the configuration.inc.php with the new version
3520
                    include __DIR__.'/update-configuration.inc.php';
3521
3522
                    $configurationFiles = [
3523
                        'mail.conf.php',
3524
                        'profile.conf.php',
3525
                        'course_info.conf.php',
3526
                        'add_course.conf.php',
3527
                        'events.conf.php',
3528
                        'auth.conf.php',
3529
                    ];
3530
3531
                    error_log('Copy conf files');
3532
3533
                    foreach ($configurationFiles as $file) {
3534
                        if (file_exists(api_get_path(SYS_CODE_PATH).'inc/conf/'.$file)) {
3535
                            copy(
3536
                                api_get_path(SYS_CODE_PATH).'inc/conf/'.$file,
3537
                                api_get_path(CONFIGURATION_PATH).$file
3538
                            );
3539
                        }
3540
                    }
3541
                }
3542
3543
                error_log('Upgrade 1.10.x process concluded! ('.date('Y-m-d H:i:s').')');
3544
            } else {
3545
                error_log('There was an error during running migrations. Check error.log');
3546
                break;
3547
            }
3548
            // no break
3549
        case '1.10.0':
3550
        case '1.10.2':
3551
        case '1.10.4':
3552
        case '1.10.6':
3553
        case '1.10.8':
3554
            $database = new Database();
3555
            $database->setManager($manager);
3556
            // Migrate using the migration files located in:
3557
            // src/Chamilo/CoreBundle/Migrations/Schema/V111
3558
            $result = migrate(
3559
                111,
3560
                $manager
3561
            );
3562
3563
            if ($result) {
3564
                fixLpId($connection, true);
3565
3566
                error_log('Migrations files were executed ('.date('Y-m-d H:i:s').')');
3567
3568
                fixPostGroupIds($connection);
3569
3570
                $sql = "UPDATE settings_current SET selected_value = '1.11.0' WHERE variable = 'chamilo_database_version'";
3571
                $connection->executeQuery($sql);
3572
                if ($processFiles) {
3573
                    error_log('Update config files');
3574
                    $fromVersionShort = '1.10';
3575
                    include __DIR__.'/update-files-1.10.0-1.11.0.inc.php';
3576
                    // Only updates the configuration.inc.php with the new version
3577
                    include __DIR__.'/update-configuration.inc.php';
3578
                }
3579
                error_log('Upgrade 1.11.x process concluded!  ('.date('Y-m-d H:i:s').')');
3580
            } else {
3581
                error_log('There was an error during running migrations. Check error.log');
3582
            }
3583
            break;
3584
        default:
3585
            break;
3586
    }
3587
3588
    echo '</div>';
3589
3590
    return true;
3591
}
3592
3593
/**
3594
 * @param \Doctrine\DBAL\Connection $connection
3595
 *
3596
 * @throws \Doctrine\DBAL\DBALException
3597
 */
3598
function fixPostGroupIds($connection)
3599
{
3600
    $connection->executeQuery("ALTER TABLE course_category MODIFY COLUMN auth_course_child VARCHAR(40) DEFAULT 'TRUE'");
3601
    error_log('Fix c_student_publication.post_group_id');
3602
3603
    // Fix post_group_id
3604
    $sql = "SELECT * FROM c_student_publication
3605
            WHERE (post_group_id <> 0 AND post_group_id is not null)";
3606
    $statement = $connection->executeQuery($sql);
3607
    $result = $statement->fetchAll();
3608
3609
    foreach ($result as $row) {
3610
        $groupId = $row['post_group_id'];
3611
        $courseId = $row['c_id'];
3612
        $workIid = $row['iid'];
3613
        $sql = "SELECT iid from c_group_info
3614
                WHERE c_id = $courseId AND id = $groupId";
3615
        $statement = $connection->executeQuery($sql);
3616
        $count = $statement->rowCount();
3617
        if ($count > 0) {
3618
            $rowGroup = $statement->fetch();
3619
            $newGroupId = $rowGroup['iid'];
3620
            if ($newGroupId == $groupId) {
3621
                continue;
3622
            }
3623
            if ($newGroupId) {
3624
                $sql = "UPDATE c_student_publication
3625
                        SET post_group_id = $newGroupId
3626
                        WHERE
3627
                            c_id = $courseId AND
3628
                            iid = $workIid
3629
                        ";
3630
                $connection->executeQuery($sql);
3631
            }
3632
        }
3633
    }
3634
3635
    error_log('End - Fix c_student_publication.post_group_id');
3636
3637
    // Delete c_student_publication from any session that doesn't exist anymore
3638
    $sql = "DELETE FROM c_student_publication
3639
            WHERE session_id NOT IN (SELECT id FROM session) AND (session_id <> 0 AND session_id is not null)";
3640
    $connection->executeQuery($sql);
3641
3642
    error_log('Fix work documents');
3643
    // Fix work documents that don't have c_item_property value
3644
    $sql = "SELECT * FROM c_student_publication WHERE parent_id IS NOT NULL";
3645
    $statement = $connection->executeQuery($sql);
3646
    $result = $statement->fetchAll();
3647
    foreach ($result as $row) {
3648
        $groupId = $row['post_group_id'];
3649
        $courseId = $row['c_id'];
3650
        $sessionId = $row['session_id'];
3651
        $workId = $row['id'];
3652
        $sessionCondition = " session_id = $sessionId";
3653
        if (empty($sessionId)) {
3654
            $sessionCondition = ' (session_id = 0 OR session_id IS NULL) ';
3655
        }
3656
        $sql = "SELECT * FROM c_item_property
3657
                WHERE
3658
                    c_id = $courseId AND
3659
                    tool = 'work' AND
3660
                    ref = $workId AND
3661
                    $sessionCondition ";
3662
        $itemInfo = $connection->fetchAssoc($sql);
3663
        if (empty($itemInfo)) {
3664
            $params = [
3665
                'c_id' => $courseId,
3666
                'to_group_id' => $groupId,
3667
                //'to_user_id' => null,
3668
                'insert_user_id' => 1,
3669
                'session_id' => $sessionId,
3670
                'tool' => 'work',
3671
                'insert_date' => api_get_utc_datetime(),
3672
                'lastedit_date' => api_get_utc_datetime(),
3673
                'ref' => $workId,
3674
                'lastedit_type' => 'visible',
3675
                'lastedit_user_id' => 1,
3676
                'visibility' => 1,
3677
            ];
3678
            $connection->insert('c_item_property', $params);
3679
            $id = $connection->lastInsertId();
3680
            $sql = "UPDATE c_item_property SET id = iid WHERE iid = $id";
3681
            $connection->executeQuery($sql);
3682
        }
3683
    }
3684
    error_log('End - Fix work documents');
3685
}
3686