Completed
Push — master ( 50617c...371d59 )
by Julito
28:11
created

install.lib.php ➔ checkAccessUrl()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 0
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/* For licensing terms, see /license.txt */
3
4
use Doctrine\ORM\EntityManager;
5
use Chamilo\CoreBundle\Entity\ExtraField;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, ExtraField.

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

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

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

// Bar.php
namespace OtherDir;

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

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

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

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

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
6
use Chamilo\CoreBundle\Entity\ExtraFieldOptions;
7
use Chamilo\CoreBundle\Entity\ExtraFieldValues;
8
9
/**
10
 * Chamilo LMS
11
 * This file contains functions used by the install and upgrade scripts.
12
 *
13
 * Ideas for future additions:
14
 * - a function get_old_version_settings to retrieve the config file settings
15
 *   of older versions before upgrading.
16
 */
17
18
/*      CONSTANTS */
19
define('SYSTEM_CONFIG_FILENAME', 'configuration.dist.php');
20
21
/**
22
 * This function detects whether the system has been already installed.
23
 * It should be used for prevention from second running the installation
24
 * script and as a result - destroying a production system.
25
 * @return bool     The detected result;
26
 * @author Ivan Tcholakov, 2010;
27
 */
28
function isAlreadyInstalledSystem()
29
{
30
    global $new_version, $_configuration;
31
32
    if (empty($new_version)) {
33
        return true; // Must be initialized.
34
    }
35
36
    $current_config_file = api_get_path(CONFIGURATION_PATH).'configuration.php';
37
    if (!file_exists($current_config_file)) {
38
        return false; // Configuration file does not exist, install the system.
39
    }
40
    require $current_config_file;
41
42
    $current_version = null;
43
    if (isset($_configuration['system_version'])) {
44
        $current_version = trim($_configuration['system_version']);
45
    }
46
47
    // If the current version is old, upgrading is assumed, the installer goes ahead.
48
    return empty($current_version) ? false : version_compare($current_version, $new_version, '>=');
49
}
50
51
/**
52
 * This function checks if a php extension exists or not and returns an HTML status string.
53
 *
54
 * @param   string  $extensionName Name of the PHP extension to be checked
55
 * @param   string  $returnSuccess Text to show when extension is available (defaults to 'Yes')
56
 * @param   string  $returnFailure Text to show when extension is available (defaults to 'No')
57
 * @param   boolean $optional Whether this extension is optional (then show unavailable text in orange rather than red)
58
 * @param   string  $enabledTerm If this string is not null, then use to check if the corresponding parameter is = 1. If not, mention it's present but not enabled. For example, for opcache, this should be 'opcache.enable'
59
 * @return  string  HTML string reporting the status of this extension. Language-aware.
60
 * @author  Christophe Gesch??
61
 * @author  Patrick Cool <[email protected]>, Ghent University
62
 * @author  Yannick Warnier <[email protected]>
63
 */
64
function checkExtension($extensionName, $returnSuccess = 'Yes', $returnFailure = 'No', $optional = false, $enabledTerm = '')
65
{
66
    if (extension_loaded($extensionName)) {
67
        if (!empty($enabledTerm)) {
68
            $isEnabled = ini_get($enabledTerm);
69
            if ($isEnabled == '1') {
70
                return Display::label($returnSuccess, 'success');
71
            } else {
72
                if ($optional) {
73
                    return Display::label(get_lang('ExtensionInstalledButNotEnabled'), 'warning');
74
                } else {
75
                    return Display::label(get_lang('ExtensionInstalledButNotEnabled'), 'important');
76
                }
77
            }
78
        } else {
79
            return Display::label($returnSuccess, 'success');
80
        }
81
    } else {
82
        if ($optional) {
83
            return Display::label($returnFailure, 'warning');
84
        } else {
85
            return Display::label($returnFailure, 'important');
86
        }
87
    }
88
}
89
90
/**
91
 * This function checks whether a php setting matches the recommended value
92
 * @param   string $phpSetting A PHP setting to check
93
 * @param   string  $recommendedValue A recommended value to show on screen
94
 * @param   mixed  $returnSuccess What to show on success
95
 * @param   mixed  $returnFailure  What to show on failure
96
 * @return  string  A label to show
97
 * @author Patrick Cool <[email protected]>, Ghent University
98
 */
99
function checkPhpSetting($phpSetting, $recommendedValue, $returnSuccess = false, $returnFailure = false)
100
{
101
    $currentPhpValue = getPhpSetting($phpSetting);
102
    if ($currentPhpValue == $recommendedValue) {
103
        return Display::label($currentPhpValue.' '.$returnSuccess, 'success');
104
    } else {
105
        return Display::label($currentPhpValue.' '.$returnSuccess, 'important');
106
    }
107
}
108
109
110
/**
111
 * This function return the value of a php.ini setting if not "" or if exists,
112
 * otherwise return false
113
 * @param   string  $phpSetting The name of a PHP setting
114
 * @return  mixed   The value of the setting, or false if not found
115
 */
116
function checkPhpSettingExists($phpSetting)
117
{
118
    if (ini_get($phpSetting) != "") {
119
        return ini_get($phpSetting);
120
    }
121
122
    return false;
123
}
124
125
/**
126
 * Check if the current url is the same root_web when the multiple_access_url is enabled
127
 * @return bool
128
 */
129
function checkAccessUrl()
130
{
131
    if (api_get_configuration_value('multiple_access_urls') !== true) {
132
        return true;
133
    }
134
135
    $currentWebPath = api_get_path(WEB_PATH);
136
    $rootWeb = api_get_configuration_value('root_web');
137
138
    return $currentWebPath === $rootWeb;
139
}
140
141
/**
142
 * Returns a textual value ('ON' or 'OFF') based on a requester 2-state ini- configuration setting.
143
 *
144
 * @param string $val a php ini value
145
 * @return boolean: ON or OFF
0 ignored issues
show
Documentation introduced by
The doc-type boolean: could not be parsed: Unknown type name "boolean:" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
146
 * @author Joomla <http://www.joomla.org>
147
 */
148
function getPhpSetting($val)
149
{
150
    return ini_get($val) == '1' ? 'ON' : 'OFF';
151
}
152
153
/**
154
 * This function returns a string "true" or "false" according to the passed parameter.
155
 *
156
 * @param integer  $var  The variable to present as text
157
 * @return  string  the string "true" or "false"
158
 * @author Christophe Gesch??
159
 */
160
function trueFalse($var)
161
{
162
    return $var ? 'true' : 'false';
163
}
164
165
/**
166
 * Removes memory and time limits as much as possible.
167
 */
168
function remove_memory_and_time_limits()
169
{
170
    if (function_exists('ini_set')) {
171
        ini_set('memory_limit', -1);
172
        ini_set('max_execution_time', 0);
173
        error_log('Update-db script: memory_limit set to -1', 0);
174
        error_log('Update-db script: max_execution_time 0', 0);
175
    } else {
176
        error_log('Update-db script: could not change memory and time limits', 0);
177
    }
178
}
179
180
/**
181
 * Detects browser's language.
182
 * @return string       Returns a language identificator, i.e. 'english', 'spanish', ...
183
 * @author Ivan Tcholakov, 2010
184
 */
185
function detect_browser_language()
186
{
187
    static $language_index = array(
188
        'ar' => 'arabic',
189
        'ast' => 'asturian',
190
        'bg' => 'bulgarian',
191
        'bs' => 'bosnian',
192
        'ca' => 'catalan',
193
        'zh' => 'simpl_chinese',
194
        'zh-tw' => 'trad_chinese',
195
        'cs' => 'czech',
196
        'da' => 'danish',
197
        'prs' => 'dari',
198
        'de' => 'german',
199
        'el' => 'greek',
200
        'en' => 'english',
201
        'es' => 'spanish',
202
        'eo' => 'esperanto',
203
        'eu' => 'basque',
204
        'fa' => 'persian',
205
        'fr' => 'french',
206
        'fur' => 'friulian',
207
        'gl' => 'galician',
208
        'ka' => 'georgian',
209
        'hr' => 'croatian',
210
        'he' => 'hebrew',
211
        'hi' => 'hindi',
212
        'id' => 'indonesian',
213
        'it' => 'italian',
214
        'ko' => 'korean',
215
        'lv' => 'latvian',
216
        'lt' => 'lithuanian',
217
        'mk' => 'macedonian',
218
        'hu' => 'hungarian',
219
        'ms' => 'malay',
220
        'nl' => 'dutch',
221
        'ja' => 'japanese',
222
        'no' => 'norwegian',
223
        'oc' => 'occitan',
224
        'ps' => 'pashto',
225
        'pl' => 'polish',
226
        'pt' => 'portuguese',
227
        'pt-br' => 'brazilian',
228
        'ro' => 'romanian',
229
        'qu' => 'quechua_cusco',
230
        'ru' => 'russian',
231
        'sk' => 'slovak',
232
        'sl' => 'slovenian',
233
        'sr' => 'serbian',
234
        'fi' => 'finnish',
235
        'sv' => 'swedish',
236
        'th' => 'thai',
237
        'tr' => 'turkish',
238
        'uk' => 'ukrainian',
239
        'vi' => 'vietnamese',
240
        'sw' => 'swahili',
241
        'yo' => 'yoruba'
242
    );
243
244
    $system_available_languages = & get_language_folder_list();
245
246
    $accept_languages = strtolower(str_replace('_', '-', $_SERVER['HTTP_ACCEPT_LANGUAGE']));
247
    foreach ($language_index as $code => $language) {
248
        if (strpos($accept_languages, $code) === 0) {
249
            if (!empty($system_available_languages[$language])) {
250
                return $language;
251
            }
252
        }
253
    }
254
255
    $user_agent = strtolower(str_replace('_', '-', $_SERVER['HTTP_USER_AGENT']));
256
    foreach ($language_index as $code => $language) {
257
        if (@preg_match("/[\[\( ]{$code}[;,_\-\)]/", $user_agent)) {
258
            if (!empty($system_available_languages[$language])) {
259
                return $language;
260
            }
261
        }
262
    }
263
264
    return 'english';
265
}
266
267
/*      FILESYSTEM RELATED FUNCTIONS */
268
269
/**
270
 * This function checks if the given folder is writable
271
 * @param   string  $folder Full path to a folder
272
 * @param   bool    $suggestion Whether to show a suggestion or not
273
 * @return  string
274
 */
275 View Code Duplication
function check_writable($folder, $suggestion = false)
276
{
277
    if (is_writable($folder)) {
278
        return Display::label(get_lang('Writable'), 'success');
279
    } else {
280
        if ($suggestion) {
281
            return Display::label(get_lang('NotWritable'), 'info');
282
        } else {
283
            return Display::label(get_lang('NotWritable'), 'important');
284
        }
285
    }
286
}
287
288
/**
289
 * This function checks if the given folder is readable
290
 * @param   string  $folder Full path to a folder
291
 * @param   bool    $suggestion Whether to show a suggestion or not
292
 *
293
 * @return  string
294
 */
295 View Code Duplication
function checkReadable($folder, $suggestion = false)
296
{
297
    if (is_readable($folder)) {
298
        return Display::label(get_lang('Readable'), 'success');
299
    } else {
300
        if ($suggestion) {
301
            return Display::label(get_lang('NotReadable'), 'info');
302
        } else {
303
            return Display::label(get_lang('NotReadable'), 'important');
304
        }
305
    }
306
}
307
308
/**
309
 * This function is similar to the core file() function, except that it
310
 * works with line endings in Windows (which is not the case of file())
311
 * @param   string  $filename
312
 *
313
 * @return  array   The lines of the file returned as an array
314
 */
315
function file_to_array($filename)
316
{
317
    if (!is_readable($filename) || is_dir($filename)) {
318
        return array();
319
    }
320
    $fp = fopen($filename, 'rb');
321
    $buffer = fread($fp, filesize($filename));
322
    fclose($fp);
323
324
    return explode('<br />', nl2br($buffer));
325
}
326
327
/**
328
 * We assume this function is called from install scripts that reside inside the install folder.
329
 */
330
function set_file_folder_permissions()
331
{
332
    @chmod('.', 0755); //set permissions on install dir
333
    @chmod('..', 0755); //set permissions on parent dir of install dir
334
}
335
336
/**
337
 * Write the main system config file
338
 * @param string $path Path to the config file
339
 */
340
function write_system_config_file($path)
341
{
342
    global $dbHostForm;
343
    global $dbPortForm;
344
    global $dbUsernameForm;
345
    global $dbPassForm;
346
    global $dbNameForm;
347
    global $urlForm;
348
    global $pathForm;
349
    global $urlAppendPath;
350
    global $languageForm;
351
    global $encryptPassForm;
352
    global $session_lifetime;
353
    global $new_version;
354
    global $new_version_stable;
355
356
    $root_sys = api_add_trailing_slash(str_replace('\\', '/', realpath($pathForm)));
357
    $content = file_get_contents(dirname(__FILE__).'/'.SYSTEM_CONFIG_FILENAME);
358
359
    $config['{DATE_GENERATED}'] = date('r');
360
    $config['{DATABASE_HOST}'] = $dbHostForm;
361
    $config['{DATABASE_PORT}'] = $dbPortForm;
362
    $config['{DATABASE_USER}'] = $dbUsernameForm;
363
    $config['{DATABASE_PASSWORD}'] = $dbPassForm;
364
    $config['{DATABASE_MAIN}'] = $dbNameForm;
365
    $config['{ROOT_WEB}'] = $urlForm;
366
    $config['{ROOT_SYS}'] = $root_sys;
367
    $config['{URL_APPEND_PATH}'] = $urlAppendPath;
368
    $config['{PLATFORM_LANGUAGE}'] = $languageForm;
369
    $config['{SECURITY_KEY}'] = md5(uniqid(rand().time()));
370
    $config['{ENCRYPT_PASSWORD}'] = $encryptPassForm;
371
372
    $config['SESSION_LIFETIME'] = $session_lifetime;
373
    $config['{NEW_VERSION}'] = $new_version;
374
    $config['NEW_VERSION_STABLE'] = trueFalse($new_version_stable);
375
376
    foreach ($config as $key => $value) {
377
        $content = str_replace($key, $value, $content);
378
    }
379
    $fp = @ fopen($path, 'w');
380
381
    if (!$fp) {
382
        echo '<strong><font color="red">Your script doesn\'t have write access to the config directory</font></strong><br />
383
                        <em>('.str_replace('\\', '/', realpath($path)).')</em><br /><br />
384
                        You probably do not have write access on Chamilo root directory,
385
                        i.e. you should <em>CHMOD 777</em> or <em>755</em> or <em>775</em>.<br /><br />
386
                        Your problems can be related on two possible causes:<br />
387
                        <ul>
388
                          <li>Permission problems.<br />Try initially with <em>chmod -R 777</em> and increase restrictions gradually.</li>
389
                          <li>PHP is running in <a href="http://www.php.net/manual/en/features.safe-mode.php" target="_blank">Safe-Mode</a>. If possible, try to switch it off.</li>
390
                        </ul>
391
                        <a href="http://forum.chamilo.org/" target="_blank">Read about this problem in Support Forum</a><br /><br />
392
                        Please go back to step 5.
393
                        <p><input type="submit" name="step5" value="&lt; Back" /></p>
394
                        </td></tr></table></form></body></html>';
395
        exit;
396
    }
397
398
    fwrite($fp, $content);
399
    fclose($fp);
400
}
401
402
/**
403
 * Returns a list of language directories.
404
 */
405
function & get_language_folder_list()
406
{
407
    static $result;
408
    if (!is_array($result)) {
409
        $result = array();
410
        $exceptions = array('.', '..', 'CVS', '.svn');
411
        $search       = array('_latin',   '_unicode',   '_corporate',   '_org'  , '_KM',   '_');
412
        $replace_with = array(' (Latin)', ' (unicode)', ' (corporate)', ' (org)', ' (KM)', ' ');
413
        $dirname = api_get_path(SYS_LANG_PATH);
414
        $handle = opendir($dirname);
415
        while ($entries = readdir($handle)) {
416
            if (in_array($entries, $exceptions)) {
417
                continue;
418
            }
419
            if (is_dir($dirname.$entries)) {
420
                if (is_file($dirname.$entries.'/install_disabled')) {
421
                    // Skip all languages that have this file present, just for
422
                    // the install process (languages incomplete)
423
                    continue;
424
                }
425
                $result[$entries] = ucwords(str_replace($search, $replace_with, $entries));
426
            }
427
        }
428
        closedir($handle);
429
        asort($result);
430
    }
431
432
    return $result;
433
}
434
435
/**
436
 * TODO: my_directory_to_array() - maybe within the main API there is already a suitable function?
437
 * @param   string  $directory  Full path to a directory
438
 * @return  array   A list of files and dirs in the directory
439
 */
440 View Code Duplication
function my_directory_to_array($directory)
441
{
442
    $array_items = array();
443
    if ($handle = opendir($directory)) {
444
        while (false !== ($file = readdir($handle))) {
445
            if ($file != "." && $file != "..") {
446
                if (is_dir($directory. "/" . $file)) {
447
                    $array_items = array_merge($array_items, my_directory_to_array($directory. '/' . $file));
448
                    $file = $directory . "/" . $file;
449
                    $array_items[] = preg_replace("/\/\//si", '/', $file);
450
                }
451
            }
452
        }
453
        closedir($handle);
454
    }
455
    return $array_items;
456
}
457
458
/**
459
 * This function returns the value of a parameter from the configuration file
460
 *
461
 * WARNING - this function relies heavily on global variables $updateFromConfigFile
462
 * and $configFile, and also changes these globals. This can be rewritten.
463
 *
464
 * @param   string  $param  the parameter of which the value is returned
465
 * @param   string  If we want to give the path rather than take it from POST
466
 * @return  string  the value of the parameter
467
 * @author Olivier Brouckaert
468
 * @author Reworked by Ivan Tcholakov, 2010
469
 */
