PageRenderTime 48ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/local/GoMage/Checkout/Helper/Data.php

https://bitbucket.org/acidel/buykoala
PHP | 438 lines | 295 code | 132 blank | 11 comment | 41 complexity | 01b48c66534539c33991c5ac9e95608d MD5 | raw file
  1. <?php
  2. /**
  3. * GoMage LightCheckout Extension
  4. *
  5. * @category Extension
  6. * @copyright Copyright (c) 2010-2012 GoMage (http://www.gomage.com)
  7. * @author GoMage
  8. * @license http://www.gomage.com/license-agreement/ Single domain license
  9. * @terms of use http://www.gomage.com/terms-of-use
  10. * @version Release: 3.2
  11. * @since Class available since Release 1.0
  12. */
  13. class GoMage_Checkout_Helper_Data extends Mage_Core_Helper_Abstract{
  14. protected $mode;
  15. protected $country_id;
  16. public function getConfigData($node){
  17. return Mage::getStoreConfig('gomage_checkout/'.$node);
  18. }
  19. public function getCheckoutMode(){
  20. if(is_null($this->mode)){
  21. if(Mage::getSingleton('gomage_checkout/type_onestep')->getQuote()->isAllowedGuestCheckout()){
  22. $this->mode = intval($this->getConfigData('registration/mode'));
  23. }else{
  24. $this->mode = 1;
  25. }
  26. }
  27. return $this->mode;
  28. }
  29. public function getAllStoreDomains(){
  30. $domains = array();
  31. foreach (Mage::app()->getWebsites() as $website) {
  32. $url = $website->getConfig('web/unsecure/base_url');
  33. if($domain = trim(preg_replace('/^.*?\\/\\/(.*)?\\//', '$1', $url))){
  34. $domains[] = $domain;
  35. }
  36. $url = $website->getConfig('web/secure/base_url');
  37. if($domain = trim(preg_replace('/^.*?\\/\\/(.*)?\\//', '$1', $url))){
  38. $domains[] = $domain;
  39. }
  40. }
  41. return array_unique($domains);
  42. }
  43. public function getAvailabelWebsites(){
  44. return $this->_w();
  45. }
  46. public function getAvailavelWebsites(){
  47. return $this->_w();
  48. }
  49. protected function _w(){
  50. if(!Mage::getStoreConfig('gomage_activation/lightcheckout/installed') ||
  51. (intval(Mage::getStoreConfig('gomage_activation/lightcheckout/count')) > 10))
  52. {
  53. return array();
  54. }
  55. $time_to_update = 60*60*24*15;
  56. $r = Mage::getStoreConfig('gomage_activation/lightcheckout/ar');
  57. $t = Mage::getStoreConfig('gomage_activation/lightcheckout/time');
  58. $s = Mage::getStoreConfig('gomage_activation/lightcheckout/websites');
  59. $last_check = str_replace($r, '', Mage::helper('core')->decrypt($t));
  60. $allsites = explode(',', str_replace($r, '', Mage::helper('core')->decrypt($s)));
  61. $allsites = array_diff($allsites, array(""));
  62. if(($last_check+$time_to_update) < time()){
  63. $this->a(Mage::getStoreConfig('gomage_activation/lightcheckout/key'),
  64. intval(Mage::getStoreConfig('gomage_activation/lightcheckout/count')),
  65. implode(',', $allsites));
  66. }
  67. return $allsites;
  68. }
  69. public function a($k, $c = 0, $s = ''){
  70. $ch = curl_init();
  71. curl_setopt($ch, CURLOPT_URL, sprintf('https://www.gomage.com/index.php/gomage_downloadable/key/check'));
  72. curl_setopt($ch, CURLOPT_POST, true);
  73. curl_setopt($ch, CURLOPT_POSTFIELDS, 'key='.urlencode($k).'&sku=lightcheckout&domains='.urlencode(implode(',', $this->getAllStoreDomains())));
  74. curl_setopt($ch, CURLOPT_TIMEOUT, 30);
  75. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  76. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  77. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  78. $content = curl_exec($ch);
  79. $r = Zend_Json::decode($content);
  80. $e = Mage::helper('core');
  81. if(empty($r)){
  82. $value1 = Mage::getStoreConfig('gomage_activation/lightcheckout/ar');
  83. $groups = array(
  84. 'lightcheckout'=>array(
  85. 'fields'=>array(
  86. 'ar'=>array(
  87. 'value'=>$value1
  88. ),
  89. 'websites'=>array(
  90. 'value'=>(string)Mage::getStoreConfig('gomage_activation/lightcheckout/websites')
  91. ),
  92. 'time'=>array(
  93. 'value'=>(string)$e->encrypt($value1.(time()-(60*60*24*15-1800)).$value1)
  94. ),
  95. 'count'=>array(
  96. 'value'=>$c+1)
  97. )
  98. )
  99. );
  100. Mage::getModel('adminhtml/config_data')
  101. ->setSection('gomage_activation')
  102. ->setGroups($groups)
  103. ->save();
  104. Mage::getConfig()->reinit();
  105. Mage::app()->reinitStores();
  106. return;
  107. }
  108. $value1 = '';
  109. $value2 = '';
  110. if(isset($r['d']) && isset($r['c'])){
  111. $value1 = $e->encrypt(base64_encode(Zend_Json::encode($r)));
  112. if (!$s) $s = Mage::getStoreConfig('gomage_activation/lightcheckout/websites');
  113. $s = array_slice(explode(',', $s), 0, $r['c']);
  114. $value2 = $e->encrypt($value1.implode(',', $s).$value1);
  115. }
  116. $groups = array(
  117. 'lightcheckout'=>array(
  118. 'fields'=>array(
  119. 'ar'=>array(
  120. 'value'=>$value1
  121. ),
  122. 'websites'=>array(
  123. 'value'=>(string)$value2
  124. ),
  125. 'time'=>array(
  126. 'value'=>(string)$e->encrypt($value1.time().$value1)
  127. ),
  128. 'installed'=>array(
  129. 'value'=>1
  130. ),
  131. 'count'=>array(
  132. 'value'=>0)
  133. )
  134. )
  135. );
  136. Mage::getModel('adminhtml/config_data')
  137. ->setSection('gomage_activation')
  138. ->setGroups($groups)
  139. ->save();
  140. Mage::getConfig()->reinit();
  141. Mage::app()->reinitStores();
  142. }
  143. public function ga(){
  144. return Zend_Json::decode(base64_decode(Mage::helper('core')->decrypt(Mage::getStoreConfig('gomage_activation/lightcheckout/ar'))));
  145. }
  146. public function getGeoipRecord(){
  147. return GeoIP_Core::getInstance(Mage::getBaseDir('media')."/geoip/GeoLiteCity.dat", GeoIP_Core::GEOIP_STANDARD)->geoip_record_by_addr(Mage::helper('core/http')->getRemoteAddr());
  148. }
  149. public function getDefaultCountryId(){
  150. if(is_null($this->country_id)){
  151. if(Mage::getStoreConfig('gomage_checkout/geoip/geoip_enabled') && file_exists(Mage::getBaseDir('media')."/geoip/GeoLiteCity.dat") && extension_loaded('mbstring')){
  152. try{
  153. $this->country_id = GeoIP_Core::getInstance(Mage::getBaseDir('media')."/geoip/GeoLiteCity.dat", GeoIP_Core::GEOIP_STANDARD)->geoip_country_code_by_addr(Mage::helper('core/http')->getRemoteAddr());
  154. }catch(Exception $e){
  155. echo $e;
  156. }
  157. }
  158. if (!$this->country_id) {
  159. $this->country_id = Mage::getStoreConfig('gomage_checkout/general/default_country');
  160. if (!$this->country_id) {
  161. $this->country_id = Mage::getStoreConfig('general/country/default');
  162. }
  163. }
  164. }
  165. return $this->country_id;
  166. }
  167. public function getDefaultShippingMethod(){
  168. $address = Mage::getSingleton('checkout/session')->getQuote()->getShippingAddress();
  169. $address->setCollectShippingRates(true)->collectShippingRates();
  170. $rates = $address->getGroupedAllShippingRates();
  171. if(count($rates) == 1){
  172. foreach($rates as $rate_code=>$methods){
  173. if(count($methods) == 1){
  174. foreach($methods as $method){
  175. return $method->getCode();
  176. }
  177. }else{
  178. return $this->getConfigData('general/default_shipping_method');
  179. }
  180. }
  181. }else{
  182. return $this->getConfigData('general/default_shipping_method');
  183. }
  184. }
  185. public function getDefaultPaymentMethod(){
  186. return $this->getConfigData('general/default_payment_method');
  187. }
  188. public function getActivePaymentMethods($store=null)
  189. {
  190. $methods = array();
  191. $config = Mage::getStoreConfig('payment', $store);
  192. foreach ($config as $code => $methodConfig) {
  193. if(isset($methodConfig['model']) && $methodConfig['model']){
  194. if(isset($methodConfig['group']) && $methodConfig['group'] == 'mbookers' && Mage::getStoreConfigFlag('moneybookers/'.$code.'/active', $store)){
  195. $method = $this->_getPaymentMethod($code, $methodConfig);
  196. $method['group'] = 'mbookers';
  197. $methods[$code] = $method;
  198. }elseif($methodConfig['model'] == 'googlecheckout/payment'){
  199. if(Mage::getStoreConfigFlag('google/checkout/active', $store)){
  200. $method = $this->_getPaymentMethod($code, $methodConfig);
  201. $methods[$code] = $method;
  202. }
  203. }elseif (Mage::getStoreConfigFlag('payment/'.$code.'/active', $store)) {
  204. $method = $this->_getPaymentMethod($code, $methodConfig);
  205. $method['group'] = '';
  206. $methods[$code] = $method;
  207. }
  208. }
  209. }
  210. return $methods;
  211. }
  212. protected function _getPaymentMethod($code, $config, $store=null)
  213. {
  214. $modelName = $config['model'];
  215. $method = Mage::getModel($modelName);
  216. if($method){
  217. $method->setId($code)->setStore($store);
  218. }
  219. return $method;
  220. }
  221. public function getVatBaseCountryMode(){
  222. return $this->getConfigData('vat/base_country');
  223. }
  224. public function getVatWithinCountryMode(){
  225. return $this->getConfigData('vat/if_not_base_country');
  226. }
  227. public function getTaxCountries(){
  228. if($rule_ids = Mage::helper('gomage_checkout')->getConfigData('vat/rule')){
  229. $resource = Mage::getSingleton('core/resource');
  230. $connection = $resource->getConnection('read');
  231. $q = sprintf('SELECT DISTINCT(`tax_country_id`) FROM `%s` WHERE `tax_calculation_rate_id` IN (SELECT `tax_calculation_rate_id` FROM `%s` WHERE `tax_calculation_rule_id` in (%s) )', $resource->getTableName('tax_calculation_rate'), $resource->getTableName('tax_calculation'), $rule_ids);
  232. return (array)$connection->fetchCol($q);
  233. }
  234. return array();
  235. }
  236. public function getIsAnymoreVersion($major, $minor, $revision = 0)
  237. {
  238. $version_info = Mage::getVersion();
  239. $version_info = explode('.', $version_info);
  240. if ($version_info[0] > $major)
  241. {
  242. return true;
  243. }
  244. elseif ($version_info[0] == $major)
  245. {
  246. if ($version_info[1] > $minor)
  247. {
  248. return true;
  249. }
  250. elseif ($version_info[1] == $minor)
  251. {
  252. if ($version_info[2] >= $revision)
  253. {
  254. return true;
  255. }
  256. else
  257. {
  258. return false;
  259. }
  260. }
  261. else
  262. {
  263. return false;
  264. }
  265. }
  266. else
  267. {
  268. return false;
  269. }
  270. }
  271. public function isMobileDevice(){
  272. $user_agent = strtolower($_SERVER['HTTP_USER_AGENT']);
  273. if (!$user_agent || strpos($user_agent, 'ipad')) return false;
  274. $regex_match="/(nokia|iphone|android|motorola|^mot-|softbank|foma|docomo|kddi|up.browser|up.link|";
  275. $regex_match.="htc|dopod|blazer|netfront|helio|hosin|huawei|novarra|CoolPad|webos|techfaith|palmsource|";
  276. $regex_match.="blackberry|alcatel|amoi|ktouch|nexian|samsung|^sam-|s[cg]h|^lge|ericsson|philips|sagem|wellcom|bunjalloo|maui|";
  277. $regex_match.="symbian|smartphone|midp|wap|phone|windows ce|iemobile|^spice|^bird|^zte-|longcos|pantech|gionee|^sie-|portalmmm|";
  278. $regex_match.="jigs browser|hiptop|^ucweb|^benq|haier|^lct|operas*mobi|opera*mini|320x320|240x320|176x220";
  279. $regex_match.=")/i";
  280. return preg_match($regex_match, strtolower($user_agent));
  281. }
  282. public function isLefttoRightWrite(){
  283. return in_array(Mage::app()->getLocale()->getLocaleCode(), array('ar_DZ','ar_EG','ar_KW','ar_MA',
  284. 'ar_SA','he_IL','fa_IR'));
  285. }
  286. public function getGiftWrapTaxAmount(Mage_Sales_Model_Quote_Address $address, $amount){
  287. if (!intval($this->getConfigData('gift_wrapping/tax_class'))){
  288. return $amount;
  289. }
  290. $calculation = Mage::getModel('tax/calculation');
  291. $request = $calculation->getRateRequest($address, null, null, Mage::app()->getStore());
  292. $request->setProductClassId(intval($this->getConfigData('gift_wrapping/tax_class')));
  293. $taxRate = $calculation->getRate($request);
  294. return ($amount + $calculation->calcTaxAmount($amount, $taxRate));
  295. }
  296. public function getCountriesStatesRequired(){
  297. $result = array();
  298. if ($this->getConfigData('address_fields/region') == 'req'){
  299. $country_collection = Mage::helper('directory')->getCountryCollection();
  300. foreach ($country_collection as $country){
  301. $result[] = $country->getCountryId();
  302. }
  303. }
  304. return Mage::helper('core')->jsonEncode($result);
  305. }
  306. }