PageRenderTime 40ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/wordpress/wp-content/plugins/nextgen-gallery/freemius/includes/sdk/FreemiusBase.php

https://bitbucket.org/spirulineteam/spiruline
PHP | 188 lines | 111 code | 23 blank | 54 comment | 8 complexity | dc4787649cc94b9c7b9564d169f79a7e MD5 | raw file
Possible License(s): GPL-3.0, GPL-2.0, Apache-2.0, LGPL-2.1
  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. if ( ! defined( 'FS_API__VERSION' ) ) {
  18. define( 'FS_API__VERSION', '1' );
  19. }
  20. if ( ! defined( 'FS_SDK__PATH' ) ) {
  21. define( 'FS_SDK__PATH', dirname( __FILE__ ) );
  22. }
  23. if ( ! defined( 'FS_SDK__EXCEPTIONS_PATH' ) ) {
  24. define( 'FS_SDK__EXCEPTIONS_PATH', FS_SDK__PATH . '/Exceptions/' );
  25. }
  26. if ( ! function_exists( 'json_decode' ) ) {
  27. throw new Exception( 'Freemius needs the JSON PHP extension.' );
  28. }
  29. // Include all exception files.
  30. $exceptions = array(
  31. 'Exception',
  32. 'InvalidArgumentException',
  33. 'ArgumentNotExistException',
  34. 'EmptyArgumentException',
  35. 'OAuthException'
  36. );
  37. foreach ( $exceptions as $e ) {
  38. require_once FS_SDK__EXCEPTIONS_PATH . $e . '.php';
  39. }
  40. if ( class_exists( 'Freemius_Api_Base' ) ) {
  41. return;
  42. }
  43. abstract class Freemius_Api_Base {
  44. const VERSION = '1.0.4';
  45. const FORMAT = 'json';
  46. protected $_id;
  47. protected $_public;
  48. protected $_secret;
  49. protected $_scope;
  50. protected $_isSandbox;
  51. /**
  52. * @param string $pScope 'app', 'developer', 'plugin', 'user' or 'install'.
  53. * @param number $pID Element's id.
  54. * @param string $pPublic Public key.
  55. * @param string $pSecret Element's secret key.
  56. * @param bool $pIsSandbox Whether or not to run API in sandbox mode.
  57. */
  58. public function Init( $pScope, $pID, $pPublic, $pSecret, $pIsSandbox = false ) {
  59. $this->_id = $pID;
  60. $this->_public = $pPublic;
  61. $this->_secret = $pSecret;
  62. $this->_scope = $pScope;
  63. $this->_isSandbox = $pIsSandbox;
  64. }
  65. public function IsSandbox() {
  66. return $this->_isSandbox;
  67. }
  68. function CanonizePath( $pPath ) {
  69. $pPath = trim( $pPath, '/' );
  70. $query_pos = strpos( $pPath, '?' );
  71. $query = '';
  72. if ( false !== $query_pos ) {
  73. $query = substr( $pPath, $query_pos );
  74. $pPath = substr( $pPath, 0, $query_pos );
  75. }
  76. // Trim '.json' suffix.
  77. $format_length = strlen( '.' . self::FORMAT );
  78. $start = $format_length * ( - 1 ); //negative
  79. if ( substr( strtolower( $pPath ), $start ) === ( '.' . self::FORMAT ) ) {
  80. $pPath = substr( $pPath, 0, strlen( $pPath ) - $format_length );
  81. }
  82. switch ( $this->_scope ) {
  83. case 'app':
  84. $base = '/apps/' . $this->_id;
  85. break;
  86. case 'developer':
  87. $base = '/developers/' . $this->_id;
  88. break;
  89. case 'user':
  90. $base = '/users/' . $this->_id;
  91. break;
  92. case 'plugin':
  93. $base = '/plugins/' . $this->_id;
  94. break;
  95. case 'install':
  96. $base = '/installs/' . $this->_id;
  97. break;
  98. default:
  99. throw new Freemius_Exception( 'Scope not implemented.' );
  100. }
  101. return '/v' . FS_API__VERSION . $base .
  102. ( ! empty( $pPath ) ? '/' : '' ) . $pPath .
  103. ( ( false === strpos( $pPath, '.' ) ) ? '.' . self::FORMAT : '' ) . $query;
  104. }
  105. abstract function MakeRequest( $pCanonizedPath, $pMethod = 'GET', $pParams = array() );
  106. /**
  107. * @param string $pPath
  108. * @param string $pMethod
  109. * @param array $pParams
  110. *
  111. * @return object[]|object|null
  112. */
  113. private function _Api( $pPath, $pMethod = 'GET', $pParams = array() ) {
  114. $pMethod = strtoupper( $pMethod );
  115. try {
  116. $result = $this->MakeRequest( $pPath, $pMethod, $pParams );
  117. } catch ( Freemius_Exception $e ) {
  118. // Map to error object.
  119. $result = (object) $e->getResult();
  120. } catch ( Exception $e ) {
  121. // Map to error object.
  122. $result = (object) array(
  123. 'error' => array(
  124. 'type' => 'Unknown',
  125. 'message' => $e->getMessage() . ' (' . $e->getFile() . ': ' . $e->getLine() . ')',
  126. 'code' => 'unknown',
  127. 'http' => 402
  128. )
  129. );
  130. }
  131. return $result;
  132. }
  133. public function Api( $pPath, $pMethod = 'GET', $pParams = array() ) {
  134. return $this->_Api( $this->CanonizePath( $pPath ), $pMethod, $pParams );
  135. }
  136. /**
  137. * Base64 encoding that does not need to be urlencode()ed.
  138. * Exactly the same as base64_encode except it uses
  139. * - instead of +
  140. * _ instead of /
  141. * No padded =
  142. *
  143. * @param string $input base64UrlEncoded string
  144. *
  145. * @return string
  146. */
  147. protected static function Base64UrlDecode( $input ) {
  148. return base64_decode( strtr( $input, '-_', '+/' ) );
  149. }
  150. /**
  151. * Base64 encoding that does not need to be urlencode()ed.
  152. * Exactly the same as base64_encode except it uses
  153. * - instead of +
  154. * _ instead of /
  155. *
  156. * @param string $input string
  157. *
  158. * @return string base64Url encoded string
  159. */
  160. protected static function Base64UrlEncode( $input ) {
  161. $str = strtr( base64_encode( $input ), '+/', '-_' );
  162. $str = str_replace( '=', '', $str );
  163. return $str;
  164. }
  165. }