470
function get_config_param($param, $updatePath = '')
471
{
472
    global $configFile, $updateFromConfigFile;
473
474
    // Look if we already have the queried parameter.
475
    if (is_array($configFile) && isset($configFile[$param])) {
476
        return $configFile[$param];
477
    }
478
    if (empty($updatePath) && !empty($_POST['updatePath'])) {
479
        $updatePath = $_POST['updatePath'];
480
    }
481
482
    if (empty($updatePath)) {
483
        $updatePath = api_get_path(SYS_PATH);
484
    }
485
    $updatePath = api_add_trailing_slash(str_replace('\\', '/', realpath($updatePath)));
486
    $updateFromInstalledVersionFile = '';
487
488
    if (empty($updateFromConfigFile)) {
489
        // If update from previous install was requested,
490
        // try to recover config file from Chamilo 1.9.x
491
        if (file_exists($updatePath.'main/inc/conf/configuration.php')) {
492
            $updateFromConfigFile = 'main/inc/conf/configuration.php';
493
        } elseif (file_exists($updatePath . 'app/config/configuration.php')) {
494
            $updateFromConfigFile = 'app/config/configuration.php';
495
        } else {
496
            // Give up recovering.
497
            //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);
498
            return null;
499
        }
500
    }
501
502
    if (file_exists($updatePath.$updateFromConfigFile) &&
503
        !is_dir($updatePath.$updateFromConfigFile)
504
    ) {
505
        require $updatePath.$updateFromConfigFile;
506
        $config = new Zend\Config\Config($_configuration);
0 ignored issues
show
Bug introduced by
The variable $_configuration does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
507
        return $config->get($param);
508
    }
509
510
    error_log('Config array could not be found in get_config_param()', 0);
511
    return null;
512
513
    /*if (file_exists($updatePath.$updateFromConfigFile)) {
514
        return $val;
515
    } else {
516
        error_log('Config array could not be found in get_config_param()', 0);
517
        return null;
518
    }*/
519
}
520
521
/*      DATABASE RELATED FUNCTIONS */
522
523
/**
524
 * Gets a configuration parameter from the database. Returns returns null on failure.
525
 * @param   string  $param Name of param we want
526
 * @return  mixed   The parameter value or null if not found
527
 */
528
function get_config_param_from_db($param = '')
529
{
530 View Code Duplication
    if (($res = Database::query("SELECT * FROM settings_current WHERE variable = '$param'")) !== false) {
531
        if (Database::num_rows($res) > 0) {
532
            $row = Database::fetch_array($res);
533
            return $row['selected_value'];
534
        }
535
    }
536
    return null;
537
}
538
539
/**
540
 * Connect to the database and returns the entity manager
541
 * @param string  $dbHostForm DB host
542
 * @param string  $dbUsernameForm DB username
543
 * @param string  $dbPassForm DB password
544
 * @param string  $dbNameForm DB name
545
 * @param int     $dbPortForm DB port
546
 *
547
 * @return EntityManager
548
 */
549
function connectToDatabase($dbHostForm, $dbUsernameForm, $dbPassForm, $dbNameForm, $dbPortForm = 3306)
550
{
551
    $dbParams = array(
552
        'driver' => 'pdo_mysql',
553
        'host' => $dbHostForm,
554
        'port' => $dbPortForm,
555
        'user' => $dbUsernameForm,
556
        'password' => $dbPassForm,
557
        'dbname' => $dbNameForm
558
    );
559
560
    $database = new \Database();
561
    $database->connect($dbParams);
562
563
    return $database->getManager();
564
}
565
566
/*      DISPLAY FUNCTIONS */
567
568
/**
569
 * This function prints class=active_step $current_step=$param
570
 * @param   int $param  A step in the installer process
571
 * @author Patrick Cool <[email protected]>, Ghent University
572
 */
573
function step_active($param)
574
{
575
    global $current_step;
576
    if ($param == $current_step) {
577
        echo 'class="current-step" ';
578
    }
579
}
580
581
/**
582
 * This function displays the Step X of Y -
583
 * @return  string  String that says 'Step X of Y' with the right values
584
 */
585
function display_step_sequence()
586
{
587
    global $current_step;
588
    return get_lang('Step'.$current_step).' &ndash; ';
589
}
590
591
/**
592
 * Displays a drop down box for selection the preferred language.
593
 */
594
function display_language_selection_box($name = 'language_list', $default_language = 'english')
595
{
596
    // Reading language list.
597
    $language_list = get_language_folder_list();
598
599
    /*
600
    // Reduction of the number of languages shown. Enable this fragment of code for customization purposes.
601
    // Modify the language list according to your preference. Don't exclude the 'english' item.
602
    $language_to_display = array('asturian', 'bulgarian', 'english', 'italian', 'french', 'slovenian', 'slovenian_unicode', 'spanish');
603
    foreach ($language_list as $key => & $value) {
604
        if (!in_array($key, $language_to_display)) {
605
            unset($language_list[$key]);
606
        }
607
    }
608
    */
609
610
    // Sanity checks due to the possibility for customizations.
611
    if (!is_array($language_list) || empty($language_list)) {
612
        $language_list = array('english' => 'English');
613
    }
614
615
    // Sorting again, if it is necessary.
616
    //asort($language_list);
617
618
    // More sanity checks.
619
    if (!array_key_exists($default_language, $language_list)) {
620
        if (array_key_exists('english', $language_list)) {
621
            $default_language = 'english';
622
        } else {
623
            $language_keys = array_keys($language_list);
624
            $default_language = $language_keys[0];
625
        }
626
    }
627
628
    // Displaying the box.
629
    $html = '';
630
    $html .= "\t\t<select class='selectpicker show-tick' name=\"$name\">\n";
631
    foreach ($language_list as $key => $value) {
632
        if ($key == $default_language) {
633
            $option_end = ' selected="selected">';
634
        } else {
635
            $option_end = '>';
636
        }
637
        $html .= "\t\t\t<option value=\"$key\"$option_end";
638
        $html .= $value;
639
        $html .= "</option>\n";
640
    }
641
    $html .= "\t\t</select>\n";
642
    return $html;
643
}
644
645
/**
646
 * This function displays a language dropdown box so that the installatioin
647
 * can be done in the language of the user
648
 */
649
function display_language_selection()
650
{ ?>
651
    <h2><?php get_lang('WelcomeToTheChamiloInstaller'); ?></h2>
652
    <div class="RequirementHeading">
653
        <h2><?php echo display_step_sequence(); ?>
654
            <?php echo get_lang('InstallationLanguage');?>
655
        </h2>
656
        <p><?php echo get_lang('PleaseSelectInstallationProcessLanguage'); ?>:</p>
657
        <form id="lang_form" method="post" action="<?php echo api_get_self(); ?>">
658
        <div class="form-group">
659
            <div class="col-sm-4">
660
                <?php echo display_language_selection_box('language_list', api_get_interface_language()); ?>
661
            </div>
662
            <div class="col-sm-6">
663
                <button type="submit" name="step1" class="btn btn-success" value="<?php echo get_lang('Next'); ?>">
664
                    <em class="fa fa-forward"> </em>
665
                    <?php echo get_lang('Next'); ?></button>
666
            </div>
667
        </div>
668
669
        <input type="hidden" name="is_executable" id="is_executable" value="-" />
670
        </form>
671
672
    </div>
673
    <div class="RequirementHeading">
674
        <?php echo get_lang('YourLanguageNotThereContactUs'); ?>
675
    </div>
676
<?php
677
}
678
679
/**
680
 * This function displays the requirements for installing Chamilo.
681
 *
682
 * @param string $installType
683
 * @param boolean $badUpdatePath
684
 * @param boolean $badUpdatePath
685
 * @param string $updatePath The updatePath given (if given)
686
 * @param array $update_from_version_8 The different subversions from version 1.9
687
 *
688
 * @author unknow
689
 * @author Patrick Cool <[email protected]>, Ghent University
690
 */
