PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/backend/config/definitions.php

https://gitlab.com/scsistemas/superprime
PHP | 262 lines | 188 code | 35 blank | 39 comment | 9 complexity | bd74cce6e00ad16de009da28f8ed782c MD5 | raw file
  1. <?php
  2. //$urlWS = 'http://qa.superprimeweb.com/admin/index.php?';
  3. $urlWS = 'http://localhost:8080/superprime/admin/index.php?';
  4. $rutaFotos = '../images/fotos/';
  5. //$rutaFotos = $_SERVER['REQUEST_SCHEME'] . '://' . $_SERVER['HTTP_HOST'] . '/backend/images/fotos/';
  6. $variable = array('ml_id','ml_secretKey', 'ml_authCode', 'ml_token', 'ml_fecToken', 'ml_expires', 'ml_redirectUri');
  7. //ini_set('display_errors', '1');
  8. foreach ($variable as $key ) {
  9. $dataML[$key] = obtenerClaveValor($urlWS, $key);
  10. }
  11. function comprobarRenovarToken($dataML,$urlWS){
  12. $fecExpira = $dataML['ml_fecToken'] + $dataML['ml_expires'];
  13. //print_r('fecha expira: ' . $fecExpira . ', fecha actual: ' . time() . ', fecha(300s): ' . ($fecExpira - time()) . '</br>');
  14. if(time() > ($fecExpira) || ($fecExpira - time()) < 300){//si el token expiro o su tiempo de vida es menor de 5 min(300 Seg) se renueva
  15. $tokenPre = $dataML['ml_token'];
  16. //print_r('TokenPre: '.$tokenPre.'</br>');
  17. $dataML = refreshToken($dataML, $urlWS);
  18. $tokenPost = $dataML['ml_token'];
  19. //print_r('TokenPost: '.$tokenPost.'</br>');
  20. if($tokenPre == $tokenPost){//hubo una falla en la renovacion del token
  21. $dataML['error'] = true;
  22. }else{
  23. $dataML['error'] = false;
  24. }
  25. }else{//no hace falta la renovacion ya que el token aun esta activo
  26. $dataML['error'] = false;
  27. }
  28. return $dataML;
  29. }
  30. function actulizarClaveValor($urlWS, $clave, $valor){
  31. $hashParam = array('p_clave' => $clave, 'p_valor' => $valor);
  32. $hashParam = json_encode($hashParam);
  33. $hashUrl = $urlWS . 'service=generalservices&metodo=actualizarClaveValor';
  34. $curl = curl_init($hashUrl);
  35. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  36. curl_setopt($curl, CURLOPT_POST, true);
  37. curl_setopt($curl, CURLOPT_POSTFIELDS, $hashParam);
  38. $responseHash = curl_exec($curl);
  39. curl_close($curl);
  40. }
  41. function obtenerClaveValor($urlWS, $clave){
  42. $urlMLData = $urlWS . 'service=generalservices&metodo=consultarClaveValor&p_clave=' . $clave;
  43. //$respMLData = file_get_contents($urlMLData);
  44. //$respMLData = substr($respMLData, 3);
  45. //$respMLData = json_decode($respMLData, true);
  46. $respMLData = consumoServicioGet($urlMLData);
  47. return $respMLData['valor'];
  48. }
  49. function obtenerCode($dataML){
  50. $service_url = 'http://auth.mercadolibre.com.ve/authorization?response_type=code&client_id=' . $dataML['ml_id'] . '&redirect_uri=' . $dataML['ml_redirectUri'];
  51. //print_r('urlML: ' . $service_url);
  52. $redir = "Location: " . $service_url;
  53. //print_r('</br>service: ' . $redir);
  54. header($redir);
  55. }
  56. function obtenerToken($dataML, $urlWS){
  57. $tokenParam = array('grant_type' => 'authorization_code',
  58. 'client_id' => $dataML['ml_id'],
  59. 'client_secret' => $dataML['ml_secretKey'],
  60. 'code' => $dataML['ml_authCode'],
  61. 'redirect_uri' => $dataML['ml_redirectUri']);
  62. //print_r($tokenParam);
  63. //print_r('tokenParam: ' . json_encode($tokenParam) . '</br>');
  64. $service_url = 'https://api.mercadolibre.com/oauth/token';
  65. $curl = curl_init($service_url);
  66. curl_setopt($curl, CURLOPT_POST, true);
  67. curl_setopt($curl, CURLOPT_POSTFIELDS, $tokenParam);
  68. curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
  69. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  70. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  71. $responseToken = curl_exec($curl);
  72. curl_close($curl);
  73. //print_r('respToken: ' . $responseToken . '</br>');
  74. //se actualizan los valores en la variable de sesion
  75. if($responseToken != null){
  76. $responseToken = json_decode($responseToken,true);
  77. //print_r($responseToken);
  78. if(!isset($responseToken['message'])){
  79. $dataML['ml_fecToken'] = (time() - 30);
  80. $dataML['ml_expires'] = $responseToken['expires_in'];
  81. $dataML['ml_token'] = $responseToken['access_token'];
  82. $dataML['ml_authCode'] = $responseToken['refresh_token'];
  83. actulizarClaveValor($urlWS, 'ml_fecToken', $dataML['ml_fecToken']);
  84. actulizarClaveValor($urlWS, 'ml_expires', $dataML['ml_expires']);
  85. actulizarClaveValor($urlWS, 'ml_token', $dataML['ml_token']);
  86. actulizarClaveValor($urlWS, 'ml_authCode', $dataML['ml_authCode']);
  87. }
  88. }
  89. return $dataML;
  90. }
  91. function refreshToken($dataML, $urlWS){
  92. $tokenParam = array('grant_type' => 'refresh_token',
  93. 'client_id' => $dataML['ml_id'],
  94. 'client_secret' => $dataML['ml_secretKey'],
  95. 'refresh_token' => $dataML['ml_authCode']);
  96. //print_r('tokenParam: ' . json_encode($tokenParam) . '</br>');
  97. //print_r('tokenParam:</br>');
  98. //print_r($tokenParam);
  99. $service_url = 'https://api.mercadolibre.com/oauth/token';
  100. $curl = curl_init($service_url);
  101. curl_setopt($curl, CURLOPT_POST, true);
  102. curl_setopt($curl, CURLOPT_POSTFIELDS, $tokenParam);
  103. curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
  104. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  105. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  106. $responseToken = curl_exec($curl);
  107. curl_close($curl);
  108. //print_r('respToken: ' . $responseToken . '</br>');
  109. //print_r(json_decode($responseToken,true));
  110. if($responseToken != null){
  111. $responseToken = json_decode($responseToken,true);
  112. //print_r('responseToken:</br>');
  113. //print_r($responseToken);
  114. if(!isset($responseToken['message'])){
  115. $dataML['ml_authCode'] = $responseToken['refresh_token'];
  116. $dataML['ml_fecToken'] = (time() - 30);
  117. $dataML['ml_expires'] = $responseToken['expires_in'];
  118. $dataML['ml_token'] = $responseToken['access_token'];
  119. actulizarClaveValor($urlWS, 'ml_authCode', $dataML['ml_authCode']);
  120. actulizarClaveValor($urlWS, 'ml_fecToken', $dataML['ml_fecToken']);
  121. actulizarClaveValor($urlWS, 'ml_expires', $dataML['ml_expires']);
  122. actulizarClaveValor($urlWS, 'ml_token', $dataML['ml_token']);
  123. }
  124. }
  125. return $dataML;
  126. }
  127. function consumoServicioGet($urlWS){
  128. try {
  129. //print_r('</br>WS get: WS: ' . $urlWS . '</br>');
  130. $curl = curl_init($urlWS);
  131. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  132. $resp = curl_exec($curl);
  133. curl_close($curl);
  134. //print_r('WS get: resp: ' . $resp . '</br>');
  135. //$resp = substr($resp, 3);
  136. $resp = json_decode($resp, true);
  137. return $resp;
  138. } catch (Exception $e) {
  139. $error = $e->getMessage();
  140. $resp = array('success' => '0', 'error' => $error);
  141. return $resp;
  142. }
  143. }
  144. function consumoServicioPost($urlWS, $params){
  145. try {
  146. //print_r('</br>WS Post: WS: ' . $urlWS . '</br>');
  147. //print_r('params: ' . $params . '</br>');
  148. $curl = curl_init($urlWS);
  149. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  150. curl_setopt($curl, CURLOPT_POST, true);
  151. curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
  152. $resp = curl_exec($curl);
  153. //print_r('WS Post: resp: ' . $resp . '</br>');
  154. //$resp = substr($resp, 3);
  155. $resp = json_decode($resp, true);
  156. curl_close($curl);
  157. return $resp;
  158. } catch (Exception $e) {
  159. $error = $e->getMessage();
  160. $resp = array('success' => '0', 'error' => $error);
  161. return $resp;
  162. }
  163. }
  164. function consumoServicioPostSSL($urlWS, $params){
  165. try {
  166. //print_r('</br>WS PostSSL: WS: ' . $urlWS . '</br>');
  167. //print_r('params: ' . $params . '</br>');
  168. $curl = curl_init($urlWS);
  169. curl_setopt($curl, CURLOPT_POST, true);
  170. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  171. curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
  172. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  173. $resp = curl_exec($curl);
  174. curl_close($curl);
  175. //print_r('WS PostSSL: resp: ' . $resp . '</br>');
  176. $resp = json_decode($resp, true);
  177. return $resp;
  178. } catch (Exception $e) {
  179. $error = $e->getMessage();
  180. $resp = array('success' => '0', 'error' => $error);
  181. return $resp;
  182. }
  183. }
  184. function consumoServicioPut($urlWS, $params){
  185. try {
  186. //print_r('</br>WS Put: WS: ' . $urlWS . '</br>');
  187. //print_r('params: ' . $params . '</br>');
  188. $curl = curl_init($urlWS);
  189. curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
  190. curl_setopt($curl, CURLOPT_HEADER, false);
  191. curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Accept: application/json'));
  192. curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
  193. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  194. $resp = curl_exec($curl);
  195. curl_close($curl);
  196. //print_r('WS Put: resp: ' . $resp . '</br>');
  197. $resp = json_decode($resp, true);
  198. return $resp;
  199. } catch (Exception $e) {
  200. $error = $e->getMessage();
  201. $resp = array('success' => '0', 'error' => $error);
  202. return $resp;
  203. }
  204. }
  205. function consumoServicioPutSSL($urlWS, $params){
  206. try {
  207. //print_r('</br>WS Put: WS: ' . $urlWS . '</br>');
  208. //print_r('params: ' . $params . '</br>');
  209. $curl = curl_init($urlWS);
  210. curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PUT");
  211. curl_setopt($curl, CURLOPT_HEADER, false);
  212. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  213. curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Accept: application/json'));
  214. curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
  215. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  216. $resp = curl_exec($curl);
  217. curl_close($curl);
  218. //print_r('WS Put: resp: ' . $resp . '</br>');
  219. $resp = json_decode($resp, true);
  220. return $resp;
  221. } catch (Exception $e) {
  222. $error = $e->getMessage();
  223. $resp = array('success' => '0', 'error' => $error);
  224. return $resp;
  225. }
  226. }
  227. ?>