AnalysisController.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <?php
  2. namespace addons\Wechat\merchant\controllers;
  3. use Yii;
  4. use yii\web\Controller;
  5. use yii\filters\AccessControl;
  6. use linslin\yii2\curl;
  7. use yii\web\NotFoundHttpException;
  8. /**
  9. * Class AnalysisController
  10. * @package addons\Wechat\merchant\controllers
  11. * @author jianyan74 <751393839@qq.com>
  12. */
  13. class AnalysisController extends Controller
  14. {
  15. /**
  16. * 行为控制
  17. *
  18. * @return array
  19. */
  20. public function behaviors()
  21. {
  22. return [
  23. 'access' => [
  24. 'class' => AccessControl::class,
  25. 'rules' => [
  26. [
  27. 'allow' => true,
  28. 'roles' => ['@'],// 登录
  29. ],
  30. ],
  31. ],
  32. ];
  33. }
  34. /**
  35. * @throws \Exception
  36. */
  37. public function actionImage()
  38. {
  39. $imgUrl = Yii::$app->request->get('attach');
  40. $imgUrl = str_replace("&amp;", "&", htmlspecialchars($imgUrl));
  41. // http开头验证
  42. if (strpos($imgUrl, "http") !== 0) {
  43. throw new NotFoundHttpException('不是一个http地址');
  44. }
  45. preg_match('/(^https?:\/\/[^:\/]+)/', $imgUrl, $matches);
  46. $host_with_protocol = count($matches) > 1 ? $matches[1] : '';
  47. // 判断是否是合法 url
  48. if (!filter_var($host_with_protocol, FILTER_VALIDATE_URL)) {
  49. throw new NotFoundHttpException('Url不合法');
  50. }
  51. // 获取请求头并检测死链
  52. $heads = get_headers($imgUrl, 1);
  53. if (!(stristr($heads[0], "200") && stristr($heads[0], "OK"))) {
  54. throw new NotFoundHttpException('文件获取失败');
  55. }
  56. // Content-Type验证)
  57. if (!isset($heads['Content-Type']) || !stristr($heads['Content-Type'], "image")) {
  58. throw new NotFoundHttpException('格式验证失败');
  59. }
  60. $curl = new curl\Curl();
  61. $response = $curl->get($imgUrl);
  62. header('Content-Type:image/jpg');
  63. echo $response;
  64. exit();
  65. }
  66. }
粤ICP备19079148号