Showing posts with label PHP. Show all posts
Showing posts with label PHP. Show all posts

Thursday, June 28, 2012

Cross Site Scripting PHP Proxy

I needed to access a REST web service from jQuery, but Chrome would throw an error during the ajax call due to the "origin" policy.  It's possible to setup a CORS filter with Tomcat and Apache, but that sounded like a lot of work.

Instead, if you can use PHP, just download the following PHP proxy:
https://github.com/developerforce/Force.com-JavaScript-REST-Toolkit/blob/master/proxy.php

Two changes are needed:

  1. Edit line 176 such that it reads  $url_query_param = 'url';
  2. Either fix the regexp at lines 172 and 173 which checks that the call is to sales force.com (set it to match your website) or  comment out lines 206 to 212 (potentially dangerous).
Now your $.ajax call needs to be modified so that the target url is part of the url.  Everything else is seamless.  See below.

 
 var req = $.ajax({
    type: 'GET',
    contentType: 'application/json',
    mimeType: 'application/json',
    url: 'http://proxy-server/app/proxy.php?mode=native&url=http://api-server/api/object/'+$("#objectID").val(),
    dataType: 'json',
    success: function(data, textStatus, jqXHR) {
 alert("Got data successfully");
 $('#responseData').text(JSON.stringify(data));
 },
    error: function(xhr, textStatus, error) {
 alert("Error: " + textStatus);
 } 
  });
That's it!

Monday, March 05, 2012

Sending Multipart Form Data from Java (and receiving it via PHP)

If you want to send binary data and some parameters, you'll need to execute a POST with the content type of multipart/form-data. This takes a different format than usual HTTP requests. The secret to sending multipart form data is sending the right number of line endings. Rather than explain it, here's the code.
String lineEnd = "\r\n"; 
String twoHyphens = "--";
String boundary = "*****";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1*1024*1024;
File temp_file = new File("foo.txt");
// open a URL connection
URL url = new URL(urlString);

// Open a HTTP connection to the URL
conn = (HttpURLConnection) url.openConnection();

// Allow Inputs
conn.setDoInput(true);

// Allow Outputs
conn.setDoOutput(true);

// Don't use a cached copy.
conn.setUseCaches(false);

// Use a post method.
conn.setRequestMethod("POST");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);

dos = new DataOutputStream( conn.getOutputStream() );

// Send parameter #1
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"param1\"" + lineEnd + lineEnd);
dos.writeBytes("foo1" + lineEnd);

// Send parameter #2
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"param2\"" + lineEnd + lineEnd);
dos.writeBytes("foo2" + lineEnd);

// Send a binary file
dos.writeBytes(twoHyphens + boundary + lineEnd);
dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + temp_file.getName() +"\"" + lineEnd);
dos.writeBytes(lineEnd);

// create a buffer of maximum size

bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];

// read file and write it into form...

bytesRead = fileInputStream.read(buffer, 0, bufferSize);

while (bytesRead > 0)
{
dos.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
}

// send multipart form data necesssary after file data...

dos.writeBytes(lineEnd);
dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

// close streams
fileInputStream.close();
dos.flush();
dos.close();


Ok, now how to read this on the other end? Here's how to do it via PHP:
// Make sure a binary file is attached to the POST
if(!$_FILES) {
echo "No file!"
}

$id = $_FILES['uploadedfile']['name'];
move_uploaded_file($_FILES['uploadedfile']['tmp_name'], "/permanent/location".$id);

// Uncomment next line to print out the array to a file
// file_put_contents("/permanent/location/".$id.".debug.txt", print_r($_POST, true));
$param1="";
$param2="";
if(isset($arguments['param1'])) {
$lat = $arguments['param1'];
}
if(isset($arguments['param2'])) {
$lon = $arguments['param2'];
}
echo "Uploaded " . $id . " with param1=" . $param1 . " and param2=" . $param2;

Tuesday, February 01, 2011

Admin Pages Generator

Writing pages to administer tables is a pain, even if they're basic. But no more. See the attached generator. Doesn't work for more complex stuff needing joins etc but its a good quick start.

PHP Data Access Object Generator

When coding in Java, I always used Netbean's auto generation of entity beans. In minutes you can have a couple dozen entity beans created (add the CXF generator and you get the CRUD services to access those entities also!).

With PHP, things aren't so advanced.

But, I did find a PHP script to generate DAOs for MySQL. The trouble is it comes with no documentation!

Luckily someone on devx wrote a tutorial. You do need to login, but that's what bugmenot is for.

Labels

Blog Archive

Contributors