PageRenderTime 55ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/mambots/system/pc_includes/ajax.php

https://bitbucket.org/dgough/annamaria-daneswood-25102012
PHP | 505 lines | 292 code | 100 blank | 113 comment | 79 complexity | e5038e4747945869297506b0aa73e401 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. (defined('_VALID_MOS') or defined('_JEXEC')) or die('Direct Access to this location is not allowed.');
  3. define('JAX_SITE_ROOT', dirname(dirname(dirname(dirname(__FILE__)))));
  4. define('AZRUL_SYSTEM' , '1.2');
  5. if(!defined('CMSLIB_DEFINED')){
  6. require(JAX_SITE_ROOT . '/components/libraries/cmslib/spframework.php');
  7. }
  8. global $jaxFuncNames;
  9. $jaxFuncNames = array();
  10. if(!class_exists('JAXResponse')){
  11. class JAXResponse
  12. {
  13. var $_response = null;
  14. function JAXResponse(){
  15. $this->_response = array();
  16. }
  17. function object_to_array($obj) {
  18. $_arr = is_object($obj) ? get_object_vars($obj) : $obj;
  19. $arr = array();
  20. foreach ($_arr as $key => $val) {
  21. $val = (is_array($val) || is_object($val)) ? $this->object_to_array($val) : $val;
  22. $arr[$key] = $val;
  23. }
  24. return $arr;
  25. }
  26. /**
  27. * Assign new sData to the $sTarget's $sAttribute property
  28. */
  29. function addAssign($sTarget,$sAttribute,$sData){
  30. //$sData = $this->_hackString($sData);
  31. //$sData = preg_replace("((\r\n)+)", '', $sData);
  32. $this->_response[] = array('as', $sTarget, $sAttribute, $sData);
  33. }
  34. /**
  35. * Clear the given target property
  36. */
  37. function addClear($sTarget,$sAttribute){
  38. $this->_response[] = array('as', $sTarget, $sAttribute, "");
  39. }
  40. function addCreate($sParent, $sTag, $sId, $sType=""){
  41. $this->_response[] = array('ce', $sParent, $sTag, $sId);
  42. }
  43. function addRemove($sTarget){
  44. $this->_response[] = array('rm', $sTarget);
  45. }
  46. /**
  47. * Assign new sData to the $sTarget's $sAttribute property
  48. */
  49. function addAlert($sData){
  50. $this->_response[] = array('al', "", "", $sData);
  51. }
  52. function _hackString($str){
  53. # Convert '{' and '}' to 0x7B and 0x7D
  54. //$str = str_replace(array('{', '}'), array('&#123;', '&#125;'), $str);
  55. return $str;
  56. }
  57. /**
  58. * Add a script call
  59. */
  60. function addScriptCall($func){
  61. $size = func_num_args();
  62. $response = "";
  63. if($size > 1){
  64. $response = array();
  65. for ($i = 1; $i < $size; $i++) {
  66. $arg = func_get_arg($i);
  67. $response[] = $arg;
  68. }
  69. }
  70. $this->_response[] = array('cs', $func, "", $response);
  71. }
  72. function encodeString($contents){
  73. $ascii = '';
  74. $strlen_var = strlen($contents);
  75. /*
  76. * Iterate over every character in the string,
  77. * escaping with a slash or encoding to UTF-8 where necessary
  78. */
  79. for ($c = 0; $c < $strlen_var; ++$c) {
  80. $ord_var_c = ord($contents{$c});
  81. switch ($ord_var_c) {
  82. case 0x08: $ascii .= '\b'; break;
  83. case 0x09: $ascii .= '\t'; break;
  84. case 0x0A: $ascii .= '\n'; break;
  85. case 0x0C: $ascii .= '\f'; break;
  86. case 0x0D: $ascii .= '\r'; break;
  87. default:
  88. $ascii .= $contents{$c};
  89. }
  90. }
  91. return $ascii;
  92. //return $this->_hackString($ascii);
  93. }
  94. /**
  95. * Flush the output back
  96. */
  97. function sendResponse(){
  98. global $mainframe;
  99. $cms =& cmsInstance('CMSCore');
  100. $obEnabled = ini_get('output_buffering');
  101. if($obEnabled == "1" || $obEnabled == 'On'){
  102. $ob_active = ob_get_length () !== FALSE;
  103. if($ob_active){
  104. while (@ ob_end_clean());
  105. if(function_exists('ob_clean')){
  106. @ob_clean();
  107. }
  108. }
  109. ob_start();
  110. }
  111. // Send text/html if we're using iframe
  112. if(isset($_GET['func'])){
  113. $lang = 'english';
  114. // loads english language file by default
  115. if ($mainframe->getCfg('lang') != '') {
  116. $lang =& $mainframe->getCfg('lang');
  117. }
  118. // If the $lang is still empty, force it to be english
  119. if($lang == '' || empty($lang))
  120. $lang = 'english';
  121. if(cmsVersion() == _CMS_JOOMLA10 || cmsVersion() == _CMS_MAMBO){
  122. include_once( JAX_SITE_ROOT .'/language/' . $lang . '.php' );
  123. $iso = explode( '=', _ISO );
  124. }elseif(cmsVersion() == _CMS_JOOMLA15){
  125. //no language file inclusion needed as it is loaded by joomla core
  126. //joomla's encoding is UTF-8
  127. $iso = 'UTF-8';
  128. }
  129. header("Content-type: text/html; $iso");
  130. }else
  131. header('Content-type: text/plain');
  132. if(!defined('SERVICES_JSON_SLICE'))
  133. include_once($cms->get_path('plugins') . '/system/pc_includes/JSON.php');
  134. $json = new Services_JSON();
  135. # Encode '{' and '}' characters
  136. # convert a complex value to JSON notation
  137. $output = $json->encode($this->_response);
  138. if(isset($_GET['func']))
  139. $output = "<body onload=\"parent.jax_iresponse();\">" . htmlentities($output). "</body>";
  140. echo(($output));
  141. exit;
  142. }
  143. }
  144. class JAX
  145. {
  146. var $_html = "";
  147. var $_funct = null;
  148. var $_path = "";
  149. var $livePath = "";
  150. var $_reqURL = "";
  151. var $_param = null;
  152. function JAX($livepath, $param= null){
  153. $this->_funct = array();
  154. $this->_path = dirname(__FILE__);
  155. $this->_livePath = $livepath;
  156. $this->_param = $param;
  157. }
  158. function registerFunction($mFunction){
  159. $this->_funct[] = $mFunction;
  160. }
  161. function addAssign(){
  162. }
  163. function setReqURI($reqURL){
  164. $this->_reqURL = $this->_fixHTTPS($reqURL);
  165. }
  166. function _fixHTTPS($content){
  167. global $mainframe;
  168. $reqURI = $mainframe->getCfg('live_site');
  169. # if host have wwww, but mosConfig doesn't
  170. if(isset($_SERVER['HTTP_HOST'])){
  171. if((substr_count($_SERVER['HTTP_HOST'], "www.") != 0) && (substr_count($reqURI, "www.") == 0)) {
  172. $reqURI = str_replace("://", "://www.", $reqURI);
  173. } else if((substr_count($_SERVER['HTTP_HOST'], "www.") == 0) && (substr_count($reqURI, "www.") != 0)) {
  174. // host do not have 'www' but mosConfig does
  175. $reqURI = str_replace("www.", "", $reqURI);
  176. }
  177. }
  178. /* Check for HTTPS */
  179. if(isset($_SERVER)){
  180. if(isset($_SERVER['HTTPS'])){
  181. if(strtolower($_SERVER['HTTPS']) == "on" ){
  182. $reqURI = str_replace("http://", "https://", $reqURI);
  183. }
  184. }
  185. } else if(isset($_SERVER['REQUEST_URI'])) {
  186. // use REQUEST_URI method
  187. if(strpos($_SERVER['REQUEST_URI'], 'https://') === FALSE){
  188. // not a https
  189. } else {
  190. $reqURI = str_replace("http://", "https://", $reqURI);
  191. }
  192. }
  193. return str_replace($mainframe->getCfg('live_site'), $reqURI, $content);
  194. }
  195. /**
  196. * Return the script thatshould be added to the <head> section
  197. */
  198. function getScript(){
  199. global $mainframe;
  200. $reqURI = $this->_reqURL;
  201. $compress = $mainframe->getCfg('gzip');
  202. # if host have wwww, but mosConfig doesn't
  203. //if (substr($_SERVER['HTTP_HOST'], 0, 4) == "www.") {
  204. if((substr_count(@$_SERVER['HTTP_HOST'], "www.") != 0) && (substr_count($reqURI, "www.") == 0)) {
  205. $reqURI = str_replace("://", "://www.", $reqURI);
  206. } else if((substr_count(@$_SERVER['HTTP_HOST'], "www.") == 0) && (substr_count($reqURI, "www.") != 0)) {
  207. // host do not have 'www' but mosConfig does
  208. $reqURI = str_replace("www.", "", $reqURI);
  209. }
  210. /* Check for HTTPS */
  211. if(isset($HTTP_SERVER_VARS)){
  212. if(isset($HTTP_SERVER_VARS['HTTPS'])){
  213. if($HTTP_SERVER_VARS['HTTPS'] == "ON" ){
  214. $reqURI = str_replace("http://", "https://", $reqURI);
  215. }
  216. }
  217. }
  218. $siteType = (cmsVersion() == _CMS_JOOMLA15) ? '1.5' : '1.0';
  219. $html =
  220. "<script type='text/javascript'>
  221. /*<![CDATA[*/
  222. var jax_live_site = '$reqURI';
  223. var jax_site_type = '$siteType';
  224. /*]]>*/
  225. </script>";
  226. $html .= "<script type=\"text/javascript\" src=\"$this->_livePath/ajax_" . AZRUL_SYSTEM . ".js\"></script>\n";
  227. return $this->_fixHTTPS($html);
  228. }
  229. function nl2brStrict($text) {
  230. return preg_replace("/\r\n|\n|\r/", " <br />", $text);
  231. }
  232. function br2nl($text){
  233. $text = str_replace(' <br />', "\n", $text);
  234. return $this->_fixQuote($text);
  235. }
  236. function _fixQuote($text){
  237. return str_replace('&quot;', '"', $text);
  238. }
  239. function singleLineIt($text){
  240. return preg_replace("((\r\n)+)", '', $text);
  241. }
  242. /**
  243. *
  244. */
  245. function process(){
  246. global $mainframe;
  247. $cms =& cmsInstance('CMSCore');
  248. if(!defined('SERVICES_JSON_SLICE'))
  249. include_once($cms->get_path('plugins') . '/system/pc_includes/JSON.php');
  250. $json = new Services_JSON();
  251. global $my, $mainframe;
  252. if(@isset($_REQUEST['task']) && ($_REQUEST['task'] == 'azrul_ajax')){
  253. if(!isset($my))
  254. $my = $mainframe->getUser();
  255. $func = @$_REQUEST['func'];
  256. // Security fix.
  257. // 1. check if user are trying to run an eval
  258. # build an array of args
  259. $args = array();
  260. $argCount = 0;
  261. # All POST data that are meant to be send to the function will
  262. # be appended by 'arg' keyword. Only pass this vars to the function
  263. foreach($_REQUEST as $key => $postData){
  264. if(substr($key, 0, 3) == 'arg' ){
  265. //if ( get_magic_quotes_gpc() ) {
  266. $postData = stripslashes($postData);
  267. //}
  268. $postData = ($this->nl2brStrict($postData));
  269. //var_dump($postData);
  270. $decoded = $json->decode($postData);
  271. $key = "";
  272. $val = "";
  273. // print_r($decoded);
  274. // exit;
  275. # if the args is an array, we need to pass it as an array
  276. # todo@ we need to expand this array further. We now assume,
  277. # if an array is passed, it comes in a pair of (key/value)
  278. if(is_array($decoded))
  279. {
  280. foreach($decoded as $index => $value)
  281. {
  282. $tempArray = array();
  283. if( is_array($value) )
  284. {
  285. foreach($value as $val)
  286. {
  287. // The value is an array so we need to chuck them in
  288. // a multidimensional array instead
  289. if( is_array($val) )
  290. {
  291. // Since the values here are array, we will
  292. // always assume that the index 0 is always the key
  293. $key = $val[0];
  294. $data = $this->br2nl( rawurldecode($val[1]) );
  295. // We will also always assume that the index 1 will be the value
  296. $decoded[$key][] = $data;
  297. }
  298. else
  299. {
  300. // We always assume that the index 0 is the key of the array.
  301. $key = $value[0];
  302. // We always assume that the index 1 is the data of the array.
  303. $data = $this->br2nl(rawurldecode($value[1]));
  304. if( substr($value[0], 0, 6) == '_d_' )
  305. {
  306. $decoded = array($val);
  307. }
  308. else
  309. {
  310. $newArray = array( $key => $data );
  311. $decoded = array_merge( $decoded, $newArray );
  312. //$newA = array($key => $val);
  313. //$decoded = array_merge($decoded, $newA);
  314. }
  315. }
  316. }
  317. } else{
  318. // If data passed is not array we treat
  319. if($value != '_d_' ){
  320. $decoded = $this->br2nl(rawurldecode($value));
  321. }
  322. }
  323. // print_r($decoded);
  324. // exit;
  325. // foreach($decoded as $d){
  326. // if(is_array($d)){
  327. // $key = $d[0];
  328. //
  329. // // if key is still an array do a for loop.
  330. // // it could be an array of values
  331. // if(is_array($key))
  332. // {
  333. // foreach($key as $value)
  334. // {
  335. // print_r($value);
  336. // }
  337. // }
  338. // else
  339. // {
  340. // $val = $this->br2nl(rawurldecode($d[1]));
  341. //
  342. // if(substr($d[0], 0, 6) == '_d_' ){
  343. // $decoded = array($val);
  344. // } else {
  345. // $newA = array($key => $val);
  346. // $decoded = array_merge($decoded, $newA);
  347. // }
  348. // }
  349. // } else {
  350. // if($d != '_d_' ){
  351. // $decoded = $this->br2nl(rawurldecode($d));
  352. // }
  353. // }
  354. // }
  355. }
  356. $args[] = $decoded;
  357. } else {
  358. $args[] = $this->br2nl(rawurldecode($decoded));
  359. }
  360. $argCount++;
  361. }
  362. }
  363. # Include the main component file
  364. $comName = $_REQUEST['option'];
  365. ob_start();
  366. global $jaxFuncNames;
  367. // This is a really silly test!
  368. if(strpos($this->_reqURL, 'hidemainmenu=1') OR strpos($_SERVER['HTTP_REFERER'], 'administrator')){
  369. include_once(JAX_SITE_ROOT . "/administrator/components/com_$comName/admin.$comName.php");
  370. // Test, and make sure $my object is valid
  371. if(!$my->id){
  372. echo "Invalid access";
  373. exit;
  374. }
  375. }
  376. else {
  377. include_once(JAX_SITE_ROOT . "/components/com_$comName/$comName.php");
  378. }
  379. @ob_end_clean();
  380. $jaxFilename = JAX_SITE_ROOT . "/components/com_$comName/jax.$comName.php";
  381. @include_once($jaxFilename);
  382. // check and make sure the fucntion name is actually registered
  383. if(!in_array($func, $jaxFuncNames)){
  384. //print_r($jaxFuncNames);
  385. echo 'invalid function calls';
  386. exit;
  387. }
  388. $funcArray = explode(',', $func);
  389. // Object call
  390. if(count($funcArray) > 1){
  391. //$response = call_user_func_array($funcArray, $args);
  392. $entryPoint = $comName.'AjaxEntry';
  393. $arg = array();
  394. $arg[] = $func;
  395. $arg[] = $args;
  396. $response = call_user_func_array($entryPoint, $arg);
  397. } else
  398. $response = call_user_func_array($func, $args);
  399. //header('Content-type: text/plain');
  400. //echo $response;
  401. //exit;
  402. }
  403. }
  404. }
  405. }