base.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. /* Ad hoc functions to make the examples marginally prettier.*/
  3. function isWebRequest()
  4. {
  5. return isset($_SERVER['HTTP_USER_AGENT']);
  6. }
  7. function pageHeader($title)
  8. {
  9. $ret = "<!doctype html>
  10. <html>
  11. <head>
  12. <title>" . $title . "</title>
  13. <link href='css/style.css' rel='stylesheet' type='text/css' />
  14. </head>
  15. <body>\n";
  16. if ($_SERVER['PHP_SELF'] != "/index.php") {
  17. $ret .= "<p><a href='index.php'>Back</a></p>";
  18. }
  19. $ret .= "<header><h1>" . $title . "</h1></header>";
  20. // Start the session (for storing access tokens and things)
  21. if (!headers_sent()) {
  22. session_start();
  23. }
  24. return $ret;
  25. }
  26. function pageFooter($file = null)
  27. {
  28. $ret = "";
  29. if ($file) {
  30. $ret .= "<h3>Code:</h3>";
  31. $ret .= "<pre class='code'>";
  32. $ret .= htmlspecialchars(file_get_contents($file));
  33. $ret .= "</pre>";
  34. }
  35. $ret .= "</html>";
  36. return $ret;
  37. }
  38. function missingApiKeyWarning()
  39. {
  40. $ret = "
  41. <h3 class='warn'>
  42. Warning: You need to set a Simple API Access key from the
  43. <a href='http://developers.google.com/console'>Google API console</a>
  44. </h3>";
  45. return $ret;
  46. }
  47. function missingClientSecretsWarning()
  48. {
  49. $ret = "
  50. <h3 class='warn'>
  51. Warning: You need to set Client ID, Client Secret and Redirect URI from the
  52. <a href='http://developers.google.com/console'>Google API console</a>
  53. </h3>";
  54. return $ret;
  55. }
  56. function missingServiceAccountDetailsWarning()
  57. {
  58. $ret = "
  59. <h3 class='warn'>
  60. Warning: You need download your Service Account Credentials JSON from the
  61. <a href='http://developers.google.com/console'>Google API console</a>.
  62. </h3>
  63. <p>
  64. Once downloaded, move them into the root directory of this repository and
  65. rename them 'service-account-credentials.json'.
  66. </p>
  67. <p>
  68. In your application, you should set the GOOGLE_APPLICATION_CREDENTIALS environment variable
  69. as the path to this file, but in the context of this example we will do this for you.
  70. </p>";
  71. return $ret;
  72. }
  73. function missingOAuth2CredentialsWarning()
  74. {
  75. $ret = "
  76. <h3 class='warn'>
  77. Warning: You need to set the location of your OAuth2 Client Credentials from the
  78. <a href='http://developers.google.com/console'>Google API console</a>.
  79. </h3>
  80. <p>
  81. Once downloaded, move them into the root directory of this repository and
  82. rename them 'oauth-credentials.json'.
  83. </p>";
  84. return $ret;
  85. }
  86. function invalidCsrfTokenWarning()
  87. {
  88. $ret = "
  89. <h3 class='warn'>
  90. The CSRF token is invalid, your session probably expired. Please refresh the page.
  91. </h3>";
  92. return $ret;
  93. }
  94. function checkServiceAccountCredentialsFile()
  95. {
  96. // service account creds
  97. $application_creds = __DIR__ . '/service-account-credentials.json';
  98. return file_exists($application_creds) ? $application_creds : false;
  99. }
  100. function getCsrfToken()
  101. {
  102. if (!isset($_SESSION['csrf_token'])) {
  103. $_SESSION['csrf_token'] = bin2hex(openssl_random_pseudo_bytes(32));
  104. }
  105. return $_SESSION['csrf_token'];
  106. }
  107. function validateCsrfToken()
  108. {
  109. return isset($_REQUEST['csrf_token'])
  110. && isset($_SESSION['csrf_token'])
  111. && $_REQUEST['csrf_token'] === $_SESSION['csrf_token'];
  112. }
  113. function getOAuthCredentialsFile()
  114. {
  115. // oauth2 creds
  116. $oauth_creds = __DIR__ . '/oauth-credentials.json';
  117. if (file_exists($oauth_creds)) {
  118. return $oauth_creds;
  119. }
  120. return false;
  121. }
  122. function setClientCredentialsFile($apiKey)
  123. {
  124. $file = __DIR__ . '/.apiKey';
  125. file_put_contents($file, $apiKey);
  126. }
  127. function getApiKey()
  128. {
  129. $file = __DIR__ . '/.apiKey';
  130. if (file_exists($file)) {
  131. return file_get_contents($file);
  132. }
  133. }
  134. function setApiKey($apiKey)
  135. {
  136. $file = __DIR__ . '/.apiKey';
  137. file_put_contents($file, $apiKey);
  138. }