Projet

Général

Profil

Paste
Télécharger (3,85 ko) Statistiques
| Branche: | Révision:

root / uploadReceive.php @ f0cea9c4

1
<?php
2
 
3
require_once('class/FormValidator.class.php');
4
require_once('class/site_point.class.php');
5
require_once('class/utils.class.php');
6
require_once('constants.inc.php');
7

    
8
class UploadReceiveError extends Exception {}
9

    
10

    
11
////////////////////// actions //////////////////////////////////////////
12

    
13
function handle_upload() {
14
  $upload_messages = array(
15
    UPLOAD_ERR_NO_FILE   => 'pas de fichier envoyé',
16
    UPLOAD_ERR_INI_SIZE  => 'fichier trop gros',
17
    UPLOAD_ERR_FORM_SIZE => 'fichier trop gros',
18
  );
19

    
20
  if (! is_dir(UPLOAD_PATH)) {
21
    if (! mkdir(UPLOAD_PATH)) {
22
      throw new UploadReceiveError(
23
        'Dossier "'.UPLOAD_PATH.'" non inscriptible ou inexistant.');
24
    }
25
  }
26
  foreach ($_FILES['files']['name'] as $i => $file) {
27
    $file_err = $_FILES['files']['error'][$i];
28
    $file_tmp = $_FILES['files']['tmp_name'][$i];
29
    $file_finalpath = utils::get_unique_filepath(UPLOAD_PATH.'/'.basename($file));
30

    
31
    if(!empty($file)) {
32
      if(isset($file) && UPLOAD_ERR_OK === $file_err) {
33
              move_uploaded_file($file_tmp, $file_finalpath);
34
        return $file_finalpath;
35
      } else {
36
        throw new UploadReceiveError(
37
          'Une erreur interne a empêché l\'envoi de l\'image :'. $upload_messages[$file_err]);
38
      }
39
    } else {
40
      throw new UploadReceiveError(
41
        'Veuillez passer par le formulaire svp !');
42
    }
43
  }
44
}
45

    
46
function existant_and_set($list, $keys) {
47
  /** For HTTP data : keys of $keys are set within $list and they are not empty
48
  * nor false.
49
  */
50
  foreach($keys as $key) {
51
    if (!isset($list[$key]) || !$list[$key]) {
52
      return false;
53
    }
54
  }
55
  return true;
56
}
57

    
58
////////////////////// main //////////////////////////////////////////
59

    
60
$fields_spec = array('lat'         => array('numeric'),
61
                     'lon'         => array('numeric'),
62
                     'alt'  => array('numeric', 'positive'),
63
                     'loop'  => array('boolean'),
64
                     'titre'  => array('required'),
65
);
66

    
67
$validator = new FormValidator($fields_spec);
68

    
69
////// STEP 1 : UPLOAD ////////
70

    
71
$upload_success = false;
72
$uploaded_filepath = '';
73

    
74
if ($validator->validate($_REQUEST)) {
75
  try {
76
    $uploaded_filepath = handle_upload();
77
    $upload_success = true;
78
    $message = sprintf("transfert de %s réalisé", basename($uploaded_filepath));
79
  } catch (UploadReceiveError $e) {
80
    $message = $e->getMessage();
81
  }
82
} else {
83
  $message = 'paramètres invalides';
84
}
85

    
86
////// STEP 2 : PARAMETERS ////////
87

    
88
$params_success = false;
89

    
90
if ($upload_success) {
91
  $vals = $validator->sane_values();
92
  // There is no point setting a part of the parameters only ; check that all
93
  // are present.  
94
  if (existant_and_set($vals, array('lat', 'alt', 'lon', 'titre'))) {
95
    try {
96
      $panorama = site_point::create($uploaded_filepath);
97
      $panorama->set_param('titre', 'Sans nom 1');//FIXME
98
      $panorama->set_param('latitude',  $vals['lat']);
99
      $panorama->set_param('longitude', $vals['lon']);
100
      $panorama->set_param('altitude',  $vals['alt']);
101
      $panorama->set_param('image_loop', $vals['loop']);
102
      $panorama->set_param('titre', $vals['titre']);
103
      $panorama->save_params();
104
      $params_success = true;
105
    } catch (Exception $e) {
106
      $message = 'erreur à la création du panorama : '.$e->getMessage();
107
    }
108
  }
109
}
110

    
111

    
112
////// STEP 3 : TILES ////////
113

    
114
// We do it in a redirection
115

    
116
if ($upload_success) {
117
  utils::relative_redirect(
118
    $panorama->get_generate_url(basename($uploaded_filepath)));
119
}
120
?>
121

    
122
<!DOCTYPE html>
123
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr" lang="fr">
124
<head>
125
   <meta http-equiv="Content-type" content="text/html; charset=UTF-8"/>
126
   <title>Transfert de panoramique</title>
127
</head>
128
<body>
129
<?php
130
if (isset($message)) {
131
  echo "<h2>$message</h2>\n";
132
  if ($validator->errors()) {
133
    foreach($validator->errors() as $key => $error) {
134
      printf('<p>"%s" : %s</p>', $_REQUEST[$key], $error);
135
    }
136
  }
137
}
138
?>
139
</body>
140
</html>