Skip to content

Commit e704a67

Browse files
committed
Red Summer
1 parent 1630dd4 commit e704a67

File tree

6 files changed

+851
-0
lines changed

6 files changed

+851
-0
lines changed

Cli/Key.php

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
<?php
2+
/**
3+
* Lib\Cli\Key
4+
* PHP version 7
5+
*
6+
* @category Tools
7+
* @package Cli
8+
* @author Bill Rocha <prbr@ymail.com>
9+
* @copyright 2016 Bill Rocha <http://google.com/+BillRocha>
10+
* @license <https://opensource.org/licenses/MIT> MIT
11+
* @version GIT: 0.0.2
12+
* @link http://paulorocha.tk/devbr
13+
*/
14+
15+
namespace Lib\Cli;
16+
17+
use Lib;
18+
19+
/**
20+
* Lib\Cli\Key Class
21+
*
22+
* @category Tools
23+
* @package Cli
24+
* @author Bill Rocha <prbr@ymail.com>
25+
* @license <https://opensource.org/licenses/MIT> MIT
26+
* @link http://paulorocha.tk/devbr
27+
*/
28+
class Key
29+
{
30+
private $cmd = null;
31+
private $arg = null;
32+
private $configKeyPath = null;
33+
34+
/**
35+
* Constructor
36+
* @param string $cmd command
37+
* @param array $arg others command line args
38+
*/
39+
function __construct(
40+
$cmd = null,
41+
$arg = null
42+
) {
43+
44+
$this->cmd = strtolower($cmd);
45+
$this->arg = $arg;
46+
$this->configKeyPath = (defined('_CONFIG') ? _CONFIG : __DIR__.'/').'Key/';
47+
}
48+
49+
/**
50+
* Run command
51+
*
52+
* @return void return from MAIN CLTool
53+
*/
54+
function run()
55+
{
56+
switch ($this->cmd) {
57+
case 'generate':
58+
return $this->cmdGenerate();
59+
break;
60+
61+
case 'list':
62+
return $this->cmdList();
63+
break;
64+
65+
default:
66+
echo "\n\n Command \"key:".$this->cmd."\" not exists!";
67+
exit(Main::help());
68+
break;
69+
}
70+
return;
71+
}
72+
73+
/**
74+
* Command Generate
75+
*
76+
* @return string display results
77+
*/
78+
private function cmdGenerate()
79+
{
80+
//check if path exists
81+
if (!is_dir($this->configKeyPath)) {
82+
Main::checkAndOrCreateDir($this->configKeyPath, true);
83+
}
84+
//Now, OPEN_SSL
85+
$this->createKeys();
86+
return "\n Can, OpenSSL keys & certificates - created success!".
87+
"\n Location: ".$this->configKeyPath."\n\n";
88+
}
89+
90+
/**
91+
* Command LIST
92+
*
93+
* @return string display results
94+
*/
95+
private function cmdList()
96+
{
97+
$o = "\n\n Ciphers:";
98+
foreach (mcrypt_list_algorithms() as $x) {
99+
$o .= "\n\t".$x;
100+
}
101+
$o .= "\n\n Cipher Modes:";
102+
foreach (mcrypt_list_modes() as $x) {
103+
$o .= "\n\t".$x;
104+
}
105+
return $o;
106+
}
107+
108+
/**
109+
* Create Can anda SSL keys
110+
*
111+
* @return void none
112+
*/
113+
private function createKeys()
114+
{
115+
//Create Can Keys
116+
shuffle(Lib\Can::$base);
117+
shuffle(Lib\Can::$extra_base);
118+
file_put_contents($this->configKeyPath.'can.key', implode(Lib\Can::$base)."\n".implode(Lib\Can::$extra_base));
119+
120+
$SSLcnf = [];
121+
$dn = [];
122+
123+
//get configurations
124+
include $this->configKeyPath.'openssl.config.php';
125+
126+
// Generate a new private (and public) key pair
127+
$privkey = openssl_pkey_new($SSLcnf);
128+
129+
// Generate a certificate signing request
130+
$csr = openssl_csr_new($dn, $privkey, $SSLcnf);
131+
132+
// You will usually want to create a self-signed certificate at this
133+
// point until your CA fulfills your request.
134+
// This creates a self-signed cert that is valid for 365 days
135+
$sscert = openssl_csr_sign($csr, null, $privkey, 365, $SSLcnf);
136+
137+
//CERTIFICADO
138+
openssl_csr_export_to_file($csr, $this->configKeyPath.'certificate.crt', false);
139+
140+
//CERTIFICADO AUTO-ASSINADO
141+
openssl_x509_export_to_file($sscert, $this->configKeyPath.'self_signed_certificate.cer', false);
142+
143+
//CHAVE PRIVADA (private.pem)
144+
openssl_pkey_export_to_file($privkey, $this->configKeyPath.'private.key', null, $SSLcnf);
145+
146+
//CHAVE PÚBLICA (public.key)
147+
file_put_contents($this->configKeyPath.'public.key', openssl_pkey_get_details($privkey)['key']);
148+
}
149+
}

