Advanced Referrers Handler
See also:
- Documentation: AdvancedReferrersHandlerInfo.
- If you're looking for how to adapt the styling of the user interface to match your own skin, see 8. css/refmenu_col.css at the end of the page.
Referrer lists generated by WikkaWiki on high-traffic servers are likely to become unmanageable, due to their ever growing size. Of course you can limit the volume of the referrers by changing the referrers_purge_time in the ConfigurationOptions, so referrers older than n days are purged from the database. To allow a better management of the referrer list without purging the DB, I've modified the referrer handlers to allow searching and filtering. --DarTar
After DarTar's first version published on this page, we discussed some ideas, and then started completely revising all handlers dealing with referrers and the blacklist in close cooperation (working together on code on IRC is fun!). The original referrers_sites handler is now completely integrated with the referrers handler, and also the review_blacklist and delete_referrer handlers have been modified to integrate seamlessly. The details are below, and the new version will soon be installed as a beta feature on this site, too. --JavaWoman
Preview
Here's an example of what the new interface looks like:
External pages linking to HomePage (last 7 days)
Note to spammers: This page is not indexed by search engines, so don't waste your time.
Total: 1 referrers linking to HomePage
Result: 1 referrers linking to HomePage
Hits | Referrers |
---|---|
1 | http://javawoman.com/ |
Features
Current version: 0.8
- search referrers/domains by string
- filter referrers/domains by number of hits
- filter referrers/domains by time interval
- seamless integration of the handlers
- valid XHTML (some of the old code wasn't)
- accessible form and results table
- internationalization-ready
Todo:
- see docblocks in the code
The code
1. wikka.php
The method LoadReferrers() (from line 754) is obsolete now. You can comment it out, or remove it, or even leave it in place, but it isn't used any more. All queries are completely dynamically generated depending on the "view" requested and the selection criteria given.
2. handlers/page/referrers_sites.php
This handler file is now obsolete as well; its functionality is completely integrated with the new referrers handler (below). It's best to remove or rename this file since it will not work together with the new handlers.
3. handlers/page/referrers.php
This has undergone a complete overhaul by both DarTar and JavaWoman. See the docblock and various comments in the code for details. Since it's still beta code, there is some debug code present as well - that will disappear by the time we get to version 1.0 (see the @todo list in the docblock).
- <?php
- /**
- * Display, filter and search a list of referrers or referring sites for the current page or the site as a whole.
- *
- * Usage: append /referrers to the URL of the page
- * add global=1 to specify referrers for the site instead of the current page
- * add sites=1 to specify referrerring domains instead of full URLs
- *
- * This handler allows logged-in users to display, filter and search the referrer list for
- * the current page and for the whole site. Current search criteria include strings,
- * number of hits, reference period.
- *
- * @package Handlers
- * @subpackage DatabaseHandlers
- * @name Referrers
- *
- * @author ?
- * @author {@link http://wikka.jsnx.com/DarTar Dario Taraborelli} - code cleanup, search/filter functionality added.
- * @author {@link http://wikka.jsnx.com/JavaWoman JavaWoman} - more code cleanup, accessibility, integration with referrers_sites
- * @version 0.8
- * @since Wikka 1.1.6.X
- *
- * @todo for 1.0:
- * - clean up debug code
- * - remove LoadReferrers() from core
- * - configurable choice hostname (NAME_GLOBAL) or 'this site' (config, installer)
- * - configurable parameters for building days dropdown (config, installer)
- * - configurable limit to express days as hours (config, installer)
- * - build an index on the referrer column in the referrers table (installer)
- * - set form id
- * - fix stylesheet kluge
- * - allow banning by domain or URL keyword (.htaccess)
- * - view and filter on recorded IP address; allow banning by IP address (.htaccess)
- * later:
- * - transfer filter parameters as well so we can redirect to the exact view we came from
- * - (global) icons to represent each of the five views, small and larger versions (menu/page)
- * - adapt text definitions to take singular-plural into account
- * - add paging
- * - turn list into form with checkboxes to allow mass blacklisting
- *
- * @input string $q optional: string used to filter the referrers;
- * default: NULL;
- * the default can be overridden by providing a POST parameter 'q'
- * @input integer $qo optional: determines the kind of search to be performed for string $q:
- * 1: search for all referrers containing a given string
- * 0: search for all referrers not containing a given string
- * default: 1;
- * the default can be overridden by providing a POST parameter 'qo'
- * @input integer $h optional: number of hits used to filter the referrers;
- * default: 1;
- * the default can be overridden by providing a POST parameter 'h'
- * @input integer $ho optional: determines the kind of filter to be applied to $h:
- * 1: search for referrers with at least $h hits;
- * 0: search for referrers with no more than $h hits;
- * default: 1;
- * the default can be overridden by providing a POST parameter 'ho'
- * @input integer $days optional: number of days used to filter the referrers;
- * default: 1;
- * the default can be overridden by providing a POST parameter 'h'
- * @input integer $global optional: switches between local/global referrers:
- * 1: display referrers for the whole site;
- * 0: display referrers for the current page;
- * default: 0;
- * the default can be overridden by providing a GET/POST parameter 'global'
- * @input integer $sites optional: switches between referring urls and domains
- * 1: display referring sites (domains);
- * 0: display referrers (URLs);
- * default: 0;
- * the default can be overridden by providing a GET/POST parameter 'sites'
- * @input integer $refdel optional: number of referrer records deleted
- * @input integer $bladd optional: number of blacklist records added
- * @output Selection and filter form for referrers; overview of resulting referrer info
- *
- * @uses GetConfigValue() # replace by $this->config[] !
- * @uses GetPageTag()
- * @uses IsAdmin()
- * @uses GetUser()
- * @uses LoadAll()
- * @uses LoadSingle()
- * @uses Href()
- * @uses makeList()
- * @uses FormOpen()
- * @uses FormClose()
- * @uses GetMethod()
- * @uses makeId()
- * @uses htmlspecialchars_ent()
- */
- // Utilities
- /**
- * Build an array of numbers consisting of 'ranges' with increasing step size in each 'range'.
- *
- * A list of numbers like this is useful for instance for a dropdown to choose
- * a period expressed in number of days: a difference between 2 and 5 days may
- * be significant while that between 92 and 95 may not be.
- *
- * @author {@link http://wikka.jsnx.com/JavaWoman JavaWoman}
- * @copyright Copyright © 2005, Marjolein Katsma
- * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
- * @version 1.0
- *
- * @param mixed $limits required: single integer or array of integers;
- * defines the upper limits of the ranges as well as the next step size
- * @param int $max required: upper limit for the whole list
- * (will be included if smaller than the largest limit)
- * @param int $firstinc optional: increment for the first range; default 1
- * @return array resulting list of numbers
- */
- function optionRanges($limits, $max, $firstinc = 1)
- {
- // initializations
- if ($firstinc < 1) $firstinc = 1;
- $inc = $firstinc;
- // first element is the first increment
- $opts[] = $inc;
- // each $limit is the upper limit of a 'range'
- foreach ($limits as $limit)
- {
- for ($i = $inc + $inc; $i <= $limit && $i < $max; $i += $inc)
- {
- $opts[] = $i;
- }
- // we quit at $max, even if there are more $limit elements
- if ($limit >= $max)
- {
- // add $max to the list; then break out of the loop
- $opts[] = $max;
- break;
- }
- // when $limit is reached, it becomes the new start and increment for the next 'range'
- $inc = $limit;
- }
- return $opts;
- }
- // constants
- // -------------------------------------
- // initialize parameters
- $q = NULL; # search string
- $qo = 1; # search string option
- $h = HITS_DEFAULT; # hits number
- $ho = 1; # hits option
- $days = DAYS_DEFAULT; # period selection
- $global = FALSE; # global (site) or this page only
- $sites = FALSE; # referrers or referring sites
- $refdel = NULL; # referrer records deleted
- $bladd = NULL; # blacklist records added
- // -------------------------------------
- // initialize internal variables
- $string_option = SEARCH_LIKE; # LIKE or NOT LIKE
- $hits_option = HITS_MIN_OPTION; # MIN (>=) or MAX (<=)
- $tag = $this->GetPageTag();
- $isAdmin = $this->IsAdmin();
- $loggedin = ($isAdmin) ? TRUE : (bool)$this->GetUser();
- $pre = $this->config['table_prefix'];
- $par = '';
- $query = '';
- $rows = 0;
- // -------------------------------------
- // User-interface strings
- // current target
- # you can use NAME_GLOBAL instead of 'this site' if the site name is short enough
- # @@@ JW: choice between 'this site' and NAME_GLOBAL could be set via configuration (later)
- // menus don't use current target but *possible* targets
- define('LIST_SUMMARY_REFERRERS','Filtered list of referrers, with hits%s, sorted by number of hits');
- define('LIST_SUMMARY_SITES','Filtered list of referring sites, with hits%s, sorted by number of hits');
- define('LIST_REF_UNKNOWN','unknown'); # make sure the *exact* same string is used in the whitelist definition (delete_referrer.php)
- define('SPAM_NOTE','Note to spammers: This page is not indexed by search engines, so don\'t waste your time.');
- // show result counts for target
- define('LIST_RESULT_COUNTER_REFERRERS','Filtered result: %1$d referrers linking to %2$s'); # @@@ does not take account of singular
- define('LIST_RESULT_COUNTER_SITES','Filtered result: %1$d domains linking to %2$s'); # @@@ does not take account of singular
- // show 'no result' summary for target
- // -------------------------------------
- // fetch and validate parameters
- // get query string and comparison method
- {
- if ('' != $tq)
- {
- {
- $qo = ($_POST['qo'] == '1') ? 1 : 0;
- $string_option = ($qo == 1) ? SEARCH_LIKE : SEARCH_UNLIKE;
- }
- }
- }
- // get hits and min or max criteria
- {
- $h = (is_numeric($_POST['h'])) ? abs((int)$_POST['h']) : HITS_DEFAULT; # cast to positive integer if numeric
- }
- {
- $ho = ($_POST['ho'] == '1') ? 1 : 0;
- $hits_option = ($ho == 1) ? HITS_MIN_OPTION : HITS_MAX_OPTION;
- }
- // get period, not longer than purge time
- {
- }
- // get search target: page or site (global)
- {
- $global = (bool)$_POST['global'];
- }
- {
- $global = (bool)$_GET['global'];
- }
- $iglobal = (int)$global;
- // get precision: URLS (referrers) or referring sites (domains)
- {
- $sites = (bool)$_POST['sites'];
- }
- {
- $sites = (bool)$_GET['sites'];
- }
- $isites = (int)$sites;
- //get reported values (no validation needed, just cast to integer)
- {
- $refdel = (int)$_GET['refdel'];
- }
- // derive parameters for 'current' links
- if (1 == $global)
- {
- if ('' != $par) $par .= '&';
- $par .= 'global=1';
- }
- if (1 == $sites)
- {
- if ('' != $par) $par .= '&';
- $par .= 'sites=1';
- }
- // -------------------------------------
- // build query from chunks depending on criteria chosen
- if ($loggedin)
- {
- $query = 'SELECT referrer';
- if ($sites)
- {
- // add 'host' = domain extracted from referrring URL using this algorithm:
- // find first char after http:// : LOCATE('//',referrer)+2
- // find first / after this: LOCATE('/',referrer,(LOCATE('//',referrer)+2)-1
- // calculate length: (LOCATE('/',referrer,(LOCATE('//',referrer)+2)-1) - (LOCATE('//',referrer)+2)
- // get host (standard): SUBSTRING(referrer FROM (LOCATE('//',referrer)+2) FOR ((LOCATE('/',referrer,(LOCATE('//',referrer)+2)-1) - (LOCATE('//',referrer)+2)))
- // *or*
- // get host (MySQL-specific): SUBSTRING(SUBSTRING_INDEX(referrer,'/',3) FROM (LOCATE('//',referrer)+1))
- $protocol_host = 'SUBSTRING_INDEX(referrer,"/",3)'; # protocol and host: everything before first single /
- $start_host = 'LOCATE("//",referrer)+2'; # start position of host: after //
- $query .= ', SUBSTRING('.$protocol_host.' FROM ('.$start_host.')) AS host';
- // NOTE: COUNT() cannot use a derived column name but it *can* take an expression
- $query .= ', COUNT(SUBSTRING('.$protocol_host.' FROM ('.$start_host.'))) AS num';
- $query .= ' FROM '.$pre.'referrers';
- if (!$global)
- {
- }
- #if ($days != $max_days)
- if ($days != DAYS_MAX)
- {
- $query .= ' TO_DAYS(NOW()) - TO_DAYS(time) <= '.$days; # filter by period
- }
- $query .= ' GROUP BY host ';
- {
- $query .= ' HAVING host '.$string_option." '%".$q."%'"; # filter by string (derived column so we use HAVING)
- }
- if ($hits_option != HITS_MIN_OPTION || $h != 1)
- {
- $query .= ' num '.$hits_option.' '.$h; # filter by hits number (derived column so we use HAVING)
- }
- }
- else
- {
- $query = 'SELECT referrer';
- $query .= ', COUNT(referrer) AS num';
- $query .= ' FROM '.$pre.'referrers';
- if (!$global)
- {
- }
- {
- $query .= ' referrer '.$string_option." '%".$q."%'"; # filter by string
- }
- #if ($days != $max_days)
- if ($days != DAYS_MAX)
- {
- $query .= ' TO_DAYS(NOW()) - TO_DAYS(time) <= '.$days; # filter by period
- }
- $query .= ' GROUP BY referrer ';
- if ($hits_option != HITS_MIN_OPTION || $h != 1)
- {
- $query .= ' HAVING num '.$hits_option.' '.$h; # filter by hits number (derived column so we use HAVING)
- }
- }
- $query .= ' ORDER BY num DESC, referrer ASC'; # set order
- // get total number of referrers (NOT records!)
- $query_refcount = 'SELECT COUNT(DISTINCT(referrer)) AS total'; # @@@ referrer column should be indexed to make this really efficient
- $query_refcount .= ' FROM '.$pre.'referrers';
- if (!$global)
- {
- }
- }
- // -------------------------------------
- // execute query (if logged in)
- // @@@ NOTE: we don't use LoadReferrers any more since the query is now completely dynamically built
- if ($loggedin)
- {
- // execute query
- $referrers = $this->LoadAll($query);
- $totalrefs = $this->LoadSingle($query_refcount);
- }
- // -------------------------------------
- // build UI elements
- // define current target
- $target = ($global) ? TARGET_GLOBAL : TARGET_PAGE;
- // title
- if ($isAdmin)
- {
- }
- if ($loggedin)
- {
- // results
- $tot = $totalrefs['total'];
- if ($creferrers > 0)
- {
- }
- else
- {
- $result = LIST_RESULT_NONE;
- }
- // menu elements: prevent wrapping within element (these *don't* use current target!
- // menu
- if ($global)
- {
- $m_referrers_page = '<a href="'.$this->Href('referrers').'">'.$menu_referrers_page.'</a>';
- $m_sites_page ='<a href="'.$this->Href('referrers','','sites=1').'">'.$menu_sites_page.'</a>';
- $m_referrers_global = ($sites) ? '<a href="'.$this->Href('referrers','','global=1').'">'.$menu_referrers_global.'</a>' : $menu_referrers_global;
- $m_sites_global = ($sites) ? $menu_sites_global : '<a href="'.$this->Href('referrers','','global=1&sites=1').'">'.$menu_sites_global.'</a>';
- }
- else
- {
- $m_referrers_page = ($sites) ? '<a href="'.$this->Href('referrers').'">'.$menu_referrers_page.'</a>' : $menu_referrers_page;
- $m_sites_page = ($sites) ? $menu_sites_page : '<a href="'.$this->Href('referrers','','sites=1').'">'.$menu_sites_page.'</a>';
- $m_referrers_global = '<a href="'.$this->Href('referrers','','global=1').'">'.$menu_referrers_global.'</a>';
- $m_sites_global = '<a href="'.$this->Href('referrers','','global=1&sites=1').'">'.$menu_sites_global.'</a>';
- }
- $m_blacklist = '<a href="'.$this->Href('review_blacklist').'">'.$menu_blacklist.'</a>';
- // don't generate id since the menu is used multiple times
- $menu = $this->makeList(array($m_referrers_page,$m_sites_page,$m_referrers_global,$m_sites_global,$m_blacklist),FALSE,'menu');
- // days dropdown content
- $daysopts = optionRanges($days_limits,DAYS_MAX);
- // form
- $form = $this->FormOpen('referrers','','POST'); # @@@ add parameter for id
- $form .= '<fieldset class="hidden">'."\n";
- $form .= '<input type="hidden" name="global" value="'.$iglobal.'" />'."\n";
- $form .= '<input type="hidden" name="sites" value="'.$isites.'" />'."\n";
- $form .= '</fieldset>'."\n";
- $form .= '<fieldset>'."\n";
- $form .= '<legend>'.FORM_LEGEND.'</legend>'."\n";
- $form .= '<label for="qo" class="mainlabel">'.(($sites) ? FORM_URL_OPT_SITES : FORM_URL_OPT_REFERRERS).'</label> '."\n";
- $form .= '<select name="qo" id="qo" title="'.FORM_URL_OPT_TITLE.'">'."\n";
- $form .= '<option value="1"'.(($qo == '1')? ' selected="selected"' : '').'>'.FORM_URL_OPT_1.'</option>'."\n";
- $form .= '<option value="0"'.(($qo == '0')? ' selected="selected"' : '').'>'.FORM_URL_OPT_0.'</option>'."\n";
- $form .= '</select> '."\n";
- $form .= '<label for="q">'.FORM_URL_STRING_LABEL.'</label> '."\n";
- $form .= '<input type ="text" name="q" id="q" title="'.FORM_URL_STRING_TITLE.'" size="10" maxlength="50" value="'.$q.'" />';
- $form .= '<br />'."\n";
- $form .= '<label for="ho" class="mainlabel">'.FORM_HITS_OPT_LABEL.'</label> '."\n";
- $form .= '<select name="ho" id="ho" title="'.FORM_HITS_OPT_TITLE.'">'."\n";
- $form .= '<option value="1"'.(($ho == '1')? ' selected="selected"' : '').'>'.FORM_HITS_OPT_1.'</option>'."\n";
- $form .= '<option value="0"'.(($ho == '0')? ' selected="selected"' : '').'>'.FORM_HITS_OPT_0.'</option>'."\n";
- $form .= '</select> '."\n";
- $form .= '<input type ="text" name="h" id="h" title="'.FORM_HITS_NUM_TITLE.'" size="5" maxlength="5" value="'.$h.'" />'."\n";
- $form .= ' <label for="h">'.FORM_HITS_NUM_LABEL.'</label>';
- $form .= '<br />'."\n";
- $form .= '<label for="days" class="mainlabel">'.FORM_DAYS_OPT_LABEL.'</label> '."\n";
- $form .= '<select name="days" id="days" title="'.FORM_DAYS_OPT_TITLE.'">'."\n";
- // build drop-down
- foreach ($daysopts as $opt)
- {
- $selected = ($opt == $days) ? ' selected="selected"' : '';
- $form .= '<option value="'.$opt.'"'.$selected.'>'.$opt.'</option>';
- }
- $form .= '</select> '."\n";
- $form .= ' <label for="h">'.FORM_DAYS_NUM_LABEL.'</label>'."\n";
- $form .= '</fieldset>'."\n";
- $form .= '<input type="submit" value="'.(($sites) ? FORM_SUBMIT_SITES : FORM_SUBMIT_URLS).'" accesskey="r" />'."\n";
- $form .= $this->FormClose();
- // referrers list with admin link for blacklisting
- if ($sites)
- {
- $refshead = LIST_HEAD_LIST_SITES;
- }
- else
- {
- $refshead = LIST_HEAD_LIST_REFERRERS;
- }
- if ($isAdmin)
- {
- $redir = ($global||$sites) ? $this->GetMethod().'&'.$par : $this->GetMethod(); # ensure we return to the same view
- $par = ($sites) ? 'spam_site' : 'spam_link';
- $blacklisturl = $this->Href('delete_referrer','',$par.'=').'%s&redirect=%s';
- $blacklink = '<a href="'.$blacklisturl.'" title="'.LIST_ACTION_BLACKLIST_TITLE.'">'.LIST_ACTION_BLACKLIST.'</a>';
- }
- // ids - use constant for variable-content heading
- $idTitle = $this->makeId('hn','title');
- $idTotal = $this->makeId('hn','total');
- $idResult = $this->makeId('hn','result');
- }
- // -------------------------------------
- // show user interface (pre-template)
- echo '<div class="page">'."\n";
- echo '<h3 id="'.$idTitle.'">'.$title.'</h3>'."\n";
- echo '<p><em>'.SPAM_NOTE.'</em></p>'."\n";
- echo '<p class="notes">Doesn\'t look right with your skin? See <a href="http://wikka.jsnx.com/AdvancedReferrersHandler">AdvancedReferrersHandler</a>.<!--temporary for site only-->'."\n";
- # debug
- if (DEBUG)
- {
- echo 'Query (ref): '.$query.'<br />';
- echo 'Query (sites): '.$query_sites.'<br />';
- echo ($global) ? 'Global: TRUE<br />' : 'Global: FALSE<br />';
- echo ($sites) ? 'Sites: TRUE<br />' : 'Sites: FALSE<br />';
- }
- # debug
- if ($loggedin)
- {
- echo '<div class="refmenu">'.$menu.'</div><br class="clear" />'."\n";
- echo '<h4 id="'.$idTotal.'">'.$total.'</h4>'."\n";
- echo '<div id="refform">'.$form.'</div>'."\n"; # @@@ kluge until FormOpen() is adapted: id should actually be on form itself and div not necessary!
- if ($creferrers != 0)
- {
- echo '<h4 id="'.$idResult.'">'.$result.'</h4>'."\n";
- echo '<table id="reflist" summary="'.$summary.'">'."\n";
- echo '<thead>';
- echo '<tr><th class="hits" scope="col">'.LIST_HEAD_HITS.'</th>';
- if ($isAdmin) echo '<th class="action" scope="col">'.LIST_HEAD_ACTION.'</th>';
- echo '<th class="refs" scope="col">'.$refshead.'</th></tr>'."\n";
- echo '</thead>'."\n";
- echo '<tbody>'."\n";
- foreach ($referrers as $referrer)
- {
- $hits = $referrer['num'];
- if ($sites)
- {
- $ref = $this->htmlspecialchars_ent($referrer['host']);
- }
- else
- {
- $ref = $this->htmlspecialchars_ent($referrer['referrer']);
- }
- echo '<tr>';
- echo '<td class="hits">'.$hits.'</td>';
- if ($isAdmin) echo '<td class="action"><span class="keys">'.sprintf($blacklink,$ref,$redir).'</span></td>';
- if ($sites)
- {
- echo '<td class="refs">'.$ref.'</td>';
- }
- else
- {
- echo '<td class="refs"><a href="'.$ref.'">'.$ref.'</a></td>';
- }
- echo '</tr>'."\n";
- }
- echo '</tbody>'."\n";
- echo '</table>'."\n";
- }
- else
- {
- echo '<h4 id="'.$idResult.'">'.$result.'</h4>'."\n";
- }
- echo '<div class="refmenu">'.$menu.'</div><br class="clear" />'."\n";
- }
- else
- {
- echo '<p><strong>'.LOGIN_NOTE.'</strong></p>'."\n";
- }
- echo '</div>'."\n";
- ?>
4. handlers/page/review_blacklist.php
This is rewritten mainly to make it integrate seamlessly with the referrers handler. There was also a problem with the output which was not valid XHTML; it now follows the same pattern as the referrers handler and got the same treatment for preparation for internationalization as well.
- <?php
- /**
- * Display, filter and search a list of blacklisted domains.
- *
- * Usage: append /review_blacklist to the URL of the page
- *
- * This handler allows logged-in users to display and search the blacklist; an admin may
- * remove blacklisted domains from the database.
- *
- * @package Handlers
- * @subpackage DatabaseHandlers
- * @name ReviewBlacklist
- *
- * @author ?
- * @author {@link http://wikka.jsnx.com/JavaWoman JavaWoman} - code cleanup, search/filter functionality added, valid XHTML, accessibility
- * @version 0.8
- * @since Wikka 1.1.6.X
- *
- * @todo for 1.0:
- * - clean up debug code
- * - configurable choice hostname (NAME_GLOBAL) or 'this site' (config, installer)
- * - make index on the spammer column in the referrer_blacklist table _unique_ (installer) and remove extra query
- * - set form id
- * - fix stylesheet kluge
- * - include options and process to handle banned IP addresses
- * later:
- * - (global) icons to represent each of the five views, small and larger versions (menu/page)
- * - adapt text definitions to take singular-plural into account
- * - add paging
- * - turn list into form with checkboxes to allow mass removing
- *
- * @input string $q optional: string used to filter the referrers;
- * default: 'NULL;
- * the default can be overridden by providing a POST parameter 'q'
- * @input integer $qo optional: determines the kind of search to be performed for string $q:
- * 1: search for all referrers containing a given string
- * 0: search for all referrers not containing a given string
- * default: 1;
- * the default can be overridden by providing a POST parameter 'qo'
- * @input string $remove optional: GET parameter - domain to be removed from the blacklist
- * default: NULL;
- * @output Selection and filter form for blacklisted domain; overview of resulting blacklist info
- *
- * @uses GetPageTag()
- * @uses IsAdmin()
- * @uses GetGetUser()
- * @uses Query()
- * @uses LoadAll()
- * @uses LoadSingle()
- * @uses Href()
- * @uses makeList()
- * @uses FormOpen()
- * @uses FormClose()
- * @uses makeId()
- * @uses htmlspecialchars_ent()
- */
- // constants
- // -------------------------------------
- // initialize parameters
- $q = NULL; # search string
- $qo = 1; # search string option
- $remove = NULL; # domain to be removed from the blacklist
- // -------------------------------------
- // initialize internal variables
- $string_option = SEARCH_LIKE; # LIKE or NOT LIKE
- $tag = $this->GetPageTag();
- $isAdmin = $this->IsAdmin();
- $loggedin = ($isAdmin) ? TRUE : (bool)$this->GetUser();
- $pre = $this->config['table_prefix'];
- $queryd = '';
- $querys = '';
- $rows = 0;
- // -------------------------------------
- // User-interface strings
- // current target
- # you can use NAME_GLOBAL instead of 'this site' if the site name is short enough
- # @@@ JW: choice between 'this site' and NAME_GLOBAL could be set via configuration (later)
- // menus don't use current target but *possible* targets
- define('LIST_RESULT_COUNTER_SITES','Filtered result: %d domains'); # @@@ does not take account of singular
- // -------------------------------------
- // fetch and validate parameters
- // get query string and comparison method
- {
- if ('' != $tq)
- {
- {
- $qo = ($_POST['qo'] == '1') ? 1 : 0;
- $string_option = ($qo == 1) ? SEARCH_LIKE : SEARCH_UNLIKE;
- }
- }
- }
- // get host(s) to be removed
- {
- }
- // -------------------------------------
- // build remove query
- if ($isAdmin)
- {
- $queryd = 'DELETE FROM '.$pre.'referrer_blacklist'
- . ' WHERE spammer = "'.$remove.'"';
- }
- // build filter query
- if ($loggedin)
- {
- $querys = 'SELECT * FROM '.$pre.'referrer_blacklist';
- {
- $querys .= ' WHERE spammer '.$string_option." '%".$q."%'"; # filter by string
- }
- $querys .= ' ORDER BY spammer ASC'; # set order
- // get total number of domains in blacklist
- $query_refcount = 'SELECT COUNT(spammer) AS total';
- $query_refcount .= ' FROM '.$pre.'referrer_blacklist';
- }
- // -------------------------------------
- // execute query (if logged in)
- // do a 'remove' query first, then follow with the select query:
- // the list should then reflect the situation after removal of a domain
- if ($loggedin)
- {
- {
- $rc = $this->Query($queryd); # TRUE on success
- $numbldeleted = mysql_affected_rows(); # @@@ report back as GET parameter (in $removeurl/$removelink!)
- }
- $blacklist = $this->LoadAll($querys);
- $totalrefs = $this->LoadSingle($query_refcount);
- }
- // -------------------------------------
- // build UI elements
- // title
- $title = TITLE;
- if ($isAdmin)
- {
- $removeurl = $this->Href('review_blacklist','','remove=').'%s';
- $removelink = '<a href="'.$removeurl.'" title="'.LIST_ACTION_BL_TITLE.'">'.LIST_ACTION_BL.'</a>';
- }
- if ($loggedin)
- {
- // results
- $tot = $totalrefs['total'];
- if ($cdomains > 0)
- {
- }
- else
- {
- $result = LIST_RESULT_NONE;
- }
- // menu elements: prevent wrapping within element (these *don't* use current target!
- // menu
- $m_referrers_page = '<a href="'.$this->Href('referrers').'">'.$menu_referrers_page.'</a>';
- $m_sites_page ='<a href="'.$this->Href('referrers','','sites=1').'">'.$menu_sites_page.'</a>';
- $m_referrers_global = '<a href="'.$this->Href('referrers','','global=1').'">'.$menu_referrers_global.'</a>';
- $m_sites_global = '<a href="'.$this->Href('referrers','','global=1&sites=1').'">'.$menu_sites_global.'</a>';
- $m_blacklist = $menu_blacklist;
- // don't generate id since the menu is used multiple times
- $menu = $this->makeList(array($m_referrers_page,$m_sites_page,$m_referrers_global,$m_sites_global,$m_blacklist),FALSE,'menu');
- // form
- $form = $this->FormOpen('review_blacklist','','POST'); # @@@ add parameter for id
- $form .= '<fieldset>'."\n";
- $form .= '<legend>'.FORM_LEGEND.'</legend>'."\n";
- $form .= '<label for="qo" class="mainlabel">'.FORM_URL_OPT_LABEL.'</label> '."\n";
- $form .= '<select name="qo" id="qo" title="'.FORM_URL_OPT_TITLE.'">'."\n";
- $form .= '<option value="1"'.(($qo == '1')? ' selected="selected"' : '').'>'.FORM_URL_OPT_1.'</option>'."\n";
- $form .= '<option value="0"'.(($qo == '0')? ' selected="selected"' : '').'>'.FORM_URL_OPT_0.'</option>'."\n";
- $form .= '</select> '."\n";
- $form .= '<label for="q">'.FORM_URL_STRING_LABEL.'</label> '."\n";
- $form .= '<input type ="text" name="q" id="q" title="'.FORM_URL_STRING_TITLE.'" size="10" maxlength="50" value="'.$q.'" />';
- $form .= '</fieldset>'."\n";
- $form .= '<input type="submit" value="'.FORM_SUBMIT_BLACKLIST.'" accesskey="b" />'."\n";
- $form .= $this->FormClose();
- // blacklist with admin link for removing
- $refshead = LIST_HEAD_BL;
- // ids - use constant for variable-content heading
- $idTitle = $this->makeId('hn','title');
- $idTotal = $this->makeId('hn','total');
- $idResult = $this->makeId('hn','result');
- }
- // -------------------------------------
- // show user interface (pre-template)
- echo '<div class="page">'."\n";
- echo '<h3 id="'.$idTitle.'">'.$title.'</h3>'."\n";
- # debug
- if (DEBUG)
- {
- echo 'Query remove: '.$queryd.'<br />';
- echo 'Query blacklist: '.$querys.'<br />';
- echo 'remove: '.$remove.'<br/>';
- echo 'removed: '.$numbldeleted.'<br/>';
- }
- # debug
- if ($loggedin)
- {
- echo '<div class="refmenu">'.$menu.'</div><br class="clear" />'."\n";
- echo '<h4 id="'.$idTotal.'">'.$total.'</h4>'."\n";
- echo '<div id="refform">'.$form.'</div>'."\n"; # @@@ kluge until FormOpen() is adapted: id should actually be on form itself and div not necessary!
- if ($cdomains != 0)
- {
- echo '<h4 id="'.$idResult.'">'.$result.'</h4>'."\n";
- echo '<table id="reflist" summary="'.$summary.'">'."\n";
- echo '<thead>';
- if ($isAdmin) echo '<th class="action" scope="col">'.LIST_HEAD_ACTION.'</th>';
- echo '<th class="refs" scope="col">'.$refshead.'</th></tr>'."\n";
- echo '</thead>'."\n";
- echo '<tbody>'."\n";
- foreach ($blacklist as $spammer)
- {
- $ref = $this->htmlspecialchars_ent($spammer['spammer']);
- echo '<tr>';
- if ($isAdmin) echo '<td class="action"><span class="keys">'.sprintf($removelink,$ref).'</span></td>';
- echo '<td class="refs">'.$ref.'</td>';
- echo '</tr>'."\n";
- }
- echo '</tbody>'."\n";
- echo '</table>'."\n";
- }
- else
- {
- echo '<h4 id="'.$idResult.'">'.$result.'</h4>'."\n";
- echo '<p><em>'.NONE_NOTE.'</em></p>'."\n";
- }
- echo '<div class="refmenu">'.$menu.'</div><br class="clear" />'."\n";
- }
- else
- {
- echo '<p><strong>'.LOGIN_NOTE.'</strong></p>'."\n";
- }
- echo '</div>'."\n";
- ?>
5. handlers/page/delete_referrer.php
Two problems here were solved: the code was not actually secure (anyone knowing how to build a URL could blacklist a domain), and when the action was completed you would get back to a single view only - often not the view you were coming form, causing an extra click to get back.
Also there is now a list of domains that are "whitelisted" so they will never be blacklisted. Basically this is for local machines, but you could add your own domains here as well. We'll make this list configurable.
For further details see the code (there's stil quite a lot of debug code which will disappear):
- <?php
- /**
- * Remove specified URL or referrer domain from the referrer list and add it to the blacklist.
- *
- * Usage: supposed to be used only from link or form produced by the referrers handler.
- *
- * Security: - can be executed only by an admin (redirect to homepage otherwise)
- * - redirect to homepage if any parameter is missing or incorrect
- * - explicitly use GET or POST to retrieve parameters
- *
- * @package Handlers
- * @subpackage DatabaseHandlers
- * @name DeleteReferrer
- *
- * @author {@link http://wikka.jsnx.com/JavaWoman JavaWoman} - code cleanup, security, (integration with referrers)
- * @version 0.8
- * @since Wikka 1.1.6.X
- *
- * @todo for 1.0:
- * - clean up debug code
- * - configurable 'whitelist' of hosts that should never be blacklisted (config, installer)
- * later:
- * - transfer filter parameters as well so we cen redirect to the exact view we came from
- * - change to fetching POST parameters when we convert to using form(s))
- *
- * @input string $spam_link required: spammer URL or domain to blacklist.
- * @input string $redirect required: handler for current page to redirect to.
- * @input int $global optional: query parameter for redirecting to the original view; default: 0
- * @input int $sites optional: query parameter for redirecting to the original view; default: 0
- */
- // constants
- define('LIST_REF_UNKNOWN','unknown'); # make sure this is *exactly* same string as used in referrers.php
- $whitelist = array(LIST_REF_UNKNOWN,'localhost','127.0.0.1'); # @@@ make this configurable via wikka.config.php
- // -------------------------------------
- // initialize parameters
- $spam_link = NULL; # site to blacklist from referrers list
- $spam_site = NULL; # domain to blacklist from sites (domains) list
- $redirect = NULL; # handler / query string of referring page to redirect to
- $global = 0; # extra parameter for redirect
- $sites = 0; # extra parameter for redirect
- // -------------------------------------
- // initialize internal variables
- $isAdmin = $this->IsAdmin();
- $home = $this->Href('',$this->config['root_page']);
- $pre = $this->config['table_prefix'];
- $par = '';
- // -------------------------------------
- // User-interface strings
- // -------------------------------------
- // check permission and immediately redirect to home page if check fails
- if (!$isAdmin) $this->Redirect($home,MSG_NOT_ALLOWED);
- // -------------------------------------
- // fetch and validate parameters
- // ensure we have a spam_link OR spam_site parameter
- {
- }
- {
- }
- // ensure we have a redirect parameter 'referrers' (we won't allow any other value)
- {
- }
- {
- }
- {
- }
- # debug
- if (DEBUG)
- {
- echo 'spamlink: '.$spam_link.'<br/>';
- echo 'spamsite: '.$spam_site.'<br/>';
- }
- # end debug
- // check required parameters and immediately redirect to home page if check fails
- // -------------------------------------
- // derive internal variables
- // With $spam_link we get a full URL and need to parse out the host name;
- // with $spam_site we get a domain: no need to parse anything;
- // for both: check against whitelist before acting on it
- {
- // referring domain already is host name (no need to parse)
- $domain = $spam_site;
- }
- else
- {
- if (FALSE !== $parsed_url)
- {
- // derive host name from referring URL
- {
- $domain = $parsed_url['host'];
- }
- }
- }
- // exclude 'unknown', 'localhost' and others in the "whitelist"
- {
- $spammer = $domain;
- }
- # debug
- if (DEBUG)
- {
- echo 'domain: '.$domain.'<br/>';
- echo 'spammer: '.$spammer.'<br/>';
- #exit;
- }
- # end debug
- // prepare extra parameters for redirect
- if (1 == $global)
- {
- if ('' != $par) $par .= '&';
- $par .= 'global=1';
- }
- if (1 == $sites)
- {
- if ('' != $par) $par .= '&';
- $par .= 'sites=1';
- }
- // -------------------------------------
- // do the blacklisting
- // if $spammer = 'wakka' $queryd should remove http://wakka...
- // but NOT http://example.com/wakka from the referrers table
- #$hspammer = mysql_real_escape_string('//'.$spammer.'/'); # string to recognize host in referrers table
- $hspammer = mysql_real_escape_string('//'.$spammer); # string to recognize host in referrers table (trailing / removed: some referrers don't have one
- $spammer = mysql_real_escape_string($spammer); # string to use for spammer in referrer_blacklist table
- $queryd = 'DELETE FROM '.$pre.'referrers'
- . ' WHERE referrer LIKE "%'.$hspammer.'%"';
- // check if domain is already blacklisted (must start with $spammer)
- # @@@ JW: should not be necessary if we'd have a _unique_ index on spammer! (let the database do the work)
- $querys = 'SELECT spammer FROM '.$pre.'referrer_blacklist'
- . ' WHERE spammer like "'.$spammer.'%"';
- // add domain to blacklist
- $queryi = 'INSERT INTO '.$pre.'referrer_blacklist'
- . ' SET spammer = "'.$spammer.'"';
- # debug
- if (DEBUG)
- {
- echo 'delete referrers: '.$queryd.'<br/>';
- echo 'check blacklist : '.$querys.'<br/>';
- echo 'blacklist domain: '.$queryi.'<br/>';
- $explain = $this->LoadAll($querye);
- echo 'Explain:<pre>';
- echo '</pre>';
- $todelete = $this->LoadAll($queryes);
- echo 'To delete:<pre>';
- echo '</pre>';
- #exit;
- }
- # end debug
- $rcd = $this->Query($queryd); # TRUE on success
- if ($rcd) $rcs = $this->LoadSingle($querys); # row (array) if spammer already blacklisted
- // if referrers were deleted, report both deleted referrers and added blacklist records
- {
- if ('' != $par) $par .= '&';
- $par .= 'refdel='.$numrefdeleted;
- $par .= '&bladd=';
- }
- # debug
- if (DEBUG)
- {
- echo 'referrers deleted: '.$numrefdeleted.'<br/>';
- echo 'blacklisted: '.$numblacklisted.'<br/>';
- echo 'par: '.$par.'<br/>';
- }
- # end debug
- }
- // redirect to current page & handler, adding any extra parameters to get back to the original view
- # debug
- if (DEBUG)
- {
- // display link instead of doing redirect so debug output can be seen
- exit;
- }
- # end debug
- $this->Redirect($this->Href($redirect,'',$par));
- ?>
6. actions/header.php
We have created an extension of the stylesheet to style the user-interface elements for these handlers; to avoid (most) problems with all the custom "skins" people are using on this site (and maybe yours as well?), this is kept in a separate file (for now) so most of the new styles will become available. Therefore the extra stylesheet file should be linked into the header template before the general display stylesheet:
Existing actions/header.php:
- <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
- <meta name="keywords" content="<?php echo $this->GetConfigValue("meta_keywords") ?>" />
- <meta name="description" content="<?php echo $this->GetConfigValue("meta_description") ?>" />
- <link rel="stylesheet" type="text/css" href="css/<?php echo ($this->GetCookie("wikiskin"))? $this->GetCookie("wikiskin"): $this->GetConfigValue("stylesheet") ?>" media="screen" />
- <link rel="stylesheet" type="text/css" href="css/print.css" media="print" />
Insert an extra link after line 15:
- <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
- <meta name="keywords" content="<?php echo $this->GetConfigValue("meta_keywords") ?>" />
- <meta name="description" content="<?php echo $this->GetConfigValue("meta_description") ?>" />
- <link rel="stylesheet" type="text/css" href="css/refmenu.css" /><!-- temp: extra styles for referrer handlers -->
- <link rel="stylesheet" type="text/css" href="css/<?php echo ($this->GetCookie("wikiskin"))? $this->GetCookie("wikiskin"): $this->GetConfigValue("stylesheet") ?>" media="screen" />
- <link rel="stylesheet" type="text/css" href="css/print.css" media="print" />
This will put the necessary styling for the referrers handler user interface in place even if a custom skin is used.
7. css/refmenu.css
This is the actual stylesheet file - it will later be integrated in the main wikka stylesheet, of course.
- /*
- This stylesheet is for the referrers and blacklist handlers.
- It will need to be integrated with the main stylesheet.
- JW 2005-07-08 - extended for the dbinfo action and forms.
- */
- h4 {
- margin-top: 0.3em !important; /* remove !important when integrating into main stylesheet or including it after that */
- }
- .refmenu {
- margin: 0;
- padding: 0;
- margin-top: 1em;
- }
- .refmenu .menu {
- margin: 0;
- padding: 0;
- }
- .refmenu .menu li {
- list-style: none;
- float: left;
- margin-right: 3px; /* margin-right goes together with float left (or vice versa) */
- padding: 1px 2px;
- font-size: 85%;
- line-height: 1.2em;
- color: #000000;
- background-color: #DDDDDD;
- }
- br.clear {
- clear: both;
- }
- form fieldset.hidden { /* for all forms! not just referrers/dbinfo */
- display: none;
- }
- #refform, #dbinfo {
- color: inherit;
- background-color: inherit;
- margin-top: 1em;
- margin-bottom: 1em;
- }
- #refform {
- width: 32em;
- }
- #form_dbsel, #form_tablesel {
- width: 40em;
- }
- #refform fieldset, #form_dbsel fieldset, #form_tablesel fieldset {
- padding: 1em;
- margin-bottom: 0.3em;
- border: 1px solid #666666;
- }
- #refform legend, #form_dbsel legend, #form_tablesel legend {
- padding: 0 2px;
- color: #000000;
- background-color: #DDDDDD;
- border: 1px solid #666666;
- margin-bottom: 0.3em;
- }
- #refform .mainlabel {
- float: left;
- width: 4.6em; /* width will work on _floated_ element, even if not a block! */
- padding-right: 0.5em;
- }
- #form_dbsel .mainlabel, #form_tablesel .mainlabel {
- float: left;
- width: 9.8em; /* width will work on _floated_ element, even if not a block! */
- padding-right: 0.5em;
- }
- #q, #qo, #ho {
- width: 10em;
- }
- #h {
- width: 3em;
- text-align: right;
- }
- #reflist {
- margin-top: 1em;
- margin-bottom: 1em;
- border: none;
- }
- #reflist .hits {
- width: 3em;
- padding-right: 5px;
- text-align: right;
- vertical-align: middle;
- }
- #reflist .action {
- width: 5em;
- padding-left: 5px;
- padding-right: 5px;
- text-align: center;
- vertical-align: middle;
- }
- #reflist .refs {
- padding-left: 5px;
- text-align: left;
- vertical-align: middle;
- }
This the version now extended for the DbInfoAction DbInfo action.
8. css/refmenu_col.css
The styling was designed to match with the default Wikka style. If you're using a custom skin here, everything should be positioned and spaced correctly, but the colors may not fit in with yours.
To save you hunting down what would need to be changed, grab this little file and copy it into your own skin on TestSkin: it contains all the color settings using in the extra stylesheet. Then simply adapt the colors to match your own: these will then override those in css/refmenu_col.css.
- /*
- For custom stylesheets: copy this into your stylesheet; the
- adapt the colors here (made to match the default Wikka skin)
- to match your own.
- JW 2005-07-08 - extended for the dbinfo action and forms.
- */
- .refmenu .menu li {
- color: #000000;
- background-color: #DDDDDD;
- }
- form fieldset.hidden { /* for all forms! not just referrers/dbinfo */
- display: none;
- }
- #refform fieldset, #form_dbsel fieldset, #form_tablesel fieldset {
- border: 1px solid #666666;
- }
- #refform legend, #form_dbsel legend, #form_tablesel legend {
- color: #000000;
- background-color: #DDDDDD;
- border: 1px solid #666666;
- }
This the version now extended for the DbInfoAction DbInfo action.
CategoryDevelopmentHandlers