<commit_before><?php
/**
 * wallabag, self hostable application allowing you to not miss any content anymore
 *
 * @category   wallabag
 * @author     <NAME> <<EMAIL>>
 * @copyright  2013
 * @license    http://opensource.org/licenses/MIT see COPYING file
 */

$errors = array();
$successes = array();

/* Function taken from at http://php.net/manual/en/function.rmdir.php#110489
 * Idea : nbari at dalmp dot com
 * Rights unknown
 * Here in case of .gitignore files
 */
function delTree($dir) {
    $files = array_diff(scandir($dir), array('.','..'));
    foreach ($files as $file) {
      (is_dir("$dir/$file")) ? delTree("$dir/$file") : unlink("$dir/$file");
    }
    return rmdir($dir);
  }

if (isset($_GET['clean'])) {
    if (is_dir('install')){
    delTree('install');
    header('Location: index.php');      
    }
}

if (isset($_POST['download'])) {
    if (!file_put_contents("cache/vendor.zip", fopen("http://static.wallabag.org/files/vendor.zip", 'r'))) {
        $errors[] = 'Impossible to download vendor.zip. Please <a href="http://wllbg.org/vendor">download it manually</a> and unzip it in your wallabag folder.';
    }
    else {
        if (extension_loaded('zip')) {
            $zip = new ZipArchive();
            if ($zip->open("cache/vendor.zip") !== TRUE){
                $errors[] = 'Impossible to open cache/vendor.zip. Please unzip it manually in your wallabag folder.';
            }
            if ($zip->extractTo(realpath(''))) {
                @unlink("cache/vendor.zip");
                $successes[] = 'twig is now installed, you can install wallabag.';
            }
            else {
                $errors[] = 'Impossible to extract cache/vendor.zip. Please unzip it manually in your wallabag folder.';
            }
            $zip->close();
        }
        else {
            $errors[] = 'zip extension is not enabled in your PHP configuration. Please unzip cache/vendor.zip in your wallabag folder.';
        }
    }
}
else if (isset($_POST['install'])) {
    if (!is_dir('vendor')) {
        $errors[] = 'You must install twig before.';
    }
    else {
        $continue = true;
        // Create config.inc.php
        if (!copy('inc/poche/config.inc.default.php', 'inc/poche/config.inc.php')) {
            $errors[] = 'Installation aborted, impossible to create inc/poche/config.inc.php file. Maybe you don\'t have write access to create it.';
            $continue = false;
        }
        else {
            function generate_salt() {
                mt_srand(microtime(true)*100000 + memory_get_usage(true));
                return md5(uniqid(mt_rand(), true));
            }

            $content = file_get_contents('inc/poche/config.inc.php');
            $salt = generate_salt();
            $content = str_replace("define ('SALT', '');", "define ('SALT', '".$salt."');", $content);
            file_put_contents('inc/poche/config.inc.php', $content);
        }

        if ($continue) {

            // User informations
            $username = trim($_POST['username']);
            $password = trim($_POST['password']);
            $salted_password = sha1($password . $username . $salt);

            // Database informations
            if ($_POST['db_engine'] == 'sqlite') {
                if (!copy('install/poche.sqlite', 'db/poche.sqlite')) {
                    $errors[] = 'Impossible to create inc/poche/config.inc.php file.';
                    $continue = false;
                }
                else {
                    $db_path = 'sqlite:' . realpath('') . '/db/poche.sqlite';
                    $handle = new PDO($db_path);
                    $sql_structure = "";
                }
            }
            else {
                $content = file_get_contents('inc/poche/config.inc.php');

                if ($_POST['db_engine'] == 'mysql') {
                    $db_path = 'mysql:host=' . $_POST['mysql_server'] . ';dbname=' . $_POST['mysql_database'];
                    $content = str_replace("define ('STORAGE_SERVER', 'localhost');", "define ('STORAGE_SERVER', '".$_POST['mysql_server']."');", $content);
                    $content = str_replace("define ('STORAGE_DB', 'poche');", "define ('STORAGE_DB', '".$_POST['mysql_database']."');", $content);
                    $content = str_replace("define ('STORAGE_USER', 'poche');", "define ('STORAGE_USER', '".$_POST['mysql_user']."');", $content);
                    $content = str_replace("define ('STORAGE_PASSWORD', '<PASSWORD>');", "define ('STORAGE_PASSWORD', '".$_POST['mysql_password']."');", $content);
                    $handle = new PDO($db_path, $_POST['mysql_user'], $_POST['mysql_password']); 

                    $sql_structure = file_get_contents('install/mysql.sql');
                }
                else if ($_POST['db_engine'] == 'postgres') {
                    $db_path = 'pgsql:host=' . $_POST['pg_server'] . ';dbname=' . $_POST['pg_database'];
                    $content = str_replace("define ('STORAGE_SERVER', 'localhost');", "define ('STORAGE_SERVER', '".$_POST['pg_server']."');", $content);
                    $content = str_replace("define ('STORAGE_DB', 'poche');", "define ('STORAGE_DB', '".$_POST['pg_database']."');", $content);
                    $content = str_replace("define ('STORAGE_USER', 'poche');", "define ('STORAGE_USER', '".$_POST['pg_user']."');", $content);
                    $content = str_replace("define ('STORAGE_PASSWORD', '<PASSWORD>');", "define ('STORAGE_PASSWORD', '".$_POST['pg_password']."');", $content);
                    $handle = new PDO($db_path, $_POST['pg_user'], $_POST['pg_password']);

                    $sql_structure = file_get_contents('install/postgres.sql');
                }

                $content = str_replace("define ('STORAGE', 'sqlite');", "define ('STORAGE', '".$_POST['db_engine']."');", $content);
                file_put_contents('inc/poche/config.inc.php', $content);
            }

            if ($continue) {

                function executeQuery($handle, $sql, $params) {
                    try
                    {
                        $query = $handle->prepare($sql);
                        $query->execute($params);
                        return $query->fetchAll();
                    }
                    catch (Exception $e)
                    {
                        return FALSE;
                    }
                }

                // create database structure
                $query = executeQuery($handle, $sql_structure, array());

                // Create user
                $handle->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

                $sql = 'INSERT INTO users (username, password, name) VALUES (?, ?, ?)';
                $params = array($username, $salted_password, $username);
                $query = executeQuery($handle, $sql, $params);

                $id_user = $handle->lastInsertId();

                $sql = 'INSERT INTO users_config ( user_id, name, value ) VALUES (?, ?, ?)';
                $params = array($id_user, 'pager', '10');
                $query = executeQuery($handle, $sql, $params);

                $sql = 'INSERT INTO users_config ( user_id, name, value ) VALUES (?, ?, ?)';
                $params = array($id_user, 'language', 'en_EN.UTF8');
                $query = executeQuery($handle, $sql, $params);

                $successes[] = 'wallabag is now installed. You can now <a href="index.php?clean=0">access it !</a>';
            }
        }
    }
}
?>
<!DOCTYPE html>
<html><commit_msg>Fix the PostgreSQL install errors<commit_after><?php
/**
 * wallabag, self hostable application allowing you to not miss any content anymore
 *
 * @category   wallabag
 * @author     <NAME> <<EMAIL>>
 * @copyright  2013
 * @license    http://opensource.org/licenses/MIT see COPYING file
 */

