2 /************************************************************************
3 * Mailer v0.2.1-FINAL Start: 06/24/2012 *
4 * =================== Last change: 06/24/2012 *
6 * -------------------------------------------------------------------- *
7 * File : ajax_installer.php *
8 * -------------------------------------------------------------------- *
9 * Short description : AJAX-related functions for installer *
10 * -------------------------------------------------------------------- *
11 * Kurzbeschreibung : AJAX-bezogene Funktionen fuer Installer *
12 * -------------------------------------------------------------------- *
15 * $Tag:: 0.2.1-FINAL $ *
17 * -------------------------------------------------------------------- *
18 * Copyright (c) 2003 - 2009 by Roland Haeder *
19 * Copyright (c) 2009 - 2012 by Mailer Developer Team *
20 * For more information visit: http://mxchange.org *
22 * This program is free software; you can redistribute it and/or modify *
23 * it under the terms of the GNU General Public License as published by *
24 * the Free Software Foundation; either version 2 of the License, or *
25 * (at your option) any later version. *
27 * This program is distributed in the hope that it will be useful, *
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
30 * GNU General Public License for more details. *
32 * You should have received a copy of the GNU General Public License *
33 * along with this program; if not, write to the Free Software *
34 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, *
36 ************************************************************************/
38 // Some security stuff...
39 if ((!defined('__SECURITY')) || (!isAjaxOutputMode()) || (!isInstallationPhase())) {
43 //-----------------------------------------------------------------------------
44 // Generic call-back functions, they all rely on session data
45 //-----------------------------------------------------------------------------
47 // Establish a database link
48 function establishAjaxInstallerDatabaseLink () {
49 // This requires some session data
50 if (!isSessionDataSet(array('mysql_host', 'mysql_dbase', 'mysql_prefix', 'mysql_login', 'mysql_password1', 'mysql_password2', 'mysql_engine'))) {
51 // Some required session data is not set
52 reportBug(__FUNCTION__, __LINE__, 'Required session data for this step not found.');
56 $linkResource = SQL_CONNECT(getSession('mysql_host'), getSession('mysql_login'), getSession('mysql_password1'), __FUNCTION__, __LINE__);
58 // Is this a link resource?
59 if (!is_resource($linkResource)) {
61 reportBug(__FUNCTION__, __LINE__, 'linkResource[]=' . gettype($linkResource) . ', expected: link resource');
64 // Does selecting the database work?
65 if (!SQL_SELECT_DB(getSession('mysql_dbase'), __FUNCTION__, __LINE__)) {
66 // Could not be selected
67 reportBug(__FUNCTION__, __LINE__, 'Could not select database ' . getSession('mysql_dbase'));
68 } elseif ((!isFileReadable(getSession('base_path') . 'install/tables.sql')) || (!isFileReadable(getSession('base_path') . 'install/menu-'.getLanguage().'.sql'))) {
69 // Installation area not found
70 reportBug(__FUNCTION__, __LINE__, 'SQL dumps not found. Please extract ALL files from the archive or checkout all files out from SVN.');
71 } elseif (ifFatalErrorsDetected()) {
72 // Some other fatal error occured
73 reportBug(__FUNCTION__, __LINE__, 'Some fatal error detected, please check debug.log for details.');
76 // Set type, prefix from POST data and database name for later queries
77 setConfigEntry('_TABLE_TYPE' , getSession('mysql_engine'));
78 setConfigEntry('_MYSQL_PREFIX', getSession('mysql_prefix'));
79 setConfigEntry('__DB_NAME' , getSession('mysql_dbase'));
82 //-----------------------------------------------------------------------------
83 // Call-back functions for processing AJAX requests
84 //-----------------------------------------------------------------------------
86 // Processes AJAX requests for installer
87 function doAjaxProcessInstall () {
88 // 'do' must always be set and installation phase must be true
89 if (!isInstallationPhase()) {
90 // This shall not happen
91 reportBug(__FUNCTION__, __LINE__, 'This AJAX request handler was called outside the installer.');
92 } elseif (!isPostRequestElementSet('do')) {
93 // This shall not happen
94 reportBug(__FUNCTION__, __LINE__, 'The JavaScript did not send "do" which is fatal.');
97 // Notify all modules that we are installing
98 $GLOBALS['__mailer_installing'] = TRUE;
100 // Again we do a call-back, so generate a function name depending on 'do'
101 $callbackName = 'doAjaxInstaller' . capitalizeUnderscoreString(postRequestElement('do'));
103 // Is the call-back function there?
104 if (!function_exists($callbackName)) {
105 // This shall not happen
106 reportBug(__FUNCTION__, __LINE__, 'AJAX call-back ' . $callbackName . ' does not exist.');
110 call_user_func($callbackName);
112 // Is the status fine or template not found (404)?
116 // Processes installer request for testing
117 function doAjaxInstallerTest () {
118 // Load the "test passed" template
119 setAjaxReplyContent(loadTemplate('ajax_test_passed', TRUE));
121 // All okay if we reach this point
122 setHttpStatus('200 OK');
125 // Processes installer requests for footer navigation
126 function doAjaxInstallerFooterNavigation () {
127 // 'tab' must always be set to determine which navigation buttons shall be visible
128 if (!isPostRequestElementSet('tab')) {
129 // This shall not happen
130 reportBug(__FUNCTION__, __LINE__, 'The JavaScript did not send "tab" which is fatal.');
133 // Init array for footer navigation
134 $enabledNavigations = array();
136 // "Detect" the 'tab' value
137 switch (postRequestElement('tab')) {
138 case 'base_data': // Also 'previous' is valid
139 case 'database_config':
143 array_push($enabledNavigations, 'previous');
144 case 'welcome': // Only 'next' works for welcome page
145 array_push($enabledNavigations, 'next');
148 case 'overview': // Enable only 'previous'
149 array_push($enabledNavigations, 'previous');
150 if (isInstallationDataCompleted()) {
152 array_push($enabledNavigations, 'finish');
156 default: // Unsupported value
157 // This shall not happen
158 reportBug(__FUNCTION__, __LINE__, 'Unsupported "tab" value ' . postRequestElement('tab') . ' detected.');
160 // This will never be reached
164 // Output the array for JSON reply
165 setAjaxReplyContent(encodeJson($enabledNavigations));
167 // All okay if we reach this point
168 setHttpStatus('200 OK');
171 // Processes installer AJAX calls for content-requests
172 function doAjaxInstallerDoStep () {
173 // 'step' must be there
174 if (!isPostRequestElementSet('step')) {
175 // This shall not happen
176 reportBug(__FUNCTION__, __LINE__, 'The JavaScript did not send "step" which is fatal.');
179 // Construct call-back name
180 $callbackName = 'doAjaxInstallerStep' . capitalizeUnderscoreString(postRequestElement('step'));
182 // Is the function there?
183 if (function_exists($callbackName)) {
184 // Call it for setting values in session
185 call_user_func($callbackName);
187 // Log missing functions
188 reportBug(__FUNCTION__, __LINE__, 'Call-back function ' . $callbackName . ' does not exist.');
192 setAjaxReplyContent(encodeJson(postRequestElement('step').'=OK'));
194 // All okay if we reach this point
195 setHttpStatus('200 OK');
198 // Processes installer AJAX calls for content-requests
199 function doAjaxInstallerRequestContent () {
200 // 'tab' must be there
201 if (!isPostRequestElementSet('tab')) {
202 // This shall not happen
203 reportBug(__FUNCTION__, __LINE__, 'The JavaScript did not send "tab" which is fatal.');
206 // Construct call-back name for value-preset
207 $callbackName = 'doAjaxPrepareInstaller' . capitalizeUnderscoreString(postRequestElement('tab'));
209 // Is the function there?
210 if (function_exists($callbackName)) {
211 // Call it for setting values in session
212 call_user_func($callbackName);
214 // Log missing functions
215 reportBug(__FUNCTION__, __LINE__, 'Call-back function ' . $callbackName . ' does not exist.');
218 // Is the HTTP status still the same? (204 No Content)
219 if (getHttpStatus() == '204 No Content') {
220 // We use the current access level 'install' as prefix and construct a template name
221 setAjaxReplyContent(loadTemplate('install_page_' . trim(postRequestElement('tab')), TRUE));
223 // Has the template been loaded?
224 if (isset($GLOBALS['template_content']['html']['install_page_' . trim(postRequestElement('tab'))])) {
225 // All okay if we reach this point
226 setHttpStatus('200 OK');
229 setHttpStatus('404 Not Found');
234 // Process installer AJAX call for change-warning
235 function doAjaxInstallerChangeWarning () {
236 // 'elements' and 'button' must be there
237 if ((!isPostRequestElementSet('elements')) || (!isPostRequestElementSet('button'))) {
238 // This shall not happen
239 reportBug(__FUNCTION__, __LINE__, 'The JavaScript did not send "elements" and/or "button" which is fatal.');
242 // "Walk" through all elements
244 foreach (explode(':', postRequestElement('elements')) as $element) {
245 // Is it an extension?
246 if (substr($element, 0, 4) == 'ext_') {
247 // Add row for extension
248 $OUT .= '<li>{%message=INSTALLER_CHANGED_ELEMENT_EXTENSION=' . str_replace('_', '-', $element) . '%}</li>';
251 $OUT .= '<li>{--INSTALLER_CHANGED_ELEMENT_' . strtoupper($element) . '--}</li>';
259 'button' => postRequestElement('button'),
260 'message' => '{--INSTALLER_TAB_NAVIGATION_' . strtoupper(postRequestElement('button')) . '_LINK--}',
263 if (in_array(postRequestElement('button'), array('previous', 'next'))) {
264 // Load 'prefixed' template
265 setAjaxReplyContent(loadTemplate('install_warning_' . postRequestElement('button'), TRUE, $content));
267 // Load 'tab' template
268 setAjaxReplyContent(loadTemplate('install_warning_tab', TRUE, $content));
271 // All okay if we reach this point
272 setHttpStatus('200 OK');
275 // Process installer AJAC call for saving changes
276 function doAjaxInstallerSaveChanges () {
277 // 'tab' must always be set to create a post-check-callback
278 if (!isPostRequestElementSet('tab')) {
279 // This shall not happen
280 reportBug(__FUNCTION__, __LINE__, 'The JavaScript did not send "tab" which is fatal.');
283 // Save the tab for pre-"filtering"
284 $currentTab = postRequestElement('tab');
286 // Remove some elements which should not be saved
287 foreach (array('tab', 'do', 'level') as $removedElement) {
288 // Remove this element from POST data
289 unsetPostRequestElement($removedElement);
292 // Default is failed save attempt (e.g. nothing to save)
294 'status' => 'failed',
295 'message' => '{--INSTALLER_SAVE_CHANGES_FAILED--}',
296 // Don't set this to false, or else it will be returned as 'failed' but is saved
298 'failed_fields' => array()
301 // Init overall status
304 // Now set all remaining data in session
305 foreach (postRequestArray() as $key => $value) {
306 // Set it, if it is valid, else it will be added to $saveStatus (call-by-reference)
307 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'key=' . $key . ',value[' . gettype($value) . '=' . $value);
308 $saveStatus['is_saved'] = (
309 // Is the data valid?
310 (isInstallerDataValid($saveStatus, $key, $value))
312 // And can it be stored in session?
313 (setSession($key, $value))
316 // Save the overall status for below final check
317 $isAllSaved = (($isAllSaved === TRUE) && ($saveStatus['is_saved'] === TRUE));
318 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'key=' . $key . ',value[' . gettype($value) . '=' . $value . ',is_saved=' . intval($saveStatus['is_saved']) . ',isAllSaved=' . intval($isAllSaved));
321 // 'is_saved' is still true?
322 if ($isAllSaved === TRUE) {
323 // Set 'done' and message
324 $saveStatus['status'] = 'done';
325 $saveStatus['message'] = '{--INSTALLER_SAVE_CHANGES_DONE--}';
327 // Then do the post-check
328 doInstallerPostCheck($currentTab, $saveStatus);
331 // Output the status array for JSON reply
332 setAjaxReplyContent(encodeJson($saveStatus));
334 // All okay if we reach this point
335 setHttpStatus('200 OK');
338 // ----------------------------------------------------------------------------
339 // Call-back functions for preparing installer page requests
340 // ----------------------------------------------------------------------------
342 // Prepare AJAX request 'welcome'
343 function doAjaxPrepareInstallerWelcome () {
344 // Kept empty to prevent logfile entry
347 // Prepare AJAX request 'base_data'
348 function doAjaxPrepareInstallerBaseData () {
349 // Is 'base_path' not set?
350 if (!isSessionVariableSet('base_path')) {
351 // Then set it from PATH
352 setSession('base_path', getPath());
355 // Is 'base_url' not set?
356 if (!isSessionVariableSet('base_url')) {
357 // Then set it from URL
358 setSession('base_url', getUrl());
361 // Is 'main_title' not set?
362 if (!isSessionVariableSet('main_title')) {
363 // Then set it from default main title
364 setSession('main_title', '{--DEFAULT_MAIN_TITLE--}');
367 // Is 'slogan' not set?
368 if (!isSessionVariableSet('slogan')) {
369 // Then set it from default slogan
370 setSession('slogan', '{--DEFAULT_SLOGAN--}');
373 // Is 'webmaster' not set?
374 if (!isSessionVariableSet('webmaster')) {
375 // Then set it from default webmaster email address
376 setSession('webmaster', '{--DEFAULT_WEBMASTER--}');
380 // Prepare AJAX request 'database_config'
381 function doAjaxPrepareInstallerDatabaseConfig () {
382 // Is 'mysql_host' not set?
383 if (!isSessionVariableSet('mysql_host')) {
384 // Then set it directly
385 setSession('mysql_host', 'localhost');
388 // Is 'mysql_dbase' not set?
389 if (!isSessionVariableSet('mysql_dbase')) {
390 // Then set it directly
391 setSession('mysql_dbase', 'your_database');
394 // Is 'mysql_prefix' not set?
395 if (!isSessionVariableSet('mysql_prefix')) {
396 // Then set it directly
397 setSession('mysql_prefix', 'mailer');
400 // Is 'mysql_login' not set?
401 if (!isSessionVariableSet('mysql_login')) {
402 // Then set it directly
403 setSession('mysql_login', 'your_login');
406 // Is 'mysql_dbase' not set?
407 if (!isSessionVariableSet('mysql_password1')) {
408 // Then set it directly
409 setSession('mysql_password1', '');
412 // Is 'mysql_password2' not set?
413 if (!isSessionVariableSet('mysql_password2')) {
414 // Then set it directly
415 setSession('mysql_password2', '');
418 // Is 'mysql_engine' not set?
419 if (!isSessionVariableSet('mysql_engine')) {
420 // Then set it directly
421 setSession('mysql_engine', 'MyISAM');
425 // Prepare AJAX request 'smtp_config'
426 function doAjaxPrepareInstallerSmtpConfig () {
427 // Kept empty to prevent logfile entry because SMTP settings are optional
430 // Prepare AJAX request 'other_config'
431 function doAjaxPrepareInstallerOtherConfig () {
432 // Is 'output_mode' not set?
433 if (!isSessionVariableSet('output_mode')) {
434 // Then set it directly
435 setSession('output_mode', 'render');
438 // Is 'warn_no_pass' not set?
439 if (!isSessionVariableSet('warn_no_pass')) {
440 // Then set it directly
441 setSession('warn_no_pass', 'Y');
444 // Is 'write_footer' not set?
445 if (!isSessionVariableSet('write_footer')) {
446 // Then set it directly
447 setSession('write_footer', 'Y');
450 // Is 'enable_backlink' not set?
451 if (!isSessionVariableSet('enable_backlink')) {
452 // Then set it directly
453 setSession('enable_backlink', 'Y');
457 // Prepare AJAX request 'extensions'
458 function doAjaxPrepareInstallerExtensions () {
459 // Is 'extensions' set?
460 if (!isSessionVariableSet('extensions')) {
462 * At least ext-sql_patches and ext-task should be installed
463 *(ext-sql_patches is a must!)
465 setSession('extensions', 'admins:sql_patches:task');
466 } elseif (strpos(getSession('extensions'), 'sql_patches') === FALSE) {
467 // Add missing ext-sql_patches
468 setSession('extensions', getSession('extensions') . ':sql_patches');
472 // Prepare AJAX request 'overview'
473 function doAjaxPrepareInstallerOverview () {
474 // 'tab' must always be set to create a post-check-callback
475 if (!isPostRequestElementSet('tab')) {
476 // This shall not happen
477 reportBug(__FUNCTION__, __LINE__, 'The JavaScript did not send "tab" which is fatal.');
480 // Save the tab for pre-"filtering"
481 $currentTab = postRequestElement('tab');
483 // Default is failed save attempt (e.g. nothing to save)
484 $verificationStatus = array(
485 // Status code, can be 'failed' or 'done'
486 'status' => 'failed',
487 // Status message (e.g. for output)
488 'message' => '{--INSTALLER_OVERVIEW_FINAL_CHECK_FAILED--}',
489 // Don't set this to false, or else it will be returned as 'failed' but is saved
492 'failed_fields' => array()
495 // Init overall status and final output
499 // Check all data in session
500 foreach (array_keys($GLOBALS['installer_groups']) as $key) {
501 // Get values from session
502 $value = getSession($key);
504 // Is the data valid?
505 $verificationStatus['is_valid'] = (isInstallerDataValid($verificationStatus, $key, $value));
507 // Is this step okay?
508 if ($verificationStatus['is_valid'] === TRUE) {
509 // Add this key/value pair to a overview group
510 addKeyValueToInstallerOverviewGroup($key, $value);
513 // Save the overall status for below final check
514 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'key=' . $key . ',value=' . $value . ',is_valid=' . intval($verificationStatus['is_valid']) . ',isAllValid=' . intval($isAllValid));
515 $isAllValid = (($isAllValid === TRUE) && ($verificationStatus['is_valid'] === TRUE));
519 if ((isInstallationDataCompleted()) && ($isAllValid === TRUE)) {
520 // Set 'done' and message
521 $verificationStatus['status'] = 'done';
522 $verificationStatus['message'] = '{--INSTALLER_OVERVIEW_FINAL_CHECK_DONE--}';
524 // Then do the post-check
525 doInstallerPostCheck($currentTab, $verificationStatus);
528 // Is it still valid?
529 if ($verificationStatus['status'] != 'done') {
531 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'Final check on all stored data failed. message=' . $verificationStatus['message']);
533 // Process failed fields
534 $verificationStatus['failed_fields'] = handleInstallerFailedFields($verificationStatus['failed_fields']);
536 // Output the array for JSON reply
537 setAjaxReplyContent(loadTemplate('install_overview_failed', TRUE, $verificationStatus));
540 * Something went wrong, this might happen when e.g. the user has tried
541 * to save invalid database login data but hit reload button on error
544 setHttpStatus('200 OK');
551 // ----------------------------------------------------------------------------
552 // Call-back functions for doing installation steps
553 // ----------------------------------------------------------------------------
555 // Call-back function to import first tables.sql file
556 function doAjaxInstallerStepImportTablesSql () {
557 // Establish database link
558 establishAjaxInstallerDatabaseLink();
564 importInstallSqlDump('tables');
566 // Are some SQLs found?
567 if (countSqls() == 0) {
569 reportBug(__FUNCTION__, __LINE__, '{--INSTALLER_SQL_IMPORT_FAILED--}');
572 // Now run all queries through
573 runFilterChain('run_sqls');
576 SQL_CLOSE(__FUNCTION__, __LINE__);
579 // Call-back function to import menu SQL file
580 function doAjaxInstallerStepImportMenuSql () {
581 // Establish database link
582 establishAjaxInstallerDatabaseLink();
588 importInstallSqlDump('menu-' . getLanguage());
590 // Are some SQLs found?
591 if (countSqls() == 0) {
593 reportBug(__FUNCTION__, __LINE__, '{--INSTALLER_SQL_IMPORT_FAILED--}');
596 // Now run all queries through
597 runFilterChain('run_sqls');
600 SQL_CLOSE(__FUNCTION__, __LINE__);
603 // Call-back function to install some important extensions
604 function doAjaxInstallerStepInstallExtensions () {
605 // Only one element is required
606 if (!isSessionVariableSet('extensions')) {
607 // Some required session data is not set
608 reportBug(__FUNCTION__, __LINE__, 'Required session data for this step not found.');
611 // Establish database link
612 establishAjaxInstallerDatabaseLink();
614 // Get all extensions
615 $extensions = explode(':', getSession('extensions'));
617 // Make sure ext-sql_patches is first
618 array_unshift($extensions, 'sql_patches');
620 // "Walk" through all extensions
621 foreach ($extensions as $key => $ext_name) {
622 // Is ext-sql_patches not at key=0?
623 if (($key == 0) && ($ext_name == 'sql_patches')) {
624 // Then skip this entry
626 } elseif ((!loadExtension($ext_name, 'test', '0.0.0', TRUE)) || (!registerExtension($ext_name, NULL))) {
628 reportBug(__FUNCTION__, __LINE__, 'Cannot load/register extension ' . $ext_name . '.');
633 SQL_CLOSE(__FUNCTION__, __LINE__);
636 // Call-back function to write local configuration file
637 function doAjaxInstallerStepWriteLocalConfig () {
639 if (!isSessionDataSet(array('base_path', 'base_url', 'main_title', 'slogan', 'webmaster', 'mysql_host', 'mysql_dbase', 'mysql_prefix', 'mysql_login', 'mysql_password1', 'mysql_password2', 'mysql_engine', 'output_mode', 'warn_no_pass', 'write_footer', 'enable_backlink'))) {
640 // Some required session data is not set
641 reportBug(__FUNCTION__, __LINE__, 'Required session data for this step not found.');
645 if (!doInstallWriteLocalConfigurationFile(
646 getSession('base_path'),
647 getSession('base_url'),
648 getSession('main_title'),
649 getSession('slogan'),
650 getSession('webmaster'),
651 getSession('warn_no_pass'),
652 getSession('write_footer'),
653 getSession('enable_backlink'),
654 getSession('mysql_host'),
655 getSession('mysql_dbase'),
656 getSession('mysql_login'),
657 getSession('mysql_password1'),
658 getSession('mysql_prefix'),
659 getSession('mysql_engine'),
660 getSession('smtp_host'),
661 getSession('smtp_user'),
662 getSession('smtp_password1')
664 // Something bad went wrong
665 removeFile(getSession('base_path') . getCachePath() . 'config-local.php');
666 reportBug(__FUNCTION__, __LINE__, 'Did not fully write config-local.php .');