From a4ee4be2b43318a91b893de27b86407cbd499144 Mon Sep 17 00:00:00 2001 From: "Michael Kaufmann (d00p)" Date: Mon, 18 Nov 2013 09:09:06 +0100 Subject: [PATCH] migrate paging class to new PDO database class, refs #1287 Signed-off-by: Michael Kaufmann (d00p) --- admin_admins.php | 2 +- admin_cronjobs.php | 2 +- admin_customers.php | 2 +- admin_domains.php | 2 +- admin_ipsandports.php | 2 +- admin_logger.php | 2 +- admin_tickets.php | 6 +- lib/classes/output/class.paging.php | 303 +++++++++++----------------- 8 files changed, 123 insertions(+), 198 deletions(-) diff --git a/admin_admins.php b/admin_admins.php index d436cf21..fc7de9e1 100644 --- a/admin_admins.php +++ b/admin_admins.php @@ -42,7 +42,7 @@ if ($page == 'admins' 'traffic_used' => $lng['customer']['traffic'] . ' (' . $lng['panel']['used'] . ')', 'deactivated' => $lng['admin']['deactivated'] ); - $paging = new paging($userinfo, $db, TABLE_PANEL_ADMINS, $fields, $settings['panel']['paging'], $settings['panel']['natsorting']); + $paging = new paging($userinfo, TABLE_PANEL_ADMINS, $fields, $settings['panel']['paging'], $settings['panel']['natsorting']); $admins = ''; $result_stmt = Database::query("SELECT * FROM `" . TABLE_PANEL_ADMINS . "` " . $paging->getSqlWhere(false) . " " . $paging->getSqlOrderBy() . " " . $paging->getSqlLimit()); $numrows_admins = Database::num_rows(); diff --git a/admin_cronjobs.php b/admin_cronjobs.php index cb6cf445..9eac5100 100644 --- a/admin_cronjobs.php +++ b/admin_cronjobs.php @@ -33,7 +33,7 @@ if ($page == 'cronjobs' || $page == 'overview') { 'c.interval' => $lng['cron']['interval'], 'c.isactive' => $lng['cron']['isactive'] ); - $paging = new paging($userinfo, $db, TABLE_PANEL_CRONRUNS, $fields, $settings['panel']['paging'], $settings['panel']['natsorting']); + $paging = new paging($userinfo, TABLE_PANEL_CRONRUNS, $fields, $settings['panel']['paging'], $settings['panel']['natsorting']); /* * @TODO Fix sorting diff --git a/admin_customers.php b/admin_customers.php index 7de5d2eb..65e59254 100644 --- a/admin_customers.php +++ b/admin_customers.php @@ -51,7 +51,7 @@ if ($page == 'customers' $field['c.backup_allowed'] = $lng['backup_allowed']; } - $paging = new paging($userinfo, $db, TABLE_PANEL_CUSTOMERS, $fields, $settings['panel']['paging'], $settings['panel']['natsorting']); + $paging = new paging($userinfo, TABLE_PANEL_CUSTOMERS, $fields, $settings['panel']['paging'], $settings['panel']['natsorting']); $customers = ''; $result_stmt = Database::prepare(" SELECT `c`.*, `a`.`loginname` AS `adminname` diff --git a/admin_domains.php b/admin_domains.php index 2bbf6dc3..dccff90b 100644 --- a/admin_domains.php +++ b/admin_domains.php @@ -51,7 +51,7 @@ if ($page == 'domains' 'c.loginname' => $lng['login']['username'], 'd.aliasdomain' => $lng['domains']['aliasdomain'] ); - $paging = new paging($userinfo, $db, TABLE_PANEL_DOMAINS, $fields, $settings['panel']['paging'], $settings['panel']['natsorting']); + $paging = new paging($userinfo, TABLE_PANEL_DOMAINS, $fields, $settings['panel']['paging'], $settings['panel']['natsorting']); $domains = ''; $result_stmt = Database::prepare(" SELECT `d`.*, `c`.`loginname`, `c`.`name`, `c`.`firstname`, `c`.`company`, `c`.`standardsubdomain`, `ad`.`id` AS `aliasdomainid`, `ad`.`domain` AS `aliasdomain` diff --git a/admin_ipsandports.php b/admin_ipsandports.php index 66a4e9eb..518df119 100644 --- a/admin_ipsandports.php +++ b/admin_ipsandports.php @@ -37,7 +37,7 @@ if ($page == 'ipsandports' 'ip' => $lng['admin']['ipsandports']['ip'], 'port' => $lng['admin']['ipsandports']['port'] ); - $paging = new paging($userinfo, $db, TABLE_PANEL_IPSANDPORTS, $fields, $settings['panel']['paging'], $settings['panel']['natsorting']); + $paging = new paging($userinfo, TABLE_PANEL_IPSANDPORTS, $fields, $settings['panel']['paging'], $settings['panel']['natsorting']); $ipsandports = ''; $result_stmt = Database::prepare("SELECT * FROM `" . TABLE_PANEL_IPSANDPORTS . "` " . $paging->getSqlWhere(false) . " " . $paging->getSqlOrderBy() . " " . $paging->getSqlLimit()); Database::pexecute($result_stmt); diff --git a/admin_logger.php b/admin_logger.php index 80433107..d64f80da 100644 --- a/admin_logger.php +++ b/admin_logger.php @@ -31,7 +31,7 @@ if ($page == 'log' 'user' => $lng['logger']['user'], 'text' => $lng['logger']['action'] ); - $paging = new paging($userinfo, $db, TABLE_PANEL_LOG, $fields, $settings['panel']['paging'], $settings['panel']['natsorting']); + $paging = new paging($userinfo, TABLE_PANEL_LOG, $fields, $settings['panel']['paging'], $settings['panel']['natsorting']); $paging->sortfield = 'date'; $paging->sortorder = 'desc'; $result_stmt = Database::query(' diff --git a/admin_tickets.php b/admin_tickets.php index c4d5e576..ca48070b 100644 --- a/admin_tickets.php +++ b/admin_tickets.php @@ -69,7 +69,7 @@ if ($page == 'tickets' 'subject' => $lng['ticket']['subject'], 'lastreplier' => $lng['ticket']['lastreplier'] ); - $paging = new paging($userinfo, $db, TABLE_PANEL_TICKETS, $fields, $settings['panel']['paging'], $settings['panel']['natsorting']); + $paging = new paging($userinfo, TABLE_PANEL_TICKETS, $fields, $settings['panel']['paging'], $settings['panel']['natsorting']); $paging->sortfield = 'lastchange'; $paging->sortorder = 'desc'; $result_stmt = Database::prepare(" @@ -475,7 +475,7 @@ if ($page == 'tickets' if ($userinfo['tickets_see_all'] != '1') { $where = " `main`.`adminid` = :adminid"; } - $paging = new paging($userinfo, $db, TABLE_PANEL_TICKET_CATS, $fields, $settings['panel']['paging'], $settings['panel']['natsorting']); + $paging = new paging($userinfo, TABLE_PANEL_TICKET_CATS, $fields, $settings['panel']['paging'], $settings['panel']['natsorting']); $result_stmt = Database::prepare(" SELECT `main`.`id`, `main`.`name`, `main`.`logicalorder`, ( SELECT COUNT(`sub`.`id`) FROM `" . TABLE_PANEL_TICKETS . "` `sub` @@ -645,7 +645,7 @@ if ($page == 'tickets' 'lastreplier' => $lng['ticket']['lastreplier'], 'priority' => $lng['ticket']['priority'] ); - $paging = new paging($userinfo, $db, TABLE_PANEL_TICKETS, $fields, $settings['panel']['paging'], $settings['panel']['natsorting']); + $paging = new paging($userinfo, TABLE_PANEL_TICKETS, $fields, $settings['panel']['paging'], $settings['panel']['natsorting']); $result_stmt = Database::prepare($query . $paging->getSqlWhere(true) . " " . $paging->getSqlOrderBy() . " " . $paging->getSqlLimit()); Database::pexecute($result_stmt, $archive_params); $sortcode = $paging->getHtmlSortCode($lng); diff --git a/lib/classes/output/class.paging.php b/lib/classes/output/class.paging.php index 6fdfdda6..505d378c 100644 --- a/lib/classes/output/class.paging.php +++ b/lib/classes/output/class.paging.php @@ -21,91 +21,72 @@ * Class to manage paging system * @package Functions */ +class paging { -class paging -{ /** * Userinfo * @var array */ - var $userinfo = array(); - /** - * Database handler - * @var db - */ - - var $db = false; - /** * MySQL-Table * @var string */ - var $table = ''; /** * Fields with description which should be selectable * @var array */ - var $fields = array(); /** * Entries per page * @var int */ - var $entriesperpage = 0; /** * Number of entries of table * @var int */ - var $entries = 0; /** * Sortorder, asc or desc * @var string */ - var $sortorder = 'asc'; /** * Sortfield * @var string */ - var $sortfield = ''; /** * Searchfield * @var string */ - var $searchfield = ''; /** * Searchtext * @var string */ - var $searchtext = ''; /** * Pagenumber * @var int */ - var $pageno = 0; /** * Switch natsorting on/off * @var bool */ - var $natSorting = false; /** @@ -117,17 +98,14 @@ class paging * @param int entries per page * @param bool Switch natsorting on/off (global, affects all calls of sort) */ + function paging($userinfo, $table, $fields, $entriesperpage, $natSorting = false) { - function paging($userinfo, $db, $table, $fields, $entriesperpage, $natSorting = false) - { $this->userinfo = $userinfo; - if(!is_array($this->userinfo['lastpaging'])) - { + if (!is_array($this->userinfo['lastpaging'])) { $this->userinfo['lastpaging'] = unserialize($this->userinfo['lastpaging']); } - $this->db = $db; $this->table = $table; $this->fields = $fields; $this->entriesperpage = $entriesperpage; @@ -135,42 +113,39 @@ class paging $checklastpaging = (isset($this->userinfo['lastpaging']['table']) && $this->userinfo['lastpaging']['table'] == $this->table); $this->userinfo['lastpaging']['table'] = $this->table; - if(isset($_REQUEST['sortorder']) - && (strtolower($_REQUEST['sortorder']) == 'desc' || strtolower($_REQUEST['sortorder']) == 'asc')) - { + if (isset($_REQUEST['sortorder']) + && (strtolower($_REQUEST['sortorder']) == 'desc' + || strtolower($_REQUEST['sortorder']) == 'asc') + ) { $this->sortorder = strtolower($_REQUEST['sortorder']); - } - else - { - if($checklastpaging - && isset($this->userinfo['lastpaging']['sortorder']) - && (strtolower($this->userinfo['lastpaging']['sortorder']) == 'desc' || strtolower($this->userinfo['lastpaging']['sortorder']) == 'asc')) - { + + } else { + + if ($checklastpaging + && isset($this->userinfo['lastpaging']['sortorder']) + && (strtolower($this->userinfo['lastpaging']['sortorder']) == 'desc' + || strtolower($this->userinfo['lastpaging']['sortorder']) == 'asc') + ) { $this->sortorder = strtolower($this->userinfo['lastpaging']['sortorder']); - } - else - { + + } else { $this->sortorder = 'asc'; } } $this->userinfo['lastpaging']['sortorder'] = $this->sortorder; - if(isset($_REQUEST['sortfield']) - && isset($fields[$_REQUEST['sortfield']])) - { + if (isset($_REQUEST['sortfield']) + && isset($fields[$_REQUEST['sortfield']]) + ) { $this->sortfield = $_REQUEST['sortfield']; - } - else - { - if($checklastpaging - && isset($this->userinfo['lastpaging']['sortfield']) - && isset($fields[$this->userinfo['lastpaging']['sortfield']])) - { + } else { + if ($checklastpaging + && isset($this->userinfo['lastpaging']['sortfield']) + && isset($fields[$this->userinfo['lastpaging']['sortfield']]) + ) { $this->sortfield = $this->userinfo['lastpaging']['sortfield']; - } - else - { + } else { $fieldnames = array_keys($fields); $this->sortfield = $fieldnames[0]; } @@ -178,21 +153,17 @@ class paging $this->userinfo['lastpaging']['sortfield'] = $this->sortfield; - if(isset($_REQUEST['searchfield']) - && isset($fields[$_REQUEST['searchfield']])) - { + if (isset($_REQUEST['searchfield']) + && isset($fields[$_REQUEST['searchfield']]) + ) { $this->searchfield = $_REQUEST['searchfield']; - } - else - { - if($checklastpaging - && isset($this->userinfo['lastpaging']['searchfield']) - && isset($fields[$this->userinfo['lastpaging']['searchfield']])) - { + } else { + if ($checklastpaging + && isset($this->userinfo['lastpaging']['searchfield']) + && isset($fields[$this->userinfo['lastpaging']['searchfield']]) + ) { $this->searchfield = $this->userinfo['lastpaging']['searchfield']; - } - else - { + } else { $fieldnames = array_keys($fields); $this->searchfield = $fieldnames[0]; } @@ -200,49 +171,56 @@ class paging $this->userinfo['lastpaging']['searchfield'] = $this->searchfield; - if(isset($_REQUEST['searchtext']) - && (preg_match('/[-_@\p{L}\p{N}*.]+$/u', $_REQUEST['searchtext']) || $_REQUEST['searchtext'] === '')) - { + if (isset($_REQUEST['searchtext']) + && (preg_match('/[-_@\p{L}\p{N}*.]+$/u', $_REQUEST['searchtext']) + || $_REQUEST['searchtext'] === '') + ) { $this->searchtext = $_REQUEST['searchtext']; - } - else - { - if($checklastpaging - && isset($this->userinfo['lastpaging']['searchtext']) - && preg_match('/[-_@\p{L}\p{N}*.]+$/u', $this->userinfo['lastpaging']['searchtext'])) - { + } else { + if ($checklastpaging + && isset($this->userinfo['lastpaging']['searchtext']) + && preg_match('/[-_@\p{L}\p{N}*.]+$/u', $this->userinfo['lastpaging']['searchtext']) + ) { $this->searchtext = $this->userinfo['lastpaging']['searchtext']; - } - else - { + } else { $this->searchtext = ''; } } $this->userinfo['lastpaging']['searchtext'] = $this->searchtext; - if(isset($_REQUEST['pageno']) - && intval($_REQUEST['pageno']) != 0) - { + if (isset($_REQUEST['pageno']) + && intval($_REQUEST['pageno']) != 0 + ) { $this->pageno = intval($_REQUEST['pageno']); - } - else - { - if($checklastpaging - && isset($this->userinfo['lastpaging']['pageno']) - && intval($this->userinfo['lastpaging']['pageno']) != 0) - { + } else { + if ($checklastpaging + && isset($this->userinfo['lastpaging']['pageno']) + && intval($this->userinfo['lastpaging']['pageno']) != 0 + ) { $this->pageno = intval($this->userinfo['lastpaging']['pageno']); - } - else - { + } else { $this->pageno = 1; } } $this->userinfo['lastpaging']['pageno'] = $this->pageno; - $query = 'UPDATE `' . TABLE_PANEL_SESSIONS . '` SET `lastpaging`="' . $this->db->escape(serialize($this->userinfo['lastpaging'])) . '" WHERE `hash`="' . $this->db->escape($userinfo['hash']) . '" AND `userid` = "' . $this->db->escape($userinfo['userid']) . '" AND `ipaddress` = "' . $this->db->escape($userinfo['ipaddress']) . '" AND `useragent` = "' . $this->db->escape($userinfo['useragent']) . '" AND `adminsession` = "' . $this->db->escape($userinfo['adminsession']) . '" '; - $this->db->query($query); + $upd_stmt = Database::prepare(" + UPDATE `" . TABLE_PANEL_SESSIONS . "` SET + `lastpaging` = :lastpaging + WHERE `hash` = :hash AND `userid` = :userid + AND `ipaddress` = :ipaddr AND `useragent` = :ua + AND `adminsession` = :adminsession + "); + $upd_data = array( + 'lastpaging' => serialize($this->userinfo['lastpaging']), + 'hash' => $userinfo['hash'], + 'userid' => $userinfo['userid'], + 'ipaddr' => $userinfo['ipaddress'], + 'ua' => $userinfo['useragent'], + 'adminsession' => $userinfo['adminsession'] + ); + Database::pexecute($upd_stmt, $upd_data); } /** @@ -250,13 +228,11 @@ class paging * * @param int entries */ + function setEntries($entries) { - function setEntries($entries) - { $this->entries = $entries; - if(($this->pageno - 1) * $this->entriesperpage > $this->entries) - { + if (($this->pageno - 1) * $this->entriesperpage > $this->entries) { $this->pageno = 1; } @@ -269,9 +245,7 @@ class paging * @param int number of row * @return bool to display or not to display, that's the question */ - - function checkDisplay($count) - { + function checkDisplay($count) { $begin = (intval($this->pageno) - 1) * intval($this->entriesperpage); $end = (intval($this->pageno) * intval($this->entriesperpage)); return (($count >= $begin && $count < $end) || $this->entriesperpage == 0); @@ -283,30 +257,21 @@ class paging * @param bool should returned condition code start with WHERE (false) or AND (true)? * @return string the condition code */ - - function getSqlWhere($append = false) - { - if($this->searchtext != '') - { - if($append == true) - { + function getSqlWhere($append = false) { + if ($this->searchtext != '') { + if ($append == true) { $condition = ' AND '; - } - else - { + } else { $condition = ' WHERE '; } $searchfield = explode('.', $this->searchfield); - foreach($searchfield as $id => $field) - { - if(substr($field, -1, 1) != '`') - { + foreach ($searchfield as $id => $field) { + if (substr($field, -1, 1) != '`') { $field.= '`'; } - if($field{0} != '`') - { + if ($field{0} != '`') { $field = '`' . $field; } @@ -315,10 +280,8 @@ class paging $searchfield = implode('.', $searchfield); $searchtext = str_replace('*', '%', $this->searchtext); - $condition.= $searchfield . ' LIKE "' . $this->db->escape($searchtext) . '" '; - } - else - { + $condition.= $searchfield . " LIKE " . Database::quote($searchtext); + } else { $condition = ''; } @@ -331,19 +294,15 @@ class paging * @param bool Switch natsorting on/off (local, affects just this call) * @return string the "order by"-code */ + function getSqlOrderBy($natSorting = null) { - function getSqlOrderBy($natSorting = null) - { $sortfield = explode('.', $this->sortfield); - foreach($sortfield as $id => $field) - { - if(substr($field, -1, 1) != '`') - { + foreach ($sortfield as $id => $field) { + if (substr($field, -1, 1) != '`') { $field.= '`'; } - if($field{0} != '`') - { + if ($field{0} != '`') { $field = '`' . $field; } @@ -353,15 +312,12 @@ class paging $sortfield = implode('.', $sortfield); $sortorder = strtoupper($this->sortorder); - if($natSorting == true - || ($natSorting === null && $this->natSorting == true)) - { + if ($natSorting == true + || ($natSorting === null && $this->natSorting == true) + ) { // Acts similar to php's natsort(), found in one comment at http://my.opera.com/cpr/blog/show.dml/160556 - $sortcode = 'ORDER BY CONCAT( IF( ASCII( LEFT( ' . $sortfield . ', 5 ) ) > 57, LEFT( ' . $sortfield . ', 1 ), \'0\' ), IF( ASCII( RIGHT( ' . $sortfield . ', 1 ) ) > 57, LPAD( ' . $sortfield . ', 255, \'0\' ), LPAD( CONCAT( ' . $sortfield . ', \'-\' ), 255, \'0\' ) ) ) ' . $sortorder; - } - else - { + } else { $sortcode = 'ORDER BY ' . $sortfield . ' ' . $sortorder; } @@ -373,13 +329,10 @@ class paging * * @return string always empty */ - - function getSqlLimit() - { + function getSqlLimit() { /** * currently not in use */ - return ''; } @@ -389,21 +342,18 @@ class paging * @param array Language array * @return string the html sortcode */ + function getHtmlSortCode($lng, $break = false) { - function getHtmlSortCode($lng, $break = false) - { $sortcode = ''; $fieldoptions = ''; $orderoptions = ''; - foreach($this->fields as $fieldname => $fieldcaption) - { + foreach ($this->fields as $fieldname => $fieldcaption) { $fieldoptions.= makeoption($fieldcaption, $fieldname, $this->sortfield, true, true); } $breakorws = ($break ? '
' : ' '); - foreach(array('asc' => $lng['panel']['ascending'], 'desc' => $lng['panel']['decending']) as $sortordertype => $sortorderdescription) - { + foreach (array('asc' => $lng['panel']['ascending'], 'desc' => $lng['panel']['decending']) as $sortordertype => $sortorderdescription) { $orderoptions.= makeoption($sortorderdescription, $sortordertype, $this->sortorder, true, true); } @@ -418,24 +368,20 @@ class paging * @param string If set, only this field will be returned * @return mixed An array or a string (if field is set) of html code of arrows */ + function getHtmlArrowCode($baseurl, $field = '') { - function getHtmlArrowCode($baseurl, $field = '') - { global $theme; - if($field != '' - && isset($this->fields[$field])) - { + if ($field != '' + && isset($this->fields[$field]) + ) { $baseurl = htmlspecialchars($baseurl); $fieldname = htmlspecialchars($field); eval("\$arrowcode =\"" . getTemplate("misc/htmlarrowcode", '1') . "\";"); - } - else - { + } else { $baseurl = htmlspecialchars($baseurl); $arrowcode = array(); - foreach($this->fields as $fieldname => $fieldcaption) - { + foreach ($this->fields as $fieldname => $fieldcaption) { $fieldname = htmlspecialchars($fieldname); eval("\$arrowcode[\$fieldname] =\"" . getTemplate("misc/htmlarrowcode", '1') . "\";"); } @@ -450,14 +396,12 @@ class paging * @param array Language array * @return string the html searchcode */ + function getHtmlSearchCode($lng) { - function getHtmlSearchCode($lng) - { $searchcode = ''; $fieldoptions = ''; $searchtext = htmlspecialchars($this->searchtext); - foreach($this->fields as $fieldname => $fieldcaption) - { + foreach ($this->fields as $fieldname => $fieldcaption) { $fieldoptions.= makeoption($fieldcaption, $fieldname, $this->searchfield, true, true); } eval("\$searchcode =\"" . getTemplate("misc/htmlsearchcode", '1') . "\";"); @@ -470,61 +414,42 @@ class paging * @param string URL to use as base for links * @return string the html pagingcode */ - - function getHtmlPagingCode($baseurl) - { - if($this->entriesperpage == 0) - { + function getHtmlPagingCode($baseurl) { + if ($this->entriesperpage == 0) { return ''; - } - else - { + } else { $pages = intval($this->entries / $this->entriesperpage); } - if($this->entries % $this->entriesperpage != 0) - { + if ($this->entries % $this->entriesperpage != 0) { $pages++; } - if($pages > 1) - { - $start = $this->pageno - 4; + if ($pages > 1) { - if($start < 1) - { + $start = $this->pageno - 4; + if ($start < 1) { $start = 1; } $stop = $this->pageno + 4; - - if($stop > $pages) - { + if ($stop > $pages) { $stop = $pages; } $pagingcode = '« < '; - for ($i = $start;$i <= $stop;$i++) - { - if($i != $this->pageno) - { + for ($i = $start;$i <= $stop;$i++) { + if ($i != $this->pageno) { $pagingcode.= ' ' . $i . ' '; - } - else - { + } else { $pagingcode.= ' ' . $i . ' '; } } - $pagingcode.= ' > »'; - } - else - { + } else { $pagingcode = ''; } return $pagingcode; } } - -?>