Projeto

Geral

Perfil

Revisão ab2f5151

Ver diferenças:

app/Console/Commands/Expresso/CyrusCredentialFile.php
1
<?php
2

  
3
namespace App\Console\Commands\Expresso;
4

  
5
use Illuminate\Console\Command;
6
use Illuminate\Support\Facades\DB;
7
use Illuminate\Support\Facades\File;
8

  
9
class CyrusCredentialFile extends Command
10
{
11
    /**
12
     * The name and signature of the console command.
13
     *
14
     * @var string
15
     */
16
    protected $signature = 'expresso:get-cyrus-credentials';
17

  
18
    /**
19
     * The console command description.
20
     *
21
     * @var string
22
     */
23
    protected $description = 'lista credenciais do cyrus';
24

  
25
    /**
26
     * Create a new command instance.
27
     *
28
     * @return void
29
     */
30
    public function __construct()
31
    {
32
        parent::__construct();
33
    }
34

  
35
    /**
36
     * Execute the console command.
37
     *
38
     * @return mixed
39
     */
40
    public function handle()
41
    {
42
        $configEAdmin = DB::connection('pgsql-expresso')->table('phpgw_emailadmin')
43
                ->where('description', env('KEY_EMAILADMIN_SOGO') )
44
                ->first();
45

  
46
        File::put( 'credential', $configEAdmin->imapadminusername . ":" . $configEAdmin->imapadminpw );
47
        
48
        $this->info( 'Arquivo de credenciais cyrus criado com sucesso' );
49
    }
50

  
51
}
52

  
app/Console/Commands/Expresso/GetUsersOULdap.php
4 4

  
5 5
use App\Commons\Ldap;
6 6
use Illuminate\Console\Command;
7
use Illuminate\Support\Facades\DB;
8
use Storage;
7
use Illuminate\Support\Facades\File;
9 8

  
10 9
class GetUsersOULdap extends Command
11 10
{
......
14 13
     *
15 14
     * @var string
16 15
     */
17
    protected $signature = 'expresso:get-users-organizations {--ou=} {cmd?}';
16
    protected $signature = 'expresso:get-organization-users {--ou=} {cmd?}';
18 17

  
19 18
    /**
20 19
     * The console command description.
21 20
     *
22 21
     * @var string
23 22
     */
24
    protected $description = 'Lista os usuarios de uma organização ( OU )';
23
    protected $description = 'lista os usuários de uma organizacao ldap ( OU )';
25 24

  
26 25
    /**
27 26
     * Create a new command instance.
......
44 43

  
45 44
        $organization = $this->option('ou') ?? null;
46 45

  
47
        if( isset($arguments['cmd']) && $arguments['cmd'] == 'help' )
48
        {
49
            $this->help();
50

  
51
        } else {
52

  
53
            if( !is_null($organization) ){
54

  
55
                $users      = $this->getUsersOu($organization);
56
                $fileName   = $arguments['cmd'] ?? null;
57

  
58
                if( is_null($fileName) ) {
59
                    foreach( $users as $user )
60
                    {
61
                        $line  = "" ;
62
                        $line .= "cn        : " . ( $user['cn'][0] ?? "" ) . PHP_EOL;
63
                        $line .= "uid       : " . ( $user['uid'][0] ?? "" ) . PHP_EOL ;
64
                        $line .= "uidnumber : " . ( $user['uidnumber'][0] ?? "" ) . PHP_EOL ;
65
                        $line .= "----------------------------------------------------------------------------" . PHP_EOL;
66
                        
67
                        print_r( $line );
68
                    }
69
                }else {
70
                    $this->writeFile( $users, $fileName );
46
        if( !is_null($organization) ){
47

  
48
            $users      = $this->getUsersOu($organization);
49
            $fileName   = $arguments['cmd'] ?? null;
50

  
51
            if( is_null($fileName) ) {
52
                foreach( $users as $user )
53
                {
54
                    $line  = "" ;
55
                    $line .= "cn        : " . ( $user['cn'][0] ?? "" ) . PHP_EOL;
56
                    $line .= "uid       : " . ( $user['uid'][0] ?? "" ) . PHP_EOL ;
57
                    $line .= "uidnumber : " . ( $user['uidnumber'][0] ?? "" ) . PHP_EOL ;
58
                    $line .= "----------------------------------------------------------------------------" . PHP_EOL;
59
                    
60
                    print_r( $line );
71 61
                }
72

  
62
            }else {
63
                $this->writeFile( $users, $fileName );
73 64
            }
74
        }
75 65

  
76
        $this->newLine();
66
        }
67
        
77 68
    }
78 69

  
79 70
    private function getUsersOu( $organization )
......
83 74
        return $result->getUsersOu( $organization );
84 75
    }
85 76

  
86
    private function help()
87
    {
88
        $this->newLine();
89
        $this->info('EXEMPLO DE UTILIZACAO : ');
90
        $this->info('Ex: php artisan get-users-organizations --ou=<ORGANIZATION> { print | file }');
91
        $this->newLine();
92
    }
93

  
94 77
    private function writeFile( array $users, string $fileName )
95 78
    {
96
        $this->newLine();
97
        
98 79
        if( is_array($users) && count($users) > 0 ){
99 80
         
100 81
            $line  = "";
......
104 85
                $line .= ( $user['uid'][0] ?? "" ) . ";" ;
105 86
                $line .= ( $user['uidnumber'][0] ?? "" ) . PHP_EOL ;
106 87

  
107
                Storage::put('/EXPRESSO/'.$fileName, $line );
88
                File::put( $fileName , $line );
108 89
            }
109 90
        }
110 91

  
111
        $this->info('Arquivo gerado com sucesso : /EXPRESSO/'. $fileName );
92
        $this->info('Arquivo gerado com sucesso : ' . $fileName );
112 93
    }
113 94
}
app/Console/Commands/Expresso/SieveGetListFile.php
1
<?php
2

  
3
namespace App\Console\Commands\Expresso;
4

  
5
use Illuminate\Console\Command;
6
use Illuminate\Support\Facades\DB;
7
use Illuminate\Support\Facades\File;
8

  
9
class SieveGetListFile extends Command
10
{
11
    /**
12
     * The name and signature of the console command.
13
     *
14
     * @var string
15
     */
16
    protected $signature = 'expresso:get-user-sieve-filters-from-file {--f|file=} {cmd?}';
17

  
18
    /**
19
     * The console command description.
20
     *
21
     * @var string
22
     */
23
    protected $description = 'lista os filtros de usuarios ( entrada: usuários em arquivo )';
24

  
25
    /**
26
     * Create a new command instance.
27
     *
28
     * @return void
29
     */
30
    public function __construct()
31
    {
32
        parent::__construct();
33
    }
34

  
35
    /**
36
     * Execute the console command.
37
     *
38
     * @return mixed
39
     */
40
    public function handle()
41
    {
42
        $arguments = $this->arguments();
43

  
44
        $fileName = $this->option('file') ?? null;
45

  
46
        if( !is_null($fileName) ){
47

  
48
            if( file_exists($fileName) ){
49
                File::delete( "sieve_" . $fileName );
50
            }
51

  
52
            $handle = fopen( $fileName, "r");
53
            if ($handle) {
54
                while ( ($line = fgets($handle)) !== false) {
55

  
56
                    // cn; uid; uidnumber
57
                    $cn         = "";
58
                    $uid        = "";
59
                    $uidnumber  = "";
60

  
61
                    list( $cn ,$uid, $uidnumber) = explode( ";", $line );
62

  
63
                    $sieveRules = $this->getSieveRules( $uidnumber );
64

  
65
                    $this->writeFile( $uid, $sieveRules, $fileName );
66
                }
67
                
68
                fclose($handle);
69
                
70
                $this->info( "Arquivo gerado com sucesso : " . "sieve_". $fileName );
71

  
72
            } else {
73
                $this->error("Arquivo não encontrado");
74
            }                 
75
        }
76
        
77
    }
78

  
79
    private function getSieveRules( $user )
80
    {
81
        $sieveRules = DB::connection('pgsql-expresso')
82
                            ->table('phpgw_sieve_rules')
83
                            ->where('fk_sieve_owner', $user )
84
                            ->get();
85

  
86
        return $sieveRules;
87
    }
88

  
89
    private function writeFile( $uid, $sieveRules, $fileName )
90
    {
91
        foreach( $sieveRules as $rule ){ 
92
            $line = "";
93
            $line .= $uid . "|" . $rule->rule . "|" . ( $rule->is_enabled  == "1" ? "1" : "0" ) . PHP_EOL; 
94
            File::append( "sieve_". $fileName, $line );
95
        }
96
    }
97
}
app/Console/Commands/Expresso/SieveGetUid.php
13 13
     *
