source: extensions/Synchronize_local_directory/SynchronizeLocalDirectory.php @ 14974

Last change on this file since 14974 was 12844, checked in by RMM, 12 years ago

first commit

File size: 5.7 KB
Line 
1<?php
2
3if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!');
4
5#include_once(PHPWG_ROOT_PATH.'include/common.inc.php' );
6include_once(PHPWG_ROOT_PATH.'admin/include/image.class.php');
7include_once(PHPWG_ROOT_PATH.'admin/include/functions_upload.inc.php');
8
9class SynchronizeLocalDirectory
10{
11    public $cnt_new_dir = 0;
12    public $cnt_new_images = 0;
13    public $cnt_new_thumbnails = 0;
14    public $debug = '';
15   
16    private $high_res_dir = 'pwg_high';
17
18   
19    function rglob($pattern='*', $flags = 0, $path='')
20    {
21         $paths=glob($path.'*', GLOB_MARK|GLOB_ONLYDIR|GLOB_NOSORT);
22         $files=glob($path.$pattern, $flags);
23         foreach ($paths as $path) { $files=array_merge($files,$this->rglob($pattern, $flags, $path)); }
24         sort($files);
25         return $files;
26    }
27
28    function directory_contains_files($path)
29    {
30        $files = glob($path.'*');
31        $found_files = false;
32        foreach($files as $file)
33        {
34            if (!is_dir($file))
35            {
36                $found_files = true;
37                break;
38            }
39        }
40        return $found_files;
41    }
42   
43    function create_thumbnail($high_res_file, $dest_file)
44    {
45        global $conf;
46        #copied this code from piwigo core. why is there no function to do that???
47        #TODO: overwrite the thumbnail, if the high_res_file is newer than the thumbnail.
48        if (! file_exists($dest_file))
49        {
50            $img = new pwg_image($high_res_file);
51            $img->pwg_resize(
52                $dest_file,
53                $conf['upload_form_thumb_maxwidth'],
54                $conf['upload_form_thumb_maxheight'],
55                $conf['upload_form_thumb_quality'],
56                false,
57                true,
58                $conf['upload_form_thumb_crop'],
59                $conf['upload_form_thumb_follow_orientation']
60            );
61            $this->cnt_new_thumbnails++;
62            $img->destroy();
63        }
64    }
65
66    function create_websize_image($high_res_file, $dest_file)
67    {
68        global $conf;
69        #TODO: overwrite the websizedimage, if the high_res_file is newer than the websized image.
70        if (! file_exists($dest_file))
71        {
72            $img = new pwg_image($high_res_file);
73            $img->pwg_resize(
74                $dest_file,
75                $conf['upload_form_websize_maxwidth'],
76                $conf['upload_form_websize_maxheight'],
77                $conf['upload_form_websize_quality'],
78                $conf['upload_form_automatic_rotation'],
79                false
80            );
81            $img->destroy();
82        }
83    }
84   
85    function create_resized_images($base_path)
86    {
87        global $conf;
88        $high_res_path = $base_path.'/'.$this->high_res_dir;
89        $files = glob($high_res_path.'/*');
90        foreach($files as $file)
91        {
92            if (!is_dir($file))
93            {
94                if (in_array(pathinfo($file, PATHINFO_EXTENSION), $conf['picture_ext']))
95                {
96                    $web_size_file = $base_path.'/'.basename($file);
97                    $thumb_file = file_path_for_type($web_size_file, 'thumb');
98               
99                    #TODO: integreate the two functions below in one, or directly add a pwg_image object as parameter
100                    #(dont know how much time it needs to instanziate this object two times, like its done now)
101                    $this->create_thumbnail($file, $thumb_file);
102                    $this->create_websize_image($file, $web_size_file);
103                } #TODO: also create thumbnails for videos
104            }
105        }
106    }
107   
108    function synchronize()
109    {
110        global $conf;
111        $sync_source_dir = '/home/gallery2/albums/Reise08';
112        $piwigo_gallery_dir = '/var/lib/piwigo/galleries/Reise08';
113        #$piwigo_gallery_dir = '/tmp/test';
114
115        #counters:
116
117        $sync_source_dirs = $this->rglob('*',GLOB_MARK|GLOB_ONLYDIR,$sync_source_dir);
118        $piwigo_dirs = $this->rglob('*',GLOB_MARK|GLOB_ONLYDIR,$piwigo_gallery_dir);
119
120        #remove dir prefixes
121        # TODO: if you have dirs with a "%" in your filename this will fail
122        $sync_source_dirs = preg_replace("%$sync_source_dir%", '', $sync_source_dirs);
123        $piwigo_dirs = preg_replace("%$piwigo_gallery_dir%", '', $piwigo_dirs);
124
125        reset($piwigo_dirs);
126        foreach ($sync_source_dirs as $source_dir)
127        {
128            $piwigo_dir = current($piwigo_dirs);
129            # dont use the thumbnail or highres dirs for comparison
130            while (basename($piwigo_dir) == $conf['dir_thumbnail'] || basename($piwigo_dir) == $this->high_res_dir)
131            {
132                next($piwigo_dirs);
133                $piwigo_dir = current($piwigo_dirs);
134            }
135
136            while (strcmp($source_dir, $piwigo_dir) > 0)
137            {
138                #TODO: there is a dir in piwigo, which doesnt exist in the source. Delete it in piwigo.
139                next($piwigo_dirs);
140                $piwigo_dir = current($piwigo_dirs);
141            }
142
143            if (strcmp($source_dir, $piwigo_dir) == 0) #equal
144            {
145                #TODO: do a comparison if there are deleted files in the source directory
146                #check if there are new files in the source directory
147                $this->create_resized_images($piwigo_gallery_dir.'/'.$source_dir);
148
149                next($piwigo_dirs);
150            } else
151            {
152                #There is a dir in the source which doesnt exist in piwigo yet. create it!
153                mkdir($piwigo_gallery_dir.'/'.$source_dir, 0775);
154                # create the thumbnail dir and a symlink to the high resolution dir, if there are files
155                # in this dir.
156                if ($this->directory_contains_files($sync_source_dir.'/'.$source_dir))
157                {
158                    mkdir($piwigo_gallery_dir.'/'.$source_dir.'/'.$conf['dir_thumbnail'], 0775);
159                    symlink($sync_source_dir.'/'.$source_dir, $piwigo_gallery_dir.'/'.$source_dir.'/'.$this->high_res_dir);
160                    $this->cnt_new_dir++;
161                    $this->create_resized_images($piwigo_gallery_dir.'/'.$source_dir);
162                }
163            }
164        }
165    }
166}
167?>
168
Note: See TracBrowser for help on using the repository browser.