Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stephenwaite/4f9ec6ef88be25d1889869043ff2988f to your computer and use it in GitHub Desktop.
Save stephenwaite/4f9ec6ef88be25d1889869043ff2988f to your computer and use it in GitHub Desktop.
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

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