<?php namespace Phpcmf\Controllers\Admin;
class Api extends \Phpcmf\Common
{
public function oauth() {
$uid = intval(\Phpcmf\Service::L('input')->get('uid'));
$name = dr_safe_replace(\Phpcmf\Service::L('input')->get('name'));
if (!$uid) {
$this->_admin_msg(0, dr_lang('uid参数传递失败'));
}
$oauth = \Phpcmf\Service::L('cache')->init()->get('admin_auth_login_'.$name.'_'.$uid);
if (!$oauth) {
$this->_admin_msg(0, dr_lang('授权信息(%s)获取失败', $name));
} elseif (SYS_TIME - $oauth > 60) {
$this->_admin_msg(0, dr_lang('授权信息(%s)验证超时', $name));
}
$data = \Phpcmf\Service::M('member')->get_member($uid);
if (!$data) {
$this->_admin_msg(0, dr_lang('账号(%s)不存在', $uid));
} elseif (!$data['is_admin']) {
$this->_admin_msg(0, dr_lang('账号(%s)不是管理员', $data['username']));
}
\Phpcmf\Service::M('auth')->login_session($data);
\Phpcmf\Service::L('cache')->init()->save('admin_auth_login_'.$name.'_'.$uid, 0, 10);
dr_redirect(ADMIN_URL.SELF);
}
public function add_main_table() {
$table = dr_safe_filename($_GET['table']);
$tables = $this->_main_table();
if (!$tables[$table]) {
$this->_json(0, dr_lang('自定义面板[%s]不存在', $table));
}
$this->_json(1, \Phpcmf\Service::M('auth')->edit_main_table($table, $tables[$table]));
}
public function gohome() {
dr_redirect('index.php');
}
public function field_call() {
$call = dr_safe_replace(\Phpcmf\Service::L('input')->get('name'));
if (!$call) {
$this->_json(0, dr_lang('没有填写函数方法'));
}
if (strpos($call, '_') === 0) {
if (method_exists(\Phpcmf\Service::L('form'), $call)) {
$this->_json(1, dr_lang('定义成功'));
} else {
$this->_json(0, 'form类方法【'.$call.'】未定义');
}
} else {
if (function_exists($call)) {
$this->_json(1, dr_lang('定义成功'));
} else {
$this->_json(0, '函数【'.$call.'】未定义');
}
}
}
public function notice() {
$id = (int)\Phpcmf\Service::L('input')->get('id');
$data = \Phpcmf\Service::M()->table('admin_notice')->get($id);
if (!$data) {
$this->_admin_msg(0, dr_lang('该数据不存在'));
}
if (!isset($this->admin['roleid'][1])) {
if ($data['to_uid'] && $data['to_uid'] != $this->uid) {
$this->_admin_msg(0, dr_lang('您无权限执行'));
} elseif ($data['to_rid'] && !isset($this->admin['roleid'][$data['to_rid']])) {
$this->_admin_msg(0, dr_lang('您无权限执行'));
}
}
list($uri, $param) = explode(':', $data['uri']);
$url = ADMIN_URL.\Phpcmf\Service::L('Router')->url($uri);
$param && $url.= '&'.http_build_query(dr_rewrite_decode($param, '/'));
if (!$data['status']) {
\Phpcmf\Service::M()->table('admin_notice')->update($id, array(
'status' => 1,
'op_uid' => $this->uid,
'op_username' => $this->admin['username'],
));
}
dr_redirect($url, 'refresh');
}
public function my() {
$color = ['default', 'blue', 'red', 'green', 'dark', 'yellow'];
$target = [0 => dr_lang('内链'), 1 => dr_lang('外链')];
if (IS_AJAX_POST) {
if (!\Phpcmf\Service::L('form')->check_captcha('code')) {
$this->_json(0, dr_lang('验证码不正确'), ['field' => 'code']);
}
$menu = [];
$data = \Phpcmf\Service::L('input')->post('data');
if ($data['usermenu']) {
foreach ($data['usermenu']['name'] as $id => $v) {
$v && $data['usermenu']['url'][$id] && $menu[$id] = [
'name' => $v,
'url' => $data['usermenu']['url'][$id],
'color' => $data['usermenu']['color'][$id],
'target' => $data['usermenu']['target'][$id],
];
}
}
$password = dr_safe_password(\Phpcmf\Service::L('input')->post('password'));
$password && \Phpcmf\Service::M('member')->edit_password($this->member, $password);
\Phpcmf\Service::M()->db->table('admin')->where('uid', $this->admin['id'])->update([
'usermenu' => dr_array2string($menu)
]);
\Phpcmf\Service::M()->db->table('member_data')->where('id', $this->admin['id'])->update([
'is_admin' => 1
]);
$this->_json(1, dr_lang('操作成功'));
}
$select = '';
foreach ($color as $t) {
$select.= '<option value="'.$t.'">'.$t.'</option>';
}
$select2 = '';
foreach ($target as $i => $t) {
$select2.= '<option value="'.$i.'">'.$t.'</option>';
}
$name = ['qq', 'weixin', 'weibo', 'wechat'];
foreach ($name as $key => $value) {
if (!isset($this->member_cache['oauth'][$value]['id'])
|| !$this->member_cache['oauth'][$value]['id']) {
unset($name[$key]);
}
}
\Phpcmf\Service::V()->assign([
'menu' => \Phpcmf\Service::M('auth')->_admin_menu(
[
'资料修改' => ['api/my', 'fa fa-user'],
'登录记录' => ['root/login_index{id='.$this->uid.'}', 'fa fa-calendar'],
]
),
'color' => $color,
'target' => $target,
'oauth_data' => $name,
'oauth_list' => \Phpcmf\Service::M('member')->oauth($this->uid),
'select_color' => $select,
'is_post_user' => \Phpcmf\Service::M('auth')->is_post_user(),
'select_target' => $select2,
]);
\Phpcmf\Service::V()->display('api_my.html');exit;
}
public function menu() {
$url = urldecode(dr_safe_replace(\Phpcmf\Service::L('input')->get('v')));
$arr = parse_url($url);
$queryParts = explode('&', $arr['query']);
$params = [];
foreach ($queryParts as $param) {
$item = explode('=', $param);
$params[$item[0]] = $item[1];
}
$uri = ($params['s'] ? $params['s'].'/' : '').($params['c'] ? $params['c'] : 'home').'/'.($params['m'] ? $params['m'] : 'index');
$menu = \Phpcmf\Service::M()->db->table('admin_menu')->select('name')->where('uri', $uri)->get()->getRowArray();
$name = $menu ? $menu['name'] : '未知名称';
$admin = \Phpcmf\Service::M()->db->table('admin')->where('uid', $this->uid)->get()->getRowArray();
if ($admin) {
$menu = dr_string2array($admin['usermenu']);
foreach ($menu as $t) {
$t['url'] == $url && $this->_json(1, dr_lang('已经存在'));
}
$menu[] = array(
'name' => $name,
'url' => $url,
);
\Phpcmf\Service::M()->db->table('admin')->where('uid', $this->uid)->update(array(
'usermenu' => dr_array2string($menu)
)
);
$this->_json(1, dr_lang('操作成功'));
}
$this->_json(0, dr_lang('加入失败'));
}
public function cache() {
$name = dr_safe_replace($_GET['id']);
\Phpcmf\Service::M('cache')->$name();
exit($this->_json(1, dr_lang('更新完成'), 0));
}
public function cache_clear() {
\Phpcmf\Service::M('cache')->update_data_cache();
exit($this->_json(1, dr_lang('前台数据缓存已被更新')));
}
public function cache_update() {
\Phpcmf\Service::M('cache')->update_cache();
exit($this->_json(1, dr_lang('更新完成')));
}
public function cache_search() {
\Phpcmf\Service::M('cache')->update_search_index();
exit($this->_json(1, dr_lang('更新完成')));
}
public function cache_site_config() {
\Phpcmf\Service::M('cache')->update_search_index();
exit($this->_json(1, dr_lang('更新完成')));
}
public function syskey() {
echo 'PHPCMF'.strtoupper(substr((md5(SYS_TIME)), rand(0, 10), 13));exit;
}
public function referer() {
$s = strtoupper(base64_encode(md5(SYS_TIME).md5(rand(0, 2015).md5(rand(0, 2015)))).md5(rand(0, 2009)));
echo str_replace('=', '', substr($s, 0, 42));exit;
}
public function domain() {
$html = '';
$post = \Phpcmf\Service::L('input')->post('data');
if ($post) {
$my = [];
foreach ($this->site_domain as $name => $sid) {
if ($sid == SITE_ID) {
unset($this->site_domain[$name]);
}
}
foreach ($post as $name => $t) {
if (!$t) {
continue;
}
if ($name == 'site_domains') {
$v = explode(',', str_replace([chr(13), PHP_EOL], ',', $t));
if ($v) {
foreach ($v as $t) {
$t && $my[] = $t;
$this->site_domain[$t] && $html.= '<p>'.$t.' 已经存在于其他站点</p>';
}
}
} else {
$my[] = $t;
$this->site_domain[$t] && $html.= $t.' 已经存在于其他站点';
}
}
$my && count($my) != count(array_unique($my)) && $html.= '<p>当前配置项存在重复域名</p>';
$html && exit($html);
}
exit('ok');
}
public function mtotal() {
$t1 = $t2 = $t3 = $t4 = $t5 =0;
$dir = dr_safe_filename(\Phpcmf\Service::L('input')->get('dir'));
$status = \Phpcmf\Service::M('auth')->get_admin_verify_status();
if (is_dir(APPSPATH.ucfirst($dir))) {
$t1 = \Phpcmf\Service::M()->db->table(SITE_ID.'_'.$dir.'_index')->where('status=9')->where('DATEDIFF(from_unixtime(inputtime),now())=0')->countAllResults();
$t2 = \Phpcmf\Service::M()->db->table(SITE_ID.'_'.$dir.'_index')->where('status=9')->countAllResults();
$t3 = \Phpcmf\Service::M()->db->table(SITE_ID.'_'.$dir.'_verify')->where(($status ? 'status IN('.implode(',', $status).')' : 'status>=0'))->countAllResults();
$t4 = \Phpcmf\Service::M()->db->table(SITE_ID.'_'.$dir.'_recycle')->where('uid', $this->uid)->countAllResults();
$t5 = \Phpcmf\Service::M()->db->table(SITE_ID.'_'.$dir.'_time')->where('uid', $this->uid)->countAllResults();
}
echo '$("#'.$dir.'_today").html('.$t1.');';
echo '$("#'.$dir.'_all").html('.$t2.');';
echo '$("#'.$dir.'_verify").html('.$t3.');';
echo '$("#'.$dir.'_recycle").html('.$t4.');';
echo '$("#'.$dir.'_timing").html('.$t5.');';
exit;
}
public function icon() {
\Phpcmf\Service::V()->display('api_icon.html');exit;
}
public function config() {
\Phpcmf\Service::V()->display('api_config.html');
exit;
}
public function phpinfo() {
phpinfo();
exit;
}
public function email_test() {
if (!SYS_EMAIL) {
$this->_json(0, dr_lang('系统邮箱没有设置'));
}
$id = intval(\Phpcmf\Service::L('input')->get('id'));
$data = \Phpcmf\Service::M()->table('mail_smtp')->get($id);
if (!$data) {
$this->_json(0, dr_lang('数据#%s不存在', $id));
}
$dmail = \Phpcmf\Service::L('email')->set([
'host' => $data['host'],
'user' => $data['user'],
'pass' => $data['pass'],
'port' => $data['port'],
'from' => $data['user']
]);
if ($dmail->send(SYS_EMAIL, 'test', 'test for '.SITE_NAME)) {
$this->_json(1, dr_lang('测试成功'));
} else {
$this->_json(0, $dmail->error());
}
}
public function site() {
$id = intval(\Phpcmf\Service::L('input')->get('id'));
if (!$this->site_info[$id]) {
$this->_admin_msg(0, dr_lang('站点不存在'));
} elseif (!$this->admin) {
$this->_admin_msg(0, dr_lang('你还没有登录'));
}
\Phpcmf\Service::L('cache')->init('', 'site')->save('admin_login_site', $this->admin, 300);
$this->_msg(1, dr_lang('正在切换到【%s】...', $this->site_info[$id]['SITE_NAME']).'<script src="'.$this->site_info[$id]['SITE_URL'].'index.php?s=api&c=sso&action=slogin&code='.dr_authcode($this->admin['uid'].'-'.md5($this->admin['uid'].$this->admin['password']), 'ENCODE').'"></script>', $this->site_info[$id]['SITE_URL'].SELF, 0);
exit;
}
public function alogin() {
$uid = intval(\Phpcmf\Service::L('input')->get('id'));
$code = md5($this->admin['id'].$this->admin['password']);
\Phpcmf\Service::L('cache')->set_data('admin_login_member', $this->admin, 300);
$sso = '';
$url = \Phpcmf\Service::M('member')->get_sso_url();
foreach ($url as $u) {
$sso.= '<script src="'.$u.'index.php?s=api&c=sso&action=alogin&code='.dr_authcode($uid.'-'.$code, 'ENCODE').'"></script>';
}
\Phpcmf\Service::V()->assign([
'menu' => '',
]);
$url = urldecode(\Phpcmf\Service::L('input')->get('url', true));
!$url && $url = MEMBER_URL;
$this->_msg(1, dr_lang('正在授权登录此用户...').$sso, $url, 0);exit;
}
public function mobile() {
\Phpcmf\Service::V()->assign([
'url' => SITE_MURL,
]);
\Phpcmf\Service::V()->display('api_mobile.html');exit;
}
public function preview() {
$data = $_GET['data'];
$data['source_image'] = WRITEPATH.'preview.png';
$data['dynamic_output'] = true;
$rt = \Phpcmf\Service::L('Image')->watermark($data);
if (!$rt) {
echo \Phpcmf\Service::L('Image')->display_errors();
}
exit;
}
public function test_attach() {
$data = \Phpcmf\Service::L('input')->post('data');
$type = intval($data['type']);
$value = $data['value'][$type];
if (!$value) {
$this->_json(0, dr_lang('参数不存在'));
}
$rt = \Phpcmf\Service::L('upload')->save_file(
'content',
'this is phpcmf file-test',
'test/test.txt',
[
'id' => 0,
'url' => $data['url'],
'type' => $type,
'value' => $value,
]
);
if (!$rt['code']) {
$this->_json(0, $rt['msg']);
} elseif (strpos(dr_catcher_data($rt['data']['url']), 'phpcmf') !== false) {
$this->_json(1, dr_lang('测试成功'));
}
$this->_json(0, dr_lang('无法访问到附件: %s', $rt['data']['url']));
}
public function test_mobile() {
$data = \Phpcmf\Service::L('input')->post('data');
if (is_file(ROOTPATH.'config/mysms.php')) {
require_once ROOTPATH.'config/mysms.php';
}
$method = 'my_sendsms_code';
if (function_exists($method)) {
$rt = call_user_func_array($method, [
$data['mobile'],
rand(10000, 99999),
$data['third'],
]);
$this->_json($rt['code'], $rt['msg']);
} else {
$this->_json(0, dr_lang('你没有定义第三方短信接口: '. $method));
}
}
public function export_field() {
exit('此功能不可用');
}
public function export_list() {
$this->_admin_msg(0, '此功能不可用');
}
public function member() {
$uid = intval(\Phpcmf\Service::L('input')->get('uid'));
if ($uid) {
$data = \Phpcmf\Service::M('member')->get_member($uid);
if (!$data) {
$this->_json(0, dr_lang('此账号%s不存在', $uid));
}
} else {
$name = dr_safe_replace(\Phpcmf\Service::L('input')->get('name'));
$data = \Phpcmf\Service::M('member')->get_member(0, $name);
if (!$data) {
$this->_json(0, dr_lang('此账号%s不存在', $name));
}
}
\Phpcmf\Service::V()->assign([
'm' => $data,
]);
\Phpcmf\Service::V()->display('api_show_member.html');
exit;
}
public function test_dir() {
$v = \Phpcmf\Service::L('input')->get('v');
if (!$v) {
$this->_json(0, dr_lang('目录为空'));
} elseif (strpos($v, ' ') === 0) {
$this->_json(0, dr_lang('不能用空格开头'));
}
$path = dr_get_dir_path($v);
if (is_file($path.SELF)) {
$this->_json(0, dr_lang('目录不能是网站根目录'));
} elseif (is_dir($path)) {
$this->_json(1, dr_lang('目录正常'));
} else {
$this->_json(0, dr_lang('目录[%s]不存在', $path));
}
}
public function test_attach_dir() {
$v = \Phpcmf\Service::L('input')->get('v');
if (!$v) {
$this->_json(1, dr_lang('默认目录'));
} elseif (strpos($v, ' ') === 0) {
$this->_json(0, dr_lang('不能用空格开头'));
}
$path = dr_get_dir_path($v);
if (is_dir($path)) {
$this->_json(1, dr_lang('目录正常'));
} else {
$this->_json(0, dr_lang('目录[%s]不存在', $path));
}
}
public function test_cache() {
$config = new \Config\Cache();
$adapter = new $config->validHandlers[$config->handler]($config);
if (!$adapter->isSupported()) {
$this->_json(0, dr_lang('缓存方式[%s]不支持', $config->handler));
}
$adapter->initialize();
$rt = $adapter->save('test', 'phpcmf', 60);
if (!$rt) {
$this->_json(1, dr_lang('缓存方式[%s]存储失败', $config->handler));
} elseif ($adapter->get('test') == 'phpcmf') {
$this->_json(1, dr_lang('缓存方式[%s]已生效', $config->handler));
} else {
$this->_json(0, dr_lang('缓存方式[%s]未生效', $config->handler));
}
}
public function count_total() {
}
}