|
11 | 11 | */ |
12 | 12 |
|
13 | 13 | // Loading configuration. |
| 14 | +use Symfony\Component\Filesystem\Filesystem; |
| 15 | +use Symfony\Component\Finder\Finder; |
| 16 | + |
14 | 17 | require_once __DIR__.'/../../../main/inc/global.inc.php'; |
15 | 18 |
|
16 | 19 | api_protect_admin_script(); |
|
21 | 24 | $dataroot = $_REQUEST['dataroot']; |
22 | 25 |
|
23 | 26 | $absalternatecourse = Virtual::getConfig('vchamilo', 'course_real_root'); |
24 | | -if (!empty($absalternatecourse)) { |
25 | | - // this is the relocated case |
26 | | - $coursedir = str_replace('//', '/', $absalternatecourse.'/'.$dataroot); |
27 | | -} else { |
28 | | - // this is the standard local case |
29 | | - $coursedir = api_get_path(SYS_PATH).$dataroot; |
30 | | -} |
31 | 27 |
|
32 | | -if (is_dir($coursedir)) { |
33 | | - $DIR = opendir($coursedir); |
34 | | - $cpt = 0; |
35 | | - $hasfiles = false; |
36 | | - while (($file = readdir($DIR)) && !$hasfiles) { |
37 | | - if (!preg_match("/^\\./", $file)) { |
38 | | - $hasfiles = true; |
| 28 | +try { |
| 29 | + if (!empty($absalternatecourse)) { |
| 30 | + if (!is_dir($absalternatecourse) || !realpath($absalternatecourse)) { |
| 31 | + throw new Exception("$absalternatecourse is not a directory"); |
39 | 32 | } |
40 | | - } |
41 | | - closedir($DIR); |
42 | 33 |
|
43 | | - if ($hasfiles) { |
44 | | - echo '<div class="error">'.$plugin->get_lang('datapathnotavailable').'</div>'; |
| 34 | + $absalternatecourse = realpath($absalternatecourse); |
| 35 | + |
| 36 | + // this is the relocated case |
| 37 | + $coursedir = str_replace('//', '/', $absalternatecourse.'/'.$dataroot); |
45 | 38 | } else { |
46 | | - echo '<div class="success">'.$plugin->get_lang('datapathavailable').'</div>'; |
| 39 | + // this is the standard local case |
| 40 | + $coursedir = api_get_path(SYS_PATH).$dataroot; |
47 | 41 | } |
48 | | - echo stripslashes($coursedir); |
49 | | -} else { |
50 | | - if (@mkdir($coursedir, 02777, true)) { |
51 | | - echo '<div class="success">'.$plugin->get_lang('datapathcreated').'</div>'; |
| 42 | + |
| 43 | + $fileSystem = new Filesystem(); |
| 44 | + |
| 45 | + $components = explode('://', $coursedir); |
| 46 | + $components = array_reverse($components); |
| 47 | + $coursedir = $components[0]; |
| 48 | + |
| 49 | + if (is_file($coursedir)) { |
| 50 | + throw new Exception("$coursedir is file"); |
| 51 | + } |
| 52 | + |
| 53 | + if ($fileSystem->exists($coursedir) && is_dir($coursedir)) { |
| 54 | + $finder = new Finder(); |
| 55 | + $finder->files()->in($coursedir); |
| 56 | + |
| 57 | + if ($finder->hasResults()) { |
| 58 | + echo '<div class="error">'.$plugin->get_lang('datapathnotavailable').'</div>'; |
| 59 | + } else { |
| 60 | + echo '<div class="success">'.$plugin->get_lang('datapathavailable').'</div>'; |
| 61 | + } |
52 | 62 | } else { |
53 | | - echo '<div class="error">'.$plugin->get_lang('couldnotcreatedataroot').'</div>'; |
| 63 | + $fileSystem->mkdir($coursedir, 02777); |
| 64 | + |
| 65 | + echo '<div class="success">'.$plugin->get_lang('datapathcreated').'</div>'; |
54 | 66 | } |
| 67 | + |
55 | 68 | echo stripslashes($coursedir); |
| 69 | +} catch (Exception $exception) { |
| 70 | + echo '<div class="error">'.$exception->getMessage().'</div>'; |
56 | 71 | } |
57 | 72 |
|
58 | 73 | echo "</p>"; |
|
0 commit comments