public static function init() { $lang_code = visitor::acceptedLanguageCode(); $lang_dir = __DIR__ . DS . '..' . DS . 'languages'; $lang_url = $lang_dir . DS . $lang_code . '.php'; $lang_url_default = $lang_dir . DS . 'en.php'; if (file_exists($lang_url)) { require_once $lang_url; } else { require_once $lang_url_default; } }
/** * Store a new comment in the database. * * @param string $hash Unique hash value of the parent page. * @return Response */ public function create($hash) { // Retrieve the parent page $page = $this->findPageByHash($hash); // Create a comment from the post data $comment = comment::fromInput(); $comment->set('page_uri', $page->uri()); // Collect user information $comment->set('author_ip', visitor::ip()); $comment->set('author_agent', visitor::ua()); // Handle signed-in users if ($user = user::current()) { $fullname = trim($user->firstname() . ' ' . $user->lastname()); $fullname = empty($fullname) ? $user->username() : $fullname; $comment->set('author', $fullname); $comment->set('author_email', $user->email()); $comment->set('username', $user->username()); } // Ensure the required comment fields are set if (!$comment->validate()) { $msg = l('comments.error.incomplete', 'Missing required fields'); return $this->error($msg, 400, array('input' => $comment->toArray(), 'errors' => $comment->errors()->toArray())); } // Check the honeypot fields. Pretend everything went fine. if ($this->isBot()) { return $this->success(); } // Throttle comment posting if ($this->isPartOfFlood($comment)) { $msg = l('comments.error.throttle', 'Number of allowed comments per interval exceeded'); return $this->error($msg, 429, array('input' => $comment->toArray(), 'errors' => array('other' => $msg))); } // Check for duplicate contents if ($this->isDuplicate($comment)) { $msg = l('comments.error.duplicate', 'Duplicate content'); return $this->error($msg, 409, array('input' => $comment->toArray(), 'errors' => array('text' => $msg))); } // Classify comment as spam or ham using Akismet. In addition allow to // blacklist authors. $discard = false; if ($this->isSpam($comment, $discard) || $this->isBlocked($comment)) { $comment->set('status', Comment::STATUS_SPAM); } // Save the comment to the database. Pretend the comment was saved // successfully for comments containing `blatant spam`. if ($discard && $comment->isSpam() || $comment->save()) { $msg = l('comments.success.saved', 'Comment saved'); return $this->success($msg, 201, array('id' => $comment->id())); } else { $msg = l('comments.error.save', 'Could not save comment'); return $this->error($msg, 400, array('input' => $comment->toArray(), 'errors' => $comment->errors()->toArray())); } }
public static function current() { $cookey = cookie::get('kirby'); $username = s::get('auth.username'); if (empty($cookey) or $cookey !== s::get('auth.key')) { static::logout(); return false; } if (s::get('auth.secret') !== sha1($username . $cookey)) { static::logout(); return false; } if (s::get('auth.ua') !== visitor::ua()) { static::logout(); return false; } // keep logged in for one week max. if (s::get('auth.created') < time() - 60 * 60 * 24 * 7) { static::logout(); return false; } // find the logged in user by token if ($user = site()->user($username)) { return $user; } else { return false; } }
} if ($email->send()) { return array('success' => true, 'message' => l::get('uniform-email-success')); } else { return array('success' => false, 'message' => l::get('uniform-email-error') . ' ' . $email->error()); } }; /* * Action to log the form data to a file */ uniform::$actions['log'] = function ($form, $actionOptions) { $file = a::get($actionOptions, 'file', false); if ($file === false) { throw new Exception('Uniform log action: No logfile specified!'); } $data = '[' . date('c') . '] ' . visitor::ip() . ' ' . visitor::userAgent(); foreach ($form as $key => $value) { $data .= "\n" . $key . ": " . $value; } $data .= "\n\n"; $success = file_put_contents($file, $data, FILE_APPEND | LOCK_EX); if ($success === false) { return array('success' => false, 'message' => l::get('uniform-log-error')); } else { return array('success' => true, 'message' => l::get('uniform-log-success')); } }; /* * Action to log in to the Kirby frontend */ uniform::$actions['login'] = function ($form, $actionOptions) {
public static function render_for(target $target) { visitor::set_target($target); return kernel::is_pss_mode() ? self::render_pss_for($target) : self::render_pjs_for($target); }
public static function save($filename, $text) { static $uri = ''; if (!defined('kern\\data_dir')) { return; } if ($uri === '' && !kernel::is_cli_mode()) { $uri = visitor::uri(); } $file = data_dir . '/log/debug_' . $filename . '.log'; @file_put_contents($file, '[' . clock::get_datetime() . '][' . $uri . '] - ' . $text . "\n", FILE_APPEND); }
public function testAcceptedLanguage() { $this->assertEquals(null, visitor::acceptedLanguage()); }
public static function build_csrf_url($csrf_role, $target, $echo = true, $for_html = true, $as_absolute = true) { if (!$target instanceof target) { $target = new target($target); } if (visitor::has_role($csrf_role)) { $role_secret = visitor::get_role_secret($csrf_role); $csrf_key = self::$base_csrf_key; if ($target->has_module()) { $csrf_key = self::$module_csrf_keys[$target->get_module_name()]; } $target->set_param($csrf_key, $role_secret); } return self::build_php_url($target, $echo, $for_html, $as_absolute); }
/** * Dirty browser sniffing for an ios device * * @return boolean */ public static function ios() { $ua = visitor::ua(); return str::contains($ua, 'iPod') or str::contains($ua, 'iPhone') or str::contains($ua, 'iPad'); }
/** * 动作:删除用户 * * @param $id */ public function getDelete(Request $request) { $visitor = visitor::find($request->input('id')); $visitor->delete(); $data = array('ret' => 0, 'msg' => '删除成功'); echo json_encode($data); }
<?php /** * 来访者,是对客户端用户的抽象。来访者请求服务,并且带回响应 * * @copyright Copyright (c) 2009-2015 Jingcheng Zhang <*****@*****.**>. All rights reserved. * @license See "LICENSE" file bundled with this distribution. */ namespace kern; visitor::__init__(); // [实体] 来访者 class visitor { public static function g_has($key) { return self::r_has('gets', $key); } public static function g_int($key, $default_value = 0) { return self::r_int('gets', $key, $default_value); } public static function g_str($key, $default_value = '') { return self::r_str('gets', $key, $default_value); } public static function g_arr($key, array $default_value = []) { return self::r_arr('gets', $key, $default_value); } public static function g($key = '', $default_value = null)
public static function set() { self::$current = visitor::acceptedLanguageCode(); }
protected static function send($content, $content_type = 'text/html; charset=utf-8') { visitor::set_content_type($content_type); visitor::set_content($content); throw new dispatch_return(); }
/** * Prepare the request data send to the Akismet API * * @param array $content Comment contents to send. * @param string $userIp IP address of the comment submitter. * @param string $userAgent User agent string of the web browser submitting the comment. * * @return array */ protected function prepareContent($content = array(), $userIp = null, $userAgent = null) { if (empty($content['comment_type'])) { $content['comment_type'] = 'comment'; } if (is_null($userIp)) { $content['user_ip'] = visitor::ip(); } else { if (!empty($userIp)) { $content['user_ip'] = $userIp; } } if (is_null($userAgent)) { $content['user_agent'] = visitor::ua(); } else { if (!empty($userAgent)) { $content['user_agent'] = $userAgent; } } return $content; }
<!DOCTYPE html> <html lang="<?php echo site()->language()->code(); ?> "> <head> <meta charset="utf-8"> <?php if (strpos(visitor::ua(), 'MSIE') !== false) { header('X-UA-Compatible: IE=edge,chrome=1'); } ?> <meta http-equiv="cache-control" content="max-age=0" /> <meta http-equiv="cache-control" content="no-cache" /> <meta http-equiv="expires" content="0" /> <meta http-equiv="expires" content="Tue, 01 Jan 1980 1:00:00 GMT" /> <meta http-equiv="pragma" content="no-cache" /> <meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="description" content="Risky-B batteria per la valutazione della propensione al rischio"> <meta name="keywords" content="rischio propensione incertezza psicologia risk uncertainty psychology"> <title><?php echo html($site->title() . ' / ' . $page->title()); ?> </title> <!-- LOAD BOOTLINT -->
/** * Return a hashed version of the visitor ip * * @return string */ protected function visitorId() { return sha1(visitor::ip()); }
/** * Tries to find the language for the current visitor * * @return Language */ public function visitorLanguage() { return $this->languages()->find(visitor::acceptedLanguageCode()); }
/** * Site routes */ public static function find($route, $params, $request) { visitor::save_update_current(); maintenance::delete_inactive_visitors(); extract($params); if (!isset($controller)) { $controller = 'content'; } $controller = strtolower($controller); $guid = $controller . '/' . $action; if ($action == 'index') { $guid = $controller; } $controllerfile = ucfirst($controller); $action = isset($params['action']) ? $params['action'] : 'index'; //$action = ucfirst($action); $slug = isset($params['slug']) ? $params['slug'] : ''; $slug2 = isset($params['slug2']) ? $params['slug2'] : ''; $slug3 = isset($params['slug3']) ? $params['slug3'] : ''; $slug4 = isset($params['slug4']) ? $params['slug4'] : ''; $slug5 = isset($params['slug5']) ? $params['slug5'] : ''; // Homepage if ($guid == 'content') { return array('controller' => 'Site', 'action' => 'index'); } // Page alias if ($controller == 'test') { return array('controller' => 'Page', 'action' => 'test'); } if ($controller == 'contact') { return array('controller' => 'Page', 'action' => 'contact'); } if ($controller == 'challenge') { if ($action == 'wall-of-fame') { return array('controller' => 'Games', 'action' => 'walloffame'); } elseif ($action == 'index') { return array('controller' => 'Games', 'action' => 'challenge'); } } if ($controller == 'leaderboard') { return array('controller' => 'Games', 'action' => 'leaderboard'); } if ($controller == 'write') { $todayslug = site::day_slug(); if (user::logged()) { $todayslug = user::get()->today_slug(); } if (empty($action) || $action == 'index') { $action = $todayslug; } $page = false; if (user::logged()) { $page = ORM::factory('Page')->where('user_id', '=', user::get()->id)->where('type', '=', 'page')->where('day', '=', $action)->find(); if (!$page->loaded() && $action == $todayslug) { $page = ORM::factory('Page')->where('user_id', '=', user::get()->id)->where('type', '=', 'autosave')->where('day', '=', $action)->find(); // It's today, but todays page doesn't exist yet. Create it if (!$page->loaded()) { $page->type = 'autosave'; $page->save(); } } } if (user::logged() && ($page && $page->loaded()) && $slug == 'stats') { return array('controller' => 'Write', 'action' => 'pagestats', 'page' => $page); } if (user::logged() && ($page && $page->loaded()) || !user::logged()) { return array('controller' => 'Write', 'action' => 'write', 'page' => $page, 'daystamp' => $action); } else { return array('controller' => 'Write', 'action' => 'daynotfound'); } } if ($controller == 'read') { return array('controller' => 'Page', 'action' => 'read', 'id' => $action); } if ($controller == 'user') { if ($action != '') { if ($action == 'password') { return array('controller' => 'User', 'action' => 'password', 'token' => $slug); } if (in_array($action, user::reservednames())) { return array('controller' => 'User', 'action' => $action); } // We're either looking at a user's public profile or 404'd $user = ORM::factory('User')->where('slug', '=', $action)->find(); if ($user->loaded()) { if ((bool) $user->option('public') || user::logged('admin')) { return array('controller' => 'Me', 'action' => 'profile', 'user' => $user); } else { return array('controller' => 'Me', 'action' => 'notpublic'); } } else { return array('controller' => 'Errors', 'action' => '404', 'params' => $params); } } else { return array('controller' => 'User', 'action' => 'options'); } } // Pages/Content $content = ORM::factory('Content'); if (!user::logged('admin')) { $content = $content->where('status', '=', 'active'); } $content = $content->where('guid', '=', $guid)->find(); if ($content->loaded()) { // Specific content $class = 'Content'; if (class_exists('Controller_' . ucfirst($content->contenttype->type))) { $class = ucfirst($content->contenttype->type); } $action = 'default'; if ($content->contenttypetype_id != 0) { if (method_exists('Controller_' . $class, 'action_' . $content->contenttypetype->key)) { $action = $content->contenttypetype->key; } } $content->hit(); return array('controller' => $class, 'action' => $action, 'content' => $content); } else { // Index page for contenttype if ($action == 'index') { $contenttype = $controller; if (class_exists('Controller_' . ucfirst($contenttype))) { $class = ucfirst($contenttype); return array('controller' => $class, 'action' => 'index'); } } } // "Static" controllers $file = 'application/classes/Controller/' . $controllerfile . '.php'; if (file_exists($file) && method_exists('Controller_' . ucfirst($controllerfile), 'action_' . $action)) { $return = array(); $return['controller'] = $controllerfile; $return['action'] = isset($action) ? $action : 'index'; $return['id'] = isset($slug) ? $slug : ''; $return['params'] = $params; return $return; } // No matches. 404 return array('controller' => 'Errors', 'action' => '404', 'params' => $params); }