/main_oauth.php

https://github.com/VineGlobal/AngularJS-eCommerce · PHP · 201 lines · 130 code · 57 blank · 14 comment · 42 complexity · 97eefed4cc6a00e613acdceb0c1a1f52 MD5 · raw file

  1. <?php
  2. error_reporting(E_ALL);
  3. ini_set('display_errors', 1);
  4. require("phpfastcache.php");
  5. include('GoogleSpreadsheetAPI.class.php');
  6. function set_google_auth($ini) {
  7. $ga = new GoogleSpreadsheetAPI('service');
  8. $ga->auth->setClientId($ini['google_client_id']);
  9. $ga->auth->setEmail($ini['google_email']);
  10. $ga->auth->setPrivateKey($ini['google_private_key']);
  11. $auth = $ga->auth->getAccessToken();
  12. // Try to get the AccessToken
  13. if ($auth['http_code'] == 200) {
  14. $accessToken = $auth['access_token'];
  15. $tokenExpires = $auth['expires_in'];
  16. $tokenCreated = time();
  17. } else {
  18. // error...
  19. die('AUTH FAILED');
  20. }
  21. $ga->setAccessToken($accessToken);
  22. return $ga;
  23. }
  24. //** fetch the ini config variables **/
  25. $ini = parse_ini_file("app_config.ini");
  26. $timeout = $ini['cachettl'];
  27. // simple Caching with:
  28. $cache = phpFastCache();
  29. $store_config = $cache->get("store_config_cache");
  30. if($store_config == null) {
  31. if (!isset($ga)) {
  32. $ga = set_google_auth($ini);
  33. }
  34. $params = array('gid'=>$ini['google_tab_store_config'], 'format'=> 'csv');
  35. $_config = $ga->getWorksheet($ini['google_worksheet_url'],$params);
  36. $store_config = array();
  37. foreach ($_config as $arr) {
  38. $store_config[$arr['key']] = $arr['value'];
  39. }
  40. // Write products to Cache in 10 minutes with same keyword
  41. $cache->set("store_config_cache",$store_config , $timeout);
  42. } else {
  43. /// echo " --> USE CACHE --> store_config_cache FROM CACHE ---> ";
  44. }
  45. // Try to get $products from Caching First
  46. $products = $cache->get("products_cache");
  47. if($products == null) {
  48. if (!isset($ga)) {
  49. $ga = set_google_auth();
  50. }
  51. $params = array('gid'=>$ini['google_tab_product_catalog'], 'format'=> 'csv');
  52. $temp_products = $ga->getWorksheet($ini['google_worksheet_url'],$params);
  53. $products = array();
  54. foreach ($temp_products as $product) {
  55. /** Parent, and Simple with no parentsku **/
  56. if ($product['producttype'] == 'Parent' || ($product['producttype'] == 'Simple' && $product['parentsku'] == '' )) {
  57. $products[] = $product;
  58. }
  59. }
  60. // Write products to Cache in 10 minutes with same keyword
  61. $cache->set("products_cache",$products , $timeout);
  62. } else {
  63. //echo " --> USE CACHE --> products_cache Visitors FROM CACHE ---> ";
  64. }
  65. $language = get_language();
  66. $translations = $cache->get("translations_cache_".$language);
  67. if($translations == null) {
  68. if (!isset($ga)) {
  69. $ga = set_google_auth();
  70. }
  71. if ($language == 'es') {
  72. $params = array('gid'=>$ini['google_tab_es'], 'format'=> 'csv');
  73. $_trans = $ga->getWorksheet($ini['google_worksheet_url'],$params);
  74. }
  75. else if ($language == 'zh') {
  76. $params = array('gid'=>$ini['google_tab_zh'], 'format'=> 'csv');
  77. $_trans = $ga->getWorksheet($ini['google_worksheet_url'],$params);
  78. }
  79. else {
  80. $params = array('gid'=>$ini['google_tab_en'], 'format'=> 'csv');
  81. $_trans = $ga->getWorksheet($ini['google_worksheet_url'],$params);
  82. }
  83. $translations = array();
  84. foreach ($_trans as $arr) {
  85. $translations[$arr['key']] = $arr['value'];
  86. }
  87. // Write products to Cache in 10 minutes with same keyword
  88. $cache->set("translations_cache_".$language,$translations , $timeout);
  89. } else {
  90. //echo " --> USE CACHE --> translations FROM CACHE ---> ";
  91. }
  92. $types = $cache->get("types_cache");
  93. if($types == null) {
  94. if (!isset($ga)) {
  95. $ga = set_google_auth();
  96. }
  97. $params = array('gid'=>$ini['google_tab_types'], 'format'=> 'csv');
  98. $types = $ga->getWorksheet($ini['google_worksheet_url'],$params);
  99. $categories = array();
  100. $product_types = array();
  101. $colors = array();
  102. foreach ($types as $data) {
  103. $categories[$data['categories']] = _l($data['categories']);
  104. if (isset($data['producttype']) && $data['producttype'] != "") {
  105. $product_types[] = $data['producttype'];
  106. }
  107. if (isset($data['colors']) && $data['colors'] != "") {
  108. $colors[] = $data['colors'];
  109. }
  110. }
  111. $types['categories'] = $categories;
  112. $types['product_types'] = $product_types;
  113. $types['colors'] = $colors;
  114. // Write products to Cache in 10 minutes with same keyword
  115. $cache->set("types_cache",$types , $timeout);
  116. } else {
  117. /// echo " --> USE CACHE --> store_config_cache FROM CACHE ---> ";
  118. }
  119. function _m($key) {
  120. global $store_config;
  121. if (isset($store_config[$key])) {
  122. return $store_config[$key];
  123. } else {
  124. return $key;
  125. }
  126. }
  127. function _l($key) {
  128. global $translations;
  129. if (isset($translations[$key])) {
  130. return $translations[$key];
  131. } else {
  132. return $key;
  133. }
  134. }
  135. function get_language() {
  136. $lang = 'en';
  137. $domain = explode('-',$_SERVER['SERVER_NAME']);
  138. if (isset($domain[1])) {
  139. $parts = explode('.',$domain[1]);
  140. $lang = $parts[0];
  141. }
  142. return $lang;
  143. }
  144. ?>