<commit_before><?php
/********************************************************************************
*                                                                               *
*   Copyright 2012 <NAME> (<EMAIL>)                      *
*   http://www.elabftw.net/                                                     *
*                                                                               *
********************************************************************************/

/********************************************************************************
*  This file is part of eLabFTW.                                                *
*                                                                               *
*    eLabFTW is free software: you can redistribute it and/or modify            *
*    it under the terms of the GNU Affero General Public License as             *
*    published by the Free Software Foundation, either version 3 of             *
*    the License, or (at your option) any later version.                        *
*                                                                               *
*    eLabFTW is distributed 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 Affero General Public License for more details.      *
*                                                                               *
*    You should have received a copy of the GNU Affero General Public           *
*    License along with eLabFTW.  If not, see <http://www.gnu.org/licenses/>.   *
*                                                                               *
********************************************************************************/
namespace Elabftw\Elabftw;

use \Exception;
use \ZipArchive;
use \RecursiveIteratorIterator;
use \RecursiveDirectoryIterator;
use \FileSystemIterator;

class ImportZip
{
    // number of item we have inserted
    public $inserted = 0;
    private $tmpPath;
    private $fileTmpName;
    private $json;

    private $itemType;
    private $title;
    private $body;
    // the newly created id of the imported item
    // we need it for linking attached file(s) to the the new item
    private $newItemId;

    public function __construct()
    {
        $this->fileTmpName = $_FILES['zipfile']['tmp_name'];
        $this->itemType = $_COOKIE['itemType'];
        // this is where we will extract the zip
        $this->tmpPath = ELAB_ROOT . 'uploads/tmp/' . uniqid();
        if (!mkdir($this->tmpPath)) {
            throw new Exception('Cannot create temporary folder');
        }

        $this->extractZip();
        $this->readJson();
        $this->checkItemType();
        $this->importAll();
    }

    /*
     * Extract the zip to the temporary folder
     *
     * @return bool
     */
    private function extractZip()
    {
        $zip = new \ZipArchive;
        if ($zip->open($this->fileTmpName) && $zip->extractTo($this->tmpPath)) {
            return true;
        } else {
            throw new Exception('Cannot open zip file!');
        }
    }

    /*
     * We get all the info we need from the embedded .json file
     *
     */
    private function readJson()
    {
        $file = $this->tmpPath . "/.elabftw.json";
        $content = file_get_contents($file);
        $this->json = json_decode($content, true);
        // we can only import database items, not experiments
        if ($this->json[0]['type'] === 'experiments') {
            throw new Exception('Cannot import an experiment!');
        }
    }

    /*
     * Item type will be a number
     *
     */
    private function checkItemType()
    {
        if (!is_pos_int($this->itemType)) {
            throw new Exception('No cookie found!');
        }
    }

    /*
     * The main SQL to create a new item with the title and body we have
     *
     */
    private function importData()
    {
        global $pdo;

        $sql = "INSERT INTO items(team, title, date, body, userid, type) VALUES(:team, :title, :date, :body, :userid, :type)";
        $req = $pdo->prepare($sql);
        $req->execute(array(
            'team' => $_SESSION['team_id'],
            'title' => $this->title,
            'date' => kdate(),
            'body' => $this->body,
            'userid' => $_SESSION['userid'],
            'type' => $this->itemType
        ));
        if (!$req->execute()) {
            throw new Exception('Cannot import in database!');
        }
        // needed in importFile()
        $this->newItemId = $pdo->lastInsertId();
    }

