auth.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. require_once(__DIR__.'/SAASAPIClient.php');
  3. require_once(__DIR__.'/SAASAPIServer.php');
  4. define("_FANWE_SAAS_LICENSE_AUTH_URL", "http://service.yun.fanwe.com/license/get");
  5. define("_FANWE_SAAS_LICENSE_ROOT_DIR", __DIR__."/licenses/");
  6. function _fsauth_failAndExit($errmsg)
  7. {
  8. echo $errmsg;
  9. exit;
  10. }
  11. function _fsauth_findLicenseFile()
  12. {
  13. // 获取域名信息
  14. $domain = $_SERVER["HTTP_HOST"];
  15. // 直接查找当前域名对应子目录下的License文件是否存在,若存在则返回License文件路径,否则继续
  16. $licfile = _FANWE_SAAS_LICENSE_ROOT_DIR.$domain.'/license';
  17. if (file_exists($licfile)) {
  18. return $licfile;
  19. }
  20. // 查找通配子域名目录,只要找到匹配的License文件,就认为是当前域名的授权文件,不管License文件中是否包含该域名
  21. $licfile = '';
  22. $fanwe = new Fanwe;
  23. $fixedDomain = $domain;
  24. do {
  25. // 获取子域名值
  26. $dotpos = strpos($fixedDomain, '.');
  27. if ($dotpos === false) {
  28. break;
  29. }
  30. $fixedDomain = trim(substr($fixedDomain, $dotpos+1));
  31. if ($fixedDomain == '') {
  32. break;
  33. }
  34. // 根据通配子域名查找
  35. $tempfile = _FANWE_SAAS_LICENSE_ROOT_DIR.'^.'.$fixedDomain.'/license';
  36. if (file_exists($tempfile)) {
  37. $licfile = $tempfile;
  38. }
  39. } while(empty($licfile));
  40. // 通配域名目录没找到时,在到License根目录查找,是否存在license文件
  41. if ($licfile == '' && file_exists(_FANWE_SAAS_LICENSE_ROOT_DIR.'license')) {
  42. $licfile = _FANWE_SAAS_LICENSE_ROOT_DIR.'license';
  43. }
  44. // 返回结果
  45. return $licfile;
  46. }
  47. function _fsauth_getLicenseFile()
  48. {
  49. // 从本地查找匹配当前域名的License文件,若找到且存在则返回Liccense文件路径,否则继续
  50. $licfile = _fsauth_findLicenseFile();
  51. if ($licfile != '' && file_exists($licfile)) {
  52. return $licfile;
  53. }
  54. // 判断是否存在公共的接口证书文件,若不存在则返回空路径,否则继续(获取最新授权文件)
  55. $keyfile = __DIR__.'/saasapi.key';
  56. if (!file_exists($keyfile)) {
  57. return '';
  58. }
  59. // 到SAAS基础平台获取最新的授权文件
  60. $domain = $_SERVER["HTTP_HOST"];
  61. $keystr = file_get_contents($keyfile);
  62. $keyinfo = json_decode($keystr, true);
  63. $appid = array_key_exists('appid', $keyinfo) ? $keyinfo['appid'] : '';
  64. $appsecret = array_key_exists('appsecret', $keyinfo) ? $keyinfo['appsecret'] : '';
  65. $client = new SAASAPIClient($appid, $appsecret);
  66. $ret = $client->invoke(_FANWE_SAAS_LICENSE_AUTH_URL, array('domain'=>$domain));
  67. if (empty($ret) || $ret['errcode'] != 0) {
  68. _fsauth_failAndExit('The domain is not licensed, Online license error: '.$ret['errmsg']);
  69. return '';
  70. }
  71. // 保存授权文件(保存到SAAS服务端返回的授权域名目录下)
  72. $retdata = $ret['data'];
  73. $licenseDomain = $retdata['domain'];
  74. $licenseContent = $retdata['license'];
  75. if (strpos($licenseDomain, '*.') === 0) { // 替换掉域名中的星号
  76. $licenseDomain = str_replace('*', '^', $licenseDomain); // 替换掉星号
  77. }
  78. $licpath = _FANWE_SAAS_LICENSE_ROOT_DIR.$licenseDomain.'/';
  79. $licfile = $licpath.'license';
  80. if (!file_exists($licpath)) {
  81. mkdir($licpath, 0777, true);
  82. }
  83. file_put_contents($licfile, $licenseContent);
  84. // 返回
  85. return $licfile;
  86. }
  87. function _fsauth_strEndWith($haystack, $needle)
  88. {
  89. $length = strlen($needle);
  90. if ($length == 0) {
  91. return true;
  92. }
  93. return (substr($haystack, -$length) === $needle);
  94. }
  95. // 进行授权认证
  96. // 1. 获取授权文件路径,授权文件不存在时,根据情况到SAAS基础平台获取
  97. $licfile = _fsauth_getLicenseFile();
  98. if ($licfile == '' || !file_exists($licfile)) {
  99. _fsauth_failAndExit('The domain is not licensed!');
  100. return;
  101. }
  102. // 2. 进行授权判断
  103. define("LICENSE_PATH", dirname($licfile).'/');
  104. $fanwe = new Fanwe;
  105. $rs = $fanwe->init();