/**
  * Construct an SS_HTTPResponse that will deliver a file to the client
  */
 static function send_file($fileData, $fileName, $mimeType = null)
 {
     if (!$mimeType) {
         $mimeType = HTTP::getMimeType($fileName);
     }
     $response = new SS_HTTPResponse($fileData);
     $response->addHeader("Content-Type", "{$mimeType}; name=\"" . addslashes($fileName) . "\"");
     $response->addHeader("Content-disposition", "attachment; filename=" . addslashes($fileName));
     $response->addHeader("Content-Length", strlen($fileData));
     $response->addHeader("Pragma", "");
     // Necessary because IE has issues sending files over SSL
     return $response;
 }
 /**
  *
  * @param String $fileRef
  *			The "FileRef" property of an object to stream the content of
  */
 public function streamObject(SharePointContentItem $object, $toFile = '', $contentType = '')
 {
     $contentUrl = $this->url . '/' . str_replace('%2F', '/', rawurlencode($object->FileRef));
     $contentType = HTTP::getMimeType($object->FileRef);
     if (!$contentType) {
         $contentType = 'application/octet-stream';
     }
     $session = curl_init($contentUrl);
     curl_setopt($session, CURLOPT_USERPWD, $this->username . ':' . $this->password);
     curl_setopt($session, CURLOPT_HTTPAUTH, CURLAUTH_NTLM);
     if (!strlen($toFile)) {
         $filename = rawurlencode($object->LinkFilename);
         header("Content-Disposition: atachment; filename={$filename}");
         header("Content-Type: {$contentType}");
         // header("Content-Length: ".filesize("$path/$filename"));
         header("Pragma: no-cache");
         header("Expires: 0");
         curl_exec($session);
     } else {
         // get the file and store it into a local item
         curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
         $response = curl_exec($session);
         $fp = fopen($toFile, 'w');
         if (!$fp) {
             throw new Exception("Could not write file to {$toFile}");
         }
         fwrite($fp, $response);
         fclose($fp);
     }
     curl_close($session);
 }
 /**
  * Stream to browser or file
  *
  * @param string $toFile 
  */
 public function streamContent($toFile = null)
 {
     $contentType = HTTP::getMimeType($this->Filename);
     // now get the file
     $contentUrl = $this->getSource()->ApiUrl . '/' . $this->Filename;
     $session = curl_init($contentUrl);
     if (!strlen($toFile)) {
         // QUICK HACK
         $n = $this->name;
         $filename = rawurlencode($n);
         header("Content-Disposition: atachment; filename={$filename}");
         header("Content-Type: {$contentType}");
         // header("Content-Length: ".filesize("$path/$filename"));
         header("Pragma: no-cache");
         header("Expires: 0");
         curl_exec($session);
     } else {
         // get the file and store it into a local item
         curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
         $response = curl_exec($session);
         $fp = fopen($toFile, 'w');
         if (!$fp) {
             throw new Exception("Could not write file to {$toFile}");
         }
         fwrite($fp, $response);
         fclose($fp);
     }
     curl_close($session);
 }
