"; saveFile( $file, $data ); } function encryptPassword( $p ) { return sha1( md5( $p ) ); } function cleanUsername( $username ) { return preg_replace( '#[^A-Za-z0-9' . preg_quote( '-_@. ' ). ']#', '', $username ); } function isAbsPath( $path ) { return $path[0] === '/'; } function cleanPath( $path ) { // prevent Poison Null Byte injections $path = str_replace( chr( 0 ), '', $path ); // prevent go out of the workspace while ( strpos( $path, '../' ) !== false ) { $path = str_replace( '../', '', $path ); } return $path; } ////////////////////////////////////////////////////////////////////// // Verify no overwrites ////////////////////////////////////////////////////////////////////// if ( ! ( defined( 'DBHOST' ) && defined( 'DBNAME' ) && defined( 'DBUSER' ) && defined( 'DBPASS' ) && defined( 'DBTYPE' ) ) ) { ////////////////////////////////////////////////////////////////// // Get POST responses ////////////////////////////////////////////////////////////////// $username = cleanUsername( $_POST['username'] ); $password = encryptPassword( $_POST['password'] ); $project_name = $_POST['project_name']; if ( isset( $_POST['project_path'] ) ) { $project_path = $_POST['project_path']; } else { $project_path = $project_name; } $timezone = $_POST['timezone']; $dbtype = $_POST['dbtype']; $dbhost = $_POST['dbhost']; $dbname = $_POST['dbname']; $dbuser = $_POST['dbuser']; $dbpass = $_POST['dbpass']; //Valid databases Codiad is able to use $db_types = [ 'mysql', 'postgresql', //'sqlite', ]; //Is selected database type valid? if( ! in_array( $dbtype, $db_types ) ) { die( "Invalid database. Please select one of " . implode( ", ", $db_types ) . "." ); } try { $connection = new PDO( "{$dbtype}:host={$dbhost};dbname={$dbname}", $dbuser, $dbpass ); } catch( exception $e ) { die( "Could not connect to database." ); die(); } $bind_vars = array(); $bind = ""; $database_sql_fullpath = $path . '/components/install/sql/' . $dbtype . '.sql'; if( ! is_file( $database_sql_fullpath ) ) { die("Could not find the sql of the database ".$dbtype." to execute"); } $sql = file_get_contents( $database_sql_fullpath ); try { //Create the database $result = $connection->exec( $sql ); } catch( PDOException $e ) { die($e->getMessage()); } $error = $connection->errorInfo(); if( ! $error[0] == "00000" ) { die( $error[2] ); } ////////////////////////////////////////////////////////////////// // Create Projects files ////////////////////////////////////////////////////////////////// $project_path = cleanPath( $project_path ); if ( ! isAbsPath( $project_path ) ) { $project_path = str_replace( " ", "_", preg_replace( '/[^\w-\.]/', '', $project_path ) ); if( ! is_dir( $workspace . "/" . $project_path ) ) { mkdir( $workspace . "/" . $project_path ); } } else { $project_path = cleanPath( $project_path ); if ( substr( $project_path, -1 ) == '/' ) { $project_path = substr( $project_path, 0, strlen( $project_path ) - 1 ); } if ( ! file_exists( $project_path ) ) { if ( ! mkdir( $project_path . '/', 0755, true ) ) { die( "Unable to create Absolute Path" ); } } else { if ( ! is_writable( $project_path ) || ! is_readable( $project_path ) ) { die( "No Read/Write Permission" ); } } } $bind_variables = array( $project_name, $project_path, $username ); $query = "INSERT INTO projects(name, path, owner) VALUES (?,?,?);"; $statement = $connection->prepare( $query ); $statement->execute( $bind_variables ); $error = $statement->errorInfo(); if( ! $error[0] == "00000" ) { die( $error[2] ); } $bind_variables = array( "", "", $username, $password, "", $project_path, "admin", "", "" ); $query = "INSERT INTO users(first_name, last_name, username, password, email, project, access, groups, token) VALUES (?,?,?,?,?,?,?,?,?)"; $statement = $connection->prepare( $query ); $statement->execute( $bind_variables ); $error = $statement->errorInfo(); if( ! $error[0] == "00000" ) { die( $error[2] ); } /** * Create sessions path. */ if ( ! is_dir( $sessions ) ) { mkdir( $sessions, 00755 ); } ////////////////////////////////////////////////////////////////// // Create Active file ////////////////////////////////////////////////////////////////// saveJSON( $active, array( '' ) ); ////////////////////////////////////////////////////////////////// // Create Config ////////////////////////////////////////////////////////////////// $config_data = '