How to open a file dialogue box from Zend AMF + AS3 - php

I want to send a request to my Zend AMF to Open a Dialogue Box for a File Download.
The process:
Send a request to the Zend Server from my flash App, process the MYSQL results with PHP, then send the result to the browser as a file download (.csv) AND a result true or false to the app
I have the code working just fine outside of the Zend Environment, but of course, when i go to echo/print the file - it sends back to flash with nothing happening.
Is there any way around this?
$file = "";
$outtype = "Content-type: application/octet-stream";
header($outtype);
$outtype = 'Content-disposition: attachment; filename="file.csv"';
header($outtype);
$stmt->bind_result($id, $username, $email, $location);
while($stmt -> fetch()){
$file .= '"'.addslashes($col1).'"';
$file .= ',"'.addslashes($col2).'"';
$file .= ',"'.addslashes($col3).'"';
$file .= "\n";
}
print $file;
exit;
Or, do i have to just request this outside of the zend request...?
So far i have changed the headers to make use of the zend headers:
$this->setHeader('Content-Type', 'application/octet-stream');
$this->setHeader('Content-disposition:', 'attachment');
$this->setHeader('filename:', 'file.csv');
But am unsure how to the attach $file to 'file.csv' and then return a result of true to Flash AND download the file.. (zend noob here i am afraid..)

Are you using Zend MVC? If so make sure you are not rendering a layout....
I am not too happy with the exit; statement... since zend uses an mvc framework, it builds the response and renders it. Causing the script to exit might mean the content does not get rendered properly.
Also, make sure you use the correct zend functions to build the headers, it is very possible that zend is adding conflicting headers to the response. See http://framework.zend.com/manual/en/zend.controller.response.html

The process could be :
-1 Send a request to Zend Framework
-2 Query Mysql, process the result & write the file to the server
-3 Zip the file
-4 return the file url to Flash
-5 inform the user that the file is ready for download
-6 a user click calls navigateToURL from Flash, using the returned url
Since it's a zip file url, the browser should open a dialog box.
According to this excerpt from the docs, it seems that your options are fairly limited, as it's not only a Flash issue...
In Flash Player 10 and later running in a browser, using this method
programmatically to open a pop-up window may not be successful.
Various browsers (and browser configurations) may block pop-up windows
at any time; it is not possible to guarantee any pop-up window will appear.
However, for the best chance of success, use this method to open a
pop-up window only in code that executes as a direct result
of a user action (for example, in an event handler for a
mouse click or key-press event.)

Related

I enabled a webhook for a 3rd party service and created a custom php script to see whats happening when this hook fires, but I see nothing happening

I have a subscription to a 3rd party service called MARCUS and I just enabled a webhook whenever the service updates. I created a php script and pointed this webhook to it, but I don't see anything happening on my php script. I currently have the php file email me, but the emails are blank.
I tried to receive it via a $_POST and have it output, but I only get array printed out. Is there a php example of how to receive a webhook and print something onto the screen?
The reason why you won't see any webhook data, is because you're using $_POST incorrectly. $_POST is mainly used for web forms and file uploads. What you should be using is php://input as that takes in RAW post data whenever a post is made to the php file. And you won't see anything because the webhook fires and its not firing to your browser. The only way to see something is to write it to a file whenever the page is called so you can see it in action.
Here is a simple PHP example to see the output:
<?php
//Create a botlog.txt file so we can log what's happening whenever this page loads.
$fWrite = fopen("botlog.txt","a");
//Write to the file, a test message even if we receive an EMPTY POST. So we can debug.
$hello = fwrite($fWrite, "\n\n\n HELLO MARCUS! --- Timestamp: ". time() ."\n\n");
//Write all the web headers that get sent when the page loads. Everytime a page loads,
// there are http headers being sent from the client to the server and vice versa.
//This is so we can debug what is actually happening.
$headers = fwrite($fWrite, print_r(getallheaders(), true));
//Instead of using the $_POST variable for webhooks, we use php://input.
// php://input allows to read raw POST data. $_POST is a superglobal variable, mainly
// used for wrap data with headers application/x-www-form-urlencoded
// (when you submit forms) or multipart/form-data (file uploads)
$decoded = json_decode(file_get_contents('php://input'));
//Write the php://input to the botlog file. Have another terminal open and "tail -f botlog.txt" to see the data show up live as you reload the page or when a webhook fires.
$wrote = fwrite($fWrite, print_r($decoded, true));
//close writing the file.
fclose($fWrite);
?>

Laravel - Force download with response()->download($fileLocation) not working

