OSDN Git Service

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