| 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 | |
|---|
| 18 | global $prefixeTable; |
|---|
| 19 | define('COMMUNITY_PERMISSIONS_TABLE', $prefixeTable.'community_permissions'); |
|---|
| 20 | define('COMMUNITY_PENDINGS_TABLE', $prefixeTable.'community_pendings'); |
|---|
| 21 | |
|---|
| 22 | include_once(COMMUNITY_PATH.'include/functions_community.inc.php'); |
|---|
| 23 | |
|---|
| 24 | /* Plugin admin */ |
|---|
| 25 | add_event_handler('get_admin_plugin_menu_links', 'community_admin_menu'); |
|---|
| 26 | function community_admin_menu($menu) |
|---|
| 27 | { |
|---|
| 28 | global $page; |
|---|
| 29 | |
|---|
| 30 | $query = ' |
|---|
| 31 | SELECT |
|---|
| 32 | COUNT(*) |
|---|
| 33 | FROM '.COMMUNITY_PENDINGS_TABLE.' |
|---|
| 34 | WHERE state = \'moderation_pending\' |
|---|
| 35 | ;'; |
|---|
| 36 | $result = pwg_query($query); |
|---|
| 37 | list($page['community_nb_pendings']) = pwg_db_fetch_row($result); |
|---|
| 38 | |
|---|
| 39 | $name = 'Community'; |
|---|
| 40 | if ($page['community_nb_pendings'] > 0) |
|---|
| 41 | { |
|---|
| 42 | $style = 'background-color:#666;'; |
|---|
| 43 | $style.= 'color:white;'; |
|---|
| 44 | $style.= 'padding:1px 5px;'; |
|---|
| 45 | $style.= '-moz-border-radius:10px;'; |
|---|
| 46 | $style.= '-webkit-border-radius:10px;'; |
|---|
| 47 | $style.= '-border-radius:10px;'; |
|---|
| 48 | $style.= 'margin-left:5px;'; |
|---|
| 49 | |
|---|
| 50 | $name.= '<span style="'.$style.'">'.$page['community_nb_pendings'].'</span>'; |
|---|
| 51 | |
|---|
| 52 | if (defined('IN_ADMIN') and IN_ADMIN and $page['page'] == 'intro') |
|---|
| 53 | { |
|---|
| 54 | global $template; |
|---|
| 55 | |
|---|
| 56 | $template->set_prefilter('intro', 'community_pendings_on_intro'); |
|---|
| 57 | $template->assign( |
|---|
| 58 | array( |
|---|
| 59 | 'COMMUNITY_PENDINGS' => sprintf( |
|---|
| 60 | '<a href="%s">'.l10n('%u pending photos').'</a>', |
|---|
| 61 | get_root_url().'admin.php?page=plugin-community-pendings', |
|---|
| 62 | $page['community_nb_pendings'] |
|---|
| 63 | ), |
|---|
| 64 | ) |
|---|
| 65 | ); |
|---|
| 66 | } |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | array_push( |
|---|
| 70 | $menu, |
|---|
| 71 | array( |
|---|
| 72 | 'NAME' => $name, |
|---|
| 73 | 'URL' => get_root_url().'admin.php?page=plugin-community' |
|---|
| 74 | ) |
|---|
| 75 | ); |
|---|
| 76 | |
|---|
| 77 | return $menu; |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | function community_pendings_on_intro($content, &$smarty) |
|---|
| 81 | { |
|---|
| 82 | $pattern = '#<li>\s*{\$DB_ELEMENTS\}#ms'; |
|---|
| 83 | $replacement = '<li>{$COMMUNITY_PENDINGS}</li><li>{$DB_ELEMENTS}'; |
|---|
| 84 | return preg_replace($pattern, $replacement, $content); |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | add_event_handler('init', 'community_load_language'); |
|---|
| 88 | function community_load_language() |
|---|
| 89 | { |
|---|
| 90 | if (!defined('IN_ADMIN') or !IN_ADMIN) |
|---|
| 91 | { |
|---|
| 92 | load_language('admin.lang'); |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | load_language('plugin.lang', COMMUNITY_PATH); |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | |
|---|
| 99 | add_event_handler('loc_end_section_init', 'community_section_init'); |
|---|
| 100 | function community_section_init() |
|---|
| 101 | { |
|---|
| 102 | global $tokens, $page; |
|---|
| 103 | |
|---|
| 104 | if ($tokens[0] == 'add_photos') |
|---|
| 105 | { |
|---|
| 106 | $page['section'] = 'add_photos'; |
|---|
| 107 | } |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | add_event_handler('loc_end_index', 'community_index'); |
|---|
| 111 | function community_index() |
|---|
| 112 | { |
|---|
| 113 | global $page; |
|---|
| 114 | |
|---|
| 115 | if (isset($page['section']) and $page['section'] == 'add_photos') |
|---|
| 116 | { |
|---|
| 117 | include(COMMUNITY_PATH.'add_photos.php'); |
|---|
| 118 | } |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | add_event_handler('blockmanager_apply' , 'community_gallery_menu'); |
|---|
| 122 | function community_gallery_menu($menu_ref_arr) |
|---|
| 123 | { |
|---|
| 124 | global $conf, $user; |
|---|
| 125 | |
|---|
| 126 | // conditional : depending on community permissions, display the "Add |
|---|
| 127 | // photos" link in the gallery menu |
|---|
| 128 | $user_permissions = community_get_user_permissions($user['id']); |
|---|
| 129 | |
|---|
| 130 | if (!$user_permissions['upload_whole_gallery'] and count($user_permissions['upload_categories']) == 0) |
|---|
| 131 | { |
|---|
| 132 | return; |
|---|
| 133 | } |
|---|
| 134 | |
|---|
| 135 | $menu = & $menu_ref_arr[0]; |
|---|
| 136 | |
|---|
| 137 | if (($block = $menu->get_block('mbMenu')) != null ) |
|---|
| 138 | { |
|---|
| 139 | load_language('plugin.lang', COMMUNITY_PATH); |
|---|
| 140 | |
|---|
| 141 | array_splice( |
|---|
| 142 | $block->data, |
|---|
| 143 | count($block->data), |
|---|
| 144 | 0, |
|---|
| 145 | array( |
|---|
| 146 | '' => array( |
|---|
| 147 | 'URL' => make_index_url(array('section' => 'add_photos')), |
|---|
| 148 | 'TITLE' => l10n('Upload your own photos'), |
|---|
| 149 | 'NAME' => l10n('Upload Photos') |
|---|
| 150 | ) |
|---|
| 151 | ) |
|---|
| 152 | ); |
|---|
| 153 | } |
|---|
| 154 | } |
|---|
| 155 | |
|---|
| 156 | |
|---|
| 157 | add_event_handler('ws_invoke_allowed', 'community_switch_user_to_admin', EVENT_HANDLER_PRIORITY_NEUTRAL, 3); |
|---|
| 158 | |
|---|
| 159 | function community_switch_user_to_admin($res, $methodName, $params) |
|---|
| 160 | { |
|---|
| 161 | global $user; |
|---|
| 162 | |
|---|
| 163 | $methods_of_permission_level[1] = array( |
|---|
| 164 | 'pwg.categories.getList', |
|---|
| 165 | 'pwg.tags.getAdminList', |
|---|
| 166 | 'pwg.tags.add', |
|---|
| 167 | 'pwg.images.exist', |
|---|
| 168 | 'pwg.images.add', |
|---|
| 169 | 'pwg.images.setInfo', |
|---|
| 170 | 'pwg.images.addChunk', |
|---|
| 171 | 'pwg.images.checkUpload', |
|---|
| 172 | ); |
|---|
| 173 | |
|---|
| 174 | // permission_level 2 has all methods of level 1 + others |
|---|
| 175 | $methods_of_permission_level[2] = array_merge( |
|---|
| 176 | $methods_of_permission_level[1], |
|---|
| 177 | array( |
|---|
| 178 | 'pwg.categories.add', |
|---|
| 179 | 'pwg.categories.setInfo', |
|---|
| 180 | ) |
|---|
| 181 | ); |
|---|
| 182 | |
|---|
| 183 | $query = ' |
|---|
| 184 | SELECT |
|---|
| 185 | permission_level |
|---|
| 186 | FROM '.COMMUNITY_TABLE.' |
|---|
| 187 | WHERE user_id = '.$user['id'].' |
|---|
| 188 | ;'; |
|---|
| 189 | $result = pwg_query($query); |
|---|
| 190 | if (1 == mysql_num_rows($result)) |
|---|
| 191 | { |
|---|
| 192 | list($permission_level) = mysql_fetch_row($result); |
|---|
| 193 | |
|---|
| 194 | if (in_array($methodName, $methods_of_permission_level[$permission_level])) |
|---|
| 195 | { |
|---|
| 196 | $user['status'] = 'admin'; |
|---|
| 197 | } |
|---|
| 198 | } |
|---|
| 199 | |
|---|
| 200 | return $res; |
|---|
| 201 | } |
|---|
| 202 | |
|---|
| 203 | add_event_handler('delete_user', 'community_delete_user'); |
|---|
| 204 | function community_delete_user($user_id) |
|---|
| 205 | { |
|---|
| 206 | $query = ' |
|---|
| 207 | DELETE |
|---|
| 208 | FROM '.COMMUNITY_PERMISSIONS_TABLE.' |
|---|
| 209 | WHERE user_id = '.$user_id.' |
|---|
| 210 | ;'; |
|---|
| 211 | pwg_query($query); |
|---|
| 212 | |
|---|
| 213 | community_reject_user_pendings($user_id); |
|---|
| 214 | } |
|---|
| 215 | ?> |
|---|