]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - lib/util.php
Move page-generation code from lib/util.php to Action class
[quix0rs-gnu-social.git] / lib / util.php
index ed73b19e668cc2e42c6386e046e605f579978bd1..009a0457ce2f7647b1f710111c8c6ace2256e39a 100644 (file)
@@ -17,7 +17,7 @@
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-/* XXX: break up into separate modules (HTTP, HTML, user, files) */
+/* XXX: break up into separate modules (HTTP, user, files) */
 
 // Show a server error
 
@@ -79,65 +79,6 @@ function common_user_error($msg, $code=400)
     common_show_footer();
 }
 
-$xw = null;
-
-// Start an HTML element
-function common_element_start($tag, $attrs=null)
-{
-    global $xw;
-    $xw->startElement($tag);
-    if (is_array($attrs)) {
-        foreach ($attrs as $name => $value) {
-            $xw->writeAttribute($name, $value);
-        }
-    } else if (is_string($attrs)) {
-        $xw->writeAttribute('class', $attrs);
-    }
-}
-
-function common_element_end($tag)
-{
-    static $empty_tag = array('base', 'meta', 'link', 'hr',
-                              'br', 'param', 'img', 'area',
-                              'input', 'col');
-    global $xw;
-    // XXX: check namespace
-    if (in_array($tag, $empty_tag)) {
-        $xw->endElement();
-    } else {
-        $xw->fullEndElement();
-    }
-}
-
-function common_element($tag, $attrs=null, $content=null)
-{
-    common_element_start($tag, $attrs);
-    global $xw;
-    if (!is_null($content)) {
-        $xw->text($content);
-    }
-    common_element_end($tag);
-}
-
-function common_start_xml($doc=null, $public=null, $system=null, $indent=true)
-{
-    global $xw;
-    $xw = new XMLWriter();
-    $xw->openURI('php://output');
-    $xw->setIndent($indent);
-    $xw->startDocument('1.0', 'UTF-8');
-    if ($doc) {
-        $xw->writeDTD($doc, $public, $system);
-    }
-}
-
-function common_end_xml()
-{
-    global $xw;
-    $xw->endDocument();
-    $xw->flush();
-}
-
 function common_init_locale($language=null)
 {
     if(!$language) {
@@ -167,346 +108,6 @@ function common_init_language()
     }
 }
 
-define('PAGE_TYPE_PREFS', 'text/html,application/xhtml+xml,application/xml;q=0.3,text/xml;q=0.2');
-
-function common_show_header($pagetitle, $callable=null, $data=null, $headercall=null)
-{
-
-    global $config, $xw;
-    global $action; /* XXX: kind of cheating here. */
-
-    common_start_html();
-
-    common_element_start('head');
-    common_element('title', null,
-                   $pagetitle . " - " . $config['site']['name']);
-    common_element('link', array('rel' => 'stylesheet',
-                                 'type' => 'text/css',
-                                 'href' => theme_path('display.css') . '?version=' . LACONICA_VERSION,
-                                 'media' => 'screen, projection, tv'));
-    foreach (array(6,7) as $ver) {
-        if (file_exists(theme_file('ie'.$ver.'.css'))) {
-            // Yes, IE people should be put in jail.
-            $xw->writeComment('[if lte IE '.$ver.']><link rel="stylesheet" type="text/css" '.
-                              'href="'.theme_path('ie'.$ver.'.css').'?version='.LACONICA_VERSION.'" /><![endif]');
-        }
-    }
-
-    common_element('script', array('type' => 'text/javascript',
-                                   'src' => common_path('js/jquery.min.js')),
-                   ' ');
-    common_element('script', array('type' => 'text/javascript',
-                                   'src' => common_path('js/jquery.form.js')),
-                   ' ');
-    common_element('script', array('type' => 'text/javascript',
-                                   'src' => common_path('js/xbImportNode.js')),
-                   ' ');
-    common_element('script', array('type' => 'text/javascript',
-                                   'src' => common_path('js/util.js?version='.LACONICA_VERSION)),
-                   ' ');
-    common_element('link', array('rel' => 'search', 'type' => 'application/opensearchdescription+xml',
-                                 'href' =>  common_local_url('opensearch', array('type' => 'people')),
-                                 'title' => common_config('site', 'name').' People Search'));
-
-    common_element('link', array('rel' => 'search', 'type' => 'application/opensearchdescription+xml',
-                                 'href' =>  common_local_url('opensearch', array('type' => 'notice')),
-                                 'title' => common_config('site', 'name').' Notice Search'));
-
-    if ($callable) {
-        if ($data) {
-            call_user_func($callable, $data);
-        } else {
-            call_user_func($callable);
-        }
-    }
-    common_element_end('head');
-    common_element_start('body', $action);
-    common_element_start('div', array('id' => 'wrap'));
-    common_element_start('div', array('id' => 'header'));
-    common_nav_menu();
-    if ((isset($config['site']['logo']) && is_string($config['site']['logo']) && (strlen($config['site']['logo']) > 0))
-        || file_exists(theme_file('logo.png')))
-    {
-        common_element_start('a', array('href' => common_local_url('public')));
-        common_element('img', array('src' => isset($config['site']['logo']) ?
-                                    ($config['site']['logo']) : theme_path('logo.png'),
-                                    'alt' => $config['site']['name'],
-                                    'id' => 'logo'));
-        common_element_end('a');
-    } else {
-        common_element_start('p', array('id' => 'branding'));
-        common_element('a', array('href' => common_local_url('public')),
-                       $config['site']['name']);
-        common_element_end('p');
-    }
-
-    common_element('h1', 'pagetitle', $pagetitle);
-
-    if ($headercall) {
-        if ($data) {
-            call_user_func($headercall, $data);
-        } else {
-            call_user_func($headercall);
-        }
-    }
-    common_element_end('div');
-    common_element_start('div', array('id' => 'content'));
-}
-
-function common_start_html($type=null, $indent=true)
-{
-
-    if (!$type) {
-        $httpaccept = isset($_SERVER['HTTP_ACCEPT']) ? $_SERVER['HTTP_ACCEPT'] : null;
-
-        // XXX: allow content negotiation for RDF, RSS, or XRDS
-
-        $type = common_negotiate_type(common_accept_to_prefs($httpaccept),
-                                      common_accept_to_prefs(PAGE_TYPE_PREFS));
-
-        if (!$type) {
-            common_user_error(_('This page is not available in a media type you accept'), 406);
-            exit(0);
-        }
-    }
-
-    header('Content-Type: '.$type);
-
-    common_start_xml('html',
-                     '-//W3C//DTD XHTML 1.0 Strict//EN',
-                     'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd', $indent);
-
-    // FIXME: correct language for interface
-
-    $language = common_language();
-
-    common_element_start('html', array('xmlns' => 'http://www.w3.org/1999/xhtml',
-                                       'xml:lang' => $language,
-                                       'lang' => $language));
-}
-
-function common_show_footer()
-{
-    global $xw, $config;
-    common_element_end('div'); // content div
-    common_foot_menu();
-    common_element_start('div', array('id' => 'footer'));
-    common_element_start('div', 'laconica');
-    if (common_config('site', 'broughtby')) {
-        $instr = _('**%%site.name%%** is a microblogging service brought to you by [%%site.broughtby%%](%%site.broughtbyurl%%). ');
-    } else {
-        $instr = _('**%%site.name%%** is a microblogging service. ');
-    }
-    $instr .= sprintf(_('It runs the [Laconica](http://laconi.ca/) microblogging software, version %s, available under the [GNU Affero General Public License](http://www.fsf.org/licensing/licenses/agpl-3.0.html).'), LACONICA_VERSION);
-    $output = common_markup_to_html($instr);
-    common_raw($output);
-    common_element_end('div');
-    common_element('img', array('id' => 'cc',
-                                'src' => $config['license']['image'],
-                                'alt' => $config['license']['title']));
-    common_element_start('p');
-    common_text(_('Unless otherwise specified, contents of this site are copyright by the contributors and available under the '));
-    common_element('a', array('class' => 'license',
-                              'rel' => 'license',
-                              'href' => $config['license']['url']),
-                   $config['license']['title']);
-    common_text(_('. Contributors should be attributed by full name or nickname.'));
-    common_element_end('p');
-    common_element_end('div');
-    common_element_end('div');
-    common_element_end('body');
-    common_element_end('html');
-    common_end_xml();
-}
-
-function common_text($txt)
-{
-    global $xw;
-    $xw->text($txt);
-}
-
-function common_raw($xml)
-{
-    global $xw;
-    $xw->writeRaw($xml);
-}
-
-function common_nav_menu()
-{
-    $user = common_current_user();
-    common_element_start('ul', array('id' => 'nav'));
-    if ($user) {
-        common_menu_item(common_local_url('all', array('nickname' => $user->nickname)),
-                         _('Home'));
-    }
-    common_menu_item(common_local_url('peoplesearch'), _('Search'));
-    if ($user) {
-        common_menu_item(common_local_url('profilesettings'),
-                         _('Settings'));
-        common_menu_item(common_local_url('invite'),
-                         _('Invite'));
-        common_menu_item(common_local_url('logout'),
-                         _('Logout'));
-    } else {
-        common_menu_item(common_local_url('login'), _('Login'));
-        if (!common_config('site', 'closed')) {
-            common_menu_item(common_local_url('register'), _('Register'));
-        }
-        common_menu_item(common_local_url('openidlogin'), _('OpenID'));
-    }
-    common_menu_item(common_local_url('doc', array('title' => 'help')),
-                     _('Help'));
-    common_element_end('ul');
-}
-
-function common_foot_menu()
-{
-    common_element_start('ul', array('id' => 'nav_sub'));
-    common_menu_item(common_local_url('doc', array('title' => 'help')),
-                     _('Help'));
-    common_menu_item(common_local_url('doc', array('title' => 'about')),
-                     _('About'));
-    common_menu_item(common_local_url('doc', array('title' => 'faq')),
-                     _('FAQ'));
-    common_menu_item(common_local_url('doc', array('title' => 'privacy')),
-                     _('Privacy'));
-    common_menu_item(common_local_url('doc', array('title' => 'source')),
-                     _('Source'));
-    common_menu_item(common_local_url('doc', array('title' => 'contact')),
-                     _('Contact'));
-    common_element_end('ul');
-}
-
-function common_menu_item($url, $text, $title=null, $is_selected=false)
-{
-    $lattrs = array();
-    if ($is_selected) {
-        $lattrs['class'] = 'current';
-    }
-    common_element_start('li', $lattrs);
-    $attrs['href'] = $url;
-    if ($title) {
-        $attrs['title'] = $title;
-    }
-    common_element('a', $attrs, $text);
-    common_element_end('li');
-}
-
-function common_input($id, $label, $value=null,$instructions=null)
-{
-    common_element_start('p');
-    common_element('label', array('for' => $id), $label);
-    $attrs = array('name' => $id,
-                   'type' => 'text',
-                   'class' => 'input_text',
-                   'id' => $id);
-    if ($value) {
-        $attrs['value'] = htmlspecialchars($value);
-    }
-    common_element('input', $attrs);
-    if ($instructions) {
-        common_element('span', 'input_instructions', $instructions);
-    }
-    common_element_end('p');
-}
-
-function common_checkbox($id, $label, $checked=false, $instructions=null, $value='true', $disabled=false)
-{
-    common_element_start('p');
-    $attrs = array('name' => $id,
-                   'type' => 'checkbox',
-                   'class' => 'checkbox',
-                   'id' => $id);
-    if ($value) {
-        $attrs['value'] = htmlspecialchars($value);
-    }
-    if ($checked) {
-        $attrs['checked'] = 'checked';
-    }
-    if ($disabled) {
-        $attrs['disabled'] = 'true';
-    }
-    common_element('input', $attrs);
-    common_text(' ');
-    common_element('label', array('class' => 'checkbox_label', 'for' => $id), $label);
-    common_text(' ');
-    if ($instructions) {
-        common_element('span', 'input_instructions', $instructions);
-    }
-    common_element_end('p');
-}
-
-function common_dropdown($id, $label, $content, $instructions=null, $blank_select=FALSE, $selected=null)
-{
-    common_element_start('p');
-    common_element('label', array('for' => $id), $label);
-    common_element_start('select', array('id' => $id, 'name' => $id));
-    if ($blank_select) {
-        common_element('option', array('value' => ''));
-    }
-    foreach ($content as $value => $option) {
-        if ($value == $selected) {
-            common_element('option', array('value' => $value, 'selected' => $value), $option);
-        } else {
-            common_element('option', array('value' => $value), $option);
-        }
-    }
-    common_element_end('select');
-    if ($instructions) {
-        common_element('span', 'input_instructions', $instructions);
-    }
-    common_element_end('p');
-}
-function common_hidden($id, $value)
-{
-    common_element('input', array('name' => $id,
-                                  'type' => 'hidden',
-                                  'id' => $id,
-                                  'value' => $value));
-}
-
-function common_password($id, $label, $instructions=null)
-{
-    common_element_start('p');
-    common_element('label', array('for' => $id), $label);
-    $attrs = array('name' => $id,
-                   'type' => 'password',
-                   'class' => 'password',
-                   'id' => $id);
-    common_element('input', $attrs);
-    if ($instructions) {
-        common_element('span', 'input_instructions', $instructions);
-    }
-    common_element_end('p');
-}
-
-function common_submit($id, $label, $cls='submit')
-{
-    global $xw;
-    common_element_start('p');
-    common_element('input', array('type' => 'submit',
-                                  'id' => $id,
-                                  'name' => $id,
-                                  'class' => $cls,
-                                  'value' => $label));
-    common_element_end('p');
-}
-
-function common_textarea($id, $label, $content=null, $instructions=null)
-{
-    common_element_start('p');
-    common_element('label', array('for' => $id), $label);
-    common_element('textarea', array('rows' => 3,
-                                     'cols' => 40,
-                                     'name' => $id,
-                                     'id' => $id),
-                   ($content) ? $content : '');
-    if ($instructions) {
-        common_element('span', 'input_instructions', $instructions);
-    }
-    common_element_end('p');
-}
-
 function common_timezone()
 {
     if (common_logged_in()) {
@@ -1018,7 +619,7 @@ function common_relative_profile($sender, $nickname, $dt=null)
     // XXX: use a join instead of a subquery
     $recipient->whereAdd('EXISTS (SELECT subscribed from subscription where subscriber = '.$sender->id.' and subscribed = id)', 'AND');
     $recipient->whereAdd('nickname = "' . trim($nickname) . '"', 'AND');
-    if ($recipient->find(TRUE)) {
+    if ($recipient->find(true)) {
         // XXX: should probably differentiate between profiles with
         // the same name by date of most recent update
         return $recipient;
@@ -1028,7 +629,7 @@ function common_relative_profile($sender, $nickname, $dt=null)
     // XXX: use a join instead of a subquery
     $recipient->whereAdd('EXISTS (SELECT subscriber from subscription where subscribed = '.$sender->id.' and subscriber = id)', 'AND');
     $recipient->whereAdd('nickname = "' . trim($nickname) . '"', 'AND');
-    if ($recipient->find(TRUE)) {
+    if ($recipient->find(true)) {
         // XXX: should probably differentiate between profiles with
         // the same name by date of most recent update
         return $recipient;
@@ -1571,7 +1172,10 @@ function common_twitter_broadcast($notice, $flink)
                      CURLOPT_FOLLOWLOCATION    => true,
                      CURLOPT_USERAGENT        => "Laconica",
                      CURLOPT_CONNECTTIMEOUT    => 120,  // XXX: Scary!!!! How long should this be?
-                     CURLOPT_TIMEOUT            => 120
+                     CURLOPT_TIMEOUT            => 120,
+
+                     # Twitter is strict about accepting invalid "Expect" headers
+                     CURLOPT_HTTPHEADER => array('Expect:')
                      );
 
     $ch = curl_init($uri);
@@ -2258,7 +1862,7 @@ function common_message_form($content, $user, $to)
     $mutual_users->free();
     unset($mutual_users);
 
-    common_dropdown('to', _('To'), $mutual, null, FALSE, $to->id);
+    common_dropdown('to', _('To'), $mutual, null, false, $to->id);
 
     common_element_start('p');