$val) {
$str .= "$key:\"$val\";";
}
}
$trusted = true;
}
$s = '
';
return $s;
}
/**
* sreg_form
*
* @param Zend_OpenId_Extension_Sreg $sreg
* @return string
*/
function sreg_form(Zend_OpenId_Extension_Sreg $sreg)
{
$s = "";
$props = $sreg->getProperties();
if (is_array($props) && count($props) > 0) {
$s = 'It also requests additinal information about you';
$s .= ' (fields marked by * are required)
';
$s .= '
';
$policy = $sreg->getPolicyUrl();
if (!empty($policy)) {
$s .= 'The private policy can be found at '.$policy.'.
';
}
}
return $s;
}
$session = new Zend_Session_Namespace("opeinid.server");
Zend_Session::start();
$ret = false;
if ($_SERVER["REQUEST_METHOD"] == "GET") {
if (!isset($_GET['openid_action']) && isset($_GET['openid_mode'])) {
$ret = $server->handle($_GET, new Zend_OpenId_Extension_Sreg());
} else {
require_once 'Zend/View.php';
$view = new Zend_View();
$view->setScriptPath(dirname(__FILE__) . '/templates');
$view->strictVars(true);
if (isset($session->id)) {
$view->id = $session->id;
}
if (isset($session->error)) {
$view->error = $session->error;
unset($session->error);
}
if (isset($_GET['openid_action'])) {
if ($_GET['openid_action'] == 'register') {
$ret = $view->render('register.phtml');
} else if ($_GET['openid_action'] == 'registration_complete' &&
isset($_GET['openid_name'])) {
$view->name = $_GET['openid_name'];
$view->url = Zend_OpenId::selfURL() . '?openid=' . $view->name;
if ($server->hasUser($view->url)) {
$view->url2 = Zend_OpenId::selfURL() . '?openid2=' . $view->name;
$ret = $view->render('registration_complete.phtml');
}
} else if ($_GET['openid_action'] == 'logout') {
$server->logout();
header('Location: ' . $_SERVER['PHP_SELF']);
exit;
} else if ($_GET['openid_action'] == 'login') {
if (isset($_GET['openid_identity'])) {
$view->id = $_GET['openid_identity'];
$view->ro = true;
}
$ret = $view->render('login.phtml');
} else if ($_GET['openid_action'] == 'trust') {
if ($server->getLoggedInUser() !== false) {
$view->site = $server->getSiteRoot($_GET);
$view->url = $server->getLoggedInUser();
$sreg = new Zend_OpenId_Extension_Sreg();
$sreg->parseRequest($_GET);
$view->sreg = sreg_form($sreg);
if ($server->hasUser($view->url)) {
$ret = $view->render('trust.phtml');
}
}
}
} else if (isset($_GET['openid'])) {
$url = Zend_OpenId::selfURL() . '?openid=' . $_GET['openid'];
if ($server->hasUser($url)) {
$view->server = Zend_OpenId::selfURL();
$view->name = $_GET['openid'];
$ret = $view->render('identity.phtml');
}
} else if (isset($_GET['openid2'])) {
$url = Zend_OpenId::selfURL() . '?openid=' . $_GET['openid2'];
if ($server->hasUser($url)) {
$view->server = Zend_OpenId::selfURL();
$view->name = $_GET['openid2'];
$ret = $view->render('identity2.phtml');
}
} else {
if ($server->getLoggedInUser() !== false) {
$view->url = $server->getLoggedInUser();
if ($server->hasUser($view->url)) {
$sites = $server->getTrustedSites();
$s = "";
foreach ($sites as $site => $trusted) {
if (is_bool($trusted) || is_array($trusted)) {
$s .= trust_form($site, $trusted);
}
}
if (empty($s)) {
$s = "None |
";
}
$view->sites = $s;
$ret = $view->render('profile.phtml');
}
} else {
$ret = $view->render('login.phtml');
}
}
}
} else if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (!isset($_POST['openid_action']) && isset($_POST['openid_mode'])) {
$ret = $server->handle($_POST, new Zend_OpenId_Extension_Sreg());
} else if (isset($_POST['openid_action'])) {
if ($_POST['openid_action'] == 'login' &&
isset($_POST['openid_url']) &&
isset($_POST['openid_password'])) {
if (!$server->login($_POST['openid_url'],
$_POST['openid_password'])) {
$session->error = 'Wrong identity/password!';
$session->id = $_POST['openid_url'];
}
unset($_GET['openid_action']);
Zend_OpenId::redirect($_SERVER['PHP_SELF'], $_GET);
} else if ($_POST['openid_action'] == 'register' &&
isset($_POST['openid_name']) &&
isset($_POST['openid_password']) &&
isset($_POST['openid_password2'])) {
$url = Zend_OpenId::selfURL() . '?openid=' . $_POST['openid_name'];
if ($_POST['openid_password'] != $_POST['openid_password2']) {
$session->name = $_POST['openid_name'];
$session->error = 'Password mismatch.';
header('Location: ' . $_SERVER['PHP_SELF'] . '?openid.action=register');
} else if ($server->register($url, $_POST['openid_password'])) {
header('Location: ' . $_SERVER['PHP_SELF'] . '?openid.action=registration_complete&openid.name=' . $_POST['openid_name']);
} else {
$session->error = 'Registration failed. Try another name.';
header('Location: ' . $_SERVER['PHP_SELF'] . '?openid.action=register');
}
exit;
} else if ($_POST['openid_action'] == 'trust') {
if (isset($_GET['openid_return_to'])) {
$sreg = new Zend_OpenId_Extension_Sreg();
$sreg->parseResponse($_POST);
if (isset($_POST['allow'])) {
if (isset($_POST['forever'])) {
$server->allowSite($server->getSiteRoot($_GET), $sreg);
}
unset($_GET['openid_action']);
$server->respondToConsumer($_GET, $sreg);
} else if (isset($_POST['deny'])) {
if (isset($_POST['forever'])) {
$server->denySite($server->getSiteRoot($_GET));
}
Zend_OpenId::redirect($_GET['openid_return_to'], array('openid.mode'=>'cancel'));
}
} else if (isset($_POST['allow'])) {
$server->allowSite($_POST['site']);
header('Location: ' . $_SERVER['PHP_SELF']);
exit;
} else if (isset($_POST['deny'])) {
$server->denySite($_POST['site']);
header('Location: ' . $_SERVER['PHP_SELF']);
exit;
} else if (isset($_POST['del'])) {
$server->delSite($_POST['site']);
header('Location: ' . $_SERVER['PHP_SELF']);
exit;
}
}
}
}
if (is_string($ret)) {
echo $ret;
} else if ($ret !== true) {
header('HTTP/1.0 403 Forbidden');
echo 'Forbidden';
}