source: extensions/community/add_photos.php @ 10756

Last change on this file since 10756 was 10756, checked in by plg, 14 years ago

feature 2294 added: ability to define photo properties before upload (name/author/description).

Because this is more a workaround than a clean way to implement it, webmaster
has to set $confcommunity_ask_for_properties = true in is local configuration file.

File size: 10.5 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based photo gallery                                    |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2011 Piwigo Team                  http://piwigo.org |
6// | Copyright(C) 2003-2008 PhpWebGallery Team    http://phpwebgallery.net |
7// | Copyright(C) 2002-2003 Pierrick LE GALL   http://le-gall.net/pierrick |
8// +-----------------------------------------------------------------------+
9// | This program is free software; you can redistribute it and/or modify  |
10// | it under the terms of the GNU General Public License as published by  |
11// | the Free Software Foundation                                          |
12// |                                                                       |
13// | This program is distributed in the hope that it will be useful, but   |
14// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
15// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
16// | General Public License for more details.                              |
17// |                                                                       |
18// | You should have received a copy of the GNU General Public License     |
19// | along with this program; if not, write to the Free Software           |
20// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
21// | USA.                                                                  |
22// +-----------------------------------------------------------------------+
23
24if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!');
25
26global $template, $conf, $user;
27
28include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
29include_once(PHPWG_ROOT_PATH.'admin/include/functions_upload.inc.php');
30include_once(COMMUNITY_PATH.'include/functions_community.inc.php');
31
32define('PHOTOS_ADD_BASE_URL', make_index_url(array('section' => 'add_photos')));
33
34prepare_upload_configuration();
35
36$user_permissions = community_get_user_permissions($user['id']);
37
38if (count($user_permissions['upload_categories']) == 0 and !$user_permissions ['create_whole_gallery'])
39{
40  redirect(make_index_url());
41}
42
43// +-----------------------------------------------------------------------+
44// |                             process form                              |
45// +-----------------------------------------------------------------------+
46
47$page['errors'] = array();
48$page['infos'] = array();
49$_POST['level'] = 16;
50
51if (isset($_GET['processed']))
52{
53  $hacking_attempt = false;
54 
55  if ('existing' == $_POST['category_type'])
56  {
57    // is the user authorized to upload in this album?
58    if (!in_array($_POST['category'], $user_permissions['upload_categories']))
59    {
60      echo 'Hacking attempt, you have no permission to upload in this album';
61      $hacking_attempt = true;
62    }
63  }
64  elseif ('new' == $_POST['category_type'])
65  {
66    if (!in_array($_POST['category_parent'], $user_permissions['create_categories']))
67    {
68      echo 'Hacking attempt, you have no permission to create this album';
69      $hacking_attempt = true;
70    }
71  }
72
73  if ($hacking_attempt)
74  {
75    if (isset($_SESSION['uploads'][ $_POST['upload_id'] ]))
76    {
77      delete_elements($_SESSION['uploads'][ $_POST['upload_id'] ], true);
78    }
79    exit();
80  }
81}
82
83include_once(PHPWG_ROOT_PATH.'admin/include/photos_add_direct_process.inc.php');
84
85if (isset($image_ids) and count($image_ids) > 0)
86{
87  // reinitialize the informations to display on the result page
88  $page['infos'] = array();
89
90  if (isset($conf['community_ask_for_properties']) and $conf['community_ask_for_properties'])
91  {
92    $data = array();
93   
94    $data['name'] = $_POST['name'];
95    $data['author'] = $_POST['author'];
96   
97    if ($conf['allow_html_descriptions'])
98    {
99      $data['comment'] = @$_POST['description'];
100    }
101    else
102    {
103      $data['comment'] = strip_tags(@$_POST['description']);
104    }
105
106    $updates = array();
107    foreach ($image_ids as $image_id)
108    {
109      $update = $data;
110      $update['id'] = $image_id;
111
112      array_push($updates, $update);
113    }
114
115    mass_updates(
116      IMAGES_TABLE,
117      array(
118        'primary' => array('id'),
119        'update' => array_keys($updates[0])
120        ),
121      $updates
122      );
123  }
124 
125  // $category_id is set in the photos_add_direct_process.inc.php included script
126  $category_infos = get_cat_info($category_id);
127  $category_name = get_cat_display_name($category_infos['upper_names']);
128
129  array_push(
130    $page['infos'],
131    sprintf(
132      l10n('%d photos uploaded into album "%s"'),
133      count($page['thumbnails']),
134      '<em>'.$category_name.'</em>'
135      )
136    );
137
138  // should the photos be moderated?
139  //
140  // if one of the user community permissions is not moderated on the path
141  // to gallery root, then the upload is not moderated. For example, if the
142  // user is allowed to upload to events/parties with no admin moderation,
143  // then he's not moderated when uploading in
144  // events/parties/happyNewYear2011
145  $moderate = true;
146  if (is_admin())
147  {
148    $moderate = false;
149  }
150  else
151  { 
152    $query = '
153SELECT
154    cp.category_id,
155    c.uppercats
156  FROM '.COMMUNITY_PERMISSIONS_TABLE.' AS cp
157    LEFT JOIN '.CATEGORIES_TABLE.' AS c ON category_id = c.id
158  WHERE cp.id IN ('.implode(',', $user_permissions['permission_ids']).')
159    AND cp.moderated = \'false\'
160;';
161    $result = pwg_query($query);
162    while ($row = pwg_db_fetch_assoc($result))
163    {
164      if (empty($row['category_id']))
165      {
166        $moderate = false;
167      }
168      elseif (preg_match('/^'.$row['uppercats'].'(,|$)/', $category_infos['uppercats']))
169      {
170        $moderate = false;
171      }
172    }
173  }
174 
175  if ($moderate)
176  {
177    $inserts = array();
178
179    $query = '
180SELECT
181    id,
182    date_available
183  FROM '.IMAGES_TABLE.'
184  WHERE id IN ('.implode(',', $image_ids).')
185;';
186    $result = pwg_query($query);
187    while ($row = pwg_db_fetch_assoc($result))
188    {
189      array_push(
190        $inserts,
191        array(
192          'image_id' => $row['id'],
193          'added_on' => $row['date_available'],
194          'state' => 'moderation_pending',
195          )
196        );
197    }
198   
199    mass_inserts(
200      COMMUNITY_PENDINGS_TABLE,
201      array_keys($inserts[0]),
202      $inserts
203      );
204
205    // the link on thumbnail must go to the websize photo
206    foreach ($page['thumbnails'] as $idx => $thumbnail)
207    {
208      $page['thumbnails'][$idx]['link'] = str_replace(
209        'thumbnail/'.$conf['prefix_thumbnail'],
210        '',
211        $thumbnail['src']
212        );
213    }
214
215    array_push(
216      $page['infos'],
217      l10n('Your photos are waiting for validation, administrators have been notified')
218      );
219  }
220  else
221  {
222    // the level of a user upload photo with no moderation is 0
223    $query = '
224UPDATE '.IMAGES_TABLE.'
225  SET level = 0
226  WHERE id IN ('.implode(',', $image_ids).')
227;';
228    pwg_query($query);
229
230    // the link on thumbnail must go to picture.php
231    foreach ($page['thumbnails'] as $idx => $thumbnail)
232    {
233      if (preg_match('/image_id=(\d+)/', $thumbnail['link'], $matches))
234      {
235        $page['thumbnails'][$idx]['link'] = make_picture_url(
236          array(
237            'image_id' => $matches[1],
238            'image_file' => $thumbnail['file'],
239            'category' => $category_infos,
240            )
241          );
242      }
243    }
244  }
245
246  invalidate_user_cache();
247
248  // let's notify administrators
249  include_once(PHPWG_ROOT_PATH.'include/functions_mail.inc.php');
250
251  $keyargs_content = array(
252    get_l10n_args('Hi administrators,', ''),
253    get_l10n_args('', ''),
254    get_l10n_args('Album: %s', get_cat_display_name($category_infos['upper_names'], null, false)),
255    get_l10n_args('User: %s', $user['username']),
256    get_l10n_args('Email: %s', $user['email']),
257    );
258
259  if ($moderate)
260  {
261    $keyargs_content[] = get_l10n_args('', '');
262   
263    array_push(
264      $keyargs_content,
265      get_l10n_args(
266        'Validation page: %s',
267        get_absolute_root_url().'admin.php?page=plugin-community-pendings'
268        )
269      );
270  }
271
272  pwg_mail_notification_admins(
273    get_l10n_args('%d photos uploaded by %s', array(count($image_ids), $user['username'])),
274    $keyargs_content,
275    false
276    );
277}
278
279// +-----------------------------------------------------------------------+
280// |                             prepare form                              |
281// +-----------------------------------------------------------------------+
282
283$template->set_filenames(array('add_photos' => dirname(__FILE__).'/add_photos.tpl'));
284
285include_once(PHPWG_ROOT_PATH.'admin/include/photos_add_direct_prepare.inc.php');
286
287// we have to change the list of uploadable albums
288$upload_categories = $user_permissions['upload_categories'];
289if (count($upload_categories) == 0)
290{
291  $upload_categories = array(-1);
292}
293
294$query = '
295SELECT id,name,uppercats,global_rank
296  FROM '.CATEGORIES_TABLE.'
297  WHERE id IN ('.implode(',', $upload_categories).')
298;';
299
300display_select_cat_wrapper(
301  $query,
302  $selected_category,
303  'category_options'
304  );
305
306$create_subcategories = false;
307if ($user_permissions['create_whole_gallery'] or count($user_permissions['create_categories']) > 0)
308{
309  $create_subcategories = true;
310}
311
312$create_categories = $user_permissions['create_categories'];
313if (count($user_permissions['create_categories']) == 0)
314{
315  $create_categories = array(-1);
316}
317
318$query = '
319SELECT id,name,uppercats,global_rank
320  FROM '.CATEGORIES_TABLE.'
321  WHERE id IN ('.implode(',', $create_categories).')
322;';
323
324display_select_cat_wrapper(
325  $query,
326  $selected_category,
327  'category_parent_options'
328  );
329
330$template->assign(
331  array(
332    'create_subcategories' => $create_subcategories,
333    'create_whole_gallery' => $user_permissions['create_whole_gallery'],
334    )
335  );
336
337if (isset($conf['community_ask_for_properties']) and $conf['community_ask_for_properties'])
338{
339  $template->assign(
340    array(
341      'community_ask_for_properties' => true,
342      )
343    );
344}
345
346// +-----------------------------------------------------------------------+
347// |                             display page                              |
348// +-----------------------------------------------------------------------+
349
350if (count($page['errors']) != 0)
351{
352  $template->assign('errors', $page['errors']);
353}
354
355if (count($page['infos']) != 0)
356{
357  $template->assign('infos', $page['infos']);
358}
359
360$title = l10n('Upload Photos');
361$page['body_id'] = 'theUploadPage';
362
363$template->assign_var_from_handle('PLUGIN_INDEX_CONTENT_BEGIN', 'add_photos');
364
365$template->clear_assign(array('U_MODE_POSTED', 'U_MODE_CREATED'));
366
367$template->assign(
368  array(
369    'TITLE' => '<a href="'.get_gallery_home_url().'">'.l10n('Home').'</a>'.$conf['level_separator'].$title,
370    )
371  );
372?>
Note: See TracBrowser for help on using the repository browser.