OSDN Git Service

NP_DragAndDropUploader v1.1
[nucleus-jp/nucleus-plugins.git] / trunk / NP_DragAndDropUploader / NP_DragAndDropUploader.php
diff --git a/trunk/NP_DragAndDropUploader/NP_DragAndDropUploader.php b/trunk/NP_DragAndDropUploader/NP_DragAndDropUploader.php
new file mode 100644 (file)
index 0000000..21a353d
--- /dev/null
@@ -0,0 +1,245 @@
+<?php\r
+// vim: tabstop=2:shiftwidth=2\r
+\r
+/**\r
+  * NP_DragAndDropUploader ($Revision: 1.11 $)\r
+  * by hsur ( http://blog.cles.jp/np_cles )\r
+  * \r
+  * Based on upload.php (http://www.plupload.com)\r
+  * \r
+  * $Id: NP_DragAndDropUploader.php,v 1.11 2010/05/08 23:26:28 hsur Exp $\r
+*/\r
+\r
+/*\r
+  * Copyright (C) 2010 CLES. All rights reserved.\r
+  *\r
+  * This program is free software; you can redistribute it and/or\r
+  * modify it under the terms of the GNU General Public License\r
+  * as published by the Free Software Foundation; either version 2\r
+  * of the License, or (at your option) any later version.\r
+  * \r
+  * This program is distributed in the hope that it will be useful,\r
+  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
+  * GNU General Public License for more details.\r
+  * \r
+  * You should have received a copy of the GNU General Public License\r
+  * along with this program; if not, write to the Free Software\r
+  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA\r
+  * \r
+  * In addition, as a special exception, cles( http://blog.cles.jp/np_cles ) gives\r
+  * permission to link the code of this program with those files in the PEAR\r
+  * library that are licensed under the PHP License (or with modified versions\r
+  * of those files that use the same license as those files), and distribute\r
+  * linked combinations including the two. You must obey the GNU General Public\r
+  * License in all respects for all of the code used other than those files in\r
+  * the PEAR library that are licensed under the PHP License. If you modify\r
+  * this file, you may extend this exception to your version of the file,\r
+  * but you are not obligated to do so. If you do not wish to do so, delete\r
+  * this exception statement from your version.\r
+*/\r
+\r
+define('NP_DRAGANDDROPUPLOADER_RUNTIMES', 'html5,gears,flash,silverlight,browserplus');                \r
+\r
+// load class\r
+require_once(dirname(__FILE__).'/sharedlibs/sharedlibs.php');\r
+require_once('cles/Template.php');\r
+\r
+class NP_DragAndDropUploader extends NucleusPlugin {\r
+       function init(){\r
+               global $DIR_LIBS;\r
+               require_once($DIR_LIBS . 'MEDIA.php');          \r
+               \r
+               // include language file for this plugin \r
+               $language = ereg_replace( '[\\|/]', '', getLanguageName()); \r
+               if (file_exists($this->getDirectory().'language/'.$language.'.php')) \r
+                       @include_once($this->getDirectory().'language/'.$language.'.php');\r
+               \r
+               $this->te =& new cles_Template(dirname(__FILE__).'/draganddropuploader/template');\r
+       }\r
+       \r
+       // name of plugin\r
+       function getName() {\r
+               return 'Drag and Drop Uploader';\r
+       }\r
+\r
+       // author of plugin\r
+       function getAuthor() {\r
+               return 'hsur';\r
+       }\r
+\r
+       // an URL to the plugin website\r
+       // can also be of the form mailto:foo@bar.com\r
+       function getURL() {\r
+               return 'http://blog.cles.jp/np_cles/category/31/subcatid/25';\r
+       }\r
+\r
+       // version of the plugin\r
+       function getVersion() {\r
+               return '1.1';\r
+       }\r
+       \r
+       function hasAdminArea() {\r
+               return 1;\r
+       }\r
+       \r
+       function getEventList() {\r
+               return array('AddItemFormExtras','EditItemFormExtras','AdminPrePageHead','BookmarkletExtraHead');\r
+       }\r
+       \r
+    function event_AddItemFormExtras(&$data){\r
+               $this->_formExtras();\r
+       }\r
+\r
+    function event_EditItemFormExtras(&$data){\r
+               $this->_formExtras();\r
+       }\r
+       \r
+       function _formExtras(){\r
+               $collections = MEDIA::getCollectionList();\r
+               $collections_html = '<select id="plugin_draganddropuploader">';\r
+               foreach($collections as $k => $v){\r
+                       $collections_html .= '<option value="'.$k.'">'.$v.'</option>';\r
+               }\r
+               $collections_html .= '</select>';\r
+               \r
+               $tplVars = array(\r
+                       'collections' => $collections_html,\r
+               );\r
+               $index = $this->te->fetch('index', strtolower(__CLASS__));\r
+               echo $this->te->fill($index, $tplVars, false);\r
+       }\r
+       \r
+  function event_BookmarkletExtraHead(&$data){\r
+               $this->_extraHead($data);\r
+  }\r
+\r
+       function event_AdminPrePageHead(&$data){\r
+               if ( !($data['action'] == 'createitem' || $data['action'] == 'itemedit') ){\r
+                       return;\r
+               }\r
+               $this->_extraHead($data);\r
+       }\r
+\r
+       function _extraHead(&$data){\r
+               $tplVars = array(\r
+                       'plugindirurl' => $this->getAdminURL(),\r
+                       'runtimes' => NP_DRAGANDDROPUPLOADER_RUNTIMES,\r
+               );\r
+               $header = $this->te->fetch('extrahead', strtolower(__CLASS__));\r
+               $data['extrahead'] .= "\n" . $this->te->fill($header, $tplVars, false);\r
+    }\r
+       \r
+       function install() {\r
+               // debug\r
+               $this->createOption('debug', 'Debug Mode ?', 'yesno', 'no');\r
+       }\r
+       \r
+       function unInstall() {\r
+       }\r
+       \r
+       function _info($msg) {\r
+               if ($this->getOption('debug') == 'yes') {\r
+                       ACTIONLOG :: add(INFO, 'DragAndDropUploader: '.$msg);\r
+               }\r
+       }\r
+\r
+       function _warn($msg) {\r
+               ACTIONLOG :: add(WARNING, 'DragAndDropUploader: '.$msg);\r
+       }\r
+       \r
+       function getMinNucleusVersion() { return 330; }\r
+       function getMinNucleusPatchLevel() { return 0; }\r
+\r
+       // a description to be shown on the installed plugins listing\r
+       function getDescription() {\r
+               return '[$Revision: 1.11 $]<br />' . NP_DRAGANDDROPUPLOADER_DESCRIPTION;\r
+       }\r
+\r
+       function supportsFeature($what) {\r
+               switch ($what) {\r
+                       case 'SqlTablePrefix' :\r
+                       case 'HelpPage':\r
+                               return 1;\r
+                       default :\r
+                               return 0;\r
+               }\r
+       }\r
+                       \r
+       function doAction($type) {\r
+               global $member;\r
+               header('Content-type: text/plain; charset=UTF-8');\r
+               header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");\r
+               header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");\r
+               header("Cache-Control: no-store, no-cache, must-revalidate");\r
+               header("Cache-Control: post-check=0, pre-check=0", false);\r
+               header("Pragma: no-cache");\r
+                               \r
+               switch ($type) {\r
+                       case 'image_upload':\r
+                               // 5 minutes execution time\r
+                               @set_time_limit(5 * 60);\r
+                               $this->_image_upload();\r
+                               return '';\r
+                               break;\r
+\r
+                       // other actions result in an error\r
+                       case '':\r
+                       default:\r
+                               return 'Unexisting action: ' . $type;\r
+               }\r
+       }\r
+       \r
+       function _jsonrpc_error($mes = "", $code = "500"){\r
+               $this->_warn("($code) $mes");\r
+               header("HTTP/1.1 $code $mes");\r
+               echo('{"jsonrpc" : "2.0", "error" : {"code": '.$code.', "message": "'.$mes.'"}, "id" : "id"}');\r
+               exit;\r
+       }\r
+       \r
+       function _jsonrpc_ok(){\r
+               echo('{"jsonrpc" : "2.0", "result" : null, "id" : "id"}');\r
+               exit;\r
+       }\r
+       \r
+       function _image_upload(){               \r
+               global $member, $DIR_MEDIA, $CONF;\r
+               if( !$member->isLoggedIn() ) return $this->_jsonrpc_error("Not Logged in.", "403");\r
+               $collection = requestVar('collection');\r
+               \r
+               $fileName = isset($_REQUEST['name']) ? requestVar('name') : 'no_name';\r
+               if( $CONF['MediaPrefix'] ){\r
+                       $exif = @exif_read_data($_FILES['file']['tmp_name']);\r
+                       if( $exif !== false && $exif["DateTime"] && strtotime($exif["DateTime"]) !== false )\r
+                               $fileName = strftime("%Y%m%d-", strtotime($exif["DateTime"])) . $fileName;\r
+                       else\r
+                               $fileName = strftime("%Y%m%d-", time()) . $fileName;\r
+               }\r
+                               \r
+               if (isset($_SERVER['HTTP_CONTENT_TYPE'])) $contentType = serverVar('HTTP_CONTENT_TYPE');\r
+               if (isset($_SERVER['CONTENT_TYPE'])) $contentType = serverVar('CONTENT_TYPE');\r
+               \r
+               if (strpos($contentType, "multipart") !== false) {\r
+                       if (isset($_FILES['file']['tmp_name']) && is_uploaded_file($_FILES['file']['tmp_name'])) {\r
+                               $err = MEDIA::addMediaObject($collection, $_FILES['file']['tmp_name'], $fileName);\r
+                               if($err){\r
+                                       $this->_jsonrpc_error('addMediaObject Error: '. $err, "500");\r
+                               }\r
+                       } else\r
+                       $this->_jsonrpc_error("Failed to move uploaded file.", "500");\r
+               } else {\r
+                       // Read binary input stream and append it to temp file\r
+                       $in = fopen("php://input", "rb");\r
+                       if ($in) {\r
+                               while ($buff = fread($in, 4096))\r
+                               $err = MEDIA::addMediaObjectRaw($collection, $fileName, $buff);\r
+                               if($err){\r
+                                       $this->_jsonrpc_error('addMediaObjectRaw Error: '. $err, "500");\r
+                               }\r
+                       } else\r
+                       $this->_jsonrpc_error("Failed to open input stream.", "500");\r
+               }\r
+               \r
+               $this->_jsonrpc_ok();\r
+       }\r
+}\r