Skip to content

Commit

Permalink
Inline STDIN.
Browse files Browse the repository at this point in the history
  • Loading branch information
seanmorris committed Oct 18, 2023
1 parent 5e16e50 commit b5fbf98
Show file tree
Hide file tree
Showing 21 changed files with 745 additions and 8,738 deletions.
260 changes: 63 additions & 197 deletions Makefile

Large diffs are not rendered by default.

19 changes: 16 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# php-wasm
# php-wasm

![php-wasm](https://img.shields.io/npm/v/php-wasm?color=609&label=php-wasm&style=for-the-badge)
[![Apache-2.0 Licence Badge](https://img.shields.io/npm/l/cv3-inject?logo=apache&color=338800&style=for-the-badge)](https://github.com/seanmorris/php-wasm/blob/master/LICENSE)
Expand Down Expand Up @@ -53,7 +53,20 @@ And run some PHP right in the page!
</script>
```
Thats it!
Inline php can use standard input, output and error with `data-` attributes. Just set the value of the attribute to a selector that will match that tag.
```html
<script async type = "text/javascript" src = "https://cdn.jsdelivr.net/npm/php-wasm/php-tags.mjs"></script>
<script id = "input" type = "text/plain">Hello, world!</script>
<script type = "text/php" data-stdin = "#input" data-stdout = "#output" data-stderr = "#error">
<?php echo file_get_contents('php://stdin');
</script>
<div id = "output"></div>
<div id = "error"></div>
```
Any NPM-enabled CDN will work:
Expand Down Expand Up @@ -268,4 +281,4 @@ Licensed under the Apache License, Version 2.0 (the "License"); you may not use
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
http://www.apache.org/licenses/LICENSE-2.0
http://www.apache.org/licenses/LICENSE-2.0
4 changes: 2 additions & 2 deletions bin/php-wasm.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const build = (flags, envName = 'web', buildType = 'js') => {

const subprocess = child_process.spawn(`make`, options, {
stdio: [ 'inherit', 'inherit', 'inherit' ],
cwd: __dirname,
cwd: __dirname + '/..',
});
};

Expand Down Expand Up @@ -120,7 +120,7 @@ const argsToFlags = args => {
}

flags[arg.substr(offset, index - offset)] = arg.substr(1 + index);

return
}))

Expand Down
116 changes: 100 additions & 16 deletions docs-source/app/initialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,93 @@ document.addEventListener('DOMContentLoaded', () => {
stdret.firstChild.remove();
}

console.log(code, persistBox.checked);
const code = `<?php
ini_set('session.save_path', '/home/web_user');
$stdErr = fopen('php://stderr', 'w');
$errors = [];
fwrite($stdErr, isset($_SESSION) && json_encode(['session' => $_SESSION]) . "\n");
register_shutdown_function(function() use($stdErr){
fwrite($stdErr, json_encode(['session_id' => session_id()]) . "\n");
fwrite($stdErr, json_encode(['headers'=>headers_list()]) . "\n");
fwrite($stdErr, json_encode(['errors' => error_get_last()]) . "\n");
fwrite($stdErr, json_encode(['session' => $_SESSION]) . "\n");
});
set_error_handler(function(...$args) use($stdErr, &$errors){
fwrite($stdErr, json_encode($args, JSON_PRETTY_PRINT) . "\n" );
});
$request = (object) json_decode(
'${ JSON.stringify({path, method, _GET, _POST}) }'
, JSON_OBJECT_AS_ARRAY
);
parse_str(substr($request->_GET, 1), $_GET);
$_POST = $request->_POST;
$origin = 'http://localhost:3333';
$docroot = '/preload/drupal-7.59';
$script = 'index.php';
$path = $request->path;
$path = preg_replace('/^\\/php-wasm/', '', $path);
$_SERVER['SERVER_SOFTWARE'] = ${JSON.stringify(navigator.userAgent)};
$_SERVER['REQUEST_URI'] = $path;
$_SERVER['REMOTE_ADDR'] = '127.0.0.1';
$_SERVER['SERVER_NAME'] = $origin;
$_SERVER['SERVER_PORT'] = 3333;
$_SERVER['REQUEST_METHOD'] = $request->method;
$_SERVER['SCRIPT_FILENAME'] = $docroot . '/' . $script;
$_SERVER['SCRIPT_NAME'] = $docroot . '/' . $script;
$_SERVER['PHP_SELF'] = $docroot . '/' . $script;
$_SERVER['DOCUMENT_ROOT'] = '/';
$_SERVER['HTTPS'] = '';
chdir($docroot);
define('DRUPAL_ROOT', getcwd());
require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
$uid = 1;
$user = user_load($uid);
$account = array('uid' => $user->uid);
user_login_submit(array(), $account);
$itemPath = $path;
$itemPath = preg_replace('/^\\/preload/', '', $itemPath);
$itemPath = preg_replace('/^\\/drupal-7.59/', '', $itemPath);
$itemPath = preg_replace('/^\\//', '', $itemPath);
if($itemPath && (substr($itemPath, 0, 4) !== 'node' || substr($itemPath, -4) === 'edit'))
{
$router_item = menu_get_item($itemPath);
$router_item['access_callback'] = true;
$router_item['access'] = true;
if ($router_item['include_file']) {
require_once DRUPAL_ROOT . '/' . $router_item['include_file'];
}
$page_callback_result = call_user_func_array(
$router_item['page_callback']
, is_string($router_item['page_arguments'])
? unserialize($router_item['page_arguments'])
: $router_item['page_arguments']
);
drupal_deliver_page($page_callback_result);
}
else
{
menu_execute_active_handler();
}`;

php.run(code).then(exitCode => {
exitLabel.innerText = exitCode;
Expand Down Expand Up @@ -302,6 +388,7 @@ document.addEventListener('DOMContentLoaded', () => {
const errorBuffer = [];

php.addEventListener('error', (event) => {
console.log(event.detail);
const content = event.detail.join('');
try{
const headers = JSON.parse(content);
Expand Down Expand Up @@ -341,26 +428,23 @@ document.addEventListener('DOMContentLoaded', () => {
{
}

for(const line of content)
{
errorBuffer.push(line);
errorBuffer.push(content);

setTimeout(()=>{
const chunk = errorBuffer.join('\n');
setTimeout(()=>{
const chunk = errorBuffer.join('\n');

if(!errorBuffer || !chunk)
{
return;
}
if(!errorBuffer || !chunk)
{
return;
}

const node = document.createTextNode(chunk);
const node = document.createTextNode(chunk);

stderr.append(node);
stderrFrame.srcdoc += chunk;
stderr.append(node);
stderrFrame.srcdoc += chunk;

while(errorBuffer.pop()){};
}, 500);
}
while(errorBuffer.pop()){};
}, 500);

});

Expand Down
4 changes: 2 additions & 2 deletions docs/DrupalWorker.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit b5fbf98

Please sign in to comment.