PageRenderTime 27ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/components/com_virtuemart/virtuemart_parser.php

http://vanphongphamdm.googlecode.com/
PHP | 310 lines | 205 code | 43 blank | 62 comment | 71 complexity | e04923ccfe024d95c85dd5f5a4788b0d MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1, Apache-2.0
  1. <?php
  2. if( !defined( '_VALID_MOS' ) && !defined( '_JEXEC' ) ) die( 'Direct Access to '.basename(__FILE__).' is not allowed.' );
  3. /**
  4. * This file prepares the VirtueMart framework
  5. * It should be included whenever a VirtueMart function is needed
  6. *
  7. * @version $Id: virtuemart_parser.php 3105 2011-04-30 13:35:30Z zanardi $
  8. * @package VirtueMart
  9. * @subpackage core
  10. * @copyright Copyright (C) 2004-2008 soeren - All rights reserved.
  11. * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.php
  12. * VirtueMart is free software. This version may have been modified pursuant
  13. * to the GNU General Public License, and as distributed it includes or
  14. * is derivative of works licensed under the GNU General Public License or
  15. * other free or open source software licenses.
  16. * See /administrator/components/com_virtuemart/COPYRIGHT.php for copyright notices and details.
  17. *
  18. * http://virtuemart.net
  19. */
  20. global $my, $db, $perm, $ps_function, $ps_module, $ps_html, $ps_vendor_id, $vendor_image,$vendor_image_url, $keyword,
  21. $ps_payment_method,$ps_zone,$sess, $page, $func, $pagename, $modulename, $vars, $default, $VM_LANG, $cmd, $ok, $mosConfig_lang,
  22. $auth, $ps_checkout,$error, $error_type, $func_perms, $func_list, $func_class, $func_method, $func_list, $dir_list, $pagePermissionsOK,
  23. $vendor_currency_display_style, $vendor_freeshipping, $mm_action_url, $limit, $limitstart, $mainframe, $ps_product, $database;
  24. include_once( dirname(__FILE__).'/../../administrator/components/com_virtuemart/compat.joomla1.5.php' );
  25. if( !defined( '_VM_PARSER_LOADED' )) {
  26. global $my;
  27. // Clean the var PHP_SELF from chars like " or '
  28. $_SERVER['PHP_SELF'] = htmlspecialchars( $_SERVER['PHP_SELF'], ENT_QUOTES );
  29. if( !empty($_SERVER['QUERY_STRING'])) {
  30. // Make sure, that the Query String only contains urlencoded values
  31. $vars = explode( '&', $_SERVER['QUERY_STRING']);
  32. $new_query_string = array();
  33. foreach( $vars as $val) {
  34. $keyvarpair = explode('=', $val);
  35. if( sizeof( $keyvarpair ) == 1 ) {
  36. $keyvarpair[1] = 0;
  37. }
  38. $new_query_string[] = $keyvarpair[0].'='.urlencode(urldecode($keyvarpair[1]));
  39. }
  40. $_SERVER['QUERY_STRING'] = implode('&', $new_query_string );
  41. }
  42. if( !empty($my->id) || !empty($user->id) ) {
  43. // This is necessary to get the real GID
  44. if( class_exists( 'jconfig' ) ) {
  45. $tmpuser = & JFactory::getUser();
  46. if( !defined( '_JLEGACY' ) ) {
  47. $GLOBALS['my']->load( $tmpuser->get('id'));
  48. $GLOBALS['my']->set('gid', $tmpuser->get('gid'));
  49. } else {
  50. $GLOBALS['my']->gid = $tmpuser->get('gid');
  51. }
  52. } else {
  53. $my->load( $my->id );
  54. }
  55. $vmuser = $my;
  56. }
  57. if( !file_exists( $mosConfig_absolute_path. "/administrator/components/com_virtuemart/virtuemart.cfg.php" )) {
  58. die( "<h3>The configuration file for VirtueMart is missing!</h3>It should be here: <strong>"
  59. . $mosConfig_absolute_path. "/administrator/components/com_virtuemart/virtuemart.cfg.php</strong>" );
  60. }
  61. // the configuration file for the Shop
  62. require_once( $mosConfig_absolute_path. "/administrator/components/com_virtuemart/virtuemart.cfg.php" );
  63. $GLOBALS['mosConfig_live_site'] = $mosConfig_live_site = substr( URL, 0, strlen(URL)-1);
  64. // the global file for VirtueMart
  65. require_once( ADMINPATH . 'global.php' );
  66. if( !vmIsAdminMode() && !is_a($mainframe, 'JAdministrator') && !isset( $_REQUEST['page']) ) {
  67. // Get the menu parameters, if any
  68. if( vmIsJoomla( '1.5' ) ) {
  69. $menuparams = $mainframe->getParams();
  70. } else {
  71. $Itemid = (int) vmRequest::getInt( 'Itemid', '' );
  72. $query = "SELECT params FROM #__menu WHERE id='".$Itemid."'";
  73. $database->setQuery( $query );
  74. $itemparams = $database->loadResult();
  75. $menuparams = new mosParameters( $itemparams );
  76. }
  77. $tmp_product_id = $menuparams->get( 'product_id' );
  78. $tmp_category_id = $menuparams->get( 'category_id' );
  79. $tmp_flypage = $menuparams->get( 'flypage' );
  80. $tmp_page = $menuparams->get( 'page' );
  81. if( !empty( $tmp_product_id ) ) {
  82. vmRequest::setVar( 'product_id', $tmp_product_id );
  83. vmRequest::setVar( 'page', 'shop.product_details' );
  84. } elseif( !empty( $tmp_category_id ) ) {
  85. vmRequest::setVar( 'category_id', $tmp_category_id );
  86. vmRequest::setVar( 'page', 'shop.browse' );
  87. }
  88. if( ( !empty( $tmp_product_id ) || !empty( $tmp_category_id ) ) && !empty( $tmp_flypage ) ) {
  89. vmRequest::setVar( 'flypage', $tmp_flypage );
  90. }
  91. if( !empty( $tmp_page ) ) {
  92. vmRequest::setVar( 'page', $tmp_page );
  93. }
  94. // Set the default page
  95. $defaultpage = HOMEPAGE;
  96. } else {
  97. $defaultpage = vmget($_SESSION,'last_page');
  98. }
  99. $page = vmRequest::getVar('page', $defaultpage );
  100. $func = vmRequest::getVar('func');
  101. $ajax_request = strtolower(vmGet($_SERVER,'HTTP_X_REQUESTED_WITH')) == 'xmlhttprequest' || vmGet($_REQUEST,'ajax_request') == '1';
  102. $option = vmRequest::getVar('option');
  103. // This makes it possible to use Shared SSL
  104. $sess->prepare_SSL_Session();
  105. if( $option == "com_virtuemart" ) {
  106. // Get sure that we have float values with a decimal point!
  107. @setlocale( LC_NUMERIC, 'en_US', 'en' );
  108. @setlocale( LC_TIME, $mosConfig_locale );
  109. $mosConfig_list_limit = isset( $mosConfig_list_limit ) ? $mosConfig_list_limit : SEARCH_ROWS;
  110. unset( $_REQUEST["error"] );
  111. // Cast all the following fields to INT
  112. $parseToIntFields = array('user_id','product_id','category_id','manufacturer_id','id','cid','vendor_id','country_id','currency_id', 'limitstart', 'tax_rate_id',
  113. 'order_id','module_id','function_id','payment_method_id','coupon_id','product_type_id', 'product_price_id', 'shopper_group_id') ;
  114. foreach( $parseToIntFields as $intField ) {
  115. if( !empty($_REQUEST[$intField]) && is_array($_REQUEST[$intField]) ) {
  116. vmArrayToInts( $_REQUEST[$intField] );
  117. } elseif ( isset($_REQUEST[$intField]) ) {
  118. $_REQUEST[$intField] = $$intField = vmRequest::getInt($intField);
  119. }
  120. }
  121. $product_id = vmRequest::getInt('product_id');
  122. $vm_mainframe->setUserState('product_id', $product_id );
  123. if( vmIsAdminMode() ) {
  124. $category_id = (int)$vm_mainframe->getUserStateFromRequest( 'category_id', 'category_id' );
  125. } else {
  126. $category_id = vmRequest::getInt('category_id');
  127. }
  128. $manufacturer_id = vmRequest::getInt('manufacturer_id');
  129. $user_info_id = vmRequest::getVar('user_info_id');
  130. $myInsecureArray = array(
  131. 'user_info_id' => $user_info_id,
  132. 'page' => $page,
  133. 'func' => $func
  134. );
  135. /**
  136. * This InputFiler Object will help us filter malicious variable contents
  137. * @global vmInputFiler vmInputFiler
  138. */
  139. $GLOBALS['vmInputFilter'] = $vmInputFilter = vmInputFilter::getInstance();
  140. // prevent SQL injection
  141. if( $perm->check('admin,storeadmin') ) {
  142. $myInsecureArray = $vmInputFilter->safeSQL( $myInsecureArray );
  143. $myInsecureArray = $vmInputFilter->process( $myInsecureArray );
  144. // Re-insert the escaped strings into $_REQUEST
  145. foreach( $myInsecureArray as $requestvar => $requestval) {
  146. $_REQUEST[$requestvar] = $requestval;
  147. }
  148. } else {
  149. // Strip all tags from all input values
  150. $_REQUEST = $vmInputFilter->process( $_REQUEST );
  151. $_REQUEST = $vmInputFilter->safeSQL( $_REQUEST );
  152. }
  153. // Limit the keyword (=search string) length to 50
  154. $keyword = substr( urldecode( vmGet( $_REQUEST, 'keyword' )), 0, 50 );
  155. $vars = vmRequest::get('', VMREQUEST_ALLOWRAW );
  156. }
  157. // The Page will change with every different parameter / argument, so provide this for identification
  158. // "call" will call the function load_that_shop_page when it is not yet cached with exactly THESE parameters
  159. // or the caching time range has expired
  160. $GLOBALS['cache_id'] = vmTemplate::getCacheId();
  161. if( $option == "com_virtuemart" ) {
  162. // Check if we have to run a Shop Function
  163. // and if the user is allowed to execute it
  164. $funcParams = $ps_function->getFuncPermissions( $func );
  165. /**********************************************
  166. ** Get Page/Directory Permissions
  167. ** Displays error if directory is not registered,
  168. ** user has no permission to view it , or file doesn't exist
  169. ************************************************/
  170. if (empty($page)) {// default page
  171. if (defined('_VM_IS_BACKEND')) {
  172. $page = "store.index";
  173. }
  174. else {
  175. $page = HOMEPAGE;
  176. }
  177. }
  178. // Let's check if the user is allowed to view the page
  179. // if not, $page is set to ERROR_PAGE
  180. $pagePermissionsOK = $ps_module->checkModulePermissions( $page );
  181. $ok = true;
  182. if ( !empty( $funcParams["method"] ) && vmGet($_REQUEST, 'task') != 'cancel' ) {
  183. // Protection against Cross-Site Request Forgery
  184. if( vmIsAdminMode() && !vmSpoofCheck(null, $sess->getSessionId() ) ) {
  185. return;
  186. }
  187. // Get the function parameters: function name and class name
  188. $q = "SELECT #__{vm}_module.module_name,#__{vm}_function.function_class";
  189. $q .= " FROM #__{vm}_module,#__{vm}_function WHERE ";
  190. $q .= "#__{vm}_module.module_id=#__{vm}_function.module_id AND ";
  191. $q .= "#__{vm}_function.function_method='".$funcParams["method"]."' AND ";
  192. $q .= "#__{vm}_function.function_class='".$funcParams["class"]."'";
  193. $db->query($q);
  194. $db->next_record();
  195. $class = $db->f('function_class');
  196. if( file_exists( CLASSPATH."$class.php" ) ) {
  197. if( $ajax_request ) {
  198. require_once( CLASSPATH . 'connectionTools.class.php' );
  199. vmConnector::sendHeaderAndContent( 200 );
  200. }
  201. // Load class definition file
  202. require_once( CLASSPATH."$class.php" );
  203. $classname = str_replace( '.class', '', $funcParams["class"]);
  204. if( !class_exists(strtolower($classname))) {
  205. $classname = 'vm'.$classname;
  206. }
  207. if( class_exists( $classname )) {
  208. // create an object
  209. $$classname = new $classname();
  210. // RUN THE FUNCTION
  211. // $ok = $class->function( $vars );
  212. $ok = $$classname->$funcParams["method"]($vars);
  213. }
  214. if ($ok == false) {
  215. $no_last = 1;
  216. $last_page = vmGet( $_SESSION, 'last_page' );
  217. if( $last_page != HOMEPAGE && !empty( $last_page ) && empty($_REQUEST['ignore_last_page']) ) {
  218. $page = $last_page;
  219. }
  220. $my_page= explode ( '.', $page );
  221. $modulename = $my_page[0];
  222. $pagename = $my_page[1];
  223. $_REQUEST['keyword']= vmGet($_SESSION['session_userstate'], 'keyword' );
  224. $_REQUEST['category_id']= vmGet( $_SESSION['session_userstate'], 'category_id' );
  225. $_REQUEST['product_id']=$product_id = $_SESSION['session_userstate']['product_id'];
  226. }
  227. }
  228. else {
  229. $vmLogger->debug( "Could not include the class file $class" );
  230. }
  231. if (!empty($vars["error"])) {
  232. $error = vmGet( $vars, 'error' );
  233. }
  234. if (!empty($error)) {
  235. echo vmCommonHTML::getErrorField($error);
  236. }
  237. }
  238. else {
  239. $no_last = 0;
  240. //$error="";
  241. }
  242. // If this is an asynchronous page load,
  243. // we clear the output buffer and just send the log messages.
  244. // the variable named 'ajax_request' has to be set to 1.
  245. if( $func && $ajax_request) {
  246. // Send an indicator wether the function call return true or false
  247. vmCommonHTML::getSuccessIndicator( $ok, $vmDisplayLogger );
  248. $vm_mainframe->close(true);//die
  249. }
  250. if ($ok == true && empty($error) && !defined('_DONT_VIEW_PAGE') && !strstr($page, 'ajax') && !$ajax_request && !@$_REQUEST['only_page']) {
  251. $_SESSION['last_page'] = $page;
  252. }
  253. }
  254. // I don't get it, why Joomla uses masked gid values!
  255. if( !defined( '_VM_IS_BACKEND' )&& !class_exists('jfactory')) {
  256. $my = $mainframe->getUser();
  257. }
  258. if( empty($_REQUEST['only_page']) ) {
  259. // the Log object holds all error messages
  260. // here we flush the buffer and print out all messages
  261. $vmLogger->flush();
  262. // Now we can switch to implicit flushing
  263. $vmDisplayLogger->_buffering = false;
  264. }
  265. define( '_VM_PARSER_LOADED', 1 );
  266. }
  267. ?>