Download phpdroid: Developing Android Apps based on WebView and PHP built-in HTTP server

Source: Internet
Author: User
Tags php server posix nameserver
Personal on Ubuntu using cross-compilation toolchain Arm-none-linux-gnueabi follow droidphp tutorial

Built a PHP interpreter (Cli,cli-server) for Android (ARM architecture) and Raspberry Pi Raspbian (ARM architecture based Debian Linux distributions).

Download Address: Http://pan.baidu.com/s/1gfJcXbX
The content includes:
Main: This directory is the project source code, mainly Mainactivity.java and assets data.
phpdroid.apk: application example, approximately 7MB in size, contains PHP and BusyBox.

The point here is that BusyBox is not something PHP must have,
It's just to make it easier for PHP to invoke the Gnu/linux commands that are commonly used inside.
After removing the BusyBox apk package can also be reduced by 600KB.

It should be emphasized that PHP in the package is path-independent and does not require root permission to run.
As long as you maintain the assets/php/directory structure, it will work properly in your application.
The PHP version is 5.5.9.

phpdroid Basic Working principle:
Java launches the PHP built-in HTTP server and then opens a webview to access this PHP-powered HTTP service.
Among them, WebView is used to realize human-computer interaction, and can be programmed with traditional html/css/jquery technology in GUI.
PHP is responsible for interacting with the local file system, SQLite database, and the network.

It should be emphasized that phpdroid is not seeking to access the API provided by the Android system like the Java app.
The advantage of phpdroid is that Html/css/js/php/sql can develop WebView-based local webapp with traditional web development techniques.
Phpdroid's built-in native PHP does not have access to Android's APIs for Java, but it can manipulate the local file system (app directory) and SQLite and network interactions.
For example, get a news list, WebView access the local php,php via Ajax, and then access the remote server via curl.
The remote server returns JSON, which contains news headlines, summaries, thumbnail URLs, and a local PHP-to-WebView after the loop output.
Visible this local PHP is both the server side of the WebView and the client of the remote server, is the WebView and remote server data interchange.
Think of WebView and native PHP as a whole, it's a local webapp that can't call the Android API.
After all, Android is the Linux kernel, the idea of everything is still there,
As long as you have permission, PHP reads some system data (such as/proc/cpuinfo) and does not have a problem.

Phpdroid Detailed working principle:
Phpdroid/app/src/main/java/net/php/phpdroid/mainactivity.java
Mainactivity replication on oncreate first boot:
/data/app/net.php.phpdroid.apk/assets/php/
To:
/data/data/net.php.phpdroid/php/

Then Runtime.getruntime (). exec executes the PHP service startup script:
/data/data/net.php.phpdroid/php/bin/start.sh
#!/system/bin/sh
chmod $1/php/bin/busybox
chmod $1/php/bin/php
$1/php/bin/php $1/php/bin/ua.php #随机生成UserAgent
$1/php/bin/php $1/php/bin/port.php #获取可用端口
$1/php/bin/php \
-C $1/php/bin/php.ini \
-D app_dir= "$" \
-D upload_tmp_dir= "$1/php/tmp" \
-D session.save_path= "$1/php/tmp" \
-S 127.0.0.2: ' Cat $1/php/bin/port ' \
-T $1/php/www \
$1/php/bin/auth.php \
>/dev/null 2>&1 &
Echo $! > $1/php/bin/pid #记录PHP的PID
return 0
The purpose of this script is to randomly generate a useragent to mark WebView, get the available ports on the 127.0.0.2,
Then start the PHP server and record its PID, which is used to close the kill.
For an introduction to PHP's built-in HTTP server, see:
Https://wiki.php.net/rfc/builtinwebserver