14 14
     * @var string
15 15
     */
16
    protected $signature = 'expresso:sieve-get {--u|uid=} {cmd?}';
16
    protected $signature = 'expresso:get-user-sieve-filters-by-line {--u|uid=} {cmd?}';
17 17

  
18 18
    /**
19 19
     * The console command description.
20 20
     *
21 21
     * @var string
22 22
     */
23
    protected $description = 'Lista os scripts sieve do usuario';
23
    protected $description = 'lista os filtros de usuários ( entrada: usuários separados por vírgula )';
24 24

  
25 25
    /**
26 26
     * Create a new command instance.
......
43 43

  
44 44
        $uid = $this->option('uid') ?? null;
45 45

  
46
        if( isset($arguments['cmd']) && $arguments['cmd'] == 'help' )
47
        {
48
            $this->help();
49

  
50
        } else {
51

  
52
            if( !is_null($uid) ){
53

  
54
                $sieveRules = $this->getSieveRules($uid);
55
                
56
                if( isset($arguments['cmd']) && $arguments['cmd'] == 'print' )
57
                {
58
                    $rules      = [];
59
                    foreach( $sieveRules as $key => $sieve ){
60
                        $rules[$key]['regra']       = unserialize( $sieve->rule );
61
                        $rules[$key]['habilitada']  = $sieve->is_enabled;
62
                        $rules[$key]['dt_criacao']  = date('d/m/Y', strtotime($sieve->created_dt));
63
                        
64
                        print_r( $rules[$key] );
65
                    }
66
                }
67

  
68
                if( isset($arguments['cmd']) && $arguments['cmd'] == 'file' )
69
                {
70
                    $lines = "Usuario : " . $uid . PHP_EOL;
71 46

  
72
                    foreach( $sieveRules as $key => $sieve ){
73
                        $lines .= "regra      : " . $sieve->rule . PHP_EOL;
74
                        $lines .= "habilitada : " . $sieve->is_enabled . PHP_EOL;
75
                        $lines .= "dt_criacao : " . date('d/m/Y', strtotime($sieve->created_dt)) . PHP_EOL;
76
                        $lines .= "************************************************************************" . PHP_EOL . PHP_EOL;
77
                    }
47
        if( !is_null($uid) ){
78 48

  
79
                    Storage::put('file/file_'.$uid.'.txt', $lines );
49
            $sieveRules = $this->getSieveRules($uid);
50
            
51
            if( isset($arguments['cmd']) && $arguments['cmd'] == 'print' )
52
            {
53
                $rules      = [];
54
                foreach( $sieveRules as $key => $sieve ){
55
                    $rules[$key]['regra']       = unserialize( $sieve->rule );
56
                    $rules[$key]['habilitada']  = $sieve->is_enabled;
57
                    $rules[$key]['dt_criacao']  = date('d/m/Y', strtotime($sieve->created_dt));
58
                    
59
                    print_r( $rules[$key] );
80 60
                }
61
            }
81 62

  
82
                if( isset($arguments['cmd']) && $arguments['cmd'] == 'fileSogo' )
83
                {
84
                    print_r( storage_path('fileSogo') );
63
            if( isset($arguments['cmd']) && $arguments['cmd'] == 'file' )
64
            {
65
                $lines = "Usuario : " . $uid . PHP_EOL;
66

  
67
                foreach( $sieveRules as $key => $sieve ){
68
                    $lines .= "regra      : " . $sieve->rule . PHP_EOL;
69
                    $lines .= "habilitada : " . $sieve->is_enabled . PHP_EOL;
70
                    $lines .= "dt_criacao : " . date('d/m/Y', strtotime($sieve->created_dt)) . PHP_EOL;
71
                    $lines .= "************************************************************************" . PHP_EOL . PHP_EOL;
85 72
                }
73

  
74
                Storage::put('file/file_'.$uid.'.txt', $lines );
86 75
            }
87
        }
88 76

  
89
        $this->newLine();
77
            if( isset($arguments['cmd']) && $arguments['cmd'] == 'fileSogo' )
78
            {
79
                print_r( storage_path('fileSogo') );
80
            }
81
        }
90 82
    }
91 83

  
92 84
    private function getSieveRules( $user )
......
98 90

  
99 91
        return $sieveRules;
100 92
    }
101

  
102
    private function help()
103
    {
104
        $this->newLine();
105
        $this->info('EXEMPLO DE UTILIZACAO : ');
106
        $this->info('Ex: php artisan expresso:sieve-get --uid=<000000> { print | file | fileSogo }');
107
        $this->newLine();
108
    }
109 93
}
app/Console/Commands/Sogo/Preferences.php
13 13
     *
14 14
     * @var string
15 15
     */
