OSDN Git Service

3.15 taka-san version
[nucleus-jp/nucleus-jp-ancient.git] / utf8 / nucleus / media.php
1 <?php\r
2 /**\r
3   * Nucleus: PHP/MySQL Weblog CMS (http://nucleuscms.org/) \r
4   * Copyright (C) 2002-2004 The Nucleus Group\r
5   *\r
6   * This program is free software; you can redistribute it and/or\r
7   * modify it under the terms of the GNU General Public License\r
8   * as published by the Free Software Foundation; either version 2\r
9   * of the License, or (at your option) any later version.\r
10   * (see nucleus/documentation/index.html#license for more info)\r
11   *\r
12   * Media popup window for Nucleus\r
13   *\r
14   * Purpose:\r
15   *   - can be openen from an add-item form or bookmarklet popup\r
16   *   - shows a list of recent files, allowing browsing, search and \r
17   *     upload of new files\r
18   *   - close the popup by selecting a file in the list. The file gets\r
19   *     passed through to the add-item form (linkto, popupimg or inline img)\r
20   */\r
21   \r
22 $CONF = array();\r
23 \r
24 // defines how much media items will be shown per page. You can override this\r
25 // in config.php if you like. (changing it in config.php instead of here will\r
26 // allow your settings to be kept even after a Nucleus upgrade)\r
27 $CONF['MediaPerPage'] = 10;\r
28 \r
29 // include all classes and config data\r
30 include('../config.php');\r
31 include($DIR_LIBS . 'MEDIA.php');       // media classes\r
32 \r
33 sendContentType('application/xhtml+xml', 'media');\r
34 \r
35 // user needs to be logged in to use this\r
36 if (!$member->isLoggedIn()) {\r
37         media_loginAndPassThrough();\r
38         exit;\r
39 }\r
40 \r
41 // check if member is on at least one teamlist\r
42 $query = 'SELECT * FROM ' . sql_table('team'). ' WHERE tmember=' . $member->getID();\r
43 $teams = mysql_query($query);\r
44 if (mysql_num_rows($teams) == 0)\r
45         media_doError(_ERROR_DISALLOWEDUPLOAD);\r
46         \r
47 // basic action:\r
48 $action = requestVar('action');\r
49 switch($action) {\r
50         case 'chooseupload':\r
51         case _MEDIA_UPLOAD_TO:\r
52         case _MEDIA_UPLOAD_NEW:\r
53                 media_choose();\r
54                 break;\r
55         case 'uploadfile':\r
56                 media_upload();\r
57                 break;\r
58         case _MEDIA_FILTER_APPLY:\r
59         case 'selectmedia':\r
60         case _MEDIA_COLLECTION_SELECT:\r
61         default:\r
62                 media_select();\r
63                 break;\r
64 }\r
65 \r
66 // select a file\r
67 function media_select() {\r
68         global $member, $CONF, $DIR_MEDIA;\r
69         \r
70         media_head();\r
71         \r
72         // show 10 files + navigation buttons \r
73         // show msg when no files\r
74         // show upload form\r
75         // files sorted according to last modification date\r
76 \r
77         // currently selected collection\r
78         $currentCollection = requestVar('collection');\r
79         if (!$currentCollection || !@is_dir($DIR_MEDIA . $currentCollection))\r
80                 $currentCollection = $member->getID();\r
81                 \r
82         \r
83         // get collection list\r
84         $collections = MEDIA::getCollectionList();\r
85 \r
86         if (sizeof($collections) > 1) {\r
87         ?>\r
88                 <form method="post" action="media.php"><div>\r
89                         <label for="media_collection"><?php echo htmlspecialchars(_MEDIA_COLLECTION_LABEL)?></label>\r
90                         <select name="collection" id="media_collection">\r
91                                 <?php                                   foreach ($collections as $dirname => $description) {\r
92                                                 echo '<option value="',htmlspecialchars($dirname),'"';\r
93                                                 if ($dirname == $currentCollection) {\r
94                                                         echo ' selected="selected"';\r
95                                                 }\r
96                                                 echo '>',htmlspecialchars($description),'</option>';\r
97                                         }\r
98                                 ?>\r
99                         </select>\r
100                         <input type="submit" name="action" value="<?php echo htmlspecialchars(_MEDIA_COLLECTION_SELECT) ?>" title="<?php echo htmlspecialchars(_MEDIA_COLLECTION_TT)?>" />\r
101                         <input type="submit" name="action" value="<?php echo htmlspecialchars(_MEDIA_UPLOAD_TO) ?>" title="<?php echo htmlspecialchars(_MEDIA_UPLOADLINK) ?>" />\r
102                 </div></form>\r
103         <?php   } else {\r
104         ?>\r
105                 <form method="post" action="media.php" style="float:right"><div>\r
106                         <input type="hidden" name="collection" value="<?php echo htmlspecialchars($currentCollection)?>" />\r
107                         <input type="submit" name="action" value="<?php echo htmlspecialchars(_MEDIA_UPLOAD_NEW) ?>" title="<?php echo htmlspecialchars(_MEDIA_UPLOADLINK) ?>" />\r
108                 </div></form>   \r
109         <?php   } // if sizeof\r
110         \r
111         $filter = requestVar('filter'); \r
112         $offset = intRequestVar('offset');      \r
113         $arr = MEDIA::getMediaListByCollection($currentCollection, $filter);\r
114 \r
115         ?>\r
116                 <form method="post" action="media.php"><div>\r
117                         <label for="media_filter"><?php echo htmlspecialchars(_MEDIA_FILTER_LABEL)?></label>\r
118                         <input id="media_filter" type="text" name="filter" value="<?php echo htmlspecialchars($filter)?>" />\r
119                         <input type="submit" name="action" value="<?php echo htmlspecialchars(_MEDIA_FILTER_APPLY) ?>" />\r
120                         <input type="hidden" name="collection" value="<?php echo htmlspecialchars($currentCollection)?>" />                     \r
121                         <input type="hidden" name="offset" value="<?php echo intval($offset)?>" />                                              \r
122                 </div></form>   \r
123         \r
124         <?php\r
125         \r
126         ?>      \r
127                 <table width="100%">\r
128                 <caption><?php echo _MEDIA_COLLECTION_LABEL . htmlspecialchars($collections[$currentCollection])?></caption>\r
129                 <tr>\r
130                  <th><?php echo _MEDIA_MODIFIED?></th><th><?php echo _MEDIA_FILENAME?></th><th><?php echo _MEDIA_DIMENSIONS?></th>\r
131                 </tr>\r
132         \r
133         <?php   \r
134         \r
135         if (sizeof($arr)>0) {\r
136         \r
137                 if (($offset + $CONF['MediaPerPage']) >= sizeof($arr))\r
138                         $offset = sizeof($arr) - $CONF['MediaPerPage'];\r
139 \r
140                 if ($offset < 0) $offset = 0;\r
141                 \r
142                 $idxStart = $offset;\r
143                 $idxEnd = $offset + $CONF['MediaPerPage'];\r
144                 $idxNext = $idxEnd;\r
145                 $idxPrev = $idxStart - $CONF['MediaPerPage'];\r
146 \r
147                 if ($idxPrev < 0) $idxPrev = 0;\r
148 \r
149                 if ($idxEnd > sizeof($arr))\r
150                         $idxEnd = sizeof($arr);\r
151 \r
152                 for($i=$idxStart;$i<$idxEnd;$i++) {\r
153                         $obj = $arr[$i];\r
154                         $filename = $DIR_MEDIA . $currentCollection . '/' . $obj->filename;\r
155 \r
156                         $old_level = error_reporting(0);\r
157                         $size = @GetImageSize($filename); \r
158                         error_reporting($old_level);\r
159                         $width = $size[0];\r
160                         $height = $size[1];\r
161                         $filetype = $size[2];\r
162                         \r
163                         echo "<tr>";\r
164                         echo "<td>". date("Y-m-d",$obj->timestamp) ."</td>";\r
165 \r
166                         if ($filetype != 0) {\r
167                                 // image (gif/jpg/png/swf)\r
168                                 echo "<td><a href='media.php' onclick='chooseImage(\"$currentCollection\",\"$obj->filename\","\r
169                                                            . "\"$width\",\"$height\""\r
170                                                            . ")' title='" . htmlspecialchars($obj->filename). "'>"\r
171                                                            . htmlspecialchars(shorten($obj->filename,25,'...'))\r
172                                                            ."</a>";\r
173                                 echo ' (<a href="', htmlspecialchars($CONF['MediaURL'] . $currentCollection . '/' . $obj->filename), '" onclick="window.open(this.href); return false;" title="',htmlspecialchars(_MEDIA_VIEW_TT),'">',_MEDIA_VIEW,'</a>)';\r
174                                 echo "</td>";\r
175                         } else {\r
176                                 // no image (e.g. mpg)\r
177                                 echo "<td><a href='media.php' onclick='chooseOther(\"$currentCollection\",\"$obj->filename\""\r
178                                                            . ")' title='" . htmlspecialchars($obj->filename). "'>"\r
179                                                            . htmlspecialchars(shorten($obj->filename,30,'...'))\r
180                                                            ."</a></td>";\r
181 \r
182                         }\r
183                         echo '<td>' , $width , 'x' , $height , '</td>';\r
184                         echo '</tr>';\r
185                 }\r
186         } // if (sizeof($arr)>0)\r
187         ?>\r
188         \r
189                 </table>\r
190         <?php   \r
191         if ($idxStart > 0)\r
192                 echo "<a href='media.php?offset=$idxPrev&amp;collection=".urlencode($currentCollection)."'>". _LISTS_PREV."</a> ";\r
193         if ($idxEnd < sizeof($arr))\r
194                 echo "<a href='media.php?offset=$idxNext&amp;collection=".urlencode($currentCollection)."'>". _LISTS_NEXT."</a> ";\r
195         \r
196         ?>\r
197                 <input id="typeradio0" type="radio" name="typeradio" onclick="setType(0);" checked="checked" /><label for="typeradio0"><?php echo _MEDIA_INLINE?></label>\r
198                 <input id="typeradio1" type="radio" name="typeradio" onclick="setType(1);" /><label for="typeradio1"><?php echo _MEDIA_POPUP?></label>\r
199         <?php   \r
200         media_foot();\r
201      \r
202                 \r
203 }\r
204 \r
205 /**\r
206   * Shows a screen where you can select the file to upload\r
207   */\r
208 function media_choose() {\r
209         global $CONF, $member;\r
210 \r
211         $currentCollection = requestVar('collection');\r
212         \r
213         $collections = MEDIA::getCollectionList();\r
214 \r
215         media_head();\r
216         ?>\r
217         <h1><?php echo _UPLOAD_TITLE?></h1>\r
218         \r
219         <p><?php echo _UPLOAD_MSG?></p>\r
220         \r
221         <form method="post" enctype="multipart/form-data" action="media.php">\r
222         <div>\r
223           <input type="hidden" name="action" value="uploadfile" />\r
224           <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $CONF['MaxUploadSize']?>" />\r
225           File:\r
226           <br />\r
227           <input name="uploadfile" type="file" size="40" />\r
228         <?php           if (sizeof($collections) > 1) {\r
229         ?>\r
230                 <br /><br /><label for="upload_collection">Collection:</label>\r
231                 <br /><select name="collection" id="upload_collection">\r
232                         <?php                           foreach ($collections as $dirname => $description) {\r
233                                         echo '<option value="',htmlspecialchars($dirname),'"';\r
234                                         if ($dirname == $currentCollection) {\r
235                                                 echo ' selected="selected"';\r
236                                         }\r
237                                         echo '>',htmlspecialchars($description),'</option>';\r
238                                 }\r
239                         ?>\r
240                 </select>\r
241         <?php           } else {\r
242         ?>\r
243                 <input name="collection" type="hidden" value="<?php echo htmlspecialchars(requestVar('collection'))?>" />                       \r
244         <?php           } // if sizeof\r
245         ?>  \r
246           <br /><br />\r
247           <input type="submit" value="<?php echo _UPLOAD_BUTTON?>" />\r
248         </div>\r
249         </form>\r
250         \r
251         <?php   \r
252         media_foot();\r
253 }\r
254 \r
255 \r
256 /**\r
257   * accepts a file for upload\r
258   */\r
259 function media_upload() {\r
260         global $DIR_MEDIA, $member, $CONF;\r
261 \r
262         $uploadInfo = postFileInfo('uploadfile');\r
263         \r
264         $filename = $uploadInfo['name'];\r
265         $filetype = $uploadInfo['type'];\r
266         $filesize = $uploadInfo['size'];\r
267         $filetempname = $uploadInfo['tmp_name'];\r
268         \r
269         if ($filesize > $CONF['MaxUploadSize'])\r
270                 media_doError(_ERROR_FILE_TOO_BIG);\r
271         \r
272         // check file type against allowed types\r
273         $ok = 0;\r
274         $allowedtypes = explode (',', $CONF['AllowedTypes']);\r
275         foreach ( $allowedtypes as $type ) \r
276                 if (eregi("\." .$type. "$",$filename)) $ok = 1;    \r
277         if (!$ok) media_doError(_ERROR_BADFILETYPE);\r
278                 \r
279         if (!is_uploaded_file($filetempname)) \r
280                 media_doError(_ERROR_BADREQUEST);\r
281 \r
282         // prefix filename with current date (YYYY-MM-DD-)\r
283         // this to avoid nameclashes\r
284         if ($CONF['MediaPrefix'])\r
285                 $filename = strftime("%Y%m%d-", time()) . $filename;\r
286 \r
287         $collection = requestVar('collection');\r
288         $res = MEDIA::addMediaObject($collection, $filetempname, $filename);\r
289 \r
290         if ($res != '') \r
291                 media_doError($res);\r
292         \r
293         // shows updated list afterwards\r
294         media_select();\r
295 }\r
296 \r
297 function media_loginAndPassThrough() {\r
298         media_head();\r
299         ?>\r
300                 <h1><?php echo _LOGIN_PLEASE?></h1>\r
301         \r
302                 <form method="post" action="media.php">\r
303                 <div>\r
304                         <input name="action" value="login" type="hidden" />\r
305                         <input name="collection" value="<?php echo htmlspecialchars(requestVar('collection'))?>" type="hidden" />                       \r
306                         <?php echo _LOGINFORM_NAME?>: <input name="login" />\r
307                         <br /><?php echo _LOGINFORM_PWD?>: <input name="password" type="password" />\r
308                         <br /><input type="submit" value="<?php echo _LOGIN?>" />\r
309                 </div>\r
310                 </form>\r
311                 <p><a href="media.php" onclick="window.close();"><?php echo _POPUP_CLOSE?></a></p>\r
312         <?php   media_foot();\r
313         exit;\r
314 }\r
315 \r
316 function media_doError($msg) {\r
317         media_head();\r
318         ?>\r
319         <h1><?php echo _ERROR?></h1>\r
320         <p><?php echo $msg?></p>\r
321         <p><a href="media.php" onclick="history.back()"><?php echo _BACK?></a></p>\r
322         <?php   media_foot();\r
323         exit;\r
324 }\r
325 \r
326 \r
327 function media_head() {\r
328 ?>\r
329         <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">\r
330         <html xmlns="http://www.w3.org/1999/xhtml">\r
331         <head>\r
332                 <meta http-equiv="Content-Type" content="text/html; charset=<?php echo _CHARSET ?>" />\r
333                 <title>Nucleus Media</title>\r
334                 <link rel="stylesheet" type="text/css" href="styles/popups.css" />\r
335                 <script type="text/javascript">\r
336                         var type = 0;\r
337                         function setType(val) { type = val; }\r
338                         \r
339                         function chooseImage(collection, filename, width, height) {\r
340                                 window.opener.focus(); \r
341                                 window.opener.includeImage(collection,\r
342                                                                                    filename, \r
343                                                            type == 0 ? 'inline' : 'popup',\r
344                                                            width,\r
345                                                            height\r
346                                                            );\r
347                                 window.close();\r
348                         }\r
349                         \r
350                         function chooseOther(collection, filename) {\r
351                                 window.opener.focus(); \r
352                                 window.opener.includeOtherMedia(collection, filename);\r
353                                 window.close();\r
354                         \r
355                         }\r
356                 </script>\r
357         </head>\r
358         <body>          \r
359 <?php }\r
360 \r
361 function media_foot() {\r
362 ?>\r
363         </body>\r
364         </html> \r
365 <?php } \r
366 \r
367 ?>\r