root/extensions/event_cats/main.inc.php @ 7084

Revision 7084, 12.4 KB (checked in by LucMorizur, 3 years ago)

At last a configuration management working as desired

Line 
1<?php
2
3/*
4Plugin Name: Event Cats
5Version: 1.2.3
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   '1.2.3' // Re-fix bug 1432 :-/ ...
47// '2.1.0' // When done, will simplfy and allow EC management from categories
48//            management pages in admin pages... yes yes...
49// '1.2.2' // Corrections for Piwigo 2.1 compatibility
50// '1.2.1' // Improve EN translation thanks to Tosca
51// '1.2.0' // Add feature 1335, Possibility to display the 'Connection'
52//         // link in identification block menu for generic users;
53//         // finish (yes !) english translation
54// '1.1.5' // Fix bugs 1324 and 1325
55// '1.1.4' // Improve help banner
56// '1.1.3' // Better help banner management; finalize banner texts
57// '1.1.2' // Better help banner example: some examples
58// '1.1.1' // Better help banner example; but still no text in it
59// '1.1.0' // First bugs (1305 and 1306) corrected;
60           // add newly created user/group association with cat/AP (was
61           // forgotten); begin help banner
62// '1.0.0' // Conception version
63); // ------------------------------------------------------------------------
64define(
65  'EVNTCATS_PATH',
66   PHPWG_PLUGINS_PATH.basename(dirname(__FILE__)).'/'
67);
68define(
69  'EVNTCATS_TABLE',
70   $prefixeTable.'event_cats'
71);
72define(
73  'ROOT_URL',
74   get_absolute_root_url()
75);
76
77include_once(EVNTCATS_PATH.'include/evntcats_main_funcs.inc.php');
78
79class event_cats {
80// Sets the administration panel of the plugin
81  function plugin_admin_menu($menu) {
82    array_push($menu,
83      array(
84        'NAME' => 'Event Cats',
85        'URL'  => get_admin_plugin_menu_link(dirname(__FILE__).
86                  '/admin/evntcats_admin.php')
87      )
88    );
89    return $menu;
90  }
91} // End class
92
93$obj = new event_cats();
94
95// Adds the translation of "duplicate" link
96load_language('duplic.lang', EVNTCATS_PATH);
97
98// Admin help management
99add_event_handler('get_popup_help_content', 'ec_popup_help_content',
100 EVENT_HANDLER_PRIORITY_NEUTRAL, 2);
101
102function ec_popup_help_content($popup_help_content, $page) {
103  return (
104   $help_content = (is_admin() and $page == 'help') ?
105    load_language($page.'.html',EVNTCATS_PATH,array('return' => true)) : false
106  ) ? $popup_help_content.$help_content : $popup_help_content;
107}
108
109//----------------------------------
110
111/**
112 *
113 * auto_log_user()
114 * the function uses the value of the argument "autolog" of the posted URL,
115 * as a code to know which username has to be logged in.
116 *
117 * @param no parameter
118 * @return no return value
119 */
120
121add_event_handler('init', 'auto_log_user');
122
123function auto_log_user() {
124  global $ec_lists;
125
126  $ec_ap  = NULL;
127  $ec_cat = NULL;
128  $ec_img = NULL;
129 
130  if (isset($_GET['autolog']) and (read_ec_conf('activated') == '1')) {
131    if (!is_a_guest()) {
132      $url = '';
133      foreach ($_GET as $item => $value) {
134        $url.= '&'.$item.'='.$value;
135      }
136      logout_user();
137      redirect(make_index_url().$url);
138    }
139    build_ec_lists();
140    foreach ($ec_lists['ec_table'] as $ec_entry) {
141      if ($code_exists = ($ec_entry['code'] == $_GET['autolog'])) break;
142    }
143    if ($code_exists) {
144      if (is_in($ec_entry['action'], 'ec_ok')) {
145        log_user($ec_entry['user_id'], false);
146        if (isset($_GET['ap'])) $ec_ap = $_GET['ap'];
147        if (isset($_GET['cat'])) $ec_cat = $_GET['cat'];
148        if (isset($_GET['img'])) $ec_img = $_GET['img'];
149        if ($ec_entry['forced'] == 'true') {
150          if (empty($ec_entry['arg1']) and !empty($ec_entry['arg2'])) {
151            $ec_ap = $ec_entry['arg2'];
152          }
153          elseif (!empty($ec_entry['arg1'])) {
154            $ec_cat = $ec_entry['arg1'];
155            if (!empty($ec_entry['arg2'])) $ec_img = $ec_entry['arg2'];
156          }
157        }
158        if (isset($ec_ap)) {
159          if (array_key_exists($ec_ap,$ec_lists['add_pages'])) {
160            redirect(
161             PHPWG_ROOT_PATH.'index.php?/additional_page/'.$ec_ap);
162          }
163        }
164        elseif (isset($ec_cat)) {
165          if (array_key_exists($ec_cat, $ec_lists['categories'])) {
166            if (isset($ec_img)) {
167              if (ec_image_exists($ec_cat, $ec_img)) {
168       redirect(PHPWG_ROOT_PATH.'picture.php?/'.$ec_img.'/category/'.$ec_cat);
169              }
170            }
171            redirect(PHPWG_ROOT_PATH.'index.php?/category/'.$ec_cat);
172          }
173        }
174        redirect(make_index_url());
175      }
176      else {
177        if (
178         $ec_entry['action'] == 'ec_nok' or
179         $ec_entry['action'] == 'ec_nok_ap_pb'
180        ) {
181          if ($ec_entry['action'] == 'ec_nok_ap_pb') access_denied();
182          $ec_ap = $ec_entry['arg2'];
183          if (array_key_exists($ec_ap, $ec_lists['add_pages'])) {
184            redirect(
185             PHPWG_ROOT_PATH.'index.php?/additional_page/'.$ec_ap);
186          }
187          access_denied();
188        }
189        else {
190          redirect(make_index_url());
191        }
192      }
193    }
194    else {
195      if (
196       read_ec_conf('unknown_code') == '2' and
197        array_key_exists(
198         read_ec_conf('unknown_code_ap_id'), $ec_lists['add_pages']
199        )
200       ) {
201        redirect(
202         PHPWG_ROOT_PATH.
203         'index.php?/additional_page/'.read_ec_conf('unknown_code_ap_id')
204        );
205      }
206      elseif (read_ec_conf('unknown_code') == '1' or
207       read_ec_conf('unknown_code') == '2') {
208        access_denied();
209      }
210      else {
211        redirect(make_index_url());
212      }
213    }
214  }
215}
216
217/**
218 *
219 * assign_perm_for_new_user()
220 * copies/paste groups+access+properties associations of previously connected
221 * username, to newly created username.
222 *
223 * @param no parameter
224 * @return no return value
225 */
226
227add_event_handler('register_user', 'assign_perm_for_new_user');
228
229function assign_perm_for_new_user($new_user) {
230  global $user;
231
232  if (read_ec_conf('activated') == '1') {
233    if (
234      !is_a_guest() and !is_admin() and (
235        read_ec_conf('dup_allow') == '1' or (
236          read_ec_conf('dup_allow') == '2' and (
237            dup_allowed($user['id']) or (
238              read_ec_conf('simple_mode_on') == '1' and
239              is_generic()
240            )
241          )
242        )
243      )
244    ) {
245      // User access
246      $result = pwg_query("
247        SELECT `cat_id`
248        FROM `".USER_ACCESS_TABLE."`
249        WHERE `user_id` = ".$user['id'].";
250      ");
251      $insert = array();
252      while ($row = pwg_db_fetch_assoc($result))
253       $insert[] = "(".$new_user['id'].",".$row['cat_id'].")";
254      if (!empty($insert)) pwg_query("
255        INSERT INTO `".USER_ACCESS_TABLE."`
256        VALUES ".implode(',', $insert).";
257      ");
258
259      // User groups
260      $result = pwg_query("
261        SELECT `group_id`
262        FROM `".USER_GROUP_TABLE."`
263        WHERE `user_id` = ".$user['id'].";
264      ");
265      $insert = array();
266      while ($row = pwg_db_fetch_assoc($result))
267       $insert[] = "(".$new_user['id'].",".$row['group_id'].")";
268      if (!empty($insert)) pwg_query("
269        INSERT INTO `".USER_GROUP_TABLE."`
270        VALUES ".implode(',', $insert).";
271      ");
272
273      // User infos
274      $result = pwg_query("
275        SELECT `level`
276        FROM `".USER_INFOS_TABLE."`
277        WHERE `user_id` = ".$user['id'].";
278      ");
279      $insert = array();
280      while ($row = pwg_db_fetch_assoc($result))
281       $insert[] = "(".$new_user['id'].",".$row['level'].")";
282      if (!empty($insert)) pwg_query("
283        UPDATE `".USER_INFOS_TABLE."`
284        SET `level` = ".$user['level']."
285        WHERE `user_id` = ".$new_user['id'].";
286      ");
287    }
288  }
289}
290
291/**
292 *
293 * duplicate_account_url()
294 * adds a link for duplicating the currently identified user in
295 * Identification block menu, in case the identifed user is granted to
296 * duplication. Displays also the connection link for generic users, if
297 * required in the configuration.
298 *
299 * ec_duplicate_prefilter
300 * is used to modify the "quick connect" block in identification menu block,
301 * so that visitors identifying themselves this way, are redirected to the
302 * page they're currently displaying, instead of home page. This
303 * functionnality has been added at version 2.1.0 in Piwigo core
304 * (see bug:1484 Redirection after quickconnect), thus it is useless to
305 * implement it from here for Piwigo versions higher or equal to 2.1.0.
306 *
307 * @param no parameter
308 * @return no return value
309 */
310
311add_event_handler('blockmanager_apply', 'duplicate_account_url');
312
313function ec_duplicate_prefilter($content, &$smarty) {
314  $search = "<legend>{'Quick connect'|@translate}</legend>";
315  $addon = '<input type="hidden" name="redirect" value="{$U_REDIRECT}">';
316  $replacement = "<legend>{'Quick connect'|@translate}</legend>".$addon;
317  return str_replace($search, $replacement, $content);
318}
319
320function duplicate_account_url() {
321  global $lang, $template, $user;
322 
323  if (read_ec_conf('activated') == '1') {
324    if (version_compare(PHPWG_VERSION, '2.1.0', '<')) {
325      // Makes the "quick connect" fieldset able to redirect to current page
326      // after user identification, just as does the "connection" link.
327      $template->assign(array('U_REDIRECT' => $_SERVER['REQUEST_URI']));
328      $template->set_prefilter('menubar', 'ec_duplicate_prefilter');
329    }
330   
331    // Add duplication link, if needed
332    if (
333      !is_admin() and !is_a_guest() and (
334        read_ec_conf('dup_allow') == '1' or (
335          read_ec_conf('dup_allow') == '2' and (
336            dup_allowed($user['id']) or (
337              read_ec_conf('simple_mode_on') == '1' and
338              is_generic()
339            )
340          )
341        )
342      )
343    ) {
344      $template->assign('U_REGISTER', get_root_url().'register.php');
345      if (
346        read_ec_conf('duplic_display') == '1' or (
347          read_ec_conf('duplic_display') == '2' and
348          !is_generic()
349        )
350      ) {
351        $lang['Register'] = $lang['Duplicate'];
352        $lang['Create a new account'] =
353         $lang['Create a new account with same properties'];
354      }
355    }
356   
357    // Adds connection link, if needed
358    if (read_ec_conf('display_connection') == '1' and is_generic()) {
359      // Adds connection link
360      $template->assign(
361        'U_LOGIN',
362        get_root_url().'identification.php?redirect='.$_SERVER['REQUEST_URI']
363      );
364    }
365  }
366}
367
368add_event_handler(
369 'get_admin_plugin_menu_links',
370 array(&$obj, 'plugin_admin_menu')
371);
372set_plugin_data($plugin['id'], $obj);
373
374?>
Note: See TracBrowser for help on using the browser.