Skip to content
Browse files

Fix formatting and add sensible gitignore

  • Loading branch information...
1 parent 500e2e2 commit 6408cab29df14d5d90e419b1f76e7e24f1f3ac61 @tedeh committed Feb 10, 2012
Showing with 70 additions and 121 deletions.
  1. +2 −0 .gitignore
  2. +39 −69 Pingback/Server.php
  3. +29 −52 Pingback/Utility.php
View
2 .gitignore
@@ -0,0 +1,2 @@
+.*
+!.gitignore
View
108 Pingback/Server.php
@@ -1,11 +1,10 @@
<?php
-require_once dirname(__FILE__).'/Exception.php';
+require_once dirname(__FILE__) . '/Exception.php';
-require_once dirname(__FILE__).'/Utility.php';
+require_once dirname(__FILE__) . '/Utility.php';
-class Pingback_Server
-{
+class Pingback_Server {
const RESPONSE_SUCCESS = -1;
const RESPONSE_FAULT_GENERIC = 0;
const RESPONSE_FAULT_SOURCE = 0x0010;
@@ -40,139 +39,110 @@ class Pingback_Server
'encoding' => 'utf-8'
);
- public function __construct($options = array())
- {
+ public function __construct($options = array()) {
$this->_server = xmlrpc_server_create();
-
$this->setOptions($options);
-
- if(!xmlrpc_server_register_method($this->_server, 'pingback.ping', array($this, '_ping')))
- {
+ if(!xmlrpc_server_register_method($this->_server, 'pingback.ping', array($this, '_ping'))) {
throw new Pingback_Exception('Failed to register method to server');
}
}
- public function __destruct()
- {
+ public function __destruct() {
xmlrpc_server_destroy($this->_server);
}
- protected function _ping($method, $parameters)
- {
+ protected function _ping($method, $parameters) {
list($this->_requestSource, $this->_requestTarget) = $parameters;
- $fault = false;
+ $fault = null;
- if(!Pingback_Utility::isURL($this->_requestSource)) $fault = self::RESPONSE_FAULT_SOURCE;
+ // is the source argument really an url?
+ if(!$fault && Pingback_Utility::isURL($this->_requestSource)) $fault = self::RESPONSE_FAULT_SOURCE;
- if(!Pingback_Utility::isURL($this->_requestTarget)) $fault = self::RESPONSE_FAULT_TARGET;
+ // is the target argument really an url?
+ if(!$fault && Pingback_Utility::isURL($this->_requestTarget)) $fault = self::RESPONSE_FAULT_TARGET;
- if(!Pingback_Utility::isPingbackEnabled($this->_requestTarget)) $fault = self::RESPONSE_FAULT_TARGET_INVALID;
+ // is the target url pingback enabled?
+ if(!$fault && Pingback_Utility::isPingbackEnabled($this->_requestTarget)) $fault = self::RESPONSE_FAULT_TARGET_INVALID;
- if(!Pingback_Utility::isBacklinking($this->_requestSource, $this->_requestTarget)) $fault = self::RESPONSE_FAULT_SOURCE_LINK;
+ // is the source backlinking to the target?
+ if(!$fault && Pingback_Utility::isBacklinking($this->_requestSource, $this->_requestTarget)) $fault = self::RESPONSE_FAULT_SOURCE_LINK;
- if($fault !== false)
- {
+ if($fault !== null) {
$this->setFault($fault);
return $this->getFaultAsArray($fault);
- }
- else
- {
+ } else {
$this->setSuccess();
return $this->getSuccessAsArray();
}
}
- public function getOption($option)
- {
+ public function getOption($option) {
return isset($this->_options[$option]) ? $this->_options[$option] : null;
}
- public function setOption($option, $value)
- {
+ public function setOption($option, $value) {
$this->_options[$option] = $value;
}
- public function setOptions($options = array())
- {
- foreach($options as $option => $value)
- {
+ public function setOptions($options = array()) {
+ foreach($options as $option => $value) {
$this->setOption($option, $value);
}
}
- public function execute($request = null)
- {
- if($request)
- {
+ public function execute($request = null) {
+ if($request) {
$this->_request = $request;
}
$this->_response = xmlrpc_server_call_method($this->_server, $this->_request, null, array('encoding' => $this->getOption('encoding')));
}
- public function setResponse($response)
- {
+ public function setResponse($response) {
$this->_response = $response;
}
- public function setRequest($request)
- {
+ public function setRequest($request) {
$this->_request = $request;
}
- public function getRequest()
- {
+ public function getRequest() {
return $this->_request;
}
- public function getResponse()
- {
+ public function getResponse() {
return $this->_response;
}
- public function getSourceURL()
- {
+ public function getSourceURL() {
return $this->_sourceURL;
}
- public function getTargetURL()
- {
+ public function getTargetURL() {
return $this->_targetURL;
}
- public function getFaultAsArray($faultCode)
- {
- return array('faultCode' => $faultCode, 'faultString' => $this->responses[$faultCode]);
+ public function getFaultAsArray($faultCode) {
+ return array(
+ 'faultCode' => $faultCode,
+ 'faultString' => $this->responses[$faultCode]
+ );
}
- public function getSuccessAsArray()
- {
+ public function getSuccessAsArray() {
return array($this->responses[self::RESPONSE_SUCCESS]);
}
- public function setFault($faultCode)
- {
+ public function setFault($faultCode) {
$this->_response = xmlrpc_encode($this->getFaultAsArray($faultCode));
}
- public function setSuccess()
- {
+ public function setSuccess() {
$this->_response = xmlrpc_encode($this->getSuccessAsArray());
}
- public function isValid()
- {
+ public function isValid() {
return !xmlrpc_is_fault($this->_response);
}
}
-
-
-
-
-
-
-
-
-
-
View
81 Pingback/Utility.php
@@ -1,55 +1,30 @@
<?php
-if(!function_exists('http_parse_headers'))
-{
- function http_parse_headers( $header )
- {
- $retVal = array();
- $fields = explode("\r\n", preg_replace('/\x0D\x0A[\x09\x20]+/', ' ', $header));
- foreach( $fields as $field ) {
- if( preg_match('/([^:]+): (.+)/m', $field, $match) ) {
- $match[1] = preg_replace('/(?<=^|[\x09\x20\x2D])./e', 'strtoupper("\0")', strtolower(trim($match[1])));
- if( isset($retVal[$match[1]]) ) {
- $retVal[$match[1]] = array($retVal[$match[1]], $match[2]);
- } else {
- $retVal[$match[1]] = trim($match[2]);
- }
- }
- }
- return $retVal;
- }
-}
-
class Pingback_Utility
{
const REGEXP_PINGBACK_LINK = '<link rel="pingback" href="([^"]+)" ?/?>';
- public static function isURL($url)
- {
+ public static function isURL($url) {
return filter_var($url, FILTER_VALIDATE_URL);
}
- public static function isPingbackEnabled($url)
- {
+ public static function isPingbackEnabled($url) {
return (bool)self::getPingbackServerURL($url);
}
- public static function getRawPostData()
- {
+ public static function getRawPostData() {
return file_get_contents('php://input');
}
- public static function getPingbackServerURL($url)
- {
+ public static function getPingbackServerURL($url) {
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HEADER, true);
curl_setopt($curl, CURLOPT_NOBODY, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$headers = http_parse_headers(curl_exec($curl));
curl_close($curl);
- if(isset($headers['X-Pingback']) && !empty($headers['X-Pingback']))
- {
+ if(isset($headers['X-Pingback']) && !empty($headers['X-Pingback'])) {
return $headers['X-Pingback'];
}
@@ -58,26 +33,21 @@ public static function getPingbackServerURL($url)
return preg_match(self::REGEXP_PINGBACK_LINK, $response, $match) ? $match[1] : false;
}
- public static function isBacklinking($from, $to)
- {
+ public static function isBacklinking($from, $to) {
$content = file_get_contents($from);
- if($content !== false)
- {
+ if($content !== false) {
$doc = new DOMDocument();
$doc->loadHTML($content);
- foreach($doc->getElementsByTagName('a') as $link)
- {
- if($link->getAttribute('href') == $to)
- {
+ foreach($doc->getElementsByTagName('a') as $link) {
+ if($link->getAttribute('href') == $to) {
return true;
}
}
}
return false;
}
- public static function sendPingback($from, $to, $server)
- {
+ public static function sendPingback($from, $to, $server) {
$request = xmlrpc_encode_request('pingback.ping', array($from, $to));
$curl = curl_init($server);
curl_setopt($curl, CURLOPT_POST, true);
@@ -89,16 +59,23 @@ public static function sendPingback($from, $to, $server)
}
}
-
-
-
-
-
-
-
-
-
-
-
-
+if(!function_exists('http_parse_headers'))
+{
+ function http_parse_headers( $header )
+ {
+ $retVal = array();
+ $fields = explode("\r\n", preg_replace('/\x0D\x0A[\x09\x20]+/', ' ', $header));
+ foreach( $fields as $field ) {
+ if( preg_match('/([^:]+): (.+)/m', $field, $match) ) {
+ $match[1] = preg_replace('/(?<=^|[\x09\x20\x2D])./e', 'strtoupper("\0")', strtolower(trim($match[1])));
+ if( isset($retVal[$match[1]]) ) {
+ $retVal[$match[1]] = array($retVal[$match[1]], $match[2]);
+ } else {
+ $retVal[$match[1]] = trim($match[2]);
+ }
+ }
+ }
+ return $retVal;
+ }
+}

0 comments on commit 6408cab

Please sign in to comment.
Something went wrong with that request. Please try again.