18 use Symfony\Component\HttpFoundation\JsonResponse;
19 use \Symfony\Component\HttpFoundation\Request;
20 use Symfony\Component\HttpFoundation\Response;
24 const NAME =
"ajax_sh_browser";
26 private $uploadtree_tablename =
"";
44 protected $agentNames = array(
'softwareHeritage' =>
'SH');
46 public function __construct()
48 parent::__construct(self::NAME, array(
49 self::TITLE => _(
"Ajax: File Browser"),
50 self::DEPENDENCIES => array(
"fileBrowse"),
52 self::REQUIRES_LOGIN =>
false
55 $this->uploadDao = $this->
getObject(
'dao.upload');
56 $this->licenseDao = $this->
getObject(
'dao.license');
57 $this->agentDao = $this->
getObject(
'dao.agent');
58 $this->shDao = $this->container->get(
'dao.softwareHeritage');
59 $sysconfig = $GLOBALS[
'SysConf'][
'SYSCONFIG'];
60 $this->configuration = [
61 'url' =>
trim($sysconfig[
'SwhURL']),
62 'uri' =>
trim($sysconfig[
'SwhBaseURL']),
63 'content' =>
trim($sysconfig[
'SwhContent']),
64 'maxtime' => intval($sysconfig[
'SwhSleep']),
65 'token' =>
trim($sysconfig[
'SwhToken'])
73 public function handle(Request $request)
75 $upload = intval($request->get(
"upload"));
76 $groupId = Auth::getGroupId();
77 if (!$this->uploadDao->isAccessible($upload, $groupId)) {
78 throw new \Exception(
"Permission Denied");
81 $item = intval($request->get(
"item"));
84 $left = $itemTreeBounds->getLeft();
86 throw new \Exception(
"Job unpack/adj2nest hasn't completed.");
89 $scannerAgents = array_keys($this->agentNames);
90 $scanJobProxy =
new ScanJobProxy($this->agentDao, $upload);
91 $scanJobProxy->createAgentStatus($scannerAgents);
92 $selectedAgentId = intval($request->get(
'agentId'));
93 $tag_pk = intval($request->get(
'tag'));
95 $UniqueTagArray = array();
96 $this->licenseProjector =
new LicenseMap($this->
getObject(
'db.manager'),$groupId,LicenseMap::CONCLUSION,
true);
97 $vars = $this->
createFileListing($tag_pk, $itemTreeBounds, $UniqueTagArray, $selectedAgentId, $groupId, $scanJobProxy, $request);
99 return new JsonResponse(array(
100 'sEcho' => intval($request->get(
'sEcho')),
101 'aaData' => $vars[
'fileData'],
102 'iTotalRecords' => $vars[
'iTotalDisplayRecords'],
103 'iTotalDisplayRecords' => $vars[
'iTotalDisplayRecords']
118 if (!empty($selectedAgentId)) {
119 $agentName = $this->agentDao->getAgentName($selectedAgentId);
120 $selectedScanners = array($agentName=>$selectedAgentId);
122 $selectedScanners = $scanJobProxy->getLatestSuccessfulAgentIds();
127 $isFlat = isset($_GET[
'flatten']);
130 $options = array(UploadTreeProxy::OPT_RANGE => $itemTreeBounds);
132 $options = array(UploadTreeProxy::OPT_REALPARENT => $itemTreeBounds->
getItemId());
135 $searchMap = array();
136 foreach (explode(
' ',$request->get(
'sSearch')) as $pair) {
137 $a = explode(
':',$pair);
138 if (count($a) == 1) {
139 $searchMap[
'head'] = $pair;
141 $searchMap[$a[0]] = $a[1];
145 if (array_key_exists(
'ext', $searchMap) && strlen($searchMap[
'ext'])>=1) {
146 $options[UploadTreeProxy::OPT_EXT] = $searchMap[
'ext'];
148 if (array_key_exists(
'head', $searchMap) && strlen($searchMap[
'head'])>=1) {
149 $options[UploadTreeProxy::OPT_HEAD] = $searchMap[
'head'];
154 $vars[
'iTotalDisplayRecords'] = $descendantView->count();
156 $columnNamesInDatabase = array($isFlat?
'ufile_name':
'lft');
157 $defaultOrder = array(array(0,
"asc"));
158 $orderString = $this->
getObject(
'utils.data_tables_utility')->getSortingString($_GET, $columnNamesInDatabase, $defaultOrder);
163 $orderString .=
" OFFSET $offset";
166 $orderString .=
" LIMIT $limit";
170 $sql = $descendantView->getDbViewQuery().
" $orderString";
171 $dbManager = $this->
getObject(
'db.manager');
173 $dbManager->prepare($stmt=__METHOD__.$orderString,$sql);
174 $res = $dbManager->execute($stmt,$descendantView->getParams());
175 $descendants = $dbManager->fetchAll($res);
176 $dbManager->freeResult($res);
179 if (!empty($tagId)) {
182 if (empty($descendants)) {
183 $vars[
'fileData'] = array();
188 $firstChild = reset($descendants);
189 $lastChild = end($descendants);
190 $nameRange = array($firstChild[
'ufile_name'],$lastChild[
'ufile_name']);
192 $nameRange = array();
196 $pfileLicenses = array();
197 foreach ($selectedScanners as $agentName=>$agentId) {
198 $licensePerPfile = $this->licenseDao->getLicenseIdPerPfileForAgentId($itemTreeBounds, $agentId, $isFlat, $nameRange);
199 foreach ($licensePerPfile as $pfile => $licenseRow) {
200 foreach ($licenseRow as $licId => $row) {
201 $lic = $this->licenseProjector->getProjectedShortname($licId);
202 $pfileLicenses[$pfile][$lic][$agentName] = $row;
209 $tableData = array();
210 $latestSuccessfulAgentIds = $scanJobProxy->getLatestSuccessfulAgentIds();
211 foreach ($descendants as $child) {
216 $baseUri, $UniqueTagArray, $isFlat);
219 $vars[
'fileData'] = $tableData;
232 private function createFileDataRow($child, $uploadId, $selectedAgentId, $uri, &$UniqueTagArray, $isFlat)
234 $fileId = $child[
'pfile_fk'];
235 $childUploadTreeId = $child[
'uploadtree_pk'];
237 if (!empty($fileId)) {
239 $linkUri .=
"?mod=view-license&upload=$uploadId&item=$childUploadTreeId";
240 if ($selectedAgentId) {
241 $linkUri .=
"&agentId=$selectedAgentId";
247 if ($isContainer && !$isFlat) {
248 $uploadtree_pk = $child[
'uploadtree_pk'];
249 $linkUri =
"$uri&item=" . $uploadtree_pk;
250 if ($selectedAgentId) {
251 $linkUri .=
"&agentId=$selectedAgentId";
253 }
else if ($isContainer) {
255 $linkUri =
"$uri&item=" . $uploadtree_pk;
256 if ($selectedAgentId) {
257 $linkUri .=
"&agentId=$selectedAgentId";
263 $fileName = htmlspecialchars($child[
'ufile_name']);
265 $fileName =
"<a href='$linkUri'><span style='color: darkblue'> <b>$fileName</b> </span></a>";
266 }
else if (! empty($linkUri)) {
267 $fileName =
"<a href='$linkUri'>$fileName</a>";
270 $pfileHash = $this->uploadDao->getUploadHashesFromPfileId($fileId);
271 $shRecord = $this->shDao->getSoftwareHetiageRecord($fileId);
274 if (! $isContainer) {
275 $text = _(
"Software Heritage");
276 $shLink = $this->configuration[
'url'] .
277 $this->configuration[
'uri'] . strtolower($pfileHash[
"sha256"]) .
278 $this->configuration[
'content'];
279 $fileListLinks .=
"[<a href='".$shLink.
"' target=\"_blank\">$text</a>]";
282 if (! $isContainer) {
283 $img = $shRecord[
"img"];
286 return [$fileName, $pfileHash[
"sha256"], $shRecord[
"license"], $img, $fileListLinks];
char * uploadtree_tablename
upload.uploadtree_tablename
createFileDataRow($child, $uploadId, $selectedAgentId, $uri, &$UniqueTagArray, $isFlat)
createFileListing($tagId, ItemTreeBounds $itemTreeBounds, &$UniqueTagArray, $selectedAgentId, $groupId, $scanJobProxy, $request)
Contains the constants and helpers for authentication of user.
Wrapper class for license map.
DirGetNonArtifact($UploadtreePk, $uploadtree_tablename='uploadtree')
Given an artifact directory (uploadtree_pk), return the first non-artifact directory (uploadtree_pk).
FileListLinks($upload_fk, $uploadtree_pk, $napk, $pfile_pk, $Recurse=True, &$UniqueTagArray=array(), $uploadtree_tablename="uploadtree", $wantTags=true)
Get list of links: [View][Info][Download]
Traceback_uri()
Get the URI without query to this location.
GetParm($parameterName, $parameterType)
This function will retrieve the variables and check data types.
Traceback_parm_keep($List)
Create a new URI, keeping only these items.
char * trim(char *ptext)
Trimming whitespace.
#define PERM_READ
Read-only permission.