MINOR: Small code improvements
[mailer.git] / inc / ajax-functions.php
1 <?php
2 /************************************************************************
3  * Mailer v0.2.1-FINAL                                Start: 09/17/2011 *
4  * ===================                          Last change: 09/17/2011 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : ajax-functions.php                               *
8  * -------------------------------------------------------------------- *
9  * Short description : AJAX-related functions                           *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : AJAX-bezogene Funktionen                         *
12  * -------------------------------------------------------------------- *
13  * $Revision::                                                        $ *
14  * $Date::                                                            $ *
15  * $Tag:: 0.2.1-FINAL                                                 $ *
16  * $Author::                                                          $ *
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                      *
21  *                                                                      *
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.                                  *
26  *                                                                      *
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.                         *
31  *                                                                      *
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,               *
35  * MA  02110-1301  USA                                                  *
36  ************************************************************************/
37
38 // Some security stuff...
39 if (!defined('__SECURITY')) {
40         die();
41 } // END - if
42
43 // Init ajax request
44 function initAjax () {
45         // Init AJAX reply array
46         $GLOBALS['ajax_reply'] = array(
47                 // Raw content
48                 'reply_content' => NULL,
49         );
50
51         // Init call-back debug information
52         $GLOBALS['ajax_callback_function'] = NULL;
53
54         // Set content type (mostly JSON)
55         setContentType('application/json');
56
57         // By default nothing is found/valid
58         setHttpStatus('204 No Content');
59
60         // Set username
61         setUsername('{--USERNAME_AJAX--}');
62
63         // In installation phase load ajax_installer.php
64         if (isInstallationPhase()) {
65                 // Load it
66                 loadIncludeOnce('inc/ajax/ajax_installer.php');
67         } // END - if
68 }
69
70 // Setter for AJAX reply content
71 function setAjaxReplyContent ($content) {
72         // Log message
73         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'content()=' . strlen($content));
74
75         // Set it, but with URL encoding
76         $GLOBALS['ajax_reply']['reply_content'] = urlencode(doFinalCompilation($content, FALSE));
77 }
78
79 /**
80  * Checks whether the AJAX access level was valid. This function doesn't need
81  * caching in $GLOBALS[__FUNCTION__] because it will be called only once.
82  */
83 function isAjaxRequestLevelValid () {
84         // By default nothing is valid
85         $isValid = FALSE;
86
87         // Switch on the 'level' value
88         switch (postRequestElement('level')) {
89                 case 'install': // Installation phase level
90                         // Simply check for it
91                         $isValid = isInstallationPhase();
92                         break;
93
94                 case 'admin': // Admin area
95                         // Load admin includes
96                         loadIncludeOnce('inc/modules/admin/admin-inc.php');
97                         loadIncludeOnce('inc/ajax/ajax_admin.php');
98
99                         // Determine correct 'what' value
100                         $what = determineWhat();
101
102                         // Get action value
103                         $action = getActionFromModuleWhat('admin', $what);
104
105                         // Check condition
106                         $isValid = ((isAdmin()) && (isAdminAllowedAccessMenu($action, $what)));
107                         break;
108
109                 default: // Unsupported level (please report this!)
110                         logDebugMessage(__FUNCTION__, __LINE__, 'Unsupported AJAX request access level ' . postRequestElement('level') . ' detected. Please report this, it means this part is not finished.');
111                         break;
112         } // END - switch
113
114         // Return validation result
115         return $isValid;
116 }
117
118 // Processes the AJAX request by generating a call-back on given 'level' value
119 function processAjaxRequest () {
120         // Generate the call-back function name
121         $callbackName = 'doAjaxProcess' . capitalizeUnderscoreString(postRequestElement('level'));
122
123         // Is the call-back function there?
124         if (!function_exists($callbackName)) {
125                 // This shall not happen
126                 reportBug(__FUNCTION__, __LINE__, 'AJAX call-back ' . $callbackName . ' does not exist.');
127         } // END - if
128
129         // Call the function
130         call_user_func($callbackName);
131 }
132
133 // Send AJAX content
134 function sendAjaxContent () {
135         // Is the status fine or template not found (404)?
136         if (isAjaxHttpStatusAccepted()) {
137                 // Then output the JSON
138                 outputHtml(encodeJson($GLOBALS['ajax_reply']));
139         } // END - if
140 }
141
142 // Checks whether the HTTP status is accepted
143 function isAjaxHttpStatusAccepted () {
144         // Is it accepted?
145         $isAccepted = in_array(strtoupper(getHttpStatus()), array('200 OK', '404 NOT FOUND'));
146
147         // Return it
148         return $isAccepted;
149 }
150
151 // [EOF]
152 ?>