Example #1
0
    public function destruct($content = '')
    {
        static $called = false;
        if ($called == false && $this->_internal_request) {
            $called = true;
            $text = $this->_request_type != self::REQUEST_COMET ? ob_get_clean() : '';
            if (empty($text) && !empty($content)) {
                $text = $content;
            }
            if (!empty($this->result_ids)) {
                $result_ids = array();
                // get the matching ids
                foreach ($this->result_ids as $r_id) {
                    if (strpos($r_id, '*')) {
                        $clear_id = str_replace('*', '\\w+?', $r_id);
                        preg_match_all('/<[^>]*?id=(?:\'|")(' . $clear_id . '\\w*?)(?:\'|")[^>]*?>/isS', $text, $ids);
                        if (!empty($ids[1])) {
                            foreach ($ids[1] as $r_id2) {
                                $result_ids[] = $r_id2;
                            }
                        }
                    } else {
                        $result_ids[] = $r_id;
                    }
                }
                foreach ($result_ids as $r_id) {
                    if (strpos($text, ' id="' . $r_id . '">') !== false) {
                        $start = strpos($text, ' id="' . $r_id . '">') + strlen(' id="' . $r_id . '">');
                        $end = strpos($text, '<!--' . $r_id . '--></');
                        $this->assignHtml($r_id, substr($text, $start, $end - $start));
                        // Assume that all data should be put to div with this ID
                    } elseif ($this->_skip_result_ids_check == true) {
                        $this->assignHtml($r_id, $text);
                    }
                }
                if ($this->full_render && preg_match('/<title>(.*?)<\\/title>/s', $text, $m)) {
                    $this->assign('title', html_entity_decode($m[1], ENT_QUOTES));
                }
                // Fix for payment processor form, should be removed after payments refactoring
                if (Embedded::isEnabled() && empty($this->_result['html']) && $this->_skip_result_ids_check == false && !empty($text)) {
                    foreach ($this->result_ids as $r_id) {
                        $text .= '<script type="text/javascript">if (document.process) { document.process.target="_parent"; document.process.submit(); }</script>';
                        $this->assignHtml($r_id, $text);
                        break;
                    }
                }
                $text = '';
            }
            if (empty($this->_result['non_ajax_notifications'])) {
                $this->assign('notifications', fn_get_notifications());
            }
            if (Embedded::isEnabled()) {
                $this->assign('session_data', array('name' => Session::getName(), 'id' => Session::getId()));
            }
            if (!empty($this->anchor)) {
                $this->assign('anchor', $this->anchor);
            }
            // we call session saving directly
            session_write_close();
            // Prepare response
            $response = $this->_result;
            if (fn_string_not_empty($text)) {
                $response['text'] = trim($text);
            }
            $response = json_encode($response, JSON_UNESCAPED_UNICODE);
            if (!headers_sent()) {
                header(' ', true, 200);
                // force 200 header, because we still need to return content
                if (Embedded::isEnabled() || $this->_request_type == self::REQUEST_JSONP_POST) {
                    header('P3P: CP="CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR"');
                    // for IE cors
                }
            }
            if ($this->_request_type == self::REQUEST_XML) {
                // Return json object
                header('Content-type: ' . $this->_content_type . '; charset=' . CHARSET);
            } elseif ($this->_request_type == self::REQUEST_JSONP) {
                // Return jsonp object
                header('Content-type: ' . $this->_content_type . '; charset=' . CHARSET);
                $response = $this->callback . '(' . $response . ');';
            } elseif ($this->_request_type == self::REQUEST_JSONP_POST) {
                // Return jsonp object
                header("X-Frame-Options: ", true);
                $response = '<script type="text/javascript" src="' . Registry::get('config.current_location') . '/js/lib/jquery/jquery.min.js' . '"></script>
                             <script type="text/javascript" src="' . Registry::get('config.current_location') . '/js/lib/postmessage/jquery.ba-postmessage.js' . '"></script>
                             <script type="text/javascript">

                                var Tygh = {};
                                Tygh.$ = jQuery.noConflict(true);
                             </script>
                             <script type="text/javascript">Tygh.$.postMessage(
                                "' . fn_js_escape($response) . '",\'' . Embedded::getUrl() . '\');</script>';
            } else {
                // Return html textarea object
                $response = '<textarea>' . fn_html_escape($response) . '</textarea>';
            }
            fn_echo($response);
        }
    }