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(); }
{ echo "I'm setter!\n"; var_dump($name, $value); } function __get($name) { echo "I'm getter!\n"; var_dump($name); } function __call($name, $args) { echo "I'm caller!\n"; var_dump($name, $args); } } $a = new V8Js(); $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); }
/** * JS VM * @return \V8Js * @throws JSV8AppException */ protected function getVM() { if (is_null($this->vm)) { // cache buster $version = rand(); // emulate a (very poor) browser \V8Js::registerExtension('browserEmulator' . $version, file_get_contents($this->basePath . 'browserEmulator.js')); // load absurd.js browser version (shared with client) \V8Js::registerExtension('absurd' . $version, file_get_contents($this->assetPath . 'absurd.js')); \V8Js::registerExtension('absurdHat' . $version, file_get_contents($this->assetPath . 'absurdhat.js')); // load generic dyncss implementation \V8Js::registerExtension('dyncss' . $version, file_get_contents($this->assetPath . 'dyncss.js')); // create variable mapping $extensions = array('browserEmulator' . $version, 'absurd' . $version, 'absurdHat' . $version, 'dyncss' . $version); // start engine (discard any output via print) ob_start(); $vm = new \V8Js('PHP', array(), $extensions); ob_end_clean(); // add error handler $vm->error = function ($error) { throw new JSV8AppException($error); }; $this->vm = $vm; } return $this->vm; }
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(); var_dump($a->executeString('Jst.write = function(s) { html += "EI TOIMI"; };' . "\n" . ' Jst.evaluate("lol testi <%= 1 %>", {});'));
echo "Called __call(): "; var_dump($name, $args); } function __Invoke($name, $arg1, $arg2) { 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";
<?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"); };
<?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); }
/** * {@inheritdoc} */ public function createContext($code) { $this->context = $this->v8->compileString($code); $this->v8->executeScript($this->context); }
<?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 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');
$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()); }
/** * */ 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(); 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" );');
<?php // 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"; }
<?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"; }
<?php V8Js::registerExtension('a', file_get_contents('js/json-template.js'), array('b')); V8Js::registerExtension('b', file_get_contents('js/jstparser.js'), array('a')); var_dump(V8JS::getExtensions()); $a = new V8Js('myobj', array(), array('b'));
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(); } }