source: extensions/event_cats/main.inc.php @ 7165

Last change on this file since 7165 was 7165, checked in by LucMorizur, 13 years ago

Begin implementation of validity date

File size: 12.7 KB
Line 
1<?php
2
3/*
4Plugin Name: Event Cats
5Version: 2.0.dev
6Description: A single URL can be enough to be identified, and a user can duplicate his account to create a new one getting immediately the same properties. / On peut être identifié grâce à un simple URL, et on peut dupliquer son compte pour créer un nouveau compte ayant immédiatement les mêmes propriétés.
7Plugin URI: http://piwigo.org/ext/extension_view.php?eid=326
8Author: LucMorizur
9Author URI: http://lucmorizur.free.fr
10*/
11
12// +-----------------------------------------------------------------------+
13// | Piwigo - a PHP based picture gallery                                  |
14// +-----------------------------------------------------------------------+
15// | Copyright(C) 2008-2009 Piwigo Team                  http://piwigo.org |
16// | Copyright(C) 2003-2008 PhpWebGallery Team    http://phpwebgallery.net |
17// | Copyright(C) 2002-2003 Pierrick LE GALL   http://le-gall.net/pierrick |
18// +-----------------------------------------------------------------------+
19// | This program is free software; you can redistribute it and/or modify  |
20// | it under the terms of the GNU General Public License as published by  |
21// | the Free Software Foundation                                          |
22// |                                                                       |
23// | This program is distributed in the hope that it will be useful, but   |
24// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
25// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
26// | General Public License for more details.                              |
27// |                                                                       |
28// | You should have received a copy of the GNU General Public License     |
29// | along with this program; if not, write to the Free Software           |
30// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
31// | USA.                                                                  |
32// +-----------------------------------------------------------------------+
33
34// Keeps file coded in UTF-8 without BOM: é
35
36if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!');
37
38// pour faciliter le debug - make debug easier :o)
39//ini_set('error_reporting', E_ALL);
40//ini_set('display_errors', true);
41
42global $conf, $prefixeTable, $ec_lists, $template;
43
44define( // -------------------------------------------------------------------
45  'EVNTCATS_INFO_VERSION', // VERSION HISTORY:
46   '2.0.dev' // Currently developping ;-)
47// '1.2.3' // Re-fix bug 1432 :-/ ...
48// '2.1.0' // When done, will simplfy and allow EC management from categories
49//            management pages in admin pages... yes yes...
50// '1.2.2' // Corrections for Piwigo 2.1 compatibility
51// '1.2.1' // Improve EN translation thanks to Tosca
52// '1.2.0' // Add feature 1335, Possibility to display the 'Connection'
53//         // link in identification block menu for generic users;
54//         // finish (yes !) english translation
55// '1.1.5' // Fix bugs 1324 and 1325
56// '1.1.4' // Improve help banner
57// '1.1.3' // Better help banner management; finalize banner texts
58// '1.1.2' // Better help banner example: some examples
59// '1.1.1' // Better help banner example; but still no text in it
60// '1.1.0' // First bugs (1305 and 1306) corrected;
61           // add newly created user/group association with cat/AP (was
62           // forgotten); begin help banner
63// '1.0.0' // Conception version
64); // ------------------------------------------------------------------------
65define(
66  'EVNTCATS_PATH',
67   PHPWG_PLUGINS_PATH.basename(dirname(__FILE__)).'/'
68);
69define(
70  'EVNTCATS_TABLE',
71   $prefixeTable.'event_cats'
72);
73define(
74  'ROOT_URL',
75   get_absolute_root_url()
76);
77
78include_once(EVNTCATS_PATH.'include/evntcats_main_funcs.inc.php');
79
80class event_cats {
81// Sets the administration panel of the plugin
82  function plugin_admin_menu($menu) {
83                                                                                // to be removed in final version :
84                                                                                global $template;
85                                                                                $template->delete_compiled_templates();
86    array_push($menu,
87      array(
88        'NAME' => 'Event Cats',
89        'URL'  => get_admin_plugin_menu_link(dirname(__FILE__).
90                  '/admin/evntcats_admin.php')
91      )
92    );
93    return $menu;
94  }
95} // End class
96
97$obj = new event_cats();
98
99// Adds the translation of "duplicate" link
100load_language('duplic.lang', EVNTCATS_PATH);
101
102// Admin help management
103add_event_handler('get_popup_help_content', 'ec_popup_help_content',
104 EVENT_HANDLER_PRIORITY_NEUTRAL, 2);
105
106function ec_popup_help_content($popup_help_content, $page) {
107  return (
108   $help_content = (is_admin() and $page == 'help') ?
109    load_language($page.'.html',EVNTCATS_PATH,array('return' => true)) : false
110  ) ? $popup_help_content.$help_content : $popup_help_content;
111}
112
113//----------------------------------
114
115/**
116 *
117 * auto_log_user()
118 * the function uses the value of the argument "autolog" of the posted URL,
119 * as a code to know which username has to be logged in.
120 *
121 * @param no parameter
122 * @return no return value
123 */
124
125add_event_handler('init', 'auto_log_user');
126
127function auto_log_user() {
128  global $ec_lists;
129
130  $ec_ap  = NULL;
131  $ec_cat = NULL;
132  $ec_img = NULL;
133 
134  if (isset($_GET['autolog']) and (read_ec_conf('activated') == '1')) {
135    if (!is_a_guest()) {
136      $url = '';
137      foreach ($_GET as $item => $value) {
138        $url.= '&'.$item.'='.$value;
139      }
140      logout_user();
141      redirect(make_index_url().$url);
142    }
143    build_ec_lists();
144    foreach ($ec_lists['ec_table'] as $ec_entry) {
145      if ($code_exists = ($ec_entry['code'] == $_GET['autolog'])) break;
146    }
147    if ($code_exists) {
148      if (is_in($ec_entry['action'], 'ec_ok')) {
149        log_user($ec_entry['user_id'], false);
150        if (isset($_GET['ap'])) $ec_ap = $_GET['ap'];
151        if (isset($_GET['cat'])) $ec_cat = $_GET['cat'];
152        if (isset($_GET['img'])) $ec_img = $_GET['img'];
153        if ($ec_entry['forced'] == 'true') {
154          if (empty($ec_entry['arg1']) and !empty($ec_entry['arg2'])) {
155            $ec_ap = $ec_entry['arg2'];
156          }
157          elseif (!empty($ec_entry['arg1'])) {
158            $ec_cat = $ec_entry['arg1'];
159            if (!empty($ec_entry['arg2'])) $ec_img = $ec_entry['arg2'];
160          }
161        }
162        if (isset($ec_ap)) {
163          if (array_key_exists($ec_ap,$ec_lists['add_pages'])) {
164            redirect(
165             PHPWG_ROOT_PATH.'index.php?/additional_page/'.$ec_ap);
166          }
167        }
168        elseif (isset($ec_cat)) {
169          if (array_key_exists($ec_cat, $ec_lists['categories'])) {
170            if (isset($ec_img)) {
171              if (ec_image_exists($ec_cat, $ec_img)) {
172       redirect(PHPWG_ROOT_PATH.'picture.php?/'.$ec_img.'/category/'.$ec_cat);
173              }
174            }
175            redirect(PHPWG_ROOT_PATH.'index.php?/category/'.$ec_cat);
176          }
177        }
178        redirect(make_index_url());
179      }
180      else {
181        if (
182         $ec_entry['action'] == 'ec_nok' or
183         $ec_entry['action'] == 'ec_nok_ap_pb'
184        ) {
185          if ($ec_entry['action'] == 'ec_nok_ap_pb') access_denied();
186          $ec_ap = $ec_entry['arg2'];
187          if (array_key_exists($ec_ap, $ec_lists['add_pages'])) {
188            redirect(
189             PHPWG_ROOT_PATH.'index.php?/additional_page/'.$ec_ap);
190          }
191          access_denied();
192        }
193        else {
194          redirect(make_index_url());
195        }
196      }
197    }
198    else {
199      if (
200       read_ec_conf('unknown_code') == '2' and
201        array_key_exists(
202         read_ec_conf('unknown_code_ap_id'), $ec_lists['add_pages']
203        )
204       ) {
205        redirect(
206         PHPWG_ROOT_PATH.
207         'index.php?/additional_page/'.read_ec_conf('unknown_code_ap_id')
208        );
209      }
210      elseif (read_ec_conf('unknown_code') == '1' or
211       read_ec_conf('unknown_code') == '2') {
212        access_denied();
213      }
214      else {
215        redirect(make_index_url());
216      }
217    }
218  }
219}
220
221/**
222 *
223 * assign_perm_for_new_user()
224 * copies/paste groups+access+properties associations of previously connected
225 * username, to newly created username.
226 *
227 * @param no parameter
228 * @return no return value
229 */
230
231add_event_handler('register_user', 'assign_perm_for_new_user');
232
233function assign_perm_for_new_user($new_user) {
234  global $user;
235
236  if (read_ec_conf('activated') == '1') {
237    if (
238      !is_a_guest() and !is_admin() and (
239        read_ec_conf('dup_allow') == '1' or (
240          read_ec_conf('dup_allow') == '2' and (
241            dup_allowed($user['id']) or (
242              read_ec_conf('simple_mode_on') == '1' and
243              is_generic()
244            )
245          )
246        )
247      )
248    ) {
249      // User access
250      $result = pwg_query("
251        SELECT `cat_id`
252        FROM `".USER_ACCESS_TABLE."`
253        WHERE `user_id` = ".$user['id'].";
254      ");
255      $insert = array();
256      while ($row = pwg_db_fetch_assoc($result))
257       $insert[] = "(".$new_user['id'].",".$row['cat_id'].")";
258      if (!empty($insert)) pwg_query("
259        INSERT INTO `".USER_ACCESS_TABLE."`
260        VALUES ".implode(',', $insert).";
261      ");
262
263      // User groups
264      $result = pwg_query("
265        SELECT `group_id`
266        FROM `".USER_GROUP_TABLE."`
267        WHERE `user_id` = ".$user['id'].";
268      ");
269      $insert = array();
270      while ($row = pwg_db_fetch_assoc($result))
271       $insert[] = "(".$new_user['id'].",".$row['group_id'].")";
272      if (!empty($insert)) pwg_query("
273        INSERT INTO `".USER_GROUP_TABLE."`
274        VALUES ".implode(',', $insert).";
275      ");
276
277      // User infos
278      $result = pwg_query("
279        SELECT `level`
280        FROM `".USER_INFOS_TABLE."`
281        WHERE `user_id` = ".$user['id'].";
282      ");
283      $insert = array();
284      while ($row = pwg_db_fetch_assoc($result))
285       $insert[] = "(".$new_user['id'].",".$row['level'].")";
286      if (!empty($insert)) pwg_query("
287        UPDATE `".USER_INFOS_TABLE."`
288        SET `level` = ".$user['level']."
289        WHERE `user_id` = ".$new_user['id'].";
290      ");
291    }
292  }
293}
294
295/**
296 *
297 * duplicate_account_url()
298 * adds a link for duplicating the currently identified user in
299 * Identification block menu, in case the identifed user is granted to
300 * duplication. Displays also the connection link for generic users, if
301 * required in the configuration.
302 *
303 * ec_duplicate_prefilter
304 * is used to modify the "quick connect" block in identification menu block,
305 * so that visitors identifying themselves this way, are redirected to the
306 * page they're currently displaying, instead of home page. This
307 * functionnality has been added at version 2.1.0 in Piwigo core
308 * (see bug:1484 Redirection after quickconnect), thus it is useless to
309 * implement it from here for Piwigo versions higher or equal to 2.1.0.
310 *
311 * @param no parameter
312 * @return no return value
313 */
314
315add_event_handler('blockmanager_apply', 'duplicate_account_url');
316
317function ec_duplicate_prefilter($content, &$smarty) {
318  $search = "<legend>{'Quick connect'|@translate}</legend>";
319  $addon = '<input type="hidden" name="redirect" value="{$U_REDIRECT}">';
320  $replacement = "<legend>{'Quick connect'|@translate}</legend>".$addon;
321  return str_replace($search, $replacement, $content);
322}
323
324function duplicate_account_url() {
325  global $lang, $template, $user;
326 
327  if (read_ec_conf('activated') == '1') {
328    if (version_compare(PHPWG_VERSION, '2.1.0', '<')) {
329      // Makes the "quick connect" fieldset able to redirect to current page
330      // after user identification, just as does the "connection" link.
331      $template->assign(array('U_REDIRECT' => $_SERVER['REQUEST_URI']));
332      $template->set_prefilter('menubar', 'ec_duplicate_prefilter');
333    }
334   
335    // Add duplication link, if needed
336    if (
337      !is_admin() and !is_a_guest() and (
338        read_ec_conf('dup_allow') == '1' or (
339          read_ec_conf('dup_allow') == '2' and (
340            dup_allowed($user['id']) or (
341              read_ec_conf('simple_mode_on') == '1' and
342              is_generic()
343            )
344          )
345        )
346      )
347    ) {
348      $template->assign('U_REGISTER', get_root_url().'register.php');
349      if (
350        read_ec_conf('duplic_display') == '1' or (
351          read_ec_conf('duplic_display') == '2' and
352          !is_generic()
353        )
354      ) {
355        $lang['Register'] = $lang['Duplicate'];
356        $lang['Create a new account'] =
357         $lang['Create a new account with same properties'];
358      }
359    }
360   
361    // Adds connection link, if needed
362    if (read_ec_conf('display_connection') == '1' and is_generic()) {
363      // Adds connection link
364      $template->assign(
365        'U_LOGIN',
366        get_root_url().'identification.php?redirect='.$_SERVER['REQUEST_URI']
367      );
368    }
369  }
370}
371
372add_event_handler(
373 'get_admin_plugin_menu_links',
374 array(&$obj, 'plugin_admin_menu')
375);
376set_plugin_data($plugin['id'], $obj);
377
378?>
Note: See TracBrowser for help on using the repository browser.