Skip to content

Commit

Permalink
add photowipe and captcha
Browse files Browse the repository at this point in the history
  • Loading branch information
skywalker512 committed Jul 12, 2018
1 parent 52e1ab7 commit f7f33a8
Show file tree
Hide file tree
Showing 27 changed files with 5,484 additions and 2 deletions.
2 changes: 1 addition & 1 deletion addons/languages/Chinese/definitions.php
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@
$definitions["message.passwordsDontMatch"] = "密码不正确!";
$definitions["message.passwordTooShort"] = "密码太短了,至少要6位!";
$definitions["message.pluginCannotBeEnabled"] = "插件<em>%s</em>不能正常开启: %s";
$definitions["message.pluginDependencyNotMet"] = "你必须升级到 %s 版本 %s 才能安装并使用这个插件.";
$definitions["message.pluginDependencyNotMet"] = "你必须安装并启用 %s,并且版本必须大于 %s 才能启用目前的插件.";
$definitions["message.pluginUninstalled"] = "卸载成功.";
$definitions["message.postNotFound"] = "找不到相关内容";
$definitions["message.postTooLong"] = "你说得太多了,已经超过了最大字符数了, 最多支持%s字符. ";
Expand Down
76 changes: 76 additions & 0 deletions addons/plugins/Captcha/CaptchaController.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

if (!defined("IN_ESOTALK")) exit;

class CaptchaController extends ETController {
public function action_test()
{
$this->form = $form = ETFactory::make('form');
$form->action = URL('captcha/test');
$this->data('form', $form);

if ( $form->validPostBack('captcha') ) {
if ( ETPlugin_Captcha::verifyCode($form->getValue('captcha')) ) {
$this->message('Success', 'success');
} else {
$this->message('Failed', 'warning');
}

$this->render('captcha/test');
} else {
$this->render('captcha/test');
}
}

// set captcha and show image
public function action_index($is2x = false) {
$fontSize = 32 * ($is2x ? 2 : 1);
$fontColor = '666666';

$code = ETPlugin_Captcha::generateCode();
$path = dirname(__FILE__) . '/resources';

$background = $is2x ? "$path/[email protected]" : "$path/bg.png";
$font = "$path/font.ttf";

// set up
list($width, $height) = getimagesize($background);
$img = imagecreatefrompng($background);

list($r, $g, $b) = $this->hex2rgb($fontColor);
$color = imagecolorallocate($img, $r, $g, $b);

// center text
list($x1, $y1, , , $x2, $y2) = imagettfbbox($fontSize, /*angle*/0, $font, $code);
imagettftext($img, $fontSize, /*angle*/0, $width/2 + ($x1-$x2)/2, $height/2 + ($y1-$y2)/2, $color, $font, $code);

// invert half image
$invert = imagecreatetruecolor($width, $height);
imagecopy($invert, $img, 0, 0, 0, 0, $width, $height);
imagefilter($invert, IMG_FILTER_NEGATE);
imagecopy($img, $invert, 0, 0, 0, 0, $width/2, $height);
imagedestroy($invert);

// output
header('Cache-Control: no-cache, must-revalidate');
header('Content-Type: image/png');
imagepng($img);
imagedestroy($img);
}

// retina support
public function action_2x()
{
$this->action_index(true);
}


private function hex2rgb($hex) {
$value = hexdec($hex);
return array(
0xFF & ($value >> 0x10),
0xFF & ($value >> 0x8),
0xFF & $value,
);
}
}
Binary file added addons/plugins/Captcha/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file.
105 changes: 105 additions & 0 deletions addons/plugins/Captcha/plugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php

if (!defined('IN_ESOTALK')) exit;

ET::$pluginInfo['Captcha'] = array(
'name' => '图形验证码',
'description' => '在用户注册时要求用户识别图形验证码',
'version' => '0.1.0',
'author' => 'rhyzx',
'authorEmail' => '[email protected]',
'authorURL' => 'https://3dgundam.org',
'license' => 'MIT',
'priority' => 0,
);

