X-Git-Url: http://git.harvie.cz/?a=blobdiff_plain;f=wwwroot%2Fbackend%2Fmysql%2Fbackend.inc;h=515b344aa00d701f4b52671bb35029d220fdd738;hb=9b7c11be09e7a553b26667f673c6315209ddc210;hp=bea0e50cba6eaa46afeb9aaea646b4fcd21c6915;hpb=bd9d442f63d31ffc0d437175aa571211c0ead8c7;p=mirrors%2FKyberia-bloodline.git diff --git a/wwwroot/backend/mysql/backend.inc b/wwwroot/backend/mysql/backend.inc index bea0e50..515b344 100644 --- a/wwwroot/backend/mysql/backend.inc +++ b/wwwroot/backend/mysql/backend.inc @@ -341,22 +341,77 @@ function getKNeurons($user_id,$interval) { return $k_array; } -// Set synapse weight, +// setSynapse -// XXX TODO +// Set synapse weight. +// If synapse does not exists, create a new one. +// Secure. +// Returns true on sucess, otherwise false. -function setSynapseWeight($synapse_weightl) { +function setSynapse($params){ global $db,$node,$error,$error_messages; - // XXX security fix -// $q="update neurons set synapse_weight='$synapse_weight' where dst = '$dst' and src = '$src'"; -// $q="insert into neurons values('$synapse_weight','$dst','$src',NULL,'synapse',XXX,now(),XXX,$src)"; + // security check + if ((!is_numeric($params['src'])) + or (!is_numeric($params['dst'])) + or (!is_numeric($params['weight']))) { -// $set=$db->query($q); + return false; + } + + $src=$params['src']; + $dst=$params['dst']; + $weight=$params['weight']; + + // weight could be only positive + if ( $weight < 0) { + return false; + } + + // if already exists + $q="select count(src) from neurons where dst ='$dst' and src = '$src'"; + $set=$db->query($q); + $set->next(); + $isrc=$set->getString('count(src)'); + + if ( $isrc > 0 ) { + $q="update neurons set synapse_weight='$weight' + where dst = '$dst' and src = '$src'"; + } else { + // FIXME no vector set + $q="insert into neurons values('$weight','$dst','$src',0,'synapse', + CURRENT_TIMESTAMP(),now(),NULL,$src)"; + } + $set=$db->query($q); return true; } +// getSynapseWeight +// If synapse does not exists, weight is 1 +// Secure. +// Returns synapse weight (from user to node) + +function getSynapseWeight($user_id,$node_id){ + global $db; + + if (!is_numeric($user_id)) + { return -1; } + if (!is_numeric($node_id)) + { return -1; } + + $set=$db->query("select synapse_weight from neurons where src =". + $user_id." and dst = ".$node_id." and link='synapse'"); + + $set->next(); + $synapse_weight=$set->getString('synapse_weight'); + + if (! ($synapse_weight) ) { $synapse_weight = 1;} + + return $synapse_weight; } +} + +?>