You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

102 lines
2.9 KiB
PHTML

<?php
namespace Base\Tool;
use \ZipArchive;
/**
* 用于读取plist文件信息
* @author elf<360197197@qq.com>
*/
class PlistParser
{
const PREG_INFO_PLIST = "/^Payload.*?\.app\/Info.plist$/";
private $xml;
public function openFromIpa($ipaFile, $preg)
{
$zip = new ZipArchive;
if ($zip->open($ipaFile) === true) {
$index = -1;
for( $i = 0; $i < $zip->numFiles; $i++ ){
$stat = $zip->statIndex($i);
if (preg_match($preg, $stat['name'], $matches)) {
$index = $stat['index'];
}
}
$content = $zip->getFromIndex($index);
$zip->close();
if ($content){
$xml = new \DOMDocument();
$xml->loadXML($content);
$this->xml = $xml;
return true;
}
}
die();
return false;
}
public function open($xmlFile)
{
$xml = new \DOMDocument();
$xml->load($xmlFile);
$this->xml = $xml;
return true;
}
public function getResult()
{
$plistTag = $this->xml->getElementsByTagName('plist');
$dict = $plistTag->item(0)->childNodes->item(1);
return $this->parseDictNode($dict);
}
public function parseDictNode($parentNode)
{
$lastKey = '';
$dict = [];
foreach ($parentNode->childNodes as $node) {
if ($node instanceof \DOMElement) {
if ($node->nodeName == 'key') {
$lastKey = $node->textContent;
} else {
if ($node->nodeName == 'dict') {
$dict[$lastKey] = $this->parseDictNode($node);
} elseif ($node->nodeName == 'array') {
$dict[$lastKey] = $this->parseArrayNode($node);
} elseif($node->nodeName == 'true') {
$dict[$lastKey] = true;
} elseif($node->nodeName == 'false') {
$dict[$lastKey] = false;
} else {
$dict[$lastKey] = $node->textContent;
}
}
}
}
return $dict;
}
public function parseArrayNode($parentNode)
{
$list = [];
foreach ($parentNode->childNodes as $node) {
if ($node instanceof \DOMElement) {
if ($node->nodeName == 'dict') {
$list[] = $this->parseDictNode($node);
} elseif ($node->nodeName == 'array') {
$list[] = $this->parseArrayNode($node);
} elseif($node->nodeName == 'true') {
$list[] = true;
} elseif($node->nodeName == 'false') {
$list[] = false;
} else {
$list[] = $node->textContent;
}
}
}
return $list;
}
}