index that shows options and setup check
authorChristian Weiske <cweiske@cweiske.de>
Wed, 9 Apr 2014 18:03:15 +0000 (20:03 +0200)
committerChristian Weiske <cweiske@cweiske.de>
Wed, 9 Apr 2014 18:03:15 +0000 (20:03 +0200)
README.rst
src/phancap/Adapter/Cutycapt.php
src/phancap/Options.php
www/index.php
www/setup.php [new file with mode: 0644]

index 78a67ec7be8766a891701539a2e090daf94e1102..17faa8e4e58011b1ae5010ef8b5d5de4cd39c566 100644 (file)
@@ -157,7 +157,7 @@ Dependencies
 - `cutycapt <http://cutycapt.sourceforge.net/>`_
 - imagemagick's ``convert``
 - ``xvfb-run``
-
+- PEAR's ``System.php``
 
 
 =======================
index 863c73212ca39dba78812533fdfc93a8b59b218d..461fdaa85f7466c35d6d04ec8d14ec3381503455 100644 (file)
@@ -6,9 +6,29 @@ class Adapter_Cutycapt
     protected $lockHdl;
     protected $lockFile = null;
 
+    /**
+     * @return mixed TRUE if all is fine, array with error messages otherwise
+     */
     public function isAvailable()
     {
-        //FIXME: setup check for xvfbrun, cutycapt, convert
+        $old = error_reporting(error_reporting() & ~E_STRICT);
+        $arErrors = array();
+        if (\System::which('xvfb-run') === false) {
+            $arErrors[] = '"xvfb-run" is not installed';
+        }
+        if (\System::which('cutycapt') === false) {
+            $arErrors[] = '"cutycapt" is not installed';
+        }
+        if (\System::which('convert') === false) {
+            $arErrors[] = '"convert" (imagemagick) is not installed';
+        }
+
+        error_reporting($old);
+        if (count($arErrors)) {
+            return $arErrors;
+        }
+
+        return true;
     }
 
     public function render(Image $img, Options $options)
