Skip to content

Commit

Permalink
+ Add api.php as entrance.
Browse files Browse the repository at this point in the history
  • Loading branch information
liugang committed Oct 26, 2017
1 parent 14ab071 commit 8f6026c
Show file tree
Hide file tree
Showing 11 changed files with 233 additions and 34 deletions.
12 changes: 12 additions & 0 deletions module/common/lang/en.php
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,7 @@
$lang->mail->menu = $lang->admin->menu;
$lang->dev->menu = $lang->admin->menu;
$lang->score->menu = $lang->admin->menu;
$lang->entry->menu = $lang->admin->menu;
$lang->webhook->menu = $lang->admin->menu;

/* 菜单分组。*/
Expand Down Expand Up @@ -421,6 +422,7 @@
$lang->error->float = "『%s』should be numbers, decimals included.";
$lang->error->email = "『%s』should be valid EMAIL.";
$lang->error->date = "『%s』should be valid date.";
$lang->error->code = "『%s』should be english or numbers.";
$lang->error->account = "『%s』should be valid account.";
$lang->error->passwordsame = "Two passwords should be consistent.";
$lang->error->passwordrule = "Password should meet requirements. It should be 6 characters at least.";
Expand All @@ -430,6 +432,16 @@
$lang->error->editedByOther = 'This record might have been changed. Please refresh and try to edit again!';
$lang->error->tutorialData = 'No data can be imported in tutorial mode. Please exit tutorial first!';

$lang->error->entry = array();
$lang->error->entry['300001'] = 'Param code not set.';
$lang->error->entry['300002'] = 'Param token not set.';
$lang->error->entry['310001'] = 'Entry not exist.';
$lang->error->entry['310002'] = 'Key of entry not set.';
$lang->error->entry['320001'] = 'IP denied.';
$lang->error->entry['330001'] = 'Invalid token.';
$lang->error->entry['340001'] = 'Session code not set.';
$lang->error->entry['340002'] = 'Session verify failed.';

/* 分页信息。*/
$lang->pager = new stdclass();
$lang->pager->noRecord = "No History";
Expand Down
10 changes: 10 additions & 0 deletions module/common/lang/zh-cn.php
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,16 @@
$lang->error->editedByOther = '该记录可能已经被改动。请刷新页面重新编辑!';
$lang->error->tutorialData = '新手模式下不会插入数据,请退出新手模式操作';

$lang->error->entry = array();
$lang->error->entry['300001'] = '缺少code参数';
$lang->error->entry['300002'] = '缺少token参数';
$lang->error->entry['310001'] = '应用不存在';
$lang->error->entry['310002'] = '应用未设置密钥';
$lang->error->entry['320001'] = '该IP访问被限制访问';
$lang->error->entry['330001'] = '不合法的token参数';
$lang->error->entry['340001'] = '缺少session code';
$lang->error->entry['340002'] = 'session验证失败';

/* 分页信息。*/
$lang->pager = new stdclass();
$lang->pager->noRecord = "暂时没有记录";
Expand Down
77 changes: 77 additions & 0 deletions module/common/model.php
Original file line number Diff line number Diff line change
Expand Up @@ -1351,6 +1351,83 @@ public static function convert2Pinyin($items)

return $convertedItems;
}

/**
* Check an entry.
*
* @access public
* @return void
*/
public function checkEntry()
{
if(!$this->session->valid_entry)
{
if(!$this->session->entry_code) $this->response(340001);
if($this->session->valid_entry != md5(md5($this->get->code) . $this->server->remote_addr)) $this->response(340002);
return true;
}

if(!$this->get->code) $this->response(300001);
if(!$this->get->token) $this->response(300002);

$entry = $this->loadModel('entry')->getByCode($this->get->code);
if(!$entry) $this->response(310001);
if(!$entry->key) $this->response(310002);

$this->checkEntryIP($entry->ip);
$this->checkEntryToken($entry->key);

$this->session->set('ENTRY_CODE', $this->get->code);
$this->session->set('VALID_ENTRY', md5(md5($this->get->code) . $this->server->remote_addr));
$this->loadModel('entry')->saveLog($entry->id, $this->server->request_uri);
}