Cli/Main.php

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
<?php
2+
/**
3+
* Lib\Cli\Main
4+
* PHP version 7
5+
*
6+
* @category Tools
7+
* @package Cli
8+
* @author Bill Rocha <prbr@ymail.com>
9+
* @copyright 2016 Bill Rocha <http://google.com/+BillRocha>
10+
* @license <https://opensource.org/licenses/MIT> MIT
11+
* @version GIT: 0.0.2
12+
* @link http://paulorocha.tk/devbr
13+
*/
14+
15+
namespace Lib\Cli;
16+
17+
use Lib;
18+
19+
/**
20+
* Lib\Cli\Main Class
21+
*
22+
* @category Tools
23+
* @package Cli
24+
* @author Bill Rocha <prbr@ymail.com>
25+
* @license <https://opensource.org/licenses/MIT> MIT
26+
* @link http://paulorocha.tk/devbr
27+
*/
28+
class Main
29+
{
30+
31+
private $timer = 0;
32+
33+
/**
34+
* Constructor
35+
*
36+
* @param array $argv Command line array
37+
*/
38+
function __construct($argv)
39+
{
40+
echo " Command Line Tool!\n";
41+
if (php_sapi_name() !== 'cli') {
42+
exit('It\'s no cli!');
43+
}
44+
45+
//Constants:
46+
$this->timer = microtime(true);
47+
48+
//Command line settings...
49+
echo $this->request($argv);
50+
51+
exit("\n Finished in ".number_format((microtime(true)-$this->timer)*1000, 3)." ms.\n");
52+
}
53+
54+
//CORE Request
55+
function request($rqst)
56+
{
57+
array_shift($rqst);
58+
$ax = $rqst;
59+
foreach ($rqst as $a) {
60+
array_shift($ax);
61+
if (strpos($a, '-h') !== false || strpos($a, '?') !== false) {
62+
return self::help();
63+
}
64+
if (strpos($a, 'optimize:') !== false) {
65+
return (new Optimizer(substr($a, 9), $ax))->run();
66+
}
67+
if (strpos($a, 'install') !== false) {
68+
return $this->cmdInstall(substr($a, 7), $ax);
69+
}
70+
if (strpos($a, 'update') !== false) {
71+
return $this->cmdUpdate(substr($a, 6), $ax);
72+
}
73+
if (strpos($a, 'key:') !== false) {
74+
return (new Key(substr($a, 4), $ax))->run();
75+
}
76+
if (strpos($a, 'make:') !== false) {
77+
return (new Make(substr($a, 5), $ax))->run();
78+
//return $this->cmdMake(substr($a, 5), $ax);
79+
}
80+
81+
//Plugins
82+
if (strpos($a, 'table:') !== false) {
83+
return (new Plugin\Table(substr($a, 6), $ax))->run();
84+
}
85+
}
86+
//or show help...
87+
return self::help();
88+
}
89+
90+
/**
91+
* It's same as cmdUpdate
92+
* @param string $v segment of command
93+
* @param array $arg all others command line argumments
94+
*
95+
* @return string Display user data
96+
*/
97+
function cmdInstall($v, $arg)
98+
{
99+
return $this->cmdUpdate($v, $arg);
100+
}
101+
102+
/**
103+
* Update command
104+
* @param string $v segment of command
105+
* @param array $arg all others command line argumments
106+
*
107+
* @return string Display user data
108+
*/
109+
function cmdUpdate($v, $arg)
110+
{
111+
$devbr = _APP.'Composer/devbr/';
112+
$dir = scandir($devbr);
113+
$o = '';
114+
115+
foreach ($dir as $k) {
116+
if ($k == '.' || $k == '..') {
117+
continue;
118+
}
119+
if (is_file($devbr.$k.'/install.php')) {
120+
$o .= include $devbr.$k.'/install.php';
121+
}
122+
}
123+
return $o;
124+
}
125+
126+
// Checa um diretório e cria se não existe - retorna false se não conseguir ou não existir
127+
/**
128+
* Check or create a directory
129+
* @param string $dir path of the directory
130+
* @param boolean $create False/true for create
131+
* @param string $perm indiucates a permission - default 0777
132+
*
133+
* @return bool status of directory (exists/created = false or true)
134+
*/
135+
static function checkAndOrCreateDir($dir, $create = false, $perm = '0777')
136+
{
137+
if (is_dir($dir) && is_writable($dir)) {
138+
return true;
139+
} elseif ($create === false) {
140+
return false;
141+
}
142+
143+
@mkdir($dir, $perm, true);
144+
@chmod($dir, $perm);
145+
146+
if (is_writable($dir)) {
147+
return true;
148+
}
149+
return false;
150+
}
151+
152+
/**
153+
* return a help information text
154+
*
155+
* @return string a help text...
156+
*/
157+
static function help()
158+
{
159+
return '
160+
161+
Usage: php index.php [command:type] [options]
162+
163+
key:generate Generate new keys
164+
key:list List all installed Cyphers
165+
166+
make:controller <name> Create a controller with <name>
167+
make:model <name> Create a model with <name>
168+
make:html <name> Create a html file with <name>
169+
170+
optimize:scan [save name] Scan CSS&JS source files
171+
Optional indicates "save" and a
172+
"name" for save in config file the scan
173+
174+
optimize:css Optimize CSS configurated files
175+
optimize:js Optimize JS configurated files
176+
optimize:all Optimize ALL configurated files
177+
178+
-h or ? Show this help
179+
';
180+
}
181+
}

0 commit comments

Comments
 (0)