typeho是一款內(nèi)核強健﹑擴展方便﹑體驗友好﹑運行流暢的輕量級開源博客程序。僅僅 7 張數(shù)據(jù)表,加上不足 400KB 的代碼,就實現(xiàn)了完整的插件與模板機制。超低的 CPU 和內(nèi)存使用率,足以發(fā)揮主機的最高性能。今天小編為大家講解一下typehoV1.2這個版本的短信插件該如何開發(fā),短信接口使用的是我們短信寶短信群發(fā)平臺,我們短信寶短信群發(fā)平臺極其穩(wěn)定,而且短信發(fā)送速度相當(dāng)快捷,驗證碼和訂單通知在3~5秒就能收到,用戶體驗非常好,注冊就送測試短信。
插件的目錄結(jié)構(gòu)如下:
├─SmsBaoSms插件目錄
│ ├─captcha 字體文件目錄
│ │ ├─resources
│ │ │ ├─fonts
│ │ │ ├─tools
│ │ │ ├─words
│ │ ├─ captcha.php
│ ├─page_regbysms.php 注冊界面
│ ├─Plugin.php 插件類
│ ├─register.php 登錄界面
│ ├─sendsms.php 短信發(fā)送類
下面具體給大家說一下每個文件的作用及代碼 Plugin.php是插件的安裝文件,代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
<?php /** * Typecho版本手機注冊插件 * @package SmsBaoSms For Typecho * @author 短信寶 * @version 1.0.1 * @link http://www.gjrencai.com/ */ class SmsBaoSms_Plugin implements Typecho_Plugin_Interface { // 激活插件 public static function activate(){ return _t( '插件已經(jīng)激活,需先配置信息!' ); } // 禁用插件 public static function deactivate(){ //恢復(fù)原注冊頁面 if (copy(dirname(__FILE__). '/register.php' ,dirname(__FILE__). '/../../../' .substr(__TYPECHO_ADMIN_DIR__,1,count(__TYPECHO_ADMIN_DIR__)-2). '/register.php' )){ } return _t( '插件已被禁用' ); } // 插件配置面板 public static function config(Typecho_Widget_Helper_Form $form){ //表單驗證 $alidayukey = new Typecho_Widget_Helper_Form_Element_Text( 'alidayukey' , null , '' , _t( '短信寶賬號:' )); $form->addInput($alidayukey->addRule( 'required' , _t( '短信寶賬號不能為空!' ))); $alidayusecret = new Typecho_Widget_Helper_Form_Element_Text( 'alidayusecret' , null , '' , _t( '短信寶密碼:' )); $form->addInput($alidayusecret->addRule( 'required' , _t( '短信寶密碼不能為空!' ))); $signname = new Typecho_Widget_Helper_Form_Element_Text( 'signname' , null , '' , _t( '短信寶簽名:' )); $form->addInput($signname->addRule( 'required' , _t( '簽名名稱不能為空!' ))); $templatecode = new Typecho_Widget_Helper_Form_Element_Text( 'templatecode' , null , '' , _t( '短信寶模版CODE:' )); $form->addInput($templatecode->addRule( 'required' , _t( '模版CODE不能為空!' ))); $isindex = new Typecho_Widget_Helper_Form_Element_Radio( 'isindex' , array( 'y' =>_t( '存在' ), 'n' =>_t( '不存在' ) ), 'y' , _t( '存在index.php' ), _t( "前臺文章url是否存在index.php:" )); $form->addInput($isindex->addRule( 'enum' , _t( '' ), array( 'y' , 'n' ))); $alidayukey = @isset($_POST[ 'alidayukey' ]) ? addslashes(trim($_POST[ 'alidayukey' ])) : '' ; $isindex = @isset($_POST[ 'isindex' ]) ? addslashes(trim($_POST[ 'isindex' ])) : '' ; if ($alidayukey!= '' ){ //$option = self::getConfig(); $db = Typecho_Db::get(); //判斷目錄權(quán)限 $queryTheme= $db->select( 'value' )->from( 'table.options' )->where( 'name = ?' , 'theme' ); $rowTheme = $db->fetchRow($queryTheme); if (!is_writable(dirname(__FILE__). '/../../themes/' .$rowTheme[ 'value' ])){ Typecho_Widget::widget( 'Widget_Notice' )->set(_t( '主題目錄不可寫,請更改目錄權(quán)限。' .__TYPECHO_THEME_DIR__. '/' .$rowTheme[ 'value' ]), 'success' ); } if (!is_writable(dirname(__FILE__). '/../../../' .substr(__TYPECHO_ADMIN_DIR__,1,count(__TYPECHO_ADMIN_DIR__)-2). '/register.php' )){ Typecho_Widget::widget( 'Widget_Notice' )->set(_t( '后臺目錄不可寫,請更改目錄權(quán)限。' .__TYPECHO_ADMIN_DIR__. 'register.php' ), 'success' ); } //如果數(shù)據(jù)表沒有添加注冊頁面就插入 $query= $db->select( 'slug' )->from( 'table.contents' )->where( 'template = ?' , 'page_regbysms.php' ); $row = $db->fetchRow($query); if (count($row)==0){ $contents = array( 'title' => '注冊用戶' , 'slug' => 'reg' , 'created' => Typecho_Date::time(), 'text' => '<!--markdown-->' , 'password' => '' , 'authorId' => Typecho_Cookie::get( '__typecho_uid' ), 'template' => 'page_regbysms.php' , 'type' => 'page' , 'status' => 'hidden' , ); $insert = $db->insert( 'table.contents' )->rows($contents); $insertId = $db->query($insert); $slug=$contents[ 'slug' ]; } else { $slug=$row[ 'slug' ]; } //如果page_regbysms.php不存在就創(chuàng)建 if (!file_exists(dirname(__FILE__). '/../../themes/' .$rowTheme[ 'value' ]. "/page_regbysms.php" )){ $regfile = fopen(dirname(__FILE__). "/page_regbysms.php" , "r" ) or die( "不能讀取page_regbysms.php文件" ); $regtext=fread($regfile,filesize(dirname(__FILE__). "/page_regbysms.php" )); fclose($regfile); $regpage = fopen(dirname(__FILE__). '/../../themes/' .$rowTheme[ 'value' ]. "/page_regbysms.php" , "w" ) or die( "不能寫入page_regbysms.php文件" ); fwrite($regpage, $regtext); fclose($regpage); } //將跳轉(zhuǎn)新注冊頁面的鏈接寫入原register.php $querySiteUrl= $db->select( 'value' )->from( 'table.options' )->where( 'name = ?' , 'siteUrl' ); $rowSiteUrl = $db->fetchRow($querySiteUrl); if ($isindex== 'y' ){ $siteUrl=$rowSiteUrl[ 'value' ]. '/index.php/' .$slug. '.html' ; } else { $siteUrl=$rowSiteUrl[ 'value' ]. '/' .$slug. '.html' ; } $registerphp= ' <?php include "common.php"; if ($user->hasLogin() || !$options->allowRegister) { $response->redirect($options->siteUrl); }else{ header("Location: ' .$siteUrl. '"); } ?> ' ; $regphp = fopen(dirname(__FILE__). '/../../../' .substr(__TYPECHO_ADMIN_DIR__,1,count(__TYPECHO_ADMIN_DIR__)-2). '/register.php' , "w" ) or die( "不能寫入register.php文件" ); fwrite($regphp, $registerphp); fclose($regphp); } } // 個人用戶配置面板 public static function personalConfig(Typecho_Widget_Helper_Form $form){} // 獲得插件配置信息 public static function getConfig(){ return Typecho_Widget::widget( 'Widget_Options' )->plugin( 'SmsBaoSms' ); } } |
2:page_regbysms.php為注冊界面 代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
|
<?php /** * 手機注冊頁面 * * @package custom */ ?> <?php session_start(); if (!defined( '__TYPECHO_ROOT_DIR__' )) exit; ?> <?php $db = Typecho_Db::get(); $action = isset($_POST[ 'action' ]) ? addslashes(trim($_POST[ 'action' ])) : '' ; /** 如果已經(jīng)登錄 */ if ($ this ->user->hasLogin()) { /** 直接返回 */ $ this ->response->redirect($ this ->options->index); } if ($action== 'regbysms' ){ $name = isset($_POST[ 'name' ]) ? addslashes(trim($_POST[ 'name' ])) : '' ; $code = isset($_POST[ 'code' ]) ? addslashes(trim($_POST[ 'code' ])) : '' ;; if ($name&&$code){ $sessionCode = isset($_SESSION[ 'code' ]) ? $_SESSION[ 'code' ] : '' ; if (strcasecmp($code,$sessionCode)==0){ $query= $db->select( 'uid' )->from( 'table.users' )->where( 'name = ?' , $name); $user = $db->fetchRow($query); if ($user){ /*登錄*/ $authCode = function_exists( 'openssl_random_pseudo_bytes' ) ? bin2hex(openssl_random_pseudo_bytes(16)) : sha1(Typecho_Common::randString(20)); $user[ 'authCode' ] = $authCode; Typecho_Cookie::set( '__typecho_uid' , $user[ 'uid' ], 0); Typecho_Cookie::set( '__typecho_authCode' , Typecho_Common::hash($authCode), 0); /*更新最后登錄時間以及驗證碼*/ $db->query($db ->update( 'table.users' ) ->expression( 'logged' , 'activated' ) ->rows(array( 'authCode' => $authCode)) ->where( 'uid = ?' , $user[ 'uid' ])); /*壓入數(shù)據(jù)*/ $ this ->push($user); $ this ->_user = $user; $ this ->_hasLogin = true ; $ this ->pluginHandle()->loginSucceed($ this , $name, '' , false ); /*重置短信驗證碼*/ $randCode = '' ; $chars = 'abcdefghijkmnpqrstuvwxyzABCDEFGHIJKLMNPRSTUVWXYZ23456789' ; for ( $i = 0; $i < 5; $i++ ){ $randCode .= substr($chars, mt_rand(0, strlen($chars) - 1), 1); } $_SESSION[ 'code' ] = strtoupper($randCode); $ this ->widget( 'Widget_Notice' )->set(_t( '用戶已存在,已為您登錄 ' ), 'success' ); /*跳轉(zhuǎn)驗證后地址*/ if (NULL != $ this ->request->referer) { $ this ->response->redirect($ this ->request->referer); } else if (!$ this ->user->pass( 'contributor' , true )) { /*不允許普通用戶直接跳轉(zhuǎn)后臺*/ $ this ->response->redirect($ this ->options->profileUrl); } else { $ this ->response->redirect($ this ->options->adminUrl); } } else { /*注冊*/ /** 如果已經(jīng)登錄 */ if ($ this ->user->hasLogin() || !$ this ->options->allowRegister) { /** 直接返回 */ $ this ->response->redirect($ this ->options->index); } $hasher = new PasswordHash(8, true ); $generatedPassword = Typecho_Common::randString(7); $dataStruct = array( 'name' => $name, 'mail' => $name. '@tongleer.com' , 'screenName' => $name, 'password' => $hasher->HashPassword($generatedPassword), 'created' => $ this ->options->time, 'group' => 'subscriber' ); $insert = $db->insert( 'table.users' )->rows($dataStruct); $insertId = $db->query($insert); $ this ->pluginHandle()->finishRegister($ this ); $ this ->user->login($name, $generatedPassword); Typecho_Cookie:: delete ( '__typecho_first_run' ); /*重置短信驗證碼*/ $randCode = '' ; $chars = 'abcdefghijkmnpqrstuvwxyzABCDEFGHIJKLMNPRSTUVWXYZ23456789' ; for ( $i = 0; $i < 5; $i++ ){ $randCode .= substr($chars, mt_rand(0, strlen($chars) - 1), 1); } $_SESSION[ 'code' ] = strtoupper($randCode); $ this ->widget( 'Widget_Notice' )->set(_t( '用戶 <strong>%s</strong> 已經(jīng)成功注冊, 密碼為 <strong>%s</strong>' , $ this ->screenName, $generatedPassword), 'success' ); $ this ->response->redirect($ this ->options->adminUrl); } } else { echo '<script>alert("驗證碼錯誤!");</script>' ; } } } ?> <?php $ this ->need( 'header.php' ); ?> <link rel= "stylesheet" href= "//cdn.bootcss.com/mdui/0.4.1/css/mdui.min.css" xmlns= "http://www.w3.org/1999/html" > <script src= "//cdn.bootcss.com/mdui/0.4.1/js/mdui.min.js" ></script> <script src= "//libs.baidu.com/jquery/2.1.4/jquery.min.js" ></script> <!-- content section --> <section> <div class= "mdui-shadow-10 mdui-center" style= "width:300px;" > <div class= "mdui-typo mdui-valign mdui-color-blue mdui-text-color-white" > <h6 class= "mdui-center" >用戶注冊</h6> </div> <form action= "" method= "post" class= "mdui-p-x-1 mdui-p-y-1" > <div class= "mdui-textfield mdui-textfield-floating-label" > <label class= "mdui-textfield-label" ><?php _e( '手機號' ); ?></label> <input class= "mdui-textfield-input" id= "name" name= "name" type= "text" required value= "<?php echo @$_POST['name']; ?>" /> <div class= "mdui-textfield-error" >手機號不能為空</div> </div> <div class= "mdui-textfield mdui-textfield-floating-label" > <label class= "mdui-textfield-label" ><?php _e( '圖形驗證碼' ); ?></label> <input class= "mdui-textfield-input" id= "CAPTCHA" style= "width:50%;*float:left;" class= "input" type= "text" size= "10" value= "" name= "captcha_code" > </input> <div class= "mdui-textfield-error" >圖形驗證碼不能為空</div> <img style= "150px;" id= "captcha_img" src= "<?php $this->options->siteUrl(); ?>usr/plugins/SmsBaoSms/captcha/captcha.php" title= "看不清?點擊更換" alt= "看不清?點擊更換" onclick= "document.getElementById('captcha_img').src='<?php $this->options->siteUrl(); ?>usr/plugins/SmsBaoSms/captcha/captcha.php?'+Math.random();document.getElementById('CAPTCHA').focus();return false;" /> <a href= "javascript:void(0)" onclick= "document.getElementById('captcha_img').src='<?php $this->options->siteUrl(); ?>usr/plugins/SmsBaoSms/captcha/captcha.php?'+Math.random();document.getElementById('CAPTCHA').focus();return false;" >點擊更換</a> </div> <div class= "mdui-textfield mdui-textfield-floating-label" > <label class= "mdui-textfield-label" ><?php _e( '手機驗證碼' ); ?></label> <input class= "mdui-textfield-input" id= "code" name= "code" type= "text" required value= "<?php echo @$_POST['code']; ?>" /> <div class= "mdui-textfield-error" >手機驗證碼不能為空</div> </div> <div class= "mdui-row-xs-2" > <div id= "smsmsg" class= "mdui-col" > <button id= "sendsmsmsg" class= "mdui-btn mdui-color-blue mdui-text-color-white" >發(fā)送驗證碼</button> </div> <div class= "mdui-col" > <input type= "hidden" id= "sitetitle" value= "<?php $this->options->title();?>" /> <input type= "hidden" name= "action" value= "regbysms" /> <button id= "reg" class= "mdui-btn mdui-btn-block mdui-btn-raised mdui-color-theme-accent mdui-ripple mdui-color-blue mdui-text-color-white" ><?php _e( '注冊' ); ?></button> </div> </div> </form> </div> </section> <!-- end content section --> <?php $ this ->need( 'footer.php' ); ?> <script> $( "#sendsmsmsg" ).click( function (){ var name=$( "#name" ).val(); var regexp = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/; var captcha = $( "#CAPTCHA" ).val(); if (!regexp.test(name)){ alert( '請輸入有效的手機號碼!' ); return false ; } // settime(); $.post( "<?php $this->options->siteUrl(); ?>usr/plugins/SmsBaoSms/sendsms.php" ,{name:name,sitetitle:$( '#sitetitle' ).val(),captcha:captcha}, function (data){ if (data== 'toofast' ){ alert( '發(fā)送頻率太快了~' ); } else if (data== 'captchaempty' ){ alert( '圖形驗證碼有誤~' ); } else if (data == 'captchafalse' ){ alert( '圖形驗證碼有誤~' ); } else if (data == 'success' ){ alert( '發(fā)送成功~' ); clearTimeout(timer); settime(); } else { alert( '發(fā)送失敗' ); } }); }); $( "#reg" ).click( function (e){ var name=$( "#name" ).val(); var regexp = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/; if (!regexp.test(name)){ alert( '請輸入有效的手機號碼!' ); return ; } var yzm = $( "input[name=code]" ).val().replace(/(^\s*)|(\s*$)/g, "" ); if (yzm== "" ){ alert( "請輸入短信驗證碼" ); return ; } $( 'form' ).submit(); }); var timer; var countdown=60; function settime() { if (countdown == 0) { $( "#smsmsg" ).html( "<button id='sendsmsmsg' class='mdui-btn mdui-btn-raised mdui-color-blue mdui-text-color-white'>發(fā)送驗證碼</button>" ); countdown = 60; clearTimeout(timer); return ; } else { $( "#smsmsg" ).html( "等待(" +countdown+ ")秒" ); countdown--; } timer=setTimeout( function () { settime() },1000) } </script> |
3:register.php為登錄界面 代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
<?php include 'common.php' ; if ($user->hasLogin() || !$options->allowRegister) { $response->redirect($options->siteUrl); } $rememberName = htmlspecialchars(Typecho_Cookie::get( '__typecho_remember_name' )); $rememberMail = htmlspecialchars(Typecho_Cookie::get( '__typecho_remember_mail' )); Typecho_Cookie:: delete ( '__typecho_remember_name' ); Typecho_Cookie:: delete ( '__typecho_remember_mail' ); $bodyClass = 'body-100' ; include 'header.php' ; ?> <div class= "typecho-login-wrap" > <div class= "typecho-login" > <form action= "<?php $options->registerAction(); ?>" method= "post" name= "register" role= "form" > <p> <label for = "name" class= "sr-only" ><?php _e( '用戶名' ); ?></label> <input type= "text" id= "name" name= "name" placeholder= "<?php _e('用戶名'); ?>" value= "<?php echo $rememberName; ?>" class= "text-l w-100" autofocus /> </p> <p> <label for = "mail" class= "sr-only" ><?php _e( 'Email' ); ?></label> <input type= "email" id= "mail" name= "mail" placeholder= "<?php _e('Email'); ?>" value= "<?php echo $rememberMail; ?>" class= "text-l w-100" /> </p> <p class= "submit" > <button type= "submit" class= "btn btn-l w-100 primary" ><?php _e( '注冊' ); ?></button> </p> </form> <p class= "more-link" > <a href= "<?php $options->siteUrl(); ?>" ><?php _e( '返回首頁' ); ?></a> • <a href= "<?php $options->adminUrl('login.php'); ?>" ><?php _e( '用戶登錄' ); ?></a> </p> </div> </div> <?php include 'common-js.php' ; ?> <script> $(document).ready( function () { $( '#name' ).focus(); }); </script> <?php include 'footer.php' ; ?> |
4:sendsms.php 為短信寶發(fā)送類文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
<?php session_start(); date_default_timezone_set( 'Asia/Shanghai' ); include '../../../config.inc.php' ; if (@$_COOKIE[ "sendcodetime" ]!= '' ){ echo 'toofast' ; return ; } //驗證圖形驗證碼 $captcha = $_POST[ 'captcha' ]; if (empty($captcha) || empty($_SESSION[ 'sms_code' ])) { echo 'captchaempty' ; return ; } else if ((trim(strtolower($captcha)) != $_SESSION[ 'sms_code' ])) { echo 'captchafalse' ; return ; } else { unset($_SESSION[ 'sms_code' ]); setcookie( "sendcodetime" , time(), time()+10); $query= $db->select( 'value' )->from( 'table.options' )->where( 'name = ?' , 'plugin:SmsBaoSms' ); $row = $db->fetchRow($query); $arr=explode( ':' ,$row[ 'value' ]); $appkeystr=$arr[6]; $secretstr=$arr[10]; $aliCode=$arr[18]; $signname=$arr[14]; $appkey=substr($appkeystr,1,count($appkeystr)-4); $secret=substr($secretstr,1,count($secretstr)-4); $aliCode=substr($aliCode,1,count($aliCode)-4); $signname=substr($signname,1,count($signname)-4); //重置短信驗證碼 $randCode = rand(10000,99999); $content = '【' .$signname. '】' .str_replace( '{$code}' ,$randCode,$aliCode); $_SESSION[ 'code' ] = $randCode; $name = isset($_POST[ 'name' ]) ? addslashes(trim($_POST[ 'name' ])) : '' ; //發(fā)送到的用戶名 $sitetitle = isset($_POST[ 'sitetitle' ]) ? addslashes(trim($_POST[ 'sitetitle' ])) : '' ; $user = $appkey; //短信平臺帳號 $pass = md5($secret); //短信平臺密碼 $content = $content; //要發(fā)送的短信內(nèi)容 $phone = $name; //要發(fā)送短信的手機號碼 $sendurl = $smsapi. "sms?u=" .$user. "&p=" .$pass. "&m=" .$phone. "&c=" .urlencode($content); $result = file_get_contents($sendurl); if ($result == '0' ){ echo 'success' ; } else { echo 'false' ; } // echo $_SESSION['code']; } ?> |
captcha文件下載鏈接 http://www.gjrencai.com/download/captcha.zip
經(jīng)過上面的替換,短信寶的短信平臺已經(jīng)替換成功了,可以正常使用了。進行測試發(fā)送:
報備一下短信寶的VIP模板,這樣就可以走短信寶的優(yōu)質(zhì)通道了,即便遇到敏感文字我們都不會人工審核,短信內(nèi)容3~5秒就可送達。
另外:我們已經(jīng)開發(fā)好完整的typeho系統(tǒng)短信寶插件,點擊此鏈接 下載及查看安裝流程。
最新更新
電商類
CMS類
微信類