base.php

Go to the documentation of this file.
00001 <?php
00013 require_once('HTTP/Request.php');
00014 require_once(PPF_ROOT_DIR.'/utilities/decompress.php');
00015 require_once(PPF_ROOT_DIR.'/utilities/errorcheck.php');
00016 
00023 class LFSWorldDataProvider
00024 {
00029     var $m_oRequest = null;
00030     
00035     var $m_aSrcMetaData = array();
00036     
00041     var $m_aParams = array();
00042     
00051     function LFSWorldDataProvider($srcMetaData=array(), $params=array())
00052     {
00053         $this->m_aSrcMetaData = array_merge($this->m_aSrcMetaData, $srcMetaData);
00054         $this->m_aParams = array_merge($this->m_aParams, $params);
00055         $this->m_oRequest = new HTTP_Request(null, array('method'=>HTTP_REQUEST_METHOD_GET, 'timeout'=>2));
00056         $this->_init();
00057     }
00058 
00065     function _init()
00066     {
00067     }
00068       
00077     function parse($rawList, &$outputList)
00078     {
00079         $rawList = explode("\n", trim($rawList));
00080 
00081         if(sizeof($rawList) == 0)
00082         {
00083             $GLOBALS['PPFLog'][] = 'Empty input to LFSWorld Parser!';
00084             return false;
00085         }
00086         
00087         foreach($rawList as $entry)
00088         {
00089             $output = $this->_parseLine($entry);
00090             if($output != false)
00091                 $outputList[] = $output;
00092         }
00093 
00094         return true;
00095     }
00096     
00103     function getData()
00104     {
00105         //Set URL
00106         $this->m_oRequest->HTTP_Request($this->getURL());
00107         //Make request
00108         $this->m_oRequest->sendRequest(true);
00109         $data = ($this->m_oRequest->getResponseCode() != 200) ? (false) : ($this->m_oRequest->getResponseBody());
00110 
00111         //Did request go OK?
00112         if($data == false)
00113         {
00114             $GLOBALS['PPFLog'][] = 'Unable to connect to LFSWorld';
00115             return false;
00116         }
00117         
00118         //Errors are not compressed, so was there an LFSWorld error?
00119         if(isLFSWorldError($data))
00120         {
00121             $GLOBALS['PPFLog'][] = 'LFSWorld error was encountered! ('.$data.')';
00122             return false;
00123         }
00124         
00125         //Decompress data if it was requested in a compressed form
00126         if(isset($this->m_aParams['c']))
00127             $data = PPFDecompress($data, $this->m_aParams['c']);
00128         
00129         //Did decompression go ok?
00130         if($data == false)
00131         {
00132             $GLOBALS['PPFLog'][] = 'LFSWorld raw data decompression failed!';
00133             return false;
00134         }
00135         
00136         $this->saveRequestTime();
00137         
00138         $parsed = null;
00139         $this->parse($data, $parsed);
00140         return $parsed;
00141     }
00142     
00149     function getParams()
00150     {
00151         return $this->m_aParams;
00152     }
00153     
00160     function getSrcMetaData()
00161     {
00162         return $this->m_aSrcMetaData;
00163     }
00164     
00171     function getURL()
00172     {
00173         return $this->m_oRequest->_url->getURL();
00174     }
00175     
00183     function initParams(&$params)
00184     {
00185         //Reset request object
00186         $this->m_oRequest->HTTP_Request($this->m_sURL, array());
00187         
00188         //IDK && Username/password handling
00189         if(isset($params['IDK']) && strlen($params['IDK']) == 32)
00190         {
00191             if(isset($params['user']))
00192                 unset($params['user']);
00193             if(isset($params['pass']))
00194                 unset($params['pass']);
00195         }
00196         else if(isset($params['user']) && isset($params['pass']))
00197         {
00198             if(isset($params['IDK']))
00199                 unset($params['IDK']);
00200             
00201             $params['user'] = urlencode($params['user']);
00202             if(strlen($params['pass']) != 32)
00203                 $params['pass'] = md5($params['pass']);
00204         }
00205         
00206         //Compression is good so if it isn't enabled yet, enable it
00207         if(!isset($params['c']))
00208             $params['c'] = 2;
00209         else
00210             $params['c'] = (int)$params['c'];
00211 
00212         //But compression is no good if the server doesn't have gzip
00213         //Test to make sure server can decompress, and if it can't then disable compression
00214         if(PPFCanDecompress($params['c']) == false)
00215             unset($params['c']);
00216         
00217         //Premium Stats "smart" handling
00218         //It will only use premium if ps is set && the tarpit for a provider is still active
00219         //@TODO Check accuracy of actualTarpit times (especially teams)
00220         if(isset($params['ps']))
00221         {
00222             $lastRequest = $this->getLastRequestTime();
00223             if(time() - ($lastRequest+$this->m_aSrcMetaData['actualTarpit']) < 0)
00224                 $params['ps'] = 1;
00225             else
00226                 unset($params['ps']);            
00227         }
00228         
00229         //Put params to the URL
00230         foreach($params as $param=>$value)
00231             $this->m_oRequest->addQueryString($param, $value);
00232             
00233         $this->m_aParams = $params;
00234 
00235         return true;
00236     }
00237     
00244     function getLastRequestTime()
00245     {
00246         if(file_exists(PPF_DATA_DIR.'/'.get_class($this).'.lrt'))
00247             return filemtime(PPF_DATA_DIR.'/'.get_class($this).'.lrt');
00248             
00249         return 0;
00250     }
00251     
00258     function saveRequestTime()
00259     {
00260         $s = fopen(PPF_DATA_DIR.'/'.get_class($this).'.lrt', 'w+');
00261         fclose($s);
00262     }
00263 }
00264 ?>

Generated on Wed Oct 25 03:13:32 2006 for LFSWorldParsingProviderFramework by  doxygen 1.4.6