2020-08-31 01:27:08 +08:00
|
|
|
<?php
|
2020-10-29 18:17:25 +08:00
|
|
|
declare(strict_types=1);
|
2020-08-31 01:27:08 +08:00
|
|
|
|
|
|
|
|
|
2021-08-11 01:04:57 +08:00
|
|
|
namespace Kiri\Core;
|
2020-08-31 01:27:08 +08:00
|
|
|
|
|
|
|
|
|
2020-12-17 14:09:14 +08:00
|
|
|
use Exception;
|
2020-12-17 14:12:44 +08:00
|
|
|
|
2020-12-17 14:09:14 +08:00
|
|
|
|
2020-08-31 01:27:08 +08:00
|
|
|
/**
|
|
|
|
|
* Class Help
|
2021-08-11 01:04:57 +08:00
|
|
|
* @package Kiri\Kiri\Core
|
2020-08-31 01:27:08 +08:00
|
|
|
*/
|
|
|
|
|
class Help
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param array $data
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2021-07-21 16:26:30 +08:00
|
|
|
public static function toXml(array $data): string
|
2020-08-31 01:27:08 +08:00
|
|
|
{
|
|
|
|
|
$xml = "<xml>";
|
|
|
|
|
foreach ($data as $key => $val) {
|
2021-02-08 16:52:27 +08:00
|
|
|
if (is_array($val)) {
|
|
|
|
|
$xml .= "<" . $key . ">" . static::xmlChild($val) . "</" . $key . ">";
|
|
|
|
|
} else if (is_numeric($val)) {
|
2020-08-31 01:27:08 +08:00
|
|
|
$xml .= "<" . $key . ">" . $val . "</" . $key . ">";
|
|
|
|
|
} else {
|
|
|
|
|
$xml .= "<" . $key . "><![CDATA[" . $val . "]]></" . $key . ">";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$xml .= "</xml>";
|
|
|
|
|
return $xml;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-02-08 16:52:27 +08:00
|
|
|
/**
|
|
|
|
|
* @param array $array
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
private static function xmlChild(array $array): string
|
|
|
|
|
{
|
|
|
|
|
$string = '';
|
|
|
|
|
foreach ($array as $key => $value) {
|
|
|
|
|
if (is_array($value)) {
|
|
|
|
|
$string .= static::xmlChild($value);
|
|
|
|
|
} else if (is_numeric($value)) {
|
|
|
|
|
$string .= "<" . $key . ">" . $value . "</" . $key . ">";
|
|
|
|
|
} else {
|
|
|
|
|
$string .= "<" . $key . "><![CDATA[" . $value . "]]></" . $key . ">";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return $string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2020-08-31 01:27:08 +08:00
|
|
|
/**
|
|
|
|
|
* @param $xml
|
2020-12-17 14:09:14 +08:00
|
|
|
* @return mixed
|
2022-01-04 17:27:37 +08:00
|
|
|
* @throws Exception
|
2020-08-31 01:27:08 +08:00
|
|
|
*/
|
2020-12-17 14:09:14 +08:00
|
|
|
public static function toArray($xml): mixed
|
2020-08-31 01:27:08 +08:00
|
|
|
{
|
|
|
|
|
if (empty($xml)) {
|
2021-05-08 11:15:17 +08:00
|
|
|
return [];
|
2020-08-31 01:27:08 +08:00
|
|
|
} else if (is_array($xml)) {
|
|
|
|
|
return $xml;
|
|
|
|
|
}
|
2020-11-18 15:00:40 +08:00
|
|
|
if (!($_xml = Xml::isXml($xml))) {
|
2020-11-18 14:59:22 +08:00
|
|
|
return static::jsonToArray($xml);
|
2020-08-31 01:27:08 +08:00
|
|
|
}
|
2020-11-18 15:00:40 +08:00
|
|
|
return $_xml;
|
2020-08-31 01:27:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2020-12-17 14:09:14 +08:00
|
|
|
/**
|
|
|
|
|
* @param $xml
|
|
|
|
|
* @return mixed
|
|
|
|
|
*/
|
|
|
|
|
public static function jsonToArray($xml): mixed
|
2020-08-31 01:27:08 +08:00
|
|
|
{
|
|
|
|
|
$_xml = json_decode($xml, true);
|
|
|
|
|
if (is_null($_xml)) {
|
2020-11-19 10:25:34 +08:00
|
|
|
return [];
|
2020-08-31 01:27:08 +08:00
|
|
|
}
|
|
|
|
|
return $_xml;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $xml
|
|
|
|
|
* @return mixed
|
|
|
|
|
*/
|
2020-12-17 14:09:14 +08:00
|
|
|
public static function xmlToArray($xml): mixed
|
2020-08-31 01:27:08 +08:00
|
|
|
{
|
|
|
|
|
if (is_array($xml)) {
|
|
|
|
|
return $xml;
|
|
|
|
|
}
|
|
|
|
|
if (($data = @simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)) !== false) {
|
|
|
|
|
return json_decode(json_encode($data), TRUE);
|
|
|
|
|
}
|
|
|
|
|
if (!is_null($json = json_decode($xml, TRUE))) {
|
|
|
|
|
return $json;
|
|
|
|
|
}
|
|
|
|
|
return $xml;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $parameter
|
|
|
|
|
* @return array|false|string
|
2020-12-17 14:09:14 +08:00
|
|
|
* @throws Exception
|
2020-08-31 01:27:08 +08:00
|
|
|
*/
|
2020-12-17 14:09:14 +08:00
|
|
|
public static function toString($parameter): bool|array|string
|
2020-08-31 01:27:08 +08:00
|
|
|
{
|
|
|
|
|
if (!is_string($parameter)) {
|
|
|
|
|
$parameter = ArrayAccess::toArray($parameter);
|
|
|
|
|
if (is_array($parameter)) {
|
2020-12-24 11:12:23 +08:00
|
|
|
$parameter = Json::encode($parameter);
|
2020-08-31 01:27:08 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return $parameter;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param mixed $json
|
2020-12-17 14:09:14 +08:00
|
|
|
* @return bool|string
|
2020-08-31 01:27:08 +08:00
|
|
|
*/
|
2020-12-17 14:09:14 +08:00
|
|
|
public static function toJson(mixed $json): bool|string
|
2020-08-31 01:27:08 +08:00
|
|
|
{
|
|
|
|
|
if (is_object($json)) {
|
|
|
|
|
$json = get_object_vars($json);
|
|
|
|
|
}
|
|
|
|
|
if (is_array($json)) {
|
|
|
|
|
return json_encode($json, JSON_UNESCAPED_UNICODE);
|
|
|
|
|
}
|
|
|
|
|
$matchQuote = '/(<\?xml.*?\?>)?<([a-zA-Z_]+)>(<([a-zA-Z_]+)><!.*?><\/\4>)+<\/\2>/';
|
|
|
|
|
if (preg_match($matchQuote, $json)) {
|
|
|
|
|
$json = self::xmlToArray($json);
|
|
|
|
|
} else {
|
|
|
|
|
$json = json_decode($json, true);
|
|
|
|
|
}
|
|
|
|
|
if (!is_array($json)) {
|
|
|
|
|
$json = [];
|
|
|
|
|
}
|
|
|
|
|
return json_encode($json, JSON_UNESCAPED_UNICODE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param int $length
|
|
|
|
|
* @return string
|
|
|
|
|
*
|
|
|
|
|
* 随机字符串
|
|
|
|
|
*/
|
2020-12-17 14:09:14 +08:00
|
|
|
public static function random($length = 20): string
|
2020-08-31 01:27:08 +08:00
|
|
|
{
|
|
|
|
|
$res = [];
|
|
|
|
|
$str = 'abcdefghijklmnopqrstuvwxyz';
|
|
|
|
|
$str .= strtoupper($str) . '1234567890';
|
|
|
|
|
for ($i = 0; $i < $length; $i++) {
|
|
|
|
|
$rand = substr($str, rand(0, strlen($str) - 2), 1);
|
|
|
|
|
if (empty($rand)) {
|
|
|
|
|
$rand = substr($str, strlen($str) - 3, 1);
|
|
|
|
|
}
|
|
|
|
|
array_push($res, $rand);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return implode($res);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param array $array
|
|
|
|
|
* @param $key
|
2021-07-21 16:26:30 +08:00
|
|
|
* @param string $type
|
2020-08-31 01:27:08 +08:00
|
|
|
* @return string
|
|
|
|
|
*/
|
2021-07-21 16:26:30 +08:00
|
|
|
public static function sign(array $array, $key, string $type = 'MD5'): string
|
2020-08-31 01:27:08 +08:00
|
|
|
{
|
|
|
|
|
ksort($array, SORT_ASC);
|
|
|
|
|
$string = [];
|
|
|
|
|
foreach ($array as $hashKey => $val) {
|
|
|
|
|
if (empty($val)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
$string[] = $hashKey . '=' . $val;
|
|
|
|
|
}
|
|
|
|
|
$string[] = 'key=' . $key;
|
|
|
|
|
$string = implode('&', $string);
|
|
|
|
|
if ($type == 'MD5') {
|
|
|
|
|
return strtoupper(md5($string));
|
|
|
|
|
} else {
|
|
|
|
|
return hash('sha256', $string);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-21 16:26:30 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $email
|
|
|
|
|
* @param string $Subject
|
|
|
|
|
* @param $messageContent
|
|
|
|
|
*/
|
|
|
|
|
public static function sendEmail($email, string $Subject, $messageContent)
|
|
|
|
|
{
|
2021-12-23 18:22:27 +08:00
|
|
|
if (!class_exists('\Swift_Mailer')) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-07-21 16:26:30 +08:00
|
|
|
$mailer = new \Swift_Mailer((new \Swift_SmtpTransport($email['host'], $email['port']))
|
|
|
|
|
->setUsername($email['username'])->setPassword($email['password']));
|
|
|
|
|
$message = (new \Swift_Message($Subject))
|
|
|
|
|
->setFrom([$email['send']['address'] => $email['send']['nickname']])
|
|
|
|
|
->setBody('Here is the message itself');
|
|
|
|
|
|
|
|
|
|
foreach ($email['receive'] as $item) {
|
|
|
|
|
$message->setTo([$item['address'], $item['address'] => $item['nickname']]);
|
|
|
|
|
}
|
|
|
|
|
$mailer->send($messageContent);
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-31 01:27:08 +08:00
|
|
|
}
|