OSDN Git Service

ADD: Mocchiが手がけたファイル管理プラグイン一式をコミット
[nucleus-jp/nucleus-plugins.git] / NP_ImageLimitSize / NP_ImageLimitSize.php
1 <?php\r
2 /**\r
3  * ImageLimitSize plugin for Nucleus CMS\r
4  * Version 0.9.6 (1.0 RC2) for PHP5\r
5  * Written By Mocchi, Apr. 04, 2011\r
6  * Original code was written by Kai Greve and maintained by shizuki and yamamoto\r
7  * This plugin depends on NP_MediaUtils\r
8  * \r
9  * This program is free software; you can redistribute it and/or\r
10  * modify it under the terms of the GNU General Public License\r
11  * as published by the Free Software Foundation; either version 3\r
12  * of the License, or (at your option) any later version.\r
13  */\r
14 \r
15 class NP_ImageLimitSize extends NucleusPlugin {\r
16         public function getName()                       {return 'ImageLimitSize';}\r
17         public function getAuthor()             {return 'Mocchi, shizuki, yamamoto, Kai Greve';}\r
18         public function getURL()                        {return 'http://japan.nucleuscms.org/wiki/plugins:imagelimitsize';}\r
19         public function getVersion()            {return '0.9.6 (1.0 RC2)';}\r
20         public function getDescription()        {return _NP_IMAGELIMITSIZE_01;}\r
21         public function getPluginDep()  {return array('NP_MediaUtils');}\r
22         public function getMinNucleusVersion()          {return 360;}\r
23         public function supportsFeature($feature)       { return in_array ($feature, array ('SqlTablePrefix', 'SqlApi'));}\r
24         public function getEventList()  {return array('PrePluginOptionsEdit', 'PreMediaUpload', 'MediaUploadFormExtras');}\r
25         \r
26         public function install() {\r
27                 $this->createOption('maxwidth', '_NP_IMAGELIMITSIZE_02', 'text', '550', 'datatype=numerical');\r
28                 $this->createOption('maxheight', '_NP_IMAGELIMITSIZE_03', 'text', '0', 'datatype=numerical');\r
29                 $this->createBlogOption('status', '_NP_IMAGELIMITSIZE_04', 'yesno', 'yes');\r
30                 $this->createBlogOption('blog_maxwidth', '_NP_IMAGELIMITSIZE_05', 'text', '0', 'datatype=numerical');\r
31                 $this->createBlogOption('blog_maxheight', '_NP_IMAGELIMITSIZE_06', 'text', '0', 'datatype=numerical');\r
32                 return;\r
33         }\r
34         \r
35         public function uninstall() {\r
36                 // plugin options are purged automatically when uninstalled.\r
37                 return;\r
38         }\r
39         \r
40         public function init() {\r
41                 if((string)$_REQUEST['action'] == 'pluginlist' && !defined('_NP_IMAGELIMITSIZE_01')) {\r
42                         $language = preg_replace('#[/|\\\\]#', '', getLanguageName());\r
43                         if (file_exists($this->getDirectory() . $language.'.php')) {\r
44                                 include($this->getDirectory() . $language.'.php');\r
45                         } else {\r
46                                 include($this->getDirectory() . 'english.php');\r
47                         }\r
48                 }\r
49                 return;\r
50         }\r
51         \r
52 /*\r
53  * for translation\r
54  */\r
55         public function event_PrePluginOptionsEdit ($data) {\r
56                 if(!defined('_NP_IMAGELIMITSIZE_01')) {\r
57                         $language = preg_replace('#[/|\\\\]#', '', getLanguageName());\r
58                         if (file_exists($this->getDirectory() . $language.'.php')) {\r
59                                 include($this->getDirectory() . $language.'.php');\r
60                         } else {\r
61                                 include($this->getDirectory() . 'english.php');\r
62                         }\r
63                 }\r
64                 if ($data['context'] != 'global') {\r
65                         foreach($data['options'] as $key => $option) {\r
66                                 if ($option['pid'] == $this->getID()) {\r
67                                         if (defined($option['description'])) {\r
68                                                 $data['options'][$key]['description'] = constant($option['description']);\r
69                                         }\r
70                                         if ($option['type'] == 'select') {\r
71                                                 foreach (explode('|', $option['typeinfo']) as $option) {\r
72                                                         if (defined($option)) {\r
73                                                                 $data['options'][$key]['typeinfo'] = str_replace($option, constant($option), $data['options'][$key]['typeinfo']);\r
74                                                         }\r
75                                                 }\r
76                                         }\r
77                                 }\r
78                         }\r
79                 } else if ($data['plugid'] == $this->getID()) {\r
80                         foreach($data['options'] as $key => $option){\r
81                                 if (defined($option['description'])) {\r
82                                         $data['options'][$key]['description'] = constant($option['description']);\r
83                                 }\r
84                                 if ($option['type'] == 'select') {\r
85                                                 foreach (explode('|', $option['typeinfo']) as $option) {\r
86                                                         if (defined($option)) {\r
87                                                                 $data['options'][$key]['typeinfo'] = str_replace($option, constant($option), $data['options'][$key]['typeinfo']);\r
88                                                         }\r
89                                                 }\r
90                                 }\r
91                         }\r
92                 }\r
93                 return;\r
94         }\r
95         \r
96 /*\r
97  * for translation\r
98  */\r
99         static private function t($text,$array=array()) {\r
100                 if (is_array($array)) {\r
101                         $search = array();\r
102                         $replace = array();\r
103                         \r
104                         foreach ($array as $key => $value){\r
105                                 if (is_array($value)) {\r
106                                         continue;\r
107                                 }\r
108                                 $search[] = '<%'.preg_replace('/[^a-zA-Z0-9_]+/','',$key).'%>';\r
109                                 $replace[] = $value;\r
110                         }\r
111                 }\r
112                 return htmlspecialchars (str_replace($search, $replace, $text), ENT_QUOTES, _CHARSET);\r
113         }\r
114         \r
115         public function event_PreMediaUpload($data) {\r
116                 global $CONF, $manager;\r
117                 \r
118                 if (!class_exists('MediaUtils', FALSE)) {\r
119                         return;\r
120                 }\r
121                 \r
122                 if (MediaUtils::$blogid == 0) {\r
123                         return;\r
124                 }\r
125                 \r
126                 if ($this->getBlogOption(MediaUtils::$blogid, 'status') == 'no') {\r
127                         return;\r
128                 }\r
129                 \r
130                 if (0 == ($maxwidth = $this->getBlogOption(MediaUtils::$blogid, 'blog_maxwidth'))) {\r
131                         $maxwidth = $this->getOption('maxwidth');\r
132                 }\r
133                 \r
134                 if (0 == ($maxheight = $this->getBlogOption(MediaUtils::$blogid, 'blog_maxheight'))) {\r
135                         $maxheight = $this->getOption('maxheight');\r
136                 }\r
137                 \r
138                 $path = basename($data['uploadfile']);\r
139                 $root = str_replace('/' . $path, '', $data['uploadfile']);\r
140                 \r
141                 if (FALSE === ($medium = new MEDIUM($root, $path, MediaUtils::$prefix))) {\r
142                         return;\r
143                 }\r
144                 \r
145                 if (!array_key_exists($medium->mime, MediaUtils::$image_mime)\r
146                  || ($maxwidth >= $medium->width && $maxheight >= $medium->height)) {\r
147                         return;\r
148                 }\r
149                 \r
150                 if (FALSE === $medium->setResampledSize($maxwidth, $maxheight)) {\r
151                         return;\r
152                 }\r
153                 \r
154                 if (!MediaUtils::storeResampledImage($root, $path, $medium)) {\r
155                         return;\r
156                 }\r
157                 return;\r
158         }\r
159         \r
160         public function event_MediaUploadFormExtras() {\r
161                 echo '<input type="hidden" name="blogid" value="' . MediaUtils::$blogid . '" />' . "\n";\r
162                 return;\r
163         }\r
164 }\r