XOOPS  2.6.0
Connection.php
Go to the documentation of this file.
1 <?php
13 
29 class Connection extends \Doctrine\DBAL\Connection
30 {
35  protected static $safe;
36 
37 
41  protected static $force;
42 
46  protected static $transactionActive;
47 
48 
56  public static function setSafe($safe = true)
57  {
58  if (is_bool($safe)) {
59  self::$safe = $safe;
60  }
61  }
62 
68  public static function getSafe()
69  {
70  return self::$safe;
71  }
72 
80  public static function setForce($force = false)
81  {
82  if (is_bool($force)) {
83  self::$force = $force;
84  }
85  }
86 
92  public static function getForce()
93  {
94  return self::$force;
95  }
96 
107  public function __construct(
108  array $params,
109  \Doctrine\DBAL\Driver $driver,
110  \Doctrine\DBAL\Configuration $config = null,
111  \Doctrine\Common\EventManager $eventManager = null
112  ) {
113  if (!defined('XOOPS_DB_PROXY') || ($_SERVER['REQUEST_METHOD'] != 'GET')) {
114  self::setSafe(true);
115  } else {
116  self::setSafe(false);
117  }
118  self::setForce(false);
119  self::$transactionActive = false;
120  try {
121  parent::__construct($params, $driver, $config, $eventManager);
122  } catch (\Exception $e) {
123  // We are dead in the water. This exception may contain very sensitive
124  // information and cannot be allowed to be displayed as is.
125  //$xoopsPreload->triggerEvent('core.exception', $e);
126  trigger_error("Cannot get database connection", E_USER_ERROR);
127  }
128 
129  }
130 
139  public static function prefix($tablename = '')
140  {
141  static $prefix;
142 
143  $prefix = \XoopsBaseConfig::get('db-prefix');
144  if ($tablename != '') {
145  return $prefix . '_' . $tablename;
146  } else {
147  return $prefix;
148  }
149  }
150 
162  public function insertPrefix($tableName, array $data, array $types = array())
163  {
164  $tableName = $this->prefix($tableName);
165  return $this->insert($tableName, $data, $types);
166  }
167 
168 
183  public function updatePrefix($tableName, array $data, array $identifier, array $types = array())
184  {
185  $tableName = $this->prefix($tableName);
186  return $this->update($tableName, $data, $identifier, $types);
187  }
188 
201  public function deletePrefix($tableName, array $identifier)
202  {
203  $tableName = $this->prefix($tableName);
204  return $this->delete($tableName, $identifier);
205  }
206 
222  public function executeQuery(
223  $query,
224  array $params = array(),
225  $types = array(),
226  \Doctrine\DBAL\Cache\QueryCacheProfile $qcp = null
227  ) {
228  return parent::executeQuery($query, $params, $types, $qcp);
229  }
230 
250  public function executeUpdate($query, array $params = array(), array $types = array())
251  {
252  $result = 0;
253  $xoopsPreload = \Xoops::getInstance()->events();
254  if (self::getSafe() || self::getForce()) {
255  if (!self::$transactionActive) {
256  self::setForce(false);
257  };
258  $xoopsPreload->triggerEvent('core.database.query.start');
259  try {
260  $result = parent::executeUpdate($query, $params, $types);
261  } catch (\Exception $e) {
262  $xoopsPreload->triggerEvent('core.exception', $e);
263  $result = 0;
264  }
265  $xoopsPreload->triggerEvent('core.database.query.end');
266  } else {
267  //$xoopsPreload->triggerEvent('core.database.query.failure', (array('Not safe:')));
268  return (int) 0;
269  }
270  if ($result != 0) {
271  //$xoopsPreload->triggerEvent('core.database.query.success', (array($query)));
272  return (int) $result;
273  } else {
274  //$xoopsPreload->triggerEvent('core.database.query.failure', (array($query)));
275  return (int) 0;
276  }
277  }
278 
284  public function beginTransaction()
285  {
286  self::$transactionActive = true;
287  parent::beginTransaction();
288  }
289 
295  public function commit()
296  {
297  self::$transactionActive = false;
298  self::setForce(false);
299  parent::commit();
300  }
301 
307  public function rollBack()
308  {
309  self::$transactionActive = false;
310  self::setForce(false);
311  parent::rollBack();
312  }
313 
326  public function query()
327  {
328  $xoopsPreload = \Xoops::getInstance()->events();
329  if (!self::getSafe() && !self::getForce()) {
330  $sql = ltrim(func_get_arg(0));
331  if (!self::getSafe() && strtolower(substr($sql, 0, 6))!= 'select') {
332  // $xoopsPreload->triggerEvent('core.database.query.failure', (array('Not safe:')));
333  return null;
334  }
335  }
336  self::setForce(false); // resets $force back to false
337  $xoopsPreload->triggerEvent('core.database.query.start');
338  try {
339  $result = call_user_func_array(array('parent', 'query'), func_get_args());
340  } catch (\Exception $e) {
341  $xoopsPreload->triggerEvent('core.exception', $e);
342  $result=null;
343  }
344  $xoopsPreload->triggerEvent('core.database.query.end');
345  if ($result) {
346  //$xoopsPreload->triggerEvent('core.database.query.success', (array('')));
347  return $result;
348  } else {
349  //$xoopsPreload->triggerEvent('core.database.query.failure', (array('')));
350  return null;
351  }
352  }
353 
362  public function queryFromFile($file)
363  {
364  if (false !== ($fp = fopen($file, 'r'))) {
365  $sql_queries = trim(fread($fp, filesize($file)));
366  \SqlUtility::splitMySqlFile($pieces, $sql_queries);
367  foreach ($pieces as $query) {
368  $prefixed_query = \SqlUtility::prefixQuery(trim($query), $this->prefix());
369  if ($prefixed_query != false) {
370  $this->query($prefixed_query[0]);
371  }
372  }
373  return true;
374  }
375  return false;
376  }
377 
386  public function quoteSlash($input)
387  {
388  return $this->quote($input);
389  }
390 
391 
397  public function createXoopsQueryBuilder()
398  {
399  return new QueryBuilder($this);
400  }
401 }
__construct(array $params,\Doctrine\DBAL\Driver $driver,\Doctrine\DBAL\Configuration $config=null,\Doctrine\Common\EventManager $eventManager=null)
Definition: Connection.php:107
executeQuery($query, array $params=array(), $types=array(),\Doctrine\DBAL\Cache\QueryCacheProfile $qcp=null)
Definition: Connection.php:222
static prefixQuery($query, $prefix)
Definition: sqlutility.php:126
static getInstance()
Definition: Xoops.php:160
static setForce($force=false)
Definition: Connection.php:80
if(!isset($xoops->paths[$path_type])) if($path_type== 'var') $file
Definition: browse.php:55
$query
Definition: index.php:37
$result
Definition: pda.php:33
$_SERVER['REQUEST_URI']
deletePrefix($tableName, array $identifier)
Definition: Connection.php:201
static splitMySqlFile(&$ret, $sql)
Definition: sqlutility.php:43
executeUpdate($query, array $params=array(), array $types=array())
Definition: Connection.php:250
static get($name)
updatePrefix($tableName, array $data, array $identifier, array $types=array())
Definition: Connection.php:183
$sql
Definition: pda.php:32
static setSafe($safe=true)
Definition: Connection.php:56
insertPrefix($tableName, array $data, array $types=array())
Definition: Connection.php:162
static prefix($tablename= '')
Definition: Connection.php:139
$config
Definition: Xoops.php:43