2018-07-13 18:39:55 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) Codiad & Andr3as, distributed
|
|
|
|
* as-is and without warranty under the MIT License. See
|
|
|
|
* [root]/license.txt for more. This information must remain intact.
|
|
|
|
*/
|
|
|
|
|
2018-10-09 21:30:00 +02:00
|
|
|
class Settings {
|
|
|
|
|
2018-11-19 19:30:49 +01:00
|
|
|
const DEFAULT_OPTIONS = array(
|
2018-12-12 16:30:14 +01:00
|
|
|
array(
|
|
|
|
"name" => "codiad.editor.autocomplete",
|
|
|
|
"value" => "false",
|
|
|
|
),
|
2019-01-15 16:33:59 +01:00
|
|
|
array(
|
|
|
|
"name" => "codiad.editor.autosave",
|
|
|
|
"value" => "true",
|
|
|
|
),
|
2018-11-19 19:30:49 +01:00
|
|
|
array(
|
|
|
|
"name" => "codiad.editor.fileManagerTrigger",
|
|
|
|
"value" => "false",
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
"name" => "codiad.editor.fontSize",
|
|
|
|
"value" => "14px",
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
"name" => "codiad.editor.highlightLine",
|
|
|
|
"value" => "true",
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
"name" => "codiad.editor.indentGuides",
|
|
|
|
"value" => "true",
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
"name" => "codiad.editor.overScroll",
|
|
|
|
"value" => "0.5",
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
"name" => "codiad.editor.persistentModal",
|
|
|
|
"value" => "true",
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
"name" => "codiad.editor.printMargin",
|
|
|
|
"value" => "true",
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
"name" => "codiad.editor.printMarginColumn",
|
|
|
|
"value" => "80",
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
"name" => "codiad.editor.rightSidebarTrigger",
|
|
|
|
"value" => "false",
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
"name" => "codiad.editor.softTabs",
|
|
|
|
"value" => "false",
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
"name" => "codiad.editor.tabSize",
|
|
|
|
"value" => "4",
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
"name" => "codiad.editor.theme",
|
|
|
|
"value" => "twilight",
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
"name" => "codiad.editor.wrapMode",
|
|
|
|
"value" => "false",
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
"name" => "codiad.settings.autosave",
|
|
|
|
"value" => "true",
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
"name" => "codiad.settings.plugin.sync",
|
|
|
|
"value" => "true",
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
"name" => "codiad.settings.plugin.sync",
|
|
|
|
"value" => "true",
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
2018-10-09 21:30:00 +02:00
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
// PROPERTIES
|
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
public $connection = '';
|
|
|
|
public $username = '';
|
|
|
|
public $settings = '';
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
// METHODS
|
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// -----------------------------||----------------------------- //
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
// Construct
|
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
}
|
|
|
|
|
|
|
|
public function delete_option( $option, $username = null ) {
|
|
|
|
|
|
|
|
if( $username == null ) {
|
|
|
|
|
|
|
|
$query = "DELETE FROM options WHERE `name`=?";
|
|
|
|
$bind = "s";
|
|
|
|
$bind_variables = array(
|
|
|
|
$option,
|
|
|
|
);
|
|
|
|
|
|
|
|
sql::sql( $query, $bind, $bind_variables, formatJSEND( "error", "Could not delete setting: $option" ) );
|
|
|
|
} else {
|
|
|
|
|
|
|
|
$query = "DELETE FROM options WHERE `name`=? AND `username`=?";
|
|
|
|
$bind = "ss";
|
|
|
|
$bind_variables = array(
|
|
|
|
$option,
|
|
|
|
$this->username,
|
|
|
|
);
|
|
|
|
|
|
|
|
sql::sql( $query, $bind, $bind_variables, formatJSEND( "error", "Could not delete setting: $option" ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function get_option( $option, $user_setting = null, $action = "return" ) {
|
|
|
|
|
|
|
|
if( $user_setting == null ) {
|
|
|
|
|
2018-10-11 16:17:41 +02:00
|
|
|
$sql = "SELECT `value` FROM `options` WHERE `name`=?;";
|
2018-10-09 21:30:00 +02:00
|
|
|
$bind = "s";
|
|
|
|
$bind_variables = array( $option );
|
|
|
|
$return = sql::sql( $sql, $bind, $bind_variables, formatJSEND( "error", "Error fetching option: $option" ) );
|
|
|
|
|
|
|
|
if( mysqli_num_rows( $return ) > 0 ) {
|
|
|
|
|
|
|
|
$return = mysqli_fetch_assoc( $return )["value"];
|
|
|
|
} else {
|
|
|
|
|
|
|
|
$return = null;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
|
2018-10-11 16:17:41 +02:00
|
|
|
$sql = "SELECT `value` FROM `user_options` WHERE `name`=? AND `username`=?;";
|
2018-10-09 21:30:00 +02:00
|
|
|
$bind = "ss";
|
|
|
|
$bind_variables = array( $option, $this->username );
|
|
|
|
$return = sql::sql( $sql, $bind, $bind_variables, formatJSEND( "error", "Error fetching option: $option" ) );
|
|
|
|
|
|
|
|
if( mysqli_num_rows( $return ) > 0 ) {
|
|
|
|
|
|
|
|
$return = mysqli_fetch_assoc( $return )["value"];
|
|
|
|
} else {
|
|
|
|
|
|
|
|
$return = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
switch( $action ) {
|
|
|
|
|
|
|
|
case( "exit" ):
|
|
|
|
|
|
|
|
exit( $return );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case( "return" ):
|
|
|
|
|
|
|
|
return( $return );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
// Save User Settings
|
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
public function Save() {
|
|
|
|
|
|
|
|
foreach( $this->settings as $option => $value ) {
|
|
|
|
|
|
|
|
$this->update_option( $option, $value, $this->username );
|
|
|
|
}
|
|
|
|
echo formatJSEND( "success", null );
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
// Load User Settings
|
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
public function Load() {
|
|
|
|
|
|
|
|
$query = "SELECT DISTINCT * FROM user_options WHERE `username`=?;";
|
|
|
|
$bind = "s";
|
|
|
|
$bind_variables = array(
|
|
|
|
$this->username
|
|
|
|
);
|
|
|
|
|
|
|
|
$options = sql::sql( $query, $bind, $bind_variables, formatJSEND( "error", "Error, Could not load user's settings." ) );
|
|
|
|
echo formatJSEND( "success", $options );
|
|
|
|
}
|
|
|
|
|
|
|
|
public function update_option( $option, $value, $user_setting = null ) {
|
|
|
|
|
2018-10-11 16:17:41 +02:00
|
|
|
$query = "INSERT INTO user_options ( `name`, `username`, `value` ) VALUES ( ?, ?, ? );";
|
2018-10-09 21:30:00 +02:00
|
|
|
$bind = "sss";
|
|
|
|
$bind_variables = array(
|
|
|
|
$option,
|
|
|
|
$this->username,
|
|
|
|
$value,
|
|
|
|
);
|
|
|
|
$result = sql::sql( $query, $bind, $bind_variables, formatJSEND( "error", "Error, Could not load user's settings." ) );
|
|
|
|
|
|
|
|
if( $result !== true ) {
|
|
|
|
|
2018-10-11 16:17:41 +02:00
|
|
|
$query = "UPDATE user_options SET `value`=? WHERE `name`=? AND `username`=?;";
|
2018-10-09 21:30:00 +02:00
|
|
|
$bind = "sss";
|
|
|
|
$bind_variables = array(
|
|
|
|
$value,
|
|
|
|
$option,
|
|
|
|
$this->username,
|
|
|
|
);
|
|
|
|
$result = sql::sql( $query, $bind, $bind_variables, formatJSEND( "error", "Error, Could not load user's settings." ) );
|
|
|
|
}
|
|
|
|
}
|
2018-07-13 18:39:55 +02:00
|
|
|
}
|