0971db9144e293067272083dfc4d67cc79897360
[Packages/TYPO3.CMS.git] / t3lib / class.t3lib_ajax.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2004-2013 Kasper Skårhøj (kasperYYYY@typo3.com)
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 * A copy is found in the textfile GPL.txt and important notices to the license
17 * from the author is found in LICENSE.txt distributed with these scripts.
18 *
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27 /**
28 * Contains the class "t3lib_ajax" containing functions for doing XMLHTTP requests
29 * to the TYPO3 backend and as well for generating replys. This technology is also known as ajax.
30 * Call ALL methods without making an object!
31 *
32 * IMPORTANT NOTICE: The API the class provides is still NOT STABLE and SUBJECT TO CHANGE!
33 * It is planned to integrate an external AJAX library, so the API will most likely change again.
34 *
35 * TYPO3 XMLHTTP class (new in TYPO3 4.0.0)
36 * This class contains two main parts:
37 * (1) generation of JavaScript code which creates an XMLHTTP object in a cross-browser manner
38 * (2) generation of XML data as a reply
39 *
40 * @author Sebastian Kurfürst <sebastian@garbage-group.de>
41 * @deprecated since 6.0, the class will be removed from core with 6.2
42 */
43 class t3lib_ajax {
44
45 /**
46 * Default constructor writes deprecation log.
47 */
48 public function __construct() {
49 \TYPO3\CMS\Core\Utility\GeneralUtility::deprecationLog('Class t3lib_ajax is deprecated and unused since TYPO3 6.0. ' . 'It will be removed with version 6.2.');
50 }
51
52 /**
53 * Gets the JavaScript code needed to handle an XMLHTTP request in the frontend.
54 * All JS functions have to call ajax_doRequest(url) to make a request to the server.
55 * USE:
56 * See examples of using this function in template.php -> getContextMenuCode and alt_clickmenu.php -> printContent
57 *
58 * @param string $handler Function JS function handling the XML data from the server. That function gets the returned XML data as parameter.
59 * @param string $fallback JS fallback function which is called with the URL of the request in case ajax is not available.
60 * @param boolean $debug If set to 1, the returned XML data is outputted as text in an alert window - useful for debugging, PHP errors are shown there, ...
61 * @return string JavaScript code needed to make and handle an XMLHTTP request
62 * @deprecated since 6.0, class will be removed with 6.2
63 */
64 public function getJScode($handlerFunction, $fallback = '', $debug = 0) {
65 \TYPO3\CMS\Core\Utility\GeneralUtility::logDeprecatedFunction();
66 // Init the XMLHTTP request object
67 $code = '
68 function ajax_initObject() {
69 var A;
70 try {
71 A=new ActiveXObject("Msxml2.XMLHTTP");
72 } catch (e) {
73 try {
74 A=new ActiveXObject("Microsoft.XMLHTTP");
75 } catch (oc) {
76 A=null;
77 }
78 }
79 if (!A && typeof XMLHttpRequest != "undefined") {
80 A = new XMLHttpRequest();
81 }
82 return A;
83 }';
84 // In case AJAX is not available, fallback function
85 if ($fallback) {
86 $fallback .= '(url)';
87 } else {
88 $fallback = 'return';
89 }
90 $code .= '
91 function ajax_doRequest(url) {
92 var x;
93
94 x = ajax_initObject();
95 if (!x) {
96 ' . $fallback . ';
97 }
98 x.open("GET", url, true);
99
100 x.onreadystatechange = function() {
101 if (x.readyState != 4) {
102 return;
103 }
104 ' . ($debug ? 'alert(x.responseText)' : '') . '
105 var xmldoc = x.responseXML;
106 var t3ajax = xmldoc.getElementsByTagName("t3ajax")[0];
107 ' . $handlerFunction . '(t3ajax);
108 }
109 x.send("");
110
111 delete x;
112 }';
113 return $code;
114 }
115
116 /**
117 * Function outputting XML data for TYPO3 ajax. The function directly outputs headers and content to the browser.
118 *
119 * @param string $innerXML XML data which will be sent to the browser
120 * @return void
121 * @deprecated since 6.0, class will be removed with 6.2
122 */
123 public function outputXMLreply($innerXML) {
124 \TYPO3\CMS\Core\Utility\GeneralUtility::logDeprecatedFunction();
125 // AJAX needs some XML data
126 header('Content-Type: text/xml');
127 $xml = '<?xml version="1.0"?>
128 <t3ajax>' . $innerXML . '</t3ajax>';
129 echo $xml;
130 }
131
132 }
133
134
135 ?>