Skip to content

Instantly share code, notes, and snippets.

@rvanlaak
Last active March 22, 2016 20:31
Show Gist options
  • Save rvanlaak/06ca1b65658a91240362 to your computer and use it in GitHub Desktop.
Save rvanlaak/06ca1b65658a91240362 to your computer and use it in GitHub Desktop.
Check .doc and .docx for active file encryption
/**
* 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