Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
3 lars 1
<?php
2
 
3
    /**
4
     * @package   mediaran
5
     * @author    Lars Tiefland <tiefland@weban.de>
6
     * @copyright 2010 Webagentur Niewerth
7
     * @license   propietary http://www.weban.de
8
     * @version   $Rev: 357 $
9
     * @filesource
10
     *
11
     */
12
 
13
    /**
14
     *
15
     * @package   mediaran
16
     * @author    Lars Tiefland <tiefland@weban.de>
17
     * @copyright 2010 Webagentur Niewerth
18
     */
19
 
20
    // SVN: $Id: del_old_images.php 357 2010-11-18 11:35:40Z tiefland $
21
 
22
    require_once "Console/GetoptPlus.php";
23
    require_once "Console/ProgressBar.php";
24
    require_once "File/Find.php";
25
    define( "VERSION",
26
        '$Rev: 357 $ $Date: 2010-11-18 12:35:40 +0100 (Do, 18 Nov 2010) $' );
27
    // Breite für den ProgressBar
28
    $width = 120;
29
 
30
    // Konfiguration für Console_GetoptPlus
31
    $config = array( 'header' => array( 'Alle unbenutzten Bilder loeschen',
32
        "", ), 'usage' => array( '--site <arg> --type <arg>',
33
        '--site <arg> --shop <arg> --type <arg>',
34
        '--site <arg> --shop <arg> --index <arg> --type <arg>' ), 'options' =>
35
        array( array( 'long' => 'site', 'short' => 'S', 'type' =>
36
        'mandatory', 'desc' => array( 'arg', 'Definiert den Kunden', '' ) ),
37
        array( 'long' => 'shop', 'type' => 'mandatory', 'short' => 's',
38
        'desc' => array( 'arg', 'definiert den Shop',
39
        'Wenn diese Option nicht angegeben wird, so wird der Wert von --site verwendet',
40
        "", ) ), array( 'long' => 'index', 'short' => 'i', 'type' =>
41
        'mandatory', 'desc' => array( 'arg',
42
        'Definiert den Bild-Index mit dem gearbeitet werden soll',
43
        'Wenn diese Option nicht angegeben wird, so wird der Wert 1 verwendet',
44
        "", ) ), array( 'long' => 'type', 'type' => 'mandatory', 'short' =>
45
        't', 'desc' => array( 'arg',
46
        'Definiert die DB-Tabelle, mit der gearbeitet werden soll',
47
        '(artikel, directory, Herstellerkatalog oder news)', "", ), ), array
48
        ( 'long' => 'version', 'type' => 'noarg', 'short' => 'v', 'desc' =>
49
        array( 'gibt die Versionsnummer aus und beendet das Skript', "", ) ),
50
        array( "long" => "progress", "short" => "p", "type" => "noarg",
51
        "desc" => array( "gibt einen Fortschrittsbalken aus", "" ), ), ),
52
        'footer' => array( '', ), );
53
 
54
    // Optionen auswerten
55
    try
56
    {
57
        $options = Console_Getoptplus::getoptplus( $config, 'short2long', true,
58
            'strict', true );
59
    }
60
    catch ( Console_GetoptPlus_Exception $e )
61
    {
62
        $error = array( $e->getCode(), $e->getMessage() );
63
 
64
        print_r( $error );
65
        exit;
66
    }
67
 
68
    if ( isset( $options[0]["version"] ) )
69
    {
70
        echo "Version: " . VERSION . "\n";
71
        exit;
72
    }
73
    if ( !( $options[0]["site"] ) )
74
    {
75
        //Console_GetoptPlus::help();
76
        die( "Kein Kunde angegeben!\n" );
77
    }
78
    else
79
    {
80
        $site = $options[0]["site"];
81
    }
82
    if ( !$options[0]["shop"] )
83
    {
84
        $shop = $site;
85
    }
86
    else
87
    {
88
        $shop = $options[0]["shop"];
89
    }
90
    if ( !$options[0]["index"] )
91
    {
92
        $index = 1;
93
    }
94
    else
95
    {
96
        $index = $options[0]["index"];
97
    }
98
    if ( !$options[0]["type"] || !in_array( $options[0]["type"], array( "artikel",
99
        "directory", "Herstellerkatalog", "news", "sonderkasten", "aktionen",
100
        "TesterLogos", ) ) )
101
    {
102
        die( "Keine Ahnung welche Tabelle geprueft werden soll.\n" );
103
    }
104
    else
105
    {
106
        $table = $options[0]["type"];
107
    }
108
 
109
    $progress = isset( $options[0]["progress"] );
110
 
111
    // Pfad
112
    if ( !file_exists( "/var/www/vhosts/" ) )
113
    {
114
        $path = "/var/www/" . $shop . "/images/";
115
    }
116
    else
117
    {
118
        $path = "/var/www/vhosts/" . $shop . "/httpdocs/images/";
119
    }
120
    switch ( $options[0]["type"] )