I'm creating a downloads functionality with Laravel. When the user clicks on the 'download' button, I initiate an ajax call to the controller which looks like this -
public function download(Resource $resource) {
// Force download of the file
$file_to_download = 'https://data.domain.com/downloads/' . $resource->file_name;
$temp_file_location = public_path('/tmp_files/' . $resource->file_name);
copy($file_to_download, $temp_file_location);
return response()->download($temp_file_location)->deleteFileAfterSend(true);
}
Chrome's inspector shows that the response gets populated with the contents of the file, but it won't trigger the actual download.
I've been trying to find an answer, but have had no success so far. Would really appreciate your help.
Thank you for your time.
You cannot download a file from an Ajax request because Javascript is unable to save files to your filesystem out of security concerns.
There are a few good packages out there like Jquery File Download
Or you can use a traditional GET request.

BIRT 4.5 - Disable saving connectstring inside report.rptdesign, PHP to assign connectstring

I am using Birt 4.5 and PHP/MYSQL.
I am able to run birt reports with php. I have enabled tomcat and copied 'birt-runtime-4_5_0/WebViewerExample' to tomcat/webapps and renamed it to birt.
So I can run birt viewer with php;
<?php
$fname = "report/test.rptdesign&__showtitle=false";
$dest = "http://localhost:8081/birt/frameset?__report=";
$dest .= $fname;
header("Location: $dest" );
?>
Above code is working fine. But report connectstring already saved in test.rptdesign file.
I want to remove DB login credentials from test.rptdesign file and assign it while report open with PHP.
I have tried with report parameters. But all the parameters will display on browser address-bar.
Is there any secure way to do this? This is very important when we need to change the database location. It is very hard to change the data source of each and every .rptdesign file.
Thank You,
Supun
I don't believe using report parameters to handle a database connection is the right way. In addition to the address-bar problem you mentionned, it will cause unexpected issues: for example you won't be able to use this database to feed the dataset of another report parameter.
With Tomcat the best approach is to externalize the database connection in a connection pool: easy, robust, and reports might run significantly faster.
Alternatively the datasource can be externalized in a BIRT library (.rptlibrary) and shared across all report-designs: thus only the library needs to be updated when the database location is changing.
I agree with Dominique that sending the database parameters via the query is most likely an inappropriate solution - and you've not given any explanation of whether this is a requirement of the system.
But it is quite trivial to proxy the request via PHP and decorate the URL with the required parameters, something like...
<?php
$_GET['__showtitle']=$_GET['__showtitle'] ? $_GET['__showtitle'] : 'false';
$_GET['__report']=$fname; // NB this should be NULL in your code!
$_GET['dbuser']='a_db_user';
$_GET['passwd']='s3cr3t';
$qry=http_build_query($_GET);
$url="http://localhost:8081/birt/frameset?" . $qry;
// if its simply returning HTML, then just....
$fin=fopen($url, 'r');
while ($l=fgets($fin)) {
print $l;
}
exit;
If the returned content contains relative links the you'll need to rewrite the output stream. If the content type is unusual or you want to project other headers (e.g. for caching) to the browser, then you'll need to use Curl, capture the headers and relay them.

Force download of file from another server in MVC using File Result

I've got a problem. I have two completely separate websites, one written in .Net using MVC2, the other written in PHP.
I have links in my MVC site to media files in my PHP site, and I'm trying to find a way to force a download when the user clicks these links.
How could I do this in MVC? I understand how I can return a FileResult like this:
public FileResult Download(string path)
{
var contentType = "audio/mp3";
FilePathResult result = new FilePathResult(Server.MapPath("~/Content/"
+ path), contentType);
result.FileDownloadName = path;
return result;
}
But the Server.MapPath requires that the files be hosted on the MVC server.
Addendum
I also tried doing this on the .php side of the house, using a download.php?file=filepath and setting the content headers that way. However, I kept getting "Cannot modify header information - headers already sent" errors. I created a separate question on SO asking why this is happening.

Can't send content-type: text/xml header from PHP at the same time getting the data from MYSQL

