OSDN Git Service

NP_DragAndDropUploader v1.2
[nucleus-jp/nucleus-plugins.git] / trunk / NP_DragAndDropUploader / NP_DragAndDropUploader.php
1 <?php\r
2 // vim: tabstop=2:shiftwidth=2\r
3 \r
4 /**\r
5   * NP_DragAndDropUploader ($Revision: 1.12 $)\r
6   * by hsur ( http://blog.cles.jp/np_cles )\r
7   * \r
8   * Based on upload.php (http://www.plupload.com)\r
9   * \r
10   * $Id: NP_DragAndDropUploader.php,v 1.12 2011/10/09 08:49:31 hsur Exp $\r
11 */\r
12 \r
13 /*\r
14   * Copyright (C) 2010 CLES. All rights reserved.\r
15   *\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
20   * \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
25   * \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
29   * \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
40 */\r
41 \r
42 define('NP_DRAGANDDROPUPLOADER_RUNTIMES', 'html5,gears,flash,silverlight,browserplus');         \r
43 \r
44 // load class\r
45 require_once(dirname(__FILE__).'/sharedlibs/sharedlibs.php');\r
46 require_once('cles/Template.php');\r
47 \r
48 class NP_DragAndDropUploader extends NucleusPlugin {\r
49         function init(){\r
50                 global $DIR_LIBS;\r
51                 require_once($DIR_LIBS . 'MEDIA.php');          \r
52                 \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
57                 \r
58                 $this->te =& new cles_Template(dirname(__FILE__).'/draganddropuploader/template');\r
59         }\r
60         \r
61         // name of plugin\r
62         function getName() {\r
63                 return 'Drag and Drop Uploader';\r
64         }\r
65 \r
66         // author of plugin\r
67         function getAuthor() {\r
68                 return 'hsur';\r
69         }\r
70 \r
71         // an URL to the plugin website\r
72         // can also be of the form mailto:foo@bar.com\r
73         function getURL() {\r
74                 return 'http://blog.cles.jp/np_cles/category/31/subcatid/25';\r
75         }\r
76 \r
77         // version of the plugin\r
78         function getVersion() {\r
79                 return '1.2';\r
80         }\r
81         \r
82         function hasAdminArea() {\r
83                 return 1;\r
84         }\r
85         \r
86         function getEventList() {\r
87                 return array('AddItemFormExtras','EditItemFormExtras','AdminPrePageHead','BookmarkletExtraHead');\r
88         }\r
89         \r
90     function event_AddItemFormExtras(&$data){\r
91                 $this->_formExtras();\r
92         }\r
93 \r
94     function event_EditItemFormExtras(&$data){\r
95                 $this->_formExtras();\r
96         }\r
97         \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
103                 }\r
104                 $collections_html .= '</select>';\r
105                 \r
106                 $tplVars = array(\r
107                         'collections' => $collections_html,\r
108                 );\r
109                 $index = $this->te->fetch('index', strtolower(__CLASS__));\r
110                 echo $this->te->fill($index, $tplVars, false);\r
111         }\r
112         \r
113   function event_BookmarkletExtraHead(&$data){\r
114                 $this->_extraHead($data);\r
115   }\r
116 \r
117         function event_AdminPrePageHead(&$data){\r
118                 if ( !($data['action'] == 'createitem' || $data['action'] == 'itemedit') ){\r
119                         return;\r
120                 }\r
121                 $this->_extraHead($data);\r
122         }\r
123 \r
124         function _extraHead(&$data){\r
125                 $tplVars = array(\r
126                         'plugindirurl' => $this->getAdminURL(),\r
127                         'runtimes' => NP_DRAGANDDROPUPLOADER_RUNTIMES,\r
128                 );\r
129                 $header = $this->te->fetch('extrahead', strtolower(__CLASS__));\r
130                 $data['extrahead'] .= "\n" . $this->te->fill($header, $tplVars, false);\r
131     }\r
132         \r
133         function install() {\r
134                 // debug\r
135                 $this->createOption('debug', 'Debug Mode ?', 'yesno', 'no');\r
136         }\r
137         \r
138         function unInstall() {\r
139         }\r
140         \r
141         function _info($msg) {\r
142                 if ($this->getOption('debug') == 'yes') {\r
143                         ACTIONLOG :: add(INFO, 'DragAndDropUploader: '.$msg);\r
144                 }\r
145         }\r
146 \r
147         function _warn($msg) {\r
148                 ACTIONLOG :: add(WARNING, 'DragAndDropUploader: '.$msg);\r
149         }\r
150         \r
151         function getMinNucleusVersion() { return 330; }\r
152         function getMinNucleusPatchLevel() { return 0; }\r
153 \r
154         // a description to be shown on the installed plugins listing\r
155         function getDescription() {\r
156                 return '[$Revision: 1.12 $]<br />' . NP_DRAGANDDROPUPLOADER_DESCRIPTION;\r
157         }\r
158 \r
159         function supportsFeature($what) {\r
160                 switch ($what) {\r
161                         case 'SqlTablePrefix' :\r
162                         case 'HelpPage':\r
163                                 return 1;\r
164                         default :\r
165                                 return 0;\r
166                 }\r
167         }\r
168                         \r
169         function doAction($type) {\r
170                 global $member;\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
177                                 \r
178                 switch ($type) {\r
179                         case 'image_upload':\r
180                                 // 5 minutes execution time\r
181                                 @set_time_limit(5 * 60);\r
182                                 $this->_image_upload();\r
183                                 return '';\r
184                                 break;\r
185 \r
186                         // other actions result in an error\r
187                         case '':\r
188                         default:\r
189                                 return 'Unexisting action: ' . $type;\r
190                 }\r
191         }\r
192         \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
197                 exit;\r
198         }\r
199         \r
200         function _jsonrpc_ok(){\r
201                 echo('{"jsonrpc" : "2.0", "result" : null, "id" : "id"}');\r
202                 exit;\r
203         }\r
204         \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
209                 \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
215                         else\r
216                                 $fileName = strftime("%Y%m%d-", time()) . $fileName;\r
217                 }\r
218                                 \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
221                 \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
225                                 if($err){\r
226                                         $this->_jsonrpc_error('addMediaObject Error: '. $err, "500");\r
227                                 }\r
228                         } else\r
229                         $this->_jsonrpc_error("Failed to move uploaded file.", "500");\r
230                 } else {\r
231                         // Read binary input stream and append it to temp file\r
232                         $in = fopen("php://input", "rb");\r
233                         if ($in) {\r
234                                 while ($buff = fread($in, 4096))\r
235                                 $err = MEDIA::addMediaObjectRaw($collection, $fileName, $buff);\r
236                                 if($err){\r
237                                         $this->_jsonrpc_error('addMediaObjectRaw Error: '. $err, "500");\r
238                                 }\r
239                         } else\r
240                         $this->_jsonrpc_error("Failed to open input stream.", "500");\r
241                 }\r
242                 \r
243                 $this->_jsonrpc_ok();\r
244         }\r
245 }\r