PluginProbe ʕ •ᴥ•ʔ
Image Widget / 4.2
Image Widget v4.2
trunk 1.0 2.0 2.1 2.2 2.2.1 2.2.2 3.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1 3.1.1 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.2 3.2.1 3.2.10 3.2.11 3.2.2 3.2.3 3.2.4 3.2.5 3.2.7 3.2.8 3.2.9 3.3 3.3.1 3.3.2 3.3.3 3.3.4 3.3.5 3.3.6 3.3.7 4.0 4.0.1 4.0.2 4.0.3 4.0.4 4.0.5 4.0.6 4.0.7 4.0.8 4.0.9 4.1 4.1.1 4.1.2 4.2 4.2.1 4.2.2 4.3 4.3.1 4.4 4.4.1 4.4.11 4.4.12 4.4.2 4.4.3 4.4.4 4.4.5 4.4.6 4.4.7 4.4.8 4.4.9
image-widget / freemius / includes / sdk / FreemiusBase.php
image-widget / freemius / includes / sdk Last commit date
Exceptions 10 years ago Freemius.php 10 years ago FreemiusBase.php 10 years ago LICENSE.txt 10 years ago
FreemiusBase.php
218 lines
1 <?php
2 /**
3 * Copyright 2014 Freemius, Inc.
4 *
5 * Licensed under the GPL v2 (the "License"); you may
6 * not use this file except in compliance with the License. You may obtain
7 * a copy of the License at
8 *
9 * http://choosealicense.com/licenses/gpl-v2/
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14 * License for the specific language governing permissions and limitations
15 * under the License.
16 */
17
18 define( 'FS_API__VERSION', '1' );
19 define( 'FS_SDK__PATH', dirname( __FILE__ ) );
20 define( 'FS_SDK__EXCEPTIONS_PATH', FS_SDK__PATH . '/Exceptions/' );
21
22 if ( ! function_exists( 'json_decode' ) ) {
23 throw new Exception( 'Freemius needs the JSON PHP extension.' );
24 }
25
26 // Include all exception files.
27 $exceptions = array(
28 'Exception',
29 'InvalidArgumentException',
30 'ArgumentNotExistException',
31 'EmptyArgumentException',
32 'OAuthException'
33 );
34
35 foreach ( $exceptions as $e ) {
36 require FS_SDK__EXCEPTIONS_PATH . $e . '.php';
37 }
38
39 abstract class Freemius_Api_Base {
40 const VERSION = '1.0.4';
41 const FORMAT = 'json';
42
43 protected $_id;
44 protected $_public;
45 protected $_secret;
46 protected $_scope;
47 protected $_sandbox;
48
49 /**
50 * @param string $pScope 'app', 'developer', 'user' or 'install'.
51 * @param number $pID Element's id.
52 * @param string $pPublic Public key.
53 * @param string $pSecret Element's secret key.
54 * @param bool $pSandbox Whether or not to run API in sandbox mode.
55 */
56 public function Init( $pScope, $pID, $pPublic, $pSecret, $pSandbox = false ) {
57 $this->_id = $pID;
58 $this->_public = $pPublic;
59 $this->_secret = $pSecret;
60 $this->_scope = $pScope;
61 $this->_sandbox = $pSandbox;
62 }
63
64 public function IsSandbox() {
65 return $this->_sandbox;
66 }
67
68 function CanonizePath( $pPath ) {
69 $pPath = trim( $pPath, '/' );
70 $query_pos = strpos( $pPath, '?' );
71 $query = '';
72
73 if ( false !== $query_pos ) {
74 $query = substr( $pPath, $query_pos );
75 $pPath = substr( $pPath, 0, $query_pos );
76 }
77
78 // Trim '.json' suffix.
79 $format_length = strlen( '.' . self::FORMAT );
80 $start = $format_length * ( - 1 ); //negative
81 if ( substr( strtolower( $pPath ), $start ) === ( '.' . self::FORMAT ) ) {
82 $pPath = substr( $pPath, 0, strlen( $pPath ) - $format_length );
83 }
84
85 switch ( $this->_scope ) {
86 case 'app':
87 $base = '/apps/' . $this->_id;
88 break;
89 case 'developer':
90 $base = '/developers/' . $this->_id;
91 break;
92 case 'user':
93 $base = '/users/' . $this->_id;
94 break;
95 case 'plugin':
96 $base = '/plugins/' . $this->_id;
97 break;
98 case 'install':
99 $base = '/installs/' . $this->_id;
100 break;
101 default:
102 throw new Freemius_Exception( 'Scope not implemented.' );
103 }
104
105 return '/v' . FS_API__VERSION . $base .
106 ( ! empty( $pPath ) ? '/' : '' ) . $pPath .
107 ( ( false === strpos( $pPath, '.' ) ) ? '.' . self::FORMAT : '' ) . $query;
108 }
109
110 abstract function MakeRequest( $pCanonizedPath, $pMethod = 'GET', $pParams = array() );
111
112 /**
113 * @param string $pPath
114 * @param string $pMethod
115 * @param array $pParams
116 *
117 * @return object[]|object|null
118 */
119 private function _Api( $pPath, $pMethod = 'GET', $pParams = array() ) {
120 $pMethod = strtoupper( $pMethod );
121
122 try {
123 $result = $this->MakeRequest( $pPath, $pMethod, $pParams );
124 } catch ( Freemius_Exception $e ) {
125 // Map to error object.
126 $result = (object) $e->getResult();
127 } catch ( Exception $e ) {
128 // Map to error object.
129 $result = (object) array(
130 'error' => array(
131 'type' => 'Unknown',
132 'message' => $e->getMessage() . ' (' . $e->getFile() . ': ' . $e->getLine() . ')',
133 'code' => 'unknown',
134 'http' => 402
135 )
136 );
137 }
138
139 return $result;
140 }
141
142 /**
143 * If successful connectivity to the API endpoint using ping.json endpoint.
144 *
145 * - OR -
146 *
147 * Validate if ping result object is valid.
148 *
149 * @param mixed $pPong
150 *
151 * @return bool
152 */
153 public function Test( $pPong = null ) {
154 $pong = is_null( $pPong ) ? $this->Ping() : $pPong;
155
156 return ( is_object( $pong ) && isset( $pong->api ) && 'pong' === $pong->api );
157 }
158
159 /**
160 * Ping API to test connectivity.
161 *
162 * @return object
163 */
164 public function Ping() {
165 return $this->_Api( '/v' . FS_API__VERSION . '/ping.json' );
166 }
167
168 /**
169 * Find clock diff between current server to API server.
170 *
171 * @since 1.0.2
172 * @return int Clock diff in seconds.
173 */
174 public function FindClockDiff() {
175 $time = time();
176 $pong = $this->_Api( '/v' . FS_API__VERSION . '/ping.json' );
177
178 return ( $time - strtotime( $pong->timestamp ) );
179 }
180
181 public function Api( $pPath, $pMethod = 'GET', $pParams = array() ) {
182 return $this->_Api( $this->CanonizePath( $pPath ), $pMethod, $pParams );
183 }
184
185 /**
186 * Base64 encoding that does not need to be urlencode()ed.
187 * Exactly the same as base64_encode except it uses
188 * - instead of +
189 * _ instead of /
190 * No padded =
191 *
192 * @param string $input base64UrlEncoded string
193 *
194 * @return string
195 */
196 protected static function Base64UrlDecode( $input ) {
197 return base64_decode( strtr( $input, '-_', '+/' ) );
198 }
199
200 /**
201 * Base64 encoding that does not need to be urlencode()ed.
202 * Exactly the same as base64_encode except it uses
203 * - instead of +
204 * _ instead of /
205 *
206 * @param string $input string
207 *
208 * @return string base64Url encoded string
209 */
210 protected static function Base64UrlEncode( $input ) {
211 $str = strtr( base64_encode( $input ), '+/', '-_' );
212 $str = str_replace( '=', '', $str );
213
214 return $str;
215 }
216
217 }
218