1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
|
<?php
/**
* Implementation of the Quota API for servers keeping quota information in a
* custom SQL database.
*
* Driver must be configured in imp/config/servers.php. Parameters supported:
* <pre>
* phptype -- Database type to connect to
* hostspec -- Database host
* username -- User name for DB connection
* password -- Password for DB connection
* database -- Database name
* query_quota -- SQL query which returns single row/column with user quota
* (in bytes). ? is replaced with current user name.
* query_used -- SQL query which returns single row/column with user used
* space (in bytes). ? is replaced with current user name.
* </pre>
*
* Example how to reuse Horde's global SQL configuration:
* <code>
* 'quota' => array(
* 'driver' => 'sql',
* 'params' => array_merge($GLOBALS['conf']['sql'],
* array('query_quota' => 'SELECT quota FROM quotas WHERE user = ?',
* 'query_used' => 'SELECT used FROM quotas WHERE user = ?'))
* ),
* </code>
*
* $Horde: imp/lib/Quota/sql.php,v 1.6.2.2 2008/01/02 11:32:06 jan Exp $
*
* Copyright 2006-2007 Tomas Simonaitis <haden@homelan.lt>
* Copyright 2006-2008 The Horde Project (http://www.horde.org/)
*
* See the enclosed file COPYING for license information (GPL). If you
* did not receive this file, see http://www.fsf.org/copyleft/gpl.html.
*
* @author Tomas Simonaitis <haden@homelan.lt>
* @author Jan Schneider <jan@horde.org>
* @since IMP 4.2
* @package IMP_Quota
*/
class IMP_Quota_sql extends IMP_Quota {
/**
* SQL connection object.
*
* @var DB
*/
var $_db;
/**
* State of SQL connection.
*
* @var boolean
*/
var $_connected = false;
/**
* Constructor.
*
* @param array $params Hash containing connection parameters.
*/
function IMP_Quota_sql($params = array())
{
$this->_params = $params;
}
/**
* Connects to the database
*
* @return boolean True on success, PEAR_Error on failure.
*/
function _connect()
{
if (!$this->_connected) {
require_once 'DB.php';
$this->_db = &DB::connect($this->_params);
if (is_a($this->_db, 'PEAR_Error')) {
return PEAR::raiseError(_("Unable to connect to SQL server."));
}
$this->_connected = true;
}
return true;
}
/**
* Returns quota information.
*
* @return mixed An associative array:
* 'limit' => Maximum quota allowed (in bytes),
* 'usage' => Currently used space (in bytes).
* PEAR_Error on failure.
*/
function getQuota()
{
$conn = $this->_connect();
if (is_a($conn, 'PEAR_Error')) {
return $conn;
}
$user = array($_SESSION['imp']['user']);
$quota = array('limit' => 0, 'usage' => 0);
if (!empty($this->_params['query_quota'])) {
$result = $this->_db->query($this->_params['query_quota'], $user);
if (is_a($result, 'PEAR_Error')) {
return $result;
}
$row = $result->fetchRow(DB_FETCHMODE_ASSOC);
if (is_array($row)) {
$quota['limit'] = current($row);
}
} else {
Horde::logMessage('IMP_Quota_sql: query_quota SQL query not set', __FILE__, __LINE__, PEAR_LOG_DEBUG);
}
if (!empty($this->_params['query_used'])) {
$result = $this->_db->query($this->_params['query_used'], $user);
if (is_a($result, 'PEAR_Error')) {
return $result;
}
$row = $result->fetchRow(DB_FETCHMODE_ASSOC);
if (is_array($row)) {
$quota['usage'] = current($row);
}
} else {
Horde::logMessage('IMP_Quota_sql: query_used SQL query not set', __FILE__, __LINE__, PEAR_LOG_DEBUG);
}
return $quota;
}
}
|