index b51f2a788569c8c7abc51680891f822244f27261..6aaa49f6131424187fd5ebf7d9119858c4691aa2 100644 (file)
@@ -3,7 +3,7 @@ namespace phancap;
 
 class Options
 {
-    public static $options = array(
+    public $options = array(
         /**
          * Browser settings
          */
@@ -96,7 +96,7 @@ class Options
      */
     public function parse($arValues)
     {
-        foreach (static::$options as $name => $arOption) {
+        foreach ($this->options as $name => $arOption) {
             $this->values[$name] = $arOption['default'];
             if (!isset($arValues[$name])) {
                 if (isset($arOption['required'])) {
@@ -263,8 +263,8 @@ class Options
     public function setConfig(Config $config)
     {
         $this->config = $config;
-        static::$options['smaxage']['default'] = $this->config->screenshotMaxAge;
-        static::$options['smaxage']['min']     = $this->config->screenshotMinAge;
+        $this->options['smaxage']['default'] = $this->config->screenshotMaxAge;
+        $this->options['smaxage']['min']     = $this->config->screenshotMinAge;
     }
 }
 ?>
index 6eff33c4ecc38463c5c95351e136842d6fdc3d25..70361041d9841ff9c4f83aa81d8f7280379f0dbf 100644 (file)
@@ -1,4 +1,76 @@
 <?php
+namespace phancap;
+/**
+ * Check if everything is setup
+ */
+header('HTTP/1.0 500 Internal Server Error');
 
+if (file_exists(__DIR__ . '/../src/phancap/Autoloader.php')) {
+    include_once __DIR__ . '/../src/phancap/Autoloader.php';
+    Autoloader::register();
+} else {
+    include_once 'phancap/Autoloader.php';
+}
+header('HTTP/1.0 200 OK');
+?>
+<?xml version="1.0" encoding="utf-8"?>
+<html>
+ <head>
+  <title>phancap</title>
+ </head>
+ <body>
+  <h1>phancap</h1>
+  <p>
+   Web service to create website screenshots.
+  </p>
+
+  <h2>API</h2>
+  <p>
+   The API is accessible at <a href="get.php">get.php</a>.
+  </p>
+  <table border="1">
+   <caption>Available URL parameters</caption>
+   <thead>
+    <tr>
+     <th>Name</th>
+     <th>Description</th>
+     <th>Type</th>
+     <th>Default</th>
+    </tr>
+   </thead>
+   <tbody>
+<?php
+$options = new Options();
+$config = new Config();
+try {
+    $config->load();
+    $options->setConfig($config);
+} catch (\Exception $e) {}
 
+foreach ($options->options as $name => $option) {
+    echo '<tr>'
+        . '<td><tt>' . $name . '</tt></td>'
+        . '<td>' . htmlspecialchars($option['title']) . '</td>'
+        . '<td>'
+        . (
+            is_array($option['type'])
+            ? ('One of: <tt>' . implode('</tt>, <tt>', $option['type']) . '</tt>')
+            : str_replace('skip', '&#160;', $option['type'])
+        )
+        . '</td>'
+        . '<td>&#160;<tt>' . $option['default'] . '</tt></td>'
+        . '</tr>';
+}
 ?>
+   </tbody>
+  </table>
+
+
+  <h2>Tools</h2>
+  <ul>
+   <li>
+    <a href="setup.php">Setup check</a> to test if everything is ok
+   </li>
+  </ul>
+ </body>
+</html>
diff --git a/www/setup.php b/www/setup.php
new file mode 100644 (file)
index 0000000..747699a
--- /dev/null
@@ -0,0 +1,95 @@
+<?php
+namespace phancap;
+/**
+ * Check if everything is setup
+ */
+header('HTTP/1.0 500 Internal Server Error');
+
+if (file_exists(__DIR__ . '/../src/phancap/Autoloader.php')) {
+    include_once __DIR__ . '/../src/phancap/Autoloader.php';
+    Autoloader::register();
+} else {
+    include_once 'phancap/Autoloader.php';
+}
+
+$messages = array();
+
+$config = new Config();
+try {
+    $config->load();
+    $messages[][] = array('ok', 'Configuration check ok');
+} catch (\Exception $e) {
+    $messages[][] = array('err', $e->getMessage());
+}
+
+$adapter = array(
+    'Cutycapt'
+);
+foreach ($adapter as $classpart) {
+    $class = '\\phancap\\Adapter_' . $classpart;
+    $adapter = new $class();
+    $adapter->setConfig($config);
+    $errors = $adapter->isAvailable();
+    if ($errors === true) {
+        $messages[][] = array(
+            'ok', 'Adapter ' . $classpart . ' is available'
+        );
+    } else {
+        foreach ($errors as $msg) {
+            $messages['Adapter: '. $classpart][] = array('err', $msg);
+        }
+    }
+}
+
+header('HTTP/1.0 200 OK');
+
+$out = <<<HTM
+<?xml version="1.0" encoding="utf-8"?>
+<html>
+ <head>
+  <title>phancap setup check</title>
+  <style type="text/css">
+    li.ok:before {
+        content: '✔';
+        color: green;
+        padding: 0 0.5ex;
+        margin-right: 1ex;
+    }
+    li.err:before {
+        content: "✘";
+        color: white;
+        background-color: red;
+        padding: 0 0.5ex;
+        margin-right: 0.5ex;
+    }
+  </style>
+ </head>
+ <body>
+  <h1>phancap setup check</h1>
+<ul>
+HTM;
+foreach ($messages as $key => $messages) {
+    if (!is_numeric($key)) {
+        $out .= '<li>' . htmlspecialchars($key)
+            . '<ul>';
+    }
+    foreach ($messages as $data) {
+        list($state, $message) = $data;
+        $out .= '<li class="' . $state . '">';
+        $out .= htmlspecialchars($message);
+        $out .= '</li>' . "\n";
+    }
+    if (!is_numeric($key)) {
+        $out .= '</ul></li>' . "\n";
+    }
+}
+$out .= <<<HTM
+  </ul>
+  <p>
+   <a href="./">back</a> to the index
+  </p>
+ </body>
+</html>
+HTM;
+echo $out;
+?>