function index($version = '0', $operation = null, $var = null) { switch ($operation) { case 'user': $this->load->library('cache'); $this->load->helper('gfx'); $jsonObj = $this->cache->get(strtolower($var), 'user-api'); if (!$jsonObj) { $jsonObj = array(); $this->load->config('gfx'); if (!preg_match('/^[a-zA-Z0-9_\\-]+$/', $var) || strlen($var) < 3 || strlen($var) > 200 || substr($var, 0, 8) === '__temp__' || in_array($var, $this->config->item('gfx_badname'))) { $jsonObj['error'] = 'No such user.'; break; } $this->load->database(); $user = $this->db->query('SELECT login, id, name, email, title, avatar, bio, web, blog, recommendation, forum_id, forum_username, feature_0, feature_1, feature_2, count ' . 'FROM users WHERE `ready` = \'Y\' AND `name` = ' . $this->db->escape($var) . ' LIMIT 1'); if ($user->num_rows() === 0) { $jsonObj['error'] = 'No such user.'; break; } $jsonObj['user'] = $user->row_array(); $user->free_result(); $jsonObj['user']['avatar'] = avatarURL($jsonObj['user']['avatar'], $jsonObj['user']['email'], $jsonObj['user']['login'], '&'); $jsonObj['user']['features'] = array(); for ($i = 0; $i < 3; $i++) { $feature = $this->db->query('SELECT name, title, description FROM features ' . 'WHERE `id` = ' . $jsonObj['user']['feature_' . $i] . ';'); $jsonObj['user']['features'][] = $feature->row_array(); $feature->free_result(); } unset($feature); $addons = $this->db->query('SELECT t1.amo_id, t1.amo_version, t1.description, t1.icon_url, t1.title, ' . 't2.group_id FROM addons t1, u2a t2 ' . 'WHERE t2.addon_id = t1.id AND t2.user_id = ' . $jsonObj['user']['id'] . ' ORDER BY t2.order ASC;'); $A = array(); foreach ($addons->result_array() as $addon) { if (!isset($A[$addon['group_id']])) { $A[$addon['group_id']] = array(); } $A[$addon['group_id']][] = $addon; } $addons->free_result(); unset($addons, $addon); $groups = $this->db->query('SELECT t1.id, t1.name, t1.title, t1.description FROM groups t1, u2g t2 ' . 'WHERE t2.group_id = t1.id AND t2.user_id = ' . $jsonObj['user']['id'] . ' ORDER BY t2.order ASC;'); $jsonObj['user']['groups'] = array(); foreach ($groups->result_array() as $group) { $group['addons'] = $A[$group['id']]; unset($group['id']); $jsonObj['user']['groups'][] = $group; } $groups->free_result(); unset($groups, $group, $A); unset($jsonObj['user']['id'], $jsonObj['user']['login'], $jsonObj['user']['email'], $jsonObj['user']['feature_0'], $jsonObj['user']['feature_1'], $jsonObj['user']['feature_2']); $this->load->_ci_cached_vars = array(); //Clean up cached vars $this->load->config('gfx'); $jsonObj['cache_expiry'] = $this->cache->save(strtolower($var), $jsonObj, 'user-api', $this->config->item('gfx_cache_time')); } else { $jsonObj['expiry'] = $this->cache->get_expiry($var, 'user-api'); } break; default: $jsonObj['error'] = 'Operation undefined.'; break; } parse_str($this->input->server('QUERY_STRING'), $G); if (isset($G['callback'])) { $this->load->view('json.php', array('jsonObj' => $jsonObj, 'jsonpCallback' => $G['callback'])); } else { $this->load->view('json.php', array('jsonObj' => $jsonObj)); } }
<?php $this->load->config('gfx'); $this->load->helper('gfx'); $avatar = avatarURL($avatar, $email, $login); ?> <div id="editor-save"> <p><button id="editor-save-button">儲存您的頁面</button>編輯完畢,儲存變更、領取宣傳貼紙!</p> </div> <div id="window_almostdone" class="window" title="快完成了..."> <p>請設定您的推薦網頁的專用網址,需使用英數字:</p> <p><?php print base_url() . form_input(array('id' => 'name', 'value' => '')); ?> </p> </div> <div id="window_editcomplete" class="window" title="完成!"> <p>感謝您向大家推薦 Mozilla Firefox,接下來...</p> <ul> <li>看看<a class="userpage-url" href="#">您的個人推薦網頁</a></li> <li>領取<a href="./sticker">宣傳貼紙</a>,在部落格、論壇宣傳!</li> </ul> <div class="shareblock"> <p>和大家分享您的抓火狐推薦頁!</p> <ul> <li><a class="newwindow" title="分享到 Facebook" href="https://www.facebook.com/sharer.php?u=<?php /* Facebook fetches sticker image and description from <head> */ print urlencode(site_url('PLACEHOLDER')); ?> "><span class="sprite facebook"></span>Facebook</a></li> <li><a class="newwindow" title="噗到 Plurk" href="http://plurk.com/?status=<?php
function userlist($type = '') { /* function name cannot be list() */ switch ($type) { case 'random-avatars': /* Here is what we do: random a number, see if the cache exists, if so output it, if not then generate one then saves it. these cache have short ttl because we do not check the data within against database. */ /* Prevent users from sending requests at this URL less than 60 secs of peroid. Responsible browsers will serve cache to xhr request if request took places less than 60 sec. */ header('Cache-Control: max-age=60, must-revalidate'); case 'random-avatars-reload': /* No Cache-Control header for this URL. */ /* No Cache-Control header for this URL. */ case 'random-avatars-frame': /* Output webpage or json will be decided later. */ $this->load->library('cache'); $i = rand(0, 99); $users = $this->cache->get($i, 'random-avatars'); if (!$users) { $this->load->database(); $this->load->helper('gfx'); /* Really expensive query, should change it right away should user > 1000 or fill the cache by using crontab instead of user request */ $query = $this->db->query('SELECT `login`, `name`, `title`, `avatar`, `email` FROM `users` WHERE `avatar` != \'\' AND `ready` = \'Y\' AND `shown` = \'Y\' ORDER BY RAND() LIMIT 10;'); $users = array(); foreach ($query->result_array() as $user) { $users[] = array('name' => $user['name'], 'title' => $user['title'], 'avatar' => avatarURL($user['avatar'], $user['email'], $user['login'], '&')); } $this->cache->save($i, $users, 'random-avatars', 300); } if ($type === 'random-avatars-frame') { $this->load->view('user/random-avatars.php', array('users' => $users)); } else { $this->load->view('json.php', array('jsonObj' => array('users' => $users))); } break; default: json_message('Invalid List type'); } }