-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
284 changed files
with
129,726 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
namespace moxuandi\ueditor; | ||
|
||
use Yii; | ||
use yii\helpers\Html; | ||
use yii\helpers\Json; | ||
use yii\helpers\Url; | ||
use yii\web\View; | ||
use yii\widgets\InputWidget; | ||
|
||
/** | ||
* UEditor renders a editor js plugin for classic editing. | ||
* | ||
* @author zhangmoxuan <[email protected]> | ||
* @link http://www.zhangmoxuan.com | ||
* @QQ 1104984259 | ||
* @Date 2017/7/14 | ||
* @see http://ueditor.baidu.com/website/ | ||
*/ | ||
class UEditor extends InputWidget | ||
{ | ||
/** | ||
* 配置接口, 参阅 UEditor 官方文档(http://fex.baidu.com/ueditor/#start-config) | ||
* @see yii2-ueditor/assets/ueditor.config.js | ||
*/ | ||
public $clientOptions = []; | ||
|
||
|
||
public function init() | ||
{ | ||
//$this->id = $this->hasModel() ? Html::getInputId($this->model, $this->attribute) : $this->id; | ||
if($this->hasModel()){ | ||
$this->id = Html::getInputId($this->model, $this->attribute); | ||
}elseif($this->attribute){ | ||
$this->id = $this->id . '_' . $this->attribute; | ||
} | ||
$_options = [ | ||
'serverUrl' => Url::to(['UeUpload']), | ||
'initialFrameWidth' => '100%', // 最小920 | ||
'initialFrameHeight' => '400', | ||
'lang' => (strtolower(Yii::$app->language) == 'en-us') ? 'en' : 'zh-cn' | ||
]; | ||
$this->clientOptions = array_merge($_options, $this->clientOptions); | ||
if($this->hasModel()){ | ||
parent::init(); // TODO: Change the autogenerated stub | ||
} | ||
} | ||
|
||
public function run() | ||
{ | ||
self::registerClientScript(); | ||
if($this->hasModel()){ | ||
return Html::activeTextarea($this->model, $this->attribute, ['id'=>$this->id]); | ||
}else{ | ||
return Html::textarea($this->id, $this->value, ['id'=>$this->id]); | ||
} | ||
} | ||
|
||
/** | ||
* 注册客户端脚本 | ||
*/ | ||
protected function registerClientScript() | ||
{ | ||
UEditorAsset::register($this->view); | ||
$clientOptions = Json::encode($this->clientOptions); | ||
$script = "UE.getEditor('{$this->id}', $clientOptions)"; | ||
$this->view->registerJs($script, View::POS_READY); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,245 @@ | ||
<?php | ||
namespace moxuandi\ueditor; | ||
|
||
use Yii; | ||
use yii\base\Action; | ||
use yii\helpers\Json; | ||
use moxuandi\helpers\Uploader; | ||
|
||
/** | ||
* UEditor 接收上传图片控制器. | ||
* | ||
* @author zhangmoxuan <[email protected]> | ||
* @link http://www.zhangmoxuan.cn | ||
* @QQ 1104984259 | ||
* @date 2017-7-14 | ||
*/ | ||
class UEditorAction extends Action | ||
{ | ||
public $config = []; // 配置接口, 详情请查看assets/php/config.json 或 http://fex.baidu.com/ueditor/#server-config | ||
|
||
|
||
public function init() | ||
{ | ||
Yii::$app->request->enableCsrfValidation = false; // 关闭csrf | ||
$_config = require(__DIR__ . '/config.php'); // 默认上传配置 | ||
$this->config = array_merge($_config, $this->config); | ||
parent::init(); // TODO: Change the autogenerated stub | ||
} | ||
|
||
public function run() | ||
{ | ||
switch(Yii::$app->request->get('action')){ | ||
case 'config': $result = Json::encode($this->config); break; | ||
// 上传图片 | ||
case 'uploadimage': | ||
// 上传涂鸦 | ||
case 'uploadscrawl': | ||
// 上传视频 | ||
case 'uploadvideo': | ||
// 上传文件 | ||
case 'uploadfile': $result = self::actionUpload(); break; | ||
// 列出图片 | ||
case 'listimage': | ||
// 列出文件 | ||
case 'listfile': $result = self::actionList(); break; | ||
// 抓取远程图片 | ||
case 'catchimage': $result = self::actionCrawler(); break; | ||
default: $result = Json::encode(['state'=>'请求地址出错']); break; | ||
} | ||
|
||
// 输出结果 | ||
if($callback = Yii::$app->request->get('callback')){ | ||
if(preg_match("/^[\w_]+$/", $callback)){ | ||
echo htmlspecialchars($callback) . '(' . $result . ')'; | ||
}else{ | ||
echo Json::encode(['state'=>'callback 参数不合法.']); | ||
} | ||
}else{ | ||
echo $result; | ||
} | ||
} | ||
|
||
/** | ||
* 处理上传 | ||
* @return string | ||
*/ | ||
protected function actionUpload() | ||
{ | ||
$base64 = 'upload'; | ||
switch(Yii::$app->request->get('action')){ | ||
// 上传图片 | ||
case 'uploadimage': | ||
$config = [ | ||
'pathFormat' => $this->config['imagePathFormat'], | ||
'maxSize' => $this->config['imageMaxSize'], | ||
'allowFiles' => $this->config['imageAllowFiles'], | ||
'thumbStatus' => $this->config['thumbStatus'], | ||
'thumbWidth' => $this->config['thumbWidth'], | ||
'thumbHeight' => $this->config['thumbHeight'], | ||
'thumbCut' => $this->config['thumbCut'], | ||
]; | ||
$fieldName = $this->config['imageFieldName']; | ||
break; | ||
// 上传涂鸦 | ||
case 'uploadscrawl': | ||
$config = [ | ||
'pathFormat' => $this->config['scrawlPathFormat'], | ||
'maxSize' => $this->config['scrawlMaxSize'], | ||
'realName' => 'scrawl.png' | ||
]; | ||
$fieldName = $this->config['scrawlFieldName']; | ||
$base64 = 'base64'; | ||
break; | ||
// 上传视频 | ||
case 'uploadvideo': | ||
$config = [ | ||
'pathFormat' => $this->config['videoPathFormat'], | ||
'maxSize' => $this->config['videoMaxSize'], | ||
'allowFiles' => $this->config['videoAllowFiles'] | ||
]; | ||
$fieldName = $this->config['videoFieldName']; | ||
break; | ||
// 上传文件 | ||
case 'uploadfile': | ||
default: | ||
$config = [ | ||
'pathFormat' => $this->config['filePathFormat'], | ||
'maxSize' => $this->config['fileMaxSize'], | ||
'allowFiles' => $this->config['fileAllowFiles'] | ||
]; | ||
$fieldName = $this->config['fileFieldName']; | ||
break; | ||
} | ||
|
||
// 生成上传实例对象并完成上传, 返回结果数据 | ||
$up = new Uploader($fieldName, $config, $base64, $this->config['saveDatabase']); | ||
return Json::encode([ | ||
'original' => $up->realName, | ||
'name' => $up->fileName, | ||
'title' => $up->fileName, | ||
'url' => '/' . $up->fullName, | ||
'size' => $up->fileSize, | ||
'type' => $up->fileType, | ||
'state' => $up->stateInfo, | ||
]); | ||
} | ||
|
||
/** | ||
* 列出已上传的文件列表 | ||
* @return string | ||
*/ | ||
protected function actionList() | ||
{ | ||
switch(Yii::$app->request->get('action')){ | ||
// 列出文件 | ||
case 'listfile': | ||
$allowFiles = $this->config['fileManagerAllowFiles']; | ||
$listSize = $this->config['fileManagerListSize']; | ||
$path = $this->config['fileManagerListPath']; | ||
break; | ||
// 列出图片 | ||
case 'listimage': | ||
default: | ||
$allowFiles = $this->config['imageManagerAllowFiles']; | ||
$listSize = $this->config['imageManagerListSize']; | ||
$path = $this->config['imageManagerListPath']; | ||
break; | ||
} | ||
|
||
// 允许列出的文件类型, eg: 'png|jpg|jpeg|gif|bmp' | ||
$allowFiles = substr(str_replace('.', '|', join('', $allowFiles)), 1); | ||
|
||
// 获取参数 | ||
$getStart = Yii::$app->request->get('start'); | ||
$getSize = Yii::$app->request->get('size'); | ||
$start = isset($getStart) ? htmlspecialchars($getStart) : 0; | ||
$size = isset($getSize) ? htmlspecialchars($getSize) : $listSize; | ||
$end = (int)$start + (int)$size; | ||
|
||
// 获取文件列表 | ||
$path = Yii::getAlias('@webroot') . (substr($path, 0, 1) == '/' ? '' : '/') . $path; | ||
$files = self::getfiles($path, $allowFiles); | ||
$len = count($files); // 文件总数 | ||
if($len === 0) | ||
return Json::encode(['state'=>'no match file', 'list'=>[], 'start'=>$start, 'total'=>$len]); | ||
|
||
// 获取指定范围的列表 | ||
for($i = min($end, $len) - 1, $list = []; $i < $len && $i >= 0 && $i >= $start; $i--){ | ||
$list[] = $files[$i]; | ||
} | ||
|
||
// 获取指定范围的列表(倒序) | ||
/*for($i = $end, $list = []; $i < $len && $i < $end; $i++){ | ||
$list[] = $files[$i]; | ||
}*/ | ||
|
||
// 返回数据 | ||
return Json::encode(['state'=>'SUCCESS', 'list'=>$list, 'start'=>$start, 'total'=>$len]); | ||
} | ||
|
||
|
||
/** | ||
* 抓取远程图片(待测试) | ||
*/ | ||
protected function actionCrawler() | ||
{ | ||
// 上传配置 | ||
$config = [ | ||
'realName' => 'scrawl.png', | ||
'pathFormat' => $this->config['catcherPathFormat'], | ||
'maxSize' => $this->config['catcherMaxSize'], | ||
'allowFiles' => $this->config['catcherAllowFiles'], | ||
]; | ||
$fieldName = $this->config['catcherFieldName']; | ||
|
||
// 抓取 | ||
$list = []; | ||
$source = Yii::$app->request->post($fieldName); | ||
foreach($source as $imgUrl){ | ||
$item = new Uploader($imgUrl, $config, 'remote'); | ||
array_push($list, [ | ||
'original' => htmlspecialchars($item->realName), | ||
'name' => htmlspecialchars($item->fileName), | ||
'url' => '/' . $item->fullName, | ||
'size' => $item->fileSize, | ||
'type' => $item->fileType, | ||
'state' => $item->stateInfo, | ||
'title' => htmlspecialchars($item->fileName), | ||
'source' => htmlspecialchars($imgUrl) | ||
]); | ||
} | ||
|
||
// 返回抓取数据 | ||
return Json::encode(['state'=>count($list) ? 'SUCCESS' : 'ERROR', 'list'=>$list]); | ||
} | ||
|
||
/** | ||
* 遍历获取目录下的指定类型的文件 | ||
* @param string $path | ||
* @param array $allowFiles | ||
* @param array $files | ||
* @return array|null | ||
*/ | ||
protected function getfiles($path, $allowFiles, &$files=[]) | ||
{ | ||
if(!is_dir($path)) | ||
return null; | ||
if(substr($path, strlen($path) - 1) != '/') | ||
$path .= '/'; | ||
$handle = opendir($path); | ||
while(false !== ($file = readdir($handle))){ | ||
if(!in_array($file , ['.', '..'])){ | ||
$path2 = $path . $file; | ||
if(is_dir($path2)){ | ||
self::getfiles($path2, $allowFiles, $files); | ||
}else{ | ||
if(preg_match('/\.(' . $allowFiles . ')$/i', $file)){ | ||
$files[] = ['url'=>substr($path2, strlen(Yii::getAlias('@webroot'))), 'mtime'=>filemtime($path2)]; | ||
} | ||
} | ||
} | ||
} | ||
return $files; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
namespace moxuandi\ueditor; | ||
|
||
use yii\web\AssetBundle; | ||
|
||
/** | ||
* Asset bundle for the UEditor | ||
* | ||
* @author zhangmoxuan <[email protected]> | ||
* @link http://www.zhangmoxuan.com | ||
* @QQ 1104984259 | ||
* @Date 2017/7/14 | ||
*/ | ||
class UEditorAsset extends AssetBundle | ||
{ | ||
public $sourcePath = '@vendor/moxuandi/yii2-ueditor/assets'; | ||
|
||
public $css = []; | ||
|
||
public $js = [ | ||
'ueditor.config.js', | ||
'ueditor.all.min.js', | ||
]; | ||
|
||
public $depends = [ | ||
'yii\web\JqueryAsset', | ||
]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" | ||
"http://www.w3.org/TR/html4/loose.dtd"> | ||
<html> | ||
<head> | ||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> | ||
<title></title> | ||
<style type="text/css"> | ||
*{color: #838383;margin: 0;padding: 0} | ||
html,body {font-size: 12px;overflow: hidden; } | ||
.content{padding:5px 0 0 15px;} | ||
input{width:210px;height:21px;line-height:21px;margin-left: 4px;} | ||
</style> | ||
</head> | ||
<body> | ||
<div class="content"> | ||
<span><var id="lang_input_anchorName"></var></span><input id="anchorName" value="" /> | ||
</div> | ||
<script type="text/javascript" src="../internal.js"></script> | ||
<script type="text/javascript"> | ||
var anchorInput = $G('anchorName'), | ||
node = editor.selection.getRange().getClosedNode(); | ||
if(node && node.tagName == 'IMG' && (node = node.getAttribute('anchorname'))){ | ||
anchorInput.value = node; | ||
} | ||
anchorInput.onkeydown = function(evt){ | ||
evt = evt || window.event; | ||
if(evt.keyCode == 13){ | ||
editor.execCommand('anchor', anchorInput.value); | ||
dialog.close(); | ||
domUtils.preventDefault(evt) | ||
} | ||
}; | ||
dialog.onok = function (){ | ||
editor.execCommand('anchor', anchorInput.value); | ||
dialog.close(); | ||
}; | ||
$focus(anchorInput); | ||
</script> | ||
</body> | ||
</html> |
Oops, something went wrong.