/** * @covers Respect\Validation\Rules\File::validate */ public function testShouldValidateObjects() { $rule = new File(); $object = $this->createMock('SplFileInfo', ['isFile'], ['somefile.txt']); $object->expects($this->once())->method('isFile')->will($this->returnValue(true)); $this->assertTrue($rule->validate($object)); }
/** * Handles a single upload by given CUploadedFile and returns an array * of informations. * * The 'error' attribute of the array, indicates there was an error. * * Informations on error: * - error: true * - errorMessage: some message * - name: name of the file * - size: file size * * Informations on success: * - error: false * - name: name of the uploaded file * - size: file size * - guid: of the file * - url: url to the file * - thumbnailUrl: url to the thumbnail if exists * * @param type $cFile * @return Array Informations about the uploaded file */ protected function handleFileUpload($cFile, $object = null) { $output = array(); $file = new File(); $file->setUploadedFile($cFile); if ($object != null) { $file->object_id = $object->getPrimaryKey(); $file->object_model = get_class($object); } if ($file->validate() && $file->save()) { $output['error'] = false; $output['guid'] = $file->guid; $output['name'] = $file->file_name; $output['title'] = $file->title; $output['size'] = $file->size; $output['mimeIcon'] = HHtml::getMimeIconClassByExtension($file->getExtension()); $output['mimeBaseType'] = $file->getMimeBaseType(); $output['mimeSubType'] = $file->getMimeSubType(); $output['url'] = $file->getUrl("", false); $output['thumbnailUrl'] = $file->getPreviewImageUrl(200, 200); } else { $output['error'] = true; $output['errors'] = $file->getErrors(); } $output['name'] = $file->file_name; $output['size'] = $file->size; $output['deleteUrl'] = ""; $output['deleteType'] = ""; $output['thumbnailUrl'] = ""; return $output; }
public function actionCreates() { $model = new News(); $file = new File(); $model->member_id = Yii::app()->user->id; $model->create_at = date('Y-m-d H:i:s'); $model->update_at = date('Y-m-d H:i:s'); // Uncomment the following line if AJAX validation is needed // $this->performAjaxValidation($model); if (isset($_POST['News']) && isset($_POST['File'])) { $model->attributes = $_POST['News']; $file->attributes = $_POST['File']; $model->pic = 'noimage.jpg'; $model->validate(); $file->validate(); if ($model->getErrors() == null && $file->getErrors() == null) { $file->file = CUploadedFile::getInstance($file, 'file'); if ($file->file != null) { $filename = time() . '.' . $file->file->getExtensionName(); $file->file->saveAs(Yii::app()->params['pathUpload'] . $filename); $model->pic = $filename; } else { $model->pic = 'noimage.jpg'; } if ($model->save()) { $this->redirect(array('view', 'id' => $model->news_id)); } } } $this->render('create', array('model' => $model, 'file' => $file)); }
/** * This function performs the validation work for complex object models. * * In addition to checking the current object, all related objects will * also be validated. If all pass then <code>true</code> is returned; otherwise * an aggreagated array of ValidationFailed objects will be returned. * * @param array $columns Array of column names to validate. * @return mixed <code>true</code> if all validations pass; array of <code>ValidationFailed</code> objets otherwise. */ protected function doValidate($columns = null) { if (!$this->alreadyInValidation) { $this->alreadyInValidation = true; $retval = null; $failureMap = array(); // We call the validate method on the following object(s) if they // were passed to this object by their coresponding set // method. This object relates to these object(s) by a // foreign key reference. if ($this->aCollection !== null) { if (!$this->aCollection->validate($columns)) { $failureMap = array_merge($failureMap, $this->aCollection->getValidationFailures()); } } if ($this->aFile !== null) { if (!$this->aFile->validate($columns)) { $failureMap = array_merge($failureMap, $this->aFile->getValidationFailures()); } } if (($retval = CollectionFilePeer::doValidate($this, $columns)) !== true) { $failureMap = array_merge($failureMap, $retval); } $this->alreadyInValidation = false; } return !empty($failureMap) ? $failureMap : true; }
/** * This function performs the validation work for complex object models. * * In addition to checking the current object, all related objects will * also be validated. If all pass then <code>true</code> is returned; otherwise * an aggreagated array of ValidationFailed objects will be returned. * * @param array $columns Array of column names to validate. * @return mixed <code>true</code> if all validations pass; array of <code>ValidationFailed</code> objets otherwise. */ protected function doValidate($columns = null) { if (!$this->alreadyInValidation) { $this->alreadyInValidation = true; $retval = null; $failureMap = array(); // We call the validate method on the following object(s) if they // were passed to this object by their coresponding set // method. This object relates to these object(s) by a // foreign key reference. if ($this->aTrack !== null) { if (!$this->aTrack->validate($columns)) { $failureMap = array_merge($failureMap, $this->aTrack->getValidationFailures()); } } if ($this->aFeaturevectortype !== null) { if (!$this->aFeaturevectortype->validate($columns)) { $failureMap = array_merge($failureMap, $this->aFeaturevectortype->getValidationFailures()); } } if ($this->aFile !== null) { if (!$this->aFile->validate($columns)) { $failureMap = array_merge($failureMap, $this->aFile->getValidationFailures()); } } if (($retval = FeaturevectorPeer::doValidate($this, $columns)) !== true) { $failureMap = array_merge($failureMap, $retval); } if ($this->collDistancesRelatedByTrackAId !== null) { foreach ($this->collDistancesRelatedByTrackAId as $referrerFK) { if (!$referrerFK->validate($columns)) { $failureMap = array_merge($failureMap, $referrerFK->getValidationFailures()); } } } if ($this->collDistancesRelatedByTrackBId !== null) { foreach ($this->collDistancesRelatedByTrackBId as $referrerFK) { if (!$referrerFK->validate($columns)) { $failureMap = array_merge($failureMap, $referrerFK->getValidationFailures()); } } } if ($this->collDistancejobs !== null) { foreach ($this->collDistancejobs as $referrerFK) { if (!$referrerFK->validate($columns)) { $failureMap = array_merge($failureMap, $referrerFK->getValidationFailures()); } } } if ($this->collFeaturevectorsegments !== null) { foreach ($this->collFeaturevectorsegments as $referrerFK) { if (!$referrerFK->validate($columns)) { $failureMap = array_merge($failureMap, $referrerFK->getValidationFailures()); } } } $this->alreadyInValidation = false; } return !empty($failureMap) ? $failureMap : true; }
/** * Constructs a SimpleXMLElement * * @param string $fileName * * @throws Xinc\Core\Project\Config\Exception\FileNotFoundException */ public static function load($fileName) { if (!file_exists($fileName)) { throw new Exception\FileNotFoundException($fileName); } else { $data = file_get_contents($fileName); } $file = new File($data); $file->validate(); return $file; }
/** * Attaches files by url which found in content text. * This is experimental and only supports image files at the moment. * * @param HActiveRecord $record to bind files to * @param String $text to parse for links */ public static function attachFiles($record, $text) { if (!$record instanceof HActiveRecord) { throw new CException("Invalid content object given!"); } $max = 5; $count = 1; $text = preg_replace_callback('/http(.*?)(\\s|$)/i', function ($match) use(&$count, &$max, &$record) { if ($max > $count) { $url = $match[0]; $ch = curl_init(); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false); curl_setopt($ch, CURLOPT_HEADER, true); curl_setopt($ch, CURLOPT_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS); curl_setopt($ch, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS); if (HSetting::Get('enabled', 'proxy')) { curl_setopt($ch, CURLOPT_PROXY, HSetting::Get('server', 'proxy')); curl_setopt($ch, CURLOPT_PROXYPORT, HSetting::Get('port', 'proxy')); if (defined('CURLOPT_PROXYUSERNAME')) { curl_setopt($ch, CURLOPT_PROXYUSERNAME, HSetting::Get('user', 'proxy')); } if (defined('CURLOPT_PROXYPASSWORD')) { curl_setopt($ch, CURLOPT_PROXYPASSWORD, HSetting::Get('pass', 'proxy')); } if (defined('CURLOPT_NOPROXY')) { curl_setopt($ch, CURLOPT_NOPROXY, HSetting::Get('noproxy', 'proxy')); } } $ret = curl_exec($ch); $contentType = curl_getinfo($ch, CURLINFO_CONTENT_TYPE); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); list($headers, $outputContent) = explode("\r\n\r\n", $ret, 2); curl_close($ch); if ($httpCode == 200 && substr($contentType, 0, 6) == 'image/') { $extension = 'img'; if ($contentType == 'image/jpeg' || $contentType == 'image/jpg') { $extension = 'jpg'; } elseif ($contentType == 'image/gif') { $extension = 'gif'; } elseif ($contentType == 'image/png') { $extension = 'png'; } $file = new File(); $file->object_model = get_class($record); $file->object_id = $record->getPrimaryKey(); $file->mime_type = $contentType; $file->title = "Link Image"; $file->file_name = "LinkImage." . $extension; $file->newFileContent = $outputContent; $file->validate(); $file->save(); } } $count++; }, $text); }
protected static function load_file($filename = null) { if (!$filename) { $filename = "files/" . $_GET['filename']; } $file = new File($filename); if ($file->validate()) { return $file; } else { Error404(); } }
public static function recycle(File $file, $verbose = true) { $recylcingFolder = Folder::find_or_make(Config::inst()->get("MetaTagCMSControlFiles", "recycling_bin_name")); if ($recylcingFolder) { if ($file) { if ($file->exists()) { if (file_exists($file->getFullPath())) { $valid = $file->validate(); if ($valid->valid()) { $record = new MetaTagCMSControlFileUse_RecyclingRecord(); $record->FileID = $file->ID; $record->FromFolderID = $file->ParentID; $record->write(); //doing it..... $file->ParentID = $recylcingFolder->ID; $file->write(); //IMPORTANT! return true; } } $record = new MetaTagCMSControlFileUse_RecyclingRecord(); $record->FileID = $file->ID; $record->FromFolderID = $file->ParentID; $record->write(); DB::query("UPDATE \"File\" SET \"ParentID\" = " . $recylcingFolder->ID . " WHERE \"File\".\"ID\" = " . $file->ID); return true; } } } return false; }