691
function display_requirements(
692
    $installType,
693
    $badUpdatePath,
694
    $updatePath = '',
695
    $update_from_version_8 = array()
696
) {
697
    global $_setting;
698
    echo '<div class="RequirementHeading"><h2>'.display_step_sequence().get_lang('Requirements')."</h2></div>";
699
    echo '<div class="RequirementText">';
700
    echo '<strong>'.get_lang('ReadThoroughly').'</strong><br />';
701
    echo get_lang('MoreDetails').' <a href="../../documentation/installation_guide.html" target="_blank">'.get_lang('ReadTheInstallationGuide').'</a>.<br />'."\n";
702
703
    if ($installType == 'update') {
704
        echo get_lang('IfYouPlanToUpgradeFromOlderVersionYouMightWantToHaveAlookAtTheChangelog').'<br />';
705
    }
706
    echo '</div>';
707
708
    $properlyAccessUrl =  checkAccessUrl();
709
710 View Code Duplication
    if (!$properlyAccessUrl) {
711
        echo '
712
            <div class="alert alert-danger">
713
                ' . Display::return_icon('error.png', get_lang('Error'), [], ICON_SIZE_MEDIUM) .
714
            ' ' .
715
            sprintf(get_lang('InstallMultiURLDetectedNotMainURL'), api_get_configuration_value('root_web')) . '
716
            </div>
717
        ';
718
    }
719
720
    //  SERVER REQUIREMENTS
721
    echo '<div class="RequirementHeading"><h4>'.get_lang('ServerRequirements').'</h4>';
722
723
    $timezone = checkPhpSettingExists("date.timezone");
724
    if (!$timezone) {
725
        echo "<div class='alert alert-warning'>".
726
            Display::return_icon('warning.png', get_lang('Warning'), '', ICON_SIZE_MEDIUM).
727
            get_lang("DateTimezoneSettingNotSet")."</div>";
728
    }
729
730
    echo '<div class="RequirementText">'.get_lang('ServerRequirementsInfo').'</div>';
731
    echo '<div class="RequirementContent">';
732
    echo '<table class="table">
733
            <tr>
734
                <td class="requirements-item">'.get_lang('PHPVersion').' >= '.REQUIRED_PHP_VERSION.'</td>
735
                <td class="requirements-value">';
736
    if (phpversion() < REQUIRED_PHP_VERSION) {
737
        echo '<strong><font color="red">'.get_lang('PHPVersionError').'</font></strong>';
738
    } else {
739
        echo '<strong><font color="green">'.get_lang('PHPVersionOK'). ' '.phpversion().'</font></strong>';
740
    }
741
    echo '</td>
742
            </tr>
743
            <tr>
744
                <td class="requirements-item"><a href="http://php.net/manual/en/book.session.php" target="_blank">Session</a> '.get_lang('Support').'</td>
745
                <td class="requirements-value">'.checkExtension('session', get_lang('Yes'), get_lang('ExtensionSessionsNotAvailable')).'</td>
746
            </tr>
747
            <tr>
748
                <td class="requirements-item"><a href="http://php.net/manual/en/book.mysql.php" target="_blank">pdo_mysql</a> '.get_lang('Support').'</td>
749
                <td class="requirements-value">'.checkExtension('pdo_mysql', get_lang('Yes'), get_lang('ExtensionMySQLNotAvailable')).'</td>
750
            </tr>
751
            <tr>
752
                <td class="requirements-item"><a href="http://php.net/manual/en/book.zip.php" target="_blank">Zip</a> '.get_lang('Support').'</td>
753
                <td class="requirements-value">'.checkExtension('zip', get_lang('Yes'), get_lang('ExtensionNotAvailable')).'</td>
754
            </tr>
755
            <tr>
756
                <td class="requirements-item"><a href="http://php.net/manual/en/book.zlib.php" target="_blank">Zlib</a> '.get_lang('Support').'</td>
757
                <td class="requirements-value">'.checkExtension('zlib', get_lang('Yes'), get_lang('ExtensionZlibNotAvailable')).'</td>
758
            </tr>
759
            <tr>
760
                <td class="requirements-item"><a href="http://php.net/manual/en/book.pcre.php" target="_blank">Perl-compatible regular expressions</a> '.get_lang('Support').'</td>
761
                <td class="requirements-value">'.checkExtension('pcre', get_lang('Yes'), get_lang('ExtensionPCRENotAvailable')).'</td>
762
            </tr>
763
            <tr>
764
                <td class="requirements-item"><a href="http://php.net/manual/en/book.xml.php" target="_blank">XML</a> '.get_lang('Support').'</td>
765
                <td class="requirements-value">'.checkExtension('xml', get_lang('Yes'), get_lang('No')).'</td>
766
            </tr>
767
            <tr>
768
                <td class="requirements-item"><a href="http://php.net/manual/en/book.intl.php" target="_blank">Internationalization</a> '.get_lang('Support').'</td>
769
                <td class="requirements-value">'.checkExtension('intl', get_lang('Yes'), get_lang('No')).'</td>
770
            </tr>
771
               <tr>
772
                <td class="requirements-item"><a href="http://php.net/manual/en/book.json.php" target="_blank">JSON</a> '.get_lang('Support').'</td>
773
                <td class="requirements-value">'.checkExtension('json', get_lang('Yes'), get_lang('No')).'</td>
774
            </tr>
775
             <tr>
776
                <td class="requirements-item"><a href="http://php.net/manual/en/book.image.php" target="_blank">GD</a> '.get_lang('Support').'</td>
777
                <td class="requirements-value">'.checkExtension('gd', get_lang('Yes'), get_lang('ExtensionGDNotAvailable')).'</td>
778
            </tr>
779
            <tr>
780
                <td class="requirements-item"><a href="http://php.net/manual/en/book.curl.php" target="_blank">cURL</a>'.get_lang('Support').'</td>
781
                <td class="requirements-value">'.checkExtension('curl', get_lang('Yes'), get_lang('No')).'</td>
782
            </tr>
783
784
            <tr>
785
                <td class="requirements-item"><a href="http://php.net/manual/en/book.mbstring.php" target="_blank">Multibyte string</a> '.get_lang('Support').' ('.get_lang('Optional').')</td>
786
                <td class="requirements-value">'.checkExtension('mbstring', get_lang('Yes'), get_lang('ExtensionMBStringNotAvailable'), true).'</td>
787
            </tr>
788
            <tr>
789
                <td class="requirements-item"><a href="http://php.net/opcache" target="_blank">Zend OpCache</a> '.get_lang('Support').' ('.get_lang('Optional').')</td>
790
                <td class="requirements-value">'.checkExtension('Zend OPcache', get_lang('Yes'), get_lang('No'), true, 'opcache.enable').'</td>
791
            </tr>
792
            <tr>
793
                <td class="requirements-item"><a href="http://php.net/apcu" target="_blank">APCu</a> '.get_lang('Support').' ('.get_lang('Optional').')</td>
794
                <td class="requirements-value">'.checkExtension('apcu', get_lang('Yes'), get_lang('No'), true, 'apc.enabled').'</td>
795
            </tr>
796
            <tr>
797
                <td class="requirements-item"><a href="http://php.net/manual/en/book.iconv.php" target="_blank">Iconv</a> '.get_lang('Support').' ('.get_lang('Optional').')</td>
798
                <td class="requirements-value">'.checkExtension('iconv', get_lang('Yes'), get_lang('No'), true).'</td>
799
            </tr>
800
            <tr>
801
                <td class="requirements-item"><a href="http://php.net/manual/en/book.ldap.php" target="_blank">LDAP</a> '.get_lang('Support').' ('.get_lang('Optional').')</td>
802
                <td class="requirements-value">'.checkExtension('ldap', get_lang('Yes'), get_lang('ExtensionLDAPNotAvailable'), true).'</td>
803
            </tr>
804
            <tr>
805
                <td class="requirements-item"><a href="http://xapian.org/" target="_blank">Xapian</a> '.get_lang('Support').' ('.get_lang('Optional').')</td>
806
                <td class="requirements-value">'.checkExtension('xapian', get_lang('Yes'), get_lang('No'), true).'</td>
807
            </tr>
808
        </table>';
809
    echo '</div>';
810
    echo '</div>';
811
812
    // RECOMMENDED SETTINGS
813
    // Note: these are the settings for Joomla, does this also apply for Chamilo?
814
    // Note: also add upload_max_filesize here so that large uploads are possible
815
    echo '<div class="RequirementHeading"><h4>'.get_lang('RecommendedSettings').'</h4>';
816
    echo '<div class="RequirementText">'.get_lang('RecommendedSettingsInfo').'</div>';
817
    echo '<div class="RequirementContent">';
818
    echo '<table class="table">
819
            <tr>
820
                <th>'.get_lang('Setting').'</th>
821
                <th>'.get_lang('Recommended').'</th>
822
                <th>'.get_lang('Actual').'</th>
823
            </tr>
824
            <tr>
825
                <td class="requirements-item"><a href="http://php.net/manual/features.safe-mode.php">Safe Mode</a></td>
826
                <td class="requirements-recommended">'.Display::label('OFF', 'success').'</td>
827
                <td class="requirements-value">'.checkPhpSetting('safe_mode', 'OFF').'</td>
828
            </tr>
829
            <tr>
830
                <td class="requirements-item"><a href="http://php.net/manual/ref.errorfunc.php#ini.display-errors">Display Errors</a></td>
831
                <td class="requirements-recommended">'.Display::label('OFF', 'success').'</td>
832
                <td class="requirements-value">'.checkPhpSetting('display_errors', 'OFF').'</td>
833
            </tr>
834
            <tr>
835
                <td class="requirements-item"><a href="http://php.net/manual/ini.core.php#ini.file-uploads">File Uploads</a></td>
836
                <td class="requirements-recommended">'.Display::label('ON', 'success').'</td>
837
                <td class="requirements-value">'.checkPhpSetting('file_uploads', 'ON').'</td>
838
            </tr>
839
            <tr>
840
                <td class="requirements-item"><a href="http://php.net/manual/ref.info.php#ini.magic-quotes-gpc">Magic Quotes GPC</a></td>
841
                <td class="requirements-recommended">'.Display::label('OFF', 'success').'</td>
842
                <td class="requirements-value">'.checkPhpSetting('magic_quotes_gpc', 'OFF').'</td>
843
            </tr>
844
            <tr>
845
                <td class="requirements-item"><a href="http://php.net/manual/ref.info.php#ini.magic-quotes-runtime">Magic Quotes Runtime</a></td>
846
                <td class="requirements-recommended">'.Display::label('OFF', 'success').'</td>
847
                <td class="requirements-value">'.checkPhpSetting('magic_quotes_runtime', 'OFF').'</td>
848
            </tr>
849
            <tr>
850
                <td class="requirements-item"><a href="http://php.net/manual/security.globals.php">Register Globals</a></td>
851
                <td class="requirements-recommended">'.Display::label('OFF', 'success').'</td>
852
                <td class="requirements-value">'.checkPhpSetting('register_globals', 'OFF').'</td>
853
            </tr>
854
            <tr>
855
                <td class="requirements-item"><a href="http://php.net/manual/ref.session.php#ini.session.auto-start">Session auto start</a></td>
856
                <td class="requirements-recommended">'.Display::label('OFF', 'success').'</td>
857
                <td class="requirements-value">'.checkPhpSetting('session.auto_start', 'OFF').'</td>
858
            </tr>
859
            <tr>
860
                <td class="requirements-item"><a href="http://php.net/manual/ini.core.php#ini.short-open-tag">Short Open Tag</a></td>
861
                <td class="requirements-recommended">'.Display::label('OFF', 'success').'</td>
862
                <td class="requirements-value">'.checkPhpSetting('short_open_tag', 'OFF').'</td>
863
            </tr>
864
            <tr>
865
                <td class="requirements-item"><a href="http://www.php.net/manual/en/session.configuration.php#ini.session.cookie-httponly">Cookie HTTP Only</a></td>
866
                <td class="requirements-recommended">'.Display::label('ON', 'success').'</td>
867
                <td class="requirements-value">'.checkPhpSetting('session.cookie_httponly', 'ON').'</td>
868
            </tr>
869
            <tr>
870
                <td class="requirements-item"><a href="http://php.net/manual/ini.core.php#ini.upload-max-filesize">Maximum upload file size</a></td>
871
                <td class="requirements-recommended">'.Display::label('>= '.REQUIRED_MIN_UPLOAD_MAX_FILESIZE.'M', 'success').'</td>
872
                <td class="requirements-value">'.compare_setting_values(ini_get('upload_max_filesize'), REQUIRED_MIN_UPLOAD_MAX_FILESIZE).'</td>
873
            </tr>
874
            <tr>
875
                <td class="requirements-item"><a href="http://php.net/manual/ini.core.php#ini.post-max-size">Maximum post size</a></td>
876
                <td class="requirements-recommended">'.Display::label('>= '.REQUIRED_MIN_POST_MAX_SIZE.'M', 'success').'</td>
877
                <td class="requirements-value">'.compare_setting_values(ini_get('post_max_size'), REQUIRED_MIN_POST_MAX_SIZE).'</td>
878
            </tr>
879
            <tr>
880
                <td class="requirements-item"><a href="http://www.php.net/manual/en/ini.core.php#ini.memory-limit">Memory Limit</a></td>
881
                <td class="requirements-recommended">'.Display::label('>= '.REQUIRED_MIN_MEMORY_LIMIT.'M', 'success').'</td>
882
                <td class="requirements-value">'.compare_setting_values(ini_get('memory_limit'), REQUIRED_MIN_MEMORY_LIMIT).'</td>
883
            </tr>
884
          </table>';
885
    echo '  </div>';
886
    echo '</div>';
887
888
    // DIRECTORY AND FILE PERMISSIONS
889
    echo '<div class="RequirementHeading"><h4>'.get_lang('DirectoryAndFilePermissions').'</h4>';
890
    echo '<div class="RequirementText">'.get_lang('DirectoryAndFilePermissionsInfo').'</div>';
891
    echo '<div class="RequirementContent">';
892
893
    $course_attempt_name = '__XxTestxX__';
894
    $course_dir = api_get_path(SYS_COURSE_PATH).$course_attempt_name;
895
896
    //Just in case
897
    @unlink($course_dir.'/test.php');
898
    @rmdir($course_dir);
899
900
    $perms_dir = array(0777, 0755, 0775, 0770, 0750, 0700);
901
    $perms_fil = array(0666, 0644, 0664, 0660, 0640, 0600);
902
903
    $course_test_was_created = false;
904
905
    $dir_perm_verified = 0777;
906
    foreach ($perms_dir as $perm) {
907
        $r = @mkdir($course_dir, $perm);
908
        if ($r === true) {
909
            $dir_perm_verified = $perm;
910
            $course_test_was_created = true;
911
            break;
912
        }
913
    }
914
915
    $fil_perm_verified = 0666;
916
    $file_course_test_was_created = false;
917
918
    if (is_dir($course_dir)) {
919
        foreach ($perms_fil as $perm) {
920
            if ($file_course_test_was_created == true) {
921
                break;
922
            }
923
            $r = @touch($course_dir.'/test.php', $perm);
924
            if ($r === true) {
925
                $fil_perm_verified = $perm;
926
                if (check_course_script_interpretation($course_dir, $course_attempt_name, 'test.php')) {
927
                    $file_course_test_was_created = true;
928
                }
929
            }
930
        }
931
    }
932
933
    @unlink($course_dir.'/test.php');
934
    @rmdir($course_dir);
935
936
    $_SESSION['permissions_for_new_directories'] = $_setting['permissions_for_new_directories'] = $dir_perm_verified;
937
    $_SESSION['permissions_for_new_files'] = $_setting['permissions_for_new_files'] = $fil_perm_verified;
938
939
    $dir_perm = Display::label('0'.decoct($dir_perm_verified), 'info');
940
    $file_perm = Display::label('0'.decoct($fil_perm_verified), 'info');
941
942
    $courseTestLabel = Display::label(get_lang('No'), 'important');
943
944
    if ($course_test_was_created && $file_course_test_was_created) {
945
        $courseTestLabel = Display::label(get_lang('Yes'), 'success');
946
    }
947
948
    if ($course_test_was_created && !$file_course_test_was_created) {
949
        $courseTestLabel = Display::label(get_lang('Warning'), 'warning');
950
        $courseTestLabel .= '<br />'.sprintf(
951
            get_lang('InstallWarningCouldNotInterpretPHP'),
952
            api_get_path(WEB_COURSE_PATH).$course_attempt_name.'/test.php'
953
        );
954
    }
955
956
    if (!$course_test_was_created && !$file_course_test_was_created) {
957
        $courseTestLabel = Display::label(get_lang('No'), 'important');
958
    }
959
960
    $oldConf = '';
961 View Code Duplication
    if (file_exists(api_get_path(SYS_CODE_PATH).'inc/conf/configuration.php')) {
962
        $oldConf = '<tr>
963
            <td class="requirements-item">'.api_get_path(SYS_CODE_PATH).'inc/conf</td>
964
            <td class="requirements-value">'.check_writable(api_get_path(SYS_CODE_PATH).'inc/conf').'</td>
965
        </tr>';
966
    }
967
968
    echo '<table class="table">
969
            '.$oldConf.'
970
            <tr>
971
                <td class="requirements-item">'.api_get_path(SYS_APP_PATH).'</td>
972
                <td class="requirements-value">'.check_writable(api_get_path(SYS_APP_PATH)).'</td>
973
            </tr>
974
            <tr>
975
                <td class="requirements-item">'.api_get_path(SYS_CODE_PATH).'default_course_document/images/</td>
976
                <td class="requirements-value">'.check_writable(api_get_path(SYS_CODE_PATH).'default_course_document/images/').'</td>
977
            </tr>
978
            <tr>
979
                <td class="requirements-item">'.api_get_path(SYS_CODE_PATH).'lang/</td>
980
                <td class="requirements-value">'.check_writable(api_get_path(SYS_CODE_PATH).'lang/', true).' <br />('.get_lang('SuggestionOnlyToEnableSubLanguageFeatureOrUpgradeProcess').')</td>
981
            </tr>
982
            <tr>
983
                <td class="requirements-item">'.api_get_path(SYS_PATH).'vendor/</td>
984
                <td class="requirements-value">'.checkReadable(api_get_path(SYS_PATH).'vendor').'</td>
985
            </tr>
986
            <tr>
987
                <td class="requirements-item">'.api_get_path(SYS_PUBLIC_PATH).'</td>
988
                <td class="requirements-value">'.check_writable(api_get_path(SYS_PUBLIC_PATH)).'</td>
989
            </tr>
990
            <tr>
991
                <td class="requirements-item">'.get_lang('CourseTestWasCreated').'</td>
992
                <td class="requirements-value">'.$courseTestLabel.' </td>
993
            </tr>
994
            <tr>
995
                <td class="requirements-item">'.get_lang('PermissionsForNewDirs').'</td>
996
                <td class="requirements-value">'.$dir_perm.' </td>
997
            </tr>
998
            <tr>
999
                <td class="requirements-item">'.get_lang('PermissionsForNewFiles').'</td>
1000
                <td class="requirements-value">'.$file_perm.' </td>
1001
            </tr>
1002
        </table>';
1003
    echo '  </div>';
1004
    echo '</div>';
1005
1006
    if ($installType == 'update' && (empty($updatePath) || $badUpdatePath)) {
1007
        if ($badUpdatePath) { ?>
1008
            <div class="alert alert-warning">
1009
                <?php echo get_lang('Error'); ?>!<br />
1010
                Chamilo <?php echo implode('|', $update_from_version_8).' '.get_lang('HasNotBeenFoundInThatDir'); ?>.
1011
            </div>
1012
        <?php }
1013
        else {
1014
            echo '<br />';
1015
        }
1016
        ?>
1017
            <div class="row">
1018
                <div class="col-md-12">
1019
                    <p><?php echo get_lang('OldVersionRootPath'); ?>:
1020
                        <input type="text" name="updatePath" size="50" value="<?php echo ($badUpdatePath && !empty($updatePath)) ? htmlentities($updatePath) : ''; ?>" />
1021
                    </p>
1022
                    <p>
1023
                        <button type="submit" class="btn btn-default" name="step1" value="<?php echo get_lang('Back'); ?>" >
1024
                            <em class="fa fa-backward"> <?php echo get_lang('Back'); ?></em>
1025
                        </button>
1026
                        <input type="hidden" name="is_executable" id="is_executable" value="-" />
1027
                        <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;" >
1028
                            <em class="fa fa-forward"> </em> <?php echo get_lang('Next'); ?>
1029
                        </button>
1030
                    </p>
1031
                </div>
1032
            </div>
1033
1034
        <?php
1035
    } else {
1036
        $error = false;
1037
        // First, attempt to set writing permissions if we don't have them yet
1038
        $perm = api_get_permissions_for_new_directories();
1039
        $perm_file = api_get_permissions_for_new_files();
1040
1041
        $notWritable = array();
1042
1043
        $checked_writable = api_get_path(SYS_APP_PATH);
1044
        if (!is_writable($checked_writable)) {
1045
            $notWritable[] = $checked_writable;
1046
            @chmod($checked_writable, $perm);
1047
        }
1048
1049
        $checked_writable = api_get_path(SYS_PUBLIC_PATH);
1050
        if (!is_writable($checked_writable)) {
1051
            $notWritable[] = $checked_writable;
1052
            @chmod($checked_writable, $perm);
1053
        }
1054
1055
        $checked_writable = api_get_path(SYS_CODE_PATH).'default_course_document/images/';
1056
        if (!is_writable($checked_writable)) {
1057
            $notWritable[] = $checked_writable;
1058
            @chmod($checked_writable, $perm);
1059
        }
1060
1061
        if ($course_test_was_created == false) {
1062
            $error = true;
1063
        }
1064
1065
        $checked_writable = api_get_path(CONFIGURATION_PATH).'configuration.php';
1066
        if (file_exists($checked_writable) && !is_writable($checked_writable)) {
1067
            $notWritable[] = $checked_writable;
1068
            @chmod($checked_writable, $perm_file);
1069
        }
1070
1071
        // Second, if this fails, report an error
1072
1073
        //--> The user would have to adjust the permissions manually
1074
        if (count($notWritable) > 0) {
1075
            $error = true;
1076
            ?>
1077
            <div class="text-danger">
1078
                <h3 class="text-center"><?php echo get_lang('Warning') ?></h3>
1079
                <p>
1080
                    <?php printf(get_lang('NoWritePermissionPleaseReadInstallGuide'), '<a href="../../documentation/installation_guide.html" target="blank">', '</a>'); ?>
1081
                </p>
1082
            </div>
1083
            <?php
1084
            echo '<ul>';
1085
            foreach ($notWritable as $value) {
1086
                echo '<li class="text-danger">'.$value.'</li>';
1087
            }
1088
            echo '</ul>';
1089
        } elseif (file_exists(api_get_path(CONFIGURATION_PATH).'configuration.php')) {
1090
            // Check wether a Chamilo configuration file already exists.
1091
            echo '<div class="alert alert-warning"><h4><center>';
1092
            echo get_lang('WarningExistingLMSInstallationDetected');
1093
            echo '</center></h4></div>';
1094
        }
1095
1096
        $deprecated = [
1097
            api_get_path(SYS_CODE_PATH) . 'exercice/',
1098
            api_get_path(SYS_CODE_PATH) . 'newscorm/',
1099
            api_get_path(SYS_PLUGIN_PATH) . 'ticket/'
1100
        ];
1101
        $deprecatedToRemove = [];
1102
1103
        foreach ($deprecated as $deprecatedDirectory) {
1104
            if (!is_dir($deprecatedDirectory)) {
1105
                continue;
1106
            }
1107
1108
            $deprecatedToRemove[] = $deprecatedDirectory;
1109
        }
1110
1111
        if (count($deprecatedToRemove) > 0) {
1112
            $error = true;
1113
            ?>
1114
            <p class="text-danger"><?php echo get_lang('WarningForDeprecatedDirectoriesForUpgrade') ?></p>
1115
            <ul>
1116
                <?php foreach ($deprecatedToRemove as $deprecatedDirectory) { ?>
1117
                    <li class="text-danger"><?php echo $deprecatedDirectory ?></li>
1118
                <?php } ?>
1119
            </ul>
1120
            <?php
1121
        }
1122
1123
        if (!$properlyAccessUrl) {
1124
            $error = true;
1125
        }
1126
1127
        // And now display the choice buttons (go back or install)
1128
        ?>
1129
        <p align="center" style="padding-top:15px">
1130
        <button type="submit" name="step1" class="btn btn-default" onclick="javascript: window.location='index.php'; return false;" value="<?php echo get_lang('Previous'); ?>" >
1131
            <em class="fa fa-backward"> </em> <?php echo get_lang('Previous'); ?>
1132
        </button>
1133
        <button type="submit" name="step2_install" class="btn btn-success" value="<?php echo get_lang("NewInstallation"); ?>" <?php if ($error) echo 'disabled="disabled"'; ?> >
1134
            <em class="fa fa-forward"> </em> <?php echo get_lang('NewInstallation'); ?>
1135
        </button>
1136
        <input type="hidden" name="is_executable" id="is_executable" value="-" />
1137
            <button type="submit" class="btn btn-default" <?php echo !$error ?: 'disabled="disabled"' ?> name="step2_update_8" value="Upgrade from Chamilo 1.9.x">
1138
                <em class="fa fa-forward" aria-hidden="true"></em> <?php echo get_lang('UpgradeVersion') ?>
1139
            </button>
1140
            </p>
1141
        <?php
1142
    }
1143
}
1144
1145
/**
1146
 * Displays the license (GNU GPL) as step 2, with
1147
 * - an "I accept" button named step3 to proceed to step 3;
1148
 * - a "Back" button named step1 to go back to the first step.
1149
 */
