1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12:
13: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
14:
15: 16: 17: 18: 19: 20:
21: class cCLISetup {
22:
23: 24: 25: 26:
27: protected $_settings;
28:
29: 30: 31: 32:
33: protected $_args;
34:
35: 36: 37: 38:
39: protected $_settingsFile;
40:
41: 42: 43: 44: 45:
46: public function __construct($args) {
47:
48: $this->_settings['db']['host'] = 'localhost';
49: $this->_settings['db']['user'] = 'root';
50: $this->_settings['db']['password'] = '';
51: $this->_settings['db']['charset'] = 'utf8';
52: $this->_settings['db']['collation'] = 'utf8_general_ci';
53: $this->_settings['db']['database'] = 'contenido';
54: $this->_settings['db']['prefix'] = 'con';
55:
56: $this->_settings['paths']['http_root_path'] = '';
57:
58: $this->_settings['setup']['client_mode'] = '';
59:
60: $this->_settings['admin_user']['password'] = '';
61: $this->_settings['admin_user']['email'] = '';
62:
63: $this->_args = $args;
64:
65: $this->_args['interactive'] = isset($this->_args['interactive']) || isset($this->_args['i']);
66:
67:
68: $this->_settingsFile = 'autoinstall.ini';
69: }
70:
71: 72: 73:
74: public function interpretCommandline() {
75: global $belang;
76:
77: $belang = ($this->_args['locale']) ? $this->_args['locale'] : "en_US";
78:
79: cI18n::init(CON_SETUP_PATH . '/locale/', $belang, 'setup');
80:
81:
82: if (isset($this->_args['h']) || $this->_args['help']) {
83: printHelpText();
84: exit(0);
85: }
86:
87:
88: if (isset($this->_args['file'])) {
89: $this->_settingsFile = $this->_args['file'];
90: }
91:
92: prntln("\r" . i18n('This program will install CONTENIDO on this computer.', 'setup'));
93:
94:
95: if ($this->_args['interactive']) {
96:
97: prntln();
98: prntln();
99:
100:
101: $this->getSettingsFromFile($this->_settingsFile);
102: $this->getSettingsFromCommandLine();
103: $this->getUserInputSettings();
104: } else if ($this->_args['noninteractive']) {
105:
106: echo(i18n('Looking for ', 'setup') . $this->_settingsFile . '...');
107: if (file_exists($this->_settingsFile)) {
108:
109: prntln(i18n('found', 'setup'));
110: prntln(i18n('CONTENIDO will use the specified settings from ', 'setup') . $this->_settingsFile);
111: prntln();
112: $this->getSettingsFromFile($this->_settingsFile);
113: $this->getSettingsFromCommandLine();
114: $this->printSettings();
115: } else {
116:
117: prntln(i18n('not found', 'setup'));
118: prntln(sprintf(i18n('CONTENIDO was unable to locate the configuration file %s, but you asked to use it.', 'setup'), $this->_settingsFile));
119: prntln(i18n('Setup can not continue.', 'setup'));
120: exit(1);
121: }
122: } else {
123:
124: echo(i18n('Looking for ', 'setup') . $this->_settingsFile . '...');
125: if (file_exists($this->_settingsFile)) {
126:
127: prntln(i18n('found', 'setup'));
128: prntln(i18n('CONTENIDO will use the specified settings from ', 'setup') . $this->_settingsFile);
129: prntln();
130: $this->getSettingsFromFile($this->_settingsFile);
131: $this->getSettingsFromCommandLine();
132: $this->printSettings();
133: } else {
134:
135: prntln(i18n('not found', 'setup'));
136: prntln();
137: prntln();
138: $this->getSettingsFromFile($this->_settingsFile);
139: $this->getSettingsFromCommandLine();
140: $this->getUserInputSettings();
141: }
142: }
143: }
144:
145: 146: 147:
148: public function printSettings() {
149: prntln(i18n('CONTENIDO will be installed with the following settings: ', 'setup'));
150: $noPasswordArray = $this->_settings;
151: $noPasswordArray['db']['password'] = '**********';
152: $noPasswordArray['admin_user']['password'] = '**********';
153: prntln(json_encode($noPasswordArray));
154: }
155:
156: 157: 158:
159: public function getSettingsFromCommandLine() {
160: $this->_settings['db']['host'] = ($this->_args['dbhost'] == '') ? $this->_settings['db']['host'] : $this->_args['dbhost'];
161: $this->_settings['db']['user'] = ($this->_args['dbuser'] == '') ? $this->_settings['db']['user'] : $this->_args['dbuser'];
162: $this->_settings['db']['password'] = ($this->_args['dbpassword'] == '') ? $this->_settings['db']['password'] : $this->_args['dbpassword'];
163: $this->_settings['db']['charset'] = ($this->_args['dbcharset'] == '') ? $this->_settings['db']['charset'] : $this->_args['dbcharset'];
164: $this->_settings['db']['collation'] = ($this->_args['dbcollation'] == '') ? $this->_settings['db']['collation'] : $this->_args['dbcollation'];
165: $this->_settings['db']['database'] = ($this->_args['dbdatabase'] == '') ? $this->_settings['db']['database'] : $this->_args['dbdatabase'];
166: $this->_settings['db']['prefix'] = ($this->_args['dbprefix'] == '') ? $this->_settings['db']['prefix'] : $this->_args['dbprefix'];
167:
168: $this->_settings['paths']['http_root_path'] = ($this->_args['pathshttprootpath'] == '') ? $this->_settings['paths']['http_root_path'] : $this->_args['pathshttprootpath'];
169:
170: $this->_settings['setup']['client_mode'] = ($this->_args['setupclientmode'] == '') ? $this->_settings['setup']['client_mode'] : $this->_args['setupclientmode'];
171:
172: $this->_settings['admin_user']['password'] = ($this->_args['adminuserpassword'] == '') ? $this->_settings['admin_user']['password'] : $this->_args['adminuserpassword'];
173: $this->_settings['admin_user']['email'] = ($this->_args['adminuseremail'] == '') ? $this->_settings['admin_user']['email'] : $this->_args['adminuseremail'];
174: $this->_settings['advanced']['delete_database'] = ($this->_args['advanceddeletedatabase'] == '') ? $this->_settings['advanced']['delete_database'] : $this->_args['advanceddeletedatabase'];
175: }
176:
177: 178: 179:
180: public function getUserInputSettings() {
181:
182:
183: prntln('>>>>> '. i18n('Welcome to CONTENIDO', 'setup'). ' <<<<<');
184: prntln('');
185: prntln(i18n('Database settings:', 'setup'));
186:
187:
188: prnt(i18n('Host', 'setup') . ' [' . $this->_settings['db']['host'] . ']: ', 1);
189: $line = trim(fgets(STDIN));
190: $this->_settings['db']['host'] = ($line == "") ? $this->_settings['db']['host'] : $line;
191:
192:
193: prnt(i18n('User', 'setup') . ' [' . $this->_settings['db']['user'] . ']: ', 1);
194: $line = trim(fgets(STDIN));
195: $this->_settings['db']['user'] = ($line == "") ? $this->_settings['db']['user'] : $line;
196:
197:
198: $dbpw = passwordPrompt(i18n('Password', 'setup'), 1);
199: $this->_settings['db']['password'] = ($dbpw == '') ? $this->_settings['db']['password'] : $dbpw;
200:
201:
202: prnt(i18n('Database name', 'setup') .' [' . $this->_settings['db']['database'] . ']: ', 1);
203: $line = trim(fgets(STDIN));
204: $this->_settings['db']['database'] = ($line == "") ? $this->_settings['db']['database'] : $line;
205:
206:
207: prnt(i18n('Charset', 'setup') . ' [' . $this->_settings['db']['charset'] . ']: ', 1);
208: $line = trim(fgets(STDIN));
209: $this->_settings['db']['charset'] = ($line == "") ? $this->_settings['db']['charset'] : $line;
210:
211:
212: prnt(i18n('Collation', 'setup') . ' [' . $this->_settings['db']['collation'] . ']: ', 1);
213: $line = trim(fgets(STDIN));
214: $this->_settings['db']['collation'] = ($line == "") ? $this->_settings['db']['collation'] : $line;
215:
216:
217: prnt(i18n('Prefix', 'setup') . ' [' . $this->_settings['db']['prefix'] . ']: ', 1);
218: $line = trim(fgets(STDIN));
219: $this->_settings['db']['prefix'] = ($line == "") ? $this->_settings['db']['prefix'] : $line;
220:
221:
222: prntln();
223: prntln(i18n('Please enter the http path to where the contenido/ folder resides', 'setup'));
224: prntln(i18n('e.g. http://localhost/', 'setup'));
225: prnt(i18n('Backend web path', 'setup') .' [' . $this->_settings['paths']['http_root_path'] . ']: ', 1);
226: $line = trim(fgets(STDIN));
227: $this->_settings['paths']['http_root_path'] = ($line == "") ? $this->_settings['paths']['http_root_path'] : $line;
228:
229:
230: prntln();
231: $displayStandard = i18n('yes', 'setup');
232: if ($this->_settings['setup']['client_mode'] == 'CLIENTMODULES') {
233: $displayStandard = i18n('modules', 'setup');
234: } else if ($this->_settings['setup']['client_mode'] == 'NOCLIENT') {
235: $displayStandard = i18n('no', 'setup');
236: }
237: $first = true;
238: while ($this->_settings['setup']['client_mode'] == "" || $first) {
239: $first = false;
240: prntln(i18n('Do you want to install the example client?', 'setup'));
241: prntln(i18n('Please choose "yes" (to install the modules AND the content), "modules" (to install only the modules) or "no"', 'setup'));
242: prnt(i18n('Examples? (yes/modules/no)', 'setup') . '[' . $displayStandard . ']: ', 1);
243: $line = cString::toLowerCase(trim(fgets(STDIN)));
244: if ($line == "") {
245: $line = $displayStandard;
246: }
247: if ($line == 'yes' || $line == i18n('yes', 'setup')) {
248: $this->_settings['setup']['client_mode'] = 'CLIENTEXAMPLES';
249: } else if ($line == 'modules' || $line == i18n('modules', 'setup')) {
250: $this->_settings['setup']['client_mode'] = 'CLIENTMODULES';
251: } else if ($line == 'no' || $line == i18n('no', 'setup')) {
252: $this->_settings['setup']['client_mode'] = 'NOCLIENT';
253: }
254: }
255:
256:
257: $password1 = "something";
258: $password2 = "something else";
259: prntln();
260: prntln(i18n('Admin information:'));
261: while ($password1 != $password2) {
262: prntln(i18n('You have to enter the password twice and they have to match', 'setup'), 1);
263: $password1 = passwordPrompt(i18n('Admin password', 'setup'), 1);
264:
265: $password2 = passwordPrompt(i18n('Repeat admin password', 'setup'), 1);
266: }
267: $this->_settings['admin_user']['password'] = ($password1 == '') ? $this->_settings['admin_user']['password'] : $password1;
268:
269:
270: prnt(i18n('Admin email', 'setup') . ' [' . $this->_settings['admin_user']['email'] . ']: ', 1);
271: $line = trim(fgets(STDIN));
272: $this->_settings['admin_user']['email'] = ($line == "") ? $this->_settings['admin_user']['email'] : $line;
273:
274:
275: prntln(i18n('Thank you.', 'setup'));
276: prntln();
277: prntln();
278: }
279:
280: 281: 282: 283: 284:
285: public function getSettingsFromFile($file) {
286: if (!cFileHandler::exists($file)) {
287: return;
288: }
289:
290: $ext = cString::toLowerCase(pathinfo($file, PATHINFO_EXTENSION));
291:
292: switch ($ext) {
293: case 'ini':
294: $this->_settings = parse_ini_file($file, true);
295: break;
296: case 'xml':
297: $xml = simplexml_load_file($file);
298: if (!$xml) {
299: return;
300: }
301:
302: $this->_settings['db']['host'] = trim($xml->db->host);
303: $this->_settings['db']['user'] = trim($xml->db->user);
304: $this->_settings['db']['password'] = trim($xml->db->password);
305: $this->_settings['db']['charset'] = trim($xml->db->charset);
306: $this->_settings['db']['database'] = trim($xml->db->database);
307: $this->_settings['db']['prefix'] = trim($xml->db->prefix);
308: $this->_settings['db']['collation'] = trim($xml->db->collation);
309:
310: $this->_settings['paths']['http_root_path'] = trim($xml->path->http_root_path);
311:
312: $this->_settings['setup']['client_mode'] = trim($xml->client->client_mode);
313:
314: $this->_settings['admin_user']['password'] = trim($xml->admin_user->password);
315: $this->_settings['admin_user']['email'] = trim($xml->admin_user->email);
316: $this->_settings['advanced']['delete_database'] = trim($xml->advanced->delete_database);
317: break;
318: case 'json':
319: $this->_settings = json_decode(file_get_contents($file), true);
320: break;
321: }
322:
323: }
324:
325: 326: 327: 328: 329: 330:
331: public function executeSystemTests() {
332: global $args, $belang;
333:
334: $cfg = cRegistry::getConfig();
335:
336: if ($this->_settings['advanced']['delete_database'] == '1' || $this->_settings['advanced']['delete_database'] == 'YES') {
337: $answer = '';
338: while ($answer != 'Y' && $answer != 'N') {
339: prnt(sprintf(i18n("You chose in the configuration file to delete the database '%s' before installing.\nDO YOU REALLY WANT TO CONTINUE WITH DELETING THIS DATABASE? (Y/N) [N]: ", 'setup'), $this->_settings['db']['database']));
340: $answer = trim(fgets(STDIN));
341: if ($answer == "") {
342: $answer = "N";
343: }
344: }
345: if ($answer != "Y") {
346: exit(3);
347: }
348:
349: $db = getSetupMySQLDBConnection(false);
350: $db->query('DROP DATABASE ' . $this->_settings['db']['database']);
351:
352: prntln();
353: prntln(sprintf(i18n('THE DATABASE %s HAS BEEN DELETED!!!', 'setup'), $this->_settings['db']['database']));
354: prntln();
355: }
356:
357: prnt(i18n('Testing your system...', 'setup'));
358:
359: $fine = true;
360:
361:
362: i18nInit('../data/locale/', $belang);
363:
364:
365: $test = new cSystemtest($cfg);
366: $test->runTests(false);
367: $test->testFilesystem(true, false);
368: $test->testFrontendFolderCreation();
369: $test->checkSetupMysql('setup', $cfg['db']['connection']['database'], $_SESSION['dbprefix'], $_SESSION['dbcharset'], $_SESSION['dbcollation']);
370:
371: $testResults = $test->getResults();
372:
373: foreach ($testResults as $testResult) {
374: if ($testResult["severity"] == cSystemtest::C_SEVERITY_NONE) {
375: continue;
376: }
377:
378: if ($testResult['result'] == false) {
379: $fine = false;
380: }
381: }
382: if (!$fine) {
383: prntln(i18n('error', 'setup'));
384: foreach ($testResults as $testResult) {
385: if ($testResult["severity"] == cSystemtest::C_SEVERITY_NONE) {
386: continue;
387: }
388:
389: if ($testResult['result'] == false) {
390: prntln(html_entity_decode(strip_tags($testResult['headline'], 1)));
391: prntln(html_entity_decode(strip_tags($testResult['message'], 2)));
392: }
393: }
394: prntln();
395: prntln(i18n('There have been errors during the system check.', 'setup'));
396: prntln(i18n('However this might be caused by differing configurations between the CLI php and the CGI php.', 'setup'));
397:
398: if ($args['noninteractive']) {
399: exit(3);
400: }
401:
402: $answer = '';
403: while ($answer != 'y' && $answer != 'n' && $answer != i18n('y', 'setup') && $answer != i18n('n', 'setup')) {
404: prnt(i18n('Do you want to continue despite the errors? (y/n) [n]: ', 'setup'));
405: $answer = trim(fgets(STDIN));
406: if ($answer == "") {
407: $answer = "n";
408: }
409: }
410: if (cString::toLowerCase($answer) == "n" || cString::toLowerCase($answer) == cString::toLowerCase(i18n('n', 'setup'))) {
411: exit(3);
412: }
413: } else {
414: prntln(i18n('Your system seems to be okay.', 'setup'));
415: prntln();
416: }
417: }
418:
419: 420: 421:
422: public function applySettings() {
423:
424: $cfg = cRegistry::getConfig();
425:
426: $cfg['db'] = array(
427: 'connection' => array(
428: 'host' => $this->_settings['db']['host'],
429: 'user' => $this->_settings['db']['user'],
430: 'password' => $this->_settings['db']['password'],
431: 'charset' => $this->_settings['db']['charset'],
432: 'database' => $this->_settings['db']['database']
433: ),
434: 'haltBehavior' => 'report',
435: 'haltMsgPrefix' => (isset($_SERVER['REQUEST_URI'])) ? $_SERVER['REQUEST_URI'] . ' ' : '',
436: 'enableProfiling' => false
437: );
438: $_SESSION['setuptype'] = 'setup';
439: $_SESSION['dbname'] = $this->_settings['db']['database'];
440: $_SESSION['configmode'] = 'save';
441: $_SESSION['override_root_http_path'] = $this->_settings['paths']['http_root_path'];
442: $_SESSION['clientmode'] = $this->_settings['setup']['client_mode'];
443: $_SESSION['adminpass'] = $this->_settings['admin_user']['password'];
444: $_SESSION['adminmail'] = $this->_settings['admin_user']['email'];
445: $_SESSION['dbprefix'] = $this->_settings['db']['prefix'];
446: $_SESSION['dbcollation'] = $this->_settings['db']['collation'];
447: $_SESSION['dbcharset'] = $this->_settings['db']['charset'];
448: $cfg['sql']['sqlprefix'] = $this->_settings['db']['prefix'];
449:
450: include($cfg['path']['contenido_config'] . 'cfg_sql.inc.php');
451: }
452: }
453: