PluginProbe
Adminify – White Label, Admin Menu Editor, Login Customizer / 1.0.9
Adminify – White Label, Admin Menu Editor, Login Customizer v1.0.9
4.3.1 4.3.0 4.2.26 4.2.25 4.2.24 4.2.23 4.2.22 4.2.21 4.2.20 4.2.19 4.2.18 4.2.17 4.2.16 4.2.15 4.2.14 4.2.13 4.2.12 4.2.11 4.2.10 4.2.9 4.2.8 4.2.7 4.2.6 4.2.5 4.1.17 All 164 releases
adminify / lib / freemius / includes / sdk / FreemiusBase.php

FreemiusBase.php in Adminify – White Label, Admin Menu Editor, Login Customizer 1.0.9, at lib/freemius/includes/sdk/FreemiusBase.php

222 lines 6.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Copyright 2014 Freemius, Inc.
5 *
6 * Licensed under the GPL v2 (the "License"); you may
7 * not use this file except in compliance with the License. You may obtain
8 * a copy of the License at
9 *
10 * http://choosealicense.com/licenses/gpl-v2/
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15 * License for the specific language governing permissions and limitations
16 * under the License.
17 */
18 if (!class_exists('Freemius_Api_Base')) {
19 if (!defined('FS_API__VERSION')) {
20 define('FS_API__VERSION', '1');
21 }
22 if (!defined('FS_SDK__PATH')) {
23 define('FS_SDK__PATH', dirname(__FILE__));
24 }
25 if (!defined('FS_SDK__EXCEPTIONS_PATH')) {
26 define('FS_SDK__EXCEPTIONS_PATH', FS_SDK__PATH . '/Exceptions/');
27 }
28
29 if (!function_exists('json_decode')) {
30 throw new Exception('Freemius needs the JSON PHP extension.');
31 }
32
33 // Include all exception files.
34 $exceptions = array(
35 'Exception',
36 'InvalidArgumentException',
37 'ArgumentNotExistException',
38 'EmptyArgumentException',
39 'OAuthException'
40 );
41
42 foreach ($exceptions as $e) {
43 require_once FS_SDK__EXCEPTIONS_PATH . $e . '.php';
44 }
45
46 abstract class Freemius_Api_Base
47 {
48 const VERSION = '1.0.4';
49 const FORMAT = 'json';
50
51 protected $_id;
52 protected $_public;
53 protected $_secret;
54 protected $_scope;
55 protected $_isSandbox;
56
57 /**
58 * @param string $pScope 'app', 'developer', 'plugin', 'user' or 'install'.
59 * @param number $pID Element's id.
60 * @param string $pPublic Public key.
61 * @param string $pSecret Element's secret key.
62 * @param bool $pIsSandbox Whether or not to run API in sandbox mode.
63 */
64 public function Init($pScope, $pID, $pPublic, $pSecret, $pIsSandbox = false)
65 {
66 $this->_id = $pID;
67 $this->_public = $pPublic;
68 $this->_secret = $pSecret;
69 $this->_scope = $pScope;
70 $this->_isSandbox = $pIsSandbox;
71 }
72
73 public function IsSandbox()
74 {
75 return $this->_isSandbox;
76 }
77
78 function CanonizePath($pPath)
79 {
80 $pPath = trim($pPath, '/');
81 $query_pos = strpos($pPath, '?');
82 $query = '';
83
84 if (false !== $query_pos) {
85 $query = substr($pPath, $query_pos);
86 $pPath = substr($pPath, 0, $query_pos);
87 }
88
89 // Trim '.json' suffix.
90 $format_length = strlen('.' . self::FORMAT);
91 $start = $format_length * (-1); //negative
92 if (substr(strtolower($pPath), $start) === ('.' . self::FORMAT)) {
93 $pPath = substr($pPath, 0, strlen($pPath) - $format_length);
94 }
95
96 switch ($this->_scope) {
97 case 'app':
98 $base = '/apps/' . $this->_id;
99 break;
100 case 'developer':
101 $base = '/developers/' . $this->_id;
102 break;
103 case 'user':
104 $base = '/users/' . $this->_id;
105 break;
106 case 'plugin':
107 $base = '/plugins/' . $this->_id;
108 break;
109 case 'install':
110 $base = '/installs/' . $this->_id;
111 break;
112 default:
113 throw new Freemius_Exception('Scope not implemented.');
114 }
115
116 return '/v' . FS_API__VERSION . $base .
117 (!empty($pPath) ? '/' : '') . $pPath .
118 ((false === strpos($pPath, '.')) ? '.' . self::FORMAT : '') . $query;
119 }
120
121 abstract function MakeRequest($pCanonizedPath, $pMethod = 'GET', $pParams = array());
122
123 /**
124 * @param string $pPath
125 * @param string $pMethod
126 * @param array $pParams
127 *
128 * @return object[]|object|null
129 */
130 private function _Api($pPath, $pMethod = 'GET', $pParams = array())
131 {
132 $pMethod = strtoupper($pMethod);
133
134 try {
135 $result = $this->MakeRequest($pPath, $pMethod, $pParams);
136 } catch (Freemius_Exception $e) {
137 // Map to error object.
138 $result = (object) $e->getResult();
139 } catch (Exception $e) {
140 // Map to error object.
141 $result = (object) array(
142 'error' => (object) array(
143 'type' => 'Unknown',
144 'message' => $e->getMessage() . ' (' . $e->getFile() . ': ' . $e->getLine() . ')',
145 'code' => 'unknown',
146 'http' => 402
147 )
148 );
149 }
150
151 return $result;
152 }
153
154 public function Api($pPath, $pMethod = 'GET', $pParams = array())
155 {
156 return $this->_Api($this->CanonizePath($pPath), $pMethod, $pParams);
157 }
158
159 /**
160 * Base64 decoding that does not need to be urldecode()-ed.
161 *
162 * Exactly the same as PHP base64 encode except it uses
163 * `-` instead of `+`
164 * `_` instead of `/`
165 * No padded =
166 *
167 * @param string $input Base64UrlEncoded() string
168 *
169 * @return string
170 */
171 protected static function Base64UrlDecode($input)
172 {
173 /**
174 * IMPORTANT NOTE:
175 * This is a hack suggested by @otto42 and @greenshady from
176 * the theme's review team. The usage of base64 for API
177 * signature encoding was approved in a Slack meeting
178 * held on Tue (10/25 2016).
179 *
180 * @todo Remove this hack once the base64 error is removed from the Theme Check.
181 *
182 * @since 1.2.2
183 * @author Vova Feldman (@svovaf)
184 */
185 $fn = 'base64' . '_decode';
186 return $fn(strtr($input, '-_', '+/'));
187 }
188
189 /**
190 * Base64 encoding that does not need to be urlencode()ed.
191 *
192 * Exactly the same as base64 encode except it uses
193 * `-` instead of `+
194 * `_` instead of `/`
195 *
196 * @param string $input string
197 *
198 * @return string Base64 encoded string
199 */
200 protected static function Base64UrlEncode($input)
201 {
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 * @author Vova Feldman (@svovaf)
213 */
214 $fn = 'base64' . '_encode';
215 $str = strtr($fn($input), '+/', '-_');
216 $str = str_replace('=', '', $str);
217
218 return $str;
219 }
220 }
221 }
222