PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/examples/templates/base.php

https://gitlab.com/crsr/google-api-php-client
PHP | 141 lines | 113 code | 24 blank | 4 comment | 7 complexity | 5384b1f1ee4839d4377ff9f8a3d9345d MD5 | raw file
  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='styles/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 checkServiceAccountCredentialsFile()
  87. {
  88. // service account creds
  89. $application_creds = __DIR__ . '/../../service-account-credentials.json';
  90. return file_exists($application_creds) ? $application_creds : false;
  91. }
  92. function getOAuthCredentialsFile()
  93. {
  94. // oauth2 creds
  95. $oauth_creds = __DIR__ . '/../../oauth-credentials.json';
  96. if (file_exists($oauth_creds)) {
  97. return $oauth_creds;
  98. }
  99. return false;
  100. }
  101. function setClientCredentialsFile($apiKey)
  102. {
  103. $file = __DIR__ . '/../../tests/.apiKey';
  104. file_put_contents($file, $apiKey);
  105. }
  106. function getApiKey()
  107. {
  108. $file = __DIR__ . '/../../tests/.apiKey';
  109. if (file_exists($file)) {
  110. return file_get_contents($file);
  111. }
  112. }
  113. function setApiKey($apiKey)
  114. {
  115. $file = __DIR__ . '/../../tests/.apiKey';
  116. file_put_contents($file, $apiKey);
  117. }