Skip to content

Commit

Permalink
Use bind params on searching data in mass.
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.code.sf.net/p/freeghost/code/trunk@5718 71f96598-fa45-0410-b640-bcd6f8691b32
  • Loading branch information
mastacontrola committed Jun 20, 2016
1 parent bf070c4 commit 4407dfe
Show file tree
Hide file tree
Showing 15 changed files with 61 additions and 43 deletions.
8 changes: 8 additions & 0 deletions packages/web/commons/schema.php
Expand Up @@ -2181,3 +2181,11 @@
"ALTER TABLE `".DATABASE_NAME."`.`snapins` ADD `sHideLog` ENUM('0','1') NOT NULL DEFAULT '0'",
"ALTER TABLE `".DATABASE_NAME."`.`snapins` ADD `sTimeout` INTEGER NOT NULL DEFAULT 0",
);
// 227
$this->schema[] = array(
"ALTER TABLE `".DATABASE_NAME."`.`hosts` CHANGE `hostPending` `hostPending` ENUM('0','1') NOT NULL",
"ALTER TABLE `".DATABASE_NAME."`.`hostMAC` CHANGE `hmPrimary` `hmPrimary` ENUM('0','1') NOT NULL",
"ALTER TABLE `".DATABASE_NAME."`.`hostMAC` CHANGE `hmPending` `hmPending` ENUM('0','1') NOT NULL",
"ALTER TABLE `".DATABASE_NAME."`.`hostMAC` CHANGE `hmIgnoreClient` `hmIgnoreClient` ENUM('0','1') NOT NULL",
"ALTER TABLE `".DATABASE_NAME."`.`hostMAC` CHANGE `hmIgnoreImaging` `hmIgnoreImaging` ENUM('0','1') NOT NULL",
);
4 changes: 2 additions & 2 deletions packages/web/lib/client/registerclient.class.php
Expand Up @@ -13,7 +13,7 @@ public function json() {
$this->Host = self::getClass('Host')
->set('name',$hostname)
->set('description',_('Pending Registration created by FOG_CLIENT'))
->set('pending',1)
->set('pending',(string)1)
->set('enforce',(int)$enforce)
->addModule(self::getSubObjectIDs('Module',array('isDefault'=>1)))
->addPriMAC($PriMAC)
Expand Down Expand Up @@ -51,7 +51,7 @@ public function send() {
$this->Host = self::getClass('Host')
->set('name',$hostname)
->set('description',_('Pending Registration created by FOG_CLIENT'))
->set('pending',1)
->set('pending',(string)1)
->set('enforce',(int)$enforce)
->addModule(self::getSubObjectIDs('Module',array('isDefault'=>1)))
->addPriMAC($PriMAC)
Expand Down
1 change: 1 addition & 0 deletions packages/web/lib/db/pdodb.class.php
Expand Up @@ -67,6 +67,7 @@ public function query($sql, $data = array(), $paramvals = array()) {
if (!self::$db_name) throw new PDOException(_('No database to work off'));
} catch (PDOException $e) {
$this->debug(sprintf('%s %s: %s',_('Failed to'),__FUNCTION__,$e->getMessage()));
die($e->getMessage());
}
return $this;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/web/lib/fog/bootmenu.class.php
Expand Up @@ -278,7 +278,7 @@ private function printImageIgnored() {
$this->printDefault();
}
private function approveHost() {
if ($this->Host->set('pending',null)->save()) {
if ($this->Host->set('pending',(string)0)->save()) {
$Send['approvesuccess'] = array(
'echo Host approved successfully',
'sleep 3'
Expand Down
2 changes: 1 addition & 1 deletion packages/web/lib/fog/fogbase.class.php
Expand Up @@ -459,7 +459,7 @@ public function parseMacList($stringlist,$image = false,$client = false) {
return strtolower(trim($element));
};
if (!is_array($stringlist) && strpos($stringlist,'|')) $MACs = array_values(array_filter(array_unique(array_map($lowerAndTrim,(array)explode('|',$stringlist)))));
if (self::getClass('MACAddressAssociationManager')->count(array('mac'=>$MACs))) $MACs = array_unique(array_merge((array)$MACs,(array)array_values(array_filter(array_unique(array_map($lowerAndTrim,(array)self::getSubObjectIDs('MACAddressAssociation',array('mac'=>$MACs,'pending'=>0),'mac')))))));
if (self::getClass('MACAddressAssociationManager')->count(array('mac'=>$MACs))) $MACs = array_unique(array_merge((array)$MACs,(array)array_values(array_filter(array_unique(array_map($lowerAndTrim,(array)self::getSubObjectIDs('MACAddressAssociation',array('mac'=>$MACs,'pending'=>array((string)0,(string)'',null)),'mac')))))));
if ($client) {
$ClientIgnoredMACs = array_map($lowerAndTrim,(array)self::getSubObjectIDs('MACAddressAssociation',array('mac'=>$MACs,'clientIgnore'=>1),'mac'));
$MACs = array_diff((array)$MACs,(array)$ClientIgnoredMACs);
Expand Down
4 changes: 2 additions & 2 deletions packages/web/lib/fog/fogcore.class.php
Expand Up @@ -79,8 +79,8 @@ public static function setSessionEnv() {
$_SESSION['PluginsInstalled'] = (array)self::getActivePlugins();
$_SESSION['FOG_VIEW_DEFAULT_SCREEN'] = self::getSetting('FOG_VIEW_DEFAULT_SCREEN');
$_SESSION['FOG_FTP_IMAGE_SIZE'] = self::getSetting('FOG_FTP_IMAGE_SIZE');
$_SESSION['Pending-Hosts'] = self::getClass('HostManager')->count(array('pending'=>1));
$_SESSION['Pending-MACs'] = self::getClass('MACAddressAssociationManager')->count(array('pending'=>1));
$_SESSION['Pending-Hosts'] = self::getClass('HostManager')->count(array('pending'=>(string)1));
$_SESSION['Pending-MACs'] = self::getClass('MACAddressAssociationManager')->count(array('pending'=>(string)1));
$_SESSION['DataReturn'] = self::getSetting('FOG_DATA_RETURNED');
$_SESSION['UserCount'] = self::getClass('UserManager')->count();
$_SESSION['GroupCount'] = self::getClass('GroupManager')->count();
Expand Down
21 changes: 15 additions & 6 deletions packages/web/lib/fog/fogmanagercontroller.class.php
Expand Up @@ -37,10 +37,19 @@ public function find($findWhere = array(), $whereOperator = 'AND', $orderBy = 'n
if (count($findWhere)) {
$count = 0;
$whereArray = array();
array_walk($findWhere,function(&$value,&$field) use (&$count,&$onecompare,&$compare,&$whereArray,&$not) {
$params = array();
$values = array();
$i = 0;
array_walk($findWhere,function(&$value,&$field) use (&$count,&$onecompare,&$compare,&$whereArray,&$not,&$params,&$values,&$i) {
$field = trim($field);
if (is_array($value)) $whereArray[] = sprintf("`%s`.`%s`%sIN ('%s')",$this->databaseTable,$this->databaseFields[$field],$not,implode("','",$value));
else $whereArray[] = sprintf("`%s`.`%s`%s%s",$this->databaseTable,$this->databaseFields[$field],(preg_match('#%#',(string)$value) ? $not.'LIKE ' : (trim($not) ? '!' : '').($onecompare ? (!$count ? $compare : '=') : $compare)), ($value === 0 || $value ? "'".(string)$value."'" : null));
$params = array_merge($params,array_map(function(&$val) use($field,&$i) {
return sprintf(':%s_%d',$field,(int)$i++);
},(array)$value));
$values = array_merge($values,array_map(function(&$val) {
return $val;
},(array)$value));
if (is_array($value)) $whereArray[] = sprintf("`%s`.`%s`%sIN (%s)",$this->databaseTable,$this->databaseFields[$field],$not,implode(',',$params));
else $whereArray[] = sprintf("`%s`.`%s`%s%s",$this->databaseTable,$this->databaseFields[$field],(preg_match('#%#',(string)$value) ? $not.'LIKE ' : (trim($not) ? '!' : '').($onecompare ? (!$count ? $compare : '=') : $compare)), $params[$i-1]);
$count++;
unset($value);
return ($whereArray);
Expand Down Expand Up @@ -92,7 +101,7 @@ public function find($findWhere = array(), $whereOperator = 'AND', $orderBy = 'n
}
$data = array();
if ($idField) {
$results = (array)self::$DB->query($query)->fetch('','fetch_all')->get();
$results = (array)self::$DB->query($query,array(),array_combine((array)$params,(array)$values))->fetch('','fetch_all')->get();
array_map(function(&$value) use (&$data) {
return array_walk($value,function(&$val,&$key) use (&$data) {
if (!isset($data[$this->databaseFieldsFlipped[$key]])) {
Expand All @@ -111,7 +120,7 @@ public function find($findWhere = array(), $whereOperator = 'AND', $orderBy = 'n
} else {
$data = array_map(function(&$item) {
return self::getClass($this->childClass)->setQuery($item);
},(array)self::$DB->query($query)->fetch('','fetch_all')->get());
},(array)self::$DB->query($query,array(),array_combine((array)$params,(array)$values))->fetch('','fetch_all')->get());
}
if ($filter) return $filter(array_values(array_filter((array)$data)));
return array_values(array_filter((array)$data));
Expand All @@ -123,7 +132,7 @@ public function count($findWhere = array(), $whereOperator = 'AND', $compare = '
if (count($findWhere)) {
array_walk($findWhere,function(&$value,&$field) use (&$whereArray,$compare){
$field = trim($field);
if (is_array($value)) $whereArray[] = sprintf("`%s`.`%s` IN ('%s')",$this->databaseTable,$this->databaseFields[$field],implode("','",$value));
if (is_array($value)) $whereArray[] = sprintf("`%s`.`%s` IN (%s)",$this->databaseTable,$this->databaseFields[$field],implode(",",$value));
else $whereArray[] = sprintf("`%s`.`%s`%s'%s'",$this->databaseTable,$this->databaseFields[$field],(preg_match('#%#',(string)$value) ? 'LIKE' : $compare), (string)$value);
unset($value,$field);
});
Expand Down
4 changes: 2 additions & 2 deletions packages/web/lib/fog/fogpage.class.php
Expand Up @@ -431,7 +431,7 @@ public function deploy_post() {
} else if ($this->obj instanceof Group && $imagingTasks) {
if ($TaskType->isMulticast() && !$this->obj->doMembersHaveUniformImages()) throw new Exception(_('Hosts do not contain the same image assignments'));
$NoImage = array();
$Hosts = (array)self::getClass('HostManager')->find(array('pending'=>array('',0),'id'=>$this->obj->get('hosts')));
$Hosts = (array)self::getClass('HostManager')->find(array('pending'=>array((string)0,(string)'',null),'id'=>$this->obj->get('hosts')));
array_map(function(&$Host) use (&$NoImage) {
if (!$Host->isValid()) return;
$NoImage[] = (bool)!$Host->getImage()->isValid();
Expand Down Expand Up @@ -494,7 +494,7 @@ public function deploy_post() {
if (!$imagingTasks) $success[] = sprintf('<li>%s</li>',$Host->get('name'));
if ($imagingTasks && $Host->getImage()->get('isEnabled')) $success[] = sprintf('<li>%s &ndash; %s</li>',$Host->get('name'),$Host->getImage()->get('name'));
unset($Host);
},(array)self::getClass('HostManager')->find(array('pending'=>array('',null,0),'id'=>$this->obj->get('hosts'))));
},(array)self::getClass('HostManager')->find(array('pending'=>array((string)0,(string)'',null),'id'=>$this->obj->get('hosts'))));
} else if ($this->obj instanceof Host) {
if ($this->obj->isValid() && !$this->obj->get('pending')) $success[] = sprintf('<li>%s &ndash; %s</li>',$this->obj->get('name'),$this->obj->getImage()->get('name'));
}
Expand Down
20 changes: 10 additions & 10 deletions packages/web/lib/fog/host.class.php
Expand Up @@ -180,22 +180,22 @@ public function save($mainObject = true) {
unset($MAC);
},(array)$DBPriMACs);
unset($DBPriMACs);
$PreOwnedMACs = self::getSubObjectIDs('MACAddressAssociation',array('hostID'=>$this->get('id'),'pending'=>1),'mac',true);
$PreOwnedMACs = self::getSubObjectIDs('MACAddressAssociation',array('hostID'=>$this->get('id'),'pending'=>(string)1),'mac',true);
$RealAddMACs = array_diff((array)$RealAddMACs,(array)$PreOwnedMACs);
unset($PreOwnedMACs);
$DBAddMACs = self::getSubObjectIDs('MACAddressAssociation',array('hostID'=>$this->get('id'),'primary'=>array(0,null),'pending'=>array(0,null)),'mac');
$DBAddMACs = self::getSubObjectIDs('MACAddressAssociation',array('hostID'=>$this->get('id'),'primary'=>array((string)0,(string)'',null),'pending'=>array((string)0,(string)'',null)),'mac');
$RemoveAddMAC = array_diff((array)$DBAddMACs,(array)$RealAddMACs);
if (count($RemoveAddMAC)) {
self::getClass('MACAddressAssociationManager')->destroy(array('hostID'=>$this->get('id'),'mac'=>$RemoveAddMAC));
$DBAddMACs = self::getSubObjectIDs('MACAddressAssociation',array('hostID'=>$this->get('id'),'primary'=>array(0,null),'pending'=>array(0,null)),'mac');
$DBAddMACs = self::getSubObjectIDs('MACAddressAssociation',array('hostID'=>$this->get('id'),'primary'=>array((string)0,(string)'',null),'pending'=>array((string)0,(string)'',null)),'mac');
unset($RemoveAddMAC);
}
array_map(function(&$RealAddMAC) {
self::getClass('MACAddressAssociation')
->set('hostID',$this->get('id'))
->set('mac',$RealAddMAC)
->set('primary',0)
->set('pending',0)
->set('primary',(string)0)
->set('pending',(string)0)
->save();
unset($RealAddMAC);
},(array)array_diff((array)$RealAddMACs,(array)$DBAddMACs));
Expand All @@ -210,22 +210,22 @@ public function save($mainObject = true) {
unset($DBPriMAC);
},(array)$DBPriMACs);
unset($DBPriMACs);
$PreOwnedMACs = self::getSubObjectIDs('MACAddressAssociation',array('hostID'=>$this->get('id'),'pending'=>array(0,null)),'mac',true);
$PreOwnedMACs = self::getSubObjectIDs('MACAddressAssociation',array('hostID'=>$this->get('id'),'pending'=>array((string)0,(string)'',null)),'mac',true);
$RealPendMACs = array_diff((array)$RealPendMACs,(array)$PreOwnedMACs);
unset($PreOwnedMACs);
$DBPendMACs = self::getSubObjectIDs('MACAddressAssociation',array('hostID'=>$this->get('id'),'primary'=>array(0,null),'pending'=>1),'mac');
$DBPendMACs = self::getSubObjectIDs('MACAddressAssociation',array('hostID'=>$this->get('id'),'primary'=>array((string)0,(string)'',null),'pending'=>(string)1),'mac');
$RemovePendMAC = array_diff((array)$DBPendMACs,(array)$RealPendMACs);
if (count($RemovePendMAC)) {
self::getClass('MACAddressAssociationManager')->destroy(array('hostID'=>$this->get('id'),'mac'=>$RemovePendMAC));
$DBPendMACs = self::getSubObjectIDs('MACAddressAssociation',array('primary'=>array(0,null),'pending'=>1),'mac');
$DBPendMACs = self::getSubObjectIDs('MACAddressAssociation',array('primary'=>array((string)0,(string)'',null),'pending'=>(string)1),'mac');
unset($RemovePendMAC);
}
array_map(function(&$RealPendMAC) {
self::getClass('MACAddressAssociation')
->set('hostID',$this->get('id'))
->set('mac',$RealPendMAC)
->set('primary',0)
->set('pending',1)
->set('primary',(string)0)
->set('pending',(string)1)
->save();
unset($RealPendMAC);
},(array)array_diff((array)$RealPendMACs,(array)$DBPendMACs));
Expand Down
2 changes: 1 addition & 1 deletion packages/web/lib/fog/hostmanager.class.php
@@ -1,7 +1,7 @@
<?php
class HostManager extends FOGManagerController {
public function getHostByMacAddresses($MACs) {
$MACHost = self::getSubObjectIDs('MACAddressAssociation',array('pending'=>0,'mac'=>$MACs),'hostID');
$MACHost = self::getSubObjectIDs('MACAddressAssociation',array('pending'=>array((string)0,(string)'',null),'mac'=>$MACs),'hostID');
if (count($MACHost) > 1) throw new Exception(self::$foglang['ErrorMultipleHosts']);
return self::getClass('Host',@min($MACHost));
}
Expand Down
8 changes: 4 additions & 4 deletions packages/web/lib/fog/macaddress.class.php
Expand Up @@ -35,16 +35,16 @@ public function isValid() {
return (bool)preg_match(self::$pattern,$this->MAC);
}
public function isPending() {
return (bool)count(self::getSubObjectIDs('MACAddressAssociation',array('mac'=>$this->__toString(),'pending'=>1)));
return (bool)count(self::getSubObjectIDs('MACAddressAssociation',array('mac'=>$this->__toString(),'pending'=>(string)1)));
}
public function isClientIgnored() {
return (bool)count(self::getSubObjectIDs('MACAddressAssociation',array('mac'=>$this->__toString(),'clientIgnore'=>1)));
return (bool)count(self::getSubObjectIDs('MACAddressAssociation',array('mac'=>$this->__toString(),'clientIgnore'=>(string)1)));
}
public function isPrimary() {
return (bool)count(self::getSubObjectIDs('MACAddressAssociation',array('mac'=>$this->__toString(),'primary'=>1)));
return (bool)count(self::getSubObjectIDs('MACAddressAssociation',array('mac'=>$this->__toString(),'primary'=>(string)1)));
}
public function isImageIgnored() {
return (bool)count(self::getSubObjectIDs('MACAddressAssociation',array('mac'=>$this->__toString(),'imageIgnore'=>1)));
return (bool)count(self::getSubObjectIDs('MACAddressAssociation',array('mac'=>$this->__toString(),'imageIgnore'=>(string)1)));
}
public function getHost() {
return self::getClass('Host',@max(self::getSubObjectIDs('MACAddressAssociation',array('mac'=>$this->__toString()),'hostID')));
Expand Down
6 changes: 3 additions & 3 deletions packages/web/lib/fog/system.class.php
@@ -1,10 +1,10 @@
<?php
class System {
public function __construct() {
define('FOG_VERSION', '8179');
define('FOG_SCHEMA', 226);
define('FOG_VERSION', '8181');
define('FOG_SCHEMA', 227);
define('FOG_BCACHE_VER',75);
define('FOG_SVN_REVISION',5717);
define('FOG_SVN_REVISION',5718);
define('FOG_SVN_LAST_UPDATE', '$LastChangedDate: 2015-01-01 14:16:56 -0500 (Thu, 01 Jan 2015) $');
define('FOG_CLIENT_VERSION', '0.11.0');
define('PHP_VERSION_REQUIRED', '5.3.0');
Expand Down
6 changes: 3 additions & 3 deletions packages/web/lib/pages/dashboardpage.class.php
Expand Up @@ -49,7 +49,7 @@ public function index() {
unset($StorageGroup);
},(array)self::getClass('StorageGroupManager')->find());
printf('<select name="groupsel" style="whitespace: no-wrap; width: 100px; position: relative; top: -22px; left: 140px;">%s</select><div class="fog-variable" id="ActivityActive"></div><div class="fog-variable" id="ActivityQueued"></div><div class="fog-variable" id="ActivitySlots"></div></div></li><!-- Variables -->',ob_get_clean());
$StorageEnabledCount = self::getClass('StorageNodeManager')->count(array('isEnabled'=>1,'isGraphEnabled'=>1));
$StorageEnabledCount = self::getClass('StorageNodeManager')->count(array('isEnabled'=>1,'isGraphEnabled'=>(string)1));
if ($StorageEnabledCount > 0) {
// Disk Usage Pane
printf('<li><h4 class="box" title="%s">%s</h4><div id="diskusage-selector">',_('The selected node\'s image storage disk usage'),_('Storage Node Disk Usage'));
Expand All @@ -69,7 +69,7 @@ public function index() {
$version = array_shift($version);
printf('<option value="%s">%s%s (%s)</option>',$StorageNode->get('id'),$StorageNode->get('name'),($StorageNode->get('isMaster') ? ' *' : ''),$version);
unset($version,$StorageNode);
},self::getClass('StorageNodeManager')->find(array('isEnabled'=>1,'isGraphEnabled'=>1)));
},self::getClass('StorageNodeManager')->find(array('isEnabled'=>1,'isGraphEnabled'=>(string)1)));
printf('<select name="nodesel" style="whitespace: no-wrap; width: 100px; position: relative; top: 100px;">%s</select></div><a href="?node=hwinfo"><div class="graph pie-graph" id="graph-diskusage"></div></a></li>',ob_get_clean());
}
echo '</ul>';
Expand Down Expand Up @@ -102,7 +102,7 @@ public function bandwidth() {
unset($URL);
$data[$StorageNode->get('name')] = json_decode(array_shift($dataSet));
unset($dataSet,$StorageNode);
},self::getClass('StorageNodeManager')->find(array('isGraphEnabled'=>1,'isEnabled'=>1)));
},self::getClass('StorageNodeManager')->find(array('isGraphEnabled'=>(string)1,'isEnabled'=>1)));
echo json_encode((array)$data);
unset($data);
exit;
Expand Down

0 comments on commit 4407dfe

Please sign in to comment.