[3673] | 1 | <?php |
---|
| 2 | /* |
---|
| 3 | Plugin Name: Community |
---|
| 4 | Version: auto |
---|
| 5 | Description: Non admin users can add photos |
---|
| 6 | Plugin URI: http://piwigo.org/ext/extension_view.php?eid=303 |
---|
| 7 | Author: plg |
---|
| 8 | Author URI: http://piwigo.wordpress.com |
---|
| 9 | */ |
---|
| 10 | |
---|
| 11 | if (!defined('PHPWG_ROOT_PATH')) |
---|
| 12 | { |
---|
| 13 | die('Hacking attempt!'); |
---|
| 14 | } |
---|
| 15 | |
---|
| 16 | define('COMMUNITY_PATH' , PHPWG_PLUGINS_PATH.basename(dirname(__FILE__)).'/'); |
---|
| 17 | |
---|
[9372] | 18 | global $prefixeTable; |
---|
| 19 | define('COMMUNITY_TABLE', $prefixeTable.'community'); |
---|
| 20 | define('COMMUNITY_PERMISSIONS_TABLE', $prefixeTable.'community_permissions'); |
---|
| 21 | define('COMMUNITY_PENDINGS_TABLE', $prefixeTable.'community_pendings'); |
---|
| 22 | |
---|
[3673] | 23 | /* Plugin admin */ |
---|
| 24 | add_event_handler('get_admin_plugin_menu_links', 'community_admin_menu'); |
---|
| 25 | |
---|
| 26 | function community_admin_menu($menu) |
---|
| 27 | { |
---|
| 28 | array_push( |
---|
| 29 | $menu, |
---|
| 30 | array( |
---|
| 31 | 'NAME' => 'Community', |
---|
[9372] | 32 | 'URL' => get_root_url().'admin.php?page=plugin-community' |
---|
[3673] | 33 | ) |
---|
| 34 | ); |
---|
| 35 | |
---|
| 36 | return $menu; |
---|
| 37 | } |
---|
| 38 | |
---|
[9372] | 39 | add_event_handler('loc_end_section_init', 'community_section_init'); |
---|
| 40 | function community_section_init() |
---|
| 41 | { |
---|
| 42 | global $tokens, $page; |
---|
| 43 | |
---|
| 44 | if ($tokens[0] == 'add_photos') |
---|
| 45 | { |
---|
| 46 | $page['section'] = 'add_photos'; |
---|
| 47 | } |
---|
| 48 | } |
---|
| 49 | |
---|
| 50 | add_event_handler('loc_end_index', 'community_index'); |
---|
| 51 | function community_index() |
---|
| 52 | { |
---|
| 53 | global $page; |
---|
| 54 | |
---|
| 55 | if (isset($page['section']) and $page['section'] == 'add_photos') |
---|
| 56 | { |
---|
| 57 | include(COMMUNITY_PATH.'add_photos.php'); |
---|
| 58 | } |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | add_event_handler('blockmanager_apply' , 'community_gallery_menu'); |
---|
| 62 | function community_gallery_menu($menu_ref_arr) |
---|
| 63 | { |
---|
| 64 | global $conf, $user; |
---|
| 65 | |
---|
| 66 | // conditional : depending on community permissions, display the "Add |
---|
| 67 | // photos" link in the gallery menu |
---|
| 68 | |
---|
| 69 | // admins are not concerned about community permissions |
---|
| 70 | if (!is_admin()) |
---|
| 71 | { |
---|
| 72 | // what are the user groups? |
---|
| 73 | $query = ' |
---|
| 74 | SELECT |
---|
| 75 | group_id |
---|
| 76 | FROM '.USER_GROUP_TABLE.' |
---|
| 77 | WHERE user_id = '.$user['id'].' |
---|
| 78 | ;'; |
---|
| 79 | $user_group_ids = array_from_query($query, 'group_id'); |
---|
| 80 | |
---|
| 81 | $query = ' |
---|
| 82 | SELECT |
---|
| 83 | COUNT(*) |
---|
| 84 | FROM '.COMMUNITY_PERMISSIONS_TABLE.' |
---|
| 85 | WHERE (type = \'any_visitor\')'; |
---|
| 86 | |
---|
| 87 | if ($user['id'] != $conf['guest_id']) |
---|
| 88 | { |
---|
| 89 | $query.= ' |
---|
| 90 | OR (type = \'any_registered_user\') |
---|
| 91 | OR (type = \'user\' AND user_id = '.$user['id'].') |
---|
| 92 | OR (type = \'group\' AND group_id IN ('.implode(',', $user_group_ids).')) |
---|
| 93 | '; |
---|
| 94 | } |
---|
| 95 | |
---|
| 96 | $query.= ' |
---|
| 97 | ;'; |
---|
| 98 | |
---|
| 99 | list($counter) = pwg_db_fetch_row(pwg_query($query)); |
---|
| 100 | if (0 == $counter) |
---|
| 101 | { |
---|
| 102 | return; |
---|
| 103 | } |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | $menu = & $menu_ref_arr[0]; |
---|
| 107 | |
---|
| 108 | if (($block = $menu->get_block('mbMenu')) != null ) |
---|
| 109 | { |
---|
| 110 | load_language('plugin.lang', COMMUNITY_PATH); |
---|
| 111 | |
---|
| 112 | array_splice( |
---|
| 113 | $block->data, |
---|
| 114 | count($block->data), |
---|
| 115 | 0, |
---|
| 116 | array( |
---|
| 117 | '' => array( |
---|
| 118 | 'URL' => make_index_url(array('section' => 'add_photos')), |
---|
| 119 | 'TITLE' => l10n('Upload your own photos'), |
---|
| 120 | 'NAME' => l10n('Upload Photos') |
---|
| 121 | ) |
---|
| 122 | ) |
---|
| 123 | ); |
---|
| 124 | } |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | |
---|
[3673] | 128 | add_event_handler('ws_invoke_allowed', 'community_switch_user_to_admin', EVENT_HANDLER_PRIORITY_NEUTRAL, 3); |
---|
| 129 | |
---|
| 130 | function community_switch_user_to_admin($res, $methodName, $params) |
---|
| 131 | { |
---|
| 132 | global $user; |
---|
| 133 | |
---|
| 134 | $methods_of_permission_level[1] = array( |
---|
| 135 | 'pwg.categories.getList', |
---|
| 136 | 'pwg.tags.getAdminList', |
---|
| 137 | 'pwg.tags.add', |
---|
| 138 | 'pwg.images.exist', |
---|
| 139 | 'pwg.images.add', |
---|
| 140 | 'pwg.images.setInfo', |
---|
| 141 | 'pwg.images.addChunk', |
---|
[6050] | 142 | 'pwg.images.checkUpload', |
---|
[3673] | 143 | ); |
---|
| 144 | |
---|
| 145 | // permission_level 2 has all methods of level 1 + others |
---|
| 146 | $methods_of_permission_level[2] = array_merge( |
---|
| 147 | $methods_of_permission_level[1], |
---|
| 148 | array( |
---|
| 149 | 'pwg.categories.add', |
---|
| 150 | 'pwg.categories.setInfo', |
---|
| 151 | ) |
---|
| 152 | ); |
---|
| 153 | |
---|
| 154 | $query = ' |
---|
| 155 | SELECT |
---|
| 156 | permission_level |
---|
| 157 | FROM '.COMMUNITY_TABLE.' |
---|
| 158 | WHERE user_id = '.$user['id'].' |
---|
| 159 | ;'; |
---|
| 160 | $result = pwg_query($query); |
---|
| 161 | if (1 == mysql_num_rows($result)) |
---|
| 162 | { |
---|
| 163 | list($permission_level) = mysql_fetch_row($result); |
---|
| 164 | |
---|
| 165 | if (in_array($methodName, $methods_of_permission_level[$permission_level])) |
---|
| 166 | { |
---|
| 167 | $user['status'] = 'admin'; |
---|
| 168 | } |
---|
| 169 | } |
---|
| 170 | |
---|
| 171 | return $res; |
---|
| 172 | } |
---|
| 173 | |
---|
| 174 | add_event_handler('delete_user', 'community_delete_user'); |
---|
| 175 | function community_delete_user($user_id) |
---|
| 176 | { |
---|
| 177 | $query = ' |
---|
| 178 | DELETE |
---|
| 179 | FROM '.COMMUNITY_TABLE.' |
---|
| 180 | WHERE user_id = '.$user_id.' |
---|
| 181 | ;'; |
---|
| 182 | pwg_query($query); |
---|
| 183 | } |
---|
| 184 | |
---|
| 185 | ?> |
---|