]> CyberLeo.Net >> Repos - Github/YOURLS.git/blob - admin/plugins.php
Plugin API & management update: active plugins stored in DB, (de)activation within...
[Github/YOURLS.git] / admin / plugins.php
1 <?php\r
2 define( 'YOURLS_ADMIN', true );\r
3 require_once( dirname(dirname(__FILE__)).'/includes/load-yourls.php' );\r
4 yourls_maybe_require_auth();\r
5 \r
6 // Handle activation/deactivation of plugins\r
7 if( isset( $_GET['action'] ) ) {\r
8 \r
9         // Check nonce\r
10         if( !isset( $_GET['nonce'] ) or !yourls_verify_nonce( $_GET['nonce'], 'manage_plugins' ) )\r
11                 yourls_die( 'Unauthorized action or expired link', 'Error', 403 );\r
12 \r
13         // Check plugin file is valid\r
14         if( isset( $_GET['plugin'] ) && yourls_validate_plugin_file( YOURLS_PLUGINDIR.'/'.$_GET['plugin'].'/plugin.php') ) {\r
15                 \r
16                 global $ydb;\r
17                 // Activate / Deactive\r
18                 switch( $_GET['action'] ) {\r
19                         case 'activate':\r
20                                 $result = yourls_activate_plugin( $_GET['plugin'].'/plugin.php' );\r
21                                 if( $result === true )\r
22                                         yourls_redirect( yourls_admin_url( 'plugins.php?success=activated' ) );\r
23 \r
24                                 break;\r
25                 \r
26                         case 'deactivate':\r
27                                 $result = yourls_deactivate_plugin( $_GET['plugin'].'/plugin.php' );\r
28                                 if( $result === true )\r
29                                         yourls_redirect( yourls_admin_url( 'plugins.php?success=deactivated' ) );\r
30 \r
31                                 break;\r
32                                 \r
33                         default:\r
34                                 $result = 'Unsupported action';\r
35                                 break;\r
36                 }\r
37         } else {\r
38                 $result = 'No plugin specified, or not a valid plugin';\r
39         }\r
40         \r
41         $result = yourls_notice_box( $result );\r
42         yourls_add_action('admin_notices', create_function( '', "echo '$result';" ) );\r
43 }\r
44 \r
45 // Handle message upon succesfull (de)activation\r
46 if( isset( $_GET['success'] ) ) {\r
47         if( $_GET['success'] == 'activated' OR $_GET['success'] == 'deactivated' ) {\r
48                 $result = yourls_notice_box( 'Plugin '.$_GET['success'] );\r
49                 yourls_add_action('admin_notices', create_function( '', "echo '$result';" ) );\r
50         }\r
51 }\r
52 \r
53 \r
54 // TODO: UPDATE PLUGIN LIST IF ONE IS MISSING ?\r
55 \r
56 yourls_html_head( 'plugins' );\r
57 yourls_html_logo();\r
58 yourls_html_menu();\r
59 ?>\r
60 \r
61         <h2>Plugins</h2>\r
62         \r
63         <?php\r
64         $plugins = (array)yourls_get_plugins();\r
65         $count = count( $plugins );\r
66         $count_active = yourls_has_active_plugins();\r
67         ?>\r
68         \r
69         <p>You currently have <strong><?php echo $count.' '.yourls_plural( 'plugin', $count ); ?></strong> installed, and <strong><?php echo $count_active; ?></strong> activated.</p>\r
70 \r
71         <table id="tblUrl" class="tblSorter" cellpadding="0" cellspacing="1">\r
72         <thead>\r
73                 <tr>\r
74                         <th>Plugin Name</th>\r
75                         <th>Version</th>\r
76                         <th>Author</th>\r
77                         <th>Description</th>\r
78                         <th>Action</th>\r
79                 </tr>\r
80         </thead>\r
81         <tbody>\r
82         <?php\r
83         \r
84         $nonce = yourls_create_nonce( 'manage_plugins' );\r
85         \r
86         foreach( $plugins as $file=>$plugin ) {\r
87                 \r
88                 // default fields to read from the plugin header\r
89                 $fields = array(\r
90                         'name'       => 'Plugin Name',\r
91                         'uri'        => 'Plugin URI',\r
92                         'desc'       => 'Description',\r
93                         'version'    => 'Version',\r
94                         'author'     => 'Author',\r
95                         'author_uri' => 'Author URI'\r
96                 );\r
97                 \r
98                 // Loop through all default fields, get value if any and reset it\r
99                 foreach( $fields as $field=>$value ) {\r
100                         if( $plugin[ $value ] ) {\r
101                                 $data[ $field ] = $plugin[ $value ];\r
102                         } else {\r
103                                 $data[ $field ] = '(no info)';\r
104                         }\r
105                         unset( $plugin[$value] );\r
106                 }\r
107                 \r
108                 $plugindir = trim( dirname( $file ), '/' );\r
109                 $action = yourls_is_active_plugin( $file ) ?\r
110                         "<a href='?action=deactivate&plugin=$plugindir&nonce=$nonce'>Deactivate</a>" :\r
111                         "<a href='?action=activate&plugin=$plugindir&nonce=$nonce'>Activate</a>" ;\r
112         \r
113                 // Other "Fields: Value" in the header? Get them too\r
114                 if( $plugin ) {\r
115                         foreach( $plugin as $extra_field=>$extra_value ) {\r
116                                 $data['desc'] .= "<br/>\n<em>$extra_field</em>: $extra_value";\r
117                                 unset( $plugin[$extra_value] );\r
118                         }\r
119                 }\r
120                 \r
121                 $data['desc'] .= "<br/><small>plugin location: $file</small>";\r
122                 \r
123                 printf( "<tr><td><a href='%s'>%s</a></td><td>%s</td><td>%s</td><td><a href='%s'>%s</a></td><td>%s</td></tr>",\r
124                         $data['uri'], $data['name'], $data['version'], $data['desc'], $data['author_uri'], $data['author'], $action\r
125                         );\r
126                 \r
127         }\r
128         ?>\r
129         </tbody>\r
130         </table>\r
131         \r
132         <script type="text/javascript">\r
133         yourls_defaultsort = 0;\r
134         yourls_defaultorder = 0;\r
135         </script>\r
136         \r
137         <p>If something goes wrong after you activate a plugin, simply delete its directory, or rename the plugin file to something different than <code>plugin.php</code>.\r
138 \r
139         \r
140 <?php yourls_html_footer(); ?>