16
    protected $signature = 'sogo:preferences-get {--u|user=} {cmd?}';
16
    protected $signature = 'sogo:get-user-preferences {--u|user=}';
17 17

  
18 18
    /**
19 19
     * The console command description.
20 20
     *
21 21
     * @var string
22 22
     */
23
    protected $description = 'Lista as preferencias sogo do usuario';
23
    protected $description = 'lista preferências sogo do usuário';
24 24

  
25 25
    /**
26 26
     * Create a new command instance.
......
43 43

  
44 44
        $user = $this->option('user') ?? null;
45 45

  
46
        if( isset($arguments['cmd']) && $arguments['cmd'] == 'help' )
47
        {
48
            $this->help();
49
            
50
        } else {
46
        if( !is_null($user) ){
51 47

  
52
            if( !is_null($user) ){
48
            $preferences =  $this->getPreferences($user);
53 49

  
54
                $preferences =  $this->getPreferences($user);
55

  
56
                print_r( json_decode($preferences->c_defaults) );
57
            }
50
            print_r( json_decode($preferences->c_defaults) );
58 51
        }
59 52

  
60 53
        $this->newLine();
......
69 62

  
70 63
        return $preferences;
71 64
    }
72

  
73
    private function help()
74
    {
75
        $this->newLine();
76
        $this->info('EXEMPLO DE UTILIZACAO : ');
77
        $this->info('Ex: php artisan sogo:preferences-get --user=<usuario> { print | file }');
78
        $this->newLine();
79
    }
80 65
}
app/Console/Commands/Sogo/SieveSetListFile.php
1
<?php
2

  
3
namespace App\Console\Commands\Sogo;
4

  
5
use Illuminate\Console\Command;
6
use Illuminate\Support\Facades\DB;
7
use Illuminate\Support\Facades\File;
8
use Illuminate\Support\Facades\Storage;
9

  
10
class SieveSetListFile extends Command
11
{
12
    /**
13
     * The name and signature of the console command.
14
     *
15
     * @var string
16
     */
