Skip to content

Instantly share code, notes, and snippets.

@pastcompute
Created March 16, 2016 23:33
Show Gist options
  • Save pastcompute/d14e7e691cef69f05822 to your computer and use it in GitHub Desktop.
Save pastcompute/d14e7e691cef69f05822 to your computer and use it in GitHub Desktop.
PHP google apps redirector
function doGet(e) {
// var params = JSON.stringify(e);
// Add a row to the spreadsheet here
var ss = SpreadsheetApp.openById(PropertiesService.getUserProperties().getProperty("ssId"));
var sheet = ss.getSheetByName("Beacons");
if (sheet == null) {
sheet = ss.insertSheet("Beacons");
}
// qs = e.queryString
// Decode entities...
qe = JSON.stringify(e.parameters)
ip = e.parameter['ip']
q = e.parameter['q']
sheet.appendRow([new Date(), ip, q, qe]);
//sheet.appendRow([new Date(), ip, q]);
return ContentService.createTextOutput('OK');
}
function onOpen(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
PropertiesService.getUserProperties().setProperty("ssId", ss.getId());
}
<?php
function clean($elem)
{
if(!is_array($elem))
$elem = htmlentities($elem,ENT_QUOTES,"UTF-8");
else
foreach ($elem as $key => $value)
$elem[$key] = clean($value);
return $elem;
}
$ip = $_SERVER['REMOTE_ADDR'];
$clean = clean($_GET); $gapp = "https://script.google.com/macros/s/blah/exec";
$q = http_build_query($clean, null, '&', PHP_QUERY_RFC3986); // ensure spaces as percent in q data
$data = array('ip'=>$ip,
'q'=>$q,
'ua'=>clean($_SERVER['HTTP_USER_AGENT']),
'uh'=>clean($_SERVER['HTTP_HOST']),
'uf'=>clean($_SERVER['HTTP_REFERER']),
'uu'=>clean($_SERVER['REMOTE_USER']),
'ur'=>clean($_SERVER['REQUEST_URI'])
);
$q2 = http_build_query($data);
//echo "GET: <br/><span style='font-color: blue;'>$_GET</span></br>";
//echo "CLEAN: <br/><span style='font-color: blue;'>$clean</span></br>";
//echo "Result1: <br/><span style='font-color: blue;'>$q</span></br>";
//echo "Invert1: <br/><span style='font-color: blue;'>".urldecode($q)."</span></br>"; // html_entity_decode ?
//echo "Result2: <br/><span style='font-color: blue;'>$q2</span></br>";
echo file_get_contents($gapp . "?" . $q2);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment