From fea72edf0314677b6d55dc879c9e1e46d4a463ca Mon Sep 17 00:00:00 2001 From: overtrue Date: Wed, 17 Feb 2016 18:04:00 +0800 Subject: [PATCH 1/6] CS. --- src/Staff/MessageBuilder.php | 2 +- src/Support/Collection.php | 7 +------ 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/src/Staff/MessageBuilder.php b/src/Staff/MessageBuilder.php index 98e18741e..2a5df3ef8 100644 --- a/src/Staff/MessageBuilder.php +++ b/src/Staff/MessageBuilder.php @@ -72,7 +72,7 @@ public function __construct(Staff $staff) /** * Set message to send. * - * @param AbstractMessage $message + * @param string|AbstractMessage $message * * @return MessageBuilder * diff --git a/src/Support/Collection.php b/src/Support/Collection.php index 8705f6270..63cd50680 100644 --- a/src/Support/Collection.php +++ b/src/Support/Collection.php @@ -30,12 +30,7 @@ /** * Class Collection. */ -class Collection implements - ArrayAccess, - Countable, - IteratorAggregate, - JsonSerializable, - Serializable +class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSerializable, Serializable { /** * The collection data. From 92c0e2b7db2b36b53c088eb9b17ed5a659a99205 Mon Sep 17 00:00:00 2001 From: overtrue Date: Thu, 18 Feb 2016 09:50:59 +0800 Subject: [PATCH 2/6] Link. --- CONTRIBUTING.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index b0660179c..0816fbf51 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -72,4 +72,4 @@ EasyWeChat 是一个开源的,社区驱动的项目。 ## 报告 Bug -当你在使用过程中遇到问题,请查阅 [疑难解答](troubleshooting.html) 或者在这里提问 [GitHub](https://github.com/overtrue/wechat/issues). 如果还是不能解决你的问题,请到 GitHub 联系我们。 +当你在使用过程中遇到问题,请查阅 [疑难解答](http://easywechat.org/docs/zh-cn/troubleshooting.html) 或者在这里提问 [GitHub](https://github.com/overtrue/wechat/issues). 如果还是不能解决你的问题,请到 GitHub 联系我们。 From c21b19b8604809c042e4fa4c6c6daee66dccec50 Mon Sep 17 00:00:00 2001 From: overtrue Date: Thu, 18 Feb 2016 12:15:49 +0800 Subject: [PATCH 3/6] Issue template. --- CONTRIBUTING.md => .github/CONTRIBUTING.md | 0 .github/ISSUE_TEMPLATE.md | 9 +++++++++ 2 files changed, 9 insertions(+) rename CONTRIBUTING.md => .github/CONTRIBUTING.md (100%) create mode 100644 .github/ISSUE_TEMPLATE.md diff --git a/CONTRIBUTING.md b/.github/CONTRIBUTING.md similarity index 100% rename from CONTRIBUTING.md rename to .github/CONTRIBUTING.md diff --git a/.github/ISSUE_TEMPLATE.md b/.github/ISSUE_TEMPLATE.md new file mode 100644 index 000000000..65ae6424c --- /dev/null +++ b/.github/ISSUE_TEMPLATE.md @@ -0,0 +1,9 @@ +## 我用的环境 + +PHP 版本: +overtrue/wechat 版本: + +## 问题及现象 + +描述你的问题现象,报错贴截图粘贴或者贴具体信息 + From ceb7a5566c781f35ef892d5eef7ca4a27b710e2a Mon Sep 17 00:00:00 2001 From: overtrue Date: Thu, 18 Feb 2016 22:12:21 +0800 Subject: [PATCH 4/6] Fixbug #290 --- src/Material/Material.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/Material/Material.php b/src/Material/Material.php index 8a1bb36af..07ecdbd9a 100644 --- a/src/Material/Material.php +++ b/src/Material/Material.php @@ -181,6 +181,11 @@ public function get($mediaId) } $json = $this->getHttp()->parseJSON($response); + + // XXX: 微信开发这帮混蛋,尼玛文件二进制输出不带header,简直日了!!! + if (!$json) { + return $response->getBody(); + } $this->checkAndThrow($json); From 8227157af567f0d5919bc334b65deeb76425facd Mon Sep 17 00:00:00 2001 From: overtrue Date: Fri, 19 Feb 2016 23:06:00 +0800 Subject: [PATCH 5/6] Improve file extension judgment. #290 --- src/Material/Temporary.php | 12 ++----- src/Support/File.php | 74 ++++++++++++++++++++++---------------- 2 files changed, 46 insertions(+), 40 deletions(-) diff --git a/src/Material/Temporary.php b/src/Material/Temporary.php index 5cda75e3c..ebd8a93f9 100644 --- a/src/Material/Temporary.php +++ b/src/Material/Temporary.php @@ -80,16 +80,8 @@ public function download($mediaId, $directory, $filename = '') public function getStream($mediaId) { $response = $this->getHttp()->get(self::API_GET, ['media_id' => $mediaId]); - - foreach ($response->getHeader('Content-Type') as $mime) { - if (preg_match('/(image|video|audio)/i', $mime)) { - return $response->getBody(); - } - } - - $this->checkAndThrow($this->getHttp()->parseJSON($response)); - - throw new RuntimeException('Unknown API response.'.$response->getBody()); + + return $response->getBody(); } /** diff --git a/src/Support/File.php b/src/Support/File.php index b7d7eec33..7981d91cf 100644 --- a/src/Support/File.php +++ b/src/Support/File.php @@ -33,29 +33,6 @@ class File * @var array */ protected static $extensionMap = array( - 'application/msword' => '.doc', - 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' => '.docx', - 'application/rtf' => '.rtf', - 'application/vnd.ms-excel' => '.xls', - 'application/x-excel' => '.xls', - 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' => '.xlsx', - 'application/vnd.ms-powerpoint' => '.ppt', - 'application/vnd.openxmlformats-officedocument.presentationml.presentation' => '.pptx', - 'application/vnd.ms-powerpoint' => '.pps', - 'application/vnd.openxmlformats-officedocument.presentationml.slideshow' => '.ppsx', - 'application/pdf' => '.pdf', - 'application/x-shockwave-flash' => '.swf', - 'application/x-msdownload' => '.dll', - 'application/octet-stream' => '.exe', - 'application/octet-stream' => '.msi', - 'application/octet-stream' => '.chm', - 'application/octet-stream' => '.cab', - 'application/octet-stream' => '.ocx', - 'application/octet-stream' => '.rar', - 'application/x-tar' => '.tar', - 'application/x-compressed' => '.tgz', - 'application/x-zip-compressed' => '.zip', - 'application/x-compress' => '.z', 'audio/wav' => '.wav', 'audio/x-ms-wma' => '.wma', 'video/x-ms-wmv' => '.wmv', @@ -69,14 +46,31 @@ class File 'image/png' => '.png', 'image/tiff' => '.tiff', 'image/jpeg' => '.jpg', - 'text/plain' => '.txt', - 'text/xml' => '.xml', - 'text/html' => '.html', - 'text/css' => '.css', - 'text/javascript' => '.js', - 'message/rfc822' => '.mhtml', ); + /** + * File header signatures. + * + * @var array + */ + protected static $signatures = [ + 'ffd8ff' => '.jpg', + '424d' => '.bmp', + '47494638' => '.gif', + '89504e47' => '.png', + '494433' => '.mp3', + 'fffb' => '.mp3', + 'fff3' => '.mp3', + '3026b2758e66cf11' => '.wma', + '52494646' => '.wav', + '57415645' => '.wav', + '41564920' => '.avi', + '000001ba' => '.mpg', + '000001b3' => '.mpg', + '2321414d52' => '.amr', + ]; + + /** * Return steam extension. * @@ -90,6 +84,26 @@ public static function getStreamExt($stream) $mime = strstr($finfo->buffer($stream), ';', true); - return isset(self::$extensionMap[$mime]) ? self::$extensionMap[$mime] : ''; + return isset(self::$extensionMap[$mime]) ? self::$extensionMap[$mime] : self::getExtBySignature($stream); + } + + /** + * Get file extension by file header signature. + * + * @param string $stream + * + * @return string + */ + public static function getExtBySignature($stream) + { + $prefix = strval(bin2hex(mb_strcut($stream, 0, 10))); + + foreach (self::$signatures as $signature => $extension) { + if (0 === strpos($prefix, strval($signature))) { + return $extension; + } + } + + return ""; } } From c4ba6025d1bb6d5808f4fade2bbf73c128ee07c5 Mon Sep 17 00:00:00 2001 From: overtrue Date: Fri, 19 Feb 2016 10:38:44 -0500 Subject: [PATCH 6/6] Applied fixes from StyleCI --- src/Material/Material.php | 2 +- src/Material/Temporary.php | 3 +-- src/Support/File.php | 11 +++++------ 3 files changed, 7 insertions(+), 9 deletions(-) diff --git a/src/Material/Material.php b/src/Material/Material.php index 07ecdbd9a..e92aea384 100644 --- a/src/Material/Material.php +++ b/src/Material/Material.php @@ -181,7 +181,7 @@ public function get($mediaId) } $json = $this->getHttp()->parseJSON($response); - + // XXX: 微信开发这帮混蛋,尼玛文件二进制输出不带header,简直日了!!! if (!$json) { return $response->getBody(); diff --git a/src/Material/Temporary.php b/src/Material/Temporary.php index ebd8a93f9..d6597a16a 100644 --- a/src/Material/Temporary.php +++ b/src/Material/Temporary.php @@ -22,7 +22,6 @@ use EasyWeChat\Core\AbstractAPI; use EasyWeChat\Core\Exceptions\InvalidArgumentException; -use EasyWeChat\Core\Exceptions\RuntimeException; use EasyWeChat\Support\File; /** @@ -80,7 +79,7 @@ public function download($mediaId, $directory, $filename = '') public function getStream($mediaId) { $response = $this->getHttp()->get(self::API_GET, ['media_id' => $mediaId]); - + return $response->getBody(); } diff --git a/src/Support/File.php b/src/Support/File.php index 7981d91cf..f06f8d7f7 100644 --- a/src/Support/File.php +++ b/src/Support/File.php @@ -55,22 +55,21 @@ class File */ protected static $signatures = [ 'ffd8ff' => '.jpg', - '424d' => '.bmp', + '424d' => '.bmp', '47494638' => '.gif', '89504e47' => '.png', '494433' => '.mp3', 'fffb' => '.mp3', 'fff3' => '.mp3', '3026b2758e66cf11' => '.wma', - '52494646' => '.wav', + '52494646' => '.wav', '57415645' => '.wav', - '41564920' => '.avi', + '41564920' => '.avi', '000001ba' => '.mpg', '000001b3' => '.mpg', '2321414d52' => '.amr', ]; - /** * Return steam extension. * @@ -90,7 +89,7 @@ public static function getStreamExt($stream) /** * Get file extension by file header signature. * - * @param string $stream + * @param string $stream * * @return string */ @@ -104,6 +103,6 @@ public static function getExtBySignature($stream) } } - return ""; + return ''; } }