Updated copyright year.
[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  * Copyright (c) 2003 - 2009 by Roland Haeder                           *
14  * Copyright (c) 2009 - 2016 by Mailer Developer Team                   *
15  * For more information visit: http://mxchange.org                      *
16  *                                                                      *
17  * This program is free software; you can redistribute it and/or modify *
18  * it under the terms of the GNU General Public License as published by *
19  * the Free Software Foundation; either version 2 of the License, or    *
20  * (at your option) any later version.                                  *
21  *                                                                      *
22  * This program is distributed in the hope that it will be useful,      *
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
25  * GNU General Public License for more details.                         *
26  *                                                                      *
27  * You should have received a copy of the GNU General Public License    *
28  * along with this program; if not, write to the Free Software          *
29  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
30  * MA  02110-1301  USA                                                  *
31  ************************************************************************/
32
33 // Some security stuff...
34 if (!defined('__SECURITY')) {
35         die();
36 } // END - if
37
38 // Init ajax request
39 function initAjax () {
40         // Init AJAX reply array
41         $GLOBALS['ajax_reply'] = array(
42                 // Raw content
43                 'reply_content' => NULL,
44         );
45
46         // Init call-back debug information
47         $GLOBALS['ajax_callback_function'] = NULL;
48
49         // Set content type (mostly JSON)
50         setContentType('application/json');
51
52         // By default nothing is found/valid
53         setHttpStatus('204 No Content');
54
55         // Set username
56         setUsername('{--USERNAME_AJAX--}');
57
58         // In installation phase load ajax_installer.php
59         if (isInstaller()) {
60                 // Load it
61                 loadIncludeOnce('inc/ajax/ajax_installer.php');
62         } // END - if
63 }
64
65 // Setter for AJAX reply content
66 function setAjaxReplyContent ($content) {
67         // Log message
68         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'content()=' . strlen($content));
69         //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'content[]=' . gettype($content));
70
71         // Set it, but with URL encoding
72         $GLOBALS['ajax_reply']['reply_content'] = urlencode(doFinalCompilation($content, FALSE));
73 }
74
75 /**
76  * Checks whether the AJAX access level was valid. This function doesn't need
77  * caching in $GLOBALS[__FUNCTION__] because it will be called only once.
78  */
79 function isValidAjaxRequestLevel () {
80         // By default nothing is valid
81         $isValid = FALSE;
82
83         // Switch on the 'level' value
84         switch (postRequestElement('level')) {
85                 case 'install': // Installation phase level
86                         // Simply check for it
87                         $isValid = isInstaller();
88                         break;
89
90                 case 'admin': // Admin area
91                         // Load admin includes
92                         loadIncludeOnce('inc/modules/admin/admin-inc.php');
93                         loadIncludeOnce('inc/ajax/ajax_admin.php');
94
95                         // Determine correct 'what' value
96                         $what = determineWhat();
97
98                         // Get action value
99                         $action = getActionFromModuleWhat('admin', $what);
100
101                         // Check condition
102                         $isValid = ((isAdmin()) && (isAdminAllowedAccessMenu($action, $what)));
103                         break;
104
105                 default: // Unsupported level (please report this!)
106                         logDebugMessage(__FUNCTION__, __LINE__, 'Unsupported AJAX request access level ' . postRequestElement('level') . ' detected. Please report this, it means this part is not finished.');
107                         break;
108         } // END - switch
109
110         // Return validation result
111         return $isValid;
112 }
113
114 // Processes the AJAX request by generating a call-back on given 'level' value
115 function processAjaxRequest () {
116         // Generate the call-back function name
117         $callbackName = 'doAjaxProcess' . capitalizeUnderscoreString(postRequestElement('level'));
118
119         // Is the call-back function there?
120         if (!function_exists($callbackName)) {
121                 // This shall not happen
122                 reportBug(__FUNCTION__, __LINE__, 'AJAX call-back ' . $callbackName . ' does not exist.');
123         } // END - if
124
125         // Call the function
126         call_user_func($callbackName);
127 }
128
129 // Send AJAX content
130 function sendAjaxContent ($forceOutput = FALSE) {
131         // Is the status fine or template not found (404)?
132         if ((isAjaxHttpStatusAccepted()) || ($forceOutput === TRUE)) {
133                 // Then output the JSON
134                 outputHtml(encodeJson($GLOBALS['ajax_reply']));
135         } // END - if
136 }
137
138 // Checks whether the HTTP status is accepted
139 function isAjaxHttpStatusAccepted () {
140         // Is it accepted?
141         $isAccepted = in_array(strtoupper(getHttpStatus()), array('200 OK', '404 NOT FOUND'));
142
143         // Return it
144         return $isAccepted;
145 }
146
147 // [EOF]
148 ?>