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

Source for file connect.php

Documentation is available at connect.php

  1. <?php
  2. /**
  3.  * Fichier pour gérer la connection à la base de donnée - initialise les connexions
  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.  * @copyright 2005, Ghislain Picard, Marin Dacos, Luc Santeramo, Gautier Poupeau, Jean Lamy, Bruno Cénou
  39.  * @copyright 2006, Marin Dacos, Luc Santeramo, Bruno Cénou, Jean Lamy, Mikaël Cixous, Sophie Malafosse
  40.  * @copyright 2007, Marin Dacos, Bruno Cénou, Sophie Malafosse, Pierre-Alain Mignot
  41.  * @licence http://www.gnu.org/copyleft/gpl.html
  42.  * @version CVS:$Id:
  43.  * @package lodel
  44.  */
  45. if (!(INC_LODELCONFIG)) {
  46.     die("inc lodelconfig please")// security
  47. }
  48. // compatibility 0.7
  49. if (!defined("DATABASE")) {
  50.     define("DATABASE"$GLOBALS['database']);
  51.     define("DBUSERNAME"$GLOBALS['dbusername']);
  52.     define("DBPASSWD"$GLOBALS['dbpasswd']);
  53.     define("DBHOST"$GLOBALS['dbhost']);
  54.     define("DBDRIVER""mysql");
  55. }
  56.  
  57. // connect to the database server
  58. require_once "adodb/adodb.inc.php";
  59. $GLOBALS['db'ADONewConnection(DBDRIVER);
  60. $GLOBALS['db']->debug = false// mettre à true pour activer le mode debug
  61.  
  62. if ($GLOBALS['site'&& $GLOBALS['singledatabase'!= "on"{
  63.     $GLOBALS['currentdb'DATABASE"_".$GLOBALS['site'];
  64. else {
  65.     $GLOBALS['currentdb'DATABASE;
  66. }
  67.  
  68. if (!defined("SINGLESITE")) {
  69.     define("SINGLESITE"$GLOBALS['singledatabase'== "on")// synonyme currently but may change in the future
  70. }
  71.  
  72. $GLOBALS['db']->connect(DBHOSTDBUSERNAMEDBPASSWD$GLOBALS['currentdb']or dberror();
  73. if (DBDRIVER == 'mysql'{
  74.     $info_mysql $GLOBALS['db']->ServerInfo();
  75.     $vs_mysql explode("."substr($info_mysql['version']03));
  76.     $GLOBALS['version_mysql'$vs_mysql[0$vs_mysql[1];
  77.  
  78.     if ($GLOBALS['version_mysql'40{
  79.         $GLOBALS['db_charset'mysql_find_db_variable($GLOBALS['currentdb']'character_set_database');
  80.         if ($GLOBALS['db_charset'=== false{
  81.             $GLOBALS['db_charset''utf8';
  82.         }
  83.         $GLOBALS['db']->execute('SET NAMES ' $GLOBALS['db_charset']);
  84.     }
  85. }
  86.  
  87. $GLOBALS['db']->SetFetchMode(ADODB_FETCH_ASSOC);
  88. $GLOBALS['tp'$GLOBALS['tableprefix'];
  89.  
  90.  
  91. /**
  92.  * Déclenche une erreur lors d'une erreur concernant la base de données
  93.  */
  94. function dberror()
  95. {
  96.     global $db;
  97.     $ret trigger_error($db->errormsg()E_USER_ERROR);
  98. }
  99.  
  100. $GLOBALS['maindb''';
  101. $GLOBALS['savedb''';
  102.  
  103. /**
  104.  * Positionne la connexion de la base de données sur la table principale (en cas d'installation
  105.  * multisite.
  106.  */
  107. function usemaindb()
  108. {
  109.     global $db$maindb$savedb;
  110.     if (DATABASE == $GLOBALS['currentdb']{
  111.         return false// nothing to do
  112.     }
  113.     if ($db->selectDB(DATABASE)) {
  114.         return true// try to selectdb
  115.     }
  116.  
  117.     if (!$maindb)    // not connected
  118.         $maindb ADONewConnection(DBDRIVER);
  119.         if (!$maindb->nconnect(DBHOSTDBUSERNAMEDBPASSWDDATABASE)) {
  120.             die("ERROR: reconnection is not allow with the driver: ".DBDRIVER);
  121.         }
  122.     }
  123.  
  124.     // set $db as $maindb
  125.     $savedb &$db;
  126.     $db &$maindb;
  127.     return true;
  128. }
  129.  
  130. /**
  131.  * Positionne la connexion de la base de données sur la base de données du site (si Lodel est
  132.  * installé en multisite, l'unique base sinon
  133.  */
  134. function usecurrentdb()
  135. {
  136.     if (DATABASE == $GLOBALS['currentdb']{
  137.         return// nothing to do
  138.     }
  139.     global $db$savedb;
  140.     if ($db->selectDB($GLOBALS['currentdb'])) {
  141.         return// try to selectdb
  142.     }
  143.     $db &$savedb;
  144. }
  145.  
  146. /**
  147.  * Lodel Query :
  148.  *
  149.  * Transforme les requêtes en résolvant les jointures et en cherchant les bonnes
  150.  * tables dans les bases de données (suivant notamment le préfix utilisé pour le nommage des
  151.  * tables).
  152.  *
  153.  * @param string $query la requête à traduire
  154.  * @return string la requête traduite
  155.  */
  156. function lq($query)
  157. {
  158.     static $cmd;
  159.     // the easiest, fats replace
  160.     $query str_replace('#_TP_'$GLOBALS['tableprefix']$query);
  161.     // any other ?
  162.     if (strpos($query'#_'!== false)    {
  163.         if (!$cmd)
  164.             $cmd array ('#_MTP_' => DATABASE.'.'.$GLOBALS['tableprefix'],
  165.     '#_entitiestypesjoin_' => "$GLOBALS[tableprefix]types INNER JOIN $GLOBALS[tableprefix]entities ON $GLOBALS[tableprefix]types.id=$GLOBALS[tableprefix]entities.idtype",
  166.  
  167.     '#_tablefieldsandgroupsjoin_' => "$GLOBALS[tableprefix]tablefieldgroups INNER JOIN $GLOBALS[tableprefix]tablefields ON $GLOBALS[tableprefix]tablefields.idgroup=$GLOBALS[tableprefix]tablefieldgroups.id",
  168.  
  169.     '#_tablefieldgroupsandclassesjoin_' => "$GLOBALS[tableprefix]tablefieldgroups INNER JOIN $GLOBALS[tableprefix]classes ON $GLOBALS[tableprefix]classes.class=$GLOBALS[tableprefix]tablefieldgroups.class");
  170.  
  171.         $query strtr($query$cmd);
  172.     }
  173.     return $query;
  174. }
  175.  
  176. /**
  177.  * Fonction nécessaire pour la gestion des id numériques uniques (dans la table object)
  178.  *
  179.  * get a unique id
  180.  * fonction for handling unique id
  181.  *
  182.  * @param string $table le nom de la table dans laquelle on veut insérer un objet
  183.  * @return integer Un entier correspondant à l'id inséré.
  184.  */
  185. function uniqueid($table)
  186. {
  187.     global $db;
  188.     $db->execute(lq("INSERT INTO #_TP_objects (classVALUES ('$table')")) or dberror();
  189.     return $db->insert_id();
  190. }
  191.  
  192. /**
  193.  * Suppression d'un identifiant uniques (table objets)
  194.  *
  195.  * erase a unique id.
  196.  * Cette fonction accepte en entrée un id ou un tableau d'id
  197.  *
  198.  * @param integer or array un id ou un tableau d'ids.
  199.  */
  200. function deleteuniqueid($id)
  201. {
  202.     global $db;
  203.     if (is_array($id&& $id)    {
  204.         $db->execute(lq("DELETE FROM $GLOBALS[tableprefix]objects WHERE id IN ("join(","$id)")"));
  205.     }    else {
  206.         $db->execute(lq("DELETE FROM $GLOBALS[tableprefix]objects WHERE id='$id'"));
  207.     }
  208. }
  209.  
  210. /**
  211.  * Recherche d'une variable MySQL
  212.  *
  213.  * @param string $database_name nom de la base de donnée
  214.  * @param string $var nom de la variable recherchée
  215.  * @return valeur de la variable
  216.  */
  217. function mysql_find_db_variable ($database_name$var 'character_set_database'{
  218.     mysql_select_db($database_nameor die ("ERROR select database");
  219.     $result mysql_query("SHOW VARIABLES LIKE '$var'");
  220.     if ($db_charset mysql_fetch_row($result)) {
  221.         return $db_charset[1]}
  222.     else return false;
  223. }
  224.  
  225. ?>

Documentation generated on Thu, 24 Jul 2008 05:08:27 +0200 by phpDocumentor 1.4.0a2