__('Sessions', __FILE__), // getModuleInfo title 'summary' => __('Enables you to browse active database sessions.', __FILE__), // getModuleInfo summary 'version' => 2, 'permanent' => false, 'requires' => array('SessionHandlerDB'), ); } const pageName = 'sessions-db'; public function init() { return parent::init(); } public function ___execute() { // clean out any stray sessions that may have not yet hit the gc probability // because we don't want them in the list that we display $sessionHandlerDB = $this->modules->get('SessionHandlerDB'); $sessionHandlerDB->gc(wire('config')->sessionExpireSeconds); $useIP = $sessionHandlerDB->useIP; $useUA = $sessionHandlerDB->useUA; $mins = $this->input->post->mins; if(!$mins) $mins = (int) $this->session->ProcessSessionDB_mins; if(!$mins) $mins = 5; $this->session->ProcessSessionDB_mins = $mins; $form = wire('modules')->get('InputfieldForm'); $field = wire('modules')->get('InputfieldInteger'); $field->attr('name', 'mins'); $field->attr('value', $mins); $field->label = sprintf($this->_n('Sessions active in last minute', 'Sessions active in last %d minutes', $mins), $mins); $field->description = $this->_('Number of minutes'); $field->collapsed = Inputfield::collapsedYes; $form->add($field); $markup = wire('modules')->get('InputfieldMarkup'); $pagePaths = array(); $userNames = array(); $table = SessionHandlerDB::dbTableName; $database = $this->wire('database'); $sql = "SELECT user_id, pages_id, ip, ua, ts FROM `$table` " . "WHERE ts > '" . date('Y-m-d H:i:s', (time() - ($mins * 60))) . "' " . "ORDER BY ts DESC LIMIT 1000"; $query = $database->prepare($sql); $result = $query->execute(); $numRows = $query->rowCount(); if($numRows) { $table = wire('modules')->get('MarkupAdminDataTable'); $header = array( $this->_('Time'), $this->_('User'), $this->_('Page') ); if($useIP) $header[] = $this->_('IP Addr'); if($useUA) $header[] = $this->_('User Agent'); $table->headerRow($header); // for first iteration $row = $query->fetch(PDO::FETCH_NUM); while($row) { list($user_id, $pages_id, $ip, $ua, $ts) = $row; if(isset($userNames[$user_id])) { $userName = $userNames[$user_id]; } else { $user = wire('users')->get($user_id); $userName = $user && $user->id ? $user->name : '.'; $userNames[$user_id] = $userName; } if(isset($pagePaths[$pages_id])) { $pagePath = $pagePaths[$pages_id]; } else { $page = wire('pages')->get($pages_id); $pagePath = $page->id ? $page->path : '.'; $pagePaths[$pages_id] = $pagePath; } $tr = array(wireRelativeTimeStr($ts), $userName, $pagePath); if($useIP) $tr[] = long2ip($ip); if($useUA) $tr[] = strip_tags($ua); // note MarkupAdminDataTable already entity encodes all output $table->row($tr); // for next iteration $row = $query->fetch(PDO::FETCH_NUM); } $markup->value = $table->render(); } else { $markup->value = $this->_('No active sessions'); } $form->add($markup); $submit = wire('modules')->get('InputfieldSubmit'); $submit->attr('value', $this->_('Refresh')); $submit->attr('id+name', 'submit_session'); $submit->attr('class', $submit->attr('class') . ' head_button_clone'); $form->add($submit); $query->closeCursor(); return $form->render(); } /** * Called only when your module is installed * * This version creates a new page with this Process module assigned. * */ public function ___install() { // create the page our module will be assigned to $page = new Page(); $page->template = 'admin'; $page->name = self::pageName; // installs to the admin "Setup" menu ... change as you see fit $page->parent = $this->pages->get($this->config->adminRootPageID)->child('name=setup'); $page->process = $this; // we will make the page title the same as our module title // but you can make it whatever you want $info = self::getModuleInfo(); $page->title = $info['title']; // save the page $page->save(); // tell the user we created this page $this->message("Created Page: {$page->path}"); } /** * Called only when your module is uninstalled * * This should return the site to the same state it was in before the module was installed. * */ public function ___uninstall() { // find the page we installed, locating it by the process field (which has the module ID) // it would probably be sufficient just to locate by name, but this is just to be extra sure. $moduleID = $this->modules->getModuleID($this); $page = $this->pages->get("template=admin, process=$moduleID, name=" . self::pageName); if($page->id) { // if we found the page, let the user know and delete it $this->message("Deleting Page: {$page->path}"); $page->delete(); } } }