2018-07-13 18:39:55 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
2019-02-04 22:42:12 +01:00
|
|
|
* Copyright (c) Codiad & Kent Safranski (codiad.com), Isaac Brown
|
|
|
|
* distributed as-is and without warranty under the MIT License. See
|
2018-07-13 18:39:55 +02:00
|
|
|
* [root]/license.txt for more. This information must remain intact.
|
|
|
|
*/
|
|
|
|
|
2018-10-09 22:04:48 +02:00
|
|
|
require_once( '../../common.php' );
|
2018-07-13 18:39:55 +02:00
|
|
|
|
2018-10-09 22:04:48 +02:00
|
|
|
class Project extends Common {
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
// PROPERTIES
|
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
|
2019-07-01 15:24:34 +02:00
|
|
|
public $access = 100;
|
|
|
|
public $name = '';
|
|
|
|
public $path = '';
|
|
|
|
public $gitrepo = false;
|
|
|
|
public $gitbranch = '';
|
|
|
|
public $projects = array();
|
|
|
|
public $no_return = false;
|
|
|
|
public $assigned = false;
|
2018-10-09 22:04:48 +02:00
|
|
|
public $command_exec = '';
|
2018-11-10 06:41:28 +01:00
|
|
|
public $public_project = false;
|
2018-11-19 19:30:49 +01:00
|
|
|
public $user = '';
|
2018-10-09 22:04:48 +02:00
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
// METHODS
|
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// -----------------------------||----------------------------- //
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
// Construct
|
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
public function __construct() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
// NEW METHODS
|
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
|
2018-10-11 16:17:41 +02:00
|
|
|
public function add_project( $project_name, $project_path, $owner = null ) {
|
2018-10-09 22:04:48 +02:00
|
|
|
|
2019-02-04 22:42:12 +01:00
|
|
|
global $sql;
|
2018-11-10 06:41:28 +01:00
|
|
|
if( $this->public_project ) {
|
2018-10-11 16:17:41 +02:00
|
|
|
|
2018-11-10 06:41:28 +01:00
|
|
|
$owner = 'nobody';
|
2018-10-11 16:17:41 +02:00
|
|
|
} else {
|
|
|
|
|
2018-11-10 06:41:28 +01:00
|
|
|
$owner = $_SESSION["user"];
|
2018-10-11 16:17:41 +02:00
|
|
|
}
|
2018-10-09 22:04:48 +02:00
|
|
|
|
2019-02-04 22:42:12 +01:00
|
|
|
$query = "INSERT INTO projects( name, path, owner ) VALUES ( ?, ?, ? );";
|
2018-10-11 16:17:41 +02:00
|
|
|
$bind_variables = array( $project_name, $project_path, $owner );
|
2019-02-04 22:42:12 +01:00
|
|
|
$return = $sql->query( $query, $bind_variables, 0, "rowCount" );
|
2018-10-11 16:17:41 +02:00
|
|
|
|
Changed $path to __DIR__ for config location, Updated auto reload variables, Removed unload listener for auto reload, Changed project default to array so that if no projects exist the program does not crash, Updated autosave to use let instead of vars, Fixed capitalization for sideExpanded variable, Added try catch to pdo initialization on install, Added more error checks on install, Removed password function on install query, Changed default settings array, Added loading div to user delete, Updated queries that threw errors when a default value was zero, Added blank username and password check,
2019-02-09 22:14:27 +01:00
|
|
|
if( ! ( $return > 0 ) ) {
|
2019-02-04 22:42:12 +01:00
|
|
|
|
Changed $path to __DIR__ for config location, Updated auto reload variables, Removed unload listener for auto reload, Changed project default to array so that if no projects exist the program does not crash, Updated autosave to use let instead of vars, Fixed capitalization for sideExpanded variable, Added try catch to pdo initialization on install, Added more error checks on install, Removed password function on install query, Changed default settings array, Added loading div to user delete, Updated queries that threw errors when a default value was zero, Added blank username and password check,
2019-02-09 22:14:27 +01:00
|
|
|
exit( formatJSEND( "error", "Error creating project $project_name" ) );
|
2019-02-04 22:42:12 +01:00
|
|
|
}
|
2018-10-09 22:04:48 +02:00
|
|
|
}
|
|
|
|
|
2018-11-19 19:30:49 +01:00
|
|
|
public function add_user() {
|
|
|
|
|
2019-02-04 22:42:12 +01:00
|
|
|
global $sql;
|
|
|
|
$query = "SELECT access FROM projects WHERE path=? AND owner=?";
|
2018-11-19 19:30:49 +01:00
|
|
|
$bind_variables = array( $this->path, $_SESSION["user"] );
|
2019-02-04 22:42:12 +01:00
|
|
|
$result = $sql->query( $query, $bind_variables, array() )[0];
|
2019-07-01 15:24:34 +02:00
|
|
|
|
2019-02-04 22:42:12 +01:00
|
|
|
if( ! empty( $result ) ) {
|
2018-11-19 19:30:49 +01:00
|
|
|
|
2019-02-04 22:42:12 +01:00
|
|
|
$access = json_decode( $result["access"] );
|
2018-11-19 19:30:49 +01:00
|
|
|
|
2019-07-01 15:24:34 +02:00
|
|
|
if( is_array( $access ) && ! empty( $access ) ) {
|
|
|
|
|
|
|
|
$is_assoc = ( array_keys( $access ) !== range( 0, count( $access ) - 1 ) );
|
2018-11-19 19:30:49 +01:00
|
|
|
|
2019-07-01 15:24:34 +02:00
|
|
|
if( $is_assoc ) {
|
2018-11-19 19:30:49 +01:00
|
|
|
|
2019-07-01 15:24:34 +02:00
|
|
|
$access[$this->user] = $this->access;
|
|
|
|
} else {
|
|
|
|
|
|
|
|
$new_access = array();
|
|
|
|
foreach( $access as $user ) {
|
|
|
|
|
|
|
|
$new_access[$user] = Permission::LEVELS["delete"];
|
|
|
|
}
|
|
|
|
$access[$this->user] = $this->access;
|
|
|
|
$access = $new_access;
|
2018-11-19 19:30:49 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
|
|
|
|
$access = array(
|
2019-07-01 15:24:34 +02:00
|
|
|
$this->user => $this->access
|
2018-11-19 19:30:49 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$access = json_encode( $access );
|
2019-02-04 22:42:12 +01:00
|
|
|
$query = "UPDATE projects SET access=? WHERE path=? AND owner=?;";
|
2018-11-19 19:30:49 +01:00
|
|
|
$bind_variables = array( $access, $this->path, $_SESSION["user"] );
|
2019-02-04 22:42:12 +01:00
|
|
|
$return = $sql->query( $query, $bind_variables, 0, "rowCount" );
|
|
|
|
|
|
|
|
if( $result > 0 ) {
|
2018-11-19 19:30:49 +01:00
|
|
|
|
|
|
|
echo( formatJSEND( "success", "Successfully added {$this->user}." ) );
|
|
|
|
} else {
|
|
|
|
|
2019-02-04 22:42:12 +01:00
|
|
|
echo formatJSEND( "error", "Error setting access for project." );
|
2018-11-19 19:30:49 +01:00
|
|
|
}
|
2019-02-04 22:42:12 +01:00
|
|
|
} else {
|
|
|
|
|
|
|
|
echo formatJSEND( "error", "Error fetching projects." );
|
2018-11-19 19:30:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-10 06:41:28 +01:00
|
|
|
public function check_owner( $path = null, $exclude_public = false ) {
|
2018-10-09 22:04:48 +02:00
|
|
|
|
2018-11-10 06:41:28 +01:00
|
|
|
if( $path === null ) {
|
2018-10-11 16:17:41 +02:00
|
|
|
|
2018-11-10 06:41:28 +01:00
|
|
|
$path = $this->path;
|
|
|
|
}
|
2019-02-04 22:42:12 +01:00
|
|
|
global $sql;
|
|
|
|
$query = "SELECT owner FROM projects WHERE path=?";
|
2018-11-10 06:41:28 +01:00
|
|
|
$bind_variables = array( $path );
|
2019-02-04 22:42:12 +01:00
|
|
|
$result = $sql->query( $query, $bind_variables, array() )[0];
|
2018-11-10 06:41:28 +01:00
|
|
|
$return = false;
|
|
|
|
|
2019-02-04 22:42:12 +01:00
|
|
|
if( ! empty( $result ) ) {
|
2018-11-10 06:41:28 +01:00
|
|
|
|
2019-02-04 22:42:12 +01:00
|
|
|
$owner = $result["owner"];
|
2018-11-10 06:41:28 +01:00
|
|
|
if( $exclude_public ) {
|
|
|
|
|
|
|
|
if( $owner == $_SESSION["user"] ) {
|
|
|
|
|
|
|
|
$return = true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
|
|
|
|
if( $owner == $_SESSION["user"] || $owner == 'nobody' ) {
|
|
|
|
|
|
|
|
$return = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return( $return );
|
|
|
|
}
|
|
|
|
|
|
|
|
public function get_access( $path = null ) {
|
|
|
|
|
|
|
|
if( $path === null ) {
|
|
|
|
|
|
|
|
$path = $this->path;
|
|
|
|
}
|
2019-02-04 22:42:12 +01:00
|
|
|
global $sql;
|
|
|
|
$query = "SELECT access FROM projects WHERE path=?";
|
2018-11-10 06:41:28 +01:00
|
|
|
$bind_variables = array( $path );
|
2019-02-04 22:42:12 +01:00
|
|
|
$return = $sql->query( $query, $bind_variables, array() )[0];
|
2018-11-10 06:41:28 +01:00
|
|
|
|
2019-02-04 22:42:12 +01:00
|
|
|
if( ! empty( $return ) ) {
|
2018-11-10 06:41:28 +01:00
|
|
|
|
2019-02-04 22:42:12 +01:00
|
|
|
$return = $return["access"];
|
2018-10-11 16:17:41 +02:00
|
|
|
} else {
|
|
|
|
|
2018-11-10 06:41:28 +01:00
|
|
|
$return = formatJSEND( "error", "Error fetching project info." );
|
2018-10-11 16:17:41 +02:00
|
|
|
}
|
|
|
|
|
2018-11-10 06:41:28 +01:00
|
|
|
return( $return );
|
|
|
|
}
|
|
|
|
|
|
|
|
public function get_owner( $path = null ) {
|
|
|
|
|
|
|
|
if( $path === null ) {
|
|
|
|
|
|
|
|
$path = $this->path;
|
|
|
|
}
|
2019-02-04 22:42:12 +01:00
|
|
|
global $sql;
|
|
|
|
$query = "SELECT owner FROM projects WHERE path=?";
|
2018-11-10 06:41:28 +01:00
|
|
|
$bind_variables = array( $path );
|
2019-02-04 22:42:12 +01:00
|
|
|
$return = $sql->query( $query, $bind_variables, array() )[0];
|
2018-10-09 22:04:48 +02:00
|
|
|
|
2019-02-04 22:42:12 +01:00
|
|
|
if( ! empty( $return ) ) {
|
2018-10-11 16:17:41 +02:00
|
|
|
|
2019-02-04 22:42:12 +01:00
|
|
|
$return = $return["owner"];
|
2018-11-10 06:41:28 +01:00
|
|
|
} else {
|
2018-10-11 16:17:41 +02:00
|
|
|
|
2018-11-10 06:41:28 +01:00
|
|
|
$return = formatJSEND( "error", "Error fetching project info." );
|
2018-10-11 16:17:41 +02:00
|
|
|
}
|
2018-11-10 06:41:28 +01:00
|
|
|
|
|
|
|
return( $return );
|
|
|
|
}
|
|
|
|
|
|
|
|
public function get_project( $project = null ) {
|
|
|
|
|
|
|
|
if( $project === null ) {
|
|
|
|
|
|
|
|
$project = $this->path;
|
|
|
|
}
|
2019-02-04 22:42:12 +01:00
|
|
|
global $sql;
|
2019-07-02 00:22:33 +02:00
|
|
|
$query = "
|
|
|
|
SELECT * FROM projects
|
|
|
|
WHERE path = ?
|
|
|
|
AND (
|
|
|
|
owner=?
|
|
|
|
OR owner='nobody'
|
|
|
|
OR id IN ( SELECT project FROM access WHERE user = ? )
|
|
|
|
) ORDER BY name;";
|
|
|
|
$bind_variables = array( $project, $_SESSION["user"], $_SESSION["user_id"] );
|
|
|
|
//$query = "SELECT * FROM projects WHERE path=? AND ( owner=? OR owner='nobody' ) ORDER BY name;";
|
|
|
|
//$bind_variables = array( $project, $_SESSION["user"] );
|
2019-02-04 22:42:12 +01:00
|
|
|
$return = $sql->query( $query, $bind_variables, array() )[0];
|
2018-11-10 06:41:28 +01:00
|
|
|
|
2019-02-04 22:42:12 +01:00
|
|
|
if( ! empty( $return ) ) {
|
2018-11-10 06:41:28 +01:00
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
$return = formatJSEND( "error", "Error fetching projects." );
|
|
|
|
}
|
|
|
|
|
|
|
|
return( $return );
|
2018-10-09 22:04:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function get_projects() {
|
|
|
|
|
2019-02-04 22:42:12 +01:00
|
|
|
global $sql;
|
2019-07-02 00:22:33 +02:00
|
|
|
$query = "
|
|
|
|
SELECT * FROM projects
|
|
|
|
WHERE owner=?
|
|
|
|
OR owner='nobody'
|
|
|
|
OR path IN ( SELECT path FROM access WHERE user = ? );";
|
|
|
|
$bind_variables = array( $_SESSION["user"], $_SESSION["user_id"] );
|
2019-02-04 22:42:12 +01:00
|
|
|
$return = $sql->query( $query, $bind_variables, array() );
|
2018-10-09 22:04:48 +02:00
|
|
|
|
2019-02-04 22:42:12 +01:00
|
|
|
if( empty( $return ) ) {
|
2018-10-11 16:17:41 +02:00
|
|
|
|
|
|
|
$return = formatJSEND( "error", "Error fetching projects." );
|
|
|
|
}
|
|
|
|
|
|
|
|
return( $return );
|
2018-10-09 22:04:48 +02:00
|
|
|
}
|
|
|
|
|
2018-11-19 19:30:49 +01:00
|
|
|
public function remove_user() {
|
|
|
|
|
2019-02-04 22:42:12 +01:00
|
|
|
global $sql;
|
|
|
|
$query = "SELECT access FROM projects WHERE path=? AND owner=?";
|
2018-11-19 19:30:49 +01:00
|
|
|
$bind_variables = array( $this->path, $_SESSION["user"] );
|
2019-02-04 22:42:12 +01:00
|
|
|
$result = $sql->query( $query, $bind_variables, array() )[0];
|
2018-11-19 19:30:49 +01:00
|
|
|
|
2019-02-04 22:42:12 +01:00
|
|
|
if( ! empty( $result ) ) {
|
2018-11-19 19:30:49 +01:00
|
|
|
|
2019-02-04 22:42:12 +01:00
|
|
|
$access = json_decode( $result["access"] );
|
2018-11-19 19:30:49 +01:00
|
|
|
|
|
|
|
if( is_array( $access ) ) {
|
|
|
|
|
|
|
|
$key = array_search( $this->user, $access );
|
|
|
|
|
|
|
|
if ( $key !== false ) {
|
|
|
|
|
|
|
|
unset( $access[$key] );
|
|
|
|
} else {
|
|
|
|
|
|
|
|
echo( formatJSEND( "error", "{$this->user} is not in the access list." ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$access = json_encode( $access );
|
2019-02-04 22:42:12 +01:00
|
|
|
$query = "UPDATE projects SET access=? WHERE path=? AND owner=?;";
|
2018-11-19 19:30:49 +01:00
|
|
|
$bind_variables = array( $access, $this->path, $_SESSION["user"] );
|
2019-02-04 22:42:12 +01:00
|
|
|
$return = $sql->query( $query, $bind_variables, 0, "rowCount" );
|
|
|
|
|
|
|
|
if( $return > 0 ) {
|
2018-11-19 19:30:49 +01:00
|
|
|
|
|
|
|
echo( formatJSEND( "success", "Successfully removed {$this->user}." ) );
|
|
|
|
} else {
|
|
|
|
|
2019-02-04 22:42:12 +01:00
|
|
|
echo formatJSEND( "error", "Error setting access for project." );
|
2018-11-19 19:30:49 +01:00
|
|
|
}
|
2019-02-04 22:42:12 +01:00
|
|
|
} else {
|
|
|
|
|
|
|
|
echo formatJSEND( "error", "Error fetching projects." );
|
2018-11-19 19:30:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-11 16:17:41 +02:00
|
|
|
public function rename_project( $old_name, $new_name, $path ) {
|
2018-10-09 22:04:48 +02:00
|
|
|
|
2019-02-04 22:42:12 +01:00
|
|
|
global $sql;
|
|
|
|
$query = "SELECT * FROM projects WHERE name=? AND path=? AND ( owner=? OR owner='nobody' );";
|
2018-10-11 16:17:41 +02:00
|
|
|
$bind_variables = array( $old_name, $path, $_SESSION["user"] );
|
2019-02-04 22:42:12 +01:00
|
|
|
$return = $sql->query( $query, $bind_variables, array() );
|
2019-02-06 23:52:49 +01:00
|
|
|
$pass = false;
|
2018-10-09 22:04:48 +02:00
|
|
|
|
2019-02-04 22:42:12 +01:00
|
|
|
if( ! empty( $return ) ) {
|
2018-10-11 16:17:41 +02:00
|
|
|
|
2019-02-04 22:42:12 +01:00
|
|
|
$query = "UPDATE projects SET name=? WHERE name=? AND path=? AND ( owner=? OR owner='nobody' );";
|
2018-10-11 16:17:41 +02:00
|
|
|
$bind_variables = array( $new_name, $old_name, $path, $_SESSION["user"] );
|
2019-02-04 22:42:12 +01:00
|
|
|
$return = $sql->query( $query, $bind_variables, 0, "rowCount");
|
|
|
|
|
|
|
|
if( $return > 0 ) {
|
|
|
|
|
|
|
|
echo( formatJSEND( "success", "Renamed " . htmlentities( $old_name ) . " to " . htmlentities( $new_name ) ) );
|
2019-02-06 23:52:49 +01:00
|
|
|
$pass = true;
|
2019-02-04 22:42:12 +01:00
|
|
|
} else {
|
|
|
|
|
2019-02-06 23:52:49 +01:00
|
|
|
exit( formatJSEND( "error", "Error renaming project." ) );
|
2019-02-04 22:42:12 +01:00
|
|
|
}
|
2018-10-11 16:17:41 +02:00
|
|
|
} else {
|
|
|
|
|
2019-02-04 22:42:12 +01:00
|
|
|
echo( formatJSEND( "error", "Error renaming project, could not find specified project." ) );
|
2018-10-11 16:17:41 +02:00
|
|
|
}
|
2019-02-06 23:52:49 +01:00
|
|
|
|
|
|
|
return $pass;
|
2018-10-09 22:04:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
// OLD METHODS
|
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// -----------------------------||----------------------------- //
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
// Get First (Default, none selected)
|
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
public function GetFirst() {
|
|
|
|
|
|
|
|
$this->name = $this->projects[0]['name'];
|
|
|
|
$this->path = $this->projects[0]['path'];
|
|
|
|
|
|
|
|
// Set Sessions
|
|
|
|
$_SESSION['project'] = $this->path;
|
|
|
|
|
|
|
|
if ( ! $this->no_return ) {
|
|
|
|
|
|
|
|
echo formatJSEND( "success", $this->projects[0] );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
// Get Name From Path
|
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
public function GetName() {
|
|
|
|
|
|
|
|
foreach ( $this->projects as $project => $data ) {
|
|
|
|
|
|
|
|
if ( $data['path'] == $this->path ) {
|
|
|
|
|
|
|
|
$this->name = $data['name'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $this->name;
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
// Open Project
|
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
public function Open() {
|
|
|
|
|
2019-02-04 22:42:12 +01:00
|
|
|
global $sql;
|
2019-07-02 00:22:33 +02:00
|
|
|
$query = "
|
|
|
|
SELECT * FROM projects
|
|
|
|
WHERE path = ?
|
|
|
|
AND (
|
|
|
|
owner=?
|
|
|
|
OR owner='nobody'
|
|
|
|
OR id IN ( SELECT project FROM access WHERE user = ? )
|
|
|
|
) ORDER BY name;";
|
|
|
|
$bind_variables = array( $this->path, $_SESSION["user"], $_SESSION["user_id"] );
|
2019-02-04 22:42:12 +01:00
|
|
|
$return = $sql->query( $query, $bind_variables, array() )[0];
|
2018-11-10 06:41:28 +01:00
|
|
|
|
2019-02-04 22:42:12 +01:00
|
|
|
if( ! empty( $return ) ) {
|
2018-10-09 22:04:48 +02:00
|
|
|
|
2019-02-04 22:42:12 +01:00
|
|
|
$query = "UPDATE users SET project=? WHERE username=?;";
|
2018-11-10 06:41:28 +01:00
|
|
|
$bind_variables = array( $this->path, $_SESSION["user"] );
|
2019-02-04 22:42:12 +01:00
|
|
|
$sql->query( $query, $bind_variables, 0, "rowCount" );
|
2018-11-10 06:41:28 +01:00
|
|
|
$this->name = $return['name'];
|
|
|
|
$_SESSION['project'] = $return['path'];
|
2019-07-02 00:22:33 +02:00
|
|
|
$_SESSION['project_id'] = $return['id'];
|
2019-02-04 22:42:12 +01:00
|
|
|
|
2018-10-09 22:04:48 +02:00
|
|
|
echo formatJSEND( "success", array( "name" => $this->name, "path" => $this->path ) );
|
|
|
|
} else {
|
|
|
|
|
|
|
|
echo formatJSEND( "error", "Error Opening Project" );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
// Create
|
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
public function Create() {
|
2019-07-01 15:24:34 +02:00
|
|
|
|
2018-10-09 22:04:48 +02:00
|
|
|
if ( $this->name != '' && $this->path != '' ) {
|
|
|
|
|
|
|
|
$this->path = $this->cleanPath();
|
|
|
|
$this->name = htmlspecialchars( $this->name );
|
|
|
|
if ( ! $this->isAbsPath( $this->path ) ) {
|
|
|
|
|
|
|
|
$this->path = $this->SanitizePath();
|
|
|
|
}
|
|
|
|
if ( $this->path != '' ) {
|
|
|
|
|
2019-02-04 22:42:12 +01:00
|
|
|
if( ! $this->public_project && ! $this->isAbsPath( $this->path ) ) {
|
2018-11-10 06:41:28 +01:00
|
|
|
|
Changed $path to __DIR__ for config location, Updated auto reload variables, Removed unload listener for auto reload, Changed project default to array so that if no projects exist the program does not crash, Updated autosave to use let instead of vars, Fixed capitalization for sideExpanded variable, Added try catch to pdo initialization on install, Added more error checks on install, Removed password function on install query, Changed default settings array, Added loading div to user delete, Updated queries that threw errors when a default value was zero, Added blank username and password check,
2019-02-09 22:14:27 +01:00
|
|
|
$user_path = WORKSPACE . '/' . preg_replace( '/[^\w-]/', '', strtolower( $_SESSION["user"] ) );
|
|
|
|
|
|
|
|
if( ! is_dir( $user_path ) ) {
|
|
|
|
|
|
|
|
mkdir( $user_path, 0755, true );
|
|
|
|
}
|
|
|
|
|
2018-11-10 06:41:28 +01:00
|
|
|
$this->path = $_SESSION["user"] . '/' . $this->path;
|
|
|
|
}
|
|
|
|
|
2018-10-09 22:04:48 +02:00
|
|
|
$pass = $this->checkDuplicate();
|
|
|
|
if ( $pass ) {
|
|
|
|
|
|
|
|
if ( ! $this->isAbsPath( $this->path ) ) {
|
|
|
|
|
|
|
|
mkdir( WORKSPACE . '/' . $this->path );
|
|
|
|
} else {
|
|
|
|
|
2019-03-11 15:04:02 +01:00
|
|
|
if( ! is_admin() ) {
|
|
|
|
|
|
|
|
die( formatJSEND( "error", "Absolute Paths are only allowed for admins" ) );
|
|
|
|
}
|
|
|
|
|
2018-10-09 22:04:48 +02:00
|
|
|
if ( defined( 'WHITEPATHS' ) ) {
|
|
|
|
|
|
|
|
$allowed = false;
|
|
|
|
foreach ( explode( ",", WHITEPATHS ) as $whitepath ) {
|
|
|
|
|
|
|
|
if ( strpos( $this->path, $whitepath ) === 0 ) {
|
|
|
|
|
|
|
|
$allowed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( ! $allowed) {
|
|
|
|
|
|
|
|
die( formatJSEND( "error", "Absolute Path Only Allowed for " . WHITEPATHS ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( ! file_exists( $this->path ) ) {
|
|
|
|
|
|
|
|
if ( ! mkdir( $this->path . '/', 0755, true ) ) {
|
|
|
|
|
|
|
|
die( formatJSEND( "error", "Unable to create Absolute Path" ) );
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
|
|
|
|
if ( ! is_writable( $this->path ) || ! is_readable( $this->path ) ) {
|
|
|
|
|
|
|
|
die( formatJSEND( "error", "No Read/Write Permission" ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->projects[] = array( "name" => $this->name, "path" => $this->path );
|
|
|
|
$this->add_project( $this->name, $this->path );
|
|
|
|
|
|
|
|
// Pull from Git Repo?
|
|
|
|
if ( $this->gitrepo && filter_var( $this->gitrepo, FILTER_VALIDATE_URL ) !== false ) {
|
|
|
|
|
|
|
|
$this->gitbranch = $this->SanitizeGitBranch();
|
|
|
|
if ( ! $this->isAbsPath( $this->path ) ) {
|
|
|
|
|
|
|
|
$this->command_exec = "cd " . escapeshellarg( WORKSPACE . '/' . $this->path ) . " && git init && git remote add origin " . escapeshellarg( $this->gitrepo ) . " && git pull origin " . escapeshellarg( $this->gitbranch );
|
|
|
|
} else {
|
|
|
|
|
|
|
|
$this->command_exec = "cd " . escapeshellarg( $this->path ) . " && git init && git remote add origin " . escapeshellarg( $this->gitrepo ) . " && git pull origin " . escapeshellarg( $this->gitbranch );
|
|
|
|
}
|
|
|
|
$this->ExecuteCMD();
|
|
|
|
}
|
|
|
|
|
|
|
|
echo formatJSEND( "success", array( "name" => $this->name, "path" => $this->path ) );
|
|
|
|
} else {
|
|
|
|
|
|
|
|
echo formatJSEND( "error", "A Project With the Same Name or Path Exists" );
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
|
|
|
|
echo formatJSEND( "error", "Project Name/Folder not allowed" );
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
|
|
|
|
echo formatJSEND( "error", "Project Name/Folder is empty" );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
// Sanitize GitBranch
|
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
public function SanitizeGitBranch() {
|
|
|
|
|
|
|
|
$sanitized = str_replace( array( "..", chr(40), chr(177), "~", "^", ":", "?", "*", "[", "@{", "\\" ), array( "" ), $this->gitbranch );
|
|
|
|
return $sanitized;
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
// Rename
|
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
public function Rename() {
|
|
|
|
|
|
|
|
$revised_array = array();
|
|
|
|
foreach ( $this->projects as $project => $data ) {
|
|
|
|
|
|
|
|
if ( $data['path'] != $this->path ) {
|
|
|
|
|
|
|
|
$revised_array[] = array( "name" => $data['name'], "path" => $data['path'] );
|
2018-10-11 16:17:41 +02:00
|
|
|
} else {
|
|
|
|
|
2019-02-06 23:52:49 +01:00
|
|
|
$rename = $this->rename_project( $data['name'], $_GET['project_name'], $data['path'] );
|
2018-10-09 22:04:48 +02:00
|
|
|
}
|
|
|
|
}
|
2018-10-11 16:17:41 +02:00
|
|
|
|
2018-10-09 22:04:48 +02:00
|
|
|
$revised_array[] = $this->projects[] = array( "name" => $_GET['project_name'], "path" => $this->path );
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
// Delete Project
|
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
public function Delete() {
|
|
|
|
|
2019-02-04 22:42:12 +01:00
|
|
|
global $sql;
|
|
|
|
$query = "DELETE FROM projects WHERE path=? AND ( owner=? OR owner='nobody' );";
|
2018-11-10 06:41:28 +01:00
|
|
|
$bind_variables = array( $this->path, $_SESSION["user"] );
|
2019-02-04 22:42:12 +01:00
|
|
|
$return = $sql->query( $query, $bind_variables, 0, "rowCount" );
|
2018-11-10 06:41:28 +01:00
|
|
|
|
2019-02-04 22:42:12 +01:00
|
|
|
if( $return > 0 ) {
|
2018-10-09 22:04:48 +02:00
|
|
|
|
2019-02-04 22:42:12 +01:00
|
|
|
echo( formatJSEND( "success", "Successfully deleted $project_name" ) );
|
2018-11-10 06:41:28 +01:00
|
|
|
} else {
|
|
|
|
|
2019-02-04 22:42:12 +01:00
|
|
|
echo formatJSEND( "error", "Error deleting project $project_name" );
|
2018-10-09 22:04:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
// Check Duplicate
|
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
public function CheckDuplicate() {
|
|
|
|
|
|
|
|
$pass = true;
|
|
|
|
foreach ( $this->projects as $project => $data ) {
|
|
|
|
|
|
|
|
if ( $data['name'] == $this->name || $data['path'] == $this->path ) {
|
|
|
|
|
|
|
|
$pass = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $pass;
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
// Sanitize Path
|
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
public function SanitizePath() {
|
|
|
|
|
|
|
|
$sanitized = str_replace( " ", "_", $this->path );
|
2018-10-11 16:17:41 +02:00
|
|
|
return preg_replace( '/[^\w-]/', '', strtolower( $sanitized ) );
|
2018-10-09 22:04:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
// Clean Path
|
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
public function cleanPath() {
|
|
|
|
|
|
|
|
// prevent Poison Null Byte injections
|
|
|
|
$path = str_replace( chr( 0 ), '', $this->path );
|
|
|
|
|
|
|
|
// prevent go out of the workspace
|
|
|
|
while( strpos( $path, '../' ) !== false ) {
|
|
|
|
|
|
|
|
$path = str_replace( '../', '', $path );
|
|
|
|
}
|
|
|
|
|
|
|
|
return $path;
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
// Execute Command
|
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
public function ExecuteCMD() {
|
|
|
|
|
|
|
|
if ( function_exists( 'system' ) ) {
|
|
|
|
|
|
|
|
ob_start();
|
|
|
|
system( $this->command_exec );
|
|
|
|
ob_end_clean();
|
|
|
|
} elseif( function_exists( 'passthru' ) ) {
|
|
|
|
|
|
|
|
//passthru
|
|
|
|
ob_start();
|
|
|
|
passthru($this->command_exec);
|
|
|
|
ob_end_clean();
|
|
|
|
} elseif ( function_exists( 'exec' ) ) {
|
|
|
|
|
|
|
|
//exec
|
|
|
|
exec( $this->command_exec, $this->output );
|
|
|
|
} elseif ( function_exists( 'shell_exec' ) ) {
|
|
|
|
|
|
|
|
//shell_exec
|
|
|
|
shell_exec( $this->command_exec );
|
|
|
|
}
|
|
|
|
}
|
2018-10-11 16:17:41 +02:00
|
|
|
}
|