|
1
|
|
|
<?php
|
|
2
|
|
|
/**
|
|
3
|
|
|
* @author Alexey Tatarinov <[email protected]>
|
|
4
|
|
|
* @link https://github.com/shogodev/argilla/
|
|
5
|
|
|
* @copyright Copyright © 2003-2014 Shogo
|
|
6
|
|
|
* @license http://argilla.ru/LICENSE
|
|
7
|
|
|
* @package
|
|
8
|
|
|
*/
|
|
9
|
|
|
class BFileUploaderController extends BController
|
|
10
|
|
|
{
|
|
11
|
|
|
const ALLOWED_FILE_NAME = '/^[^\.].*$/';
|
|
12
|
|
|
|
|
13
|
|
|
const UPLOAD_PATH = '/f/upload/';
|
|
14
|
|
|
|
|
15
|
|
|
public $name = 'Загрузка файлов';
|
|
16
|
|
|
|
|
17
|
|
|
public function beforeAction($action)
|
|
18
|
|
|
{
|
|
19
|
|
|
Yii::app()->log->getRoutes()[1]->enabled = false;
|
|
20
|
|
|
|
|
21
|
|
|
return parent::beforeAction($action);
|
|
22
|
|
|
}
|
|
23
|
|
|
|
|
24
|
|
|
public function actions()
|
|
25
|
|
|
{
|
|
26
|
|
|
return array(
|
|
|
|
|
|
|
27
|
|
|
'connector' => array(
|
|
28
|
|
|
'class' => 'ext.elFinder.ElFinderConnectorAction',
|
|
29
|
|
|
// https://github.com/Studio-42/elFinder/wiki/Connector-configuration-options
|
|
30
|
|
|
'settings' => array(
|
|
31
|
|
|
'roots' => array(
|
|
32
|
|
|
array(
|
|
33
|
|
|
'driver' => 'LocalFileSystem',
|
|
34
|
|
|
'path' => GlobalConfig::instance()->rootPath.self::UPLOAD_PATH.'/',
|
|
35
|
|
|
'URL' => self::UPLOAD_PATH,
|
|
36
|
|
|
'alias' => self::UPLOAD_PATH,
|
|
37
|
|
|
'acceptedName' => self::ALLOWED_FILE_NAME,
|
|
38
|
|
|
'attributes' => array(
|
|
39
|
|
|
array(
|
|
40
|
|
|
'pattern' => '/\/[.].*$/',
|
|
41
|
|
|
'read' => false,
|
|
42
|
|
|
'write' => false,
|
|
43
|
|
|
'hidden' => true,
|
|
44
|
|
|
),
|
|
45
|
|
|
),
|
|
46
|
|
|
)
|
|
47
|
|
|
),
|
|
48
|
|
|
)
|
|
49
|
|
|
),
|
|
50
|
|
|
|
|
51
|
|
|
'elfinderCKEditor' => array(
|
|
52
|
|
|
'class' => 'ext.elFinder.ElFinderPopupAction',
|
|
53
|
|
|
'connectorRoute' => 'connector',
|
|
54
|
|
|
),
|
|
55
|
|
|
);
|
|
56
|
|
|
}
|
|
57
|
|
|
|
|
58
|
|
|
public function actionQuickUpload()
|
|
59
|
|
|
{
|
|
60
|
|
|
$uploadPath = GlobalConfig::instance()->rootPath.self::UPLOAD_PATH.'/';
|
|
61
|
|
|
$file = CUploadedFile::getInstanceByName('upload');
|
|
62
|
|
|
|
|
63
|
|
|
$fileName = UploadHelper::prepareFileName($uploadPath, $file->getName());
|
|
64
|
|
|
$uploadNum = intval(Yii::app()->request->getParam('CKEditorFuncNum'));
|
|
65
|
|
|
|
|
66
|
|
|
$uploadFile = '';
|
|
67
|
|
|
$error = '';
|
|
68
|
|
|
|
|
69
|
|
|
if( !preg_match(self::ALLOWED_FILE_NAME, $fileName, $matches) )
|
|
70
|
|
|
$error = 'Ошибка, не верный формат файла!';
|
|
71
|
|
|
|
|
72
|
|
|
if( empty($error) && !$file->saveAs($uploadPath.$fileName) )
|
|
73
|
|
|
$error = 'Ошибка, не удалось загрузить файл!';
|
|
74
|
|
|
|
|
75
|
|
|
if( empty($error) )
|
|
76
|
|
|
{
|
|
77
|
|
|
if( $file->getName() != $fileName )
|
|
78
|
|
|
$error = 'Файл был переименован в '.CHtml::encode($fileName);
|
|
79
|
|
|
|
|
80
|
|
|
$uploadFile = self::UPLOAD_PATH.$fileName;
|
|
81
|
|
|
}
|
|
82
|
|
|
|
|
83
|
|
|
@chmod($uploadPath.$fileName, 0664);
|
|
|
|
|
|
|
84
|
|
|
|
|
85
|
|
|
echo "<script type=\"text/javascript\">window.parent.CKEDITOR.tools.callFunction(".$uploadNum.", '".$uploadFile."', '".$error."');</script>";
|
|
86
|
|
|
}
|
|
87
|
|
|
} |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.