PluginProbe ʕ •ᴥ•ʔ
Robin Image Optimizer – Unlimited Image Optimization, WebP & AVIF / trunk
Robin Image Optimizer – Unlimited Image Optimization, WebP & AVIF vtrunk
2.0.5 trunk 1.3.7 1.4.0 1.4.1 1.4.2 1.4.6 1.5.0 1.5.3 1.5.6 1.5.8 1.6.5 1.6.6 1.6.9 1.7.0 1.7.4 1.8.1 1.8.2 1.9.0 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4
robin-image-optimizer / libs / factory / freemius / includes / sdk / FreemiusBase.php
robin-image-optimizer / libs / factory / freemius / includes / sdk Last commit date
Exceptions 5 months ago FreemiusBase.php 5 months ago FreemiusWordPress.php 5 months ago LICENSE.txt 3 years ago index.php 6 months ago
FreemiusBase.php
220 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 namespace WBCR\Factory_Freemius_Rio_600\Sdk;
19
20 if ( ! defined( 'ABSPATH' ) ) {
21 exit;
22 }
23
24 if ( ! defined( 'FREEMIUS_API__VERSION' ) ) {
25 define( 'FREEMIUS_API__VERSION', '1' );
26 }
27 if ( ! defined( 'FREEMIUS_SDK__PATH' ) ) {
28 define( 'FREEMIUS_SDK__PATH', __DIR__ );
29 }
30 if ( ! defined( 'FREEMIUS_SDK__EXCEPTIONS_PATH' ) ) {
31 define( 'FREEMIUS_SDK__EXCEPTIONS_PATH', FREEMIUS_SDK__PATH . '/Exceptions/' );
32 }
33
34 if ( ! function_exists( 'json_decode' ) ) {
35 throw new \Exception( 'Freemius needs the JSON PHP extension.' );
36 }
37
38 // Include all exception files.
39 $exceptions = [
40 'Exception',
41 'InvalidArgumentException',
42 'ArgumentNotExistException',
43 'EmptyArgumentException',
44 'OAuthException',
45 ];
46
47 foreach ( $exceptions as $e ) {
48 require_once FREEMIUS_SDK__EXCEPTIONS_PATH . $e . '.php';
49 }
50
51 if ( class_exists( 'WBCR\Factory_Freemius_Rio_600\Sdk\Freemius_Api_Base' ) ) {
52 return;
53 }
54
55 abstract class Freemius_Api_Base {
56
57 const VERSION = '1.0.4';
58 const FORMAT = 'json';
59
60 protected $_id;
61 protected $_public;
62 protected $_secret;
63 protected $_scope;
64 protected $_isSandbox;
65
66 /**
67 * @param string $pScope 'app', 'developer', 'plugin', 'user' or 'install'.
68 * @param number $pID Element's id.
69 * @param string $pPublic Public key.
70 * @param string $pSecret Element's secret key.
71 * @param bool $pIsSandbox Whether or not to run API in sandbox mode.
72 */
73 public function Init( $pScope, $pID, $pPublic, $pSecret, $pIsSandbox = false ) {
74 $this->_id = $pID;
75 $this->_public = $pPublic;
76 $this->_secret = $pSecret;
77 $this->_scope = $pScope;
78 $this->_isSandbox = $pIsSandbox;
79 }
80
81 public function IsSandbox() {
82 return $this->_isSandbox;
83 }
84
85 function CanonizePath( $pPath ) {
86 $pPath = trim( $pPath, '/' );
87 $query_pos = strpos( $pPath, '?' );
88 $query = '';
89
90 if ( false !== $query_pos ) {
91 $query = substr( $pPath, $query_pos );
92 $pPath = substr( $pPath, 0, $query_pos );
93 }
94
95 // Trim '.json' suffix.
96 $format_length = strlen( '.' . self::FORMAT );
97 $start = $format_length * ( -1 ); // negative
98 if ( substr( strtolower( $pPath ), $start ) === ( '.' . self::FORMAT ) ) {
99 $pPath = substr( $pPath, 0, strlen( $pPath ) - $format_length );
100 }
101
102 switch ( $this->_scope ) {
103 case 'app':
104 $base = '/apps/' . $this->_id;
105 break;
106 case 'developer':
107 $base = '/developers/' . $this->_id;
108 break;
109 case 'user':
110 $base = '/users/' . $this->_id;
111 break;
112 case 'plugin':
113 $base = '/plugins/' . $this->_id;
114 break;
115 case 'install':
116 $base = '/installs/' . $this->_id;
117 break;
118 default:
119 throw new Freemius_Exception( 'Scope not implemented.' );
120 }
121
122 return '/v' . FREEMIUS_API__VERSION . $base . ( ! empty( $pPath ) ? '/' : '' ) . $pPath . ( ( false === strpos( $pPath, '.' ) ) ? '.' . self::FORMAT : '' ) . $query;
123 }
124
125 abstract function MakeRequest( $pCanonizedPath, $pMethod = 'GET', $pParams = [] );
126
127 /**
128 * @param string $pPath
129 * @param string $pMethod
130 * @param array $pParams
131 *
132 * @return object[]|object|null
133 */
134 private function _Api( $pPath, $pMethod = 'GET', $pParams = [] ) {
135 $pMethod = strtoupper( $pMethod );
136
137 try {
138 $result = $this->MakeRequest( $pPath, $pMethod, $pParams );
139 } catch ( Freemius_Exception $e ) {
140 // Map to error object.
141 $result = (object) $e->getResult();
142 } catch ( \Exception $e ) {
143 // Map to error object.
144 $result = (object) [
145 'error' => [
146 'type' => 'Unknown',
147 'message' => $e->getMessage() . ' (' . $e->getFile() . ': ' . $e->getLine() . ')',
148 'code' => 'unknown',
149 'http' => 402,
150 ],
151 ];
152 }
153
154 return $result;
155 }
156
157 public function Api( $pPath, $pMethod = 'GET', $pParams = [] ) {
158 return $this->_Api( $this->CanonizePath( $pPath ), $pMethod, $pParams );
159 }
160
161 /**
162 * Base64 decoding that does not need to be urldecode()-ed.
163 *
164 * Exactly the same as PHP base64 encode except it uses
165 * `-` instead of `+`
166 * `_` instead of `/`
167 * No padded =
168 *
169 * @param string $input Base64UrlEncoded() string
170 *
171 * @return string
172 */
173 protected static function Base64UrlDecode( $input ) {
174 /**
175 * IMPORTANT NOTE:
176 * This is a hack suggested by @otto42 and @greenshady from
177 * the theme's review team. The usage of base64 for API
178 * signature encoding was approved in a Slack meeting
179 * held on Tue (10/25 2016).
180 *
181 * @todo Remove this hack once the base64 error is removed from the Theme Check.
182 *
183 * @since 1.2.2
184 */
185 $fn = 'base64' . '_decode';
186
187 return $fn( strtr( $input, '-_', '+/' ) );
188 }
189
190 /**
191 * Base64 encoding that does not need to be urlencode()ed.
192 *
193 * Exactly the same as base64 encode except it uses
194 * `-` instead of `+
195 * `_` instead of `/`
196 *
197 * @param string $input string
198 *
199 * @return string Base64 encoded string
200 */
201 protected static function Base64UrlEncode( $input ) {
202 /**
203 * IMPORTANT NOTE:
204 * This is a hack suggested by @otto42 and @greenshady from
205 * the theme's review team. The usage of base64 for API
206 * signature encoding was approved in a Slack meeting
207 * held on Tue (10/25 2016).
208 *
209 * @todo Remove this hack once the base64 error is removed from the Theme Check.
210 *
211 * @since 1.2.2
212 */
213 $fn = 'base64' . '_encode';
214 $str = strtr( $fn( $input ), '+/', '-_' );
215 $str = str_replace( '=', '', $str );
216
217 return $str;
218 }
219 }
220