/**
  * \brief testing from GetFileLicenses
  * in this test case, this pfile have 2 same license 
  */
 function testGetFileLicensesDul()
 {
     print "test function GetFileLicenses()\n";
     global $PG_CONN;
     global $upload_pk;
     global $uploadtree_pk_parent;
     global $pfile_pk_parent;
     global $agent_pk;
     $sql = "INSERT INTO license_file(rf_fk, agent_fk, pfile_fk) VALUES(1, {$agent_pk}, {$pfile_pk_parent});";
     $result = pg_query($PG_CONN, $sql);
     DBCheckResult($result, $sql, __FILE__, __LINE__);
     pg_free_result($result);
     $license_array = GetFileLicenses($agent_pk, '', $uploadtree_pk_parent, $this->uploadtree_tablename, "yes");
     /** the expected license value */
     $sql = "SELECT rf_shortname from license_ref where rf_pk = 1;";
     $result = pg_query($PG_CONN, $sql);
     DBCheckResult($result, $sql, __FILE__, __LINE__);
     $row = pg_fetch_assoc($result);
     $license_value_expected = $row['rf_shortname'];
     pg_free_result($result);
     $count = count($license_array);
     $this->assertEquals(2, $count);
     $this->assertEquals($license_value_expected, $license_array[1]);
     $this->assertEquals($license_value_expected, $license_array[3]);
 }
示例#2
0
 /**
  * \brief Display the loaded menu and plugins.
  */
 function Output()
 {
     global $Plugins;
     global $PG_CONN;
     //phpinfo();
     $CriteriaCount = 0;
     $V = "";
     $GETvars = "";
     $upload_pk = GetParm("upload", PARM_INTEGER);
     $detail = GetParm("detail", PARM_INTEGER);
     $detail = empty($detail) ? 0 : 1;
     $folic = GetParm("folic", PARM_INTEGER);
     $savebtn = GetParm("savebtn", PARM_RAW);
     $spdxbtn = GetParm("spdxbtn", PARM_RAW);
     $agent_pk = LatestAgentpk($upload_pk, "nomos_ars");
     if (empty($agent_pk)) {
         echo "Missing fossology license data.  Run a license scan on this upload.<br>";
         exit;
     }
     $uploadtree_tablename = GetUploadtreeTableName($upload_pk);
     // Check if we have data in the acme_upload table, if not then load it
     $acme_uploadRec = GetSingleRec("acme_upload", "where upload_fk={$upload_pk} ");
     if (empty($acme_uploadRec)) {
         // populate acme_upload
         $MinCount = 1;
         $nomosAgentpk = LatestAgentpk($upload_pk, "nomos_ars");
         $acme_project_array = $this->GetProjectArray1($upload_pk, $nomosAgentpk, $MinCount);
         // low level
         $this->Populate_acme_upload($acme_project_array, $upload_pk, 1);
         $acme_project_array = $this->GetProjectArray0($upload_pk, $nomosAgentpk, $MinCount);
         // high level
         $this->Populate_acme_upload($acme_project_array, $upload_pk, 0);
     }
     $sql = "select * from acme_upload, acme_project where acme_project_pk=acme_project_fk and detail={$detail} and upload_fk={$upload_pk}";
     $result = pg_query($PG_CONN, $sql);
     DBCheckResult($result, $sql, __FILE__, __LINE__);
     $acme_project_array = pg_fetch_all($result);
     $acme_project_array_orig = $acme_project_array;
     // save the original state so we know which records to update
     /* If the save or spdx buttons were clicked, update $acme_project_array and save the data in the acme_upload table */
     if (!empty($savebtn) or !empty($spdxbtn)) {
         /* First set all projects include to false */
         foreach ($acme_project_array as &$project) {
             $project['include'] = 'f';
         }
         /* Now turn on projects include to match form */
         if (array_key_exists('includeproj', $_POST)) {
             $includeArray = $_POST['includeproj'];
             foreach ($acme_project_array as &$project) {
                 if (array_key_exists($project['acme_project_fk'], $includeArray)) {
                     $project['include'] = "t";
                 }
             }
         }
         /* Finally, update the db with any changed include states */
         $NumRecs = count($acme_project_array);
         for ($i = 0; $i < $NumRecs; $i++) {
             $project = $acme_project_array[$i];
             $project_orig = $acme_project_array_orig[$i];
             if ($project['include'] != $project_orig['include']) {
                 $include = $project['include'] ? "true" : "false";
                 $sql = "update acme_upload set include='{$include}' where acme_upload_pk='{$project['acme_upload_pk']}'";
                 $result = pg_query($PG_CONN, $sql);
                 DBCheckResult($result, $sql, __FILE__, __LINE__);
                 pg_free_result($result);
             }
         }
     }
     /* aggregate the fossology licenses for each pfile and each acme_project */
     if ($folic) {
         foreach ($acme_project_array as &$project) {
             $sql = "select uploadtree_pk from acme_pfile, uploadtree where acme_project_fk={$project['acme_project_fk']} \n                and acme_pfile.pfile_fk=uploadtree.pfile_fk and uploadtree.upload_fk={$upload_pk}";
             $result = pg_query($PG_CONN, $sql);
             DBCheckResult($result, $sql, __FILE__, __LINE__);
             $LicArray = array();
             $ItemLicArray = array();
             while ($acme_pfileRow = pg_fetch_assoc($result)) {
                 $LicArray = GetFileLicenses($agent_pk, '', $acme_pfileRow['uploadtree_pk'], $uploadtree_tablename);
                 foreach ($LicArray as $key => $license) {
                     $ItemLicArray[$key] = $license;
                 }
             }
             $project['licenses'] = '';
             foreach ($ItemLicArray as $license) {
                 if ($license == "No_license_found") {
                     continue;
                 }
                 if (!empty($project['licenses'])) {
                     $project['licenses'] .= ", ";
                 }
                 $project['licenses'] .= $license;
             }
         }
     }
     /* sort $acme_project_array by count desc */
     usort($acme_project_array, 'proj_cmp');
     /* generate and download spdx file */
     if (!empty($spdxbtn)) {
         $spdxfile = $this->GenerateSPDX($acme_project_array);
         $rv = DownloadString2File($spdxfile, "SPDX.rdf file", "xml");
         if ($rv !== true) {
             echo $rv;
         }
     }
     switch ($this->OutputType) {
         case "XML":
             break;
         case "HTML":
             $this->NoHeader = 0;
             $this->OutputOpen("HTML", 1);
             $V .= $this->HTMLForm($acme_project_array, $upload_pk);
             break;
         case "Text":
             break;
         default:
             break;
     }
     if (!$this->OutputToStdout) {
         return $V;
     }
     print $V;
     return;
 }
