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

postgres.php

Go to the documentation of this file.
00001 <? //$Revision: 1.28 $
00002 /*
00003  *   This file is part of PhpCompta.
00004  *
00005  *   PhpCompta is free software; you can redistribute it and/or modify
00006  *   it under the terms of the GNU General Public License as published by
00007  *   the Free Software Foundation; either version 2 of the License, or
00008  *   (at your option) any later version.
00009  *
00010  *   PhpCompta is distributed in the hope that it will be useful,
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  *   GNU General Public License for more details.
00014  *
00015  *   You should have received a copy of the GNU General Public License
00016  *   along with PhpCompta; if not, write to the Free Software
00017  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 */
00019 
00020  //$Revision: 1.28 $
00021 // Copyright Author Dany De Bontridder ddebontridder@yahoo.fr
00032 function ShowDossier($p_type,$p_first=0,$p_max=10,$p_Num=0) {
00033   $l_user=$_SESSION['g_user'];
00034   if ( $p_max == 0 ) {
00035     $l_step="";
00036   } else {
00037     $l_step="LIMIT $p_max OFFSET $p_first";
00038   }
00039   $cn=DbConnect();
00040   if ( $p_type == "all") {
00041     $l_sql="select *, 'W' as priv_priv from ac_dossier ORDER BY dos_name  ";
00042     $p_Num=CountSql($cn,$l_sql);
00043   } else {
00044     $l_sql="select * from jnt_use_dos 
00045                                natural join ac_dossier 
00046                                natural join ac_users 
00047                                inner join priv_user on priv_jnt=jnt_id where 
00048                                use_login='".$l_user."' and priv_priv !='NO'
00049                                order by dos_name ";
00050     $p_Num=CountSql($cn,$l_sql);
00051   }
00052   $l_sql=$l_sql.$l_step;
00053   $p_res=ExecSql($cn,$l_sql);
00054 
00055   echo_debug('postgres.php',__LINE__,"ShowDossier:".$p_res." Line = $p_Num");
00056 
00057   $Max=pg_NumRows($p_res);
00058   if ( $Max == 0 ) return null;
00059   for ( $i=0;$i<$Max; $i++) {   
00060     //    echo_debug ("i = $i");
00061     $row[]=pg_fetch_array($p_res,$i);
00062     //echo $row[dossier];
00063   }
00064   return $row;
00065 }
00066 
00072 function DbConnect($p_db=-1,$p_type='dossier') {
00073   if ( $p_db==-1) 
00074     $l_dossier=sprintf("%saccount_repository",domaine);
00075   else {
00076     if ( $p_db == -2 )
00077       $l_dossier=$p_type;
00078     else 
00079       switch ($p_type) {
00080       case 'dossier':
00081         $l_dossier=sprintf("%sdossier%d",domaine,$p_db);
00082         break;
00083       case 'mod':
00084         $l_dossier=sprintf("%smod%d",domaine,$p_db);
00085         break;
00086       }
00087   }
00088   $password=phpcompta_password;
00089   $a=pg_connect("dbname=$l_dossier host=127.0.0.1 user='phpcompta' password='$password'");
00090   echo_debug ('postgres.php',__LINE__,"connect to $p_db dbname $l_dossier");
00091   return $a;
00092 }
00099 function ExecSql($p_connection, $p_string) {
00100   echo_debug('postgres.php',__LINE__,"SQL = $p_string");
00101   // probl. with Ubuntu & UTF8
00102   //----
00103   pg_set_client_encoding($p_connection,'latin1');
00104   $ret=pg_query($p_connection,$p_string);
00105   if ( $ret == false ) { 
00106     echo_error ("SQL ERROR ::: $p_string");
00107     exit(" Operation cancelled due to error : $p_string");
00108   }
00109 
00110   return $ret;
00111 }
00112 
00117 function GetAllUser() {
00118   echo_debug('postgres.php',__LINE__,"GetUser");
00119   $cn=DbConnect();
00120   $sql="select * from ac_users where use_login!='phpcompta'";
00121   echo_debug('postgres.php',__LINE__,"ExecSql");
00122   $Res=ExecSql($cn,$sql);
00123   $Num=pg_NumRows($Res);
00124   if ( $Num == 0 ) return null;
00125   for ($i=0;$i < $Num; $i++) {
00126     $User[]=pg_fetch_array($Res,$i);
00127   }
00128   return $User;
00129 }
00134 function GetUid($p_uid) {
00135   $cn=DbConnect();
00136   $Res=ExecSql($cn,"select * from ac_users where use_id=".$p_uid);
00137   $Num=pg_NumRows($Res);
00138   if ( $Num == 0 ) { return false; }
00139   for ($i=0;$i < $Num; $i++) {
00140     $Prop[]=pg_fetch_array($Res,$i);
00141   }
00142   return $Prop;
00143 }
00147 function GetPriv($p_dossier,$p_login)
00148 {
00149   $cn=DbConnect();
00150   $Res=ExecSql($cn,"select priv_priv 
00151                     from priv_user  left join jnt_use_dos on jnt_id=priv_jnt
00152                         inner join ac_users on ac_users.use_id=jnt_use_dos.use_id
00153                     where use_login='$p_login' and dos_id=$p_dossier");
00154   $Num=pg_NumRows($Res);
00155   echo_debug('postgres.php',__LINE__,"Found ".$Num." rows in GetPriv");
00156   if ( $Num==0) { return 0;}
00157   for($i=0;$i < $Num;$i++) {
00158     $Right=pg_fetch_array($Res,$i); 
00159     $Priv[]=$Right['priv_priv'];
00160   }
00161   return $Priv;
00162 }
00168 function ExisteJnt($p_dossier,$p_user)
00169 {
00170   $cn=DbConnect();
00171   $Res=ExecSql($cn,"select * from jnt_use_dos where dos_id=".$p_dossier." and use_id=".$p_user);
00172   return pg_NumRows($Res);
00173 }
00174 /* ExistePriv
00175  * 
00176  * 
00177  * 
00178  */
00179 function ExistePriv($p_jntid)
00180 {
00181   $cn=DbConnect();
00182   $Res=ExecSql($cn,"select * from priv_user where priv_jnt=".$p_jntid);
00183   return pg_NumRows($Res);
00184 }
00185 /* GetJnt
00186  * Get the jnt
00187  * 
00188  * 
00189  */
00190 function GetJnt($p_dossier,$p_user)
00191 {
00192   $cn=DbConnect();
00193   $Res=ExecSql($cn,"select jnt_id from jnt_use_dos where dos_id=".$p_dossier." and use_id=".$p_user);
00194   $R=pg_fetch_array($Res,0);
00195   return $R['jnt_id'];
00196 }
00197 /* GetDbId
00198  * Get the dos_id of a dossier
00199  * parm: name of the folder
00200  */
00201 function GetDbId($p_name)
00202 {
00203   $cn=DbConnect();
00204   $r_sql=ExecSql($cn,"select dos_id from ac_dossier
00205                  where dos_name='".$p_name."'");
00206   $num=pg_NumRows($r_sql);
00207   if ( $num == 0 ) {
00208     return 0;
00209   } else {
00210     $l_db=pg_fetch_array($r_sql,0);
00211     return $l_db['dos_id'];
00212   }
00213      
00214 }
00215 /* CountSql
00216  * \brief Count the number of row
00217  * 
00218  * \param $p_conn connection handler
00219  * \param $p_sql sql string
00220  */
00221 
00222 function CountSql($p_conn,$p_sql)
00223 {
00224   $r_sql=ExecSql($p_conn,$p_sql);
00225   return pg_NumRows($r_sql);
00226 
00227 }
00232 function GetDossierName($p_dossier)
00233 {
00234   $cn=DbConnect();
00235   $Ret=ExecSql($cn,"select dos_name from ac_dossier where dos_id=".$p_dossier);
00236   $r= pg_fetch_array($Ret,0);
00237   return $r['dos_name'];
00238 }
00239 
00242 function GetSequence($p_cn,$p_seq)
00243 {
00244   $Res=ExecSql($p_cn,"select currval('$p_seq') as seq");
00245   $seq=pg_fetch_array($Res,0);
00246   return $seq['seq'];
00247 }
00248 
00251 function NextSequence($p_cn,$p_seq)
00252 {
00253   $Res=ExecSql($p_cn,"select nextval('$p_seq') as seq");
00254   $seq=pg_fetch_array($Res,0);
00255   return $seq['seq'];
00256 }
00257 function StartSql($p_cn) {
00258   $Res=ExecSql($p_cn,"start transaction");
00259 }
00260 function EndSql($p_cn) {
00261   $Res=ExecSql($p_cn,"end transaction");
00262 }
00263 function Commit($p_cn) {
00264   $Res=ExecSql($p_cn,"commit");
00265 }
00266 function Rollback($p_cn) {
00267   $Res=ExecSql($p_cn,"rollback");
00268 }
00269 function AlterSequence($p_cn,$p_name,$p_value) {
00270   
00271   $Res=ExecSql($p_cn,"alter sequence $p_name restart $p_value");
00272 }
00273 function GetLogin($p_uid)
00274 {
00275   $cn=DbConnect();
00276   $Res=ExecSql($cn,"select use_login from ac_users where use_id=$p_uid");
00277   if ( pg_NumRows($Res) == 0 ) return null;
00278   $a_login=pg_fetch_array($Res,0);
00279   return $a_login['use_login'];
00280 }
00281 
00289 function SyncRight($p_dossier,$p_user) {
00290   $priv=GetPriv($p_dossier,$p_user);
00291   $right=$priv[0];
00292 
00293   $cn=DbConnect($p_dossier);
00294 
00295  $sql="insert into user_sec_jrn(uj_login,uj_jrn_id,uj_priv) ".
00296    "select '".$p_user."',jrn_def_id,'".$right."' from jrn_def ".
00297    "where jrn_def_id not in ".
00298    "(select uj_jrn_id from user_sec_jrn where uj_login='".$p_user."')";
00299  $Res=ExecSql($cn,$sql);
00300 }
00315 function GetUserProperty($p_cn,$p_user)
00316 {
00317  $sql="select use_login,use_first_name,use_name,use_admin,use_usertype,g_theme
00318      from ac_users where use_login='$p_user'";
00319  $Ret=ExecSql($p_cn,$sql);
00320  if ( pg_NumRows($Ret) == 0) 
00321    return array('use_first_name'=>'?',
00322                 'use_name'=>'Unknown',
00323                 'use_admin'=>0,
00324                 'use_usertype'=>'user',
00325                 'g_theme'=>'classic',
00326                 'use_login'=>$p_user);
00327 
00328  $a=pg_fetch_array($Ret,0);
00329  return $a;
00330 }
00340 function GetModeleId($p_cn,$p_modname) {
00341   $Res=ExecSql($p_cn,"select mod_id from modeledef where mod_name='$p_modname'");
00342   if (pg_NumRows($Res) == 0) return 0;
00343   $name=pg_fetch_array($Res,0);
00344   return $name['mod_id'];
00345 }
00346 
00347 
00353 function GetArray($p_cn,$p_sql) {
00354   echo_debug('postgres.php',__LINE__,"GetArray");
00355   $r=ExecSql($p_cn,$p_sql);
00356   if ( ($Max=  pg_NumRows($r)) == 0 ) return null;
00357   $array=pg_fetch_all($r);
00358   echo_debug('postgres.php',__LINE__,var_export($array,true));
00359   return $array;
00360 }
00371 function save_upload_document ($cn,$seq) {
00372 
00373   $new_name=tempnam('/tmp','pj');
00374   echo_debug('postgres.php',__LINE__,"new name=".$new_name);
00375   if ( strlen ($_FILES['pj']['tmp_name']) != 0 ) {
00376       if (move_uploaded_file($_FILES['pj']['tmp_name'],
00377                              $new_name)) {
00378         // echo "Image saved";
00379 
00380         $oid= pg_lo_import($cn,$new_name);
00381         if ( $oid == false ) {
00382           echo_error('postgres.php',__LINE__,"cannot upload document");
00383           Rollback($cn);
00384           return;
00385         }
00386         echo_debug('postgres.php',__LINE__,"Loading document");
00387         // Remove old document
00388         $ret=ExecSql($cn,"select jr_pj from jrn where jr_grpt_id=$seq");
00389         if (pg_num_rows($ret) != 0) {
00390           $r=pg_fetch_array($ret,0);
00391           $old_oid=$r['jr_pj'];
00392           if (strlen($old_oid) != 0) 
00393             pg_lo_unlink($cn,$old_oid);
00394         }
00395         // Load new document
00396         ExecSql($cn,"update jrn set jr_pj=".$oid.", jr_pj_name='".$_FILES['pj']['name']."', ".
00397                 "jr_pj_type='".$_FILES['pj']['type']."'  where jr_grpt_id=$seq");
00398         return $oid;
00399 
00400       }      else {
00401         echo "<H1>Error</H1>";
00402         Rollback($cn);
00403         return null;
00404       }
00405     }
00406 
00407  }
00414 function getDbValue($p_cn,$sql)
00415 {
00416   $ret=ExecSql($p_cn,$sql);
00417   if ( pg_NumRows($ret) == 0 ) return "";
00418   $r=pg_fetch_row($ret,0);
00419   return $r[0];
00420 }
00421 
00422 ?>