<?php

  class env
  {
    public $config_file;
    public $PATHES;
    public $PARAMS;
    public $CONFIG;
    public $prefix;
    public $additional_get_params;
    public $bdd;
    public $acces;
    public $mod_path;

    function __construct($config_file, $path_prefix = "")
    { $this->mod_path = "";
      $this->config_file = $config_file;
      if(file_exists($config_file))
      { $this->PATHES = array();
        $this->bdd = array();
        $this->CONFIG = array();
        require $config_file;
        if(file_exists($this->path("app")."config.xml"))
        { $app_config = simplexml_load_file($this->path("app")."config.xml");
          foreach($app_config->pathes[0] as $path_name => $path_value)
          { $this->PATHES[$path_name] = $this->path("app").utf8_decode($path_value);
          }
          $this->PATHES["punbb"] = str_replace("app/../", "", $this->PATHES["punbb"]);
          $this->PARAMS = array();
          foreach($app_config->params[0] as $code_param => $get_param)
          { $this->PARAMS[$code_param] = utf8_decode($get_param);
          }
          $this->init_additional_get_params();
          $this->prefix = utf8_decode($app_config->prefix);
          $this->bdd["prefix_code"] = utf8_decode($app_config->sql_prefix);
        }
        else
        { echo "[erreur] config : impossible de trouver le fichier de configuration pour l'application";
          exit();
        }
      }
      else
      { echo "[erreur] config : impossible de trouver le fichier de configuration pour l'installation";
        exit();
      }
    }

    function set_config($config)
    { if(is_array($config))
      { foreach($config as $key => $value) $this->CONFIG[$key] = $value;
        return true;
      }
      return false;
    }

    function init_additional_get_params()
    { $this->additional_get_params = array();
      $_params = $_SERVER["QUERY_STRING"];
      $v_params = explode("&", $_params);
      foreach($v_params as $param)
      { if($param)
        { $key = strpos($param, "=") === false ? $param : substr($param, 0, strpos($param, "="));
          $value = strpos($param, "=") === false ? "" : substr($param, strpos($param, "=") + 1);
          if(!$this->is_a_param($key)) $this->additional_get_params[$key] = $value;
        }
      }
    }

    function is_a_param($key)
    { foreach($this->PARAMS as $_key => $_value) if(strcmp($key, $_value) == 0) return true;
      return false;
    }

    function path($name) { return $this->PATHES[$name]; }
    function param($name) { return $this->PARAMS[$name]; }
    function config($name) { return $this->CONFIG[$name]; }

    function url($_params, $script_name = "index.php", $_url = "")
    { if($_url)
      { if
        ( strpos($_url, "http://") === 0  ||
          strpos($_url, "https://") === 0 ||
          strpos($_url, "ftp://") === 0
        ) $url = $_url;
        else $url = $this->path("web").$_url;
      }
      elseif($_params)
      { $get_params = "";
        foreach($this->additional_get_params as $key => $value) $get_params .= ($get_params ? "&amp;" : "?").$key."=".$value;
        foreach($_params as $key => $value) $get_params .= ($get_params ? "&amp;" : "?").$this->param($key)."=".$value;
        $url = $this->path("web").$script_name.$get_params;
      }
      return $url;
    }

    function out_file_exists($file)
    { return
      ( $this->config("out") ?
          file_exists($this->config("out").$file) ||
          file_exists($this->path("out").$file) ||
          file_exists($this->path("dist_out").$file)
        : file_exists($this->path("out").$file) ||
          file_exists($this->path("dist_out").$file)
      );
    }

    function out_file($file)
    { return
      ( $this->config("out") ?
          ( file_exists($this->config("out").$file) ?
              $this->config("out").$file
            : ( file_exists($this->path("out").$file) ?
                  $this->path("out").$file
                : (file_exists($this->path("dist_out").$file) ? $this->path("dist_out").$file : $file)
              )
          )
        : ( file_exists($this->path("out").$file) ?
              $this->path("out").$file
            : (file_exists($this->path("dist_out").$file) ? $this->path("dist_out").$file : $file)
          )
      );
    }

    function aff_content()
    { $env = $this;
      if($env->message) require $env->out_file("views/message.php");
      if($env->out_file_exists("content/".$env->prefix.$env->e.".php"))
      { require $env->out_file("content/".$env->prefix.$env->e.".php");
      }
    }

    function aff_emplacement($emplacement)
    { $env = $this;
      if($env->out["blocs"]["list"]) foreach($env->out["blocs"]["list"] as $id_bloc => $bloc)
      { if($bloc["emplacement"] == $emplacement)
        { $env->out["bloc"] = $bloc;
          $env->_aff_bloc();
        }
      }
    }

    function aff_bloc($bloc)
    { $env = $this;
      if($env->out["blocs"]["list"]) foreach($env->out["blocs"]["list"] as $id_bloc => $_bloc)
      { if(is_int($bloc) ? $id_bloc == $bloc : $_bloc["nom"] == $bloc)
        { $env->out["bloc"] = $env->out["blocs"]["list"][$id_bloc];
          $env->_aff_bloc();
        }
      }
    }

    function _aff_bloc()
    { $env = $this;
      require $env->out_file("views/bloc.php");
    }

  }

?>