XOOPS  2.6.0
page_dbsettings.php
Go to the documentation of this file.
1 <?php
2 /*
3  You may not change or alter any portion of this comment or credits
4  of supporting developers from this source code or any supporting source code
5  which is considered copyrighted (c) material of the original comment or credit authors.
6 
7  This program is distributed in the hope that it will be useful,
8  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 */
11 
27 require_once __DIR__ . '/include/common.inc.php';
28 
29 /* @var $wizard XoopsInstallWizard */
30 $wizard = $_SESSION['wizard'];
31 
32 $settings = $_SESSION['settings'];
33 
34 if ($_SERVER['REQUEST_METHOD'] == 'POST') {
35  $params = array('DB_NAME');
36  foreach ($params as $name) {
37  $settings[$name] = isset($_POST[$name]) ? $_POST[$name] : "";
38  }
39  $settings['DB_PARAMETERS'] = serialize(getDbConnectionParams());
40  $_SESSION['settings'] = $settings;
41 }
42 
43 $platform=false;
44 $error = '';
46 
47 $tried_create = false;
48 $connection = null;
50 // if we specified the dbname and failed, try again without it
51 // we will try and create it later
52 if (!$connection && !empty($settings['DB_NAME'])) {
53  $hold_name=$settings['DB_NAME'];
54  unset($settings['DB_NAME']);
55  $_SESSION['settings'] = $settings;
56  $hold_error = $error;
57  $error='';
59  $settings['DB_NAME'] = $hold_name;
60  $_SESSION['settings'] = $settings;
61  if ($connection) {
62  // we have a database name and did not connect
63  if (!empty($settings['DB_NAME']) && !$tried_create) {
64  $platform = $connection->getDatabasePlatform();
65  $canCreate = $platform->supportsCreateDropDatabase();
66  if ($canCreate) {
67  $tried_create = true;
68  try {
69  $sql = $platform->getCreateDatabaseSQL($connection->quoteIdentifier($settings['DB_NAME']));
70  $result = $connection->exec($sql);
71  if ($result) {
72  // try to reconnect with the database specified
73  $connection = null;
75  } else {
77  }
78  } catch (Exception $e) {
79  $error = $e->getMessage();
80  }
81  } else {
83  }
84  }
85  } else { // keep original error
86  $error = $hold_error;
87  }
88 }
89 
90 // leave if we are already connected to a database from earlier input
91 if ($connection && empty($error)) {
92  $currentDb = $connection->getDatabase();
93  if (!empty($currentDb)) {
94  $settings['DB_PARAMETERS'] = serialize(getDbConnectionParams());
95  $_SESSION['settings'] = $settings;
96  $wizard->redirectToPage('+1');
97  exit();
98  }
99 }
100 
101 if ($connection) {
102  $platform = $connection->getDatabasePlatform();
103  try {
104  $sql = $platform->getListDatabasesSQL();
105  $dbResults = $connection->fetchAll($sql);
106  } catch (Exception $e) {
107  $dbResults = false;
108  }
109 
110  $dbIgnored = $wizard->configs['db_types'][$settings['DB_DRIVER']]['ignoredb'];
111  if ($dbResults) {
112  foreach ($dbResults as $dbrow) {
113  if (is_array($dbrow)) {
114  $dbase = reset($dbrow); // get first value in array
115  } else {
116  $dbase = $dbrow;
117  }
118  if (!in_array($dbase, $dbIgnored)) {
119  $availableDatabases[] = $dbase;
120  }
121  }
122  }
123 }
124 
125 if (is_array($availableDatabases) && count($availableDatabases)==1) {
126  if (empty($settings['DB_NAME'])) {
127  $settings['DB_NAME'] = $availableDatabases[0];
128  }
129 }
130 
131 $_SESSION['settings'] = $settings;
132 
133 ob_start();
134 ?>
135 <?php
136 if (!empty($error)) {
137  echo '<div class="x2-note errorMsg">' . $error . "</div>\n";
138 }
139 ?>
140 <script type="text/javascript">
141 function updateDbName(){
142 var e = document.getElementById("DB_AVAILABLE");
143 var dbSelected = e.options[e.selectedIndex].text;
144 
145 document.getElementById("DB_NAME").value=dbSelected;
146 }
147 </script>
148 <fieldset>
149 <?php
150 if (!empty($availableDatabases)) {
151  echo '<legend>' . LEGEND_DATABASE . '</legend>';
152  echo '<div class="xoform-help">' . DB_AVAILABLE_HELP . '</div>';
153  echo '<label class="xolabel" for="DB_DATABASE_LABEL" class="center">';
154  echo DB_AVAILABLE_LABEL;
155  echo ' <select size="1" name="DB_AVAILABLE" id="DB_AVAILABLE" onchange="updateDbName();">';
156  $selected = ($settings['DB_NAME'] == '') ? 'selected' : '';
157  echo '<option value="" {$selected}>-----------</option>';
158  foreach ($availableDatabases as $dbase) {
159  $selected = ($settings['DB_NAME'] == $dbase) ? 'selected' : '';
160  echo "<option value=\"{$dbase}\" {$selected}>{$dbase}</option>";
161  }
162 }
163 ?>
164  </select>
165  </label>
166 
167  <?php echo xoFormField('DB_NAME', $settings['DB_NAME'], DB_NAME_LABEL, DB_NAME_HELP); ?>
168 </fieldset>
169 
170 <?php
171 $content = ob_get_contents();
172 ob_end_clean();
173 
174 $_SESSION['pageHasHelp'] = true;
175 $_SESSION['pageHasForm'] = true;
176 $_SESSION['content'] = $content;
177 $_SESSION['settings'] = $settings;
178 include XOOPS_INSTALL_PATH . '/include/install_tpl.php';
const ERR_NO_DATABASE
Definition: install.php:152
$settings
$result
Definition: pda.php:33
$_SERVER['REQUEST_URI']
$tried_create
const LEGEND_DATABASE
Definition: install.php:85
$availableDatabases
exit
Definition: browse.php:104
if($_SERVER['REQUEST_METHOD']== 'POST') $platform
if(!$connection &&!empty($settings['DB_NAME'])) if($connection &&empty($error)) if($connection) if(is_array($availableDatabases)&&count($availableDatabases)==1) $_SESSION['settings']
const ERR_NO_CREATEDB
Definition: install.php:155
const XOOPS_INSTALL_PATH
Definition: common.inc.php:33
const DB_AVAILABLE_HELP
Definition: install.php:119
xoFormField($name, $value, $label, $help= '')
Definition: functions.php:76
$connection
const DB_AVAILABLE_LABEL
Definition: install.php:118
const DB_NAME_HELP
Definition: install.php:94
$sql
Definition: pda.php:32
getDbConnection(&$error)
Definition: functions.php:502
const DB_NAME_LABEL
Definition: install.php:93
getDbConnectionParams()
Definition: functions.php:472