forked from pollen8/fabrik
-
Notifications
You must be signed in to change notification settings - Fork 381
/
connection.php
569 lines (521 loc) · 14.5 KB
/
connection.php
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
<?php
/**
* Fabrik Connection Model
*
* @package Joomla
* @subpackage Fabrik
* @copyright Copyright (C) 2005 Fabrik. All rights reserved.
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.php
*/
// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die();
jimport('joomla.application.component.model');
/**
* Fabrik Connection Model
*
* @package Joomla
* @subpackage Fabrik
* @since 3.0
*/
class FabrikFEModelConnection extends JModelLegacy
{
/**
* Current connection
*
* @var JTable
*/
protected $connection = null;
/**
* Default connection table
*
* @var JTable
*/
protected $defaultConnection = null;
/**
* Containing db connections
*
* @var array
*/
protected static $dbs = array();
/**
* Connection id
*
* @var int
*/
protected $id = null;
/**
* Constructor
*/
public function __construct()
{
parent::__construct();
}
/**
* Method to set the element id
*
* @param int $id element ID number
*
* @return null
*/
public function setId($id)
{
// Set new element ID
$this->id = $id;
}
/**
* Is the conenction table the default connection
*
* @deprecated - dont think its used
*
* @return bool
*/
public function isDefault()
{
return $this->getConnection()->default;
}
/**
* Creates a html dropdown box for the current connection
*
* @param string $javascript to add to select box
* @param string $name of dropdown box
* @param string $selected the selected element in the list
* @param string $class input class name
*
* @deprecated - don't think its used
*
* @return string
*/
public function getTableDdForThisConnection($javascript = '', $name = 'table_join', $selected = '', $class = 'inputbox')
{
$tableOptions = array();
$cn = $this->getConnection();
if ($cn->host and $cn->published == '1')
{
if (@mysql_connect($cn->host, $cn->user, $cn->password))
{
// Ensure db files are included
jimport('joomla.database.database');
$options = $this->getConnectionOptions($cn);
$fabrikDb = JDatabase::getInstance($options);
$tables = $fabrikDb->getTableList();
$tableOptions[] = JHTML::_('select.option', '', '-');
if (is_array($tables))
{
foreach ($tables as $table)
{
$tableOptions[] = JHTML::_('select.option', $table, $table);
}
}
}
else
{
$tableOptions[] = JHTML::_('select.option', 'couldnt connect');
}
}
else
{
$tableOptions[] = JHTML::_('select.option', 'host not set');
}
$options = 'class="' . $class . '" size="1" id="' . $name . '" ' . $javascript;
return JHTML::_('select.genericlist', $tableOptions, $name, $options, 'value', 'text', $selected);
}
/**
* Decrypt once a connection password - if its params->encryptedPw option is true
*
* @param JTable &$cnn Connection
*
* @since 3.1rc1
*
* @return void
*/
protected function decryptPw(&$cnn)
{
if (isset($cnn->decrypted) && $cnn->decrypted)
{
return;
}
$crypt = FabrikWorker::getCrypt();
$params = json_decode($cnn->params);
if (is_object($params) && $params->encryptedPw == true)
{
$cnn->password = $crypt->decrypt($cnn->password);
$cnn->decrypted = true;
}
}
/**
* Get a connection table object
*
* @param int $id connection id
*
* @return object connection tables
*/
public function &getConnection($id = null)
{
if (!is_null($id))
{
$this->setId($id);
}
if (!is_object($this->connection))
{
$session = JFactory::getSession();
$key = 'fabrik.connection.' . $this->id;
if ($session->has($key))
{
$connProperties = unserialize($session->get($key));
// $$$ rob since J1.6 - connection properties stored as an array (in f2 it was an object)
if (is_a($connProperties, '__PHP_Incomplete_Class') || JArrayHelper::getValue($connProperties, 'id') == '')
{
$session->clear($key);
}
else
{
$this->connection = FabTable::getInstance('connection', 'FabrikTable');
$this->connection->bind($connProperties);
$this->decryptPw($this->connection);
return $this->connection;
}
}
if ($this->id == -1 || $this->id == '')
{
$this->connection = $this->loadDefaultConnection();
}
else
{
$this->connection = FabTable::getInstance('Connection', 'FabrikTable');
$this->connection->load($this->id);
}
// $$$ rob store the connection for later use as it may be required by modules/plugins
$session->set($key, serialize($this->connection->getProperties()));
}
$this->decryptPw($this->connection);
return $this->connection;
}
/**
* Load the connection associated with the table
*
* @return object database object using connection details false if connection error
*/
public function &getDb()
{
if (!isset(self::$dbs))
{
self::$dbs = array();
}
$cn = $this->getConnection();
$session = JFactory::getSession();
$app = JFactory::getApplication();
$input = $app->input;
if ($input->get('task') == 'test')
{
$session->clear('fabrik.connection.' . $cn->id);
self::$dbs = array();
$this->connection = null;
$cn = $this->getConnection();
}
if (!array_key_exists($cn->id, self::$dbs))
{
// $$$rob lets see if we have an exact config match with J db if so just return that
$conf = JFactory::getConfig();
$host = $conf->get('host');
$user = $conf->get('user');
$password = $conf->get('password');
$database = $conf->get('db');
$prefix = $conf->get('dbprefix');
$driver = $conf->get('dbtype');
$debug = $conf->get('debug');
$deafult_options = array('driver' => $driver, 'host' => $host, 'user' => $user, 'password' => $password, 'database' => $database,
'prefix' => $prefix);
$options = $this->getConnectionOptions($cn);
if ($this->compareConnectionOpts($deafult_options, $options))
{
self::$dbs[$cn->id] = FabrikWorker::getDbo();
}
else
{
self::$dbs[$cn->id] = JDatabase::getInstance($options);
}
if (is_a(self::$dbs[$cn->id], 'JException') || self::$dbs[$cn->id]->getErrorNum() !== 0)
{
/**
* $$$Rob - not sure why this is happening on badmintonrochelais.com (mySQL 4.0.24) but it seems like
* you can only use one connection on the site? As JDatabase::getInstance() forces a new connection if its options
* signature is not found, then fabrik's default connection won't be created, hence defaulting to that one
*/
if ($cn->default == 1 && $input->get('task') !== 'test')
{
self::$dbs[$cn->id] = FabrikWorker::getDbo();
// $$$rob remove the error from the error stack
// if we dont do this the form is not rendered
JError::getError(true);
}
else
{
$app = JFactory::getApplication();
if (!$app->isAdmin())
{
JError::raiseError(E_ERROR, 'Could not connection to database', $deafult_options);
jexit('Could not connection to database - possibly a menu item which doesn\'t link to a fabrik table');
}
else
{
// $$$ rob - unset the connection as caching it will mean that changes we make to the incorrect connection in admin, will not result
// in the test connection link informing the user that the changed connection properties are now correct
if ($input->get('task') == 'test')
{
$session->clear('fabrik.connection.' . $cn->id);
$this->connection = null;
$level = E_NOTICE;
}
else
{
$level = E_ERROR;
}
return JError::raise($level, 500, 'Could not connection to database cid = ' . $cn->id);
}
}
}
}
return self::$dbs[$cn->id];
}
/**
* Compare two arrays of connection details. Ignore prefix as this may be set to '' if using koowna
*
* @param array $opts1 first compare
* @param array $opts2 second compare
*
* @return bool
*/
private function compareConnectionOpts($opts1, $opts2)
{
return ($opts1['driver'] == $opts2['driver'] && $opts1['host'] == $opts2['host'] && $opts1['user'] == $opts2['user']
&& $opts1['password'] == $opts2['password'] && $opts1['database'] == $opts2['database']);
}
/**
* Get the options to connect to a db
*
* @param object &$cn connection table
*
* @return array connection options
*/
public function getConnectionOptions(&$cn)
{
$conf = JFactory::getConfig();
$host = $cn->host;
$user = $cn->user;
$password = $cn->password;
$database = $cn->database;
/**
* $$$ hugh - new version of Koowa no longer uses the empty dbprefix
* As used by new DOCman / EXTman
* http://fabrikar.com/forums/showthread.php?t=26898
* I've contacted the docman folk to see if they have any J! 2.5 product
* out there that still requires this workaround, but for now I'm just commenting
* it out, as we've had several folk htuting this while testing the new DOCman
/*
if (defined('KOOWA'))
{
$prefix = '';
}
else
{
$prefix = $conf->get('dbprefix');
}
*/
$prefix = $conf->get('dbprefix');
$driver = $conf->get('dbtype');
// Test for sawpping db table names
$driver .= '_fab';
$debug = $conf->get('debug');
$options = array('driver' => $driver, 'host' => $host, 'user' => $user, 'password' => $password, 'database' => $database, 'prefix' => $prefix);
return $options;
}
/**
* Gets object of connections
*
* @return array of connection tables id, description
*/
public function getConnections()
{
$db = FabrikWorker::getDbo();
$query = $db->getQuery(true);
$query->select('*, id AS value, description AS text')->from('#__fabrik_connections')->where('published = 1');
$db->setQuery($query);
$connections = $db->loadObjectList();
foreach ($connections as &$cnn)
{
$this->decryptPw($cnn);
}
return $connections;
}
/**
* Gets dropdown list of published connections
*
* @param object $connections connections stored in database
* @param string $javascript to run on change
* @param int $selected default value
* @param string $id element id
* @param string $name of connection drop down
* @param string $attribs dropdown properties
*
* @deprecated - can't see that its used
*
* @return string connection dropdown
*/
public function getConnectionsDd($connections, $javascript, $selected, $id = '', $name = 'connection_id', $attribs = 'class="inputbox" size="1" ')
{
if ($id == '')
{
$id = $name;
}
$cnns[] = JHTML::_('select.option', '-1', JText::_('COM_FABRIK_PLEASE_SELECT'));
$cnns = array_merge($cnns, $connections);
$attribs .= $javascript;
return JHTML::_('select.genericlist', $cnns, $name, $attribs, 'value', 'text', $selected, $id);
}
/**
* Queries all published connections and returns an multidimensional array
* of tables and fields for each connection
* WARNING: this is likely to
* exceed php script execution time if querying a larger remote database
*
* @param object $connections all available connections
*
* @return array
*/
public function getConnectionTableFields($connections)
{
$connectionTableFields = array();
$connectionTableFields[-1] = array();
$connectionTableFields[-1][] = JHTML::_('select.option', '-1', JText::_('COM_FABRIK_PLEASE_SELECT'));
foreach ($connections as $cn)
{
$connectionTableFields[$cn->value] = array();
if ($cn->host and $cn->published == '1')
{
$options = $this->getConnectionOptions($cn);
$fabrikDb = JDatabase::getInstance($options);
if (JError::isError($fabrikDb))
{
$connectionTableFields[$cn->value][$key] = "unable to connect to $cn->text<br />";
}
else
{
if ($fabrikDb->getErrorNum() == 0)
{
$tables = $fabrikDb->getTableList();
$fields = $fabrikDb->getTableColumns($tables);
$connectionTableFields[$cn->value][$key] = $fields;
}
else
{
$connectionTableFields[$cn->value][$key] = "unable to connect to $cn->text<br />";
}
}
}
}
return $connectionTableFields;
}
/**
* Queries all published connections and returns an multidimensional array
* of tables for each connection
*
* @param array $connections all available connections
*
* @return array
*/
public function getConnectionTables($connections)
{
$connectionTables = array();
$connectionTables[-1] = array();
$db = FabrikWorker::getDbo();
$connectionTables[-1][] = JHTML::_('select.option', '-1', JText::_('COM_FABRIK_PLEASE_SELECT'));
foreach ($connections as $cn)
{
$connectionTables[$cn->value] = array();
if ($cn->host and $cn->published == '1')
{
$this->connection = null;
$this->id = $cn->id;
$fabrikDb = $this->getDb();
if (JError::isError($fabrikDb))
{
$connectionTables[$cn->value][] = JHTML::_('select.option', '', "unable to connect to $cn->description");
}
else
{
if ($fabrikDb->getErrorNum() == 0)
{
$tables = $fabrikDb->getTableList();
$connectionTables[$cn->value][] = JHTML::_('select.option', '', '- Please select -');
if (is_array($tables))
{
foreach ($tables as $table)
{
$connectionTables[$cn->value][] = JHTML::_('select.option', $table, $table);
}
}
}
else
{
$connectionTables[$cn->value][] = JHTML::_('select.option', '', "unable to connect to $cn->description");
}
}
}
}
return $connectionTables;
}
/**
* Get the tables names in the loaded connection
*
* @param bool $addBlank add an empty record to the beginning of the list
*
* @return array tables
*/
public function getThisTables($addBlank = false)
{
$cn = $this->getConnection();
$fabrikDb = $this->getDb();
$tables = $fabrikDb->getTableList();
if (is_array($tables))
{
if ($addBlank)
{
$tables = array_merge(array(""), $tables);
}
return $tables;
}
else
{
return array();
}
}
/**
* Tests if you can connect to the connection
*
* @return bool true if connection made otherwise false
*/
public function testConnection()
{
$db = $this->getDb();
return JError::isError($db) ? false : true;
}
/**
* Load the default connection
*
* @return object default connection
*/
public function &loadDefaultConnection()
{
if (!$this->defaultConnection)
{
// $$$ rob connections are pooled for all packages - each package should use
// jos_fabrik_connections and not jos_{package}_connections
$row = FabTable::getInstance('Connection', 'FabrikTable');
$row->load(array('default' => 1));
$this->decryptPw($row);
$this->defaultConnection = $row;
}
$this->connection = $this->defaultConnection;
return $this->defaultConnection;
}
}