http_request.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. // This file is part of CorfoWS for Moodle - http://moodle.org/
  3. //
  4. // Moodle is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // Moodle is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * @package local_corfows
  18. * @author Hackware Human <human@hackware.cl>
  19. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  20. */
  21. namespace local_corfows;
  22. defined('MOODLE_INTERNAL') || die();
  23. class http_request {
  24. public function __construct() {
  25. $this->env = getenv('CORFOWS_ENV') ?
  26. getenv('CORFOWS_ENV') : 'production';
  27. }
  28. protected static function log($data) {
  29. global $CFG;
  30. file_put_contents(
  31. $CFG->dataroot . '/corfows.log',
  32. date('c') . " $data\n",
  33. FILE_APPEND
  34. );
  35. }
  36. public function post() {
  37. $ch = curl_init($this->url);
  38. curl_setopt($ch, CURLOPT_POST, true);
  39. curl_setopt($ch, CURLOPT_HTTPHEADER, $this->headers);
  40. curl_setopt($ch, CURLOPT_POSTFIELDS, $this->data);
  41. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  42. if(getenv('CORFOWS_DONT_VERIFY_SSL')) {
  43. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  44. }
  45. $response = curl_exec($ch);
  46. if(getenv('CORFOWS_LOG')) {
  47. if (curl_error($ch)) $response = curl_error($ch);
  48. static::log(
  49. $this->url . ' - ' .
  50. json_encode( $this->data ) . ' - ' .
  51. $response
  52. );
  53. }
  54. curl_close($ch);
  55. return $response;
  56. }
  57. protected static function bearer_token() {
  58. $request = new static();
  59. if ($request->env === 'testing') {
  60. $request->url = 'https://apitest.corfo.cl:9101/api/oauth/token';
  61. } else if ($request->env === 'production') {
  62. $request->url = 'https://www.corfo.cl/oauth/token';
  63. }
  64. $request->data = http_build_query([
  65. 'scope' => 'resource.READ',
  66. 'client_secret' => getenv('CORFOWS_CLIENT_SECRET'),
  67. 'client_id' => getenv('CORFOWS_CLIENT_ID'),
  68. 'grant_type' => 'client_credentials'
  69. ], '', '&');
  70. $request->headers = [
  71. 'Content-type: application/x-www-form-urlencoded;charset=utf-8'
  72. ];
  73. $response = $request->post();
  74. return $response ? json_decode($response) : false;
  75. }
  76. public static function validation($codcert) {
  77. \require_login();
  78. $tokenresp = static::bearer_token();
  79. if (property_exists($tokenresp, 'error_description')) {
  80. return $tokenresp;
  81. }
  82. global $USER, $COURSE;
  83. $request = new static();
  84. if ($request->env === 'testing') {
  85. $request->url = 'https://apitest.corfo.cl:9101/OAG/API_WS_MOOC/Validate';
  86. } else if ($request->env === 'production') {
  87. $request->url = 'https://www.corfo.cl/api/StartupJourneyWSApiMooc/Validate';
  88. }
  89. $request->data = json_encode([
  90. 'Institucion' => getenv('CORFOWS_INSTITUTION_ID'),
  91. 'Rut' => $USER->username,
  92. 'Contenido' => $COURSE->idnumber,
  93. 'NombreContenido' => $COURSE->fullname,
  94. 'CodigoCertificacion' => $codcert,
  95. 'Evaluacion' => null,
  96. 'Correo' => $USER->email
  97. ]);
  98. $request->headers = [
  99. "Authorization:Bearer $tokenresp->access_token",
  100. 'Content-Type:application/json',
  101. ];
  102. $response = $request->post();
  103. return $response ? json_decode($response) : false;
  104. }
  105. }