<?php

/**
 * Perform an HTTP request.
 *
 * This is a flexible and powerful HTTP client implementation. Correctly handles
 * GET, POST, PUT or any other HTTP requests. Handles redirects.
 *
 * @param $url
 *   A string containing a fully qualified URI.
 * @param $headers
 *   An array containing an HTTP header => value pair.
 * @param $method
 *   A string defining the HTTP request to use.
 * @param $data
 *   A string containing data to include in the request.
 * @param $retry
 *   An integer representing how many times to retry the request in case of a
 *   redirect.
 * @return
 *   An object containing the HTTP request headers, response code, headers,
 *   data, and redirect status.
 */
function drupal_http_request($url$headers = array(), $method 'GET'$data NULL$retry 3) {
  
$result = new StdClass();
  
$result->location $url;

  
// Parse the URL, and make sure we can handle the schema.
  
$uri parse_url($url);
  switch (
$uri['scheme']) {
    case 
'http':
      
$port = isset($uri['port']) ? $uri['port'] : 80;
      
$host $uri['host'] . ($port != 80 ':'$port '');
      
$fp = @fsockopen($uri['host'], $port$errno$errstr15);
      break;
    case 
'https':
      
// Note: Only works for PHP 4.3 compiled with OpenSSL.
      
$port = isset($uri['port']) ? $uri['port'] : 443;
      
$host $uri['host'] . ($port != 443 ':'$port '');
      
$fp = @fsockopen('ssl://'$uri['host'], $port$errno$errstr20);
      break;
    default:
      
$result->error 'invalid schema '$uri['scheme'];
      return 
$result;
  }

  
// Make sure the socket opened properly.
  
if (!$fp) {
    
$result->error trim($errno .' '$errstr);
    return 
$result;
  }

  
// Construct the path to act on.
  
$path = isset($uri['path']) ? $uri['path'] : '/';
  if (isset(
$uri['query'])) {
    
$path .= '?'$uri['query'];
  }

  
// Create HTTP request.
  
$defaults = array(
    
// RFC 2616: "non-standard ports MUST, default ports MAY be included".
    // We don't add the port to prevent from breaking rewrite rules checking
    // the host that do not take into account the port number.
    
'Host' => "Host: $host",
    
'User-Agent' => 'User-Agent: termzizzle',
    
'Content-Length' => 'Content-Length: 'strlen($data)
  );

  foreach (
$headers as $header => $value) {
    
$defaults[$header] = $header .': '$value;
  }

  
$request $method .' '$path ." HTTP/1.0\r\n";
  
$request .= implode("\r\n"$defaults);
  
$request .= "\r\n\r\n";
  if (
$data) {
    
$request .= $data ."\r\n";
  }
  
$result->request $request;

  
fwrite($fp$request);

  
// Fetch response.
  
$response '';
  while (!
feof($fp) && $chunk fread($fp1024)) {
    
$response .= $chunk;
  }
  
fclose($fp);

  
// Parse response.
  
list($split$result->data) = explode("\r\n\r\n"$response2);
  
$split preg_split("/\r\n|\n|\r/"$split);

  list(
$protocol$code$text) = explode(' 'trim(array_shift($split)), 3);
  
$result->headers = array();

  
// Parse headers.
  
while ($line trim(array_shift($split))) {
    list(
$header$value) = explode(':'$line2);
    if (isset(
$result->headers[$header]) && $header == 'Set-Cookie') {
      
// RFC 2109: the Set-Cookie response header comprises the token Set-
      // Cookie:, followed by a comma-separated list of one or more cookies.
      
$result->headers[$header] .= ','trim($value);
    }
    else {
      
$result->headers[$header] = trim($value);
    }
  }

  
$responses = array(
    
100 => 'Continue'101 => 'Switching Protocols',
    
200 => 'OK'201 => 'Created'202 => 'Accepted'203 => 'Non-Authoritative Information'204 => 'No Content'205 => 'Reset Content'206 => 'Partial Content',
    
300 => 'Multiple Choices'301 => 'Moved Permanently'302 => 'Found'303 => 'See Other'304 => 'Not Modified'305 => 'Use Proxy'307 => 'Temporary Redirect',
    
400 => 'Bad Request'401 => 'Unauthorized'402 => 'Payment Required'403 => 'Forbidden'404 => 'Not Found'405 => 'Method Not Allowed'406 => 'Not Acceptable'407 => 'Proxy Authentication Required'408 => 'Request Time-out'409 => 'Conflict'410 => 'Gone'411 => 'Length Required'412 => 'Precondition Failed'413 => 'Request Entity Too Large'414 => 'Request-URI Too Large'415 => 'Unsupported Media Type'416 => 'Requested range not satisfiable'417 => 'Expectation Failed',
    
500 => 'Internal Server Error'501 => 'Not Implemented'502 => 'Bad Gateway'503 => 'Service Unavailable'504 => 'Gateway Time-out'505 => 'HTTP Version not supported'
  
);
  
// RFC 2616 states that all unknown HTTP codes must be treated the same as
  // the base code in their class.
  
if (!isset($responses[$code])) {
    
$code floor($code 100) * 100;
  }

  switch (
$code) {
    case 
200// OK
    
case 304// Not modified
      
break;
    case 
301// Moved permanently
    
case 302// Moved temporarily
    
case 307// Moved temporarily
      
$location $result->headers['Location'];
      if (
$location[0] == '/') { $location 'http://www.journals.royalsoc.ac.uk'$location; }

      if (
$retry) {
        
$result drupal_http_request($location$headers$method$data, --$retry);
        
$result->redirect_code $result->code;
      }
      
$result->redirect_url $location;

      break;
    default:
      
$result->error $text;
  }

  
$result->code $code;
  return 
$result;
}

