forked from BitweaverCMS/kernel
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBitBase.php
More file actions
400 lines (354 loc) · 12.1 KB
/
BitBase.php
File metadata and controls
400 lines (354 loc) · 12.1 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
<?php
/**
* Virtual bitweaver base class
*
* @package kernel
* @version $Header: /cvsroot/bitweaver/_bit_kernel/BitBase.php,v 1.48 2010/02/08 22:27:51 wjames5 Exp $
*
* Copyright (c) 2004 bitweaver.org
* All Rights Reserved. See below for details and a complete list of authors.
* Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See http://www.gnu.org/copyleft/lesser.html for details
*
* Virtual base class (as much as one can have such things in PHP) for all
* derived tikiwiki classes that require database access.
*
* created 2004/8/15
*
* @author spider <spider@steelsun.com>
*/
/**
* required setup
*/
require_once ( KERNEL_PKG_PATH.'BitDbBase.php' );
define( 'STORAGE_BINARY', 1 );
define( 'STORAGE_IMAGE', 2 );
/**
* Virtual base class (as much as one can have such things in PHP) for all
* derived bitweaver classes that require database access.
*
* @package kernel
*/
class BitBase {
/**
* Error hash that will contain an error codes we encounter along
* the way this hash can be used by presentation layer ti give feedback
* to the user.
* @todo not used yet
* @private
*/
var $mErrors;
/**
* Same idea as the error hash but this is for successful operations
* @private
*/
var $mSuccess;
/**
* String used to refer to preference caching and database table
* @private
*/
var $mName;
/**
* Used to store database mechanism
* @private
*/
var $mDb;
/**
* Used to store database type
* @private
*/
var $dType;
/**
* Standard Query Cache Time. Variable can be set to 0 to flush particular queries
* @private
*/
var $mCacheTime;
/**
* Data hash that represents this classes row(s) in the db
**/
var $mInfo = array();
/**
* Data hash that contains logging information relevant to database operations
**/
var $mLogs = array();
/**
* During initialisation, we assign a name which is used by the class.
* @param pName a unique identified used in caching and database
* mechanisms
**/
function BitBase( $pName = '' ) {
global $gBitDb;
$this->mName = $pName;
$this->mCacheTime = BIT_QUERY_CACHE_TIME;
if( is_object( $gBitDb ) ) {
$this->setDatabase($gBitDb);
}
$this->mErrors = array();
$this->mSuccess = array();
$this->mInfo = array();
}
/**
* Sets database mechanism for the instance
* @param pDB the instance of the database mechanism
**/
function setDatabase( &$pDB ) {
// set internal db and retrieve values
$this->mDb = &$pDB;
$this->dType = $this->mDb->mType;
}
/**
* Determines if there is a valide database connection
**/
function isDatabaseValid() {
return( !empty( $this->mDb ) && $this->mDb->isValid() );
}
/**
* Return pointer to current Database
**/
function getDb() {
return ( !empty( $this->mDb ) ? $this->mDb : NULL );
}
/**
* Switch debug level in database
*
**/
function debug( $pLevel = 99 ) {
if( is_object( $this->mDb ) ) {
$this->mDb->debug( $pLevel );
}
}
// =-=-=-=-=-=-=-=-=-=-=- Non-DB related functions =-=-=-=-=-=-=-=-=-=-=-=-=
/**
* verifyId Determines if any given variable exists and is a number
*
* @param mixed $pId this can be a string, number or array. if it's an array, all values in the array will be checked to see if they are numeric
* @access public
* @return TRUE if the input was numeric, FALSE if it wasn't
*/
function verifyId( $pId ) {
if( empty( $pId )) {
return FALSE;
}
if( is_array( $pId )) {
foreach( $pId as $id ) {
if( !is_numeric( $id )) {
return FALSE;
}
}
return TRUE;
}
return( is_numeric( $pId ));
}
/**
* Returns entry from the mInfo hash if field exists
* @param pFieldName the instance of the database mechanism
**/
function getField( $pFieldName, $pDefault = NULL ) {
return( !empty( $this->mInfo[$pFieldName] ) ? $this->mInfo[$pFieldName] : $pDefault );
}
/**
* Prepares parameters with default values for any getList function
* @param pParamHash hash of parameters for any getList() function
* @return the link to display the page.
*/
function prepGetList( &$pListHash ) {
global $gBitSmarty, $gBitSystem;
// if sort_mode is not set then use last_modified_desc
if( empty( $pListHash['sort_mode'] )) {
if( empty( $_REQUEST["sort_mode"] )) {
$pListHash['sort_mode'] = 'last_modified_desc';
} else {
$pListHash['sort_mode'] = $_REQUEST['sort_mode'];
}
}
// valid_sort_modes are set, we check them against our selected sort_mode
if( !empty( $pListHash['sort_mode'] ) && !empty( $pListHash['valid_sort_modes'] ) && is_array( $pListHash['valid_sort_modes'] )) {
if( is_string( $pListHash['sort_mode'] )) {
if( !$this->verifySortMode( $pListHash['sort_mode'], $pListHash['valid_sort_modes'] )) {
$pListHash['sort_mode'] = '';
}
} elseif( is_array( $pListHash['sort_mode'] )) {
// make sure all values of the sort_mode array match something in the valid valid_sort_modes hash
foreach( $pListHash['sort_mode'] as $key => $mode ) {
if( !$this->verifySortMode( $mode, $pListHash['valid_sort_modes'] )) {
unset( $pListHash['sort_mode'][$key] );
}
}
}
}
if( empty( $pListHash['max_records'] )) {
global $gBitSystem;
$pListHash['max_records'] = $gBitSystem->getConfig( "max_records", 10 );
}
if( !isset( $pListHash['offset'] )) {
if( isset($pListHash['page'] )) {
$pListHash['offset'] = ($pListHash['page'] - 1) * $pListHash['max_records'];
} else {
if( isset( $_REQUEST["offset"] )) {
$pListHash['offset'] = $_REQUEST['offset'];
} elseif( isset( $_REQUEST['page'] ) && is_numeric( $_REQUEST['page'] ) && $_REQUEST['page'] > 0 ) {
$pListHash['offset'] = ($_REQUEST['page'] - 1) * $pListHash['max_records'];
} elseif( isset( $_REQUEST['list_page'] ) && is_numeric( $_REQUEST['list_page'] ) && $_REQUEST['list_page'] > 0 ) {
$pListHash['offset'] = ( $_REQUEST['list_page'] - 1 ) * $pListHash['max_records'];
} else {
$pListHash['offset'] = 0;
}
}
}
// migrate towards a safer hash key
if( empty( $pListHash['user_id'] ) && !empty( $pListHash['lookup_user_id'] ) ) {
$pListHash['user_id'] = $pListHash['lookup_user_id'];
}
// Don't use $_REQUEST["find"] as it can really screw with modules on search pages
if( !empty( $pListHash["find"] )) {
$pListHash['find']= $pListHash["find"];
} else {
$pListHash['find'] = NULL;
}
$gBitSmarty->assign( 'find', $pListHash['find'] );
if( isset( $_REQUEST['date'] )) {
$pListHash['date']= $_REQUEST['date'];
} else {
$pListHash['date'] = $gBitSystem->getUTCTime();
}
if( empty( $pListHash['load_comments'] )) {
$pListHash['load_comments'] = FALSE;
}
if( empty( $pListHash['load_num_comments'] )) {
$pListHash['load_num_comments'] = FALSE;
}
if( empty( $pListHash['parse_data'] )) {
$pListHash['parse_data'] = FALSE;
}
}
/**
* verifySortMode is used to validate a given sort_mode agains an array of valid sort modes
*
* @param string $pSortMode sort mode to check
* @param array $pValidSortModes array of available sort modes
* @access public
* @return TRUE on success, FALSE on failure
*/
function verifySortMode( $pSortMode, $pValidSortModes ) {
if( !empty( $pSortMode ) && is_string( $pSortMode ) && !empty( $pValidSortModes ) && is_array( $pValidSortModes )) {
foreach( $pValidSortModes as $mode ) {
// we will not check the table - that would just be too complicated...
if( preg_match( "/^(\w+\.)?{$mode}_(desc|asc)$/", $pSortMode )) {
return TRUE;
}
}
}
return FALSE;
}
/**
* Updates results from any getList function to provide the control set
* displaying in the smarty template
* @param array hash of parameters returned by any getList() function
* @return - none the hash is updated via the reference
*/
function postGetList( &$pListHash ) {
global $gBitSystem;
$pListHash['listInfo']['total_records'] = $pListHash["cant"];
$pListHash['listInfo']['total_pages'] = ceil( $pListHash["cant"] / $pListHash['max_records'] );
$pListHash['listInfo']['current_page'] = 1 + ( $pListHash['offset'] / $pListHash['max_records'] );
if( $pListHash["cant"] > ( $pListHash['offset'] + $pListHash['max_records'] ) ) {
$pListHash['listInfo']['next_offset'] = $pListHash['offset'] + $pListHash['max_records'];
} else {
$pListHash['listInfo']['next_offset'] = -1;
}
// If offset is > 0 then prev_offset
if( $pListHash['offset'] > 0 ) {
$pListHash['listInfo']['prev_offset'] = $pListHash['offset'] - $pListHash['max_records'];
} else {
$pListHash['listInfo']['prev_offset'] = -1;
}
$pListHash['listInfo']['offset'] = $pListHash['offset'];
$pListHash['listInfo']['find'] = $pListHash['find'];
$pListHash['listInfo']['sort_mode'] = $pListHash['sort_mode'];
$pListHash['listInfo']['max_records'] = $pListHash['max_records'];
// calculate what links to show
if( $gBitSystem->isFeatureActive( 'site_direct_pagination' ) ) {
// number of continuous links to display on either side
$continuous = 5;
// number of skipping links to display on either side
$skipping = 5;
// size of steps to take when skipping
// if you have more than 1000 pages, you should consider not using the pagination form
if( $pListHash['listInfo']['total_pages'] < 50 ) {
$step = 5;
} elseif( $pListHash['listInfo']['total_pages'] < 100 ) {
$step = 10;
} elseif( $pListHash['listInfo']['total_pages'] < 250 ) {
$step = 25;
} elseif( $pListHash['listInfo']['total_pages'] < 500 ) {
$step = 50;
} else {
$step = 100;
}
$prev = ( $pListHash['listInfo']['current_page'] - $continuous > 0 ) ? $pListHash['listInfo']['current_page'] - $continuous : 1;
$next = ( $pListHash['listInfo']['current_page'] + $continuous < $pListHash['listInfo']['total_pages'] ) ? $pListHash['listInfo']['current_page'] + $continuous : $pListHash['listInfo']['total_pages'];
for( $i = $pListHash['listInfo']['current_page'] - 1; $i >= $prev; $i -= 1 ) {
$pListHash['listInfo']['block']['prev'][$i] = $i;
}
if( $prev != 1 ) {
// replace the last of the continuous links with a ...
$pListHash['listInfo']['block']['prev'][$i + 1] = "…";
// add $skipping links to pages separated by $step pages
if( ( $min = $pListHash['listInfo']['current_page'] - $continuous - ( $step * $skipping ) ) < 0 ) {
$min = 0;
}
for( $j = ( floor( $i / $step ) * $step ); $j > $min; $j -= $step ) {
$pListHash['listInfo']['block']['prev'][$j] = $j;
}
$pListHash['listInfo']['block']['prev'][1] = 1;
}
// reverse array that links are in the correct order
if( !empty( $pListHash['listInfo']['block']['prev'] ) ) {
$pListHash['listInfo']['block']['prev'] = array_reverse( $pListHash['listInfo']['block']['prev'], TRUE );
}
// here we start adding next links
for( $i = $pListHash['listInfo']['current_page'] + 1; $i <= $next; $i += 1 ) {
$pListHash['listInfo']['block']['next'][$i] = $i;
}
if( $next != $pListHash['listInfo']['total_pages'] ) {
// replace the last of the continuous links with a ...
$pListHash['listInfo']['block']['next'][$i - 1] = "…";
// add $skipping links to pages separated by $step pages
if( ( $max = $pListHash['listInfo']['current_page'] + $continuous + ( $step * $skipping ) ) > $pListHash['listInfo']['total_pages'] ) {
$max = $pListHash['listInfo']['total_pages'];
}
for( $j = ( ceil( $i / $step ) * $step ); $j < $max; $j += $step ) {
$pListHash['listInfo']['block']['next'][$j] = $j;
}
$pListHash['listInfo']['block']['next'][$pListHash['listInfo']['total_pages']] = $pListHash['listInfo']['total_pages'];
}
}
}
function getErrors(){
return $this->mErrors;
}
function getSuccess(){
return $this->mSuccess;
}
function getErrorValue( $pErrorKey ){
if( !empty( $this->mErrors[$pErrorKey] ) ){
return $this->mErrors[$pErrorKey];
}
return NULL;
}
function getSuccessValue( $pSuccessKey ){
if( !empty( $this->mSuccesss[$pSuccessKey] ) ){
return $this->mSuccesss[$pSuccessKey];
}
return NULL;
}
function setError( $pErrorKey, $pValue ){
$this->mErrors[$pErrorKey] = $pValue;
}
function setSuccess( $pSuccessKey, $pValue ){
$this->mSuccess[$pSuccessKey] = $pValue;
}
function resetErrors(){
$this->mErrors = array();
}
}
?>