[ Index ] |
PHP Cross Reference of Joomla 4.2.2 documentation |
[Summary view] [Print] [Text view]
1 <?php 2 3 /** 4 * @package Joomla.Site 5 * @subpackage com_ajax 6 * 7 * @copyright (C) 2013 Open Source Matters, Inc. <https://www.joomla.org> 8 * @license GNU General Public License version 2 or later; see LICENSE.txt 9 */ 10 11 defined('_JEXEC') or die; 12 13 use Joomla\CMS\Factory; 14 use Joomla\CMS\Language\Text; 15 use Joomla\CMS\Log\Log; 16 use Joomla\CMS\Plugin\PluginHelper; 17 use Joomla\CMS\Response\JsonResponse; 18 use Joomla\CMS\Table\Table; 19 20 /* 21 * References 22 * Support plugins in your component 23 * - https://docs.joomla.org/Special:MyLanguage/Supporting_plugins_in_your_component 24 * 25 * Best way for JSON output 26 * - https://groups.google.com/d/msg/joomla-dev-cms/WsC0nA9Fixo/Ur-gPqpqh-EJ 27 */ 28 29 /** @var \Joomla\CMS\Application\CMSApplication $app */ 30 $app = Factory::getApplication(); 31 $app->allowCache(false); 32 33 // Prevent the api url from being indexed 34 $app->setHeader('X-Robots-Tag', 'noindex, nofollow'); 35 36 // JInput object 37 $input = $app->input; 38 39 // Requested format passed via URL 40 $format = strtolower($input->getWord('format', '')); 41 42 // Initialize default response and module name 43 $results = null; 44 $parts = null; 45 46 // Check for valid format 47 if (!$format) { 48 $results = new InvalidArgumentException(Text::_('COM_AJAX_SPECIFY_FORMAT'), 404); 49 } elseif ($input->get('module')) { 50 /** 51 * Module support. 52 * 53 * modFooHelper::getAjax() is called where 'foo' is the value 54 * of the 'module' variable passed via the URL 55 * (i.e. index.php?option=com_ajax&module=foo). 56 * 57 */ 58 $module = $input->get('module'); 59 $table = Table::getInstance('extension'); 60 $moduleId = $table->find(array('type' => 'module', 'element' => 'mod_' . $module)); 61 62 if ($moduleId && $table->load($moduleId) && $table->enabled) { 63 $helperFile = JPATH_BASE . '/modules/mod_' . $module . '/helper.php'; 64 65 if (strpos($module, '_')) { 66 $parts = explode('_', $module); 67 } elseif (strpos($module, '-')) { 68 $parts = explode('-', $module); 69 } 70 71 if ($parts) { 72 $class = 'Mod'; 73 74 foreach ($parts as $part) { 75 $class .= ucfirst($part); 76 } 77 78 $class .= 'Helper'; 79 } else { 80 $class = 'Mod' . ucfirst($module) . 'Helper'; 81 } 82 83 $method = $input->get('method') ?: 'get'; 84 85 $moduleInstance = $app->bootModule('mod_' . $module, $app->getName()); 86 87 if ($moduleInstance instanceof \Joomla\CMS\Helper\HelperFactoryInterface && $helper = $moduleInstance->getHelper(substr($class, 3))) { 88 $results = method_exists($helper, $method . 'Ajax') ? $helper->{$method . 'Ajax'}() : null; 89 } 90 91 if ($results === null && is_file($helperFile)) { 92 JLoader::register($class, $helperFile); 93 94 if (method_exists($class, $method . 'Ajax')) { 95 // Load language file for module 96 $basePath = JPATH_BASE; 97 $lang = Factory::getLanguage(); 98 $lang->load('mod_' . $module, $basePath) 99 || $lang->load('mod_' . $module, $basePath . '/modules/mod_' . $module); 100 101 try { 102 $results = call_user_func($class . '::' . $method . 'Ajax'); 103 } catch (Exception $e) { 104 $results = $e; 105 } 106 } else { 107 // Method does not exist 108 $results = new LogicException(Text::sprintf('COM_AJAX_METHOD_NOT_EXISTS', $method . 'Ajax'), 404); 109 } 110 } elseif ($results === null) { 111 // The helper file does not exist 112 $results = new RuntimeException(Text::sprintf('COM_AJAX_FILE_NOT_EXISTS', 'mod_' . $module . '/helper.php'), 404); 113 } 114 } else { 115 // Module is not published, you do not have access to it, or it is not assigned to the current menu item 116 $results = new LogicException(Text::sprintf('COM_AJAX_MODULE_NOT_ACCESSIBLE', 'mod_' . $module), 404); 117 } 118 } elseif ($input->get('plugin')) { 119 /** 120 * Plugin support by default is based on the "Ajax" plugin group. 121 * An optional 'group' variable can be passed via the URL. 122 * 123 * The plugin event triggered is onAjaxFoo, where 'foo' is 124 * the value of the 'plugin' variable passed via the URL 125 * (i.e. index.php?option=com_ajax&plugin=foo) 126 * 127 */ 128 $group = $input->get('group', 'ajax'); 129 PluginHelper::importPlugin($group); 130 $plugin = ucfirst($input->get('plugin')); 131 132 try { 133 $results = Factory::getApplication()->triggerEvent('onAjax' . $plugin); 134 } catch (Exception $e) { 135 $results = $e; 136 } 137 } elseif ($input->get('template')) { 138 /** 139 * Template support. 140 * 141 * tplFooHelper::getAjax() is called where 'foo' is the value 142 * of the 'template' variable passed via the URL 143 * (i.e. index.php?option=com_ajax&template=foo). 144 * 145 */ 146 $template = $input->get('template'); 147 $table = Table::getInstance('extension'); 148 $templateId = $table->find(array('type' => 'template', 'element' => $template)); 149 150 if ($templateId && $table->load($templateId) && $table->enabled) { 151 $basePath = ($table->client_id) ? JPATH_ADMINISTRATOR : JPATH_SITE; 152 $helperFile = $basePath . '/templates/' . $template . '/helper.php'; 153 154 if (strpos($template, '_')) { 155 $parts = explode('_', $template); 156 } elseif (strpos($template, '-')) { 157 $parts = explode('-', $template); 158 } 159 160 if ($parts) { 161 $class = 'Tpl'; 162 163 foreach ($parts as $part) { 164 $class .= ucfirst($part); 165 } 166 167 $class .= 'Helper'; 168 } else { 169 $class = 'Tpl' . ucfirst($template) . 'Helper'; 170 } 171 172 $method = $input->get('method') ?: 'get'; 173 174 if (is_file($helperFile)) { 175 JLoader::register($class, $helperFile); 176 177 if (method_exists($class, $method . 'Ajax')) { 178 // Load language file for template 179 $lang = Factory::getLanguage(); 180 $lang->load('tpl_' . $template, $basePath) 181 || $lang->load('tpl_' . $template, $basePath . '/templates/' . $template); 182 183 try { 184 $results = call_user_func($class . '::' . $method . 'Ajax'); 185 } catch (Exception $e) { 186 $results = $e; 187 } 188 } else { 189 // Method does not exist 190 $results = new LogicException(Text::sprintf('COM_AJAX_METHOD_NOT_EXISTS', $method . 'Ajax'), 404); 191 } 192 } else { 193 // The helper file does not exist 194 $results = new RuntimeException(Text::sprintf('COM_AJAX_FILE_NOT_EXISTS', 'tpl_' . $template . '/helper.php'), 404); 195 } 196 } else { 197 // Template is not assigned to the current menu item 198 $results = new LogicException(Text::sprintf('COM_AJAX_TEMPLATE_NOT_ACCESSIBLE', 'tpl_' . $template), 404); 199 } 200 } 201 202 // Return the results in the desired format 203 switch ($format) { 204 // JSONinzed 205 case 'json': 206 echo new JsonResponse($results, null, false, $input->get('ignoreMessages', true, 'bool')); 207 208 break; 209 210 // Handle as raw format 211 default: 212 // Output exception 213 if ($results instanceof Exception) { 214 // Log an error 215 Log::add($results->getMessage(), Log::ERROR); 216 217 // Set status header code 218 $app->setHeader('status', $results->getCode(), true); 219 220 // Echo exception type and message 221 $out = get_class($results) . ': ' . $results->getMessage(); 222 } elseif (is_scalar($results)) { 223 // Output string/ null 224 $out = (string) $results; 225 } else { 226 // Output array/ object 227 $out = implode((array) $results); 228 } 229 230 echo $out; 231 232 break; 233 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Wed Sep 7 05:41:13 2022 | Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer |