X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=lib%2Futil.php;h=1e874e04b2be26637da91a76e79af299384422f8;hb=93e249de2a28775e3a40e63324371fec359fb93b;hp=03d549334a2a6db5ca0d675b3216d8972be5c62e;hpb=73a0271960a5e7e2bc6e040595d3071d7b689a99;p=quix0rs-gnu-social.git
diff --git a/lib/util.php b/lib/util.php
index 03d549334a..009a0457ce 100644
--- a/lib/util.php
+++ b/lib/util.php
@@ -17,1146 +17,1931 @@
* along with this program. If not, see .
*/
-/* XXX: break up into separate modules (HTTP, HTML, user, files) */
-
-# Show a server error
-
-function common_server_error($msg, $code=500) {
- static $status = array(500 => 'Internal Server Error',
- 501 => 'Not Implemented',
- 502 => 'Bad Gateway',
- 503 => 'Service Unavailable',
- 504 => 'Gateway Timeout',
- 505 => 'HTTP Version Not Supported');
+/* XXX: break up into separate modules (HTTP, user, files) */
- if (!array_key_exists($code, $status)) {
- $code = 500;
- }
+// Show a server error
- $status_string = $status[$code];
+function common_server_error($msg, $code=500)
+{
+ static $status = array(500 => 'Internal Server Error',
+ 501 => 'Not Implemented',
+ 502 => 'Bad Gateway',
+ 503 => 'Service Unavailable',
+ 504 => 'Gateway Timeout',
+ 505 => 'HTTP Version Not Supported');
+
+ if (!array_key_exists($code, $status)) {
+ $code = 500;
+ }
+
+ $status_string = $status[$code];
+
+ header('HTTP/1.1 '.$code.' '.$status_string);
+ header('Content-type: text/plain');
+
+ print $msg;
+ print "\n";
+ exit();
+}
+
+// Show a user error
+function common_user_error($msg, $code=400)
+{
+ static $status = array(400 => 'Bad Request',
+ 401 => 'Unauthorized',
+ 402 => 'Payment Required',
+ 403 => 'Forbidden',
+ 404 => 'Not Found',
+ 405 => 'Method Not Allowed',
+ 406 => 'Not Acceptable',
+ 407 => 'Proxy Authentication Required',
+ 408 => 'Request Timeout',
+ 409 => 'Conflict',
+ 410 => 'Gone',
+ 411 => 'Length Required',
+ 412 => 'Precondition Failed',
+ 413 => 'Request Entity Too Large',
+ 414 => 'Request-URI Too Long',
+ 415 => 'Unsupported Media Type',
+ 416 => 'Requested Range Not Satisfiable',
+ 417 => 'Expectation Failed');
+
+ if (!array_key_exists($code, $status)) {
+ $code = 400;
+ }
+
+ $status_string = $status[$code];
+
+ header('HTTP/1.1 '.$code.' '.$status_string);
+
+ common_show_header('Error');
+ common_element('div', array('class' => 'error'), $msg);
+ common_show_footer();
+}
+
+function common_init_locale($language=null)
+{
+ if(!$language) {
+ $language = common_language();
+ }
+ putenv('LANGUAGE='.$language);
+ putenv('LANG='.$language);
+ return setlocale(LC_ALL, $language . ".utf8",
+ $language . ".UTF8",
+ $language . ".utf-8",
+ $language . ".UTF-8",
+ $language);
+}
+
+function common_init_language()
+{
+ mb_internal_encoding('UTF-8');
+ $language = common_language();
+ // So we don't have to make people install the gettext locales
+ $locale_set = common_init_locale($language);
+ bindtextdomain("laconica", common_config('site','locale_path'));
+ bind_textdomain_codeset("laconica", "UTF-8");
+ textdomain("laconica");
+ setlocale(LC_CTYPE, 'C');
+ if(!$locale_set) {
+ common_log(LOG_INFO,'Language requested:'.$language.' - locale could not be set:',__FILE__);
+ }
+}
+
+function common_timezone()
+{
+ if (common_logged_in()) {
+ $user = common_current_user();
+ if ($user->timezone) {
+ return $user->timezone;
+ }
+ }
+
+ global $config;
+ return $config['site']['timezone'];
+}
+
+function common_language()
+{
+
+ // If there is a user logged in and they've set a language preference
+ // then return that one...
+ if (common_logged_in()) {
+ $user = common_current_user();
+ $user_language = $user->language;
+ if ($user_language)
+ return $user_language;
+ }
+
+ // Otherwise, find the best match for the languages requested by the
+ // user's browser...
+ $httplang = isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) ? $_SERVER['HTTP_ACCEPT_LANGUAGE'] : null;
+ if (!empty($httplang)) {
+ $language = client_prefered_language($httplang);
+ if ($language)
+ return $language;
+ }
+
+ // Finally, if none of the above worked, use the site's default...
+ return common_config('site', 'language');
+}
+// salted, hashed passwords are stored in the DB
+
+function common_munge_password($password, $id)
+{
+ return md5($password . $id);
+}
- header('HTTP/1.1 '.$code.' '.$status_string);
- header('Content-type: text/plain');
+// check if a username exists and has matching password
+function common_check_user($nickname, $password)
+{
+ // NEVER allow blank passwords, even if they match the DB
+ if (mb_strlen($password) == 0) {
+ return false;
+ }
+ $user = User::staticGet('nickname', $nickname);
+ if (is_null($user)) {
+ return false;
+ } else {
+ if (0 == strcmp(common_munge_password($password, $user->id),
+ $user->password)) {
+ return $user;
+ } else {
+ return false;
+ }
+ }
+}
+
+// is the current user logged in?
+function common_logged_in()
+{
+ return (!is_null(common_current_user()));
+}
- print $msg;
- print "\n";
- exit();
+function common_have_session()
+{
+ return (0 != strcmp(session_id(), ''));
}
-# Show a user error
-function common_user_error($msg, $code=400) {
- static $status = array(400 => 'Bad Request',
- 401 => 'Unauthorized',
- 402 => 'Payment Required',
- 403 => 'Forbidden',
- 404 => 'Not Found',
- 405 => 'Method Not Allowed',
- 406 => 'Not Acceptable',
- 407 => 'Proxy Authentication Required',
- 408 => 'Request Timeout',
- 409 => 'Conflict',
- 410 => 'Gone',
- 411 => 'Length Required',
- 412 => 'Precondition Failed',
- 413 => 'Request Entity Too Large',
- 414 => 'Request-URI Too Long',
- 415 => 'Unsupported Media Type',
- 416 => 'Requested Range Not Satisfiable',
- 417 => 'Expectation Failed');
-
- if (!array_key_exists($code, $status)) {
- $code = 400;
- }
-
- $status_string = $status[$code];
-
- header('HTTP/1.1 '.$code.' '.$status_string);
-
- common_show_header('Error');
- common_element('div', array('class' => 'error'), $msg);
- 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) {
- global $xw;
- # TODO: switch based on $tag
- $xw->fullEndElement();
-}
-
-function common_element($tag, $attrs=NULL, $content=NULL) {
- common_element_start($tag, $attrs);
- global $xw;
- $xw->text($content);
- common_element_end($tag);
-}
-
-function common_start_xml($doc=NULL, $public=NULL, $system=NULL) {
- global $xw;
- $xw = new XMLWriter();
- $xw->openURI('php://output');
- $xw->setIndent(true);
- $xw->startDocument('1.0', 'UTF-8');
- if ($doc) {
- $xw->writeDTD($doc, $public, $system);
- }
-}
-
-function common_end_xml() {
- global $xw;
- $xw->endDocument();
- $xw->flush();
-}
-
-define('PAGE_TYPE_PREFS', 'application/xhtml+xml,text/html;q=0.7,application/xml;q=0.3,text/xml;q=0.2');
-
-function common_show_header($pagetitle, $callable=NULL, $data=NULL, $headercall=NULL) {
- global $config, $xw;
-
- $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(_t('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');
-
- # FIXME: correct language for interface
-
- common_element_start('html', array('xmlns' => 'http://www.w3.org/1999/xhtml',
- 'xml:lang' => 'en',
- 'lang' => 'en'));
-
- 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'),
- '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.']> 'text/javascript',
- 'src' => common_path('js/jquery.min.js')),
- ' ');
- common_element('script', array('type' => 'text/javascript',
- 'src' => common_path('js/util.js')),
- ' ');
-
- if ($callable) {
- if ($data) {
- call_user_func($callable, $data);
- } else {
- call_user_func($callable);
- }
- }
- common_element_end('head');
- common_element_start('body');
- common_element_start('div', array('id' => 'wrap'));
- common_element_start('div', array('id' => 'header'));
- common_nav_menu();
- if ((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' => ($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_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 = _t('**%%site.name%%** is a microblogging service brought to you by [%%site.broughtby%%](%%site.broughtbyurl%%). ');
- } else {
- $instr = _t('**%%site.name%%** is a microblogging service. ');
- }
- $instr .= _t('It runs the [Laconica](http://laconi.ca/) ' .
- 'microblogging software, version ' . LACONICA_VERSION . ', ' .
- 'available under the ' .
- '[GNU Affero General Public License]' .
- '(http://www.fsf.org/licensing/licenses/agpl-3.0.html).');
- $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(_t('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(_t('. 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)),
- _t('Home'));
- }
- common_menu_item(common_local_url('public'), _t('Public'));
- common_menu_item(common_local_url('doc', array('title' => 'help')),
- _t('Help'));
- if ($user) {
- common_menu_item(common_local_url('profilesettings'),
- _t('Settings'));
- common_menu_item(common_local_url('logout'),
- _t('Logout'));
- } else {
- common_menu_item(common_local_url('login'), _t('Login'));
- common_menu_item(common_local_url('register'), _t('Register'));
- common_menu_item(common_local_url('openidlogin'), _t('OpenID'));
- }
- 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' => 'about')),
- _t('About'));
- common_menu_item(common_local_url('doc', array('title' => 'faq')),
- _t('FAQ'));
- common_menu_item(common_local_url('doc', array('title' => 'privacy')),
- _t('Privacy'));
- common_menu_item(common_local_url('doc', array('title' => 'source')),
- _t('Source'));
- common_menu_item(common_local_url('doc', array('title' => 'contact')),
- _t('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')
-{
- 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';
- }
- common_element('input', $attrs);
- # XXX: use a