Make it possible to run indieauth-openid as .phar
[indieauth-openid.git] / www / index.php
index 3a32eea7d9e10d7e781d77e6a9734fd26a43db4d..cb698454c0e88bdbfe7932bc705e9ccb013b95ed 100644 (file)
@@ -29,7 +29,22 @@ require_once 'OpenID/Exception.php';
 
 function loadDb()
 {
-    $db = new PDO('sqlite:' . __DIR__ . '/../data/tokens.sq3');
+    $pharFile = \Phar::running();
+    if ($pharFile == '') {
+        $dsn = 'sqlite:' . __DIR__ . '/../data/tokens.sq3';
+        $cfgFilePath = __DIR__ . '/config.php';
+    } else {
+        //remove phar:// from the path
+        $dir = dirname(substr($pharFile, 7)) . '/';
+        $dsn = 'sqlite:' . $dir . '/tokens.sq3';
+        $cfgFilePath = substr($pharFile, 7) . '.config.php';
+    }
+    //allow overriding DSN
+    if (file_exists($cfgFilePath)) {
+        include $cfgFilePath;
+    }
+
+    $db = new PDO($dsn);
     $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
     $db->exec("CREATE TABLE IF NOT EXISTS authtokens(
 code TEXT,
@@ -175,6 +190,8 @@ if (isset($_GET['openid_mode']) && $_GET['openid_mode'] != '') {
         }
     } catch (OpenID_Exception $e) {
         error('Error verifying OpenID login: ' . $e->getMessage());
+    } catch (Exception $e) {
+        error(get_class($e) . ': ' . $e->getMessage());
     }
 }
 
@@ -201,12 +218,19 @@ if ($_SERVER['REQUEST_METHOD'] == 'GET') {
 
     try {
         $o = new \OpenID_RelyingParty($returnTo, $realm, $me);
+        //if you get timeouts (errors like
+        // OpenID error: Request timed out after 3 second(s)
+        //) then uncomment the following line which disables
+        // all timeouts:
+        //$o->setRequestOptions(array('follow_redirects' => true));
         $authRequest = $o->prepare();
         $url = $authRequest->getAuthorizeURL();
         header("Location: $url");
         exit(0);
     } catch (OpenID_Exception $e) {
         error('OpenID error: ' . $e->getMessage());
+    } catch (Exception $e) {
+        error(get_class($e) . ': ' . $e->getMessage());
     }
 } else if ($_SERVER['REQUEST_METHOD'] == 'POST') {
     $redirect_uri = verifyUrlParameter($_POST, 'redirect_uri');