~lss-team/lilsoftstats/trunk

« back to all changes in this revision

Viewing changes to inc/class.mysql.php

  • Committer: Nick
  • Date: 2011-11-14 04:10:28 UTC
  • Revision ID: nick@little-apps.org-20111114041028-cvmpwq6z6hx3pkya
first commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/**
 
3
 * Little Software Stats
 
4
 *
 
5
 * An open source program that allows developers to keep track of how their software is being used
 
6
 *
 
7
 * @package             Little Software Stats
 
8
 * @author              Little Apps
 
9
 * @copyright           Copyright (c) 2011, Little Apps
 
10
 * @license             http://www.gnu.org/licenses/gpl.html GNU General Public License v3
 
11
 * @link                http://little-apps.org
 
12
 * @since               Version 0.1
 
13
 * @filesource
 
14
 */
 
15
 
 
16
/**
 
17
 * PHP MySQL Class
 
18
 * Manage connection to MySQL database
 
19
 * 
 
20
 * @package Little Software Stats
 
21
 * @author Ed Rackham
 
22
 * @link http://github.com/a1phanumeric/PHP-MySQL-Class
 
23
 */
 
24
class MySQL {
 
25
        // Base variables
 
26
        public $sLastError;                             // Holds the last error
 
27
        public $sLastQuery;                             // Holds the last query
 
28
        public $aResult;                                        // Holds the MySQL query result
 
29
        public $iRecords;                                       // Holds the total number of records returned
 
30
        public $iAffected;                                      // Holds the total number of records affected
 
31
        public $aRawResults;                            // Holds raw 'arrayed' results
 
32
        public $aArrayedResult;                 // Holds a single 'arrayed' result
 
33
        public $aArrayedResults;                        // Holds multiple 'arrayed' results (usually with a set key)
 
34
        
 
35
        private $sHostname = MYSQL_HOST;        // MySQL Hostname
 
36
        private $sUsername = MYSQL_USER;        // MySQL Username
 
37
        private $sPassword = MYSQL_PASS;        // MySQL Password
 
38
        private $sDatabase = MYSQL_DB;          // MySQL Database
 
39
        private $sPrefix = MYSQL_PREFIX;        // MySQL Prefix
 
40
        
 
41
        private $sDBLink;                       // Database Connection Link
 
42
        
 
43
        /**
 
44
         * Class Constructor
 
45
         * Assigning values to variables
 
46
         */
 
47
        function __construct() {
 
48
            $this->Connect();
 
49
        }
 
50
 
 
51
        /**
 
52
         * Connects class to database
 
53
         * @access private
 
54
         * @param bool $bPersistant Use persistant connection?
 
55
         * @return bool Returns true if connection has been made 
 
56
         */
 
57
        private function Connect($bPersistant = false){
 
58
                if($this->sDBLink){
 
59
                        mysql_close($this->sDBLink);
 
60
                }
 
61
                
 
62
                if($bPersistant){
 
63
                        $this->sDBLink = mysql_pconnect($this->sHostname, $this->sUsername, $this->sPassword);
 
64
                }else{
 
65
                        $this->sDBLink = mysql_connect($this->sHostname, $this->sUsername, $this->sPassword);
 
66
                }
 
67
                
 
68
                if (!$this->sDBLink){
 
69
                        $this->sLastError = 'Could not connect to server: ' . mysql_error($this->sDBLink);
 
70
                        return false;
 
71
                }
 
72
                
 
73
                if(!$this->UseDB()){
 
74
                        $this->sLastError = 'Could not connect to database: ' . mysql_error($this->sDBLink);
 
75
                        return false;
 
76
                }
 
77
                return true;
 
78
        }
 
79
 
 
80
        /**
 
81
         * Select database to use
 
82
         * @access private
 
83
         * @return bool Returns true if database was selected 
 
84
         */
 
85
        private function UseDB(){
 
86
                if (!mysql_select_db($this->sDatabase, $this->sDBLink)) {
 
87
                        $this->sLastError ='Cannot select database: ' . mysql_error($this->sDBLink);
 
88
                        return false;
 
89
                }else{
 
90
                        return true;
 
91
                }
 
92
        }
 
93
        
 
94
        /**
 
95
         * Executes MySQL query
 
96
         * @access public
 
97
         * @param string $sSQLQuery Query to execute
 
98
         * @return bool Returns true if execution was successful 
 
99
         */
 
100
        public function ExecuteSQL($sSQLQuery){
 
101
                $this->sLastQuery       = $sSQLQuery;
 
102
                if($this->aResult               = mysql_query($sSQLQuery, $this->sDBLink)){
 
103
                        $this->iRecords         = @mysql_num_rows($this->aResult);
 
104
                        $this->iAffected        = @mysql_affected_rows($this->sDBLink);
 
105
                        return true;
 
106
                }else{
 
107
                        $this->sLastError = mysql_error($this->sDBLink);
 
108
                        return false;
 
109
                }
 
110
        }
 
111
        
 
112
        /**
 
113
         * Adds a record to the database based on the array key names
 
114
         * @access public
 
115
         * @param array $aVars Variables to insert
 
116
         * @param type $sTable Table to insert variables into
 
117
         * @param array $aExclude Column(s) to exclude
 
118
         * @return bool Returns true if insert was successful 
 
119
         */
 
120
        public function Insert($aVars, $sTable, $aExclude = ''){
 
121
                // Catch Exceptions
 
122
                if($aExclude == ''){
 
123
                        $aExclude = array();
 
124
                }
 
125
                
 
126
                array_push($aExclude, 'MAX_FILE_SIZE');
 
127
                
 
128
                // Prepare Variables
 
129
                $aVars = $this->SecureData($aVars);
 
130
                
 
131
                $sSQLQuery = 'INSERT INTO `' . $sPrefix . $sTable . '` SET ';
 
132
                foreach($aVars as $iKey=>$sValue){
 
133
                        if(in_array($iKey, $aExclude)){
 
134
                                continue;
 
135
                        }
 
136
                        $sSQLQuery .= '`' . $iKey . '` = "' . $sValue . '", ';
 
137
                }
 
138
                
 
139
                $sSQLQuery = substr($sSQLQuery, 0, -2);
 
140
                
 
141
                if($this->ExecuteSQL($sSQLQuery)){
 
142
                        return true;
 
143
                }else{
 
144
                        return false;
 
145
                }
 
146
        }
 
147
        
 
148
        /**
 
149
         * Deletes a record from the database
 
150
         * @access public
 
151
         * @param string $sTable Table to delete from
 
152
         * @param array $aWhere Column(s) to search for
 
153
         * @param string $sLimit Limit of how many records
 
154
         * @param bool $bLike Use like to search
 
155
         * @return bool Returns true if delete was successful 
 
156
         */
 
157
        public function Delete($sTable, $aWhere='', $sLimit='', $bLike=false){
 
158
                $sSQLQuery = 'DELETE FROM `' . $sPrefix . $sTable . '` WHERE ';
 
159
                if(is_array($aWhere) && $aWhere != ''){
 
160
                        // Prepare Variables
 
161
                        $aWhere = $this->SecureData($aWhere);
 
162
                        
 
163
                        foreach($aWhere as $iKey=>$sValue){
 
164
                                if($bLike){
 
165
                                        $sSQLQuery .= '`' . $iKey . '` LIKE "%' . $sValue . '%" AND ';
 
166
                                }else{
 
167
                                        $sSQLQuery .= '`' . $iKey . '` = "' . $sValue . '" AND ';
 
168
                                }
 
169
                        }
 
170
                        
 
171
                        $sSQLQuery = substr($sSQLQuery, 0, -5);
 
172
                }
 
173
                
 
174
                if($sLimit != ''){
 
175
                        $sSQLQuery .= ' LIMIT ' .$sLimit;
 
176
                }
 
177
                
 
178
                if($this->ExecuteSQL($sSQLQuery)){
 
179
                        return true;
 
180
                }else{
 
181
                        return false;
 
182
                }
 
183
        }
 
184
        
 
185
        /**
 
186
         * Selects row(s) from table
 
187
         * @access public
 
188
         * @param string $sFrom Table to select from
 
189
         * @param array $aWhere Column(s) to search for
 
190
         * @param string $sOrderBy Column to order by
 
191
         * @param string $sLimit Number of rows that can be returned
 
192
         * @param bool $bLike Use 'LIKE' instead of '='
 
193
         * @param string $sOperand Operand to use ('AND' or 'OR')
 
194
         * @return bool Returns true if select was successful
 
195
         */
 
196
        public function Select($sFrom, $aWhere='', $sOrderBy='', $sLimit='', $bLike=false, $sOperand='AND'){
 
197
                // Catch Exceptions
 
198
                if(trim($sFrom) == ''){
 
199
                        return false;
 
200
                }
 
201
                
 
202
                $sSQLQuery = 'SELECT * FROM `' . $sPrefix . $sFrom . '` WHERE ';
 
203
                
 
204
                if(is_array($aWhere) && $aWhere != ''){
 
205
                        // Prepare Variables
 
206
                        $aWhere = $this->SecureData($aWhere);
 
207
                        
 
208
                        foreach($aWhere as $iKey=>$sValue){
 
209
                                if($bLike){
 
210
                                        $sSQLQuery .= '`' . $iKey . '` LIKE "%' . $sValue . '%" ' . $sOperand . ' ';
 
211
                                }else{
 
212
                                        $sSQLQuery .= '`' . $iKey . '` = "' . $sValue . '" ' . $sOperand . ' ';
 
213
                                }
 
214
                        }
 
215
                        
 
216
                        $sSQLQuery = substr($sSQLQuery, 0, -5);
 
217
 
 
218
                }else{
 
219
                        $sSQLQuery = substr($sSQLQuery, 0, -7);
 
220
                }
 
221
                
 
222
                if($sOrderBy != ''){
 
223
                        $sSQLQuery .= ' ORDER BY ' .$sOrderBy;
 
224
                }
 
225
                
 
226
                if($sLimit != ''){
 
227
                        $sSQLQuery .= ' LIMIT ' .$sLimit;
 
228
                }
 
229
                
 
230
                if($this->ExecuteSQL($sSQLQuery)){
 
231
                        if($this->iRecords == 1) 
 
232
                                $this->ArrayResult();
 
233
                        else if($this->iRecords > 1)
 
234
                                $this->ArrayResults();
 
235
                        return true;
 
236
                }else{
 
237
                        return false;
 
238
                }
 
239
                
 
240
        }
 
241
        
 
242
        
 
243
        /**
 
244
         * Updates a record in the database based on WHERE
 
245
         * @access public
 
246
         * @param string $sTable Table to update
 
247
         * @param array $aSet Variables to update
 
248
         * @param array $aWhere Column(s) to search for
 
249
         * @param array $aExclude Column(s) to exclude
 
250
         * @return bool Returns true if update was successful 
 
251
         */
 
252
        public function Update($sTable, $aSet, $aWhere, $aExclude = ''){
 
253
                // Catch Exceptions
 
254
                if(trim($sTable) == '' || !is_array($aSet) || !is_array($aWhere)){
 
255
                        return false;
 
256
                }
 
257
                if($aExclude == ''){
 
258
                        $aExclude = array();
 
259
                }
 
260
                
 
261
                array_push($aExclude, 'MAX_FILE_SIZE');
 
262
                
 
263
                $aSet   = $this->SecureData($aSet);
 
264
                $aWhere = $this->SecureData($aWhere);
 
265
                
 
266
                // SET
 
267
                
 
268
                $sSQLQuery = 'UPDATE `' . $sPrefix . $sTable . '` SET ';
 
269
                
 
270
                foreach($aSet as $iKey=>$sValue){
 
271
                        if(in_array($iKey, $aExclude)){
 
272
                                continue;
 
273
                        }
 
274
                        $sSQLQuery .= '`' . $iKey . '` = "' . $sValue . '", ';
 
275
                }
 
276
                
 
277
                $sSQLQuery = substr($sSQLQuery, 0, -2);
 
278
                
 
279
                // WHERE
 
280
                
 
281
                $sSQLQuery .= ' WHERE ';
 
282
                
 
283
                foreach($aWhere as $iKey=>$sValue){
 
284
                        $sSQLQuery .= '`' . $iKey . '` = "' . $sValue . '" AND ';
 
285
                }
 
286
                
 
287
                $sSQLQuery = substr($sSQLQuery, 0, -5);
 
288
                
 
289
                if($this->ExecuteSQL($sSQLQuery)){
 
290
                        return true;
 
291
                }else{
 
292
                        return false;
 
293
                }
 
294
        }
 
295
        
 
296
        /**
 
297
         *'Arrays' a single result
 
298
         * @access public
 
299
         * @return array Returns a single row 
 
300
         */
 
301
        public function ArrayResult(){
 
302
                $this->aArrayedResult = mysql_fetch_assoc($this->aResult) or die (mysql_error($this->sDBLink));
 
303
                return $this->aArrayedResult;
 
304
        }
 
305
 
 
306
        /**
 
307
         * 'Arrays' multiple result
 
308
         * @access public
 
309
         * @return array Returns multiple rows
 
310
         */
 
311
        public function ArrayResults(){
 
312
                $this->aArrayedResults = array();
 
313
                while ($aData = mysql_fetch_assoc($this->aResult)){
 
314
                        $this->aArrayedResults[] = $aData;
 
315
                }
 
316
                return $this->aArrayedResults;
 
317
        }
 
318
        
 
319
        /**
 
320
         * Performs a 'mysql_real_escape_string' on the entire array/string
 
321
         * @access public
 
322
         * @param array|string $aData Data to prevent from SQL injection
 
323
         * @return array|string Returns SQL injection stripped data
 
324
         */
 
325
        public function SecureData($aData){
 
326
                if(is_array($aData)){
 
327
                        foreach($aData as $iKey=>$sVal){
 
328
                                if(!is_array($aData[$iKey])){
 
329
                                        $aData[$iKey] = mysql_real_escape_string($aData[$iKey], $this->sDBLink);
 
330
                                }
 
331
                        }
 
332
                }else{
 
333
                        $aData = mysql_real_escape_string($aData, $this->sDBLink);
 
334
                }
 
335
                return $aData;
 
336
        }
 
337
}
 
338
 
 
339
?>