1: <?php
2: /**
3: * REST access to a PEAR server.
4: *
5: * PHP version 5
6: *
7: * @category Horde
8: * @package Pear
9: * @author Gunnar Wrobel <wrobel@pardus.de>
10: * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
11: * @link http://pear.horde.org/index.php?package=Pear
12: * @link http://pear.php.net/manual/en/core.rest.php
13: */
14:
15: /**
16: * REST access to a PEAR server.
17: *
18: * This implements a subset of the REST methods detailed in
19: * http://pear.php.net/manual/en/core.rest.php
20: *
21: * Copyright 2011-2012 Horde LLC (http://www.horde.org/)
22: *
23: * See the enclosed file COPYING for license information (LGPL). If you
24: * did not receive this file, see http://www.horde.org/licenses/lgpl21.
25: *
26: * @category Horde
27: * @package Pear
28: * @author Gunnar Wrobel <wrobel@pardus.de>
29: * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
30: * @link http://pear.horde.org/index.php?package=Pear
31: * @link http://pear.php.net/manual/en/core.rest.php
32: */
33: class Horde_Pear_Rest
34: {
35: /**
36: * The HTTP client.
37: *
38: * @var Horde_Http_Client
39: */
40: private $_client;
41:
42: /**
43: * The base URL for the remote PEAR server
44: *
45: * @var string
46: */
47: private $_url;
48:
49: /**
50: * The package states already fetched.
51: *
52: * @var array
53: */
54: private $_package_states;
55:
56: /**
57: * Constructor.
58: *
59: * @param Horde_Http_Client $client The HTTP client.
60: * @param string $url The URL for the remote PEAR server.
61: */
62: public function __construct($client, $url)
63: {
64: $this->_client = $client;
65: $this->_url = 'http://' . $url;
66: }
67:
68: /**
69: * Set the server name.
70: *
71: * @params string $server The server name.
72: *
73: * @return NULL
74: */
75: public function setServer($server)
76: {
77: $this->_url = 'http://' . $server;
78: }
79:
80: /**
81: * Return the complete list of packages on the server.
82: *
83: * @return resource A stream with the package list.
84: */
85: public function fetchPackageList()
86: {
87: return $this->_get($this->_url . '/rest/p/packages.xml');
88: }
89:
90: /**
91: * Return the information on a specific package from the server.
92: *
93: * @param string $package The name of the package to retrieve information
94: * for.
95: *
96: * @return resource A stream with the package information.
97: */
98: public function fetchPackageInformation($package)
99: {
100: return $this->_get(
101: $this->_url . '/rest/p/' . strtolower($package) . '/info.xml'
102: );
103: }
104:
105: /**
106: * Return the release list for a specific package from the server.
107: *
108: * @param string $package The name of the package to retrieve the releases
109: * for.
110: *
111: * @return resource A stream with the package release information.
112: */
113: public function fetchPackageReleases($package)
114: {
115: return $this->_get(
116: $this->_url . '/rest/r/' . strtolower($package) . '/allreleases.xml'
117: );
118: }
119:
120: /**
121: * Return the latest releases for a specific package.
122: *
123: * @param string $package The name of the package to retrieve the latest
124: * releases for.
125: *
126: * @return array A list of latest releases per level of stability.
127: */
128: public function fetchLatestPackageReleases($package)
129: {
130: if (!isset($this->_package_states[$package])) {
131: $base = $this->_url . '/rest/r/' . strtolower($package);
132: $this->_package_states[$package] = array(
133: 'stable' => $this->_read($base . '/stable.txt'),
134: 'alpha' => $this->_read($base . '/alpha.txt'),
135: 'beta' => $this->_read($base . '/beta.txt'),
136: 'devel' => $this->_read($base . '/devel.txt'),
137: );
138: }
139: return $this->_package_states[$package];
140: }
141:
142: /**
143: * Return the latest release version for a specific package.
144: *
145: * @param string $package The name of the package to retrieve the latest
146: * release for.
147: *
148: * @return string The version of the latest release.
149: */
150: public function fetchLatestRelease($package)
151: {
152: return $this->_read($this->_url . '/rest/r/' . strtolower($package) . '/latest.txt');
153: }
154:
155: /**
156: * Return the release information for a specific package version from the
157: * server.
158: *
159: * @param string $package The name of the package.
160: * @param string $version The version of the release.
161: *
162: * @return resource A stream with the package release information.
163: */
164: public function fetchReleaseInformation($package, $version)
165: {
166: return $this->_get(
167: $this->_url . '/rest/r/' . strtolower($package) . '/' . $version . '.xml'
168: );
169: }
170:
171: /**
172: * Test if the specified release exists.
173: *
174: * @param string $package The name of the package.
175: * @param string $version The version of the release.
176: *
177: * @return boolean True if the release exists.
178: */
179: public function releaseExists($package, $version)
180: {
181: $response = $this->_client->get(
182: $this->_url . '/rest/r/' . strtolower($package) . '/' . $version . '.xml'
183: );
184: if ($response->code === 200) {
185: return true;
186: } else {
187: return false;
188: }
189: }
190:
191: /**
192: * Return the package.xml for a specific release from the server.
193: *
194: * @param string $package The name of the package.
195: * @param string $version The version of the release.
196: *
197: * @return resource A stream with the package.xml information.
198: */
199: public function fetchReleasePackageXml($package, $version)
200: {
201: return $this->_get(
202: $this->_url . '/rest/r/' . strtolower($package) . '/package.' . $version . '.xml'
203: );
204: }
205:
206: /**
207: * Return the serialized package dependencies for a specific release from
208: * the server.
209: *
210: * @param string $package The name of the package.
211: * @param string $version The version of the release.
212: *
213: * @return string The serialized dependencies.
214: */
215: public function fetchPackageDependencies($package, $version)
216: {
217: return $this->_read(
218: $this->_url . '/rest/r/' . strtolower($package) . '/deps.' . $version . '.txt'
219: );
220: }
221:
222: /**
223: * Return the channel.xml from the server.
224: *
225: * @return string The content of the channel.xml file.
226: */
227: public function fetchChannelXml()
228: {
229: return $this->_read($this->_url . '/channel.xml');
230: }
231:
232: /**
233: * Fetch the provided URL as stream.
234: *
235: * @param string $url The URL.
236: *
237: * @return resource The response as stream.
238: */
239: private function _get($url)
240: {
241: return $this->_client->get($url)->getStream();
242: }
243:
244: /**
245: * Fetch the provided URL as string.
246: *
247: * @param string $url The URL.
248: *
249: * @return string The response as string.
250: */
251: private function _read($url)
252: {
253: $response = $this->_client->get($url);
254: if ($response->code === 200) {
255: return $response->getBody();
256: } else {
257: return false;
258: }
259: }
260: }