/** * Call this function only after we have successfully logged in. * Updates user status etc. */ function handle_post_login() { global $messages; if (!isset($messages)) { // we might be in auto-login, create a temporary message field anyway $messages = array(); } $user = get_user(user_id()); // display warning if account was disabled if ($user['is_disabled']) { $messages[] = t("Your account was disabled :ago due to inactivity; your account is now re-enabled, and account data will be updated again soon.", array(':ago' => recent_format($user['disabled_at']))); $q = db()->prepare("UPDATE user_properties SET is_disabled=0,logins_after_disabled=logins_after_disabled+1 WHERE id=?"); $q->execute(array($user['id'])); } // keep track of users that logged in after receiving a warning if ($user['is_disable_warned']) { $q = db()->prepare("UPDATE user_properties SET is_disable_warned=0,logins_after_disable_warned=logins_after_disable_warned+1 WHERE id=?"); $q->execute(array($user['id'])); } // update locale if ($user['locale']) { I18n::setLocale($user['locale']); } // update login time $query = db()->prepare("UPDATE user_properties SET last_login=NOW(),is_disabled=0 WHERE id=?"); $query->execute(array($user["id"])); // if we don't have an IP set, update it now if (!$user["user_ip"]) { $q = db()->prepare("UPDATE user_properties SET user_ip=? WHERE id=?"); $q->execute(array(user_ip(), $user['id'])); } }
/** * A default locale is included. */ function testAllLocalesIncludesDefault() { foreach (I18n::getAvailableLocales() as $localeInstance) { if ($localeInstance instanceof Openclerk\DefaultLocale) { return; } } $this->fail("Could not find DefaultLocale"); }
/** * Tests that all locales defined by {@link get_all_locales()} exist. */ function testAllLocales() { foreach (I18n::getAvailableLocales() as $locale => $instance) { if ($locale == 'en') { continue; } $f = __DIR__ . "/../locale/" . $locale . ".php"; $this->assertTrue(file_exists($f), "Locale file " . $f . " should exist"); } }
function page_header_old($page_title, $page_id = false, $options = array()) { define('PAGE_RENDER_START', microtime(true)); header('Content-type: text/html; charset=utf-8'); $html_classes = array(); if (has_required_admin()) { $html_classes[] = "body_admin"; } $html_classes[] = get_site_config('site_id'); if (is_admin()) { $html_classes[] = "is_admin"; } ?> <!DOCTYPE HTML> <html<?php echo " class=\"" . implode(" ", $html_classes) . "\""; ?> > <head> <title><?php echo htmlspecialchars($page_title); if (has_required_admin()) { echo " [admin]"; } ?> </title> <link rel="stylesheet" type="text/css" href="<?php echo htmlspecialchars(url_for('styles/generated.css' . '?' . get_site_config('openclerk_version'))); ?> " /> <link rel="stylesheet" type="text/css" href="<?php echo htmlspecialchars(url_for(get_site_config('default_css') . '?' . get_site_config('openclerk_version'))); ?> " /> <?php if (get_site_config('custom_css')) { ?> <link rel="stylesheet" type="text/css" href="<?php echo htmlspecialchars(url_for(get_site_config('custom_css') . '?' . get_site_config('openclerk_version'))); ?> " /> <?php } ?> <?php if (has_required_admin()) { ?> <link rel="stylesheet" type="text/css" href="<?php echo htmlspecialchars(url_for('admin.css' . '?' . get_site_config('openclerk_version'))); ?> " /> <?php } ?> <?php if (isset($options["refresh"])) { ?> <meta http-equiv="refresh" content="<?php echo htmlspecialchars($options['refresh']); ?> "> <?php } ?> <script type="text/javascript" src="<?php echo htmlspecialchars(url_for('js/jquery-1.9.1.min.js')); ?> "></script> <script type="text/javascript" src="<?php echo htmlspecialchars(url_for('js/common.js' . '?' . get_site_config('openclerk_version'))); ?> "></script> <script type="text/javascript" src="<?php echo htmlspecialchars(url_for('js/locale/' . I18n::getCurrentLocale() . '.js?' . get_site_config('openclerk_version'))); ?> "></script> <?php if (isset($options['jsapi']) && $options['jsapi']) { ?> <script type="text/javascript" src="https://www.google.com/jsapi"></script> <script type="text/javascript"> <?php $user = user_logged_in() ? get_user(user_id()) : false; if ($user) { if ($user['disable_graph_refresh'] || isset($graph_type['no_refresh']) && $graph_type['no_refresh']) { $timeout = 0; // disable refresh } else { $timeout = get_premium_value(get_user(user_id()), 'graph_refresh'); } } else { $timeout = get_site_config('graph_refresh_public'); } // TODO move this into a more helpful location rather than in the template head ?> window.UserGraphRefresh = <?php echo $timeout * 1000 * 60; ?> ; // ms </script> <?php } ?> <?php if (isset($options["js"]) && $options["js"]) { if (!is_array($options['js'])) { $options['js'] = array($options['js']); } foreach ($options['js'] as $js) { $js_hash = ""; if (strpos($js, "?") !== false) { $js_hash = "&" . substr($js, strpos($js, "?") + 1); $js = substr($js, 0, strpos($js, "?")); } ?> <script type="text/javascript" src="<?php echo htmlspecialchars(url_for('js/' . $js . '.js' . '?' . get_site_config('openclerk_version') . $js_hash)); ?> "></script> <?php } } ?> <?php require_template("templates_head"); ?> <?php $head_compiled = __DIR__ . "/../site/head-compiled.html"; if (file_exists($head_compiled)) { require $head_compiled; } else { // fix relative paths $input = file_get_contents(__DIR__ . "/head.html"); $input = str_replace("src=\"", "src=\"" . htmlspecialchars(calculate_relative_path()), $input); echo $input; } ?> </head> <body<?php if ($page_id) { echo ' id="' . $page_id . '"'; } if (isset($options['class'])) { echo " class=\"" . htmlspecialchars($options['class']) . "\""; } ?> > <div class="body_wrapper"> <?php require_template("templates_header"); ?> <div id="navigation"> <ul> <li class="home"><a href="<?php echo url_for('index'); ?> " title="<?php echo htmlspecialchars(get_site_config('site_name')); ?> "><span class="text"><?php echo htmlspecialchars(get_site_config('site_name')); ?> </span></a></li> <?php if (user_logged_in()) { ?> <li class="profile"><a href="<?php echo url_for('profile'); ?> " title="<?php echo ht("Your Reports"); ?> "><span class="text"><?php echo ht("Your Reports"); ?> </span><span class="responsive-text"><?php echo ht("Reports"); ?> </span></a></li> <li class="finance"><a href="<?php echo url_for('your_transactions'); ?> " title="<?php echo ht("Finance"); ?> "><span class="text"><?php echo ht("Finance"); ?> </span></a></li> <li class="accounts"><a href="<?php echo url_for('wizard_currencies'); ?> " title="<?php echo ht("Configure Accounts"); ?> "><span class="text"><?php echo ht("Configure Accounts"); ?> </span><span class="responsive-text"><?php echo ht("Configure"); ?> </span></a></li> <li class="user"><a href="<?php echo url_for('user'); ?> " title="<?php echo ht("User Profile"); ?> "><span class="text"><?php echo ht("User Profile"); ?> </span></a></li> <li class="logout"><a href="<?php echo url_for('login', array('logout' => 1)); ?> " title="<?php echo ht("Logout"); ?> "><span class="text"><?php echo ht("Logout"); ?> </span></a></li> <?php if (is_admin()) { ?> <li class="admin"><a href="<?php echo url_for('admin'); ?> " title="<?php echo ht("Admin"); ?> "><span class="text"><?php echo ht("Admin"); ?> </span></a></li> <?php } ?> <?php } else { ?> <li class="signup"><a href="<?php echo url_for('signup'); ?> " title="<?php echo ht("Signup"); ?> "><span class="text"><?php echo ht("Signup"); ?> </span></a></li> <li class="login"><a href="<?php echo url_for('login'); ?> " title="<?php echo ht("Login"); ?> "><span class="text"><?php echo ht("Login"); ?> </span></a></li> <?php } ?> <li class="premium"><a href="<?php echo url_for('premium'); ?> " title="<?php echo ht("Premium"); ?> "><span class="text"><?php echo ht("Premium"); ?> </span></a></li> <li class="help"><a href="<?php echo url_for('help'); ?> " title="<?php echo ht("Help"); ?> "><span class="text"><?php echo ht("Help"); ?> </span></a></li> </ul> </div> <?php if (did_autologin()) { ?> <div id="autologin"> <?php echo t("Automatically logged in. Hi, :user!", array(':user' => "<a href=\"" . url_for('user') . "\" class=\"disabled\">" . ($_SESSION["user_name"] ? htmlspecialchars($_SESSION["user_name"]) : "<i>" . t("anonymous") . "</i>") . "</a>")); ?> (<a href="<?php echo url_for('login', array('logout' => 1)); ?> "><?php echo ht("This isn't me."); ?> </a>) </div> <?php } ?> <div id="page_content"> <?php // always display messages on every page as necessary display_messages(); }
{ return $this->title; } function load() { require __DIR__ . "/../locale/" . $this->key . ".php"; return $result; } } $locales = array('de' => 'German', 'fr' => 'French', 'jp' => 'Japanese', 'ru' => 'Russian', 'zh' => 'Chinese'); foreach ($locales as $locale => $title) { I18n::addAvailableLocale(new GenericLocale($locale, $title)); } I18n::addDefaultKeys(array(':site_name' => get_site_config('site_name'))); // set locale as necessary if (isset($_COOKIE["locale"]) && in_array($_COOKIE["locale"], array_keys(I18n::getAvailableLocales()))) { I18n::setLocale($_COOKIE["locale"]); } \Openclerk\Events::on('i18n_missing_string', function ($data) { $locale = $data['locale']; $key = $data['key']; log_uncaught_exception(new LocaleException("Locale '{$locale}': Missing key '{$key}'")); }); /** * Helper function to mark strings that need to be translated on the client-side. */ function ct($s) { // do not do any translation here - we have to do it on the client side! return $s; }
<?php use Openclerk\I18n; ?> <form action="<?php echo htmlspecialchars(url_for('set_locale')); ?> " method="post" id="locale_selector"> <select class="language-list locale locale-<?php echo htmlspecialchars(I18n::getCurrentLocale()); ?> " name="locale"> <?php foreach (I18n::getAvailableLocales() as $locale) { $selected = I18n::getCurrentLocale() == $locale->getKey(); echo "<option value=\"" . htmlspecialchars($locale->getKey()) . "\" class=\"locale locale-" . htmlspecialchars($locale->getKey()) . "\"" . ($selected ? " selected" : "") . ">" . htmlspecialchars($locale->getTitle()) . "</option>\n"; } ?> </select> <input type="hidden" name="redirect" value="<?php echo htmlspecialchars(url_for(request_url_relative(), $_GET)); ?> "> </form>
/** * Tests {@link I18n#stripKey()} */ function testStripKey() { $this->assertEquals("hello", I18n::stripKey("hello")); $this->assertEquals("hello world hello world", I18n::stripKey("hello world hello world")); $this->assertEquals("hello world hello world", I18n::stripKey("\n hello world\n hello world\n ")); $this->assertEquals("hello world hello world", I18n::stripKey("hello\tworld\thello\tworld")); $this->assertEquals("hello world hello world", I18n::stripKey("hello world\nhello world")); }
<?php /** * Set the current session, cookie or user language. */ use Openclerk\I18n; $locale = require_post("locale"); $redirect = require_post("redirect"); $available = I18n::getAvailableLocales(); if (!isset($available[$locale])) { throw new LocaleException("Locale '{$locale}' does not exist for user selection"); } I18n::setLocale($locale); // update cookies setcookie('locale', $locale, time() + 60 * 60 * 24 * 365 * 10); // update users if (user_logged_in()) { $user = get_user(user_id()); $q = db()->prepare("UPDATE user_properties SET locale=? WHERE id=?"); $q->execute(array($locale, user_id())); } // go back to their previous page redirect($redirect);