Also apply the __toString fix to UserInfo
[cacert-mgr.git] / external / ZendFramework-1.9.5 / demos / Zend / Gdata / InstallationChecker.php
1 <?php
2 /**
3 * Zend Framework
4 *
5 * LICENSE
6 *
7 * This source file is subject to the new BSD license that is bundled
8 * with this package in the file LICENSE.txt.
9 * It is also available through the world-wide-web at this URL:
10 * http://framework.zend.com/license/new-bsd
11 * If you did not receive a copy of the license and are unable to
12 * obtain it through the world-wide-web, please send an email
13 * to license@zend.com so we can send you a copy immediately.
14 *
15 * @category Zend
16 * @package Zend_Gdata
17 * @subpackage Demos
18 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
19 * @license http://framework.zend.com/license/new-bsd New BSD License
20 */
21
22 /**
23 * Simple class to verify that the server that this is run on has a correct
24 * installation of the Zend Framework Gdata component.
25 */
26 class InstallationChecker {
27
28 const CSS_WARNING = '.warning { color: #fff; background-color: #AF0007;}';
29 const CSS_SUCCESS = '.success { color: #000; background-color: #69FF4F;}';
30 const CSS_ERROR = '.error { color: #fff; background-color: #FF9FA3;}';
31 const PHP_EXTENSION_ERRORS = 'PHP Extension Errors';
32 const PHP_MANUAL_LINK_FRAGMENT = 'http://us.php.net/manual/en/book.';
33 const PHP_REQUIREMENT_CHECKER_ID = 'PHP Requirement checker v0.1';
34 const SSL_CAPABILITIES_ERRORS = 'SSL Capabilities Errors';
35 const YOUTUBE_API_CONNECTIVITY_ERRORS = 'YouTube API Connectivity Errors';
36 const ZEND_GDATA_INSTALL_ERRORS = 'Zend Framework Installation Errors';
37 const ZEND_SUBVERSION_URI = 'http://framework.zend.com/download/subversion';
38
39 private static $REQUIRED_EXTENSIONS = array(
40 'ctype', 'dom', 'libxml', 'spl', 'standard', 'openssl');
41
42 private $_allErrors = array(
43 self::PHP_EXTENSION_ERRORS => array(
44 'tested' => false, 'errors' => null),
45 self::ZEND_GDATA_INSTALL_ERRORS => array(
46 'tested' => false, 'errors' => null),
47 self::SSL_CAPABILITIES_ERRORS => array(
48 'tested' => false, 'errors' => null),
49 self::YOUTUBE_API_CONNECTIVITY_ERRORS => array(
50 'tested' => false, 'errors' => null)
51 );
52
53 private $_sapiModeCLI = null;
54
55 /**
56 * Create a new InstallationChecker object and run verifications.
57 * @return void
58 */
59 public function __construct()
60 {
61 $this->determineIfInCLIMode();
62 $this->runAllVerifications();
63 $this->outputResults();
64 }
65
66 /**
67 * Set the sapiModeCLI variable to true if we are running CLI mode.
68 *
69 * @return void
70 */
71 private function determineIfInCLIMode()
72 {
73 if (php_sapi_name() == 'cli') {
74 $this->_sapiModeCLI = true;
75 }
76 }
77
78 /**
79 * Getter for sapiModeCLI variable.
80 *
81 * @return boolean True if we are running in CLI mode.
82 */
83 public function runningInCLIMode()
84 {
85 if ($this->_sapiModeCLI) {
86 return true;
87 } else {
88 return false;
89 }
90 }
91
92 /**
93 * Run verifications, stopping at each step if there is a failure.
94 *
95 * @return void
96 */
97 public function runAllVerifications()
98 {
99 if (!$this->validatePHPExtensions()) {
100 return;
101 }
102 if (!$this->validateZendFrameworkInstallation()) {
103 return;
104 }
105 if (!$this->testSSLCapabilities()) {
106 return;
107 }
108 if (!$this->validateYouTubeAPIConnectivity()) {
109 return;
110 }
111 }
112
113 /**
114 * Validate that the required PHP Extensions are installed and available.
115 *
116 * @return boolean False if there were errors.
117 */
118 private function validatePHPExtensions()
119 {
120 $phpExtensionErrors = array();
121 foreach (self::$REQUIRED_EXTENSIONS as $requiredExtension) {
122 if (!extension_loaded($requiredExtension)) {
123 $requiredExtensionError = $requiredExtension .
124 ' extension missing';
125 $documentationLink = null;
126 if ($requiredExtension != 'standard') {
127 $documentationLink = self::PHP_MANUAL_LINK_FRAGMENT .
128 $requiredExtension . '.php';
129 $documentationLink =
130 $this->checkAndAddHTMLLink($documentationLink);
131 } else {
132 $documentationLink = self::PHP_MANUAL_LINK_FRAGMENT .
133 'spl.php';
134 $documentationLink =
135 $this->checkAndAddHTMLLink($documentationLink);
136 }
137
138 if ($documentationLink) {
139 $phpExtensionErrors[] = $requiredExtensionError .
140 ' - refer to ' . $documentationLink;
141 }
142 }
143 }
144 $this->_allErrors[self::PHP_EXTENSION_ERRORS]['tested'] = true;
145 if (count($phpExtensionErrors) > 0) {
146 $this->_allErrors[self::PHP_EXTENSION_ERRORS]['errors'] =
147 $phpExtensionErrors;
148 return false;
149 }
150 return true;
151 }
152
153 /**
154 * Validate that the Gdata component of Zend Framework is installed
155 * properly. Also checks that the required YouTube API helper methods are
156 * found.
157 *
158 * @return boolean False if there were errors.
159 */
160 private function validateZendFrameworkInstallation()
161 {
162 $zendFrameworkInstallationErrors = array();
163 $zendLoaderPresent = false;
164 try {
165 $zendLoaderPresent = @fopen('Zend/Loader.php', 'r', true);
166 } catch (Exception $e) {
167 $zendFrameworkInstallationErrors[] = 'Exception thrown trying to ' .
168 'access Zend/Loader.php using \'use_include_path\' = true ' .
169 'Make sure you include the Zend Framework in your ' .
170 'include_path which currently contains: "' .
171 ini_get('include_path') . '"';
172 }
173
174 if ($zendLoaderPresent) {
175 @fclose($zendLoaderPresent);
176 require_once('Zend/Loader.php');
177 require_once('Zend/Version.php');
178 Zend_Loader::loadClass('Zend_Gdata_YouTube');
179 Zend_Loader::loadClass('Zend_Gdata_YouTube_VideoEntry');
180 $yt = new Zend_Gdata_YouTube();
181 $videoEntry = $yt->newVideoEntry();
182 if (!method_exists($videoEntry, 'setVideoTitle')) {
183 $zendFrameworkMessage = 'Your version of the ' .
184 'Zend Framework ' . Zend_Version::VERSION . ' is too old' .
185 ' to run the YouTube demo application and does not' .
186 ' contain the new helper methods. Please check out a' .
187 ' newer version from Zend\'s repository: ' .
188 checkAndAddHTMLLink(self::ZEND_SUBVERSION_URI);
189 $zendFrameworkInstallationErrors[] = $zendFrameworkMessage;
190 }
191 } else {
192 if (count($zendFrameworkInstallationErrors) < 1) {
193 $zendFrameworkInstallationErrors[] = 'Exception thrown trying' .
194 ' to access Zend/Loader.php using \'use_include_path\' =' .
195 ' true. Make sure you include Zend Framework in your' .
196 ' include_path which currently contains: ' .
197 ini_get('include_path');
198 }
199 }
200
201 $this->_allErrors[self::ZEND_GDATA_INSTALL_ERRORS]['tested'] = true;
202
203 if (count($zendFrameworkInstallationErrors) > 0) {
204 $this->_allErrors[self::ZEND_GDATA_INSTALL_ERRORS]['errors'] =
205 $zendFrameworkInstallationErrors;
206 return false;
207 }
208 return true;
209 }
210
211 /**
212 * Create HTML link from an input string if not in CLI mode.
213 *
214 * @param string The error message to be converted to a link.
215 * @return string Either the original error message or an HTML version.
216 */
217 private function checkAndAddHTMLLink($inputString) {
218 if (!$this->runningInCLIMode()) {
219 return $this->makeHTMLLink($inputString);
220 } else {
221 return $inputString;
222 }
223 }
224
225 /**
226 * Create an HTML link from a string.
227 *
228 * @param string The string to be made into link text and anchor target.
229 * @return string HTML link.
230 */
231 private function makeHTMLLink($inputString)
232 {
233 return '<a href="'. $inputString . '" target="_blank">' .
234 $inputString . '</a>';
235 }
236
237 /**
238 * Validate that SSL Capabilities are available.
239 *
240 * @return boolean False if there were errors.
241 */
242 private function testSSLCapabilities()
243 {
244 $sslCapabilitiesErrors = array();
245 require_once 'Zend/Loader.php';
246 Zend_Loader::loadClass('Zend_Http_Client');
247
248 $httpClient = new Zend_Http_Client(
249 'https://www.google.com/accounts/AuthSubRequest');
250 $response = $httpClient->request();
251 $this->_allErrors[self::SSL_CAPABILITIES_ERRORS]['tested'] = true;
252
253 if ($response->isError()) {
254 $sslCapabilitiesErrors[] = 'Response from trying to access' .
255 ' \'https://www.google.com/accounts/AuthSubRequest\' ' .
256 $response->getStatus() . ' - ' . $response->getMessage();
257 }
258
259 if (count($sslCapabilitiesErrors) > 0) {
260 $this->_allErrors[self::SSL_CAPABILITIES_ERRORS]['errors'] =
261 $sslCapabilitiesErrors;
262 return false;
263 }
264 return true;
265 }
266
267 /**
268 * Validate that we can connect to the YouTube API.
269 *
270 * @return boolean False if there were errors.
271 */
272 private function validateYouTubeAPIConnectivity()
273 {
274 $connectivityErrors = array();
275 require_once 'Zend/Loader.php';
276 Zend_Loader::loadClass('Zend_Gdata_YouTube');
277 $yt = new Zend_Gdata_YouTube();
278 $topRatedFeed = $yt->getTopRatedVideoFeed();
279 if ($topRatedFeed instanceof Zend_Gdata_YouTube_VideoFeed) {
280 if ($topRatedFeed->getTotalResults()->getText() < 1) {
281 $connectivityErrors[] = 'There was less than 1 video entry' .
282 ' in the \'Top Rated Video Feed\'';
283 }
284 } else {
285 $connectivityErrors[] = 'The call to \'getTopRatedVideoFeed()\' ' .
286 'did not result in a Zend_Gdata_YouTube_VideoFeed object';
287 }
288
289 $this->_allErrors[self::YOUTUBE_API_CONNECTIVITY_ERRORS]['tested'] =
290 true;
291 if (count($connectivityErrors) > 0) {
292 $this->_allErrors[self::YOUTUBE_API_CONNECTIVITY_ERRORS]['tested'] =
293 $connectivityErrors;
294 return false;
295 }
296 return true;
297 }
298
299 /**
300 * Dispatch a call to outputResultsInHTML or outputResultsInText pending
301 * the current SAPI mode.
302 *
303 * @return void
304 */
305 public function outputResults()
306 {
307 if ($this->_sapiModeCLI) {
308 print $this->getResultsInText();
309 } else {
310 print $this->getResultsInHTML();
311 }
312 }
313
314
315 /**
316 * Return a string representing the results of the verifications.
317 *
318 * @return string A string representing the results.
319 */
320 private function getResultsInText()
321 {
322 $output = "== Ran PHP Installation Checker using CLI ==\n";
323
324 $error_count = 0;
325 foreach($this->_allErrors as $key => $value) {
326 $output .= $key . ' -- ';
327 if (($value['tested'] == true) && (count($value['errors']) == 0)) {
328 $output .= "No errors found\n";
329 } elseif ($value['tested'] == true) {
330 $output .= "Tested\n";
331 $error_count = 0;
332 foreach ($value['errors'] as $error) {
333 $output .= "Error number: " . $error_count . "\n--" .
334 $error . "\n";
335 }
336 } else {
337 $output .= "Not tested\n";
338 }
339 $error_count++;
340 }
341 return $output;
342 }
343
344 /**
345 * Return an HTML table representing the results of the verifications.
346 *
347 * @return string An HTML string representing the results.
348 */
349 private function getResultsInHTML()
350 {
351 $html = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" " .
352 "\"http://www.w3.org/TR/html4/strict.dtd\">\n".
353 "<html><head>\n<title>PHP Installation Checker</title>\n" .
354 "<style type=\"text/css\">\n" .
355 self::CSS_WARNING . "\n" .
356 self::CSS_SUCCESS . "\n" .
357 self::CSS_ERROR . "\n" .
358 "</style></head>\n" .
359 "<body>\n<table class=\"verification_table\">" .
360 "<caption>Ran PHP Installation Checker on " .
361 gmdate('c') . "</caption>\n";
362
363 $error_count = 0;
364 foreach($this->_allErrors as $key => $value) {
365 $html .= "<tr><td class=\"verification_type\">" . $key . "</td>";
366 if (($value['tested'] == true) && (count($value['errors']) == 0)) {
367 $html .= "<td class=\"success\">Tested</td></tr>\n" .
368 "<tr><td colspan=\"2\">No errors found</td></tr>\n";
369 } elseif ($value['tested'] == true) {
370 $html .= "<td class=\"warning\">Tested</td></tr>\n";
371 $error_count = 0;
372 foreach ($value['errors'] as $error) {
373 $html .= "<tr><td class=\"error\">" . $error_count . "</td>" .
374 "<td class=\"error\">" . $error . "</td></tr>\n";
375 }
376 } else {
377 $html .= "<td class=\"warning\">Not tested</td></tr>\n";
378 }
379 $error_count++;
380 }
381 $html .= "</body></html>";
382 return $html;
383 }
384 }
385
386 $installationChecker = new InstallationChecker();