2016-03-09 00:06:58 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Zotlabs\Web;
|
|
|
|
|
|
|
|
|
|
|
|
class HttpMeta {
|
|
|
|
|
|
|
|
private $vars = null;
|
2019-10-11 01:34:44 +00:00
|
|
|
private $og = null;
|
2016-03-09 00:06:58 +00:00
|
|
|
|
|
|
|
function __construct() {
|
|
|
|
|
2019-09-30 05:36:52 +00:00
|
|
|
$this->vars = [];
|
|
|
|
$this->og = [];
|
2020-09-14 02:08:07 +00:00
|
|
|
$this->ogproperties = [];
|
2016-03-09 00:06:58 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-09-14 02:08:07 +00:00
|
|
|
//Set Meta Value
|
|
|
|
// Mode:
|
|
|
|
// 0 = Default - set if no value currently exists
|
|
|
|
// 1 = Overwrite - replace existing value(s)
|
|
|
|
// 2 = Multi - append to the array of values
|
|
|
|
function set($property,$value,$mode=0) {
|
|
|
|
$ogallowsmulti = ['image','audio','video'];
|
2019-09-30 05:36:52 +00:00
|
|
|
if (strpos($property,'og:') === 0) {
|
2020-09-14 02:08:07 +00:00
|
|
|
$count = 0;
|
|
|
|
foreach ($this->og as $ogk => $ogdata) {
|
|
|
|
if (strpos($ogdata['property'],$property) === 0) {
|
|
|
|
if ($mode == 1) {
|
|
|
|
unset($this->og[$ogk]);
|
|
|
|
unset($this->ogproperties[$property]);
|
|
|
|
}
|
|
|
|
elseif ($mode == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
elseif ($value == $ogdata['value']) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($value !== null) {
|
|
|
|
//mode = 1 with value === null will delete the property entirely.
|
|
|
|
$components = explode(':',$property);
|
|
|
|
$ogp=$components[1];
|
|
|
|
|
|
|
|
if (!$count || in_array($ogp,$ogallowsmulti)) {
|
|
|
|
$this->og[]=['property'=>$property,'value'=>$value];
|
|
|
|
$this->ogproperties[$property] = $property;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2016-03-09 00:06:58 +00:00
|
|
|
$this->vars[$property] = $value;
|
2019-09-30 05:36:52 +00:00
|
|
|
}
|
2016-03-09 00:06:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function check_required() {
|
2019-09-30 05:36:52 +00:00
|
|
|
if (
|
2020-09-14 02:08:07 +00:00
|
|
|
in_array('og:title',$this->ogproperties)
|
|
|
|
&& in_array('og:type', $this->ogproperties)
|
|
|
|
&& (in_array('og:image',$this->ogproperties)
|
|
|
|
|| in_array('og:image:url',$this->ogproperties))
|
2020-09-14 03:36:02 +00:00
|
|
|
&& (array_key_exists('og:url', $this->ogproperties)
|
|
|
|
|| array_key_exists('og:url:secure_url', $this->ogproperties))
|
2020-09-14 02:08:07 +00:00
|
|
|
&& array_key_exists('og:description', $this->ogproperties)
|
2019-09-30 05:36:52 +00:00
|
|
|
) {
|
2016-03-09 00:06:58 +00:00
|
|
|
return true;
|
2019-09-30 05:36:52 +00:00
|
|
|
}
|
2016-03-09 00:06:58 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
function get_field($field) {
|
2019-09-30 05:36:52 +00:00
|
|
|
if (strpos($field,'og:') === 0) {
|
2020-09-14 02:08:07 +00:00
|
|
|
foreach ($this->og as $ogdata) {
|
|
|
|
if (strpos($ogdata['property'],$field) === 0) {
|
|
|
|
$arr[$field][] = $ogdata['value'];
|
|
|
|
}
|
|
|
|
}
|
2019-09-30 05:36:52 +00:00
|
|
|
}
|
|
|
|
else {
|
2016-03-09 00:06:58 +00:00
|
|
|
$arr = $this->vars;
|
2019-09-30 05:36:52 +00:00
|
|
|
}
|
|
|
|
|
2021-03-11 05:31:13 +00:00
|
|
|
if (isset($arr) && is_array($arr) && array_key_exists($field,$arr) && $arr[$field]) {
|
2016-03-09 00:06:58 +00:00
|
|
|
return $arr[$field];
|
2019-09-30 05:36:52 +00:00
|
|
|
}
|
2016-03-09 00:06:58 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function get() {
|
2020-07-28 04:28:15 +00:00
|
|
|
// use 'name' for most meta fields, and 'property' for opengraph properties
|
2016-03-09 00:06:58 +00:00
|
|
|
$o = '';
|
2019-09-30 05:36:52 +00:00
|
|
|
if ($this->vars) {
|
|
|
|
foreach ($this->vars as $k => $v) {
|
2020-07-28 04:28:15 +00:00
|
|
|
$o .= '<meta name="' . htmlspecialchars($k,ENT_COMPAT,'UTF-8',false) . '" content="' . htmlspecialchars($v,ENT_COMPAT,'UTF-8',false) . '" />' . "\r\n" ;
|
2016-03-09 00:06:58 +00:00
|
|
|
}
|
|
|
|
}
|
2019-09-30 05:36:52 +00:00
|
|
|
if ($this->check_required()) {
|
2020-09-14 02:08:07 +00:00
|
|
|
foreach ($this->og as $ogdata) {
|
|
|
|
$o .= '<meta property="' . htmlspecialchars($ogdata['property'],ENT_COMPAT,'UTF-8',false) . '" content="' . htmlspecialchars($ogdata['value'],ENT_COMPAT,'UTF-8',false) . '" />' . "\r\n" ;
|
2016-03-09 00:06:58 +00:00
|
|
|
}
|
|
|
|
}
|
2019-09-30 05:36:52 +00:00
|
|
|
if ($o) {
|
2016-03-09 00:06:58 +00:00
|
|
|
return "\r\n" . $o;
|
2019-09-30 05:36:52 +00:00
|
|
|
}
|
2016-03-09 00:06:58 +00:00
|
|
|
return $o;
|
|
|
|
}
|
|
|
|
|
2020-09-12 08:41:57 +00:00
|
|
|
}
|