/**
* Check ip of an entry.
*
* @param string $ip
* @access public
* @return void
*/
public function checkEntryIP($ip)
{
$ipWhiteList = $this->config->ipWhiteList;
$this->config->ipWhiteList = $ip;
$result = $this->checkIP();
$this->config->ipWhiteList = $ipWhiteList;
if(!$result) $this->response(320001);
}

/**
* Check token of an entry.
*
* @param string $key
* @access public
* @return void
*/
public function checkEntryToken($key)
{
parse_str($this->server->query_String, $queryString);
unset($queryString['token']);
$queryString = http_build_query($queryString);
if($_GET['token'] != md5(md5($queryString) . $key)) $this->response(330001);
}

/**
* Response.
*
* @param int $code
* @access public
* @return void
*/
public function response($code)
{
$response = new stdclass();
$response->errcode = $code;
$response->errmsg = $this->lang->error->entry[$code];

die(helper::jsonEncode($response));
}
}

class common extends commonModel
Expand Down
40 changes: 25 additions & 15 deletions module/entry/control.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ public function create()
{
if($_POST)
{
$entryID = $this->entry->create();
$id = $this->entry->create();
if(dao::isError()) $this->send(array('result' => 'fail', 'message' => dao::getError()));

$this->loadModel('action')->create('entry', $entryID, 'created');
$this->loadModel('action')->create('entry', $id, 'created');
$this->send(array('result' => 'success', 'message' => $this->lang->entry->saveSuccess, 'locate' => inlink('browse')));
}

Expand All @@ -57,26 +57,26 @@ public function create()
/**
* Edit an entry.
*
* @param int $entryID
* @param int $id
* @access public
* @return void
*/
public function edit($entryID)
public function edit($id)
{
if($_POST)
{
$changes = $this->entry->update($entryID);
$changes = $this->entry->update($id);
if(dao::isError()) $this->send(array('result' => 'fail', 'message' => dao::getError()));

if($changes)
{
$actionID = $this->loadModel('action')->create('entry', $entryID, 'edited');
$actionID = $this->loadModel('action')->create('entry', $id, 'edited');
$this->action->logHistory($actionID, $changes);
}
$this->send(array('result' => 'success', 'message' => $this->lang->entry->saveSuccess, 'locate' => inlink('browse')));
}

$entry = $this->entry->getById($entryID);
$entry = $this->entry->getById($id);
$this->view->title = $this->lang->entry->edit . $this->lang->colon . $entry->name;
$this->view->entry = $entry;
$this->display();
Expand All @@ -85,30 +85,40 @@ public function edit($entryID)
/**
* Delete an entry.
*
* @param int $entryID
* @param int $id
* @access public
* @return void
*/
public function delete($entryID)
public function delete($id)
{
$this->entry->delete(TABLE_ENTRY, $entryID);
$this->entry->delete(TABLE_ENTRY, $id);
if(dao::isError()) $this->send(array('result' => 'fail', 'message' => dao::getError()));

$this->send(array('result' => 'success'));
}

/**
* Show access logs of entry.
* Browse logs of an entry.
*
* @param int $entryID
* @param int $id
* @param string $orderBy
* @param int $recTotal
* @param int $recPerPage
* @param int $pageID
* @access public
* @return void
*/
public function log($entryID)
public function log($id, $orderBy = 'id_desc', $recTotal = 0, $recPerPage = 20, $pageID = 1)
{
$entry = $this->entry->getById($entryID);
$this->app->loadClass('pager', $static = true);
$pager = new pager($recTotal, $recPerPage, $pageID);

$entry = $this->entry->getByID($id);
$this->view->title = $this->lang->entry->log . $this->lang->colon . $entry->name;
$this->view->actions = $this->loadModel('action')->getList('entry', $entryID);
$this->view->logs = $this->entry->getLogList($id, $orderBy, $pager);
$this->view->entry = $entry;
$this->view->orderBy = $orderBy;
$this->view->pager = $pager;
$this->display();
}
}
4 changes: 3 additions & 1 deletion module/entry/lang/zh-cn.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
$lang->entry->createdDate = '创建时间';
$lang->entry->editedby = '最后编辑';
$lang->entry->editedDate = '编辑时间';
$lang->entry->date = '请求时间';
$lang->entry->url = '请求地址';

$lang->entry->apiIndex = '应用入口';
$lang->entry->saveSuccess = '保存成功';
Expand All @@ -30,6 +32,6 @@
$lang->entry->note = new stdClass();
$lang->entry->note->name = '授权应用名称';
$lang->entry->note->code = '授权应用代号,必须为字母或数字的组合';
$lang->entry->note->ip = "允许访问应用的ip,多个ip用逗号隔开。支持IP段,如192.168.1.*";
$lang->entry->note->ip = "允许访问API的应用ip,多个ip用逗号隔开。支持IP段,如192.168.1.*";
$lang->entry->note->allIP = '无限制';
$lang->entry->note->api = 'moduleName、methodName以及参数列表替换成实际的值';
19 changes: 19 additions & 0 deletions module/entry/model.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,25 @@ public function getList($orderBy = 'id_desc', $pager = null)
return $this->dao->select('*')->from(TABLE_ENTRY)->where('deleted')->eq('0')->orderBy($orderBy)->page($pager)->fetchAll('id');
}