/**
 * \brief  Same as GetFileLicenses() but returns license list as a single string
 * \param $agent_pk - agent id
 * \param $pfile_pk - pfile id, (if empty, $uploadtree_pk must be given)
 * \param $uploadtree_pk - (used only if $pfile_pk is empty)
 * \param $uploadtree_tablename
 *
 * \return Licenses string for specified file
 * \see GetFileLicenses() 
 */
function GetFileLicenses_string($agent_pk, $pfile_pk, $uploadtree_pk, $uploadtree_tablename = 'uploadtree')
{
    $LicStr = "";
    $LicArray = GetFileLicenses($agent_pk, $pfile_pk, $uploadtree_pk, $uploadtree_tablename);
    return implode($LicArray, ', ');
}
 /**
  * \brief Add license array to Children array.
  */
 function AddLicStr($TreeInfo, &$Children)
 {
     if (!is_array($Children)) {
         return;
     }
     $agent_pk = $TreeInfo['agent_pk'];
     foreach ($Children as &$Child) {
         /** do not get duplicated licenses */
         $Child['licarray'] = GetFileLicenses($agent_pk, 0, $Child['uploadtree_pk']);
         $Child['licstr'] = implode(", ", $Child['licarray']);
     }
 }
 /**
  * This function is called when user output is
  * requested.  This function is responsible for content.
  * The $ToStdout flag is "1" if output should go to stdout, and
  * 0 if it should be returned as a string.  (Strings may be parsed
  * and used by other plugins.)
  */
 function Output()
 {
     if ($this->State != PLUGIN_STATE_READY) {
         return;
     }
     $V = "";
     global $Plugins;
     $View =& $Plugins[plugin_find_id("view")];
     $LicShortname = GetParm("lic", PARM_TEXT);
     $LicIdSet = GetParm("licset", PARM_INTEGER);
     $Item = GetParm("item", PARM_INTEGER);
     $nomosagent_pk = GetParm("napk", PARM_INTEGER);
     $Upload = GetParm("upload", PARM_INTEGER);
     $LicenseFileId = GetParm("fl_pk", PARM_INTEGER);
     /* only display nomos results if we know the nomosagent_pk 
        Otherwise, we don't know what results to display.  */
     $nomos_out = "";
     if (!empty($nomosagent_pk)) {
         $pfile_pk = 0;
         // unknown, only have uploadtree_pk aka $Item
         $nomos_license_array = GetFileLicenses($nomosagent_pk, $pfile_pk, $Item, "uploadtree", "yes");
         //$nomos_license_array = explode(",", $nomos_license_string);
         //print "nomos_license_string is:$nomos_license_string\n";
         //print_r($nomos_license_array);
         if (!empty($nomos_license_array)) {
             $text = _("The Nomos license scanner found:");
             $nomos_out = "{$text} <b>";
         }
         $rec_flag = 0;
         foreach ($nomos_license_array as $fl_pk => $one_license) {
             $one_license = trim($one_license);
             if (0 == $rec_flag) {
                 $rec_flag = 1;
             } else {
                 $nomos_out .= " ,";
             }
             $url_one_license = urlencode($one_license);
             $text = _("License Reference");
             $nomos_out .= "<a title='{$text}' href='javascript:;'";
             $nomos_out .= "onClick=\"javascript:window.open('";
             $nomos_out .= Traceback_uri();
             $nomos_out .= "?mod=view-license";
             $nomos_out .= "&lic=";
             $nomos_out .= $url_one_license;
             $nomos_out .= "&upload=";
             $nomos_out .= $Upload;
             $nomos_out .= "&item=";
             $nomos_out .= $Item;
             $text = _("License Text");
             $nomos_out .= "','{$text}','width=600,height=400,toolbar=no,scrollbars=yes,resizable=yes');\"";
             $nomos_out .= ">{$one_license}";
             $nomos_out .= "</a>";
             /** edit this license */
             $text = _("Edit");
             $nomosAgentpk = LatestAgentpk($Upload, "nomos_ars");
             /** go to the license change page */
             if (plugin_find_id('change_license') >= 0) {
                 $text1 = _("Edit This Licence Reference");
                 $nomos_out .= "<a title='{$text1}' href='" . Traceback_uri() . "?mod=change_license&fl_pk={$fl_pk}";
                 $nomos_out .= "&upload={$Upload}&item={$Item}&napk={$nomosAgentpk}";
                 $nomos_out .= "' style='color:#ff0000;font-style:italic'>[{$text}]</a>";
             }
         }
     }
     if (!empty($LicShortname)) {
         $this->ViewLicenseText($Item, $LicShortname, $LicIdSet, $nomos_out);
         return;
     }
     if (empty($Item)) {
         return;
     }
     $ModBack = GetParm("modback", PARM_STRING);
     if (empty($ModBack) && !empty($nomos_out)) {
         $ModBack = "nomoslicense";
     }
     /* Load bSAM licenses for this file */
     $bsam_plugin_key = plugin_find_id("license");
     /** -1, can not find bsam plugin, or find */
     /** if the bsam plugin does exist, get and show bSAM licenses */
     if (-1 != $bsam_plugin_key) {
         $Results = $this->LicenseGetForFile($Item);
         /* Show bSAM licenses  */
         if (count($Results) <= 0) {
             /*
               Since LicenseGetForFile() doesn't distinguish between files that
               bSAM ran on and found no licenses, and files that bSAM was never
               run on (both cases return no $Results rows), don't tell the
               user a misleading "No licenses found".
             */
             // $View->AddHighlight(-1,-1,'white',NULL,"No licenses found");
             if (empty($ModBack)) {
                 $ModBack = "browse";
             }
         } else {
             foreach ($Results as $R) {
                 if (empty($R['pfile_path'])) {
                     continue;
                 }
                 if (!empty($R['phrase_text'])) {
                     $RefURL = NULL;
                     if ($R['licterm_name'] != 'Phrase') {
                         $R['phrase_text'] = '';
                     }
                 } else {
                     $RefURL = Traceback() . "&lic=" . $R['lic_fk'] . "&licset=" . $R['tok_pfile_start'];
                 }
                 $this->ConvertLicPathToHighlighting($R, $R['licterm_name'], $RefURL);
             }
             if (empty($ModBack)) {
                 $ModBack = "license";
             }
         }
     }
     $View->ShowView(NULL, $ModBack, 1, 1, $nomos_out);
     return;
 }
/**
 * \brief  Same as GetFileLicenses() but returns license list as a single string
 * \param $agent_pk - agent id
 * \param $pfile_pk - pfile id, (if empty, $uploadtree_pk must be given)
 * \param $uploadtree_pk - (used only if $pfile_pk is empty)
 * \param $uploadtree_tablename
 *
 * \return Licenses string for specified file
 * \see GetFileLicenses() 
 */
function GetFileLicenses_string($agent_pk, $pfile_pk, $uploadtree_pk, $uploadtree_tablename = 'uploadtree')
{
    $LicStr = "";
    $LicArray = GetFileLicenses($agent_pk, $pfile_pk, $uploadtree_pk, $uploadtree_tablename);
    $first = true;
    foreach ($LicArray as $Lic) {
        if ($first) {
            $first = false;
        } else {
            $LicStr .= " ,";
        }
        $LicStr .= $Lic;
    }
    return $LicStr;
}