-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathajax.php
147 lines (132 loc) · 3.98 KB
/
ajax.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
/**
* @package Joomla.Plugin
* @subpackage System.Plugin
* @copyright Copyright (C) 2013 Company, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
// No direct access.
defined('JPATH_BASE') or die;
jimport('joomla.filesystem.file');
jimport('joomla.application.helper');
jimport('legacy.application.helper');
/**
* Joomla! Ajax Plugin
*
* @package Joomla.Plugin
* @subpackage System.ajax
*/
class PlgSystemAjax extends JPlugin
{
public function onAfterRoute()
{
// Reference global application object
$this->app = JFactory::getApplication();
// Instantiate the JDispatcher class
$this->dispatcher = JDispatcher::getInstance();
// Jinput
$this->input = $this->app->input;
// check request mode
$request_mode = $this->params->get('ajax_mode','request_with');
switch ($request_mode)
{
case 'variable':
$request_param = $this->params->get('custom_request_variable','ajax');
$request_param_value = $this->input->getCmd($request_param);
if (!empty($request_param_value))
$this->doAjax();
break;
case 'option':
// check option request
if ($this->input->getCmd('component') == $this->params->get('custom_request_variable','com_ajax')) {
$this->doAjax();
}
break;
case 'request_with':
$requestWith = strtolower($this->input->server->get('HTTP_X_REQUESTED_WITH'));
if(!empty($requestWith) && $requestWith == 'xmlhttprequest') {
$this->doAjax();
}
break;
}
if (JFactory::getSession()->get('ajax', false)) {
$this->doAjax();
}
}
/**
* Request Ajax
*/
private function doAjax()
{
JFactory::getLanguage()->load('plg_system_ajax');
$request_format = $this->input->getCmd('format','raw');
$module = $this->input->getCmd('module');
$plugin = $this->input->getCmd('plugin');
$component = $this->input->getCmd('option');
$ajax_checktoken = $this->params->get('ajax_checktoken');
$enable_sufix = $this->params->get('ajax_use_prefix_token', false);
$ajax_prefix = '';
if ($enable_sufix == true) {
$ajax_prefix = $this->params->get('ajax_prefix');
}
if (JFactory::getSession()->get('ajax', false)) {
$messages = $this->app->getMessageQueue();
// Build the sorted message list
if (is_array($messages) && !empty($messages))
{
foreach ($messages as $msg)
{
if (isset($msg['type']) && isset($msg['message']))
{
$results[] = $msg['message'];
}
}
} else {
$results = array();
}
JFactory::getSession()->set('ajax', false);
}
if (empty($results) || !isset($results)) {
if ($module) {
$client = $this->input->getCmd('client','site');
$module = $this->input->getCmd('module');
$helper = $this->input->getCmd('helper', 'helper');
$class = $this->input->getCmd('class', 'mod' . ucfirst($module) . 'Helper');
$method = $this->input->getCmd('method', 'getAjax');
$client = JApplicationHelper::getClientInfo($client);
$helper_path = $client->path . '/modules/mod_' . $module . '/' . $helper . '.php';
if (JFile::exists($helper_path)) {
require_once $helper_path;
$results = $class::$method($params);
} else {
$results = array();
}
} else if ($plugin) {
JPluginHelper::importPlugin('ajax');
$plugin = ucfirst($plugin);
$results = $this->dispatcher->trigger('onAjax' . $plugin);
} else if ($component) {
JFactory::getSession()->set('ajax', true);
$this->app->dispatch();
} else {
$results = array();
}
}
// Return the results from this plugin group in the desired format
switch ($request_format) {
case 'jsonp':
$cb = $this->input->get('callback','callbackResponse');
$response = $cb.'('.$ajax_prefix.json_encode($results).$ajax_prefix.')';
break;
case 'json':
$response = $ajax_prefix.json_encode($results).$ajax_prefix;
break;
case 'raw':
default:
$response = $ajax_prefix.implode($results).$ajax_prefix;
break;
}
echo $response;
$this->app->close();
}
}