PluginProbe ʕ •ᴥ•ʔ
EmbedPress – PDF Embedder, Embed PDF viewer, YouTube Videos, 3D FlipBook, Social feeds & more / 1.7.3
EmbedPress – PDF Embedder, Embed PDF viewer, YouTube Videos, 3D FlipBook, Social feeds & more v1.7.3
4.5.6 4.5.5 4.5.4 4.5.3 4.5.2 trunk 1.0.0 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.3.0 1.3.1 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.5.0 1.6.0 1.6.1 1.6.2 1.6.3 1.7.0 1.7.1 1.7.2 1.7.3 1.7.4 1.7.5 2.0.0 2.0.1 2.0.2 2.0.3 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.2.0 2.2.1 2.2.2 2.3.0 2.3.1 2.3.2 2.3.3 2.4.0 2.4.1 2.5.0 2.5.1 2.5.2 2.5.3 2.5.4 2.5.5 2.6.0 2.6.1 2.6.2 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.7.6 2.7.7 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.1.0 3.1.1 3.1.2 3.1.3 3.2.0 3.2.1 3.3.0 3.3.1 3.3.2 3.3.3 3.3.4 3.3.5 3.3.6 3.3.7 3.4.0 3.4.1 3.4.2 3.4.3 3.5.0 3.5.1 3.5.2 3.5.3 3.6.0 3.6.1 3.6.2 3.6.3 3.6.4 3.6.5 3.6.6 3.6.7 3.6.8 3.7.0 3.7.1 3.7.2 3.7.3 3.8.0 3.8.1 3.8.2 3.8.3 3.8.4 3.8.5 3.9.0 3.9.1 3.9.10 3.9.11 3.9.12 3.9.13 3.9.14 3.9.15 3.9.16 3.9.17 3.9.2 3.9.3 3.9.4 3.9.5 3.9.6 3.9.7 3.9.8 3.9.9 4.0.0 4.0.1 4.0.10 4.0.11 4.0.12 4.0.13 4.0.14 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.0 4.1.1 4.1.10 4.1.2 4.1.3 4.1.4 4.1.5 4.1.6 4.1.7 4.1.8 4.1.9 4.2.0 4.2.1 4.2.2 4.2.3 4.2.4 4.2.5 4.2.6 4.2.7 4.2.8 4.2.9 4.3.0 4.3.1 4.4.0 4.4.1 4.4.10 4.4.11 4.4.2 4.4.3 4.4.4 4.4.5 4.4.6 4.4.7 4.4.8 4.4.9 4.5.0 4.5.1
embedpress / freemius / includes / sdk / FreemiusBase.php
embedpress / freemius / includes / sdk Last commit date
Exceptions 9 years ago Freemius.php 9 years ago FreemiusBase.php 9 years ago LICENSE.txt 9 years ago index.php 9 years ago
FreemiusBase.php
189 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 if ( ! defined( 'FS_API__VERSION' ) ) {
19 define( 'FS_API__VERSION', '1' );
20 }
21 if ( ! defined( 'FS_SDK__PATH' ) ) {
22 define( 'FS_SDK__PATH', dirname( __FILE__ ) );
23 }
24 if ( ! defined( 'FS_SDK__EXCEPTIONS_PATH' ) ) {
25 define( 'FS_SDK__EXCEPTIONS_PATH', FS_SDK__PATH . '/Exceptions/' );
26 }
27
28 if ( ! function_exists( 'json_decode' ) ) {
29 throw new Exception( 'Freemius needs the JSON PHP extension.' );
30 }
31
32 // Include all exception files.
33 $exceptions = array(
34 'Exception',
35 'InvalidArgumentException',
36 'ArgumentNotExistException',
37 'EmptyArgumentException',
38 'OAuthException'
39 );
40
41 foreach ( $exceptions as $e ) {
42 require_once FS_SDK__EXCEPTIONS_PATH . $e . '.php';
43 }
44
45 if ( class_exists( 'Freemius_Api_Base' ) ) {
46 return;
47 }
48
49 abstract class Freemius_Api_Base {
50 const VERSION = '1.0.4';
51 const FORMAT = 'json';
52
53 protected $_id;
54 protected $_public;
55 protected $_secret;
56 protected $_scope;
57 protected $_isSandbox;
58
59 /**
60 * @param string $pScope 'app', 'developer', 'plugin', 'user' or 'install'.
61 * @param number $pID Element's id.
62 * @param string $pPublic Public key.
63 * @param string $pSecret Element's secret key.
64 * @param bool $pIsSandbox Whether or not to run API in sandbox mode.
65 */
66 public function Init( $pScope, $pID, $pPublic, $pSecret, $pIsSandbox = false ) {
67 $this->_id = $pID;
68 $this->_public = $pPublic;
69 $this->_secret = $pSecret;
70 $this->_scope = $pScope;
71 $this->_isSandbox = $pIsSandbox;
72 }
73
74 public function IsSandbox() {
75 return $this->_isSandbox;
76 }
77
78 function CanonizePath( $pPath ) {
79 $pPath = trim( $pPath, '/' );
80 $query_pos = strpos( $pPath, '?' );
81 $query = '';
82
83 if ( false !== $query_pos ) {
84 $query = substr( $pPath, $query_pos );
85 $pPath = substr( $pPath, 0, $query_pos );
86 }
87
88 // Trim '.json' suffix.
89 $format_length = strlen( '.' . self::FORMAT );
90 $start = $format_length * ( - 1 ); //negative
91 if ( substr( strtolower( $pPath ), $start ) === ( '.' . self::FORMAT ) ) {
92 $pPath = substr( $pPath, 0, strlen( $pPath ) - $format_length );
93 }
94
95 switch ( $this->_scope ) {
96 case 'app':
97 $base = '/apps/' . $this->_id;
98 break;
99 case 'developer':
100 $base = '/developers/' . $this->_id;
101 break;
102 case 'user':
103 $base = '/users/' . $this->_id;
104 break;
105 case 'plugin':
106 $base = '/plugins/' . $this->_id;
107 break;
108 case 'install':
109 $base = '/installs/' . $this->_id;
110 break;
111 default:
112 throw new Freemius_Exception( 'Scope not implemented.' );
113 }
114
115 return '/v' . FS_API__VERSION . $base .
116 ( ! empty( $pPath ) ? '/' : '' ) . $pPath .
117 ( ( false === strpos( $pPath, '.' ) ) ? '.' . self::FORMAT : '' ) . $query;
118 }
119
120 abstract function MakeRequest( $pCanonizedPath, $pMethod = 'GET', $pParams = array() );
121
122 /**
123 * @param string $pPath
124 * @param string $pMethod
125 * @param array $pParams
126 *
127 * @return object[]|object|null
128 */
129 private function _Api( $pPath, $pMethod = 'GET', $pParams = array() ) {
130 $pMethod = strtoupper( $pMethod );
131
132 try {
133 $result = $this->MakeRequest( $pPath, $pMethod, $pParams );
134 } catch ( Freemius_Exception $e ) {
135 // Map to error object.
136 $result = (object) $e->getResult();
137 } catch ( Exception $e ) {
138 // Map to error object.
139 $result = (object) array(
140 'error' => array(
141 'type' => 'Unknown',
142 'message' => $e->getMessage() . ' (' . $e->getFile() . ': ' . $e->getLine() . ')',
143 'code' => 'unknown',
144 'http' => 402
145 )
146 );
147 }
148
149 return $result;
150 }
151
152 public function Api( $pPath, $pMethod = 'GET', $pParams = array() ) {
153 return $this->_Api( $this->CanonizePath( $pPath ), $pMethod, $pParams );
154 }
155
156 /**
157 * Base64 encoding that does not need to be urlencode()ed.
158 * Exactly the same as base64_encode except it uses
159 * - instead of +
160 * _ instead of /
161 * No padded =
162 *
163 * @param string $input base64UrlEncoded string
164 *
165 * @return string
166 */
167 protected static function Base64UrlDecode( $input ) {
168 return base64_decode( strtr( $input, '-_', '+/' ) );
169 }
170
171 /**
172 * Base64 encoding that does not need to be urlencode()ed.
173 * Exactly the same as base64_encode except it uses
174 * - instead of +
175 * _ instead of /
176 *
177 * @param string $input string
178 *
179 * @return string base64Url encoded string
180 */
181 protected static function Base64UrlEncode( $input ) {
182 $str = strtr( base64_encode( $input ), '+/', '-_' );
183 $str = str_replace( '=', '', $str );
184
185 return $str;
186 }
187
188 }
189