Advertisement
Guest User

ok

a guest
May 28th, 2015
317
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 32.36 KB | None | 0 0
  1. <?php
  2. /**
  3. * Configuration helper class
  4. *
  5. * This class provides some functions that are used throughout the VirtueMart shop to access confgiuration values.
  6. *
  7. * @package VirtueMart
  8. * @subpackage Helpers
  9. * @author Max Milbers
  10. * @copyright Copyright (c) 2004-2008 Soeren Eberhardt-Biermann, 2009-2014 VirtueMart Team. All rights reserved.
  11. */
  12. defined('_JEXEC') or die('Restricted access');
  13.  
  14. /**
  15. *
  16. * We need this extra paths to have always the correct path undependent by loaded application, module or plugin
  17. * Plugin, module developers must always include this config at start of their application
  18. * $vmConfig = VmConfig::loadConfig(); // load the config and create an instance
  19. * $vmConfig -> jQuery(); // for use of jQuery
  20. * Then always use the defined paths below to ensure future stability
  21. */
  22. defined('DS') or define('DS', DIRECTORY_SEPARATOR);
  23. //defined('_JEXEC') or define('_JEXEC', 1);
  24.  
  25. $app = JFactory::getApplication();
  26. $admin = '';
  27. if(!$app->isSite()){
  28. $admin = DS.'administrator';//echo('in administrator');
  29. }
  30.  
  31. if(defined('JPATH_ROOT')){ //We are in joomla
  32. defined ('VMPATH_ROOT') or define ('VMPATH_ROOT', JPATH_ROOT);
  33. if(version_compare(JVERSION,'3.0.0','ge')) {
  34. defined('JVM_VERSION') or define ('JVM_VERSION', 3);
  35. }
  36. if(version_compare(JVERSION,'1.7.0','ge')) {
  37. defined('JPATH_VM_LIBRARIES') or define ('JPATH_VM_LIBRARIES', JPATH_PLATFORM);
  38. defined('JVM_VERSION') or define ('JVM_VERSION', 2);
  39. }
  40. else {
  41. if (version_compare (JVERSION, '1.6.0', 'ge')) {
  42. defined ('JPATH_VM_LIBRARIES') or define ('JPATH_VM_LIBRARIES', JPATH_LIBRARIES);
  43. defined ('JVM_VERSION') or define ('JVM_VERSION', 2);
  44. }
  45. else {
  46. defined ('JPATH_VM_LIBRARIES') or define ('JPATH_VM_LIBRARIES', JPATH_LIBRARIES);
  47. defined ('JVM_VERSION') or define ('JVM_VERSION', 1);
  48. }
  49. }
  50. $vmPathLibraries = JPATH_VM_LIBRARIES;
  51. } else {
  52. defined ('JVM_VERSION') or define ('JVM_VERSION', 0);
  53. defined ('VMPATH_ROOT') or define ('VMPATH_ROOT', dirname( __FILE__ ));
  54. $vmPathLibraries = '';
  55. }
  56.  
  57. defined ('VMPATH_LIBS') or define ('VMPATH_LIBS', $vmPathLibraries);
  58. defined ('VMPATH_SITE') or define ('VMPATH_SITE', VMPATH_ROOT.DS.'components'.DS.'com_virtuemart' );
  59. defined ('VMPATH_ADMIN') or define ('VMPATH_ADMIN', VMPATH_ROOT.DS.'administrator'.DS.'components'.DS.'com_virtuemart' );
  60. defined ('VMPATH_BASE') or define ('VMPATH_BASE',VMPATH_ROOT.$admin);
  61. defined ('VMPATH_PLUGINLIBS') or define ('VMPATH_PLUGINLIBS', VMPATH_ADMIN.DS.'plugins');
  62. defined ('VMPATH_PLUGINS') or define ('VMPATH_PLUGINS', VMPATH_ROOT.DS.'plugins' );
  63. defined ('VMPATH_MODULES') or define ('VMPATH_MODULES', VMPATH_ROOT.DS.'modules' );
  64. defined ('VMPATH_THEMES') or define ('VMPATH_THEMES', VMPATH_ROOT.$admin.DS.'templates' );
  65.  
  66. //legacy
  67. defined ('JPATH_VM_SITE') or define('JPATH_VM_SITE', VMPATH_SITE );
  68. defined ('JPATH_VM_ADMINISTRATOR') or define('JPATH_VM_ADMINISTRATOR', VMPATH_ADMIN);
  69. // define( 'VMPATH_ADMIN', JPATH_ROOT.DS.'administrator'.DS.'components'.DS.'com_virtuemart' );
  70. define( 'JPATH_VM_PLUGINS', VMPATH_PLUGINLIBS );
  71. define( 'JPATH_VM_MODULES', VMPATH_MODULES );
  72.  
  73.  
  74. defined('VM_VERSION') or define ('VM_VERSION', 3);
  75.  
  76. //This number is for obstruction, similar to the prefix jos_ of joomla it should be avoided
  77. //to use the standard 7, choose something else between 1 and 99, it is added to the ordernumber as counter
  78. // and must not be lowered.
  79. defined('VM_ORDER_OFFSET') or define('VM_ORDER_OFFSET',3);
  80.  
  81. require(VMPATH_ADMIN.DS.'version.php');
  82. defined('VM_REV') or define('VM_REV',vmVersion::$REVISION);
  83.  
  84. if(!class_exists('VmTable')){
  85. require(VMPATH_ADMIN.DS.'helpers'.DS.'vmtable.php');
  86. }
  87. VmTable::addIncludePath(VMPATH_ADMIN.DS.'tables');
  88.  
  89. if (!class_exists ('VmModel')) {
  90. require(VMPATH_ADMIN . DS . 'helpers' . DS . 'vmmodel.php');
  91. }
  92.  
  93. if(!class_exists('vRequest')) require(VMPATH_ADMIN.DS.'helpers'.DS.'vrequest.php');
  94. if(!class_exists('vmText')) require(VMPATH_ADMIN.DS.'helpers'.DS.'vmtext.php');
  95. if(!class_exists('vmJsApi')) require(VMPATH_ADMIN.DS.'helpers'.DS.'vmjsapi.php');
  96.  
  97. /**
  98. * Where type can be one of
  99. * 'warning' - yellow
  100. * 'notice' - blue
  101. * 'error' - red
  102. * 'message' (or empty) - green
  103. * This function shows an info message, the messages gets translated with vmText::,
  104. * you can overload the function, so that automatically sprintf is taken, when needed.
  105. * So this works vmInfo('COM_VIRTUEMART_MEDIA_NO_PATH_TYPE',$type,$link )
  106. * and also vmInfo('COM_VIRTUEMART_MEDIA_NO_PATH_TYPE');
  107. *
  108. * @author Max Milbers
  109. * @param string $publicdescr
  110. * @param string $value
  111. */
  112.  
  113. function vmInfo($publicdescr,$value=NULL){
  114.  
  115. $app = JFactory::getApplication();
  116.  
  117. $msg = '';
  118. $type = 'notice';//'info';
  119. if(VmConfig::$maxMessageCount<VmConfig::$maxMessage){
  120. $lang = JFactory::getLanguage();
  121. if($value!==NULL){
  122.  
  123. $args = func_get_args();
  124. if (count($args) > 0) {
  125. $args[0] = $lang->_($args[0]);
  126. $msg = call_user_func_array('sprintf', $args);
  127. }
  128. } else {
  129. $msg = vmText::_($publicdescr);
  130. }
  131. }
  132. else {
  133. if (VmConfig::$maxMessageCount == VmConfig::$maxMessage) {
  134. $msg = 'Max messages reached';
  135. $type = 'warning';
  136. VmConfig::$maxMessageCount++;
  137. } else {
  138. return false;
  139. }
  140. }
  141.  
  142. if(!empty($msg)){
  143. VmConfig::$maxMessageCount++;
  144. $app ->enqueueMessage($msg,$type);
  145. } else {
  146. vmTrace('vmInfo Message empty '.$msg);
  147. }
  148.  
  149. return $msg;
  150. }
  151.  
  152. /**
  153. * Informations for the vendors or the administrators of the store, but not for developers like vmdebug
  154. * @param $publicdescr
  155. * @param null $value
  156. */
  157. function vmAdminInfo($publicdescr,$value=NULL){
  158.  
  159. if(VmConfig::$echoAdmin){
  160.  
  161. $app = JFactory::getApplication();
  162.  
  163. if(VmConfig::$maxMessageCount<VmConfig::$maxMessage){
  164. $lang = JFactory::getLanguage();
  165. if($value!==NULL){
  166.  
  167. $args = func_get_args();
  168. if (count($args) > 0) {
  169. $args[0] = $lang->_($args[0]);
  170. VmConfig::$maxMessageCount++;
  171. $app ->enqueueMessage(call_user_func_array('sprintf', $args),'info');
  172. }
  173. } else {
  174. VmConfig::$maxMessageCount++;
  175. $publicdescr = $lang->_($publicdescr);
  176. $app ->enqueueMessage('Info: '.vmText::_($publicdescr),'info');
  177. }
  178. }
  179. else {
  180. if (VmConfig::$maxMessageCount == VmConfig::$maxMessage) {
  181. $app->enqueueMessage ('Max messages reached', 'info');
  182. VmConfig::$maxMessageCount++;
  183. }else {
  184. return false;
  185. }
  186. }
  187. }
  188.  
  189. }
  190.  
  191. function vmWarn($publicdescr,$value=NULL){
  192.  
  193.  
  194. $app = JFactory::getApplication();
  195. $msg = '';
  196. if(VmConfig::$maxMessageCount<VmConfig::$maxMessage){
  197. $lang = JFactory::getLanguage();
  198. if($value!==NULL){
  199.  
  200. $args = func_get_args();
  201. if (count($args) > 0) {
  202. $args[0] = $lang->_($args[0]);
  203. $msg = call_user_func_array('sprintf', $args);
  204.  
  205. }
  206. } else {
  207. $msg = $lang->_($publicdescr);
  208. }
  209. }
  210. else {
  211. if (VmConfig::$maxMessageCount == VmConfig::$maxMessage) {
  212. $msg = 'Max messages reached';
  213. VmConfig::$maxMessageCount++;
  214. } else {
  215. return false;
  216. }
  217. }
  218.  
  219. if(!empty($msg)){
  220. VmConfig::$maxMessageCount++;
  221. $app ->enqueueMessage($msg,'warning');
  222. return $msg;
  223. } else {
  224. vmTrace('vmWarn Message empty');
  225. return false;
  226. }
  227.  
  228. }
  229.  
  230. /**
  231. * Shows an error message, sensible information should be only in the first one, the second one is for non BE users
  232. * @author Max Milbers
  233. */
  234. function vmError($descr,$publicdescr=''){
  235.  
  236. $msg = '';
  237. $lang = JFactory::getLanguage();
  238. $descr = $lang->_($descr);
  239. $adminmsg = 'vmError: '.$descr;
  240. if (empty($descr)) {
  241. vmTrace ('vmError message empty');
  242. return;
  243. }
  244. logInfo($adminmsg,'error');
  245. if(VmConfig::$maxMessageCount< (VmConfig::$maxMessage+5)){
  246.  
  247. if(VmConfig::$echoAdmin){
  248. $msg = $adminmsg;
  249. } else {
  250. if(!empty($publicdescr)){
  251. $msg = $lang->_($publicdescr);
  252. }
  253. }
  254. }
  255. else {
  256. if (VmConfig::$maxMessageCount == (VmConfig::$maxMessage+5)) {
  257. $msg = 'Max messages reached';
  258. VmConfig::$maxMessageCount++;
  259. } else {
  260. return false;
  261. }
  262. }
  263.  
  264. if(!empty($msg)){
  265. VmConfig::$maxMessageCount++;
  266. $app = JFactory::getApplication();
  267. $app ->enqueueMessage($msg,'error');
  268. return $msg;
  269. }
  270.  
  271. return $msg;
  272.  
  273. }
  274.  
  275. /**
  276. * A debug dumper for VM, it is only shown to backend users.
  277. *
  278. * @author Max Milbers
  279. * @param unknown_type $descr
  280. * @param unknown_type $values
  281. */
  282. function vmdebug($debugdescr,$debugvalues=NULL){
  283.  
  284. if(VMConfig::showDebug() ){
  285. $app = JFactory::getApplication();
  286.  
  287. if(VmConfig::$maxMessageCount<VmConfig::$maxMessage){
  288. if($debugvalues!==NULL){
  289. $args = func_get_args();
  290. if (count($args) > 1) {
  291. for($i=1;$i<count($args);$i++){
  292. if(isset($args[$i])){
  293. $debugdescr .=' Var'.$i.': <pre>'.print_r($args[$i],1).'<br />'.print_r(get_class_methods($args[$i]),1).'</pre>';
  294. }
  295. }
  296.  
  297. }
  298. }
  299.  
  300. if(VmConfig::$echoDebug){
  301. VmConfig::$maxMessageCount++;
  302. echo $debugdescr."\n";
  303. } else if(VmConfig::$logDebug){
  304. logInfo($debugdescr,'vmdebug');
  305. }else {
  306. VmConfig::$maxMessageCount++;
  307. $app = JFactory::getApplication();
  308. $app ->enqueueMessage('<span class="vmdebug" >vmdebug '.$debugdescr.'</span>');
  309. }
  310.  
  311. }
  312. else {
  313. if (VmConfig::$maxMessageCount == VmConfig::$maxMessage) {
  314. $app->enqueueMessage ('Max messages reached', 'info');
  315. VmConfig::$maxMessageCount++;
  316. }
  317. }
  318.  
  319. }
  320.  
  321. }
  322.  
  323. function vmTrace($notice,$force=FALSE){
  324.  
  325. if($force || (VMConfig::showDebug() ) ){
  326. ob_start();
  327. echo '<pre>';
  328. debug_print_backtrace();
  329. echo '</pre>';
  330. $body = ob_get_contents();
  331. ob_end_clean();
  332. if(VmConfig::$echoDebug){
  333. echo $notice.' <pre>'.$body.'</pre>';
  334. } else if(VmConfig::$logDebug){
  335. logInfo($body,$notice);
  336. } else {
  337. $app = JFactory::getApplication();
  338. $app ->enqueueMessage($notice.' '.$body.' ');
  339. }
  340.  
  341. }
  342.  
  343. }
  344.  
  345. function vmRam($notice,$value=NULL){
  346. vmdebug($notice.' used Ram '.round(memory_get_usage(TRUE)/(1024*1024),2).'M ',$value);
  347. }
  348.  
  349. function vmRamPeak($notice,$value=NULL){
  350. vmdebug($notice.' memory peak '.round(memory_get_peak_usage(TRUE)/(1024*1024),2).'M ',$value);
  351. }
  352.  
  353.  
  354. function vmSetStartTime($name='current'){
  355. VmConfig::$_starttime[$name] = microtime(TRUE);
  356. }
  357.  
  358. function vmTime($descr,$name='current'){
  359.  
  360. if (empty($descr)) {
  361. $descr = $name;
  362. }
  363. $starttime = VmConfig::$_starttime ;
  364. if(empty($starttime[$name])){
  365. vmdebug('vmTime: '.$descr.' starting '.microtime(TRUE));
  366. VmConfig::$_starttime[$name] = microtime(TRUE);
  367. }
  368. else {
  369. if ($name == 'current') {
  370. vmdebug ('vmTime: ' . $descr . ' time consumed ' . (microtime (TRUE) - $starttime[$name]));
  371. VmConfig::$_starttime[$name] = microtime (TRUE);
  372. }
  373. else {
  374. if (empty($descr)) {
  375. $descr = $name;
  376. }
  377. $tmp = 'vmTime: ' . $descr . ': ' . (microtime (TRUE) - $starttime[$name]);
  378. vmdebug ($tmp);
  379. }
  380. }
  381.  
  382. }
  383.  
  384. /**
  385. * logInfo
  386. * to help debugging Payment notification for example
  387. */
  388. function logInfo ($text, $type = 'message') {
  389.  
  390. static $file = null;
  391. //vmSetStartTime('logInfo');
  392. $head = false;
  393.  
  394. if($file===null){
  395. if(!class_exists('JFile')) require(VMPATH_LIBS.DS.'joomla'.DS.'filesystem'.DS.'file.php');
  396.  
  397. $config = JFactory::getConfig();
  398. $log_path = $config->get('log_path', VMPATH_ROOT . "/log" );
  399. $file = $log_path . "/" . VmConfig::$logFileName . VmConfig::LOGFILEEXT;
  400.  
  401. if (!is_dir($log_path)) {
  402. jimport('joomla.filesystem.folder');
  403. if (!JFolder::create($log_path)) {
  404. if (VmConfig::$echoAdmin){
  405. $msg = 'Could not create path ' . $log_path . ' to store log information. Check your folder ' . $log_path . ' permissions.';
  406. $app = JFactory::getApplication();
  407. $app->enqueueMessage($msg, 'error');
  408. }
  409. return;
  410. }
  411. }
  412. if (!is_writable($log_path)) {
  413. if (VmConfig::$echoAdmin){
  414. $msg = 'Path ' . $log_path . ' to store log information is not writable. Check your folder ' . $log_path . ' permissions.';
  415. $app = JFactory::getApplication();
  416. $app->enqueueMessage($msg, 'error');
  417. }
  418. return;
  419. }
  420.  
  421. if (!JFile::exists($file)) {
  422. // blank line to prevent information disclose: https://bugs.php.net/bug.php?id=60677
  423. // from Joomla log file
  424. $head = "#\n";
  425. $head .= '#<?php die("Forbidden."); ?>'."\n";
  426.  
  427. }
  428. }
  429.  
  430.  
  431. // Initialise variables.
  432. /*if(!class_exists('JClientHelper')) require(VMPATH_LIBS.DS.'joomla'.DS.'client'.DS.'helper.php');
  433. $FTPOptions = JClientHelper::getCredentials('ftp');
  434. if (!empty($FTPOptions['enabled'] == 0)){
  435. //For logging we do not support FTP. For loggin without file permissions using FTP, we need to load the file,..
  436. //append the text and replace the file. This cannot be fast per FTP and therefore we disable it.
  437. } else {*/
  438.  
  439. $fp = fopen ($file, 'a');
  440. if ($fp) {
  441. if ($head) {
  442. fwrite ($fp, $head);
  443. }
  444.  
  445. fwrite ($fp, "\n" . JFactory::getDate()->format ('Y-m-d H:i:s'));
  446. fwrite ($fp, " ".strtoupper($type) . ' ' . $text);
  447. fclose ($fp);
  448. } else {
  449. if (VmConfig::$echoAdmin){
  450. $msg = 'Could not write in file ' . $file . ' to store log information. Check your file ' . $file . ' permissions.';
  451. $app = JFactory::getApplication();
  452. $app->enqueueMessage($msg, 'error');
  453. }
  454. }
  455. //}
  456. //vmTime('time','logInfo');
  457. return;
  458.  
  459. }
  460.  
  461. /**
  462. * The time how long the config in the session is valid.
  463. * While configuring the store, you should lower the time to 10 seconds.
  464. * Later in a big store it maybe useful to rise this time up to 1 hr.
  465. * That would mean that changing something in the config can take up to 1 hour until this change is effecting the shoppers.
  466. */
  467.  
  468. /**
  469. * We use this Class STATIC not dynamically !
  470. */
  471. class VmConfig {
  472.  
  473. // instance of class
  474. private static $_jpConfig = NULL;
  475. private static $_debug = NULL;
  476. private static $_secret = NULL;
  477. public static $_starttime = array();
  478. public static $loaded = FALSE;
  479.  
  480. public static $maxMessageCount = 0;
  481. public static $maxMessage = 100;
  482. public static $echoDebug = FALSE;
  483. public static $logDebug = FALSE;
  484. public static $logFileName = 'com_virtuemart';
  485. public static $echoAdmin = FALSE;
  486. const LOGFILEEXT = '.log.php';
  487.  
  488. public static $_virtuemart_vendor_id = null;
  489. public static $vmlang = false;
  490. public static $defaultLang = false;
  491. public static $vmlangTag = '';
  492. public static $langs = array();
  493. public static $langCount = 0;
  494. var $_params = array();
  495. var $_raw = array();
  496. public static $installed = false;
  497.  
  498.  
  499. private function __construct() {
  500.  
  501. if(function_exists('mb_ereg_replace')){
  502. mb_regex_encoding('UTF-8');
  503. mb_internal_encoding('UTF-8');
  504. }
  505. self::echoAdmin();
  506. ini_set('precision', 15); //We need at least 20 for correct precision if json is using a bigInt ids
  507. //But 15 has the best precision, using higher precision adds fantasy numbers to the end, but creates also errors in rounding
  508. ini_set('serialize_precision',16);
  509. }
  510.  
  511. static function getStartTime(){
  512. return self::$_starttime;
  513. }
  514.  
  515. static function setStartTime($name,$value){
  516. self::$_starttime[$name] = $value;
  517. }
  518.  
  519. static function getSecret(){
  520. return self::$_secret;
  521. }
  522.  
  523. static function echoAdmin(){
  524.  
  525. if(self::$echoAdmin===FALSE){
  526. $user = JFactory::getUser();
  527. if($user->authorise('core.admin','com_virtuemart') or $user->authorise('core.manage','com_virtuemart')){
  528. self::$echoAdmin = true;
  529. } else {
  530. self::$echoAdmin = false;
  531. }
  532. }
  533.  
  534. }
  535.  
  536. static function showDebug($override=false){
  537.  
  538. if(self::$_debug===NULL or $override!=false){
  539. if($override) {
  540. $debug = $override;
  541. $dev = $override;
  542. } else {
  543. $debug = VmConfig::get('debug_enable','none');
  544. $dev = VmConfig::get('vmdev',0);
  545. }
  546. //$debug = 'all'; //this is only needed, when you want to debug THIS file
  547. // 1 show debug only to admins
  548. if($debug === 'admin' ){
  549. if(VmConfig::$echoAdmin){
  550. self::$_debug = TRUE;
  551. } else {
  552. self::$_debug = FALSE;
  553. }
  554. }
  555. // 2 show debug to anyone
  556. else {
  557. if ($debug === 'all') {
  558. self::$_debug = TRUE;
  559. }
  560. // else dont show debug
  561. else {
  562. self::$_debug = FALSE;
  563. }
  564. }
  565.  
  566. if($dev === 'admin' ){
  567. if(VmConfig::$echoAdmin){
  568. $dev = TRUE;
  569. } else {
  570. $dev = FALSE;
  571. }
  572. }
  573. // 2 show debug to anyone
  574. else {
  575. if ($dev === 'all') {
  576. $dev = TRUE;
  577. }
  578. // else dont show debug
  579. else {
  580. $dev = FALSE;
  581. }
  582. }
  583.  
  584. self::setErrorReporting($dev);
  585.  
  586. }
  587.  
  588. return self::$_debug;
  589. }
  590.  
  591. static function setErrorReporting($dev,$force = false){
  592.  
  593. $ret = array();
  594. if($dev){
  595. $ret[0] = ini_set('display_errors', '-1');
  596. if(version_compare(phpversion(),'5.4.0','<' )){
  597. vmdebug('PHP 5.3');
  598. $ret[1] = error_reporting( E_ALL ^ E_STRICT );
  599. } else {
  600. vmdebug('PHP 5.4');
  601. $ret[1] = error_reporting( E_ALL );
  602. }
  603. vmdebug('Show All Errors');
  604.  
  605. } else {
  606. $jconfig = JFactory::getConfig();
  607. $errep = $jconfig->get('error_reporting');
  608. if ( $errep == 'default' or $force) {
  609. $ret[0] = ini_set('display_errors', 0);
  610. $ret[1] = error_reporting(E_ERROR | E_WARNING | E_PARSE);
  611. }
  612. }
  613. return $ret;
  614. }
  615.  
  616. /**
  617. * Ensures a certain Memory limit for php (if server supports it)
  618. * @author Max Milbers
  619. * @param int $minMemory
  620. */
  621. static function ensureMemoryLimit($minMemory=0){
  622.  
  623. if($minMemory === 0) $minMemory = VmConfig::get('minMemory','128M');
  624. $memory_limit = VmConfig::getMemoryLimit();
  625.  
  626. if($memory_limit<$minMemory) @ini_set( 'memory_limit', $minMemory.'M' );
  627. }
  628.  
  629. /**
  630. * Returns the PHP memory limit of the server in MB, regardless the used unit
  631. * @author Max Milbers
  632. * @return float|int PHP memory limit in MB
  633. */
  634. static function getMemoryLimit(){
  635.  
  636. $iniValue = ini_get('memory_limit');
  637.  
  638. if($iniValue<=0) return 2048; //We assume 2048MB as unlimited setting
  639. $iniValue = strtoupper($iniValue);
  640. if(strpos($iniValue,'M')!==FALSE){
  641. $memory_limit = (int) substr($iniValue,0,-1);
  642. } else if(strpos($iniValue,'K')!==FALSE){
  643. $memory_limit = (int) substr($iniValue,0,-1) / 1024.0;
  644. } else if(strpos($iniValue,'G')!==FALSE){
  645. $memory_limit = (int) substr($iniValue,0,-1) * 1024.0;
  646. } else {
  647. $memory_limit = (int) $iniValue / 1048576.0;
  648. }
  649. return $memory_limit;
  650. }
  651.  
  652. static function ensureExecutionTime($minTime=0){
  653.  
  654. if($minTime === 0) $minTime = (int) VmConfig::get('minTime',120);
  655. $max_execution_time = self::getExecutionTime();
  656. if((int)$max_execution_time<$minTime) {
  657. @ini_set( 'max_execution_time', $minTime );
  658. }
  659. }
  660.  
  661. static function getExecutionTime(){
  662. $max_execution_time = (int) ini_get('max_execution_time');
  663. if(empty($max_execution_time)){
  664. $max_execution_time = (int) VmConfig::get('minTime',120);
  665. }
  666. return $max_execution_time;
  667. }
  668.  
  669. /**
  670. * loads a language file, the trick for us is that always the config option enableEnglish is tested
  671. * and the path are already set and the correct order is used
  672. * We use first the english language, then the default
  673. *
  674. * @author Max Milbers
  675. * @static
  676. * @param $name
  677. * @return bool
  678. */
  679. static public function loadJLang($name,$site=false,$tag=0){
  680.  
  681. $jlang =JFactory::getLanguage();
  682. if(empty($tag))$tag = $jlang->getTag();
  683.  
  684. $path = $basePath = VMPATH_ADMIN;
  685. if($site){
  686. $path = $basePath = VMPATH_SITE;
  687. }
  688.  
  689. if(VmConfig::get('enableEnglish', true) and $tag!='en-GB'){
  690. $testpath = $basePath.DS.'language'.DS.'en-GB'.DS.'en-GB.'.$name.'.ini';
  691. if(!file_exists($testpath)){
  692. $epath = JPATH_ADMINISTRATOR;
  693. if($site){
  694. $epath = JPATH_SITE;
  695. }
  696. } else {
  697. $epath = $path;
  698. }
  699. $jlang->load($name, $epath, 'en-GB');
  700. }
  701.  
  702. $testpath = $basePath.DS.'language'.DS.$tag.DS.$tag.'.'.$name.'.ini';
  703. if(!file_exists($testpath)){
  704. $path = JPATH_ADMINISTRATOR;
  705. if($site){
  706. $path = JPATH_SITE;
  707. }
  708. }
  709.  
  710. $jlang->load($name, $path,$tag,true);
  711.  
  712. return $jlang;
  713. }
  714.  
  715. /**
  716. * @static
  717. * @author Valerie Isaksen
  718. * @param $name
  719. */
  720. static public function loadModJLang($name){
  721.  
  722. $jlang =JFactory::getLanguage();
  723. $tag = $jlang->getTag();
  724.  
  725. $path = $basePath = JPATH_VM_MODULES.DS.$name;
  726.  
  727. if(VmConfig::get('enableEnglish', true) and $tag!='en-GB'){
  728. $testpath = $basePath.DS.'language'.DS.'en-GB'.DS.'en-GB.'.$name.'.ini';
  729. if(!file_exists($testpath)){
  730. $path = JPATH_ADMINISTRATOR;
  731. }
  732. $jlang->load($name, $path, 'en-GB');
  733. }
  734.  
  735. $testpath = $basePath.DS.'language'.DS.$tag.DS.$tag.'.'.$name.'.ini';
  736. if(!file_exists($testpath)){
  737. $path = JPATH_ADMINISTRATOR;
  738. }
  739.  
  740. $jlang->load($name, $path,$tag,true);
  741.  
  742. return $jlang;
  743. }
  744.  
  745.  
  746. /**
  747. * Loads the configuration and works as singleton therefore called static. The call using the program cache
  748. * is 10 times faster then taking from the session. The session is still approx. 30 times faster then using the file.
  749. * The db is 10 times slower then the session.
  750. *
  751. * Performance:
  752. *
  753. * Fastest is
  754. * Program Cache: 1.5974044799805E-5
  755. * Session Cache: 0.00016094612121582
  756. *
  757. * First config db load: 0.00052118301391602
  758. * Parsed and in session: 0.001554012298584
  759. *
  760. * After install from file: 0.0040450096130371
  761. * Parsed and in session: 0.0051419734954834
  762. *
  763. *
  764. * Functions tests if already loaded in program cache, session cache, database and at last the file.
  765. *
  766. * Load the configuration values from the database into a session variable.
  767. * This step is done to prevent accessing the database for every configuration variable lookup.
  768. *
  769. * @author Max Milbers
  770. * @param $force boolean Forces the function to load the config from the db
  771. */
  772. static public function loadConfig($force = FALSE,$fresh = FALSE) {
  773.  
  774. if($fresh){
  775. self::$_jpConfig = new VmConfig();
  776. return self::$_jpConfig;
  777. }
  778. vmSetStartTime('loadConfig');
  779. if(!$force){
  780. if(!empty(self::$_jpConfig) && !empty(self::$_jpConfig->_params)){
  781. return self::$_jpConfig;
  782. }
  783. }
  784.  
  785. self::$_jpConfig = new VmConfig();
  786.  
  787. if(!class_exists('VirtueMartModelConfig')) require(VMPATH_ADMIN .'/models/config.php');
  788. $configTable = VirtueMartModelConfig::checkConfigTableExists();
  789.  
  790. $app = JFactory::getApplication();
  791. $db = JFactory::getDBO();
  792.  
  793. self::$installed = true;
  794. $install = vRequest::getInt('install',false);
  795. $redirected = vRequest::getInt('redirected',false);
  796. $link='';
  797. $msg = '';
  798.  
  799. if(empty($configTable) ){
  800. self::$installed = false;
  801. $jlang =JFactory::getLanguage();
  802. $selectedLang = $jlang->getTag();
  803.  
  804. if(empty($selectedLang)){
  805. $selectedLang = $jlang->setLanguage($selectedLang);
  806. }
  807.  
  808. $q = 'SELECT `element` FROM `#__extensions` WHERE type = "language" and enabled = "1"';
  809. $db->setQuery($q);
  810. $knownLangs = $db->loadColumn();
  811. //vmdebug('Selected language '.$selectedLang.' $knownLangs ',$knownLangs);
  812.  
  813. if($app->isAdmin() and !$redirected and !in_array($selectedLang,$knownLangs)){
  814. //$option = vRequest::getVar('option');
  815. //VmConfig::$_debug=true;
  816. //vmdebug('my option',$option,$_REQUEST);
  817. //if($option!='com_languages'){
  818. $msg = 'Install your selected language <b>'.$selectedLang.'</b> first in <a href="'.$link.'">joomla language manager</a>, just select then the component VirtueMart under menu "component", to proceed with the installation ';
  819. //$link = 'index.php?option=com_installer&view=languages&redirected=1';
  820. //$app->redirect($link,$msg);
  821. //}
  822. $app->enqueueMessage($msg);
  823. }
  824.  
  825. self::$installed = VirtueMartModelConfig::checkVirtuemartInstalled();
  826. if(!self::$installed){
  827. if(!$redirected and !$install){
  828. $link = 'index.php?option=com_virtuemart&view=updatesmigration&redirected=1';
  829.  
  830. if($app->isSite()){
  831. $link = JURI::root(true).'/administrator/'.$link;
  832. } else {
  833. if(empty($msg)) $msg = 'Install Virtuemart first, click on the menu component and select VirtueMart';
  834. }
  835. }
  836. }
  837. } else {
  838. $query = ' SELECT `config` FROM `#__virtuemart_configs` WHERE `virtuemart_config_id` = "1";';
  839. $db->setQuery($query);
  840. self::$_jpConfig->_raw = $db->loadResult();
  841. //vmTime('time to load config','loadConfig');
  842. }
  843.  
  844. if(empty(self::$_jpConfig->_raw)){
  845. $_value = VirtueMartModelConfig::readConfigFile();
  846. if (!$_value) {
  847. vmError('Serious error, config file could not be filled with data');
  848. return FALSE;
  849. }
  850. $_value = join('|', $_value);
  851. self::$_jpConfig->_raw = $_value;
  852. self::$_jpConfig->setParams(self::$_jpConfig->_raw);
  853.  
  854. self::$_jpConfig->storeConfig();
  855. } else {
  856. self::$_jpConfig->setParams(self::$_jpConfig->_raw);
  857. }
  858.  
  859. self::$_secret = JFactory::getConfig()->get('secret');
  860.  
  861. self::$_jpConfig->_params['sctime'] = microtime(TRUE);
  862. self::$_jpConfig->_params['vmlang'] = self::setdbLanguageTag();
  863.  
  864. vmTime('time to load config','loadConfig');
  865.  
  866. if($app->isSite()){
  867. // try plugins
  868. JPluginHelper::importPlugin('vmuserfield');
  869. $dispatcher = JDispatcher::getInstance();
  870. $dispatcher->trigger('plgVmInitialise', array());
  871. }
  872.  
  873. if(!self::$installed){
  874. $user = JFactory::getUser();
  875. if($user->authorise('core.admin','com_virtuemart') and ($install or $redirected)){
  876. VmConfig::$_jpConfig->set('dangeroustools',1);
  877. }
  878. if(!empty($msg)) $app->enqueueMessage($msg);
  879. if(!empty($link)) $app->redirect($link);
  880. }
  881.  
  882. return self::$_jpConfig;
  883. }
  884.  
  885. static public function storeConfig(){
  886.  
  887. $user = JFactory::getUser();
  888. if($user->authorise('core.admin','com_virtuemart')){
  889. $installed = VirtueMartModelConfig::checkVirtuemartInstalled();
  890. if($installed){
  891.  
  892. VirtueMartModelConfig::installVMconfigTable();
  893.  
  894. $confData = array();
  895. $confData['virtuemart_config_id'] = 1;
  896.  
  897. $confData['config'] = VmConfig::$_jpConfig->toString();
  898. $confTable = VmTable::getInstance('configs', 'Table', array());
  899.  
  900. if (!$confTable->bindChecknStore($confData)) {
  901. vmError('storeConfig was not able to store config');
  902. }
  903. } else {
  904. self::$_virtuemart_vendor_id = 1;
  905. }
  906. }
  907. }
  908.  
  909. /*
  910. * Set defaut language tag for translatable table
  911. *
  912. * @author Max Milbers
  913. * @return string valid langtag
  914. */
  915. static public function setdbLanguageTag() {
  916.  
  917. if (self::$vmlang) {
  918. return self::$vmlang;
  919. }
  920.  
  921. $langs = (array)self::get('active_languages',array());
  922. self::$langCount = count($langs);
  923. $siteLang = vRequest::getString('vmlang',false );
  924.  
  925. $params = JComponentHelper::getParams('com_languages');
  926. $defaultLang = $params->get('site', 'en-GB');//use default joomla
  927.  
  928. if( JFactory::getApplication()->isSite()){
  929. if (!$siteLang) {
  930. jimport('joomla.language.helper');
  931. $siteLang = JFactory::getLanguage()->getTag();
  932. }
  933. } else {
  934. if(!$siteLang){
  935. $siteLang = $defaultLang;
  936. }
  937. }
  938.  
  939. if(!in_array($siteLang, $langs)) {
  940. if(count($langs)===0){
  941. $siteLang = $defaultLang;
  942. } else {
  943. $siteLang = $langs[0];
  944. }
  945. }
  946. self::$vmlangTag = $siteLang;
  947. self::$vmlang = strtolower(strtr($siteLang,'-','_'));
  948. self::$defaultLang = strtolower(strtr($defaultLang,'-','_'));
  949. vmdebug('$siteLang: '.$siteLang.' self::$_jpConfig->lang '.self::$vmlang);
  950. //@deprecated just fallback
  951. defined('VMLANG') or define('VMLANG', self::$vmlang );
  952.  
  953. return self::$vmlang;
  954. }
  955.  
  956. /**
  957. * Find the configuration value for a given key
  958. *
  959. * @author Max Milbers
  960. * @param string $key Key name to lookup
  961. * @return Value for the given key name
  962. */
  963. static function get($key, $default='',$allow_load=FALSE)
  964. {
  965.  
  966. $value = '';
  967. if ($key) {
  968.  
  969. if (empty(self::$_jpConfig->_params) && $allow_load) {
  970. self::loadConfig();
  971. }
  972.  
  973. if (!empty(self::$_jpConfig->_params)) {
  974. if(array_key_exists($key,self::$_jpConfig->_params) && isset(self::$_jpConfig->_params[$key])){
  975. $value = self::$_jpConfig->_params[$key];
  976. } else {
  977. $value = $default;
  978. }
  979.  
  980. } else {
  981. $value = $default;
  982. }
  983.  
  984. } else {
  985. $app = JFactory::getApplication();
  986. $app -> enqueueMessage('VmConfig get, empty key given');
  987. }
  988.  
  989. return $value;
  990. }
  991.  
  992. static function set($key, $value){
  993.  
  994. if (empty(self::$_jpConfig->_params)) {
  995. self::loadConfig();
  996. }
  997.  
  998. if($admin = JFactory::getUser()->authorise('core.admin', 'com_virtuemart')){
  999. if (!empty(self::$_jpConfig->_params)) {
  1000. self::$_jpConfig->_params[$key] = $value;
  1001. }
  1002. }
  1003.  
  1004. }
  1005.  
  1006. /**
  1007. * For setting params, needs assoc array
  1008. * @author Max Milbers
  1009. */
  1010. function setParams($params){
  1011.  
  1012. $config = explode('|', $params);
  1013. $app = JFactory::getApplication();
  1014. foreach($config as $item){
  1015. $item = explode('=',$item);
  1016. if(!empty($item[1])){
  1017. $value = self::parseJsonUnSerialize($item[1],$item[0]);
  1018. if($value!==null){
  1019. $pair[$item[0]] = $value;
  1020. }
  1021.  
  1022. } else {
  1023. $pair[$item[0]] ='';
  1024. }
  1025.  
  1026. }
  1027.  
  1028. self::$_jpConfig->_params = $pair;
  1029.  
  1030. }
  1031.  
  1032.  
  1033. public static function parseJsonUnSerialize($in,$b64Str = false){
  1034.  
  1035. $value = json_decode($in ,$b64Str);
  1036. $ser = false;
  1037. switch(json_last_error()) {
  1038. case JSON_ERROR_DEPTH:
  1039. echo ' - Maximum stack depth exceeded';
  1040. return null;
  1041. case JSON_ERROR_CTRL_CHAR:
  1042. echo ' - Unexpected control character found';
  1043. $ser = true;
  1044. break;
  1045. case JSON_ERROR_SYNTAX:
  1046. //echo ' - Syntax error, malformed JSON';
  1047. $ser = true;
  1048. break;
  1049. case JSON_ERROR_NONE:
  1050. return $value;
  1051. }
  1052.  
  1053. if($ser){
  1054. try {
  1055. if($b64Str and $b64Str==='offline_message' ){
  1056. $value = @unserialize(base64_decode($in) );
  1057. } else {
  1058. $value = @unserialize( $in );
  1059. }
  1060. vmdebug('Error in Json_encode use unserialize ',$in,$value);
  1061. return $value;
  1062. }catch (Exception $e) {
  1063. vmdebug('Exception in loadConfig for unserialize '. $e->getMessage(),$in);
  1064. }
  1065. }
  1066. }
  1067.  
  1068. /**
  1069. * Writes the params as string and escape them before
  1070. * @author Max Milbers
  1071. */
  1072. function toString(){
  1073. $raw = '';
  1074.  
  1075. foreach(self::$_jpConfig->_params as $paramkey => $value){
  1076.  
  1077. //Texts get broken, when serialized, therefore we do a simple encoding,
  1078. //btw we need serialize for storing arrays note by Max Milbers
  1079. //if($paramkey!=='offline_message'){
  1080. $raw .= $paramkey.'='.json_encode($value).'|';
  1081. /*} else {
  1082. $raw .= $paramkey.'='.base64_encode(serialize($value)).'|';
  1083. }*/
  1084. }
  1085. self::$_jpConfig->_raw = substr($raw,0,-1);
  1086. return self::$_jpConfig->_raw;
  1087. }
  1088.  
  1089. /**
  1090. * Find the currenlty installed version
  1091. *
  1092. * @author RickG
  1093. * @param boolean $includeDevStatus True to include the development status
  1094. * @return String of the currently installed version
  1095. */
  1096. static function getInstalledVersion($includeDevStatus=FALSE) {
  1097. return vmVersion::$RELEASE;
  1098. }
  1099.  
  1100. /**
  1101. * Return if the used joomla function is j15
  1102. * @deprecated use JVM_VERSION instead
  1103. */
  1104. function isJ15(){
  1105. return (strpos(JVERSION,'1.5') === 0);
  1106. }
  1107.  
  1108. /**
  1109. * Checks if user is admin or has vendorId=1,
  1110. * if superadmin, but not a vendor it gives back vendorId=1 (single vendor, but multiuser administrated)
  1111. *
  1112. * @author Mattheo Vicini
  1113. * @author Max Milbers
  1114. */
  1115. static public function isSuperVendor($adminId = null){
  1116.  
  1117.  
  1118. if(!isset(self::$_virtuemart_vendor_id[$adminId])){
  1119.  
  1120. self::$_virtuemart_vendor_id[$adminId] = 0;
  1121. if(empty($adminId)){
  1122. $adminId = JFactory::getSession()->get('vmAdminID',null);
  1123. if($adminId) {
  1124. if(!class_exists('vmCrypt'))
  1125. require(VMPATH_ADMIN.DS.'helpers'.DS.'vmcrypt.php');
  1126. $adminId = vmCrypt::decrypt( $adminId );
  1127. }
  1128. $user = JFactory::getUser($adminId);
  1129. } else {
  1130. $user = JFactory::getUser($adminId);
  1131. }
  1132.  
  1133. if(!empty( $user->id)){
  1134. $q='SELECT `virtuemart_vendor_id` FROM `#__virtuemart_vmusers` `au`
  1135. WHERE `au`.`virtuemart_user_id`="' .$user->id.'" AND `au`.`user_is_vendor` = "1" ';
  1136.  
  1137. $db= JFactory::getDbo();
  1138. $db->setQuery($q);
  1139. $virtuemart_vendor_id = $db->loadResult();
  1140.  
  1141. if ($virtuemart_vendor_id) {
  1142. self::$_virtuemart_vendor_id[$adminId] = $virtuemart_vendor_id;
  1143. } else {
  1144. if($user->authorise('core.admin', 'com_virtuemart') or (self::get('multix','none')=='none' and $user->authorise('core.manage', 'com_virtuemart') ) ){
  1145. self::$_virtuemart_vendor_id[$adminId] = 1;
  1146. }
  1147. }
  1148. }
  1149. if(empty(self::$_virtuemart_vendor_id[$adminId]))vmdebug('Not a vendor');
  1150. }
  1151. return self::$_virtuemart_vendor_id[$adminId];
  1152. }
  1153.  
  1154. }
  1155.  
  1156.  
  1157. class vmURI{
  1158.  
  1159. static function getCleanUrl ($JURIInstance = 0,$parts = array('scheme', 'user', 'pass', 'host', 'port', 'path', 'query', 'fragment')) {
  1160.  
  1161. if(!class_exists('JFilterInput')) require (VMPATH_LIBS.DS.'joomla'.DS.'filter'.DS.'input.php');
  1162. $_filter = JFilterInput::getInstance(array('br', 'i', 'em', 'b', 'strong'), array(), 0, 0, 1);
  1163. if($JURIInstance===0)$JURIInstance = JURI::getInstance();
  1164. return $_filter->clean($JURIInstance->toString($parts));
  1165. }
  1166. }
  1167.  
  1168.  
  1169. // pure php no closing tag
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement