OSDN Git Service

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