-
Notifications
You must be signed in to change notification settings - Fork 1
/
uploadReceive.php
145 lines (122 loc) · 4 KB
/
uploadReceive.php
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php
require_once('class/FormValidator.class.php');
require_once('class/site_point.class.php');
require_once('class/utils.class.php');
require_once('constants.inc.php');
class UploadReceiveError extends Exception {}
////////////////////// actions //////////////////////////////////////////
function handle_upload() {
$upload_messages = array(
UPLOAD_ERR_NO_FILE => 'pas de fichier envoyé',
UPLOAD_ERR_INI_SIZE => 'fichier trop gros',
UPLOAD_ERR_FORM_SIZE => 'fichier trop gros',
);
if (! is_dir(UPLOAD_PATH)) {
if (! mkdir(UPLOAD_PATH)) {
throw new UploadReceiveError(
'Dossier "'.UPLOAD_PATH.'" non inscriptible ou inexistant.');
}
}
foreach ($_FILES['files']['name'] as $i => $file) {
$file_err = $_FILES['files']['error'][$i];
$file_tmp = $_FILES['files']['tmp_name'][$i];
$file_finalpath = utils::get_unique_filepath(UPLOAD_PATH.'/'.basename($file));
if(!empty($file)) {
if(isset($file) && UPLOAD_ERR_OK === $file_err) {
move_uploaded_file($file_tmp, $file_finalpath);
return $file_finalpath;
} else {
throw new UploadReceiveError(
'Une erreur interne a empêché l\'envoi de l\'image :'. $upload_messages[$file_err]);
}
} else {
throw new UploadReceiveError(
'Veuillez passer par le formulaire svp !');
}
}
}
function existant_and_set($list, $keys) {
/** For HTTP data : keys of $keys are set within $list and they are not empty
* nor false.
*/
foreach($keys as $key) {
if (!isset($list[$key]) || !$list[$key]) {
return false;
}
}
return true;
}
////////////////////// main //////////////////////////////////////////
$fields_spec = array('lat' => array('numeric'),
'lon' => array('numeric'),
'alt' => array('numeric', 'positive'),
'loop' => array('boolean'),
'titre' => array('required'),
);
$validator = new FormValidator($fields_spec);
////// STEP 1 : UPLOAD ////////
$upload_success = false;
$uploaded_filepath = '';
if ($validator->validate($_REQUEST)) {
try {
$uploaded_filepath = handle_upload();
$upload_success = true;
$message = sprintf("transfert de %s réalisé", basename($uploaded_filepath));
} catch (UploadReceiveError $e) {
$message = $e->getMessage();
}
} else {
$message = 'paramètres invalides';
}
////// STEP 2 : PARAMETERS ////////
$params_success = false;
if ($upload_success) {
$vals = $validator->sane_values();
// There is no point setting a part of the parameters only ; check that all
// are present.
if (existant_and_set($vals, array('lat', 'alt', 'lon', 'titre'))) {
try {
$panorama = site_point::create($uploaded_filepath);
$panorama->set_param('titre', 'Sans nom 1');//FIXME
$panorama->set_param('latitude', $vals['lat']);
$panorama->set_param('longitude', $vals['lon']);
$panorama->set_param('altitude', $vals['alt']);
$panorama->set_param('image_loop', $vals['loop']);
$panorama->set_param('titre', $vals['titre']);
$panorama->save_params();
$params_success = true;
} catch (Exception $e) {
$message = 'erreur à la création du panorama : '.$e->getMessage();
}
}
}
////// STEP 3 : TILES ////////
// We do it in a redirection
if ($upload_success) {
utils::relative_redirect(
$panorama->get_generate_url(basename($uploaded_filepath)));
}
?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr" lang="fr">
<head>
<meta http-equiv="Content-type" content="text/html; charset=UTF-8"/>
<title>Transfert de panoramique</title>
</head>
<body>
<?php
if (isset($message)) {
echo "<h2>$message</h2>\n";
if ($validator->errors()) {
foreach($validator->errors() as $key => $error) {
printf('<p>"%s" : %s</p>', $_REQUEST[$key], $error);
}
} else {
?>
<p>Pour acceder à la liste des images transférées afin de convertir en panorama <a href="creerPano.php">cliquer ici</a></p>
<?php
}
}
?>
</body>
</html>