Projeto

Geral

Perfil

Estatísticas
| Branch: | Revisão:

expressolivretools / app / Console / Commands / Expresso / SieveGetListFile.php @ master

Histórico | Ver | Anotar | Baixar (2,45 KB)

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
}