    /*
     * If files are attached we want them!
     *
     * @param string $file The path of the file in the archive
     */
    private function importFile($file)
    {
        global $pdo;

        // first move the file to the uploads folder
        $longName = hash("sha512", uniqid(rand(), true)) . '.' . get_ext($file);
        $newPath = ELAB_ROOT . 'uploads/' . $longName;
        if (!rename($this->tmpPath . '/' . $file, $newPath)) {
            throw new Exception('Cannot rename file!');<commit_msg>one time is enough!<commit_after><?php
/********************************************************************************
*                                                                               *
*   Copyright 2012 <NAME> (<EMAIL>)                      *
*   http://www.elabftw.net/                                                     *
*                                                                               *
********************************************************************************/

/********************************************************************************
*  This file is part of eLabFTW.                                                *
*                                                                               *
*    eLabFTW is free software: you can redistribute it and/or modify            *
*    it under the terms of the GNU Affero General Public License as             *
*    published by the Free Software Foundation, either version 3 of             *
*    the License, or (at your option) any later version.                        *
*                                                                               *
*    eLabFTW is distributed 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 Affero General Public License for more details.      *
*                                                                               *
*    You should have received a copy of the GNU Affero General Public           *
*    License along with eLabFTW.  If not, see <http://www.gnu.org/licenses/>.   *
*                                                                               *
********************************************************************************/
namespace Elabftw\Elabftw;

use \Exception;
use \ZipArchive;
use \RecursiveIteratorIterator;
use \RecursiveDirectoryIterator;
use \FileSystemIterator;

class ImportZip
{
    // number of item we have inserted
    public $inserted = 0;
    private $tmpPath;
    private $fileTmpName;
    private $json;

    private $itemType;
    private $title;
    private $body;
    // the newly created id of the imported item
    // we need it for linking attached file(s) to the the new item
    private $newItemId;

    public function __construct()
    {
        $this->fileTmpName = $_FILES['zipfile']['tmp_name'];
        $this->itemType = $_COOKIE['itemType'];
        // this is where we will extract the zip
        $this->tmpPath = ELAB_ROOT . 'uploads/tmp/' . uniqid();
        if (!mkdir($this->tmpPath)) {
            throw new Exception('Cannot create temporary folder');
        }

        $this->extractZip();
        $this->readJson();
        $this->checkItemType();
        $this->importAll();
    }

    /*
     * Extract the zip to the temporary folder
     *
     * @return bool
     */
    private function extractZip()
    {
        $zip = new \ZipArchive;
        if ($zip->open($this->fileTmpName) && $zip->extractTo($this->tmpPath)) {
            return true;
        } else {
            throw new Exception('Cannot open zip file!');
        }
    }

    /*
     * We get all the info we need from the embedded .json file
     *
     */
    private function readJson()
    {
        $file = $this->tmpPath . "/.elabftw.json";
        $content = file_get_contents($file);
        $this->json = json_decode($content, true);
        // we can only import database items, not experiments
        if ($this->json[0]['type'] === 'experiments') {
            throw new Exception('Cannot import an experiment!');
        }
    }

    /*
     * Item type will be a number
     *
     */
    private function checkItemType()
    {
        if (!is_pos_int($this->itemType)) {
            throw new Exception('No cookie found!');
        }
    }

    /*
     * The main SQL to create a new item with the title and body we have
     *
     */
    private function importData()
    {
        global $pdo;

        $sql = "INSERT INTO items(team, title, date, body, userid, type) VALUES(:team, :title, :date, :body, :userid, :type)";
        $req = $pdo->prepare($sql);
        $req->bindParam(':team', $_SESSION['team_id'], \PDO::PARAM_INT);
        $req->bindParam(':title', $this->title);
        $req->bindParam(':date', kdate());
        $req->bindParam(':body', $this->body);
        $req->bindParam(':userid', $_SESSION['userid'], \PDO::PARAM_INT);
        $req->bindParam(':type', $this->itemType);

        if (!$req->execute()) {
            throw new Exception('Cannot import in database!');
        }
        // needed in importFile()
        $this->newItemId = $pdo->lastInsertId();
    }

    /*
     * If files are attached we want them!
     *
     * @param string $file The path of the file in the archive
     */
    private function importFile($file)
    {
        global $pdo;

        // first move the file to the uploads folder
        $longName = hash("sha512", uniqid(rand(), true)) . '.' . get_ext($file);
        $newPath = ELAB_ROOT . 'uploads/' . $longName;
        if (!rename($this->tmpPath . '/' . $file, $newPath)) {
            throw new Exception('Cannot rename file!');
{'fraction_non_alphanumeric': 0.1673148060324386, 'fraction_numerical': 0.001896993265673907, 'mean_word_length': 1.591937069813176, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 70, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 44, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2234007', 'n_tokens_mistral': 2639, 'n_tokens_neox': 2540, 'n_words': 791}