class ETPlugin_Captcha extends ETPlugin {

public function __construct($rootDirectory)
{
parent::__construct($rootDirectory);
ETFactory::registerController('captcha', 'CaptchaController', dirname(__FILE__).'/CaptchaController.class.php');
}

public function handler_renderBefore($sender)
{
$sender->addCSSFile($this->resource('captcha.css'));
$sender->addJSFile($this->resource('captcha.js'));
}

public function handler_userController_initJoin($sender, $form)
{
if ($this->skipCaptcha()) return;
$form->addSection('captcha', '验证码');
$form->addField('captcha', 'captcha', function($form) use ($sender)
{
return $sender->getViewContents($this->view('captcha/captcha'), array('form' => $form, 'tips' => true));
},
function($form, $key, &$data) use ($sender)
{
if ( !self::verifyCode($form->getValue($key)) ) {
$form->error($key, '验证码错误');
}
});
}


public function handler_conversationController_renderFormButtonsAfter($sender, &$content, $form, $conversation)
{
if ($this->skipCaptcha()) return;
$result = $sender->getViewContents($this->view('captcha/captcha'), array('form' => $form, 'tabindex' => 290));
addToArray($content, $result, 0);
}

public function handler_conversationController_reply($sender, $form)
{
if ($this->skipCaptcha()) return;
$form->addField('captcha', 'captcha', null,
function($form, $key, &$data) use ($sender)
{
if ( !self::verifyCode($form->getValue($key)) ) {
$form->error($key, '验证码错误');
}
});
}
public function handler_conversationController_start($sender, $form)
{
if ($this->skipCaptcha()) return;
$form->addField('captcha', 'captcha', null,
function($form, $key, &$data) use ($sender)
{
if ( !self::verifyCode($form->getValue($key)) ) {
$form->error($key, '验证码错误');
}
});
}


private function skipCaptcha()
{
if (ET::$session->user && ET::$session->user['countPosts'] >= 10) {
return true;
}
return false;
}


public static function verifyCode($code = '') {
$code = strtoupper($code);
return ET::$session->get('plugin_captcha') === $code;
}
public static function generateCode() {
static $codes = 'ABCDEFGHIJKLMNPQRSTUVWXYZ01245678';
$count = strlen($codes);

$code = '';
for ($i=0; $i < 4; $i++) {
$code .= substr($codes, rand() % $count, 1);
}

// case insensitive
$code = strtoupper($code);
ET::$session->store('plugin_captcha', $code);
return $code;
}
}
Binary file added addons/plugins/Captcha/resources/bg.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 22 additions & 0 deletions addons/plugins/Captcha/resources/captcha.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
.sheet .buttons .captcha {
float: right;
margin-left: 10px;
}
.captcha img {
width: 102px;
height: 34px;
vertical-align: middle;
cursor: pointer;
}
.captcha img.loading {
cursor: wait;
background-position: center center;
box-shadow: inset 0 0 1px rgba(0,0,0,.5);
}
.captcha input {
padding: 7px;
width: 72px;
}
.fieldGroup .captcha input {
width: 154px;
}
55 changes: 55 additions & 0 deletions addons/plugins/Captcha/resources/captcha.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// reload captcha
void function () {

$(document).off('click.captcha')
.on('click.captcha', '.captcha img:not(.loading)', function () {
reload($(this))
})

if (typeof ETConversation !== 'undefined') {
var startConversation = ETConversation.startConversation
ETConversation.startConversation = function () {
startConversation.apply(startConversation, arguments)
.done(function (res) {
if (typeof res.redirect !== 'undefined') return
reload($('#reply .captcha img'))
$('#reply .captcha input').val('')
})
.fail(function () {
reload($('#reply .captcha img'))
$('#reply .captcha input').val('')
})
}

var addReply = ETConversation.addReply
ETConversation.addReply = function () {
addReply.apply(addReply, arguments)
.always(function () {
reload($('#reply .captcha img'))
$('#reply .captcha input').val('')
})
}
}


function reload($img) {
var t = $.now()

$img
.addClass('loading')
.one('load error', function () {
$img.removeClass('loading')
})
// remove attr to show loading image
.removeAttr('src')
.removeAttr('srcset')
.attr('src', '/captcha?t=' + t)
.attr('srcset', '/captcha/2x?t=' + t + ' 2x')
}


// tooltip
$(function () {
$('.captcha img').tooltip()
})
}();
Binary file added addons/plugins/Captcha/resources/font.ttf
Binary file not shown.
11 changes: 11 additions & 0 deletions addons/plugins/Captcha/views/captcha/captcha.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php
if (!defined('IN_ESOTALK')) exit;
$form = $data['form'];
?>
<div class="captcha">
<img src="/captcha" srcset="/captcha/2x 2x" alt="" width="102" height="34" title="点击更换验证码" role="button">
<?php echo $form->input('captcha', 'text', array('placeholder' => '输入验证码', 'value' => '', 'tabindex' => $data['tabindex'] )) ?>
<?php if ($data['tips']): ?>
<br><small>请输入图片中的验证码</small>
<?php endif ?>
</div>
19 changes: 19 additions & 0 deletions addons/plugins/Captcha/views/captcha/test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
if (!defined("IN_ESOTALK")) exit;
$form = $data["form"];
?>

