Ejemplo n.º 1
0
 public function saveInto(DataObjectInterface $record)
 {
     if (!isset($_FILES[$this->name])) {
         return false;
     }
     $fileClass = File::get_class_for_file_extension(pathinfo($_FILES[$this->name]['name'], PATHINFO_EXTENSION));
     if ($this->relationAutoSetting) {
         // assume that the file is connected via a has-one
         $hasOnes = $record->has_one($this->name);
         // try to create a file matching the relation
         $file = is_string($hasOnes) ? Object::create($hasOnes) : new $fileClass();
     } else {
         $file = new $fileClass();
     }
     $this->upload->loadIntoFile($_FILES[$this->name], $file, $this->folderName);
     if ($this->upload->isError()) {
         return false;
     }
     $file = $this->upload->getFile();
     if ($this->relationAutoSetting) {
         if (!$hasOnes) {
             return false;
         }
         // save to record
         $record->{$this->name . 'ID'} = $file->ID;
     }
     return $this;
 }
 public function saveInto(DataObject $record)
 {
     if (!isset($_FILES[$this->name])) {
         return false;
     }
     if ($this->relationAutoSetting) {
         // assume that the file is connected via a has-one
         $hasOnes = $record->has_one($this->name);
         // try to create a file matching the relation
         $file = is_string($hasOnes) ? Object::create($hasOnes) : new File();
     } else {
         $file = new File();
     }
     $this->upload->loadIntoFile($_FILES[$this->name], $file, $this->folderName);
     if ($this->upload->isError()) {
         return false;
     }
     $file = $this->upload->getFile();
     if ($this->relationAutoSetting) {
         if (!$hasOnes) {
             return false;
         }
         // save to record
         $record->{$this->name . 'ID'} = $file->ID;
     }
 }
 function doUpload($data, $form)
 {
     if (isset($data['UploadedMedia']['tmp_name'])) {
         if (!empty($data['UploadedMedia']['name'])) {
             // create new single file array from file uploads array
             $file = array();
             $file['name'] = $data['UploadedMedia']['name'];
             $file['type'] = $data['UploadedMedia']['type'];
             $file['tmp_name'] = $data['UploadedMedia']['tmp_name'];
             $file['error'] = $data['UploadedMedia']['error'];
             $file['size'] = $data['UploadedMedia']['size'];
             // create & write uploaded file in DB
             try {
                 $newfile = new File();
                 $upload = new Upload();
                 // get folder from form upload field
                 $folder = $form->Fields()->fieldByName('UploadedMedia')->getFolderName();
                 $upload->loadIntoFile($file, $newfile, $folder);
                 $fileObj = $upload->getFile();
                 $EventID = Session::get('UploadMedia.PresentationID');
                 if ($EventID) {
                     $Event = VideoPresentation::get()->byID($EventID);
                 }
                 if (isset($Event)) {
                     $Event->UploadedMediaID = $fileObj->ID;
                     $Event->MediaType = 'File';
                     $Event->write();
                     Session::set('UploadMedia.Success', TRUE);
                     Session::set('UploadMedia.FileName', $fileObj->Name);
                     Session::set('UploadMedia.Type', 'File');
                     Controller::curr()->redirect($form->controller()->link() . 'Success');
                 }
             } catch (ValidationException $e) {
                 $form->sessionMessage('Extension not allowed...', 'bad');
                 return $this->controller()->redirectBack();
             }
         }
     }
 }
