[ Index ]

PHP Cross Reference of Eventum

title

Body

[close]

/include/ -> class.link_filter.php (source)

   1  <?php
   2  /* vim: set expandtab tabstop=4 shiftwidth=4 encoding=utf-8: */
   3  // +----------------------------------------------------------------------+
   4  // | Eventum - Issue Tracking System                                      |
   5  // +----------------------------------------------------------------------+
   6  // | Copyright (c) 2003, 2004, 2005, 2006, 2007 MySQL AB                  |
   7  // |                                                                      |
   8  // | This program is free software; you can redistribute it and/or modify |
   9  // | it under the terms of the GNU General Public License as published by |
  10  // | the Free Software Foundation; either version 2 of the License, or    |
  11  // | (at your option) any later version.                                  |
  12  // |                                                                      |
  13  // | This program is distributed in the hope that it will be useful,      |
  14  // | but WITHOUT ANY WARRANTY; without even the implied warranty of       |
  15  // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        |
  16  // | GNU General Public License for more details.                         |
  17  // |                                                                      |
  18  // | You should have received a copy of the GNU General Public License    |
  19  // | along with this program; if not, write to:                           |
  20  // |                                                                      |
  21  // | Free Software Foundation, Inc.                                       |
  22  // | 59 Temple Place - Suite 330                                          |
  23  // | Boston, MA 02111-1307, USA.                                          |
  24  // +----------------------------------------------------------------------+
  25  // | Authors: Bryan Alsdorf <bryan@mysql.com>                             |
  26  // +----------------------------------------------------------------------+
  27  //
  28  // @(#) $Id: class.link_filter.php 3246 2007-02-09 09:10:12Z glen $
  29  //
  30  
  31  require_once (APP_INC_PATH . "class.user.php");
  32  
  33  /**
  34   * Class to handle parsing content for links.
  35   * 
  36   * @author  Bryan Alsdorf <bryan@mysql.com>
  37   * @version 1.0
  38   */
  39  class Link_Filter
  40  {
  41      /**
  42       * Returns information about a specific link filter.
  43       * 
  44       * @access  public
  45       * @param   integer $lfi_id The ID of the link filter to return info about.
  46       * @return  array An array of information.
  47       */
  48      function getDetails($lfi_id)
  49      {
  50          $sql = "SELECT
  51                      lfi_id,
  52                      lfi_description,
  53                      lfi_usr_role,
  54                      lfi_pattern,
  55                      lfi_replacement
  56                  FROM
  57                      " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "link_filter
  58                  WHERE
  59                      lfi_id = " . Misc::escapeInteger($lfi_id);
  60          $res = $GLOBALS["db_api"]->dbh->getRow($sql, DB_FETCHMODE_ASSOC);
  61          if (PEAR::isError($res)) {
  62              Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__);
  63              return array();
  64          } elseif (count($res) > 0) {
  65              $sql = "SELECT
  66                          plf_prj_id
  67                      FROM
  68                          " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "project_link_filter
  69                      WHERE
  70                          plf_lfi_id = " . $res['lfi_id'];
  71              $projects = $GLOBALS["db_api"]->dbh->getCol($sql);
  72              if (PEAR::isError($projects)) {
  73                  Error_Handler::logError(array($projects->getMessage(), $projects->getDebugInfo()), __FILE__, __LINE__);
  74                  $projects = array();
  75              } elseif (is_null($projects)) {
  76                  $projects = array();
  77              }
  78              $res["projects"] = $projects;
  79          }
  80          return $res;
  81      }
  82  
  83  
  84      /**
  85       * Lists the link filters currently in the system.
  86       * 
  87       * @return array An array of information.
  88       */
  89      function getList()
  90      {
  91          $sql = "SELECT
  92                      lfi_id,
  93                      lfi_description,
  94                      lfi_usr_role,
  95                      lfi_pattern,
  96                      lfi_replacement
  97                  FROM
  98                      " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "link_filter
  99                  ORDER BY
 100                      lfi_id";
 101          $res = $GLOBALS["db_api"]->dbh->getAll($sql, DB_FETCHMODE_ASSOC);
 102          if (PEAR::isError($res)) {
 103              Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__);
 104              return array();
 105          }
 106          for ($i = 0; $i < count($res); $i++) {
 107              $sql = "SELECT
 108                          plf_prj_id,
 109                          prj_title
 110                      FROM
 111                          " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "project_link_filter,
 112                          " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "project
 113                      WHERE
 114                          prj_id = plf_prj_id AND
 115                          plf_lfi_id = " . $res[$i]['lfi_id'];
 116              $projects = $GLOBALS["db_api"]->dbh->getAssoc($sql);
 117              if (PEAR::isError($projects)) {
 118                  Error_Handler::logError(array($projects->getMessage(), $projects->getDebugInfo()), __FILE__, __LINE__);
 119                  $projects = array();
 120              } elseif (is_null($projects)) {
 121                  $projects = array();
 122              }
 123              $res[$i]["projects"] = array_keys($projects);
 124              $res[$i]["project_names"] = array_values($projects);
 125              $res[$i]["min_usr_role_name"] = User::getRole($res[$i]["lfi_usr_role"]);
 126          }
 127          return $res;
 128      }
 129  
 130  
 131      /**
 132       * Inserts a new link filter into the database.
 133       * 
 134       * @return integer 1 if insert was successful, -1 otherwise
 135       */
 136      function insert()
 137      {
 138          $sql = "INSERT INTO
 139                      " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "link_filter
 140                  (
 141                      lfi_pattern,
 142                      lfi_replacement,
 143                      lfi_usr_role,
 144                      lfi_description
 145                  ) VALUES (
 146                      '" . Misc::escapeString($_REQUEST["pattern"]) . "',
 147                      '" . Misc::escapeString($_REQUEST["replacement"]) . "',
 148                      '" . Misc::escapeInteger($_REQUEST["usr_role"]) . "',
 149                      '" . Misc::escapeString($_REQUEST["description"]) . "'
 150                  )";
 151          $res = $GLOBALS["db_api"]->dbh->query($sql);
 152          if (PEAR::isError($res)) {
 153              Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__);
 154              return -1;
 155          } else {
 156              $lfi_id = $GLOBALS["db_api"]->get_last_insert_id();
 157              foreach ($_REQUEST["projects"] as $prj_id) {
 158                  $sql = "INSERT INTO
 159                              " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "project_link_filter
 160                          (
 161                              plf_prj_id,
 162                              plf_lfi_id
 163                          ) VALUES (
 164                              $prj_id,
 165                              $lfi_id
 166                          )";
 167                  $res = $GLOBALS["db_api"]->dbh->query($sql);
 168                  if (PEAR::isError($res)) {
 169                      Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__);
 170                      return -1;
 171                  }
 172              }
 173              return 1;
 174          }
 175      }
 176  
 177  
 178      /**
 179       * Removes link filters from the database
 180       * 
 181       * @return integer 1 if delete was successful, -1 otherwise.
 182       */
 183      function remove()
 184      {
 185          $sql = "DELETE FROM
 186                      " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "link_filter
 187                  WHERE
 188                      lfi_id IN(" . join(',', Misc::escapeInteger($_REQUEST["items"])) . ")";
 189          $res = $GLOBALS["db_api"]->dbh->query($sql);
 190          if (PEAR::isError($res)) {
 191              Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__);
 192              return -1;
 193          }
 194          $sql = "DELETE FROM
 195                      " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "project_link_filter
 196                  WHERE
 197                      plf_lfi_id IN(" . join(',', Misc::escapeInteger($_REQUEST["items"])) . ")";
 198          $res = $GLOBALS["db_api"]->dbh->query($sql);
 199          if (PEAR::isError($res)) {
 200              Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__);
 201              return -1;
 202          }
 203          return 1;
 204      }
 205  
 206  
 207      /**
 208       * Updates link filter information.
 209       * 
 210       * @return integer 1 if insert was successful, -1 otherwise
 211       */
 212      function update()
 213      {
 214          $sql = "UPDATE
 215                      " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "link_filter
 216                  SET
 217                      lfi_pattern = '" . Misc::escapeString($_REQUEST["pattern"]) . "',
 218                      lfi_replacement = '" . Misc::escapeString($_REQUEST["replacement"]) . "',
 219                      lfi_usr_role = '" . Misc::escapeInteger($_REQUEST["usr_role"]) . "',
 220                      lfi_description = '" . Misc::escapeString($_REQUEST["description"]) . "'
 221                  WHERE
 222                      lfi_id = " . $_REQUEST["id"];
 223          $res = $GLOBALS["db_api"]->dbh->query($sql);
 224          if (PEAR::isError($res)) {
 225              Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__);
 226              return -1;
 227          } else {
 228              $sql = "DELETE FROM
 229                          " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "project_link_filter
 230                      WHERE
 231                          plf_lfi_id = " . Misc::escapeInteger($_REQUEST["id"]);
 232              $res = $GLOBALS["db_api"]->dbh->query($sql);
 233              if (PEAR::isError($res)) {
 234                  Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__);
 235                  return -1;
 236              }
 237              foreach (Misc::escapeInteger($_REQUEST["projects"]) as $prj_id) {
 238                  $sql = "INSERT INTO
 239                              " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "project_link_filter
 240                          (
 241                              plf_prj_id,
 242                              plf_lfi_id
 243                          ) VALUES (
 244                              $prj_id,
 245                              " . Misc::escapeInteger($_REQUEST["id"]) . "
 246                          )";
 247                  $res = $GLOBALS["db_api"]->dbh->query($sql);
 248                  if (PEAR::isError($res)) {
 249                      Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__);
 250                      return -1;
 251                  }
 252              }
 253              return 1;
 254          }
 255      }
 256  
 257  
 258      /**
 259       * Processes text through all link filters.
 260       * 
 261       * @access  public
 262       * @param   integer $prj_id The ID of the project
 263       * @param   string $text The text to process
 264       * @param   string $class The CSS class to use on the actual links
 265       * @return  string The processed text.
 266       */
 267      function processText($prj_id, $text, $class = "link")
 268      {
 269          
 270          // process issue link seperatly since it has to do something special
 271          $text = Misc::activateLinks($text, $class);
 272          $text = Link_Filter::processIssueSpecificLinks($text);
 273          
 274          $filters = Link_Filter::getFilters($prj_id);
 275          
 276          if (count($filters) > 0) {
 277              foreach ($filters as $filter) {
 278                  $text = preg_replace('/' . $filter[0] . '/i', $filter[1], $text);
 279              }
 280          }
 281          
 282          return $text;
 283      }
 284  
 285  
 286      /**
 287       * Callback function to be used from template class.
 288       * 
 289       * @access  public
 290       * @param   string $text The text to process
 291       * @return  string the processed text.
 292       */
 293      function activateLinks($text)
 294      {
 295          return Link_Filter::processText(Auth::getCurrentProject(), $text);
 296      }
 297  
 298  
 299      /**
 300       * Returns an array of patterns and replacements.
 301       * 
 302       * @access  private
 303       * @param   integer $prj_id The ID of the project
 304       * @return  array An array of patterns and replacements
 305       */
 306      function getFilters($prj_id)
 307      {
 308          static $filters;
 309  
 310          $prj_id = Misc::escapeInteger($prj_id);
 311          
 312          // poor man's caching system
 313          if (!empty($filters[$prj_id])) {
 314              return $filters[$prj_id];
 315          }
 316  
 317          $stmt = "SELECT
 318                      lfi_pattern,
 319                      lfi_replacement
 320                  FROM
 321                      " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "link_filter,
 322                      " . APP_DEFAULT_DB . "." . APP_TABLE_PREFIX . "project_link_filter
 323                  WHERE
 324                      lfi_id = plf_lfi_id AND
 325                      lfi_usr_role < " . Auth::getCurrentRole() . " AND
 326                      plf_prj_id = $prj_id
 327                  ORDER BY
 328                      lfi_id";
 329          $res = $GLOBALS["db_api"]->dbh->getAll($stmt);
 330          if (PEAR::isError($res)) {
 331              Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__);
 332              return array();
 333          } else {
 334              $filters[$prj_id] = $res;
 335              return $res;
 336          }
 337      }
 338  
 339  
 340      /**
 341       * Method used as a callback with the regular expression code that parses
 342       * text and creates links to other issues.
 343       *
 344       * @access  public
 345       * @param   array $matches Regular expression matches
 346       * @return  string The link to the appropriate issue
 347       */
 348      function callbackIssueLinks($matches)
 349      {
 350          require_once (APP_INC_PATH . "class.issue.php");
 351          // check if the issue is still open
 352          if (Issue::isClosed($matches[5])) {
 353              $class = 'closed_link';
 354          } else {
 355              $class = 'link';
 356          }
 357          $issue_title = Issue::getTitle($matches[5]);
 358          return "<a title=\"issue " . $matches[5] . " - $issue_title\" class=\"" . $class . "\" href=\"view.php?id=" . $matches[5] . "\">" . $matches[1] . $matches[2] . $matches[3] . $matches[4] . $matches[5] . "</a>";
 359      }
 360  
 361  
 362      /**
 363       * Method used to parse the given string for references to issues in the
 364       * system, and creating links to those if any are found.
 365       *
 366       * @access  private
 367       * @param   string $text The text to search against
 368       * @param   string $class The CSS class to use on the actual links
 369       * @return  string The parsed string
 370       */
 371      function processIssueSpecificLinks($text, $class = "link")
 372      {
 373          $text = preg_replace_callback("/(issue)(:)?(\s)(\#)?(\d+)/i", array('Link_Filter', 'callbackIssueLinks'), $text);
 374          return $text;
 375      }
 376  }


Generated: Wed Dec 19 21:21:33 2007 Cross-referenced by PHPXref 0.7