Aleksander Machniak
2016-05-22 77b5d7ee304a688a2eb115ce04b460b43c0dd700
commit | author | age
ceb508 1 #!/usr/bin/env php
AM 2 <?php
3 /*
4  +-----------------------------------------------------------------------+
5  | bin/updatecss.sh                                                      |
6  |                                                                       |
7  | This file is part of the Roundcube Webmail client                     |
8  | Copyright (C) 2010-2013, The Roundcube Dev Team                       |
9  |                                                                       |
10  | Licensed under the GNU General Public License version 3 or            |
11  | any later version with exceptions for skins & plugins.                |
12  | See the README file for a full license statement.                     |
13  |                                                                       |
14  | PURPOSE:                                                              |
15  |   Update cache-baster marks for css background images                 |
16  +-----------------------------------------------------------------------+
17  | Author: Aleksander Machniak <alec@alec.pl>                            |
18  +-----------------------------------------------------------------------+
19 */
20
0ea079 21 define('INSTALL_PATH', realpath(__DIR__ . '/..') . '/' );
ceb508 22
AM 23 require_once INSTALL_PATH . 'program/include/clisetup.php';
24
25 // get arguments
26 $opts = rcube_utils::get_opt(array(
27     'd' => 'dir',
28 ));
29
30 if (empty($opts['dir'])) {
ac5b58 31     print "Skin directory not specified (--dir). Using skins/ and plugins/*/skins/.\n";
ceb508 32
AM 33     $dir     = INSTALL_PATH . 'skins';
34     $dir_p   = INSTALL_PATH . 'plugins';
35     $skins   = glob("$dir/*", GLOB_ONLYDIR);
36     $skins_p = glob("$dir_p/*/skins/*", GLOB_ONLYDIR);
37
38     $dirs = array_merge($skins, $skins_p);
39 }
40 // Check if directory exists
41 else if (!file_exists($opts['dir'])) {
42     rcube::raise_error("Specified directory doesn't exist.", false, true);
43 }
44 else {
45     $dirs = array($opts['dir']);
46 }
47
48 foreach ($dirs as $dir) {
49     $img_dir = $dir . '/images';
50     if (!file_exists($img_dir)) {
51         continue;
52     }
53
54     $files   = get_files($dir);
55     $images  = get_images($img_dir);
56     $find    = array();
57     $replace = array();
58
59     // build regexps array
60     foreach ($images as $path => $sum) {
61         $path_ex   = str_replace('.', '\\.', $path);
62         $find[]    = "#url\(['\"]?images/$path_ex(\?v=[a-f0-9-\.]+)?['\"]?\)#";
63         $replace[] = "url(images/$path?v=$sum)";
64     }
65
66     foreach ($files as $file) {
67         $file    = $dir . '/' . $file;
68         print "File: $file\n";
69         $content = file_get_contents($file);
70         $content = preg_replace($find, $replace, $content, -1, $count);
71         if ($count) {
72             file_put_contents($file, $content);
73         }
74     }
75 }
76
77
78 function get_images($dir)
79 {
80     $images = array();
81     $dh     = opendir($dir);
82
83     while ($file = readdir($dh)) {
84         if (preg_match('/^(.+)\.(gif|ico|png|jpg|jpeg)$/', $file, $m)) {
85             $filepath = "$dir/$file";
86             $images[$file] = substr(md5_file($filepath), 0, 4) . '.' . filesize($filepath);
87             print "Image: $filepath ({$images[$file]})\n";
88         }
89         else if ($file != '.' && $file != '..' && is_dir($dir . '/' . $file)) {
90             foreach (get_images($dir . '/' . $file) as $img => $sum) {
91                 $images[$file . '/' . $img] = $sum;
92             }
93         }
94     }
95
96     closedir($dh);
97
98     return $images;
99 }
100
101 function get_files($dir)
102 {
103     $files = array();
104     $dh    = opendir($dir);
105
106     while ($file = readdir($dh)) {
107         if (preg_match('/^(.+)\.(css|html)$/', $file, $m)) {
108             $files[] = $file;
109         }
110         else if ($file != '.' && $file != '..' && is_dir($dir . '/' . $file)) {
111             foreach (get_files($dir . '/' . $file) as $f) {
112                 $files[] = $file . '/' . $f;
113             }
114         }
115     }
116
117     closedir($dh);
118
119     return $files;
120 }
121
122 ?>