<commit_before><?php

/*
 * handles the data for api.php
 */
class datahandler {
    // (string) folder to store data in relative to position of api.php
    // webserver has to have write access to this folder
    // must end with a slash
    const DATA_FOLDER = 'data/';
    
    // (int) length of ids used to identify data
    const ID_LENGTH = 10;
    
    // (string) characters used to generate a new id
    const ID_CHARACTERS = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    
    public function __construct() {
        // check if data folder exists and is writeable
        // if data folder does not exist, try to create it
        if (!is_writeable(self::DATA_FOLDER)) {
            
            // check if data folder exists
            if (!file_exists(self::DATA_FOLDER)) {
                
                // try to create data folder
                if(mkdir(self::DATA_FOLDER)) {
                    
                    // put empty index.html in data folder to prevent directory listing
                    file_put_contents(self::DATA_FOLDER . "index.html", '');
                    
                    // check if newly created data folder is writeable
                    if (!is_writeable(self::DATA_FOLDER)) {
                        throw new Exception("data folder created but is not writeable");
                    }
                }
                else {
                    throw new Exception("data folder does not exists and can not be created");
                }
            }
            else {
                throw new Exception("data folder already exists but is not writeable");
            }
        }
    }

    private function deletePoll($poll_id) {
        $folder = self::DATA_FOLDER . "/" . $poll_id;
        $user_folder = $folder . "/user";
        
        // delete user folder
        if (is_dir($user_folder)) {
            $dir = opendir($user_folder);
            while(false !== ($file = readdir($dir))) {
                if($file === '.' || $file === '..') {
                  continue;
                }
                unlink($user_folder . '/' . $file);
            }
            rmdir($user_folder);
        }

        unlink($folder . '/poll_data');
        
        rmdir($folder);
    }
    
    /*
     * read poll data
     * return false, if there is no data with this id or if data could not be read
     */
    public function get($poll_id) {<commit_msg>directory handle resource should be closed<commit_after><?php

/*
 * handles the data for api.php
 */
class datahandler {
    // (string) folder to store data in relative to position of api.php
    // webserver has to have write access to this folder
    // must end with a slash
    const DATA_FOLDER = 'data/';
    
    // (int) length of ids used to identify data
    const ID_LENGTH = 10;
    
    // (string) characters used to generate a new id
    const ID_CHARACTERS = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    
    public function __construct() {
        // check if data folder exists and is writeable
        // if data folder does not exist, try to create it
        if (!is_writeable(self::DATA_FOLDER)) {
            
            // check if data folder exists
            if (!file_exists(self::DATA_FOLDER)) {
                
                // try to create data folder
                if(mkdir(self::DATA_FOLDER)) {
                    
                    // put empty index.html in data folder to prevent directory listing
                    file_put_contents(self::DATA_FOLDER . "index.html", '');
                    
                    // check if newly created data folder is writeable
                    if (!is_writeable(self::DATA_FOLDER)) {
                        throw new Exception("data folder created but is not writeable");
                    }
                }
                else {
                    throw new Exception("data folder does not exists and can not be created");
                }
            }
            else {
                throw new Exception("data folder already exists but is not writeable");
            }
        }
    }

    private function deletePoll($poll_id) {
        $folder = self::DATA_FOLDER . "/" . $poll_id;
        $user_folder = $folder . "/user";
        
        // delete user folder
        if (is_dir($user_folder)) {
            $dir = opendir($user_folder);
            while(false !== ($file = readdir($dir))) {
                if($file === '.' || $file === '..') {
                  continue;
                }
                unlink($user_folder . '/' . $file);
            }
            closedir($dir);
            rmdir($user_folder);
        }

        unlink($folder . '/poll_data');
        
        rmdir($folder);
    }
    
    /*
     * read poll data
     * return false, if there is no data with this id or if data could not be read
     */
    public function get($poll_id) {
{'fraction_non_alphanumeric': 0.09602582206980029, 'fraction_numerical': 0.004841638087552955, 'mean_word_length': 1.4556711243189697, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, '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': '5500948', 'n_tokens_mistral': 1355, 'n_tokens_neox': 1280, 'n_words': 406}