Revision [8053]

This is an old revision of AdvancedReferrersHandler made by JavaWoman on 2005-05-12 21:21:32.

 

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.
This is the development page for an advanced referrers handler.
 


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 then 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 teh referrers handler, and also the review_blacklist and delete_referrer handlers have been modified to integrate seamlessly. The details are below, and the new versiosn will soon be installed as a beta feature on this site, too. --JavaWoman

Preview


Here's an example of how the new interface looks like (it will look better once the extended stylesheet is in place!):

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

Filter view:

Result: 1 referrers linking to HomePage

HitsReferrers
1http://javawoman.com/



Features


Current version: 0.8


Todo:


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).

  1. <?php
  2. /**
  3.  * Display, filter and search a list of referrers or referring sites for the current page or the site as a whole.
  4.  *
  5.  * Usage: append /referrers to the URL of the page
  6.  *      add global=1 to specify referrers for the site instead of the current page
  7.  *      add sites=1 to specify referrerring domains instead of full URLs
  8.  *
  9.  * This handler allows logged-in users to display, filter and search the referrer list for
  10.  * the current page and for the whole site. Current search criteria include strings,
  11.  * number of hits, reference period.
  12.  *
  13.  * @package     Handlers
  14.  * @subpackage  DatabaseHandlers
  15.  * @name        Referrers
  16.  *
  17.  * @author      {@link http://wikka.jsnx.com/DarTar Dario Taraborelli} - code cleanup, search/filter functionality added.
  18.  * @author      {@link http://wikka.jsnx.com/JavaWoman JavaWoman} - more code cleanup, accessibility, integration with referrers_sites
  19.  * @version     0.8
  20.  * @since       Wikka 1.1.6.X
  21.  *
  22.  * @todo        for 1.0:
  23.  *              - clean up debug code
  24.  *              - remove LoadReferrers() from core
  25.  *              - configurable choice hostname (NAME_GLOBAL) or 'this site' (config, installer)
  26.  *              - configurable parameters for building days dropdown (config, installer)
  27.  *              - configurable limit to express days as hours (config, installer)
  28.  *              - build an index on the referrer column in the referrers table (installer)
  29.  *              later:
  30.  *              - (global) icons to represent each of the five views, small and larger versions (menu/page)
  31.  *              - adapt FormOpen() to accept id; then fix form kluge here and in stylesheet
  32.  *              - adapt text definitions to take singular-plural into account
  33.  *              - add paging
  34.  *              - turn list into form with checkboxes to allow mass blacklisting
  35.  *
  36.  * @input       string  $q  optional: string used to filter the referrers;
  37.  *              default: NULL;
  38.  *              the default can be overridden by providing a POST parameter 'q'
  39.  * @input       integer $qo optional: determines the kind of search to be performed for string $q:
  40.  *              1: search for all referrers containing a given string
  41.  *              0: search for all referrers not containing a given string
  42.  *              default: 1;
  43.  *              the default can be overridden by providing a POST parameter 'qo'
  44.  * @input       integer $h  optional: number of hits used to filter the referrers;
  45.  *              default: 1;
  46.  *              the default can be overridden by providing a POST parameter 'h'
  47.  * @input       integer $ho optional: determines the kind of filter to be applied to $h:
  48.  *              1: search for referrers with at least $h hits;
  49.  *              0: search for referrers with no more than $h hits;
  50.  *              default: 1;
  51.  *              the default can be overridden by providing a POST parameter 'ho'
  52.  * @input       integer $days  optional: number of days used to filter the referrers;
  53.  *              default: 1;
  54.  *              the default can be overridden by providing a POST parameter 'h'
  55.  * @input       integer $global optional: switches between local/global referrers:
  56.  *              1: display referrers for the whole site;
  57.  *              0: display referrers for the current page;
  58.  *              default: 0;
  59.  *              the default can be overridden by providing a GET/POST parameter 'global'
  60.  * @input       integer $sites  optional: switches between referring urls and domains
  61.  *              1: display referring sites (domains);
  62.  *              0: display referrers (URLs);
  63.  *              default: 0;
  64.  *              the default can be overridden by providing a GET/POST parameter 'sites'
  65.  * @input       integer $refdel optional: number of referrer records deleted
  66.  * @inpur       integer $bladd  optional: number of blacklist records added
  67.  */
  68.  
  69. // Utilities
  70.  
  71. /**
  72.  * Build an array of numbers consisting of 'ranges' with increasing step size in each 'range'.
  73.  *
  74.  * A list of numbers like this is useful for instance for a dropdown to choose
  75.  * a period expressed in number of days: a difference between 2 and 5 days may
  76.  * be significant while that between 92 and 95 may not be.
  77.  *
  78.  * @author      {@link http://wikka.jsnx.com/JavaWoman JavaWoman}
  79.  * @copyright   Copyright © 2005, Marjolein Katsma
  80.  * @license     http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
  81.  * @version     1.0
  82.  *
  83.  * @param   mixed   $limits required: single integer or array of integers;
  84.  *                  defines the upper limits of the ranges as well as the next step size
  85.  * @param   int     $max    required: upper limit for the whole list
  86.  *                  (will be included if smaller than the largest limit)
  87.  * @param   int     $firstinc optional: increment for the first range; default 1
  88.  * @return  array   resulting list of numbers
  89.  */
  90. function optionRanges($limits, $max, $firstinc = 1)
  91. {
  92.     // initializations
  93.     if (is_int($limits)) $limits = array($limits);
  94.     if ($firstinc < 1) $firstinc = 1;
  95.     $opts = array();
  96.     $inc = $firstinc;
  97.  
  98.     // first element is the first increment
  99.     $opts[] = $inc;
  100.     // each $limit is the upper limit of a 'range'
  101.     foreach ($limits as $limit)
  102.     {
  103.         for ($i = $inc + $inc; $i <= $limit && $i < $max; $i += $inc)
  104.         {
  105.             $opts[] = $i;
  106.         }
  107.         // we quit at $max, even if there are more $limit elements
  108.         if ($limit >= $max)
  109.         {
  110.             // add $max to the list; then break out of the loop
  111.             $opts[] = $max;
  112.             break;
  113.         }
  114.         // when $limit is reached, it becomes the new start and increment for the next 'range'
  115.         $inc = $limit;
  116.     }
  117.  
  118.     return $opts;
  119. }
  120.  
  121. // constants
  122.  
  123. define('DEBUG',FALSE);      # @@@ set TRUE to generate debugging output
  124.  
  125. define('SEARCH_LIKE','LIKE');           # search string operator
  126. define('SEARCH_UNLIKE','NOT LIKE');     # search string operator
  127. define('HITS_DEFAULT', '1');            # (was 0 for referrers, 1 for sites)
  128. define('HITS_MIN_OPTION', '>=');
  129. define('HITS_MAX_OPTION', '<=');
  130.  
  131. define('HOURS_LIMIT',2);                # days expressed as hours               @@@ could be made configurable
  132. define('DAYS_MAX', $this->GetConfigValue('referrers_purge_time'));
  133. define('DAYS_DEFAULT', '7');                    # default period to retrieve    @@@ make configurable
  134.  
  135. $days_limits = array(7,30,90,365);              # ranges for days dropdown      @@@ make configurable
  136.  
  137. // -------------------------------------
  138.  
  139. // initialize parameters
  140.  
  141. $q = NULL;                              # search string
  142. $qo = 1;                                # search string option
  143. $h = HITS_DEFAULT;                      # hits number
  144. $ho = 1;                                # hits option
  145. $days = DAYS_DEFAULT;                   # period selection
  146. $global = FALSE;                        # global (site) or this page only
  147. $sites = FALSE;                         # referrers or referring sites
  148. $refdel = NULL;                         # referrer records deleted
  149. $bladd = NULL;                          # blacklist records added
  150.  
  151. // -------------------------------------
  152.  
  153. // initialize internal variables
  154.  
  155. $string_option = SEARCH_LIKE;           # LIKE or NOT LIKE
  156. $hits_option = HITS_MIN_OPTION;         # MIN (>=) or MAX (<=)
  157. $tag = $this->GetPageTag();
  158. $isAdmin = $this->IsAdmin();
  159. $loggedin = ($isAdmin) ? TRUE : (bool)$this->GetUser();
  160. $pre = $this->config['table_prefix'];
  161. $par = '';
  162.  
  163. $query = '';
  164. $rows = 0;
  165.  
  166. // -------------------------------------
  167.  
  168. // User-interface strings
  169.  
  170. define('NAME_GLOBAL',$this->GetConfigValue('wakka_name'));
  171.  
  172. define('TITLE_REFERRERS','External pages linking to %s');
  173. define('TITLE_SITES','Domains linking to %s');
  174.  
  175. define('REPORT_BLACKLIST','Referrer records removed: %d; blacklist records added: %d');
  176.  
  177. define('TOTAL_REFERRERS','Total: %d referrers linking to %s');
  178. define('TOTAL_SITES','Total: %d referrers linking to %s');
  179.  
  180. // current target
  181. # you can use NAME_GLOBAL instead of 'this site' if the site name is short enough
  182. # @@@ JW: choice between 'this site' and NAME_GLOBAL could be set via configuration (later)
  183. define('TARGET_GLOBAL','this site');
  184. define('TARGET_PAGE',$tag);
  185.  
  186. // menus don't use current target but *possible* targets
  187. define('MENU_REFERRERS','Referrers to %s');
  188. define('MENU_SITES','Domains linking to %s');
  189. define('MENU_REFERRERS_PAGE',sprintf(MENU_REFERRERS,TARGET_PAGE));
  190. define('MENU_SITES_PAGE',sprintf(MENU_SITES,TARGET_PAGE));
  191. define('MENU_REFERRERS_GLOBAL',sprintf(MENU_REFERRERS,TARGET_GLOBAL));
  192. define('MENU_SITES_GLOBAL',sprintf(MENU_SITES,TARGET_GLOBAL));
  193. define('MENU_BLACKLIST','Blacklisted sites');
  194.  
  195. define('FORM_LEGEND','Filter view:');
  196. define('FORM_URL_OPT_REFERRERS','URL:');
  197. define('FORM_URL_OPT_SITES','Domain:');
  198. define('FORM_URL_OPT_TITLE','Select search option');
  199. define('FORM_URL_OPT_1','containing');
  200. define('FORM_URL_OPT_0','not containing');
  201. define('FORM_URL_STRING_LABEL','string');
  202. define('FORM_URL_STRING_TITLE','Enter a search string');
  203. define('FORM_HITS_OPT_LABEL','Hits:');
  204. define('FORM_HITS_OPT_TITLE','Select filter option');
  205. define('FORM_HITS_OPT_1','at least');
  206. define('FORM_HITS_OPT_0','no more than');
  207. define('FORM_HITS_NUM_LABEL','hits');
  208. define('FORM_HITS_NUM_TITLE','Enter number of hits');
  209. define('FORM_DAYS_OPT_LABEL','Period:');
  210. define('FORM_DAYS_OPT_TITLE','Select period in days');
  211. define('FORM_DAYS_NUM_LABEL','days');
  212. define('FORM_SUBMIT_URLS','Show referrers');
  213. define('FORM_SUBMIT_SITES','Show referring domains');
  214.  
  215. define('LIST_PERIOD_HOURS',' (last %d hours)');
  216. define('LIST_PERIOD_DAYS',' (last %d days)');
  217. define('LIST_SUMMARY_REFERRERS','Filtered list of referrers, with hits%s, sorted by number of hits');
  218. define('LIST_SUMMARY_SITES','Filtered list of referring sites, with hits%s, sorted by number of hits');
  219. define('LIST_HEAD_HITS','Hits');
  220. define('LIST_HEAD_ACTION','Action');
  221. define('LIST_HEAD_LIST_REFERRERS','Referrers');
  222. define('LIST_HEAD_LIST_SITES','Referring hosts');
  223. define('LIST_REF_UNKNOWN','unknown');           # make sure the *exact* same string is used in the whitelist definition (delete_referrer.php)
  224. define('LIST_ACTION_DESC',' and links to blacklist spammers');
  225. define('LIST_ACTION_BLACKLIST','Blacklist');
  226. define('LIST_ACTION_BLACKLIST_TITLE','Blacklist this domain');
  227.  
  228. define('SPAM_NOTE','Note to spammers: This page is not indexed by search engines, so don\'t waste your time.');
  229. define('LOGIN_NOTE','You need to login to see referring sites.');
  230.  
  231. // show result counts for target
  232. define('LIST_RESULT_COUNTER_REFERRERS','Result: %d referrers linking to %s');   # @@@ does not take account of singular
  233. define('LIST_RESULT_COUNTER_SITES','Result: %d domains linking to %s');         # @@@ does not take account of singular
  234. // show 'no result' summary for target
  235. define('NONE_NOTE_REFERRERS','No referrers found linking to %s');
  236. define('NONE_NOTE_SITES','No domains found linking to %s');
  237.  
  238.  
  239. // -------------------------------------
  240.  
  241. // fetch and validate parameters
  242.  
  243. // get query string and comparison method
  244. if (isset($_POST['q']))
  245. {
  246.     $tq = trim(strip_tags($_POST['q']));
  247.     if ('' != $tq)
  248.     {
  249.         $q = mysql_real_escape_string($tq);
  250.         if (isset($_POST['qo']))
  251.         {
  252.             $qo = ($_POST['qo'] == '1') ? 1 : 0;
  253.             $string_option = ($qo == 1) ? SEARCH_LIKE : SEARCH_UNLIKE;
  254.         }
  255.     }
  256. }
  257. // get hits and min or max criteria
  258. if (isset($_POST['h']))
  259. {
  260.     $h = (is_numeric($_POST['h'])) ? abs((int)$_POST['h']) : HITS_DEFAULT;  # cast to positive integer if numeric
  261. }
  262. if (isset($_POST['ho']))
  263. {
  264.     $ho = ($_POST['ho'] == '1') ? 1 : 0;
  265.     $hits_option = ($ho == 1) ? HITS_MIN_OPTION : HITS_MAX_OPTION;
  266. }
  267. // get period, not longer than purge time
  268. if (isset($_POST['days']))
  269. {
  270.     $days = (is_numeric($_POST['days'])) ? min(abs((int)$_POST['days']),DAYS_MAX) : DAYS_DEFAULT;
  271. }
  272. // get search target: page or site (global)
  273. if (isset($_POST['global']))
  274. {
  275.     $global = (bool)$_POST['global'];
  276. }
  277. elseif (isset($_GET['global']))
  278. {
  279.     $global = (bool)$_GET['global'];
  280. }
  281. $iglobal = (int)$global;
  282. // get precision: URLS (referrers) or referring sites (domains)
  283. if (isset($_POST['sites']))
  284. {
  285.     $sites = (bool)$_POST['sites'];
  286. }
  287. elseif (isset($_GET['sites']))
  288. {
  289.     $sites = (bool)$_GET['sites'];
  290. }
  291. $isites = (int)$sites;
  292. //get reported values (no validation needed, just cast to integer)
  293. if (isset($_GET['refdel']))
  294. {
  295.     $refdel = (int)$_GET['refdel'];
  296.     $bladd  = (isset($_GET['bladd'])) ? $bladd = (int)$_GET['bladd'] : 0;
  297. }
  298.  
  299. // derive parameters for 'current' links
  300. if ('' != $par)     $par .= '&amp;';
  301. if (1 == $global)   $par .= 'global=1';
  302. if ('' != $par)     $par .= '&amp;';
  303. if (1 == $sites)    $par .= 'sites=1';
  304.  
  305. // -------------------------------------
  306.  
  307. // build query from chunks depending on criteria chosen
  308.  
  309. if ($loggedin)
  310. {
  311.     $query  = 'SELECT referrer';
  312.     if ($sites)
  313.     {
  314.         // add 'host' = domain extracted from referrring URL using this algorithm:
  315.         // find first char after http:// : LOCATE('//',referrer)+2
  316.         // find first / after this: LOCATE('/',referrer,(LOCATE('//',referrer)+2)-1
  317.         // calculate length: (LOCATE('/',referrer,(LOCATE('//',referrer)+2)-1) - (LOCATE('//',referrer)+2)
  318.         // get host (standard): SUBSTRING(referrer FROM (LOCATE('//',referrer)+2) FOR ((LOCATE('/',referrer,(LOCATE('//',referrer)+2)-1) - (LOCATE('//',referrer)+2)))
  319.         // *or*
  320.         // get host (MySQL-specific): SUBSTRING(SUBSTRING_INDEX(referrer,'/',3) FROM (LOCATE('//',referrer)+1))
  321.         $protocol_host = 'SUBSTRING_INDEX(referrer,"/",3)';     # protocol and host: everything before first single /
  322.         $start_host = 'LOCATE("//",referrer)+2';                # start position of host: after //
  323.         $query .= ', SUBSTRING('.$protocol_host.' FROM ('.$start_host.')) AS host';
  324.         // NOTE: COUNT() cannot use a derived column name but it *can* take an expression
  325.         $query .= ', COUNT(SUBSTRING('.$protocol_host.' FROM ('.$start_host.'))) AS num';
  326.         $query .= ' FROM '.$pre.'referrers';
  327.         if (!$global)
  328.         {
  329.             $query .= " WHERE page_tag = '".mysql_real_escape_string($tag)."'";
  330.         }
  331.         if ($days != $max_days)
  332.         {
  333.             $query .= (!strpos($query,'WHERE')) ? ' WHERE' : ' AND';
  334.             $query .= ' TO_DAYS(NOW()) - TO_DAYS(time) <= '.$days;          # filter by period
  335.         }
  336.         $query .= ' GROUP BY host ';
  337.         if (isset($q))
  338.         {
  339.             $query .= ' HAVING host '.$string_option." '%".$q."%'";         # filter by string (derived column so we use HAVING)
  340.         }
  341.         if ($hits_option != HITS_MIN_OPTION || $h != 1)
  342.         {
  343.             $query .= (!strpos($query,'HAVING')) ? ' HAVING' : ' AND';
  344.             $query .= ' num '.$hits_option.' '.$h;                          # filter by hits number (derived column so we use HAVING)
  345.         }
  346.     }
  347.     else
  348.     {
  349.         $query  = 'SELECT referrer';
  350.         $query .= ', COUNT(referrer) AS num';
  351.         $query .= ' FROM '.$pre.'referrers';
  352.         if (!$global)
  353.         {
  354.             $query .= " WHERE page_tag = '".mysql_real_escape_string($tag)."'";
  355.         }
  356.         if (isset($q))
  357.         {
  358.             $query .= (!strpos($query,'WHERE')) ? ' WHERE' : ' AND';
  359.             $query .= ' referrer '.$string_option." '%".$q."%'";            # filter by string
  360.         }
  361.         if ($days != $max_days)
  362.         {
  363.             $query .= (!strpos($query,'WHERE')) ? ' WHERE' : ' AND';
  364.             $query .= ' TO_DAYS(NOW()) - TO_DAYS(time) <= '.$days;          # filter by period
  365.         }
  366.         $query .= ' GROUP BY referrer ';
  367.         if ($hits_option != HITS_MIN_OPTION || $h != 1)
  368.         {
  369.             $query .= ' HAVING num '.$hits_option.' '.$h;                   # filter by hits number (derived column so we use HAVING)
  370.         }
  371.     }
  372.     $query .= ' ORDER BY num DESC, referrer ASC';                           # set order
  373.  
  374.     // get total number of referrers (NOT records!)
  375.     $query_refcount  = 'SELECT COUNT(DISTINCT(referrer)) AS total';         # @@@ referrer column should be indexed to make this really efficient
  376.     $query_refcount .= ' FROM '.$pre.'referrers';
  377.     if (!$global)
  378.     {
  379.         $query_refcount .= " WHERE page_tag = '".mysql_real_escape_string($tag)."'";
  380.     }
  381. }
  382.  
  383. // -------------------------------------
  384.  
  385. // execute query (if logged in)
  386.  
  387. // @@@ NOTE: we don't use LoadReferrers any more since the query is now completely dynamically built
  388. if ($loggedin)
  389. {
  390.     // execute query
  391.     $referrers = $this->LoadAll($query);
  392.     $totalrefs = $this->LoadSingle($query_refcount);
  393. }
  394.  
  395. // -------------------------------------
  396.  
  397. // build UI elements
  398.  
  399. // define current target
  400. # you can use NAME_GLOBAL instead of 'this site' if the site name is short enough
  401. # @@@ JW: choice between 'this site' and NAME_GLOBAL could be set via configuration (later)
  402. $target = ($global) ? TARGET_GLOBAL : TARGET_PAGE;
  403.  
  404. // title
  405. $title  = ($sites) ? sprintf(TITLE_SITES,$target) : sprintf(TITLE_REFERRERS,$target);
  406. $title .= ($days <= HOURS_LIMIT) ? sprintf(LIST_PERIOD_HOURS,24*$days) : sprintf(LIST_PERIOD_DAYS,$days);
  407.  
  408. if (isset($refdel)) $rptblacklisted = sprintf(REPORT_BLACKLIST,$refdel,$bladd);
  409.  
  410. $tot = $totalrefs['total'];
  411. $total = ($sites) ? sprintf(TOTAL_SITES,$tot,$target) : sprintf(TOTAL_REFERRERS,$tot,$target);
  412.  
  413. #$result = ($sites) ? sprintf(LIST_RESULT_COUNTER_SITES,$creferrers,$tot,$target) : sprintf(LIST_RESULT_COUNTER_REFERRERS,$creferrers,$tot,$target);
  414. $creferrers = count($referrers);
  415. $result = ($sites) ? sprintf(LIST_RESULT_COUNTER_SITES,$creferrers,$target) : sprintf(LIST_RESULT_COUNTER_REFERRERS,$creferrers,$target);
  416.  
  417. // menu elements: prevent wrapping within element (these *don't* use current target!
  418. $menu_referrers_page    = str_replace(' ','&nbsp;',MENU_REFERRERS_PAGE);
  419. $menu_sites_page        = str_replace(' ','&nbsp;',MENU_SITES_PAGE);
  420. $menu_referrers_global  = str_replace(' ','&nbsp;',MENU_REFERRERS_GLOBAL);
  421. $menu_sites_global      = str_replace(' ','&nbsp;',MENU_SITES_GLOBAL);
  422. $menu_blacklist         = str_replace(' ','&nbsp;',MENU_BLACKLIST);
  423.  
  424. if ($loggedin)
  425. {
  426.     // menu
  427.     if ($global)
  428.     {
  429.         $m_referrers_page = '<a href="'.$this->Href('referrers').'">'.$menu_referrers_page.'</a>';
  430.         $m_sites_page ='<a href="'.$this->Href('referrers','','sites=1').'">'.$menu_sites_page.'</a>';
  431.         $m_referrers_global = ($sites) ? '<a href="'.$this->Href('referrers','','global=1').'">'.$menu_referrers_global.'</a>' : $menu_referrers_global;
  432.         $m_sites_global = ($sites) ? $menu_sites_global : '<a href="'.$this->Href('referrers','','global=1&sites=1').'">'.$menu_sites_global.'</a>';
  433.     }
  434.     else
  435.     {
  436.         $m_referrers_page = ($sites) ? '<a href="'.$this->Href('referrers').'">'.$menu_referrers_page.'</a>' : $menu_referrers_page;
  437.         $m_sites_page = ($sites) ? $menu_sites_page : '<a href="'.$this->Href('referrers','','sites=1').'">'.$menu_sites_page.'</a>';
  438.         $m_referrers_global = '<a href="'.$this->Href('referrers','','global=1').'">'.$menu_referrers_global.'</a>';
  439.         $m_sites_global = '<a href="'.$this->Href('referrers','','global=1&sites=1').'">'.$menu_sites_global.'</a>';
  440.     }
  441.     $m_blacklist = '<a href="'.$this->Href('review_blacklist').'">'.$menu_blacklist.'</a>';
  442.     $menu  = '<ul class="menu">'."\n";
  443.     $menu .= '<li>'.$m_referrers_page.'</li>';
  444.     $menu .= '<li>'.$m_sites_page.'</li>';
  445.     $menu .= '<li>'.$m_referrers_global.'</li>';
  446.     $menu .= '<li>'.$m_sites_global.'</li>';
  447.     $menu .= '<li>'.$m_blacklist.'</li>';
  448.     $menu .= "\n".'</ul>'."\n";
  449.  
  450.     // days dropdown content
  451.     $daysopts = optionRanges($days_limits,DAYS_MAX);
  452.  
  453.     // form
  454.     $form  = $this->FormOpen('referrers','','post');        # @@@ add parameter for id
  455.     $form .= '<fieldset class="hidden">'."\n";
  456.     $form .= '<input type="hidden" name="global" value="'.$iglobal.'" />'."\n";
  457.     $form .= '<input type="hidden" name="sites" value="'.$isites.'" />'."\n";
  458.     $form .= '</fieldset>'."\n";
  459.     $form .= '<fieldset>'."\n";
  460.     $form .= '<legend>'.FORM_LEGEND.'</legend>'."\n";
  461.  
  462.     $form .= '<label for="qo" class="mainlabel">'.(($sites) ? FORM_URL_OPT_SITES : FORM_URL_OPT_REFERRERS).'</label> '."\n";
  463.     $form .= '<select name="qo" id="qo" title="'.FORM_URL_OPT_TITLE.'">'."\n";
  464.     $form .= '<option value="1"'.(($qo == '1')? ' selected="selected"' : '').'>'.FORM_URL_OPT_1.'</option>'."\n";
  465.     $form .= '<option value="0"'.(($qo == '0')? ' selected="selected"' : '').'>'.FORM_URL_OPT_0.'</option>'."\n";
  466.     $form .= '</select> '."\n";
  467.     $form .= '<label for="q">'.FORM_URL_STRING_LABEL.'</label> '."\n";
  468.     $form .= '<input type ="text" name="q" id="q" title="'.FORM_URL_STRING_TITLE.'" size="10" maxlength="50" value="'.$q.'" />';
  469.  
  470.     $form .= '<br />'."\n";
  471.  
  472.     $form .= '<label for="ho" class="mainlabel">'.FORM_HITS_OPT_LABEL.'</label> '."\n";
  473.     $form .= '<select name="ho" id="ho" title="'.FORM_HITS_OPT_TITLE.'">'."\n";
  474.     $form .= '<option value="1"'.(($ho == '1')? ' selected="selected"' : '').'>'.FORM_HITS_OPT_1.'</option>'."\n";
  475.     $form .= '<option value="0"'.(($ho == '0')? ' selected="selected"' : '').'>'.FORM_HITS_OPT_0.'</option>'."\n";
  476.     $form .= '</select> '."\n";
  477.     $form .= '<input type ="text" name="h" id="h" title="'.FORM_HITS_NUM_TITLE.'" size="5" maxlength="5" value="'.$h.'" />'."\n";
  478.     $form .= ' <label for="h">'.FORM_HITS_NUM_LABEL.'</label>';
  479.  
  480.     $form .= '<br />'."\n";
  481.  
  482.     $form .= '<label for="days" class="mainlabel">'.FORM_DAYS_OPT_LABEL.'</label> '."\n";
  483.     $form .= '<select name="days" id="days" title="'.FORM_DAYS_OPT_TITLE.'">'."\n";
  484.     // build drop-down
  485.     foreach ($daysopts as $opt)
  486.     {
  487.         $selected = ($opt == $days) ? ' selected="selected"' : '';
  488.         $form .= '<option value="'.$opt.'"'.$selected.'>'.$opt.'</option>';
  489.     }
  490.     $form .= '</select> '."\n";
  491.     $form .= ' <label for="h">'.FORM_DAYS_NUM_LABEL.'</label>'."\n";
  492.  
  493.     $form .= '</fieldset>'."\n";
  494.  
  495.     $form .= '<input type="submit" value="'.(($sites) ? FORM_SUBMIT_SITES : FORM_SUBMIT_URLS).'" accesskey="r" />'."\n";
  496.     $form .= $this->FormClose();
  497.  
  498.     // referrers list with admin link for blacklisting
  499.     if ($sites)
  500.     {
  501.         $summary  = ($isAdmin) ? sprintf(LIST_SUMMARY_SITES,LIST_ACTION_DESC) : sprintf(LIST_SUMMARY_SITES,'');
  502.         $refshead = LIST_HEAD_LIST_SITES;
  503.     }
  504.     else
  505.     {
  506.         $summary  = ($isAdmin) ? sprintf(LIST_SUMMARY_REFERRERS,LIST_ACTION_DESC) : sprintf(LIST_SUMMARY_REFERRERS,'');
  507.         $refshead = LIST_HEAD_LIST_REFERRERS;
  508.     }
  509.     if ($isAdmin)
  510.     {
  511.         $redir = ($global||$sites) ? $this->GetMethod().'&amp;'.$par : $this->GetMethod();  # ensure we return to the same view
  512.         $par = ($sites) ? 'spam_site' : 'spam_link';
  513.         $blacklisturl = $this->Href('delete_referrer','',$par.'=').'%s&amp;redirect=%s';
  514.         $blacklink = '<a href="'.$blacklisturl.'" title="'.LIST_ACTION_BLACKLIST_TITLE.'">'.LIST_ACTION_BLACKLIST.'</a>';
  515.     }
  516. }
  517.  
  518. // -------------------------------------
  519.  
  520. // show user interface (pre-template)
  521.  
  522. echo '<div class="page">'."\n";
  523. echo '<h3>'.$title.'</h3>'."\n";
  524. echo '<p><em>'.SPAM_NOTE.'</em></p>'."\n";
  525. # debug
  526. if (DEBUG)
  527. {
  528.     echo 'Query (ref): '.$query.'<br />';
  529.     echo 'Query (sites): '.$query_sites.'<br />';
  530.     echo ($global) ? 'Global: TRUE<br />' : 'Global: FALSE<br />';
  531.     echo ($sites)  ? 'Sites: TRUE<br />' : 'Sites: FALSE<br />';
  532. }
  533. # debug
  534.  
  535. if ($loggedin)
  536. {
  537.     if (isset($refdel)) echo '<p class="notes">'.$rptblacklisted.'</p>';
  538.     echo '<div class="refmenu">'.$menu.'</div><br class="clear" />'."\n";
  539.     echo '<h4>'.$total.'</h4>'."\n";
  540.     echo '<div id="refform">'.$form.'</div>'."\n";  # @@@ kluge until FormOpen() is adapted: id should actually be on form itself and div not necessary!
  541.  
  542.     if ($creferrers != 0)
  543.     {
  544.         echo '<h4>'.$result.'</h4>'."\n";
  545.         echo '<table id="reflist" summary="'.$summary.'">'."\n";
  546.         echo '<thead>';
  547.         echo '<tr><th class="hits" scope="col">'.LIST_HEAD_HITS.'</th>';
  548.         if ($isAdmin) echo '<th class="action" scope="col">'.LIST_HEAD_ACTION.'</th>';
  549.         echo '<th class="refs" scope="col">'.$refshead.'</th></tr>'."\n";
  550.         echo '</thead>'."\n";
  551.         echo '<tbody>'."\n";
  552.  
  553.         foreach ($referrers as $referrer)
  554.         {
  555.             $hits   = $referrer['num'];
  556.             if ($sites)
  557.             {
  558.                 $ref    = $this->htmlspecialchars_ent($referrer['host']);
  559.             }
  560.             else
  561.             {
  562.                 $ref    = $this->htmlspecialchars_ent($referrer['referrer']);
  563.             }
  564.             echo '<tr>';
  565.             echo '<td class="hits">'.$hits.'</td>';
  566.             if ($isAdmin) echo '<td class="action"><span class="keys">'.sprintf($blacklink,$ref,$redir).'</span></td>';
  567.             if ($sites)
  568.             {
  569.                 echo '<td class="refs">'.$ref.'</td>';
  570.             }
  571.             else
  572.             {
  573.                 echo '<td class="refs"><a href="'.$ref.'">'.$ref.'</a></td>';
  574.             }
  575.             echo '</tr>'."\n";
  576.         }
  577.  
  578.         echo '</tbody>'."\n";
  579.         echo '</table>'."\n";
  580.     }
  581.     else
  582.     {
  583.         #echo '<p><em>'.(($sites) ? sprintf(NONE_NOTE_SITES,$tot,$target) : sprintf(NONE_NOTE_REFERRERS,$tot,$target)).'</em></p>'."\n";
  584.         echo '<p><em>'.(($sites) ? sprintf(NONE_NOTE_SITES,$target) : sprintf(NONE_NOTE_REFERRERS,$target)).'</em></p>'."\n";
  585.     }
  586.  
  587.     echo '<div class="refmenu">'.$menu.'</div><br class="clear" />'."\n";
  588. }
  589. else
  590. {
  591.     echo '<p><strong>'.LOGIN_NOTE.'</strong></p>'."\n";
  592. }
  593. echo '</div>'."\n";
  594. ?>


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.

  1. <?php
  2. /**
  3.  * Display, filter and search a list of blacklisted domains.
  4.  *
  5.  * Usage: append /review_blacklist to the URL of the page
  6.  *
  7.  * This handler allows logged-in users to display and search the blacklist; an admin may
  8.  * remove blacklisted domains from the database.
  9.  *
  10.  * @package     Handlers
  11.  * @subpackage  DatabaseHandlers
  12.  * @name        Referrers
  13.  *
  14.  * @author      {@link http://wikka.jsnx.com/JavaWoman JavaWoman} - code cleanup, search/filter functionality added, valid XHTML, accessibility
  15.  * @version     0.8
  16.  * @since       Wikka 1.1.6.X
  17.  *
  18.  * @todo        for 1.0:
  19.  *              - clean up debug code
  20.  *              - configurable choice hostname (NAME_GLOBAL) or 'this site' (config, installer)
  21.  *              - make index on the spammer column in the referrer_blacklist table _unique_ (installer) and remove extra query
  22.  *              later:
  23.  *              - (global) icons to represent each of the five views, small and larger versions (menu/page)
  24.  *              - adapt text definitions to take singular-plural into account
  25.  *              - add paging
  26.  *              - turn list into form with checkboxes to allow mass removing
  27.  *
  28.  * @input       string  $q  optional: string used to filter the referrers;
  29.  *              default: 'NULL;
  30.  *              the default can be overridden by providing a POST parameter 'q'
  31.  * @input       integer $qo optional: determines the kind of search to be performed for string $q:
  32.  *              1: search for all referrers containing a given string
  33.  *              0: search for all referrers not containing a given string
  34.  *              default: 1;
  35.  *              the default can be overridden by providing a POST parameter 'qo'
  36.  * @input       string  $remove  optional: GET parameter - domain to be removed from the blacklist
  37.  *              default: NULL;
  38.  */
  39.  
  40. // constants
  41.  
  42. define('DEBUG',FALSE);      # @@@ set TRUE to generate debugging output
  43.  
  44. define('SEARCH_LIKE','LIKE');           # search string operator
  45. define('SEARCH_UNLIKE','NOT LIKE');     # search string operator
  46.  
  47. // -------------------------------------
  48.  
  49. // initialize parameters
  50.  
  51. $q = NULL;                              # search string
  52. $qo = 1;                                # search string option
  53. $remove = NULL;                         # domain to be removed from the blacklist
  54.  
  55. // -------------------------------------
  56.  
  57. // initialize internal variables
  58.  
  59. $string_option = SEARCH_LIKE;           # LIKE or NOT LIKE
  60. $tag = $this->GetPageTag();
  61. $isAdmin = $this->IsAdmin();
  62. $loggedin = ($isAdmin) ? TRUE : (bool)$this->GetUser();
  63. $pre = $this->config['table_prefix'];
  64.  
  65. $queryd = '';
  66. $querys = '';
  67. $rows = 0;
  68.  
  69. // -------------------------------------
  70.  
  71. // User-interface strings
  72.  
  73. define('TITLE','Blacklisted domains');
  74.  
  75. define('REPORT_REMOVED','Removed: %d records');                         # @@@ does not take account of singular
  76.  
  77. define('TOTAL_BL','Total: %d blacklisted domain');
  78.  
  79. define('MENU_REFERRERS','Referrers to %s');
  80. define('MENU_SITES','Domains linking to %s');
  81. define('MENU_REFERRERS_PAGE',sprintf(MENU_REFERRERS,$tag));
  82. define('MENU_SITES_PAGE',sprintf(MENU_SITES,$tag));
  83. # you can use NAME_GLOBAL instead of 'this site' if the site name is short enough
  84. # @@@ JW: choice between 'this site' and NAME_GLOBAL could be set via configuration (later)
  85. define('MENU_REFERRERS_GLOBAL',sprintf(MENU_REFERRERS,'this site'));
  86. define('MENU_SITES_GLOBAL',sprintf(MENU_SITES,'this site'));
  87. define('MENU_BLACKLIST','Blacklisted sites');
  88.  
  89. define('FORM_LEGEND','Filter view:');
  90. define('FORM_URL_OPT_LABEL','Domain:');
  91. define('FORM_URL_OPT_TITLE','Select search option');
  92. define('FORM_URL_OPT_1','containing');
  93. define('FORM_URL_OPT_0','not containing');
  94. define('FORM_URL_STRING_LABEL','string');
  95. define('FORM_URL_STRING_TITLE','Enter a search string');
  96. define('FORM_SUBMIT_BLACKLIST','Show blacklisted domains');
  97.  
  98. define('LIST_SUMMARY_BL','Filtered list of blacklisted domains%s, sorted alphabetically');
  99. define('LIST_HEAD_ACTION','Action');
  100. define('LIST_HEAD_BL','Blacklisted domains');
  101. define('LIST_ACTION_DESC',' and links to remove domains from the blacklist');
  102. define('LIST_ACTION_BL','Remove');
  103. define('LIST_ACTION_BL_TITLE','Remove this domain from the blacklist');
  104.  
  105. define('LOGIN_NOTE','You need to login to see blacklisted domains.');
  106.  
  107. define('LIST_RESULT_COUNTER_SITES','Result: %d domains');               # @@@ does not take account of singular
  108. define('NONE_NOTE','Blacklist is empty');
  109.  
  110. // -------------------------------------
  111.  
  112. // fetch and validate parameters
  113.  
  114. // get query string and comparison method
  115. if (isset($_POST['q']))
  116. {
  117.     $tq = trim(strip_tags($_POST['q']));
  118.     if ('' != $tq)
  119.     {
  120.         $q = mysql_real_escape_string($tq);
  121.         if (isset($_POST['qo']))
  122.         {
  123.             $qo = ($_POST['qo'] == '1') ? 1 : 0;
  124.             $string_option = ($qo == 1) ? SEARCH_LIKE : SEARCH_UNLIKE;
  125.         }
  126.     }
  127. }
  128. // get host(s) to be removed
  129. if (isset($_GET['remove']))
  130. {
  131.     $remove = mysql_real_escape_string(strip_tags($_GET['remove']));
  132. }
  133.  
  134. // -------------------------------------
  135.  
  136. // build remove query
  137.  
  138. if ($isAdmin)
  139. {
  140.     $queryd = 'DELETE FROM '.$pre.'referrer_blacklist'
  141.             . ' WHERE spammer = "'.$remove.'"';
  142. }
  143.  
  144. // build filter query
  145.  
  146. if ($loggedin)
  147. {
  148.     $querys = 'SELECT * FROM '.$pre.'referrer_blacklist';
  149.     if (isset($q))
  150.     {
  151.         $querys .= ' WHERE spammer '.$string_option." '%".$q."%'";  # filter by string
  152.     }
  153.     $querys .= ' ORDER BY spammer ASC';                             # set order
  154.  
  155.     // get total number of domains in blacklist
  156.     $query_refcount  = 'SELECT COUNT(spammer) AS total';
  157.     $query_refcount .= ' FROM '.$pre.'referrer_blacklist';
  158. }
  159.  
  160. // -------------------------------------
  161.  
  162. // execute query (if logged in)
  163.  
  164. // do a 'remove' query first, then follow with the select query:
  165. // the list should then reflect the situation after removal of a domain
  166. if ($loggedin)
  167. {
  168.     if ($isAdmin && isset($remove))
  169.     {
  170.         $rc = $this->Query($queryd);                                # TRUE on success
  171.         $numbldeleted = mysql_affected_rows();                      # @@@ report back as GET parameter (in $removeurl/$removelink!)
  172.     }
  173.     $blacklist = $this->LoadAll($querys);
  174.     $totalrefs = $this->LoadSingle($query_refcount);
  175. }
  176.  
  177. // -------------------------------------
  178.  
  179. // build UI elements
  180.  
  181. // title
  182. $title = TITLE;
  183.  
  184. if (isset($numbldeleted)) $rptremoved = sprintf(REPORT_REMOVED,$numbldeleted);
  185.  
  186. $tot = $totalrefs['total'];
  187. $total = sprintf(TOTAL_BL,$tot);
  188.  
  189. $cdomains = count($blacklist);
  190. #$result = sprintf(LIST_RESULT_COUNTER_SITES,$cdomains,$tot);
  191. $result = sprintf(LIST_RESULT_COUNTER_SITES,$cdomains);
  192.  
  193. if ($isAdmin)
  194. {
  195.     $removeurl = $this->Href('review_blacklist','','remove=').'%s';
  196.     $removelink = '<a href="'.$removeurl.'" title="'.LIST_ACTION_BL_TITLE.'">'.LIST_ACTION_BL.'</a>';
  197. }
  198.  
  199. // menu elements: prevent wrapping within element (these *don't* use current target!
  200. $menu_referrers_page    = str_replace(' ','&nbsp;',MENU_REFERRERS_PAGE);
  201. $menu_sites_page        = str_replace(' ','&nbsp;',MENU_SITES_PAGE);
  202. $menu_referrers_global  = str_replace(' ','&nbsp;',MENU_REFERRERS_GLOBAL);
  203. $menu_sites_global      = str_replace(' ','&nbsp;',MENU_SITES_GLOBAL);
  204. $menu_blacklist         = str_replace(' ','&nbsp;',MENU_BLACKLIST);
  205.  
  206. if ($loggedin)
  207. {
  208.     // menu
  209.     $m_referrers_page = '<a href="'.$this->Href('referrers').'">'.$menu_referrers_page.'</a>';
  210.     $m_sites_page ='<a href="'.$this->Href('referrers','','sites=1').'">'.$menu_sites_page.'</a>';
  211.     $m_referrers_global = '<a href="'.$this->Href('referrers','','global=1').'">'.$menu_referrers_global.'</a>';
  212.     $m_sites_global = '<a href="'.$this->Href('referrers','','global=1&sites=1').'">'.$menu_sites_global.'</a>';
  213.     $m_blacklist = $menu_blacklist;
  214.     $menu  = '<ul class="menu">'."\n";
  215.     $menu .= '<li>'.$m_referrers_page.'</li>';
  216.     $menu .= '<li>'.$m_sites_page.'</li>';
  217.     $menu .= '<li>'.$m_referrers_global.'</li>';
  218.     $menu .= '<li>'.$m_sites_global.'</li>';
  219.     $menu .= '<li>'.$m_blacklist.'</li>';
  220.     $menu .= "\n".'</ul>'."\n";
  221.  
  222.     // form
  223.     $form  = $this->FormOpen('review_blacklist','','post');     # @@@ add parameter for id
  224.     $form .= '<fieldset>'."\n";
  225.     $form .= '<legend>'.FORM_LEGEND.'</legend>'."\n";
  226.  
  227.     $form .= '<label for="qo" class="mainlabel">'.FORM_URL_OPT_LABEL.'</label> '."\n";
  228.     $form .= '<select name="qo" id="qo" title="'.FORM_URL_OPT_TITLE.'">'."\n";
  229.     $form .= '<option value="1"'.(($qo == '1')? ' selected="selected"' : '').'>'.FORM_URL_OPT_1.'</option>'."\n";
  230.     $form .= '<option value="0"'.(($qo == '0')? ' selected="selected"' : '').'>'.FORM_URL_OPT_0.'</option>'."\n";
  231.     $form .= '</select> '."\n";
  232.     $form .= '<label for="q">'.FORM_URL_STRING_LABEL.'</label> '."\n";
  233.     $form .= '<input type ="text" name="q" id="q" title="'.FORM_URL_STRING_TITLE.'" size="10" maxlength="50" value="'.$q.'" />';
  234.  
  235.     $form .= '</fieldset>'."\n";
  236.  
  237.     $form .= '<input type="submit" value="'.FORM_SUBMIT_BLACKLIST.'" accesskey="b" />'."\n";
  238.     $form .= $this->FormClose();
  239.  
  240.     // blacklist with admin link for removing
  241.     $summary  = ($isAdmin) ? sprintf(LIST_SUMMARY_BL,LIST_ACTION_DESC) : sprintf(LIST_SUMMARY_BL,'');
  242.     $refshead = LIST_HEAD_BL;
  243. }
  244.  
  245. // -------------------------------------
  246.  
  247. // show user interface (pre-template)
  248.  
  249. echo '<div class="page">'."\n";
  250. echo '<h3>'.$title.'</h3>'."\n";
  251. # debug
  252. if (DEBUG)
  253. {
  254.     echo 'Query remove: '.$queryd.'<br />';
  255.     echo 'Query blacklist: '.$querys.'<br />';
  256.     echo 'remove: '.$remove.'<br/>';
  257.     echo 'removed: '.$numbldeleted.'<br/>';
  258. }
  259. # debug
  260. if ($loggedin)
  261. {
  262.     if (isset($numbldeleted)) echo '<p class="notes">'.$rptremoved.'</p>';
  263.     echo '<div class="refmenu">'.$menu.'</div><br class="clear" />'."\n";
  264.     echo '<h4>'.$total.'</h4>'."\n";
  265.     echo '<div id="refform">'.$form.'</div>'."\n";  # @@@ kluge until FormOpen() is adapted: id should actually be on form itself and div not necessary!
  266.  
  267.     if ($cdomains != 0)
  268.     {
  269.         echo '<h4>'.$result.'</h4>'."\n";
  270.         echo '<table id="reflist" summary="'.$summary.'">'."\n";
  271.         echo '<thead>';
  272.         if ($isAdmin) echo '<th class="action" scope="col">'.LIST_HEAD_ACTION.'</th>';
  273.         echo '<th class="refs" scope="col">'.$refshead.'</th></tr>'."\n";
  274.         echo '</thead>'."\n";
  275.         echo '<tbody>'."\n";
  276.         foreach ($blacklist as $spammer)
  277.         {
  278.             $ref    = $this->htmlspecialchars_ent($spammer['spammer']);
  279.             echo '<tr>';
  280.             if ($isAdmin) echo '<td class="action"><span class="keys">'.sprintf($removelink,$ref).'</span></td>';
  281.             echo '<td class="refs">'.$ref.'</td>';
  282.             echo '</tr>'."\n";
  283.         }
  284.         echo '</tbody>'."\n";
  285.         echo '</table>'."\n";
  286.     }
  287.     else
  288.     {
  289.         #echo '<p><em>'.sprintf(NONE_NOTE,$tot).'</em></p>'."\n";
  290.         echo '<p><em>'.sprintf(NONE_NOTE).'</em></p>'."\n";
  291.     }
  292.  
  293.     echo '<div class="refmenu">'.$menu.'</div><br class="clear" />'."\n";
  294. }
  295. else
  296. {
  297.     echo '<p><strong>'.LOGIN_NOTE.'</strong></p>'."\n";
  298. }
  299. echo '</div>'."\n";
  300. ?>


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):

  1. <?php
  2. /**
  3.  * Remove specified URL or referrer domain from the referrer list and add it to the blacklist.
  4.  *
  5.  * Usage: supposed to be used only from link or form produced by the referrers handler.
  6.  *
  7.  * Security:    - can be executed only by an admin (redirect to homepage otherwise)
  8.  *              - redirect to homepage if any parameter is missing or incorrect
  9.  *              - explicitly use GET or POST to retrieve parameters
  10.  *
  11.  * @package     Handlers
  12.  * @subpackage  DatabaseHandlers
  13.  * @name        DeleteReferrer
  14.  *
  15.  * @author      {@link http://wikka.jsnx.com/JavaWoman JavaWoman} - code cleanup, security, (integration with referrers)
  16.  * @version     0.8
  17.  * @since       Wikka 1.1.6.X
  18.  *
  19.  * @todo        for 1.0:
  20.  *              - clean up debug code
  21.  *              - configurable 'whitelist' of hosts that should never be blacklisted (config, installer)
  22.  *              later:
  23.  *              - change to fetching POST parameters when we convert to using form(s))
  24.  *
  25.  * @input       string  $spam_link  required: spammer URL or domain to blacklist.
  26.  * @input       string  $redirect   required: handler for current page to redirect to.
  27.  * @input       int     $global     optional: query parameter for redirecting to the original view; default: 0
  28.  * @input       int     $sites      optional: query parameter for redirecting to the original view; default: 0
  29.  */
  30.  
  31. // constants
  32.  
  33. define('DEBUG',FALSE);      # @@@ set TRUE to generate debugging output
  34.  
  35. define('LIST_REF_UNKNOWN','unknown');                   # make sure this is *exactly* same string as used in referrers.php
  36.  
  37. $whitelist = array(LIST_REF_UNKNOWN,'localhost','127.0.0.1');   # @@@ make this configurable via wikka.config.php
  38.  
  39. // -------------------------------------
  40.  
  41. // initialize parameters
  42.  
  43. $spam_link = NULL;          # site to blacklist from referrers list
  44. $spam_site = NULL;          # domain to blacklist from sites (domains) list
  45. $redirect = NULL;           # handler / query string of referring page to redirect to
  46. $global = 0;                # extra parameter for redirect
  47. $sites = 0;                 # extra parameter for redirect
  48.  
  49. // -------------------------------------
  50.  
  51. // initialize internal variables
  52.  
  53. $isAdmin = $this->IsAdmin();
  54. $home = $this->Href('',$this->config['root_page']);
  55. $pre = $this->config['table_prefix'];
  56. $par = '';
  57.  
  58. // -------------------------------------
  59.  
  60. // User-interface strings
  61.  
  62. define('MSG_NOT_ALLOWED','Blacklisting not allowed');
  63. define('MSG_PAR_ERROR','Cannot blacklist: missing or incorrect parameter');
  64.  
  65. // -------------------------------------
  66.  
  67. // check permission and immediately redirect to home page if check fails
  68.  
  69. if (!$isAdmin) $this->Redirect($home,MSG_NOT_ALLOWED);
  70.  
  71. // -------------------------------------
  72.  
  73. // fetch and validate parameters
  74.  
  75. // ensure we have a spam_link OR spam_site parameter
  76. if(isset($_GET['spam_link']))
  77. {
  78.     $spam_link = strip_tags($_GET['spam_link']);            # blacklisting from referrers list
  79. }
  80. elseif (isset($_GET['spam_site']))
  81. {
  82.     $spam_site = strip_tags($_GET['spam_site']);            # blacklisting from sites list
  83. }
  84. // ensure we have a redirect parameter 'referrers' (we won't allow any other value)
  85. if (isset($_GET['redirect']))
  86. {
  87.     $redirect = preg_match('/^referrers$/',$_GET['redirect']) ? strip_tags($_GET['redirect']) : NULL;
  88. }
  89. if (isset($_GET['global']))
  90. {
  91.     $global = abs((int)$_GET['global']);                    # make sure we have a positive integer
  92. }
  93. if (isset($_GET['sites']))
  94. {
  95.     $sites = abs((int)$_GET['sites']);                      # make sure we have a positive integer
  96. }
  97. # debug
  98. if (DEBUG)
  99. {
  100.     echo 'spamlink: '.$spam_link.'<br/>';
  101.     echo 'spamsite: '.$spam_site.'<br/>';
  102. }
  103. # end debug
  104.  
  105. // check required parameters and immediately redirect to home page if check fails
  106.  
  107. if (!(isset($spam_link) || (isset($spam_site))) || !isset($redirect)) $this->Redirect($home,MSG_PAR_ERROR);
  108.  
  109. // -------------------------------------
  110.  
  111. // derive internal variables
  112.  
  113. // With $spam_link we get a full URL and need to parse out the host name;
  114. // with $spam_site we get a domain: no need to parse anything;
  115. // for both: check against whitelist before acting on it
  116. if (isset($spam_site))
  117. {
  118.     // referring domain already is host name (no need to parse)
  119.     $domain = $spam_site;
  120. }
  121. else
  122. {
  123.     $parsed_url = parse_url($spam_link);
  124.     if (FALSE !== $parsed_url)
  125.     {
  126.         // derive host name from referring URL
  127.         if (isset($parsed_url['host']))
  128.         {
  129.             $domain = $parsed_url['host'];
  130.         }
  131.     }
  132. }
  133. // exclude 'unknown', 'localhost' and others in the "whitelist"
  134. if (!in_array($domain,$whitelist))
  135. {
  136.     $spammer = $domain;
  137. }
  138. # debug
  139. if (DEBUG)
  140. {
  141.     echo 'domain: '.$domain.'<br/>';
  142.     echo 'spammer: '.$spammer.'<br/>';
  143.     #exit;
  144. }
  145. # end debug
  146.  
  147. // prepare extra parameters for redirect
  148. if ('' != $par)     $par .= '&amp;';
  149. if (1 == $global)   $par .= 'global=1';
  150. if ('' != $par)     $par .= '&amp;';
  151. if (1 == $sites)    $par .= 'sites=1';
  152.  
  153. // -------------------------------------
  154.  
  155. // do the blacklisting
  156.  
  157. if (isset($spammer)) {
  158.     // if $spammer = 'wakka' $queryd should remove http://wakka...
  159.     // but NOT http://example.com/wakka from the referrers table
  160.     $hspammer = mysql_real_escape_string('//'.$spammer.'/');    # string to recognize host in referrers table
  161.     $spammer  = mysql_real_escape_string($spammer);             # string to use for spammer in referrer_blacklist table
  162.  
  163.     $queryd = 'DELETE FROM '.$pre.'referrers'
  164.             . ' WHERE referrer LIKE "%'.$hspammer.'%"';
  165.     // check if domain is already blacklisted (must start with $spammer)
  166.     # @@@ JW: should not be necessary if we'd have a _unique_ index on spammer! (let the database do the work)
  167.     $querys = 'SELECT spammer FROM '.$pre.'referrer_blacklist'
  168.             . ' WHERE spammer like "'.$spammer.'%"';
  169.     // add domain to blacklist
  170.     $queryi = 'INSERT INTO '.$pre.'referrer_blacklist'
  171.             . ' SET spammer = "'.$spammer.'"';
  172. # debug
  173. if (DEBUG)
  174. {
  175.     echo 'delete referrers: '.$queryd.'<br/>';
  176.     echo 'check blacklist : '.$querys.'<br/>';
  177.     echo 'blacklist domain: '.$queryi.'<br/>';
  178.     $querye = str_replace('DELETE','EXPLAIN SELECT *',$queryd);
  179.     $explain = $this->LoadAll($querye);
  180.     echo 'Explain:<pre>';
  181.     print_r($explain);
  182.     echo '</pre>';
  183.     $queryes = str_replace('DELETE','SELECT *',$queryd);
  184.     $todelete = $this->LoadAll($queryes);
  185.     echo 'To delete:<pre>';
  186.     print_r($todelete);
  187.     echo '</pre>';
  188.     #exit;
  189. }
  190. # end debug
  191.  
  192.     $rcd = $this->Query($queryd);                           # TRUE on success
  193.     $numrefdeleted = mysql_affected_rows();                 # @@@ report back as GET parameter (in $par)
  194.     if ($rcd) $rcs = $this->LoadSingle($querys);            # row (array) if spammer already blacklisted
  195.     if (!is_array($rcs)) $rci = $this->Query($queryi);      # TRUE on success
  196.     $numblacklisted = mysql_affected_rows();                # @@@ report back as GET parameter (in $par)
  197.  
  198.     // if referrers were deleted, report both deleted referrers and added blacklist records
  199.     if (isset($numrefdeleted))
  200.     {
  201.         if ('' != $par) $par .= '&amp;';
  202.         $par .= 'refdel='.$numrefdeleted;
  203.         $par .= '&amp;bladd=';
  204.         $par .= (isset($numblacklisted)) ? $numblacklisted : 0;
  205.     }
  206. # debug
  207. if (DEBUG)
  208. {
  209.     echo 'referrers deleted: '.$numrefdeleted.'<br/>';
  210.     echo 'blacklisted: '.$numblacklisted.'<br/>';
  211.     echo 'par: '.$par.'<br/>';
  212. }
  213. # end debug
  214. }
  215.  
  216. // redirect to current page & handler, adding any extra parameters to get back to the original view
  217. # debug
  218. if (DEBUG)
  219. {
  220.     // display link instead of doing redirect so debug output can be seen
  221.     echo '<a href="'.$this->Href($redirect,'',$par).'">Back</a>';
  222.     exit;
  223. }
  224. # end debug
  225. $this->Redirect($this->Href($redirect,'',$par));
  226. ?>


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:
  1.     <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  2.     <meta name="keywords" content="<?php echo $this->GetConfigValue("meta_keywords") ?>" />
  3.     <meta name="description" content="<?php echo $this->GetConfigValue("meta_description") ?>" />
  4.     <link rel="stylesheet" type="text/css" href="css/<?php echo ($this->GetCookie("wikiskin"))? $this->GetCookie("wikiskin"): $this->GetConfigValue("stylesheet") ?>" media="screen" />
  5.     <link rel="stylesheet" type="text/css" href="css/print.css" media="print" />


