Created
March 23, 2022 22:41
-
-
Save stephenwaite/4f9ec6ef88be25d1889869043ff2988f to your computer and use it in GitHub Desktop.
php de-crypto docs
This file contains hidden or 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
<?php | |
$_GET['site'] = 'default'; | |
$ignoreAuth = true; | |
require_once __DIR__ . '/vendor/autoload.php'; | |
require_once("interface/globals.php"); | |
use OpenEMR\Common\Crypto\CryptoGen; | |
$crypto = new CryptoGen(); | |
$dir = new RecursiveDirectoryIterator(__DIR__ . "/sites/default/documents/", FilesystemIterator::SKIP_DOTS); | |
$subdirs = new RecursiveIteratorIterator($dir); | |
foreach($subdirs as $filename_path) { | |
$parent_dir = $subdirs->getSubPath(); | |
// echo $filename_path, PHP_EOL; | |
// documents are stored under numeric pid in documents folder | |
if (is_numeric($parent_dir)) { | |
$filename = basename($filename_path); | |
// echo $filename, PHP_EOL; | |
$new_name = $filename_path . "-decrypted"; | |
//echo $new_name; | |
if (!$fp = fopen($new_name, 'w')) { | |
echo "Cannot write file ($new_name)"; | |
exit; | |
} | |
$encrypted_file_contents = file_get_contents($filename_path); | |
//echo $encrypted_file_contents, PHP_EOL; | |
$file_contents = $crypto->decryptStandard($encrypted_file_contents, '', 'database'); | |
if (fwrite($fp, $file_contents) === FALSE) { | |
echo "Cannot write to file ($new_name)"; | |
exit; | |
} | |
echo "Success, wrote ($file_contents) to file ($new_name)"; | |
fclose($fp); | |
} | |
} | |
<?php
/*
********************************************************************
* Filename: decrypt_patient_documents.php *
* Decrypts all OpenEMR patient documents and outputs them to a *
* new folder outside of the original documents folder, retaining *
* the origin folder structure such that patient id's are retained. *
********************************************************************
********************************************************************
* Author: Joshua Fischburg ([email protected]) *
* Date: 20220822 *
* Version: 0.1 *
* Tested against: OpenEMR 5_0_2_4 *
********************************************************************
*/
$_GET['site'] = 'default';
$ignoreAuth = true;
require_once __DIR__ . '/vendor/autoload.php';
require_once("interface/globals.php");
require_once("src/Common/Crypto/CryptoGen.php");
use OpenEMR\Common\Crypto\CryptoGen;
$crypto = new CryptoGen();
$dir = new RecursiveDirectoryIterator(__DIR__ . "/sites/default/documents/", FilesystemIterator::SKIP_DOTS);
$target_dir = "/var/www/html/decrypted_docs";
$subdirs = new RecursiveIteratorIterator($dir);
foreach($subdirs as $filename_path) {
$parent_dir = $subdirs->getSubPath();
echo $filename_path, PHP_EOL;
echo $parent_dir, PHP_EOL;
// documents are stored under numeric pid in documents folder
if (is_numeric($parent_dir)) {
$filename = basename($filename_path);
// echo $filename, PHP_EOL;
$new_dir = "$target_dir/$parent_dir";
if (!file_exists($new_dir)){
echo "Creating folder $new_dir", PHP_EOL;
mkdir($new_dir,0777,true);
}
$new_name = "$new_dir/$filename";
echo "Copying filepath to: $filename_path $new_name", PHP_EOL;
if (!$fp = fopen($new_name, 'w')) {
echo "Cannot write file ($new_name)";
exit;
}
$encrypted_file_contents = file_get_contents($filename_path);
//echo $encrypted_file_contents, PHP_EOL;
$file_contents = $crypto->decryptStandard($encrypted_file_contents, '', 'database');
if (fwrite($fp, $file_contents) === FALSE) {
echo "Cannot write to file ($new_name)";
exit;
}
fclose($fp);
}
}
hello @jfischburg-lifemesh how put this code to? i was created new php but it not work. please tell me how to do with this code? thanks
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
We have recently had a need for a script like this (v5.0.2). We found a few complaints about "use OpenEMR\Common\Crypto\CryptoGen;" and also didn't like the idea of needing to sort through duplicates of files, so we moved them outside OpenEMR. I might suggest that $target_dir be parameterized to assure the path exists. Further, because we stored documents on a cloud file system (AWS EFS), we found improved performance decrypting and copying to a local drive (instead of making a round trip to EFS). Content is included in the subsequent comment. This might be a useful utility to have handy within source. @stephenwaite