PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/upload/src/windid/service/base/WindidUtility.php

https://gitlab.com/wuhang2003/phpwind
PHP | 133 lines | 101 code | 9 blank | 23 comment | 13 complexity | b9879c567a6637ce95d7aa8531300340 MD5 | raw file
  1. <?php
  2. /**
  3. * Windid工具库
  4. *
  5. * @author xiaoxia.xu <xiaoxia.xuxx@aliyun-inc.com> 2010-11-2
  6. * @license http://www.phpwind.com
  7. * @version $Id: WindidUtility.php 32085 2014-08-20 08:48:50Z gao.wanggao $
  8. * @package Windid.library
  9. */
  10. class WindidUtility {
  11. /**
  12. * 生成密码
  13. *
  14. * @param string $password 源密码
  15. * @param string $salt
  16. * @return string
  17. */
  18. public static function buildPassword($password, $salt) {
  19. return md5(md5($password) . $salt);
  20. }
  21. /**
  22. * 安全问题加密
  23. *
  24. * @param string $question
  25. * @param string $answer
  26. * @return bool
  27. */
  28. public static function buildQuestion($question, $answer) {
  29. return substr(md5($question . $answer), 8, 8);
  30. }
  31. public static function appKey($apiId, $time, $secretkey, $get, $post) {
  32. $array = array('windidkey', 'clientid', 'time', '_json', 'jcallback', 'csrf_token', 'Filename', 'Upload', 'token', '__data');
  33. $str = '';
  34. ksort($get);
  35. ksort($post);
  36. foreach ($get AS $k=>$v) {
  37. if (in_array($k, $array)) continue;
  38. $str .=$k.$v;
  39. }
  40. foreach ($post AS $k=>$v) {
  41. if (in_array($k, $array)) continue;
  42. $str .=$k.$v;
  43. }
  44. return md5(md5($apiId.'||'.$secretkey).$time.$str);
  45. }
  46. public static function buildRequest($url, $params = array(), $isreturn = true, $timeout = 10, $method = 'post') {
  47. $request = Wind::getComponent('httptransfer', array($url, $timeout));
  48. $request->setWaitResponse($isreturn);
  49. if ($method == 'post') {
  50. if (!$params) $params = array('__data' => '1');//兼容部分版本post content不能为空的错误
  51. return $request->post($params);
  52. } else {
  53. return $request->get($params);
  54. }
  55. }
  56. public static function buildMultiRequest($urls, $params = array()) {
  57. $result = array();
  58. foreach ($urls as $k => $url) {
  59. $request = Wind::getComponent('httptransfer', array($url));
  60. $result[$k] = $request->post($params[$k]);
  61. }
  62. return $result;
  63. }
  64. public static function uploadRequest($url, $file, $timeout = 30) {
  65. if (function_exists('curl_init')) {
  66. $curl = curl_init($url);
  67. curl_setopt($curl,CURLOPT_POST, true);
  68. curl_setopt($curl,CURLOPT_POSTFIELDS, array('FileData'=>'@'.$file));
  69. curl_setopt($curl,CURLOPT_TIMEOUT, $timeout);
  70. curl_setopt($curl,CURLOPT_FOLLOWLOCATION, false);
  71. curl_setopt($curl,CURLOPT_RETURNTRANSFER, true);
  72. $response = curl_exec($curl);
  73. curl_close($curl);
  74. return $response;
  75. } elseif (function_exists('fsockopen')) {
  76. $urlArr = parse_url($url);
  77. $port = isset($urlArr['port']) ? $urlArr['port'] : 80;
  78. $boundary = "---------------------".substr(md5(rand(0,32000)),0,10);
  79. $header = "POST ".$urlArr['path'].'?'. $urlArr['query']." HTTP/1.0\r\n";
  80. $header .= "Host: ".$urlArr['host']."\r\n";
  81. $header .= "Content-type: multipart/form-data, boundary=".$boundary."\r\n";
  82. if (!file_exists($file)) return false;
  83. $imageInfo = @getimagesize($file);
  84. $exts = array('1'=>'gif', '2'=>'jpg', '3'=>'png');
  85. if (!isset($exts[$imageInfo[2]])) continue;
  86. $ext = $exts[$imageInfo[2]];
  87. $filename = rand(1000,9999). '.'.$ext;
  88. $data = '';
  89. $data .= "--$boundary\r\n";
  90. $data .="Content-Disposition: form-data; name=\"FileData\"; filename=\"".$filename."\"\r\n";
  91. $data .= "Content-Type: ".$imageInfo['mime']."\r\n\r\n";
  92. $data .= WindFile::read($file)."\r\n";
  93. $data .="--$boundary--\r\n";
  94. $header .= "Content-length: " . strlen($data) . "\r\n\r\n";
  95. $fp = fsockopen($urlArr['host'], $port);
  96. fputs($fp, $header.$data);
  97. $response = '';
  98. while (!feof($fp)) {
  99. $response .= fgets($fp, 128);
  100. }
  101. fclose($fp);
  102. preg_match("/Content-Length:.?(\d+)/", $response, $matches);
  103. if (isset($matches[1])) {
  104. $response = substr($response, strlen($response) - intval($matches[1]));
  105. }
  106. return $response;
  107. } else {
  108. return false;
  109. }
  110. }
  111. public static function buildClientUrl($url, $notiFile) {
  112. $url = $url . '/' .$notiFile;
  113. $_url = parse_url($url);
  114. $query = isset($_url['query']) ? '&' : '?';
  115. return $url. $query;
  116. }
  117. public static function result($result) {
  118. if ($result instanceof WindidError) {
  119. return $result->getCode();
  120. }
  121. return $result ? WindidError::SUCCESS : WindidError::FAIL;
  122. }
  123. }