Ejemplo n.º 1
0
	/**
	 * Send a single Pingback
	 * @param string $source_uri The URI source of the ping (here)
	 * @param string $target_uri The URI destination of the ping (there, the site linked to in the content)
	 * @param Post $post The post	object that is initiating the ping, used to track the pings that were sent
	 * @todo If receive error code of already pinged, add to the successful.
	 */
	public function send_pingback( $source_uri, $target_uri, $post = NULL )
	{
		// RemoteRequest makes it easier to retrieve the headers.
		try {
			$rr = new RemoteRequest( $target_uri );
			$rr->execute();
			if ( ! $rr->executed() ) {
				return false;
			}
		}
		catch ( Exception $e ) {
			// log the pingback error
			EventLog::log( _t( 'Unable to retrieve target, can\'t detect pingback endpoint. (Source: %1$s | Target: %2$s)', array( $source_uri, $target_uri ) ), 'err', 'Pingback' );
			return false;
		}

		$headers = $rr->get_response_headers();
		$body = $rr->get_response_body();

		// Find a Pingback endpoint.
		if ( isset( $headers['X-Pingback'] ) ) {
			$pingback_endpoint = $headers['X-Pingback'];
		}
		elseif ( preg_match( '/<link rel="pingback" href="([^"]+)" ?\/?'.'>/is', $body, $matches ) ) {
			$pingback_endpoint = $matches[1];
		}
		else {
			// No Pingback endpoint found.
			return false;
		}

		try {
			$response = XMLRPCClient::open( $pingback_endpoint )->pingback->ping( $source_uri, $target_uri );
		}
		catch ( Exception $e ) {
			EventLog::log( _t( 'Invalid Pingback endpoint - %1$s (Source: %2$s | Target: %3$s)', array( $pingback_endpoint, $source_uri, $target_uri ) ), 'err', 'Pingback' );
			return false;
		}

		if ( isset( $response->faultString ) ) {
			EventLog::log( _t( 'Pingback error: %1$s - %2$s (Source: %3$s | Target: %4$s)', array( $response->faultCode, $response->faultString, $source_uri, $target_uri ) ), 'err', 'Pingback' );
			return false;
		}
		else {
			// The pingback has been registered and is stored as a successful pingback.
			if ( is_object( $post ) ) {
				if ( isset( $post->info->pingbacks_successful ) ) {
					$pingbacks_successful = $post->info->pingbacks_successful;
					$pingbacks_successful[]= $target_uri;
					$post->info->pingbacks_successful = $pingbacks_successful;
				}
				else {
					$post->info->pingbacks_successful = array( $target_uri );
				}
				$post->info->commit();
			}
			return true;
		}
	}
Ejemplo n.º 2
0
 /**
  * Send a single Pingback
  * @param string $source_uri The URI source of the ping (here)
  * @param string $target_uri The URI destination of the ping (there, the site linked to in the content)
  * @param Post $post The post	object that is initiating the ping, used to track the pings that were sent
  * @todo If receive error code of already pinged, add to the successful.
  */
 public function send_pingback($source_uri, $target_uri, $post = NULL)
 {
     // RemoteRequest makes it easier to retrieve the headers.
     $rr = new RemoteRequest($target_uri);
     $rr->execute();
     if (!$rr->executed()) {
         return false;
     }
     $headers = $rr->get_response_headers();
     $body = $rr->get_response_body();
     // Find a Pingback endpoint.
     if (preg_match('/^X-Pingback: (\\S*)/im', $headers, $matches)) {
         $pingback_endpoint = $matches[1];
     } elseif (preg_match('/<link rel="pingback" href="([^"]+)" ?\\/?' . '>/is', $body, $matches)) {
         $pingback_endpoint = $matches[1];
     } else {
         // No Pingback endpoint found.
         return false;
     }
     try {
         $response = XMLRPCClient::open($pingback_endpoint)->pingback->ping($source_uri, $target_uri);
     } catch (Exception $e) {
         EventLog::log('Invalid Pingback endpoint - ' . $pingback_endpoint . '  (Source: ' . $source_uri . ' | Target: ' . $target_uri . ')', 'info', 'Pingback');
         return false;
     }
     if (isset($response->faultString)) {
         EventLog::log($response->faultCode . ' - ' . $response->faultString . ' (Source: ' . $source_uri . ' | Target: ' . $target_uri . ')', 'info', 'Pingback');
         return false;
     } else {
         // The pingback has been registered and is stored as a successful pingback.
         if (is_object($post)) {
             if (isset($post->info->pingbacks_successful)) {
                 $pingbacks_successful = $post->info->pingbacks_successful;
                 $pingbacks_successful[] = $target_uri;
                 $post->info->pingbacks_successful = $pingbacks_successful;
             } else {
                 $post->info->pingbacks_successful = array($target_uri);
             }
             $post->info->commit();
         }
         return true;
     }
 }