// Fetch index page
print ("Fetching index page\n");
$root "http://www.journals.royalsoc.ac.uk";
$request drupal_http_request($root .'/');

// Extract session_id
preg_match('!/\(([a-z0-9]+)\)/!'$request->location$matches);
$session_id $matches[1];
print (
"Session ID: "$session_id ."\n\n");

// Extract available journals
preg_match_all('!<a href="/\('$session_id .'\)/link.asp\?id=([0-9]+)" class="journaltitletext">(.+?)<!'$request->data$matchesPREG_SET_ORDER);
print 
"Available journals:\n";
$journals = array();
foreach (
$matches as $journal) {
  
$journals[$journal[1]] = $journal[2];
}
print_r($journals);

foreach (
$journals as $id => $journal) {


  
// Spider Journal
  
print "Fetching '$journal'\n";
  
$url $root "/("$session_id .")/app/home/journal.asp?referrer=parent&backto=linkingpublicationresults,1:"$id .",1";
  
$request drupal_http_request($url);

  
// Extra volume count
  
if (preg_match('!"issue.asp\?referrer=parent&backto=journal,1,([0-9]+);linkingpublicationresults,1:'$id ',1"!'$request->data$matches)) {
    
$volumes $matches[1];
    print 
'  Found '$volumes ." volumes\n";

    
// Spider volumes
    
for ($i 1$i <= $volumes$i++) {
      
$volume $i;
      
$url $root "/("$session_id .")/app/home/issue.asp?referrer=parent&backto=journal,$i,$volumes;linkingpublicationresults,1:"$id .",1";
      
$request drupal_http_request($url);

      
// Extra issue count
      
if (preg_match('!"contribution.asp\?referrer=parent&backto=issue,1,([0-9]+);journal,'$i .','$volumes .';linkingpublicationresults,1:'$id ',1"!'$request->data$matches)) {
        
$issues $matches[1];
        print 
'  Found '$issues ." issues in volume $i\n";

        
// Spider issues
        
for ($j 1$j <= $issues$j++) {
          
$issue $j;
          
$url $root "/("$session_id .")/app/home/contribution.asp?referrer=parent&backto=issue,$j,$issues;journal,$i,$volumes;linkingpublicationresults,1:"$id .",1";
          
$request drupal_http_request($url);
          
          if (!
preg_match('!<B>(.+?)<!'$request->data$matches)) {
            continue;
          }
          
$title $matches[1];

          
// Look for PDF link
          
if (preg_match('!HREF="(content.asp\?referrer=contribution&format=2.+?)">Open Full Text!'$request->data$matches)) {
            
$filename $id .'-'$volume .'-'$issue .'-'preg_replace('![^A-Za-z0-9]+!''-'html_entity_decode($title));
            
$url $root ."/("$session_id .")/app/home/"$matches[1];

            
// Resolve real link
            
$request drupal_http_request($url, array(), 'GET'NULL0);
            print 
"    Found Publication '$title' @ {$request->redirect_url}\n";

            if (!
file_exists("$filename.pdf")) {

                
$wget "wget "$request->redirect_url " -O " $filename ".pdf";
                print 
$wget "\n";
                
$wget_o = `$wget`;
                print 
$wget_o "\n";
            }

            
//print $wget_o;
            //$free_space = disk_free_space(".");
            //if ($free_space < 500000000 [> 500mb, sorta <]) {
            //    // out of space, lets move some stuff to the server
            //    $rsync = "rsync -avz -e ssh ". $filename . ".pdf term.ie:rsj &";
            //    print $rsync . "\n";
            //    $rsync_o = `$rsync`;
            //    print $rsync_o . "\n";
            //} 

            // alreight
          
}
        }
      }
    }
  }
}

