MediaController.php
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2015 wemove digital solutions GmbH
5  *
6  * Licensed under the terms of the MIT License.
7  *
8  * See the LICENSE file distributed with this work for
9  * additional information.
10  */
12 
17 
18 if (!class_exists('\elFinder')) {
19  throw new \wcmf\lib\config\ConfigurationException(
20  'wcmf\application\controller\MediaController requires '.
21  'elFinder. If you are using composer, add nao-pon/elfinder-nightly '.
22  'as dependency to your project');
23 }
24 
25 /**
26  * MediaController integrates elFinder (https://github.com/Studio-42/elFinder) into wCMF.
27  *
28  * @note This class requires ElFinder
29  *
30  * The controller supports the following actions:
31  *
32  * <div class="controller-action">
33  * <div> __Action__ browseMedia </div>
34  * <div>
35  * Run elFinder.
36  * | Parameter | Description
37  * |--------------------------|-------------------------
38  * | _in_ `directory` | elFinder _startPath_ parameter
39  * | _in_ / _out_ `fieldName` | The name of the input field that should receive the url of the selected file. if not given, elFinder will search for a CkEditor instance and set the url on that.
40  * | _out_ `rootUrl` | Root url of all media as derived from the configuration value _uploadDir_ in the configuration section _media_
41  * | _out_ `rootPath` | Root path of all media as derived from the configuration value _uploadDir_ in the configuration section _media_
42  * | __Response Actions__ | |
43  * | `ok` | In all cases
44  * </div>
45  * </div>
46  *
47  * <div class="controller-action">
48  * <div> __Action__ crop </div>
49  * <div>
50  * Crop the given image.
51  * | Parameter | Description
52  * |--------------------------|-------------------------
53  * | _in_ `directory` | elFinder _startPath_ parameter
54  * | _in_ / _out_ `fieldName` | The name of the input field that should receive the url of the selected file. if not given, elFinder will search for a CkEditor instance and set the url on that.
55  * | _in_ / _out_ `oid` | The filename of the image to crop
56  * | _in_ `cropX` | The x value of the top-left corner of the crop frame
57  * | _in_ `cropY` | The y value of the top-left corner of the crop frame
58  * | _in_ `cropWidth` | The width of the crop frame
59  * | _in_ `cropHeight` | The height of the crop frame
60  * | _out_ `rootUrl` | Root url of all media as derived from the configuration value _uploadDir_ in the configuration section _media_
61  * | _out_ `rootPath` | Root path of all media as derived from the configuration value _uploadDir_ in the configuration section _media_
62  * | __Response Actions__ | |
63  * | `browseMedia` | If all crop parameters are defined
64  * | `ok` | In crop parameters are missing
65  * </div>
66  * </div>
67  *
68  * @note elFinder defines action names in the _cmd_ parameter.
69  *
70  * @author ingo herwig <ingo@wemove.com>
71  */
72 class MediaController extends Controller {
73 
74  /**
75  * @see Controller::doExecute()
76  */
77  protected function doExecute() {
78  $request = $this->getRequest();
79  $response = $this->getResponse();
80 
81  $fileUtil = new FileUtil();
82 
83  // get root path and root url for the browser
84  $rootPath = $this->getResourceBaseDir();
85  $relRootPath = URIUtil::makeRelative($rootPath, dirname($fileUtil->realpath($_SERVER['SCRIPT_FILENAME'])));
86  $refURL = dirname(URIUtil::getProtocolStr().$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME']).'/';
87  $rootUrl = URIUtil::makeAbsolute($relRootPath, $refURL);
88 
89  // requested directory (ElFinder expects DIRECTORY_SEPARATOR)
90  if (!is_dir($request->getValue('directory'))) {
91  // empty if not valid
92  $request->clearValue('directory');
93  }
94  else {
95  // force ElFinder to use directory parameter
96  $request->setValue('target', '');
97  }
98  $directory = $request->hasValue('directory') ? $request->getValue('directory') : $rootPath;
99  $absDirectory = $fileUtil->realpath($directory).'/';
100 
101  // set common response values
102  if ($request->hasValue('fieldName')) {
103  $response->setValue('fieldName', $request->getValue('fieldName'));
104  }
105  $response->setValue('rootUrl', $rootUrl);
106  $response->setValue('rootPath', $rootPath);
107 
108  if ($request->getAction() == "browseMedia") {
109  $opts = array(
110  // 'debug' => true,
111  'roots' => array(
112  array(
113  'driver' => 'LocalFileSystem',
114  'path' => str_replace('/', DIRECTORY_SEPARATOR, rtrim($rootPath, '/')),
115  'URL' => $rootUrl,
116  'alias' => 'Media',
117  'tmbBgColor' => 'transparent',
118  'startPath' => str_replace('/', DIRECTORY_SEPARATOR, rtrim($absDirectory, '/'))
119  )
120  ),
121  'bind' => array(
122  'rename rm paste' => array($this, 'onFileMoved')
123  )
124  );
125 
126  // run elFinder
127  $connector = new \elFinderConnector(new \elFinder($opts));
128  $queryParams = $request->getValues();
129  $connector->run($queryParams);
130 
131  // unreachable, since elFinder calls exit()
132  $response->setAction('ok');
133  }
134  else {
135  // custom crop action
136  if ($request->getAction() == "crop") {
137  $file = $request->getValue('oid');
138  $response->setValue('oid', $file);
139  if ($request->hasValue('cropX') && $request->hasValue('cropY') &&
140  $request->hasValue('cropWidth') && $request->hasValue('cropHeight')) {
141  // extract crop info
142  $x = $request->getValue('cropX');
143  $y = $request->getValue('cropY');
144  $w = $request->getValue('cropWidth');
145  $h = $request->getValue('cropHeight');
146 
147  // define target file name
148  $cropInfo = 'x'.$x.'y'.$y.'w'.$w.'h'.$h;
149  $pathParts = pathinfo($file);
150  $targetFile = $pathParts['dirname'].'/'.$pathParts['filename'].'_'.$cropInfo.'.'.$pathParts['extension'];
151 
152  // crop the image
153  $graphicsUtil = new GraphicsUtil();
154  $graphicsUtil->cropImage($file, $targetFile, $w, $h, $x, $y);
155  $response->setValue('fieldName', $request->getValue('fieldName'));
156  $response->setAction('browseMedia');
157  }
158  else {
159  $response->setAction('ok');
160  }
161  }
162  }
163  }
164 
165  /**
166  * Called when file is moved
167  * @param $cmd elFinder command name
168  * @param $result Command result as array
169  * @param $args Command arguments from client
170  * @param $elfinder elFinder instance
171  **/
172  protected function onFileMoved($cmd, $result, $args, $elfinder) {
173  $addedFiles = $result['added'];
174  $removedFiles = $result['removed'];
175  for ($i=0, $count=sizeof($removedFiles); $i<$count; $i++) {
176  $source = $removedFiles[$i]['realpath'];
177  $target = $elfinder->realpath($addedFiles[$i]['hash']);
178  }
179  $logger = $this->getLogger();
180  $logger->debug($cmd." file: ".$source." -> ".$target);
181  }
182 
183  /**
184  * Get the base directory for resources. The default implementation
185  * returns the directory configured by the 'uploadDir' key in section 'media'.
186  * @return The directory name
187  * @note Subclasses will override this method to implement special application requirements
188  */
189  protected function getResourceBaseDir() {
190  $config = $this->getConfiguration();
191  $rootPath = $config->getDirectoryValue('uploadDir', 'media');
192  return $rootPath;
193  }
194 }
195 ?>
getRequest()
Get the Request instance.
Definition: Controller.php:190
Controller is the base class of all controllers.
Definition: Controller.php:48
onFileMoved($cmd, $result, $args, $elfinder)
Called when file is moved.
static getProtocolStr()
Definition: URIUtil.php:156
static makeRelative($abs_uri, $base)
Convert an absolute URI to a relative code from http://www.webmasterworld.com/forum88/334.htm.
Definition: URIUtil.php:26
getConfiguration()
Get the Configuration instance.
Definition: Controller.php:262
getResourceBaseDir()
Get the base directory for resources.
Application controllers.
Definition: namespaces.php:3
MediaController integrates elFinder (https://github.com/Studio-42/elFinder) into wCMF.
GraphicsUtil provides support for graphic manipulation.
FileUtil provides basic support for file functionality like HTTP file upload.
Definition: FileUtil.php:22
static makeAbsolute($rel_uri, $base)
Convert a relative URI to an absolute code from http://99webtools.com/relative-path-into-absolute-url...
Definition: URIUtil.php:63
getLogger()
Get the Logger instance.
Definition: Controller.php:206
getResponse()
Get the Response instance.
Definition: Controller.php:198