17
    protected $signature = 'sogo:set-user-sieve-script {--f|file=} {cmd?}';
18

  
19
    /**
20
     * The console command description.
21
     *
22
     * @var string
23
     */
24
    protected $description = 'cria script(s) sieve para usuario(s) sogo';
25

  
26
    /**
27
     * Create a new command instance.
28
     *
29
     * @return void
30
     */
31
    public function __construct()
32
    {
33
        parent::__construct();
34
    }
35

  
36
    /**
37
     * Execute the console command.
38
     *
39
     * @return mixed
40
     */
41
    public function handle()
42
    {
43
        $arguments = $this->arguments();
44

  
45
        $fileName = $this->option('file') ?? null;
46

  
47
        if( !file_exists('credential') ){
48
            $this->error(' Crie o arquivo de CREDENCIAIS  do cyrus.');
49
            die();
50
        }
51

  
52
        if( !is_null($fileName) ){
53

  
54
            $handle = fopen( $fileName, "r");
55
            
56
            if ($handle) {
57
                
58
                while ( ($line = fgets($handle)) !== false) {
59

  
60
                    // user | rule | is_active
61
                    $user     = "";
62
                    $rule     = "";
63
                    $isActive = "";
64

  
65
                    list( $user, $rule, $isActive ) = explode("|", $line );
66

  
67
                    // error offset
68
                    $ruleText = preg_replace_callback ( '!s:(\d+):"(.*?)";!', function($match) {      
69
                        return ($match[1] == strlen($match[2])) ? $match[0] : 's:' . strlen($match[2]) . ':"' . $match[2] . '";';
70
                    }, $rule );
71

  
72
                    $this->writeFile( $user, $ruleText, $isActive );
73
                }
74
                
75
                fclose($handle);
76
                
77
                $sieveFiles = Storage::files( 'sieve' );
78

  
79
                foreach( $sieveFiles as $file )
80
                {
81
                    $user = preg_replace('/sieve\/sieve_/', '', $file );
82

  
83
                    $content = Storage::get( $file ) ;
84
                    $content = preg_replace('/\n/',',', $content );
85
                    $content = '{"SOGOSieveFilters":['.$content.']}';
86

  
87
                    File::put('fileSogoSieveFilter.json', $content );
88
                    
89
                    $commandSogoTool = "sogo-tool user-preferences set defaults {$user} -p credential SOGoSieveFilters -f fileSogoSieveFilter.json";
90

  
91
                    exec( $commandSogoTool );
92
                }
93

  
94
                if( file_exists('fileSogoSieveFilter.json') ){
95
                    File::delete('fileSogoSieveFilter.json');
96
                }
97

  
98
                if( file_exists('credential') ){
99
                    File::delete('credential');
100
                }
101
        
102
            } else {
103
                $this->error("Arquivo não encontrado");
104
            }                 
105
        }
106
        
107
    }
