php-upnp/src/SSDP/Service.php

110 lines
2.4 KiB
PHP
Raw Normal View History

2017-02-16 15:29:55 +00:00
<?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>
*/
2017-02-18 12:12:36 +00:00
use JsonSerializable;
2017-02-16 15:29:55 +00:00
use SimpleXMLElement;
2017-02-18 12:12:36 +00:00
class Service implements JsonSerializable
2017-02-16 15:29:55 +00:00
{
2017-02-18 12:12:36 +00:00
protected $device;
2017-02-16 15:29:55 +00:00
protected $serviceType;
protected $serviceId;
protected $scpdUrl;
protected $controlUrl;
protected $eventSubUrl;
2017-02-18 12:12:36 +00:00
public function __construct(Device $device, SimpleXMLElement $spec)
2017-02-16 15:29:55 +00:00
{
2017-02-18 12:12:36 +00:00
$this->device = $device;
2017-02-16 15:29:55 +00:00
$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;
}
2017-02-16 16:40:02 +00:00
public function getServiceType()
{
return $this->serviceType;
}
public function getServiceId()
{
return $this->serviceId;
}
public function getScpdUrl()
{
return $this->scpdUrl;
}
2017-03-31 14:45:52 +00:00
/**
* Get the service description URL
*
* @return string
*/
public function getUrl()
2017-02-18 12:12:36 +00:00
{
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;
}
}
2017-02-16 16:40:02 +00:00
public function getControlUrl()
{
return $this->controlUrl;
}
public function getEventSubUrl()
{
return $this->eventSubUrl;
}
2017-02-16 15:29:55 +00:00
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
);
}
2017-02-18 12:12:36 +00:00
public function jsonSerialize()
{
return get_object_vars($this);
}
2017-02-16 15:29:55 +00:00
}