Projet

Général

Profil

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

root / uploadReceive.php @ c621f296

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
  if (! is_dir(UPLOAD_PATH)) {
15
    if (! mkdir(UPLOAD_PATH)) {
16
      throw new UploadReceiveError(
17
        'Dossier "'.UPLOAD_PATH.'" non inscriptible ou inexistant.');
18
    }
19
  }
20
  foreach ($_FILES['files']['name'] as $i => $file) {
21
    $file_err = $_FILES['files']['error'][$i];
22
    $file_tmp = $_FILES['files']['tmp_name'][$i];
23
    $file_finalpath = utils::get_unique_filepath(UPLOAD_PATH.'/'.basename($file));
24

    
25
    if(!empty($file)) {
26
      if(isset($file) && UPLOAD_ERR_OK === $file_err) {
27
              move_uploaded_file($file_tmp, $file_finalpath);
28
        return $file_finalpath;
29
      } else {
30
        throw new UploadReceiveError(
31
          'Une erreur interne a empêché l\'envoi de l\'image :'. $file_err);
32
      }
33
    } else {
34
      throw new UploadReceiveError(
35
        'Veuillez passer par le formulaire svp !');
36
    }
37
  }
38
}
39

    
40
function existant_and_set($list, $keys) {
41
  /** For HTTP data : keys of $keys are set within $list and they are not empty
42
  * nor false.
43
  */
44
  foreach($keys as $key) {
45
    if (!isset($list[$key]) || !$list[$key]) {
46
      return false;
47
    }
48
  }
49
  return true;
50
}
51

    
52
////////////////////// main //////////////////////////////////////////
53

    
54
$fields_spec = array('lat'         => array('numeric', 'positive'),
55
                     'lon'         => array('numeric', 'positive'),
56
                     'alt'  => array('numeric', 'positive'),
57
                     'loop'  => array('boolean'),
58
                     'titre'  => array('required'),
59
);
60

    
61
$validator = new FormValidator($fields_spec);
62

    
63
////// STEP 1 : UPLOAD ////////
64

    
65
$upload_success = false;
66
$uploaded_filepath = '';
67

    
68
if ($validator->validate($_REQUEST)) {
69
  try {
70
    $uploaded_filepath = handle_upload();
71
    $upload_success = true;
72
    $message = sprintf("transfert de %s réalisé", basename($uploaded_filepath));
73
  } catch (UploadReceiveError $e) {
74
    $message = $e->getMessage();
75
  }
76
} else {
77
  $message = 'paramètres invalides';
78
}
79

    
80
////// STEP 2 : PARAMETERS ////////
81

    
82
$params_success = false;
83

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

    
105

    
106
?>
107

    
108
<!DOCTYPE html>
109
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr" lang="fr">
110
<head>
111
   <meta http-equiv="Content-type" content="text/html; charset=UTF-8"/>
112
   <title>Transfert de panoramique</title>
113
</head>
114
<body>
115
<?php
116
if (isset($message)) {
117
  echo "<h2>$message</h2>\n";
118
  if ($validator->errors()) {
119
    foreach($validator->errors() as $key => $error) {
120
      printf('<p>"%s" : %s</p>', $_REQUEST[$key], $error);
121
    }
122

    
123
  } else {
124
?>
125
    <p>Pour acceder à la liste des images transférées afin de convertir en panorama <a href="creerPano.php">cliquer ici</a></p>
126
<?php 
127
  }
128
}
129
?>
130
</body>
131
</html>