Commit 56912109 authored by Gary Allan's avatar Gary Allan
Browse files

synchronize functions/classes with https://github.com/phpipam/phpipam

Fixup index.php and functions/classes/class.phpipamAgent.php

php7.x fixes
parent 8ea87c88
This diff is collapsed.
This diff is collapsed.
<?php
/**
* phpIPAM config.php class
*/
class Config {
private static $config = false;
/**
* Read config.php info self::$config
* @return void
*/
private static function read_config() {
require( dirname(__FILE__)."/../../config.php" );
self::$config = (object) get_defined_vars();
}
/**
* Get setting from config.php
* @param string $name
* @param mixed $default_value
* @return mixed
*/
public static function ValueOf($name, $default_value = false) {
if (self::$config === false) {
self::read_config();
}
if (isset(self::$config->{$name}))
return self::$config->{$name};
else
return $default_value;
}
}
\ No newline at end of file
<?php
/**
* Dummy logging class to make importing code from phpIPAM codebase eaasier.
*/
class Logging {
/**
* write log function
*
* @access public
* @param mixed $command
* @param mixed $details (default: NULL)
* @param int $severity (default: 0)
* @param mixed $username (default: NULL)
* @return void
*/
public function write ($command, $details = NULL, $severity = 0, $username = null) {
}
/**
* Write new changelog to db or send to syslog
*
* @access public
* @param string $object_type
* @param string $action
* @param string $result
* @param array $old (default: array())
* @param array $new (default: array())
* @param bool $mail_changelog (default: true)
* @return boolean|null
*/
public function write_changelog ($object_type, $action, $result, $old = array(), $new = array(), $mail_changelog = true) {
}
}
......@@ -8,27 +8,25 @@
*
*/
class phpipam_mail {
class phpipam_mail extends Common_functions {
/**
* public variables
*/
private $settings = null; //(obj) phpipam settings
private $mail_settings = null; //(obj) mail settings
/**
* private variables
*/
/**
* protected variables
* (obj) mail settings
*
* (default value: null)
*
* @var mixed
* @access private
*/
private $mail_settings = null;
/**
* object holders
* Php_mailer object
*
* @var mixed
* @access public
*/
protected $Result; //for Result printing
public $Php_mailer; //for Php mailer object
public $Php_mailer;
......@@ -40,36 +38,35 @@ class phpipam_mail {
*
* @access public
* @param mixed $settings
* @return void
*/
public function __construct ($settings, $mail_settings) {
# set settings and mailsettings
$this->settings = $settings;
$this->mail_settings= $mail_settings;
# initialize Result
$this->Result = new Result ();
}
$this->mail_settings = $mail_settings;
/**
* Initializes mailer object.
*
* @access public
* @return void
*/
public function initialize_mailer () {
# we need phpmailer
require_once( dirname(__FILE__) . '/../PHPMailer/class.phpmailer.php');
if(file_exists(dirname(__FILE__).'/../PHPMailer/PHPMailerAutoload.php')) {
// legacy versions
require_once( dirname(__FILE__).'/../PHPMailer/PHPMailerAutoload.php');
# initialize object
$this->Php_mailer = new PHPMailer(true); //localhost by default
}
elseif (file_exists(dirname(__FILE__).'/../PHPMailer/src/Exception.php')) {
require_once( dirname(__FILE__).'/../PHPMailer/src/Exception.php');
require_once( dirname(__FILE__).'/../PHPMailer/src/PHPMailer.php');
require_once( dirname(__FILE__).'/../PHPMailer/src/SMTP.php');
$this->Php_mailer = new PHPMailer\PHPMailer\PHPMailer();
} else {
throw new Exception(_('PHPMailer submodule is missing.'));
}
# initialize object
$this->Php_mailer = new PHPMailer(true); //localhost by default
$this->Php_mailer->CharSet="UTF-8"; //set utf8
$this->Php_mailer->SMTPDebug = 0; //default no debugging
# localhost or smtp?
if($this->mail_settings->mtype=="smtp") { $this->set_smtp(); }
if ($this->mail_settings->mtype=="smtp") { $this->set_smtp(); }
}
/**
......@@ -82,11 +79,21 @@ class phpipam_mail {
//set smtp
$this->Php_mailer->isSMTP();
//tls, ssl?
if($this->mail_settings->msecure!='none')
$this->Php_mailer->SMTPSecure = $this->mail_settings->msecure=='ssl' ? 'ssl' : 'tls';
if($this->mail_settings->msecure!='none') {
$this->Php_mailer->SMTPAutoTLS = true;
$this->Php_mailer->SMTPSecure = $this->mail_settings->msecure=='ssl' ? 'ssl' : 'tls';
}
else {
$this->Php_mailer->SMTPAutoTLS = false;
$this->Php_mailer->SMTPSecure = '';
}
//server
$this->Php_mailer->Host = $this->mail_settings->mserver;
$this->Php_mailer->Port = $this->mail_settings->mport;
//permit self-signed certs and dont verify certs
$this->Php_mailer->SMTPOptions = array("ssl"=>array("verify_peer"=>false, "verify_peer_name"=>false, "allow_self_signed"=>true));
// uncomment this to disable AUTOTLS if security is set to none
$this->Php_mailer->SMTPAutoTLS = false;
//set smtp auth
$this->set_smtp_auth();
}
......@@ -97,13 +104,12 @@ class phpipam_mail {
* @access private
* @return void
*/
private function set_smtp_auth () {
if($this->mail_settings->mauth=="yes") {
private function set_smtp_auth() {
if ($this->mail_settings->mauth == "yes") {
$this->Php_mailer->SMTPAuth = true;
$this->Php_mailer->Username = $this->mail_settings->muser;
$this->Php_mailer->Password = $this->mail_settings->mpass;
}
else {
} else {
$this->Php_mailer->SMTPAuth = false;
}
}
......@@ -115,9 +121,9 @@ class phpipam_mail {
* @param mixed $override_settings
* @return void
*/
public function override_settings ($override_settings) {
foreach($override_settings as $k=>$s) {
$this->mail_settings->$k = $s;
public function override_settings($override_settings) {
foreach ($override_settings as $k=>$s) {
$this->mail_settings->{$k} = $s;
}
}
......@@ -129,7 +135,7 @@ class phpipam_mail {
* @return void
*/
public function set_debugging ($level = 2) {
$this->Php_mailer->SMTPDebug = $level==1 ? 1 : 2;
$this->Php_mailer->SMTPDebug = $level == 1 ? 1 : 2;
// output
$this->Php_mailer->Debugoutput = 'html';
}
......@@ -145,10 +151,11 @@ class phpipam_mail {
* Generates mail message
*
* @access public
* @param mixed $body
* @return void
* @param string $body
* @return string
*/
public function generate_message ($body) {
$html = array();
$html[] = $this->set_header (); //set header
$html[] = $this->set_body_start (); //start body
$html[] = $body; //set body
......@@ -166,6 +173,7 @@ class phpipam_mail {
* @return void
*/
public function generate_message_plain ($body) {
$html = array();
$html[] = $body; //set body
$html[] = $this->set_footer_plain (); //set footer
}
......@@ -174,12 +182,14 @@ class phpipam_mail {
* set_header function.
*
* @access private
* @return void
* @return string
*/
private function set_header () {
$html = array();
$html[] = "<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN' 'http://www.w3.org/TR/html4/loose.dtd'>";
$html[] = "<html><head>";
$html[] = "<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>";
$html[] = "<meta name='viewport' content='width=device-width, initial-scale=0.7, maximum-scale=1, user-scalable=no'>";
$html[] = "</head>";
# return
return implode("\n", $html);
......@@ -189,10 +199,29 @@ class phpipam_mail {
* Begins message body
*
* @access private
* @return void
* @return string
*/
private function set_body_start () {
return "<body style='margin:0px;padding:0px;background:#f9f9f9;border-collapse:collapse;'>";
# read config
$config = Config::ValueOf('config');
// set width
$logo_width = isset($config['logo_width']) ? $config['logo_width'] : 220;
$html = array();
$html[] = "<body style='margin:0px;padding:0px;background:#f9f9f9;border-collapse:collapse;'>";
# logo
if(!file_exists( dirname(__FILE__)."/../../css/images/logo/logo.png")) {
$img = ''; // Load built-in image
require( dirname(__FILE__).'/../../app/admin/settings/logo/logo-builtin.php' );
$html[] = $img;
}
else {
$html[] = "<img style='max-width:".$logo_width."px;margin-top:15px;margin-bottom:20px;' alt='phpipam' src='data:image/png;base64,".base64_encode(file_get_contents(dirname(__FILE__)."/../../css/images/logo/logo.png"))."'>";
}
# return
return implode("\n", $html);
}
/**
......@@ -210,7 +239,7 @@ class phpipam_mail {
* ends message body and html
*
* @access private
* @return void
* @return string
*/
private function set_body_end () {
return "</body></html>";
......@@ -220,19 +249,16 @@ class phpipam_mail {
* Sets footer
*
* @access public
* @return void
* @return string
*/
public function set_footer () {
$html[] = "<table style='margin-left:10px;margin-top:25px;width:auto;padding:0px;border-collapse:collapse;'>";
$html[] = "<tr>";
$html[] = " <td><font face='Helvetica, Verdana, Arial, sans-serif' style='font-size:13px;'>E-mail</font></td>";
$html[] = " <td><font face='Helvetica, Verdana, Arial, sans-serif' style='font-size:13px;'><a href='mailto:".$this->settings->siteAdminMail."' style='color:#08c;'>".$this->settings->siteAdminName."</a></font></td>";
$html[] = "</tr>";
$html[] = "<tr>";
$html[] = " <td><font face='Helvetica, Verdana, Arial, sans-serif' style='font-size:13px;'>www</font></td>";
$html[] = " <td><font face='Helvetica, Verdana, Arial, sans-serif' style='font-size:13px;'><a href='".$this->settings->siteURL."' style='color:#08c;'>".$this->settings->siteURL."</a></font></td>";
$html[] = "</tr>";
$html[] = "</table>";
$html = array();
$html[] = "<hr style='margin-left:10px;width:300px;height:0px;margin-top:40px;margin-left:0px;border-top:0px;border-bottom:1px solid #ddd;'>";
$html[] = "<div class='padding-left:10px;'>";
$html[] = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $this->mail_font_style_light This email was automatically generated. You can change your notification settings in account details!</font><br>";
$html[] = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href='".$this->settings->siteURL."' font-size:'11px;'>$this->mail_font_style_href ".$this->settings->siteURL."</font></a><br>";
$html[] = "</div>";
# return
return implode("\n", $html);
}
......@@ -241,12 +267,10 @@ class phpipam_mail {
* Sets plain footer
*
* @access public
* @return void
* @return string
*/
public function set_footer_plain () {
return "\r\n------------------------------\r\n".$this->settings->siteAdminName." (".$this->settings->siteAdminMail.") :: ".$this->settings->siteURL;
}
}
?>
\ No newline at end of file
......@@ -78,6 +78,20 @@ abstract class DB {
*/
protected $port = '3306';
/**
* Cache file to store all results from queries to
*
* structure:
*
* [table][index] = (object) $content
*
*
* (default value: array())
*
* @var array
* @access public
*/
public $cache = array();
......@@ -249,15 +263,24 @@ abstract class DB {
* @access public
* @param mixed $query
* @param array $values (default: array())
* @param integer|null &$rowCount (default: null)
* @return void
*/
public function runQuery($query, $values = array()) {
public function runQuery($query, $values = array(), &$rowCount = null) {
if (!$this->isConnected()) $this->connect();
$result = null;
$statement = $this->pdo->prepare($query);
//debuq
$this->log_query ($statement, $values);
return $statement->execute((array)$values); //this array cast allows single values to be used as the parameter
$this->log_query($statement, $values);
if (is_object($statement)) {
$result = $statement->execute((array)$values); //this array cast allows single values to be used as the parameter
$rowCount = $statement->rowCount();
}
return $result;
}
/**
......@@ -269,9 +292,14 @@ abstract class DB {
* @return void
*/
public function escape($str) {
$str = (string) $str;
if (strlen($str) == 0) return "";
if (!$this->isConnected()) $this->connect();
return $this->unquote_outer($this->pdo->quote((string)$str));
// SQL Injection - strip backquote character
$str = str_replace('`', '', $str);
return $this->unquote_outer($this->pdo->quote($str));
}
/**
......@@ -364,6 +392,12 @@ abstract class DB {
$preparedParamArr[] = '`' . $this->escape($objParam) . '`=?';
}
// exit on no parameters
if(sizeof($preparedParamArr)==0) {
throw new Exception('No values to update');
return false;
}
$preparedParamStr = implode(',', $preparedParamArr);
//primary key 2?
......@@ -497,6 +531,10 @@ abstract class DB {
$sortStr = 'DESC';
}
// change sort fields for vlans and vrfs. ugly :/
if ($tableName=='vlans' && $sortField=='id') { $sortField = "vlanId"; }
if ($tableName=='vrf' && $sortField=='id') { $sortField = "vrfId"; }
//we should escape all of the params that we need to
$tableName = $this->escape($tableName);
$sortField = $this->escape($sortField);
......@@ -512,9 +550,7 @@ abstract class DB {
$results = array();
if (is_object($statement)) {
while ($newObj = $statement->fetchObject($class)) {
$results[] = $newObj;
}
$results = $statement->fetchAll($class == 'stdClass' ? PDO::FETCH_CLASS : PDO::FETCH_NUM);
}
return $results;
......@@ -574,9 +610,32 @@ abstract class DB {
$results = array();
if (is_object($statement)) {
while ($newObj = $statement->fetchObject($class)) {
$results[] = $newObj;
}
$results = $statement->fetchAll($class == 'stdClass' ? PDO::FETCH_CLASS : PDO::FETCH_NUM);
}
return $results;
}
/**
* Get all objects groped by $groupField, array of (id,count(*)) pairs
*
* @param string $tableName
* @param string $groupField
* @return array
*/
public function getGroupBy($tableName, $groupField = 'id') {
if (!$this->isConnected()) $this->connect();
$statement = $this->pdo->prepare("SELECT `$groupField`,COUNT(*) FROM `$tableName` GROUP BY `$groupField`");
//debug
$this->log_query ($statement, array());
$statement->execute();
$results = array();
if (is_object($statement)) {
$results = $statement->fetchAll(PDO::FETCH_KEY_PAIR);
}
return $results;
......@@ -666,6 +725,23 @@ abstract class DB {
}
}
/**
* Escape $result_fields parameter
*
* @access public
* @param string|array $result_fields
* @return string
*/
public function escape_result_fields($result_fields) {
if (empty($result_fields)) return "*";
if (is_array($result_fields)) {
foreach ($result_fields as $i => $f) $result_fields[$i] = "`$f`";
$result_fields = implode(',', $result_fields);
}
return $result_fields;
}
/**
* Searches for object in database
*
......@@ -677,6 +753,7 @@ abstract class DB {
* @param bool $sortAsc (default: true)
* @param bool $like (default: false)
* @param bool $negate (default: false)
* @param string|array $result_fields (default: "*")
* @return void
*/
public function findObjects($table, $field, $value, $sortField = 'id', $sortAsc = true, $like = false, $negate = false, $result_fields = "*") {
......@@ -686,23 +763,18 @@ abstract class DB {
$like === true ? $operator = "LIKE" : $operator = "=";
$negate === true ? $negate_operator = "NOT " : $negate_operator = "";
// set fields
if($result_fields!="*") {
$result_fields_arr = array();
foreach ($result_fields as $f) {
$result_fields_arr[] = "`$f`";
}
// implode
$result_fields = implode(",", $result_fields);
}
$result_fields = $this->escape_result_fields($result_fields);
// change sort fields for vlans and vrfs. ugly :/
if ($table=='vlans' && $sortField=='id') { $sortField = "vlanId"; }
if ($table=='vrf' && $sortField=='id') { $sortField = "vrfId"; }
// subnets
if ($table=="subnets" && $sortField=="subnet_int") {
return $this->getObjectsQuery('SELECT '.$result_fields.',CAST(subnet AS DECIMAL(39,0)) as subnet_int FROM `' . $table . '` WHERE `'. $field .'`'.$negate_operator. $operator .'? ORDER BY `'.$sortField.'` ' . ($sortAsc ? '' : 'DESC') . ';', array($value));
}
else {
return $this->getObjectsQuery('SELECT '.$result_fields.' FROM `' . $table . '` WHERE `'. $field .'`'.$negate_operator. $operator .'? ORDER BY `'.$sortField.'` ' . ($sortAsc ? '' : 'DESC') . ';', array($value));
}
// subnets
if ($table=='subnets' && $sortField=='subnet') {
return $this->getObjectsQuery('SELECT '.$result_fields.' FROM `' . $table . '` WHERE `'. $field .'`'.$negate_operator. $operator .'? ORDER BY LPAD(`subnet`,39,0) ' . ($sortAsc ? '' : 'DESC') . ';', array($value));
} else {
return $this->getObjectsQuery('SELECT '.$result_fields.' FROM `' . $table . '` WHERE `'. $field .'`'.$negate_operator. $operator .'? ORDER BY `'.$sortField.'` ' . ($sortAsc ? '' : 'DESC') . ';', array($value));
}
}
/**
......@@ -734,6 +806,10 @@ abstract class DB {
$objs = $this->getObjectsQuery($query, $values, $class);
$list = array();
if (!is_array($objs))
return $list;
foreach ($objs as $obj) {
$columns = array_values((array)$obj);
$list[] = $columns[0];
......@@ -770,6 +846,22 @@ abstract class DB {
return $this->runQuery('DELETE FROM `'.$tableName.'` WHERE ' . implode(' OR ', $idParts), $ids);
}
/**
* Delete a list of objects from the database based on identifier
*
* @method deleteObjects
* @param string $tableName
* @param string $identifier
* @param mixed $ids
* @return bool
*/
public function deleteObjectsByIdentifier($tableName, $identifier = "id", $id = 0) {
$tableName = $this->escape($tableName);
$identifier = $this->escape($identifier);
return $this->runQuery('DELETE FROM `'.$tableName.'` WHERE `'.$identifier.'` = ?', $id);
}
/**
* Delete specified row
*
......@@ -782,9 +874,10 @@ abstract class DB {
public function deleteRow($tableName, $field, $value, $field2=null, $value2 = null) {
$tableName = $this->escape($tableName);
$field = $this->escape($field);
$field2 = $this->escape($field2);
//multiple
if(!is_null($field2))
if(!empty($field2))
return $this->runQuery('DELETE FROM `'.$tableName.'` WHERE `'.$field.'`=? and `'.$field2.'`=?;', array($value, $value2));
else
return $this->runQuery('DELETE FROM `'.$tableName.'` WHERE `'.$field.'`=?;', array($value));
......@@ -803,6 +896,36 @@ abstract class DB {
//execute
return $this->runQuery('TRUNCATE TABLE `'.$tableName.'`;');
}
/**
* Begin SQL Transaction
*
* @access public
* @return bool
*/
public function beginTransaction() {
return $this->pdo->beginTransaction();
}
/**
* Commit SQL Transaction
*
* @access public
* @return bool
*/
public function