2 // vim: tabstop=2:shiftwidth=2
\r
5 * NP_DragAndDropUploader ($Revision: 1.13 $)
\r
6 * by hsur ( http://blog.cles.jp/np_cles )
\r
8 * Based on upload.php (http://www.plupload.com)
\r
10 * $Id: NP_DragAndDropUploader.php,v 1.13 2011/10/13 14:35:52 hsur Exp $
\r
14 * Copyright (C) 2010 CLES. All rights reserved.
\r
16 * This program is free software; you can redistribute it and/or
\r
17 * modify it under the terms of the GNU General Public License
\r
18 * as published by the Free Software Foundation; either version 2
\r
19 * of the License, or (at your option) any later version.
\r
21 * This program is distributed in the hope that it will be useful,
\r
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
24 * GNU General Public License for more details.
\r
26 * You should have received a copy of the GNU General Public License
\r
27 * along with this program; if not, write to the Free Software
\r
28 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
\r
30 * In addition, as a special exception, cles( http://blog.cles.jp/np_cles ) gives
\r
31 * permission to link the code of this program with those files in the PEAR
\r
32 * library that are licensed under the PHP License (or with modified versions
\r
33 * of those files that use the same license as those files), and distribute
\r
34 * linked combinations including the two. You must obey the GNU General Public
\r
35 * License in all respects for all of the code used other than those files in
\r
36 * the PEAR library that are licensed under the PHP License. If you modify
\r
37 * this file, you may extend this exception to your version of the file,
\r
38 * but you are not obligated to do so. If you do not wish to do so, delete
\r
39 * this exception statement from your version.
\r
42 define('NP_DRAGANDDROPUPLOADER_RUNTIMES', 'html5,gears,flash,silverlight,browserplus');
\r
45 require_once(dirname(__FILE__).'/sharedlibs/sharedlibs.php');
\r
46 require_once('cles/Template.php');
\r
48 class NP_DragAndDropUploader extends NucleusPlugin {
\r
51 require_once($DIR_LIBS . 'MEDIA.php');
\r
53 // include language file for this plugin
\r
54 $language = ereg_replace( '[\\|/]', '', getLanguageName());
\r
55 if (file_exists($this->getDirectory().'language/'.$language.'.php'))
\r
56 @include_once($this->getDirectory().'language/'.$language.'.php');
\r
58 $this->te =& new cles_Template(dirname(__FILE__).'/draganddropuploader/template');
\r
62 function getName() {
\r
63 return 'Drag and Drop Uploader';
\r
67 function getAuthor() {
\r
71 // an URL to the plugin website
\r
72 // can also be of the form mailto:foo@bar.com
\r
74 return 'http://blog.cles.jp/np_cles/category/31/subcatid/25';
\r
77 // version of the plugin
\r
78 function getVersion() {
\r
82 function hasAdminArea() {
\r
86 function getEventList() {
\r
87 return array('AddItemFormExtras','EditItemFormExtras','AdminPrePageHead','BookmarkletExtraHead');
\r
90 function event_AddItemFormExtras(&$data){
\r
91 $this->_formExtras();
\r
94 function event_EditItemFormExtras(&$data){
\r
95 $this->_formExtras();
\r
98 function _formExtras(){
\r
99 $collections = MEDIA::getCollectionList();
\r
100 $collections_html = '<select id="plugin_draganddropuploader">';
\r
101 foreach($collections as $k => $v){
\r
102 $collections_html .= '<option value="'.$k.'">'.$v.'</option>';
\r
104 $collections_html .= '</select>';
\r
107 'collections' => $collections_html,
\r
109 $index = $this->te->fetch('index', strtolower(__CLASS__));
\r
110 echo $this->te->fill($index, $tplVars, false);
\r
113 function event_BookmarkletExtraHead(&$data){
\r
114 $this->_extraHead($data);
\r
117 function event_AdminPrePageHead(&$data){
\r
118 if ( !($data['action'] == 'createitem' || $data['action'] == 'itemedit') ){
\r
121 $this->_extraHead($data);
\r
124 function _extraHead(&$data){
\r
126 'plugindirurl' => $this->getAdminURL(),
\r
127 'runtimes' => NP_DRAGANDDROPUPLOADER_RUNTIMES,
\r
129 $header = $this->te->fetch('extrahead', strtolower(__CLASS__));
\r
130 $data['extrahead'] .= "\n" . $this->te->fill($header, $tplVars, false);
\r
133 function install() {
\r
135 $this->createOption('debug', 'Debug Mode ?', 'yesno', 'no');
\r
138 function unInstall() {
\r
141 function _info($msg) {
\r
142 if ($this->getOption('debug') == 'yes') {
\r
143 ACTIONLOG :: add(INFO, 'DragAndDropUploader: '.$msg);
\r
147 function _warn($msg) {
\r
148 ACTIONLOG :: add(WARNING, 'DragAndDropUploader: '.$msg);
\r
151 function getMinNucleusVersion() { return 330; }
\r
152 function getMinNucleusPatchLevel() { return 0; }
\r
154 // a description to be shown on the installed plugins listing
\r
155 function getDescription() {
\r
156 return '[$Revision: 1.13 $]<br />' . NP_DRAGANDDROPUPLOADER_DESCRIPTION;
\r
159 function supportsFeature($what) {
\r
161 case 'SqlTablePrefix' :
\r
169 function doAction($type) {
\r
171 header('Content-type: text/plain; charset=UTF-8');
\r
172 header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
\r
173 header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
\r
174 header("Cache-Control: no-store, no-cache, must-revalidate");
\r
175 header("Cache-Control: post-check=0, pre-check=0", false);
\r
176 header("Pragma: no-cache");
\r
179 case 'image_upload':
\r
180 // 5 minutes execution time
\r
181 @set_time_limit(5 * 60);
\r
182 $this->_image_upload();
\r
186 // other actions result in an error
\r
189 return 'Unexisting action: ' . $type;
\r
193 function _jsonrpc_error($mes = "", $code = "500"){
\r
194 $this->_warn("($code) $mes");
\r
195 header("HTTP/1.1 $code $mes");
\r
196 echo('{"jsonrpc" : "2.0", "error" : {"code": '.$code.', "message": "'.$mes.'"}, "id" : "id"}');
\r
200 function _jsonrpc_ok(){
\r
201 echo('{"jsonrpc" : "2.0", "result" : null, "id" : "id"}');
\r
205 function _image_upload(){
\r
206 global $member, $DIR_MEDIA, $CONF;
\r
207 if( !$member->isLoggedIn() ) return $this->_jsonrpc_error("Not Logged in.", "403");
\r
208 $collection = requestVar('collection');
\r
210 $fileName = isset($_REQUEST['name']) ? requestVar('name') : 'no_name';
\r
211 if( $CONF['MediaPrefix'] ){
\r
212 $exif = @exif_read_data($_FILES['file']['tmp_name']);
\r
213 if( $exif !== false && $exif["DateTime"] && strtotime($exif["DateTime"]) !== false )
\r
214 $fileName = strftime("%Y%m%d-", strtotime($exif["DateTime"])) . $fileName;
\r
216 $fileName = strftime("%Y%m%d-", time()) . $fileName;
\r
219 if (isset($_SERVER['HTTP_CONTENT_TYPE'])) $contentType = serverVar('HTTP_CONTENT_TYPE');
\r
220 if (isset($_SERVER['CONTENT_TYPE'])) $contentType = serverVar('CONTENT_TYPE');
\r
222 if (strpos($contentType, "multipart") !== false) {
\r
223 if (isset($_FILES['file']['tmp_name']) && is_uploaded_file($_FILES['file']['tmp_name'])) {
\r
224 $err = MEDIA::addMediaObject($collection, $_FILES['file']['tmp_name'], $fileName);
\r
226 $this->_jsonrpc_error('addMediaObject Error: '. $err, "500");
\r
229 $this->_jsonrpc_error("Failed to move uploaded file.", "500");
\r
231 // Read binary input stream and append it to temp file
\r
232 $in = fopen("php://input", "rb");
\r
234 while ($buff = fread($in, 4096))
\r
235 $err = MEDIA::addMediaObjectRaw($collection, $fileName, $buff);
\r
237 $this->_jsonrpc_error('addMediaObjectRaw Error: '. $err, "500");
\r
240 $this->_jsonrpc_error("Failed to open input stream.", "500");
\r
243 $this->_jsonrpc_ok();
\r