121
    {
122
        case "artikel":
123
            $feld = "bild_" . $index . "_url";
124
            $path_extra = "Bild_" . $index;
125
            break;
126
        case "directory":
127
            $path_extra = "directory/" . $index . "/";
128
            if ( $index > 1 )
129
            {
130
                $feld = "bild_url" . $index;
131
            }
132
            else
133
            {
134
                $feld = "bild_url";
135
            }
136
            break;
137
        case "Herstellerkatalog":
138
            $feld = "Logo" . $index;
139
            $path_extra = "Hersteller/" . $index . "/";
140
            break;
141
        case "news":
142
            $feld = "bild_" . $index . "_url";
143
            $path_extra = "news/" . $index;
144
            break;
145
        case "aktionen":
146
            $feld = "bild_" . $index . "_url";
147
            $path_extra = "aktionen/" . $index;
148
            $table = "Aktionen";
149
            break;
150
        case "sonderkasten":
151
            $feld = "bild_url";
152
            unset( $index );
153
            $path_extra = "sonderkasten/";
154
            break;
155
        case "TesterLogos":
156
            $feld = "Logo" . $index;
157
            $path_extra = "TesterLogos/" . $index . "/";
158
            $table = "WarenTesterLogos";
159
            break;
160
    }
161
 
162
    $res = chdir( $path );
163
    if ( !$res )
164
    {
165
        die( "FEHLER: Ungueltige Pfad " . $path . "!\n" );
166
    }
167
    if ( !file_exists( $path_extra ) )
168
    {
169
        die( "FEHLER: Ungueltige Index " . $index . "!\n" );
170
    }
171
 
172
    // Initialisierung
173
    define( "__SITE__", $site );
174
    define( "__SHOP__", $shop );
175
    define( "__USE_MDB2__", true );
176
    require_once "module/functions.common.php";
177
    init_shop();
178
 
179
    //SQL
180
    $sql = "SELECT
181
            id
182
        FROM
183
            $table
184
        WHERE
185
            $feld=?
186
    ";
187
 
188
    // SQL vorbereiten
189
    $GLOBALS["sql_sth_s"] = $GLOBALS["db"]->prepare( $sql );
190
    if ( PEAR::isError( $GLOBALS["sql_sth_s"] ) )
191
    {
192
        die( $GLOBALS["sql_sth_s"]->getUserInfo() );
193
    }
194
 
195
    // Dateien zusamensuchen
196
    $files = File_Find::glob( "/.*\..*/", $path_extra, "perl" );
197
    $f_anz = count( $files );
198
 
199
    echo "Konfiguration:\n";
200
    echo "Site: $site\nShop: $shop\nTabelle: $table\nIndex: $index\n";
201
    echo str_repeat( "=", $width );
202
    echo "\n";
203
 
204
    $ok = 0;
205
    $nok = 0;
206
    $continued = 0;
207
    $f_nr = 1;
208
 
209
    if ( $f_anz )
210
    {
211
        if ( $progress )
212
        {
213
            $pb = new Console_ProgressBar( '%fraction% [%bar%] %percent% in %elapsed%',
214
                '=>', '-', $width, $f_anz );
215
        } //exit;
216
 
217
        foreach ( $files as $file )
218
        {
219
            if ( !is_file( $path_extra . "/" . $file ) )
220
            {
221
                // Verzeichnisse etc. überspringen
222
                echo "$file ==> Keine Datei\n";
223
                $continued++;
224
                continue;
225
                //exit;
226
            }
227
            $res = $GLOBALS["sql_sth_s"]->execute( $file );
228
            if ( PEAR::isError( $res ) )
229
            {
230
                die( $res->getUserInfo() );
231
            }
232
            if ( $res->numRows() )
233
            {
234
                //Bild wird noch verwendet
235
                //echo "OK\n";
236
                $ok++;
237
            }
238
            else
239
            {
240
                if ( !file_exists( $path_extra . "/trash" ) || !is_dir( $path_extra .
241
                    "/trash" ) )
242
                {
243
                    $res = mkdir( $path_extra . "/trash", 0777 );
244
                    if ( !$res )
245
                    {
246
                        die( "Konnte Verzeichnis " . $path_extra .
247
                            "/trash nicht anlegen!" );
248
                    }
249
                    $res = chown( $path_extra . "/trash", "www-data" );
250
                    if ( !$res )
251
                    {
252
                        die( "Konnte Besitzer fuer das Verzeichnis " . $path_extra .
253
                            "/trash nicht aendern!" );
254
                    }
255
                    $res = chgrp( $path_extra . "/trash", "www-data" );
256
                    if ( !$res )
257
                    {
258
                        die( "Konnte Gruppe fuer das Verzeichnis " . $path_extra .
259
                            "/trash nicht aendern!" );
260
                    }
261
                }
262
                //echo "NOK\n";
263
                rename( $path_extra . "/" . $file, $path_extra . "/trash/" .
264
                    $file );
265
                foreach ( array( "kl", "gr", "mini" ) as $size )
266
                {
267
                    if ( file_exists( $path_extra . "/" . $size . "/" . $file ) )
268
                    {
269
                        unlink( $path_extra . "/" . $size . "/" . $file );
270
                    }
271
                }
272
                $nok++;
273
            }
274
            if ( $progress )
275
            {
276
                $pb->update( $f_nr );
277
            }
278
            $f_nr++;
279
        }
280
        if ( $progress )
281
        {
282
            echo "\n";
283
        }
284
    }
285
    echo $f_anz . " Dateien gesamt\n";
286
    echo $ok . " Dateien beibehalten\n";
287
    echo $nok . " Dateien geloescht\n";
288
    echo $continued . " Dateien uebersprungen\n";
289
    //
290
 ?>