Skip to content
This repository was archived by the owner on Sep 10, 2021. It is now read-only.

Commit e3cc6a7

Browse files
author
Jamie Snape
committed
Cleanup install controller and use PDO to test database connectivity
1 parent 3d21387 commit e3cc6a7

File tree

4 files changed

+105
-144
lines changed

4 files changed

+105
-144
lines changed

core/configs/database.ini

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ database.params.host = localhost
66
database.params.port = 3306
77
database.params.username = root
88
database.params.password = set_your_password
9-
database.params.dbname = midas3
9+
database.params.dbname = midas
1010

1111
[development]
1212
database.type = pdo
@@ -16,7 +16,7 @@ database.params.host = localhost
1616
database.params.port = 3306
1717
database.params.username = root
1818
database.params.password = set_your_password
19-
database.params.dbname = midas3
19+
database.params.dbname = midas
2020

2121
[testing]
2222
database.type = pdo
@@ -26,4 +26,4 @@ database.params.host = localhost
2626
database.params.port = 3306
2727
database.params.username = root
2828
database.params.password = set_your_password
29-
database.params.dbname = midas3_test
29+
database.params.dbname = midas_test

core/controllers/InstallController.php

Lines changed: 71 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -33,83 +33,78 @@ class InstallController extends AppController
3333
*/
3434
function init()
3535
{
36-
if(file_exists(BASE_PATH.'/core/configs/database.local.ini') &&
37-
file_exists(BASE_PATH.'/core/configs/application.local.ini') &&
36+
if(file_exists(BASE_PATH . '/core/configs/database.local.ini') &&
37+
file_exists(BASE_PATH . '/core/configs/application.local.ini') &&
3838
Zend_Controller_Front::getInstance()->getRequest()->getActionName() != 'step3')
3939
{
40-
throw new Zend_Exception("Midas is already installed.");
40+
throw new Zend_Exception('Midas is already installed.');
4141
}
42-
} //end init
42+
}
4343

4444
/**
4545
* @method indexAction()
4646
*/
4747
function indexAction()
4848
{
49-
if(file_exists(BASE_PATH."/core/configs/database.local.ini"))
49+
if(file_exists(BASE_PATH . '/core/configs/database.local.ini'))
5050
{
5151
$this->_redirect('/install/step3');
5252
}
53-
$this->view->header = "Step1: Server Configuration";
54-
// Check PHP extension / function
53+
$this->view->header = 'Step 1: Server Configuration';
54+
// Check PHP extensions / functions
5555
$phpextensions = array (
56-
"simplexml" => array(false, ""),
56+
'simplexml' => array(false, ''),
5757
);
5858
$this->view->phpextension_missing = $this->Component->Utility->checkPhpExtensions($phpextensions);
59-
$this->view->writable = is_writable(BASE_PATH.'/core/configs');
59+
$this->view->writable = is_writable(BASE_PATH . '/core/configs');
6060
$this->view->basePath = BASE_PATH;
6161
if(!empty($_POST) && $this->view->writable)
6262
{
63-
$this->_redirect("/install/step2");
63+
$this->_redirect('/install/step2');
6464
}
65-
} // end method indexAction
65+
}
6666

