php获取apk包信息的方法

有时候在使用php上传安卓apk包的时候,我们需要获取安卓apk包内的信息,本文以实例形式讲述了php获取apk包信息的方法。具体实现方法如下:

<?php
/*解析安卓apk包中的压缩XML文件,还原和读取XML内容
依赖功能:需要PHP的ZIP包函数支持。*/
include('./Apkparser.php');
$appObj  = new Apkparser();
$targetFile = a.apk;//apk所在的路径地址
$res   = $appObj->open($targetFile);
$appObj->getAppName();     // 应用名称
$appObj->getPackage();    // 应用包名
$appObj->getVersionName();  // 版本名称
$appObj->getVersionCode();  // 版本代码
?>

以下是Apkparser类包,把以下代码复制出来保存为Apkparser.php就可以执行以上代码

<?php
//-------------------------------
//Apkparser类包开始
//-------------------------------
class ApkParser{
//----------------------
// 公共函数,供外部调用
//----------------------
  public function open($apk_file, $xml_file='AndroidManifest.xml'){
    $zip = new \ZipArchive;
    if ($zip->open($apk_file) === TRUE) {
      $xml = $zip->getFromName($xml_file);
      $zip->close();
      if ($xml){
        try {
          return $this->parseString($xml);
        }catch (Exception $e){
        }
      }
    }
    return false;
  }
  public function parseString($xml){
    $this->xml = $xml;
    $this->length = strlen($xml);
    $this->root = $this->parseBlock(self::AXML_FILE);
    return true;
  }
  public function getXML($node=NULL, $lv=-1){
    if ($lv == -1) $node = $this->root;
    if (!$node) return '';
    if ($node['type'] == self::END_TAG) $lv--;
    $xml = @($node['line'] == 0 || $node['line'] == $this->line) ? '' : "\n".str_repeat(' ', $lv);
    $xml .= $node['tag'];
    $this->line = @$node['line'];
    foreach ($node['child'] as $c){
      $xml .= $this->getXML($c, $lv+1);
    }
    return $xml;
  }
  public function getPackage(){
    return $this->getAttribute('manifest', 'package');
  }
  public function getVersionName(){
    return $this->getAttribute('manifest', 'android:versionName');
  }
  public function getVersionCode(){
    return $this->getAttribute('manifest', 'android:versionCode');
  }
  public function getAppName(){
    return $this->getAttribute('manifest/application', 'android:name');
  }
  public function getMainActivity(){
    for ($id=0; true; $id++){
      $act = $this->getAttribute("manifest/application/activity[{$id}]/intent-filter/action", 'android:name');
      if (!$act) break;
      if ($act == 'android.intent.action.MAIN') return $this->getActivity($id);
    }
    return NULL;
  }
  public function getActivity($idx=0){
    $idx = intval($idx);
    return $this->getAttribute("manifest/application/activity[{$idx}]", 'android:name');
  }
  public function getAttribute($path, $name){
    $r = $this->getElement($path);
    if (is_null($r)) return NULL;
    if (isset($r['attrs'])){
      foreach ($r['attrs'] as $a){
        if ($a['ns_name'] == $name) return $this->getAttributeValue($a);
      }
    }
    return NULL;
  }
//----------------------
// 类型常量定义
//----------------------
  const AXML_FILE       = 0x00080003;
  const STRING_BLOCK     = 0x001C0001;
  const RESOURCEIDS      = 0x00080180;
  const START_NAMESPACE    = 0x00100100;
  const END_NAMESPACE     = 0x00100101;
  const START_TAG       = 0x00100102;
  const END_TAG        = 0x00100103;
  const TEXT         = 0x00100104;
  const TYPE_NULL       =0;
  const TYPE_REFERENCE    =1;
  const TYPE_ATTRIBUTE    =2;
  const TYPE_STRING      =3;
  const TYPE_FLOAT      =4;
  const TYPE_DIMENSION    =5;
  const TYPE_FRACTION     =6;
  const TYPE_INT_DEC     =16;
  const TYPE_INT_HEX     =17;
  const TYPE_INT_BOOLEAN   =18;
  const TYPE_INT_COLOR_ARGB8 =28;
  const TYPE_INT_COLOR_RGB8  =29;
  const TYPE_INT_COLOR_ARGB4 =30;
  const TYPE_INT_COLOR_RGB4  =31;
  const UNIT_MASK       = 15;
  private static $RADIX_MULTS = array(0.00390625, 3.051758E-005, 1.192093E-007, 4.656613E-010);
  private static $DIMENSION_UNITS = array("px","dip","sp","pt","in","mm","","");
  private static $FRACTION_UNITS = array("%","%p","","","","","","");
  private $xml='';
  private $length = 0;
  private $stringCount = 0;
  private $styleCount = 0;
  private $stringTab = array();
  private $styleTab = array();
  private $resourceIDs = array();
  private $ns = array();
  private $cur_ns = NULL;
  private $root = NULL;
  private $line = 0;
//----------------------
// 内部私有函数
//----------------------
  private function getElement($path){
    if (!$this->root) return NULL;
    $ps = explode('/', $path);
    $r = $this->root;
    foreach ($ps as $v){
      if (preg_match('/([^\[]+)\[([0-9]+)\]$/', $v, $ms)){
        $v = $ms[1];
        $off = $ms[2];
      }else {
        $off = 0;
      }
      foreach ($r['child'] as $c){
        if ($c['type'] == self::START_TAG && $c['ns_name'] == $v){
          if ($off == 0){
            $r = $c; continue 2;
          }else {
            $off--;
          }
        }
      }
      // 没有找到节点
      return NULL;
    }
    return $r;
  }
  private function parseBlock($need = 0){
    $o = 0;
    $type = $this->get32($o);
    if ($need && $type != $need) throw new Exception('Block Type Error', 1);
    $size = $this->get32($o);
    if ($size < 8 || $size > $this->length) throw new Exception('Block Size Error', 2);
    $left = $this->length - $size;
    $props = false;
    switch ($type){
      case self::AXML_FILE:
        $props = array(
          'line' => 0,
          'tag' => '<?xml version="1.0" encoding="utf-8"?>'
        );
      break;
      case self::STRING_BLOCK:
        $this->stringCount = $this->get32($o);
        $this->styleCount = $this->get32($o);
        $o += 4;
        $strOffset = $this->get32($o);
        $styOffset = $this->get32($o);
        $strListOffset = $this->get32array($o, $this->stringCount);
        $styListOffset = $this->get32array($o, $this->styleCount);
        $this->stringTab = $this->stringCount > 0 ? $this->getStringTab($strOffset, $strListOffset) : array();
        $this->styleTab = $this->styleCount > 0 ? $this->getStringTab($styOffset, $styListOffset) : array();
        $o = $size;
      break;
      case self::RESOURCEIDS:
        $count = $size / 4 - 2;
        $this->resourceIDs = $this->get32array($o, $count);
      break;
      case self::START_NAMESPACE:
        $o += 8;
        $prefix = $this->get32($o);
        $uri = $this->get32($o);
        if (empty($this->cur_ns)){
          $this->cur_ns = array();
          $this->ns[] = &$this->cur_ns;
        }
        $this->cur_ns[$uri] = $prefix;
      break;
      case self::END_NAMESPACE:
        $o += 8;
        $prefix = $this->get32($o);
        $uri = $this->get32($o);
        if (empty($this->cur_ns)) break;
        unset($this->cur_ns[$uri]);
      break;
      case self::START_TAG:
        $line = $this->get32($o);
        $o += 4;
        $attrs = array();
        $props = array(
          'line' => $line,
          'ns' => $this->getNameSpace($this->get32($o)),
          'name' => $this->getString($this->get32($o)),
          'flag' => $this->get32($o),
          'count' => $this->get16($o),
          'id' => $this->get16($o)-1,
          'class' => $this->get16($o)-1,
          'style' => $this->get16($o)-1,
          'attrs' => &$attrs
        );
        $props['ns_name'] = $props['ns'].$props['name'];
        for ($i=0; $i < $props['count']; $i++){
          $a = array(
            'ns' => $this->getNameSpace($this->get32($o)),
            'name' => $this->getString($this->get32($o)),
            'val_str' => $this->get32($o),
            'val_type' => $this->get32($o),
            'val_data' => $this->get32($o)
          );
          $a['ns_name'] = $a['ns'].$a['name'];
          $a['val_type'] >>= 24;
          $attrs[] = $a;
        }
        // 处理TAG字符串
        $tag = "<{$props['ns_name']}";
        foreach ($this->cur_ns as $uri => $prefix){
          $uri = $this->getString($uri);
          $prefix = $this->getString($prefix);
          $tag .= " xmlns:{$prefix}=\"{$uri}\"";
        }
        foreach ($props['attrs'] as $a){
          $tag .= " {$a['ns_name']}=\"".
              $this->getAttributeValue($a).
              '"';
        }
        $tag .= '>';
        $props['tag'] = $tag;
        unset($this->cur_ns);
        $this->cur_ns = array();
        $this->ns[] = &$this->cur_ns;
        $left = -1;
      break;
      case self::END_TAG:
        $line = $this->get32($o);
        $o += 4;
        $props = array(
          'line' => $line,
          'ns' => $this->getNameSpace($this->get32($o)),
          'name' => $this->getString($this->get32($o))
        );
        $props['ns_name'] = $props['ns'].$props['name'];
        $props['tag'] = "</{$props['ns_name']}>";
        if (count($this->ns) > 1){
          array_pop($this->ns);
          unset($this->cur_ns);
          $this->cur_ns = array_pop($this->ns);
          $this->ns[] = &$this->cur_ns;
        }
      break;
      case self::TEXT:
        $o += 8;
        $props = array(
          'tag' => $this->getString($this->get32($o))
        );
        $o += 8;
      break;
      default:
        throw new Exception('Block Type Error', 3);
      break;
    }
    $this->skip($o);
    $child = array();
    while ($this->length > $left){
      $c = $this->parseBlock();
      if ($props && $c) $child[] = $c;
      if ($left == -1 && $c['type'] == self::END_TAG){
        $left = $this->length;
        break;
      }
    }
    if ($this->length != $left) throw new Exception('Block Overflow Error', 4);
    if ($props){
      $props['type'] = $type;
      $props['size'] = $size;
      $props['child'] = $child;
      return $props;
    }else {
      return false;
    }
  }
  private function getAttributeValue($a){
    $type = &$a['val_type'];
    $data = &$a['val_data'];
    switch ($type){
      case self::TYPE_STRING:
        return $this->getString($a['val_str']);
      case self::TYPE_ATTRIBUTE:
        return sprintf('?%s%08X', self::_getPackage($data), $data);
      case self::TYPE_REFERENCE:
        return sprintf('@%s%08X', self::_getPackage($data), $data);
      case self::TYPE_INT_HEX:
        return sprintf('0x%08X', $data);
      case self::TYPE_INT_BOOLEAN:
        return ($data != 0 ? 'true' : 'false');
      case self::TYPE_INT_COLOR_ARGB8:
      case self::TYPE_INT_COLOR_RGB8:
      case self::TYPE_INT_COLOR_ARGB4:
      case self::TYPE_INT_COLOR_RGB4:
        return sprintf('#%08X', $data);
      case self::TYPE_DIMENSION:
        return $this->_complexToFloat($data).self::$DIMENSION_UNITS[$data & self::UNIT_MASK];
      case self::TYPE_FRACTION:
        return $this->_complexToFloat($data).self::$FRACTION_UNITS[$data & self::UNIT_MASK];
      case self::TYPE_FLOAT:
        return $this->_int2float($data);
    }
    if ($type >=self::TYPE_INT_DEC && $type < self::TYPE_INT_COLOR_ARGB8){
      return (string)$data;
    }
    return sprintf('<0x%X, type 0x%02X>', $data, $type);
  }
  private function _complexToFloat($data){
    return (float)($data & 0xFFFFFF00) * self::$RADIX_MULTS[($data>>4) & 3];
  }
  private function _int2float($v) {
    $x = ($v & ((1 << 23) - 1)) + (1 << 23) * ($v >> 31 | 1);
    $exp = ($v >> 23 & 0xFF) - 127;
    return $x * pow(2, $exp - 23);
  }
  private static function _getPackage($data){
    return ($data >> 24 == 1) ? 'android:' : '';
  }
  private function getStringTab($base, $list){
    $tab = array();
    foreach ($list as $off){
      $off += $base;
      $len = $this->get16($off);
      $mask = ($len >> 0x8) & 0xFF;
      $len = $len & 0xFF;
      if ($len == $mask){
        if ($off + $len > $this->length) throw new Exception('String Table Overflow', 11);
        $tab[] = substr($this->xml, $off, $len);
      }else {
        if ($off + $len * 2 > $this->length) throw new Exception('String Table Overflow', 11);
        $str = substr($this->xml, $off, $len * 2);
        $tab[] = mb_convert_encoding($str, 'UTF-8', 'UCS-2LE');
      }
    }
    return $tab;
  }
  private function getString($id){
    if ($id > -1 && $id < $this->stringCount){
      return $this->stringTab[$id];
    }else {
      return '';
    }
  }
  private function getNameSpace($uri){
    for ($i=count($this->ns); $i > 0; ){
      $ns = $this->ns[--$i];
      if (isset($ns[$uri])){
        $ns = $this->getString($ns[$uri]);
        if (!empty($ns)) $ns .= ':';
        return $ns;
      }
    }
    return '';
  }
  private function get32(&$off){
    $int = unpack('V', substr($this->xml, $off, 4));
    $off += 4;
    return array_shift($int);
  }
  private function get32array(&$off, $size){
    if ($size <= 0) return NULL;
    $arr = unpack('V*', substr($this->xml, $off, 4 * $size));
    if (count($arr) != $size) throw new Exception('Array Size Error', 10);
    $off += 4 * $size;
    return $arr;
  }
  private function get16(&$off){
    $int = unpack('v', substr($this->xml, $off, 2));
    $off += 2;
    return array_shift($int);
  }
  private function skip($size){
    $this->xml = substr($this->xml, $size);
    $this->length -= $size;
  }
}
//---------------------
//Apkparser类包结束
//---------------------
?>

