1 | <?php |
||
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() ) { |
||
151 | |||
152 | public function Api( $pPath, $pMethod = 'GET', $pParams = array() ) { |
||
155 | |||
156 | /** |
||
157 | * Base64 decoding that does not need to be urldecode()-ed. |
||
158 | * |
||
159 | * Exactly the same as PHP base64 encode except it uses |
||
160 | * `-` instead of `+` |
||
161 | * `_` instead of `/` |
||
162 | * No padded = |
||
163 | * |
||
164 | * @param string $input Base64UrlEncoded() string |
||
165 | * |
||
166 | * @return string |
||
167 | */ |
||
168 | protected static function Base64UrlDecode( $input ) { |
||
184 | |||
185 | /** |
||
186 | * Base64 encoding that does not need to be urlencode()ed. |
||
187 | * |
||
188 | * Exactly the same as base64 encode except it uses |
||
189 | * `-` instead of `+ |
||
190 | * `_` instead of `/` |
||
191 | * |
||
192 | * @param string $input string |
||
193 | * |
||
194 | * @return string Base64 encoded string |
||
195 | */ |
||
196 | protected static function Base64UrlEncode( $input ) { |
||
215 | } |
||
216 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.