6767
/**
6868
* @method step2Action()
6969
*/
7070
function step2Action()
7171
{
72-
if(file_exists(BASE_PATH."/core/configs/database.local.ini"))
72+
if(file_exists(BASE_PATH . '/core/configs/database.local.ini'))
7373
{
7474
$this->_redirect('/install/step3');
7575
}
76-
$this->view->header = "Step2: Database Configuration";
77-
// Check PHP extension / function
78-
$phpextensions = array (
79-
"mysql" => array(false, ''),
80-
"pgsql" => array(false, ''),
81-
"oci" => array(false, ''),
82-
"sqlite" => array(false, ''),
83-
"ibm" => array(false, ''),
84-
);
76+
$this->view->header = 'Step 2: Database Configuration';
8577

78+
$databases = array('mysql', 'pgsql');
8679
$this->view->databaseType = array();
87-
88-
foreach($phpextensions as $key => $t)
80+
foreach($databases as $database)
8981
{
90-
if(!file_exists(BASE_PATH."/core/database/".$key))
82+
if(!extension_loaded($database) || !file_exists(BASE_PATH . '/core/database/' . $database))
9183
{
92-
unset($phpextensions[$key]);
84+
unset($database);
9385
}
9486
else
9587
{
96-
$form = $this->Form->Install->createDBForm($key);
88+
$form = $this->Form->Install->createDBForm($database);
9789
$port = $form->getElement('port');
98-
switch($key)
90+
$username = $form->getElement('username');
91+
switch($database)
9992
{
10093
case 'mysql':
10194
$port->setValue('3306');
95+
$username->setValue('root');
10296
break;
10397
case 'pgsql':
10498
$port->setValue('5432');
99+
$username->setValue('postgres');
105100
break;
106101
default:
107102
break;
108103
}
109-
$this->view->databaseType[$key] = $this->getFormAsArray($form);
104+
$this->view->databaseType[$database] = $this->getFormAsArray($form);
110105
}
111106
}
112-
$this->view->phpextension_missing = $this->Component->Utility->checkPhpExtensions($phpextensions);
107+
113108
$this->view->writable = is_writable(BASE_PATH);
114109
$this->view->basePath = BASE_PATH;
115110

@@ -119,18 +114,16 @@ function step2Action()
119114
$form = $this->Form->Install->createDBForm($type);
120115
if($form->isValid($this->getRequest()->getPost()))
121116
{
122-
$databaseConfig = parse_ini_file(BASE_PATH.'/core/configs/database.ini', true);
123-
$MyDirectory = opendir(BASE_PATH."/core/database/".$type);
124-
125-
require_once BASE_PATH.'/core/controllers/components/UpgradeComponent.php';
117+
$databaseConfig = parse_ini_file(BASE_PATH . '/core/configs/database.ini', true);
118+
require_once BASE_PATH . '/core/controllers/components/UpgradeComponent.php';
126119
$upgradeComponent = new UpgradeComponent();
127-
$upgradeComponent->dir = BASE_PATH."/core/database/".$type;
120+
$upgradeComponent->dir = BASE_PATH . '/core/database/'.$type;
128121
$upgradeComponent->init = true;
129122
$sqlFile = $upgradeComponent->getNewestVersion(true);
130-
$sqlFile = BASE_PATH."/core/database/".$type."/".$sqlFile.'.sql';
123+
$sqlFile = BASE_PATH . '/core/database/'.$type.'/'.$sqlFile.'.sql';
131124
if(!isset($sqlFile) || !file_exists($sqlFile))
132125
{
133-
throw new Zend_Exception("Unable to find sql file");
126+
throw new Zend_Exception('Unable to find sql file');
134127
}
135128

136129
switch($type)
@@ -161,7 +154,7 @@ function step2Action()
161154
$databaseConfig['development']['database.params.port'] = $form->getValue('port');
162155
$databaseConfig['production']['database.params.port'] = $form->getValue('port');
163156

164-
$db = Zend_Db::factory("PDO_MYSQL", $params);
157+
$db = Zend_Db::factory('Pdo_Mysql', $params);
165158
Zend_Db_Table::setDefaultAdapter($db);
166159
Zend_Registry::set('dbAdapter', $db);
167160

@@ -193,7 +186,7 @@ function step2Action()
193186
$databaseConfig['development']['database.params.port'] = $form->getValue('port');
194187
$databaseConfig['production']['database.params.port'] = $form->getValue('port');
195188

196-
$db = Zend_Db::factory("PDO_PGSQL", $params);
189+
$db = Zend_Db::factory('Pdo_Pgsql', $params);
197190
Zend_Db_Table::setDefaultAdapter($db);
198191
Zend_Registry::set('dbAdapter', $db);
199192
$dbtype = 'PDO_PGSQL';
@@ -204,21 +197,21 @@ function step2Action()
204197
$databaseConfig['production']['version'] = str_replace('.sql', '', basename($sqlFile));
205198
$databaseConfig['development']['version'] = str_replace('.sql', '', basename($sqlFile));
206199

207-
$this->Component->Utility->createInitFile(BASE_PATH.'/core/configs/database.local.ini', $databaseConfig);
200+
$this->Component->Utility->createInitFile(BASE_PATH . '/core/configs/database.local.ini', $databaseConfig);
208201

209202
// Must generate and store our password salt before we create our first user
210-
$appConfig = parse_ini_file(BASE_PATH.'/core/configs/application.ini', true);
203+
$appConfig = parse_ini_file(BASE_PATH . '/core/configs/application.ini', true);
211204
$appConfig['global']['password.prefix'] = UtilityComponent::generateRandomString(32);
212205

213206
// Verify whether the user wants to use gravatars or not
214207
$appConfig['global']['gravatar'] = $form->getValue('gravatar');
215208

216209
// Save the new config
217-
$this->Component->Utility->createInitFile(BASE_PATH.'/core/configs/application.local.ini', $appConfig);
218-
$configGlobal = new Zend_Config_Ini(BASE_PATH.'/core/configs/application.local.ini', 'global', true);
210+
$this->Component->Utility->createInitFile(BASE_PATH . '/core/configs/application.local.ini', $appConfig);
211+
$configGlobal = new Zend_Config_Ini(BASE_PATH . '/core/configs/application.local.ini', 'global', true);
219212
Zend_Registry::set('configGlobal', $configGlobal);
220213

221-
require_once BASE_PATH.'/core/controllers/components/UpgradeComponent.php';
214+
require_once BASE_PATH . '/core/controllers/components/UpgradeComponent.php';
222215
$upgradeComponent = new UpgradeComponent();
223216
$db = Zend_Registry::get('dbAdapter');
224217

@@ -230,36 +223,36 @@ function step2Action()
230223
$this->userSession->Dao = $userModel->createUser($form->getValue('email'), $form->getValue('userpassword1'),
231224
$form->getValue('firstname'), $form->getValue('lastname'), 1);
232225

233-
//create default assetstrore
226+
// create default assetstore
234227
$assetstoreDao = new AssetstoreDao();
235228
$assetstoreDao->setName('Local');
236-
$assetstoreDao->setPath(BASE_PATH.'/data/assetstore');
229+
$assetstoreDao->setPath(BASE_PATH . '/data/assetstore');
237230
$assetstoreDao->setType(MIDAS_ASSETSTORE_LOCAL);
238231
$this->Assetstore = new AssetstoreModel(); //reset Database adapter
239232
$this->Assetstore->save($assetstoreDao);
240-
$this->_redirect("/install/step3");
233+
$this->_redirect('/install/step3');
241234
}
242235
}
243-
} // end method step2Action
236+
}
244237

