FOSSology  4.4.0
Open Source License Compliance by Open Source Software
parseBrowseMenu.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2008 Hewlett-Packard Development Company, L.P.
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
24 {
25  public $page;
26  public $browseList = array();
27  public $noRows = FALSE;
28  public $emptyTable = FALSE;
29  private $tableId;
30  private $title;
31  protected $anchors = array();
32 
33  private $test;
34 
35  function __construct($page,$tblId,$title=1)
36  {
37  if (empty ($page)) { return; }
38  $this->page = $page;
39  if (strlen($tblId) == 0) { return; }
40  $this->tableId = $tblId;
41  $this->title = $title;
42  }
43 
77  function fromFile($nodeString)
78  {
79  $parts = explode(': ', $nodeString);
80  //print "parts are:\n";print_r($parts) . "\n";
81  if(!empty($parts[1]))
82  {
83  $uploadName = $parts[1];
84  //print "fromFile: returning $uploadName\n";
85  return($uploadName);
86  }
87  return(NULL);
88  }
89 
90  function fromFS($nodeString)
91  {
92  $parts = explode(': ', $nodeString);
93  //print "parts are:\n";print_r($parts) . "\n";
94  $uploadName = pathinfo($parts[1], PATHINFO_FILENAME);
95  if(strlen($uploadName) != 0)
96  {
97  //print "fromFS: returning $uploadName\n";
98  return($uploadName);
99  }
100  return(NULL);
101  }
102 
103  function fromURL($nodeString)
104  {
105  $parts = explode(': ', $nodeString);
106  //print "parts are:\n";print_r($parts) . "\n";
107  $urlParts = parse_url($parts[1]);
108  $uploadName = pathinfo($urlParts['path'], PATHINFO_FILENAME);
109  //print "fromURL: returning $uploadName\n";
110  return($uploadName);
111  }
112 
113  function getAnchors($node, $uploadName)
114  {
115  $anchorList = $node->getElementsByTagName('a');
116  foreach($anchorList as $anchorEle)
117  {
118  $anchorText = $anchorEle->textContent;
119  //print "the anchor text is:$anchorText\n";
120  //print "inserting anchor text is:$anchorText\n";
121  $this->anchors[$uploadName][$anchorText] = $anchorEle->getAttribute('href');
122  }
123  }
124 
125  function parseBrowseMenuFiles()
126  {
127 
128  $dom = new domDocument;
129  @$dom->loadHTML($this->page);
130  /*** discard white space ***/
131  $dom->preserveWhiteSpace = false;
132  $table = $dom->getElementById($this->tableId);
133  if(empty($table)) {
134  $this->emptyTable = TRUE;
135  print "DPLTDB: table is empty, can't find table! with table id of:$this->tableId\n";
136  return($this->browseList=array());
137  }
138 
139  foreach ($table->childNodes as $tblChildNode)
140  {
141  if($tblChildNode->nodeName == 'tr')
142  {
143  $childNodes = $tblChildNode->childNodes;
144  $clen = $childNodes->length;
145  for($i=0; $i<$clen; $i++)
146  {
147  $node = $childNodes-> item($i);
148  $nn = $node->nodeName;
149  if($node->nodeName == 'td')
150  {
151  $fileName = $node->nodeValue;
152  $childNodes = $node->childNodes;
153  $tdclen = $childNodes->length;
154  for($i=0; $i<$tdclen; $i++)
155  {
156  $tdnode = $childNodes->item($i);
157  $tdnn = $tdnode->nodeName;
158  //print "\tname of td-node is:$tdnn\n";
159  if($tdnn == '#text')
160  {
161  $tdNodeValue = $tdnode->nodeValue;
162  //print "\tDB-td: #text node value is:$tdNodeValue\n";
163 
164  $fromFS = 'Added from filesystem:';
165  $fromFile = 'Added by file upload:';
166  $fromURL = 'Added by URL:';
167 
168  $matches = 0;
169 
170  if($matches = preg_match("/$fromFile/",$tdNodeValue,$ffMatch))
171  {
172  $fileUploadName = $this->fromFile($tdNodeValue);
173  $this->getAnchors($node, $fileUploadName);
174  }
175  else if($matches = preg_match("/$fromFS/",$tdNodeValue,$fsMatch))
176  {
177  $fsUploadName = $this->fromFS($tdNodeValue);
178  $this->getAnchors($node, $fsUploadName);
179  }
180  else if($matches = preg_match("/$fromURL/",$tdNodeValue,$urlMatch))
181  {
182  $urlUploadName = $this->fromURL($tdNodeValue);
183  $this->getAnchors($node, $urlUploadName);
184  }
185  }
186  }
187  }
188  }
189  }
190  if(!empty($this->anchors))
191  {
192  $this->browseList = array_merge($this->browseList, $this->anchors);
193  $this->anchors = array();
194  }
195  } // foreach
196 
197  if(empty($this->browseList)) {
198  $this->noRows = TRUE;
199  }
200  //print "at the end of parse, browseList is:\n";print_r($this->browseList) . "\n";
201  } //parseBrowseMenu
202 
215  {
216  $matches = preg_match_all("/.*?\[<a href='(.*?)'.*?>([V|I|Down].*?)</", $this->page, $fileMini, PREG_PATTERN_ORDER);
217  print "fileMini Menus are:";
218  print_r($fileMini) . "\n";
219  return ($this->_createMiniArray($fileMini, $matches));
220  }
230  {
231  $matches = preg_match_all("/.+class='mono'.*?<a href='(.*)'>(.*?)<\/a>/", $this->page, $dirs, PREG_PATTERN_ORDER);
232  //print "dirs is:";
233  //print_r($dirs) . "\n";
234  return ($this->_createRtnArray($dirs, $matches));
235  }
236 
237  function _createRtnArray($array, $matches)
238  {
239  /*
240  * if we have a match, the create return array, else return empty
241  * array.
242  */
243  if ($matches > 0)
244  {
245  $numMenus = count($array[1]);
246  $rtnList = array ();
247  for ($i = 0; $i <= $numMenus -1; $i++)
248  {
249  $rtnList[$array[2][$i]] = $array[1][$i];
250  }
251  return ($rtnList);
252  } else
253  {
254  return (array ());
255  }
256  }
257 
269  function _createMiniArray($array, $matches)
270  {
271  /*
272  * if we have a match, then create return array, else return empty
273  * array. file mini menus have duplicated keys (view,meta,download)
274  * so they must be processed a different way.
275  */
276  //print "_CMiniA: matches is:$matches\n";
277  if ($matches > 0)
278  {
279  $triple = array ();
280  $numMenus = count($array[1]);
281  $loopCnt = $numMenus / 3;
282  $rtnList = array ();
283  /* index is used to step through all the links*/
284  $index = 0;
285  for ($i = 0; $i <= $loopCnt -1; $i++)
286  {
287  $triple = array ();
288  for ($j = 0; $j <= 2; $j++)
289  {
290  $triple[$array[2][$j]] = $array[1][$index];
291  $index++;
292  }
293  $rtnList[$i] = $triple;
294  }
295  return ($rtnList);
296  } else
297  {
298  return (array ());
299  }
300 
301  }
302 }
_createMiniArray($array, $matches)
fromFile($nodeString)