Insert an extra link after line 15:
  1.     <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  2.     <meta name="keywords" content="<?php echo $this->GetConfigValue("meta_keywords") ?>" />
  3.     <meta name="description" content="<?php echo $this->GetConfigValue("meta_description") ?>" />
  4.     <link rel="stylesheet" type="text/css" href="css/refmenu.css" /><!-- temp: extra styles for referrer handlers -->
  5.     <link rel="stylesheet" type="text/css" href="css/<?php echo ($this->GetCookie("wikiskin"))? $this->GetCookie("wikiskin"): $this->GetConfigValue("stylesheet") ?>" media="screen" />
  6.     <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.

  1. /*
  2.     This stylesheet is for the referrers and blacklist handlers.
  3.     It will need to be integrated  with the main stylesheet.
  4. */
  5.  
  6. h4 {
  7.     margin-top: 0.3em !important;   /* remove !important when integrating into main stylesheet or including it after that */
  8. }
  9.  
  10. .refmenu {
  11.     margin: 0;
  12.     padding: 0;
  13.     margin-top: 1em;
  14. }
  15. .refmenu .menu {
  16.     margin: 0;
  17.     padding: 0;
  18. }
  19. .refmenu .menu li {
  20.     list-style: none;
  21.     float: left;
  22.     margin-right: 3px;              /* margin-right goes together with float left (or vice versa) */
  23.     padding: 1px 2px;
  24.     font-size: 85%;
  25.     line-height: 1.2em;
  26.     color: #000000;
  27.     background-color: #DDDDDD;
  28. }
  29. br.clear {
  30.     clear: both;
  31. }
  32.  
  33. form fieldset.hidden {              /* for all forms! not just referrers */
  34.     display: none;
  35. }
  36.  
  37. #refform {
  38.     color: inherit;
  39.     background-color: inherit;
  40.     margin-top: 1em;
  41.     margin-bottom: 1em;
  42.     width: 32em;
  43. }
  44.  
  45. #refform fieldset {
  46.     padding: 1em;
  47.     margin-bottom: 0.3em;
  48.     border: 1px solid #666666;
  49. }
  50.  
  51. #refform legend {
  52.     padding: 0 2px;
  53.     color: #000000;
  54.     background-color: #DDDDDD;
  55.     border: 1px solid #666666;
  56.     margin-bottom: 0.3em;
  57. }
  58.  
  59. #refform .mainlabel {
  60.     float: left;
  61.     width: 4.6em;   /* width will work on _floated_ element, even if not a block! */
  62.     padding-right: 0.5em;
  63. }
  64.  
  65. #q, #qo, #ho {
  66.     width: 10em;
  67. }
  68. #h {
  69.     width: 3em;
  70.     text-align: right;
  71. }
  72.  
  73. #reflist {
  74.     margin-top: 1em;
  75.     margin-bottom: 1em;
  76.     border: none;
  77. }
  78. #reflist .hits {
  79.     width: 3em;
  80.     padding-right: 5px;
  81.     text-align: right;
  82.     vertical-align: middle;
  83. }
  84. #reflist .action {
  85.     width: 4em;
  86.     padding-left: 5px;
  87.     padding-right: 5px;
  88.     text-align: center;
  89.     vertical-align: middle;
  90. }
  91. #reflist .refs {
  92.     padding-left: 5px;
  93.     text-align: left;
  94.     vertical-align: middle;
  95. }


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.

  1. /*
  2.     For custom stylesheets: copy this into your stylesheet; the
  3.     adapt the colors here (made to match the default Wikka skin)
  4.     to match your own.
  5. */
  6.  
  7. .refmenu .menu li {
  8.     color: #000000;
  9.     background-color: #DDDDDD;
  10. }
  11. #refform fieldset {
  12.     border: 1px solid #666666;
  13. }
  14. #refform legend {
  15.     color: #000000;
  16.     background-color: #DDDDDD;
  17.     border: 1px solid #666666;
  18. }



CategoryDevelopment
There are 8 comments on this page. [Show comments]
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki