Use PHP functions in JavaScript

JavaScript popen

Execute a command and open either a read or a write pipe to it

1
2
3
4
56
7
8
9
1011
12
13
14
1516
17
18
19
2021
22
23
24
2526
27
28
29
3031
32
33
34
3536
37
38
39
4041
42
43
44
4546
47
48
49
5051
52
53
54
5556
57
58
59
6061
62
63
64
6566
67
68
69
7071
72
73
74
7576
77
78
79
8081
82
83
84
8586
87
88
89
9091
92
93
94
9596
97
98
99
100101
function popen (filename, mode, use_include_path, context) {
    // Execute a command and open either a read or a write pipe to it  
    // 
    // version: 1109.2015
    // discuss at: http://phpjs.org/functions/popen    // +   original by: Brett Zamir (http://brett-zamir.me)
    // +   input by: Paul Smith
    // +   bugfixed by: Brett Zamir (http://brett-zamir.me)
    // -    depends on: file_get_contents
    // *     example 1: popen('http://kevin.vanzonneveld.net/pj_test_supportfile_1.htm', 'r');    // *     returns 1: 'Resource id #1'
    var resource = {},
        i = 0,
        that = this;
    var getFuncName = function (fn) {        var name = (/\W*function\s+([\w\$]+)\s*\(/).exec(fn);
        if (!name) {
            return '(Anonymous)';
        }
        return name[1];    };
 
    // BEGIN file inclusion: file_get_contents
    var file_get_contents = function (url) {
        var req = that.window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();        if (!req) {
            throw new Error('XMLHttpRequest not supported');
        }
        if (!(/^http/).test(url)) { // Allow references within or below the same directory (should fix to allow other relative references or root reference; could make dependent on parse_url())
            url = that.window.location.href + '/' + url;        }
        req.open("GET", url, false);
        req.send(null);
        return req.responseText;
    };    // END file inclusion
 
    if (use_include_path === 1 || use_include_path === '1' || use_include_path === true) {
        // Not implemented yet: Search for file in include path too
    }    if (context) {
        // Not implemented yet, but could be useful to modify nature of HTTP request, etc.
    }
 
    for (i = 0; i < mode.length; i++) { // Have to deal with other flags if ever allow        switch (mode.charAt(i)) {
        case 'r':
            if (!mode.charAt(i + 1) || mode.charAt(i + 1) !== '+') {
                break;
            }        case 'w':
            // or 'w+'
        case 'a':
            // or 'a+'
        case 'x':            // or 'x+'
            throw 'Writing is not implemented';
        case 'b':
        case 't':
            throw 'Windows-only modes are not supported';        default:
            throw 'Unrecognized file mode passed to ' + getFuncName(arguments.caller) + '()';
        }
    }
     // BEGIN REDUNDANT
    this.php_js = this.php_js || {};
    this.php_js.resourceData = this.php_js.resourceData || {};
    this.php_js.resourceDataPointer = this.php_js.resourceDataPointer || {};
    this.php_js.resourceIdCounter = this.php_js.resourceIdCounter || 0;    // END REDUNDANT
 
    // BEGIN STATIC
 
    function PHPJS_Resource(type, id, opener) { // Can reuse the following for other resources, just changing the instantiation        // See http://php.net/manual/en/resource.php for types
        this.type = type;
        this.id = id;
        this.opener = opener;
    }    PHPJS_Resource.prototype.toString = function () {
        return 'Resource id #' + this.id;
    };
    PHPJS_Resource.prototype.get_resource_type = function () {
        return this.type;    };
    PHPJS_Resource.prototype.var_dump = function () {
        return 'resource(' + this.id + ') of type (' + this.type + ')';
    };
    // END STATIC 
    this.php_js.resourceIdCounter++;
 
    this.php_js.resourceData[this.php_js.resourceIdCounter] = this.file_get_contents(filename);
    this.php_js.resourceDataPointer[this.php_js.resourceIdCounter] = 0; 
    resource = new PHPJS_Resource('stream', this.php_js.resourceIdCounter, 'popen');
    resource.mode = mode; // Add file-specific attributes
 
    return resource; // may be 'file' instead of 'stream' type on some systems}
external links: original PHP docs | raw js source

Examples

Running

1
popen('http://kevin.vanzonneveld.net/pj_test_supportfile_1.htm', 'r');

Should return

1
'Resource id #1'

Dependencies

In order to use this function, you also need:

Open syntax issues

php.js uses JsLint to help us keep our code consistent and prevent some common bugs.

Eventually we want all code to pass or at least take into consideration most fixes suggested by JsLint, following this JsLint configuration we’ve decided on.


Authors

Thanks to the following developers, you get to have popen goodness in JavaScript.

Comments

Add Comment
Use:
[CODE]
your_stuff('here');
[/CODE]
for proper code formatting
By submitting code here you are allowing us to use it in php.js hence dual licensing it under the MIT and GPL licenses

No comments yet. Be the first!


Contribute a New function

More functions

In this category

basename
dirname
fclose
feof
fgetc
fgetcsv
fgets
fgetss
file
file_exists
file_get_contents
filemtime
filesize
fopen
fpassthru
fread
fscanf
fseek
ftell
pathinfo
pclose
» popen
readfile
realpath
rewind

Support us

spread the word:


Use any PHP function in JavaScript


These kind folks have already donated: AYHAN BARI*, Nikita Ekshiyan, Nikita Ekshiyan, Petr Pavel, @HalfWinter, Paulo Freitas, Andros Peña Romo, @andorosu, Raimund Szabo, Nitin Gupta, @nikosdion, Anonymous, Anonymous and Shawn Houser.
<your name here>

Click here to lend your support to: phpjs and make a donation at www.pledgie.com !