Config.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. <?php
  2. namespace Qiniu;
  3. final class Config
  4. {
  5. const SDK_VER = '7.12.0';
  6. const BLOCK_SIZE = 4194304; //4*1024*1024 分块上传块大小,该参数为接口规格,不能修改
  7. const RSF_HOST = 'rsf.qiniuapi.com';
  8. const API_HOST = 'api.qiniuapi.com';
  9. const RS_HOST = 'rs.qiniuapi.com'; //RS Host
  10. const UC_HOST = 'uc.qbox.me'; //UC Host
  11. const QUERY_REGION_HOST = 'kodo-config.qiniuapi.com';
  12. const RTCAPI_HOST = 'http://rtc.qiniuapi.com';
  13. const ARGUS_HOST = 'ai.qiniuapi.com';
  14. const CASTER_HOST = 'pili-caster.qiniuapi.com';
  15. const SMS_HOST = "https://sms.qiniuapi.com";
  16. const RTCAPI_VERSION = 'v3';
  17. const SMS_VERSION = 'v1';
  18. // Zone 空间对应的存储区域
  19. public $region;
  20. //BOOL 是否使用https域名
  21. public $useHTTPS;
  22. //BOOL 是否使用CDN加速上传域名
  23. public $useCdnDomains;
  24. /**
  25. * @var Region
  26. */
  27. public $zone;
  28. // Zone Cache
  29. private $regionCache;
  30. // UC Host
  31. private $ucHost;
  32. private $queryRegionHost;
  33. // backup UC Hosts
  34. private $backupQueryRegionHosts;
  35. // backup UC Hosts max retry time
  36. public $backupUcHostsRetryTimes;
  37. // 构造函数
  38. public function __construct(Region $z = null)
  39. {
  40. $this->zone = $z;
  41. $this->useHTTPS = false;
  42. $this->useCdnDomains = false;
  43. $this->regionCache = array();
  44. $this->ucHost = Config::UC_HOST;
  45. $this->queryRegionHost = Config::QUERY_REGION_HOST;
  46. $this->backupQueryRegionHosts = array(
  47. "uc.qbox.me",
  48. "api.qiniu.com"
  49. );
  50. $this->backupUcHostsRetryTimes = 2;
  51. }
  52. public function setUcHost($ucHost)
  53. {
  54. $this->ucHost = $ucHost;
  55. $this->setQueryRegionHost($ucHost);
  56. }
  57. public function getUcHost()
  58. {
  59. if ($this->useHTTPS === true) {
  60. $scheme = "https://";
  61. } else {
  62. $scheme = "http://";
  63. }
  64. return $scheme . $this->ucHost;
  65. }
  66. public function setQueryRegionHost($host, $backupHosts = array())
  67. {
  68. $this->queryRegionHost = $host;
  69. $this->backupQueryRegionHosts = $backupHosts;
  70. }
  71. public function getQueryRegionHost()
  72. {
  73. if ($this->useHTTPS === true) {
  74. $scheme = "https://";
  75. } else {
  76. $scheme = "http://";
  77. }
  78. return $scheme . $this->queryRegionHost;
  79. }
  80. public function setBackupQueryRegionHosts($hosts = array())
  81. {
  82. $this->backupQueryRegionHosts = $hosts;
  83. }
  84. public function getBackupQueryRegionHosts()
  85. {
  86. return $this->backupQueryRegionHosts;
  87. }
  88. public function getUpHost($accessKey, $bucket, $reqOpt = null)
  89. {
  90. $region = $this->getRegion($accessKey, $bucket, $reqOpt);
  91. if ($this->useHTTPS === true) {
  92. $scheme = "https://";
  93. } else {
  94. $scheme = "http://";
  95. }
  96. $host = $region->srcUpHosts[0];
  97. if ($this->useCdnDomains === true) {
  98. $host = $region->cdnUpHosts[0];
  99. }
  100. return $scheme . $host;
  101. }
  102. public function getUpHostV2($accessKey, $bucket, $reqOpt = null)
  103. {
  104. list($region, $err) = $this->getRegionV2($accessKey, $bucket, $reqOpt);
  105. if ($err != null) {
  106. return array(null, $err);
  107. }
  108. if ($this->useHTTPS === true) {
  109. $scheme = "https://";
  110. } else {
  111. $scheme = "http://";
  112. }
  113. $host = $region->srcUpHosts[0];
  114. if ($this->useCdnDomains === true) {
  115. $host = $region->cdnUpHosts[0];
  116. }
  117. return array($scheme . $host, null);
  118. }
  119. public function getUpBackupHost($accessKey, $bucket, $reqOpt = null)
  120. {
  121. $region = $this->getRegion($accessKey, $bucket, $reqOpt);
  122. if ($this->useHTTPS === true) {
  123. $scheme = "https://";
  124. } else {
  125. $scheme = "http://";
  126. }
  127. $host = $region->cdnUpHosts[0];
  128. if ($this->useCdnDomains === true) {
  129. $host = $region->srcUpHosts[0];
  130. }
  131. return $scheme . $host;
  132. }
  133. public function getUpBackupHostV2($accessKey, $bucket, $reqOpt = null)
  134. {
  135. list($region, $err) = $this->getRegionV2($accessKey, $bucket, $reqOpt);
  136. if ($err != null) {
  137. return array(null, $err);
  138. }
  139. if ($this->useHTTPS === true) {
  140. $scheme = "https://";
  141. } else {
  142. $scheme = "http://";
  143. }
  144. $host = $region->cdnUpHosts[0];
  145. if ($this->useCdnDomains === true) {
  146. $host = $region->srcUpHosts[0];
  147. }
  148. return array($scheme . $host, null);
  149. }
  150. public function getRsHost($accessKey, $bucket, $reqOpt = null)
  151. {
  152. $region = $this->getRegion($accessKey, $bucket, $reqOpt);
  153. if ($this->useHTTPS === true) {
  154. $scheme = "https://";
  155. } else {
  156. $scheme = "http://";
  157. }
  158. return $scheme . $region->rsHost;
  159. }
  160. public function getRsHostV2($accessKey, $bucket, $reqOpt = null)
  161. {
  162. list($region, $err) = $this->getRegionV2($accessKey, $bucket, $reqOpt);
  163. if ($err != null) {
  164. return array(null, $err);
  165. }
  166. if ($this->useHTTPS === true) {
  167. $scheme = "https://";
  168. } else {
  169. $scheme = "http://";
  170. }
  171. return array($scheme . $region->rsHost, null);
  172. }
  173. public function getRsfHost($accessKey, $bucket, $reqOpt = null)
  174. {
  175. $region = $this->getRegion($accessKey, $bucket, $reqOpt);
  176. if ($this->useHTTPS === true) {
  177. $scheme = "https://";
  178. } else {
  179. $scheme = "http://";
  180. }
  181. return $scheme . $region->rsfHost;
  182. }
  183. public function getRsfHostV2($accessKey, $bucket, $reqOpt = null)
  184. {
  185. list($region, $err) = $this->getRegionV2($accessKey, $bucket, $reqOpt);
  186. if ($err != null) {
  187. return array(null, $err);
  188. }
  189. if ($this->useHTTPS === true) {
  190. $scheme = "https://";
  191. } else {
  192. $scheme = "http://";
  193. }
  194. return array($scheme . $region->rsfHost, null);
  195. }
  196. public function getIovipHost($accessKey, $bucket, $reqOpt = null)
  197. {
  198. $region = $this->getRegion($accessKey, $bucket, $reqOpt);
  199. if ($this->useHTTPS === true) {
  200. $scheme = "https://";
  201. } else {
  202. $scheme = "http://";
  203. }
  204. return $scheme . $region->iovipHost;
  205. }
  206. public function getIovipHostV2($accessKey, $bucket, $reqOpt = null)
  207. {
  208. list($region, $err) = $this->getRegionV2($accessKey, $bucket, $reqOpt);
  209. if ($err != null) {
  210. return array(null, $err);
  211. }
  212. if ($this->useHTTPS === true) {
  213. $scheme = "https://";
  214. } else {
  215. $scheme = "http://";
  216. }
  217. return array($scheme . $region->iovipHost, null);
  218. }
  219. public function getApiHost($accessKey, $bucket, $reqOpt = null)
  220. {
  221. $region = $this->getRegion($accessKey, $bucket, $reqOpt);
  222. if ($this->useHTTPS === true) {
  223. $scheme = "https://";
  224. } else {
  225. $scheme = "http://";
  226. }
  227. return $scheme . $region->apiHost;
  228. }
  229. public function getApiHostV2($accessKey, $bucket, $reqOpt = null)
  230. {
  231. list($region, $err) = $this->getRegionV2($accessKey, $bucket, $reqOpt);
  232. if ($err != null) {
  233. return array(null, $err);
  234. }
  235. if ($this->useHTTPS === true) {
  236. $scheme = "https://";
  237. } else {
  238. $scheme = "http://";
  239. }
  240. return array($scheme . $region->apiHost, null);
  241. }
  242. /**
  243. * 从缓存中获取区域
  244. *
  245. * @param string $cacheId 缓存 ID
  246. * @return null|Region
  247. */
  248. private function getRegionCache($cacheId)
  249. {
  250. if (isset($this->regionCache[$cacheId]) &&
  251. isset($this->regionCache[$cacheId]["deadline"]) &&
  252. time() < $this->regionCache[$cacheId]["deadline"]) {
  253. return $this->regionCache[$cacheId]["region"];
  254. }
  255. return null;
  256. }
  257. /**
  258. * 将区域设置到缓存中
  259. *
  260. * @param string $cacheId 缓存 ID
  261. * @param Region $region 缓存 ID
  262. * @return void
  263. */
  264. private function setRegionCache($cacheId, $region)
  265. {
  266. $this->regionCache[$cacheId] = array(
  267. "region" => $region,
  268. );
  269. if (isset($region->ttl)) {
  270. $this->regionCache[$cacheId]["deadline"] = time() + $region->ttl;
  271. }
  272. }
  273. /**
  274. * 从缓存中获取区域
  275. *
  276. * @param string $accessKey
  277. * @param string $bucket
  278. * @return Region
  279. *
  280. * @throws \Exception
  281. */
  282. private function getRegion($accessKey, $bucket, $reqOpt = null)
  283. {
  284. if (isset($this->zone)) {
  285. return $this->zone;
  286. }
  287. $cacheId = "$accessKey:$bucket";
  288. $regionCache = $this->getRegionCache($cacheId);
  289. if ($regionCache) {
  290. return $regionCache;
  291. }
  292. $region = Zone::queryZone(
  293. $accessKey,
  294. $bucket,
  295. $this->getQueryRegionHost(),
  296. $this->getBackupQueryRegionHosts(),
  297. $this->backupUcHostsRetryTimes,
  298. $reqOpt
  299. );
  300. if (is_array($region)) {
  301. list($region, $err) = $region;
  302. if ($err != null) {
  303. throw new \Exception($err->message());
  304. }
  305. }
  306. $this->setRegionCache($cacheId, $region);
  307. return $region;
  308. }
  309. private function getRegionV2($accessKey, $bucket, $reqOpt = null)
  310. {
  311. if (isset($this->zone)) {
  312. return array($this->zone, null);
  313. }
  314. $cacheId = "$accessKey:$bucket";
  315. $regionCache = $this->getRegionCache($cacheId);
  316. if (isset($regionCache)) {
  317. return array($regionCache, null);
  318. }
  319. $region = Zone::queryZone(
  320. $accessKey,
  321. $bucket,
  322. $this->getQueryRegionHost(),
  323. $this->getBackupQueryRegionHosts(),
  324. $this->backupUcHostsRetryTimes,
  325. $reqOpt
  326. );
  327. if (is_array($region)) {
  328. list($region, $err) = $region;
  329. return array($region, $err);
  330. }
  331. $this->setRegionCache($cacheId, $region);
  332. return array($region, null);
  333. }
  334. }