1150
1151
function display_license_agreement()
1152
{
1153
    echo '<div class="RequirementHeading"><h2>'.display_step_sequence().get_lang('Licence').'</h2>';
1154
    echo '<p>'.get_lang('LMSLicenseInfo').'</p>';
1155
    echo '<p><a href="../../documentation/license.html" target="_blank">'.get_lang('PrintVers').'</a></p>';
1156
    echo '</div>';
1157
    ?>
1158
    <div class="row">
1159
        <div class="col-md-12">
1160
            <pre style="overflow: auto; height: 200px; margin-top: 5px;">
1161
                <?php echo api_htmlentities(@file_get_contents(api_get_path(SYS_PATH).'documentation/license.txt')); ?>
1162
            </pre>
1163
            <div class="checkbox">
1164
                <label>
1165
                    <input type="checkbox" name="accept" id="accept_licence" value="1" />
1166
                    <?php echo get_lang('IAccept'); ?>
1167
                </label>
1168
            </div>
1169
        </div>
1170
    </div>
1171
    <div class="row">
1172
        <div class="col-md-12">
1173
            <p class="alert alert-info"><?php echo get_lang('LMSMediaLicense'); ?></p>
1174
        </div>
1175
    </div>
1176
1177
    <!-- Contact information form -->
1178
    <div class="section-parameters">
1179
        <a href="javascript://" class = "advanced_parameters" >
1180
        <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>
1181
        </a>
1182
    </div>
1183
1184
    <div id="id_contact_form" style="display:block">
1185
        <div class="normal-message"><?php echo get_lang('ContactInformationDescription') ?></div>
1186
        <div id="contact_registration">
1187
            <p><?php echo get_contact_registration_form() ?></p><br />
1188
        </div>
1189
    </div>
1190
    <div class="text-center">
1191
    <button type="submit" class="btn btn-default" name="step1" value="&lt; <?php echo get_lang('Previous'); ?>" >
1192
        <em class="fa fa-backward"> </em> <?php echo get_lang('Previous'); ?>
1193
    </button>
1194
    <input type="hidden" name="is_executable" id="is_executable" value="-" />
1195
    <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;" >
1196
        <em class="fa fa-forward"> </em> <?php echo get_lang('Next'); ?>
1197
    </button>
1198
    </div>
1199
    <?php
1200
}
1201
1202
1203
/**
1204
 * Get contact registration form
1205
 */
1206
function get_contact_registration_form()
1207
{
1208
1209
    $html ='
1210
   <div class="form-horizontal">
1211
    <div class="panel panel-default">
1212
    <div class="panel-body">
1213
    <div id="div_sent_information"></div>
1214
    <div class="form-group">
1215
            <label class="col-sm-3"><span class="form_required">*</span>'.get_lang('Name').'</label>
1216
            <div class="col-sm-9"><input id="person_name" class="form-control" type="text" name="person_name" size="30" /></div>
1217
    </div>
1218
    <div class="form-group">
1219
            <label class="col-sm-3"><span class="form_required">*</span>'.get_lang('Email').'</label>
1220
            <div class="col-sm-9"><input id="person_email" class="form-control" type="text" name="person_email" size="30" /></div>
1221
    </div>
1222
    <div class="form-group">
1223
            <label class="col-sm-3"><span class="form_required">*</span>'.get_lang('CompanyName').'</label>
1224
            <div class="col-sm-9"><input id="company_name" class="form-control" type="text" name="company_name" size="30" /></div>
1225
    </div>
1226
    <div class="form-group">
1227
        <label class="col-sm-3"><span class="form_required">*</span>'.get_lang('CompanyActivity').'</label>
1228
        <div class="col-sm-9">
1229
            <select class="selectpicker show-tick" name="company_activity" id="company_activity" >
1230
                <option value="">--- '.get_lang('SelectOne').' ---</option>
1231
                <Option value="Advertising/Marketing/PR">Advertising/Marketing/PR</Option><Option value="Agriculture/Forestry">Agriculture/Forestry</Option>
1232
                <Option value="Architecture">Architecture</Option><Option value="Banking/Finance">Banking/Finance</Option>
1233
                <Option value="Biotech/Pharmaceuticals">Biotech/Pharmaceuticals</Option><Option value="Business Equipment">Business Equipment</Option>
1234
                <Option value="Business Services">Business Services</Option><Option value="Construction">Construction</Option>
1235
                <Option value="Consulting/Research">Consulting/Research</Option><Option value="Education">Education</Option>
1236
                <Option value="Engineering">Engineering</Option><Option value="Environmental">Environmental</Option>
1237
                <Option value="Government">Government</Option><Option value="Healthcare">Health Care</Option>
1238
                <Option value="Hospitality/Lodging/Travel">Hospitality/Lodging/Travel</Option><Option value="Insurance">Insurance</Option>
1239
                <Option value="Legal">Legal</Option><Option value="Manufacturing">Manufacturing</Option>
1240
                <Option value="Media/Entertainment">Media/Entertainment</Option><Option value="Mortgage">Mortgage</Option>
1241
                <Option value="Non-Profit">Non-Profit</Option><Option value="Real Estate">Real Estate</Option>
1242
                <Option value="Restaurant">Restaurant</Option><Option value="Retail">Retail</Option>
1243
                <Option value="Shipping/Transportation">Shipping/Transportation</Option>
1244
                <Option value="Technology">Technology</Option><Option value="Telecommunications">Telecommunications</Option>
1245
                <Option value="Other">Other</Option>
1246
            </select>
1247
        </div>
1248
    </div>
1249
1250
    <div class="form-group">
1251
        <label class="col-sm-3"><span class="form_required">*</span>'.get_lang('PersonRole').'</label>
1252
        <div class="col-sm-9">
1253
            <select class="selectpicker show-tick" name="person_role" id="person_role" >
1254
                <option value="">--- '.get_lang('SelectOne').' ---</option>
1255
                <Option value="Administration">Administration</Option><Option value="CEO/President/ Owner">CEO/President/ Owner</Option>
1256
                <Option value="CFO">CFO</Option><Option value="CIO/CTO">CIO/CTO</Option>
1257
                <Option value="Consultant">Consultant</Option><Option value="Customer Service">Customer Service</Option>
1258
                <Option value="Engineer/Programmer">Engineer/Programmer</Option><Option value="Facilities/Operations">Facilities/Operations</Option>
1259
                <Option value="Finance/ Accounting Manager">Finance/ Accounting Manager</Option><Option value="Finance/ Accounting Staff">Finance/ Accounting Staff</Option>
1260
                <Option value="General Manager">General Manager</Option><Option value="Human Resources">Human Resources</Option>
1261
                <Option value="IS/IT Management">IS/IT Management</Option><Option value="IS/ IT Staff">IS/ IT Staff</Option>
1262
                <Option value="Marketing Manager">Marketing Manager</Option><Option value="Marketing Staff">Marketing Staff</Option>
1263
                <Option value="Partner/Principal">Partner/Principal</Option><Option value="Purchasing Manager">Purchasing Manager</Option>
1264
                <Option value="Sales/ Business Dev. Manager">Sales/ Business Dev. Manager</Option><Option value="Sales/ Business Dev.">Sales/ Business Dev.</Option>
1265
                <Option value="Vice President/Senior Manager">Vice President/Senior Manager</Option><Option value="Other">Other</Option>
1266
            </select>
1267
        </div>
1268
    </div>
1269
1270
    <div class="form-group">
1271
        <label class="col-sm-3"><span class="form_required">*</span>'.get_lang('CompanyCountry').'</label>
1272
        <div class="col-sm-9">'.get_countries_list_from_array(true).'</div>
1273
    </div>
1274
    <div class="form-group">
1275
        <label class="col-sm-3">'.get_lang('CompanyCity').'</label>
1276
        <div class="col-sm-9">
1277
                <input type="text" class="form-control" id="company_city" name="company_city" size="30" />
1278
        </div>
1279
    </div>
1280
    <div class="form-group">
1281
        <label class="col-sm-3">'.get_lang('WhichLanguageWouldYouLikeToUseWhenContactingYou').'</label>
1282
        <div class="col-sm-9">
1283
            <select class="selectpicker show-tick" id="language" name="language">
1284
                <option value="bulgarian">Bulgarian</option>
1285
                <option value="indonesian">Bahasa Indonesia</option>
1286
                <option value="bosnian">Bosanski</option>
1287
                <option value="german">Deutsch</option>
1288
                <option selected="selected" value="english">English</option>
1289
                <option value="spanish">Spanish</option>
1290
                <option value="french">Français</option>
1291
                <option value="italian">Italian</option>
1292
                <option value="hungarian">Magyar</option>
1293
                <option value="dutch">Nederlands</option>
1294
                <option value="brazilian">Português do Brasil</option>
1295
                <option value="portuguese">Português europeu</option>
1296
                <option value="slovenian">Slovenčina</option>
1297
            </select>
1298
        </div>
1299
    </div>
1300
1301
    <div class="form-group">
1302
        <label class="col-sm-3">'.get_lang('HaveYouThePowerToTakeFinancialDecisions').'</label>
1303
        <div class="col-sm-9">
1304
            <div class="radio">
1305
                <label>
1306
                    <input type="radio" name="financial_decision" id="financial_decision1" value="1" checked /> ' . get_lang('Yes') . '
1307
                </label>
1308
            </div>
1309
            <div class="radio">
1310
                <label>
1311
                    <input type="radio" name="financial_decision" id="financial_decision2" value="0" /> '.get_lang('No').'
1312
                </label>
1313
            </div>
1314
        </div>
1315
    </div>
1316
    <div class="clear"></div>
1317
    <div class="form-group">
1318
            <div class="col-sm-3">&nbsp;</div>
1319
            <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>
1320
    </div>
1321
    <div class="form-group">
1322
            <div class="col-sm-3">&nbsp;</div>
1323
            <div class="col-sm-9"><span class="form_required">*</span><small>'.get_lang('FieldRequired').'</small></div>
1324
    </div></div></div>
1325
    </div>';
1326
1327
    return $html;
1328
}
1329
1330
/**
1331
 * Displays a parameter in a table row.
1332
 * Used by the display_database_settings_form function.
1333
 * @param   string  Type of install
1334
 * @param   string  Name of parameter
1335
 * @param   string  Field name (in the HTML form)
1336
 * @param   string  Field value
1337
 * @param   string  Extra notice (to show on the right side)
1338
 * @param   boolean Whether to display in update mode
1339
 * @param   string  Additional attribute for the <tr> element
1340
 * @return  void    Direct output
1341
 */
1342
function displayDatabaseParameter(
1343
    $installType,
1344
    $parameterName,
1345
    $formFieldName,
1346
    $parameterValue,
1347
    $extra_notice,
1348
    $displayWhenUpdate = true,
1349
    $tr_attribute = ''
1350
) {
1351
    //echo "<tr ".$tr_attribute.">";
1352
    echo "<label class='col-sm-4'>$parameterName</label>";
1353
1354
    if ($installType == INSTALL_TYPE_UPDATE && $displayWhenUpdate) {
1355
        echo '<input type="hidden" name="'.$formFieldName.'" id="'.$formFieldName.'" value="'.api_htmlentities($parameterValue).'" />'.$parameterValue;
1356
    } else {
1357
        $inputType = $formFieldName == 'dbPassForm' ? 'password' : 'text';
1358
1359
        //Slightly limit the length of the database prefix to avoid having to cut down the databases names later on
1360
        $maxLength = $formFieldName == 'dbPrefixForm' ? '15' : MAX_FORM_FIELD_LENGTH;
1361
        if ($installType == INSTALL_TYPE_UPDATE) {
1362
            echo '<input type="hidden" name="'.$formFieldName.'" id="'.$formFieldName.'" value="'.api_htmlentities($parameterValue).'" />';
1363
            echo api_htmlentities($parameterValue);
1364
        } else {
1365
            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>";
1366
            echo '<div class="col-sm-3">' . $extra_notice . '</div>';
1367
        }
1368
    }
1369
}
1370
1371
/**
1372
 * Displays step 3 - a form where the user can enter the installation settings
1373
 * regarding the databases - login and password, names, prefixes, single
1374
 * or multiple databases, tracking or not...
1375
 * @param string $installType
1376
 * @param string $dbHostForm
1377
 * @param string $dbUsernameForm
1378
 * @param string $dbPassForm
1379
 * @param string $dbNameForm
1380
 * @param int    $dbPortForm
1381
 * @param string $installationProfile
1382
 */
1383
function display_database_settings_form(
1384
    $installType,
1385
    $dbHostForm,
1386
    $dbUsernameForm,
1387
    $dbPassForm,
1388
    $dbNameForm,
1389
    $dbPortForm = 3306,
1390
    $installationProfile = ''
1391
) {
1392
    if ($installType == 'update') {
1393
        global $_configuration;
1394
        $dbHostForm = $_configuration['db_host'];
1395
        $dbUsernameForm = $_configuration['db_user'];
1396
        $dbPassForm = $_configuration['db_password'];
1397
        $dbNameForm = $_configuration['main_database'];
1398
        $dbPortForm = isset($_configuration['db_port']) ? $_configuration['db_port'] : '';
1399
1400
        echo '<div class="RequirementHeading"><h2>' . display_step_sequence() .get_lang('DBSetting') . '</h2></div>';
1401
        echo '<div class="RequirementContent">';
1402
        echo get_lang('DBSettingUpgradeIntro');
1403
        echo '</div>';
1404
    } else {
1405
        echo '<div class="RequirementHeading"><h2>' . display_step_sequence() .get_lang('DBSetting') . '</h2></div>';
1406
        echo '<div class="RequirementContent">';
1407
        echo get_lang('DBSettingIntro');
1408
        echo '</div>';
1409
    }
1410
    ?>
1411
    <div class="panel panel-default">
1412
        <div class="panel-body">
1413
        <div class="form-group">
1414
            <label class="col-sm-4"><?php echo get_lang('DBHost'); ?> </label>
1415 View Code Duplication
            <?php if ($installType == 'update'){ ?>
1416
            <div class="col-sm-5">
1417
                <input type="hidden" name="dbHostForm" value="<?php echo htmlentities($dbHostForm); ?>" /><?php echo $dbHostForm; ?>
1418
            </div>
1419
            <div class="col-sm-3"></div>
1420
            <?php }else{ ?>
1421
            <div class="col-sm-5">
1422
                <input type="text" class="form-control" size="25" maxlength="50" name="dbHostForm" value="<?php echo htmlentities($dbHostForm); ?>" />
1423
            </div>
1424
            <div class="col-sm-3"><?php echo get_lang('EG').' localhost'; ?></div>
1425
            <?php } ?>
1426
        </div>
1427
        <div class="form-group">
1428
            <label class="col-sm-4"><?php echo get_lang('DBPort'); ?> </label>
1429 View Code Duplication
            <?php if ($installType == 'update'){ ?>
1430
            <div class="col-sm-5">
1431
                <input type="hidden" name="dbPortForm" value="<?php echo htmlentities($dbPortForm); ?>" /><?php echo $dbPortForm; ?>
1432
            </div>
1433
            <div class="col-sm-3"></div>
1434
            <?php }else{ ?>
1435
            <div class="col-sm-5">
1436
                <input type="text" class="form-control" size="25" maxlength="50" name="dbPortForm" value="<?php echo htmlentities($dbPortForm); ?>" />
1437
            </div>
1438
            <div class="col-sm-3"><?php echo get_lang('EG').' 3306'; ?></div>
1439
            <?php } ?>
1440
        </div>
1441
        <div class="form-group">
1442
            <?php
1443
                //database user username
1444
                $example_login = get_lang('EG').' root';
1445
                displayDatabaseParameter($installType, get_lang('DBLogin'), 'dbUsernameForm', $dbUsernameForm, $example_login);
1446
            ?>
1447
        </div>
1448
        <div class="form-group">
1449
            <?php
1450
            //database user password
1451
            $example_password = get_lang('EG').' '.api_generate_password();
1452
            displayDatabaseParameter($installType, get_lang('DBPassword'), 'dbPassForm', $dbPassForm, $example_password);
1453
1454
            ?>
1455
        </div>
1456
        <div class="form-group">
1457
            <?php
1458
            //Database Name fix replace weird chars
1459
            if ($installType != INSTALL_TYPE_UPDATE) {
1460
                $dbNameForm = str_replace(array('-','*', '$', ' ', '.'), '', $dbNameForm);
1461
            }
1462
1463
            displayDatabaseParameter(
1464
                $installType,
1465
                get_lang('MainDB'),
1466
                'dbNameForm',
1467
                $dbNameForm,
1468
                '&nbsp;',
1469
                null,
1470
                'id="optional_param1"'
1471
                );
1472
            ?>
1473
        </div>
1474
       <?php if ($installType != INSTALL_TYPE_UPDATE) { ?>
1475
        <div class="form-group">
1476
            <div class="col-sm-3"></div>
1477
            <div class="col-sm-9">
1478
            <button type="submit" class="btn btn-primary" name="step3" value="step3">
1479
                <em class="fa fa-refresh"> </em>
1480
                <?php echo get_lang('CheckDatabaseConnection'); ?>
1481
            </button>
1482
            </div>
1483
        </div>
1484
        <?php } ?>
1485
1486
        </div>
1487
    </div>
1488
1489
        <?php
1490
1491
        $database_exists_text = '';
1492
        $manager = null;
1493
        try {
1494
            $manager = connectToDatabase(
1495
                $dbHostForm,
1496
                $dbUsernameForm,
1497
                $dbPassForm,
1498
                null,
1499
                $dbPortForm
1500
            );
1501
            $databases = $manager->getConnection()->getSchemaManager()->listDatabases();
1502
            if (in_array($dbNameForm, $databases)) {
1503
                $database_exists_text = '<div class="alert alert-warning">'.get_lang('ADatabaseWithTheSameNameAlreadyExists').'</div>';
1504
            }
1505
        } catch (Exception $e) {
1506
            $database_exists_text = $e->getMessage();
1507
        }
1508
1509
    if ($manager && $manager->getConnection()->isConnected()): ?>
1510
        <?php echo $database_exists_text ?>
1511
        <div id="db_status" class="alert alert-success">
1512
            Database host: <strong><?php echo $manager->getConnection()->getHost(); ?></strong><br/>
1513
            Database port: <strong><?php echo $manager->getConnection()->getPort(); ?></strong><br/>
1514
            Database driver: <strong><?php echo $manager->getConnection()->getDriver()->getName(); ?></strong><br/>
1515
        </div>
1516
    <?php else: ?>
1517
        <div id="db_status" class="alert alert-danger">
1518
            <p><?php echo get_lang('FailedConectionDatabase'); ?></strong></p>
1519
            <code><?php echo $database_exists_text ?></code>
1520
        </div>
1521
    <?php endif; ?>
1522
   <div class="form-group">
1523
       <div class="col-sm-6">
1524
           <button type="submit" name="step2" class="btn btn-default pull-right" value="&lt; <?php echo get_lang('Previous'); ?>" >
1525
               <em class="fa fa-backward"> </em> <?php echo get_lang('Previous'); ?>
1526
           </button>
1527
       </div>
1528
      <div class="col-sm-6">
1529
       <input type="hidden" name="is_executable" id="is_executable" value="-" />
1530
       <?php if ($manager) { ?>
1531
           <button type="submit"  class="btn btn-success" name="step4" value="<?php echo get_lang('Next'); ?> &gt;" >
1532
               <em class="fa fa-forward"> </em> <?php echo get_lang('Next'); ?>
1533
           </button>
1534
       <?php } else { ?>
1535
           <button disabled="disabled" type="submit" class="btn btn-success disabled" name="step4" value="<?php echo get_lang('Next'); ?> &gt;" >
1536
               <em class="fa fa-forward"> </em> <?php echo get_lang('Next'); ?>
1537
           </button>
1538
       <?php } ?>
1539
      </div>
1540
   </div>
1541
1542
    <?php
1543
}
1544
1545
function panel($content = null, $title = null, $id = null, $style = null) {
1546
    $html = '';
1547
    if (empty($style)) {
1548
        $style = 'default';
1549
    }
1550
    if (!empty($title)) {
1551
        $panelTitle = Display::div($title, array('class' => 'panel-heading'));
1552
        $panelBody = Display::div($content, array('class' => 'panel-body'));
1553
        $panelParent = Display::div($panelTitle . $panelBody, array('id' => $id, 'class' => 'panel panel-'.$style));
1554
    } else {
1555
        $panelBody = Display::div($html, array('class' => 'panel-body'));
1556
        $panelParent = Display::div($panelBody, array('id' => $id, 'class' => 'panel panel-'.$style));
1557
    }
1558
    $html .= $panelParent;
1559
    return $html;
1560
}
1561
1562
/**
1563
 * Displays a parameter in a table row.
1564
 * Used by the display_configuration_settings_form function.
1565
 * @param string $installType
1566
 * @param string $parameterName
1567
 * @param string $formFieldName
1568
 * @param string $parameterValue
1569
 * @param string $displayWhenUpdate
1570
 */
