Diff
14 years ago
.htaccess
14 years ago
Diff.php
14 years ago
GeoIP.dat
13 years ago
IPTraf.php
13 years ago
diffResult.php
14 years ago
email_genericAlert.php
14 years ago
email_newIssues.php
14 years ago
email_unlockRequest.php
14 years ago
menu_activity.php
13 years ago
menu_blockedIPs.php
13 years ago
menu_countryBlocking.php
13 years ago
menu_options.php
13 years ago
menu_scan.php
13 years ago
menu_scanSchedule.php
13 years ago
schedWeekEntry.php
13 years ago
sysinfo.php
14 years ago
unknownFiles.php
13 years ago
viewFullActivityLog.php
13 years ago
wf503.php
13 years ago
wfAPI.php
13 years ago
wfAction.php
14 years ago
wfArray.php
13 years ago
wfBrowscap.php
14 years ago
wfBrowscapCache.php
14 years ago
wfBulkCountries.php
13 years ago
wfConfig.php
13 years ago
wfCountryMap.php
13 years ago
wfCrawl.php
13 years ago
wfDB.php
13 years ago
wfDict.php
14 years ago
wfGeoIP.php
13 years ago
wfIssues.php
13 years ago
wfLockedOut.php
14 years ago
wfLog.php
13 years ago
wfModTracker.php
14 years ago
wfRate.php
14 years ago
wfScanEngine.php
13 years ago
wfSchema.php
13 years ago
wfUnlockMsg.php
14 years ago
wfUtils.php
13 years ago
wfViewResult.php
14 years ago
wordfenceClass.php
13 years ago
wordfenceConstants.php
13 years ago
wordfenceHash.php
13 years ago
wordfenceScanner.php
13 years ago
wordfenceURLHoover.php
13 years ago
wfAPI.php
174 lines
| 1 | <?php |
| 2 | require_once('wordfenceConstants.php'); |
| 3 | require_once('wordfenceClass.php'); |
| 4 | class wfAPI { |
| 5 | public $lastHTTPStatus = ''; |
| 6 | public $lastCurlErrorNo = ''; |
| 7 | private $curlDataWritten = 0; |
| 8 | private $curlContent = 0; |
| 9 | private $APIKey = ''; |
| 10 | private $wordpressVersion = ''; |
| 11 | private static $maintMsg = "The Wordfence scanning server could not be contacted."; |
| 12 | public function __construct($apiKey, $wordpressVersion){ |
| 13 | $this->APIKey = $apiKey; |
| 14 | $this->wordpressVersion = $wordpressVersion; |
| 15 | } |
| 16 | public function call($action, $getParams = array(), $postParams = array()){ |
| 17 | $json = $this->getURL($this->getAPIURL() . '/v' . WORDFENCE_API_VERSION . '/?' . $this->makeAPIQueryString() . '&' . http_build_query( |
| 18 | array_merge( |
| 19 | array('action' => $action), |
| 20 | $getParams |
| 21 | )), $postParams); |
| 22 | if(! $json){ |
| 23 | throw new Exception("We received an empty data response from the Wordfence scanning servers when calling the '$action' function."); |
| 24 | } |
| 25 | |
| 26 | $dat = json_decode($json, true); |
| 27 | if(! is_array($dat)){ |
| 28 | throw new Exception("We received a data structure that is not the expected array when contacting the Wordfence scanning servers and calling the '$action' function."); |
| 29 | } |
| 30 | if(is_array($dat) && isset($dat['errorMsg'])){ |
| 31 | throw new Exception($dat['errorMsg']); |
| 32 | } |
| 33 | return $dat; |
| 34 | } |
| 35 | public function curlWrite($h, $d){ |
| 36 | $this->curlContent .= $d; |
| 37 | if($this->curlDataWritten > 10000000){ //10 megs |
| 38 | return 0; |
| 39 | } else { |
| 40 | return strlen($d); |
| 41 | } |
| 42 | } |
| 43 | protected function getURL($url, $postParams = array()){ |
| 44 | if(function_exists('curl_init')){ |
| 45 | $this->curlDataWritten = 0; |
| 46 | $this->curlContent = ""; |
| 47 | $curl = curl_init($url); |
| 48 | curl_setopt ($curl, CURLOPT_TIMEOUT, 300); |
| 49 | curl_setopt ($curl, CURLOPT_USERAGENT, "Wordfence.com UA " . (defined('WORDFENCE_VERSION') ? WORDFENCE_VERSION : '[Unknown version]') ); |
| 50 | curl_setopt ($curl, CURLOPT_RETURNTRANSFER, TRUE); |
| 51 | curl_setopt ($curl, CURLOPT_HEADER, 0); |
| 52 | curl_setopt ($curl, CURLOPT_SSL_VERIFYPEER, false); |
| 53 | curl_setopt ($curl, CURLOPT_SSL_VERIFYHOST, false); |
| 54 | curl_setopt ($curl, CURLOPT_WRITEFUNCTION, array($this, 'curlWrite')); |
| 55 | curl_setopt($curl, CURLOPT_POST, true); |
| 56 | curl_setopt($curl, CURLOPT_POSTFIELDS, $postParams); |
| 57 | |
| 58 | $curlResult = curl_exec($curl); |
| 59 | $httpStatus = curl_getinfo($curl, CURLINFO_HTTP_CODE); |
| 60 | $this->lastCurlErrorNo = curl_errno($curl); |
| 61 | if($httpStatus == 200){ |
| 62 | curl_close($curl); |
| 63 | return $this->curlContent; |
| 64 | } else { |
| 65 | $cerror = curl_error($curl); |
| 66 | curl_close($curl); |
| 67 | throw new Exception("We received an error response when trying to contact the Wordfence scanning servers. The HTTP status code was [$httpStatus]" . ($cerror ? (' and the error from CURL was ' . $cerror) : '')); |
| 68 | } |
| 69 | } else { |
| 70 | $data = $this->fileGet($url, $postParams); |
| 71 | if($data === false){ |
| 72 | $err = error_get_last(); |
| 73 | if($err){ |
| 74 | throw new Exception("We received an error response when trying to contact the Wordfence scanning servers using PHP's file_get_contents function. The error was: " . $err); |
| 75 | } else { |
| 76 | throw new Exception("We received an empty response when trying to contact the Wordfence scanning servers using PHP's file_get_contents function."); |
| 77 | } |
| 78 | } |
| 79 | return $data; |
| 80 | } |
| 81 | |
| 82 | } |
| 83 | private function fileGet($url, $postParams){ |
| 84 | $body = ""; |
| 85 | if(is_array($postParams)){ |
| 86 | $bodyArr = array(); |
| 87 | foreach($postParams as $key => $val){ |
| 88 | $bodyArr[] = urlencode($key) . '=' . urlencode($val); |
| 89 | } |
| 90 | $body = implode('&', $bodyArr); |
| 91 | } else { |
| 92 | $body = $postParams; |
| 93 | } |
| 94 | $opts = array('http' => |
| 95 | array( |
| 96 | 'method' => 'POST', |
| 97 | 'content' => $body, |
| 98 | 'header' => "Content-Type: application/x-www-form-urlencoded\r\n", |
| 99 | 'timeout' => 60 |
| 100 | ) |
| 101 | ); |
| 102 | $context = stream_context_create($opts); |
| 103 | return @file_get_contents($url, false, $context, -1); |
| 104 | } |
| 105 | public function binCall($func, $postData){ |
| 106 | $url = $this->getAPIURL() . '/v' . WORDFENCE_API_VERSION . '/?' . $this->makeAPIQueryString() . '&action=' . $func; |
| 107 | if(function_exists('curl_init')){ |
| 108 | $curl = curl_init($url); |
| 109 | curl_setopt ($curl, CURLOPT_TIMEOUT, 300); |
| 110 | //curl_setopt($curl, CURLOPT_VERBOSE, true); |
| 111 | curl_setopt ($curl, CURLOPT_USERAGENT, "Wordfence"); |
| 112 | curl_setopt ($curl, CURLOPT_RETURNTRANSFER, TRUE); |
| 113 | curl_setopt ($curl, CURLOPT_SSL_VERIFYPEER, false); |
| 114 | curl_setopt ($curl, CURLOPT_SSL_VERIFYHOST, false); |
| 115 | curl_setopt($curl, CURLOPT_POST, true); |
| 116 | if($postData){ |
| 117 | curl_setopt($curl, CURLOPT_POSTFIELDS, $postData); |
| 118 | } else { |
| 119 | curl_setopt($curl, CURLOPT_POSTFIELDS, array()); |
| 120 | } |
| 121 | $data = curl_exec($curl); |
| 122 | $httpStatus = curl_getinfo($curl, CURLINFO_HTTP_CODE); |
| 123 | if($httpStatus != 200){ |
| 124 | $cError = curl_error($curl); |
| 125 | curl_close($curl); |
| 126 | if($cError){ |
| 127 | throw new Exception("We received an error response when trying to fetch binary data from the Wordfence scanning server. The HTTP status was [$httpStatus] with error: $cError"); |
| 128 | } else { |
| 129 | throw new Exception("We received an error HTTP response when trying to fetch binary data from the Wordfence scanning server: [$httpStatus]"); |
| 130 | } |
| 131 | } |
| 132 | } else { |
| 133 | $data = $this->fileGet($url, $postData); |
| 134 | if($data === false){ |
| 135 | $err = error_get_last(); |
| 136 | if($err){ |
| 137 | throw new Exception("We received an error response when trying to fetch binary data from the Wordfence scanning server using file_get_contents: $err"); |
| 138 | } else { |
| 139 | throw new Exception("We received an error when trying to fetch binary data from the Wordfence scanning server using file_get_contents. There was no message explaining the error."); |
| 140 | } |
| 141 | } |
| 142 | $httpStatus = '200'; |
| 143 | } |
| 144 | if(preg_match('/\{.*errorMsg/', $data)){ |
| 145 | $jdat = @json_decode($data, true); |
| 146 | if(is_array($jdat) && $jdat['errorMsg']){ |
| 147 | throw new Exception($jdat['errorMsg']); |
| 148 | } |
| 149 | } |
| 150 | return array('code' => $httpStatus, 'data' => $data); |
| 151 | } |
| 152 | public function makeAPIQueryString(){ |
| 153 | $siteurl = ''; |
| 154 | if(function_exists('get_bloginfo')){ |
| 155 | $siteurl = get_bloginfo('siteurl'); |
| 156 | } |
| 157 | return http_build_query(array( |
| 158 | 'v' => $this->wordpressVersion, |
| 159 | 's' => $siteurl, |
| 160 | 'k' => $this->APIKey |
| 161 | )); |
| 162 | } |
| 163 | private function getAPIURL(){ |
| 164 | $ssl_supported = false; |
| 165 | if(defined('CURL_VERSION_SSL') && function_exists('curl_version')){ |
| 166 | $version = curl_version(); |
| 167 | $ssl_supported = ($version['features'] & CURL_VERSION_SSL); |
| 168 | } |
| 169 | return $ssl_supported ? WORDFENCE_API_URL_SEC : WORDFENCE_API_URL_NONSEC; |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | ?> |
| 174 |