Overview

Packages

  • Beatnik
  • None

Classes

  • Autogenerate
  • Beatnik
  • Beatnik_Driver
  • Beatnik_Driver_ldap2dns
  • Beatnik_Driver_pdnsgsql
  • Beatnik_Driver_sql
  • DeleteRecord
  • EditRecord
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * Copyright 2006-2012 Horde LLC (http://www.horde.org/)
  4:  *
  5:  * See the enclosed file COPYING for license information (GPL). If you
  6:  * did not receive this file, see http://www.horde.org/licenses/gpl.
  7:  *
  8:  * @author Duck <duck@obala.net>
  9:  * @package Beatnik
 10:  */
 11: 
 12: class Beatnik_Driver_sql extends Beatnik_Driver
 13: {
 14:     /**
 15:      * Hash containing connection parameters.
 16:      *
 17:      * @var array
 18:      */
 19:     var $_params = array();
 20: 
 21:     /**
 22:      * Handle for the current database connection.
 23:      *
 24:      * @var DB
 25:      */
 26:     var $_db;
 27: 
 28:     /**
 29:      * Handle for the current database connection, used for writing. Defaults
 30:      * to the same handle as $_db if a separate write database is not required.
 31:      *
 32:      * @var DB
 33:      */
 34:     var $_write_db;
 35: 
 36:     /**
 37:      * Boolean indicating whether or not we're connected to the SQL server.
 38:      *
 39:      * @var boolean
 40:      */
 41:     var $_connected = false;
 42: 
 43:     /**
 44:     * Constructs a new Beatnik DB driver object.
 45:     *
 46:     * @param array  $params    A hash containing connection parameters.
 47:     */
 48:     function Beatnik_Driver_sql($params = array())
 49:     {
 50:         parent::Beatnik_Driver($params);
 51:         $this->_connect();
 52:     }
 53: 
 54:     /**
 55:      * Get any record types  available specifically in this driver.
 56:      *
 57:      * @return array Records available only to this driver
 58:      */
 59:     function getRecDriverTypes()
 60:     {
 61:         return array();
 62:     }
 63: 
 64: 
 65:     /**
 66:      * Get any fields available specifically in this driver by record type.
 67:      *
 68:      * @param string $type Record type for which fields should be returned
 69:      *
 70:      * @return array Fields specific to this driver
 71:      */
 72:     function getRecDriverFields($type) {
 73: 
 74:         return array();
 75:     }
 76: 
 77:     /**
 78:      * Gets all zones
 79:      *
 80:      * @access private
 81:      *
 82:      * @return array Array with zone records numerically indexed
 83:      */
 84:     function _getDomains()
 85:     {
 86:         $query = 'SELECT * FROM beatnik_soa ORDER BY zonename';
 87:         return $this->_db->getAll($query, null, DB_FETCHMODE_ASSOC);
 88:     }
 89: 
 90:     /**
 91:      * Return SOA for a single domain
 92:      *
 93:      * @param string $domain   Domain for which to return SOA information
 94:      *
 95:      * @return array           Domain SOA
 96:      */
 97:     function getDomain($domainname)
 98:     {
 99:         $query = 'SELECT * FROM beatnik_soa WHERE zonename = ? ORDER BY zonename';
100:         return $this->_db->getRow($query, array($domainname), DB_FETCHMODE_ASSOC);
101:     }
102: 
103:     /**
104:      * Gets all records associated with the given zone
105:      *
106:      * @param string $domain Retrieve records for this domain
107:      *
108:      * @return array Array with zone records
109:      */
110:     function getRecords($domain)
111:     {
112:         $zonedata = array();
113:         $params = array($domain);
114: 
115:         foreach (array_keys(Beatnik::getRecTypes()) as $type) {
116:             if ($type == 'soa') {
117:                 continue;
118:             }
119:             if ($type == 'mx') {
120:                 $order = 'pointer';
121:             } else {
122:                 $order = 'hostname';
123:             }
124: 
125:             $query = 'SELECT * FROM beatnik_' . $type . ' WHERE zonename = ? ORDER BY ' .  $order . ' ASC';
126:             $result = $this->_db->getAll($query, $params, DB_FETCHMODE_ASSOC);
127:             if (is_a($result, 'PEAR_Error') || empty($result)) {
128:                 continue;
129:             }
130: 
131:             $zonedata[$type] = $result;
132:         }
133: 
134:         return $zonedata;
135:     }
136: 
137:     /**
138:      * Saves a new or edited record to the DNS backend
139:      *
140:      * @access private
141:      *
142:      * @param array $info Array of record data
143:      *
144:      * @return boolean true on success
145:      */
146:     function _saveRecord($info)
147:     {
148:         $fields = array_keys(Beatnik::getRecFields($info['rectype']));
149:         $params = array();
150:         foreach ($fields as $i => $key) {
151:             if (!isset($info[$key])) {
152:                 unset($fields[$i]);
153:                 continue;
154:             }
155:             $params[$key] = $info[$key];
156:         }
157: 
158:         if (isset($params['id']) && $params['id']) {
159:             unset($params['id'], $fields[0]);
160:             $query = 'UPDATE beatnik_' . $info['rectype'] . ' SET ';
161:             foreach ($fields as $key) {
162:                 $query .= $key . ' = ?, ';
163:                 $params[$key] = $info[$key];
164:             }
165:             $query = substr($query, 0, -2) . ' WHERE id = ?';
166:             $params['id'] = $info['id'];
167:         } else {
168:             unset($params['id'], $fields[0]);
169:             if ($info['rectype'] != 'soa') {
170:                 $fields[] = 'zonename';
171:                 $params['zonename'] =  $_SESSION['beatnik']['curdomain']['zonename'];
172:             }
173:             $query = 'INSERT INTO beatnik_' . $info['rectype'] . ' (' . implode(', ', $fields) . ') ' . 
174:                      ' VALUES (' . substr(str_repeat('?, ', sizeof($params)), 0, -2) . ')';
175:         }
176: 
177:         return $this->_write_db->query($query, $params);
178:     }
179: 
180:     /**
181:      * Delete record from backend
182:      *
183:      * @access private
184:      *
185:      * @param array $data  Reference to array of record data to be deleted
186:      *
187:      * @return boolean true on success, PEAR::Error on error
188:      */
189:     function _deleteRecord($data)
190:     {
191:         // delete just one record
192:         if ($data['rectype'] != 'soa') {
193:             return $this->_write_db->query('DELETE FROM beatnik_' . $data['rectype'] . ' WHERE id = ?', array($data['id']));
194:         }
195: 
196:         // delete all subrecords
197:         $params = array($data['curdomain']);
198:         foreach (array_keys(Beatnik::getRecTypes()) as $type) {
199:             if ($type == 'soa') {
200:                 continue;
201:             }
202:             $result = $this->_write_db->query('DELETE FROM beatnik_' . $type . ' WHERE zonename = ?', $params);
203:             if (is_a($result, 'PEAR_Error')) {
204:                 return $result;
205:             }
206:         }
207: 
208:         // we are cuccesfull so, delete even soa
209:         return $this->_write_db->query('DELETE FROM beatnik_soa WHERE zonename = ?', $params);
210:     }
211: 
212:     /**
213:      * Attempts to open a persistent connection to the SQL server.
214:      *
215:      * @access private
216:      *
217:      * @return boolean  True on success.
218:      */
219:     function _connect()
220:     {
221:         if ($this->_connected) {
222:             return true;
223:         }
224: 
225:         $this->_db = $GLOBALS['injector']->getInstance('Horde_Core_Factory_DbPear')->create('read', 'beatnik', 'storage');
226:         $this->_write_db = $GLOBALS['injector']->getInstance('Horde_Core_Factory_DbPear')->create('rw', 'beatnik', 'storage');
227: 
228:         return true;
229:     }
230: }
231: 
API documentation generated by ApiGen