1571
function display_configuration_parameter(
1572
    $installType,
1573
    $parameterName,
1574
    $formFieldName,
1575
    $parameterValue,
1576
    $displayWhenUpdate = 'true'
1577
) {
1578
    $html = '<div class="form-group">';
1579
    $html .= '<label class="col-sm-6 control-label">' . $parameterName . '</label>';
1580
    if ($installType == INSTALL_TYPE_UPDATE && $displayWhenUpdate) {
1581
        $html .= '<input type="hidden" name="' . $formFieldName . '" value="'. api_htmlentities($parameterValue, ENT_QUOTES). '" />' . $parameterValue;
1582
    } else {
1583
        $html .= '<div class="col-sm-6"><input class="form-control" type="text" size="'.FORM_FIELD_DISPLAY_LENGTH.'" maxlength="'.MAX_FORM_FIELD_LENGTH.'" name="'.$formFieldName.'" value="'.api_htmlentities($parameterValue, ENT_QUOTES).'" />'."</div>";
1584
    }
1585
    $html .= "</div>";
1586
1587
    return $html;
1588
}
1589
1590
/**
1591
 * Displays step 4 of the installation - configuration settings about Chamilo itself.
1592
 * @param string $installType
1593
 * @param string $urlForm
1594
 * @param string $languageForm
1595
 * @param string $emailForm
1596
 * @param string $adminFirstName
1597
 * @param string $adminLastName
1598
 * @param string $adminPhoneForm
1599
 * @param string $campusForm
1600
 * @param string $institutionForm
1601
 * @param string $institutionUrlForm
1602
 * @param string $encryptPassForm
1603
 * @param bool $allowSelfReg
1604
 * @param bool $allowSelfRegProf
1605
 * @param string $loginForm
1606
 * @param string $passForm
1607
 */
1608
function display_configuration_settings_form(
1609
    $installType,
1610
    $urlForm,
1611
    $languageForm,
1612
    $emailForm,
1613
    $adminFirstName,
1614
    $adminLastName,
1615
    $adminPhoneForm,
1616
    $campusForm,
1617
    $institutionForm,
1618
    $institutionUrlForm,
1619
    $encryptPassForm,
1620
    $allowSelfReg,
1621
    $allowSelfRegProf,
1622
    $loginForm,
1623
    $passForm
1624
) {
1625
    if ($installType != 'update' && empty($languageForm)) {
1626
        $languageForm = $_SESSION['install_language'];
1627
    }
1628
    echo '<div class="RequirementHeading">';
1629
    echo "<h2>" . display_step_sequence() . get_lang("CfgSetting") . "</h2>";
1630
    echo '</div>';
1631
1632
    echo '<p>'.get_lang('ConfigSettingsInfo').' <strong>app/config/configuration.php</strong></p>';
1633
1634
    // Parameter 1: administrator's login
1635
1636
    $html = '';
1637
1638
    $html .= display_configuration_parameter($installType, get_lang('AdminLogin'), 'loginForm', $loginForm, $installType == 'update');
1639
1640
    // Parameter 2: administrator's password
1641
    if ($installType != 'update') {
1642
        $html .= display_configuration_parameter($installType, get_lang('AdminPass'), 'passForm', $passForm, false);
1643
    }
1644
1645
    // Parameters 3 and 4: administrator's names
1646
1647
    $html .=  display_configuration_parameter($installType, get_lang('AdminFirstName'), 'adminFirstName', $adminFirstName);
1648
    $html .=  display_configuration_parameter($installType, get_lang('AdminLastName'), 'adminLastName', $adminLastName);
1649
1650
    //Parameter 3: administrator's email
1651
    $html .=  display_configuration_parameter($installType, get_lang('AdminEmail'), 'emailForm', $emailForm);
1652
1653
    //Parameter 6: administrator's telephone
1654
    $html .=  display_configuration_parameter($installType, get_lang('AdminPhone'), 'adminPhoneForm', $adminPhoneForm);
1655
1656
1657
    echo panel($html, get_lang('Administrator'), 'administrator');
1658
1659
1660
    //echo '<table class="table">';
1661
1662
    //First parameter: language
1663
    $html = '<div class="form-group">';
1664
    $html .= '<label class="col-sm-6 control-label">'.get_lang('MainLang')."</label>";
1665
    if ($installType == 'update') {
1666
        $html .= '<input type="hidden" name="languageForm" value="'.api_htmlentities($languageForm, ENT_QUOTES).'" />'.$languageForm;
1667
1668
    } else { // new installation
1669
        $html .= '<div class="col-sm-6">';
1670
        $html .= display_language_selection_box('languageForm', $languageForm);
1671
        $html .= '</div>';
1672
    }
1673
    $html.= "</div>";
1674
1675
1676
    //Second parameter: Chamilo URL
1677
    $html .= '<div class="form-group">';
1678
    $html .= '<label class="col-sm-6 control-label">'.get_lang('ChamiloURL') .get_lang('ThisFieldIsRequired').'</label>';
1679
1680
1681
1682
    if ($installType == 'update') {
1683
        $html .= api_htmlentities($urlForm, ENT_QUOTES)."\n";
1684
    } else {
1685
        $html .= '<div class="col-sm-6">';
1686
        $html .= '<input class="form-control" type="text" size="40" maxlength="100" name="urlForm" value="'.api_htmlentities($urlForm, ENT_QUOTES).'" />';
1687
        $html .= '</div>';
1688
    }
1689
    $html .= '</div>';
1690
1691
    //Parameter 9: campus name
1692
    $html .= display_configuration_parameter($installType, get_lang('CampusName'), 'campusForm', $campusForm);
1693
1694
    //Parameter 10: institute (short) name
1695
    $html .= display_configuration_parameter($installType, get_lang('InstituteShortName'), 'institutionForm', $institutionForm);
1696
1697
    //Parameter 11: institute (short) name
1698
    $html .= display_configuration_parameter($installType, get_lang('InstituteURL'), 'institutionUrlForm', $institutionUrlForm);
1699
1700
1701
    $html .= '<div class="form-group">
1702
            <label class="col-sm-6 control-label">' . get_lang("EncryptMethodUserPass") . '</label>
1703
        <div class="col-sm-6">';
1704
    if ($installType == 'update') {
1705
        $html .= '<input type="hidden" name="encryptPassForm" value="'. $encryptPassForm .'" />'. $encryptPassForm;
1706
    } else {
1707
1708
        $html .= '<div class="checkbox">
1709
                    <label>
1710
                        <input  type="radio" name="encryptPassForm" value="bcrypt" id="encryptPass1" '. ($encryptPassForm == 'bcrypt' ? 'checked="checked" ':'') .'/> bcrypt
1711
                    </label>';
1712
1713
        $html .= '<label>
1714
                        <input  type="radio" name="encryptPassForm" value="sha1" id="encryptPass1" '. ($encryptPassForm == 'sha1' ? 'checked="checked" ':'') .'/> sha1
1715
                    </label>';
1716
1717
        $html .= '<label>
1718
                        <input type="radio" name="encryptPassForm" value="md5" id="encryptPass0" '. ($encryptPassForm == 'md5' ? 'checked="checked" ':'') .'/> md5
1719
                    </label>';
1720
1721
        $html .= '<label>
1722
                        <input type="radio" name="encryptPassForm" value="none" id="encryptPass2" '. ($encryptPassForm == 'none' ? 'checked="checked" ':'') .'/>'. get_lang('None').'
1723
                    </label>';
1724
        $html .= '</div>';
1725
    }
1726
    $html .= '</div></div>';
1727
1728
    $html .= '<div class="form-group">
1729
            <label class="col-sm-6 control-label">' . get_lang('AllowSelfReg') . '</label>
1730
            <div class="col-sm-6">';
1731
    if ($installType == 'update') {
1732
        if ($allowSelfReg == 'true') {
1733
            $label = get_lang('Yes');
1734
        } elseif ($allowSelfReg == 'false') {
1735
            $label = get_lang('No');
1736
        } else {
1737
            $label = get_lang('AfterApproval');
1738
        }
1739
        $html .= '<input type="hidden" name="allowSelfReg" value="'. $allowSelfReg .'" />'. $label;
1740
    } else {
1741
        $html .= '<div class="control-group">';
1742
        $html .= '<label class="checkbox-inline">
1743
                        <input type="radio" name="allowSelfReg" value="true" id="allowSelfReg1" '. ($allowSelfReg == 'true' ? 'checked="checked" ' : '') . ' /> '. get_lang('Yes') .'
1744
                    </label>';
1745
        $html .= '<label class="checkbox-inline">
1746
                        <input type="radio" name="allowSelfReg" value="false" id="allowSelfReg0" '. ($allowSelfReg == 'false' ? '' : 'checked="checked" ') .' /> '. get_lang('No') .'
1747
                    </label>';
1748
         $html .= '<label class="checkbox-inline">
1749
                    <input type="radio" name="allowSelfReg" value="approval" id="allowSelfReg2" '. ($allowSelfReg == 'approval' ? '' : 'checked="checked" ') .' /> '. get_lang('AfterApproval') .'
1750
                </label>';
1751
        $html .= '</div>';
1752
    }
1753
    $html .= '</div>';
1754
    $html .= '</div>';
1755
1756
    $html .= '<div class="form-group">';
1757
    $html .= '<label class="col-sm-6 control-label">'. get_lang('AllowSelfRegProf') .'</label>
1758
        <div class="col-sm-6">';
1759
    if ($installType == 'update') {
1760
        if ($allowSelfRegProf == 'true') {
1761
            $label = get_lang('Yes');
1762
        } else {
1763
            $label = get_lang('No');
1764
        }
1765
        $html .= '<input type="hidden" name="allowSelfRegProf" value="'. $allowSelfRegProf.'" />'. $label;
1766
    } else {
1767
        $html .= '<div class="control-group">
1768
                <label class="checkbox-inline">
1769
                    <input type="radio" name="allowSelfRegProf" value="1" id="allowSelfRegProf1" '. ($allowSelfRegProf ? 'checked="checked" ' : '') .'/>
1770
                ' . get_lang('Yes') .'
1771
                </label>';
1772
        $html .= '<label class="checkbox-inline">
1773
                    <input type="radio" name="allowSelfRegProf" value="0" id="allowSelfRegProf0" '. ($allowSelfRegProf ? '' : 'checked="checked" ') .' />
1774
                   '. get_lang('No') .'
1775
                </label>';
1776
        $html .= '</div>';
1777
    }
1778
    $html .= '</div>
1779
    </div>';
1780
1781
    echo panel($html, get_lang('Platform'), 'platform');
1782
 ?>
1783
    <div class='form-group'>
1784
        <div class="col-sm-6">
1785
            <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>
1786
            <input type="hidden" name="is_executable" id="is_executable" value="-" />
1787
        </div>
1788
        <div class="col-sm-6">
1789
            <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>
1790
        </div>
1791
    </div>
1792
1793
    <?php
1794
}
1795
1796
/**
1797
 * After installation is completed (step 6), this message is displayed.
1798
 * @param string $installType
1799
 */
1800
function display_after_install_message($installType)
1801
{
1802
    echo '<div class="RequirementContent">'.get_lang('FirstUseTip').'</div>';
1803
    echo '<div class="alert alert-warning">';
1804
    echo '<strong>'.get_lang('SecurityAdvice').'</strong>';
1805
    echo ': ';
1806
    printf(get_lang('ToProtectYourSiteMakeXReadOnlyAndDeleteY'), 'app/config/', 'main/install/');
1807
    echo '</div>';
1808
    ?></form>
1809
    <br />
1810
    <a class="btn btn-success btn-block" href="../../index.php">
1811
        <?php echo get_lang('GoToYourNewlyCreatedPortal'); ?>
1812
    </a>
1813
    <?php
1814
}
1815
1816
/**
1817
 * This function return countries list from array (hardcoded)
1818
 * @param   bool  $combo  (Optional) True for returning countries list with select html
1819
 * @return  array|string countries list
1820
 */
1821
function get_countries_list_from_array($combo = false)
1822
{
1823
    $a_countries = array(
1824
        "Afghanistan", "Albania", "Algeria", "Andorra", "Angola", "Antigua and Barbuda", "Argentina", "Armenia", "Australia", "Austria", "Azerbaijan",
1825
        "Bahamas", "Bahrain", "Bangladesh", "Barbados", "Belarus", "Belgium", "Belize", "Benin", "Bhutan", "Bolivia", "Bosnia and Herzegovina", "Botswana", "Brazil", "Brunei", "Bulgaria", "Burkina Faso", "Burundi",
1826
        "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",
1827
        "Denmark", "Djibouti", "Dominica", "Dominican Republic",
1828
        "East Timor (Timor Timur)", "Ecuador", "Egypt", "El Salvador", "Equatorial Guinea", "Eritrea", "Estonia", "Ethiopia",
1829
        "Fiji", "Finland", "France",
1830
        "Gabon", "Gambia, The", "Georgia", "Germany", "Ghana", "Greece", "Grenada", "Guatemala", "Guinea", "Guinea-Bissau", "Guyana",
1831
        "Haiti", "Honduras", "Hungary",
1832
        "Iceland", "India", "Indonesia", "Iran", "Iraq", "Ireland", "Israel", "Italy",
1833
        "Jamaica", "Japan", "Jordan",
1834
        "Kazakhstan", "Kenya", "Kiribati", "Korea, North", "Korea, South", "Kuwait", "Kyrgyzstan",
1835
        "Laos", "Latvia", "Lebanon", "Lesotho", "Liberia", "Libya", "Liechtenstein", "Lithuania", "Luxembourg",
1836
        "Macedonia", "Madagascar", "Malawi", "Malaysia", "Maldives", "Mali", "Malta", "Marshall Islands", "Mauritania", "Mauritius", "Mexico", "Micronesia", "Moldova", "Monaco", "Mongolia", "Morocco", "Mozambique", "Myanmar",
1837
        "Namibia", "Nauru", "Nepa", "Netherlands", "New Zealand", "Nicaragua", "Niger", "Nigeria", "Norway",
1838
        "Oman",
1839
        "Pakistan", "Palau", "Panama", "Papua New Guinea", "Paraguay", "Peru", "Philippines", "Poland","Portugal",
1840
        "Qatar",
1841
        "Romania", "Russia", "Rwanda",
1842
        "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",
1843
        "Taiwan", "Tajikistan", "Tanzania", "Thailand", "Togo", "Tonga", "Trinidad and Tobago", "Tunisia", "Turkey", "Turkmenistan", "Tuvalu",
1844
        "Uganda", "Ukraine", "United Arab Emirates", "United Kingdom", "United States", "Uruguay", "Uzbekistan",
1845
        "Vanuatu", "Vatican City", "Venezuela", "Vietnam",
1846
        "Yemen",
1847
        "Zambia", "Zimbabwe"
1848
    );
1849
1850
    $country_select = '';
1851
    if ($combo) {
1852
        $country_select = '<select class="selectpicker show-tick" id="country" name="country">';
1853
        $country_select .= '<option value="">--- '.get_lang('SelectOne').' ---</option>';
1854
        foreach ($a_countries as $country) {
1855
            $country_select .= '<option value="'.$country.'">'.$country.'</option>';
1856
        }
1857
        $country_select .= '</select>';
1858
        return $country_select;
1859
    }
1860
1861
    return $a_countries;
1862
}
1863
1864
/**
1865
 * Lock settings that can't be changed in other portals
1866
 */