<div class="sheet">
<div class="sheetContent">
<h3>全自动人机区别图灵测试</h3>
<?php echo $form->open(); ?>
<div class="buttons">
<button type="submit" class="submit button big">Test</button>
<span class="captcha">
<img src="/captcha" srcset="/captcha/2x 2x" alt="" width="102" height="34">
<input type="text" name="captcha" value="" placeholder="输入验证码">
</span>
</div>
<?php echo $form->close(); ?>
</div>
</div>
Binary file added addons/plugins/Photoswipe/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file.
33 changes: 33 additions & 0 deletions addons/plugins/Photoswipe/plugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

if (!defined("IN_ESOTALK")) exit;

ET::$pluginInfo["Photoswipe"] = array(
"name" => "Photoswipe",
"description" => "允许用户点击帖子中的图片,形成图片画廊样式,与 Upyun 结合使用",
'version' => '0.1.0',
'author' => 'rhyzx',
'authorEmail' => '[email protected]',
'authorURL' => 'https://3dgundam.org',
'license' => 'MIT',
"priority" => "0",
'dependencies' => array(
'Upyun' => '0',
)
);

class ETPlugin_Photoswipe extends ETPlugin {

public function handler_conversationController_renderBefore($sender){
$PhotoswipeCSS= "<link rel=\"stylesheet\" href=\"//cdn.bootcss.com/photoswipe/4.1.2/photoswipe.min.css\">
<link rel=\"stylesheet\" href=\"//cdn.bootcss.com/photoswipe/4.1.2/default-skin/default-skin.min.css\">";
$PhotoswipeJS = "<script src=\"//cdn.bootcss.com/photoswipe/4.1.2/photoswipe.min.js\"></script>
<script src=\"//cdn.bootcss.com/photoswipe/4.1.2/photoswipe-ui-default.min.js\"></script>
";

$sender->addToHead($PhotoswipeCSS);
$sender->addToHead($PhotoswipeJS);
$sender->addCSSFile($this->resource("photoswipe.css"));
$sender->addJSFile($this->resource("photoswipe.js"));
}
}
4 changes: 4 additions & 0 deletions addons/plugins/Photoswipe/resources/photoswipe.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.link-external > img + i:not(.icon-external-link),
.link-external > img + i:not(.icon-external-link) + .icon-external-link {
display: none;
}
Loading

0 comments on commit f7f33a8

Please sign in to comment.