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

Revision 10054, 12.0 KB checked in by LucMorizur, 9 years ago (diff)

One day I'll do things as they should be done

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