/*$links = array(
  120177 => )
<TABLE WIDTH="95%" CELLSPACING="2" CELLPADDING="2" BORDER="0">  <TR>    <TD COLSPAN=2 HEIGHT=25 VALIGN="TOP" NOWRAP><B CLASS="SectionHead">Journals</B></TD>  </TR><tr><td width="5%" align="LEFT" valign="TOP">&nbsp;</td><td width="95%" align="LEFT" valign="TOP"><a href="/(14zgywf34lbsvgmmgupr5a45)/link.asp?id=" class="journaltitletext">Biographical Memoirs of Fellows of the Royal Society (1932 - )</a></td></tr><tr><td width="5%" align="LEFT" valign="TOP">&nbsp;</td><td width="95%" align="LEFT" valign="TOP"><a href="/(14zgywf34lbsvgmmgupr5a45)/link.asp?id=110824" class="journaltitletext">Biology Letters (2005 - )</a></td></tr><tr><td width="5%" align="LEFT" valign="TOP">&nbsp;</td><td width="95%" align="LEFT" valign="TOP"><a href="/(14zgywf34lbsvgmmgupr5a45)/link.asp?id=111337" class="journaltitletext">Journal of the Royal Society Interface (2004 - )</a></td></tr><tr><td width="5%" align="LEFT" valign="TOP">&nbsp;</td><td width="95%" align="LEFT" valign="TOP"><a href="/(14zgywf34lbsvgmmgupr5a45)/link.asp?id=110825" class="journaltitletext">Notes and Records of the Royal Society (1938 - )</a></td></tr><tr><td width="5%" align="LEFT" valign="TOP">&nbsp;</td><td width="95%" align="LEFT" valign="TOP"><a href="/(14zgywf34lbsvgmmgupr5a45)/link.asp?id=102021" class="journaltitletext">Philosophical Transactions of the Royal Society A: Mathematical, Physical and Engineering Sciences (1887 - )</a></td></tr><tr><td width="5%" align="LEFT" valign="TOP">&nbsp;</td><td width="95%" align="LEFT" valign="TOP"><a href="/(14zgywf34lbsvgmmgupr5a45)/link.asp?id=102022" class="journaltitletext">Philosophical Transactions of the Royal Society B: Biological Sciences (1887 - )</a></td></tr><tr><td width="5%" align="LEFT" valign="TOP">&nbsp;</td><td width="95%" align="LEFT" valign="TOP"><a href="/(14zgywf34lbsvgmmgupr5a45)/link.asp?id=120135" class="journaltitletext">Philosophical Transactions of the Royal Society of London (1665 - 1886)</a></td></tr><tr><td width="5%" align="LEFT" valign="TOP">&nbsp;</td><td width="95%" align="LEFT" valign="TOP"><a href="/(14zgywf34lbsvgmmgupr5a45)/link.asp?id=102023" class="journaltitletext">Proceedings of the Royal Society A: Mathematical, Physical and Engineering Sciences (1905 - )</a></td></tr><tr><td width="5%" align="LEFT" valign="TOP">&nbsp;</td><td width="95%" align="LEFT" valign="TOP"><a href="/(14zgywf34lbsvgmmgupr5a45)/link.asp?id=102024" class="journaltitletext">Proceedings of the Royal Society B: Biological Sciences (1905 - )</a></td></tr><tr><td width="5%" align="LEFT" valign="TOP">&nbsp;</td><td width="95%" align="LEFT" valign="TOP"><a href="/(14zgywf34lbsvgmmgupr5a45)/link.asp?id=120144" class="journaltitletext">Proceedings of the Royal Society of London (1800 - 1905)</a></td></tr></table><p></p><TABLE WIDTH="95%" CELLSPACING="0" CELLPADDING="2" BORDER="0">  <TR>    <TD COLSPAN=2 HEIGHT=25 VALIGN="TOP" NOWRAP><B CLASS="SectionHead">Quick Search</B></TD>    <TD COLSPAN=2 HEIGHT=25 VALIGN="TOP" ALIGN="RIGHT"><A CLASS="Treeview" HREF="search-cit
*/