Go to the documentation of this file.00001 <?php
00010 class form_fileUploaded extends object {
00011
00017 protected $file = array();
00018
00024 protected $dir;
00025
00031 protected $saved;
00032
00038 protected $subdir;
00039
00045 protected $helper;
00046
00047 protected function afterInit() {
00048 $this->dir = $this->cfg->dir;
00049 if ($this->cfg->subdir) {
00050 $this->subdir = $this->cfg->subdir.DS;
00051 $this->dir.= $this->subdir;
00052 }
00053 file::createDir($this->dir);
00054
00055 if (strpos($this->cfg->name, '[')) {
00056 $tmp = explode('[', str_replace(']', '', $this->cfg->name));
00057 $tmpfile = utils::getValInArray($_FILES, $tmp);
00058 if (!empty($tmpfile) && is_array($tmpfile)) {
00059 $this->file = $tmpfile;
00060 $this->file['saved'] = false;
00061 }
00062 } else if (array_key_exists($this->cfg->name, $_FILES)) {
00063 $this->file = $_FILES[$this->cfg->name];
00064 $this->file['saved'] = false;
00065 }
00066
00067 $this->helper = factory::isCreable('helper_'.$this->cfg->helper)
00068 ? factory::getHelper($this->cfg->helper, $this->getHelperPrm('factory'))
00069 : null;
00070
00071 if ($this->cfg->autoSave && !empty($this->file) && $this->isvalid() === true)
00072 $this->save();
00073 }
00074
00080 public function getHelper() {
00081 return $this->helper;
00082 }
00083
00091 protected function getHelperPrm($name) {
00092 $tmp = $this->cfg->getInArray('helperPrm', $name);
00093 return is_array($tmp)? $tmp : array();
00094 }
00095
00104 protected function callHelper($fct, $filename = null, $defRet = true) {
00105 $callback = array($this->helper, $fct);
00106 if ($this->helper && is_callable($callback))
00107 return call_user_func($callback, $filename, $this->getHelperPrm($fct));
00108 return $defRet;
00109 }
00110
00115 public function save() {
00116 if (!$this->file['saved'] && $this->isValid() === true) {
00117 $name = $this->safeFileName($this->file['name']);
00118 $savePath = $this->dir.$name;
00119 if (move_uploaded_file($this->file['tmp_name'], $savePath)) {
00120 $tmpName = $this->callHelper('upload', $savePath);
00121 if ($tmpName && !is_bool($tmpName)) {
00122 $name = $tmpName;
00123 $savePath = $this->dir.$tmpName;
00124 }
00125 $webPath = str_replace(DS, '/', $this->subdir.$name);
00126 $this->file['saved'] = array(
00127 'name'=>$name,
00128 'savePath'=>$savePath,
00129 'webPath'=>$webPath
00130 );
00131 if ($this->cfg->deleteCurrent && ($current = $this->getCurrent())
00132 && str_replace('/', DS, $savePath) != str_replace('/', DS, $this->cfg->dir.$current)
00133 && (!array_key_exists('webPath', $this->file) || $current != $this->file['webPath'])) {
00134 $this->callHelper('delete', $current);
00135 file::delete($this->cfg->dir.$current);
00136 $this->setCurrent(null);
00137 }
00138 $this->saved = $webPath;
00139 }
00140 }
00141 }
00142
00148 public function getInfo() {
00149 return $this->file;
00150 }
00151
00157 public function getCurrent() {
00158 return $this->saved? $this->saved : $this->cfg->current;
00159 }
00160
00167 public function setCurrent($file, $refill=false) {
00168 if ($file || !$refill)
00169 $this->cfg->current = $file;
00170 }
00171
00177 public function getView() {
00178 $ret = null;
00179 if ($current = $this->getCurrent()) {
00180 if (!$ret = $this->callHelper('view', $current, null))
00181 $ret = utils::htmlTag('a', array('href'=>request::uploadedUri($current)), $current);
00182 }
00183 return $ret;
00184 }
00185
00191 public function isSaved() {
00192 return array_key_exists('saved', $this->file) && $this->file['saved'];
00193 }
00194
00200 public function delete() {
00201 $ret = false;
00202 $current = $this->getCurrent();
00203 if ($current && strpos($current, $this->cfg->dir) === false)
00204 $current = $this->cfg->dir.$current;
00205 if ($current && file::exists($current)) {
00206 $ret = $this->callHelper('delete', $current);
00207 file::delete($current);
00208 $this->setCurrent(null);
00209 }
00210 return $ret;
00211 }
00212
00218 public function isValid() {
00219 $file = $this->getInfo();
00220 $tmp = !request::isPost() || ($this->cfg->current) ||
00221 (array_key_exists('error', $file) && $file['error'] === 0
00222 && array_key_exists('size', $file) && $file['size'] > 0);
00223 $helperValid = $this->callHelper('valid', $file);
00224 return $tmp? (is_bool($helperValid) ? $helperValid : $helperValid) : ($this->cfg->required ? 'required' : true);
00225 }
00226
00233 protected function safeFileName($name) {
00234 $name = utils::urlify(strtolower($name), '.');
00235 $ext = file::getExt($name);
00236 if ($ext)
00237 $ext = '.'.$ext;
00238 $initName = substr($name, 0, -strlen($ext));
00239 $i = 2;
00240 while(file::exists($this->dir.DS.$name)) {
00241 $name = $initName.'-'.$i.$ext;
00242 $i++;
00243 }
00244 return $name;
00245 }
00246
00247 public function __toString() {
00248 return $this->getCurrent()? $this->getCurrent() : '';
00249 }
00250
00251 }