pgt-db.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. /*
  3. * Copyright © 2003-2010, The ESUP-Portail consortium & the JA-SIG Collaborative.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * * Redistributions of source code must retain the above copyright notice,
  10. * this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. * * Neither the name of the ESUP-Portail consortium & the JA-SIG
  15. * Collaborative nor the names of its contributors may be used to endorse or
  16. * promote products derived from this software without specific prior
  17. * written permission.
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  22. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  25. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. /**
  30. * @file CAS/PGTStorage/pgt-db.php
  31. * Basic class for PGT database storage
  32. */
  33. /**
  34. * @class PGTStorageDB
  35. * The PGTStorageDB class is a class for PGT database storage. An instance of
  36. * this class is returned by CASClient::SetPGTStorageDB().
  37. *
  38. * @author Pascal Aubry <pascal.aubry at univ-rennes1.fr>
  39. *
  40. * @ingroup internalPGTStorageDB
  41. */
  42. class PGTStorageDB extends PGTStorage
  43. {
  44. /**
  45. * @addtogroup internalPGTStorageDB
  46. * @{
  47. */
  48. /**
  49. * a string representing a PEAR DB URL to connect to the database. Written by
  50. * PGTStorageDB::PGTStorageDB(), read by getURL().
  51. *
  52. * @hideinitializer
  53. * @private
  54. */
  55. var $_url='';
  56. /**
  57. * This method returns the PEAR DB URL to use to connect to the database.
  58. *
  59. * @return a PEAR DB URL
  60. *
  61. * @private
  62. */
  63. function getURL()
  64. {
  65. return $this->_url;
  66. }
  67. /**
  68. * The handle of the connection to the database where PGT's are stored. Written by
  69. * PGTStorageDB::init(), read by getLink().
  70. *
  71. * @hideinitializer
  72. * @private
  73. */
  74. var $_link = null;
  75. /**
  76. * This method returns the handle of the connection to the database where PGT's are
  77. * stored.
  78. *
  79. * @return a handle of connection.
  80. *
  81. * @private
  82. */
  83. function getLink()
  84. {
  85. return $this->_link;
  86. }
  87. /**
  88. * The name of the table where PGT's are stored. Written by
  89. * PGTStorageDB::PGTStorageDB(), read by getTable().
  90. *
  91. * @hideinitializer
  92. * @private
  93. */
  94. var $_table = '';
  95. /**
  96. * This method returns the name of the table where PGT's are stored.
  97. *
  98. * @return the name of a table.
  99. *
  100. * @private
  101. */
  102. function getTable()
  103. {
  104. return $this->_table;
  105. }
  106. // ########################################################################
  107. // DEBUGGING
  108. // ########################################################################
  109. /**
  110. * This method returns an informational string giving the type of storage
  111. * used by the object (used for debugging purposes).
  112. *
  113. * @return an informational string.
  114. * @public
  115. */
  116. function getStorageType()
  117. {
  118. return "database";
  119. }
  120. /**
  121. * This method returns an informational string giving informations on the
  122. * parameters of the storage.(used for debugging purposes).
  123. *
  124. * @public
  125. */
  126. function getStorageInfo()
  127. {
  128. return 'url=`'.$this->getURL().'\', table=`'.$this->getTable().'\'';
  129. }
  130. // ########################################################################
  131. // CONSTRUCTOR
  132. // ########################################################################
  133. /**
  134. * The class constructor, called by CASClient::SetPGTStorageDB().
  135. *
  136. * @param $cas_parent the CASClient instance that creates the object.
  137. * @param $user the user to access the data with
  138. * @param $password the user's password
  139. * @param $database_type the type of the database hosting the data
  140. * @param $hostname the server hosting the database
  141. * @param $port the port the server is listening on
  142. * @param $database the name of the database
  143. * @param $table the name of the table storing the data
  144. *
  145. * @public
  146. */
  147. function PGTStorageDB($cas_parent,$user,$password,$database_type,$hostname,$port,$database,$table)
  148. {
  149. phpCAS::traceBegin();
  150. // call the ancestor's constructor
  151. $this->PGTStorage($cas_parent);
  152. if ( empty($database_type) ) $database_type = CAS_PGT_STORAGE_DB_DEFAULT_DATABASE_TYPE;
  153. if ( empty($hostname) ) $hostname = CAS_PGT_STORAGE_DB_DEFAULT_HOSTNAME;
  154. if ( $port==0 ) $port = CAS_PGT_STORAGE_DB_DEFAULT_PORT;
  155. if ( empty($database) ) $database = CAS_PGT_STORAGE_DB_DEFAULT_DATABASE;
  156. if ( empty($table) ) $table = CAS_PGT_STORAGE_DB_DEFAULT_TABLE;
  157. // build and store the PEAR DB URL
  158. $this->_url = $database_type.':'.'//'.$user.':'.$password.'@'.$hostname.':'.$port.'/'.$database;
  159. // XXX should use setURL and setTable
  160. phpCAS::traceEnd();
  161. }
  162. // ########################################################################
  163. // INITIALIZATION
  164. // ########################################################################
  165. /**
  166. * This method is used to initialize the storage. Halts on error.
  167. *
  168. * @public
  169. */
  170. function init()
  171. {
  172. phpCAS::traceBegin();
  173. // if the storage has already been initialized, return immediatly
  174. if ( $this->isInitialized() )
  175. return;
  176. // call the ancestor's method (mark as initialized)
  177. parent::init();
  178. //include phpDB library (the test was introduced in release 0.4.8 for
  179. //the integration into Tikiwiki).
  180. if (!class_exists('DB')) {
  181. include_once('DB.php');
  182. }
  183. // try to connect to the database
  184. $this->_link = DB::connect($this->getURL());
  185. if ( DB::isError($this->_link) ) {
  186. phpCAS::error('could not connect to database ('.DB::errorMessage($this->_link).')');
  187. }
  188. var_dump($this->_link);
  189. phpCAS::traceBEnd();
  190. }
  191. /** @} */
  192. }
  193. ?>