OSDN Git Service

Checked
[nucleus-jp/nucleus-plugins.git] / NP_gallery / trunk / gallery / add_picture.php
1 <style type="text/css">\r
2 <!--\r
3 body,td,th {\r
4         font-family: Courier New, Courier, monospace;\r
5         font-size: 12px;\r
6         color: #000000;\r
7 }\r
8 body {\r
9         margin-left: 20px;\r
10         margin-top: 20px;\r
11 }\r
12 -->\r
13 </style>\r
14 <?php\r
15 \r
16 //add_picture.php\r
17 include('../../../config.php');\r
18 include_once(dirname(__FILE__).'/config.php'); //gallery config\r
19 include_once($DIR_LIBS . 'ITEM.php');\r
20 \r
21 \r
22 class NPG_PROMO_ACTIONS extends BaseActions {\r
23 \r
24         var $parser;\r
25         var $thumbnails;\r
26         var $template;\r
27         var $CurrentThumb;\r
28 \r
29         function NPG_PROMO_ACTIONS() {\r
30                 $this->BaseActions();\r
31         }\r
32 \r
33         function getdefinedActions() {\r
34                 return array(\r
35                         'images',\r
36                         'thumbnail',\r
37                         'centeredtopmargin',\r
38                         'picturelink',\r
39                         'description'\r
40                         );      \r
41         }\r
42         \r
43         function setTemplate(&$template) {$this->template = &$template;}\r
44         function setParser(&$parser) {$this->parser =& $parser; }\r
45         function setCurrentThumb(&$CurrentThumb) {$this->CurrentThumb = &$CurrentThumb;}\r
46         \r
47         function addimagethumbnail($thumbnails) {$this->thumbnails = $this->thumbnails . $thumbnails;}\r
48         function needtoaddpromo() {if($this->thumbnails <> '') return true; return false;}\r
49         \r
50         function parse_images() { echo $this->thumbnails; }\r
51         function parse_description() {echo $this->CurrentThumb->getdescription(); }\r
52         function parse_thumbnail() {echo $this->CurrentThumb->getthumbfilename();}\r
53         function parse_picturelink() {echo '<%gallery(link,picture,'.$this->CurrentThumb->getID().')%>';}\r
54         function parse_centeredtopmargin($height = 140,$adjustment = 0) {\r
55                 $image_size = getimagesize($this->CurrentThumb->getthumbfilename());\r
56                 $topmargin = ((intval($height) - intval($image_size[1])) / 2) + intval($adjustment);\r
57                 echo 'margin-top: '.$topmargin.'px;';\r
58         }\r
59 }\r
60 \r
61 //globals for add_picture.php\r
62 global $NPG_CONF,$gmember,$manager,$NP_BASE_DIR;\r
63 \r
64 if(!$NPG_CONF['temp_table']) {\r
65         $NPG_CONF['temp_table'] = 'gallery_temp';\r
66         setNPGoption('temp_table','gallery_temp');\r
67 }\r
68 \r
69 if(!$NPG_CONF['batch_add_num']) $NPG_CONF['batch_add_num'] = 10;\r
70 \r
71 //todo: display header\r
72 \r
73 if (!preg_match('/^([a-z0-9_]+|`[^`]+`)$/i',$NPG_CONF['temp_table'])) exit;\r
74 $type = requestvar('type');\r
75 switch($type) {\r
76         case 'firststage':\r
77                         $exist_temp_table = mysql_query('SELECT 1 FROM '.$NPG_CONF['temp_table'].' LIMIT 0');\r
78                         if ($exist_temp_table) sql_query('drop table '. $NPG_CONF['temp_table']);\r
79                 if(requestVar('id')) $albumid = requestVar('id'); else $albumid = 0;\r
80                 $number_of_uploads=$NPG_CONF['batch_add_num'];\r
81                 addPictureForm($albumid, $number_of_uploads);\r
82                 break;\r
83         case 'secondstage':\r
84                 if(requestVar('id')) $albumid = requestVar('id'); else $albumid = 0;\r
85                 $result = mysql_query('create table '.$NPG_CONF['temp_table']\r
86                                                 .'(tempid int unsigned not null auto_increment PRIMARY KEY, '\r
87                                                 .'memberid int unsigned, '\r
88                                                 .'albumid int unsigned, '\r
89                                                 .'filename varchar(60), '\r
90                                                 .'intfilename varchar(60), '\r
91                                                 .'thumbfilename varchar(60), '\r
92                                                 .'title varchar(40), '\r
93                                                 .'description varchar(255), '\r
94                                                 .'promote tinyint unsigned default 0, '\r
95                                                 .'error varchar(60) default NULL)' );\r
96 \r
97                 $i=0;\r
98                 while ($uploadInfo = postFileInfo('uploadpicture'.$i)) {\r
99                         if ($filename = $uploadInfo['name']) {\r
100                                 $filetype = $uploadInfo['type'];\r
101                                 $filesize = $uploadInfo['size'];\r
102                                 $filetempname = $uploadInfo['tmp_name'];\r
103                                 //this gets picasa captions from ITPC metadata\r
104                                 $size = getimagesize($filetempname, $info);\r
105                                 if (isset($info["APP13"])) {\r
106                                         $iptc = iptcparse($info["APP13"]);\r
107                                         $description = $iptc["2#120"][0];\r
108                                 }\r
109                                 //adds a picture to the temp table so user can add description, etc before actually adding to database\r
110                                 add_temp($albumid, $filename, $filetype, $filesize, $filetempname,$description);\r
111                                 }\r
112                         $i++;\r
113                 }\r
114                 addTempPictureForm($albumid);\r
115                 break;\r
116         case 'massupload' :\r
117                 //if the referring address is not this page, drop the table.\r
118                 if(!stristr($_SERVER['HTTP_REFERER'],'add_picture.php')) {\r
119                         $exist_temp_table = mysql_query('SELECT 1 FROM '.$NPG_CONF['temp_table'].' LIMIT 0');\r
120                         if ($exist_temp_table) sql_query('drop table '. $NPG_CONF['temp_table']);\r
121                         echo 'starting a fresh massupload batch </br>';\r
122                 }\r
123                 else echo '...continuing a massupload batch </br>';\r
124                 //create a table (if the table is already there, mysql just adds to the table)\r
125                 if(requestVar('id')) $albumid = requestVar('id'); else $albumid = 0;\r
126                 $result = mysql_query('create table '.$NPG_CONF['temp_table']\r
127                   .'(tempid int unsigned not null auto_increment PRIMARY KEY, '\r
128                   .'memberid int unsigned, '\r
129                   .'albumid int unsigned, '\r
130                   .'filename varchar(60), '\r
131                   .'intfilename varchar(60), '\r
132                   .'thumbfilename varchar(60), '\r
133                   .'title varchar(40), '\r
134                   .'description varchar(255), '\r
135                   .'promote tinyint unsigned default 0, '\r
136                   .'error varchar(60) default NULL)' );\r
137       //checks to see how many files are in the upload directory\r
138                 if ($handle = opendir($NP_BASE_DIR.'upload')) {\r
139                                 while (false !== ($filename = readdir($handle))) {\r
140                                         if (stristr($filename,'jpeg') or stristr($filename,'jpg')){\r
141                                                 $scandir[] = $filename;\r
142                                         }\r
143                                 }\r
144                                 closedir($handle);\r
145                                 $numpics = count($scandir);\r
146 \r
147          echo 'there are ' . $numpics .' pictures(jpg) left in the upload directory </br>';\r
148          echo 'the massupload script will loop though '.$NPG_CONF['batch_add_num'].' pictures per time to prevent timeouts</br>';\r
149          //if there are more than 10 files, remember to refresh, \r
150          //this is to solve the incomplete database writes when scripts timeout\r
151          //have to reopen the directory so it starts from the first file again\r
152          $handle = opendir($NP_BASE_DIR.'upload');\r
153          for ( $i=0; false !==($file=readdir($handle)) and $i<=$NPG_CONF['batch_add_num']; $i++ ) {\r
154                         if (stristr($file,'jpeg') or stristr($file,'jpg')){\r
155                                         $filename = $file;\r
156                                         //echo $filename . 'uploaded </br>';\r
157                                 $filetype = filetype($NP_BASE_DIR.'upload/' . $file);\r
158                                 $filesize = filesize($NP_BASE_DIR.'upload/' . $file);\r
159                                 $filetempname = $NP_BASE_DIR.'upload/' . $file;\r
160                                 $size = getimagesize($filetempname, $info);\r
161                                         //this gets picasa captions from ITPC metadata\r
162                                                 if (isset($info["APP13"])) {\r
163                                                         $iptc = iptcparse($info["APP13"]);\r
164                                                         $description = $iptc["2#120"][0];\r
165                                                         $description = addslashes($description);\r
166                                                 }\r
167                                         //adds a picture to the temp table so user can add description, etc before actually adding to database\r
168                                         echo 'creating thumbnail for ';\r
169                                         echo $filename;\r
170                                         add_temp($albumid, $filename, $filetype, $filesize, $filetempname,$description);\r
171                                         echo '.... done. </br>';\r
172                                         }         \r
173          }\r
174          closedir($handle);\r
175       }\r
176       //if there were more than 10 files, refresh, else proceed.\r
177       if($numpics > $NPG_CONF['batch_add_num']){\r
178                 echo "<SCRIPT LANGUAGE=\"JavaScript\">window.location=\" ".$_SERVER['SCRIPT_NAME']."?type=massupload&id=".$albumid." \";</script>";\r
179                 }\r
180         addTempPictureForm($albumid);\r
181         break;\r
182         \r
183         case 'addpictures':\r
184                 $i=0;\r
185                 $promoallowed = postvar('promopost');\r
186                 $promo_ids = array();\r
187                 $setorpromo = $NPG_CONF['setorpromo'];\r
188                 \r
189                 if(!$NPG_CONF['template']) $NPG_CONF['template'] = 1;\r
190                 $template = & new NPG_TEMPLATE($NPG_CONF['template']);\r
191                 \r
192                 $actions = new NPG_PROMO_ACTIONS();\r
193                 $parser = new PARSER($actions->getdefinedActions(),$actions);\r
194                 $actions->setparser($parser);\r
195                 $actions->settemplate($template);\r
196                 \r
197                 while($tempid = postvar('tid'.$i)) {\r
198                         $title = postvar('title'.$i);\r
199                         $description = postvar('description'.$i);\r
200                         $promote = postvar('promote'.$i);\r
201                         $albumid = postvar('album'.$i);\r
202 \r
203                         $filename = $NPG_CONF['galleryDir'].'/'.postvar('filename'.$i);\r
204                         $int_filename = postvar('intfilename'.$i);\r
205                         $thumb_filename = postvar('thumbfilename'.$i);\r
206                         $keywords = postvar('keywords'.$i);\r
207 \r
208                         //check permissions to add\r
209                         if($gmember->canAddPicture($albumid)) {\r
210                                 $pict = new PICTURE(0);\r
211                                 $newid = $pict->add_new($albumid, $filename, $title, $description, $int_filename, $thumb_filename,$keywords);\r
212                                 echo "$filename added<br/>";\r
213                                 \r
214                                 //promotion post\r
215                                 if ($promoallowed == '1' && $promote == 'yes') {\r
216                                         ob_start();\r
217                                         $actions->setCurrentThumb($pict);\r
218                                         $parser->parse($template->section['PROMO_IMAGES']);\r
219                                         $capt = ob_get_contents();\r
220                                         ob_end_clean();\r
221                                         $actions->addimagethumbnail($capt);\r
222                                         array_push($promo_ids, $newid);\r
223                                 }\r
224                                 unset($pict);\r
225                                 \r
226                                 $manager->notify('NPgPostAddPicture',array('pictureid' => $newid));\r
227                         }\r
228                         else {\r
229                                 echo "$filename not added due to bad album permissions<br/>";\r
230                                 //delete files\r
231                                 if(file_exists($galleryDir.'/'.$filename))  unlink($galleryDir.'/'.$filename);\r
232                                 if(file_exists($int_filename))  unlink($int_filename);\r
233                                 if(file_exists($thumb_filename))  unlink($thumb_filename);\r
234                                 \r
235                         }\r
236 \r
237                         $i++;\r
238                 }\r
239 \r
240                 if ($promoallowed == '1' && $actions->needtoaddpromo() ) {\r
241 \r
242                         $today = getdate();\r
243                         $hour = $today['hours'];\r
244                         $minutes = $today['minutes'];\r
245                         $day = $today['mday'];\r
246                         $month = $today['mon'];\r
247                         $year = $today['year'];\r
248                         \r
249                         \r
250                         ob_start();\r
251                         $parser->parse($template->section['PROMO_TITLE']);\r
252                         $title = ob_get_contents();\r
253                         ob_end_clean();\r
254                         if ($setorpromo=='promo'){\r
255                                 ob_start();\r
256                                 $parser->parse($template->section['PROMO_BODY']);\r
257                                 $body = ob_get_contents();\r
258                                 ob_end_clean();\r
259                         }\r
260                         if ($setorpromo=='sets'){\r
261                                 $body = '<%gallery(keywords,'.$promokeywords.',desc)%>';\r
262                         }\r
263 \r
264                         ?>\r
265                                 <br/><hr/><br/>\r
266                                 <form><input type="button" value="<?php echo __NPG_PROMO_FORM_CANCEL; ?>" onclick="window.close()"/></form>\r
267                                 <form method="post" action="add_picture.php"><div>\r
268                                         <input type="hidden" name="type" value="promopost" />\r
269                                         <input type="hidden" name="catid" value="<?php echo $NPG_CONF['blog_cat']; ?>" />\r
270                                         <input type="hidden" name="hour" value="<?php echo $hour; ?>" />\r
271                                         <input type="hidden" name="minutes" value="<?php echo $minutes; ?>" />\r
272                                         <input type="hidden" name="day" value="<?php echo $day; ?>" />\r
273                                         <input type="hidden" name="year" value="<?php echo $year; ?>" />\r
274                                         <input type="hidden" name="month" value="<?php echo $month; ?>" />\r
275                                         <input type="hidden" name="ids" value="<?php echo implode(",",$promo_ids); ?>" />\r
276                                         \r
277                                         <p><label for "title_f"><?php echo __NPG_PROMO_FORM_TITLE.'<br/>'; ?></label>\r
278                                         <input type="text" name="title" id="title_f" value="<?php echo $title; ?>" /></p>\r
279                                         \r
280                                         <p><label for "body_f"><?php echo __NPG_PROMO_FORM_BODY.'<br/>'; ?></label>\r
281                                         <textarea rows="10" cols="50" name="body" id="body_f" >\r
282                                         <?php echo $body; ?></textarea></p>\r
283                                         <input type="submit" value="<?php echo __NPG_PROMO_FORM_SUBMIT; ?>" />\r
284                                         <br/>\r
285                                         \r
286                         <?php\r
287                         \r
288                 }\r
289                 else {\r
290                         echo '<br/><hr/><br/>';\r
291                         echo '<form><input type="button" value="'.__NPG_PROMO_FORM_CLOSE.'" onclick="window.close()"/></form>';\r
292                 }\r
293                                 \r
294                 \r
295                 $exist_temp_table = mysql_query('SELECT 1 FROM '.$NPG_CONF['temp_table'].' LIMIT 0');\r
296                 if ($exist_temp_table) sql_query('drop table '. $NPG_CONF['temp_table']);\r
297 \r
298                 break;\r
299         \r
300         case 'promopost':\r
301                 global $manager;\r
302                 $ids = explode(",", postvar('ids'));\r
303                 $result = ITEM::createFromRequest();\r
304                 if ($result['status'] == 'error')\r
305                         echo $result['message']; \r
306                         else {\r
307                                 $j=0;\r
308                                 while($ids[$j]) {\r
309                                         $query = 'insert into '.sql_table('plug_gallery_promo').' values ('.intval($ids[$j]).', '.intval($result['itemid']).')';\r
310                                         sql_query($query);\r
311                                         $j++;\r
312                                 }\r
313                                 echo __NPG_PROMO_FORM_SUCCESS.'<br/>';\r
314                                 echo '<form><input type="button" value="' . __NPG_PROMO_FORM_CLOSE . '" onclick="window.close()"/></form>';\r
315                                 \r
316                         }\r
317                 break;\r
318                 \r
319         case 'picasa' :\r
320       if(requestVar('id')) $albumid = requestVar('id'); else $albumid = 0;\r
321       $result = mysql_query('create temporary table '.$NPG_CONF['temp_table']\r
322                   .'(tempid int unsigned not null auto_increment PRIMARY KEY, '\r
323                   .'memberid int unsigned, '\r
324                   .'albumid int unsigned, '\r
325                   .'filename varchar(60), '\r
326                   .'intfilename varchar(60), '\r
327                   .'thumbfilename varchar(60), '\r
328                   .'title varchar(40), '\r
329                   .'description varchar(255), '\r
330                   .'promote tinyint unsigned default 0, '\r
331                   .'error varchar(60) default NULL)' );\r
332       //creates an xml parser and puts the xml into an array\r
333       $p = xml_parser_create();\r
334       if (!($fp = fopen($NP_BASE_DIR."upload/index.xml", "r"))) {die("unable to open XML");}\r
335       $contents = fread($fp, filesize($NP_BASE_DIR."upload/index.xml"));\r
336       xml_parse_into_struct($p,$contents,$vals,$index);\r
337       fclose($fp);\r
338       xml_parser_free($p);\r
339       //get album item count and loop through the pictures, putting filename and description into the temp database\r
340       $count = $index["ALBUMITEMCOUNT"][0];\r
341       $albumitemcount = $vals[$count]["value"];\r
342       for ($i=0; $i<$albumitemcount;$i++) {\r
343          $count = $index["ITEMNAME"][$i];\r
344          $filename = $vals[$count]["value"];\r
345          $count = $index["ITEMCAPTION"][$i];\r
346          $description = $vals[$count]["value"];\r
347          $filename = trim($filename);\r
348          $filetempname = $NP_BASE_DIR.'upload/images/' . $filename ;\r
349          $filetype = filetype($filetempname);\r
350          $filesize = filesize($filetempname);\r
351          //adds a picture to the temp table so user can add description, etc before actually adding to database\r
352          add_temp($albumid, $filename, $filetype, $filesize, $filetempname, $description);\r
353          \r
354       }\r
355         \r
356                 \r
357         default:\r
358                 break;\r
359 }\r
360 \r
361 \r
362 \r
363 \r
364 //adds a picture to the temp table so user can add description, etc before actually adding to database\r
365 \r
366 function add_temp($albumid = 0, $filename, $filetype, $filesize, $filetempname, $description = '') {\r
367         global $NPG_CONF, $gmember, $NP_BASE_DIR,$manager;\r
368         $memberid = $gmember->getID();\r
369         if (!preg_match('/^([a-z0-9_]+|`[^`]+`)$/i',$NPG_CONF['temp_table'])) exit;\r
370         $temp_table = $NPG_CONF['temp_table'];\r
371         $int_filename = '';\r
372         $thumb_filename = '';\r
373         $error = '';\r
374         $defaulttitle = $filename;\r
375         $NPG_CONF['randomprefix'] = 6;\r
376 \r
377         //add prefix to filename -- from http://www.phpdig.net/ref/rn22re349.html\r
378                 //or add current date to filename , option set in plugin admin\r
379         $dateorrandom = $NPG_CONF['dateorrandom'];\r
380 \r
381         if ($dateorrandom == 'randomprefix'){\r
382                 $str = "";\r
383                 for ($i = 0; $i < $NPG_CONF['randomprefix']; ++$i) {\r
384                 $str .= chr(rand() % 26 + 97);\r
385                 }\r
386                 $filename = $str . $filename;\r
387         }\r
388         if ($dateorrandom == 'dateprefix'){\r
389                 $str = "";\r
390                 $str = date('Y-m-d');\r
391                 $filename = $str . $filename;\r
392         }\r
393         //check filesize\r
394 \r
395         if ($filesize > $NPG_CONF['max_filesize']) \r
396                 $error = 'FILE_TOO_BIG';\r
397         else {\r
398                 //check filetype -- currently only jpeg supported       \r
399                 if (eregi("\.jpeg$",$filename)) $ok = 1;\r
400                 if (eregi("\.jpg$",$filename)) $ok = 1;\r
401                 if (!$ok) \r
402                         $error='BADFILETYPE';\r
403                         else {\r
404                                 //check if gallery directory exists, try to create if it doesn't, check write permssions\r
405                                 $mediadir = $NPG_CONF['galleryDir'];\r
406                                 if (!@is_dir($NP_BASE_DIR.$mediadir)) {\r
407                                         $error = 'Disallowed';\r
408                                         $oldumask = umask(0000);\r
409                                         if (!@mkdir($NP_BASE_DIR.$mediadir, 0777)) $error='Cannot create gallery directory'; \r
410                                         else {\r
411                                                 $error = NULL;\r
412                                                 umask($oldumask);                               \r
413                                         }\r
414                                 }\r
415                                 else {\r
416                                         if (!is_writeable($NP_BASE_DIR.$mediadir)) $error = 'Gallery directory not writable';\r
417                                         else {\r
418                                                 // add trailing slash (don't add it earlier since it causes mkdir to fail on some systems)\r
419                                                 $mediadir .= '/';\r
420                                                 //check if file already exists -- todo:if it does, add it to the database?\r
421                                                 if (file_exists($NP_BASE_DIR.$mediadir . $filename)) $error = 'UPLOADDUPLICATE';\r
422                                                 else {\r
423                                                         //move file : courtesy of Leslie Holmes www.CyberSparrow.com\r
424                                                         if (is_file($filetempname)) {\r
425                                                                 if (@rename($filetempname,$NP_BASE_DIR. $mediadir . $filename)){\r
426                                                                 // it worked, no problems\r
427                                                                 } elseif (copy($filetempname,$NP_BASE_DIR. $mediadir . $filename)){\r
428                                                                 // rename didn't work, so we tried copy and it worked, now delete original upload file\r
429                                                                         unlink($filetempname);\r
430                                                                 } else {\r
431                                                                 // neither method worked, so report error\r
432                                                                         $error = 'ERROR_UPLOADCOPY,ERROR_UPLOADMOVE' ;\r
433                                                                 }\r
434                                                         }               \r
435                                                         //chmod file\r
436                                                         $oldumask = umask(0000);\r
437                                                         @chmod($NP_BASE_DIR.$mediadir . $filename, 0644); \r
438                                                         umask($oldumask);\r
439 \r
440                                                         if(($NPG_CONF['graphics_library']=='gd' && GDisPresent()) || ($NPG_CONF['graphics_library']=='im' && IMisPresent()) ) {\r
441                                                                 //make intermediate file and thumbnail\r
442                                                                 $int_filename = resizeImage($mediadir.$filename, $NPG_CONF['maxwidth'], $NPG_CONF['maxheight'], $mediadir.$NPG_CONF['int_prefix'].$filename);\r
443                                                                 $thumb_filename = resizeImage($mediadir.$filename, $NPG_CONF['thumbwidth'], $NPG_CONF['thumbheight'], $mediadir.$NPG_CONF['thumb_prefix'].$filename);\r
444                                                         }\r
445                                                         else {\r
446                                                                 $error = 'Graphics library not configured properly';\r
447                                                         }\r
448                                                 }\r
449                                         }\r
450                                 }\r
451                         }\r
452                 }\r
453 \r
454                 \r
455 \r
456         \r
457 \r
458      $query = 'insert into '\r
459       .$temp_table\r
460       .'(tempid,memberid,albumid,filename,intfilename,thumbfilename,title,description,promote,error)'\r
461       ." values (NULL, ".intval($memberid).", ".intval($albumid).", '".addslashes($filename)."', '".addslashes($int_filename)."', '".addslashes($thumb_filename)."', '".addslashes($defaulttitle)."', '".addslashes($description)."', 0, '".addslashes($error)."') ";\r
462    //echo $query.'<br/>';\r
463    $result = sql_query($query);\r
464 \r
465 \r
466 \r
467 }\r
468 \r
469 \r
470 ?>\r