<commit_before><?php
// return all labels in the project

include_once( 'errors.inc.php' );
include_once( 'db.pg.class.php' );
include_once( 'session.class.php' );
include_once( 'tools.inc.php' );
include_once( 'json.inc.php' );

$db =& getDB();
$ses =& getSession();

$pid = isset( $_REQUEST[ 'pid' ] ) ? intval( $_REQUEST[ 'pid' ] ) : 0;
$uid = $ses->isSessionValid() ? $ses->getId() : 0;

$cid = isset( $_REQUEST[ 'cid' ] ) ? intval( $_REQUEST[ 'cid' ] ) : 0;
$nid = isset( $_REQUEST[ 'nid' ] ) ? intval( $_REQUEST[ 'nid' ] ) : 0;

$tags = isset( $_REQUEST[ 'tags' ] ) ? $_REQUEST[ 'tags' ] : '[]';
$ntype = isset( $_REQUEST[ 'ntype' ] ) ? $_REQUEST[ 'ntype' ] : 'treenode';


if ( $pid )
{
  if ( $uid )
  {
    $label_id = $db->getClassId( $pid, 'label' );
    if(!$label_id) { echo makeJSON( array( '"error"' => 'Can not find "label" class for this project' ) ); return; }
    
    $labeled_as_id = $db->getRelationId( $pid, 'labeled_as' );
    if(!$labeled_as_id) { echo makeJSON( array( '"error"' => 'Can not find "labeled_as" relation for this project' ) ); return; }
    
    // delete all treenode_class_instance to labels
    
    if($ntype == "treenode") {
      
      $ids = $db->deleteFrom("treenode_class_instance", ' "treenode_class_instance"."treenode_id" = '.$nid.' AND
      "treenode_class_instance"."relation_id" = '.$labeled_as_id);
    
    } else if ($ntype == "location") {

      $ids = $db->deleteFrom("connector_class_instance", ' "connector_class_instance"."connector_id" = '.$nid.' AND
      "connector_class_instance"."relation_id" = '.$labeled_as_id);

    }
    
    // see which labels have to be created a new and which ones are retrieved
    $ta = explode(',', $tags);
    // remove empty strings
    foreach($ta as $val) {
      // exclusion criteria
      // if value empty, skip
      if(empty($val))
        continue;
      
      // see if label exists
      $res = $db->getResult('SELECT "ci"."name" as "name", "ci"."id" as "id" FROM "class_instance" as "ci" WHERE
      "ci"."class_id" = '.$label_id.' AND 
      "ci"."name" = \''.$val.'\' AND
      "ci"."project_id" = '.$pid);
      if(empty($res)) {<commit_msg>Fix an SQL injection attack<commit_after><?php
// return all labels in the project

include_once( 'errors.inc.php' );
include_once( 'db.pg.class.php' );
include_once( 'session.class.php' );
include_once( 'tools.inc.php' );
include_once( 'json.inc.php' );

$db =& getDB();
$ses =& getSession();

$pid = isset( $_REQUEST[ 'pid' ] ) ? intval( $_REQUEST[ 'pid' ] ) : 0;
$uid = $ses->isSessionValid() ? $ses->getId() : 0;

$cid = isset( $_REQUEST[ 'cid' ] ) ? intval( $_REQUEST[ 'cid' ] ) : 0;
$nid = isset( $_REQUEST[ 'nid' ] ) ? intval( $_REQUEST[ 'nid' ] ) : 0;

$tags = isset( $_REQUEST[ 'tags' ] ) ? $_REQUEST[ 'tags' ] : '[]';
$ntype = isset( $_REQUEST[ 'ntype' ] ) ? $_REQUEST[ 'ntype' ] : 'treenode';


if ( $pid )
{
  if ( $uid )
  {
    $label_id = $db->getClassId( $pid, 'label' );
    if(!$label_id) { echo makeJSON( array( '"error"' => 'Can not find "label" class for this project' ) ); return; }
    
    $labeled_as_id = $db->getRelationId( $pid, 'labeled_as' );
    if(!$labeled_as_id) { echo makeJSON( array( '"error"' => 'Can not find "labeled_as" relation for this project' ) ); return; }
    
    // delete all treenode_class_instance to labels
    
    if($ntype == "treenode") {
      
      $ids = $db->deleteFrom("treenode_class_instance", ' "treenode_class_instance"."treenode_id" = '.$nid.' AND
      "treenode_class_instance"."relation_id" = '.$labeled_as_id);
    
    } else if ($ntype == "location") {

      $ids = $db->deleteFrom("connector_class_instance", ' "connector_class_instance"."connector_id" = '.$nid.' AND
      "connector_class_instance"."relation_id" = '.$labeled_as_id);

    }
    
    // see which labels have to be created a new and which ones are retrieved
    $ta = explode(',', $tags);
    // remove empty strings
    foreach($ta as $val) {
      // exclusion criteria
      // if value empty, skip
      if(empty($val))
        continue;
      
      // see if label exists
      $res = $db->getResult('SELECT "ci"."name" as "name", "ci"."id" as "id" FROM "class_instance" as "ci" WHERE
      "ci"."class_id" = '.$label_id.' AND 
      "ci"."name" = \''.pg_escape_string($val).'\' AND
      "ci"."project_id" = '.$pid);
      if(empty($res)) {
{'fraction_non_alphanumeric': 0.19724137931034483, 'fraction_numerical': 0.0018390804597701149, 'mean_word_length': 3.195756991321119, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3689807', 'n_tokens_mistral': 1604, 'n_tokens_neox': 1566, 'n_words': 372}