public function postrender() { if (!$this->used) { return ''; } // Go through all the inline scripts and sanitize $sanitized_scripts = array(); if ($this->script_infos) { foreach ($this->script_infos as $script_info) { if (isset($script_info['inline'])) { $sanitized_scripts[] = array('inline' => self::sanitize_code($script_info['inline'], $this->appid)); } else { if (isset($script_info['src'])) { // FBOPEN:NOTE - if js sources are fetched from outside, these will // have to be fetched, cached, sanitized, and stored. Requests then // would need to be directed to your cached version. The open source // code at this point does not support such caching. // $sanitized_scripts[] = array('src' => FBJSUrlRef::get_url($script_info['src'], $this->appid, 'js')); } } } } // If this is our first postrender build some bootstrapping code $bootstrap = false; if (!$this->postrendered) { $bootstrap = 'var app=new fbjs_sandbox(' . $this->appid . ');'; $profile = $this->fbml->get_env('profile', false, 0); $validation_vars = get_fb_validation_vars(array('user' => $this->user), $this->appid, $profile ? array('profile' => $profile) : array()); $bootstrap .= 'app.validation_vars=' . json_encode($validation_vars) . ';'; $context = $this->fbml->add_context(); $bootstrap .= 'app.context=\'' . escape_js_quotes($context) . '\';'; $bootstrap .= 'app.contextd=\'' . escape_js_quotes($this->fbml->_contexts[$context]) . '\';'; $bootstrap .= 'app.data=' . json_encode(array('user' => $this->user, 'installed' => $this->user ? is_platform_app_installed($this->appid, $this->user) : false, 'loggedin' => $this->user ? (bool) api_get_valid_session_key($this->user, $this->appid) : false)) . ';'; } // Render all inline scripts $html = ''; if ($this->fbml->_flavor->allows('script_onload')) { if (!$this->postrendered) { $bootstrap .= 'app.bootstrap();'; } foreach ($sanitized_scripts as $script) { if (isset($script['inline'])) { $html .= render_js_inline($script['inline']) . "\n"; } else { $script_include = '<script src="' . $script['src'] . '"></script>'; $html .= $script_include; } } } else { foreach ($sanitized_scripts as $script) { if (isset($script['inline'])) { $bootstrap .= 'app.pending_bootstraps.push(\'' . escape_js_quotes($script['inline']) . '\');'; } else { // We don't support script include for this flavor at this time. throw new FBMLJSParseError('Cannot allow external script'); } } } $this->used = false; $this->postrendered = true; return render_js_inline($bootstrap) . $html; }
public function onloadRegister($js) { return render_js_inline('(function(){' . $js . '})();'); }