1867 View Code Duplication
function lockSettings()
1868
{
1869
    $access_url_locked_settings = api_get_locked_settings();
1870
    $table = Database::get_main_table(TABLE_MAIN_SETTINGS_CURRENT);
1871
    foreach ($access_url_locked_settings as $setting) {
1872
        $sql = "UPDATE $table SET access_url_locked = 1 WHERE variable  = '$setting'";
1873
        Database::query($sql);
1874
    }
1875
}
1876
1877
/**
1878
 * Update dir values
1879
 */
1880
function updateDirAndFilesPermissions()
1881
{
1882
    $table = Database::get_main_table(TABLE_MAIN_SETTINGS_CURRENT);
1883
    $permissions_for_new_directories = isset($_SESSION['permissions_for_new_directories']) ? $_SESSION['permissions_for_new_directories'] : 0770;
1884
    $permissions_for_new_files = isset($_SESSION['permissions_for_new_files']) ? $_SESSION['permissions_for_new_files'] : 0660;
1885
    // use decoct() to store as string
1886
    $sql = "UPDATE $table SET selected_value = '0" . decoct($permissions_for_new_directories) . "'
1887
              WHERE variable  = 'permissions_for_new_directories'";
1888
    Database::query($sql);
1889
1890
    $sql = "UPDATE $table SET selected_value = '0" . decoct($permissions_for_new_files) . "' WHERE variable  = 'permissions_for_new_files'";
1891
    Database::query($sql);
1892
1893
    if (isset($_SESSION['permissions_for_new_directories'])) {
1894
        unset($_SESSION['permissions_for_new_directories']);
1895
    }
1896
1897
    if (isset($_SESSION['permissions_for_new_files'])) {
1898
        unset($_SESSION['permissions_for_new_files']);
1899
    }
1900
}
1901
1902
/**
1903
 * @param $current_value
1904
 * @param $wanted_value
1905
 * @return string
1906
 */
1907
function compare_setting_values($current_value, $wanted_value)
1908
{
1909
    $current_value_string = $current_value;
1910
    $current_value = (float)$current_value;
1911
    $wanted_value = (float)$wanted_value;
1912
1913
    if ($current_value >= $wanted_value) {
1914
        return Display::label($current_value_string, 'success');
1915
    } else {
1916
        return Display::label($current_value_string, 'important');
1917
    }
1918
}
1919
1920
/**
1921
 * @param $course_dir
1922
 * @param $course_attempt_name
1923
 * @param string $file
1924
 * @return bool
1925
 */
1926
function check_course_script_interpretation($course_dir, $course_attempt_name, $file = 'test.php')
1927
{
1928
    $output = false;
1929
    //Write in file
1930
    $file_name = $course_dir.'/'.$file;
1931
    $content = '<?php echo "123"; exit;';
1932
1933
    if (is_writable($file_name)) {
1934
        if ($handler = @fopen($file_name, "w")) {
1935
            //write content
1936
            if (fwrite($handler, $content)) {
1937
                $sock_errno = '';
1938
                $sock_errmsg = '';
1939
                $url = api_get_path(WEB_PATH).'app/courses/'.$course_attempt_name.'/'.$file;
1940
1941
                $parsed_url = parse_url($url);
1942
                //$scheme = isset($parsedUrl['scheme']) ? $parsedUrl['scheme'] : ''; //http
1943
                $host = isset($parsed_url['host']) ? $parsed_url['host'] : '';
1944
                // Patch if the host is the default host and is used through
1945
                // the IP address (sometimes the host is not taken correctly
1946
                // in this case)
1947
                if (empty($host) && !empty($_SERVER['HTTP_HOST'])) {
1948
                    $host = $_SERVER['HTTP_HOST'];
1949
                    $url = preg_replace('#:///#', '://'.$host.'/', $url);
1950
                }
1951
                $path = isset($parsed_url['path']) ? $parsed_url['path'] : '/';
1952
                $port = '';
1953
                $scheme = '';
1954
                switch ($parsed_url['scheme']) {
1955
                    case 'https':
1956
                        $scheme = 'ssl://';
1957
                        $port = 443;
1958
                        break;
1959
                    case 'http':
1960
                    default:
1961
                        $scheme = '';
1962
                        $port = 80;
1963
                }
1964
1965
                //Check fsockopen (not sure it works with https). If that is your case, you might want to try the
1966
                // suggestion at https://support.chamilo.org/issues/8260#note-3 (although it ignores SSL peer checks)
1967
                if ($fp = @fsockopen(str_replace('http://', $scheme, $url), $port, $sock_errno, $sock_errmsg, 60)) {
1968
                    $out  = "GET $path HTTP/1.1\r\n";
1969
                    $out .= "Host: $host\r\n";
1970
                    $out .= "Connection: Close\r\n\r\n";
1971
1972
                    fwrite($fp, $out);
1973
                    while (!feof($fp)) {
1974
                        $result = str_replace("\r\n", '',fgets($fp, 128));
1975
                        if (!empty($result) && $result == '123') {
1976
                            $output = true;
1977
                        }
1978
                    }
1979
                    fclose($fp);
1980
                    //Check allow_url_fopen
1981
                } elseif (ini_get('allow_url_fopen')) {
1982
                    if ($fp = @fopen($url, 'r')) {
1983
                        while ($result = fgets($fp, 1024)) {
1984
                            if (!empty($result) && $result == '123') {
1985
                                $output = true;
1986
                            }
1987
                        }
1988
                        fclose($fp);
1989
                    }
1990
                    // Check if has support for cURL
1991
                } elseif (function_exists('curl_init')) {
1992
                    $ch = curl_init();
1993
                    curl_setopt($ch, CURLOPT_HEADER, 0);
1994
                    curl_setopt($ch, CURLOPT_URL, $url);
1995
                    //curl_setopt($ch, CURLOPT_TIMEOUT, 30);
1996
                    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
1997
                    $result = curl_exec ($ch);
1998
                    if (!empty($result) && $result == '123') {
1999
                        $output = true;
2000
                    }
2001
                    curl_close($ch);
2002
                }
2003
            }
2004
            @fclose($handler);
2005
        }
2006
    }
2007
2008
    return $output;
2009
}
2010
2011
/**
2012
 * Save settings values
2013
 *
2014
 * @param string $organizationName
2015
 * @param string $organizationUrl
2016
 * @param string $siteName
2017
 * @param string $adminEmail
2018
 * @param string $adminLastName
2019
 * @param string $adminFirstName
2020
 * @param string $language
2021
 * @param string $allowRegistration
2022
 * @param string $allowTeacherSelfRegistration
2023
 * @param string $installationProfile The name of an installation profile file in main/install/profiles/
2024
 */
2025
function installSettings(
2026
    $organizationName,
2027
    $organizationUrl,
2028
    $siteName,
2029
    $adminEmail,
2030
    $adminLastName,
2031
    $adminFirstName,
2032
    $language,
2033
    $allowRegistration,
2034
    $allowTeacherSelfRegistration,
2035
    $installationProfile = ''
2036
) {
2037
    $allowTeacherSelfRegistration = $allowTeacherSelfRegistration ? 'true' : 'false';
2038
2039
    // Use PHP 5.3 to avoid issue with weird peripherical auto-installers like travis-ci
2040
    $settings = array(
2041
        'Institution' => $organizationName,
2042
        'InstitutionUrl' => $organizationUrl,
2043
        'siteName' => $siteName,
2044
        'emailAdministrator' => $adminEmail,
2045
        'administratorSurname' => $adminLastName,
2046
        'administratorName' => $adminFirstName,
2047
        'platformLanguage' => $language,
2048
        'allow_registration' => $allowRegistration,
2049
        'allow_registration_as_teacher' => $allowTeacherSelfRegistration,
2050
    );
2051
2052
    foreach ($settings as $variable => $value) {
2053
        $sql = "UPDATE settings_current
2054
                SET selected_value = '$value'
2055
                WHERE variable = '$variable'";
2056
        Database::query($sql);
2057
    }
2058
    installProfileSettings($installationProfile);
2059
}
2060
2061
/**
2062
 * Executes DB changes based in the classes defined in
2063
 * src/Chamilo/CoreBundle/Migrations/Schema/*
2064
 *
2065
 * @param string $chamiloVersion
2066
 * @param EntityManager $manager
2067
 * @throws \Doctrine\DBAL\DBALException
2068
 */
2069
function migrate($chamiloVersion, EntityManager $manager)
2070
{
2071
    $debug = true;
2072
    $connection = $manager->getConnection();
2073
2074
    $config = new \Doctrine\DBAL\Migrations\Configuration\Configuration($connection);
2075
2076
    // Table name that will store migrations log (will be created automatically,
2077
    // default name is: doctrine_migration_versions)
2078
    $config->setMigrationsTableName('version');
2079
    // Namespace of your migration classes, do not forget escape slashes, do not add last slash
2080
    $config->setMigrationsNamespace('Application\Migrations\Schema\V'.$chamiloVersion);
2081
    // Directory where your migrations are located
2082
    $config->setMigrationsDirectory(api_get_path(SYS_PATH).'app/Migrations/Schema/V'.$chamiloVersion);
2083
    // Load your migrations
2084
    $config->registerMigrationsFromDirectory($config->getMigrationsDirectory());
2085
2086
    $migration = new \Doctrine\DBAL\Migrations\Migration($config);
2087
    $versions = $config->getMigrations();
2088
2089
    /** @var Doctrine\DBAL\Migrations\Version $migrationItem */
2090
    foreach ($versions as $version) {
2091
        $version->getMigration()->setEntityManager($manager);
2092
    }
2093
2094
    $to = null; // if $to == null then schema will be migrated to latest version
2095
2096
    echo "<pre>";
2097
2098
    try {
2099
        // Execute migration!
2100
        $migratedSQL = $migration->migrate($to);
2101
2102
        if ($debug) {
2103
            foreach ($migratedSQL as $version => $sqlList) {
2104
                echo "VERSION: $version<br>";
2105
                echo "----------------------------------------------<br>";
2106
                $total = count($sqlList);
2107
                error_log("VERSION: $version");
2108
                error_log("# queries: ".$total);
2109
                $counter = 1;
2110
                foreach ($sqlList as $sql) {
2111
                    echo "<code>$sql</code><br>";
2112
                    error_log("$counter/$total : $sql");
2113
                    $counter++;
2114
                }
2115
            }
2116
2117
            echo "<br>DONE!<br>";
2118
        }
2119
2120
        return true;
2121
    } catch (Exception $ex) {
2122
        if ($debug) {
2123
            echo "ERROR: {$ex->getMessage()}<br>";
2124
            return false;
2125
        }
2126
    }
2127
2128
    echo "</pre>";
2129
2130
    return false;
2131
}
2132
2133
/**
2134
* @param EntityManager $em
2135
 *
2136
* @throws \Doctrine\DBAL\DBALException
2137
 */
