nyroFwk  0.2
fileUploaded.class.php
Go to the documentation of this file.
1 <?php
10 class form_fileUploaded extends object {
11 
17  protected $file = array();
18 
24  protected $dir;
25 
31  protected $saved;
32 
38  protected $subdir;
39 
45  protected $helper;
46 
47  protected function afterInit() {
48  $this->dir = $this->cfg->dir;
49  if ($this->cfg->subdir) {
50  $this->subdir = $this->cfg->subdir.DS;
51  $this->dir.= $this->subdir;
52  }
53  file::createDir($this->dir);
54 
55  $uploaded = false;
56  if (strpos($this->cfg->name, '[')) {
57  $tmp = explode('[', str_replace(']', '', $this->cfg->name));
58  $tmpfile = utils::getValInArray($_FILES, $tmp);
59  if (!empty($tmpfile) && is_array($tmpfile)) {
60  $this->file = $tmpfile;
61  $this->file['saved'] = false;
62  $uploaded = $this->file['error'] == UPLOAD_ERR_OK;
63  }
64  } else if (array_key_exists($this->cfg->name, $_FILES)) {
65  $this->file = $_FILES[$this->cfg->name];
66  $this->file['saved'] = false;
67  $uploaded = $this->file['error'] == UPLOAD_ERR_OK;
68  }
69  if ($this->cfg->current && !$uploaded) {
70  $name = basename($this->cfg->current);
71  $savePath = $this->dir.$name;
72  $webPath = str_replace(DS, '/', $this->subdir.$name);
73  $this->file = array(
74  'name'=>$name,
75  'type'=>file::getType($savePath),
76  'tmp_name'=>'',
77  'error'=>UPLOAD_ERR_OK,
78  'size'=>file::size($savePath),
79  'saved'=>array(
80  'name'=>$name,
81  'savePath'=>$savePath,
82  'webPath'=>$webPath
83  ),
84  'savedFromValue'=>true
85  );
86  $this->saved = $webPath;
87  }
88 
89  $this->helper = factory::isCreable('helper_'.$this->cfg->helper)
90  ? factory::getHelper($this->cfg->helper, $this->getHelperPrm('factory'))
91  : null;
92 
93  if ($this->cfg->autoSave && !empty($this->file) && $this->isValid() === true)
94  $this->save();
95  }
96 
102  public function getHelper() {
103  return $this->helper;
104  }
105 
113  protected function getHelperPrm($name) {
114  $tmp = $this->cfg->getInArray('helperPrm', $name);
115  return is_array($tmp)? $tmp : array();
116  }
117 
126  protected function callHelper($fct, $filename = null, $defRet = true) {
127  $callback = array($this->helper, $fct);
128  if ($this->helper && is_callable($callback))
129  return call_user_func($callback, $filename, $this->getHelperPrm($fct));
130  return $defRet;
131  }
132 
137  public function save() {
138  if (!$this->file['saved'] && $this->isValid() === true) {
139  $name = $this->safeFileName($this->file['name']);
140  $savePath = $this->dir.$name;
141  if (move_uploaded_file($this->file['tmp_name'], $savePath)) {
142  $tmpName = $this->callHelper('upload', $savePath);
143  if ($tmpName && !is_bool($tmpName)) {
144  $name = $tmpName;
145  $savePath = $this->dir.$tmpName;
146  }
147  $webPath = str_replace(DS, '/', $this->subdir.$name);
148  $this->file['saved'] = array(
149  'name'=>$name,
150  'savePath'=>$savePath,
151  'webPath'=>$webPath
152  );
153  if ($this->cfg->deleteCurrent && ($current = $this->getCurrent())
154  && str_replace('/', DS, $savePath) != str_replace('/', DS, $this->cfg->dir.$current)
155  && (!array_key_exists('webPath', $this->file) || $current != $this->file['webPath'])) {
156  $this->callHelper('delete', $current);
157  file::delete($this->cfg->dir.$current);
158  $this->setCurrent(null);
159  }
160  $this->saved = $webPath;
161  }
162  }
163  }
164 
170  public function getInfo() {
171  return $this->file;
172  }
173 
179  public function getCurrent() {
180  return $this->saved? $this->saved : $this->cfg->current;
181  }
182 
189  public function setCurrent($file, $refill=false) {
190  if ($file || !$refill)
191  $this->cfg->current = $file;
192  }
193 
199  public function getView() {
200  $ret = null;
201  if ($current = $this->getCurrent()) {
202  if (!$ret = $this->callHelper('view', $current, null))
203  $ret = utils::htmlTag('a', array_merge($this->cfg->previewPrm, array('href'=>request::uploadedUri($current))), $this->cfg->previewText ? $this->cfg->previewText : basename($current));
204  }
205  return $ret;
206  }
207 
213  public function isSaved($fromRequest = false) {
214  $ret = array_key_exists('saved', $this->file) && $this->file['saved'];
215  if ($ret && $fromRequest)
216  $ret = !(isset($this->file['savedFromValue']) && $this->file['savedFromValue']);
217  return $ret;
218  }
219 
225  public function delete() {
226  $ret = false;
227  $current = $this->getCurrent();
228  if ($current && strpos($current, $this->cfg->dir) === false)
229  $current = $this->cfg->dir.$current;
230  if ($current && file::exists($current)) {
231  $ret = $this->callHelper('delete', $current);
232  file::delete($current);
233  }
234  $this->setCurrent(null);
235  $this->saved = false;
236  return $ret;
237  }
238 
244  public function isValid() {
245  $file = $this->getInfo();
246  $tmp = !request::isPost() || ($this->cfg->current) ||
247  (array_key_exists('error', $file) && $file['error'] === 0
248  && array_key_exists('size', $file) && $file['size'] > 0);
249  $helperValid = $this->callHelper('valid', $file);
250  return $tmp? (is_bool($helperValid) ? $helperValid : $helperValid) : ($this->cfg->required ? 'required' : true);
251  }
252 
259  protected function safeFileName($name) {
260  $name = strtolower(utils::urlify($name, '.'));
261  $ext = file::getExt($name);
262  if ($ext)
263  $ext = '.'.$ext;
264  $initName = substr($name, 0, -strlen($ext));
265  $i = 2;
266  while(file::exists($this->dir.DS.$name)) {
267  $name = $initName.'-'.$i.$ext;
268  $i++;
269  }
270  return $name;
271  }
272 
273  public function __toString() {
274  return $this->getCurrent()? $this->getCurrent() : '';
275  }
276 
277 }
static urlify($text, $ignore=null)
static htmlTag($tag, array $attributes, $content=null)
Definition: utils.class.php:46
if(!defined('NYROROOT')) define('NYROROOT' ROOT DS
Definition: start.inc.php:56
static getHelper($className, array $cfg=array())
setCurrent($file, $refill=false)
isSaved($fromRequest=false)
static size($file)
Definition: file.class.php:325
static exists($file)
Definition: file.class.php:97
callHelper($fct, $filename=null, $defRet=true)
static getExt($file)
Definition: file.class.php:400
static getType($file)
Definition: file.class.php:413
static uploadedUri($file, array $prm=array())
static createDir($path, $chmod=0777)
Definition: file.class.php:291
static delete($file)
Definition: file.class.php:355
static isCreable($className)
static getValInArray(array $source, array $keys)
static isPost()
Generated on Sun Oct 15 2017 22:25:20 for nyroFwk by doxygen 1.8.13