PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/proxyRequest.php

https://github.com/kmlawson/libZotero
PHP | 143 lines | 97 code | 23 blank | 23 comment | 18 complexity | 2c05167c5d409f01063c1c605b6b5389 MD5 | raw file
  1. <?php
  2. require_once './config.php'; //library credentials
  3. require_once './build/libZoteroSingle.php';
  4. $library = new Zotero_Library($libraryType, $libraryID, $librarySlug, $apiKey);
  5. $requestUrl = isset($_GET["requestUrl"]) ? $_GET["requestUrl"] : false;
  6. $requestQueryParams = isset($_GET['requestQueryParams']) ? $_GET['requestQueryParams'] : false;
  7. //limit requestUrl to zotero.org
  8. if(strpos($requestUrl, 'zotero.org') === false){
  9. die;
  10. }
  11. //act as transparent proxy until JS lib can make requests directly to api
  12. $requestMethod = $_SERVER['REQUEST_METHOD'];
  13. //raw body of the request
  14. $rawbody = @file_get_contents('php://input');
  15. $passedIfMatch = getHeader('If-Match');
  16. $headers = array();
  17. if($passedIfMatch){
  18. $headers['If-Match'] = $passedIfMatch;
  19. }
  20. $response = $library->proxyHttpRequest($requestUrl, strtoupper($requestMethod), $rawbody, $headers);
  21. //take the http response we got and send all of it along
  22. $httpHeaders = $response->getHeaders();
  23. $statusSent = false;
  24. foreach($httpHeaders as $key=>$val){
  25. $headerText = $key . ': ' . $val;
  26. if(!$statusSent){
  27. header($headerText, true, $response->getStatus());
  28. $statusSent = true;
  29. }
  30. else{
  31. header($headerText);
  32. }
  33. }
  34. echo $response->getBody();
  35. //getHeader function from Zend_Controller_Request_Http
  36. function getHeader($header){
  37. // Try to get it from the $_SERVER array first
  38. $temp = 'HTTP_' . strtoupper(str_replace('-', '_', $header));
  39. if (isset($_SERVER[$temp])) {
  40. return $_SERVER[$temp];
  41. }
  42. // This seems to be the only way to get the Authorization header on
  43. // Apache
  44. if (function_exists('apache_request_headers')) {
  45. $headers = apache_request_headers();
  46. if (isset($headers[$header])) {
  47. return $headers[$header];
  48. }
  49. $header = strtolower($header);
  50. foreach ($headers as $key => $value) {
  51. if (strtolower($key) == $header) {
  52. return $value;
  53. }
  54. }
  55. }
  56. return false;
  57. }
  58. function parsePathVars($basePath, $pathname = null) {
  59. //parse variables out of library urls
  60. //:userslug/items/:itemKey/*
  61. //:userslug/items/collection/:collectionKey
  62. //groups/:groupidentifier/items/:itemKey/*
  63. //groups/:groupidentifier/items/collection/:collectionKey/*
  64. if(!$pathname){
  65. $pathname = $this->getRequest()->getRequestUri();
  66. }
  67. $replaced = trim(str_replace($basePath, '', $pathname), '/');
  68. $split_replaced = explode('/', $replaced);
  69. $pathVars = array();
  70. if(count($split_replaced) == 1) return $pathVars;
  71. for($i = 0; $i < count($split_replaced); $i = $i + 2){
  72. $pathVar = isset($pathVars[$split_replaced[$i]]) ? $pathVars[$split_replaced[$i]] : null;
  73. //if var already present change to array and/or push
  74. if($pathVar){
  75. if(is_array($pathVar)){
  76. array_push($pathVar, $split_replaced[$i+1]);
  77. }
  78. else{
  79. $ar = array($pathVar);
  80. array_push($ar, $split_replaced[$i+1]);
  81. $pathVar = $ar;
  82. }
  83. }
  84. //otherwise just set the value in the object
  85. else{
  86. $pathVar = $split_replaced[$i+1];
  87. }
  88. $pathVars[$split_replaced[$i]] = $pathVar;
  89. }
  90. return $pathVars;
  91. }
  92. function proper_parse_str($str) {
  93. # result array
  94. $arr = array();
  95. # split on outer delimiter
  96. $pairs = explode('&', $str);
  97. # loop through each pair
  98. foreach ($pairs as $i) {
  99. # split into name and value
  100. list($name,$value) = explode('=', $i, 2);
  101. $name = urldecode($name);
  102. $value = urldecode($value);
  103. # if name already exists
  104. if( isset($arr[$name]) ) {
  105. # stick multiple values into an array
  106. if( is_array($arr[$name]) ) {
  107. $arr[$name][] = $value;
  108. }
  109. else {
  110. $arr[$name] = array($arr[$name], $value);
  111. }
  112. }
  113. # otherwise, simply stick it in a scalar
  114. else {
  115. $arr[$name] = $value;
  116. }
  117. }
  118. # return result array
  119. return $arr;
  120. }