/modules/Dropbox/AppInfo.php

https://gitlab.com/x33n/ampache · PHP · 238 lines · 152 code · 15 blank · 71 comment · 8 complexity · b08006228ca38ac11c1a431646536ad9 MD5 · raw file

  1. <?php
  2. namespace Dropbox;
  3. /**
  4. * Your app's API key and secret.
  5. */
  6. final class AppInfo
  7. {
  8. /**
  9. * Your Dropbox <em>app key</em> (OAuth calls this the <em>consumer key</em>). You can
  10. * create an app key and secret on the <a href="http://dropbox.com/developers/apps">Dropbox developer website</a>.
  11. *
  12. * @return string
  13. */
  14. function getKey() { return $this->key; }
  15. /** @var string */
  16. private $key;
  17. /**
  18. * Your Dropbox <em>app secret</em> (OAuth calls this the <em>consumer secret</em>). You can
  19. * create an app key and secret on the <a href="http://dropbox.com/developers/apps">Dropbox developer website</a>.
  20. *
  21. * Make sure that this is kept a secret. Someone with your app secret can impesonate your
  22. * application. People sometimes ask for help on the Dropbox API forums and
  23. * copy/paste code that includes their app secret. Do not do that.
  24. *
  25. * @return string
  26. */
  27. function getSecret() { return $this->secret; }
  28. /** @var string */
  29. private $secret;
  30. /**
  31. * The set of servers your app will use. This defaults to the standard Dropbox servers
  32. * {@link Host::getDefault}.
  33. *
  34. * @return Host
  35. *
  36. * @internal
  37. */
  38. function getHost() { return $this->host; }
  39. /** @var Host */
  40. private $host;
  41. /**
  42. * Constructor.
  43. *
  44. * @param string $key
  45. * See {@link getKey()}
  46. * @param string $secret
  47. * See {@link getSecret()}
  48. */
  49. function __construct($key, $secret)
  50. {
  51. self::checkKeyArg($key);
  52. self::checkSecretArg($secret);
  53. $this->key = $key;
  54. $this->secret = $secret;
  55. // The $host parameter is sort of internal. We don't include it in the param list because
  56. // we don't want it to be included in the documentation. Use PHP arg list hacks to get at
  57. // it.
  58. $host = null;
  59. if (\func_num_args() == 3) {
  60. $host = \func_get_arg(2);
  61. Host::checkArgOrNull("host", $host);
  62. }
  63. if ($host === null) {
  64. $host = Host::getDefault();
  65. }
  66. $this->host = $host;
  67. }
  68. /**
  69. * Loads a JSON file containing information about your app. At a minimum, the file must include
  70. * the "key" and "secret" fields. Run 'php authorize.php' in the examples directory
  71. * for details about what this file should look like.
  72. *
  73. * @param string $path
  74. * Path to a JSON file
  75. *
  76. * @return AppInfo
  77. *
  78. * @throws AppInfoLoadException
  79. */
  80. static function loadFromJsonFile($path)
  81. {
  82. list($rawJson, $appInfo) = self::loadFromJsonFileWithRaw($path);
  83. return $appInfo;
  84. }
  85. /**
  86. * Loads a JSON file containing information about your app. At a minimum, the file must include
  87. * the "key" and "secret" fields. Run 'php authorize.php' in the examples directory
  88. * for details about what this file should look like.
  89. *
  90. * @param string $path
  91. * Path to a JSON file
  92. *
  93. * @return array
  94. * A list of two items. The first is a PHP array representation of the raw JSON, the second
  95. * is an AppInfo object that is the parsed version of the JSON.
  96. *
  97. * @throws AppInfoLoadException
  98. *
  99. * @internal
  100. */
  101. static function loadFromJsonFileWithRaw($path)
  102. {
  103. if (!file_exists($path)) {
  104. throw new AppInfoLoadException("File doesn't exist: \"$path\"");
  105. }
  106. $str = file_get_contents($path);
  107. $jsonArr = json_decode($str, true);
  108. if (is_null($jsonArr)) {
  109. throw new AppInfoLoadException("JSON parse error: \"$path\"");
  110. }
  111. $appInfo = self::loadFromJson($jsonArr);
  112. return array($jsonArr, $appInfo);
  113. }
  114. /**
  115. * Parses a JSON object to build an AppInfo object. If you would like to load this from a file,
  116. * use the loadFromJsonFile() method.
  117. *
  118. * @param array $jsonArr Output from json_decode($str, true)
  119. *
  120. * @return AppInfo
  121. *
  122. * @throws AppInfoLoadException
  123. */
  124. static function loadFromJson($jsonArr)
  125. {
  126. if (!is_array($jsonArr)) {
  127. throw new AppInfoLoadException("Expecting JSON object, got something else");
  128. }
  129. $requiredKeys = array("key", "secret");
  130. foreach ($requiredKeys as $key) {
  131. if (!array_key_exists($key, $jsonArr)) {
  132. throw new AppInfoLoadException("Missing field \"$key\"");
  133. }
  134. if (!is_string($jsonArr[$key])) {
  135. throw new AppInfoLoadException("Expecting field \"$key\" to be a string");
  136. }
  137. }
  138. // Check app_key and app_secret
  139. $appKey = $jsonArr["key"];
  140. $appSecret = $jsonArr["secret"];
  141. $tokenErr = self::getTokenPartError($appKey);
  142. if (!is_null($tokenErr)) {
  143. throw new AppInfoLoadException("Field \"key\" doesn't look like a valid app key: $tokenErr");
  144. }
  145. $tokenErr = self::getTokenPartError($appSecret);
  146. if (!is_null($tokenErr)) {
  147. throw new AppInfoLoadException("Field \"secret\" doesn't look like a valid app secret: $tokenErr");
  148. }
  149. // Check for the optional 'host' field
  150. if (!array_key_exists('host', $jsonArr)) {
  151. $host = null;
  152. }
  153. else {
  154. $baseHost = $jsonArr["host"];
  155. if (!is_string($baseHost)) {
  156. throw new AppInfoLoadException("Optional field \"host\" must be a string");
  157. }
  158. $api = "api-$baseHost";
  159. $content = "api-content-$baseHost";
  160. $web = "meta-$baseHost";
  161. $host = new Host($api, $content, $web);
  162. }
  163. return new AppInfo($appKey, $appSecret, $host);
  164. }
  165. /**
  166. * Use this to check that a function argument is of type <code>AppInfo</code>
  167. *
  168. * @internal
  169. */
  170. static function checkArg($argName, $argValue)
  171. {
  172. if (!($argValue instanceof self)) Checker::throwError($argName, $argValue, __CLASS__);
  173. }
  174. /**
  175. * Use this to check that a function argument is either <code>null</code> or of type
  176. * <code>AppInfo</code>.
  177. *
  178. * @internal
  179. */
  180. static function checkArgOrNull($argName, $argValue)
  181. {
  182. if ($argValue === null) return;
  183. if (!($argValue instanceof self)) Checker::throwError($argName, $argValue, __CLASS__);
  184. }
  185. /** @internal */
  186. static function getTokenPartError($s)
  187. {
  188. if ($s === null) return "can't be null";
  189. if (strlen($s) === 0) return "can't be empty";
  190. if (strstr($s, ' ')) return "can't contain a space";
  191. return null; // 'null' means "no error"
  192. }
  193. /** @internal */
  194. static function checkKeyArg($key)
  195. {
  196. $error = self::getTokenPartError($key);
  197. if ($error === null) return;
  198. throw new \InvalidArgumentException("Bad 'key': \"$key\": $error.");
  199. }
  200. /** @internal */
  201. static function checkSecretArg($secret)
  202. {
  203. $error = self::getTokenPartError($secret);
  204. if ($error === null) return;
  205. throw new \InvalidArgumentException("Bad 'secret': \"$secret\": $error.");
  206. }
  207. }