Steps for PHP to get the APK package information

Source: Internet
Author: User
Tags constant definition unpack
PHP How to get APK package information
Original address: http://www.jb51.net/article/53780.htm


This article mainly introduces PHP to get APK package information method, very practical features, the need for friends can refer to the next

Sometimes when uploading an Android APK package using PHP, we need to get the information inside the Android APK package, which is an example of how PHP gets the APK package information. The implementation method is as follows:
?
1
2
3
4
5
6
7
8
9
10
11
12
/* Parse the compressed XML file in the Android APK package to restore and read the XML content
Dependency function: Requires PHP's ZIP package function support. */
Include ('./apkparser.php ');
$APPOBJ = new Apkparser ();
$targetFile = Path address where a.apk;//apk is located
$res = $APPOBJ->open ($targetFile);
$APPOBJ->getappname (); App Name
$APPOBJ->getpackage (); App Package Name
$APPOBJ->getversionname (); Version name
$APPOBJ->getversioncode (); Version Code
?>
The following is the Apkparser class package, copy the following code to save as apkparser.php can execute the above code
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21st
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
60V
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
//-------------------------------
Apkparser Class Pack Start
//-------------------------------
Class apkparser{
//----------------------
Public function, for external invocation
//----------------------
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;
}
//----------------------
Type constant definition
//----------------------
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;
//----------------------
Internal private functions
//----------------------
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--;
}
}
}
Node not found
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 ' = ' '
);
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;
}
Handling Tag Strings
$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 '] = " ";
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 << 1)) + (1 <<) * ($v >> 31 | 1);
$exp = ($v >> & 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 End of Class pack
//---------------------
?>
Interested friends can debug run a sample of this article, I believe that the development of PHP program for everyone to bring some inspiration.
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.