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
00063 $inc_path=get_include_path();
00064 if ( strpos($inc_path,";") != 0 ) {
00065   $new_path=$inc_path.';..\..\include;addon';
00066  } else {
00067   $new_path=$inc_path.':../../include:addon';
00068  }
00069 
00070 set_include_path($new_path);
00071 
00072 include_once('constant.php');
00073 include_once('postgres.php');
00074 include_once('debug.php');
00075 include_once('ac_common.php');
00076 /* function GetVersion
00077  **************************************************
00078  * Purpose : Get version of a database
00079  *        
00080  * parm : 
00081  *      - $p_cn database connection
00082  * gen :
00083  *      - none
00084  * return:
00085  *        none
00086  */
00087 function GetVersion($p_cn) {
00088         $Res=ExecSql($p_cn,"select val from version");
00089         $a=pg_fetch_array($Res,0);
00090         return $a['val'];
00091 }
00092 /* function ExecuteScript
00093  **************************************************
00094  * Purpose : Execute a sql script
00095  *        
00096  * parm : 
00097  *      - $p_cn database connection
00098  *      - $script script name
00099  * gen :
00100  *      - none
00101  * return:
00102  *        none
00103  */
00104 function ExecuteScript($p_cn,$script) {
00105   $hf=fopen($script,'r');
00106   if ( $hf == false ) {
00107           echo 'Ne peut ouvrir '.$script;
00108           exit();
00109           }
00110   $sql="";
00111   $flag_function=false;
00112   while (!feof($hf)) {
00113     $buffer=fgets($hf);
00114     $buffer=str_replace ("$","\$",$buffer);
00115     print $buffer."<br>";
00116     // comment are not execute
00117     if ( substr($buffer,0,2) == "--" ) {
00118       //echo "comment $buffer";
00119       continue;
00120     }
00121     // Blank Lines Are Skipped
00122     If ( Strlen($buffer)==0) {
00123             //echo "Blank $buffer";
00124       Continue;
00125     }
00126     if ( strpos(strtolower($buffer),"create function")===0 ) {
00127             echo "found a function";
00128             $flag_function=true;
00129             $sql=$buffer;
00130             continue;
00131     }
00132  if ( strpos(strtolower($buffer),"create or replace function")===0 ) {
00133             echo "found a function";
00134             $flag_function=true;
00135             $sql=$buffer;
00136             continue;
00137     }
00138     // No semi colon -> multiline command
00139     if ( $flag_function== false && strpos($buffer,';') == false ) {
00140       $sql.=$buffer;
00141       continue;
00142     } 
00143     if ( $flag_function ) {
00144       if ( strpos(strtolower($buffer), "language plpgsql") === false && 
00145            strpos(strtolower($buffer), "language 'plpgsql'") === false ) {
00146                 $sql.=$buffer;
00147                 continue;
00148             }
00149     } else  {
00150             // cut the semi colon
00151             $buffer=str_replace (';','',$buffer);
00152             }
00153     $sql.=$buffer;
00154     if ( ExecSql($p_cn,$sql) == false ) {
00155             Rollback($p_cn);
00156             if ( DEBUG=='false' ) ob_end_flush();
00157             print "ERROR : $sql";
00158             exit();
00159             }
00160     $sql="";
00161     $flag_function=false;
00162     print "<hr>";
00163   } // while (feof)
00164   fclose($hf);
00165 }
00166 
00167 // Verify some PHP parameters
00168 // magic_quotes_gpc = Off
00169 // magic_quotes_runtime = Off
00170 // magic_quotes_sybase = Off
00171 // include_path
00172 
00173 ?>
00174 <h2>Info</h2>
00175 Vous utilisez le domaine <? echo domaine; ?>
00176 <h2>Php setting</h2>
00177 <?
00178 
00179 $flag_php=0;
00180 foreach (array('magic_quotes_gpc','magic_quotes_runtime') as $a) {
00181 
00182   if ( ini_get($a) == false ) print $a.': Ok  <br>';
00183   else {
00184         print ("<h2 class=\"error\">$a has a bad value  !!!</h2>");
00185         $flag_php++;
00186   }
00187 
00188 }
00189 if ( ini_get("max_execution_time") < 60 )  {
00190         print '<h2 class="info"> max_execution_time should be set to 60 minimum</h2>';
00191 }
00192 if ( ini_get("session.auto_start") == false )  {
00193         print '<h2 class="error"> session.auto_start must be set to true </h2>';
00194         $flag_php++;
00195 }
00196 if ( ini_get("session.use_trans_sid") == false )  {
00197         print '<h2 class="error"> avertissement session.use_trans_sid should be set to true </h2>';
00198 }
00199 if ( ereg("..\/include",$inc_path) == 0 and ereg("..\\include",$inc_path) == 0)
00200 {
00201   print ("<h2 class=\"error\">include_path incorrect  !!!".$inc_path."</h2>");
00202         $flag_php++;
00203 }
00204  else
00205  if ( ereg("addon",$inc_path) == 0) {
00206   print ("<h2 class=\"error\">include_path incorrect  !!!".$inc_path."</h2>");
00207         $flag_php++;
00208  }else
00209    print 'include_path : ok ('.$inc_path.')<br>';
00210 
00211 if ( $flag_php==0 ) {
00212         echo '<p class="info">php.ini est bien configuré</p>';
00213 } else {
00214         echo '<p class="error"> php mal configuré</p>';
00215         exit -1;
00216 }
00217 $cn=DbConnect(-2,'phpcompta');
00218 
00219 if ($cn == false ) {
00220   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)'
00221   puis  la commande 'createdb -O phpcompta phpcompta'. </p>
00222 <p>Ces commandes créeront l'utilisateur phpcompta
00223 puis la base de données par défaut de phpcompta.</p>";
00224   exit();
00225  }
00226 ?>
00227 <h2>Database version </h2>
00228 <?
00229  // Verify Psql version
00230  //--
00231 $sql="select setting from pg_settings where name='server_version'";
00232 $Res=ExecSql($cn,$sql);
00233 $row=pg_fetch_array($Res,0);
00234 $version=$row[0];
00235 
00236 var_dump($version);
00237 
00238 if ( $version[0]  != '8' ) {
00239 ?>
00240   <p> Vous devez absolument utiliser au minimum une version 8 de PostGresql, si votre distribution n'en
00241 offre pas, installez en une en la compilant. </p><p>Lisez attentivement la notice sur postgresql.org pour migrer
00242 vos bases de données en 8
00243 </p>
00244 <? exit(); //'
00245 }
00246 
00247 ?>
00248 <h2>Database Setting</h2> 
00249 <?
00250 // Language plsql is installed 
00251 //--
00252 $sql="select lanname from pg_language where lanname='plpgsql'";
00253 $Res=CountSql($cn,$sql);
00254 if ( $Res==0) { ?>
00255 <p> Vous devez installer le langage plpgsql pour permettre aux fonctions SQL de fonctionner.</p>
00256 <p>Pour cela, sur la ligne de commande, faites 
00257 createlang plpgsql pour chaque base de données que vous possédez (y compris template0 et template1).
00258 </p>
00259 <p>Pour afficher toutes les bases de données, tapez sur la ligne de commande "psql -l"</p>
00260 <? exit(); }
00261 
00262 // Memory setting
00263 //--
00264 $sql="select name,setting 
00265       from pg_settings 
00266       where 
00267       name in ('effective_cache_size','shared_buffers','work_mem')";
00268 $Res=ExecSql($cn,$sql);
00269 $flag=0;
00270 for ($e=0;$e<pg_NumRows($Res);$e++) {
00271   $a=pg_fetch_array($Res,$e);
00272   switch ($a['name']){
00273   case 'effective_cache_size':
00274     if ( $a['setting'] < 1000 ){
00275       print '<p class="warning">Attention le paramètre effective_cache_size est de '.
00276         $a['setting']." au lieu de 1000 </p>";
00277       $flag++;
00278     }
00279     break;
00280   case 'shared_buffers':
00281     if ( $a['setting'] < 640 ){
00282       print '<p class="warning">Attention le paramètre shared_buffer est de '.
00283         $a['setting']."au lieu de 640</p>";
00284       $flag++;
00285     }
00286     break;
00287   case 'work_mem':
00288     if ( $a['setting'] < 8192 ){
00289       print '<p class="warning">Attention le paramètre work_mem est de '.
00290         $a['setting']." au lieu de 8192 </p>";
00291     $flag++;
00292     }
00293     break;
00294 
00295   }
00296  }
00297 if ( $flag == 0 ) {
00298   echo '<p class="info">La base de données est bien configurée</p>';
00299  } else {
00300   echo '<p class="warning">Il y a '.$flag.' paramètre qui sont trop bas</p>';
00301  }
00302 if ( ! isset($_POST['go']) ) {
00303 ?>
00304 <FORM action="setup.php" METHOD="post">
00305 <input type="submit" name="go" value="Prêt à commencer la mise à jour ou l'installation?">
00306 </form>
00307 <?
00308 }
00309 if ( ! isset($_POST['go']) )
00310         exit();
00311 // Check if account_repository exists
00312 $account=CountSql($cn,
00313                   "select * from pg_database where datname='".domaine."account_repository'");
00314 
00315 // Create the account_repository
00316 if ($account == 0 ) {
00317 
00318   echo "Creation of ".domaine."account_repository";
00319   if ( DEBUG=='false') ob_start();  
00320   ExecSql($cn,"create database ".domaine."account_repository encoding='latin1'");
00321   $cn=DbConnect();
00322   StartSql($cn);
00323   ExecuteScript($cn,"sql/account_repository/schema.sql");
00324   ExecuteScript($cn,"sql/account_repository/data.sql");
00325   Commit($cn);
00326  if ( DEBUG=='false') ob_end_clean();
00327   echo "Creation of Démo";
00328   if ( DEBUG=='false') ob_start();  
00329   ExecSql($cn,"create database ".domaine."dossier1 encoding='latin1'");
00330   $cn=DbConnect(1,'dossier');
00331   StartSql($cn);
00332   ExecuteScript($cn,'sql/dossier1/schema.sql');
00333   ExecuteScript($cn,'sql/dossier1/data.sql');
00334   Commit($cn);
00335 
00336  if ( DEBUG=='false') ob_end_clean();
00337 
00338   echo "Creation of Modele1";
00339   if ( DEBUG=='false') ob_start();  
00340   ExecSql($cn,"create database ".domaine."mod1 encoding='latin1'");
00341   $cn=DbConnect(1,'mod');
00342   StartSql($cn);
00343   ExecuteScript($cn,'sql/mod1/schema.sql');
00344   ExecuteScript($cn,'sql/mod1/data.sql');
00345   Commit($cn);
00346  if ( DEBUG=='false') ob_end_clean();
00347  }// end if
00348 // Add a french accountancy model
00349 //--
00350 $cn=DbConnect();
00351 $Res=CountSql($cn,"select * from modeledef where mod_id=2");
00352 // ----------------------------------------------------------------------
00353 // to be remove 
00354 if ( $Res == 1 )
00355  {
00356   $cn=DbConnect();
00357   ExecSql($cn,"drop database ".domaine."mod2;");
00358   ExecSql($cn,"delete from modeledef where mod_id=2");
00359  }
00360 //----------------------------------------------------------------------
00361 $Res=CountSql($cn,"select * from modeledef where mod_id=2");
00362 if ( $Res == 0) {
00363   echo "Creation of Modele2";
00364   ExecSql($cn,"create database ".domaine."mod2 encoding='latin1'");
00365   $cn=DbConnect(2,'mod');
00366   if ( DEBUG=='false') { ob_start();  }
00367   ExecuteScript($cn,'sql/mod2/schema.sql');
00368   ExecuteScript($cn,'sql/mod2/data.sql');
00369   $sql="INSERT INTO modeledef VALUES (2, '(FR) Basique', 'Comptabilité Française, tout doit être adaptée');";
00370   $cn=DbConnect();
00371   ExecSql($cn,$sql);
00372  if ( DEBUG=='false') ob_end_clean();
00373 }
00374 // 
00375 // Test the connection
00376 //--
00377 $a=DbConnect();
00378 if ( $a==false) {
00379    exit ("<h2 class=\"error\">".__LINE__." test has failed !!!</h2>");
00380 
00381 }
00382 if ( ($Res=ExecSql($a,"select  * from ac_users") ) == false ) {
00383         exit ("<h2 class=\"error\">".__LINE__." test has failed !!!</h2>");
00384 } else 
00385         print "Connect to database success <br>";
00386 echo "<h2 class=\"info\"> Congratulation : Test successfull</h2>";
00387 
00388 echo "<h2 class=\"info\"> Patching databases</h2>";
00389 
00390 $cn=DbConnect();
00391 $Resdossier=ExecSql($cn,"select dos_id, dos_name from ac_dossier");
00392 $MaxDossier=pg_NumRows($Resdossier);
00393 
00394 for ($e=0;$e < $MaxDossier;$e++) {
00395   $db_row=pg_fetch_array($Resdossier,$e);
00396   $db=DbConnect($db_row['dos_id'],'dossier');
00397   echo "Patching ".$db_row['dos_name']." from the version ".GetVersion($db)."<hr>";
00398 if ( DEBUG=='false' ) ob_start();
00399   if ( GetVersion($db) <= 4 ) { 
00400     ExecuteScript($db,'sql/patch/upgrade4.sql');
00401       
00402     $sql="select jrn_def_id from jrn_def ";
00403     $Res=ExecSql($db,$sql);
00404     $Max=pg_NumRows($Res);
00405     for ($seq=0;$seq<$Max;$seq++) {
00406             $row=pg_fetch_array($Res,$seq);
00407             $sql=sprintf ("create sequence s_jrn_%d",$row['jrn_def_id']);
00408             ExecSql($db,$sql);
00409     }
00410   } // version == 4
00411   //--
00412   // update to the version 5
00413   //--
00414   if ( GetVersion($db) == 5 ) { 
00415     ExecuteScript($db,'sql/patch/upgrade5.sql');
00416   } // version == 5
00417 
00418 
00419   //--
00420   // update to the version 7
00421   //--
00422   if ( GetVersion($db) == 6 ) { 
00423     ExecuteScript($db,'sql/patch/upgrade6.sql');
00424   } // version == 6
00425 
00426   //--
00427   // update to the version 8
00428   //--
00429   if ( GetVersion($db) == 7 ) { 
00430     ExecuteScript($db,'sql/patch/upgrade7.sql');
00431     // now we use sequence instead of computing a max
00432     // 
00433     $Res2=ExecSql($db,'select coalesce(max(jr_grpt_id),1) as l from jrn');
00434     $Max2= pg_NumRows($Res2) ;
00435     if ( $Max2 == 1) {
00436       $Row=pg_fetch_array($Res2,0);
00437       var_dump($Row);
00438       $M=$Row['l'];
00439       ExecSql($db,"select setval('s_grpt',$M,true)");
00440     }
00441   } // version == 7
00442   // version 8 -> 9
00443   if ( GetVersion($db) == 8 ) { 
00444     ExecuteScript($db,'sql/patch/upgrade8.sql');
00445   } // version == 9->10
00446   if ( GetVersion($db) == 9 ) { 
00447     ExecuteScript($db,'sql/patch/upgrade9.sql');
00448   } // version == 10->11
00449   if ( GetVersion($db) == 10 ) { 
00450     ExecuteScript($db,'sql/patch/upgrade10.sql');
00451   } // version 
00452   if ( GetVersion($db) == 11 ) { 
00453     ExecuteScript($db,'sql/patch/upgrade11.sql');
00454   } // version 
00455   if ( GetVersion($db) == 12 ) { 
00456     ExecuteScript($db,'sql/patch/upgrade12.sql');
00457   } // version 
00458 
00459   if ( GetVersion($db) == 13 ) { 
00460     ExecuteScript($db,'sql/patch/upgrade13.sql');
00461   } // version 
00462 
00463   if ( GetVersion($db) == 14 ) { 
00464     ExecuteScript($db,'sql/patch/upgrade14.sql');
00465   } // version 
00466 
00467 
00468 if ( DEBUG == 'false') ob_end_clean();
00469  }//for
00470 
00471 $Resdossier=ExecSql($cn,"select mod_id, mod_name from modeledef");
00472 $MaxDossier=pg_NumRows($Resdossier);
00473 echo "Upgrading Dossier";
00474 for ($e=0;$e < $MaxDossier;$e++) {
00475   $db_row=pg_fetch_array($Resdossier,$e);
00476   echo "Patching ".$db_row['mod_name']."<hr>";
00477   $db=DbConnect($db_row['mod_id'],'mod');
00478 if (DEBUG == 'false' ) ob_start();
00479   if ( GetVersion($db) <= 4 ) { 
00480     ExecuteScript($db,'sql/patch/upgrade4.sql');
00481       
00482     $sql="select jrn_def_id from jrn_def ";
00483     $Res=ExecSql($db,$sql);
00484     $Max=pg_NumRows($Res);
00485     for ($seq=0;$seq<$Max;$seq++) {
00486             $row=pg_fetch_array($Res,$seq);
00487             $sql=sprintf ("create sequence s_jrn_%d",$row['jrn_def_id']);
00488             ExecSql($db,$sql);
00489     }
00490  } // version == 4
00491   if ( GetVersion($db) == 5 ) { 
00492     ExecuteScript($db,'sql/patch/upgrade5.sql');
00493   } // version == 5
00494 
00495 
00496   //--
00497   // update to the version 7
00498   //--
00499   if ( GetVersion($db) == 6 ) { 
00500     ExecuteScript($db,'sql/patch/upgrade6.sql');
00501   } // version == 6
00502 
00503   //--
00504   // update to the version 8
00505   //--
00506   if ( GetVersion($db) == 7 ) { 
00507     ExecuteScript($db,'sql/patch/upgrade7.sql');
00508     // now we use sequence instead of computing a max
00509     // 
00510     $Res2=ExecSql($db,'select coalesce(max(jr_grpt_id),1) as l from jrn');
00511     $Max2= pg_NumRows($Res2) ;
00512     if ( $Max2 == 1) {
00513       $Row=pg_fetch_array($Res2,0);
00514       $M=$Row['l'];
00515       ExecSql($db,"select setval('s_grpt',$M,true)");
00516     }
00517   } // version == 8
00518   //--
00519   // update to the version 9
00520   //--
00521   if ( GetVersion($db) == 8 ) { 
00522     ExecuteScript($db,'sql/patch/upgrade8.sql');
00523   } // version == 9
00524   // update to the version 10
00525   //--
00526   if ( GetVersion($db) == 9 ) { 
00527     ExecuteScript($db,'sql/patch/upgrade9.sql');
00528   } // version == 9
00529   if ( GetVersion($db) == 10 ) { 
00530     ExecuteScript($db,'sql/patch/upgrade10.sql');
00531   } // version 
00532   if ( GetVersion($db) == 11 ) { 
00533     ExecuteScript($db,'sql/patch/upgrade11.sql');
00534   } // version 
00535   if ( GetVersion($db) == 12 ) { 
00536     ExecuteScript($db,'sql/patch/upgrade12.sql');
00537   } // version 
00538   if ( GetVersion($db) == 13 ) { 
00539     ExecuteScript($db,'sql/patch/upgrade13.sql');
00540   } // version 
00541   if ( GetVersion($db) == 14 ) { 
00542     ExecuteScript($db,'sql/patch/upgrade14.sql');
00543   } // version 
00544 
00545 if ( DEBUG == 'false') ob_end_clean();
00546  }
00547 
00548 echo "Upgrading Repository";
00549 $cn=DbConnect();
00550 if ( DEBUG == 'false') ob_start();
00551 if ( GetVersion($cn) <= 4 ) {
00552   ExecuteScript($cn,'sql/patch/ac-upgrade4.sql');
00553  }
00554 if ( GetVersion($cn) == 5 ) {
00555   ExecuteScript($cn,'sql/patch/ac-upgrade5.sql');
00556  }
00557 if ( GetVersion($cn) == 6 ) {
00558   ExecuteScript($cn,'sql/patch/ac-upgrade6.sql');
00559  }
00560 if ( GetVersion($cn) == 7 ) {
00561   ExecuteScript($cn,'sql/patch/ac-upgrade7.sql');
00562  }
00563 
00564 if (DEBUG=='false') ob_end_clean();
00565 echo "<h2 class=\"info\">Voilà tout est installé ;-)</h2>";