Added get all user options function, Continued work on SQL library, Fixed issue where project was not loaded from settings, Improved performance for getting all options, Refactored install process to fit with new library, Refactored active module to use SQL, Fixed issue where a non focused file could not be closed when first logged in, Fixed issue of text mode not being selected after renaming a file,

This commit is contained in:
xevidos 2019-04-10 19:31:28 -04:00
parent 494675a9a0
commit 22a5ddc115
17 changed files with 425 additions and 699 deletions

View File

@ -1,173 +1,194 @@
<?php <?php
/** /**
* Copyright (c) Codiad, Kent Safranski (codiad.com), and Isaac Brown (telaaedifex.com), distributed * Copyright (c) Codiad, Kent Safranski (codiad.com), and Isaac Brown (telaaedifex.com), distributed
* as-is and without warranty under the MIT License. See * as-is and without warranty under the MIT License. See
* [root]/license.txt for more. This information must remain intact. * [root]/license.txt for more. This information must remain intact.
*/ */
require_once('../../common.php'); require_once('../../common.php');
class Active extends Common class Active extends Common {
{
//////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////// // PROPERTIES
// PROPERTIES //////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////
public $username = "";
public $username = ""; public $path = "";
public $path = ""; public $new_path = "";
public $new_path = "";
public $actives = ""; //////////////////////////////////////////////////////////////////
// METHODS
////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////
// METHODS
////////////////////////////////////////////////////////////////// // -----------------------------||----------------------------- //
// -----------------------------||----------------------------- // //////////////////////////////////////////////////////////////////
// Construct
////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////
// Construct
////////////////////////////////////////////////////////////////// public function __construct() {
}
public function __construct()
{ //////////////////////////////////////////////////////////////////
$this->actives = getJSON('active.php'); // List User's Active Files
} //////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////// public function ListActive() {
// List User's Active Files
////////////////////////////////////////////////////////////////// global $sql;
$query = "SELECT path,focused FROM active WHERE username=?";
public function ListActive() $bind_variables = array( $this->username );
{ $result = $sql->query( $query, $bind_variables, array() );
$active_list = array(); $tainted = false;
$tainted = false; $root = WORKSPACE;
$root = WORKSPACE; $active_list = $result;
if ($this->actives) {
foreach ($this->actives as $active => $data) { if( ! empty( $return ) ) {
if (is_array($data) && isset($data['username']) && $data['username']==$this->username) {
if ($this->isAbsPath($data['path'])) { foreach ( $result as $id => $data ) {
$root = "";
} else { if ( $this->isAbsPath( $data['path'] ) ) {
$root = $root.'/';
} $root = "";
if (file_exists($root.$data['path'])) { } else {
$focused = isset($data['focused']) ? $data['focused'] : false;
$active_list[] = array('path'=>$data['path'], 'focused'=>$focused); $root = $root.'/';
} else { }
unset($this->actives[$active]);
$tainted = true; if ( ! file_exists( $root . $data['path'] ) ) {
}
} $tainted = true;
} unset( $active_list[$id] );
} }
if ($tainted) { }
saveJSON('active.php', $this->actives); }
}
echo formatJSEND("success", $active_list); if( $tainted ) {
}
$this->update_active( $active_list );
////////////////////////////////////////////////////////////////// }
// Check File
////////////////////////////////////////////////////////////////// echo formatJSEND( "success", $active_list );
}
public function Check()
{ //////////////////////////////////////////////////////////////////
$cur_users = array(); // Check File
foreach ($this->actives as $active => $data) { //////////////////////////////////////////////////////////////////
if (is_array($data) && isset($data['username']) && $data['username']!=$this->username && $data['path']==$this->path) {
$cur_users[] = $data['username']; public function Check() {
}
} global $sql;
if (count($cur_users)!=0) { $query = "SELECT username FROM active WHERE path=?";
//echo formatJSEND("error", "Warning: File ".substr($this->path, strrpos($this->path, "/")+1)." Currently Opened By: " . implode(", ", $cur_users)); $bind_variables = array( $this->path );
} else { $result = $sql->query( $query, $bind_variables, array() );
echo formatJSEND("success"); $tainted = false;
} $user = false;
} $users = array();
$root = WORKSPACE;
//////////////////////////////////////////////////////////////////
// Add File foreach( $result as $id => $data ) {
//////////////////////////////////////////////////////////////////
array_push( $users, $data["username"] );
public function Add() if( $data["username"] == $this->username ) {
{
$process_add = true; $user = true;
foreach ($this->actives as $active => $data) { }
if (is_array($data) && isset($data['username']) && $data['username']==$this->username && $data['path']==$this->path) { }
$process_add = false;
} if ( ( count( $result ) == 1 && ! $user ) || count( $result ) > 1 ) {
}
if ($process_add) { echo formatJSEND( "warning", "Warning: File " . substr( $this->path, strrpos( $this->path, "/" ) +1 ) . " Currently Opened By: " . implode( ", ", $users ) );
$this->actives[] = array("username"=>$this->username,"path"=>$this->path); } else {
saveJSON('active.php', $this->actives);
echo formatJSEND("success"); echo formatJSEND("success");
} }
} }
////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////
// Rename File // Add File
////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////
public function Rename() public function Add() {
{
$revised_actives = array(); global $sql;
foreach ($this->actives as $active => $data) { $query = "INSERT INTO active( username, path, focused ) VALUES ( ?, ?, ? );";
if (is_array($data) && isset($data['username'])) { $bind_variables = array( $this->username, $this->path, false );
$revised_actives[] = array("username"=>$data['username'],"path"=>str_replace($this->path, $this->new_path, $data['path'])); $return = $sql->query( $query, $bind_variables, 0, "rowCount" );
}
} if( $return > 0 ) {
saveJSON('active.php', $revised_actives);
echo formatJSEND("success"); echo formatJSEND( "success" );
} }
}
//////////////////////////////////////////////////////////////////
// Remove File //////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////// // Rename File
//////////////////////////////////////////////////////////////////
public function Remove()
{ public function Rename() {
foreach ($this->actives as $active => $data) {
if (is_array($data) && isset($data['username']) && $this->username==$data['username'] && $this->path==$data['path']) { global $sql;
unset($this->actives[$active]); $query = "UPDATE active SET path=? WHERE path=?;";
} $bind_variables = array( $this->new_path, $this->path );
} $return = $sql->query( $query, $bind_variables, 0, "rowCount" );
saveJSON('active.php', $this->actives);
echo formatJSEND("success"); if( $return > 0 ) {
}
echo formatJSEND( "success" );
////////////////////////////////////////////////////////////////// }
// Remove All Files }
//////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////
public function RemoveAll() // Remove File
{ //////////////////////////////////////////////////////////////////
foreach ($this->actives as $active => $data) {
if (is_array($data) && isset($data['username']) && $this->username==$data['username']) { public function Remove() {
unset($this->actives[$active]);
} global $sql;
} $query = "DELETE FROM active WHERE path=? AND username=?;";
saveJSON('active.php', $this->actives); $bind_variables = array( $this->path, $this->username );
echo formatJSEND("success"); $return = $sql->query( $query, $bind_variables, 0, "rowCount" );
}
if( $return > 0 ) {
//////////////////////////////////////////////////////////////////
// Mark File As Focused echo formatJSEND( "success" );
// All other files will be marked as non-focused. }
////////////////////////////////////////////////////////////////// }
public function MarkFileAsFocused() //////////////////////////////////////////////////////////////////
{ // Remove All Files
foreach ($this->actives as $active => $data) { //////////////////////////////////////////////////////////////////
if (is_array($data) && isset($data['username']) && $this->username==$data['username']) {
$this->actives[$active]['focused']=false; public function RemoveAll() {
if ($this->path==$data['path']) {
$this->actives[$active]['focused']=true; global $sql;
} $query = "DELETE FROM active WHERE username=?;";
} $bind_variables = array( $this->username );
} $return = $sql->query( $query, $bind_variables, 0, "rowCount" );
saveJSON('active.php', $this->actives);
echo formatJSEND("success"); if( $return > 0 ) {
}
echo formatJSEND( "success" );
}
}
//////////////////////////////////////////////////////////////////
// Mark File As Focused
// All other files will be marked as non-focused.
//////////////////////////////////////////////////////////////////
public function MarkFileAsFocused() {
global $sql;
$query = "UPDATE active SET focused=? WHERE username=?;UPDATE active SET focused=? WHERE path=? AND username=?;";
$bind_variables = array( false, $this->username, true, $this->path, $this->username );
$return = $sql->query( $query, $bind_variables, 0, "rowCount" );
if( $return > 0 ) {
echo formatJSEND( "success" );
}
}
} }

View File

@ -669,9 +669,8 @@
var newSession = this.sessions[newPath]; var newSession = this.sessions[newPath];
// Change Editor Mode // Change Editor Mode
var ext = codiad.filemanager.getExtension(newPath); var mode = codiad.editor.selectMode(newPath);
var mode = codiad.editor.selectMode(ext);
// handle async mode change // handle async mode change
var fn = function() { var fn = function() {
codiad.editor.setModeDisplay(newSession); codiad.editor.setModeDisplay(newSession);

View File

@ -99,7 +99,17 @@
amplify.subscribe( 'active.onClose', function( path ) { amplify.subscribe( 'active.onClose', function( path ) {
let _this = codiad.auto_save; let _this = codiad.auto_save;
_this.editor.removeEventListener( "change", _this.change ); try {
_this.editor.removeEventListener( "change", _this.change );
} catch( e ) {
/**
* If the listener is not currently on file and we
* try to close it, the program will throw an exception and
* stop you from closing the file
*/
}
}); });
/* Subscribe to know when a file become active. */ /* Subscribe to know when a file become active. */

View File

@ -321,18 +321,18 @@
// //
////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////
getSettings: function() { getSettings: async function() {
var boolVal = null; let boolVal = null;
var _this = this; let _this = this;
var options = [ let options = [
'editor.fontSize', 'editor.fontSize',
'editor.overScroll', 'editor.overScroll',
'editor.printMarginColumn', 'editor.printMarginColumn',
'editor.tabSize', 'editor.tabSize',
'editor.theme', 'editor.theme',
]; ];
var bool_options = [ let bool_options = [
'editor.autocomplete', 'editor.autocomplete',
'settings.autosave', 'settings.autosave',
'editor.printMargin', 'editor.printMargin',
@ -345,9 +345,11 @@
'editor.persistentModal', 'editor.persistentModal',
]; ];
$.each( options, async function( idx, key ) { let user_settings = await codiad.settings.get_options();
$.each( options, function( idx, key ) {
let localValue = await codiad.settings.get_option( 'codiad.' + key ); let localValue = user_settings['codiad.' + key];
if ( localValue != null ) { if ( localValue != null ) {
_this.settings[key.split('.').pop()] = localValue; _this.settings[key.split('.').pop()] = localValue;
@ -356,7 +358,7 @@
$.each( bool_options, async function(idx, key) { $.each( bool_options, async function(idx, key) {
let localValue = await codiad.settings.get_option( 'codiad.' + key ); let localValue = user_settings['codiad.' + key];
if ( localValue != null ) { if ( localValue != null ) {
_this.settings[key.split('.').pop()] = (localValue == 'true'); _this.settings[key.split('.').pop()] = (localValue == 'true');

View File

@ -1,5 +1,12 @@
<?php <?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
require_once( __DIR__ . "/../sql/class.sql.php" );
require_once( __DIR__ . "/../settings/class.settings.php" );
class Install { class Install {
public $active = ""; public $active = "";
@ -15,24 +22,30 @@ class Install {
function __construct() { function __construct() {
$path = $_POST['path']; if( isset( $_POST["path"] ) ) {
$rel = str_replace( '/components/install/install.php', '', $_SERVER['REQUEST_URI'] );
$path = $_POST['path'];
$this->active = $path . "/data/active.php"; $rel = str_replace( '/components/install/install.php', '', $_SERVER['REQUEST_URI'] );
$this->config = $path . "/config.php";
$this->projects = $path . "/data/projects.php"; $this->active = $path . "/data/active.php";
$this->path = $path; $this->config = $path . "/config.php";
$this->sessions = $path . "/data/sessions"; $this->projects = $path . "/data/projects.php";
$this->users = $path . "/data/users.php"; $this->path = $path;
$this->rel = $rel; $this->sessions = $path . "/data/sessions";
$this->workspace = $path . "/workspace"; $this->users = $path . "/data/users.php";
$this->db_types = sql::db_types; $this->rel = $rel;
$this->workspace = $path . "/workspace";
$this->check(); $this->db_types = sql::DB_TYPES;
$this->project_name = $_POST["project_name"];
require_once( "../sql/class.sql.php" ); $this->project_path = $this->clean_path( $_POST["project_path"] );
$this->sql = new sql(); $this->username = $this->clean_username( $_POST["username"] );
$this->install(); $this->password = $this->encrypt_password( $_POST["password"] );
$this->check();
$this->sql = new sql();
$this->install();
exit;
}
} }
function check() { function check() {
@ -51,7 +64,12 @@ class Install {
if( ! in_array( DBTYPE, $this->db_types ) ) { if( ! in_array( DBTYPE, $this->db_types ) ) {
$this->JSEND( "Invalid database. Please select one of the following: " . implode( ", ", $db_types ), addslashes( json_encode( array( $dbtype, $db_types ) ) ) ); $this->JSEND( "Invalid database. Please select one of the following: " . implode( ", ", $db_types ), json_encode( array( $dbtype, $db_types ) ) );
}
if( ! is_dir( $this->sessions ) ) {
mkdir( $this->sessions, 00755 );
} }
} }
@ -134,20 +152,20 @@ define("WSURL", BASE_URL . "/workspace");
// Marketplace // Marketplace
//define("MARKETURL", "http://market.codiad.com/json"); //define("MARKETURL", "http://market.codiad.com/json");
'; ';
saveFile( $config, $config_data ); $this->save_file( $this->config, $config_data );
echo( "success" ); echo( "success" );
} }
function create_project() { function create_project() {
$project_path = $this->clean_path( $project_path ); $project_path = $this->project_path;
if ( ! $this->is_abs_path( $project_path ) ) { if ( ! $this->is_abs_path( $project_path ) ) {
$project_path = str_replace( " ", "_", preg_replace( '/[^\w-\.]/', '', $project_path ) ); $project_path = str_replace( " ", "_", preg_replace( '/[^\w-\.]/', '', $project_path ) );
if( ! is_dir( $workspace . "/" . $project_path ) ) { if( ! is_dir( $this->workspace . "/" . $project_path ) ) {
mkdir( $workspace . "/" . $project_path ); mkdir( $this->workspace . "/" . $project_path );
} }
} else { } else {
@ -171,11 +189,12 @@ define("WSURL", BASE_URL . "/workspace");
} }
$bind_variables = array( $bind_variables = array(
$project_name, $this->project_name,
$project_path, $project_path,
$username $this->username
); );
$query = "INSERT INTO projects(name, path, owner) VALUES (?,?,?);"; $query = "INSERT INTO projects(name, path, owner) VALUES (?,?,?);";
$connection = $this->sql->connect();
$statement = $connection->prepare( $query ); $statement = $connection->prepare( $query );
$statement->execute( $bind_variables ); $statement->execute( $bind_variables );
$error = $statement->errorInfo(); $error = $statement->errorInfo();
@ -190,6 +209,18 @@ define("WSURL", BASE_URL . "/workspace");
$this->sql->create_tables( $this->sql->create_tables(
array( array(
"active" => array(
"fields" => array(
"username" => "string",
"path" => "text",
"focused" => "string"
),
"attributes" => array(
"username" => array( "not null", "unique" ),
"path" => array( "not null", "unique" ),
"focused" => array( "not null" ),
)
),
"options" => array( "options" => array(
"fields" => array( "fields" => array(
"id" => "int", "id" => "int",
@ -216,7 +247,7 @@ define("WSURL", BASE_URL . "/workspace");
"name" => array( "not null" ), "name" => array( "not null" ),
"path" => array( "not null", "unique" ), "path" => array( "not null", "unique" ),
"owner" => array( "not null", "unique" ), "owner" => array( "not null", "unique" ),
"access" => array( "not null" ), "access" => array(),
) )
), ),
"users" => array( "users" => array(
@ -262,15 +293,16 @@ define("WSURL", BASE_URL . "/workspace");
$bind_variables = array( $bind_variables = array(
"", "",
"", "",
$username, $this->username,
$password, $this->password,
"", "",
$project_path, $this->project_path,
"admin", "admin",
"", "",
"" ""
); );
$query = "INSERT INTO users(first_name, last_name, username, password, email, project, access, groups, token) VALUES (?,?,?,?,?,?,?,?,?)"; $query = "INSERT INTO users(first_name, last_name, username, password, email, project, access, groups, token) VALUES (?,?,?,?,?,?,?,?,?)";
$connection = $this->sql->connect();
$statement = $connection->prepare( $query ); $statement = $connection->prepare( $query );
$statement->execute( $bind_variables ); $statement->execute( $bind_variables );
$error = $statement->errorInfo(); $error = $statement->errorInfo();
@ -279,6 +311,13 @@ define("WSURL", BASE_URL . "/workspace");
die( '{"message":"Could not create user in database.","error":"' . addslashes(json_encode( $error )) .'"}' ); die( '{"message":"Could not create user in database.","error":"' . addslashes(json_encode( $error )) .'"}' );
} }
$this->set_default_options();
}
function encrypt_password( $string ) {
return sha1( md5( $string ) );
} }
function is_abs_path( $path ) { function is_abs_path( $path ) {
@ -309,6 +348,7 @@ define("WSURL", BASE_URL . "/workspace");
$this->create_tables(); $this->create_tables();
$this->create_project(); $this->create_project();
$this->create_user(); $this->create_user();
//exit( "stop" );
$this->create_config(); $this->create_config();
} }
@ -325,6 +365,37 @@ define("WSURL", BASE_URL . "/workspace");
exit( json_encode( $message ) ); exit( json_encode( $message ) );
} }
function save_file( $file, $data ) {
$write = fopen( $file, 'w' ) or die( '{"message": "can\'t open file"}' );
fwrite( $write, $data );
fclose( $write );
}
public function set_default_options() {
foreach( Settings::DEFAULT_OPTIONS as $id => $option ) {
$query = "INSERT INTO user_options ( name, username, value ) VALUES ( ?, ?, ? );";
$bind_variables = array(
$option["name"],
$this->username,
$option["value"],
);
$result = $this->sql->query( $query, $bind_variables, 0, "rowCount" );
if( $result == 0 ) {
$query = "UPDATE user_options SET value=? WHERE name=? AND username=?;";
$bind_variables = array(
$option["value"],
$option["name"],
$this->username,
);
$result = $this->sql->query( $query, $bind_variables, 0, "rowCount" );
}
}
}
} }
$Install = new Install(); $Install = new Install();

View File

@ -1,295 +0,0 @@
<?php
/*
* Copyright (c) Codiad & Kent Safranski (codiad.com), Isaac Brown (telaaedifex.com),
* distributed as-is and without warranty under the MIT License. See
* [root]/license.txt for more. This information must remain intact.
*/
//////////////////////////////////////////////////////////////////////
// Paths
//////////////////////////////////////////////////////////////////////
$path = $_POST['path'];
$rel = str_replace( '/components/install/process.php', '', $_SERVER['REQUEST_URI'] );
$workspace = $path . "/workspace";
$users = $path . "/data/users.php";
$projects = $path . "/data/projects.php";
$active = $path . "/data/active.php";
$sessions = $path . "/data/sessions";
$config = $path . "/config.php";
//////////////////////////////////////////////////////////////////////
// Functions
//////////////////////////////////////////////////////////////////////
function saveFile( $file, $data ) {
$write = fopen( $file, 'w' ) or die( '{"message": "can\'t open file"}' );
fwrite( $write, $data );
fclose( $write );
}
function saveJSON( $file, $data ) {
$data = "<?php/*|\r\n" . json_encode( $data ) . "\r\n|*/?>";
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',
'pgsql',
//'sqlite',
];
//Is selected database type valid?
if( ! in_array( $dbtype, $db_types ) ) {
die( '{"message": "Invalid database. Please select one of the following: ' . implode( ", ", $db_types ) . '.", "error": "' . addslashes(json_encode( array( $dbtype, $db_types ) ) ) . '"}' );
}
try {
$connection = new PDO( "{$dbtype}:host={$dbhost};dbname={$dbname}", $dbuser, $dbpass );
} catch( PDOException $e ) {
die( '{"message":"Could not connect to database.","error":"' . addslashes( json_encode( $e->getMessage() ) ) .'"}' );
}
$bind_vars = array();
$bind = "";
$database_sql_fullpath = $path . '/components/install/sql/' . $dbtype . '.sql';
if( ! is_file( $database_sql_fullpath ) ) {
die( '{"message":"Could not find the sql script for the database type: ' . $dbtype . '","error":"' . addslashes( json_encode( array( "path" => $database_sql_fullpath, "dbtype" => $dbtype ) ) ) .'"}' );
}
$sql = file_get_contents( $database_sql_fullpath );
try {
//Create the database
$result = $connection->exec( $sql );
} catch( PDOException $e ) {
die( '{"message":"Could not create initial tables in database.","error":"' . addslashes( json_encode( $e->getMessage() ) ) .'"}' );
}
$error = $connection->errorInfo();
if( ! $error[0] == "00000" ) {
die( '{"message":"Could not create initial tables in database.","error":"' . addslashes( json_encode( $error ) ) .'"}' );
}
//////////////////////////////////////////////////////////////////
// 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( '{"message": "Unable to create Absolute Path"}' );
}
} else {
if ( ! is_writable( $project_path ) || ! is_readable( $project_path ) ) {
die( '{"message": "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( '{"message":"Could not create project in database.","error":"' . addslashes(json_encode( $error )) .'"}' );
}
$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( '{"message":"Could not create user in database.","error":"' . addslashes(json_encode( $error )) .'"}' );
}
/**
* Create sessions path.
*/
if ( ! is_dir( $sessions ) ) {
mkdir( $sessions, 00755 );
}
//////////////////////////////////////////////////////////////////
// Create Active file
//////////////////////////////////////////////////////////////////
saveJSON( $active, array( '' ) );
//////////////////////////////////////////////////////////////////
// Create Config
//////////////////////////////////////////////////////////////////
$config_data = '<?php
/*
* Copyright (c) Codiad & Kent Safranski (codiad.com), Isaac Brown (telaaedifex.com),
* distributed as-is and without warranty under the MIT License. See
* [root]/license.txt for more. This information must remain intact.
*/
//////////////////////////////////////////////////////////////////
// CONFIG
//////////////////////////////////////////////////////////////////
// PATH TO CODIAD
define("BASE_PATH", "' . $path . '");
// BASE URL TO CODIAD (without trailing slash)
define("BASE_URL", "' . $_SERVER["HTTP_HOST"] . $rel . '");
// THEME : default, modern or clear (look at /themes)
define("THEME", "default");
// ABSOLUTE PATH
define("WHITEPATHS", BASE_PATH . ",/home");
// SESSIONS (e.g. 7200)
$cookie_lifetime = "0";
// TIMEZONE
date_default_timezone_set("' . $_POST['timezone'] . '");
// External Authentification
//define("AUTH_PATH", "/path/to/customauth.php");
// Site Name
define("SITE_NAME", "' . $_POST['site_name'] . '");
// Database Information
define( "DBHOST", "' . $_POST['dbhost'] . '" );
define( "DBNAME", "' . $_POST['dbname'] . '" );
define( "DBUSER", "' . $_POST['dbuser'] . '" );
define( "DBPASS", "' . $_POST['dbpass'] . '" );
define( "DBTYPE", "' . $_POST['dbtype'] . '" );
//////////////////////////////////////////////////////////////////
// ** DO NOT EDIT CONFIG BELOW **
//////////////////////////////////////////////////////////////////
// PATHS
define("COMPONENTS", BASE_PATH . "/components");
define("PLUGINS", BASE_PATH . "/plugins");
define("THEMES", BASE_PATH . "/themes");
define("DATA", BASE_PATH . "/data");
define("WORKSPACE", BASE_PATH . "/workspace");
// URLS
define("WSURL", BASE_URL . "/workspace");
// Marketplace
//define("MARKETURL", "http://market.codiad.com/json");
';
saveFile( $config, $config_data );
echo( "success" );
}

View File

@ -1,57 +0,0 @@
--
-- Table structure for table options
--
CREATE TABLE IF NOT EXISTS options (
id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
name varchar(255) NOT NULL,
value text NOT NULL,
CONSTRAINT option_name UNIQUE (name)
);
-- --------------------------------------------------------
--
-- Table structure for table projects
--
CREATE TABLE IF NOT EXISTS projects (
id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
name varchar(255) NOT NULL,
path varchar(255) NOT NULL,
owner varchar(255) NOT NULL,
access text,
CONSTRAINT project UNIQUE (path, owner)
);
-- --------------------------------------------------------
--
-- Table structure for table users
--
CREATE TABLE IF NOT EXISTS users (
id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
first_name varchar(255) DEFAULT NULL,
last_name varchar(255) DEFAULT NULL,
username varchar(255) NOT NULL,
password text NOT NULL,
email varchar(255) DEFAULT NULL,
project varchar(255) DEFAULT NULL,
access varchar(255) NOT NULL,
groups text,
token text,
CONSTRAINT username UNIQUE (username)
);
--
-- Table structure for table user_options
--
CREATE TABLE IF NOT EXISTS user_options (
id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
name varchar(255) NOT NULL,
username varchar(255) NOT NULL,
value text NOT NULL,
CONSTRAINT option_name UNIQUE (name,username)
);

View File

@ -1,53 +0,0 @@
--
-- Table structure for table options
--
CREATE TABLE IF NOT EXISTS options (
id SERIAL PRIMARY KEY,
name varchar(255) NOT NULL UNIQUE,
value TEXT NOT NULL
);
-- --------------------------------------------------------
--
-- Table structure for table projects
--
CREATE TABLE IF NOT EXISTS projects (
id SERIAL PRIMARY KEY,
name varchar(255) NOT NULL,
path varchar(255) NOT NULL UNIQUE,
owner varchar(255) NOT NULL UNIQUE,
access text
);
-- --------------------------------------------------------
--
-- Table structure for table users
--
CREATE TABLE IF NOT EXISTS users (
id SERIAL PRIMARY KEY,
first_name varchar(255) DEFAULT NULL,
last_name varchar(255) DEFAULT NULL,
username varchar(255) NOT NULL UNIQUE,
password text NOT NULL,
email varchar(255) DEFAULT NULL,
project varchar(255) DEFAULT NULL,
access varchar(255) NOT NULL,
groups text,
token text
);
--
-- Table structure for table user_options
--
CREATE TABLE IF NOT EXISTS user_options (
id SERIAL PRIMARY KEY,
name varchar(255) NOT NULL UNIQUE,
username varchar(255) NOT NULL UNIQUE,
value text NOT NULL
);

View File

@ -1,53 +0,0 @@
--
-- Table structure for table options
--
CREATE TABLE IF NOT EXISTS options (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name CHAR(255) NOT NULL UNIQUE,
value TEXT NOT NULL
);
-- --------------------------------------------------------
--
-- Table structure for table projects
--
CREATE TABLE IF NOT EXISTS projects (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name CHAR(255) NOT NULL,
path CHAR(255) NOT NULL UNIQUE,
owner CHAR(255) NOT NULL UNIQUE,
access text
);
-- --------------------------------------------------------
--
-- Table structure for table users
--
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
first_name CHAR(255) DEFAULT NULL,
last_name CHAR(255) DEFAULT NULL,
username CHAR(255) NOT NULL UNIQUE,
password text NOT NULL,
email CHAR(255) DEFAULT NULL,
project CHAR(255) DEFAULT NULL,
access CHAR(255) NOT NULL,
groups text,
token text
);
--
-- Table structure for table user_options
--
CREATE TABLE IF NOT EXISTS user_options (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name CHAR(255) NOT NULL UNIQUE,
username CHAR(255) NOT NULL UNIQUE,
value text NOT NULL
);

View File

@ -7,9 +7,9 @@
* [root]/license.txt for more. This information must remain intact. * [root]/license.txt for more. This information must remain intact.
*/ */
require_once( "./install.php" ); require_once( __DIR__ . "/install.php" );
$path = rtrim(str_replace("index.php", "", $_SERVER['SCRIPT_FILENAME']), "/"); $path = rtrim( str_replace("index.php", "", $_SERVER['SCRIPT_FILENAME']), "/");
$workspace = is_writable($path . "/workspace"); $workspace = is_writable($path . "/workspace");
$data = is_writable($path . "/data"); $data = is_writable($path . "/data");
@ -179,7 +179,8 @@ if ($newrelic) {
<label><?php i18n("Database Type"); ?></label> <label><?php i18n("Database Type"); ?></label>
<select name="dbtype"> <select name="dbtype">
<?php <?php
foreach ($aValidDBType as $db_name => $key) { $select_dbtypes = "";
foreach (sql::DB_TYPES as $db_name => $key) {
if ($autocomplete['dbtype'] == $key) { if ($autocomplete['dbtype'] == $key) {
$select_dbtypes .= '<option selected="selected" value="' . $key . '">' . $db_name . '</option>'; $select_dbtypes .= '<option selected="selected" value="' . $key . '">' . $db_name . '</option>';
} else { } else {
@ -366,7 +367,7 @@ if ($newrelic) {
if(!password_match){ alert('The passwords entered do not match'); } if(!password_match){ alert('The passwords entered do not match'); }
if(!empty_fields && password_match && check_path){ if(!empty_fields && password_match && check_path){
$.post('components/install/process.php',$('#install').serialize(),function( data ) { $.post('components/install/install.php',$('#install').serialize(),function( data ) {
if( data == 'success' ){ if( data == 'success' ){
window.location.reload(); window.location.reload();

View File

@ -257,6 +257,7 @@
$.get( this.controller + '?action=get_current', function( data ) { $.get( this.controller + '?action=get_current', function( data ) {
var projectInfo = codiad.jsend.parse( data ); var projectInfo = codiad.jsend.parse( data );
if ( projectInfo != 'error' ) { if ( projectInfo != 'error' ) {
$( '#file-manager' ) $( '#file-manager' )

View File

@ -168,6 +168,36 @@ class Settings {
} }
} }
public function get_options( $action = "return" ) {
global $sql;
$query = "SELECT name, value FROM user_options WHERE username=?;";
$bind_variables = array( $option, $this->username );
$return = $sql->query( $query, $bind_variables, array() );
$options = array();
foreach( $return as $id => $data ) {
$options[$data["name"]] = $data["value"];
}
$options = json_encode( $options );
switch( $action ) {
case( "exit" ):
exit( $options );
break;
case( "return" ):
return( $options );
break;
}
}
////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////
// Save User Settings // Save User Settings
////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////

View File

@ -54,6 +54,12 @@ if ( $_GET['action'] == 'get_option' ) {
$Settings->get_option( $_POST['option'], $_SESSION["user"], "exit" ); $Settings->get_option( $_POST['option'], $_SESSION["user"], "exit" );
} }
if ( $_GET['action'] == 'get_options' ) {
$Settings->username = $_SESSION['user'];
$Settings->get_options( "exit" );
}
if ( $_GET['action'] == 'update_option' ) { if ( $_GET['action'] == 'update_option' ) {
$Settings->username = $_SESSION['user']; $Settings->username = $_SESSION['user'];

View File

@ -56,6 +56,29 @@
} }
}, },
get_options: async function() {
let result;
try {
result = await $.ajax({
url: this.controller + '?action=get_options',
type: "POST",
dataType: 'html',
data: {
},
});
return result;
} catch (error) {
console.log(error);
throw error;
}
},
////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////
// Save Settings // Save Settings
////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////
@ -142,7 +165,7 @@
}, },
success: function( data ) { success: function( data ) {
console.log( "Data: " + data ) console.log( `Update Option ( ${option} ): ` + data )
}, },
error: function(jqXHR, textStatus, errorThrown) { error: function(jqXHR, textStatus, errorThrown) {

View File

@ -165,9 +165,9 @@ class sql_conversions {
"unique" => array( "unique" => array(
"mysql" => "CONSTRAINT '%constraint_name%' UNIQUE ( %field_names% )", "mysql" => "CONSTRAINT %constraint_name% UNIQUE ( %field_names% )",
"pgsql" => "CONSTRAINT '%constraint_name%' UNIQUE ( %field_names% )", "pgsql" => "CONSTRAINT %constraint_name% UNIQUE ( %field_names% )",
"sqlite" => "CONSTRAINT '%constraint_name%' UNIQUE ( %field_names% )", "sqlite" => "CONSTRAINT %constraint_name% UNIQUE ( %field_names% )",
), ),
); );
@ -338,33 +338,44 @@ class sql_conversions {
$query .= "{$id} {$this->data_types[$type][$dbtype]}"; $query .= "{$id} {$this->data_types[$type][$dbtype]}";
foreach( $attributes[$id] as $attribute ) { if( isset( $attributes[$id] ) ) {
$attribute_string = $this->specials["$attribute"][$dbtype]; foreach( $attributes[$id] as $attribute ) {
if( $attribute == "unique" ) {
continue; $attribute_string = $this->specials["$attribute"][$dbtype];
}
if( ! strpos( $attribute_string, "%table_name%" ) === FALSE ) {
$attribute_string = str_replace( "%table_name%", $table_name, $attribute_string ); if( $attribute == "unique" ) {
}
if( ! strpos( $attribute_string, "%fields%" ) === FALSE ) {
$fields_string = "";
foreach( $fields as $field ) {
$fields_string .= "{$id_open}field{$id_close},"; continue;
} }
$fields_string = substr( $fields_string, 0, -1 ); if( $dbtype == "pgsql" ) {
$attribute_string = str_replace( "%fields%", $fields_string, $attribute_string );
if( $id == "id" ) {
$query = substr( $query, 0, -( strlen( " {$this->data_types[$type][$dbtype]}" ) ) );
}
}
if( ! strpos( $attribute_string, "%table_name%" ) === FALSE ) {
$attribute_string = str_replace( "%table_name%", $table_name, $attribute_string );
}
if( ! strpos( $attribute_string, "%fields%" ) === FALSE ) {
$fields_string = "";
foreach( $fields as $field ) {
$fields_string .= "{$id_open}field{$id_close},";
}
$fields_string = substr( $fields_string, 0, -1 );
$attribute_string = str_replace( "%fields%", $fields_string, $attribute_string );
}
$query .= " {$attribute_string}";
} }
$query .= " {$attribute_string}";
} }
$query .= ","; $query .= ",";
} }
@ -380,14 +391,14 @@ class sql_conversions {
if( $unique_string == "" ) { if( $unique_string == "" ) {
$unique_string = $this->specials["unique"] . ","; $unique_string = $this->specials["unique"][$dbtype] . ",";
} }
$fields_string .= "{$id_open}{$id}{$id_close},"; $fields_string .= "{$id_open}{$id}{$id_close},";
} }
} }
$unique_string = str_replace( "%constraint_name%", $fields_string, $unique_string ); $unique_string = str_replace( "%constraint_name%", strtolower( preg_replace( '#[^A-Za-z0-9' . preg_quote( '-_@. ').']#', '', $fields_string ) ), $unique_string );
$unique_string = str_replace( "%field_names%", $fields_string, $unique_string ); $unique_string = str_replace( "%field_names%", substr( $fields_string, 0, -1 ), $unique_string );
$query .= $unique_string; $query .= $unique_string;
$query = substr( $query, 0, -1 ); $query = substr( $query, 0, -1 );

View File

@ -4,6 +4,13 @@ require_once( __DIR__ . "/class.sql.conversions.php" );
class sql { class sql {
const DB_TYPES = array(
"MySQL" => "mysql",
"PostgresSQL" => "pgsql",
"SQLite" => "sqlite",
);
public $connection = null; public $connection = null;
public $conversions = null; public $conversions = null;
public $identifier_character = null; public $identifier_character = null;
@ -77,8 +84,9 @@ class sql {
*/ */
$query = $this->conversions->tables( $table ); $query = $this->conversions->tables( $table );
//echo var_dump( $query ) . "<br>"; $connection = $this->connect();
$result = $this->query( $query, array(), array() ); $result = $connection->exec( $query );
//echo var_dump( $query, $result, $connection->errorInfo() ) . "<br>";
} }
public static function escape_identifier( $i ) { public static function escape_identifier( $i ) {
@ -156,6 +164,7 @@ class sql {
} }
$error = $statement->errorInfo(); $error = $statement->errorInfo();
if( ! $error[0] == "00000" ) { if( ! $error[0] == "00000" ) {
echo var_export( $error ); echo var_export( $error );

View File

@ -252,7 +252,7 @@ class User {
$_SESSION['lang'] = $this->lang; $_SESSION['lang'] = $this->lang;
$_SESSION['theme'] = $this->theme; $_SESSION['theme'] = $this->theme;
$_SESSION["login_session"] = true; $_SESSION["login_session"] = true;
$user = $return; $user = $return[0];
$query = "UPDATE users SET token=? WHERE username=?;"; $query = "UPDATE users SET token=? WHERE username=?;";
$bind_variables = array( sha1( $token ), $this->username ); $bind_variables = array( sha1( $token ), $this->username );