Ejemplo n.º 4
0
 public function saveInto(DataObjectInterface $record)
 {
     if (!isset($_FILES[$this->name])) {
         return false;
     }
     $fileClass = File::get_class_for_file_extension(File::get_file_extension($_FILES[$this->name]['name'], PATHINFO_EXTENSION));
     if ($this->relationAutoSetting) {
         // assume that the file is connected via a has-one
         $objectClass = $record->hasOne($this->name);
         if ($objectClass === 'File' || empty($objectClass)) {
             // Create object of the appropriate file class
             $file = Object::create($fileClass);
         } else {
             // try to create a file matching the relation
             $file = Object::create($objectClass);
         }
     } else {
         if ($record instanceof File) {
             $file = $record;
         } else {
             $file = Object::create($fileClass);
         }
     }
     $this->upload->loadIntoFile($_FILES[$this->name], $file, $this->getFolderName());
     if ($this->upload->isError()) {
         return false;
     }
     if ($this->relationAutoSetting) {
         if (!$objectClass) {
             return false;
         }
         $file = $this->upload->getFile();
         $record->{$this->name . 'ID'} = $file->ID;
     }
     return $this;
 }
 public function testFileVersioningWithAnExistingFile()
 {
     $upload = function ($tmpFileName) {
         // create tmp file
         $tmpFilePath = TEMP_FOLDER . '/' . $tmpFileName;
         $tmpFileContent = '';
         for ($i = 0; $i < 10000; $i++) {
             $tmpFileContent .= '0';
         }
         file_put_contents($tmpFilePath, $tmpFileContent);
         // emulates the $_FILES array
         $tmpFile = array('name' => $tmpFileName, 'type' => 'text/plaintext', 'size' => filesize($tmpFilePath), 'tmp_name' => $tmpFilePath, 'extension' => 'jpg', 'error' => UPLOAD_ERR_OK);
         $v = new UploadTest_Validator();
         // test upload into default folder
         $u = new Upload();
         $u->setReplaceFile(false);
         $u->setValidator($v);
         $u->loadIntoFile($tmpFile);
         return $u->getFile();
     };
     // test empty file version prefix
     Config::inst()->update('SilverStripe\\Filesystem\\Storage\\DefaultAssetNameGenerator', 'version_prefix', '');
     $file1 = $upload('UploadTest-IMG001.jpg');
     $this->assertEquals('UploadTest-IMG001.jpg', $file1->Name, 'File does not receive new name');
     $file2 = $upload('UploadTest-IMG001.jpg');
     $this->assertEquals('UploadTest-IMG002.jpg', $file2->Name, 'File does receive new name');
     $file3 = $upload('UploadTest-IMG002.jpg');
     $this->assertEquals('UploadTest-IMG003.jpg', $file3->Name, 'File does receive new name');
     $file4 = $upload('UploadTest-IMG3.jpg');
     $this->assertEquals('UploadTest-IMG3.jpg', $file4->Name, 'File does not receive new name');
     $file1->delete();
     $file2->delete();
     $file3->delete();
     $file4->delete();
     // test '-v' file version prefix
     Config::inst()->update('SilverStripe\\Filesystem\\Storage\\DefaultAssetNameGenerator', 'version_prefix', '-v');
     $file1 = $upload('UploadTest2-IMG001.jpg');
     $this->assertEquals('UploadTest2-IMG001.jpg', $file1->Name, 'File does not receive new name');
     $file2 = $upload('UploadTest2-IMG001.jpg');
     $this->assertEquals('UploadTest2-IMG001-v2.jpg', $file2->Name, 'File does receive new name');
     $file3 = $upload('UploadTest2-IMG001.jpg');
     $this->assertEquals('UploadTest2-IMG001-v3.jpg', $file3->Name, 'File does receive new name');
     $file4 = $upload('UploadTest2-IMG001-v3.jpg');
     $this->assertEquals('UploadTest2-IMG001-v4.jpg', $file4->Name, 'File does receive new name');
 }
Ejemplo n.º 6
0
 function testUploadFileWithNoExtensionTwiceAppendsNumber()
 {
     // create tmp file
     $tmpFileName = 'UploadTest-testUpload';
     $tmpFilePath = TEMP_FOLDER . '/' . $tmpFileName;
     $tmpFileContent = '';
     for ($i = 0; $i < 10000; $i++) {
         $tmpFileContent .= '0';
     }
     file_put_contents($tmpFilePath, $tmpFileContent);
     // emulates the $_FILES array
     $tmpFile = array('name' => $tmpFileName, 'type' => 'text/plaintext', 'size' => filesize($tmpFilePath), 'tmp_name' => $tmpFilePath, 'extension' => 'txt', 'error' => UPLOAD_ERR_OK);
     // Make sure there are none here, otherwise they get renamed incorrectly for the test.
     $this->deleteTestUploadFiles("/UploadTest-testUpload.*/");
     $v = new UploadTest_Validator();
     $v->setAllowedExtensions(array(''));
     // test upload into default folder
     $u = new Upload();
     $u->setValidator($v);
     $u->load($tmpFile);
     $file = $u->getFile();
     $this->assertEquals('UploadTest-testUpload', $file->Name, 'File is uploaded without extension');
     $u = new Upload();
     $u->setValidator($v);
     $u->load($tmpFile);
     $file2 = $u->getFile();
     $this->assertEquals('UploadTest-testUpload-2', $file2->Name, 'File receives a number attached to the end');
     $file->delete();
     $file2->delete();
 }
