public function testDynCSS() { $vm = new \V8Js('PHP', array(), array()); \V8Js::registerExtension('browser_test', file_get_contents(MODULE_PATH . '/rz_root/module/lib/dyncss/js/browserEmulator.js')); \V8Js::registerExtension('absurdhat_test', file_get_contents(MODULE_PATH . '/rz_root/assets/notlive/dyncss/absurdhat.js')); \V8Js::registerExtension('absurd_test', file_get_contents(MODULE_PATH . '/rz_root/assets/notlive/dyncss/absurd.js')); $vm = new \V8Js('PHP', array(), array('browser_test', 'absurdhat_test', 'absurd_test')); $code = file_get_contents(MODULE_PATH . '/rz_root/assets/notlive/dyncss/dyncss.js'); $vm->executeString($code, 'dyncss_TEST'); }
/** * Executes Javascript using V8Js * * @param string $js JS code to be executed * @return string The execution response */ public function executeJs($js) { $this->initV8(); ob_start(); try { $this->v8->executeString($js); } catch (\V8JsScriptException $e) { ob_end_clean(); throw $e; } return ob_get_clean(); }
class LineProcessor { protected $_processor; public function readLineLoop() { $fh = fopen('php://stdin', 'r'); $p = $this->_processor; while ($line = fgets($fh)) { echo $p($line); } } public function setProcessor($p) { $this->_processor = $p; } } $v8 = new V8Js(); $v8->lp = new LineProcessor(); $v8->startDebugAgent('LineProcessor', 9222, V8Js::DEBUG_AUTO_BREAK_NEVER); $JS = <<<EOT print("Hello LineProcessor User!\\n"); PHP.lp.setProcessor(function (foo) { \treturn foo.toUpperCase(); }); PHP.lp.readLineLoop(); EOT; $v8->executeString($JS, 'processor.js');
$obj = new Foo(); $a->arr = array("foobar" => $obj); $JS = <<<'EOF' var example = new Object; example.foo = function () { print("this is foo"); } example.bar = function () { print("this is bar"); } example.__noSuchMethod__ = function (id, args) { print("tried to handle unknown method " + id); if (args.length != 0) print("it had arguments: " + args); } example.foo(); // alerts "this is foo" example.bar(); // alerts "this is bar" example.grill(); // alerts "tried to handle unknown method grill" example.ding("dong"); // alerts "tried to handle unknown method ding" EOF; try { $a->executeString("var myarr = new Array(); myarr[0] = 'foo'; myarr[1] = 'bar'; var_dump(myarr); var_dump(new Date('September 8, 1975 09:00:00'))", "call_test1.js"); $a->executeString("var_dump(PHP.arr.foobar.bar);", "call_test2.js"); $a->executeString("var_dump(PHP.arr.foobar.bar[0]);", "call_test3.js"); $a->executeString("var_dump(var_dump(PHP.arr));", "call_test4.js"); $a->executeString("var patt1=/[^a-h]/g; var_dump(patt1);", "call_test5.js"); $a->executeString("var_dump(Math.PI, Infinity, null, undefined);", "call_test6.js"); // $a->executeString($JS); } catch (V8JsScriptException $e) { echo $e->getMessage(), "\n"; }
function GetPictures($uri) { $html = http_proxy_get($uri, "template/xxbh", 10); $html = str_replace("text/html; charset=gb2312", "text/html; charset=gb18030", $html); $xpath = new XPath($html); $scripts = $xpath->query("/html/head/script", null); if ($scripts->length > 1) { $script = $scripts->item(1)->nodeValue; $js = new V8Js(); $js->executeString($script, "xxbh", V8Js::FLAG_FORCE_ARRAY); $base64 = $js->executeString("qTcms_S_m_murl_e;", "xxbh", V8Js::FLAG_FORCE_ARRAY); $url = base64_decode($base64); $urls = explode('$qingtiandy$', $url); return $urls; } else { return array(); } }
/** * @param $reactFunction * @param $componentPath * @param null $props * @return string * @throws \Exception */ private function render($reactFunction, $componentPath, $props = null) { $javascript = $this->getJavaScript($reactFunction, $componentPath, $props); $markup = ''; try { ob_start(); $markup = $this->v8->executeString($javascript); $loggableErrors = ob_get_clean(); if ($loggableErrors) { $this->log("Errors in v8 javascript execution", ["errors" => $loggableErrors]); } if (!is_string($markup)) { throw new RuntimeException("Value returned from v8 executeString isn't a string"); } } catch (\Exception $e) { $this->log($e->getMessage()); if ($reactFunction === 'renderComponentToStaticMarkup') { throw $e; } } return $markup; }
/** * Get markup string * If an error occurs, the error handler will be executed if exists, won't do anything otherwise * @return string */ public function markup() { $react = $this->react_prefix . 'React'; $component = $this->component; $code = $this->src; $code .= "var componentFactory = {$react}.createFactory({$component});"; $code .= sprintf("{$react}.renderToString(componentFactory(%s));", json_encode($this->data)); try { return $this->v8->executeString($code); } catch (\Exception $e) { if (is_callable($this->errorHandler)) { call_user_func($this->errorHandler, $e->getMessage(), $code); } return ''; } }
/** * @param string $js * @return null|string */ private function executeJS($js) { $jsString = null; try { ob_start(); $this->v8->executeString($js); $jsString = ob_get_clean(); } catch (V8JsException $e) { if ($this->errorHandler) { call_user_func($this->errorHandler, $e); } // default error handler blows up bad echo "<pre>"; echo $e->getMessage(); echo "</pre>"; die; } return $jsString; }
<?php $a = new V8Js(); $a->func = function ($a) { echo "Closure..\n"; }; try { $a->executeString("print(PHP.func); PHP.func(1);", "closure_test.js"); $a->executeString("print(PHP.func); PHP.func(1);", "closure_test.js"); } catch (V8JsScriptException $e) { echo $e->getMessage(), "\n"; }
/** * */ protected function executeScripts($page) { preg_match_all('/<script[^>]+src=("|\')(.+?)\\1/i', $page->html, $matches); if ($urls = $matches[2]) { foreach ($urls as $url) { if (!preg_match('/^https?:\\/\\//', $url)) { $url = $page->url . '/' . $url; } try { $result = $this->curl($url); } catch (WappalyzerException $e) { if ($this->debug) { echo $e->getMessage() . "\n"; } continue; } $v8 = new V8Js(); try { $v8->executeString(' var document = {}, window = { document: document } ; '); $v8->executeString($result->html, $url); $result = $v8->executeString('Object.keys(window);'); var_dump($result); } catch (V8JsException $e) { if ($this->debug) { echo "\n", print_r($e->getJsTrace()), "\n\n"; } continue; } } } }
<?php $a = new V8Js(); var_dump($a->executeString('Jst.write = function(s) { html += "EI TOIMI"; };' . "\n" . ' Jst.evaluate("lol testi <%= 1 %>", {});'));
<?php $a = new V8Js(); // Should not work with closure $a->test = function ($params) { return method_exists($params, 'cb1') ? $params->cb1("hello") : false; }; $ret = $a->executeString('PHP.test(function (foo) { return foo + " world"; });'); var_dump(__LINE__, $ret); // Test is_a() $a->test = function ($params) { return is_a($params, 'V8Object') ? $params->cb1("hello") : false; }; $ret = $a->executeString('PHP.test({ "cb1" : function (foo) { return foo + " world"; } });'); var_dump(__LINE__, $ret); // Test is_a() $a->test = function ($params) { return is_a($params, 'V8Function') ? $params("hello") : false; }; $ret = $a->executeString('PHP.test(function (foo) { return foo + " world"; });'); var_dump(__LINE__, $ret); // Should not work with object $a->test = function ($params) { return is_a($params, 'Closure') ? $params("hello") : false; }; $ret = $a->executeString('PHP.test({ "cb1" : function (foo) { return foo + " world"; } });'); var_dump(__LINE__, $ret); $a->test = function ($params) { var_dump($params); return $params->cb1("hello"); };
{ echo "Called __invoke(): "; var_dump(func_get_args()); return 'foobar'; } function __toString() { echo "Called __tostring: "; return $this->bar; } } $blaa = new V8Js(); $blaa->obj = new Foo(); try { echo "__invoke()\n"; $blaa->executeString("var_dump(PHP.obj('arg1','arg2','arg3'));", "invoke_test1 #1.js"); echo "------------\n"; echo " __invoke() with new\n"; $blaa->executeString("myobj = new PHP.obj('arg1','arg2','arg3'); var_dump(myobj);", "invoke_test2 #2.js"); echo "------------\n"; echo " __tostring()\n"; $blaa->executeString('print(PHP.obj + "\\n");', "tostring_test #3.js"); echo "------------\n"; echo " __isset() not called with existing property\n"; $blaa->executeString('if ("bar" in PHP.obj) print("bar exists\\n");', "isset_test1 #4.js"); echo "------------\n"; echo " __isset() with non-existing property\n"; $blaa->executeString('if (!("foobar" in PHP.obj)) print("foobar does not exist\\n"); else print("We called __isset and it said yes!\\n");', "isset_test2 #5.js"); echo "------------\n"; echo " __get() not called with existing property\n"; $blaa->executeString('var_dump(PHP.obj.bar);', "get_test1 #6.js");
/** * {@inheritdoc} */ public function evalJs($code) { return $this->v8->executeString($code); }
<?php $v8 = new V8Js(); $v8->startDebugAgent('LineProcessor', 9222, V8Js::DEBUG_AUTO_BREAK_ALWAYS); $JS = <<<EOT print("Hello LineProcessor User!\\n"); function processLine(foo) { return foo.toUpperCase(); }; EOT; $v8->executeString($JS, 'processor.js'); $fh = fopen('php://stdin', 'r'); while ($line = fgets($fh)) { echo $v8->executeString('processLine(' . json_encode($line) . ');'); }
<?php $v8 = new V8Js(); /* basic.js */ $JS = <<<EOT len = print('Hello' + ' ' + 'World!' + "\\n"); len; EOT; try { var_dump($v8->executeString($JS, 'basic.js')); } catch (V8JsException $e) { var_dump($e); }
<?php $a = new V8Js(); try { var_dump($a->executeString("date = new Date('September 8, 1975 09:00:00'); print(date + '\\n'); date;", "test.js")); } catch (V8JsScriptException $e) { echo $e->getMessage(), "\n"; }
<?php class Foo { var $bar = "bar"; function bar($what) { echo "I'm a ", $what, "!\n"; } } $foo = new Foo(); echo $foo->bar, "\n"; $foo->bar("function"); $v8 = new V8Js(); $v8->foo = new Foo(); $v8->executeString('print(PHP.foo.$bar, "\\n");'); $v8->executeString('PHP.foo.__call("bar", ["function"])'); // // Hello, v8 // class Foo { // var $bar = null; // } // // $v8 = new V8Js(); // $v8->foo = new Foo; // $v8->executeString('print( "bar" in PHP.foo ? "yes" : "no" );');
// Test class class Testing { public $foo = 'ORIGINAL'; private $my_private = 'arf'; // Should not show in JS side protected $my_protected = 'argh'; // Should not show in JS side function mytest($a, $b, $c = NULL) { var_dump(func_get_args()); } } $a = new V8Js(); $a->myobj = new Testing(); $a->executeString("PHP.myobj.mytest('arg1', 'arg2');", "test1.js"); $a->executeString("PHP.myobj.mytest(true, false, 1234567890);", "test2.js"); $a->executeString("PHP.myobj.mytest(3.14, 42, null);", "test3.js"); // Invalid parameters try { $a->executeString("PHP.myobj.mytest();", "test4.js"); } catch (V8JsScriptException $e) { echo $e->getMessage(), "\n"; } try { $a->executeString("PHP.myobj.mytest('arg1', 'arg2', 'arg3', 'extra_arg');", "test5.js"); } catch (V8JsScriptException $e) { echo $e->getMessage(), "\n"; } // Array / Object try {
$a->executeString($JS, "test1.js"); $a->executeString("bigarray()", "test1.js"); try { echo $a->executeString($jstparser, "test2.js"), "\n"; var_dump($a->executeString($jsontemplate, "test1.js")); } catch (V8JsScriptException $e) { echo $e->getMessage(); } // Test for context handling $a->executeString($JS, "test1.js"); $a->executeString("bigarray();"); echo '$a->obj: ', "\n"; $a->executeString("dump(PHP.obj);"); echo '$a->arr: ', "\n"; $a->executeString("dump(PHP.arr);"); echo '$a->argv: ', "\n"; $a->executeString("dump(PHP.argv);"); var_dump($a->argv); var_dump($a->executeString("test();")); var_dump($a->executeString("test();")); $b = new V8Js(); var_dump($a->phpver, $a->executeString("test();")); $b->executeString($JS, "test2.js"); var_dump($b->executeString("test();")); var_dump($b->executeString("print('foobar\\n');")); // Exception methods try { $b->executeString("foobar; foo();", "extest.js"); } catch (V8JsScriptException $e) { var_dump($e, $e->getJsFileName(), $e->getJsLineNumber(), $e->getJsSourceLine(), $e->getJsTrace()); }
function GetChapter($bookid, $chapterid) { list($bname, $bid) = explode("_", $bookid); if (strlen($bname) > 0) { $uri = "http://www.imanhua.com/comic/{$bid}/{$bname}{$chapterid}.shtml"; } else { $uri = "http://www.imanhua.com/comic/{$bid}/list_{$chapterid}.html"; } $html = $this->http->get($uri, "foot_chapter.js"); $html = str_replace("charset=gb2312", "charset=gb18030", $html); if (strlen($html) < 1) { return False; } //file_put_contents("imanhua-$bookid-$chapterid.html", $html); //$html = file_get_contents("imanhua-$bookid-$chapterid.html"); $xpath = new XPath($html); $scripts = $xpath->query("/html/head/script"); if ($scripts->length > 0) { $script = $scripts->item(0)->nodeValue; $js = new V8Js(); $js->executeString($script, "imanhua", V8Js::FLAG_FORCE_ARRAY); $cInfo = $js->executeString("cInfo;", "imanhua", V8Js::FLAG_FORCE_ARRAY); //$servers = array('c5.mangafiles.com', 'c4.mangafiles.com', 't5.mangafiles.com', 't4.mangafiles.com'); if ($cInfo["cid"] > 7910) { // http://www.imanhua.com/comic/76/list_61224.html // http://c4.mangafiles.com/Files/Images/76/61224/imanhua_001.png // "/Files/Images/"+cInfo.bid+"/"+cInfo.cid+"/"+$cInfo["files"][$i] $pictures = array(); foreach ($cInfo["files"] as $file) { $pictures[] = "http://c4.mangafiles.com" . "/Files/Images/" . $cInfo["bid"] . "/" . $cInfo["cid"] . "/" . $file; } return $pictures; } else { // http://www.imanhua.com/comic/135/list_7198.html // "/pictures/135/7198/trdh01.jpg" foreach ($cInfo["files"] as $file) { $pictures[] = "http://t4.mangafiles.com" . $file; } return $cInfo["files"]; } } else { return array(); } }