Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
BigKuCha committed Jan 29, 2015
0 parents commit d1c7240
Show file tree
Hide file tree
Showing 311 changed files with 131,605 additions and 0 deletions.
54 changes: 54 additions & 0 deletions UEditor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

/**
* @link http://ueditor.baidu.com/website/index.html
*/
namespace kucha\ueditor;

use Yii;
use yii\helpers\ArrayHelper;
use yii\helpers\Json;
use yii\helpers\Url;
use yii\web\View;
use yii\widgets\InputWidget;

class UEditor extends InputWidget
{
//配置选项,参阅Ueditor官网文档(定制菜单等)
public $jsOptions;

//预设配置
protected $_options;

/**
* @throws \yii\base\InvalidConfigException
*/
public function init()
{
$this->_options = [
'serverUrl' => Url::to(['upload']),
];
$this->jsOptions = ArrayHelper::merge($this->_options, $this->jsOptions);
parent::init();
}

public function run()
{
$this->registerClientScript();
if ($this->hasModel()) {
$this->value = $this->model->getAttributes($this->attribute);
}
echo '<script id="' . $this->id . '" type="text/plain" style="width:1024px;height:500px;">' . $this->value . '</script>';
}

/**
* 注册客户端脚本
*/
protected function registerClientScript()
{
UEditorAsset::register($this->view);
$jsonOptions = Json::encode($this->jsOptions);
$script = "UE.getEditor('" . $this->id . "', " . $jsonOptions . ")";
$this->view->registerJs($script, View::POS_READY);
}
}
281 changes: 281 additions & 0 deletions UEditorAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,281 @@
<?php
namespace kucha\ueditor;

use Yii;
use yii\base\Action;
use yii\helpers\ArrayHelper;