感兴趣的朋友可以调试运行一下本文实例,相信会对大家的php程序开发带来一定的启发。

(0)

相关推荐

  • 查看apk签名信息方法

    用shell写了一个查看apk签名的脚本.代码很少也很简单 复制代码 代码如下: #!/bin/bash mkdir .temp_for_certificate cd .temp_for_certificate count=0 while [ -n "$1" ] do count=$[$count+1] echo "(#$count) "`basename "$1"`":" echo "" path=`ja

  • Android实现检查并下载APK更新、安装APK及获取网络信息的方法

    本文所述实例为一个天气预报中的android代码,主要包括了下载和安装APK.检查Apk更新.显示'已经是最新'或者'无法获取版本信息'对话框.获取当前客户端版本信息.显示版本更新通知对话框.显示下载对话框.判断是否挂载了SD卡.显示文件大小格式:2个小数点显示等.具体实现代码如下: import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileOutputStream; import java.io.I

  • java获取手机已安装APK的签名摘要

    将APK发布到应用商店或接入第三方的SDK时,有时需要提供APK的签名摘要信息,可以通过摘要算法MD5或SHA-1来获取签名的摘要,除了获取自己的APK签名,也可以获取手机上其他已安装的APK签名,只需要传入其他APK的包名即可. private static final char[] HEX_CHAR = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; /** 获取签名的MD

  • Android中获取apk安装包信息的方法

    一.获取安装包信息 复制代码 代码如下: /** * 获取apk包的信息:版本号,名称,图标等 * @param absPath apk包的绝对路径 * @param context  */  public void apkInfo(String absPath,Context context) { PackageManager pm = context.getPackageManager();      PackageInfo pkgInfo = pm.getPackageArchiveInf

  • unity3d发布apk在android虚拟机中运行的详细步骤(unity3d导出android apk)

    unity3d发布apk在android虚拟机中运行的详细步骤(unity3d导出android apk),总的流程分为以下6个步骤: 1.安装java_jdk 2.配置java环境变量 3.更新android的sdk 4.从Unity3d中发布出apk文件 5.创建android虚拟机并运行 6.将apk文件安装到android虚拟机中 (为方便新手,在下面对每个步骤的具体操作及可能遇到的问题详细提一下) 1.安装java_jdk 官网(www.java.com),免费,我安装的文件的名字是j

  • 使用android-apktool来逆向(反编译)APK包方法介绍

    谷歌官方提供了apktool可以逆向已经发布出去的APK应用,即反编译已经打包成功的APK文件,使用它可以将其反编译成非常接近打包前的原始格式,对于APK来说,可以具体的逆向AndroidManifest.xml.资源文件resources.arsc以及将dex文件反编译成可以调试的smali文件. Warnning 但apktool并不等于是可以用来侵犯前作者的作品的工具,所以使用apktool工具的用户千万不用用其来进行不正当.非法的使用. It is NOT intended for pi

  • jarsigner重新签名apk无法安装的解决方法

    因为一些原因,要给未签名的apk重新签名,需要用到jarsigner工具,但是签名之后,apk无法安装,百思不得其解.终于找到一个解决方案,现在分享给大家: 在签名时,要加上2个参数: 复制代码 代码如下: -digestalg SHA1 -sigalg MD5withRSA 造成无法签名错误的原因,初步估计是JDK版本的问题.JDK 1.6签名没事,但是1.7会出这个问题. 完整的签名命令如下: 复制代码 代码如下: jarsigner -verbose -keystore feelyou.k

  • 教你如何使用platform密钥对apk进行签名

    1.进入<Android_Source_Path>/build/target/product/security,找到[platform.pk8]和[platform.x509.pem]系统密钥. 2.进入<Android_Source_Path>/build/tools/signapk找到SignApk.java,运行javac编译成SignApk.class 3.执行命令java com.android.signapk.SignApk platform.x509.pem plat

  • Android快速分析apk工具aapt的使用教程

    前面walfred已经介绍了使用apktool对apk进行逆向编译,通过apktool我们的确可以反编译已经序列化后的AndroidManifest.xml和资源文件等等,但是有没有一种快速有效的工具在不解包的前提下可以获得某一apk的基本内容.权限以及该apk的基本信息呢?答案就是有,这个工具就是今天要介绍的aapt. aapt简介 aapt即Android Asset Packaging Tool,我们可以在SDK的platform-tools目录下找到该工具.aapt可以查看. 创建. 更

  • 使用python编写脚本获取手机当前应用apk的信息

    前提是已设置ANDROID_HOME环境变量,使用aapt工具获取apk的信息,保存至脚本所在目录下的PackageInfo.txt文件中: import os import tempfile import re tempFile = tempfile.gettempdir() def get_aapt(): if "ANDROID_HOME" in os.environ: rootDir = os.path.join(os.environ["ANDROID_HOME&quo

随机推荐