Last active
March 22, 2016 20:31
-
-
Save rvanlaak/06ca1b65658a91240362 to your computer and use it in GitHub Desktop.
Check .doc and .docx for active file encryption
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Check or the file is password protected based on the top file contents. | |
* | |
* @param string $absolutePath | |
* @return bool | |
*/ | |
public function checkFileEncryption($absolutePath) | |
{ | |
$content = utf8_encode(file_get_contents($absolutePath)); | |
if (mb_substr($content, 0, 2) == "ÐÏ") { | |
// DOCX/XLSX 2007+ | |
$start = str_replace("\x00", " ", mb_substr($content, 0, 2000)); | |
if (mb_strstr($start, 'E n c r y p t e d P a c k a g e') !== false) { | |
return true; | |
} | |
// DOC 2005- | |
if (mb_substr($content, 0x20B, 1) == "\x13") { | |
return true; | |
} | |
} | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment