Blog Posts » General » [PHP] 4shared Search Engine
[PHP] 4shared Search Engine
Demo: http://test.anggit.com/search.php
Unfinished project, working!
[search.php]
[Textarea][RAW][Download]
- <h2>Search Files</h2>
- <form id="searchform" action="search.php" method="get">
- <div>
- <input type="text" size="20" name="q" value="" />
- <input id="searchsubmit" type="submit" value="Submit" />
- </div>
- </form>
- <?php
- # (c) 2013 http://anggit.com
- function get_contents($url) {
- $url = stripos($url, 'http://') === 0 ? $url : 'http://'.$url;
- $useragent = 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.28) Gecko/20120306 Firefox/3.6.28';
- $timeout = 10;
- switch(true) {
- case function_exists('curl_init'):
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
- $contents = curl_exec($ch) or die(curl_error($ch));
- return $contents;
- curl_close($ch);
- break;
- case function_exists('fsockopen'):
- $host = parse_url($url, PHP_URL_HOST);
- $port = parse_url($url, PHP_URL_PORT) ? parse_url($url, PHP_URL_PORT) : 80;
- $path = parse_url($url, PHP_URL_PATH) ? parse_url($url, PHP_URL_PATH) : '/';
- $fp = @fsockopen($host, $port, $errno, $errstr, $timeout) or die($errstr);
- stream_set_timeout($fp, $timeout);
- $header = "GET $path HTTP/1.0\r\n";
- $header .= "Host: $host\r\n";
- $header .= "User-Agent: $useragent\r\n";
- $header .= "Connection: Close\r\n\r\n";
- fwrite($fp, $header);
- while(!feof($fp)) {
- @$contents .= fgets($fp, 1024);
- }
- fclose($fp);
- $contents = substr($contents, strpos($contents, '<'));
- return $contents;
- break;
- case function_exists('file_get_contents'):
- $opts = array('http' => array('header' => 'User-Agent: '.$useragent.''));
- $context = stream_context_create($opts);
- $contents = @file_get_contents($url, false, $context) or die('Error');
- return $contents;
- break;
- default:
- return 'Please enable curl, fsockopen or file_get_contents!';
- break;
- }
- }
- $max = 10; // last search max
- $file = 'last.txt'; // last search file
- $data = file($file);
- $count = count($data);
- //* Begin Display Last Search *//
- echo '<div><p><b>Last Search:</b><br />';
- $titles = array_reverse($data);
- foreach($titles as $title) {
- $title = rtrim($title);
- echo '<a href="search.php?q='.urlencode($title).'">'.htmlspecialchars($title).'</a> / ';
- }
- echo '</p></div>';
- //* End Display Last Search *//
- $q = @urlencode($_GET['q']);
- $dl = @$_POST['dl'];
- if ($q) {
- //* Begin Write Last Search *//
- $q1 = $_GET['q']."\r\n";
- if (in_array($q1, $data)) {
- unset($data[array_search($q1, $data)]);
- $write = implode($data).$q1;
- $fh = fopen($file, 'w');
- fwrite($fh, $write);
- fclose($fh);
- }
- else {
- if ($count >= $max) array_shift($data);
- array_push($data, $q1);
- $write = implode($data);
- $fh = fopen($file, 'w');
- fwrite($fh, $write);
- fclose($fh);
- }
- if ($count > $max) {
- $fh = fopen($file, 'w');
- $write = implode(array_slice($data, $count-$max)).$q1;
- fwrite($fh, $write);
- fclose($fh);
- }
- //* End Write Last Search *//
- $p = @intval($_GET['p']);
- $start = 10 * $p;
- $contents = get_contents('http://search.4shared.com/network/searchXml.jsp?q='.$q.'&start='.$start);
- $tf = explode('<total-files>', $contents);
- $tf = strtok($tf[1], '<');
- $pt = explode('<pages-total>', $contents);
- $tp = strtok($pt[1], '<');
- $name = explode('<name>', $contents);
- $size = explode('<size>', $contents);
- $url = explode('<url>', $contents);
- $count = count($name) - 1;
- echo '<div><b>Files Found:</b> '.$tf.' ('.($start+1).'-'.($start+$count).')</div><ul>';
- if ($count) {
- for($i=1; $i<=$count; $i++) {
- $file = strtok($name[$i], '<');
- $url_encoded = base64_encode(gzdeflate(trim(strtok($url[$i], '<')), 9));
- echo '<li><a href="search.php?q='.urlencode(htmlspecialchars_decode($file)).'">'.$file.'</a> ('.strtok($size[$i], '<').')';
- echo (pathinfo(strtolower($file), PATHINFO_EXTENSION) == 'mp3') ? '<ul><li><form action="/search.php" method="post"><div><input type="hidden" name="dl" type="text" value="'.$url_encoded.'" /><input type="submit" value="Download" style="background-color: #FFF; border: 0; padding: 0; color: blue;" /></div></form></li></ul></li>' : '<ul><li>Download</li></ul></li>';
- }
- }
- else {
- echo '<li>No file(s) match for "<b>'.htmlspecialchars($_GET['q']).'</b>"</li>';
- }
- echo '</ul><div id="navigation">';
- //* Begin Pagination *//
- $lpp = 5; // link pagination to show
- $lps = floor($p/$lpp) * $lpp; // link pagination start
- $lpf = $lps + $lpp; // link pagination finish
- if ($lps > 0) echo '<a href="search.php?q='.$q.'&p='.($lps-1).'">[<]</a> ';
- for($lps; $lps<$lpf; $lps++) {
- if ($lps == $p) echo ' ['.$p.'] ';
- elseif ($lps < 0) echo '';
- elseif ($lps > $tp) echo '';
- else echo '<a href="search.php?q='.$q.'&p='.$lps.'">'.$lps.'</a> ';
- }
- if ($lpf < $tp) echo '<a href="search.php?q='.$q.'&p='.$lpf.'">[>]</a> ';
- //* End Pagination *//
- echo '</div><!-- navigation -->';
- }
- elseif ($dl) {
- $url = gzinflate(base64_decode(($dl)));
- $html = get_contents($url);
- libxml_use_internal_errors(true);
- $doc = new DomDocument();
- $doc->loadHTML($html);
- $xpath = new DOMXPath($doc);
- $metas = $xpath->query('//meta[@property="og:url"]');
- foreach($metas as $meta) $property = $meta->getAttribute('content');
- echo '<div><a href="'.$property.'">Download now!</a></div>';
- }
- ?>
- <div id="footer">
- <p>© 2013 <a href="http://anggit.com">anggit.com</a><p>
- </div><!-- footer -->