/**
* Get log list of an entry .
*
* @param int $id
* @param string $orderBy
* @param object $pager
* @access public
* @return array
*/
public function getLogList($id, $orderBy = 'date_desc', $pager = null)
{
return $this->dao->select('*')->from(TABLE_LOG)
->where('objectType')->eq('entry')
->andWhere('objectID')->eq($id)
->orderBy($orderBy)
->page($pager)
->fetchAll('id');
}

/**
* Create an entry.
*
Expand Down
37 changes: 25 additions & 12 deletions module/entry/view/log.html.php
Original file line number Diff line number Diff line change
@@ -1,32 +1,45 @@
<?php
/**
* The log view file of entry module of RanZhi.
* The log view file of log module of ZenTaoPMS.
*
* @copyright Copyright 2009-2017 青岛易软天创网络科技有限公司(QingDao Nature Easy Soft Network Technology Co,LTD, www.cnezsoft.com)
* @license ZPL (http://zpl.pub/page/zplv12.html)
* @author Gang Liu <[email protected]>
* @package entry
* @package log
* @version $Id$
* @link http://www.ranzhico.com
* @link http://www.zentao.net
*/
?>
<?php include 'header.html.php';?>
<table class='table table-condensed table-hover table-striped'>
<?php include '../../common/view/header.html.php';?>
<div id="titlebar">
<div class="heading">
<strong><?php echo html::a(inlink('browse'), $lang->entry->common);?></strong>
<small class="text-muted"> <?php echo $entry->name;?> </small>
<small class="text-muted"> <?php echo $lang->entry->log;?> <i class="icon-file-text-o"></i></small>
</div>
</div>
<table id='logList' class='table table-condensed table-hover table-striped tablesorter table-fixed'>
<thead>
<tr>
<th class='w-120px'><?php echo $lang->action->date;?></th>
<th class='w-100px'><?php echo $lang->action->actor;?></th>
<th><?php echo $lang->entry->desc;?></th>
<?php $vars = "id={$entry->id}&orderBy=%s&recTotal={$pager->recTotal}&recPerPage={$pager->recPerPage}&pageID={$pager->pageID}";?>
<th class='w-60px'><?php common::printOrderLink('id', $orderBy, $vars, $lang->entry->id);?></th>
<th class='w-160px'><?php common::printOrderLink('date', $orderBy, $vars, $lang->entry->date);?></th>
<th><?php common::printOrderLink('url', $orderBy, $vars, $lang->entry->url);?></th>
</tr>
</thead>
<tbody>
<?php foreach($actions as $action);?>
<?php foreach($logs as $id => $log):?>
<tr>
<td><?php echo $action->date;?></td>
<td><?php echo zget($users, $action->actor);?></td>
<td><?php echo $action->extra;?></td>
<td class='text-center'><?php echo $id;?></td>
<td><?php echo $log->date;?></td>
<td class='text' title='<?php echo $log->url;?>'><?php echo $log->url;?></td>
</tr>
<?php endforeach;?>
</tbody>
<tfoot>
<tr>
<td colspan='3'><?php $pager->show();?></td>
</tr>
</tfoot>
</table>
<?php include '../../common/view/footer.html.php';?>
5 changes: 2 additions & 3 deletions module/webhook/control.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,16 +144,15 @@ public function asyncSend()
$snoopy = $this->app->loadClass('snoopy');
foreach($dataList as $data)
{
$webhook = zget($webhooks, $data->webhook, '');
$httpCode = 0;
$webhook = zget($webhooks, $data->webhook, '');
if($webhook)
{
$contentType = zget($this->config->webhook->contentTypes, $webhook->contentType, 'application/json');
$result = $this->webhook->fetchHook($contentType, $webhook->url, $data->data);
$this->saveLog($data->webhook, $data->action, $webhook->url, $contentType, $data->data, $result);
}

if($httpCode == 200) $this->dao->update(TABLE_WEBHOOKDATA)->set('status')->eq('sended')->where('id')->eq($data->id)->exec();
$this->dao->update(TABLE_WEBHOOKDATA)->set('status')->eq('sended')->where('id')->eq($data->id)->exec();
}