$errors = array();
$successes = array();

/* Function taken from at http://php.net/manual/en/function.rmdir.php#110489
 * Idea : nbari at dalmp dot com
 * Rights unknown
 * Here in case of .gitignore files
 */
function delTree($dir) {
    $files = array_diff(scandir($dir), array('.','..'));
    foreach ($files as $file) {
      (is_dir("$dir/$file")) ? delTree("$dir/$file") : unlink("$dir/$file");
    }
    return rmdir($dir);
  }

if (isset($_GET['clean'])) {
    if (is_dir('install')){
    delTree('install');
    header('Location: index.php');      
    }
}

if (isset($_POST['download'])) {
    if (!file_put_contents("cache/vendor.zip", fopen("http://static.wallabag.org/files/vendor.zip", 'r'))) {
        $errors[] = 'Impossible to download vendor.zip. Please <a href="http://wllbg.org/vendor">download it manually</a> and unzip it in your wallabag folder.';
    }
    else {
        if (extension_loaded('zip')) {
            $zip = new ZipArchive();
            if ($zip->open("cache/vendor.zip") !== TRUE){
                $errors[] = 'Impossible to open cache/vendor.zip. Please unzip it manually in your wallabag folder.';
            }
            if ($zip->extractTo(realpath(''))) {
                @unlink("cache/vendor.zip");
                $successes[] = 'twig is now installed, you can install wallabag.';
            }
            else {
                $errors[] = 'Impossible to extract cache/vendor.zip. Please unzip it manually in your wallabag folder.';
            }
            $zip->close();
        }
        else {
            $errors[] = 'zip extension is not enabled in your PHP configuration. Please unzip cache/vendor.zip in your wallabag folder.';
        }
    }
}
else if (isset($_POST['install'])) {
    if (!is_dir('vendor')) {
        $errors[] = 'You must install twig before.';
    }
    else {
        $continue = true;
        // Create config.inc.php
        if (!copy('inc/poche/config.inc.default.php', 'inc/poche/config.inc.php')) {
            $errors[] = 'Installation aborted, impossible to create inc/poche/config.inc.php file. Maybe you don\'t have write access to create it.';
            $continue = false;
        }
        else {
            function generate_salt() {
                mt_srand(microtime(true)*100000 + memory_get_usage(true));
                return md5(uniqid(mt_rand(), true));
            }

            $content = file_get_contents('inc/poche/config.inc.php');
            $salt = generate_salt();
            $content = str_replace("define ('SALT', '');", "define ('SALT', '".$salt."');", $content);
            file_put_contents('inc/poche/config.inc.php', $content);
        }

        if ($continue) {

            // User informations
            $username = trim($_POST['username']);
            $password = trim($_POST['password']);
            $salted_password = sha1($password . $username . $salt);

            // Database informations
            if ($_POST['db_engine'] == 'sqlite') {
                if (!copy('install/poche.sqlite', 'db/poche.sqlite')) {
                    $errors[] = 'Impossible to create inc/poche/config.inc.php file.';
                    $continue = false;
                }
                else {
                    $db_path = 'sqlite:' . realpath('') . '/db/poche.sqlite';
                    $handle = new PDO($db_path);
                    $sql_structure = "";
                }
            }
            else {
                $content = file_get_contents('inc/poche/config.inc.php');

                if ($_POST['db_engine'] == 'mysql') {
                    $db_path = 'mysql:host=' . $_POST['mysql_server'] . ';dbname=' . $_POST['mysql_database'];
                    $content = str_replace("define ('STORAGE_SERVER', 'localhost');", "define ('STORAGE_SERVER', '".$_POST['mysql_server']."');", $content);
                    $content = str_replace("define ('STORAGE_DB', 'poche');", "define ('STORAGE_DB', '".$_POST['mysql_database']."');", $content);
                    $content = str_replace("define ('STORAGE_USER', 'poche');", "define ('STORAGE_USER', '".$_POST['mysql_user']."');", $content);
                    $content = str_replace("define ('STORAGE_PASSWORD', '<PASSWORD>');", "define ('STORAGE_PASSWORD', '".$_POST['mysql_password']."');", $content);
                    $handle = new PDO($db_path, $_POST['mysql_user'], $_POST['mysql_password']); 

                    $sql_structure = file_get_contents('install/mysql.sql');
                }
                else if ($_POST['db_engine'] == 'postgres') {
                    $db_path = 'pgsql:host=' . $_POST['pg_server'] . ';dbname=' . $_POST['pg_database'];
                    $content = str_replace("define ('STORAGE_SERVER', 'localhost');", "define ('STORAGE_SERVER', '".$_POST['pg_server']."');", $content);
                    $content = str_replace("define ('STORAGE_DB', 'poche');", "define ('STORAGE_DB', '".$_POST['pg_database']."');", $content);
                    $content = str_replace("define ('STORAGE_USER', 'poche');", "define ('STORAGE_USER', '".$_POST['pg_user']."');", $content);
                    $content = str_replace("define ('STORAGE_PASSWORD', '<PASSWORD>');", "define ('STORAGE_PASSWORD', '".$_POST['pg_password']."');", $content);
                    $handle = new PDO($db_path, $_POST['pg_user'], $_POST['pg_password']);

                    $sql_structure = file_get_contents('install/postgres.sql');
                }

                $content = str_replace("define ('STORAGE', 'sqlite');", "define ('STORAGE', '".$_POST['db_engine']."');", $content);
                file_put_contents('inc/poche/config.inc.php', $content);
            }

            if ($continue) {

                function executeQuery($handle, $sql, $params) {
                    try
                    {
                        $query = $handle->prepare($sql);
                        $query->execute($params);
                        return $query->fetchAll();
                    }
                    catch (Exception $e)
                    {
                        return FALSE;
                    }
                }

                // create database structure
                $query = $handle->exec($sql_structure);

                // Create user
                $handle->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

                $sql = "INSERT INTO users (username, password, name, email) VALUES (?, ?, ?, '')";
                $params = array($username, $salted_password, $username);
                $query = executeQuery($handle, $sql, $params);

                $id_user = (int)$handle->lastInsertId('users_id_seq');

                $sql = 'INSERT INTO users_config ( user_id, name, value ) VALUES (?, ?, ?)';
                $params = array($id_user, 'pager', '10');
                $query = executeQuery($handle, $sql, $params);

                $sql = 'INSERT INTO users_config ( user_id, name, value ) VALUES (?, ?, ?)';
                $params = array($id_user, 'language', 'en_EN.UTF8');
                $query = executeQuery($handle, $sql, $params);

                $successes[] = 'wallabag is now installed. You can now <a href="index.php?clean=0">access it !</a>';
            }
        }
    }
}
?>
<!DOCTYPE html>
<html>
{'fraction_non_alphanumeric': 0.17239780030477705, 'fraction_numerical': 0.0029152587292122175, 'mean_word_length': 2.0309236947791165, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 44, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5854810', 'n_tokens_mistral': 4512, 'n_tokens_neox': 4281, 'n_words': 914}