1<?php
2
3
4/***********************************************************
5 Copyright (C) 2008 Hewlett-Packard Development Company, L.P.
6
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License
9 version 2 as published by the Free Software Foundation.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19 ***********************************************************/
20
21/**
22 * Parse the part of the page that has the folder path and mini menu.
23 *
24 * @param string $page the xhtml page to parse
25 *
26 * @return array of assocative arrays. Each associative array uses the
27 * folder or leaf name for the key and the value is a link (if there
28 * is one.)
29 *
30 * Can return an empty array indicating nothing on the page to browse.
31 *
32 * @version "$Id: parseFolderPath.php 2866 2010-03-10 19:32:44Z rrando $" Created on Aug 21, 2008
33 */
34
35class parseFolderPath
36{
37  public $page;
38  public $host;
39  public $filesWithLicense;
40  private $test;
41
42  function __construct($page, $url)
43  {
44    /* to do: check for http?  if not return null...)? */
45    if (empty ($page))
46    {
47      return;
48    }
49    $this->page = $page;
50    if (empty ($url))
51    {
52      return;
53    }
54    $this->host = getHost($url);
55  }
56
57  /**
58   * function countFiles()
59   *
60   * Parse  the part of the page that has the folder path and mini menu,
61   * return the count of 'Folder' items found.
62   *
63   * @param string $page the xhtml page to parse
64   *
65   * @return integer $count the count of items found, can be 0.
66   *
67   */
68   function countFiles()
69   {
70    /* Extract the folder path line from the page */
71    $regExp = "Folder<\/b>:.*?/font>";
72    $numberMatched = preg_match_all("|$regExp|s", $this->page, $pathLines, PREG_SET_ORDER);
73    $this->filesWithLicense = $pathLines;
74    //print "PFP:countFiles:matched is:$numberMatched\nFilesWithLicense:\n";
75    //print_r($this->filesWithLicense) . "\n";
76
77    return($numberMatched);
78   }
79
80  /**
81   * function parseFolderPath
82   *
83   * Parse the part of the page that has the folder path and mini-menu,
84   * this method only parses the folder path, see parseMiniMenu.
85   *
86   * @returns array of assocative arrays. Each assocative array
87   * is ordered by folder names with the last key being the
88   * leafname, which can be and empty directory.  Usually no link is
89   * associated with the leaf node, so it's typically NULL.
90   *
91   * An empty array is returned if no license paths on that page.
92   */
93  function parseFolderPath() {
94
95    $paths = array();
96
97    /* Gather up the line(s) with Folder*/
98    $this->countFiles();
99    foreach ($this->filesWithLicense as $aptr)
100    {
101      foreach ($aptr as $path)
102      {
103        $paths[] = $path;
104      }
105    }
106    foreach ($paths as $apath)
107    {
108      $regExp = ".*?href='(.*?)'>(.*?)<\/a>(.*?)<";
109      $matches = preg_match_all("|$regExp|i", $apath, $pathList, PREG_SET_ORDER);
110      if ($matches > 0)
111      {
112        $dirList[] = $this->_createRtnArray($pathList, $matches);
113        return ($dirList);
114      } else
115      {
116        return (array ());
117      }
118
119    }
120  } // parseFolderPath
121
122  /**
123   * clean up the links to be usable
124   *
125   * @param array $list, the list to clean up
126   * @param int $matches, the size of the list
127   *
128   * @return array, the cleaned up list_bucket_files
129   *
130   * @todo fix the docs above to much more detailed.
131   */
132  function _createRtnArray($list, $matches)
133  {
134    global $host;
135
136    /*
137     * The last entry in the array is always a leaf name with no link
138     * but it has to be cleaned up a bit....
139     */
140
141    for ($i = 0; $i < $matches; $i++)
142    {
143      $cleanKey = trim($list[$i][2], "\/<>b");
144      if (empty ($cleanKey))
145      {
146        continue;
147      }
148      // Make a real link that can be used
149      $partLink = $list[$i][1];
150      $link = makeUrl($this->host, $partLink);
151      $rtnList[$cleanKey] = $link;
152      /* check for anything in the leaf entry, if there is, remove
153       * the preceeding /
154       */
155      if (!empty ($list[$i][3]))
156      {
157        $cleanKey = trim($list[$i][3], "\/ ");
158        if (empty ($cleanKey))
159        {
160          continue;
161        }
162        $rtnList[$cleanKey] = NULL;
163      }
164    }
165    return ($rtnList);
166  } // _createRtnArray
167
168  public function setPage($page)
169  {
170    if (!empty ($page))
171    {
172      $this->page = $page;
173    }
174  }
175}
176?>
177