OSDN Git Service

FIX: NP_ImageLimitSizeをNucleus 3.65/PHP5.4/MySQL5.5で動作するよう修正
[nucleus-jp/nucleus-plugins.git] / NP_ImageLimitSize / NP_ImageLimitSize.php
1 <?php\r
2 /**\r
3  * ImageLimitSize plugin for Nucleus CMS\r
4  * Version 1.0.0 for PHP5\r
5  * Written By Mocchi, Oct. 20, 2012\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 {\r
17         public function getName()                       {return 'ImageLimitSize';}\r
18         public function getAuthor()             {return 'Mocchi, shizuki, yamamoto, Kai Greve';}\r
19         public function getURL()                        {return 'http://japan.nucleuscms.org/wiki/plugins:imagelimitsize';}\r
20         public function getVersion()            {return '1.0.0';}\r
21         public function getDescription()        {return _NP_IMAGELIMITSIZE_01;}\r
22         public function getPluginDep()  {return array('NP_MediaUtils');}\r
23         public function getMinNucleusVersion()          {return 360;}\r
24         public function supportsFeature($feature)       { return in_array ($feature, array ('SqlTablePrefix', 'SqlApi'));}\r
25         public function getEventList()  {return array('PrePluginOptionsEdit', 'PreMediaUpload', 'MediaUploadFormExtras');}\r
26         \r
27         public function install()\r
28         {\r
29                 $this->createOption('maxwidth', '_NP_IMAGELIMITSIZE_02', 'text', '550', 'datatype=numerical');\r
30                 $this->createOption('maxheight', '_NP_IMAGELIMITSIZE_03', 'text', '0', 'datatype=numerical');\r
31                 $this->createBlogOption('status', '_NP_IMAGELIMITSIZE_04', 'yesno', 'yes');\r
32                 $this->createBlogOption('blog_maxwidth', '_NP_IMAGELIMITSIZE_05', 'text', '0', 'datatype=numerical');\r
33                 $this->createBlogOption('blog_maxheight', '_NP_IMAGELIMITSIZE_06', 'text', '0', 'datatype=numerical');\r
34                 return;\r
35         }\r
36         \r
37         public function uninstall()\r
38         {\r
39                 // plugin options are purged automatically when uninstalled.\r
40                 return;\r
41         }\r
42         \r
43         public function init()\r
44         {\r
45                 $locale = '';\r
46                 \r
47                 /* new API */\r
48                 if ( class_exists('i18n', FALSE) )\r
49                 {\r
50                         $locale = i18n::get_current_locale() . '.' . i18n::get_current_charset() . '.php';\r
51                 }\r
52                 /* old API */\r
53                 else\r
54                 {\r
55                         $language = preg_replace('#[/|\\\\]#', '', getLanguageName());\r
56                         if ( $language == 'japanese-euc' )\r
57                         {\r
58                                 $locale = 'ja_Jpan_JP.EUC-JP.php';\r
59                         }\r
60                         else if ( $language = 'japanese-utf8' )\r
61                         {\r
62                                 $locale = 'ja_Jpan_JP.UTF-8.php';\r
63                         }\r
64                 }\r
65                 \r
66                 if ( !$locale || !file_exists($this->getDirectory() . $locale) )\r
67                 {\r
68                         include($this->getDirectory() . 'en_Latn_US.ISO-8859-1.php');\r
69                 }\r
70                 else\r
71                 {\r
72                         include($this->getDirectory() . $locale);\r
73                 }\r
74                 \r
75                 return;\r
76         }\r
77         \r
78         /*\r
79          * for translation\r
80          */\r
81         public function event_PrePluginOptionsEdit(&$data)\r
82         {\r
83                 /* Old version do not support natively */\r
84                 if ( getNucleusVersion() < 400  )\r
85                 {\r
86                         if ( $data['context'] != 'global' )\r
87                         {\r
88                                 foreach ( $data['options'] as $key => $option )\r
89                                 {\r
90                                         if ( $option['pid'] == $this->getID() )\r
91                                         {\r
92                                                 if ( defined($option['description']) )\r
93                                                 {\r
94                                                         $data['options'][$key]['description'] = constant($option['description']);\r
95                                                 }\r
96                                                 if ( $option['type'] == 'select' )\r
97                                                 {\r
98                                                         foreach ( explode('|', $option['typeinfo']) as $option )\r
99                                                         {\r
100                                                                 if ( defined($option) )\r
101                                                                 {\r
102                                                                         $data['options'][$key]['typeinfo'] = str_replace($option, constant($option), $data['options'][$key]['typeinfo']);\r
103                                                                 }\r
104                                                         }\r
105                                                 }\r
106                                         }\r
107                                 }\r
108                         }\r
109                         else if ($data['plugid'] == $this->getID() )\r
110                         {\r
111                                 foreach ( $data['options'] as $key => $option )\r
112                                 {\r
113                                         if ( defined($option['description']) )\r
114                                         {\r
115                                                 $data['options'][$key]['description'] = constant($option['description']);\r
116                                         }\r
117                                         if ( $option['type'] == 'select' )\r
118                                         {\r
119                                                         foreach ( explode('|', $option['typeinfo']) as $option )\r
120                                                         {\r
121                                                                 if ( defined($option) )\r
122                                                                 {\r
123                                                                         $data['options'][$key]['typeinfo'] = str_replace($option, constant($option), $data['options'][$key]['typeinfo']);\r
124                                                                 }\r
125                                                         }\r
126                                         }\r
127                                 }\r
128                         }\r
129                 }\r
130                 \r
131                 return;\r
132         }\r
133         \r
134         public function event_PreMediaUpload(&$data)\r
135         {\r
136                 global $CONF, $manager;\r
137                 \r
138                 if ( !class_exists('MediaUtils', FALSE) )\r
139                 {\r
140                         return;\r
141                 }\r
142                 \r
143                 if ( MediaUtils::$blogid == 0 )\r
144                 {\r
145                         return;\r
146                 }\r
147                 \r
148                 if ( $this->getBlogOption(MediaUtils::$blogid, 'status') == 'no' )\r
149                 {\r
150                         return;\r
151                 }\r
152                 \r
153                 if ( 0 == ($maxwidth = $this->getBlogOption(MediaUtils::$blogid, 'blog_maxwidth')) )\r
154                 {\r
155                         $maxwidth = $this->getOption('maxwidth');\r
156                 }\r
157                 \r
158                 if ( 0 == ($maxheight = $this->getBlogOption(MediaUtils::$blogid, 'blog_maxheight')) )\r
159                 {\r
160                         $maxheight = $this->getOption('maxheight');\r
161                 }\r
162                 \r
163                 $path = basename($data['uploadfile']);\r
164                 $root = str_replace('/' . $path, '', $data['uploadfile']);\r
165                 \r
166                 if ( FALSE === ($medium = new MEDIUM($root, $path, MediaUtils::$prefix)) )\r
167                 {\r
168                         return;\r
169                 }\r
170                 \r
171                 if ( !array_key_exists($medium->mime, MediaUtils::$image_mime)\r
172                    || ($maxwidth >= $medium->width && $maxheight >= $medium->height) )\r
173                 {\r
174                         return;\r
175                 }\r
176                 \r
177                 if ( FALSE === $medium->setResampledSize($maxwidth, $maxheight) )\r
178                 {\r
179                         return;\r
180                 }\r
181                 \r
182                 if ( !MediaUtils::storeResampledImage($root, $path, $medium) )\r
183                 {\r
184                         return;\r
185                 }\r
186                 return;\r
187         }\r
188         \r
189         public function event_MediaUploadFormExtras()\r
190         {\r
191                 echo '<input type="hidden" name="blogid" value="' . MediaUtils::$blogid . '" />' . "\n";\r
192                 return;\r
193         }\r
194 }\r