<commit_before><?php

/*
 * Copyright (C) 2014 <NAME> <<EMAIL>>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distribauted in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

namespace Service;

/**
 * Facade to real Gnupg (from extension) as there was no way to make decryption
 * work properly. Implements only methods needed in Behigorri PM project.
 * @author alayn
 */
class Gnupg {

    private $gnupg;

    public function __construct(\Gnupg $gnupg) 
    {
        $this->gnupg = $gnupg;
        $this->gnupg->setErrorMode(\Gnupg::ERROR_EXCEPTION);
    }
    
    public function addEncryptKey($fingerprint)
    {
        $this->gnupg->addEncryptKey($fingerprint);
    }
    
    public function clearEncryptKeys()
    {
        $this->gnupg->clearEncryptKeys();
    }
    
    public function encrypt($data)
    {
        return $this->gnupg->encrypt($data);
    }
    
    /*
     * TODO: Make this shell_exec safer.
     */
    public function decrypt($encryptedData, $password)
    {
        $encFile = tempnam(sys_get_temp_dir(), '');
        file_put_contents($encFile, $encryptedData);
        $command = sprintf(
            'gpg --homedir %s --batch --passphrase %s --yes -d %s 2> /dev/null', 
            escapeshellarg(getenv('GNUPGHOME')), 
            escapeshellarg($password), 
            escapeshellarg($encFile)
        );
        
        $aClearData = [];
        $returnVar = null;
        exec($command, $aClearData, $returnVar);
        
        if ($returnVar != 0) {
            throw new \Exception('Data could not be decrypted');
        }
        unlink($encFile);
        return implode("\n", $aClearData);
    }
}<commit_msg>Using passthru and ob_* instead of exec, to avoid binary data problems when imploding data<commit_after><?php

/*
 * Copyright (C) 2014 <NAME> <<EMAIL>>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distribauted in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

namespace Service;

/**
 * Facade to real Gnupg (from extension) as there was no way to make decryption
 * work properly. Implements only methods needed in Behigorri PM project.
 * @author alayn
 */
class Gnupg {

    private $gnupg;

    public function __construct(\Gnupg $gnupg)
    {
        $this->gnupg = $gnupg;
        $this->gnupg->setErrorMode(\Gnupg::ERROR_EXCEPTION);
    }

    public function addEncryptKey($fingerprint)
    {
        $this->gnupg->addEncryptKey($fingerprint);
    }

    public function clearEncryptKeys()
    {
        $this->gnupg->clearEncryptKeys();
    }

    public function encrypt($data)
    {
        return $this->gnupg->encrypt($data);
    }

    /*
     * TODO: Make this shell_exec safer.
     */
    public function decrypt($encryptedData, $password)
    {
        $encFile = tempnam(sys_get_temp_dir(), '');
        file_put_contents($encFile, $encryptedData);
        $command = sprintf(
            'gpg --homedir %s --batch --passphrase %s --yes -d %s 2> /dev/null',
            escapeshellarg(getenv('GNUPGHOME')),
            escapeshellarg($password),
            escapeshellarg($encFile)
        );

        $aClearData = [];
        $returnVar = null;

        ob_start();
        passthru($command, $returnVar);
        $data = ob_get_contents();
        ob_end_clean();
//        exec($command, $aClearData, $returnVar);

        if ($returnVar != 0) {
            throw new \Exception('Data could not be decrypted');
        }

        unlink($encFile);
        return $data;
    }

}
{'fraction_non_alphanumeric': 0.10673199829569663, 'fraction_numerical': 0.008947592671495527, 'mean_word_length': 2.586707410236822, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5422804', 'n_tokens_mistral': 1434, 'n_tokens_neox': 1370, 'n_words': 459}