Browse Source

Base version

Phyks (Lucas Verney) 5 years ago
parent
commit
d00db792a9
4 changed files with 170 additions and 0 deletions
  1. 1
    0
      .gitignore
  2. 2
    0
      README.md
  3. 137
    0
      index.php
  4. 30
    0
      main.css

+ 1
- 0
.gitignore View File

@@ -0,0 +1 @@
1
+data/

+ 2
- 0
README.md View File

@@ -2,3 +2,5 @@ DefiVelib
2 2
 =========
3 3
 
4 4
 A website to compare travel times in Velib at Paris.
5
+
6
+Currently working on…

+ 137
- 0
index.php View File

@@ -0,0 +1,137 @@
1
+<?php
2
+    if(is_file('data/data')) {
3
+        $data = unserialize(gzinflate(base64_decode(file_get_contents('data/data'))));
4
+    }
5
+    else {
6
+        $data = array();
7
+    }
8
+
9
+    if((!empty($_POST['time_min']) || !empty($_POST['time_sec'])) && !empty($_POST['start']) && !empty($_POST['end'])) {
10
+        $min = (!empty($_POST['time_min'])) ? (int) $_POST['time_min'] : 0;
11
+        $sec = (!empty($_POST['time_sec'])) ? (int) $_POST['time_sec'] : 0;
12
+
13
+        $data[] = array("start"=>(int) $_POST['start'], "end"=>(int) $_POST['end'], "min"=>$min, "sec"=>$sec);
14
+
15
+        $last_data = end($data);
16
+
17
+        if($min != $last_data['min'] || $sec != $last_data['sec'] || $_POST['start'] != $last_data['start'] || $_POST['end'] != $last_data['end']) {
18
+            file_put_contents('data/data', base64_encode(gzdeflate(serialize($data))));
19
+        }
20
+    }
21
+?>
22
+<!DOCTYPE html>
23
+<html lang="fr">
24
+	<head>
25
+		<meta charset="utf-8">
26
+		<title>Vélibs à proximité</title>
27
+		<meta name="author" content="phyks">
28
+		<link rel="stylesheet" href="main.css" type="text/css" media="screen">
29
+	</head>
30
+	<body>
31
+<div id="main">
32
+		<h1><a href="index.php">DéfiVélib</a></h1>
33
+        <?php
34
+        if(!is_dir('data/')) {
35
+            mkdir('data/');
36
+        }
37
+
38
+        if(!is_file('data/config')) //First run
39
+        {
40
+            //Define a new synchronisation code
41
+            $code_synchro = substr(sha1(rand(0,30).time().rand(0,30)),0,10);
42
+
43
+            file_put_contents('data/config', base64_encode(gzdeflate(serialize(array($code_synchro))))); //Save it in data/data file
44
+
45
+            $_GET['code'] = $code_synchro;
46
+
47
+            echo "<p>
48
+                Définition du code de synchronisation.<br/>
49
+                Vous pouvez désormais mettre à jour la liste des stations en visitant l'adresse suivante (update URL) :<br/>
50
+                <a href='http://" . $_SERVER["SERVER_NAME"].$_SERVER['REQUEST_URI']."?update=1&code=".$code_synchro."'>http://" . $_SERVER["SERVER_NAME"].$_SERVER['REQUEST_URI']."?update=1&code=".$code_synchro."</a>
51
+                </p>
52
+                <p>
53
+                Il est possible d'automatiser la tâche via une tâche cron. Par exemple (see README) :<br/>
54
+                * * * * * wget -q -O <a href='http://" . $_SERVER["SERVER_NAME"].$_SERVER['REQUEST_URI']."?update=1&code=".$code_synchro."'>http://" . $_SERVER["SERVER_NAME"].$_SERVER['REQUEST_URI']."?update=1&code=".$code_synchro."</a> #Commande de mise a jour des stations de velib
55
+                </p>";
56
+        }
57
+
58
+        if(!empty($_GET['update']) || !empty($code_synchro)) //If we want to make an update (or first run)
59
+        {
60
+            if(empty($code_synchro) && is_file('data/config')) //If not first run, get the synchronisation code from data file
61
+            {
62
+                $data = unserialize(gzinflate(base64_decode(file_get_contents('data/config'))));
63
+                $code_synchro = $data[0];
64
+            }
65
+
66
+            if(!empty($_GET['code']) && $_GET['code'] == $code_synchro) //Once we have the code and it is correct
67
+            {
68
+                $stations_xml = simplexml_load_file('http://www.velib.paris.fr/service/carto');
69
+
70
+                $liste_stations = array();
71
+                foreach($stations_xml->markers->marker as $station) {
72
+                    $liste_stations[(int) $station['number']] = array('name'=>(string) $station['name'], 'address'=>(string) $station['fullAddress'], 'lat'=>(float) $station['lat'], 'lng'=>(float) $station['lng']);
73
+                }
74
+
75
+                file_put_contents('data/stations', base64_encode(gzdeflate(serialize($liste_stations))));
76
+
77
+                echo "<p>Mise à jour de la liste des stations effectuée avec succès (Update successful).</p>";
78
+            }
79
+            else
80
+            {
81
+                echo "<p>Mauvais code de vérification (Error : bad synchronisation code). Veuillez réessayer la mise à jour. Se référer au README pour plus d'informations sur la mise à jour.</p>";
82
+            }
83
+            echo "<p><a href='index.php'>Retourner à l'application (Back to index)</a></p></body></html>";
84
+            exit();
85
+        }
86
+        $liste_stations = unserialize(gzinflate(base64_decode(file_get_contents('data/stations'))));
87
+    ?>
88
+    <h2>Ajouter un trajet</h2>
89
+    <form method="post" action="index.php">
90
+        <p><label name="start">Station de départ : </label>
91
+            <select name="start">
92
+                <?php
93
+                    foreach($liste_stations as $key=>$station) {
94
+                        echo "<option value=\"".$key."\">".$station['name']."</option>";
95
+                    }
96
+                ?>
97
+            </select>
98
+        </p>
99
+        <p><label for="end">Station d'arrivée : </label>
100
+            <select name="end">
101
+                <?php
102
+                    foreach($liste_stations as $key=>$station) {
103
+                        echo "<option value=\"".$key."\">".$station['name']."</option>";
104
+                    }
105
+                ?>
106
+            </select>
107
+        </p>
108
+        <p><label for="time_min">Durée du trajet : </label><input type="int" name="time_min" id="time_min" size="2"/>min <input type="int" name="time_sec" id="time_sec" size="2"/>s</p>
109
+        <p><input type="submit" value="Envoyer"></p>
110
+    </form>
111
+    <h2>Derniers trajets ajoutés</h2>
112
+    <?php
113
+        if(!empty($data)) {
114
+    ?>
115
+            <table>
116
+                <tr>
117
+                    <th>Départ</th>
118
+                    <th>Arrivée</th>
119
+                    <th>Temps</th>
120
+                </tr>
121
+                <?php
122
+                    foreach($data as $trajet) {
123
+                        echo "<tr><td>".htmlspecialchars($liste_stations[$trajet['start']]['name'])."</td><td>".htmlspecialchars($liste_stations[$trajet['end']]['name'])."</td><td>".(int) $trajet['min']."min ".(int) $trajet['sec']."s</td></tr>";
124
+                    }
125
+                ?>
126
+            </table>
127
+    <?php
128
+        }
129
+        else {
130
+    ?>
131
+            <p>Aucun trajet enregistré.</p>
132
+    <?php
133
+        }
134
+    ?>
135
+    </div>
136
+    </body>
137
+</html>

+ 30
- 0
main.css View File

@@ -0,0 +1,30 @@
1
+html, body {
2
+    margin: 0;
3
+    padding: 0;
4
+    width: 100%;
5
+    height: 100%;
6
+}
7
+
8
+#main {
9
+    width: 50%;
10
+    min-height: 50%;
11
+    margin: auto;
12
+    text-align: center;
13
+    background-color: lightgrey;
14
+    padding: 1em;
15
+    -moz-box-sizing: border-box; -webkit-box-sizing: border-box; box-sizing: border-box;
16
+}
17
+
18
+#main h1 {
19
+    margin: 0;
20
+}
21
+
22
+table td, th {
23
+    padding: 0.5em;
24
+    border: 1px solid black;
25
+}
26
+
27
+table {
28
+    border-collapse: collapse;
29
+    margin: auto;
30
+}