Ampache PHP Cross Reference Groupware Applications

Source: /modules/Dropbox/AppInfo.php - 238 lines - 7213 bytes - Summary - Text - Print

Description: Your app's API key and secret.

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

title

Description

title

Description

title

Description

title

title

Body