aboutsummaryrefslogtreecommitdiff
path: root/bin/install_fullcalendar.php
blob: ee3dacac84fd118d5c0903867d1db3d57aba2682 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php
// bin/install_fullcalendar.php

function installFullCalendar(): void
{
	$path = 'public/js/fullcalendar';
    $links = [
        'index.global.min.js' => "https://cdn.jsdelivr.net/npm/fullcalendar/index.global.min.js",
        'fr.global.min.js' => "https://cdn.jsdelivr.net/npm/@fullcalendar/core/locales/fr.global.min.js"
    ];

    foreach($links as $key => $link){
        $curl = curl_init($link);
        if(!$curl){ // lien non valide
            echo "Erreur : Impossible d'initialiser cURL.\n";
            return;
        }

        if(!is_dir($path)){
            mkdir($path, 0755, true);
        }

        $old_version = getVersion($path . '/' . $key); // peut être null

        $file = @fopen($path . '/' . $key, 'w+'); // @masque l'erreur pour la traiter soi-même
        if(!$file){ // erreur écriture fichier
            echo "Erreur : Impossible d'ouvrir le fichier $path pour l'écriture.\n";
            echo "Détails de l'erreur : " . error_get_last()['message'] . "\n";
            return;
        }

        curl_setopt($curl, CURLOPT_FILE, $file);
        curl_setopt($curl, CURLOPT_HEADER, 0);

        $response = curl_exec($curl);
        if(!$response){ // erreur téléchargement
            echo "Erreur : Le téléchargement a échoué. cURL Error: " . curl_error($curl) . "\n";
        }

        fclose($file);
        curl_close($curl);

        $new_version = getVersion($path . '/' . $key);

        if(!$old_version){
            echo 'public/js/fullcalendar/' . $key . ' installé ' . $new_version . "\n";
        }
        elseif($old_version === $new_version){
            echo 'public/js/fullcalendar/' . $key . ' réinstallé ' . $new_version . "\n";
        }
        else{
            echo 'public/js/fullcalendar/' . $key . ' mis à jour de ' . $old_version . ' vers ' . $new_version . "\n";
        }
    }
}

function getVersion(string $path): ?string
{
    if(!is_file($path)){
        return null;
    }

    $file = fopen($path, 'r');
    if(!$file){
        return null;
    }

    for($i = 0; $i < 5 && ($line = fgets($file)) !== false; $i++){
        if(preg_match('/FullCalendar .*? (v[0-9.]+)/', $line, $matches)){
            fclose($file);
            return $matches[1];
        }
    }

    fclose($file);
    return null;
}

installFullCalendar();