Advertisement
Guest User

rcp_database.class.php

a guest
May 26th, 2015
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.87 KB | None | 0 0
  1. <?php
  2. /**
  3. * hal-ko tryst-cms
  4. * ütf-8 release
  5. *
  6. * @package tryst-cms
  7. * @author hal.sascha
  8. * Modified for remoteCP usage
  9. */
  10. class rcp_database
  11. {
  12. /**
  13. * Array of database connection handles
  14. */
  15. private $connections = array();
  16.  
  17. /**
  18. * Tells the DB object which connection to use
  19. * setActiveConnection($id) allows us to change this
  20. */
  21. private $activeConnection = false;
  22.  
  23. /**
  24. * Create a new database connection
  25. * @param String database hostname
  26. * @param String database username
  27. * @param String database password
  28. * @param String database we are using
  29. * @return mixed the id of the new connection
  30. */
  31. public function newConnection($id, $dsn, $username, $password)
  32. {
  33. // Get dsn type
  34. $type = explode(':', $dsn);
  35. $type = $type[0];
  36.  
  37. // Connect
  38. try {
  39. $this->connections[$id] = array(
  40. 'handle' => new PDO($dsn, $username, $password),
  41. 'dsn' => $dsn,
  42. 'username' => $username,
  43. 'password' => $password,
  44. 'type' => $type
  45. );
  46. } catch(PDOException $e) {
  47. trigger_error($e->getMessage(), E_USER_WARNING);
  48. return false;
  49. }
  50.  
  51. $this->activeConnection = $id;
  52. $db = $this->getConnection();
  53. $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  54.  
  55. // UTF8-ish the connection
  56. $db->exec("SET NAMES 'utf8'");
  57. $db->exec("SET CHARACTER SET 'utf8'");
  58. return $this->activeConnection;
  59. }
  60.  
  61. /**
  62. * Returns the currently active PDO connection
  63. * @return object
  64. */
  65. public function getConnection()
  66. {
  67. return $this->connections[$this->activeConnection]['handle'];
  68. }
  69.  
  70. /**
  71. * Returns the current status of a PDO connection
  72. * @return boolean
  73. */
  74. public function checkConnection()
  75. {
  76. return ($this->connections[$this->activeConnection]['handle']) ? true : false;
  77. }
  78.  
  79. /**
  80. * Pings the current PDO connection, reconnects if needed
  81. * @return boolean
  82. */
  83. public function pingConnection()
  84. {
  85. // Check if connection allready exists
  86. if($this->checkConnection()) return true;
  87.  
  88. // Try reconnect
  89. $this->closeConnection();
  90. if($this->newConnection(
  91. $this->activeConnection,
  92. $this->connections[$this->activeConnection]['dsn'],
  93. $this->connections[$this->activeConnection]['username'],
  94. $this->connections[$this->activeConnection]['password']
  95. )) {
  96. return true;
  97. }
  98.  
  99. return false;
  100. }
  101.  
  102. /**
  103. * Close the active connection
  104. * @return void
  105. */
  106. public function closeConnection()
  107. {
  108. $this->connections[$this->activeConnection]['handle'] = null;
  109. unset($this->connections[$this->activeConnection]);
  110. }
  111.  
  112. /**
  113. * Change which database connection is actively used for the next operation
  114. * @param int the new connection id
  115. * @return void
  116. */
  117. public function setActiveConnection($id)
  118. {
  119. $this->activeConnection = $id;
  120. }
  121.  
  122. /**
  123. * Deconstruct the object
  124. * close all of the database connections
  125. */
  126. public function __deconstruct()
  127. {
  128. foreach($this->connections as $connection)
  129. {
  130. $connection['handle'] = null;
  131. }
  132. }
  133.  
  134. /**
  135. * Imports a sql file into the database
  136. * @param string $plugin
  137. */
  138. public function fileImport($plugin, $pluginpath = false)
  139. {
  140. // Check connection
  141. if(!$this->checkConnection()) return;
  142. $db = $this->getConnection();
  143.  
  144. // Get pluginpath
  145. $pluginpath = ($pluginpath === false) ? Core::getSetting('pluginpath') : $pluginpath;
  146.  
  147. // Get type
  148. $type = $this->connections[$this->activeConnection]['type'];
  149.  
  150. // Load file
  151. $import = file_get_contents($pluginpath.'/'.$plugin.'/'.$type.'_'.$plugin.'.sql');
  152.  
  153. // Remove comments
  154. $import = preg_replace("%/\*(.*)\*/%Us", '', $import);
  155. $import = preg_replace("%^--(.*)\n%mU", '', $import);
  156. $import = preg_replace("%^$\n%mU", '', $import);
  157.  
  158. // Get query chunks
  159. $import = explode (";", $import);
  160.  
  161. // Exec queries
  162. foreach($import as $query)
  163. {
  164. if(!empty($query)) {
  165. $db->exec($query);
  166. }
  167. }
  168. }
  169. }
  170. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement