common.inc.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. /* Copyright (c) 2009 Google Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. * Author: Eric Bidelman <e.bidelman@google.com>
  17. */
  18. $PRIV_KEY_FILE = '/path/to/your/rsa_private_key.pem';
  19. // OAuth library - http://oauth.googlecode.com/svn/code/php/
  20. require_once('OAuth.php');
  21. // Google's accepted signature methods
  22. $hmac_method = new OAuthSignatureMethod_HMAC_SHA1();
  23. $rsa_method = new OAuthSignatureMethod_RSA_SHA1();
  24. $SIG_METHODS = array($rsa_method->get_name() => $rsa_method,
  25. $hmac_method->get_name() => $hmac_method);
  26. /**
  27. * Makes an HTTP request to the specified URL
  28. *
  29. * @param string $http_method The HTTP method (GET, POST, PUT, DELETE)
  30. * @param string $url Full URL of the resource to access
  31. * @param array $extraHeaders (optional) Additional headers to include in each
  32. * request. Elements are header/value pair strings ('Host: example.com')
  33. * @param string $postData (optional) POST/PUT request body
  34. * @param bool $returnResponseHeaders True if resp. headers should be returned.
  35. * @return string Response body from the server
  36. */
  37. function send_signed_request($http_method, $url, $extraHeaders=null,
  38. $postData=null, $returnResponseHeaders=true) {
  39. $curl = curl_init($url);
  40. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  41. curl_setopt($curl, CURLOPT_FAILONERROR, false);
  42. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  43. // Return request headers in the reponse
  44. // curl_setopt($curl, CURLINFO_HEADER_OUT, true);
  45. // Return response headers ni the response?
  46. if ($returnResponseHeaders) {
  47. curl_setopt($curl, CURLOPT_HEADER, true);
  48. }
  49. $headers = array();
  50. //$headers[] = 'GData-Version: 2.0'; // use GData v2 by default
  51. if (is_array($extraHeaders)) {
  52. $headers = array_merge($headers, $extraHeaders);
  53. }
  54. // Setup default curl options for each type of HTTP request.
  55. // This is also a great place to add additional headers for each request.
  56. switch($http_method) {
  57. case 'GET':
  58. curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
  59. break;
  60. case 'POST':
  61. curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
  62. curl_setopt($curl, CURLOPT_POST, 1);
  63. curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
  64. break;
  65. case 'PUT':
  66. $headers[] = 'If-Match: *';
  67. curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
  68. curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $http_method);
  69. curl_setopt($curl, CURLOPT_POSTFIELDS, $postData);
  70. break;
  71. case 'DELETE':
  72. $headers[] = 'If-Match: *';
  73. curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
  74. curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $http_method);
  75. break;
  76. default:
  77. curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
  78. }
  79. // Execute the request. If an error occures, fill the response body with it.
  80. $response = curl_exec($curl);
  81. if (!$response) {
  82. $response = curl_error($curl);
  83. }
  84. // Add server's response headers to our response body
  85. $response = curl_getinfo($curl, CURLINFO_HEADER_OUT) . $response;
  86. curl_close($curl);
  87. return $response;
  88. }
  89. /**
  90. * Takes XML as a string and returns it nicely indented
  91. *
  92. * @param string $xml The xml to beautify
  93. * @param boolean $html_output True if returned XML should be escaped for HTML.
  94. * @return string The beautified xml
  95. */
  96. function xml_pretty_printer($xml, $html_output=false) {
  97. $xml_obj = new SimpleXMLElement($xml);
  98. $level = 2;
  99. // Get an array containing each XML element
  100. $xml = explode("\n", preg_replace('/>\s*</', ">\n<", $xml_obj->asXML()));
  101. // Hold current indentation level
  102. $indent = 0;
  103. $pretty = array();
  104. // Shift off opening XML tag if present
  105. if (count($xml) && preg_match('/^<\?\s*xml/', $xml[0])) {
  106. $pretty[] = array_shift($xml);
  107. }
  108. foreach ($xml as $el) {
  109. if (preg_match('/^<([\w])+[^>\/]*>$/U', $el)) {
  110. // opening tag, increase indent
  111. $pretty[] = str_repeat(' ', $indent) . $el;
  112. $indent += $level;
  113. } else {
  114. if (preg_match('/^<\/.+>$/', $el)) {
  115. $indent -= $level; // closing tag, decrease indent
  116. }
  117. if ($indent < 0) {
  118. $indent += $level;
  119. }
  120. $pretty[] = str_repeat(' ', $indent) . $el;
  121. }
  122. }
  123. $xml = implode("\n", $pretty);
  124. return $html_output ? htmlentities($xml) : $xml;
  125. }
  126. /**
  127. * Joins key/value pairs by $inner_glue and each pair together by $outer_glue.
  128. *
  129. * Example: implode_assoc('=', '&', array('a' => 1, 'b' => 2)) === 'a=1&b=2'
  130. *
  131. * @param string $inner_glue What to implode each key/value pair with
  132. * @param string $outer_glue What to impode each key/value string subset with
  133. * @param array $array Associative array of query parameters
  134. * @return string Urlencoded string of query parameters
  135. */
  136. function implode_assoc($inner_glue, $outer_glue, $array) {
  137. $output = array();
  138. foreach($array as $key => $item) {
  139. $output[] = $key . $inner_glue . urlencode($item);
  140. }
  141. return implode($outer_glue, $output);
  142. }
  143. /**
  144. * Explodes a string of key/value url parameters into an associative array.
  145. * This method performs the compliment operations of implode_assoc().
  146. *
  147. * Example: explode_assoc('=', '&', 'a=1&b=2') === array('a' => 1, 'b' => 2)
  148. *
  149. * @param string $inner_glue What each key/value pair is joined with
  150. * @param string $outer_glue What each set of key/value pairs is joined with.
  151. * @param array $array Associative array of query parameters
  152. * @return array Urlencoded string of query parameters
  153. */
  154. function explode_assoc($inner_glue, $outer_glue, $params) {
  155. $tempArr = explode($outer_glue, $params);
  156. foreach($tempArr as $val) {
  157. $pos = strpos($val, $inner_glue);
  158. $key = substr($val, 0, $pos);
  159. $array2[$key] = substr($val, $pos + 1, strlen($val));
  160. }
  161. return $array2;
  162. }
  163. ?>