@@ -12,63 +12,63 @@ |
||
| 12 | 12 | use Requests; |
| 13 | 13 | |
| 14 | 14 | class OAuth { |
| 15 | - /* |
|
| 15 | + /* |
|
| 16 | 16 | * token life time in second |
| 17 | 17 | * */ |
| 18 | - public $tokenLifeTime; |
|
| 19 | - public $key; |
|
| 20 | - public $iss; |
|
| 18 | + public $tokenLifeTime; |
|
| 19 | + public $key; |
|
| 20 | + public $iss; |
|
| 21 | 21 | |
| 22 | - protected $expireTimestamp; |
|
| 23 | - protected $accessToken; |
|
| 22 | + protected $expireTimestamp; |
|
| 23 | + protected $accessToken; |
|
| 24 | 24 | |
| 25 | - /** |
|
| 26 | - * OAuth constructor. |
|
| 27 | - * |
|
| 28 | - * @param $key |
|
| 29 | - * @param $iss |
|
| 30 | - */ |
|
| 31 | - public function __construct( $key, $iss, $tokenLifeTime = 3600 ) { |
|
| 32 | - $this->key = $key; |
|
| 33 | - $this->iss = $iss; |
|
| 34 | - $this->tokenLifeTime = $tokenLifeTime; |
|
| 35 | - } |
|
| 25 | + /** |
|
| 26 | + * OAuth constructor. |
|
| 27 | + * |
|
| 28 | + * @param $key |
|
| 29 | + * @param $iss |
|
| 30 | + */ |
|
| 31 | + public function __construct( $key, $iss, $tokenLifeTime = 3600 ) { |
|
| 32 | + $this->key = $key; |
|
| 33 | + $this->iss = $iss; |
|
| 34 | + $this->tokenLifeTime = $tokenLifeTime; |
|
| 35 | + } |
|
| 36 | 36 | |
| 37 | - protected function requestAccessToken() { |
|
| 38 | - $currentTimestamp = time(); |
|
| 39 | - $this->expireTimestamp = $currentTimestamp + $this->tokenLifeTime; |
|
| 40 | - $jsonToken = array( |
|
| 41 | - "iss" => $this->iss, |
|
| 42 | - "scope" => "https://www.googleapis.com/auth/firebase.database https://www.googleapis.com/auth/userinfo.email", |
|
| 43 | - "aud" => "https://www.googleapis.com/oauth2/v4/token", |
|
| 44 | - "exp" => $this->expireTimestamp, |
|
| 45 | - "iat" => $currentTimestamp |
|
| 46 | - ); |
|
| 47 | - $jwt = JWT::encode( $jsonToken, $this->key, 'RS256' ); |
|
| 37 | + protected function requestAccessToken() { |
|
| 38 | + $currentTimestamp = time(); |
|
| 39 | + $this->expireTimestamp = $currentTimestamp + $this->tokenLifeTime; |
|
| 40 | + $jsonToken = array( |
|
| 41 | + "iss" => $this->iss, |
|
| 42 | + "scope" => "https://www.googleapis.com/auth/firebase.database https://www.googleapis.com/auth/userinfo.email", |
|
| 43 | + "aud" => "https://www.googleapis.com/oauth2/v4/token", |
|
| 44 | + "exp" => $this->expireTimestamp, |
|
| 45 | + "iat" => $currentTimestamp |
|
| 46 | + ); |
|
| 47 | + $jwt = JWT::encode( $jsonToken, $this->key, 'RS256' ); |
|
| 48 | 48 | |
| 49 | - $OAuthResponse = Requests::post( 'https://www.googleapis.com/oauth2/v4/token', array(), array( |
|
| 50 | - 'grant_type' => 'urn:ietf:params:oauth:grant-type:jwt-bearer', |
|
| 51 | - 'assertion' => $jwt |
|
| 52 | - ) ); |
|
| 49 | + $OAuthResponse = Requests::post( 'https://www.googleapis.com/oauth2/v4/token', array(), array( |
|
| 50 | + 'grant_type' => 'urn:ietf:params:oauth:grant-type:jwt-bearer', |
|
| 51 | + 'assertion' => $jwt |
|
| 52 | + ) ); |
|
| 53 | 53 | |
| 54 | - if ( $OAuthResponse->status_code == 200 ) { |
|
| 55 | - $this->accessToken = json_decode( $OAuthResponse->body )->access_token; |
|
| 54 | + if ( $OAuthResponse->status_code == 200 ) { |
|
| 55 | + $this->accessToken = json_decode( $OAuthResponse->body )->access_token; |
|
| 56 | 56 | |
| 57 | - return true; |
|
| 58 | - } |
|
| 57 | + return true; |
|
| 58 | + } |
|
| 59 | 59 | |
| 60 | - return false; |
|
| 61 | - } |
|
| 60 | + return false; |
|
| 61 | + } |
|
| 62 | 62 | |
| 63 | - public function getAccessToken() { |
|
| 64 | - $currentTime = time(); |
|
| 65 | - if ( $this->expireTimestamp < $currentTime ) { |
|
| 66 | - $startTime = time(); |
|
| 67 | - $this->requestAccessToken(); |
|
| 68 | - $endTime = time(); |
|
| 69 | - $this->expireTimestamp -= ( $endTime - $startTime ); |
|
| 70 | - } |
|
| 63 | + public function getAccessToken() { |
|
| 64 | + $currentTime = time(); |
|
| 65 | + if ( $this->expireTimestamp < $currentTime ) { |
|
| 66 | + $startTime = time(); |
|
| 67 | + $this->requestAccessToken(); |
|
| 68 | + $endTime = time(); |
|
| 69 | + $this->expireTimestamp -= ( $endTime - $startTime ); |
|
| 70 | + } |
|
| 71 | 71 | |
| 72 | - return $this->accessToken; |
|
| 73 | - } |
|
| 72 | + return $this->accessToken; |
|
| 73 | + } |
|
| 74 | 74 | } |
| 75 | 75 | \ No newline at end of file |
@@ -28,7 +28,7 @@ discard block |
||
| 28 | 28 | * @param $key |
| 29 | 29 | * @param $iss |
| 30 | 30 | */ |
| 31 | - public function __construct( $key, $iss, $tokenLifeTime = 3600 ) { |
|
| 31 | + public function __construct($key, $iss, $tokenLifeTime = 3600) { |
|
| 32 | 32 | $this->key = $key; |
| 33 | 33 | $this->iss = $iss; |
| 34 | 34 | $this->tokenLifeTime = $tokenLifeTime; |
@@ -44,15 +44,15 @@ discard block |
||
| 44 | 44 | "exp" => $this->expireTimestamp, |
| 45 | 45 | "iat" => $currentTimestamp |
| 46 | 46 | ); |
| 47 | - $jwt = JWT::encode( $jsonToken, $this->key, 'RS256' ); |
|
| 47 | + $jwt = JWT::encode($jsonToken, $this->key, 'RS256'); |
|
| 48 | 48 | |
| 49 | - $OAuthResponse = Requests::post( 'https://www.googleapis.com/oauth2/v4/token', array(), array( |
|
| 49 | + $OAuthResponse = Requests::post('https://www.googleapis.com/oauth2/v4/token', array(), array( |
|
| 50 | 50 | 'grant_type' => 'urn:ietf:params:oauth:grant-type:jwt-bearer', |
| 51 | 51 | 'assertion' => $jwt |
| 52 | - ) ); |
|
| 52 | + )); |
|
| 53 | 53 | |
| 54 | - if ( $OAuthResponse->status_code == 200 ) { |
|
| 55 | - $this->accessToken = json_decode( $OAuthResponse->body )->access_token; |
|
| 54 | + if ($OAuthResponse->status_code == 200) { |
|
| 55 | + $this->accessToken = json_decode($OAuthResponse->body)->access_token; |
|
| 56 | 56 | |
| 57 | 57 | return true; |
| 58 | 58 | } |
@@ -62,11 +62,11 @@ discard block |
||
| 62 | 62 | |
| 63 | 63 | public function getAccessToken() { |
| 64 | 64 | $currentTime = time(); |
| 65 | - if ( $this->expireTimestamp < $currentTime ) { |
|
| 65 | + if ($this->expireTimestamp < $currentTime) { |
|
| 66 | 66 | $startTime = time(); |
| 67 | 67 | $this->requestAccessToken(); |
| 68 | 68 | $endTime = time(); |
| 69 | - $this->expireTimestamp -= ( $endTime - $startTime ); |
|
| 69 | + $this->expireTimestamp -= ($endTime - $startTime); |
|
| 70 | 70 | } |
| 71 | 71 | |
| 72 | 72 | return $this->accessToken; |
@@ -10,33 +10,33 @@ |
||
| 10 | 10 | |
| 11 | 11 | |
| 12 | 12 | class JCFirebaseOption { |
| 13 | - const __default = null; |
|
| 14 | - |
|
| 15 | - const OPTION_SHALLOW = 'shallow'; |
|
| 16 | - const SHALLOW_TRUE = 'true'; |
|
| 17 | - const SHALLOW_FALSE = 'false'; |
|
| 18 | - |
|
| 19 | - const OPTION_PRINT = 'print'; |
|
| 20 | - const PRINT_PRETTY = 'pretty'; |
|
| 21 | - const PRINT_SILENT = 'silent'; |
|
| 22 | - |
|
| 23 | - const REQ_TYPE_GET = 0; |
|
| 24 | - const REQ_TYPE_PUT = 1; |
|
| 25 | - const REQ_TYPE_POST = 2; |
|
| 26 | - const REQ_TYPE_PATCH = 3; |
|
| 27 | - const REQ_TYPE_DELETE = 4; |
|
| 28 | - |
|
| 29 | - public static function isAllowPrint( $reqType = self::REQ_TYPE_GET, $pType = self::PRINT_PRETTY ) { |
|
| 30 | - if ( $pType == self::PRINT_PRETTY ) { |
|
| 31 | - return true; |
|
| 32 | - } |
|
| 33 | - |
|
| 34 | - if ( $pType == self::PRINT_SILENT ) { |
|
| 35 | - if ( $reqType == self::REQ_TYPE_DELETE ) { |
|
| 36 | - return false; |
|
| 37 | - } |
|
| 38 | - } |
|
| 39 | - |
|
| 40 | - return true; |
|
| 41 | - } |
|
| 13 | + const __default = null; |
|
| 14 | + |
|
| 15 | + const OPTION_SHALLOW = 'shallow'; |
|
| 16 | + const SHALLOW_TRUE = 'true'; |
|
| 17 | + const SHALLOW_FALSE = 'false'; |
|
| 18 | + |
|
| 19 | + const OPTION_PRINT = 'print'; |
|
| 20 | + const PRINT_PRETTY = 'pretty'; |
|
| 21 | + const PRINT_SILENT = 'silent'; |
|
| 22 | + |
|
| 23 | + const REQ_TYPE_GET = 0; |
|
| 24 | + const REQ_TYPE_PUT = 1; |
|
| 25 | + const REQ_TYPE_POST = 2; |
|
| 26 | + const REQ_TYPE_PATCH = 3; |
|
| 27 | + const REQ_TYPE_DELETE = 4; |
|
| 28 | + |
|
| 29 | + public static function isAllowPrint( $reqType = self::REQ_TYPE_GET, $pType = self::PRINT_PRETTY ) { |
|
| 30 | + if ( $pType == self::PRINT_PRETTY ) { |
|
| 31 | + return true; |
|
| 32 | + } |
|
| 33 | + |
|
| 34 | + if ( $pType == self::PRINT_SILENT ) { |
|
| 35 | + if ( $reqType == self::REQ_TYPE_DELETE ) { |
|
| 36 | + return false; |
|
| 37 | + } |
|
| 38 | + } |
|
| 39 | + |
|
| 40 | + return true; |
|
| 41 | + } |
|
| 42 | 42 | } |
| 43 | 43 | \ No newline at end of file |
@@ -26,13 +26,13 @@ |
||
| 26 | 26 | const REQ_TYPE_PATCH = 3; |
| 27 | 27 | const REQ_TYPE_DELETE = 4; |
| 28 | 28 | |
| 29 | - public static function isAllowPrint( $reqType = self::REQ_TYPE_GET, $pType = self::PRINT_PRETTY ) { |
|
| 30 | - if ( $pType == self::PRINT_PRETTY ) { |
|
| 29 | + public static function isAllowPrint($reqType = self::REQ_TYPE_GET, $pType = self::PRINT_PRETTY) { |
|
| 30 | + if ($pType == self::PRINT_PRETTY) { |
|
| 31 | 31 | return true; |
| 32 | 32 | } |
| 33 | 33 | |
| 34 | - if ( $pType == self::PRINT_SILENT ) { |
|
| 35 | - if ( $reqType == self::REQ_TYPE_DELETE ) { |
|
| 34 | + if ($pType == self::PRINT_SILENT) { |
|
| 35 | + if ($reqType == self::REQ_TYPE_DELETE) { |
|
| 36 | 36 | return false; |
| 37 | 37 | } |
| 38 | 38 | } |
@@ -16,198 +16,198 @@ |
||
| 16 | 16 | * reference https://www.firebase.com/docs/rest/api/ |
| 17 | 17 | */ |
| 18 | 18 | class JCFirebase { |
| 19 | - public $firebaseURI; |
|
| 20 | - public $firebaseDefaultPath; |
|
| 21 | - public $requestHeader = array( |
|
| 22 | - 'accept' => 'application/json', |
|
| 23 | - 'contentType' => 'application/json; charset=utf-8', |
|
| 24 | - 'dataType' => 'json' |
|
| 25 | - ); |
|
| 26 | - public $requestOptions = array(); |
|
| 27 | - /** |
|
| 28 | - * @var OAuth |
|
| 29 | - */ |
|
| 30 | - protected $auth; |
|
| 31 | - |
|
| 32 | - |
|
| 33 | - /** |
|
| 34 | - * JCFirebase constructor. |
|
| 35 | - * |
|
| 36 | - * @param $firebaseURI |
|
| 37 | - * @param array $firebaseAuth |
|
| 38 | - * @param string $firebaseDefaultPath |
|
| 39 | - */ |
|
| 40 | - public function __construct( $firebaseURI, $firebaseAuth = array(), $firebaseDefaultPath = '/' ) { |
|
| 41 | - $this->firebaseURI = $firebaseURI; |
|
| 42 | - $this->firebaseDefaultPath = $firebaseDefaultPath; |
|
| 43 | - $this->setAuth( $firebaseAuth ); |
|
| 44 | - } |
|
| 45 | - |
|
| 46 | - /** |
|
| 47 | - * @param $firebaseURI |
|
| 48 | - * @param $keyFile |
|
| 49 | - * @param string $firebaseDefaultPath |
|
| 50 | - * |
|
| 51 | - * @return JCFirebase |
|
| 52 | - * @throws \Exception |
|
| 53 | - */ |
|
| 54 | - public static function fromKeyFile( $firebaseURI, $keyFile, $firebaseDefaultPath = '/' ) { |
|
| 55 | - $keyData = null; |
|
| 56 | - try { |
|
| 57 | - $keyData = json_decode( file_get_contents( $keyFile ) ); |
|
| 58 | - } catch ( \Exception $exception ) { |
|
| 59 | - $keyData = json_decode( Requests::get( $keyFile ) ); |
|
| 60 | - } |
|
| 61 | - |
|
| 62 | - if ( $keyData ) { |
|
| 63 | - $serviceAccount = $keyData->client_email; |
|
| 64 | - $privateKey = $keyData->private_key; |
|
| 65 | - |
|
| 66 | - return new self( $firebaseURI, array( |
|
| 67 | - 'key' => $privateKey, |
|
| 68 | - 'iss' => $serviceAccount |
|
| 69 | - ), $firebaseDefaultPath ); |
|
| 70 | - } else { |
|
| 71 | - throw new \Exception( "can't get data from key file" ); |
|
| 72 | - } |
|
| 73 | - } |
|
| 74 | - |
|
| 75 | - public function setAuth( $firebaseServiceAccount ) { |
|
| 76 | - if ( isset( $firebaseServiceAccount['key'] ) && isset( $firebaseServiceAccount['iss'] ) ) { |
|
| 77 | - $this->auth = new OAuth( $firebaseServiceAccount['key'], $firebaseServiceAccount['iss'] ); |
|
| 78 | - } |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - public function getPathURI( $path = '', $print = '' ) { |
|
| 82 | - //remove last slash from firebaseURI |
|
| 83 | - $template = '/'; |
|
| 84 | - $this->firebaseURI = rtrim( $this->firebaseURI, $template ); |
|
| 85 | - $path = rtrim( $path, $template ); |
|
| 86 | - $path = ltrim( $path, $template ); |
|
| 87 | - |
|
| 88 | - //check https |
|
| 89 | - if ( strpos( $this->firebaseURI, 'http://' ) !== false ) { |
|
| 90 | - throw new \Exception( "https is required." ); |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - //check firebaseURI |
|
| 94 | - if ( empty( $this->firebaseURI ) ) { |
|
| 95 | - throw new \Exception( "firebase URI is required" ); |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - if ( strpos( $this->firebaseDefaultPath, "/" ) !== 0 ) { |
|
| 99 | - throw new \Exception( "firebase default path must contain /" ); |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - $pathURI = $this->firebaseURI . $this->firebaseDefaultPath . $path . ".json"; |
|
| 103 | - |
|
| 104 | - //set query data |
|
| 105 | - $queryData = array(); |
|
| 106 | - if ( ! empty( $print ) ) { |
|
| 107 | - $queryData[ JCFirebaseOption::OPTION_PRINT ] = $print; |
|
| 108 | - } |
|
| 109 | - if ( ! empty( $queryData ) ) { |
|
| 110 | - $pathURI = $pathURI . '?' . http_build_query( $queryData ); |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - $this->refreshToken(); |
|
| 114 | - |
|
| 115 | - return $pathURI; |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - public function getShallow( $path = '', $options = array() ) { |
|
| 119 | - return Requests::get( |
|
| 120 | - $this->getPathURI( $path ) . '?' . http_build_query( array( |
|
| 121 | - JCFirebaseOption::OPTION_SHALLOW => JCFirebaseOption::SHALLOW_TRUE |
|
| 122 | - ) ), |
|
| 123 | - $this->requestHeader, |
|
| 124 | - $this->addDataToRequest( $options ) |
|
| 125 | - ); |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - /** |
|
| 129 | - * @param string $path |
|
| 130 | - * @param array $options |
|
| 131 | - * |
|
| 132 | - * @return \Requests_Response |
|
| 133 | - */ |
|
| 134 | - public function get( $path = '', $options = array() ) { |
|
| 135 | - return Requests::get( |
|
| 136 | - $this->addDataToPathURI( $path, $options ), $this->requestHeader, |
|
| 137 | - $this->addDataToRequest( $options ) |
|
| 138 | - ); |
|
| 139 | - } |
|
| 140 | - |
|
| 141 | - /** |
|
| 142 | - * @param string $path |
|
| 143 | - * @param array $options |
|
| 144 | - * |
|
| 145 | - * @return \Requests_Response |
|
| 146 | - */ |
|
| 147 | - public function put( $path = '', $options = array() ) { |
|
| 148 | - return Requests::put( $this->getPathURI( $path ), $this->requestHeader, |
|
| 149 | - $this->addDataToRequest( $options, true ) ); |
|
| 150 | - } |
|
| 151 | - |
|
| 152 | - /** |
|
| 153 | - * @param string $path |
|
| 154 | - * @param array $options |
|
| 155 | - * |
|
| 156 | - * @return \Requests_Response |
|
| 157 | - */ |
|
| 158 | - public function post( $path = '', $options = array() ) { |
|
| 159 | - return Requests::post( $this->getPathURI( $path ), $this->requestHeader, |
|
| 160 | - $this->addDataToRequest( $options, true ) ); |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - /** |
|
| 164 | - * @param string $path |
|
| 165 | - * @param array $options |
|
| 166 | - * |
|
| 167 | - * @return \Requests_Response |
|
| 168 | - */ |
|
| 169 | - public function patch( $path = '', $options = array() ) { |
|
| 170 | - return Requests::patch( $this->getPathURI( $path ), $this->requestHeader, |
|
| 171 | - $this->addDataToRequest( $options, true ) ); |
|
| 172 | - } |
|
| 173 | - |
|
| 174 | - /** |
|
| 175 | - * @param string $path |
|
| 176 | - * @param array $options |
|
| 177 | - * |
|
| 178 | - * @return \Requests_Response |
|
| 179 | - */ |
|
| 180 | - public function delete( $path = '', $options = array() ) { |
|
| 181 | - return Requests::delete( $this->getPathURI( $path ), $this->requestHeader, |
|
| 182 | - $this->addDataToRequest( $options ) ); |
|
| 183 | - } |
|
| 184 | - |
|
| 185 | - protected function refreshToken() { |
|
| 186 | - $this->requestHeader['Authorization'] = 'Bearer ' . $this->auth->getAccessToken(); |
|
| 187 | - } |
|
| 188 | - |
|
| 189 | - protected function addDataToPathURI( $path = '', $options = array(), $reqType = JCFirebaseOption::REQ_TYPE_GET ) { |
|
| 190 | - $print = ''; |
|
| 191 | - if ( isset( $options['print'] ) ) { |
|
| 192 | - if ( JCFirebaseOption::isAllowPrint( $reqType, $options['print'] ) ) { |
|
| 193 | - $print = $options['print']; |
|
| 194 | - } |
|
| 195 | - } |
|
| 196 | - |
|
| 197 | - return $this->getPathURI( $path, $print ); |
|
| 198 | - } |
|
| 199 | - |
|
| 200 | - protected function addDataToRequest( $options = array(), $jsonEncode = false ) { |
|
| 201 | - $requestOptions = array(); |
|
| 202 | - |
|
| 203 | - if ( isset( $options['data'] ) ) { |
|
| 204 | - $requestOptions = array_merge( $options['data'], $requestOptions ); |
|
| 205 | - } |
|
| 206 | - |
|
| 207 | - if ( $jsonEncode ) { |
|
| 208 | - $requestOptions = json_encode( $requestOptions ); |
|
| 209 | - } |
|
| 210 | - |
|
| 211 | - return $requestOptions; |
|
| 212 | - } |
|
| 19 | + public $firebaseURI; |
|
| 20 | + public $firebaseDefaultPath; |
|
| 21 | + public $requestHeader = array( |
|
| 22 | + 'accept' => 'application/json', |
|
| 23 | + 'contentType' => 'application/json; charset=utf-8', |
|
| 24 | + 'dataType' => 'json' |
|
| 25 | + ); |
|
| 26 | + public $requestOptions = array(); |
|
| 27 | + /** |
|
| 28 | + * @var OAuth |
|
| 29 | + */ |
|
| 30 | + protected $auth; |
|
| 31 | + |
|
| 32 | + |
|
| 33 | + /** |
|
| 34 | + * JCFirebase constructor. |
|
| 35 | + * |
|
| 36 | + * @param $firebaseURI |
|
| 37 | + * @param array $firebaseAuth |
|
| 38 | + * @param string $firebaseDefaultPath |
|
| 39 | + */ |
|
| 40 | + public function __construct( $firebaseURI, $firebaseAuth = array(), $firebaseDefaultPath = '/' ) { |
|
| 41 | + $this->firebaseURI = $firebaseURI; |
|
| 42 | + $this->firebaseDefaultPath = $firebaseDefaultPath; |
|
| 43 | + $this->setAuth( $firebaseAuth ); |
|
| 44 | + } |
|
| 45 | + |
|
| 46 | + /** |
|
| 47 | + * @param $firebaseURI |
|
| 48 | + * @param $keyFile |
|
| 49 | + * @param string $firebaseDefaultPath |
|
| 50 | + * |
|
| 51 | + * @return JCFirebase |
|
| 52 | + * @throws \Exception |
|
| 53 | + */ |
|
| 54 | + public static function fromKeyFile( $firebaseURI, $keyFile, $firebaseDefaultPath = '/' ) { |
|
| 55 | + $keyData = null; |
|
| 56 | + try { |
|
| 57 | + $keyData = json_decode( file_get_contents( $keyFile ) ); |
|
| 58 | + } catch ( \Exception $exception ) { |
|
| 59 | + $keyData = json_decode( Requests::get( $keyFile ) ); |
|
| 60 | + } |
|
| 61 | + |
|
| 62 | + if ( $keyData ) { |
|
| 63 | + $serviceAccount = $keyData->client_email; |
|
| 64 | + $privateKey = $keyData->private_key; |
|
| 65 | + |
|
| 66 | + return new self( $firebaseURI, array( |
|
| 67 | + 'key' => $privateKey, |
|
| 68 | + 'iss' => $serviceAccount |
|
| 69 | + ), $firebaseDefaultPath ); |
|
| 70 | + } else { |
|
| 71 | + throw new \Exception( "can't get data from key file" ); |
|
| 72 | + } |
|
| 73 | + } |
|
| 74 | + |
|
| 75 | + public function setAuth( $firebaseServiceAccount ) { |
|
| 76 | + if ( isset( $firebaseServiceAccount['key'] ) && isset( $firebaseServiceAccount['iss'] ) ) { |
|
| 77 | + $this->auth = new OAuth( $firebaseServiceAccount['key'], $firebaseServiceAccount['iss'] ); |
|
| 78 | + } |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + public function getPathURI( $path = '', $print = '' ) { |
|
| 82 | + //remove last slash from firebaseURI |
|
| 83 | + $template = '/'; |
|
| 84 | + $this->firebaseURI = rtrim( $this->firebaseURI, $template ); |
|
| 85 | + $path = rtrim( $path, $template ); |
|
| 86 | + $path = ltrim( $path, $template ); |
|
| 87 | + |
|
| 88 | + //check https |
|
| 89 | + if ( strpos( $this->firebaseURI, 'http://' ) !== false ) { |
|
| 90 | + throw new \Exception( "https is required." ); |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + //check firebaseURI |
|
| 94 | + if ( empty( $this->firebaseURI ) ) { |
|
| 95 | + throw new \Exception( "firebase URI is required" ); |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + if ( strpos( $this->firebaseDefaultPath, "/" ) !== 0 ) { |
|
| 99 | + throw new \Exception( "firebase default path must contain /" ); |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + $pathURI = $this->firebaseURI . $this->firebaseDefaultPath . $path . ".json"; |
|
| 103 | + |
|
| 104 | + //set query data |
|
| 105 | + $queryData = array(); |
|
| 106 | + if ( ! empty( $print ) ) { |
|
| 107 | + $queryData[ JCFirebaseOption::OPTION_PRINT ] = $print; |
|
| 108 | + } |
|
| 109 | + if ( ! empty( $queryData ) ) { |
|
| 110 | + $pathURI = $pathURI . '?' . http_build_query( $queryData ); |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + $this->refreshToken(); |
|
| 114 | + |
|
| 115 | + return $pathURI; |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + public function getShallow( $path = '', $options = array() ) { |
|
| 119 | + return Requests::get( |
|
| 120 | + $this->getPathURI( $path ) . '?' . http_build_query( array( |
|
| 121 | + JCFirebaseOption::OPTION_SHALLOW => JCFirebaseOption::SHALLOW_TRUE |
|
| 122 | + ) ), |
|
| 123 | + $this->requestHeader, |
|
| 124 | + $this->addDataToRequest( $options ) |
|
| 125 | + ); |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + /** |
|
| 129 | + * @param string $path |
|
| 130 | + * @param array $options |
|
| 131 | + * |
|
| 132 | + * @return \Requests_Response |
|
| 133 | + */ |
|
| 134 | + public function get( $path = '', $options = array() ) { |
|
| 135 | + return Requests::get( |
|
| 136 | + $this->addDataToPathURI( $path, $options ), $this->requestHeader, |
|
| 137 | + $this->addDataToRequest( $options ) |
|
| 138 | + ); |
|
| 139 | + } |
|
| 140 | + |
|
| 141 | + /** |
|
| 142 | + * @param string $path |
|
| 143 | + * @param array $options |
|
| 144 | + * |
|
| 145 | + * @return \Requests_Response |
|
| 146 | + */ |
|
| 147 | + public function put( $path = '', $options = array() ) { |
|
| 148 | + return Requests::put( $this->getPathURI( $path ), $this->requestHeader, |
|
| 149 | + $this->addDataToRequest( $options, true ) ); |
|
| 150 | + } |
|
| 151 | + |
|
| 152 | + /** |
|
| 153 | + * @param string $path |
|
| 154 | + * @param array $options |
|
| 155 | + * |
|
| 156 | + * @return \Requests_Response |
|
| 157 | + */ |
|
| 158 | + public function post( $path = '', $options = array() ) { |
|
| 159 | + return Requests::post( $this->getPathURI( $path ), $this->requestHeader, |
|
| 160 | + $this->addDataToRequest( $options, true ) ); |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + /** |
|
| 164 | + * @param string $path |
|
| 165 | + * @param array $options |
|
| 166 | + * |
|
| 167 | + * @return \Requests_Response |
|
| 168 | + */ |
|
| 169 | + public function patch( $path = '', $options = array() ) { |
|
| 170 | + return Requests::patch( $this->getPathURI( $path ), $this->requestHeader, |
|
| 171 | + $this->addDataToRequest( $options, true ) ); |
|
| 172 | + } |
|
| 173 | + |
|
| 174 | + /** |
|
| 175 | + * @param string $path |
|
| 176 | + * @param array $options |
|
| 177 | + * |
|
| 178 | + * @return \Requests_Response |
|
| 179 | + */ |
|
| 180 | + public function delete( $path = '', $options = array() ) { |
|
| 181 | + return Requests::delete( $this->getPathURI( $path ), $this->requestHeader, |
|
| 182 | + $this->addDataToRequest( $options ) ); |
|
| 183 | + } |
|
| 184 | + |
|
| 185 | + protected function refreshToken() { |
|
| 186 | + $this->requestHeader['Authorization'] = 'Bearer ' . $this->auth->getAccessToken(); |
|
| 187 | + } |
|
| 188 | + |
|
| 189 | + protected function addDataToPathURI( $path = '', $options = array(), $reqType = JCFirebaseOption::REQ_TYPE_GET ) { |
|
| 190 | + $print = ''; |
|
| 191 | + if ( isset( $options['print'] ) ) { |
|
| 192 | + if ( JCFirebaseOption::isAllowPrint( $reqType, $options['print'] ) ) { |
|
| 193 | + $print = $options['print']; |
|
| 194 | + } |
|
| 195 | + } |
|
| 196 | + |
|
| 197 | + return $this->getPathURI( $path, $print ); |
|
| 198 | + } |
|
| 199 | + |
|
| 200 | + protected function addDataToRequest( $options = array(), $jsonEncode = false ) { |
|
| 201 | + $requestOptions = array(); |
|
| 202 | + |
|
| 203 | + if ( isset( $options['data'] ) ) { |
|
| 204 | + $requestOptions = array_merge( $options['data'], $requestOptions ); |
|
| 205 | + } |
|
| 206 | + |
|
| 207 | + if ( $jsonEncode ) { |
|
| 208 | + $requestOptions = json_encode( $requestOptions ); |
|
| 209 | + } |
|
| 210 | + |
|
| 211 | + return $requestOptions; |
|
| 212 | + } |
|
| 213 | 213 | } |
| 214 | 214 | \ No newline at end of file |
@@ -37,10 +37,10 @@ discard block |
||
| 37 | 37 | * @param array $firebaseAuth |
| 38 | 38 | * @param string $firebaseDefaultPath |
| 39 | 39 | */ |
| 40 | - public function __construct( $firebaseURI, $firebaseAuth = array(), $firebaseDefaultPath = '/' ) { |
|
| 40 | + public function __construct($firebaseURI, $firebaseAuth = array(), $firebaseDefaultPath = '/') { |
|
| 41 | 41 | $this->firebaseURI = $firebaseURI; |
| 42 | 42 | $this->firebaseDefaultPath = $firebaseDefaultPath; |
| 43 | - $this->setAuth( $firebaseAuth ); |
|
| 43 | + $this->setAuth($firebaseAuth); |
|
| 44 | 44 | } |
| 45 | 45 | |
| 46 | 46 | /** |
@@ -51,63 +51,63 @@ discard block |
||
| 51 | 51 | * @return JCFirebase |
| 52 | 52 | * @throws \Exception |
| 53 | 53 | */ |
| 54 | - public static function fromKeyFile( $firebaseURI, $keyFile, $firebaseDefaultPath = '/' ) { |
|
| 54 | + public static function fromKeyFile($firebaseURI, $keyFile, $firebaseDefaultPath = '/') { |
|
| 55 | 55 | $keyData = null; |
| 56 | 56 | try { |
| 57 | - $keyData = json_decode( file_get_contents( $keyFile ) ); |
|
| 58 | - } catch ( \Exception $exception ) { |
|
| 59 | - $keyData = json_decode( Requests::get( $keyFile ) ); |
|
| 57 | + $keyData = json_decode(file_get_contents($keyFile)); |
|
| 58 | + } catch (\Exception $exception) { |
|
| 59 | + $keyData = json_decode(Requests::get($keyFile)); |
|
| 60 | 60 | } |
| 61 | 61 | |
| 62 | - if ( $keyData ) { |
|
| 62 | + if ($keyData) { |
|
| 63 | 63 | $serviceAccount = $keyData->client_email; |
| 64 | 64 | $privateKey = $keyData->private_key; |
| 65 | 65 | |
| 66 | - return new self( $firebaseURI, array( |
|
| 66 | + return new self($firebaseURI, array( |
|
| 67 | 67 | 'key' => $privateKey, |
| 68 | 68 | 'iss' => $serviceAccount |
| 69 | - ), $firebaseDefaultPath ); |
|
| 69 | + ), $firebaseDefaultPath); |
|
| 70 | 70 | } else { |
| 71 | - throw new \Exception( "can't get data from key file" ); |
|
| 71 | + throw new \Exception("can't get data from key file"); |
|
| 72 | 72 | } |
| 73 | 73 | } |
| 74 | 74 | |
| 75 | - public function setAuth( $firebaseServiceAccount ) { |
|
| 76 | - if ( isset( $firebaseServiceAccount['key'] ) && isset( $firebaseServiceAccount['iss'] ) ) { |
|
| 77 | - $this->auth = new OAuth( $firebaseServiceAccount['key'], $firebaseServiceAccount['iss'] ); |
|
| 75 | + public function setAuth($firebaseServiceAccount) { |
|
| 76 | + if (isset($firebaseServiceAccount['key']) && isset($firebaseServiceAccount['iss'])) { |
|
| 77 | + $this->auth = new OAuth($firebaseServiceAccount['key'], $firebaseServiceAccount['iss']); |
|
| 78 | 78 | } |
| 79 | 79 | } |
| 80 | 80 | |
| 81 | - public function getPathURI( $path = '', $print = '' ) { |
|
| 81 | + public function getPathURI($path = '', $print = '') { |
|
| 82 | 82 | //remove last slash from firebaseURI |
| 83 | 83 | $template = '/'; |
| 84 | - $this->firebaseURI = rtrim( $this->firebaseURI, $template ); |
|
| 85 | - $path = rtrim( $path, $template ); |
|
| 86 | - $path = ltrim( $path, $template ); |
|
| 84 | + $this->firebaseURI = rtrim($this->firebaseURI, $template); |
|
| 85 | + $path = rtrim($path, $template); |
|
| 86 | + $path = ltrim($path, $template); |
|
| 87 | 87 | |
| 88 | 88 | //check https |
| 89 | - if ( strpos( $this->firebaseURI, 'http://' ) !== false ) { |
|
| 90 | - throw new \Exception( "https is required." ); |
|
| 89 | + if (strpos($this->firebaseURI, 'http://') !== false) { |
|
| 90 | + throw new \Exception("https is required."); |
|
| 91 | 91 | } |
| 92 | 92 | |
| 93 | 93 | //check firebaseURI |
| 94 | - if ( empty( $this->firebaseURI ) ) { |
|
| 95 | - throw new \Exception( "firebase URI is required" ); |
|
| 94 | + if (empty($this->firebaseURI)) { |
|
| 95 | + throw new \Exception("firebase URI is required"); |
|
| 96 | 96 | } |
| 97 | 97 | |
| 98 | - if ( strpos( $this->firebaseDefaultPath, "/" ) !== 0 ) { |
|
| 99 | - throw new \Exception( "firebase default path must contain /" ); |
|
| 98 | + if (strpos($this->firebaseDefaultPath, "/") !== 0) { |
|
| 99 | + throw new \Exception("firebase default path must contain /"); |
|
| 100 | 100 | } |
| 101 | 101 | |
| 102 | - $pathURI = $this->firebaseURI . $this->firebaseDefaultPath . $path . ".json"; |
|
| 102 | + $pathURI = $this->firebaseURI.$this->firebaseDefaultPath.$path.".json"; |
|
| 103 | 103 | |
| 104 | 104 | //set query data |
| 105 | 105 | $queryData = array(); |
| 106 | - if ( ! empty( $print ) ) { |
|
| 107 | - $queryData[ JCFirebaseOption::OPTION_PRINT ] = $print; |
|
| 106 | + if (!empty($print)) { |
|
| 107 | + $queryData[JCFirebaseOption::OPTION_PRINT] = $print; |
|
| 108 | 108 | } |
| 109 | - if ( ! empty( $queryData ) ) { |
|
| 110 | - $pathURI = $pathURI . '?' . http_build_query( $queryData ); |
|
| 109 | + if (!empty($queryData)) { |
|
| 110 | + $pathURI = $pathURI.'?'.http_build_query($queryData); |
|
| 111 | 111 | } |
| 112 | 112 | |
| 113 | 113 | $this->refreshToken(); |
@@ -115,13 +115,13 @@ discard block |
||
| 115 | 115 | return $pathURI; |
| 116 | 116 | } |
| 117 | 117 | |
| 118 | - public function getShallow( $path = '', $options = array() ) { |
|
| 118 | + public function getShallow($path = '', $options = array()) { |
|
| 119 | 119 | return Requests::get( |
| 120 | - $this->getPathURI( $path ) . '?' . http_build_query( array( |
|
| 120 | + $this->getPathURI($path).'?'.http_build_query(array( |
|
| 121 | 121 | JCFirebaseOption::OPTION_SHALLOW => JCFirebaseOption::SHALLOW_TRUE |
| 122 | - ) ), |
|
| 122 | + )), |
|
| 123 | 123 | $this->requestHeader, |
| 124 | - $this->addDataToRequest( $options ) |
|
| 124 | + $this->addDataToRequest($options) |
|
| 125 | 125 | ); |
| 126 | 126 | } |
| 127 | 127 | |
@@ -131,10 +131,10 @@ discard block |
||
| 131 | 131 | * |
| 132 | 132 | * @return \Requests_Response |
| 133 | 133 | */ |
| 134 | - public function get( $path = '', $options = array() ) { |
|
| 134 | + public function get($path = '', $options = array()) { |
|
| 135 | 135 | return Requests::get( |
| 136 | - $this->addDataToPathURI( $path, $options ), $this->requestHeader, |
|
| 137 | - $this->addDataToRequest( $options ) |
|
| 136 | + $this->addDataToPathURI($path, $options), $this->requestHeader, |
|
| 137 | + $this->addDataToRequest($options) |
|
| 138 | 138 | ); |
| 139 | 139 | } |
| 140 | 140 | |
@@ -144,9 +144,9 @@ discard block |
||
| 144 | 144 | * |
| 145 | 145 | * @return \Requests_Response |
| 146 | 146 | */ |
| 147 | - public function put( $path = '', $options = array() ) { |
|
| 148 | - return Requests::put( $this->getPathURI( $path ), $this->requestHeader, |
|
| 149 | - $this->addDataToRequest( $options, true ) ); |
|
| 147 | + public function put($path = '', $options = array()) { |
|
| 148 | + return Requests::put($this->getPathURI($path), $this->requestHeader, |
|
| 149 | + $this->addDataToRequest($options, true)); |
|
| 150 | 150 | } |
| 151 | 151 | |
| 152 | 152 | /** |
@@ -155,9 +155,9 @@ discard block |
||
| 155 | 155 | * |
| 156 | 156 | * @return \Requests_Response |
| 157 | 157 | */ |
| 158 | - public function post( $path = '', $options = array() ) { |
|
| 159 | - return Requests::post( $this->getPathURI( $path ), $this->requestHeader, |
|
| 160 | - $this->addDataToRequest( $options, true ) ); |
|
| 158 | + public function post($path = '', $options = array()) { |
|
| 159 | + return Requests::post($this->getPathURI($path), $this->requestHeader, |
|
| 160 | + $this->addDataToRequest($options, true)); |
|
| 161 | 161 | } |
| 162 | 162 | |
| 163 | 163 | /** |
@@ -166,9 +166,9 @@ discard block |
||
| 166 | 166 | * |
| 167 | 167 | * @return \Requests_Response |
| 168 | 168 | */ |
| 169 | - public function patch( $path = '', $options = array() ) { |
|
| 170 | - return Requests::patch( $this->getPathURI( $path ), $this->requestHeader, |
|
| 171 | - $this->addDataToRequest( $options, true ) ); |
|
| 169 | + public function patch($path = '', $options = array()) { |
|
| 170 | + return Requests::patch($this->getPathURI($path), $this->requestHeader, |
|
| 171 | + $this->addDataToRequest($options, true)); |
|
| 172 | 172 | } |
| 173 | 173 | |
| 174 | 174 | /** |
@@ -177,35 +177,35 @@ discard block |
||
| 177 | 177 | * |
| 178 | 178 | * @return \Requests_Response |
| 179 | 179 | */ |
| 180 | - public function delete( $path = '', $options = array() ) { |
|
| 181 | - return Requests::delete( $this->getPathURI( $path ), $this->requestHeader, |
|
| 182 | - $this->addDataToRequest( $options ) ); |
|
| 180 | + public function delete($path = '', $options = array()) { |
|
| 181 | + return Requests::delete($this->getPathURI($path), $this->requestHeader, |
|
| 182 | + $this->addDataToRequest($options)); |
|
| 183 | 183 | } |
| 184 | 184 | |
| 185 | 185 | protected function refreshToken() { |
| 186 | - $this->requestHeader['Authorization'] = 'Bearer ' . $this->auth->getAccessToken(); |
|
| 186 | + $this->requestHeader['Authorization'] = 'Bearer '.$this->auth->getAccessToken(); |
|
| 187 | 187 | } |
| 188 | 188 | |
| 189 | - protected function addDataToPathURI( $path = '', $options = array(), $reqType = JCFirebaseOption::REQ_TYPE_GET ) { |
|
| 189 | + protected function addDataToPathURI($path = '', $options = array(), $reqType = JCFirebaseOption::REQ_TYPE_GET) { |
|
| 190 | 190 | $print = ''; |
| 191 | - if ( isset( $options['print'] ) ) { |
|
| 192 | - if ( JCFirebaseOption::isAllowPrint( $reqType, $options['print'] ) ) { |
|
| 191 | + if (isset($options['print'])) { |
|
| 192 | + if (JCFirebaseOption::isAllowPrint($reqType, $options['print'])) { |
|
| 193 | 193 | $print = $options['print']; |
| 194 | 194 | } |
| 195 | 195 | } |
| 196 | 196 | |
| 197 | - return $this->getPathURI( $path, $print ); |
|
| 197 | + return $this->getPathURI($path, $print); |
|
| 198 | 198 | } |
| 199 | 199 | |
| 200 | - protected function addDataToRequest( $options = array(), $jsonEncode = false ) { |
|
| 200 | + protected function addDataToRequest($options = array(), $jsonEncode = false) { |
|
| 201 | 201 | $requestOptions = array(); |
| 202 | 202 | |
| 203 | - if ( isset( $options['data'] ) ) { |
|
| 204 | - $requestOptions = array_merge( $options['data'], $requestOptions ); |
|
| 203 | + if (isset($options['data'])) { |
|
| 204 | + $requestOptions = array_merge($options['data'], $requestOptions); |
|
| 205 | 205 | } |
| 206 | 206 | |
| 207 | - if ( $jsonEncode ) { |
|
| 208 | - $requestOptions = json_encode( $requestOptions ); |
|
| 207 | + if ($jsonEncode) { |
|
| 208 | + $requestOptions = json_encode($requestOptions); |
|
| 209 | 209 | } |
| 210 | 210 | |
| 211 | 211 | return $requestOptions; |