$this->dao->delete()->from(TABLE_WEBHOOKDATA)->where('status')->eq('sended')->exec();
Expand Down
6 changes: 5 additions & 1 deletion module/webhook/lang/zh-cn.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,12 @@
$lang->webhook->project = '关联项目';
$lang->webhook->action = '触发动作';
$lang->webhook->desc = '描述';
$lang->webhook->createdBy = '由谁创建';
$lang->webhook->createdDate = '创建时间';
$lang->webhook->editedby = '最后编辑';
$lang->webhook->editedDate = '编辑时间';
$lang->webhook->data = '数据';
$lang->webhook->status = '状态';
$lang->webhook->result = '结果';

$lang->webhook->sendTypeList['sync'] = '同步';
$lang->webhook->sendTypeList['async'] = '异步';
Expand Down
4 changes: 2 additions & 2 deletions module/webhook/view/log.html.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<?php include '../../common/view/header.html.php';?>
<div id="titlebar">
<div class="heading">
<strong><?php echo html::a(inlink('browse', "type={$webhook->type}"), $webhook->type ? $lang->webhook->dingding : $lang->webhook->common);?></strong>
<strong><?php echo html::a(inlink('browse', "type={$webhook->type}"), $webhook->type == 'dingding' ? $lang->webhook->dingding : $lang->webhook->common);?></strong>
<small class="text-muted"> <?php echo $webhook->name;?> </small>
<small class="text-muted"> <?php echo $lang->webhook->log;?> <i class="icon-file-text-o"></i></small>
</div>
Expand All @@ -26,7 +26,7 @@
<th><?php common::printOrderLink('url', $orderBy, $vars, $lang->webhook->url);?></th>
<th class='w-300px'><?php common::printOrderLink('action', $orderBy, $vars, $lang->webhook->action);?></th>
<th class='w-200px'><?php common::printOrderLink('contentType', $orderBy, $vars, $lang->webhook->contentType);?></th>
<th class='w-200px'><?php common::printOrderLink('status', $orderBy, $vars, $lang->webhook->status);?></th>
<th class='w-200px'><?php common::printOrderLink('result', $orderBy, $vars, $lang->webhook->result);?></th>
</tr>
</thead>
<tbody>
Expand Down
Loading

0 comments on commit 8f6026c

Please sign in to comment.