Projet

Général

Profil

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

root / uploadReceive.php @ 2858c9fe

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
  * or false nor empty
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
);
59

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

    
62
////// STEP 1 : UPLOAD ////////
63

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

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

    
79
////// STEP 2 : PARAMETERS ////////
80

    
81
$params_success = false;
82

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

    
103

    
104
?>
105

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

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