Exemple #4
0
 /**
  * Construct an SS_HTTPResponse that will deliver a file to the client
  */
 static function send_file($fileData, $fileName, $mimeType = null)
 {
     if (!$mimeType) {
         $mimeType = HTTP::getMimeType($fileName);
     }
     $response = new SS_HTTPResponse($fileData);
     $response->addHeader("Content-Type", "{$mimeType}; name=\"" . addslashes($fileName) . "\"");
     $response->addHeader("Content-disposition", "attachment; filename=" . addslashes($fileName));
     $response->addHeader("Content-Length", strlen($fileData));
     $response->addHeader("Pragma", "");
     // Necessary because IE has issues sending files over SSL
     if (strstr($_SERVER["HTTP_USER_AGENT"], "MSIE") == true) {
         $response->addHeader('Cache-Control', 'max-age=3, must-revalidate');
         // Workaround for IE6 and 7
     }
     return $response;
 }
 /**
  * Process the form that is submitted through the site
  * 
  * @param Array Data
  * @param Form Form 
  * @return Redirection
  */
 function process($data, $form)
 {
     Session::set("FormInfo.{$form->FormName()}.data", $data);
     Session::clear("FormInfo.{$form->FormName()}.errors");
     foreach ($this->Fields() as $field) {
         $messages[$field->Name] = $field->getErrorMessage()->HTML();
         if ($field->Required) {
             if (!isset($data[$field->Name]) || !$data[$field->Name] || !$field->getFormField()->validate($this->validator)) {
                 $form->addErrorMessage($field->Name, $field->getErrorMessage()->HTML(), 'bad');
             }
         }
     }
     if (Session::get("FormInfo.{$form->FormName()}.errors")) {
         Director::redirectBack();
         return;
     }
     $submittedForm = Object::create('SubmittedForm');
     $submittedForm->SubmittedByID = ($id = Member::currentUserID()) ? $id : 0;
     $submittedForm->ParentID = $this->ID;
     // if saving is not disabled save now to generate the ID
     if (!$this->DisableSaveSubmissions) {
         $submittedForm->write();
     }
     $values = array();
     $attachments = array();
     $submittedFields = new DataObjectSet();
     foreach ($this->Fields() as $field) {
         if (!$field->showInReports()) {
             continue;
         }
         $submittedField = $field->getSubmittedFormField();
         $submittedField->ParentID = $submittedForm->ID;
         $submittedField->Name = $field->Name;
         $submittedField->Title = $field->getField('Title');
         // save the value from the data
         if ($field->hasMethod('getValueFromData')) {
             $submittedField->Value = $field->getValueFromData($data);
         } else {
             if (isset($data[$field->Name])) {
                 $submittedField->Value = $data[$field->Name];
             }
         }
         if (!empty($data[$field->Name])) {
             if (in_array("EditableFileField", $field->getClassAncestry())) {
                 if (isset($_FILES[$field->Name])) {
                     // create the file from post data
                     $upload = new Upload();
                     $file = new File();
                     $file->ShowInSearch = 0;
                     $upload->loadIntoFile($_FILES[$field->Name], $file);
                     // write file to form field
                     $submittedField->UploadedFileID = $file->ID;
                     // attach a file only if lower than 1MB
                     if ($file->getAbsoluteSize() < 1024 * 1024 * 1) {
                         $attachments[] = $file;
                     }
                 }
             }
         }
         if (!$this->DisableSaveSubmissions) {
             $submittedField->write();
         }
         $submittedFields->push($submittedField);
     }
     $emailData = array("Sender" => Member::currentUser(), "Fields" => $submittedFields);
     // email users on submit.
     if ($this->EmailRecipients()) {
         $email = new UserDefinedForm_SubmittedFormEmail($submittedFields);
         $email->populateTemplate($emailData);
         if ($attachments) {
             foreach ($attachments as $file) {
                 if ($file->ID != 0) {
                     $email->attachFile($file->Filename, $file->Filename, HTTP::getMimeType($file->Filename));
                 }
             }
         }
         foreach ($this->EmailRecipients() as $recipient) {
             $email->populateTemplate($recipient);
             $email->populateTemplate($emailData);
             $email->setFrom($recipient->EmailFrom);
             $email->setBody($recipient->EmailBody);
             $email->setSubject($recipient->EmailSubject);
             $email->setTo($recipient->EmailAddress);
             // check to see if they are a dynamic sender. eg based on a email field a user selected
             if ($recipient->SendEmailFromField()) {
                 $submittedFormField = $submittedFields->find('Name', $recipient->SendEmailFromField()->Name);
                 if ($submittedFormField) {
                     $email->setFrom($submittedFormField->Value);
                 }
             }
             // check to see if they are a dynamic reciever eg based on a dropdown field a user selected
             if ($recipient->SendEmailToField()) {
                 $submittedFormField = $submittedFields->find('Name', $recipient->SendEmailToField()->Name);
                 if ($submittedFormField) {
                     $email->setTo($submittedFormField->Value);
                 }
             }
             if ($recipient->SendPlain) {
                 $body = strip_tags($recipient->EmailBody) . "\n ";
                 if (isset($emailData['Fields']) && !$recipient->HideFormData) {
                     foreach ($emailData['Fields'] as $Field) {
                         $body .= $Field->Title . ' - ' . $Field->Value . ' \\n';
                     }
                 }
                 $email->setBody($body);
                 $email->sendPlain();
             } else {
                 $email->send();
             }
         }
     }
     Session::clear("FormInfo.{$form->FormName()}.errors");
     Session::clear("FormInfo.{$form->FormName()}.data");
     $referrer = isset($data['Referrer']) ? '?referrer=' . urlencode($data['Referrer']) : "";
     return Director::redirect($this->Link() . 'finished' . $referrer);
 }
	/**
	 * Construct an HTTPResponse that will deliver a file to the client
	 */
	static function send_file($fileData, $fileName, $mimeType = null) {
		if(!$mimeType) $mimeType = HTTP::getMimeType($fileName);

		$response = new HTTPResponse($fileData);
		$response->addHeader("Content-Type", "$mimeType; name=\"" . addslashes($fileName) . "\"");
		$response->addHeader("Content-disposition", "attachment; filename=" . addslashes($fileName));
		$response->addHeader("Content-Length", strlen($fileData));
		
		return $response;
	}
 /**
  * File found response
  *
  * @param $file File to send
  * @param $alternate_path string If supplied, return the file from this path instead, for
  * example, resampled images.
  */
 function fileFound(File $file, $alternate_path = null)
 {
     // File properties
     $file_name = $file->Name;
     $file_path = Director::getAbsFile($alternate_path ? $alternate_path : $file->FullPath);
     $file_size = filesize($file_path);
     // Testing mode - return an HTTPResponse
     if (self::$use_ss_sendfile) {
         if (ClassInfo::exists('SS_HTTPRequest')) {
             return SS_HTTPRequest::send_file(file_get_contents($file_path), $file_name);
         } else {
             return HTTPRequest::send_file(file_get_contents($file_path), $file_name);
         }
     }
     // Normal operation:
     $mimeType = HTTP::getMimeType($file_name);
     header("Content-Type: {$mimeType}; name=\"" . addslashes($file_name) . "\"");
     header("Content-Disposition: attachment; filename=" . addslashes($file_name));
     header("Cache-Control: max-age=1, private");
     header("Content-Length: {$file_size}");
     header("Pragma: ");
     if (self::$use_x_sendfile) {
         session_write_close();
         header('X-Sendfile: ' . $file_path);
         exit;
     } elseif ($filePointer = @fopen($file_path, 'rb')) {
         session_write_close();
         $this->flush();
         // Push the file while not EOF and connection exists
         while (!feof($filePointer) && !connection_aborted()) {
             print fread($filePointer, 1024 * self::$chunck_size_kb);
             $this->flush();
         }
         fclose($filePointer);
         exit;
     } else {
         // Edge case - either not found anymore or can't read
         return $this->fileNotFound();
     }
 }