* testing.  This almost definitely is not a suitable mechanism for a
 * production environment, but shows how easy it is to setup TOTP.
 */
#create secret
function createSecret($secretLength = 16)
{
    $validChars = _getBase32LookupTable();
    unset($validChars[32]);
    $secret = '';
    for ($i = 0; $i < $secretLength; $i++) {
        $secret .= $validChars[array_rand($validChars)];
    }
    return $secret;
}
function getQRCode($name, $secret)
{
    global $tempDir;
    $url = 'otpauth://totp/' . $name . '?secret=' . $secret . '';
    QRcode::png($url, $tempDir . $secret . ".png", QR_ECLEVEL_L, 10);
    return $tempDir . $secret . ".png";
}
function _getBase32LookupTable()
{
    return array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '2', '3', '4', '5', '6', '7', '=');
}
echo '<h1>Hello 2factor!</h1>';
$secret = createSecret();
echo "<strong>Your secret code is</strong>: {$secret}<br/>";
echo "<strong>QR Code fun</strong>: <br />";
$qr_path = getQRCode("MattronixIDP", $secret);
echo "<img src='{$qr_path}' />";
 public function add()
 {
     $model = $this->model;
     if (IS_POST) {
         $Model = D(parse_name(get_table_name($model['id']), 1));
         // 获取模型的字段信息
         $Model = $this->checkAttr($Model, $model['id']);
         if ($Model->create()) {
             $data['token'] = get_token();
             $data['ctime'] = time();
             $data['scene_name'] = $_POST['scene_name'];
             $data['keyword'] = $_POST['keyword'];
             if ($_POST['scene_type'] == 0) {
                 $data['expire'] = $_POST['expire'] ? $_POST['expire'] : 1800;
                 $qrCode = M('scene_qrcode')->where(array('token' => get_token(), 'scene_type' => 0))->order('scene_id desc')->find();
                 if (!$qrCode['scene_id']) {
                     $data['scene_id'] = 320001;
                 } else {
                     $data['scene_id'] = intval($qrCode['scene_id']) + 1;
                 }
                 $data['scene_type'] = 0;
                 $qrCode = getQRCode($data['scene_id'], 0, $data['expire']);
                 if (!$qrCode) {
                     $this->error('未能成功创建二维码,请稍后再试~');
                     exit;
                 }
                 $data['ticket'] = $qrCode['ticket'];
                 $data['url'] = $qrCode['url'];
                 $data['short_url'] = getShortUrl(getQRUrl($qrCode['ticket']));
             }
             if ($_POST['scene_type'] == 1) {
                 if ($_POST['scene_str'] == '') {
                     // 如果没有填场景值字符串,则系统自动生成场景值ID
                     $qrCode = M('scene_qrcode')->where(array('token' => get_token(), 'scene_type' => 1))->order('scene_id desc')->find();
                     if (!$qrCode['scene_id']) {
                         $data['scene_id'] = 100001;
                     } else {
                         $data['scene_id'] = intval($qrCode['scene_id']) + 1;
                     }
                 } else {
                     // 如果填了场景值字符串,则使用场景值字符串
                     $data['scene_str'] = $_POST['scene_str'];
                 }
                 $temp_scene_id = $data['scene_id'] ? $data['scene_id'] : $data['scene_str'];
                 $data['scene_type'] = 1;
                 $qrCode = getQRCode($temp_scene_id, $data['scene_type']);
                 if (!$qrCode) {
                     $this->error('未能成功创建二维码,请稍后再试~');
                     exit;
                 }
                 $data['ticket'] = $qrCode['ticket'];
                 $data['url'] = $qrCode['url'];
                 $data['short_url'] = getShortUrl(getQRUrl($qrCode['ticket']));
             }
             $res = $Model->add($data);
             if ($res) {
                 $this->_saveKeyword($model, $id);
                 // 清空缓存
                 method_exists($Model, 'clear') && $Model->clear($id, 'add');
                 $this->success('添加' . $model['title'] . '成功!', U('lists?model=' . $model['name'], $this->get_param));
             }
         } else {
             $this->error($Model->getError());
         }
     } else {
         $fields = get_model_attribute($model['id']);
         $this->assign('fields', $fields);
         $templateFile || ($templateFile = $model['template_add'] ? $model['template_add'] : '');
         $this->display($templateFile);
     }
 }
 function text($data)
 {
     if ($data['Content'] == '功能演示') {
         // replyText("这是文本消息处理钩子里设置的文本回复");
     }
     if ($data['Content'] == '微捐赠') {
         // replyText('weijuanz');
     }
     if ($data['Content'] == 'jssdk' || $data['Content'] == 'demo' || $data['Content'] == '功能演示') {
         $params['token'] = get_token();
         $jump_url = addons_url('Example://Example/demo', $params);
         replyText('<a href="' . $jump_url . '">功能演示</a>');
     }
     if ($data['Content'] == '获取消息数组') {
         $revData = getRevData();
         replyText('消息数组:' . json_encode($revData));
     }
     if ($data['Content'] == '获取用户ID') {
         $revFrom = getRevFrom();
         replyText('用户id是:' . $revFrom);
     }
     if ($data['Content'] == '获取公众号ID') {
         $revTo = getRevTo();
         replyText('公众号id是:' . $revTo);
     }
     if ($data['Content'] == '获取消息类型') {
         $revType = getRevType();
         replyText('你发送的消息类型是:' . $revType);
     }
     if ($data['Content'] == '获取消息ID') {
         $revId = getRevId();
         replyText('你发送的消息ID是:' . $revId);
     }
     if ($data['Content'] == '获取消息发送时间') {
         $revCtime = getRevCtime();
         replyText('你发送的消息发送时间是:' . $revCtime);
     }
     if ($data['Content'] == '获取消息内容') {
         $revContent = getRevContent();
         replyText('你发送的消息内容是:' . $revContent);
     }
     if ($data['Content'] == '回复文本') {
         replyText("这是文本回复");
     }
     if ($data['Content'] == '回复单图文') {
         $articles[0] = array('Title' => '单图文消息标题', 'Description' => '单图文消息描述', 'PicUrl' => 'https://mmbiz.qlogo.cn/mmbiz/bcPR8rBzAQhibbpsMEC1WOYIaD1l85XJRWCoricvUnfricBXO9eIuPEJgB2EsucewMYWjE3dLxohzINoxoaD6R6aA/0?wx_fmt=jpeg', 'Url' => 'http://idouly.com/wenda/');
         replyNews($articles);
     }
     if ($data['Content'] == '回复多图文') {
         $articles[0] = array('Title' => '单图文消息标题1', 'Description' => '单图文消息描述1', 'PicUrl' => 'https://mmbiz.qlogo.cn/mmbiz/bcPR8rBzAQhibbpsMEC1WOYIaD1l85XJRWCoricvUnfricBXO9eIuPEJgB2EsucewMYWjE3dLxohzINoxoaD6R6aA/0?wx_fmt=jpeg', 'Url' => 'http://idouly.com/wenda/');
         $articles[1] = array('Title' => '单图文消息标题2', 'Description' => '单图文消息描述2', 'PicUrl' => 'https://mmbiz.qlogo.cn/mmbiz/bcPR8rBzAQhibbpsMEC1WOYIaD1l85XJRWCoricvUnfricBXO9eIuPEJgB2EsucewMYWjE3dLxohzINoxoaD6R6aA/0?wx_fmt=jpeg', 'Url' => 'http://idouly.com/wenda/');
         $articles[2] = array('Title' => '单图文消息标题3', 'Description' => '单图文消息描述3', 'PicUrl' => 'https://mmbiz.qlogo.cn/mmbiz/bcPR8rBzAQhibbpsMEC1WOYIaD1l85XJRWCoricvUnfricBXO9eIuPEJgB2EsucewMYWjE3dLxohzINoxoaD6R6aA/0?wx_fmt=jpeg', 'Url' => 'http://idouly.com/wenda/');
         $articles[3] = array('Title' => '单图文消息标题4', 'Description' => '单图文消息描述4', 'PicUrl' => 'https://mmbiz.qlogo.cn/mmbiz/bcPR8rBzAQhibbpsMEC1WOYIaD1l85XJRWCoricvUnfricBXO9eIuPEJgB2EsucewMYWjE3dLxohzINoxoaD6R6aA/0?wx_fmt=jpeg', 'Url' => 'http://idouly.com/wenda/');
         replyNews($articles);
     }
     if ($data['Content'] == '回复音乐') {
         replyMusic('我的歌声里', '曲婉婷', 'http://yinyueshiting.baidu.com/data2/music/5a5631832313a6007da895d12994e311/255967781/255704757248400128.mp3?xcode=67dd7bdbf2d520d5bd5cc648288db746');
     }
     if ($data['Content'] == '转接客服') {
         transferCustomerService();
     }
     if ($data['Content'] == '获取菜单') {
         $menu = getMenu();
         replyText(json_encode($menu));
     }
     if ($data['Content'] == '删除菜单') {
         if (deleteMenu()) {
             replyText('删除菜单成功');
         }
     }
     if ($data['Content'] == '创建菜单') {
         $btn1 = array('name' => 'button1', 'type' => 'click', 'key' => 'button1_key');
         $data['button'][0] = $btn1;
         if (createMenu($data)) {
             replyText('创建菜单成功');
         } else {
             replyText('创建菜单失败');
         }
     }
     if ($data['Content'] == '发送模板消息') {
         $temp_data['touser'] = getRevFrom();
         $temp_data['template_id'] = 'A93dk7bWn9W4oSvM7UcXm95X-WehNeLeegRq4RaKF8g';
         $temp_data['url'] = 'http://idouly.com/wenda/';
         $temp_data['topcolor'] = '#44b549';
         $temp_data['data']['first']['value'] = '有新的用户捐赠,请注意查收';
         $temp_data['data']['first']['color'] = '#da4224';
         $temp_data['data']['keyword1']['value'] = '用户捐赠通知';
         $temp_data['data']['keyword1']['color'] = '#0baae4';
         $temp_data['data']['keyword2']['value'] = '2015年1月7日';
         $temp_data['data']['keyword2']['color'] = '#0baae4';
         $temp_data['data']['remark']['value'] = '用户昵称:艾逗笔,捐赠额:66元';
         $temp_data['data']['remark']['color'] = '#333';
         //replyText(json_encode($temp_data));
         sendTemplateMessage($temp_data);
         // replyText(json_encode($temp_data));
         exit;
     }
     if ($data['Content'] == '发送文本客服消息') {
         $kf_data['touser'] = getRevFrom();
         $kf_data['msgtype'] = 'text';
         $kf_data['text']['content'] = '你好啊,亲~';
         //replyText(json_encode($kf_data));
         sendCustomMessage($kf_data);
         exit;
     }
     if ($data['Content'] == '发送图文客服消息') {
         $articles[0] = array('title' => '单图文消息标题1', 'description' => '单图文消息描述1', 'picurl' => 'https://mmbiz.qlogo.cn/mmbiz/bcPR8rBzAQhibbpsMEC1WOYIaD1l85XJRWCoricvUnfricBXO9eIuPEJgB2EsucewMYWjE3dLxohzINoxoaD6R6aA/0?wx_fmt=jpeg', 'url' => 'http://idouly.com/wenda/');
         $articles[1] = array('title' => '单图文消息标题2', 'description' => '单图文消息描述2', 'picurl' => 'https://mmbiz.qlogo.cn/mmbiz/bcPR8rBzAQhibbpsMEC1WOYIaD1l85XJRWCoricvUnfricBXO9eIuPEJgB2EsucewMYWjE3dLxohzINoxoaD6R6aA/0?wx_fmt=jpeg', 'url' => 'http://idouly.com/wenda/');
         $articles[2] = array('title' => '单图文消息标题3', 'description' => '单图文消息描述3', 'picurl' => 'https://mmbiz.qlogo.cn/mmbiz/bcPR8rBzAQhibbpsMEC1WOYIaD1l85XJRWCoricvUnfricBXO9eIuPEJgB2EsucewMYWjE3dLxohzINoxoaD6R6aA/0?wx_fmt=jpeg', 'url' => 'http://idouly.com/wenda/');
         $articles[3] = array('title' => '单图文消息标题4', 'description' => '单图文消息描述4', 'picurl' => 'https://mmbiz.qlogo.cn/mmbiz/bcPR8rBzAQhibbpsMEC1WOYIaD1l85XJRWCoricvUnfricBXO9eIuPEJgB2EsucewMYWjE3dLxohzINoxoaD6R6aA/0?wx_fmt=jpeg', 'url' => 'http://idouly.com/wenda/');
         $kf_data['touser'] = getRevFrom();
         $kf_data['msgtype'] = 'news';
         $kf_data['news']['articles'] = $articles;
         //replyText(json_encode($kf_data));
         sendCustomMessage($kf_data);
         exit;
     }
     if ($data['Content'] == '获取二维码') {
         $qrCode = getQRCode(88, 2);
         replyText(json_encode($qrCode));
     }
 }