2138
function fixIds(EntityManager $em)
2139
{
2140
    $connection = $em->getConnection();
2141
    $database = new Database();
2142
    $database->setManager($em);
2143
    $debug = true;
2144
    if ($debug) {
2145
        error_log('fixIds');
2146
    }
2147
2148
    // Create temporary indexes to increase speed of the following operations
2149
    // Adding and removing indexes will usually take much less time than
2150
    // the execution without indexes of the queries in this function, particularly
2151
    // for large tables
2152
    $sql = "ALTER TABLE c_document ADD INDEX tmpidx_doc(c_id, id)";
2153
    $connection->executeQuery($sql);
2154
    $sql = "ALTER TABLE c_student_publication ADD INDEX tmpidx_stud (c_id, id)";
2155
    $connection->executeQuery($sql);
2156
    $sql = "ALTER TABLE c_quiz ADD INDEX tmpidx_quiz (c_id, id)";
2157
    $connection->executeQuery($sql);
2158
    $sql = "ALTER TABLE c_item_property ADD INDEX tmpidx_ip (to_group_id)";
2159
    $connection->executeQuery($sql);
2160
2161
    $sql = "SELECT * FROM c_lp_item";
2162
    $result = $connection->fetchAll($sql);
2163
    foreach ($result as $item) {
2164
        $courseId = $item['c_id'];
2165
        $iid = isset($item['iid']) ? intval($item['iid']) : 0;
2166
        $ref = isset($item['ref']) ? intval($item['ref']) : 0;
2167
        $sql = null;
2168
2169
        $newId = '';
2170
2171
        switch ($item['item_type']) {
2172 View Code Duplication
            case TOOL_LINK:
2173
                $sql = "SELECT * FROM c_link WHERE c_id = $courseId AND id = $ref";
2174
                $data = $connection->fetchAssoc($sql);
2175
                if ($data) {
2176
                    $newId = $data['iid'];
2177
                }
2178
                break;
2179 View Code Duplication
            case TOOL_STUDENTPUBLICATION:
2180
                $sql = "SELECT * FROM c_student_publication WHERE c_id = $courseId AND id = $ref";
2181
                $data = $connection->fetchAssoc($sql);
2182
                if ($data) {
2183
                    $newId = $data['iid'];
2184
                }
2185
                break;
2186 View Code Duplication
            case TOOL_QUIZ:
2187
                $sql = "SELECT * FROM c_quiz WHERE c_id = $courseId AND id = $ref";
2188
                $data = $connection->fetchAssoc($sql);
2189
                if ($data) {
2190
                    $newId = $data['iid'];
2191
                }
2192
                break;
2193 View Code Duplication
            case TOOL_DOCUMENT:
2194
                $sql = "SELECT * FROM c_document WHERE c_id = $courseId AND id = $ref";
2195
                $data = $connection->fetchAssoc($sql);
2196
                if ($data) {
2197
                    $newId = $data['iid'];
2198
                }
2199
                break;
2200 View Code Duplication
            case TOOL_FORUM:
2201
                $sql = "SELECT * FROM c_forum_forum WHERE c_id = $courseId AND forum_id = $ref";
2202
                $data = $connection->fetchAssoc($sql);
2203
                if ($data) {
2204
                    $newId = $data['iid'];
2205
                }
2206
                break;
2207 View Code Duplication
            case 'thread':
2208
                $sql = "SELECT * FROM c_forum_thread WHERE c_id = $courseId AND thread_id = $ref";
2209
                $data = $connection->fetchAssoc($sql);
2210
                if ($data) {
2211
                    $newId = $data['iid'];
2212
                }
2213
                break;
2214
        }
2215
2216
        if (!empty($sql) && !empty($newId) && !empty($iid)) {
2217
            $sql = "UPDATE c_lp_item SET ref = $newId WHERE iid = $iid";
2218
2219
            $connection->executeQuery($sql);
2220
        }
2221
    }
2222
2223
    // Set NULL if session = 0
2224
    $sql = "UPDATE c_item_property SET session_id = NULL WHERE session_id = 0";
2225
    $connection->executeQuery($sql);
2226
2227
    // Set NULL if group = 0
2228
    $sql = "UPDATE c_item_property SET to_group_id = NULL WHERE to_group_id = 0";
2229
    $connection->executeQuery($sql);
2230
2231
    // Set NULL if insert_user_id = 0
2232
    $sql = "UPDATE c_item_property SET insert_user_id = NULL WHERE insert_user_id = 0";
2233
    $connection->executeQuery($sql);
2234
2235
    // Delete session data of sessions that don't exist.
2236
    $sql = "DELETE FROM c_item_property
2237
            WHERE session_id IS NOT NULL AND session_id NOT IN (SELECT id FROM session)";
2238
    $connection->executeQuery($sql);
2239
2240
    // Delete group data of groups that don't exist.
2241
    $sql = "DELETE FROM c_item_property
2242
            WHERE to_group_id IS NOT NULL AND to_group_id NOT IN (SELECT DISTINCT id FROM c_group_info)";
2243
    $connection->executeQuery($sql);
2244
2245
    // This updates the group_id with c_group_info.iid instead of c_group_info.id
2246
2247
    if ($debug) {
2248
        error_log('update iids');
2249
    }
2250
2251
    $groupTableToFix = [
2252
        'c_group_rel_user',
2253
        'c_group_rel_tutor',
2254
        'c_permission_group',
2255
        'c_role_group',
2256
        'c_survey_invitation',
2257
        'c_attendance_calendar_rel_group'
2258
    ];
2259
2260
    foreach ($groupTableToFix as $table) {
2261
        $sql = "SELECT * FROM $table";
2262
        $result = $connection->fetchAll($sql);
2263
        foreach ($result as $item) {
2264
            $iid = $item['iid'];
2265
            $courseId = $item['c_id'];
2266
            $groupId = intval($item['group_id']);
2267
2268
            // Fix group id
2269 View Code Duplication
            if (!empty($groupId)) {
2270
                $sql = "SELECT * FROM c_group_info
2271
                        WHERE c_id = $courseId AND id = $groupId
2272
                        LIMIT 1";
2273
                $data = $connection->fetchAssoc($sql);
2274
                if (!empty($data)) {
2275
                    $newGroupId = $data['iid'];
2276
                    $sql = "UPDATE $table SET group_id = $newGroupId
2277
                            WHERE iid = $iid";
2278
                    $connection->executeQuery($sql);
2279
                } else {
2280
                    // The group does not exists clean this record
2281
                    $sql = "DELETE FROM $table WHERE iid = $iid";
2282
                    $connection->executeQuery($sql);
2283
                }
2284
            }
2285
        }
2286
    }
2287
2288
    // Fix c_item_property
2289
    if ($debug) {
2290
        error_log('update c_item_property');
2291
    }
2292
2293
    $sql = "SELECT * FROM course";
2294
    $courseList = $connection->fetchAll($sql);
2295
    if ($debug) {
2296
        error_log('Getting course list');
2297
    }
2298
2299
    $totalCourse = count($courseList);
2300
    $counter = 0;
2301
2302
    foreach ($courseList as $courseData) {
2303
        $courseId = $courseData['id'];
2304
        if ($debug) {
2305
            error_log('Updating course: '.$courseData['code']);
2306
        }
2307
2308
        $sql = "SELECT * FROM c_item_property WHERE c_id = $courseId";
2309
        $result = $connection->fetchAll($sql);
2310
        foreach ($result as $item) {
2311
            $sessionId = intval($item['session_id']);
2312
            $groupId = intval($item['to_group_id']);
2313
            $iid = $item['iid'];
2314
            $ref = $item['ref'];
2315
2316
            // Fix group id
2317 View Code Duplication
            if (!empty($groupId)) {
2318
                $sql = "SELECT * FROM c_group_info
2319
                        WHERE c_id = $courseId AND id = $groupId";
2320
                $data = $connection->fetchAssoc($sql);
2321
                if (!empty($data)) {
2322
                    $newGroupId = $data['iid'];
2323
                    $sql = "UPDATE c_item_property SET to_group_id = $newGroupId
2324
                            WHERE iid = $iid";
2325
                    $connection->executeQuery($sql);
2326
                } else {
2327
                    // The group does not exists clean this record
2328
                    $sql = "DELETE FROM c_item_property WHERE iid = $iid";
2329
                    $connection->executeQuery($sql);
2330
                }
2331
            }
2332
2333
            $sql = '';
2334
            $newId = '';
2335
            switch ($item['tool']) {
2336
                case TOOL_LINK:
2337
                    $sql = "SELECT * FROM c_link WHERE c_id = $courseId AND id = $ref ";
2338
                    break;
2339
                case TOOL_STUDENTPUBLICATION:
2340
                    $sql = "SELECT * FROM c_student_publication WHERE c_id = $courseId AND id = $ref";
2341
                    break;
2342
                case TOOL_QUIZ:
2343
                    $sql = "SELECT * FROM c_quiz WHERE c_id = $courseId AND id = $ref";
2344
                    break;
2345
                case TOOL_DOCUMENT:
2346
                    $sql = "SELECT * FROM c_document WHERE c_id = $courseId AND id = $ref";
2347
                    break;
2348
                case TOOL_FORUM:
2349
                    $sql = "SELECT * FROM c_forum_forum WHERE c_id = $courseId AND id = $ref";
2350
                    break;
2351
                case 'thread':
2352
                    $sql = "SELECT * FROM c_forum_thread WHERE c_id = $courseId AND id = $ref";
2353
                    break;
2354
            }
2355
2356 View Code Duplication
            if (!empty($sql) && !empty($newId)) {
2357
                $data = $connection->fetchAssoc($sql);
2358
                if (isset($data['iid'])) {
2359
                    $newId = $data['iid'];
2360
                }
2361
                $sql = "UPDATE c_item_property SET ref = $newId WHERE iid = $iid";
2362
                error_log($sql);
2363
                $connection->executeQuery($sql);
2364
            }
2365
        }
2366
2367
        if ($debug) {
2368
            // Print a status in the log once in a while
2369
            error_log("Course process #$counter/$totalCourse");
2370
        }
2371
        $counter++;
2372
    }
2373
2374
    if ($debug) {
2375
        error_log('update gradebook_link');
2376
    }
2377
2378
    // Fix gradebook_link
2379
    $sql = "SELECT * FROM gradebook_link";
2380
    $result = $connection->fetchAll($sql);
2381
    foreach ($result as $item) {
2382
        $courseCode = $item['course_code'];
2383
2384
        $sql = "SELECT * FROM course WHERE code = '$courseCode'";
2385
        $courseInfo = $connection->fetchAssoc($sql);
2386
        if (empty($courseInfo)) {
2387
            continue;
2388
        }
2389
2390
        $courseId = $courseInfo['id'];
2391
2392
        $ref = $item['ref_id'];
2393
        $iid = $item['id'];
2394
2395
        $sql = '';
2396
        switch ($item['type']) {
2397
            case LINK_LEARNPATH:
2398
                $sql = "SELECT * FROM c_link WHERE c_id = $courseId AND id = $ref ";
2399
                break;
2400
            case LINK_STUDENTPUBLICATION:
2401
                $sql = "SELECT * FROM c_student_publication WHERE c_id = $courseId AND id = $ref";
2402
                break;
2403
            case LINK_EXERCISE:
2404
                $sql = "SELECT * FROM c_quiz WHERE c_id = $courseId AND id = $ref";
2405
                break;
2406
            case LINK_ATTENDANCE:
2407
                //$sql = "SELECT * FROM c_document WHERE c_id = $courseId AND id = $ref";
2408
                break;
2409
            case LINK_FORUM_THREAD:
2410
                $sql = "SELECT * FROM c_forum_thread WHERE c_id = $courseId AND thread_id = $ref";
2411
                break;
2412
        }
2413
2414 View Code Duplication
        if (!empty($sql)) {
2415
            $data = $connection->fetchAssoc($sql);
2416
            if (isset($data) && isset($data['iid'])) {
2417
                $newId = $data['iid'];
2418
                $sql = "UPDATE gradebook_link SET ref_id = $newId
2419
                        WHERE id = $iid";
2420
                $connection->executeQuery($sql);
2421
            }
2422
        }
2423
    }
2424
2425
    if ($debug) {
2426
        error_log('update groups');
2427
    }
2428
2429
    $sql = "SELECT * FROM groups";
2430
    $result = $connection->executeQuery($sql);
2431
    $groups = $result->fetchAll();
2432
2433
    $oldGroups = array();
2434
2435
    if (!empty($groups)) {
2436
        foreach ($groups as $group) {
2437
            if (empty($group['name'])) {
2438
                continue;
2439
            }
2440
2441
            $params = [
2442
                'name' => $group['name'],
2443
                'description' => $group['description'],
2444
                'group_type' => 1,
2445
                'picture' => $group['picture_uri'],
2446
                'url' => $group['url'],
2447
                'visibility' => $group['visibility'],
2448
                'updated_at' => $group['updated_on'],
2449
                'created_at' => $group['created_on']
2450
            ];
2451
            $connection->insert('usergroup', $params);
2452
            $id = $connection->lastInsertId('id');
2453
            $oldGroups[$group['id']] = $id;
2454
        }
2455
    }
2456
2457
    if (!empty($oldGroups)) {
2458
        error_log('Moving group files');
2459
        foreach ($oldGroups as $oldId => $newId) {
2460
            $path = get_group_picture_path_by_id(
2461
                $oldId,
2462
                'system'
2463
            );
2464
2465
            if (!empty($path)) {
2466
                $newPath = str_replace(
2467
                    "groups/$oldId/",
2468
                    "groups/$newId/",
2469
                    $path['dir']
2470
                );
2471
                $command = "mv {$path['dir']} $newPath ";
2472
                error_log("Executing $command");
2473
                system($command);
2474
            }
2475
        }
2476
2477
        $sql = "SELECT * FROM group_rel_user";
2478
        $result = $connection->executeQuery($sql);
2479
        $dataList = $result->fetchAll();
2480
2481
        if (!empty($dataList)) {
2482
            foreach ($dataList as $data) {
2483
                if (isset($oldGroups[$data['group_id']])) {
2484
                    $data['group_id'] = $oldGroups[$data['group_id']];
2485
                    $userId = $data['user_id'];
2486
2487
                    $sql = "SELECT id FROM user WHERE user_id = $userId";
2488
                    $userResult = $connection->executeQuery($sql);
2489
                    $userInfo = $userResult->fetch();
2490
                    if (empty($userInfo)) {
2491
                        continue;
2492
                    }
2493
2494
                    $sql = "INSERT INTO usergroup_rel_user (usergroup_id, user_id, relation_type)
2495
                            VALUES ('{$data['group_id']}', '{$userId}', '{$data['relation_type']}')";
2496
                    $connection->executeQuery($sql);
2497
                }
2498
            }
2499
        }
2500
2501
        $sql = "SELECT * FROM group_rel_group";
2502
        $result = $connection->executeQuery($sql);
2503
        $dataList = $result->fetchAll();
2504
2505
        if (!empty($dataList)) {
2506
            foreach ($dataList as $data) {
2507
                if (isset($oldGroups[$data['group_id']]) && isset($oldGroups[$data['subgroup_id']])) {
2508
                    $data['group_id'] = $oldGroups[$data['group_id']];
2509
                    $data['subgroup_id'] = $oldGroups[$data['subgroup_id']];
2510
                    $sql = "INSERT INTO usergroup_rel_usergroup (group_id, subgroup_id, relation_type)
2511
                            VALUES ('{$data['group_id']}', '{$data['subgroup_id']}', '{$data['relation_type']}')";
2512
                    $connection->executeQuery($sql);
2513
                }
2514
            }
2515
        }
2516
2517
        $sql = "SELECT * FROM announcement_rel_group";
2518
        $result = $connection->executeQuery($sql);
2519
        $dataList = $result->fetchAll();
2520
2521
        if (!empty($dataList)) {
2522
            foreach ($dataList as $data) {
2523
                if (isset($oldGroups[$data['group_id']])) {
2524
                    // Deleting relation
2525
                    $sql = "DELETE FROM announcement_rel_group WHERE group_id = {$data['group_id']}";
2526
                    $connection->executeQuery($sql);
2527
2528
                    // Add new relation
2529
                    $data['group_id'] = $oldGroups[$data['group_id']];
2530
                    $sql = "INSERT INTO announcement_rel_group(group_id, announcement_id)
2531
                            VALUES ('{$data['group_id']}', '{$data['announcement_id']}')";
2532
                    $connection->executeQuery($sql);
2533
                }
2534
            }
2535
        }
2536
2537
        $sql = "SELECT * FROM group_rel_tag";
2538
        $result = $connection->executeQuery($sql);
2539
        $dataList = $result->fetchAll();
2540
        if (!empty($dataList)) {
2541
            foreach ($dataList as $data) {
2542
                if (isset($oldGroups[$data['group_id']])) {
2543
                    $data['group_id'] = $oldGroups[$data['group_id']];
2544
                    $sql = "INSERT INTO usergroup_rel_tag (tag_id, usergroup_id)
2545
                            VALUES ('{$data['tag_id']}', '{$data['group_id']}')";
2546
                    $connection->executeQuery($sql);
2547
                }
2548
            }
2549
        }
2550
    }
2551
2552
    if ($debug) {
2553
        error_log('update extra fields');
2554
    }
2555
2556
    // Extra fields
2557
    $extraFieldTables = [
2558
        ExtraField::USER_FIELD_TYPE => Database::get_main_table(TABLE_MAIN_USER_FIELD),
2559
        ExtraField::COURSE_FIELD_TYPE => Database::get_main_table(TABLE_MAIN_COURSE_FIELD),
2560
        //ExtraField::LP_FIELD_TYPE => Database::get_main_table(TABLE_MAIN_LP_FIELD),
2561
        ExtraField::SESSION_FIELD_TYPE => Database::get_main_table(TABLE_MAIN_SESSION_FIELD),
2562
        //ExtraField::CALENDAR_FIELD_TYPE => Database::get_main_table(TABLE_MAIN_CALENDAR_EVENT_FIELD),
2563
        //ExtraField::QUESTION_FIELD_TYPE => Database::get_main_table(TABLE_MAIN_CALENDAR_EVENT_FIELD),
2564
        //ExtraField::USER_FIELD_TYPE => //Database::get_main_table(TABLE_MAIN_SPECIFIC_FIELD),
2565
    ];
2566
2567
    foreach ($extraFieldTables as $type => $table) {
2568
        $sql = "SELECT * FROM $table ";
2569
        if ($debug) {
2570
            error_log($sql);
2571
        }
2572
        $result = $connection->query($sql);
2573
        $fields = $result->fetchAll();
2574
2575
        foreach ($fields as $field) {
2576
            if ($debug) {
2577
                error_log("Loading field: ".$field['field_variable']);
2578
            }
2579
            $originalId = $field['id'];
2580
2581
            $params = [
2582
                'extra_field_type' => $type,
2583
                'variable' => $field['field_variable'],
2584
                'field_type' => $field['field_type'],
2585
                'display_text' => $field['field_display_text'],
2586
                'default_value' => $field['field_default_value'],
2587
                'field_order' => $field['field_order'],
2588
                'visible' => $field['field_visible'],
2589
                'changeable' => $field['field_changeable'],
2590
                'filter' => $field['field_filter']
2591
            ];
2592
2593
            $connection->insert('extra_field', $params);
2594
            $newExtraFieldId = $connection->lastInsertId();
2595
2596
            $values = array();
2597
            $handlerId = null;
2598
            switch ($type) {
2599
                case ExtraField::USER_FIELD_TYPE:
2600
                    $optionTable = Database::get_main_table(
2601
                        TABLE_MAIN_USER_FIELD_OPTIONS
2602
                    );
2603
                    $valueTable = Database::get_main_table(
2604
                        TABLE_MAIN_USER_FIELD_VALUES
2605
                    );
2606
                    $handlerId = 'user_id';
2607
                    break;
2608
                case ExtraField::COURSE_FIELD_TYPE:
2609
                    $optionTable = Database::get_main_table(
2610
                        TABLE_MAIN_COURSE_FIELD_OPTIONS
2611
                    );
2612
                    $valueTable = Database::get_main_table(
2613
                        TABLE_MAIN_COURSE_FIELD_VALUES
2614
                    );
2615
                    $handlerId = 'c_id';
2616
                    break;
2617
                case ExtraField::SESSION_FIELD_TYPE:
2618
                    $optionTable = Database::get_main_table(
2619
                        TABLE_MAIN_SESSION_FIELD_OPTIONS
2620
                    );
2621
                    $valueTable = Database::get_main_table(
2622
                        TABLE_MAIN_SESSION_FIELD_VALUES
2623
                    );
2624
                    $handlerId = 'session_id';
2625
                    break;
2626
            }
2627
2628
            if (!empty($optionTable)) {
2629
                $sql = "SELECT * FROM $optionTable WHERE field_id = $originalId ";
2630
                $result = $connection->query($sql);
2631
                $options = $result->fetchAll();
2632
2633
                foreach ($options as $option) {
2634
                    $params = [
2635
                        'display_text' => $option['option_display_text'],
2636
                        'field_id' => $newExtraFieldId,
2637
                        'option_order' => $option['option_order'],
2638
                        'option_value' => $option['option_value']
2639
                    ];
2640
                    $connection->insert('extra_field_options', $params);
2641
                }
2642
2643
                $sql = "SELECT * FROM $valueTable WHERE field_id = $originalId ";
2644
                $result = $connection->query($sql);
2645
                $values = $result->fetchAll();
2646
                if ($debug) {
2647
                    error_log("Fetch all values for field");
2648
                }
2649
            }
2650
2651
            if (!empty($values)) {
2652
                if ($debug) {
2653
                    error_log("Saving field value in new table");
2654
                }
2655
                $k = 0;
2656
                foreach ($values as $value) {
2657
                    if (isset($value[$handlerId])) {
2658
                        // Insert without the use of the entity as it reduces
2659
                        // speed to 2 records per second (much too slow)
2660
                        $params = [
2661
                            'field_id' => $newExtraFieldId,
2662
                            'value' => $value['field_value'],
2663
                            'item_id' => $value[$handlerId]
2664
                        ];
2665
                        $connection->insert('extra_field_values', $params);
2666
                        if ($debug && ($k % 10000 == 0)) {
2667
                            error_log("Saving field $k");
2668
                        }
2669
                        $k++;
2670
                    }
2671
                }
2672
            }
2673
        }
2674
    }
2675
2676
    if ($debug) {
2677
        error_log('Remove index');
2678
    }
2679
2680
    // Drop temporary indexes added to increase speed of this function's queries
2681
    $sql = "ALTER TABLE c_document DROP INDEX tmpidx_doc";
2682
    $connection->executeQuery($sql);
2683
    $sql = "ALTER TABLE c_student_publication DROP INDEX tmpidx_stud";
2684
    $connection->executeQuery($sql);
2685
    $sql = "ALTER TABLE c_quiz DROP INDEX tmpidx_quiz";
2686
    $connection->executeQuery($sql);
2687
    $sql = "ALTER TABLE c_item_property DROP INDEX tmpidx_ip";
2688
    $connection->executeQuery($sql);
2689
2690
    if ($debug) {
2691
        error_log('Finish fixId function');
2692
    }
2693
}
2694
2695
/**
2696
 *
2697
 * After the schema was created (table creation), the function adds
2698
 * admin/platform information.
2699
 *
2700
 * @param EntityManager $manager
2701
 * @param string $sysPath
2702
 * @param string $encryptPassForm
2703
 * @param string $passForm
2704
 * @param string $adminLastName
2705
 * @param string $adminFirstName
2706
 * @param string $loginForm
2707
 * @param string $emailForm
2708
 * @param string $adminPhoneForm
2709
 * @param string $languageForm
2710
 * @param string $institutionForm
2711
 * @param string $institutionUrlForm
2712
 * @param string $siteName
2713
 * @param string $allowSelfReg
2714
 * @param string $allowSelfRegProf
2715
 * @param string $installationProfile Installation profile, if any was provided
2716
 */
