Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
php de-crypto docs
<?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);
}
}
@jfischburg-lifemesh
Copy link

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

@jfischburg-lifemesh
Copy link

jfischburg-lifemesh commented Aug 22, 2022

<?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 (jfischburg@lifemesh.ai)                *
* 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);
    }
}

@sovannapp
Copy link

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