root/trunk/gregarius/extlib/Sajax.php

Revision 1695, 5.6 kB (checked in by mbonetti, 19 months ago)

more fixes for IE

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1<?php   
2if (!isset($SAJAX_INCLUDED)) {
3
4        /* 
5         * GLOBALS AND DEFAULTS
6         *
7         */ 
8        $sajax_debug_mode = 0;
9        $sajax_export_list = array();
10        $sajax_request_type = "GET";
11        $sajax_remote_uri = "";
12       
13        /*
14         * CODE
15         *
16         */ 
17        function sajax_init() {
18        }
19       
20        function sajax_get_my_uri() {
21                global $REQUEST_URI;
22               
23                return $REQUEST_URI;
24        }
25       
26        $sajax_remote_uri = sajax_get_my_uri();
27
28        function sajax_handle_client_request() {
29                global $sajax_export_list;
30               
31                $mode = "";
32               
33                if (! empty($_GET["rs"])) 
34                        $mode = "get";
35               
36                if (!empty($_POST["rs"]))
37                        $mode = "post";
38                       
39                if (empty($mode)) 
40                        return;
41
42                if ($mode == "get") {
43                        // Bust cache in the head
44                        header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT");    // Date in the past
45                        header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
46                        // always modified
47                        header ("Cache-Control: no-cache, must-revalidate");  // HTTP/1.1
48                        header ("Pragma: no-cache");                          // HTTP/1.0
49                        $func_name = $_GET["rs"];
50                        if (! empty($_GET["rsargs"])) 
51                                $args = $_GET["rsargs"];
52                        else
53                                $args = array();
54                }
55                else {
56                        $func_name = $_POST["rs"];
57                        if (! empty($_POST["rsargs"])) 
58                                $args = $_POST["rsargs"];
59                        else
60                                $args = array();
61                }
62               
63                if (! in_array($func_name, $sajax_export_list))
64                        echo "-:$func_name not callable";
65                else {
66                        echo "+:";
67                        $result = call_user_func_array($func_name, $args);
68                        echo $result;
69                }
70                exit;
71        }
72       
73        function sajax_get_common_js() {
74                global $sajax_debug_mode;
75                global $sajax_request_type;
76                global $sajax_remote_uri;
77               
78                $t = strtoupper($sajax_request_type);
79                if ($t != "GET" && $t != "POST") 
80                        return "// Invalid type: $t.. \n\n";
81               
82                ob_start();
83                ?>
84               
85                // remote scripting library
86                // (c) copyright 2005 modernmethod, inc
87                var sajax_debug_mode = <?php echo $sajax_debug_mode ? "true" : "false"; ?>;
88                var sajax_request_type = "<?php echo $t; ?>";
89               
90                function sajax_debug(text) {
91                        if (sajax_debug_mode)
92                                alert("RSD: " + text)
93                }
94                function sajax_init_object() {
95                        sajax_debug("sajax_init_object() called..")
96                       
97                        var A;
98                        try {
99                                A=new ActiveXObject("Msxml2.XMLHTTP");
100                        } catch (e) {
101                                try {
102                                        A=new ActiveXObject("Microsoft.XMLHTTP");
103                                } catch (oc) {
104                                        A=null;
105                                }
106                        }
107                        if(!A && typeof XMLHttpRequest != "undefined")
108                                A = new XMLHttpRequest();
109                        if (!A)
110                                sajax_debug("Could not create connection object.");
111                        return A;
112                }
113                function sajax_do_call(func_name, args) {
114                        var i, x, n;
115                        var uri;
116                        var post_data;
117                       
118                        uri = "<?php echo $sajax_remote_uri; ?>";
119                        if (sajax_request_type == "GET") {
120                                if (uri.indexOf("?") == -1)
121                                        uri = uri + "?rs=" + escape(func_name);
122                                else
123                                        uri = uri + "&rs=" + escape(func_name);
124                                for (i = 0; i < args.length-1; i++)
125                                        uri = uri + "&rsargs[]=" + escape(args[i]);
126                                uri = uri + "&rsrnd=" + new Date().getTime();
127                                post_data = null;
128                        } else {
129                                post_data = "rs=" + escape(func_name);
130                                for (i = 0; i < args.length-1; i++)
131                                        post_data = post_data + "&rsargs[]=" + escape(args[i]);
132                        }
133                       
134                        rss_sajax_busy();
135                       
136                        x = sajax_init_object();
137                        x.open(sajax_request_type, uri, true);
138                        if (sajax_request_type == "POST") {
139                                x.setRequestHeader("Method", "POST " + uri + " HTTP/1.1");
140                                x.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
141                        }
142                        x.onreadystatechange = function() {
143                                if (x.readyState != 4)
144                                        return;
145                                sajax_debug("received " + x.responseText);
146                               
147                                var status;
148                                var data;
149                                status = x.responseText.charAt(0);
150                                data = x.responseText.substring(2);
151                                if (status == "-") {
152                                        alert("Error: " + data);
153                                } else {
154                                        args[args.length-1](data);
155                                }
156                                rss_sajax_unbusy();
157                        }
158                        sajax_debug(func_name + " uri = " + uri + "\n/post = " + post_data);
159                        x.send(post_data);
160                        sajax_debug(func_name + " waiting..");
161                        delete x;
162                }
163               
164                function rss_sajax_unbusy() {
165                        span = document.getElementById('ajax_throbber');
166                        if (span) {
167                                devnull = span.parentNode.removeChild(span);
168                        }
169                       
170                }
171               
172                function rss_sajax_busy() {                     
173                        var span=document.createElement('span');
174                        span.id='ajax_throbber';
175                        span.style.position=(document.all?'absolute':'fixed');
176                        span.style.left = '5px';
177        span.style.top = '5px';
178        span.style.padding = '3px';
179        span.className = 'frame';       
180        span.innerHTML = '<img src="<?php echo getExternalThemeFile('media/busy.gif'); ?>" />';
181                        document.body.appendChild(span);
182                }
183               
184                <?php
185                $html = ob_get_contents();
186                ob_end_clean();
187                return $html;
188        }
189       
190        function sajax_show_common_js() {
191                echo sajax_get_common_js();
192        }
193       
194        // javascript escape a value
195        function sajax_esc($val)
196        {
197                return str_replace('"', '\\\\"', $val);
198        }
199
200        function sajax_get_one_stub($func_name) {
201                ob_start();     
202                ?>
203               
204                // wrapper for <?php echo $func_name; ?>
205               
206                function x_<?php echo $func_name; ?>() {
207                        sajax_do_call("<?php echo $func_name; ?>",
208                                x_<?php echo $func_name; ?>.arguments);
209                }
210               
211                <?php
212                $html = ob_get_contents();
213                ob_end_clean();
214                return $html;
215        }
216       
217        function sajax_show_one_stub($func_name) {
218                echo sajax_get_one_stub($func_name);
219        }
220       
221        function sajax_export() {
222                global $sajax_export_list;
223               
224                $n = func_num_args();
225                for ($i = 0; $i < $n; $i++) {
226                        $sajax_export_list[] = func_get_arg($i);
227                }
228        }
229       
230        $sajax_js_has_been_shown = 0;
231        function sajax_get_javascript()
232        {
233                global $sajax_js_has_been_shown;
234                global $sajax_export_list;
235               
236                $html = "";
237                if (! $sajax_js_has_been_shown) {
238                        $html .= sajax_get_common_js();
239                        $sajax_js_has_been_shown = 1;
240                }
241                foreach ($sajax_export_list as $func) {
242                        $html .= sajax_get_one_stub($func);
243                }
244                return $html;
245        }
246       
247        function sajax_show_javascript()
248        {
249                echo sajax_get_javascript();
250        }
251   
252       
253        $SAJAX_INCLUDED = 1;
254}
255?>
Note: See TracBrowser for help on using the browser.