245238
/**
246239
* @method step3Action()
247240
*/
248241
function step3Action()
249242
{
250243
$this->requireAdminPrivileges();
251-
if(!file_exists(BASE_PATH.'/core/configs/database.local.ini'))
244+
if(!file_exists(BASE_PATH . '/core/configs/database.local.ini'))
252245
{
253246
$this->_redirect('/install/index');
254247
}
255-
$this->view->header = 'Step3: Midas Configuration';
248+
$this->view->header = 'Step 3: Midas Server Configuration';
256249
$userDao = $this->userSession->Dao;
257250
if(!isset($userDao) || !$userDao->isAdmin())
258251
{
259-
unlink(BASE_PATH."/core/configs/database.local.ini");
252+
unlink(BASE_PATH . '/core/configs/database.local.ini');
260253
$this->_redirect('/install/index');
261254
}
262-
$applicationConfig = parse_ini_file(BASE_PATH.'/core/configs/application.local.ini', true);
255+
$applicationConfig = parse_ini_file(BASE_PATH . '/core/configs/application.local.ini', true);
263256

264257
$form = $this->Form->Install->createConfigForm();
265258
$formArray = $this->getFormAsArray($form);
@@ -280,7 +273,7 @@ function step3Action()
280273
$allModules = $this->Component->Utility->getAllModules();
281274
foreach($allModules as $key => $module)
282275
{
283-
$configLocal = BASE_PATH.'/core/configs/'.$key.'.local.ini';
276+
$configLocal = BASE_PATH . '/core/configs/'.$key.'.local.ini';
284277
if(file_exists($configLocal))
285278
{
286279
unlink($configLocal);
@@ -295,60 +288,40 @@ function step3Action()
295288
$applicationConfig['global']['smartoptimizer'] = $form->getValue('smartoptimizer');
296289
$applicationConfig['global']['default.timezone'] = $form->getValue('timezone');
297290

298-
$this->Component->Utility->createInitFile(BASE_PATH.'/core/configs/application.local.ini', $applicationConfig);
299-
$this->_redirect("/admin#tabs-modules");
291+
$this->Component->Utility->createInitFile(BASE_PATH . '/core/configs/application.local.ini', $applicationConfig);
292+
$this->_redirect('/admin#tabs-modules');
300293
}
301-
} // end method step2Action
294+
}
302295

