westdc-zf1/application/default/controllers/OpenController.php

166 lines
3.7 KiB
PHP

<?php
use \Helpers\View as view;
use \Open\App;
use \Open\Open;
use \Open\OAuth2\Server;
class OpenController extends \Zend_Controller_Action
{
private $config = NULL;
private $db = NULL;
private $open = NULL;
function preDispatch()
{
$this->view->config = $this->config = \Zend_Registry::get('config');
$this->db = \Zend_Registry::get('db');
$this->view->theme = new Theme();
$this->_helper->layout->setLayout('layout-open');
}
function indexAction()
{
$this->view->pageID = "open-index";
}
//我的应用
function myappAction()
{
$this->view->pageID = "open-myapp";
$app = new App();
$user_state = $app->checkinfo();
if( $user_state !== true)
{
view::post($this,$user_state,"/account/edit");
}
$ac = $this->_getParam('ac');
$id = $this->_getParam('id');
$submit = $this->_getParam('submit');
if(empty($ac))
{
$this->view->myapp = $app->getUserApp();
return true;
}
if($ac == "create")
{
$this->_helper->viewRenderer('myapp-create');
$this->view->appStatus = $app->appStatus();
if(!empty($id))
{
$this->view->info = $app->getAppInfo($id);
}
if(!empty($submit))
{
if(empty($id))
{
$status = $app->appCreate();
}else{
$this->view->info = $app->getAppCreateParam();
$status = $app->appCreate($id);
}
if($status !== true && !is_numeric($status))
{
$this->view->error = view::Error($status);
}else{
if(!empty($id))
view::Post($this,"修改成功!",'/open/myapp/ac/view/id/'.$id);
else
view::Post($this,"添加成功!","/open/myapp/ac/view/id/".$status);
}
}
return true;
}
if($ac == "delete")
{
$status = $app->delete($id);
if($status !== true)
{
view::Post($this,$status,"/open/myapp");
}else{
view::Post($this,'删除成功',"/open/myapp");
}
return false;
}
if($ac == "view")
{
$this->_helper->viewRenderer('myapp-view');
$this->view->data = $app->getAppInfo($id);
$this->view->appStatus = $app->appStatus();
return true;
}
}
function authenticateAction()
{
$this->_helper->layout->disableLayout();
$this->_helper->viewRenderer->setNoRender();
$data = array(
'client_id' => $this->_getParam('client_id'),
'redirect_uri' => $this->_getParam('redirect_uri'),
'client_secret' => $this->_getParam('client_secret'),
'host' => $_SERVER['HTTP_HOST'],
'ip' => $_SERVER['REMOTE_ADDR']
);
$server = new Server();
$status = $server->clientCredentials($data['client_id'],$data['client_secret']);
if($status !== true)
{
echo $status;
}else{
}
}
function authorizeAction()
{
$submit = $this->_getParam('submit');
if(empty($submit))
{
return true;
}
$sv = new open\server();
$server = $sv->bootstrap();
$request = OAuth2\Request::createFromGlobals();
$response = new OAuth2\Response();
// validate the authorize request
if (!$server->validateAuthorizeRequest($request, $response)) {
$response->send();
die;
}
// print the authorization code if the user has authorized your client
$is_authorized = ($_POST['authorized'] === 'yes');
$server->handleAuthorizeRequest($request, $response, $is_authorized);
if ($is_authorized) {
// this is only here so that you get to see your code in the cURL request. Otherwise, we'd redirect back to the client
$code = substr($response->getHttpHeader('Location'), strpos($response->getHttpHeader('Location'), 'code=')+5, 40);
exit("SUCCESS! Authorization Code: $code");
}
$response->send();
}
}