which
/data/data/net.php.phpdroid/php/bin/ua.php
File_put_contents (DirName (__file__). ' /ua ', SHA1 (Uniqid (Mt_rand (), true));

/data/data/net.php.phpdroid/php/bin/port.php
PHP uses Fsockopen to detect if a port is occupied and returns an available port.
$port = 8181;
while ($fp = @fsockopen (' 127.0.0.2 ', $port, $errno, $ERRSTR, 1)) {
Fclose ($FP);
$port + +;
}
File_put_contents (DirName (__file__). ' /port ', $port);

/data/data/net.php.phpdroid/php/bin/auth.php
Triggers PHP service process to open resolv_php.conf, requires resolv_php.conf to be in a directory with auth.php
gethostbyname (' localhost ');
$ua = file_get_contents (dirname (__file__). ' /ua ');
if (Isset ($_server[' http_user_agent ')) && $_server[' http_user_agent ']=== $ua) {
return false;
} else {
Exit (' Auth Failed ');
}
The PHP service executes the auth.php file before processing each request.
If UA (useragent) does not match, the program exits with exit.
Android previous app for one user, each app directory only allows the app to access the user,
So unless the phone is rooted, other apps can't read the data in the Phpdroid app directory.
You can access the PHP service by reading the UA file in the Mainactivity.java and setting it to WebView useragent.
Other apps on your phone, like browsers, because you don't have access to other app directories such as/data/data/net.php.phpdroid,
You will not be able to read the UA generated by phpdroid, and you will not be able to access PHP services.
Mainactivity.java
Webview.getsettings (). setuseragentstring (UA);
Webview.loadurl ("http://127.0.0.2:" + port);

With regard to DNS resolution, glibc default access is/etc/resolv.conf
#define _PATH_RESCONF "/etc/resolv.conf"
When compiling the glibc, I changed to a relative path:
#define _PATH_RESCONF "./resolv_php.conf"

/data/data/net.php.phpdroid/php/bin/resolv_php.conf
# Baidu Public DNS http://dudns.baidu.com/
NameServer 180.76.76.76
# cnnic Public DNS http://www.sdns.cn/
NameServer 1.2.4.8
NameServer 210.2.4.8
A static link to the GLIBC Library of PHP, in the execution of auth.php in the gethostbyname (' localhost ') operation,
Will trigger access to the resolv_php.conf in the directory where auth.php is located, for DNS.

A better approach would be to call Android's Getprop net.dns1 to get the local DNS and then add it to resolv_php.conf.
But strangely, executing getprop net.dns1 in the ADB shell can output correctly,
A set of Echo shell_exec (' Getprop net.dns1 ') in PHP; There is no output.
Perform echo shell_exec (' Vmstat '); Calls to other commands are output normally.
here do not know what is the problem, authority problem?

As for the compilation of GLIBC, I also changed the/bin/sh of the command to Android/system/bin/sh,
In this way, PHP shell_exec functions to function properly.
Sed-i "s{/bin/sh{/system/bin/sh{"./libio/oldiopopen.c
Sed-i "s{/bin/sh{/system/bin/sh{"./libio/iopopen.c
Sed-i "s{/bin/sh{/system/bin/sh{"./posix/tst-vfork3.c
Sed-i "s{/bin/sh{/system/bin/sh{"./posix/bug-regex9.c
Sed-i "s{/bin/sh{/system/bin/sh{"./sysdeps/posix/system.c
Sed-i "s{/bin/sh{/system/bin/sh{"./sysdeps/generic/paths.h
Sed-i "s{/bin/sh{/system/bin/sh{"./sysdeps/unix/sysv/linux/paths.h
The Proc_open function in PHP has to be similarly modified:
Sed-i "s{/bin/sh{/system/bin/sh{" ext/standard/proc_open.c
This allows PHP to happily invoke the Gnu/linux commands available in Android and BusyBox.


Mainactivity when OnKeyDown Press the back key Keycode_back exit the app:
Will call stop.sh to close the PHP service, stop.sh content as follows:
#!/system/bin/sh
Ua=$1/php/bin/ua
If [-R $ua]; Then
RM $ua
Fi
Port=$1/php/bin/port
If [-R $port]; Then
RM $port
Fi
Pid=$1/php/bin/pid
If [-R $pid]; Then
Kill ' Cat $pid '
RM $pid
Fi
return 0
is to erase the Ua,port two files and close the PHP process.
In fact, Mainactivity will also call stop.sh at startup to clean up what was left by the last application that might have unexpectedly exited.
  • Related Article

    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.