108

  
109
    private function writeFile( $user, $content, $isActive )
110
    {
111
        $rule = unserialize($content);
112

  
113
        $isActiveRule = ( intval($isActive) === 1 ? "1" : "0" );
114

  
115
        $newRule = '{"actions": [%actions%], "name": %name%, "rules": [%rules%], "match": %match%, "active": '.$isActiveRule.'}';
116

  
117
        // actions, name
118
        if( isset($rule['action']) ){
119

  
120
            $actions        = "";
121
            $name           = "";
122

  
123
            if( preg_match('/fileinto/', $rule['action']) ){
124
                $folderName = preg_replace('/fileinto:/','',$rule['action']);
125
                $actions = '{"argument": "'.$this->_toISO88591($folderName).'", "method": "fileinto"}';
126
                $name   .= 'ARQUIVAR em ' . $this->_toISO88591($folderName);
127
            }
128

  
129
            if( preg_match('/rejectText/', $rule['action']) ){
130
                $message = preg_replace('/rejectText:/','',$rule['action']);
131
                $message = $this->_toISO88591($message);
132
                $actions = '{"argument": "'. $message .'", "method": "reject"}';
133
                $name   .= 'REJEITAR com a mensagem : ' . $message;
134
            }
135

  
136
            if( preg_match('/flagged/', $rule['action']) ){
137
                $actions = '{"argument": "$label1", "method": "addflag"}';
138
                $name   .= 'MARCAR COMO IMPORTANTE ';
139
            }
140

  
141
            if( preg_match('/redirect/', $rule['action']) ){
142
                $emails = explode(';', preg_replace('/redirect:/','', $this->_toISO88591($rule['action'])));
143
                foreach( $emails as $email ){
144
                    $actions .= '{"argument": "'.$email.'", "method": "redirect"},';
145
                }
146
                $actions = rtrim($actions, ",");
147
                $name   .= 'REDIRECIONAR para ' . implode(', ', $emails );
148
            }
149

  
150
            if( preg_match('/discard/', $rule['action']) ){
151
                $actions = '{"argument": "", "method": "discard"}';
152
                $name   .= 'APAGAR mensagem ';
153
            }
154

  
155
            $newRule = preg_replace('/%actions%/', $actions, $newRule );
156
            $newRule = preg_replace('/%name%/', '"'.$name.'"', $newRule );
157
        }
158

  
159
        // match
160
        if( isset($rule['condition']) ){
161
            if($rule['condition'] === "anyof"){
162
                $newRule = preg_replace('/%match%/', '"any"', $newRule );
163
            }
164
            if($rule['condition'] === "allof"){
165
                $newRule = preg_replace('/%match%/', '"all"', $newRule );
166
            }
167
        }
168

  
169
        // fields from, to, subject
170
        $fieldFrom     = "";
171
        $fieldTo       = "";
172
        $fieldSubject  = "";
173
        
174
        if( isset($rule['from']) ){ $fieldFrom = '{"field": "from", "operator": "contains", "value": "'.$this->_toISO88591($rule['from']).'"}'; }
175
        if( isset($rule['to']) ){ $fieldTo = '{"field": "to", "operator": "contains", "value": "'.$this->_toISO88591($rule['to']).'"}'; }
176
        if( isset($rule['subject']) ){ $fieldSubject = '{"field": "subject", "operator": "contains", "value": "'.$this->_toISO88591($rule['subject']).'"}'; }
177

  
178
        $fields  = $fieldFrom;
179
        $fields .= ",".$fieldTo;
180
        $fields .= ",".$fieldSubject;
181

  
182
        if( trim($fields) !== "" ){ 
183
            $fields = preg_replace("/^\,{1,2}/","",$fields);
184
            $fields = preg_replace("/\,{1,2}$/","",$fields);
185
            $newRule = preg_replace('/%rules%/', $fields, $newRule );
186
        }
187

  
188
        Storage::append( 'sieve/sieve_'.$user, $newRule );
189
    }
