Potřeboval jsem nějak upravit vzhled formuláře od Googlu. Z předpřipravených motivů jsem si prostě nevybral, tak jsem začal bádat, jak do formuláře podstrčit vlastní CSS až jsem narazil na tento php skript a stránku o něm.
Funguje jako jakýsi proxy skript mezi vaší stránkou a Googlem. Díky tomu lze do kódu formuláře přidat potřebné CSS.
Skript v původní podobě mi ale do formuláře předával pouze první hodnotu. Další už byla vyhodnocena jako nezadaná, takže níže uvádím skript i s mou opravou.
<?php
// the URL to your Google Forms document that you got from Google.
//$surveyUrl = "odkaz na vas formular google";
$style = "<style type='text/css'>kod vaseho stylu</style>";
// replacements of text strings with some other text
$replacements = array(
"<link href='/static/spreadsheets" => "<link href='https://spreadsheets.google.com/static/spreadsheets",
"</head>" => $style."</head>"
);
// check fopen is allowed
if (ini_get('allow_url_fopen') != '1') {
die('no fopen support');
}
// load content
$content = "";
$proxiedUrl = $_POST["proxiedUrl"];
if( $proxiedUrl == "" ) {
$surveyUrl = str_replace("https", "http", $surveyUrl);
$content = file_get_contents($surveyUrl);
if ($content === false) {
die("Failed to load content of $surveyUrl");
}
} else {
unset($_POST["proxiedUrl"]);
$proxiedUrl = str_replace("https", "http", $proxiedUrl);
$response = httpRequest($proxiedUrl, $_POST, "POST");
$content = $response["content"];
}
// change form target and method to our proxy
preg_match('/action=\\"(\S*)\\"/', $content, $matches);
$googleUrl = $matches[1];
$content = str_replace($googleUrl, /* vas odkaz na skript */, $content);
$proxiedUrlTag = '<input type="hidden" name="proxiedUrl" value="' . $googleUrl . '"/></form>';
$content = str_replace("</form>", $proxiedUrlTag, $content);
preg_match('/<a href=\\"(\S*)\\"/', $content, $matches);
$googleUrl = $matches[1];
$content = str_replace($googleUrl, /* vas odkaz na skript */ , $content);
// other replacements
foreach ($replacements as $k => $v) {
$content = str_replace($k, $v, $content);
}
// print
echo $content;
function httpRequest($url, $data, $method = "GET", $referer='') {
/* Tento kod zde byl puvodne */
//$data = http_build_query($data);
// hack to get the underscores back to dots - may break the values
//$data = str_replace("_", ".", $data);
/* Nahradil jsem jej timto, aby doslo k predani vicero hodnot formulari */
foreach ($data as $key => $value)
$post_items[] = urlencode($key)."=".urlencode($value);
$data = implode ('&', $post_items);
$data = str_replace("_", ".", $data);
/* Jeste jedna oprava - u otazek, kde je vice voleb a jedna uzivatelska */
$data = str_replace("other.option.", "other_option_", $data);
$url = parse_url($url);
$host = $url['host'];
$path = $url['path'];
$query = $url['query'];
$method = strtoupper($method);
$fp = fsockopen($host, 80, $errno, $errstr, 60);
if ($fp){
fputs($fp, "$method http://$host$path?$query HTTP/1.1\r\n");
fputs($fp, "Host: $host\r\n");
fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n");
if ($referer != '') fputs($fp, "Referer: $referer\r\n");
if ($method == 'POST') fputs($fp, "Content-length: ". strlen($data) ."\r\n");
fputs($fp, "Connection: close\r\n\r\n");
if ($method == 'POST') fputs($fp, $data);
$result = '';
while(!feof($fp)) {
// receive the results of the request
$result .= fgets($fp, 128);
}
} else {
die("$errstr ($errno)");
}
fclose($fp);
$result = explode("\r\n\r\n", $result, 2);
$header = isset($result[0]) ? $result[0] : '';
$content = isset($result[1]) ? $result[1] : '';
return array(
'status' => 'ok',
'header' => $header,
'content' => $content
);
}
?>