/wordpress/wp-content/plugins/worker/src/Dropbox/AppInfo.php

https://github.com/kronda/kronda · PHP · 237 lines · 151 code · 15 blank · 71 comment · 8 complexity · 438442d68f9d2813c124c863112cb269 MD5 · raw file

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