contact_auth.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. /*
  3. FusionPBX
  4. Version: MPL 1.1
  5. The contents of this file are subject to the Mozilla Public License Version
  6. 1.1 (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.mozilla.org/MPL/
  9. Software distributed under the License is distributed on an "AS IS" basis,
  10. WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11. for the specific language governing rights and limitations under the
  12. License.
  13. The Original Code is FusionPBX
  14. The Initial Developer of the Original Code is
  15. Mark J Crane <[email protected]>
  16. Portions created by the Initial Developer are Copyright (C) 2008-2013
  17. the Initial Developer. All Rights Reserved.
  18. Contributor(s):
  19. Mark J Crane <[email protected]>
  20. */
  21. require_once "root.php";
  22. require_once "resources/require.php";
  23. require_once "resources/check_auth.php";
  24. if (permission_exists('contact_add')) {
  25. //access granted
  26. }
  27. else {
  28. echo "access denied";
  29. exit;
  30. }
  31. /*
  32. echo "bang!";
  33. exit;
  34. */
  35. //add multi-lingual support
  36. $language = new text;
  37. $text = $language->get();
  38. $_SESSION['contact_auth']['source'] = ($_SESSION['contact_auth']['source'] == '') ? $_REQUEST['source'] : $_SESSION['contact_auth']['source'];
  39. $_SESSION['contact_auth']['target'] = ($_SESSION['contact_auth']['target'] == '') ? $_REQUEST['target'] : $_SESSION['contact_auth']['target'];
  40. //google api authentication
  41. if ($_SESSION['contact_auth']['source'] == 'google') {
  42. if ($_REQUEST['error']) {
  43. message::add(($text['message-'.$_REQUEST['error']] != '') ? $text['message-'.$_REQUEST['error']] : $_REQUEST['error'], 'negative');
  44. header("Location: ".$_SESSION['contact_auth']['referer']);
  45. unset($_SESSION['contact_auth']);
  46. exit;
  47. }
  48. if (isset($_REQUEST['signout'])) {
  49. unset($_SESSION['contact_auth']['token']);
  50. message::add($text['message-google_signed_out']);
  51. header("Location: https://www.google.com/accounts/Logout?continue=https://appengine.google.com/_ah/logout?continue=".(($_SERVER["HTTPS"] == "on") ? "https" : "http")."://".$_SERVER['HTTP_HOST'].PROJECT_PATH."/app/contacts/".$_SESSION['contact_auth']['referer']);
  52. exit;
  53. }
  54. if ($_GET['code'] == '') {
  55. header("Location: https://accounts.google.com/o/oauth2/auth?client_id=".$_SESSION['contact']['google_oauth_client_id']['text']."&redirect_uri=".(($_SERVER["HTTPS"] == "on") ? "https" : "http")."://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']."&scope=https://www.google.com/m8/feeds/&response_type=code");
  56. exit;
  57. }
  58. else {
  59. $auth_code = $_GET["code"];
  60. }
  61. /*******************************************************************************************/
  62. // request access token
  63. $fields = array(
  64. 'code' => urlencode($auth_code),
  65. 'client_id' => urlencode($_SESSION['contact']['google_oauth_client_id']['text']),
  66. 'client_secret' => urlencode($_SESSION['contact']['google_oauth_client_secret']['text']),
  67. 'redirect_uri' => urlencode((($_SERVER["HTTPS"] == "on") ? "https" : "http")."://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']),
  68. 'grant_type' => urlencode('authorization_code')
  69. );
  70. foreach($fields as $key => $value) {
  71. $post_fields[] = $key.'='.$value;
  72. }
  73. $post_fields = implode("&", $post_fields);
  74. $curl = curl_init();
  75. curl_setopt($curl, CURLOPT_URL, 'https://accounts.google.com/o/oauth2/token');
  76. curl_setopt($curl, CURLOPT_POST, 5);
  77. curl_setopt($curl, CURLOPT_POSTFIELDS, $post_fields);
  78. curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
  79. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
  80. $result = curl_exec($curl);
  81. curl_close($curl);
  82. $response = json_decode($result);
  83. $access_token = $response->access_token;
  84. if ($access_token != '') {
  85. // redirect to target script
  86. $_SESSION['contact_auth']['token'] = $access_token;
  87. header("Location: ".$_SESSION['contact_auth']['target']);
  88. exit;
  89. }
  90. }
  91. else {
  92. message::add($text['message-access_denied'], 'negative');
  93. header("Location: ".$_SESSION['contact_auth']['referer']);
  94. unset($_SESSION['contact_auth']);
  95. exit;
  96. }
  97. ?>