]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - lib/htmloutputter.php
Fixes for updating indices, charset/collation and engine type on plugin-created tables.
[quix0rs-gnu-social.git] / lib / htmloutputter.php
index 45e61d2fc6b32137ca2f875a24e2a1679f7fe9b6..7786b5941e25ba770d3f11c029f360af5b322f50 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * Laconica, the distributed open-source microblogging tool
+ * StatusNet, the distributed open-source microblogging tool
  *
  * Low-level generator for HTML
  *
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  *
  * @category  Output
- * @package   Laconica
- * @author    Evan Prodromou <evan@controlyourself.ca>
- * @author    Sarven Capadisli <csarven@controlyourself.ca>
- * @copyright 2008 Control Yourself, Inc.
+ * @package   StatusNet
+ * @author    Evan Prodromou <evan@status.net>
+ * @author    Sarven Capadisli <csarven@status.net>
+ * @copyright 2008 StatusNet, Inc.
  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
- * @link      http://laconi.ca/
+ * @link      http://status.net/
  */
 
-if (!defined('LACONICA')) {
+if (!defined('STATUSNET') && !defined('LACONICA')) {
     exit(1);
 }
 
 require_once INSTALLDIR.'/lib/xmloutputter.php';
 
-define('PAGE_TYPE_PREFS',
-       'text/html,application/xhtml+xml,'.
-       'application/xml;q=0.3,text/xml;q=0.2');
+// Can include XHTML options but these are too fragile in practice.
+define('PAGE_TYPE_PREFS', 'text/html');
 
 /**
  * Low-level generator for HTML
@@ -47,11 +46,11 @@ define('PAGE_TYPE_PREFS',
  * HTML-creation class.
  *
  * @category Output
- * @package  Laconica
- * @author   Evan Prodromou <evan@controlyourself.ca>
- * @author   Sarven Capadisli <csarven@controlyourself.ca>
+ * @package  StatusNet
+ * @author   Evan Prodromou <evan@status.net>
+ * @author   Sarven Capadisli <csarven@status.net>
  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
- * @link     http://laconi.ca/
+ * @link     http://status.net/
  *
  * @see      Action
  * @see      XMLOutputter
@@ -68,7 +67,7 @@ class HTMLOutputter extends XMLOutputter
      * @param boolean $indent Whether to indent output, default true
      */
 
-    function __construct($output='php://output', $indent=true)
+    function __construct($output='php://output', $indent=null)
     {
         parent::__construct($output, $indent);
     }
@@ -101,19 +100,21 @@ class HTMLOutputter extends XMLOutputter
             $type = common_negotiate_type($cp, $sp);
 
             if (!$type) {
-                common_user_error(_('This page is not available in a '.
-                                    'media type you accept'), 406);
-                exit(0);
+                throw new ClientException(_('This page is not available in a '.
+                                            'media type you accept'), 406);
             }
         }
 
         header('Content-Type: '.$type);
 
         $this->extraHeaders();
-
-        $this->startXML('html',
-                        '-//W3C//DTD XHTML 1.0 Strict//EN',
-                        'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd');
+        if (preg_match("/.*\/.*xml/", $type)) {
+            // Required for XML documents
+            $this->xw->startDocument('1.0', 'UTF-8');
+        }
+        $this->xw->writeDTD('html',
+                            '-//W3C//DTD XHTML 1.0 Strict//EN',
+                            'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd');
 
         $language = $this->getLanguage();
 
@@ -339,6 +340,150 @@ class HTMLOutputter extends XMLOutputter
                                       'title' => $title));
     }
 
