<commit_before><?php
/**
 * This file is part of PHP Skeleton App.
 *
 * (c) 2014 <NAME>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

/**
 * Controller for the Web App Installer module.
 *
 * @author <NAME> <<EMAIL>>
 * @copyright   2014 <NAME>
 * @link        https://github.com/ghalusa/PHP-Skeleton-App
 * @license     https://github.com/ghalusa/PHP-Skeleton-App/wiki/License
 * @version     1.0.0
 * @package     PHP Skeleton App
 */

function form(){
  $app = \Slim\Slim::getInstance();
  $env = $app->environment();
  global $final_global_template_vars;

  // Redirect to the installer if database variables aren't present, and if we aren't already there.
  if(
    isset($final_global_template_vars["db_connection"]["name"]) && 
    isset($final_global_template_vars["db_connection"]["host"]) && 
    isset($final_global_template_vars["db_connection"]["user"]) && 
    isset($final_global_template_vars["db_connection"]["password"]) && 
    $_SERVER["REQUEST_URI"] == "/webapp_installer/"
  ) {
    header("Location: ".$final_global_template_vars["login_url"]."/");
    exit;
  }

  require_once $_SERVER["PATH_TO_FRAMEWORKS"] . "wixel/gump/gump.class.php";
  $gump = new GUMP();

  $data = $posted_data = $app->request()->post() ? $app->request()->post() : false;

  // GUMP validation rules
  $rules = array(
    "user_account_email" => "required"
    ,"user_account_password" => "<PASSWORD>"
    ,"first_name" => "required"
    ,"last_name" => "required"
    ,"application_name" => "required"
    ,"session_key" => "required"
    ,"cname" => "required"
    // ,"http_mode" => "required"
    ,"database_host" => "required"
    ,"database_name" => "required"
    ,"database_username" => "required"
    ,"database_password" => "<PASSWORD>"
  );

  // Validation using GUMP
  if($posted_data) {
    $validated = array();
    $errors = array();
    $validated = $gump->validate($posted_data, $rules);
    if($validated !== TRUE){
      $errors = \slimlocal\models\utility::gump_parse_errors($validated);
    }
    if($errors) {
      $env = $app->environment();
      $env["default_validation_errors"] = $errors;
    }
  }

  $default_validation_errors = isset($env["default_validation_errors"]) ? $env["default_validation_errors"] : false;

  // If there are no errors, begin the second round of checks
  if( !$default_validation_errors && $posted_data ) {

    // Check to see if the database user exists
    $link = @mysqli_connect($posted_data['database_host'], $posted_data['database_username'], $posted_data['database_password']);
    if(!$link) {
      // die('Could not connect to the database. Please check your parameters.');
      $app->flash('message', 'Could not connect to the database. Please check your parameters.');
      $app->redirect($final_global_template_vars["path_to_this_module"]);
    }
    // Next, check to see if the database exists by making $posted_data['database_name'] the current db
    $db_selected = mysqli_select_db($posted_data['database_name'], $link);
    if(!$db_selected) {
      // die('Cannot use the "'.$posted_data['database_name'].'" database. Does it exist?');
      $app->flash('message', 'Cannot use the "'.$posted_data['database_name'].'" database. Does it exist?');
      $app->redirect($final_global_template_vars["path_to_this_module"]);
    }

    // If there are no MYSQL errors, overwrite the default_global_settings.php file
    $file_name = "default_global_settings.php";
    $original_file = file_get_contents($_SERVER['DOCUMENT_ROOT'].'/'.$file_name);
    $parsed = str_replace('#~site_name~#', '"site_name" => "'.$posted_data['application_name'].'",', $original_file);
    $parsed = str_replace('#~session_key~#', ',"session_key" => "'.$posted_data['session_key'].'"', $parsed);
    $parsed = str_replace('#~name~#', '"name" => ($_SERVER["IS_DEV"] == "true") ? "'.$posted_data['database_name'].'" : "'.$posted_data['database_name'].'"', $parsed);
    $parsed = str_replace('#~host~#', ',"host" => "'.$posted_data['database_host'].'"', $parsed);
    $parsed = str_replace('#~user~#', ',"user" => "'.$posted_data['database_username'].'"', $parsed);
    $parsed = str_replace('#~password~#', ',"password" => "'.$posted_data['database_password'].'",', $parsed);
    $parsed = str_replace('#~admin_emails~#', ',"admin_emails" => "'.$posted_data['user_account_email'].'",', $parsed);
    unlink($_SERVER['DOCUMENT_ROOT'].'/'.$file_name);
    $file_handle = fopen($_SERVER['DOCUMENT_ROOT'].'/'.$file_name, 'w') or die("can't open file");
    fwrite($file_handle, $parsed);
    fclose($file_handle);
    chmod($_SERVER['DOCUMENT_ROOT'].'/'.$file_name, 0664);
<commit_msg>Fixed mysqli_select_db() variables<commit_after><?php
/**
 * This file is part of PHP Skeleton App.
 *
 * (c) 2014 <NAME>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

/**
 * Controller for the Web App Installer module.
 *
 * @author <NAME> <<EMAIL>>
 * @copyright   2014 <NAME>
 * @link        https://github.com/ghalusa/PHP-Skeleton-App
 * @license     https://github.com/ghalusa/PHP-Skeleton-App/wiki/License
 * @version     1.0.0
 * @package     PHP Skeleton App
 */

function form(){
  $app = \Slim\Slim::getInstance();
  $env = $app->environment();
  global $final_global_template_vars;

  // Redirect to the installer if database variables aren't present, and if we aren't already there.
  if(
    isset($final_global_template_vars["db_connection"]["name"]) && 
    isset($final_global_template_vars["db_connection"]["host"]) && 
    isset($final_global_template_vars["db_connection"]["user"]) && 
    isset($final_global_template_vars["db_connection"]["password"]) && 
    $_SERVER["REQUEST_URI"] == "/webapp_installer/"
  ) {
    header("Location: ".$final_global_template_vars["login_url"]."/");
    exit;
  }

  require_once $_SERVER["PATH_TO_FRAMEWORKS"] . "wixel/gump/gump.class.php";
  $gump = new GUMP();

  $data = $posted_data = $app->request()->post() ? $app->request()->post() : false;

  // GUMP validation rules
  $rules = array(
    "user_account_email" => "required"
    ,"user_account_password" => "<PASSWORD>"
    ,"first_name" => "required"
    ,"last_name" => "required"
    ,"application_name" => "required"
    ,"session_key" => "required"
    ,"cname" => "required"
    // ,"http_mode" => "required"
    ,"database_host" => "required"
    ,"database_name" => "required"
    ,"database_username" => "required"
    ,"database_password" => "<PASSWORD>"
  );

  // Validation using GUMP
  if($posted_data) {
    $validated = array();
    $errors = array();
    $validated = $gump->validate($posted_data, $rules);
    if($validated !== TRUE){
      $errors = \slimlocal\models\utility::gump_parse_errors($validated);
    }
    if($errors) {
      $env = $app->environment();
      $env["default_validation_errors"] = $errors;
    }
  }

  $default_validation_errors = isset($env["default_validation_errors"]) ? $env["default_validation_errors"] : false;

  // If there are no errors, begin the second round of checks
  if( !$default_validation_errors && $posted_data ) {

    // Check to see if the database user exists
    $link = @mysqli_connect($posted_data['database_host'], $posted_data['database_username'], $posted_data['database_password']);
    if(!$link) {
      // die('Could not connect to the database. Please check your parameters.');
      $app->flash('message', 'Could not connect to the database. Please check your parameters.');
      $app->redirect($final_global_template_vars["path_to_this_module"]);
    }
    // Next, check to see if the database exists by making $posted_data['database_name'] the current db
    $db_selected = mysqli_select_db($link, $posted_data['database_name']);
    if(!$db_selected) {
      // die('Cannot use the "'.$posted_data['database_name'].'" database. Does it exist?');
      $app->flash('message', 'Cannot use the "'.$posted_data['database_name'].'" database. Does it exist?');
      $app->redirect($final_global_template_vars["path_to_this_module"]);
    }

    // If there are no MYSQL errors, overwrite the default_global_settings.php file
    $file_name = "default_global_settings.php";
    $original_file = file_get_contents($_SERVER['DOCUMENT_ROOT'].'/'.$file_name);
    $parsed = str_replace('#~site_name~#', '"site_name" => "'.$posted_data['application_name'].'",', $original_file);
    $parsed = str_replace('#~session_key~#', ',"session_key" => "'.$posted_data['session_key'].'"', $parsed);
    $parsed = str_replace('#~name~#', '"name" => ($_SERVER["IS_DEV"] == "true") ? "'.$posted_data['database_name'].'" : "'.$posted_data['database_name'].'"', $parsed);
    $parsed = str_replace('#~host~#', ',"host" => "'.$posted_data['database_host'].'"', $parsed);
    $parsed = str_replace('#~user~#', ',"user" => "'.$posted_data['database_username'].'"', $parsed);
    $parsed = str_replace('#~password~#', ',"password" => "'.$posted_data['database_password'].'",', $parsed);
    $parsed = str_replace('#~admin_emails~#', ',"admin_emails" => "'.$posted_data['user_account_email'].'",', $parsed);
    unlink($_SERVER['DOCUMENT_ROOT'].'/'.$file_name);
    $file_handle = fopen($_SERVER['DOCUMENT_ROOT'].'/'.$file_name, 'w') or die("can't open file");
    fwrite($file_handle, $parsed);
    fclose($file_handle);
    chmod($_SERVER['DOCUMENT_ROOT'].'/'.$file_name, 0664);

{'fraction_non_alphanumeric': 0.19067751823274495, 'fraction_numerical': 0.003170912165733009, 'mean_word_length': 4.523642732049037, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 77, 'https://': 4, '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': '959614', 'n_tokens_mistral': 3106, 'n_tokens_neox': 3056, 'n_words': 606}