Added filers
This commit is contained in:
commit
33899c38a4
5 changed files with 385 additions and 0 deletions
5
.gitignore
vendored
Normal file
5
.gitignore
vendored
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
*~
|
||||
composer.lock
|
||||
vendor/*
|
||||
!vendor/phangoapp
|
||||
settings/
|
||||
12
composer.json
Normal file
12
composer.json
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
|
||||
{
|
||||
"repositories": [
|
||||
{
|
||||
"type": "path",
|
||||
"url": "./vendor/phangoapp/cuchuluphputils"
|
||||
}
|
||||
],
|
||||
"require": {
|
||||
"phangoapp/cuchuluphputils": "dev-master"
|
||||
}
|
||||
}
|
||||
20
vendor/phangoapp/cuchuluphputils/composer.json
vendored
Normal file
20
vendor/phangoapp/cuchuluphputils/composer.json
vendored
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
{
|
||||
"name": "phangoapp/cuchuluphputils",
|
||||
"description": "A set of classes and methods for make shell operations in linux",
|
||||
"require-dev": {
|
||||
"phpunit/phpunit": "^12"
|
||||
},
|
||||
"license": "GPL3",
|
||||
"authors": [
|
||||
{
|
||||
"name": "Antonio de la Rosa",
|
||||
"email": "antonio.delarosa@cuchulu.com"
|
||||
}
|
||||
],
|
||||
"minimum-stability": "stable",
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"PhangoApp\\CuchuluPHPUtils\\": "src"
|
||||
}
|
||||
}
|
||||
}
|
||||
327
vendor/phangoapp/cuchuluphputils/src/Linux.php
vendored
Normal file
327
vendor/phangoapp/cuchuluphputils/src/Linux.php
vendored
Normal file
|
|
@ -0,0 +1,327 @@
|
|||
<?php
|
||||
|
||||
namespace PhangoApp\CuchuluPHPUtils;
|
||||
|
||||
|
||||
class Linux {
|
||||
|
||||
static public function shell_command($arr_command, $exit_if_error=1) {
|
||||
|
||||
$error=0;
|
||||
|
||||
for($x=1;$x<count($arr_command);$x++) {
|
||||
|
||||
$arr_command[$x]=escapeshellcmd($arr_command[$x]);
|
||||
|
||||
}
|
||||
|
||||
$final_command=implode(' ', $arr_command);
|
||||
|
||||
$descriptorspec = array(
|
||||
0 => array("pipe", "r"), // stdin
|
||||
1 => array("pipe", "w"), // stdout
|
||||
2 => array("pipe", "w") // stderr
|
||||
);
|
||||
|
||||
$pipes=[];
|
||||
|
||||
$process=proc_open($final_command, $descriptorspec, $pipes);
|
||||
|
||||
if(is_resource($process)) {
|
||||
|
||||
while($s=fgets($pipes[1])) {
|
||||
|
||||
echo $s;
|
||||
|
||||
flush();
|
||||
}
|
||||
|
||||
while($e=fgets($pipes[2])) {
|
||||
|
||||
echo $e;
|
||||
|
||||
flush();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
fclose($pipes[0]);
|
||||
fclose($pipes[1]);
|
||||
fclose($pipes[2]);
|
||||
|
||||
$result=proc_close($process);
|
||||
|
||||
if($result>0) {
|
||||
|
||||
echo 'Error executing command '.$final_command."\n";
|
||||
|
||||
if($exit_if_error) {
|
||||
|
||||
exit($result);
|
||||
|
||||
}
|
||||
else {
|
||||
|
||||
return $result;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
static public function get_linux_distro() {
|
||||
|
||||
$file_distro=__DIR__.'/../../../../settings/distro.php';
|
||||
|
||||
$dir_distro=__DIR__.'/../../../../settings/';
|
||||
|
||||
if(!is_file($file_distro)) {
|
||||
|
||||
ob_start();
|
||||
|
||||
Linux::shell_command(['python3 -c "import distro;print(distro.id())"']);
|
||||
|
||||
$distro=trim(ob_get_contents());
|
||||
|
||||
ob_end_clean();
|
||||
|
||||
$distro_data="<?php\n\ndefine('DISTRO_LINUX', '".$distro."');";
|
||||
|
||||
if(!is_dir($dir_distro)) {
|
||||
|
||||
if(!mkdir($dir_distro)) {
|
||||
|
||||
throw new \Exception('Sorry, cannot create settings directory');
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if(!file_put_contents($file_distro, $distro_data)) {
|
||||
|
||||
throw new \Exception('Sorry, cannot create settings config file');
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
else {
|
||||
|
||||
if(!defined('DISTRO_LINUX')) {
|
||||
|
||||
include_once($file_distro);
|
||||
|
||||
}
|
||||
|
||||
$distro=DISTRO_LINUX;
|
||||
|
||||
}
|
||||
|
||||
return $distro;
|
||||
|
||||
}
|
||||
|
||||
static public function add_new_user($username, $password='', $user_directory='', $shell='/usr/sbin/nologin') {
|
||||
|
||||
$str_user="sudo useradd -m -s ${shell}";
|
||||
|
||||
if($password!='') {
|
||||
|
||||
$salt_str='\$6\$'.\str_replace('$', '\$', Linux::generate_random_password(10)).'\$';
|
||||
|
||||
$hash=crypt($password, $salt_str);
|
||||
|
||||
$str_user.=" -p \"${hash}\"";
|
||||
|
||||
echo $str_user;
|
||||
|
||||
}
|
||||
|
||||
if($user_directory!='') {
|
||||
|
||||
|
||||
$str_user.=" -d \"${user_directory}\"";
|
||||
|
||||
|
||||
}
|
||||
|
||||
$str_user.=" ${username}";
|
||||
|
||||
return Linux::shell_command([$str_user]);
|
||||
|
||||
}
|
||||
|
||||
static public function user_delete($username) {
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
static public function install_package($package) {
|
||||
|
||||
$distro=Linux::get_linux_distro();
|
||||
|
||||
if(!isset($package[$distro])) {
|
||||
|
||||
echo "Sorry, you don't have a package install for this distro\n";
|
||||
|
||||
exit(1);
|
||||
|
||||
}
|
||||
|
||||
if($distro=='debian' || $distro=='ubuntu') {
|
||||
|
||||
return Linux::shell_command(['sudo DEBIAN_FRONTEND="noninteractive" apt-get install -y '.$package[$distro]]);
|
||||
|
||||
}
|
||||
else if($distro=='fedora' || $distro=='almalinux' || $distro=='rocky') {
|
||||
|
||||
//if call("sudo dnf install -y {}".format(package[linux_distro]), shell=True) > 0:
|
||||
return Linux::shell_command(['sudo dnf install -y '.$package[$distro]]);
|
||||
|
||||
}
|
||||
else if($distro=='arch') {
|
||||
|
||||
//sudo pacman -S --noconfirm
|
||||
return Linux::shell_command(['sudo pacman -S --noconfirm '.$package[$distro]]);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
static public function patch_file($original_file, $patch_file) {
|
||||
|
||||
$distro=Linux::get_linux_distro();
|
||||
|
||||
//if call("sudo patch {} < {}".format(original_file[linux_distro], patch_file[linux_distro]), shell=True) > 0:
|
||||
|
||||
if(!isset($original_file[$distro])) {
|
||||
|
||||
echo "Sorry, you don't have a patch for this distro\n\n";
|
||||
|
||||
exit(1);
|
||||
|
||||
}
|
||||
|
||||
Linux::shell_command(["sudo patch ${original_file[$distro]} < ${patch_file[$distro]}"]);
|
||||
|
||||
}
|
||||
|
||||
static public function systemd_service($action, $service) {
|
||||
|
||||
$distro=Linux::get_linux_distro();
|
||||
|
||||
if(!isset($service[$distro])) {
|
||||
|
||||
echo "Sorry, you don't have a service reload for this distro\n\n";
|
||||
|
||||
exit(1);
|
||||
|
||||
}
|
||||
|
||||
return Linux::shell_command(["sudo systemctl ${action} ".$service[$distro]]);
|
||||
|
||||
}
|
||||
|
||||
static public function exec($executable) {
|
||||
|
||||
$distro=Linux::get_linux_distro();
|
||||
|
||||
if(!isset($executable[$distro])) {
|
||||
|
||||
echo "Sorry, you don't have a exec for this distro\n\n";
|
||||
|
||||
exit(1);
|
||||
|
||||
}
|
||||
|
||||
return Linux::shell_command($executable[$distro]);
|
||||
|
||||
}
|
||||
|
||||
static public function sed($arr_sed) {
|
||||
|
||||
$distro=Linux::get_linux_distro();
|
||||
|
||||
if(!isset($arr_sed[$distro])) {
|
||||
|
||||
echo "Sorry, you don't have a sed str for this distro\n\n";
|
||||
|
||||
exit(1);
|
||||
|
||||
}
|
||||
|
||||
return Linux::shell_command(["sudo sed -i", "\"s/".$arr_sed[$distro][0]."/".$arr_sed[$distro][1]."/g\" ".$arr_sed[$distro][2]]);
|
||||
|
||||
}
|
||||
|
||||
|
||||
static public function json_log($message, $error=0, $status=0, $progress=0, $no_progress=0) {
|
||||
|
||||
$log=["error" => $error, "status" => $status, "progress" => $progress, "no_progress" => $no_progress, "message" => $message];
|
||||
|
||||
echo json_encode($log)."\n";
|
||||
|
||||
}
|
||||
|
||||
static public function check_domain_name($domain_name) {
|
||||
|
||||
return preg_match("/^(a-z\d*)(\.(a-z\d*))*$/i", $domain_name);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Function used for generate a simple random password. Used RamdomLib from Ircmaxell
|
||||
*
|
||||
* @param string $length_pass A variable used for set the character's length the password. More length, password more secure
|
||||
*
|
||||
*/
|
||||
|
||||
static public function generate_random_password($length_pass=32)
|
||||
{
|
||||
|
||||
$x=0;
|
||||
$z=0;
|
||||
|
||||
$abc=array( 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '*', '+', '!', '-', '_', '@', '#', '$');
|
||||
|
||||
$disorder_abc=[];
|
||||
|
||||
//Simple disorder using random_int
|
||||
|
||||
while(count($abc)>0) {
|
||||
|
||||
$c_abc=count($abc);
|
||||
|
||||
$num_element_move=random_int(0, $c_abc-1);
|
||||
|
||||
$disorder_abc[]=$abc[$num_element_move];
|
||||
|
||||
unset($abc[$num_element_move]);
|
||||
|
||||
$abc=array_values($abc);
|
||||
|
||||
}
|
||||
|
||||
//Get randomly elements from the randomly generated array.
|
||||
|
||||
$c=count($disorder_abc);
|
||||
|
||||
$password_final='';
|
||||
|
||||
for($x=0;$x<$length_pass;$x++) {
|
||||
|
||||
$num_element_pass=random_int(0, $c-1);
|
||||
|
||||
$password_final.=$disorder_abc[$num_element_pass];
|
||||
|
||||
}
|
||||
|
||||
//Add strange characters
|
||||
|
||||
return $password_final;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
21
vendor/phangoapp/cuchuluphputils/tests/LinuxTest.php
vendored
Normal file
21
vendor/phangoapp/cuchuluphputils/tests/LinuxTest.php
vendored
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
<?php
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
include("vendor/autoload.php");
|
||||
|
||||
final class LinuxTest extends TestCase {
|
||||
|
||||
public function testDistro() {
|
||||
|
||||
$distro=PhangoApp\CuchuluPHPUtils\Linux::get_linux_distro();
|
||||
|
||||
$this->assertEquals($distro, 'manjaro');
|
||||
|
||||
unlink('./settings/distro.php');
|
||||
|
||||
rmdir('./settings');
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue