Development

/plugins/sfMediaLibraryPlugin/modules/sfMediaLibrary/lib/BasesfMediaLibraryActions.class.php

You must first sign up to be able to contribute.

root/plugins/sfMediaLibraryPlugin/modules/sfMediaLibrary/lib/BasesfMediaLibraryActions.class.php

Revision 7744, 8.5 kB (checked in by fabien, 5 years ago)

sfMediaLibraryPlugin: fixed 1.1 compatibility

Line 
1 <?php
2
3 /*
4  * This file is part of the symfony package.
5  * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
6  *
7  * For the full copyright and license information, please view the LICENSE
8  * file that was distributed with this source code.
9  */
10
11 /**
12  *
13  * @package    symfony
14  * @subpackage plugin
15  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
16  * @version    SVN: $Id: actions.class.php 1949 2006-09-05 14:40:20Z fabien $
17  */
18 class BasesfMediaLibraryActions extends sfActions
19 {
20   protected
21     $uploadDir     = '',
22     $uploadDirName = '',
23     $useThumbnails = false,
24     $thumbnailsDir = '';
25
26   public function preExecute()
27   {
28     if (sfConfig::get('app_sfMediaLibrary_use_thumbnails', true) && class_exists('sfThumbnail'))
29     {
30       $this->useThumbnails = true;
31       $this->thumbnailsDir = sfConfig::get('app_sfMediaLibrary_thumbnails_dir', 'thumbnail');
32     }
33
34     $this->uploadDirName = sfConfig::get('app_sfMediaLibrary_upload_dir', str_replace(sfConfig::get('sf_web_dir'), '', sfConfig::get('sf_upload_dir')).'/assets');
35     $this->uploadDir     = sfConfig::get('sf_web_dir').'/'.$this->uploadDirName;
36   }
37
38   public function executeIndex()
39   {
40     $currentDir = $this->dot2slash($this->getRequestParameter('dir'));
41     $this->currentDir = $this->getRequestParameter('dir');
42     $this->current_dir_slash = $currentDir.'/';
43     $this->webAbsCurrentDir = $this->getRequest()->getRelativeUrlRoot().'/'.$this->uploadDirName.'/'.$currentDir;
44     $this->absCurrentDir = $this->uploadDir.'/'.$currentDir;
45
46     $this->forward404Unless(is_dir($this->absCurrentDir));
47
48     // directories
49     $dirsQuery = sfFinder::type('dir')->maxdepth(0)->prune('.*')->discard('.*')->relative();
50     if ($this->useThumbnails)
51     {
52       $dirsQuery = $dirsQuery->discard($this->thumbnailsDir);
53     }
54     $dirs = $dirsQuery->in($this->absCurrentDir);
55     sort($dirs);
56     $this->dirs = $dirs;
57
58     // files, with stats
59     $files = sfFinder::type('file')->maxdepth(0)->prune('.*')->discard('.*')->relative()->in($this->absCurrentDir);
60     sort($files);
61     $infos = array();
62     foreach ($files as $file)
63     {
64       $ext = substr($file, strpos($file, '.') - strlen($file) + 1);
65       if (!$this->getRequestParameter('images_only') || $this->isImage($ext))
66       {
67         $infos[$file] = $this->getInfo($file);
68       }
69     }
70     $this->files = $infos;
71
72     // parent dir
73     $tmp = explode(' ', $this->currentDir);
74     array_pop($tmp);
75     $this->parentDir = implode(' ', $tmp);
76   }
77
78   protected function isImage($ext)
79   {
80     return in_array(strtolower($ext), array('png', 'jpg', 'gif'));
81   }
82
83   public function executeChoice()
84   {
85     $this->executeIndex();
86   }
87
88   public function executeRename()
89   {
90     $currentDir = $this->dot2slash($this->getRequestParameter('current_path'));
91     $this->currentDir = $this->getRequestParameter('current_path');
92     $type = $this->getRequestParameter('type');
93     $this->count = $this->getRequestParameter('count');
94     $this->webAbsCurrentDir = '/'.$this->uploadDirName.'/'.$currentDir;
95     $absCurrentDir = $this->uploadDir.'/'.$currentDir;
96     
97     $this->forward404Unless(is_dir($absCurrentDir));
98
99     $name = $this->getRequestParameter('name');
100     $new_name = $this->getRequestParameter('new_name');
101     if ($type === 'folder')
102     {
103       $new_name = $this->sanitizeDir($new_name);
104       $this->forward404Unless(is_dir($absCurrentDir.'/'.$name));
105     }
106     else
107     {
108       $new_name = $this->sanitizeFile($new_name);
109       $this->forward404Unless(is_file($absCurrentDir.'/'.$name));
110     }
111
112     @rename($absCurrentDir.'/'.$name, $absCurrentDir.'/'.$new_name);
113
114     if ($this->useThumbnails && ($type === 'file') && file_exists($absCurrentDir.'/'.$this->thumbnailsDir.'/'.$name))
115     {
116       @rename($absCurrentDir.'/'.$this->thumbnailsDir.'/'.$name, $absCurrentDir.'/'.$this->thumbnailsDir.'/'.$new_name);
117     }
118
119     $this->absCurrentDir = $absCurrentDir;
120     $this->info = array();
121     if (is_dir($absCurrentDir.'/'.$new_name) and ($type === 'folder'))
122     {
123       $this->name = $new_name;
124     }
125     else if (is_file($absCurrentDir.'/'.$new_name) and ($type === 'file'))
126     {
127       $this->name = $new_name;
128       $this->info = $this->getInfo($new_name);
129     }
130     else
131     {
132       $this->name = $name;
133       $this->info = $this->getInfo($name);
134     }
135
136     $this->type = $type;
137   }
138
139   protected function getInfo($filename)
140   {
141     $info = array();
142     $info['ext']  = substr($filename, strpos($filename, '.') - strlen($filename) + 1);
143     $stats = stat($this->absCurrentDir.'/'.$filename);
144     $info['size'] = $stats['size'];
145     $info['thumbnail'] = true;
146     if ($this->isImage($info['ext']))
147     {
148       if ($this->useThumbnails && is_readable(sfConfig::get('sf_web_dir').$this->webAbsCurrentDir.'/'.$this->thumbnailsDir.'/'.$filename))
149       {
150         $info['icon'] = $this->webAbsCurrentDir.'/'.$this->thumbnailsDir.'/'.$filename;
151       }
152       else
153       {
154         $info['icon'] = $this->webAbsCurrentDir.'/'.$filename;
155         $info['thumbnail'] = false;
156       }
157     }
158     else
159     {
160       if (is_readable(sfConfig::get('sf_web_dir').'/sfMediaLibraryPlugin/images/'.$info['ext'].'.png'))
161       {
162         $info['icon'] = '/sfMediaLibraryPlugin/images/'.$info['ext'].'.png';
163       }
164       else
165       {
166         $info['icon'] = '/sfMediaLibraryPlugin/images/unknown.png';
167       }
168     }
169
170     return $info;
171   }
172
173   public function executeUpload()
174   {
175     $currentDir = $this->dot2slash($this->getRequestParameter('current_dir'));
176     $webAbsCurrentDir = '/'.$this->uploadDirName.'/'.$currentDir;
177     $absCurrentDir = $this->uploadDir.'/'.$currentDir;
178
179     $this->forward404Unless(is_dir($absCurrentDir));
180
181     $filename = $this->sanitizeFile($this->getRequest()->getFileName('file'));
182     $info['ext']  = substr($filename, strpos($filename, '.') - strlen($filename) + 1);
183
184     if ($this->isImage($info['ext']) && $this->useThumbnails)
185     {
186       if (!is_dir($absCurrentDir.'/'.$this->thumbnailsDir))
187       {
188         // If the thumbnails directory doesn't exist, create it now
189         $old = umask(0000);
190         @mkdir($absCurrentDir.'/'.$this->thumbnailsDir, 0777, true);
191         umask($old);
192       }
193       $thumbnail = new sfThumbnail(64, 64);
194       $thumbnail->loadFile($this->getRequest()->getFilePath('file'));
195       $thumbnail->save($absCurrentDir.'/'.$this->thumbnailsDir.'/'.$filename);
196     }
197     $this->getRequest()->moveFile('file', $absCurrentDir.'/'.$filename);
198
199     $this->redirect('sfMediaLibrary/index?dir='.$this->getRequestParameter('current_dir'));
200   }
201
202   public function executeDelete()
203   {
204     $currentDir = $this->dot2slash($this->getRequestParameter('current_path'));
205     $currentFile = $this->getRequestParameter('name');
206     $absCurrentFile = $this->uploadDir.'/'.$currentDir.'/'.$currentFile;
207
208     $this->forward404Unless(is_readable($absCurrentFile));
209
210     unlink($absCurrentFile);
211
212     if ($this->useThumbnails)
213     {
214       $absThumbnailFile = $this->uploadDir.'/'.$currentDir.'/'.$this->thumbnailsDir.'/'.$currentFile;
215       if (is_readable($absThumbnailFile))
216       {
217         unlink($absThumbnailFile);
218       }
219     }
220
221     $this->redirect('sfMediaLibrary/index?dir='.$this->getRequestParameter('current_path'));
222   }
223
224   public function executeMkdir()
225   {
226     $currentDir = $this->dot2slash($this->getRequestParameter('current_dir'));
227     $dirName = $this->sanitizeDir($this->getRequestParameter('name'));
228     $absCurrentDir = $this->uploadDir.'/'.(empty($currentDir) ? '' : $currentDir.'/').$dirName;
229
230     $old = umask(0000);
231     @mkdir($absCurrentDir, 0777);
232     if ($this->useThumbnails)
233     {
234       @mkdir($absCurrentDir.'/'.$this->thumbnailsDir, 0777);
235     }
236     umask($old);
237
238     $this->redirect('sfMediaLibrary/index?dir='.$this->getRequestParameter('current_dir'));
239   }
240
241   public function executeRmdir()
242   {
243     $currentDir = $this->dot2slash('.'.$this->getRequestParameter('current_path'));
244     $absCurrentDir = $this->uploadDir.'/'.$currentDir.'/'.$this->getRequestParameter('name');
245
246     $this->forward404Unless(is_dir($absCurrentDir));
247
248     if($this->useThumbnails && is_readable($absCurrentDir.'/'.$this->thumbnailsDir))
249     {
250       rmdir($absCurrentDir.'/'.$this->thumbnailsDir);
251     }
252
253     rmdir($absCurrentDir);
254
255     $this->redirect('sfMediaLibrary/index?dir='.$this->getRequestParameter('current_path'));
256   }
257
258   protected function dot2slash($txt)
259   {
260     return preg_replace('#[\+\s]+#', '/', $txt);
261   }
262
263   protected function slash2dot($txt)
264   {
265     return preg_replace('#/+#', '+', $txt);
266   }
267
268   protected function sanitizeDir($dir)
269   {
270     return preg_replace('/[^a-z0-9_-]/i', '_', $dir);
271   }
272
273   protected function sanitizeFile($file)
274   {
275     return preg_replace('/[^a-z0-9_\.-]/i', '_', $file);
276   }
277 }
278
Note: See TracBrowser for help on using the browser.