phpDocumentor lodeladmin
[ class tree: lodeladmin ] [ index: lodeladmin ] [ all elements ]

Source for file class.Install.php

Documentation is available at class.Install.php

  1. <?php
  2. /**
  3.  * Fichier racine de lodeladmin
  4.  *
  5.  * PHP versions 4 et 5
  6.  *
  7.  * LODEL - Logiciel d'Edition ELectronique.
  8.  *
  9.  * Copyright (c) 2001-2002, Ghislain Picard, Marin Dacos
  10.  * Copyright (c) 2003, Ghislain Picard, Marin Dacos, Luc Santeramo, Nicolas Nutten, Anne Gentil-Beccot
  11.  * Copyright (c) 2004, Ghislain Picard, Marin Dacos, Luc Santeramo, Anne Gentil-Beccot, Bruno Cénou
  12.  * Copyright (c) 2005, Ghislain Picard, Marin Dacos, Luc Santeramo, Gautier Poupeau, Jean Lamy, Bruno Cénou
  13.  * Copyright (c) 2006, Marin Dacos, Luc Santeramo, Bruno Cénou, Jean Lamy, Mikaël Cixous, Sophie Malafosse
  14.  * Copyright (c) 2007, Marin Dacos, Bruno Cénou, Sophie Malafosse, Pierre-Alain Mignot
  15.  *
  16.  * Home page: http://www.lodel.org
  17.  *
  18.  * E-Mail: lodel@lodel.org
  19.  *
  20.  * All Rights Reserved
  21.  *
  22.  * This program is free software; you can redistribute it and/or modify
  23.  * it under the terms of the GNU General Public License as published by
  24.  * the Free Software Foundation; either version 2 of the License, or
  25.  * (at your option) any later version.
  26.  *
  27.  * This program is distributed in the hope that it will be useful,
  28.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  29.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  30.  * GNU General Public License for more details.
  31.  *
  32.  * You should have received a copy of the GNU General Public License
  33.  * along with this program; if not, write to the Free Software
  34.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  35.  *
  36.  * @author Ghislain Picard
  37.  * @author Jean Lamy
  38.  * @author Sophie Malafosse
  39.  * @author Pierre-Alain MIGNOT
  40.  * @copyright 2005, Ghislain Picard, Marin Dacos, Luc Santeramo, Gautier Poupeau, Jean Lamy, Bruno Cénou
  41.  * @copyright 2006, Marin Dacos, Luc Santeramo, Bruno Cénou, Jean Lamy, Mikaël Cixous, Sophie Malafosse
  42.  * @copyright 2007, Marin Dacos, Bruno Cénou, Sophie Malafosse, Pierre-Alain Mignot
  43.  * @licence http://www.gnu.org/copyleft/gpl.html
  44.  * @version CVS:$Id:
  45.  * @package lodeladmin
  46.  */
  47.  
  48. class Install {
  49.  
  50.     /**
  51.      * Suffixe de la version de Lodel
  52.      * @var string 
  53.      */
  54.     var $versionsuffix;
  55.  
  56.     /**
  57.      * Version à installer
  58.      * @var string 
  59.      */    
  60.     var $versioninstall;
  61.  
  62.     /**
  63.      * Fichier de configuration Lodel
  64.      * @var string 
  65.      */
  66.     var $lodelconfig;
  67.  
  68.     /**
  69.      * Plateforme Lodel
  70.      * @var string 
  71.      */
  72.     var $plateform;
  73.  
  74.     /**
  75.      * Répertoire de la plateforme
  76.      * @var string 
  77.      */
  78.     var $plateformdir;
  79.  
  80.     /**
  81.      * Plateforme lodelconfig
  82.      * @var string 
  83.      */
  84.     var $lodelconfigplatform;
  85.  
  86.     /**
  87.      * Tableau des répertoires à protéger avec un htaccess
  88.      * @var string 
  89.      */
  90.     var $protecteddir;
  91.  
  92.     /**
  93.      * Choix de la langue
  94.      * @var string 
  95.      */
  96.     var $langChoice;
  97.  
  98.     /**
  99.      * Chmod ?
  100.      * @var string 
  101.      */
  102.     var $have_chmod;
  103.  
  104.  
  105.     /**
  106.      * Constructeur
  107.      *
  108.      * Instancie un objet de la classe
  109.      *
  110.      * @param string $lodelconfig chemin vers fichier lodelconfig temporaire
  111.      * @param array $context le contexte passé par référence
  112.      */
  113.     function Install($lodelconfig$have_chmod$plateformdir)
  114.     {
  115.         $this->lodelconfig = $lodelconfig;
  116.         $this->home $home;
  117.         $this->have_chmod = $have_chmod;
  118.         $this->plateformdir = $plateformdir;
  119.     }
  120.  
  121.     /**
  122.      * Accesseur
  123.      *
  124.      * Cette fonction renvoit la variable $var passée en paramètre
  125.      *
  126.      * @param var $var variable à renvoyer
  127.      */
  128.     function get($var)
  129.     {
  130.         return $this->$var;
  131.     }
  132.  
  133.     /**
  134.      * Accesseur
  135.      *
  136.      * Cette fonction alloue la valeur $valeur à la variable $var
  137.      *
  138.      * @param var $var variable à modifier
  139.      * @param var $valeur valeur à allouer
  140.      */
  141.     function set($var$valeur)
  142.     {
  143.         $this->$var $valeur;
  144.     }
  145.  
  146.  
  147.     /**
  148.      * Test connexion à la base de données
  149.      *
  150.      * Cette fonction tente de se connecter à la base de données, met le charset par defaut et identifie l'utilisateur
  151.      * comme administrateur si jamais il existe un enregistrement dans la table username
  152.      */
  153.     function testInstallDB()
  154.     {
  155.         require_once "../lodel".$this->versionsuffix."/scripts/auth.php";
  156.         @include($this->lodelconfig);
  157.         if (@mysql_connect($dbhost,$dbusername,$dbpasswd)) {
  158.             @mysql_select_db($database);
  159.             $this->set_mysql_charset();
  160.         
  161.             // test whether we access to a DB and whether the table users exists or not and whether it is empty or not.
  162.         
  163.             $result=mysql_query("SELECT username FROM `".$tableprefix."users` LIMIT 0,1");
  164.             if ($result && mysql_num_rows($result)>0)
  165.                 authenticate(LEVEL_ADMINLODEL);
  166.         else {
  167.             // well, no access to the DB but a lodelconfig ?
  168.             // ask for erasing the lodelconfig.php ?
  169.             $this->problem("lodelconfig_but_no_database");
  170.         }
  171.  
  172.         if ($_REQUEST['installoption']$installoption=$_REQUEST['installoption']// overwrite the lodelconfig
  173.     }
  174.  
  175.     /**
  176.      * Installation de la configuration
  177.      *
  178.      * Cette fonction copie le fichier de configuration dans le CACHE, chmod et MAJ de celui-ci
  179.      *
  180.      * @param int $installoptions option d'installation (1 = monosite, 2 = multisite)
  181.      * @param string $testfile fichier sur lequel deviner filemask
  182.      */
  183.     function installConf($installoptions$testfile)
  184.     {
  185.         $this->plateform=preg_replace("/[^A-Za-z_-]/","",$this->plateform);
  186.         if (!$this->plateform$this->plateform="default";
  187.         
  188.         $this->lodelconfigplatform=$this->plateformdir."/lodelconfig-".$this->plateform.".php";
  189.         if (file_exists($this->lodelconfigplatform)) {
  190.             // essai de copier ce fichier dans le CACHE
  191.             if (!@copy($this->lodelconfigplatform,$this->lodelconfig)) die ("probl&egrave;me de droits... &eacute;trange on a d&eacute;j&agrave; v&eacute;rifi&eacute;")}
  192.             if (file_exists(LODELROOT."lodelloader.php")) {
  193.             // the installer has been use, let's chmod safely
  194.             $chmod=decoct(fileperms(LODELROOT."lodel-".$this->versioninstall));
  195.             else {
  196.             $chmod=0600;  // c'est plus sur, surtout a cause du mot de passe sur la DB qui apparaitra dans ce fichier.
  197.             }
  198.             @chmod($this->lodelconfig,$chmod);
  199.             $this->maj_lodelconfig(array("home"=>'$pathroot/lodel'.$this->versionsuffix.'/scripts/'));
  200.         else {
  201.             die("ERROR: ".$this->lodelconfigplatform." does not exist. Internal error, please report this bug.");
  202.         }
  203.         $arr=array();
  204.         $needoptions=false;
  205.         $arr['installoption']=intval($installoptions);
  206.         
  207.         // guess the urlroot
  208.         $me=$_SERVER['PHP_SELF'];
  209.         if ($me{
  210.             // enleve moi
  211.             $urlroot=preg_replace("/\/+lodeladmin".$this->versionsuffix."\/install.php$/","",$me);
  212.             if ($urlroot==$medie("ERROR: the install.php script is not at the right place, please report this bug.");
  213.             if (LODELROOT!="../"die("ERROR: the lodeladmin directory has been moved, please report this bug.");
  214.             
  215.             $arr['urlroot']=$urlroot."/";
  216.         }
  217.         
  218.         // is there a filemask ?
  219.         
  220.         if ($_REQUEST['filemask']{
  221.             // passed via the URL
  222.             $arr['filemask']="0".$_REQUEST['filemask'];
  223.         elseif ($filemask{
  224.             // was in the previous lodelconfig.php
  225.             $arr['filemask']=$GLOBALS['filemask'];
  226.         else {
  227.             $arr['filemask']="0".decoct($this->guessfilemask($testfile));
  228.         }
  229.         
  230.         if (preg_match("/^\w{2}(-\w{2})?$/",$_REQUEST['lang'])) {
  231.             // passed via the URL
  232.             $arr['installlang']=$this->installlang=$_REQUEST['lang'];
  233.         }
  234.         
  235.         if ($installoptions==1{
  236.             // try to guess the options.
  237.             // use pclzip ?
  238.             if (function_exists("gzopen")) {
  239.                 $arr['unzipcmd']=$arr['zipcmd']="pclzip";
  240.             else {
  241.                 $arr['unzipcmd']=$arr['zipcmd']="";
  242.                 $needoptions=true;
  243.             }
  244.         }
  245.         if ($installoptions==1$arr['extensionscripts']="php";
  246.         
  247.         $arr['chooseoptions']=$needoptions && $installoptions=="oui" "non";
  248.         return $this->maj_lodelconfig($arr);
  249.     }
  250.  
  251.     /**
  252.      * Mise à jour configuration base de données
  253.      *
  254.      * Cette fonction met à jour les informations de connexion au serveur de base de données dans le fichier de configuration
  255.      *
  256.      * @param string $newdbusername identifiant
  257.      * @param string $newdbpasswd mot de passe
  258.      * @param string $newdbhost adresse du serveur
  259.      */
  260.     function majConfDB($newdbusername$newdbpasswd$newdbhost)
  261.     {
  262.           $this->maj_lodelconfig(array("dbusername"=>$newdbusername"dbpasswd"=>$newdbpasswd"dbhost"=>$newdbhost));
  263.     }
  264.  
  265.     /**
  266.      * Installation base de données
  267.      *
  268.      * Cette fonction s'occupe de l'installation de la base de données
  269.      *
  270.      * @param string $erasetables on efface les tables d'une base de données existante ?
  271.      * @param string $singledatabase est-on en mode base de données unique ?
  272.      * @param string $newdatabase nom de la base de données
  273.      * @param string $newsingledatabase 
  274.      * @param string $newtableprefix prefixe des tables pour installation multi site
  275.      * @param string $createdatabase on crée la base de données ou pas ?
  276.      * @param string $existingdatabase on utilise une base existante ?
  277.      */
  278.     function manageDB($erasetables$singledatabase$newdatabase$newsingledatabase$newtableprefix$createdatabase$existingdatabase)
  279.     {
  280.         if($erasetables)
  281.         {
  282.             @include($this->lodelconfig);    // insert the lodelconfig. Should not be a problem.
  283.             @mysql_connect($dbhost,$dbusername,$dbpasswd)// connect
  284.             
  285.             /*$version_mysql_num = explode(".", substr(mysql_get_server_info(), 0, 3));
  286.             if ($version_mysql_num[0].$version_mysql_num[1] > 40)
  287.                     { mysql_query('SET NAMES UTF8'); }*/
  288.             @mysql_select_db($database)// selectionne la database
  289.  
  290.             $this->set_mysql_charset();
  291.             // erase the table of each site
  292.             
  293.             $result=mysql_query("SELECT name FROM ".$tableprefix."sites"or die (mysql_error());
  294.  
  295.             if ($singledatabase{
  296.                 // currently singledatabase implies single site ! That's shame but...
  297.                 // Let's destroyed everything in the database with the prefix !
  298.                 if (!$tableprefix{
  299.                     // we can't destroy... too dangerous. Should find another solution.
  300.                     die("Sans tableprefix les tables ne peuvent pas etre efface en toute securite. Veuillez effacer vous-même les tables de Lodel. Merci.");
  301.                 else {
  302.                     // get all table names.
  303.                     $result=mysql_list_tables($database);
  304.  
  305.                     while ($row mysql_fetch_row($result)) {
  306.                         if (preg_match("/^$tableprefix/",$row[0])) {
  307.                             // let's drop it
  308.                             mysql_query("DROP TABLE $row[0]");
  309.                         }
  310.                     }
  311.                 }
  312.             else {
  313.                 die(utf8_encode("<p>L'effacement des tables avec plusieurs bases de données n'est pas implementé. Veuillez effacer les bases de données vous même. Merci.</p>"));
  314.             }
  315.             // erase the main tables below.
  316.         else // normal case
  317.             $set=array();
  318.             
  319.             @include($this->lodelconfig);    // insert the lodelconfig. Should not be a problem.
  320.             
  321.             if ($installoption>1{
  322.                 $set['singledatabase']=$newsingledatabase "on" "";
  323.                 $set['tableprefix']=$newtableprefix;
  324.             }
  325.  
  326.             if ($newdatabase==-1$newdatabase=$existingdatabase;
  327.             if ($newdatabase==-2
  328.                 $newdatabase=$createdatabase;
  329.             else {
  330.                 $createdatabase="";
  331.             }
  332.             $set['database']=$newdatabase;
  333.  
  334.             $this->maj_lodelconfig($set);
  335.             if ($createdatabase// il faut creer la database
  336.                 @include($this->lodelconfig)// insere lodelconfig, normalement pas de probleme
  337.                 @mysql_connect($dbhost,$dbusername,$dbpasswd)// connect
  338.                     $version_mysql_num explode("."substr(mysql_get_server_info()03));
  339.                     if ($version_mysql_num[0].$version_mysql_num[140{
  340.                         mysql_query('SET NAMES UTF8');
  341.                         $db_charset 'CHARACTER SET utf8 COLLATE utf8_general_ci';
  342.                     else 
  343.                         $db_charset '';
  344.                     }
  345.                 if (!@mysql_query("CREATE DATABASE $createdatabase $db_charset")) {
  346.                     return false;
  347.                 }
  348.             else {
  349.             // check whether the database contains something. If so, we just ask what to do
  350. //                 @mysql_connect($dbhost,$dbusername,$dbpasswd); // connect
  351.  //                if(@mysql_select_db($newdatabase)){
  352.  //                    return false;
  353.  //                }
  354.             }
  355.         }
  356.         return true;
  357.     }
  358.  
  359.     /**
  360.      * Création administrateur
  361.      *
  362.      * Cette fonction permet de créer le premier utilisateur (administrateur)
  363.      *
  364.      * @param string $adminusername login
  365.      * @param string $adminpasswd mot de passe premier input du formulaire
  366.      * @param string $adminpasswd2 mot de passe deuxieme input du formulaire
  367.      * @param string $lang langue par défaut pour l'utilisateur créé
  368.      * @param string $site site lié à l'utilisation en cours de création
  369.      */
  370.     function manageAdmin($adminusername$adminpasswd$adminpasswd2$lang$site)
  371.     {
  372.         @include($this->lodelconfig)// insere lodelconfig, normalement pas de probleme
  373.  
  374.         if(empty($adminusername|| empty($adminpasswd)) {
  375.             return "error_user";
  376.         }
  377.         
  378.         if (strlen($adminpasswd|| strlen($adminpasswd255 || !preg_match("/^[0-9A-Za-z_;.?!@:,&]+$/"$adminpasswd)) {
  379.             return "error_passwd";    
  380.         }
  381.         
  382.         if ($adminpasswd2 != $adminpasswd{
  383.             return "error_confirmpasswd";
  384.         }
  385.  
  386.         if (!$homedie("ERROR: \$home is not defined");
  387.         @mysql_connect($dbhost,$dbusername,$dbpasswd)// connect
  388.             /*$version_mysql_num = explode(".", substr(mysql_get_server_info(), 0, 3));
  389.             if ($version_mysql_num[0].$version_mysql_num[1] > 40) {
  390.                 mysql_query('SET NAMES UTF8');
  391.             }*/
  392.         @mysql_select_db($database)// selectionne la database
  393.         $this->set_mysql_charset();
  394.  
  395.         $adminusername=addslashes($adminusername);
  396.         $pass=md5($adminpasswd.$adminusername);
  397.         if (!preg_match("/^\w{2}(-\w{2})?/",$lang)) die("ERROR: invalid lang");
  398.         
  399.         if (!@mysql_query("REPLACE INTO ".$tableprefix."users (username,passwd,email,userrights,langVALUES ('$adminusername','$pass','',128,'$lang')")) {
  400.             return "error_create";
  401.         }
  402.  
  403.         unset($pass);
  404.         return true;
  405.     }
  406.  
  407.     /**
  408.      * Installation htaccess
  409.      *
  410.      * Cette fonction installe les .htaccess dans les répertoires contenus dans la variable privée protecteddir
  411.      *
  412.      * @param string $verify 
  413.      * @param string $write Lodel installe lui même les htaccess ?
  414.      * @param string $nohtaccess pas de htaccess ?
  415.      */
  416.     function set_htaccess($verify$write$nohtaccess)
  417.     {    
  418.         $currentLodelDir "lodel".$this->versionsuffix;
  419.         
  420.         if ($verify || $write$this->maj_lodelconfig("htaccess","on");
  421.         if ($nohtaccess$this->maj_lodelconfig("htaccess","off");
  422.         if ($write{
  423.             foreach ($this->protecteddir as $dir{
  424.                 if (file_exists(LODELROOT.$dir&& !file_exists(LODELROOT.$dir."/.htaccess")) {
  425.                     $file=@fopen(LODELROOT.$dir."/.htaccess","w");
  426.                     if (!$file{
  427.                         $erreur_htaccesswrite=1;
  428.                     else {
  429.                         $erreur_htaccesswrite=0;
  430.                         fputs($file,"deny from all\n");
  431.                         fclose($file);
  432.                     }
  433.                 }
  434.             }
  435.         }
  436.         return $erreur_htaccesswrite;
  437.     }
  438.  
  439.     /**
  440.      * Mise à jour des informations du site
  441.      *
  442.      * Cette fonction met à jour le fichier de configuration du site en cours d'installation
  443.      *
  444.      * @param string $newurlroot url vers répertoire racine de lodel
  445.      * @param string $permission permissions
  446.      * @param string $pclzip utilisation de pclzip ?
  447.      * @param string $newimportdir chemin vers répertoire 'import'
  448.      * @param string $newextensionscripts extension du script à afficher (.php ou .html ?)
  449.      * @param string $newusesymlink utilisation des liens symboliques ?
  450.      * @param string $newcontactbug adresse mail à contacter en cas de bug
  451.      * @param string $newunzipcmd commande unzip spécifiée par l'utilisateur
  452.      * @param string $newzipcmd commande zip spécifiée par l'utilisateur
  453.      * @param string $newuri type d'url affichée
  454.      */
  455.     function maj_options($newurlroot$permission$pclzip$newimportdir$newextensionscripts$newusesymlink$newcontactbug$newunzipcmd$newzipcmd$newuri)
  456.     {
  457.         $newurlroot $newurlroot."/"// ensure their is a / at the end
  458.         $newurlroot preg_replace("/\/\/+/","/",$newurlroot)// ensure there is no double slashes because it causes problem with the cookies
  459.         $filemask "07" (5*($permission[group][read]!="")+2*($permission[group][write]!="")) (5*($permission[all][read]!="")+2*($permission[all][write]!=""));
  460.         
  461.         if ($pclzip=="pclzip"$newunzipcmd=$newzipcmd="pclzip"}
  462.         
  463.         $this->maj_lodelconfig(array("chooseoptions"=>"oui",
  464.                     "urlroot"=>$newurlroot,
  465.                     "importdir"=>$newimportdir,
  466.                     "extensionscripts"=>$newextensionscripts,
  467.                     "usesymlink"=>$newusesymlink,
  468.                     "filemask"=>$filemask,
  469.                     "contactbug"=>$newcontactbug,
  470.                     "unzipcmd"=>$newunzipcmd,
  471.                     "zipcmd"=>$newzipcmd,
  472.                     "URI"=>$newuri
  473.                     ));
  474.     }
  475.  
  476.     /**
  477.      * Téléchargement de lodelconfig.php
  478.      *
  479.      * Cette fonction lance le téléchargement du fichier de configuration du site
  480.      *
  481.      * @param string $logversion version navigateur
  482.      */
  483.     function downloadlodelconfig($log_version)
  484.     {
  485.         header("Content-type: application/force-download");
  486.         header('Expires: ' gmdate('D, d M Y H:i:s'' GMT');
  487.