i hope you can cast some light on my problem. I need to do an AJAX / PHP / MYSQL application to display posts and stuff on the page i'm writing.
I only discovered how to do some simple stuff in PHP after taking some mushrooms but that was years ago and now i don't have mushrooms and i'm just stuck!
So here's the problem:
i think i need to send a proper "xml" file through php so the ajax part can take it but: when i try to put the header on top of the php it displays this error:
" Extra content at the end of the document "
When i looked at some tutorials people were using the "header" fearlesly to do such stuff as i want to do and no comments suggested that it didn't work. so why it doesn't work on my local server?
I'm running:
WAMP
Apache 2.2.11
PHP 5.3.0
It also doesn't work on a remote server (PHP 5.3.0) :/
I read all the stuff i could find till 5am and decided to ask you for help for the first time :)
Thank you!
header('content-type: application/xhtml+xml; charset=utf-8');
require_once("allyouneed.php");
require_once("bazingablob.php");
$category=$_GET["category"];
$post_tags=$_GET["post_tags"];
$language=$_GET["language"];
$author=$_GET["author"];
$posts_per_page=$_GET["posts_per_page"];
$current_page=$_GET["current_page"];
$order=$_GET["order"];
$hard_limit=$_GET["hard_limit"];
$show_hidden=$_GET["show_hidden"];*/
$wypluj="";
$wypluj="<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>";
$bazinga_blob = new bazingablob;
if (!$bazinga_blob->connect_to_database())
{
$wypluj.="<IsOK>0</IsOK>";
echo $wypluj;
exit;
}
else
{
$wypluj.="<IsOK>jedziem</IsOK>";
}
$bb_result=$bazinga_blob->get_all_posts($category,$post_tags,$language,$author,$posts_per_page,$current_page,$order,$hard_limit,$show_hidden);
if ($bb_result) //udalo sie cos znalezc w bazie wedlug kryteriow
{
$wypluj.="<Pagination>";
$wypluj.="<CurrentPage>";
$wypluj.=$bazinga_blob->posts_pagination["current_page"];
$wypluj.="</CurrentPage>";
$wypluj.="<LastPage>";
$wypluj.=$bazinga_blob->posts_pagination["last_page"];
$wypluj.="</LastPage>";
$wypluj.="<PostsCount>";
$wypluj.=$bazinga_blob->posts_pagination["posts_count"];
$wypluj.="</PostsCount>";
$wypluj.="</Pagination>";
$wypluj.="<Posts>";
foreach ($bb_result as $item)
{
$wypluj.="<Post>";
$wypluj.="<PostId>".$item->post_id."</PostId>";
$wypluj.="<PostAuthor>".$item->post_author."</PostAuthor>";
$wypluj.="<PostLangId>".$item->post_langid."</PostLangId>";
$wypluj.="<PostSlug>".$item->post_slug."</PostSlug>";
$wypluj.="<PostTitle>".$item->post_title."</PostTitle>";
$wypluj.="<PostGreetingPicture>".$item->post_greeting_picture."</PostGreetingPicture>";
$wypluj.="<PostGreetingVideo>".$item->post_greeting_video."</PostGreetingVideo>";
$wypluj.="<PostGreetingSound>".$item->post_greeting_sound."</PostGreetingSound>";
$wypluj.="<PostShort>".$item->post_short."</PostShort>";
$wypluj.="<PostBody>".$item->post_body."</PostBody>";
$wypluj.="<PostDate>".$item->post_date."</PostDate>";
$wypluj.="<PostPublished>".$item->post_published."</PostPublished>";
$wypluj.="<PostSticky>".$item->post_sticky."</PostSticky>";
$wypluj.="<PostComments>".$item->post_comments."</PostComments>";
$wypluj.="<PostProtected>".$item->post_protected."</PostProtected>";
$wypluj.="</Post>";
}
$wypluj.="</Posts>";
}
echo $wypluj;
The error comes from your browser and indicates that your XML is malformed.
Setting the application/xhtml+xml header tells the browser to process the document as serious XML. XML needs to be "well-formed", i.e. it must not contain any syntax errors. Apparently you do have a syntax error on line 1 at column 73, which makes the browser abort the attempt to process the document.
For this reason it's a pain to hand-code XML, you should really look into a library that takes care of the well-formedness for you, like PHP's own XMLWriter.
Have you validated your XML?
http://friendsofed.infopop.net/4/OpenTopic?a=tpc&s=989094322&f=5283032876&m=4521066061
I'm honestly not sure what you're trying to do with the header, it's not any Ajax method I've ever been taught. The header method you're doing looks just a few lines short of outputting the XML to a download prompt.
Here's my favorite way to do AJAX. Simple, understandable, and quick.
Include Jquery.
Setup your data--whether by form with a Serialize (gets form data into a Javascript Variable) or by just setting some variables as it seems you're doing above.
Send via Jquery Ajax to a separate processing page. The page will receive the data you setup as a $_REQUEST variable, with the method depending on whether you defined it as a POST or not (defaults to a GET)
The processing page --does-- stuff with the REQUEST data and may or may not respond back to the page. This is where you can do stuff like update divs, alert that it worked, etc.
Here's a great tutorial. Focus on the code under "Hello Ajax, Meet Jquery"
If you get yourself any more of those mushrooms, a PHP familiar way to do AJAX is with XAJAX. It allows you to do asynchronous calls to PHP functions. Be aware, though, that the forums are not the most english-friendly and documentation is a bit cryptic.

Categories