jelix /lib/jelix-legacy/core/response/jResponseBasicHtml.class.php

Language PHP Lines 299
MD5 Hash 9bb5565cadd599e602d3cb7ef54c497e
Repository https://github.com/gmarrot/jelix.git View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
<?php
/**
 * @package     jelix
 * @subpackage  core_response
 * @author      Laurent Jouanneau
 * @contributor Julien Issler, Brice Tence
 * @copyright   2010-2012 Laurent Jouanneau
 * @copyright   2011 Julien Issler, 2011 Brice Tence
 * @link        http://www.jelix.org
 * @licence     GNU Lesser General Public Licence see LICENCE file or http://www.gnu.org/licenses/lgpl.html
 */


/**
 * interface for plugins for jResponseBasicHtml or jResponseHtml, which allows
 * to make changes in the response at several step
 */
interface jIHTMLResponsePlugin {

    public function __construct(jResponse $c);

    /**
     * called just before the jResponseBasicHtml::doAfterActions() call
     */
    public function afterAction();

    /**
     * called just before the final output. This is the opportunity
     * to make changes before the head and body output. At this step
     * the main content (if any) is already generated.
     */
    public function beforeOutput();

    /**
     * called when the content is generated, and potentially sent, except
     * the body end tag and the html end tags. This method can output
     * directly some contents.
     */
    public function atBottom();

    /**
     * called just before the output of an error page
     */
    public function beforeOutputError();
}

/**
 * Basic HTML response. the HTML content should be provided by a simple php file.
 * @package  jelix
 * @subpackage core_response
 */
class jResponseBasicHtml extends jResponse {
    /**
    * jresponse id
    * @var string
    */
    protected $_type = 'html';

    /**
     * the charset of the document
     * @var string
     */
    protected $_charset;

    /**
     * the lang of the document
     * @var string
     */
    protected $_lang;

    /**
     * says if the document is in xhtml or html
     */
    protected $_isXhtml = false;

    /**
     * says if xhtml content type should be send or not.
     * it true, a verification of HTTP_ACCEPT is done.
     * @var boolean
     */
    public $xhtmlContentType = false;

    /**
     * top content for head
     */
    protected $_headTop  = array ();

    /**
     * bottom content for head
     */
    protected $_headBottom  = array ();

    /**#@+
     * content for the body
     * @var array
     */
    protected $_bodyTop = array();
    protected $_bodyBottom = array();
    /**#@-*/

    /**
     * full path of php file to output. it should content php instruction
     * to display these variables:
     * - $HEADTOP: content added just after the opening <head> tag
     * - $HEADBOTTOM: content before the closing </head> tag
     * - $BODYTOP: content just after the <body> tag, at the top of the page
     * - $BODYBOTTOM: content just before the </body> tag, at the bottom of the page
     * - $BASEPATH: base path of the application, for links of your style sheets etc..
     * @var string
     */
    public $htmlFile = '';

    /**
     * list of plugins
     * @var array  array of jIHTMLResponsePlugin
     * @since 1.3a1
     */
    protected $plugins = array();

    /**
    * constructor;
    * setup the charset, the lang
    */
    function __construct (){

        $this->_charset = jApp::config()->charset;
        $this->_lang = jApp::config()->locale;

        // load plugins
        $plugins = jApp::config()->jResponseHtml['plugins'];
        if ($plugins) {
            $plugins = preg_split('/ *, */', $plugins);
            foreach ($plugins as $name) {
                if (!$name)
                    continue;
                $plugin = jApp::loadPlugin($name, 'htmlresponse', '.htmlresponse.php', $name.'HTMLResponsePlugin', $this);
                if ($plugin)
                    $this->plugins[$name] = $plugin;
                // do nothing if the plugin does not exist, we could be already into the error handle
            }
        }

        parent::__construct();
    }

    /**
     * return the corresponding plugin
     * @param string $name the name of the plugin
     * @return jIHTMLResponsePlugin|null the plugin or null if it isn't loaded
     * @since 1.3a1
     */
    function getPlugin($name) {
        if (isset($this->plugins[$name]))
            return $this->plugins[$name];
        return null;
    }

