We are no longer offering accounts on this server. Consider https://gitlab.freedesktop.org/ as a place to host projects.

location-ws.php 2.37 KB
Newer Older
tobyink's avatar
tobyink committed
1 2
<?php

3
/* GNU FM -- a free network service for sharing your music listening habits
tobyink's avatar
tobyink committed
4

5
   Copyright (C) 2009 Free Software Foundation, Inc
tobyink's avatar
tobyink committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU Affero General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU Affero General Public License for more details.

   You should have received a copy of the GNU Affero General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/

# This file should probably be moved out of the web root. /js/edit_profile.js will
# need updating if you do.

// The place being searched for.
$query = $_GET['q'];

if (!$query)
{
	header("Content-Type: text/plain");
	die("Must supply a query argument.\r\n");
}

// The number of results requested
$num = $_GET['n'];
if (! $num)
	$num = 10;

// When translated versions become available, should be able to calculate
// the language code from the subdomain.
$lang = 'en';

$uri =  sprintf('http://ws.geonames.org/searchJSON?q=%s&maxRows=%d&lang=%s&style=full',
	urlencode($query),
	$num,
	urlencode($lang));

# We'll try to use cURL if the extension is installed on this server.
if (function_exists('curl_init'))
{
	header("Content-Type: application/json");
	$ch = curl_init($uri);
	curl_setopt($ch, CURLOPT_HEADER, 0);
	curl_setopt($ch, CURLOPT_USERAGENT, 'libre.fm');
	curl_exec($ch);
	curl_close($ch);
}

# Otherwise, we'll fall back to direct socket calls. Ugly.
elseif (function_exists('parse_url'))
{
	$_uri = parse_url($uri);
	if (! $_uri['port'])
		$_uri['port'] = 80;
clint's avatar
clint committed
65

tobyink's avatar
tobyink committed
66 67 68 69 70
	if (! ($nh = fsockopen($_uri['host'], $_uri['port'], $errno, $errstr, 20)) )
	{
		header("Content-Type: text/plain");
		die("Could not open network connection! ($errno - $errstr)\r\n");
	}
clint's avatar
clint committed
71

tobyink's avatar
tobyink committed
72 73 74 75 76 77 78 79 80 81 82
	fwrite($nh, "GET {$_uri[path]}?{$_uri[query]} HTTP/1.0\r\n"
		. "Host: {$_uri['host']}\r\n"
		. "User-Agent: libre.fm\r\n"
		. "Connection: close\r\n\r\n"
		);
	header("Content-Type: application/json");
	while (!feof($nh))
	{
		$output .= fgets($nh, 128);
	}
	fclose($nh);
clint's avatar
clint committed
83

tobyink's avatar
tobyink committed
84 85 86 87
	// Remove HTTP header.
	echo substr(strstr($output, "\r\n\r\n"), 4);
}