Freemius_Api_Base

The NextGEN Gallery Freemius Api Base class.

Defined (1)

The class is defined in the following location(s).

/freemius/includes/sdk/FreemiusBase.php  
  1. abstract class Freemius_Api_Base { 
  2. const VERSION = '1.0.4'; 
  3. const FORMAT = 'json'; 
  4.  
  5. protected $_id; 
  6. protected $_public; 
  7. protected $_secret; 
  8. protected $_scope; 
  9. protected $_isSandbox; 
  10.  
  11. /** 
  12. * @param string $pScope 'app', 'developer', 'plugin', 'user' or 'install'. 
  13. * @param number $pID Element's id. 
  14. * @param string $pPublic Public key. 
  15. * @param string $pSecret Element's secret key. 
  16. * @param bool $pIsSandbox Whether or not to run API in sandbox mode. 
  17. */ 
  18. public function Init( $pScope, $pID, $pPublic, $pSecret, $pIsSandbox = false ) { 
  19. $this->_id = $pID; 
  20. $this->_public = $pPublic; 
  21. $this->_secret = $pSecret; 
  22. $this->_scope = $pScope; 
  23. $this->_isSandbox = $pIsSandbox; 
  24.  
  25. public function IsSandbox() { 
  26. return $this->_isSandbox; 
  27.  
  28. function CanonizePath( $pPath ) { 
  29. $pPath = trim( $pPath, '/' ); 
  30. $query_pos = strpos( $pPath, '?' ); 
  31. $query = ''; 
  32.  
  33. if ( false !== $query_pos ) { 
  34. $query = substr( $pPath, $query_pos ); 
  35. $pPath = substr( $pPath, 0, $query_pos ); 
  36.  
  37. // Trim '.json' suffix. 
  38. $format_length = strlen( '.' . self::FORMAT ); 
  39. $start = $format_length * ( - 1 ); //negative 
  40. if ( substr( strtolower( $pPath ), $start ) === ( '.' . self::FORMAT ) ) { 
  41. $pPath = substr( $pPath, 0, strlen( $pPath ) - $format_length ); 
  42.  
  43. switch ( $this->_scope ) { 
  44. case 'app': 
  45. $base = '/apps/' . $this->_id; 
  46. break; 
  47. case 'developer': 
  48. $base = '/developers/' . $this->_id; 
  49. break; 
  50. case 'user': 
  51. $base = '/users/' . $this->_id; 
  52. break; 
  53. case 'plugin': 
  54. $base = '/plugins/' . $this->_id; 
  55. break; 
  56. case 'install': 
  57. $base = '/installs/' . $this->_id; 
  58. break; 
  59. default: 
  60. throw new Freemius_Exception( 'Scope not implemented.' ); 
  61.  
  62. return '/v' . FS_API__VERSION . $base . 
  63. ( ! empty( $pPath ) ? '/' : '' ) . $pPath . 
  64. ( ( false === strpos( $pPath, '.' ) ) ? '.' . self::FORMAT : '' ) . $query; 
  65.  
  66. abstract function MakeRequest( $pCanonizedPath, $pMethod = 'GET', $pParams = array() ); 
  67.  
  68. /** 
  69. * @param string $pPath 
  70. * @param string $pMethod 
  71. * @param array $pParams 
  72. * @return object[]|object|null 
  73. */ 
  74. private function _Api( $pPath, $pMethod = 'GET', $pParams = array() ) { 
  75. $pMethod = strtoupper( $pMethod ); 
  76.  
  77. try { 
  78. $result = $this->MakeRequest( $pPath, $pMethod, $pParams ); 
  79. } catch ( Freemius_Exception $e ) { 
  80. // Map to error object. 
  81. $result = (object) $e->getResult(); 
  82. } catch ( Exception $e ) { 
  83. // Map to error object. 
  84. $result = (object) array( 
  85. 'error' => array( 
  86. 'type' => 'Unknown',  
  87. 'message' => $e->getMessage() . ' (' . $e->getFile() . ': ' . $e->getLine() . ')',  
  88. 'code' => 'unknown',  
  89. 'http' => 402 
  90. ); 
  91.  
  92. return $result; 
  93.  
  94. public function Api( $pPath, $pMethod = 'GET', $pParams = array() ) { 
  95. return $this->_Api( $this->CanonizePath( $pPath ), $pMethod, $pParams ); 
  96.  
  97. /** 
  98. * Base64 encoding that does not need to be urlencode()ed. 
  99. * Exactly the same as base64_encode except it uses 
  100. * - instead of + 
  101. * _ instead of / 
  102. * No padded = 
  103. * @param string $input base64UrlEncoded string 
  104. * @return string 
  105. */ 
  106. protected static function Base64UrlDecode( $input ) { 
  107. return base64_decode( strtr( $input, '-_', '+/' ) ); 
  108.  
  109. /** 
  110. * Base64 encoding that does not need to be urlencode()ed. 
  111. * Exactly the same as base64_encode except it uses 
  112. * - instead of + 
  113. * _ instead of / 
  114. * @param string $input string 
  115. * @return string base64Url encoded string 
  116. */ 
  117. protected static function Base64UrlEncode( $input ) { 
  118. $str = strtr( base64_encode( $input ), '+/', '-_' ); 
  119. $str = str_replace( '=', '', $str ); 
  120.  
  121. return $str; 
  122.