using System.IO.Compression;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace JiDangJia.Contracts;
public sealed record LegalDocument(string Key,string Title,long Version,string Content,string ContentSha256,string PublishedAt,string PublishedBy,string ChangeNote,string SourceFileName,string ExternalUrl="");
public sealed record LegalPolicies(LegalDocument UserAgreement,LegalDocument PrivacyPolicy,LegalDocument ChinaMobile)
{
public bool Matches(LegalConsent? consent)=>consent is {UserAgreementAccepted:true,PrivacyPolicyAccepted:true,ChinaMobileAccepted:true}
&&consent.UserAgreementVersion==UserAgreement.Version&&consent.PrivacyPolicyVersion==PrivacyPolicy.Version&&consent.ChinaMobileVersion==ChinaMobile.Version;
public LegalConsent Consent(bool userAgreement,bool privacyPolicy,bool chinaMobile)=>new(UserAgreement.Version,PrivacyPolicy.Version,ChinaMobile.Version,userAgreement,privacyPolicy,chinaMobile);
}
public sealed record LegalConsent(long UserAgreementVersion,long PrivacyPolicyVersion,long ChinaMobileVersion,bool UserAgreementAccepted,bool PrivacyPolicyAccepted,bool ChinaMobileAccepted,string DeviceId="");
public sealed record PublishLegalRequest(long ExpectedVersion,string Title,string Content,string ChangeNote,string SourceFileName="");
public sealed record LegalVersionInfo(string Key,string Title,long Version,string ContentSha256,string PublishedAt,string ChangeNote,string SourceFileName);
public sealed record LegalAcceptanceStatus(bool Current,long UserAgreementVersion,long PrivacyPolicyVersion,long ChinaMobileVersion,string AcceptedAt);
public static class LegalPolicyRules
{
public const string UserAgreement="USER_AGREEMENT",PrivacyPolicy="PRIVACY_POLICY",ChinaMobile="CHINA_MOBILE";
public const string ChinaMobileUrl="https://wap.cmpassport.com/resources/html/contract.html";
public const int MaxCharacters=60000,MaxImportBytes=2*1024*1024;
public const string ChinaMobileNotice="中国移动认证服务条款由中国移动提供,请打开官方网页阅读完整条款及其隐私政策。当前版本尚未接入中国移动一键登录或号码认证,不会因勾选本条款而调用中国移动认证 SDK、获取本机号码或向其传输认证数据。将来使用该认证功能时,会另行告知实际处理的数据并取得所需授权;本条款仅在使用中国移动认证服务时适用,不能替代该环节的单独授权。官方网页内容以中国移动发布的内容为准。";
public static bool Editable(string key)=>key is UserAgreement or PrivacyPolicy;
public static bool AllAccepted(LegalConsent? consent)=>consent is {UserAgreementAccepted:true,PrivacyPolicyAccepted:true,ChinaMobileAccepted:true};
public static string Normalize(string value)=>(value??"").Trim('\uFEFF',' ','\t','\r','\n').Replace("\r\n","\n").Replace('\r','\n');
}
// Import plain text only. DOCX scripts, links, attachments and macros are never executed.
public static class LegalDocumentImport
{
public static string Read(byte[] bytes,string fileName)
{
if(bytes.Length is 0 or >LegalPolicyRules.MaxImportBytes)throw new ArgumentException("协议文件需为 1 字节至 2 MB");
var extension=Path.GetExtension(fileName).ToLowerInvariant();string text;
if(extension is ".txt" or ".md")
{
text=new UTF8Encoding(false,true).GetString(bytes);
if(text.Contains('\0'))throw new ArgumentException("文本文件请使用 UTF-8 编码");
}
else if(extension==".docx")
{
using var memory=new MemoryStream(bytes);using var zip=new ZipArchive(memory,ZipArchiveMode.Read);
if(zip.Entries.Count>500)throw new ArgumentException("Word 文件内容过多");
var document=zip.GetEntry("word/document.xml")??throw new ArgumentException("Word 文件缺少正文");
if(document.Length>1024*1024)throw new ArgumentException("Word 正文超过限制");
using var stream=document.Open();using var reader=XmlReader.Create(stream,new XmlReaderSettings{DtdProcessing=DtdProcessing.Prohibit,XmlResolver=null,MaxCharactersInDocument=1024*1024});
var xml=XDocument.Load(reader);XNamespace w="http://schemas.openxmlformats.org/wordprocessingml/2006/main";
var body=xml.Root?.Element(w+"body")??throw new ArgumentException("Word 文件缺少正文");
text=string.Join("\n",body.Descendants(w+"p").Select(p=>string.Concat(p.Descendants().Select(n=>n.Name==w+"t"?n.Value:n.Name==w+"br"?"\n":n.Name==w+"tab"?"\t":""))));
}
else throw new ArgumentException("请选择 UTF-8 的 TXT、Markdown 或 DOCX 文件");
text=LegalPolicyRules.Normalize(text);
if(text.Length is <100 or >LegalPolicyRules.MaxCharacters)throw new ArgumentException("协议正文需为 100–60000 字符");
return text;
}
}
using System.IO.Compression;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace JiDangJia.Contracts;
public sealed record LegalDocument(string Key,string Title,long Version,string Content,string ContentSha256,string PublishedAt,string PublishedBy,string ChangeNote,string SourceFileName,string ExternalUrl="");
public sealed record LegalPolicies(LegalDocument UserAgreement,LegalDocument PrivacyPolicy,LegalDocument ChinaMobile)
{
public bool Matches(LegalConsent? consent)=>consent is {UserAgreementAccepted:true,PrivacyPolicyAccepted:true,ChinaMobileAccepted:true}
&&consent.UserAgreementVersion==UserAgreement.Version&&consent.PrivacyPolicyVersion==PrivacyPolicy.Version&&consent.ChinaMobileVersion==ChinaMobile.Version;
public LegalConsent Consent(bool userAgreement,bool privacyPolicy,bool chinaMobile)=>new(UserAgreement.Version,PrivacyPolicy.Version,ChinaMobile.Version,userAgreement,privacyPolicy,chinaMobile);
}
public sealed record LegalConsent(long UserAgreementVersion,long PrivacyPolicyVersion,long ChinaMobileVersion,bool UserAgreementAccepted,bool PrivacyPolicyAccepted,bool ChinaMobileAccepted,string DeviceId="");
public sealed record PublishLegalRequest(long ExpectedVersion,string Title,string Content,string ChangeNote,string SourceFileName="");
public sealed record LegalVersionInfo(string Key,string Title,long Version,string ContentSha256,string PublishedAt,string ChangeNote,string SourceFileName);
public sealed record LegalAcceptanceStatus(bool Current,long UserAgreementVersion,long PrivacyPolicyVersion,long ChinaMobileVersion,string AcceptedAt);
public static class LegalPolicyRules
{
public const string UserAgreement="USER_AGREEMENT",PrivacyPolicy="PRIVACY_POLICY",ChinaMobile="CHINA_MOBILE";
public const string ChinaMobileUrl="https://wap.cmpassport.com/resources/html/contract.html";
public const int MaxCharacters=60000,MaxImportBytes=2*1024*1024;
public const string ChinaMobileNotice="中国移动认证服务条款由中国移动提供,请打开官方网页阅读完整条款及其隐私政策。当前版本尚未接入中国移动一键登录或号码认证,不会因勾选本条款而调用中国移动认证 SDK、获取本机号码或向其传输认证数据。将来使用该认证功能时,会另行告知实际处理的数据并取得所需授权;本条款仅在使用中国移动认证服务时适用,不能替代该环节的单独授权。官方网页内容以中国移动发布的内容为准。";
public static bool Editable(string key)=>key is UserAgreement or PrivacyPolicy;
public static bool AllAccepted(LegalConsent? consent)=>consent is {UserAgreementAccepted:true,PrivacyPolicyAccepted:true,ChinaMobileAccepted:true};
public static string Normalize(string value)=>(value??"").Trim('\uFEFF',' ','\t','\r','\n').Replace("\r\n","\n").Replace('\r','\n');
}
// Import plain text only. DOCX scripts, links, attachments and macros are never executed.
public static class LegalDocumentImport
{
public static string Read(byte[] bytes,string fileName)
{
if(bytes.Length is 0 or >LegalPolicyRules.MaxImportBytes)throw new ArgumentException("协议文件需为 1 字节至 2 MB");
var extension=Path.GetExtension(fileName).ToLowerInvariant();string text;
if(extension is ".txt" or ".md")
{
text=new UTF8Encoding(false,true).GetString(bytes);
if(text.Contains('\0'))throw new ArgumentException("文本文件请使用 UTF-8 编码");
}
else if(extension==".docx")
{
using var memory=new MemoryStream(bytes);using var zip=new ZipArchive(memory,ZipArchiveMode.Read);
if(zip.Entries.Count>500)throw new ArgumentException("Word 文件内容过多");
var document=zip.GetEntry("word/document.xml")??throw new ArgumentException("Word 文件缺少正文");
if(document.Length>1024*1024)throw new ArgumentException("Word 正文超过限制");
using var stream=document.Open();using var reader=XmlReader.Create(stream,new XmlReaderSettings{DtdProcessing=DtdProcessing.Prohibit,XmlResolver=null,MaxCharactersInDocument=1024*1024});
var xml=XDocument.Load(reader);XNamespace w="http://schemas.openxmlformats.org/wordprocessingml/2006/main";
var body=xml.Root?.Element(w+"body")??throw new ArgumentException("Word 文件缺少正文");
text=string.Join("\n",body.Descendants(w+"p").Select(p=>string.Concat(p.Descendants().Select(n=>n.Name==w+"t"?n.Value:n.Name==w+"br"?"\n":n.Name==w+"tab"?"\t":""))));
}
else throw new ArgumentException("请选择 UTF-8 的 TXT、Markdown 或 DOCX 文件");
text=LegalPolicyRules.Normalize(text);
if(text.Length is <100 or >LegalPolicyRules.MaxCharacters)throw new ArgumentException("协议正文需为 100–60000 字符");
return text;
}
}