westdc-zf1/application/module/Open/Handler/AppHandler.php

141 lines
3.0 KiB
PHP
Raw Permalink Normal View History

<?php
namespace Open\Handler;
use \Helpers\View as view;
use \Helpers\dbh;
use \Helpers\Table;
use \Files\Files;
class AppHandler implements \Open\Event\AppEvent
{
private $db; //传入PDO对象误
private $config; //全局配置
public $table;
public $tbl_maillog = ""; //邮件日志表
function __construct($db = NULL)
{
if(empty($db))
{
$this->db = \Zend_Registry::get('db');
}else{
$this->db = $db;
}
$this->config = \Zend_Registry::get('config');
$this->table = new Table();
}
//检查app参数
public function appCheckParam(\Zend_EventManager_Event $e)
{
$data = $e->getParam('data');
if(!is_array($data))
{
return "参数错误";
}
if(empty($data['subject']))
{
return "请输入应用名称";
}
if(empty($data['client_domain']))
{
return "请填写应用使用的域名不带www";
}
if(empty($data['redirect_uri']))
{
return "请输入授权成功后的回调地址";
}
$sql = "SELECT id FROM {$this->table->oauth_clients} WHERE subject='{$data['subject']}'";
$rs = $this->db->query($sql);
$row = $rs->fetch();
if(!empty($row['id']))
{
return "此应用名称已存在,请重新输入";
}
return true;
}
//处理app参数数据
public function appProcessData(\Zend_EventManager_Event $e)
{
$data = $e->getParam('data');
$id = $e->getParam('id');
if(empty($id))
{
$data['client_id'] = $this->create_client_guid(__CLASS__);
$data['client_secret'] = strtoupper(substr(md5($data['client_id'].$data['client_domain'].$data['redirect_uri'].time()),10,24));
}
$data['user_id'] = view::User('id');
return $data;
}
//创建App成功后
public function appCreated(\Zend_EventManager_Event $e)
{
$id = $e->getParam('id');
$data = $e->getParam('data');
return true;
}
//App编辑成功后
public function appEdited(\Zend_EventManager_Event $e)
{
$id = $e->getParam('id');
$data = $e->getParam('data');
return true;
}
public function create_client_guid($namespace = '') {
static $guid = '';
$uid = uniqid("", true);
$data = $namespace;
if(isset($_SERVER['REQUEST_TIME']))
$data .= $_SERVER['REQUEST_TIME'];
if(isset($_SERVER['HTTP_USER_AGENT']))
$data .= $_SERVER['HTTP_USER_AGENT'];
if(isset($_SERVER['LOCAL_ADDR']))
$data .= $_SERVER['LOCAL_ADDR'];
if(isset($_SERVER['LOCAL_PORT']))
$data .= $_SERVER['LOCAL_PORT'];
if(isset($_SERVER['REMOTE_ADDR']))
$data .= $_SERVER['REMOTE_ADDR'];
if(isset($_SERVER['REMOTE_PORT']))
$data .= $_SERVER['REMOTE_PORT'];
$data .= time();
$hash = strtoupper(hash('ripemd128', $uid . $guid . md5($data)));
$guid = '' .
substr($hash, 0, 8) .
'-' .
substr($hash, 8, 4) .
'-' .
substr($hash, 12, 4) .
'-' .
substr($hash, 16, 4) .
'-' .
substr($hash, 20, 12) .
'';
return $guid;
}
}