Ejemplo n.º 7
0
 public function testFileVersioningWithAnExistingFile()
 {
     $upload = function ($tmpFileName) {
         // create tmp file
         $tmpFilePath = TEMP_FOLDER . '/' . $tmpFileName;
         $tmpFileContent = '';
         for ($i = 0; $i < 10000; $i++) {
             $tmpFileContent .= '0';
         }
         file_put_contents($tmpFilePath, $tmpFileContent);
         // emulates the $_FILES array
         $tmpFile = array('name' => $tmpFileName, 'type' => 'text/plaintext', 'size' => filesize($tmpFilePath), 'tmp_name' => $tmpFilePath, 'extension' => 'jpg', 'error' => UPLOAD_ERR_OK);
         $v = new UploadTest_Validator();
         // test upload into default folder
         $u = new Upload();
         $u->setReplaceFile(false);
         $u->setValidator($v);
         $u->load($tmpFile);
         return $u->getFile();
     };
     $file1 = $upload('UploadTest-testUpload.jpg');
     $this->assertEquals('UploadTest-testUpload.jpg', $file1->Name, 'File does not receive new name');
     $file2 = $upload('UploadTest-testUpload.jpg');
     $this->assertEquals('UploadTest-testUpload2.jpg', $file2->Name, 'File does receive new name');
     $file3 = $upload('UploadTest-testUpload.jpg');
     $this->assertEquals('UploadTest-testUpload3.jpg', $file3->Name, 'File does receive new name');
     $file4 = $upload('UploadTest-testUpload3.jpg');
     $this->assertEquals('UploadTest-testUpload4.jpg', $file4->Name, 'File does receive new name');
     $file1->delete();
     $file2->delete();
     $file3->delete();
     $file4->delete();
 }
Ejemplo n.º 8
0
 public function imageupload()
 {
     if (!Member::currentUserID()) {
         $return = array('error' => 1, 'text' => "Cannot upload there");
         return Convert::raw2json($return);
     }
     if (isset($_FILES['NewImage']) && ($tempfile = $_FILES['NewImage'])) {
         // validate //
         $allowed = array('jpg', 'jpeg', 'gif', 'png', 'ico');
         $nameBits = explode('.', $tempfile['name']);
         $ext = end($nameBits);
         if (!in_array(strtolower($ext), $allowed)) {
             $return = array('error' => 1, 'text' => "Your image must be in jpg, gif or png format");
             return Convert::raw2json($return);
         }
         $maxsize = $_POST['MAX_FILE_SIZE'];
         if ($tempfile['size'] > $maxsize) {
             $size = number_format($maxsize / 1024 / 1024, 2) . 'MB';
             $return = array('error' => 1, 'text' => "Your image must be smaller than {$size}");
             return Convert::raw2json($return);
         }
         // upload //
         $upload = new Upload();
         $file = new Image();
         $upload->loadIntoFile($tempfile, $file);
         if ($upload->isError()) {
             return false;
         }
         $file = $upload->getFile();
         $return = array('link' => $file->Link());
         return Convert::raw2json($return);
     } else {
         // no file to upload
         return false;
     }
 }
 private function buildScreenshots(Addon $addon, PackageInterface $package, $path)
 {
     $extra = $package->getExtra();
     $screenshots = array();
     $target = self::SCREENSHOTS_DIR . '/' . $addon->Name;
     if (isset($extra['screenshots'])) {
         $screenshots = (array) $extra['screenshots'];
     } elseif (isset($extra['screenshot'])) {
         $screenshots = (array) $extra['screenshot'];
     }
     // Delete existing screenshots.
     foreach ($addon->Screenshots() as $screenshot) {
         $screenshot->delete();
     }
     $addon->Screenshots()->removeAll();
     foreach ($screenshots as $screenshot) {
         if (!is_string($screenshot)) {
             continue;
         }
         $scheme = parse_url($screenshot, PHP_URL_SCHEME);
         // Handle absolute image URLs.
         if ($scheme == 'http' || $scheme == 'https') {
             $temp = TEMP_FOLDER . '/' . md5($screenshot);
             if (!copy($screenshot, $temp)) {
                 continue;
             }
             $data = array('name' => basename($screenshot), 'size' => filesize($temp), 'tmp_name' => $temp, 'error' => 0);
         } else {
             $source = $path . '/' . ltrim($screenshot, '/');
             // Prevent directory traversal.
             if ($source != realpath($source)) {
                 continue;
             }
             if (!file_exists($source)) {
                 continue;
             }
             $data = array('name' => basename($source), 'size' => filesize($source), 'tmp_name' => $source, 'error' => 0);
         }
         $upload = new Upload();
         $upload->setValidator(new AddonBuilderScreenshotValidator());
         $upload->load($data, $target);
         if ($file = $upload->getFile()) {
             $addon->Screenshots()->add($file);
         }
     }
 }