190

  
191
	private function _toMailBox( $folder )
192
	{
193
		return $this->_toUTF8( $folder, false, 'UTF7-IMAP' );
194
	}
195

  
196
	private function _toUTF8( $str, $charset = false, $to = 'UTF-8' )
197
	{
198
		return mb_convert_encoding( $str, 'UTF-8', ( $charset === false? mb_detect_encoding( $str, 'UTF-8, ISO-8859-1', true ) : $charset ) );
199
    }
200

  
201
    private function _toISO88591( $str, $charset = false )
202
	{
203
		return mb_convert_encoding( $str, 'ISO-8859-1', ( $charset === false? mb_detect_encoding( $str, 'UTF-8, ISO-8859-1', true ) : $charset ) );
204
    }
205

  
206
}
app/Console/Commands/Teste.php
2 2

  
3 3
namespace App\Console\Commands;
4 4

  
5
use App\Commons\Ldap;
6 5
use Illuminate\Console\Command;
7
use Illuminate\Support\Facades\DB;
8
use LdapRecord\Connection;
9
use Storage;
6
use Illuminate\Support\Facades\Storage;
7
use Illuminate\Support\Facades\File;
10 8

  
11 9
class Teste extends Command
12 10
{
......
15 13
     *
16 14
     * @var string
17 15
     */
16
    //protected $signature = 'teste:comando {--o1|opt1=} {--o2|opt2=}';
18 17
    protected $signature = 'teste:comando';
19 18

  
20 19
    /**
......
41 40
     */
42 41
    public function handle()
43 42
    {
44
        $ldap = new Ldap();
43
        // $opt1 = $this->option('opt1') ?? null;
44
        // $opt2 = $this->option('opt2') ?? null;
45 45

  
46
        //print_r( $ldap->getUidNumber("alexandrecorreia") );
47

  
48
        //print_r( $ldap->getUid("500074") );
49

  
50
        print_r( $ldap->getUsersOu("celepar") );
46
        // if( !is_null($opt1) )
47
        //     print_r( 'opt1 : ' . $opt1 . PHP_EOL);
51 48

  
49
        // if( !is_null($opt2) )
50
        //     print_r( 'opt2 : ' . $opt2 . PHP_EOL );
52 51
    }
52

  
53 53
}
54 54

  
app/Console/Kernel.php
17 17
        // Commands Expresso
18 18
        \App\Console\Commands\Expresso\GetUsersOULdap::class,
19 19
        \App\Console\Commands\Expresso\SieveGetUid::class,
20
        \App\Console\Commands\Expresso\SieveGetListFile::class,
21
        \App\Console\Commands\Expresso\CyrusCredentialFile::class,
20 22
        
21 23
        // Commands SOGO
22 24
        \App\Console\Commands\Sogo\Preferences::class,
25
        \App\Console\Commands\Sogo\SieveSetListFile::class,
23 26
        
24 27
        // TESTE
25 28
        \App\Console\Commands\Teste::class

Exportar para Unified diff