class UEditorAction extends Action
{
/**
* @var array
*/
public $config = [];


public function init()
{
//close csrf
Yii::$app->request->enableCsrfValidation = false;
//默认设置
$_config = require(__DIR__ . '/config.php');
//load config file
$this->config = ArrayHelper::merge($_config, $this->config);
parent::init();
}

public function run()
{
$this->handleAction();
}

/**
* 处理action
*/
protected function handleAction()
{
$action = Yii::$app->request->get('action');
switch ($action) {
case 'config':
$result = json_encode($this->config);
break;

/* 上传图片 */
case 'uploadimage':
/* 上传涂鸦 */
case 'uploadscrawl':
/* 上传视频 */
case 'uploadvideo':
/* 上传文件 */
case 'uploadfile':
$result = $this->actionUpload();
break;

/* 列出图片 */
case 'listimage':
/* 列出文件 */
case 'listfile':
$result = $this->actionList();
break;

/* 抓取远程文件 */
case 'catchimage':
$result = $this->actionCrawler();
break;

default:
$result = json_encode(array(
'state' => '请求地址出错'
));
break;
}
/* 输出结果 */
if (isset($_GET["callback"])) {
if (preg_match("/^[\w_]+$/", $_GET["callback"])) {
echo htmlspecialchars($_GET["callback"]) . '(' . $result . ')';
} else {
echo json_encode(array(
'state' => 'callback参数不合法'
));
}
} else {
echo $result;
}
}

/**
* 上传
* @return string
*/
protected function actionUpload()
{
$base64 = "upload";
switch (htmlspecialchars($_GET['action'])) {
case 'uploadimage':
$config = array(
"pathFormat" => $this->config['imagePathFormat'],
"maxSize" => $this->config['imageMaxSize'],
"allowFiles" => $this->config['imageAllowFiles']
);
$fieldName = $this->config['imageFieldName'];
break;
case 'uploadscrawl':
$config = array(
"pathFormat" => $this->config['scrawlPathFormat'],
"maxSize" => $this->config['scrawlMaxSize'],
"allowFiles" => $this->config['scrawlAllowFiles'],
"oriName" => "scrawl.png"
);
$fieldName = $this->config['scrawlFieldName'];
$base64 = "base64";
break;
case 'uploadvideo':
$config = array(
"pathFormat" => $this->config['videoPathFormat'],
"maxSize" => $this->config['videoMaxSize'],
"allowFiles" => $this->config['videoAllowFiles']
);
$fieldName = $this->config['videoFieldName'];
break;
case 'uploadfile':
default:
$config = array(
"pathFormat" => $this->config['filePathFormat'],
"maxSize" => $this->config['fileMaxSize'],
"allowFiles" => $this->config['fileAllowFiles']
);
$fieldName = $this->config['fileFieldName'];
break;
}
/* 生成上传实例对象并完成上传 */

$up = new Uploader($fieldName, $config, $base64);
/**
* 得到上传文件所对应的各个参数,数组结构
* array(
* "state" => "", //上传状态,上传成功时必须返回"SUCCESS"
* "url" => "", //返回的地址
* "title" => "", //新文件名
* "original" => "", //原始文件名
* "type" => "" //文件类型
* "size" => "", //文件大小
* )
*/

/* 返回数据 */
return json_encode($up->getFileInfo());
}

/**
* 获取已上传的文件列表
* @return string
*/
protected function actionList()
{
/* 判断类型 */
switch ($_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'];
}
$allowFiles = substr(str_replace(".", "|", join("", $allowFiles)), 1);

/* 获取参数 */
$size = isset($_GET['size']) ? htmlspecialchars($_GET['size']) : $listSize;
$start = isset($_GET['start']) ? htmlspecialchars($_GET['start']) : 0;
$end = (int)$start + (int)$size;

/* 获取文件列表 */
$path = $_SERVER['DOCUMENT_ROOT'] . (substr($path, 0, 1) == "/" ? "" : "/") . $path;
$files = $this->getfiles($path, $allowFiles);
if (!count($files)) {
return json_encode(array(
"state" => "no match file",
"list" => array(),
"start" => $start,
"total" => count($files)
));
}

/* 获取指定范围的列表 */
$len = count($files);
for ($i = min($end, $len) - 1, $list = array(); $i < $len && $i >= 0 && $i >= $start; $i--) {
$list[] = $files[$i];
}
//倒序
//for ($i = $end, $list = array(); $i < $len && $i < $end; $i++){
// $list[] = $files[$i];
//}

/* 返回数据 */
$result = json_encode(array(
"state" => "SUCCESS",
"list" => $list,
"start" => $start,
"total" => count($files)
));

return $result;
}

/**
* 抓取远程图片
* @return string
*/
protected function actionCrawler()
{
/* 上传配置 */
$config = array(
"pathFormat" => $this->config['catcherPathFormat'],
"maxSize" => $this->config['catcherMaxSize'],
"allowFiles" => $this->config['catcherAllowFiles'],
"oriName" => "remote.png"
);
$fieldName = $this->config['catcherFieldName'];

/* 抓取远程图片 */
$list = array();
if (isset($_POST[$fieldName])) {
$source = $_POST[$fieldName];
} else {
$source = $_GET[$fieldName];
}
foreach ($source as $imgUrl) {
$item = new Uploader($imgUrl, $config, "remote");
$info = $item->getFileInfo();
array_push($list, array(
"state" => $info["state"],
"url" => $info["url"],
"size" => $info["size"],
"title" => htmlspecialchars($info["title"]),
"original" => htmlspecialchars($info["original"]),
"source" => htmlspecialchars($imgUrl)
));
}

/* 返回抓取数据 */
return json_encode(array(
'state' => count($list) ? 'SUCCESS' : 'ERROR',
'list' => $list
));
}

/**
* 遍历获取目录下的指定类型的文件
* @param $path
* @param $allowFiles
* @param array $files
* @return array|null
*/
protected function getfiles($path, $allowFiles, &$files = array())
{
if (!is_dir($path)) return null;
if (substr($path, strlen($path) - 1) != '/') $path .= '/';
$handle = opendir($path);
while (false !== ($file = readdir($handle))) {
if ($file != '.' && $file != '..') {
$path2 = $path . $file;
if (is_dir($path2)) {
$this->getfiles($path2, $allowFiles, $files);
} else {
if (preg_match("/\.(" . $allowFiles . ")$/i", $file)) {
$files[] = array(
'url' => substr($path2, strlen($_SERVER['DOCUMENT_ROOT'])),
'mtime' => filemtime($path2)
);
}
}
}
}
return $files;
}
}
19 changes: 19 additions & 0 deletions UEditorAsset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
namespace kucha\ueditor;


use yii\web\AssetBundle;
use yii\web\View;

class UEditorAsset extends AssetBundle
{
public $sourcePath = '@app/widgets/ueditor/assets';
public $js = [
'js/ueditor.config.js',
'js/ueditor.all.js',
'lang/zh-cn/zh-cn.js'
];
public $jsOptions = [
'position' =>View::POS_HEAD,
];
}
Loading

0 comments on commit d1c7240

Please sign in to comment.