Ejemplo n.º 10
0
 public function testDeleteResampledImagesOnUpload()
 {
     $tmpFileName = 'UploadTest-testUpload.jpg';
     $tmpFilePath = TEMP_FOLDER . '/' . $tmpFileName;
     $uploadImage = function () use($tmpFileName, $tmpFilePath) {
         copy(__DIR__ . '/gdtest/test_jpg.jpg', $tmpFilePath);
         // emulates the $_FILES array
         $tmpFile = array('name' => $tmpFileName, 'type' => 'text/plaintext', 'size' => filesize($tmpFilePath), 'tmp_name' => $tmpFilePath, 'extension' => 'jpg', 'error' => UPLOAD_ERR_OK);
         $v = new UploadTest_Validator();
         // test upload into default folder
         $u = new Upload();
         $u->setReplaceFile(true);
         $u->setValidator($v);
         $u->load($tmpFile);
         return $u->getFile();
     };
     // Image upload and generate a resampled image
     $image = $uploadImage();
     $resampled = $image->ResizedImage(123, 456);
     $resampledPath = $resampled->getFullPath();
     $this->assertTrue(file_exists($resampledPath));
     // Re-upload the image, overwriting the original
     // Resampled images should removed when their parent file is overwritten
     $image = $uploadImage();
     $this->assertFalse(file_exists($resampledPath));
     unlink($tmpFilePath);
     $image->delete();
 }
Ejemplo n.º 11
0
 /**
  *
  * @param type $key
  * @return boolean
  * @throws Exception
  */
 public static function getFile($key)
 {
     return Upload::getFile($key);
 }
Ejemplo n.º 12
0
 public function saveappletupload($request)
 {
     $location = (int) $request->postVar('Location');
     if ($location) {
         $folder = DataObject::get_by_id('Folder', $location);
         if (isset($_FILES) && isset($_FILES['screenshot'])) {
             $upload = new Upload();
             if ($upload->load($_FILES['screenshot'], substr($folder->Filename, 7))) {
                 // need to base64decode the file data
                 $data = file_get_contents($upload->getFile()->getFullPath());
                 file_put_contents($upload->getFile()->getFullPath(), base64_decode($data));
                 $upload->getFile()->ClassName = 'Image';
                 $upload->getFile()->write();
                 return '{"file": ' . Convert::raw2json($upload->getFile()->toMap()) . '}';
             }
         }
     }
 }
Ejemplo n.º 13
0
        $sRender = $pGeshi->parse_code();
        file_put_contents($sCacheItem, $sRender);
    } else {
        $sRender = file_get_contents($sCacheItem);
    }
    header("Cache-Control: public");
    header("Last-Modified: " . date("r", filemtime($sCacheItem)));
    header("Content-Length: " . filesize($sCacheItem));
    header("Content-Type: text/html");
    header("Content-Transfer-Encoding: binary");
    header("Content-MD5: " . md5_file($sCacheItem));
    header("Content-Disposition: inline; filename=" . $pFunctions->quote($pUpload->file_name));
    echo $sRender;
} else {
    header("Cache-Control: public");
    header("Last-Modified: " . date("r", filemtime($pUpload->local_path)));
    header("Content-Length: {$pUpload->file_size}");
    header("Content-Type: {$pUpload->mime_type}");
    header("Content-Transfer-Encoding: binary");
    header("Content-MD5: {$pUpload->file_hash}");
    header("Content-Disposition: inline; filename=" . $pFunctions->quote($pUpload->file_name));
    $rPointer = $pUpload->getFile();
    $sContents = "";
    while ($sContents = fread($rPointer, 1024)) {
        echo $sContents;
        flush();
    }
    fclose($rPointer);
}
$pUpload->incrementViews();
return;