Main Page | Namespace List | Class Hierarchy | Data Structures | Directories | File List | Data Fields | Globals | Related Pages

setup.php

Go to the documentation of this file.
00001 
00002 <style type="text/css">
00003 <!--
00004 body {
00005         font-family:sans-serif;
00006         font-size:12px;
00007         color:blue;
00008  }
00009 h2.info {
00010         color:green;
00011         font-size:20px;
00012         font-family:sans-serif;
00013 }
00014 h2.error {
00015         color:red;
00016         font-size:20px;
00017         font-family:sans-serif;
00018 }
00019 .warning  {
00020         font-family:sans-serif;
00021         font-size:12px;
00022         color:red;
00023  }
00024 .info {
00025         color:green;
00026         font-size:12px;
00027         font-family:sans-serif;
00028 }
00029 
00030 -->
00031 </style>
00032 <p align="center">
00033   <IMG SRC="../image/logo7.jpg" alt="Logo">
00034 </p>
00035 <?
00036 /*
00037  *   This file is part of PhpCompta.
00038  *
00039  *   PhpCompta is free software; you can redistribute it and/or modify
00040  *   it under the terms of the GNU General Public License as published by
00041  *   the Free Software Foundation; either version 2 of the License, or
00042  *   (at your option) any later version.
00043  *
00044  *   PhpCompta is distributed in the hope that it will be useful,
00045  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00046  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00047  *   GNU General Public License for more details.
00048  *
00049  *   You should have received a copy of the GNU General Public License
00050  *   along with PhpCompta; if not, write to the Free Software
00051  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00052 */
00053 /* $Revision*/
00054 // Copyright Author Dany De Bontridder ddebontridder@yahoo.fr
00055 
00056 $inc_path=get_include_path();
00057 if ( strpos($inc_path,";") != 0 ) {
00058   $new_path=$inc_path.';..\..\include;addon';
00059  } else {
00060   $new_path=$inc_path.':../../include:addon';
00061  }
00062 
00063 set_include_path($new_path);
00064 
00065 include_once('constant.php');
00066 include_once('postgres.php');
00067 include_once('debug.php');
00068 include_once('ac_common.php');
00069 /* function GetVersion
00070  **************************************************
00071  * Purpose : Get version of a database
00072  *        
00073  * parm : 
00074  *      - $p_cn database connection
00075  * gen :
00076  *      - none
00077  * return:
00078  *        none
00079  */
00080 function GetVersion($p_cn) {
00081         $Res=ExecSql($p_cn,"select val from version");
00082         $a=pg_fetch_array($Res,0);
00083         return $a['val'];
00084 }
00085 /* function ExecuteScript
00086  **************************************************
00087  * Purpose : Execute a sql script
00088  *        
00089  * parm : 
00090  *      - $p_cn database connection
00091  *      - $script script name
00092  * gen :
00093  *      - none
00094  * return:
00095  *        none
00096  */
00097 function ExecuteScript($p_cn,$script) {
00098   $hf=fopen($script,'r');
00099   $sql="";
00100   $flag_function=false;
00101   while (!feof($hf)) {
00102     $buffer=fgets($hf);
00103     $buffer=str_replace ("$","\$",$buffer);
00104     print $buffer."<br>";
00105     // comment are not execute
00106     if ( substr($buffer,0,2) == "--" ) {
00107       //echo "comment $buffer";
00108       continue;
00109     }
00110     // Blank Lines Are Skipped
00111     If ( Strlen($buffer)==0) {
00112             //echo "Blank $buffer";
00113       Continue;
00114     }
00115     if ( strpos(strtolower($buffer),"create function")===0 ) {
00116             echo "found a function";
00117             $flag_function=true;
00118             $sql=$buffer;
00119             continue;
00120     }
00121  if ( strpos(strtolower($buffer),"create or replace function")===0 ) {
00122             echo "found a function";
00123             $flag_function=true;
00124             $sql=$buffer;
00125             continue;
00126     }
00127     // No semi colon -> multiline command
00128     if ( $flag_function== false && strpos($buffer,';') == false ) {
00129       $sql.=$buffer;
00130       continue;
00131     } 
00132     if ( $flag_function ) {
00133       if ( strpos(strtolower($buffer), "language plpgsql") === false ) {
00134                 $sql.=$buffer;
00135                 continue;
00136             }
00137     } else  {
00138             // cut the semi colon
00139             $buffer=str_replace (';','',$buffer);
00140             }
00141     $sql.=$buffer;
00142     if ( ExecSql($p_cn,$sql) == false ) {
00143             Rollback($p_cn);
00144             if ( DEBUG=='false' ) ob_end_flush();
00145             print "ERROR : $sql";
00146             exit();
00147             }
00148     $sql="";
00149     $flag_function=false;
00150     print "<hr>";
00151   } // while (feof)
00152   fclose($hf);
00153 }
00154 
00155 // Verify some PHP parameters
00156 // magic_quotes_gpc = Off
00157 // magic_quotes_runtime = Off
00158 // magic_quotes_sybase = Off
00159 // include_path
00160 
00161 ?>
00162 <h2>Info</h2>
00163 Vous utilisez le domaine <? echo domaine; ?>
00164 <h2>Php setting</h2>
00165 <?
00166 
00167 $flag_php=0;
00168 foreach (array('magic_quotes_gpc','magic_quotes_runtime') as $a) {
00169 
00170   if ( ini_get($a) == false ) print $a.': Ok  <br>';
00171   else {
00172         print ("<h2 class=\"error\">$a has a bad value  !!!</h2>");
00173         $flag_php++;
00174   }
00175 
00176 }
00177 if ( ini_get("max_execution_time") < 60 )  {
00178         print '<h2 class="info"> max_execution_time should be set to 60 minimum</h2>';
00179 }
00180 if ( ini_get("session.auto_start") == false )  {
00181         print '<h2 class="error"> session.auto_start must be set to true </h2>';
00182         $flag_php++;
00183 }
00184 if ( ini_get("session.use_trans_sid") == false )  {
00185         print '<h2 class="error"> avertissement session.use_trans_sid should be set to true </h2>';
00186 }
00187 if ( ereg("..\/include",$inc_path) == 0 and ereg("..\\include",$inc_path) == 0)
00188 {
00189   print ("<h2 class=\"error\">include_path incorrect  !!!".$inc_path."</h2>");
00190         $flag_php++;
00191 }
00192  else
00193  if ( ereg("addon",$inc_path) == 0) {
00194   print ("<h2 class=\"error\">include_path incorrect  !!!".$inc_path."</h2>");
00195         $flag_php++;
00196  }else
00197    print 'include_path : ok ('.$inc_path.')<br>';
00198 
00199 if ( $flag_php==0 ) {
00200         echo '<p class="info">php.ini est bien configuré</p>';
00201 } else {
00202         echo '<p class="error"> php mal configuré</p>';
00203         exit -1;
00204 }
00205 $cn=DbConnect(-2,'phpcompta');
00206 
00207 if ($cn == false ) {
00208   print "<p> Vous devez absolument taper dans une console la commande 'createuser -A -d -P  phpcompta et vous donnez dany comme mot de passe (voir la documentation)'
00209   puis  la commande 'createdb -O phpcompta phpcompta'. </p>
00210 <p>Ces commandes créeront l'utilisateur phpcompta
00211 puis la base de données par défaut de phpcompta.</p>";
00212   exit();
00213  }
00214 ?>
00215 <h2>Database version </h2>
00216 <?
00217  // Verify Psql version
00218  //--
00219 $sql="select setting from pg_settings where name='server_version'";
00220 $Res=ExecSql($cn,$sql);
00221 $row=pg_fetch_array($Res,0);
00222 $version=$row[0];
00223 
00224 if ( $version[0]  != '8' ) {
00225 ?>
00226   <p> Vous devez absolument utiliser au minimum une version 8 de PostGresql, si votre distribution n'en
00227 offre pas, installez en une en la compilant. </p><p>Lisez attentivement la notice sur postgresql.org pour migrer
00228 vos bases de données en 8
00229 </p>
00230 <? exit(); //'
00231 }
00232 
00233 ?>
00234 <h2>Database Setting</h2> 
00235 <?
00236 // Language plsql is installed 
00237 //--
00238 $sql="select lanname from pg_language where lanname='plpgsql'";
00239 $Res=CountSql($cn,$sql);
00240 if ( $Res==0) { ?>
00241 <p> Vous devez installer le langage plpgsql pour permettre aux fonctions SQL de fonctionner.</p>
00242 <p>Pour cela, sur la ligne de commande, faites 
00243 createlang plpgsql pour chaque base de données que vous possédez (y compris template0 et template1).
00244 </p>
00245 <p>Pour afficher toutes les bases de données, tapez sur la ligne de commande "psql -l"</p>
00246 <? exit(); }
00247 
00248 // Memory setting
00249 //--
00250 $sql="select name,setting 
00251       from pg_settings 
00252       where 
00253       name in ('effective_cache_size','shared_buffers','work_mem')";
00254 $Res=ExecSql($cn,$sql);
00255 $flag=0;
00256 for ($e=0;$e<pg_NumRows($Res);$e++) {
00257   $a=pg_fetch_array($Res,$e);
00258   switch ($a['name']){
00259   case 'effective_cache_size':
00260     if ( $a['setting'] < 1000 ){
00261       print '<p class="warning">Attention le paramètre effective_cache_size est de '.
00262         $a['setting']." au lieu de 1000 </p>";
00263       $flag++;
00264     }
00265     break;
00266   case 'shared_buffers':
00267     if ( $a['setting'] < 640 ){
00268       print '<p class="warning">Attention le paramètre shared_buffer est de '.
00269         $a['setting']."au lieu de 640</p>";
00270       $flag++;
00271     }
00272     break;
00273   case 'work_mem':
00274     if ( $a['setting'] < 8192 ){
00275       print '<p class="warning">Attention le paramètre work_mem est de '.
00276         $a['setting']." au lieu de 8192 </p>";
00277     $flag++;
00278     }
00279     break;
00280 
00281   }
00282  }
00283 if ( $flag == 0 ) {
00284   echo '<p class="info">La base de données est bien configurée</p>';
00285  } else {
00286   echo '<p class="warning">Il y a '.$flag.' paramètre qui sont trop bas</p>';
00287  }
00288 if ( ! isset($_POST['go']) ) {
00289 ?>
00290 <FORM action="setup.php" METHOD="post">
00291 <input type="submit" name="go" value="Prêt à commencer la mise à jour ou l'installation?">
00292 </form>
00293 <?
00294 }
00295 if ( ! isset($_POST['go']) )
00296         exit();
00297 // Check if account_repository exists
00298 $account=CountSql($cn,
00299                   "select * from pg_database where datname='".domaine."account_repository'");
00300 
00301 // Create the account_repository
00302 if ($account == 0 ) {
00303 
00304   echo "Creation of ".domaine."account_repository";
00305   if ( DEBUG=='false') ob_start();  
00306   ExecSql($cn,"create database ".domaine."account_repository encoding='latin1'");
00307   $cn=DbConnect();
00308   ExecuteScript($cn,"sql/account_repository/schema.sql");
00309   ExecuteScript($cn,"sql/account_repository/data.sql");
00310  if ( DEBUG=='false') ob_end_clean();
00311   echo "Creation of Démo";
00312   if ( DEBUG=='false') ob_start();  
00313   ExecSql($cn,"create database ".domaine."dossier1 encoding='latin1'");
00314   $cn=DbConnect(1,'dossier');
00315   ExecuteScript($cn,'sql/dossier1/schema.sql');
00316   ExecuteScript($cn,'sql/dossier1/data.sql');
00317  if ( DEBUG=='false') ob_end_clean();
00318 
00319   echo "Creation of Modele1";
00320   if ( DEBUG=='false') ob_start();  
00321   ExecSql($cn,"create database ".domaine."mod1 encoding='latin1'");
00322   $cn=DbConnect(1,'mod');
00323   ExecuteScript($cn,'sql/mod1/schema.sql');
00324   ExecuteScript($cn,'sql/mod1/data.sql');
00325  if ( DEBUG=='false') ob_end_clean();
00326  }// end if
00327  
00328 // Add a french accountancy model
00329 //--
00330 $cn=DbConnect();
00331 $Res=CountSql($cn,"select * from modeledef where mod_id=2");
00332 if ( $Res == 0) {
00333   echo "Creation of Modele2";
00334   ExecSql($cn,"create database ".domaine."mod2 encoding='latin1'");
00335   $cn=DbConnect(2,'mod');
00336   if ( DEBUG=='false') { ob_start();  }
00337   ExecuteScript($cn,'sql/mod2/schema.sql');
00338   ExecuteScript($cn,'sql/mod2/data.sql');
00339   $sql="INSERT INTO modeledef VALUES (2, '(FR) Basique', 'Comptabilité Française, tout doit être adaptée');";
00340   $cn=DbConnect();
00341   ExecSql($cn,$sql);
00342  if ( DEBUG=='false') ob_end_clean();
00343 }
00344 // 
00345 // Test the connection
00346 //--
00347 $a=DbConnect();
00348 if ( $a==false) {
00349    exit ("<h2 class=\"error\">".__LINE__." test has failed !!!</h2>");
00350 
00351 }
00352 if ( ($Res=ExecSql($a,"select  * from ac_users") ) == false ) {
00353         exit ("<h2 class=\"error\">".__LINE__." test has failed !!!</h2>");
00354 } else 
00355         print "Connect to database success <br>";
00356 echo "<h2 class=\"info\"> Congratulation : Test successfull</h2>";
00357 
00358 echo "<h2 class=\"info\"> Patching databases</h2>";
00359 
00360 $cn=DbConnect();
00361 $Resdossier=ExecSql($cn,"select dos_id, dos_name from ac_dossier");
00362 $MaxDossier=pg_NumRows($Resdossier);
00363 
00364 for ($e=0;$e < $MaxDossier;$e++) {
00365   $db_row=pg_fetch_array($Resdossier,$e);
00366   $db=DbConnect($db_row['dos_id'],'dossier');
00367   echo "Patching ".$db_row['dos_name']." from the version ".GetVersion($db)."<hr>";
00368 if ( DEBUG=='false' ) ob_start();
00369   if ( GetVersion($db) <= 4 ) { 
00370     ExecuteScript($db,'sql/patch/upgrade4.sql');
00371       
00372     $sql="select jrn_def_id from jrn_def ";
00373     $Res=ExecSql($db,$sql);
00374     $Max=pg_NumRows($Res);
00375     for ($seq=0;$seq<$Max;$seq++) {
00376             $row=pg_fetch_array($Res,$seq);
00377             $sql=sprintf ("create sequence s_jrn_%d",$row['jrn_def_id']);
00378             ExecSql($db,$sql);
00379     }
00380   } // version == 4
00381   //--
00382   // update to the version 5
00383   //--
00384   if ( GetVersion($db) == 5 ) { 
00385     ExecuteScript($db,'sql/patch/upgrade5.sql');
00386   } // version == 5
00387 
00388 
00389   //--
00390   // update to the version 7
00391   //--
00392   if ( GetVersion($db) == 6 ) { 
00393     ExecuteScript($db,'sql/patch/upgrade6.sql');
00394   } // version == 6
00395 
00396   //--
00397   // update to the version 8
00398   //--
00399   if ( GetVersion($db) == 7 ) { 
00400     ExecuteScript($db,'sql/patch/upgrade7.sql');
00401     // now we use sequence instead of computing a max
00402     // 
00403     $Res2=ExecSql($db,'select coalesce(max(jr_grpt_id),1) as l from jrn');
00404     $Max2= pg_NumRows($Res2) ;
00405     if ( $Max2 == 1) {
00406       $Row=pg_fetch_array($Res2,0);
00407       var_dump($Row);
00408       $M=$Row['l'];
00409       ExecSql($db,"select setval('s_grpt',$M,true)");
00410     }
00411   } // version == 7
00412   // version 8 -> 9
00413   if ( GetVersion($db) == 8 ) { 
00414     ExecuteScript($db,'sql/patch/upgrade8.sql');
00415   } // version == 9->10
00416   if ( GetVersion($db) == 9 ) { 
00417     ExecuteScript($db,'sql/patch/upgrade9.sql');
00418   } // version == 10->11
00419   if ( GetVersion($db) == 10 ) { 
00420     ExecuteScript($db,'sql/patch/upgrade10.sql');
00421   } // version 
00422   if ( GetVersion($db) == 11 ) { 
00423     ExecuteScript($db,'sql/patch/upgrade11.sql');
00424   } // version 
00425 
00426 if ( DEBUG == 'false') ob_end_clean();
00427  }//for
00428 
00429 $Resdossier=ExecSql($cn,"select mod_id, mod_name from modeledef");
00430 $MaxDossier=pg_NumRows($Resdossier);
00431 echo "Upgrading Dossier";
00432 for ($e=0;$e < $MaxDossier;$e++) {
00433   $db_row=pg_fetch_array($Resdossier,$e);
00434   echo "Patching ".$db_row['mod_name']."<hr>";
00435   $db=DbConnect($db_row['mod_id'],'mod');
00436 if (DEBUG == 'false' ) ob_start();
00437   if ( GetVersion($db) <= 4 ) { 
00438     ExecuteScript($db,'sql/patch/upgrade4.sql');
00439       
00440     $sql="select jrn_def_id from jrn_def ";
00441     $Res=ExecSql($db,$sql);
00442     $Max=pg_NumRows($Res);
00443     for ($seq=0;$seq<$Max;$seq++) {
00444             $row=pg_fetch_array($Res,$seq);
00445             $sql=sprintf ("create sequence s_jrn_%d",$row['jrn_def_id']);
00446             ExecSql($db,$sql);
00447     }
00448  } // version == 4
00449   if ( GetVersion($db) == 5 ) { 
00450     ExecuteScript($db,'sql/patch/upgrade5.sql');
00451   } // version == 5
00452 
00453 
00454   //--
00455   // update to the version 7
00456   //--
00457   if ( GetVersion($db) == 6 ) { 
00458     ExecuteScript($db,'sql/patch/upgrade6.sql');
00459   } // version == 6
00460 
00461   //--
00462   // update to the version 8
00463   //--
00464   if ( GetVersion($db) == 7 ) { 
00465     ExecuteScript($db,'sql/patch/upgrade7.sql');
00466     // now we use sequence instead of computing a max
00467     // 
00468     $Res2=ExecSql($db,'select coalesce(max(jr_grpt_id),1) as l from jrn');
00469     $Max2= pg_NumRows($Res2) ;
00470     if ( $Max2 == 1) {
00471       $Row=pg_fetch_array($Res2,0);
00472       $M=$Row['l'];
00473       ExecSql($db,"select setval('s_grpt',$M,true)");
00474     }
00475   } // version == 8
00476   //--
00477   // update to the version 9
00478   //--
00479   if ( GetVersion($db) == 8 ) { 
00480     ExecuteScript($db,'sql/patch/upgrade8.sql');
00481   } // version == 9
00482   // update to the version 10
00483   //--
00484   if ( GetVersion($db) == 9 ) { 
00485     ExecuteScript($db,'sql/patch/upgrade9.sql');
00486   } // version == 9
00487   if ( GetVersion($db) == 10 ) { 
00488     ExecuteScript($db,'sql/patch/upgrade10.sql');
00489   } // version 
00490   if ( GetVersion($db) == 11 ) { 
00491     ExecuteScript($db,'sql/patch/upgrade11.sql');
00492   } // version 
00493 
00494 if ( DEBUG == 'false') ob_end_clean();
00495  }
00496 
00497 echo "Upgrading Repository";
00498 $cn=DbConnect();
00499 if ( DEBUG == 'false') ob_start();
00500 if ( GetVersion($cn) <= 4 ) {
00501   ExecuteScript($cn,'sql/patch/ac-upgrade4.sql');
00502  }
00503 if ( GetVersion($cn) == 5 ) {
00504   ExecuteScript($cn,'sql/patch/ac-upgrade5.sql');
00505  }
00506 if ( GetVersion($cn) == 6 ) {
00507   ExecuteScript($cn,'sql/patch/ac-upgrade6.sql');
00508  }
00509 if (DEBUG=='false') ob_end_clean();
00510 echo "<h2 class=\"info\">Voilà tout est installé ;-)</h2>";

Generated on Fri Jun 23 00:12:45 2006 for phpcompta by  doxygen 1.4.4