PageRenderTime 47ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/simple-facebook-twitter-widget/lib/freemius/includes/sdk/FreemiusBase.php

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