2717
function finishInstallation(
2718
    $manager,
2719
    $sysPath,
2720
    $encryptPassForm,
2721
    $passForm,
2722
    $adminLastName,
2723
    $adminFirstName,
2724
    $loginForm,
2725
    $emailForm,
2726
    $adminPhoneForm,
2727
    $languageForm,
2728
    $institutionForm,
2729
    $institutionUrlForm,
2730
    $siteName,
2731
    $allowSelfReg,
2732
    $allowSelfRegProf,
2733
    $installationProfile = ''
2734
) {
2735
    $sysPath = !empty($sysPath) ? $sysPath : api_get_path(SYS_PATH);
2736
2737
    // Inserting data
2738
    $data = file_get_contents($sysPath.'main/install/data.sql');
2739
    $result = $manager->getConnection()->prepare($data);
2740
    $result->execute();
2741
    $result->closeCursor();
2742
2743
    UserManager::setPasswordEncryption($encryptPassForm);
2744
2745
    // Create admin user.
2746
    @UserManager::create_user(
2747
        $adminFirstName,
2748
        $adminLastName,
2749
        1,
2750
        $emailForm,
2751
        $loginForm,
2752
        $passForm,
2753
        'ADMIN', //$official_code = '',
2754
        $languageForm,
2755
        $adminPhoneForm,
2756
        '', //$picture_uri = '',
2757
        PLATFORM_AUTH_SOURCE,
2758
        '',//$expirationDate,
2759
        1,
2760
        0,
2761
        null,
2762
        '',
2763
        false,  //$send_mail = false,
2764
        true //$isAdmin = false
2765
    );
2766
2767
    // Create anonymous user.
2768
    @UserManager::create_user(
2769
        'Joe',
2770
        'Anonymous',
2771
        6,
2772
        'anonymous@localhost',
2773
        'anon',
2774
        'anon',
2775
        'anonymous', //$official_code = '',
2776
        $languageForm,
2777
        '',
2778
        '', //$picture_uri = '',
2779
        PLATFORM_AUTH_SOURCE,
2780
        '',
2781
        1,
2782
        0,
2783
        null,
2784
        '',
2785
        false,  //$send_mail = false,
2786
        false //$isAdmin = false
2787
    );
2788
2789
    // Set default language
2790
    $sql = "UPDATE language SET available = 1 WHERE dokeos_folder = '$languageForm'";
2791
    Database::query($sql);
2792
2793
    // Install settings
2794
    installSettings(
2795
        $institutionForm,
2796
        $institutionUrlForm,
2797
        $siteName,
2798
        $emailForm,
2799
        $adminLastName,
2800
        $adminFirstName,
2801
        $languageForm,
2802
        $allowSelfReg,
2803
        $allowSelfRegProf,
2804
        $installationProfile
2805
    );
2806
2807
    lockSettings();
2808
    updateDirAndFilesPermissions();
2809
2810
    // Set the latest version
2811
    $path = $sysPath.'app/Migrations/Schema/V111/';
2812
    $finder = new \Symfony\Component\Finder\Finder();
2813
    $files = $finder->files()->in($path);
2814
2815
    // Needed for chash
2816
    $sql = 'CREATE TABLE IF NOT EXISTS version (id int unsigned NOT NULL AUTO_INCREMENT, version varchar(255), PRIMARY KEY(id), UNIQUE(version));';
2817
    Database::query($sql);
2818
2819
    foreach ($files as $version) {
2820
        $version = str_replace(['Version',  '.php' ], '', $version->getFilename());
2821
        $sql = "INSERT INTO version (version) VALUES ('$version')";
2822
        Database::query($sql);
2823
    }
2824
}
2825
2826
/**
2827
 * Update settings based on installation profile defined in a JSON file
2828
 * @param string $installationProfile The name of the JSON file in main/install/profiles/ folder
2829
 *
2830
 * @return bool false on failure (no bad consequences anyway, just ignoring profile)
2831
 */
2832
function installProfileSettings($installationProfile = '')
2833
{
2834
    if (empty($installationProfile)) {
2835
        return false;
2836
    }
2837
    $jsonPath = api_get_path(SYS_PATH).'main/install/profiles/'.$installationProfile.'.json';
2838
    // Make sure the path to the profile is not hacked
2839
    if (!Security::check_abs_path($jsonPath, api_get_path(SYS_PATH).'main/install/profiles/')) {
2840
        return false;
2841
    }
2842
    if (!is_file($jsonPath)) {
2843
        return false;
2844
    }
2845
    if (!is_readable($jsonPath)) {
2846
        return false;
2847
    }
2848
    if (!function_exists('json_decode')) {
2849
        // The php-json extension is not available. Ignore profile.
2850
        return false;
2851
    }
2852
    $json = file_get_contents($jsonPath);
2853
    $params = json_decode($json);
2854
    if ($params === false or $params === null) {
2855
        return false;
2856
    }
2857
    $settings = $params->params;
2858
    if (!empty($params->parent)) {
2859
        installProfileSettings($params->parent);
2860
    }
2861
    foreach ($settings as $id => $param) {
2862
        $sql = "UPDATE settings_current
2863
                SET selected_value = '".$param->selected_value."'
2864
                WHERE variable = '".$param->variable."'";
2865
        if (!empty($param->subkey)) {
2866
            $sql .= " AND subkey='" . $param->subkey . "'";
2867
        }
2868
        Database::query($sql);
2869
    }
2870
2871
    return true;
2872
}
2873
2874
/**
2875
 * Quick function to remove a directory with its subdirectories
2876
 * @param $dir
2877
 */
2878
function rrmdir($dir)
2879
{
2880
    if (is_dir($dir)) {
2881
        $objects = scandir($dir);
2882
        foreach ($objects as $object) {
2883
            if ($object != "." && $object != "..") {
2884
                if (filetype($dir."/".$object) == "dir") {
2885
                    @rrmdir($dir."/".$object);
2886
                } else {
2887
                    @unlink($dir."/".$object);
2888
                }
2889
            }
2890
        }
2891
        reset($objects);
2892
        rmdir($dir);
2893
    }
2894
}
2895
2896
function get_group_picture_path_by_id($id, $type = 'web', $preview = false, $anonymous = false)
2897
{
2898
    switch ($type) {
2899
        case 'system': // Base: absolute system path.
2900
            $base = api_get_path(SYS_UPLOAD_PATH);
2901
            break;
2902
        case 'web': // Base: absolute web path.
2903
        default:
2904
            $base = api_get_path(WEB_UPLOAD_PATH);
2905
            break;
2906
    }
2907
2908
    $noPicturePath = array('dir' => $base.'img/', 'file' => 'unknown.jpg');
2909
2910
    if (empty($id) || empty($type)) {
2911
        return $anonymous ? $noPicturePath : array('dir' => '', 'file' => '');
2912
    }
2913
2914
    $id = intval($id);
2915
2916
    //$group_table = Database :: get_main_table(TABLE_MAIN_GROUP);
2917
    $group_table = 'groups';
2918
    $sql = "SELECT picture_uri FROM $group_table WHERE id=".$id;
2919
    $res = Database::query($sql);
2920
2921
    if (!Database::num_rows($res)) {
2922
        return $anonymous ? $noPicturePath : array('dir' => '', 'file' => '');
2923
    }
2924
2925
    $user = Database::fetch_array($res);
2926
    $picture_filename = trim($user['picture_uri']);
2927
2928 View Code Duplication
    if (api_get_setting('split_users_upload_directory') === 'true') {
2929
        if (!empty($picture_filename)) {
2930
            $dir = $base.'groups/'.substr($picture_filename, 0, 1).'/'.$id.'/';
2931
        } elseif ($preview) {
2932
            $dir = $base.'groups/'.substr((string) $id, 0, 1).'/'.$id.'/';
2933
        } else {
2934
            $dir = $base.'groups/'.$id.'/';
2935
        }
2936
    } else {
2937
        $dir = $base.'groups/'.$id.'/';
2938
    }
2939
2940
    if (empty($picture_filename) && $anonymous) {
2941
        return $noPicturePath;
2942
    }
2943
2944
    return array('dir' => $dir, 'file' => $picture_filename);
2945
}
2946
2947
/**
2948
 * @param string $fromVersion
2949
 * @param EntityManager $manager
2950
 * @param bool $processFiles
2951
 */
2952
function migrateSwitch($fromVersion, $manager, $processFiles = true)
2953
{
2954
    error_log('Starting migration process from '.$fromVersion.' ('.date('Y-m-d H:i:s').')');
2955
2956
    echo '<a class="btn btn-default" href="javascript:void(0)" id="details_button">'.get_lang('Details').'</a><br />';
2957
    echo '<div id="details" style="display:none">';
2958
2959
    $connection = $manager->getConnection();
2960
2961
    $database = new Database();
2962
    $database->setManager($manager);
2963
2964
    switch ($fromVersion) {
2965
        case '1.9.0':
2966
        case '1.9.2':
2967
        case '1.9.4':
2968
        case '1.9.6':
2969
        case '1.9.6.1':
2970
        case '1.9.8':
2971
        case '1.9.8.1':
2972
        case '1.9.8.2':
2973
        case '1.9.10':
2974
        case '1.9.10.2':
2975
        case '1.9.10.4':
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
Coding Style introduced by
There must be a comment when fall-through is intentional in a non-empty case body
Loading history...
2976
2977
            $database = new Database();
2978
            $database->setManager($manager);
2979
2980
            // Fix type "enum" before running the migration with Doctrine
2981
            $connection->executeQuery("ALTER TABLE course_category MODIFY COLUMN auth_course_child VARCHAR(40) DEFAULT 'TRUE'");
2982
            $connection->executeQuery("ALTER TABLE course_category MODIFY COLUMN auth_cat_child VARCHAR(40) DEFAULT 'TRUE'");
2983
            $connection->executeQuery("ALTER TABLE c_quiz_answer MODIFY COLUMN hotspot_type varchar(40) default NULL");
2984
            $connection->executeQuery("ALTER TABLE c_tool MODIFY COLUMN target varchar(20) NOT NULL default '_self'");
2985
            $connection->executeQuery("ALTER TABLE c_link MODIFY COLUMN on_homepage char(10) NOT NULL default '0'");
2986
            $connection->executeQuery("ALTER TABLE c_blog_rating MODIFY COLUMN rating_type char(40) NOT NULL default 'post'");
2987
            $connection->executeQuery("ALTER TABLE c_survey MODIFY COLUMN anonymous char(10) NOT NULL default '0'");
2988
            $connection->executeQuery("ALTER TABLE c_document MODIFY COLUMN filetype char(10) NOT NULL default 'file'");
2989
            $connection->executeQuery("ALTER TABLE c_student_publication MODIFY COLUMN filetype char(10) NOT NULL default 'file'");
2990
2991
            // Migrate using the migration files located in:
2992
            // src/Chamilo/CoreBundle/Migrations/Schema/V110
2993
            $result = migrate(
2994
                110,
2995
                $manager
2996
            );
2997
2998
            if ($result) {
2999
                error_log('Migrations files were executed ('.date('Y-m-d H:i:s').')');
3000
                fixIds($manager);
3001
                error_log('fixIds finished ('.date('Y-m-d H:i:s').')');
3002
3003
                $connection->executeQuery("UPDATE settings_current SET selected_value = '1.10.0' WHERE variable = 'chamilo_database_version'");
3004
3005
                if ($processFiles) {
3006
3007
                    include __DIR__.'/update-files-1.9.0-1.10.0.inc.php';
3008
                    // Only updates the configuration.inc.php with the new version
3009
                    include __DIR__.'/update-configuration.inc.php';
3010
3011
                    $configurationFiles = array(
3012
                        'mail.conf.php',
3013
                        'profile.conf.php',
3014
                        'course_info.conf.php',
3015
                        'add_course.conf.php',
3016
                        'events.conf.php',
3017
                        'auth.conf.php',
3018
                        'portfolio.conf.php'
3019
                    );
3020
3021
                    error_log('Copy conf files');
3022
3023
                    foreach ($configurationFiles as $file) {
3024
                        if (file_exists(api_get_path(SYS_CODE_PATH).'inc/conf/'.$file)) {
3025
                            copy(
3026
                                api_get_path(SYS_CODE_PATH).'inc/conf/'.$file,
3027
                                api_get_path(CONFIGURATION_PATH).$file
3028
                            );
3029
                        }
3030
                    }
3031
                }
3032
3033
                error_log('Upgrade 1.10.x process concluded! ('.date('Y-m-d H:i:s').')');
3034
            } else {
3035
                error_log('There was an error during running migrations. Check error.log');
3036
                break;
3037
            }
3038
        case '1.10.0':
3039
            // no break
3040
        case '1.10.2':
3041
            // no break
3042
        case '1.10.4':
3043
            // no break
3044
        case '1.10.6':
3045
            // no break
3046
        case '1.10.8':
3047
            $database = new Database();
3048
            $database->setManager($manager);
3049
            // Migrate using the migration files located in:
3050
            // src/Chamilo/CoreBundle/Migrations/Schema/V111
3051
            $result = migrate(
3052
                111,
3053
                $manager
3054
            );
3055
3056
            if ($result) {
3057
                error_log('Migrations files were executed ('.date('Y-m-d H:i:s').')');
3058
3059
                $connection->executeQuery("ALTER TABLE course_category MODIFY COLUMN auth_course_child VARCHAR(40) DEFAULT 'TRUE'");
3060
3061
                // Fix post_group_id
3062
                $sql = "SELECT * FROM c_student_publication where (post_group_id <> 0 or post_group_id is not null)";
3063
                $statement = $connection->executeQuery($sql);
3064
                $result = $statement->fetchAll();
3065
                foreach ($result as $row) {
3066
                    $groupId = $row['post_group_id'];
3067
                    $courseId = $row['c_id'];
3068
                    $sessionId = $row['session_id'];
3069
                    $workIid = $row['iid'];
3070
                    $sql = "SELECT iid from c_group_info where c_id = $courseId and iid = $groupId";
3071
                    $statement = $connection->executeQuery($sql);
3072
                    $count = $statement->rowCount();
3073
                    if ($count == 0) {
3074
                        $sql = "SELECT iid from c_group_info where c_id = $courseId and id = $groupId";
3075
                        $statement = $connection->executeQuery($sql);
3076
                        $count = $statement->rowCount();
3077
                        if ($count > 0) {
3078
                            $rowGroup = $statement->fetch();
3079
                            $newGroupId = $rowGroup['iid'];
3080
                            if ($newGroupId) {
3081
                                $sqlUpdate = "UPDATE c_student_publication 
3082
                                              SET post_group_id = $newGroupId 
3083
                                              WHERE 
3084
                                                c_id = $courseId AND 
3085
                                                post_group_id = $groupId AND 
3086
                                                iid = $workIid
3087
                                              ";
3088
                                $connection->executeQuery($sqlUpdate);
3089
                            }
3090
                        }
3091
                    }
3092
                }
3093
3094
                // Fix work documents that don't have c_item_property value
3095
                $sql = "SELECT * FROM c_student_publication WHERE parent_id IS NOT NULL";
3096
                $statement = $connection->executeQuery($sql);
3097
                $result = $statement->fetchAll();
3098
                foreach ($result as $row) {
3099
                    $groupId  = $row['post_group_id'];
3100
                    $courseId = $row['c_id'];
3101
                    $sessionId = $row['session_id'];
3102
                    $workId = $row['id'];
3103
                    $itemInfo = api_get_item_property_info(
3104
                        $courseId,
3105
                        'work',
3106
                        $workId,
3107
                        $sessionId
3108
                    );
3109
                    $courseInfo = api_get_course_info_by_id($courseId);
3110
                    if (empty($itemInfo)) {
3111
                        api_item_property_update(
3112
                            $courseInfo,
3113
                            'work',
3114
                            $workId,
3115
                            'visible',
3116
                            1,
3117
                            $groupId,
3118
                            null,
3119
                            null,
3120
                            null,
3121
                            $sessionId
3122
                        );
3123
                    }
3124
                }
3125
                $sql = "UPDATE settings_current SET selected_value = '1.11.0' WHERE variable = 'chamilo_database_version'";
3126
                $connection->executeQuery($sql);
3127
3128
                if ($processFiles) {
3129
                    include __DIR__.'/update-files-1.10.0-1.11.0.inc.php';
3130
                }
3131
                error_log('Upgrade 1.11.x process concluded!  ('.date('Y-m-d H:i:s').')');
3132
            } else {
3133
                error_log('There was an error during running migrations. Check error.log');
3134
            }
3135
            break;
3136
        default:
3137
            break;
3138
    }
3139
3140
    echo '</div>';
3141
3142
    return true;
3143
}
3144