/** * Validate the field against the validation rules. * * @uses Transit\Transit * @uses Transit\File * @uses Transit\Validator\ImageValidator * * @param Model $model * @param array $data * @param string $method * @param array $params * @return bool * @throws UnexpectedValueException */ protected function _validate(Model $model, $data, $method, array $params) { foreach ($data as $field => $value) { if ($this->_allowEmpty($model, $field, $value)) { return true; } else { if ($this->_isEmpty($value)) { return false; } } $file = null; // Upload, use temp file if (is_array($value)) { $file = new File($value); // Import, copy file for validation } else { if (preg_match('/^http/i', $value)) { $target = TMP . md5($value); // Already imported from previous validation if (file_exists($target)) { $file = new File($target); // Attempt to copy } else { $transit = new \Transit\Transit($value); $transit->setDirectory(TMP); if ($transit->importFromRemote()) { $file = $transit->getOriginalFile(); $file->rename(basename($target)); } } // Save temp so we can delete later if ($file) { $this->_tempFile = $file; } } } if (!$file) { $this->log(sprintf('Invalid upload or import for validation: %s', json_encode($value)), LOG_DEBUG); return false; } $validator = new ImageValidator(); $validator->setFile($file); return call_user_func_array(array($validator, $method), $params); } return false; }
<?php require_once 'include.php'; if (!empty($_POST['url'])) { $transit = new Transit\Transit($_POST['url']); $transit->setDirectory(__DIR__ . '/tmp/'); try { if ($transit->importFromRemote()) { debug($transit->getOriginalFile()->toArray()); } } catch (Exception $e) { debug($e->getMessage()); } } ?> <!DOCTYPE html> <head> <title>Transit - Import Remote</title> </head> <body> <form action="" method="POST" enctype="multipart/form-data"> <input type="text" name="url" placeholder="URL"> <button type="submit">Import</button> </form> </body>