commit 2b7be83a7d5ce229badd8f5d6dba626ed8db7b22
Author: Jocelyn Delande <jocelyn at delalande dot fr>
Date:   Mon Dec 9 23:34:49 2013 +0100

    moved panorama generation to class : PanoramaGenerator

diff --git a/class/TilesGenerator.php b/class/TilesGenerator.php
new file mode 100644
index 0000000..763cc25
--- /dev/null
+++ b/class/TilesGenerator.php
@@ -0,0 +1,81 @@
+<?php
+
+require_once(dirname(__FILE__).'/../constants.inc.php');
+
+class TilesGeneratorException extends Exception {}
+class TilesGeneratorRightsException extends TilesGeneratorException {}
+class TilesGeneratorScriptException extends TilesGeneratorException {}
+
+class TilesGenerator {
+	private $panorama;
+
+	const SCRIPT_RELATIVE_PATH = 'to_tiles/gen_tiles.sh';
+
+	public function __construct($img_path, $panorama) {
+		/** @param $img_path : path of the source image
+		 *  @param $panorama : a site_point instance
+		 */
+		$this->panorama = $panorama;
+		$this->img_path = $img_path;
+	}
+
+	public function prepare() {
+		$err = false;
+		$pano_path = $this->panorama->tiles_path();
+
+		if (! is_dir(PANORAMA_PATH)) {
+			if (! mkdir(PANORAMA_PATH)) {
+				$err = "le répertoire \"PANORAMA_PATH\" n'est pas accessible et ne peut être créé";
+			}
+		} else if (file_exists($pano_path)) {
+			$err = sprintf("le nom de répertoire \"%s\" est déjà pris",
+			               $pano_path);
+		} else {
+			mkdir($pano_path);
+		}
+		if ($err) {
+			throw (new TilesGeneratorRightsException($err));
+		}
+
+	}
+
+	public function mk_command() {
+		/** Returns the command to execute
+		 */
+		$c = sprintf('%s/%s -p "%s" "%s"',
+		             CELUTZ_PATH, $this::SCRIPT_RELATIVE_PATH,
+		             $this->panorama->tiles_prefix(), $this->img_path);
+		return escapeshellcmd($c);
+	}
+
+	public function process() {
+		$err = false;
+		if ($fp = popen($this->mk_command(), 'r')) {
+			while (!feof($fp)) {
+				$results = fgets($fp, 4096);
+				if (strlen($results) == 0) {
+					// stop the browser timing out
+					flush();
+				} else {
+					$tok = strtok($results, "\n");
+					while ($tok !== false) {
+						echo htmlspecialchars(sprintf("%s\n",$tok))."<br/>";
+						flush();
+						$tok = strtok("\n");
+					}
+				}
+			}
+			if (pclose($fp) !== 0) {
+				$err = "Opération en échec durant l'exécution du script";
+			}
+		} else {
+			$err =  'Opération en échec à l\'ouverture du script !';
+		}
+
+		if ($err) {
+			throw (new TilesGeneratorScriptException($err));
+		}
+	}
+}
+
+?>
diff --git a/class/site_point.class.php b/class/site_point.class.php
index d19749c..4df9494 100644
--- a/class/site_point.class.php
+++ b/class/site_point.class.php
@@ -18,9 +18,9 @@ class site_point {
 
   public function __construct($dir) {
     // si $dir n'est pas un répertoire il ne s'agit pas d'un panorama.
-	  if (!is_dir($dir)) {
-		  throw new PanoramaFormatException("$dir does not contain a panorama");
-	  }
+	  // if (!is_dir($dir)) {
+	  // 	  throw new PanoramaFormatException("$dir does not contain a panorama");
+	  // }
     $this->base_dir = $dir;
     $this->prefix = basename($dir);
   }
@@ -29,6 +29,14 @@ class site_point {
 	  return $this->base_dir.'/'.$this->prefix.'.params';
   }
 
+  public function tiles_path() {
+	  return $this->base_dir;
+  }
+
+  public function tiles_prefix() {
+	  return $this->base_dir.'/'.$this->get_prefix();
+  }
+
   private function parse_and_cache_params() {
     if (is_file($this->params_path())) {
 	    $params = parse_ini_file($this->params_path());
@@ -82,6 +90,7 @@ class site_point {
 
   public function get_magnifications() {
     $dir_fd = opendir($this->base_dir);
+    $zoom_array = array();
     while (false !== ($file = readdir($dir_fd))) {                // extraction des paramètres de grossissement par le serveur
        //echo $file;
        if (preg_match('/(.*)_([0-9]+)_([0-9]+)_([0-9]+)\.jpg$/', $file, $reg)) {
diff --git a/class/utils.class.php b/class/utils.class.php
index 0c2faff..2c2c5c3 100644
--- a/class/utils.class.php
+++ b/class/utils.class.php
@@ -42,6 +42,12 @@ abstract class utils {
 	  }
 	  return $ret;
   }
+  public static function strip_extension($filename) {
+	  /** Removes the extension from a file name
+	   * @return the stripped name
+	   */
+	  return preg_replace('/\.[^.]+$/', '', $filename);
+  }
 }
 
 ?>
diff --git a/genererPano.php b/genererPano.php
index 4d334b4..8e1effa 100644
--- a/genererPano.php
+++ b/genererPano.php
@@ -6,55 +6,45 @@
   <title>convertiseur image vers panorama</title>
 </head>
 
+<body>
 <?php
 require_once 'class/utils.class.php';
+require_once 'class/site_point.class.php';
+require_once 'class/TilesGenerator.php';
 require_once 'constants.inc.php';
 utils::init();
 if (isset($_GET['name'])) {
-  $pano_name = $_GET['name'];
-  $pano_basename = preg_replace('/\.[^.]+$/', '', $pano_name);
-  
-  //Partie exécutante du script gen_tiles qui gènere les tuiles à partir d'une image.
-  $pano_dest = PANORAMA_PATH.'/'.$pano_basename;
-  if (! is_dir(PANORAMA_PATH)) {
-    if (! mkdir(PANORAMA_PATH)) {
-        echo "<p class=\"error\">le répertoire \"PANORAMA_PATH\" n'est pas accessible et ne peut être créé</p>\n";
-    }
-  } else if (file_exists($pano_dest)) {
-    echo "<p class=\"error\">le nom de répertoire \"$pano_dest\" est déjà pris</p>\n";
-  } else {
-    mkdir($pano_dest);
-    $escaped_command = escapeshellcmd('./to_tiles/gen_tiles.sh -p '.$pano_dest.'/'.$pano_basename.' ./upload/'.$pano_name);
-                
-    printf("<h2>Exécution de la commande :</h2>\n<p class=\"cmd\"><samp>%s</samp></p>\n<pre>", htmlspecialchars($escaped_command));
-    if ($fp = popen($escaped_command, 'r')) {
-      while (!feof($fp)) {
-        //set_time_limit (20); 
-        $results = fgets($fp, 4096);
-        if (strlen($results) == 0) {
-          // stop the browser timing out
-          flush();
-        } else {
-          $tok = strtok($results, "\n");
-          while ($tok !== false) {
-            echo htmlspecialchars(sprintf("%s\n",$tok))."<br/>";
-            flush(); 
-            $tok = strtok("\n");
-          }
-        }
-      }
-      print("</pre>\n");
-      if (pclose($fp) === 0) {
-        print("<h4><span class=\"success\">Opération réussie</span></h4>\n");
-        printf("<p>Pour acceder directement au panorama <a href=\"panorama.php?dir=%s&amp;panorama=%s\">cliquer ici</a></p>\n", 
-               PANORAMA_PATH, $pano_basename);
-      } else {
-        print("<h4><span class=\"error\">Opération en échec durant l'exécution du script !</span></h4>\n");
-      }
-    } else {
-      print("<h4><span class=\"error\">Opération en échec à l'ouverture du script !</span></h4>\n");
-    }
+  $image_path = UPLOAD_PATH.'/'.$_GET['name'];
+  // We init the panorama with the same name as image.
+  $pano_name = utils::strip_extension($_GET['name']);
+  $panorama = site_point::get($pano_name);
+
+  $tiles_generator = new TilesGenerator($image_path, $panorama);
+
+  try {
+    $tiles_generator->prepare();
+    printf("<h2>Exécution de la commande :</h2>\n");
+    printf("<p class=\"cmd\"><samp>%s</samp></p>\n",
+           $tiles_generator->mk_command());
+
+    echo "<pre>\n";
+    $tiles_generator->process();
+    print("</pre>\n");
+
+
+    print("<h4><span class=\"success\">Opération réussie</span></h4>\n");
+    printf("<p>Pour acceder directement au panorama <a href=\"%s\">cliquer ici</a></p>\n",
+           $panorama->get_url());
+    print("<p>Pour acceder à la liste des panoramas <a href=\".\">cliquer ici</a></p>\n") ;
+
+
+  } catch (TilesGeneratorRightsException $e) {
+    printf("<p class=\"error\">%s</p>\n", $e->getMessage());
+  } catch (TilesGeneratorScriptException $e) {
+    printf("<h4><span class=\"error\">%s</span></h4>\n", $e->getMessage());
+    print("</pre>\n");
   }
-  print("<p>Pour acceder à la liste des panoramas <a href=\".\">cliquer ici</a></p>\n") ;
 }
 ?>
+</body>
+</html>