+    /**
+     * output a script (almost always javascript) tag
+     *
+     * @param string $src          relative or absolute script path
+     * @param string $type         'type' attribute value of the tag
+     *
+     * @return void
+     */
+    function script($src, $type='text/javascript')
+    {
+        if(Event::handle('StartScriptElement', array($this,&$src,&$type))) {
+
+            $url = parse_url($src);
+
+            if( empty($url['scheme']) && empty($url['host']) && empty($url['query']) && empty($url['fragment']))
+            {
+                if (strpos($src, 'plugins/') === 0 || strpos($src, 'local/') === 0) {
+
+                    $src = common_path($src) . '?version=' . STATUSNET_VERSION;
+
+                }else{
+
+                    $path = common_config('javascript', 'path');
+
+                    if (empty($path)) {
+                        $path = common_config('site', 'path') . '/js/';
+                    }
+
+                    if ($path[strlen($path)-1] != '/') {
+                        $path .= '/';
+                    }
+
+                    if ($path[0] != '/') {
+                        $path = '/'.$path;
+                    }
+
+                    $server = common_config('javascript', 'server');
+
+                    if (empty($server)) {
+                        $server = common_config('site', 'server');
+                    }
+
+                    $ssl = common_config('javascript', 'ssl');
+
+                    if (is_null($ssl)) { // null -> guess
+                        if (common_config('site', 'ssl') == 'always' &&
+                            !common_config('javascript', 'server')) {
+                            $ssl = true;
+                        } else {
+                            $ssl = false;
+                        }
+                    }
+
+                    $protocol = ($ssl) ? 'https' : 'http';
+
+                    $src = $protocol.'://'.$server.$path.$src . '?version=' . STATUSNET_VERSION;
+                }
+            }
+
+            $this->element('script', array('type' => $type,
+                                                   'src' => $src),
+                                   ' ');
+
+            Event::handle('EndScriptElement', array($this,$src,$type));
+        }
+    }
+
+    /**
+     * output a script (almost always javascript) tag with inline
+     * code.
+     *
+     * @param string $code         code to put in the script tag
+     * @param string $type         'type' attribute value of the tag
+     *
+     * @return void
+     */
+
+    function inlineScript($code, $type='text/javascript')
+    {
+        if(Event::handle('StartInlineScriptElement', array($this,&$code,&$type))) {
+            $this->elementStart('script', array('type' => $type));
+            if($type == 'text/javascript') {
+                $this->raw('/*<![CDATA[*/ '); // XHTML compat
+            }
+            $this->raw($code);
+            if($type == 'text/javascript') {
+                $this->raw(' /*]]>*/'); // XHTML compat
+            }
+            $this->elementEnd('script');
+            Event::handle('EndInlineScriptElement', array($this,$code,$type));
+        }
+    }
+
+    /**
+     * output a css link
+     *
+     * @param string $src     relative path within the theme directory, or an absolute path
+     * @param string $theme        'theme' that contains the stylesheet
+     * @param string media         'media' attribute of the tag
+     *
+     * @return void
+     */
+    function cssLink($src,$theme=null,$media=null)
+    {
+        if(Event::handle('StartCssLinkElement', array($this,&$src,&$theme,&$media))) {
+            $url = parse_url($src);
+            if( empty($url['scheme']) && empty($url['host']) && empty($url['query']) && empty($url['fragment']))
+            {
+                if(file_exists(Theme::file($src,$theme))){
+                   $src = Theme::path($src, $theme);
+                }else{
+                   $src = common_path($src);
+                }
+                $src.= '?version=' . STATUSNET_VERSION;
+            }
+            $this->element('link', array('rel' => 'stylesheet',
+                                    'type' => 'text/css',
+                                    'href' => $src,
+                                    'media' => $media));
+            Event::handle('EndCssLinkElement', array($this,$src,$theme,$media));
+        }
+    }
+
+    /**
+     * output a style (almost always css) tag with inline
+     * code.
+     *
+     * @param string $code         code to put in the style tag
+     * @param string $type         'type' attribute value of the tag
+     * @param string $media        'media' attribute value of the tag
+     *
+     * @return void
+     */
+
+    function style($code, $type = 'text/css', $media = null)
+    {
+        if(Event::handle('StartStyleElement', array($this,&$code,&$type,&$media))) {
+            $this->elementStart('style', array('type' => $type, 'media' => $media));
+            $this->raw($code);
+            $this->elementEnd('style');
+            Event::handle('EndStyleElement', array($this,$code,$type,$media));
+        }
+    }
+
     /**
      * output an HTML textarea and associated elements
      *
@@ -366,4 +511,21 @@ class HTMLOutputter extends XMLOutputter
             $this->element('p', 'form_guide', $instructions);
         }
     }
+
+    /**
+    * Internal script to autofocus the given element on page onload.
+    *
+    * @param string $id element ID, must refer to an existing element
+    *
+    * @return void
+    *
+    */
+    function autofocus($id)
+    {
+        $this->inlineScript(
+                   ' $(document).ready(function() {'.
+                   ' var el = $("#' . $id . '");'.
+                   ' if (el.length) { el.focus(); }'.
+                   ' });');
+    }
 }