303296
/** AJAX function which tests connectivity to a database */
304297
public function testconnectionAction()
305298
{
306299
$this->requireAjaxRequest();
307300
$this->_helper->layout->disableLayout();
308301
$this->_helper->viewRenderer->setNoRender();
309-
$type = $this->_getParam('type');
310-
$username = $this->_getParam('username');
311-
$password = $this->_getParam('password');
312-
$host = $this->_getParam('host');
313-
$dbname = $this->_getParam('dbname');
314-
$port = $this->_getParam('port');
315-
switch($type)
302+
try
316303
{
317-
case 'mysql':
318-
$link = mysql_connect($host.":".$port, "$username", "$password");
319-
if(!$link)
320-
{
321-
$return = array(false, "Could not connect to the server '" . $host . "': ".mysql_error());
322-
break;
323-
}
324-
$dbcheck = mysql_select_db("$dbname");
325-
if(!$dbcheck)
326-
{
327-
$return = array(false, "Could not connect to the server '" . $host . "': ".mysql_error());
328-
break;
329-
}
330-
$sql = "SHOW TABLES FROM ".$dbname;
331-
$result = mysql_query($sql);
332-
if(mysql_num_rows($result) > 0)
333-
{
334-
$return = array(false, "The database is not empty");
335-
break;
336-
}
337-
$return = array(true, "The database is reachable");
338-
break;
339-
case 'pgsql':
340-
$link = pg_connect("host = ".$host." port = ".$port." dbname = ".$dbname." user = ".$username." password = ".$password);
341-
if(!$link)
342-
{
343-
$return = array(false, "Could not connect to the server '" . $host . "': ".pg_last_error($link));
344-
break;
345-
}
346-
$return = array(true, "The database is reachable");
347-
break;
348-
default:
349-
$return = array(false, "Database not defined");
350-
break;
304+
$db = Zend_Db::factory('Pdo_' . ucfirst($this->_getParam('type')), array(
305+
'host' => $this->_getParam('host'),
306+
'port' => $this->_getParam('port'),
307+
'username' => $this->_getParam('username'),
308+
'password' => $this->_getParam('password'),
309+
'dbname' => $this->_getParam('dbname')));
310+
$tables = $db->listTables();
311+
if(count($tables) > 0)
312+
{
313+
$return = array(false, 'The database is not empty');
314+
}
315+
else
316+
{
317+
$return = array(true, 'The database is reachable');
318+
}
319+
$db->closeConnection();
320+
}
321+
catch(Zend_Exception $exception)
322+
{
323+
$return = array(false, 'Could not connect to the database server: ' . $exception->getMessage());
351324
}
352325
echo JsonComponent::encode($return);
353-
}//end getElementInfo
354-
} // end class
326+
}
327+
}

0 commit comments

Comments
 (0)