| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
class BasesfMediaLibraryActions extends sfActions |
|---|
| 19 |
{ |
|---|
| 20 |
public function executeIndex() |
|---|
| 21 |
{ |
|---|
| 22 |
$currentDir = $this->dot2slash($this->getRequestParameter('dir')); |
|---|
| 23 |
$this->currentDir = $this->getRequestParameter('dir'); |
|---|
| 24 |
$this->current_dir_slash = $currentDir . '/'; |
|---|
| 25 |
$this->webAbsCurrentDir = '/'.sfConfig::get('sf_upload_dir_name').'/assets'.$currentDir; |
|---|
| 26 |
$this->absCurrentDir = sfConfig::get('sf_upload_dir').'/assets/'.$currentDir; |
|---|
| 27 |
|
|---|
| 28 |
$this->forward404Unless(is_dir($this->absCurrentDir)); |
|---|
| 29 |
|
|---|
| 30 |
$dirs = sfFinder::type('dir')->maxdepth(0)->prune('.*')->discard('.*')->relative()->in($this->absCurrentDir); |
|---|
| 31 |
$files = sfFinder::type('file')->maxdepth(0)->prune('.*')->discard('.*')->relative()->in($this->absCurrentDir); |
|---|
| 32 |
|
|---|
| 33 |
sort($dirs); |
|---|
| 34 |
sort($files); |
|---|
| 35 |
|
|---|
| 36 |
$this->dirs = $dirs; |
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 |
$infos = array(); |
|---|
| 40 |
foreach ($files as $file) |
|---|
| 41 |
{ |
|---|
| 42 |
$info = array(); |
|---|
| 43 |
|
|---|
| 44 |
$info['ext'] = substr($file, strpos($file, '.') - strlen($file) + 1); |
|---|
| 45 |
if ($this->getRequestParameter('images_only') && !$this->isImage($info['ext'])) |
|---|
| 46 |
{ |
|---|
| 47 |
continue; |
|---|
| 48 |
} |
|---|
| 49 |
$stats = stat($this->absCurrentDir.'/'.$file); |
|---|
| 50 |
$info['size'] = $stats['size']; |
|---|
| 51 |
$info['icon'] = $this->isImage($info['ext']) ? $this->webAbsCurrentDir.'/'.$file : (is_readable(sfConfig::get('sf_web_dir').'/sfMediaLibraryPlugin/images/'.$info['ext'].'.png') ? '/sfMediaLibraryPlugin/images/'.$info['ext'].'.png' : '/sfMediaLibraryPlugin/images/unknown.png'); |
|---|
| 52 |
$infos[$file] = $info; |
|---|
| 53 |
} |
|---|
| 54 |
$this->files = $infos; |
|---|
| 55 |
|
|---|
| 56 |
|
|---|
| 57 |
$tmp = explode(' ', $this->currentDir); |
|---|
| 58 |
array_pop($tmp); |
|---|
| 59 |
$this->parentDir = implode(' ', $tmp); |
|---|
| 60 |
} |
|---|
| 61 |
|
|---|
| 62 |
protected function isImage($ext) |
|---|
| 63 |
{ |
|---|
| 64 |
return in_array($ext, array('png', 'jpg', 'gif')); |
|---|
| 65 |
} |
|---|
| 66 |
|
|---|
| 67 |
public function executeChoice() |
|---|
| 68 |
{ |
|---|
| 69 |
$this->executeIndex(); |
|---|
| 70 |
} |
|---|
| 71 |
|
|---|
| 72 |
public function executeRename() |
|---|
| 73 |
{ |
|---|
| 74 |
$current_path = $this->dot2slash($this->getRequestParameter('current_path')); |
|---|
| 75 |
$this->current_path = $this->getRequestParameter('current_path'); |
|---|
| 76 |
$type = $this->getRequestParameter('type'); |
|---|
| 77 |
$this->count = $this->getRequestParameter('count'); |
|---|
| 78 |
|
|---|
| 79 |
$this->abs_current_path = sfConfig::get('sf_upload_dir').'/assets/'.$current_path; |
|---|
| 80 |
$this->web_abs_current_path = '/'.sfConfig::get('sf_upload_dir_name').'/assets/'.$current_path; |
|---|
| 81 |
|
|---|
| 82 |
$name = $this->getRequestParameter('name'); |
|---|
| 83 |
$new_name = $this->getRequestParameter('new_name'); |
|---|
| 84 |
if ($type === 'folder') |
|---|
| 85 |
{ |
|---|
| 86 |
$new_name = $this->sanitizeDir($new_name); |
|---|
| 87 |
$this->forward404Unless(is_dir($this->abs_current_path.'/'.$name)); |
|---|
| 88 |
} |
|---|
| 89 |
else |
|---|
| 90 |
{ |
|---|
| 91 |
$new_name = $this->sanitizeFile($new_name); |
|---|
| 92 |
$this->forward404Unless(is_file($this->abs_current_path.'/'.$name)); |
|---|
| 93 |
} |
|---|
| 94 |
|
|---|
| 95 |
@rename($this->abs_current_path.'/'.$name, $this->abs_current_path.'/'.$new_name); |
|---|
| 96 |
|
|---|
| 97 |
$this->info = array(); |
|---|
| 98 |
if (is_dir($this->abs_current_path.'/'.$new_name) and ($type === 'folder')) |
|---|
| 99 |
{ |
|---|
| 100 |
$this->name = $new_name; |
|---|
| 101 |
} |
|---|
| 102 |
else if (is_file($this->abs_current_path.'/'.$new_name) and ($type === 'file')) |
|---|
| 103 |
{ |
|---|
| 104 |
$this->name = $new_name; |
|---|
| 105 |
$this->getInfo($new_name); |
|---|
| 106 |
} |
|---|
| 107 |
else |
|---|
| 108 |
{ |
|---|
| 109 |
$this->name = $name; |
|---|
| 110 |
$this->getInfo($name); |
|---|
| 111 |
} |
|---|
| 112 |
$this->type = $type; |
|---|
| 113 |
} |
|---|
| 114 |
|
|---|
| 115 |
protected function getInfo($filename) |
|---|
| 116 |
{ |
|---|
| 117 |
$this->info['ext'] = substr($filename, strpos($filename, '.') - strlen($filename) + 1); |
|---|
| 118 |
$stats = stat($this->abs_current_path.'/'.$filename); |
|---|
| 119 |
$this->info['size'] = $stats['size']; |
|---|
| 120 |
$this->info['icon'] = $this->isImage($this->info['ext']) ? $this->web_abs_current_path.'/'.$filename : (is_readable(sfConfig::get('sf_web_dir').'/sfMediaLibraryPlugin/images/'.$this->info['ext'].'.png') ? '/sfMediaLibraryPlugin/images/'.$this->info['ext'].'.png' : '/sfMediaLibraryPlugin/images/unknown.png'); |
|---|
| 121 |
} |
|---|
| 122 |
|
|---|
| 123 |
public function executeUpload() |
|---|
| 124 |
{ |
|---|
| 125 |
$currentDir = $this->dot2slash($this->getRequestParameter('current_dir')); |
|---|
| 126 |
$webAbsCurrentDir = '/'.sfConfig::get('sf_upload_dir_name').'/assets/'.$currentDir; |
|---|
| 127 |
$absCurrentDir = sfConfig::get('sf_upload_dir').'/assets/'.$currentDir; |
|---|
| 128 |
|
|---|
| 129 |
$this->forward404Unless(is_dir($absCurrentDir)); |
|---|
| 130 |
|
|---|
| 131 |
$fileName = $this->sanitizeFile($this->getRequest()->getFileName('file')); |
|---|
| 132 |
|
|---|
| 133 |
$this->getRequest()->moveFile('file', $absCurrentDir.'/'.$fileName); |
|---|
| 134 |
|
|---|
| 135 |
$this->redirect('sfMediaLibrary/index?dir='.$this->getRequestParameter('current_dir')); |
|---|
| 136 |
} |
|---|
| 137 |
|
|---|
| 138 |
public function executeDelete() |
|---|
| 139 |
{ |
|---|
| 140 |
$currentDir = $this->dot2slash($this->getRequestParameter('current_path')); |
|---|
| 141 |
$currentFile = $this->getRequestParameter('name'); |
|---|
| 142 |
$absCurrentFile = sfConfig::get('sf_upload_dir').'/assets/'.$currentDir.'/'.$currentFile; |
|---|
| 143 |
|
|---|
| 144 |
$this->forward404Unless(is_readable($absCurrentFile)); |
|---|
| 145 |
|
|---|
| 146 |
unlink($absCurrentFile); |
|---|
| 147 |
|
|---|
| 148 |
$this->redirect('sfMediaLibrary/index?dir='.$this->getRequestParameter('current_path')); |
|---|
| 149 |
} |
|---|
| 150 |
|
|---|
| 151 |
public function executeMkdir() |
|---|
| 152 |
{ |
|---|
| 153 |
$currentDir = $this->dot2slash($this->getRequestParameter('current_dir')); |
|---|
| 154 |
$dirName = $this->sanitizeDir($this->getRequestParameter('name')); |
|---|
| 155 |
$absCurrentDir = sfConfig::get('sf_upload_dir').'/assets/'.(empty($currentDir) ? '' : $currentDir.'/').$dirName; |
|---|
| 156 |
|
|---|
| 157 |
$old = umask(0000); |
|---|
| 158 |
@mkdir($absCurrentDir, 0777); |
|---|
| 159 |
umask($old); |
|---|
| 160 |
|
|---|
| 161 |
$this->redirect('sfMediaLibrary/index?dir='.$this->getRequestParameter('current_dir')); |
|---|
| 162 |
} |
|---|
| 163 |
|
|---|
| 164 |
public function executeRmdir() |
|---|
| 165 |
{ |
|---|
| 166 |
$currentDir = $this->dot2slash('.'.$this->getRequestParameter('current_path')); |
|---|
| 167 |
$absCurrentDir = sfConfig::get('sf_upload_dir').'/assets/'.$currentDir.'/'.$this->getRequestParameter('name'); |
|---|
| 168 |
|
|---|
| 169 |
$this->forward404Unless(is_dir($absCurrentDir)); |
|---|
| 170 |
|
|---|
| 171 |
rmdir($absCurrentDir); |
|---|
| 172 |
|
|---|
| 173 |
$this->redirect('sfMediaLibrary/index?dir='.$this->getRequestParameter('current_path')); |
|---|
| 174 |
} |
|---|
| 175 |
|
|---|
| 176 |
protected function dot2slash($txt) |
|---|
| 177 |
{ |
|---|
| 178 |
return preg_replace('#[\+\s]+#', '/', $txt); |
|---|
| 179 |
} |
|---|
| 180 |
|
|---|
| 181 |
protected function slash2dot($txt) |
|---|
| 182 |
{ |
|---|
| 183 |
return preg_replace('#/+#', '+', $txt); |
|---|
| 184 |
} |
|---|
| 185 |
|
|---|
| 186 |
protected function sanitizeDir($dir) |
|---|
| 187 |
{ |
|---|
| 188 |
return preg_replace('/[^a-z0-9_-]/i', '_', $dir); |
|---|
| 189 |
} |
|---|
| 190 |
|
|---|
| 191 |
protected function sanitizeFile($file) |
|---|
| 192 |
{ |
|---|
| 193 |
return preg_replace('/[^a-z0-9_\.-]/i', '_', $file); |
|---|
| 194 |
} |
|---|
| 195 |
} |
|---|
| 196 |
|
|---|