aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMario <mario@mariovavti.com>2021-11-24 09:08:11 +0000
committerMario <mario@mariovavti.com>2021-11-24 09:08:11 +0000
commitc95f708c9174be3138d2cff133eaa210b7222cd6 (patch)
tree316533f52b5ede79059cff6e3fba14bc8702c25f
parent06e214e5675769318a40abc9850bf8d5c85a7ee3 (diff)
downloadvolse-hubzilla-c95f708c9174be3138d2cff133eaa210b7222cd6.tar.gz
volse-hubzilla-c95f708c9174be3138d2cff133eaa210b7222cd6.tar.bz2
volse-hubzilla-c95f708c9174be3138d2cff133eaa210b7222cd6.zip
port httpmeta from zap
-rw-r--r--Zotlabs/Web/HttpMeta.php109
1 files changed, 73 insertions, 36 deletions
diff --git a/Zotlabs/Web/HttpMeta.php b/Zotlabs/Web/HttpMeta.php
index ceaa82162..7cf93dda9 100644
--- a/Zotlabs/Web/HttpMeta.php
+++ b/Zotlabs/Web/HttpMeta.php
@@ -6,71 +6,108 @@ namespace Zotlabs\Web;
class HttpMeta {
private $vars = null;
- private $og = null;
+ private $og = null;
function __construct() {
- $this->vars = array();
- $this->og = array();
+ $this->vars = [];
+ $this->og = [];
+ $this->ogproperties = [];
}
- function set($property,$value) {
- if(strpos($property,'og:') === 0)
- $this->og[$property] = $value;
- else
+ //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'];
+ if (strpos($property,'og:') === 0) {
+ $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 {
$this->vars[$property] = $value;
+ }
}
function check_required() {
- if(
- ($this->og)
- && array_key_exists('og:title',$this->og)
- && array_key_exists('og:type', $this->og)
- && array_key_exists('og:image',$this->og)
- && array_key_exists('og:url', $this->og)
- )
+ if (
+ 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))
+ && (array_key_exists('og:url', $this->ogproperties)
+ || array_key_exists('og:url:secure_url', $this->ogproperties))
+ && array_key_exists('og:description', $this->ogproperties)
+ ) {
return true;
+ }
return false;
}
function get_field($field) {
- if(strpos($field,'og:') === 0)
- $arr = $this->og;
- else
+ if (strpos($field,'og:') === 0) {
+ foreach ($this->og as $ogdata) {
+ if (strpos($ogdata['property'],$field) === 0) {
+ $arr[$field][] = $ogdata['value'];
+ }
+ }
+ }
+ else {
$arr = $this->vars;
+ }
- if($arr && array_key_exists($field,$arr) && $arr[$field])
+ if (isset($arr) && is_array($arr) && array_key_exists($field,$arr) && $arr[$field]) {
return $arr[$field];
+ }
return false;
}
function get() {
+ // use 'name' for most meta fields, and 'property' for opengraph properties
$o = '';
- if($this->vars) {
- foreach($this->vars as $k => $v) {
- $o .= '<meta property="' . $k . '" content="' . urlencode($v) . '" />' . "\r\n" ;
+ if ($this->vars) {
+ foreach ($this->vars as $k => $v) {
+ $o .= '<meta name="' . htmlspecialchars($k,ENT_COMPAT,'UTF-8',false) . '" content="' . htmlspecialchars($v,ENT_COMPAT,'UTF-8',false) . '" />' . "\r\n" ;
}
}
- if($this->check_required()) {
- $arrayproperties = [ 'og:image' ];
- foreach($this->og as $k => $v) {
- if (in_array($k,$arrayproperties)) {
- if (is_array($v)) {
- foreach ($v as $v2) {
- $o .= '<meta property="' . $k . '" content="' . $v2 . '" />' . "\r\n" ;
- }
- } else {
- $o .= '<meta property="' . $k . '" content="' . $v . '" />' . "\r\n" ;
- }
- } else {
- $o .= '<meta property="' . $k . '" content="' . $v . '" />' . "\r\n" ;
- }
+ if ($this->check_required()) {
+ 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" ;
}
}
- if($o)
+ if ($o) {
return "\r\n" . $o;
+ }
return $o;
}