PageRenderTime 43ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/application/libraries/gmailoath.php

https://gitlab.com/engrmuhammadawais/test
PHP | 410 lines | 357 code | 42 blank | 11 comment | 29 complexity | 51cd0ce1b150b3c9ab4dbe6910809790 MD5 | raw file
Possible License(s): LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, Apache-2.0
  1. <?php
  2. class GmailOath {
  3. public $oauth_consumer_key;
  4. public $oauth_consumer_secret;
  5. public $progname;
  6. public $debug;
  7. public $callback;
  8. function __construct($params) {
  9. $this->oauth_consumer_key = $params['consumer_key'];
  10. $this->oauth_consumer_secret = $params['consumer_secret'];
  11. $this->progname = $params['argarray'];
  12. $this->debug = $params['debug']; // Set to 1 for verbose debugging output
  13. $this->callback = $params['callback'];
  14. }
  15. ////////////////// global.php open//////////////
  16. function logit($msg, $preamble=true) {
  17. // date_default_timezone_set('America/Los_Angeles');
  18. $now = date(DateTime::ISO8601, time());
  19. error_log(($preamble ? "+++${now}:" : '') . $msg);
  20. }
  21. function do_get($url, $port=80, $headers=NULL) {
  22. $retarr = array(); // Return value
  23. $curl_opts = array(CURLOPT_URL => $url,
  24. CURLOPT_PORT => $port,
  25. CURLOPT_POST => false,
  26. CURLOPT_SSL_VERIFYHOST => false,
  27. CURLOPT_SSL_VERIFYPEER => false,
  28. CURLOPT_RETURNTRANSFER => true);
  29. if ($headers) {
  30. $curl_opts[CURLOPT_HTTPHEADER] = $headers;
  31. }
  32. $response = $this->do_curl($curl_opts);
  33. if (!empty($response)) {
  34. $retarr = $response;
  35. }
  36. return $retarr;
  37. }
  38. function do_post($url, $postbody, $port=80, $headers=NULL) {
  39. $retarr = array(); // Return value
  40. $curl_opts = array(CURLOPT_URL => $url,
  41. CURLOPT_PORT => $port,
  42. CURLOPT_POST => true,
  43. CURLOPT_SSL_VERIFYHOST => false,
  44. CURLOPT_SSL_VERIFYPEER => false,
  45. CURLOPT_POSTFIELDS => $postbody,
  46. CURLOPT_RETURNTRANSFER => true);
  47. if ($headers) {
  48. $curl_opts[CURLOPT_HTTPHEADER] = $headers;
  49. }
  50. $response = do_curl($curl_opts);
  51. if (!empty($response)) {
  52. $retarr = $response;
  53. }
  54. return $retarr;
  55. }
  56. function do_curl($curl_opts) {
  57. $retarr = array(); // Return value
  58. if (!$curl_opts) {
  59. if ($this->debug) {
  60. $this->logit("do_curl:ERR:curl_opts is empty");
  61. }
  62. return $retarr;
  63. }
  64. // Open curl session
  65. $ch = curl_init();
  66. if (!$ch) {
  67. if ($this->debug) {
  68. $this->logit("do_curl:ERR:curl_init failed");
  69. }
  70. return $retarr;
  71. }
  72. // Set curl options that were passed in
  73. curl_setopt_array($ch, $curl_opts);
  74. // Ensure that we receive full header
  75. curl_setopt($ch, CURLOPT_HEADER, true);
  76. if ($this->debug) {
  77. curl_setopt($ch, CURLINFO_HEADER_OUT, true);
  78. curl_setopt($ch, CURLOPT_VERBOSE, true);
  79. }
  80. // Send the request and get the response
  81. ob_start();
  82. $response = curl_exec($ch);
  83. $curl_spew = ob_get_contents();
  84. ob_end_clean();
  85. if ($this->debug && $curl_spew) {
  86. $this->logit("do_curl:INFO:curl_spew begin");
  87. $this->logit($curl_spew, false);
  88. $this->logit("do_curl:INFO:curl_spew end");
  89. }
  90. // Check for errors
  91. if (curl_errno($ch)) {
  92. $errno = curl_errno($ch);
  93. $errmsg = curl_error($ch);
  94. if ($this->debug) {
  95. $this->logit("do_curl:ERR:$errno:$errmsg");
  96. }
  97. curl_close($ch);
  98. unset($ch);
  99. return $retarr;
  100. }
  101. if ($this->debug) {
  102. $this->logit("do_curl:DBG:header sent begin");
  103. $header_sent = curl_getinfo($ch, CURLINFO_HEADER_OUT);
  104. $this->logit($header_sent, false);
  105. $this->logit("do_curl:DBG:header sent end");
  106. }
  107. // Get information about the transfer
  108. $info = curl_getinfo($ch);
  109. // Parse out header and body
  110. $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
  111. $header = substr($response, 0, $header_size);
  112. $body = substr($response, $header_size);
  113. // Close curl session
  114. curl_close($ch);
  115. unset($ch);
  116. if ($this->debug) {
  117. $this->logit("do_curl:DBG:response received begin");
  118. if (!empty($response)) {
  119. $this->logit($response, false);
  120. }
  121. $this->logit("do_curl:DBG:response received end");
  122. }
  123. // Set return value
  124. array_push($retarr, $info, $header, $body);
  125. return $retarr;
  126. }
  127. function json_pretty_print($json, $html_output=false) {
  128. $spacer = ' ';
  129. $level = 1;
  130. $indent = 0; // current indentation level
  131. $pretty_json = '';
  132. $in_string = false;
  133. $len = strlen($json);
  134. for ($c = 0; $c < $len; $c++) {
  135. $char = $json[$c];
  136. switch ($char) {
  137. case '{':
  138. case '[':
  139. if (!$in_string) {
  140. $indent += $level;
  141. $pretty_json .= $char . "\n" . str_repeat($spacer, $indent);
  142. } else {
  143. $pretty_json .= $char;
  144. }
  145. break;
  146. case '}':
  147. case ']':
  148. if (!$in_string) {
  149. $indent -= $level;
  150. $pretty_json .= "\n" . str_repeat($spacer, $indent) . $char;
  151. } else {
  152. $pretty_json .= $char;
  153. }
  154. break;
  155. case ',':
  156. if (!$in_string) {
  157. $pretty_json .= ",\n" . str_repeat($spacer, $indent);
  158. } else {
  159. $pretty_json .= $char;
  160. }
  161. break;
  162. case ':':
  163. if (!$in_string) {
  164. $pretty_json .= ": ";
  165. } else {
  166. $pretty_json .= $char;
  167. }
  168. break;
  169. case '"':
  170. if ($c > 0 && $json[$c - 1] != '\\') {
  171. $in_string = !$in_string;
  172. }
  173. default:
  174. $pretty_json .= $char;
  175. break;
  176. }
  177. }
  178. return ($html_output) ?
  179. '<pre>' . htmlentities($pretty_json) . '</pre>' :
  180. $pretty_json . "\n";
  181. }
  182. function oauth_http_build_query($params, $excludeOauthParams=false) {
  183. $query_string = '';
  184. if (!empty($params)) {
  185. // rfc3986 encode both keys and values
  186. $keys = $this->rfc3986_encode(array_keys($params));
  187. $values = $this->rfc3986_encode(array_values($params));
  188. $params = array_combine($keys, $values);
  189. uksort($params, 'strcmp');
  190. $kvpairs = array();
  191. foreach ($params as $k => $v) {
  192. if ($excludeOauthParams && substr($k, 0, 5) == 'oauth') {
  193. continue;
  194. }
  195. if (is_array($v)) {
  196. // If two or more parameters share the same name,
  197. // they are sorted by their value. OAuth Spec: 9.1.1 (1)
  198. natsort($v);
  199. foreach ($v as $value_for_same_key) {
  200. array_push($kvpairs, ($k . '=' . $value_for_same_key));
  201. }
  202. } else {
  203. // For each parameter, the name is separated from the corresponding
  204. // value by an '=' character (ASCII code 61). OAuth Spec: 9.1.1 (2)
  205. array_push($kvpairs, ($k . '=' . $v));
  206. }
  207. }
  208. // Each name-value pair is separated by an '&' character, ASCII code 38.
  209. // OAuth Spec: 9.1.1 (2)
  210. $query_string = implode('&', $kvpairs);
  211. }
  212. return $query_string;
  213. }
  214. function oauth_parse_str($query_string) {
  215. $query_array = array();
  216. if (isset($query_string)) {
  217. // Separate single string into an array of "key=value" strings
  218. $kvpairs = explode('&', $query_string);
  219. // Separate each "key=value" string into an array[key] = value
  220. foreach ($kvpairs as $pair) {
  221. list($k, $v) = explode('=', $pair, 2);
  222. // Handle the case where multiple values map to the same key
  223. // by pulling those values into an array themselves
  224. if (isset($query_array[$k])) {
  225. // If the existing value is a scalar, turn it into an array
  226. if (is_scalar($query_array[$k])) {
  227. $query_array[$k] = array($query_array[$k]);
  228. }
  229. array_push($query_array[$k], $v);
  230. } else {
  231. $query_array[$k] = $v;
  232. }
  233. }
  234. }
  235. return $query_array;
  236. }
  237. function build_oauth_header($params, $realm='') {
  238. $header = 'Authorization: OAuth';
  239. foreach ($params as $k => $v) {
  240. if (substr($k, 0, 5) == 'oauth') {
  241. $header .= ',' . $this->rfc3986_encode($k) . '="' . $this->rfc3986_encode($v) . '"';
  242. }
  243. }
  244. return $header;
  245. }
  246. function oauth_compute_plaintext_sig($consumer_secret, $token_secret) {
  247. return ($consumer_secret . '&' . $token_secret);
  248. }
  249. public function oauth_compute_hmac_sig($http_method, $url, $params, $consumer_secret, $token_secret) {
  250. $base_string = $this->signature_base_string($http_method, $url, $params);
  251. $signature_key = $this->rfc3986_encode($consumer_secret) . '&' . $this->rfc3986_encode($token_secret);
  252. $sig = base64_encode(hash_hmac('sha1', $base_string, $signature_key, true));
  253. if ($this->debug) {
  254. $this->logit("oauth_compute_hmac_sig:DBG:sig:$sig");
  255. }
  256. return $sig;
  257. }
  258. /**
  259. * Make the URL conform to the format scheme://host/path
  260. * @param string $url
  261. * @return string the url in the form of scheme://host/path
  262. */
  263. function normalize_url($url) {
  264. $parts = parse_url($url);
  265. $scheme = $parts['scheme'];
  266. $host = $parts['host'];
  267. $port = isset($parts['port']) ? $parts['port'] : false ;
  268. $path = $parts['path'];
  269. if (!$port) {
  270. $port = ($scheme == 'https') ? '443' : '80';
  271. }
  272. if (($scheme == 'https' && $port != '443')
  273. || ($scheme == 'http' && $port != '80')) {
  274. $host = "$host:$port";
  275. }
  276. return "$scheme://$host$path";
  277. }
  278. /**
  279. * Returns the normalized signature base string of this request
  280. * @param string $http_method
  281. * @param string $url
  282. * @param array $params
  283. * The base string is defined as the method, the url and the
  284. * parameters (normalized), each urlencoded and the concated with &.
  285. * @see http://oauth.net/core/1.0/#rfc.section.A.5.1
  286. */
  287. function signature_base_string($http_method, $url, $params) {
  288. // Decompose and pull query params out of the url
  289. $query_str = parse_url($url, PHP_URL_QUERY);
  290. if ($query_str) {
  291. $parsed_query = $this->oauth_parse_str($query_str);
  292. // merge params from the url with params array from caller
  293. $params = array_merge($params, $parsed_query);
  294. }
  295. // Remove oauth_signature from params array if present
  296. if (isset($params['oauth_signature'])) {
  297. unset($params['oauth_signature']);
  298. }
  299. // Create the signature base string. Yes, the $params are double encoded.
  300. $base_string = $this->rfc3986_encode(strtoupper($http_method)) . '&' .
  301. $this->rfc3986_encode($this->normalize_url($url)) . '&' .
  302. $this->rfc3986_encode($this->oauth_http_build_query($params));
  303. $this->logit("signature_base_string:INFO:normalized_base_string:$base_string");
  304. return $base_string;
  305. }
  306. /**
  307. * Encode input per RFC 3986
  308. * @param string|array $raw_input
  309. * @return string|array properly rfc3986 encoded raw_input
  310. * If an array is passed in, rfc3896 encode all elements of the array.
  311. * @link http://oauth.net/core/1.0/#encoding_parameters
  312. */
  313. function rfc3986_encode($raw_input){
  314. if (is_array($raw_input)) {
  315. //return array_map($this->rfc3986_encode, $raw_input);
  316. return array_map(array($this, 'rfc3986_encode'), $raw_input);
  317. // return $this->rfc3986_encode($raw_input);
  318. } else if (is_scalar($raw_input)) {
  319. return str_replace('%7E', '~', rawurlencode($raw_input));
  320. } else {
  321. return '';
  322. }
  323. }
  324. function rfc3986_decode($raw_input) {
  325. return rawurldecode($raw_input);
  326. }
  327. }
  328. ?>