FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
parseBrowseMenu.php
1 <?php
2 /***********************************************************
3  Copyright (C) 2008 Hewlett-Packard Development Company, L.P.
4 
5  This program is free software; you can redistribute it and/or
6  modify it under the terms of the GNU General Public License
7  version 2 as published by the Free Software Foundation.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License along
15  with this program; if not, write to the Free Software Foundation, Inc.,
16  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  ***********************************************************/
18 
35 {
36  public $page;
37  public $browseList = array();
38  public $noRows = FALSE;
39  public $emptyTable = FALSE;
40  private $tableId;
41  private $title;
42  protected $anchors = array();
43 
44  private $test;
45 
46  function __construct($page,$tblId,$title=1)
47  {
48  if (empty ($page)) { return; }
49  $this->page = $page;
50  if (strlen($tblId) == 0) { return; }
51  $this->tableId = $tblId;
52  $this->title = $title;
53  }
54 
88  function fromFile($nodeString)
89  {
90  $parts = explode(': ', $nodeString);
91  //print "parts are:\n";print_r($parts) . "\n";
92  if(!empty($parts[1]))
93  {
94  $uploadName = $parts[1];
95  //print "fromFile: returning $uploadName\n";
96  return($uploadName);
97  }
98  return(NULL);
99  }
100 
101  function fromFS($nodeString)
102  {
103  $parts = explode(': ', $nodeString);
104  //print "parts are:\n";print_r($parts) . "\n";
105  $uploadName = pathinfo($parts[1], PATHINFO_FILENAME);
106  if(strlen($uploadName) != 0)
107  {
108  //print "fromFS: returning $uploadName\n";
109  return($uploadName);
110  }
111  return(NULL);
112  }
113 
114  function fromURL($nodeString)
115  {
116  $parts = explode(': ', $nodeString);
117  //print "parts are:\n";print_r($parts) . "\n";
118  $urlParts = parse_url($parts[1]);
119  $uploadName = pathinfo($urlParts['path'], PATHINFO_FILENAME);
120  //print "fromURL: returning $uploadName\n";
121  return($uploadName);
122  }
123 
124  function getAnchors($node, $uploadName)
125  {
126  $anchorList = $node->getElementsByTagName('a');
127  foreach($anchorList as $anchorEle)
128  {
129  $anchorText = $anchorEle->textContent;
130  //print "the anchor text is:$anchorText\n";
131  //print "inserting anchor text is:$anchorText\n";
132  $this->anchors[$uploadName][$anchorText] = $anchorEle->getAttribute('href');
133  }
134  }
135 
136  function parseBrowseMenuFiles()
137  {
138 
139  $dom = new domDocument;
140  @$dom->loadHTML($this->page);
141  /*** discard white space ***/
142  $dom->preserveWhiteSpace = false;
143  $table = $dom->getElementById($this->tableId);
144  if(empty($table)) {
145  $this->emptyTable = TRUE;
146  print "DPLTDB: table is empty, can't find table! with table id of:$this->tableId\n";
147  return($this->browseList=array());
148  }
149 
150  foreach ($table->childNodes as $tblChildNode)
151  {
152  if($tblChildNode->nodeName == 'tr')
153  {
154  $childNodes = $tblChildNode->childNodes;
155  $clen = $childNodes->length;
156  for($i=0; $i<$clen; $i++)
157  {
158  $node = $childNodes-> item($i);
159  $nn = $node->nodeName;
160  if($node->nodeName == 'td')
161  {
162  $fileName = $node->nodeValue;
163  $childNodes = $node->childNodes;
164  $tdclen = $childNodes->length;
165  for($i=0; $i<$tdclen; $i++)
166  {
167  $tdnode = $childNodes->item($i);
168  $tdnn = $tdnode->nodeName;
169  //print "\tname of td-node is:$tdnn\n";
170  if($tdnn == '#text')
171  {
172  $tdNodeValue = $tdnode->nodeValue;
173  //print "\tDB-td: #text node value is:$tdNodeValue\n";
174 
175  $fromFS = 'Added from filesystem:';
176  $fromFile = 'Added by file upload:';
177  $fromURL = 'Added by URL:';
178 
179  $matches = 0;
180 
181  if($matches = preg_match("/$fromFile/",$tdNodeValue,$ffMatch))
182  {
183  $fileUploadName = $this->fromFile($tdNodeValue);
184  $this->getAnchors($node, $fileUploadName);
185  }
186  else if($matches = preg_match("/$fromFS/",$tdNodeValue,$fsMatch))
187  {
188  $fsUploadName = $this->fromFS($tdNodeValue);
189  $this->getAnchors($node, $fsUploadName);
190  }
191  else if($matches = preg_match("/$fromURL/",$tdNodeValue,$urlMatch))
192  {
193  $urlUploadName = $this->fromURL($tdNodeValue);
194  $this->getAnchors($node, $urlUploadName);
195  }
196  }
197  }
198  }
199  }
200  }
201  if(!empty($this->anchors))
202  {
203  $this->browseList = array_merge($this->browseList, $this->anchors);
204  $this->anchors = array();
205  }
206  } // foreach
207 
208  if(empty($this->browseList)) {
209  $this->noRows = TRUE;
210  }
211  //print "at the end of parse, browseList is:\n";print_r($this->browseList) . "\n";
212  } //parseBrowseMenu
213 
226  {
227  $matches = preg_match_all("/.*?\[<a href='(.*?)'.*?>([V|I|Down].*?)</", $this->page, $fileMini, PREG_PATTERN_ORDER);
228  print "fileMini Menus are:";
229  print_r($fileMini) . "\n";
230  return ($this->_createMiniArray($fileMini, $matches));
231  }
241  {
242  $matches = preg_match_all("/.+class='mono'.*?<a href='(.*)'>(.*?)<\/a>/", $this->page, $dirs, PREG_PATTERN_ORDER);
243  //print "dirs is:";
244  //print_r($dirs) . "\n";
245  return ($this->_createRtnArray($dirs, $matches));
246  }
247 
248  function _createRtnArray($array, $matches)
249  {
250  /*
251  * if we have a match, the create return array, else return empty
252  * array.
253  */
254  if ($matches > 0)
255  {
256  $numMenus = count($array[1]);
257  $rtnList = array ();
258  for ($i = 0; $i <= $numMenus -1; $i++)
259  {
260  $rtnList[$array[2][$i]] = $array[1][$i];
261  }
262  return ($rtnList);
263  } else
264  {
265  return (array ());
266  }
267  }
268 
280  function _createMiniArray($array, $matches)
281  {
282  /*
283  * if we have a match, then create return array, else return empty
284  * array. file mini menus have duplicated keys (view,meta,download)
285  * so they must be processed a different way.
286  */
287  //print "_CMiniA: matches is:$matches\n";
288  if ($matches > 0)
289  {
290  $triple = array ();
291  $numMenus = count($array[1]);
292  $loopCnt = $numMenus / 3;
293  $rtnList = array ();
294  /* index is used to step through all the links*/
295  $index = 0;
296  for ($i = 0; $i <= $loopCnt -1; $i++)
297  {
298  $triple = array ();
299  for ($j = 0; $j <= 2; $j++)
300  {
301  $triple[$array[2][$j]] = $array[1][$index];
302  $index++;
303  }
304  $rtnList[$i] = $triple;
305  }
306  return ($rtnList);
307  } else
308  {
309  return (array ());
310  }
311 
312  }
313 }
314 ?>
_createMiniArray($array, $matches)
fromFile($nodeString)