Source for file Common.php

Documentation is available at Common.php

  1. <?php
  2. /**
  3.  * Common.php
  4.  *
  5.  * PHP version 5
  6.  *
  7.  * LICENSE: This program is free software; you can redistribute it and/or
  8.  * modify it under the terms of the GNU General Public License as published
  9.  * by the Free Software Foundation.
  10.  *
  11.  * @package                     FreeNAC
  12.  * @author                      Hector Ortiz (FreeNAC Core Team)
  13.  * @copyright               2007 FreeNAC
  14.  * @license                     http://www.gnu.org/copyleft/gpl.html   GNU Public License Version 2
  15.  * @version                     SVN: $Id$
  16.  * @link                        http://www.freenac.net
  17.  */
  18.  
  19. /**
  20.  * Define this common parent class to ensure consistent logging and access to configuration settings.
  21.  * This class can be extended with 'common' code to simplify derived classes and ensure consistency.
  22. */
  23. class Common {
  24.    public $conf$logger$db_conn;      // allow access outside derived classes, ideally would be read-only
  25.    
  26.    /**
  27.    * Get the current instance of our Settings and Logger classes
  28.    */
  29.    public function __construct()
  30.    {
  31.       $this->conf=Settings::getInstance();
  32.       $this->logger=Logger::getInstance();
  33.    }    
  34.  
  35.    /**
  36.    * Connect to database via mysqli
  37.    */
  38.   public function getConnection()
  39.   {
  40.     global $dbhost$dbuser$dbpass$dbname;
  41.     try {
  42.       $this->db_conn = new mysqli($dbhost$dbuser$dbpass$dbname);
  43.       if (mysqli_connect_errno(!== 0{
  44.         throw new DatabaseErrorException(mysqli_connect_error());
  45.       }
  46.       //$this->logger->debug( get_class($this). " getConnection() to {$dbname} on $dbhost.", 3);
  47.  
  48.     catch (Exception $e{
  49.       if ($in_db_conn === NULL and isset($conn))
  50.         $conn->close();
  51.       throw $e;
  52.     }
  53.     return $this->db_conn;
  54.   }
  55.  
  56.  
  57.   /**
  58.    * htmlescape: escape date before outputting to a Browser
  59.    */
  60.   public function htmlescape($in_string)
  61.   {
  62.     #return htmlentities($in_string, ENT_QUOTES, 'UTF-8');
  63.     return htmlentities($in_stringENT_QUOTES'ISO-8859-1');
  64.   }
  65.  
  66.   /**
  67.    * sqlescape: call real_escape_string1 with DB connection
  68.    */
  69.   public function sqlescape ($in_string$in_removePct=FALSE)
  70.   {
  71.     $conn=$this->getConnection();     //  make sure we have a DB connection
  72.     return $this->real_escape_string1($in_string$conn$in_removePct);
  73.   }
  74.  
  75.  
  76.   /**
  77.    * Escape possibly dangerous characters, to prevent SQL injection
  78.    * Trim leading/trailing spaces too
  79.    * TBD: what about ';'?
  80.    */
  81.   public function real_escape_string1 ($in_string$in_conn$in_removePct=FALSE)
  82.   {
  83.     $this->logger->debug("real_escape_string1: in_string=$in_string,"3);
  84.     $str=$in_string;
  85.  
  86.     if (!is_numeric($str)){
  87.       // mysqli: prepends backslashes to: \x00, \n, \r, \, ', " and \x1a
  88.       $str $in_conn->real_escape_string($str);
  89.  
  90.       if ($in_removePct)     // escape %
  91.         $str ereg_replace('(%)'"\\\1'"$str);
  92.     }
  93.  
  94.     $this->logger->debug("real_escape_string1: ret=$str"3);
  95.     #return rtrim($str);
  96.     return trim($str);
  97.   }
  98.  
  99. /**
  100.  * Write key events to naclog which is visible from the GUI
  101.  * To view recent entries:
  102.  *   select * from naclog ORDER BY datetime DESC LIMIT 5;
  103.  * @param mixed $level   Level of severity of the message
  104.  * @param mixed $msg     Message to log
  105. */
  106. function log2db($level='info'$msg='')
  107. {
  108.      $conn=$this->getConnection();     //  make sure we have a DB connection
  109.  
  110.      $msg=rtrim($msg);
  111.  
  112.        if (isset($_SERVER['HOSTNAME']))
  113.           $host=$_SERVER['HOSTNAME'];
  114.        else if (isset($_ENV['HOSTNAME']))
  115.           $host=$_ENV['HOSTNAME'];     
  116.        //else do uname -n?     
  117.        else
  118.           $host='';
  119.           
  120.        $q="insert into naclog set what='" .sqlescape($msg)
  121.          ."', host='$host', priority='$level'";
  122.          $this->debug($q3);
  123.          $res $conn->query($q);
  124.        if ($res === FALSE)
  125.           throw new DatabaseErrorException($conn->error);
  126.          
  127.        return true;
  128. }
  129.  
  130. }

Documentation generated on Mon, 17 Nov 2008 01:10:27 +0100 by phpDocumentor 1.4.0