<?php
/**
* Misplaced methods from HB class
*
* Do not use this class directly
*
* this methods available as HB::$method
*
*/
class HB_Legacy {
public const LINK_TYPE_WHOLE_LINE = 'whole_line';
public const LINK_TYPE_STREET_ONLY = 'street_only';
// is radaris site (or any DVP copy of it)
static function isRD() { # 1=radaris.com, 2=rd.XXX, 3=https://test.phoneowner.com/61728863
$h = $_SERVER['HTTP_HOST']??"";
if ($h == "radaris.com" || $h == "m.radaris.com")
return 1;
if (fm('!^(rd)\.[a-z]+$!', $h)=='rd')
return 2;
if (! $h) // CLI
return 3;
}
// radaris.com URL from any context
// If you radaris OR dvp server keep url as is
static function rdURL($url, array $params=[]) {
$url = url($url, $params);
if (substr($url, 0, 4)=='http') // do not touch abs urls
return $url;
if (C(".radaris") == 'trustoria.com')
return "https://radaris.com".$url;
if (HB::isRD()) {
if (empty($_SERVER['HTTP_REFERER']) || false !== strpos($_SERVER['HTTP_REFERER']??"", "radaris")) {
return $url;
}
}
return "https://".C(".radaris").$url;
}
/**
*
* @param string $phone
* @param bool $mask_phone
* @param bool $add_schema_org_markup
* @return string
*/
static function rdPhoneLink($phone, $mask_phone = false, $add_schema_org_markup = false) {
$clean_phone = Phone::clean($phone);
$link = "";
#$link = Phone::phoneProfileUrl($phone, true);
$phone_s = Phone::us($phone);
if ($mask_phone) {
$phone_s = Phone::mask_number($phone_s);
}
$phone_len = strlen($clean_phone);
if ($phone_len == 11 && $link) {
if ($add_schema_org_markup) {
return "<a href=\"$link\" itemprop=\"telephone\" class=\"ph\" rel=\"nofollow\">$phone_s</a>";
} else {
return "<a href=\"$link\" class=\"ph\" rel=\"nofollow\">$phone_s</a>";
}
} else {
// do not put schema.org markup on non-well formatted phones
return "<span class=\"ph\">$phone_s</span>";
}
}
/**
*
* @param int $phone
* @param bool $with_link
* @param bool $mask_phone
* @param bool $add_schema_org_markup https://schema.org/telephone
* @return string
*/
static function rdPhoneHTML($phone, $with_link = true, $mask_phone = false, $add_schema_org_markup = false) {
$ph = Phone::clean($phone);
if (strlen($ph) == 11 && $ph[0] == "1") {
$ph = substr($ph, 1);
}
$with_link = false;
if ($with_link) {
return self::rdPhoneLink($phone, $mask_phone, $add_schema_org_markup);
} else {
$phone_s = Phone::us($phone);
if ($mask_phone) {
$phone_s = Phone::mask_number($phone_s);
}
if ($add_schema_org_markup) {
return "<span itemprop=\"telephone\" class=\"ph\">$phone_s</span>";
} else {
return "<span class=\"ph\">$phone_s</span>";
}
}
}
// return Lines/Hits per second calculation once in $timeout
// Usage: if ($lps = HB::lps()) echo "LPS: $lps\n";
static function lps($timeout=10) { # LPS | false (timeout not reached)
static $time;
$hits = once("lps-calc", $timeout);
if (! $time)
$time = time();
if (! $hits || $hits === true) // init event
return false;
$time_diff = microtime(1) - $time;
if (! $time_diff)
return false;
$time = microtime(1);
return $hits / $time_diff;
}
/**
* !! Should be moved to GeoHelper or LOC related class (Ex: SH("Loc"))
*
* Render properly formatted PLAIN TEXT string from loc structure
* @param mixed $loc String, location hash or array of locations hashes
* @param bool $mask_address hide or not building number.
*
* @return string|mixed
*/
static function locToPlainText($loc, $mask_address = false, $one_line = true){ # full address string
if (!$loc) {
return "";
}
if (!is_array($loc)) {
return $loc;
}
if (!isset($loc["region"]) && isset($loc[0])) { //prevent tries to render buggy locs like ["region" => 19, "city" => 13 ... 0 => ["region" => 19, "city" => 13 ...]]
//array of locations
$superset = [];
foreach ($loc as $loc1) {
if ($tmp = self::locToPlainText($loc1, $mask_address)) {
$superset[] = $tmp;
}
}
return join("\n", $superset);
}
$parts = [];
if ($pobox = (int) ($loc["po-box"]??0)) {
$parts[] = "PO Box $pobox";
} elseif ($loc["str"]??0) {
$street = mb_convert_case(trim(NVL($loc['street2']??"", $loc['str'])), MB_CASE_TITLE, "utf-8");
if ($loc["pre-dir"]??0) {
$street = $loc["pre-dir"] . " " . $street;
}
if ($loc["post-dir"]??0) {
$street = $street . " " . $loc["post-dir"];
}
if ($loc["nn"]??0) {
$street = $loc["nn"] . " " . $street;
}
if ($loc["sec-nn"]??0) {
$sec_abbr = NVL(($loc["sec-abbr"]??''), "#");
if ("#" != $sec_abbr) {
$sec_abbr = $sec_abbr. " ";
}
$street = $street . " " . $sec_abbr . $loc["sec-nn"];
}
if ($mask_address) {
$parts[] = preg_replace("!^(\s*\d+\s)!i", "**** ", $street);
} else {
$parts[] = $street;
}
} elseif ($loc["street"]??0) {
$street = mb_convert_case(trim($loc['street']), MB_CASE_TITLE, "utf-8");
if ($mask_address) {
$parts[] = preg_replace("!^(\s*\d+\s)!i", "**** ", $street);
} else {
try {
} catch (UnexpectedValueException $e) {
//Bad/misspelled address
return "";
}
$parts[] = $street;
}
}
if ($loc["region"]??0) {
$tmp = "";
if (($loc["city"]??0) && !($loc["city_name"]??"")) {
$tmp = Geo::_city($loc["region"], $loc["city"]);
$tmp = $tmp[0]. cs(", %s", $tmp[2]);
} else {
$country = null;
if ($loc["region"]>255){
[$country] = Geo::name_path( $loc["region"] );
}
if ($loc["city_name"] ?? 0) {
$tmp = mb_convert_case($loc["city_name"], MB_CASE_TITLE, "utf-8");
if ($loc["state"] ?? 0) {
$tmp .= (", ".$loc["state"]);
} elseif($loc["region_name"] ?? 0) {
$tmp .= (", ".$loc["region_name"]);
}
if ($country) {
$tmp .= (", ". $country);
}
} elseif ($loc["county"] ?? 0){
if (!($tmp = $loc["region_name"]??0)) {
$tmp = Geo::region_name($loc["region"]);
}
if ($loc["county_name"] ?? 0){
$tmp = $loc["county_name"]. cs(", %s", $tmp);
} elseif ($county=Geo::county_name($loc["county"], $loc["region"])) {
$tmp = $county. cs(", %s", $tmp);
}
} else {
if ($country) {
$tmp=$country;
} else {
if (!$tmp = $loc["region_name"] ?? 0) {
$tmp = Geo::region_name($loc["region"]);
}
}
}
}
if ($loc["zip"] ?? 0) {
if ($zip = sprintf("%05d", $loc["zip"])) {/*.cs("-%04d", $loc["zip4"]) 29 Jan 2015 Dan said zip4 NEVER SHOULD APPEAR*/;
$tmp .= " $zip";
}
}
$parts[] = $tmp;
}
$s = "";
if ($parts) {
$separator = $one_line ? ', ' : '<br />';
$s = join($separator, $parts);
}
return $s;
}
/**
* Render properly formatted string from loc structure
* @param mixed $loc String, location hash or array of locations hashes
* @param bool $mask_address hide or not building number.
* @param int $f fname_id
* @param int $l $lname_id
* @param mixed $with_link
* - "whole_line" - put the link on whole address string. 13 Oct 2015 Dan requested to put link to whole address line at infobox of profile pages only.
* - "street_only" - put the link on the street part of address string. 13 Oct 2015 Dan requested to put link to street part at infobox of profile pages only.
* - other non empty value - add link as >>> after address line
* @param bool $add_schema_org_markup http://schema.org/address http://schema.org/PostalAddress
* @param bool $pretty_wrap - if true then address text parts are being wrapped into spans nowrap like <span>Address line 1,</span> <span>City, ST</span> <span>zip</span>
* @param string $link_class_name add to tag <a class
* @return string
*/
static function loc2str($loc, $mask_address = false, $f=0, $l=0, $with_link = true, $add_schema_org_markup = false, $pretty_wrap = false, $link_class_name=''){ # full address string
if ($add_schema_org_markup) {
$pretty_wrap = false;
}
if ("whole_line" === $with_link || "street_only" === $with_link) {
$add_schema_org_markup = false;
}
if (!$loc) {
return "";
}
if (!is_array($loc)) {
return $loc;
}
if (!isset($loc["region"]) && isset($loc[0])) { //prevent tries to render buggy locs like ["region" => 19, "city" => 13 ... 0 => ["region" => 19, "city" => 13 ...]]
//array of locations
$superset = [];
foreach ($loc as $loc1) {
if ($tmp = self::loc2str($loc1, $mask_address, $f, $l, $with_link, $add_schema_org_markup, $pretty_wrap)) {
$superset[] = $tmp;
}
}
return join("<br/>", $superset);
}
$zip = $loc["zip"] ?? null;
$street_link_url = "";
$street_link = "";
$parts = [];
if ($pobox = (int) ($loc["po-box"]??0)) {
$with_link = false;
if ($add_schema_org_markup) {
$parts[] = "PO Box <span itemprop=\"postOfficeBoxNumber\">$pobox</span>";
} elseif($pretty_wrap) {
$parts[] = "<span class='addr-wrap'>PO Box $pobox,</span>";
} else {
$parts[] = "PO Box $pobox";
}
} elseif ($loc["street"] ?? null) {
$street = mb_convert_case($loc['street'], MB_CASE_TITLE, "utf-8");
if ($mask_address) {
$parts[] = preg_replace("!^(\s*\d+\s)!i", "**** ", $street);
} else {
try {
$rc = Geo::rc($loc);
} catch (UnexpectedValueException $e) {
//Bad/misspelled address
return "";
}
if ($rc & 0xffff) {// have city
//IMPORTANT!!!! Street should be the last parameter
if ($f) {
$p["f"] = $f;
}
if ($l) {
$p["l"] = $l;
}
$p["r"] = $rc;
$p["s"] = $street;
if (preg_match('![P]\.?[O][\.\ ]BOX*!iU', $street)) {
$with_link = false;
}
if ($with_link) {
$absolute_part = "";
$absolute_part = "https://".RDSite::siteHost();
if ("whole_line" === $with_link) {
$street_link_url = url("$absolute_part/srv/a.address", $p);
} elseif ("street_only" === $with_link) {
$street = a("$absolute_part/srv/a.address", $p, $street, 'title="'.$street.'" class="addr_link" rel="nofollow"');
} else {
$street_link = a("$absolute_part/srv/a.address", $p, ">", 'title="'.$street.'" class="addr_link arrow" rel="nofollow"');
}
}
}
if ($add_schema_org_markup) {
$parts[] = "<span itemprop=\"streetAddress\">$street</span>";
} elseif ($pretty_wrap) {
$parts[] = "<span class='addr-wrap'>$street,</span>";
} else {
$parts[] = $street;
}
}
}
$zip_str = "";
if (!empty($zip)) {
if ($zip = sprintf("%05d", $loc["zip"])) {/*.cs("-%04d", $loc["zip4"]) 29 Jan 2015 Dan said zip4 NEVER SHOULD APPEAR*/;
if ($add_schema_org_markup) {
$zip_str .= " <span itemprop=\"postalCode\">$zip</span>";;
} elseif ($pretty_wrap) {
$zip_str .= " $zip";
} else {
$zip_str .= " $zip";
}
}
}
if (!empty($loc["region"])) {
$tmp = "";
if (!empty($loc["city"])) {
$tmp = Geo::_city($loc["region"], $loc["city"]);
if ($add_schema_org_markup) {
$tmp = "<span itemprop=\"addressLocality\">$tmp[0]</span>". cs(", <span itemprop=\"addressRegion\">%s</span>", $tmp[2]) . $zip_str;
} elseif ($pretty_wrap) {
$tmp = "<span class='addr-wrap'>" . $tmp[0]. cs(", %s", $tmp[2]??'') . $zip_str . "</span>";
} else {
$tmp = ($tmp[0]??''). cs(", %s", $tmp[2]??'') . $zip_str;
}
} else {
if($loc["region"]>255){
list($country)=Geo::name_path( $loc["region"] );
}
if (!empty($loc["city_name"])) {
if ($add_schema_org_markup) {
$tmp = "<span itemprop=\"addressLocality\">".mb_convert_case($loc["city_name"], MB_CASE_TITLE, "utf-8")."</span>";
if(!empty($loc["region_name"])){
$tmp .= ", <span itemprop=\"addressRegion\">".mb_convert_case($loc["region_name"], MB_CASE_TITLE, "utf-8")."</span>";
}
if(!empty($country)){
$tmp .= ", <span itemprop=\"addressCountry\">".mb_convert_case($country, MB_CASE_TITLE, "utf-8")."</span>";
}
} elseif ($pretty_wrap) {
$tmp = $loc["city_name"].(!empty($loc["region_name"])?", ".$loc["region_name"]:'').(!empty($country)?", ".$country:'');
$tmp = mb_convert_case($tmp, MB_CASE_TITLE, "utf-8");
$tmp = "<span class='addr-wrap'>" . $tmp . $zip_str . "</span>";
} else {
$tmp = $loc["city_name"].(!empty($loc["region_name"])?", ".$loc["region_name"]:'').(!empty($country)?", ".$country:'');
$tmp = mb_convert_case($tmp, MB_CASE_TITLE, "utf-8");
}
} else {
if(!empty($country)) {
$tmp=$country;
if ($add_schema_org_markup) {
$tmp = "<span itemprop=\"addressCountry\">$tmp</span>";
} elseif ($pretty_wrap) {
$tmp = "<span class='addr-wrap'>$tmp</span>";
}
} else {
$tmp = Geo::name($loc["region"]);
if ($add_schema_org_markup) {
$tmp = "<span itemprop=\"addressRegion\">$tmp</span>";
} elseif ($pretty_wrap) {
$tmp = "<span class='addr-wrap'>$tmp" . $zip_str . "</span>";
}
}
}
}
if ("whole_line" !== $with_link) {
$parts[] = "<span>$tmp".cs(" %s", $street_link)."</span>";
} else {
$parts[] = $tmp;
}
}
$s = "";
if ($parts) {
if ($pretty_wrap) {
$s = join(" ", $parts);
} else {
$s = join(", ", $parts);
}
if ("whole_line" === $with_link && !empty($street_link_url)) {
$_h = $link_class_name? ' class="'.$link_class_name.'"':'';
if ($pretty_wrap) {
$s = '<a href="'.$street_link_url.'" '.$_h.'>' . $s . '</a>';
} else {
$s = a($street_link_url, [], $s,$_h);
}
}
if ($add_schema_org_markup) {
$s = "<span class=\"addr-cont\" itemprop=\"address\" itemscope itemtype=\"http://schema.org/PostalAddress\">$s</span>";
} else {
$s = "<span class=\"addr-cont\">$s</span>";
}
}
return $s;
}
}