php-upnp/src/SSDP/Service.php

110 lines
2.4 KiB
PHP

<?php
namespace NoccyLabs\UPnP\SSDP;
/*
<serviceType>urn:schemas-upnp-org:service:Layer3Forwarding:1</serviceType>
<serviceId>urn:upnp-org:serviceId:L3Forwarding1</serviceId>
<SCPDURL>/L3F.xml</SCPDURL>
<controlURL>/ctl/L3F</controlURL>
<eventSubURL>/evt/L3F</eventSubURL>
*/
use JsonSerializable;
use SimpleXMLElement;
class Service implements JsonSerializable
{
protected $device;
protected $serviceType;
protected $serviceId;
protected $scpdUrl;
protected $controlUrl;
protected $eventSubUrl;
public function __construct(Device $device, SimpleXMLElement $spec)
{
$this->device = $device;
$this->serviceType = (string)$spec->serviceType;
$this->serviceId = (string)$spec->serviceId;
$this->scpdUrl = (string)$spec->SCPDURL;
$this->controlUrl = (string)$spec->controlURL;
$this->eventSubUrl = (string)$spec->eventSubURL;
}
public function getServiceType()
{
return $this->serviceType;
}
public function getServiceId()
{
return $this->serviceId;
}
public function getScpdUrl()
{
return $this->scpdUrl;
}
/**
* Get the service description URL
*
* @return string
*/
public function getUrl()
{
if (strpos($this->scpdUrl,"://")!==false) {
return $this->scpdUrl;
}
$dev = ['user'=>null, 'pass'=>null, 'port'=>80, 'path'=>null];
$dev = array_merge($dev, parse_url($this->device->getUrl()));
$url = $this->scpdUrl;
$auth = $dev['user']?($dev['user'].":".$dev['path']):"";
$base = $dev['scheme']."://".$dev['host'].":".($dev['port']?:80);
if ($url[0]=="/") {
return $base.$url;
} else {
return $base.rtrim($dev['path'],"/")."/".$url;
}
}
public function getControlUrl()
{
return $this->controlUrl;
}
public function getEventSubUrl()
{
return $this->eventSubUrl;
}
public function __toString()
{
return sprintf("Service: %s [%s]\nControl URL: %s\nEventSub URL: %s\nSCPD URL: %s\n",
$this->serviceId,
$this->serviceType,
$this->controlUrl,
$this->eventSubUrl,
$this->scpdUrl
);
}
public function jsonSerialize()
{
return get_object_vars($this);
}
}