    /**
     * add additional content into the document head
     * @param string $content
     * @param boolean $toTop true if you want to add it at the top of the head content, else false for the bottom
     * @since 1.0b1
     */
    final public function addHeadContent ($content, $toTop = false) {
        if ($toTop) {
            $this->_headTop[] = $content;
        }
        else {
            $this->_headBottom[] = $content;
        }
    }

    /**
     * add content to the body
     * you can add additionnal content, before or after the content of body
     * @param string $content additionnal html content
     * @param boolean $before true if you want to add it before the content, else false for after
     */
    function addContent($content, $before = false){
        if ($before) {
            $this->_bodyTop[]=$content;
        }
        else {
            $this->_bodyBottom[]=$content;
        }
    }

    /**
     *  set the content-type in the http headers
     */
    protected function setContentType() {
        if($this->_isXhtml && $this->xhtmlContentType && strstr($_SERVER['HTTP_ACCEPT'],'application/xhtml+xml')){
            $this->_httpHeaders['Content-Type']='application/xhtml+xml;charset='.$this->_charset;
        }else{
            $this->_httpHeaders['Content-Type']='text/html;charset='.$this->_charset;
        }
    }

    /**
     * output the html content
     *
     * @return boolean    true if the generated content is ok
     */
    public function output(){

        if($this->_outputOnlyHeaders){
            $this->sendHttpHeaders();
            return true;
        }
    
        foreach($this->plugins as $name=>$plugin)
            $plugin->afterAction();

        $this->doAfterActions();

        if ($this->htmlFile == '')
            throw new Exception('static page is missing');

        $this->setContentType();

        jLog::outputLog($this);

        foreach($this->plugins as $name=>$plugin)
            $plugin->beforeOutput();

        $HEADTOP = implode("\n", $this->_headTop);
        $HEADBOTTOM = implode("\n", $this->_headBottom);
        $BODYTOP = implode("\n", $this->_bodyTop);
        $BODYBOTTOM = implode("\n", $this->_bodyBottom);
        $BASEPATH = jApp::config()->urlengine['basePath'];

        ob_start();
        foreach($this->plugins as $name=>$plugin)
            $plugin->atBottom();
        $BODYBOTTOM .= ob_get_clean();

        $this->sendHttpHeaders();
        include($this->htmlFile);

        return true;
    }

    /**
     * The method you can overload in your inherited html response
     * overload it if you want to add processes (stylesheet, head settings, additionnal content etc..)
     * after any actions
     * @since 1.1
     */
    protected function doAfterActions(){

    }

    /**
     * output errors
     */
    public function outputErrors(){

        if (file_exists(jApp::appPath('responses/error.en_US.php')))
            $file = jApp::appPath('responses/error.en_US.php');
        else
            $file = JELIX_LIB_CORE_PATH.'response/error.en_US.php';
        // we erase already generated content
        $this->_headTop = array();
        $this->_headBottom = array();
        $this->_bodyBottom = array();
        $this->_bodyTop = array();

        jLog::outputLog($this);

        foreach($this->plugins as $name=>$plugin)
            $plugin->beforeOutputError();

        $HEADTOP = implode("\n", $this->_headTop);
        $HEADBOTTOM = implode("\n", $this->_headBottom);
        $BODYTOP = implode("\n", $this->_bodyTop);
        $BODYBOTTOM = implode("\n", $this->_bodyBottom);
        $BASEPATH = jApp::config()->urlengine['basePath'];

        header("HTTP/{$this->httpVersion} 500 Internal jelix error");
        header('Content-Type: text/html;charset='.$this->_charset);
        include($file);
    }

    /**
     * change the type of html for the output
     * @param boolean $xhtml true if you want xhtml, false if you want html
     */
    public function setXhtmlOutput($xhtml = true){
        $this->_isXhtml = $xhtml;
    }

    /**
     * says if the response will be xhtml or html
     * @return boolean true if it is xhtml
     */
    final public function isXhtml(){ return $this->_isXhtml; }

}
Back to Top