


How to automatically extract apk package information after php uploads apk (example download)_PHP tutorial
The first project after entering the company is to do marketing. So you need to upload APK software and the like in the background. For convenience, after uploading the APK, the system automatically extracts relevant information of the APK file, such as: apk package name, product name, version information, APK Code, program size, ICON, etc. initial treatment
Get the cmdAfter.xml file through the command: java -jar AXMLPrinter2.jar AndroidManifest.xml > cmdAfter.xml
, and then analyze the cmdAfter.xml file to obtain relevant information.
But unfortunately, the apk package name can be obtained from this file, but the ico icon file name and other related information cannot be obtained. As shown in the picture below
In the above picture, labels, icons, etc. are all flag values, and the required results cannot be obtained directly. I once analyzed the relationship between this value and the internal files of the APK file, but different APKs have different structures and it is too troublesome to implement. In fact, on some websites such as the Android Market, when you upload an APK, in addition to extracting the APK package name, it also includes ICON icon, size and other information. So since someone else can do it, I figured there must be a way around this. So after research, the expected results were obtained. I will record the method here and welcome exchanges.
Core extraction APK information code/***
* 分析已上传的APK文件,提取所需要的数据
*/
function upAPK(){
global $_config_product_apktool_count;//使用apktool.jar解压的次数,原因下面有说明。
if($this->msg!='')return;//如果有错误,返回
$dir=$this->upload_path;//上传路径
$stringsXML_exists=false;
if(file_exists($dir.'package/res/values/strings.xml'))unlink($dir.'package/res/values/strings.xml');
for($i=0;$i //针对UC的APK包或其类似的APK包,解压一次并不能完全得到strings.xml文件或相关文件。目前只有采用这个办法了。
//在系统cmd下直接使用java -jar ...执行解压,有时可以得到strings.xml文件,有时也得不到,不知道是不是jar包的问题。
exec('java -jar ../apktool.jar d -f '.$this->tmpFile.' '.$dir.'package');//注释:解压完毕再往下执行
$stringsXML_exists=file_exists($dir.'package/res/values/strings.xml');
}
//检查AndroidManifest.xml文件是否存在,如果不存在,则不是合法的APK文件
if(!file_exists($dir.'package/AndroidManifest.xml')){$this->msg='不是合法的APK文件,请重新上传!';return;}
$AndroidManifestXML=file_get_contents($dir.'package/AndroidManifest.xml');//读取AndroidManifest.xml
if(preg_match('/package=\"([^\"]*)\"/i',$AndroidManifestXML,$package))$returnVal['package']=$package[1];//如果有包名,返回到数组
//增加versionCode
if(preg_match('/versionCode=\"([^\"]*)\"/i',$AndroidManifestXML,$versionCode))$returnVal['versionCode']=$versionCode[1];//如果有版本代码,返回到数组
//检测到包名后判断数据库中是否已经存在。
if($this->id==0){//添加新产品时检测,修改产品不检测
if($returnVal['package']!=''){
$sql='select id from product where package='.SqlEncode($package[1]);
$result=mysql_query($sql);
if(mysql_num_rows($result)>0){
$this->msg='该APK已经存在,请更换!';
return;
}
}else{
$this->msg='系统无法检测该APK信息,请联系管理员!';
return;
}
}
if($stringsXML_exists)$stringXML=file_get_contents($dir.'package/res/values/strings.xml');//If there is strings.xml, read the strings.xml file
if(preg_match( '/versionName="([^"]*)"/i',$AndroidManifestXML,$ver))$returnVal['ver']=$ver[1];//If there is a version number, return it to the array
//There are currently two types of version numbers: 1. The version number is listed directly in AndroidManifest.xml; it can be extracted through the above regular rules
//2. The version number is the same as the label and is placed in strings.xml In the file
//2011-11-23 add
if($stringXML!='' && strstr($ver[1],'@')){
if(preg_match('/^@ string/(.*)/i',$ver[1],$findVer)){
If(preg_match('/
}
}
//// ////////////////////////////////////////
if(preg_match('/< ;application[sS]*? android:icon="@drawable/([^"]*)"/i',$AndroidManifestXML,$icon))$returnVal['thumbimg']=$icon[1];// If there is an icon, return to the array
if($stringsXML_exists && preg_match('/
$returnVal['name']=$name[1];//If there is a product name, return to the array
/**
Baidu: strings.xml
Special case 1:
*/
$returnVal[ 'name']=preg_replace('/s|"/','',$returnVal['name']);
}
}
//$this->msg=$returnVal[ 'package'].'--'.$returnVal['ver'].'--'.$returnVal['thumbimg'].'--'.$returnVal['name'];
if($ this->oldAPK!=''){//Re-upload will delete the original apk file and icon.png picture
unlink($dir.$this->oldAPK);
unlink($dir.$ this->oldAPK.'.png');
}
//Traverse the directories under the package/res directory [drawable|drawable-hdpi|drawable-nodpi|drawable-ldpi|drawable-mdpi]
//The system takes the icon with the largest icon size
$tmpArr[0]=0;$tmpArr[1]=0;$tmpArr[2]='drawable';
$dirs=opendir($dir. 'package/res');
while(($file=readdir($dirs))){
preg_match('/(drawable(-.*?dpi)?)/i',$file,$ drawable_folder);
$iconPath=$dir.'package/res/'.$drawable_folder[1].'/'.$returnVal['thumbimg'].'.png';
if(file_exists($ iconPath)){
$iconInfo=getimagesize($iconPath);
if($iconInfo[0]>$tmpArr[0] && $iconInfo[1]>$tmpArr[1]){
$tmpArr[0]=$iconInfo[0];$tmpArr[1]=$iconInfo[1];$tmpArr[2]=$drawable_folder[1]; 🎜> //$this->msg=$iconInfo[0].'---'.$iconInfo[1];
closedir($dirs);
if(rename($dir.'package /res/'.$tmpArr[2].'/'.$returnVal['thumbimg'].'.png',$dir.$this->iframe_key.'.apk.png')){//Found directory and successfully moved
$returnVal['thumbimg']=$this->iframe_key.'.apk.png';
}
if(!move_uploaded_file($this->tmpFile,$dir .$this->iframe_key.'.apk')){$this->msg='Upload failed! ';return;}//Transfer apk file
$returnVal['filename']=$this->iframe_key.'.apk';
$returnVal['size']=$this->size ;
$this->result=$returnVal;
}
Extraction information process
1. First, extract the package/res/values/string.xml file in the apk file through the apktool.jar command. For some unknown reason, when releasing the apk file, sometimes the string.xml file is not necessarily obtained. Therefore, the background adds: $_config_product_apktool_count parameter to control the maximum number of releases.
2. Read the AndroidManifest.xml file in the release root directory. From this file, you can get the APK package name and version information.
3. Check whether the package name exists in the database if it is a newly uploaded APK. It is forbidden to upload APKs with the same package name. Modification is not detected.
4. Obtain the required information through regular expressions.
Why do we need to extract the string.xml file here?
Because not all information is in AndroidManifest.xml. Some information is only used as a "reference" in AndroidManifest.xml, and the actual record is in string.xml. For example
The values of Label and icon in AndroidManifest.xml.
In the above picture: label="@string/app_name" indicates that the name attribute of string in string.xml is the value of app_name, which is the "software name" of the APK. Here is the "Market", as shown in the figure below Display:
@drawable/quickflick_icon, indicating that quickflick_icon is the file name of ICON.
Due to special needs, I need to find the largest ICON icon, see the code below:
//Traverse the directories under the package/res directory [drawable|drawable-hdpi|drawable-nodpi|drawable-ldpi|drawable-mdpi]
//The system takes the icon with the largest icon size
$tmpArr[0] =0;$tmpArr[1]=0;$tmpArr[2]='drawable';
$dirs=opendir($dir.'package/res');
while(($file=readdir( $dirs))){
preg_match('/(drawable(-.*?dpi)?)/i',$file,$drawable_folder);
$iconPath=$dir.'package/res/' .$drawable_folder[1].'/'.$returnVal['thumbimg'].'.png';
If(file_exists($iconPath)){
$iconInfo=getimagesize($iconPath);
If($iconInfo[0]>$tmpArr[0] && $iconInfo[1]>$tmpArr[1]){
$tmpArr[0]=$iconInfo[0];$tmpArr[1] =$iconInfo[1];$tmpArr[2]=$drawable_folder[1];
}
}
}
//$this->msg=$iconInfo[0].' ---'.$iconInfo[1];
closedir($dirs);
After analysis, ICON icons are generally stored in the following directories in APK: drawable|drawable-hdpi|drawable-nodpi|drawable-ldpi|drawable-mdpi. The largest ICON icon is obtained through traversal comparison and moved to the temporary Table of contents.
Save all the information that needs to be extracted into an array and write it to the form through javascript. As shown below:
Extract APK information summary
The above code, so far, can extract information normally from the uploaded APK, and no errors have been found. We can also see in the comments of the above code that information cannot be extracted about the "Pocket Baidu" APK because of its special processing method, namely:
In implementing this APK extraction function, the key is to find the organizational rules of the APK package. Only by finding the rules, program implementation is a natural thing.
Pay attention to the content when releasing the APK file
exec('java -jar ../apktool.jar d -f '.$this->tmpFile.' '.$dir.'package');
To successfully execute the above statement, the following conditions must be met:
1. Install the java package. For the java directory, the permissions of the users group are: read and run, list folder directories, and read
2. cmd.exe file, the permissions of the users user group are: read and run, read
3. PHP allows calling exec
4. Make sure the upload directory has permission to write files
If there is a better extraction method, welcome to communicate and learn from each other.
PHP extracts APK information DEMO demo download
Download address: http://xiazai.jb51.net/201304/yuanma/php_apk_jb51net.rar

PHPidentifiesauser'ssessionusingsessioncookiesandsessionIDs.1)Whensession_start()iscalled,PHPgeneratesauniquesessionIDstoredinacookienamedPHPSESSIDontheuser'sbrowser.2)ThisIDallowsPHPtoretrievesessiondatafromtheserver.

The security of PHP sessions can be achieved through the following measures: 1. Use session_regenerate_id() to regenerate the session ID when the user logs in or is an important operation. 2. Encrypt the transmission session ID through the HTTPS protocol. 3. Use session_save_path() to specify the secure directory to store session data and set permissions correctly.

PHPsessionfilesarestoredinthedirectoryspecifiedbysession.save_path,typically/tmponUnix-likesystemsorC:\Windows\TemponWindows.Tocustomizethis:1)Usesession_save_path()tosetacustomdirectory,ensuringit'swritable;2)Verifythecustomdirectoryexistsandiswrita

ToretrievedatafromaPHPsession,startthesessionwithsession_start()andaccessvariablesinthe$_SESSIONarray.Forexample:1)Startthesession:session_start().2)Retrievedata:$username=$_SESSION['username'];echo"Welcome,".$username;.Sessionsareserver-si

The steps to build an efficient shopping cart system using sessions include: 1) Understand the definition and function of the session. The session is a server-side storage mechanism used to maintain user status across requests; 2) Implement basic session management, such as adding products to the shopping cart; 3) Expand to advanced usage, supporting product quantity management and deletion; 4) Optimize performance and security, by persisting session data and using secure session identifiers.

The article explains how to create, implement, and use interfaces in PHP, focusing on their benefits for code organization and maintainability.

The article discusses the differences between crypt() and password_hash() in PHP for password hashing, focusing on their implementation, security, and suitability for modern web applications.

Article discusses preventing Cross-Site Scripting (XSS) in PHP through input validation, output encoding, and using tools like OWASP ESAPI and HTML Purifier.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 English version
Recommended: Win version, supports code prompts!

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment
