linea21-externals
[ class tree: linea21-externals ] [ index: linea21-externals ] [ all elements ]

Source for file Installer.php

Documentation is available at Installer.php

  1. <?php
  2. /**
  3.  * @package linea21.externals
  4.  * @subpackage Class Installer
  5.  * @author Vadim V. Gabriel <vadimg88@gmail.com> - modified by Simon Georget <simon@linea21.com>
  6.  * @link http://www.vadimg.co.il/
  7.  * @version 1.0.0a
  8.  * @license GNU Lesser General Public License
  9.  */
  10.  
  11. /** Load the template class **/
  12. require_once(INSTALLER_PATH '/Installer_Template.php');
  13.  
  14.  
  15. /**
  16.  * Class installer
  17.  *
  18.  */
  19. class Installer
  20. {
  21.   /**
  22.    * Options property
  23.    *
  24.    * @var array 
  25.    */
  26.   protected $_options = array();
  27.  
  28.   /**
  29.    * View object
  30.    *
  31.    * @var object 
  32.    */
  33.   protected $view;
  34.  
  35.   /**
  36.    * Language array
  37.    *
  38.    * @var array 
  39.    */
  40.   protected $lang = array();
  41.  
  42.   protected $default_lang = U_L;
  43.  
  44.   /**
  45.    * Constructor
  46.    *
  47.    */
  48.   public function __construct()
  49.   {
  50.       // get lang from browser
  51.       $lang substr($_SERVER['HTTP_ACCEPT_LANGUAGE']02);
  52.       
  53.       if(file_exists('../languages/' .$lang'/installer.utf-8.php')) {
  54.           $this->default_lang = $lang;
  55.       else {
  56.           $this->default_lang = 'fr';
  57.       }
  58.        
  59.     # Do we have a cookie
  60.     if(isset($_COOKIE['lang']&& $_COOKIE['lang'!= '')
  61.     {
  62.       $this->default_lang = $_COOKIE['lang'];
  63.     }
  64.  
  65.     # Change language
  66.     if(isset($_POST['lang']&& $_POST['lang'!= '' && $this->default_lang != $_POST['lang'])
  67.     {
  68.       $path INSTALLER_PATH DIRECTORY_SEPARATOR 'data' DIRECTORY_SEPARATOR 'lang' DIRECTORY_SEPARATOR $_POST['lang''.php';
  69.  
  70.       $path '../languages/' .$_POST['lang']'/installer.utf-8.php';
  71.  
  72.       if(file_exists($path))
  73.       {
  74.         $this->default_lang = $_POST['lang'];
  75.         @setcookie('lang'$this->default_langtime(60 60 24);
  76.         $_POST['lang'0;
  77.         $this->nextStep('index');
  78.       }
  79.     }
  80.  
  81.     # Load the language file
  82.     require_once'../languages/' .$this->default_lang'/installer.utf-8.php' );
  83.  
  84.     $this->lang = $lang;
  85.  
  86.     # Load the template class
  87.     $this->view = new Installer_Template($this->lang);
  88.     
  89.     # Are config files writable?
  90.     if(!is_writable('../install/'|| !is_writable('../install/installer/data/')) {
  91.         $this->view->error($this->lang['L-13']);
  92.     }
  93.     # Is config folder writable?
  94.     if(!is_writable('../config/')) {
  95.         $this->view->error($this->lang['L-12']);
  96.     }
  97.  
  98.     # Did we run it again?
  99.     if(file_exists(INSTALLER_PATH DIRECTORY_SEPARATOR 'data' DIRECTORY_SEPARATOR 'installer.lock'))
  100.     {
  101.       $this->view->error($this->lang['L-01']);
  102.     }
  103.  
  104.     $allwed_steps array('index' => 'indexAction''db' => 'dbAction''cfg' => 'configAction''database' => 'dbTables''config' => 'configWrite''finish' => 'finishInstaller');
  105.     if(!in_array($_POST['step']array_keys($allwed_steps)))
  106.     {
  107.       $this->nextStep('index');
  108.     }
  109.  
  110.     # Display the right step
  111.     $this->$allwed_steps[$_POST['step']]($_POST);
  112.   }
  113.  
  114.   /**
  115.    * Show welcome message
  116.    *
  117.    */
  118.   public function indexAction()
  119.   {
  120.     $_SESSION array()// unset session
  121.  
  122.     $options CultureSelectBox('lang'$this->default_lang);
  123.  
  124.     $this->view->vars array('options' => $options);
  125.     $this->view->render('index');
  126.   }
  127.  
  128.   /**
  129.    * Show database setup stuff
  130.    *
  131.    */
  132.   public function dbAction()
  133.   {
  134.     $this->view->render('db');
  135.   }
  136.  
  137.   public function configAction()
  138.   {
  139.     $root_path str_replace('install'''ROOT_PATH);
  140.     $root_url =str_replace('install/install.php'''BASE_URL);
  141.     $this->view->vars array('root_url' => $root_url'path' => $root_path);
  142.     $this->view->render('config');
  143.   }
  144.  
  145.   /**
  146.    * Handle DB table quries
  147.    *
  148.    * @param array $options 
  149.    */
  150.   public function dbTables(array $options)
  151.   {
  152.     # Make sure we have an array of options
  153.     if(!is_array($options))
  154.     {
  155.       $this->view->error($this->lang['L-02']);
  156.     }
  157.     # Make sure we have everything we need!
  158.     $required_db_options array('dbtype''dbname''dbuser''dbpass''dbhost');
  159.     foreach ($required_db_options as $required_db_option)
  160.     {
  161.       // set in session
  162.       if(isset($_SESSION[$required_db_option])) unset ($_SESSION[$required_db_option]);
  163.       $_SESSION[$required_db_option$options[$required_db_option];
  164.  
  165.       if(!isset($options[$required_db_option]))
  166.       {
  167.         $this->view->error($this->lang['L-03']);
  168.       }
  169.     }
  170.  
  171.     # Pass in to the options property
  172.     $this->_options $options;
  173.  
  174.     if($this->_options['dbtype']=='mysql'{
  175.       # First test the connection
  176.       $link mysql_connect($this->_options['dbhost']$this->_options['dbuser']$this->_options['dbpass']);
  177.       if(!$link)
  178.       {
  179.         $this->view->error($this->lang['L-04']);
  180.       }
  181.  
  182.       # Select the DB
  183.       $db_selected mysql_select_db($this->_options['dbname']$link);
  184.       mysql_query("SET NAMES 'utf8'");
  185.       if(!$db_selected && $this->_options['create_database']==0)
  186.       {
  187.         $this->view->error($this->lang['L-05']);
  188.       }
  189.       elseif (!$db_selected && $this->_options['create_database']==1)
  190.       {
  191.         # Create the DB
  192.         $result mysql_query("CREATE DATABASE {$this->_options['dbname']}"$link);
  193.         if (!$result)
  194.         {
  195.           $this->view->error(sprintf($this->lang['L-06']$this->_options['dbname']htmlspecialchars(mysql_error())));
  196.         else {
  197.           $db_selected mysql_select_db($this->_options['dbname']$link);
  198.           mysql_query("SET NAMES 'utf8'");
  199.         }
  200.       }
  201.     elseif($this->_options['dbtype']=='pgsql'{
  202.       $linkpg_connect('host=' .$this->_options['dbhost']' port=5432 dbname=' $this->_options['dbname'' user=' $this->_options['dbuser']' password='  $this->_options['dbpass']);
  203.       if(!$link)
  204.       {
  205.         $this->view->error($this->lang['L-04']);
  206.       }
  207.  
  208.     }
  209.  
  210.     # Load the database stuff
  211.     require_once'../languages/' .$this->default_lang'/installer_input.utf-8.php' );
  212.     $path INSTALLER_PATH DIRECTORY_SEPARATOR 'data' DIRECTORY_SEPARATOR 'database.php' ;
  213.     if(!file_exists($path))
  214.     {
  215.       $this->view->error(sprintf($this->lang['L-07']$path));
  216.     }
  217.  
  218.     $SQL array();
  219.     require_once($path);
  220.  
  221.     $count 0;
  222.     $errors_count 0;
  223.  
  224.     # Loop if we have any
  225.     if(count($SQL))
  226.     {
  227.       # Start the count
  228.  
  229.       $errors array();
  230.       foreach ($SQL as $query)
  231.       {
  232.         if($this->_options['dbtype'== 'mysql')  $result mysql_query($query$link);
  233.         else $result pg_query($link$query);
  234.  
  235.         if (!$result)
  236.         {
  237.           if($this->_options['dbtype'== 'mysql')  $errors[sprintf($this->lang['L-08']htmlspecialchars(mysql_error()));
  238.           else $errors[sprintf($this->lang['L-08']htmlspecialchars(pg_last_error()));
  239.           $errors_count++;
  240.           continue;
  241.         }
  242.  
  243.         # Increase it
  244.         $count++;
  245.       }
  246.     }
  247.  
  248.     $error_string '';
  249.  
  250.     # Did we had any errors?
  251.     if(count($errors))
  252.     {
  253.       $error_string "<br /><br />".sprintf($this->lang['I-14']implode("<br /><br />"$errors));
  254.     }
  255.  
  256.     # Redirect
  257.     $this->view->vars array('message' => sprintf($this->lang['L-09']$count$errors_count$error_string)'button' => $error_string $this->lang['I-16'$this->lang['I-03']);
  258.     $this->view->render('dbdone');
  259.  
  260.   }
  261.  
  262.   /**
  263.    * Display everything
  264.    *
  265.    */
  266.   public function display()
  267.   {
  268.     $this->view->display();
  269.   }
  270.  
  271.   /**
  272.    * Write the configuration File
  273.    *
  274.    */
  275.   public function configWrite()
  276.   {
  277.       global $l21config;
  278.  
  279.     // update main ini file
  280.     $ini_r=array();
  281.     
  282.     // database settings
  283.     $ini_r['DB_NAME']=$_SESSION['dbname'];
  284.     $ini_r['DB_USER']=$_SESSION['dbuser'];
  285.     $ini_r['DB_PASS']=$_SESSION['dbpass'];
  286.     $ini_r['DB_HOST']=$_SESSION['dbhost'];
  287.  
  288.     $ini_r['LANGUAGE']=$this->default_lang;
  289.     
  290.     if(substr($this->default_lang02== 'en'$ini_r['DATE_FORMAT']="yyyy-mm-dd";
  291.     else $ini_r['DATE_FORMAT']="dd-mm-yyyy";
  292.     
  293.     $ini_r['SITE_NAME']=$_POST['SITE_NAME'];
  294.     $ini_r['SITE_PATH']=$_POST['SITE_PATH'];
  295.     $ini_r['SITE_ROOT_URL']=$_POST['SITE_ROOT_URL'];
  296.  
  297.     // mail settings
  298.     $ini_r['SITE_MAIL']=$_POST['MAIL_FROM'];
  299.     $ini_r['MAIL_FROM']=$_POST['MAIL_FROM'];
  300.     $ini_r['MAIL_FROMNAME']=$_POST['MAIL_FROMNAME'];
  301.     $ini_r['MAIL_REPLY']=$_POST['MAIL_FROM'];
  302.     $ini_r['MAIL_REPLYNAME']=$_POST['MAIL_FROMNAME'];
  303.     $ini_r['SQL']=$_SESSION['dbtype'];
  304.  
  305.     $l21config->setParams($ini_r);
  306.  
  307.     $l21config->writeReleaseParams();
  308.  
  309.     $this->finishInstaller();
  310.   }
  311.  
  312.   /**
  313.    * Finish the installer proccess
  314.    *
  315.    */
  316.   public function finishInstaller()
  317.   {
  318.     # Lock the installer
  319.     $path INSTALLER_PATH DIRECTORY_SEPARATOR 'data' DIRECTORY_SEPARATOR 'installer.lock';
  320.     file_put_contents($path"installer lock file");
  321.     $_SESSION array();
  322.     $this->view->render('finish');
  323.   }
  324.  
  325.   /**
  326.    * Redirect to the next step
  327.    *
  328.    * @param string $step 
  329.    */
  330.   public function nextStep($step)
  331.   {
  332.     $url BASE_URL '?step='.$step;
  333.     if(!headers_sent())
  334.     {
  335.       header('Location: '$url);
  336.       exit;
  337.     }
  338.  
  339.     print "<html><body><meta http-equiv='refresh' content='1;url={$url}'></body></html>";
  340.     exit;
  341.   }
  342.  
  343.   /**
  344.    * Redirect screen with a message
  345.    *
  346.    * @param string $message 
  347.    */
  348.   public function redirectScreen($message$step)
  349.   {
  350.     $this->view->redirect($message$step);
  351.   }
  352.  
  353.  
  354.   /**
  355.    * Destructor
  356.    *
  357.    */
  358.   public function __destruct()
  359.   {
  360.  
  361.  
  362.     @mysql_close();
  363.     @pg_close();
  364.     # Clear
  365.     unset($this);
  366.   }
  367.  
  368. }

Documentation generated on Thu, 20 Mar 2014 16:48:09 +0100 by phpDocumentor 1.4.1