PageRenderTime 47ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/core/admin/admin_plugins_add.php

http://snowcms.googlecode.com/
PHP | 292 lines | 143 code | 40 blank | 109 comment | 31 complexity | b94131c46c4a30cb0d2814676fdfc892 MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. <?php
  2. ////////////////////////////////////////////////////////////////////////////
  3. // SnowCMS v2.0 //
  4. // By the SnowCMS Team //
  5. // www.snowcms.com //
  6. // Released under the Microsoft Reciprocal License //
  7. // www.opensource.org/licenses/ms-rl.html //
  8. ////////////////////////////////////////////////////////////////////////////
  9. // //
  10. // SnowCMS originally pawned by soren121 started in early 2008 //
  11. // //
  12. ////////////////////////////////////////////////////////////////////////////
  13. // //
  14. // SnowCMS v2.0 began in November 2009 //
  15. // //
  16. ////////////////////////////////////////////////////////////////////////////
  17. // File version: SnowCMS 2.0 //
  18. ////////////////////////////////////////////////////////////////////////////
  19. if(!defined('INSNOW'))
  20. {
  21. die('Nice try...');
  22. }
  23. // Title: Add Plugin
  24. if(!function_exists('admin_plugins_add'))
  25. {
  26. /*
  27. Function: admin_plugins_add
  28. Handles the downloading and extracting of plugins.
  29. Parameters:
  30. none
  31. Returns:
  32. void - Nothing is returned by this function.
  33. Note:
  34. This function is overloadable.
  35. */
  36. function admin_plugins_add()
  37. {
  38. api()->run_hooks('admin_plugins_add');
  39. // Can you add plugins?
  40. if(!member()->can('add_plugins'))
  41. {
  42. // That's what I thought!
  43. admin_access_denied();
  44. }
  45. admin_plugins_add_generate_form();
  46. $form = api()->load_class('Form');
  47. if(!empty($_POST['add_plugins_form']))
  48. {
  49. $form->process('add_plugins_form');
  50. }
  51. // We may need to do a bit of cleanup in the plugin directory. There may
  52. // be some temporary files that don't need to be there anymore.
  53. if(empty($_SESSION['plugindir_cleaned']) || ((int)$_SESSION['plugindir_cleaned'] + 86400) < time_utc())
  54. {
  55. foreach(scandir(plugindir) as $filename)
  56. {
  57. // We don't want to delete any directories, files not ending with
  58. // .tmp, or a file that is newer than a few hours.
  59. if(is_dir(plugindir. '/'. $filename) || substr($filename, -4, 4) != '.tmp' || (filemtime(plugindir. '/'. $filename) + 10800) > time_utc())
  60. {
  61. continue;
  62. }
  63. @unlink(plugindir. '/'. $filename);
  64. }
  65. // Thanks for your help, but we won't have you do it again for awhile!
  66. $_SESSION['plugindir_cleaned'] = time_utc();
  67. }
  68. admin_current_area('plugins_add');
  69. theme()->set_title(l('Add Plugin'));
  70. api()->context['form'] = $form;
  71. theme()->render('admin_plugins_add');
  72. }
  73. }
  74. if(!function_exists('admin_plugins_add_generate_form'))
  75. {
  76. /*
  77. Function: admin_plugins_add_generate_form
  78. Generates the form which allows you to upload or download a plugin.
  79. Parameters:
  80. none
  81. Returns:
  82. void - Nothing is returned by this function.
  83. Note:
  84. This function is overloadable.
  85. */
  86. function admin_plugins_add_generate_form()
  87. {
  88. $form = api()->load_class('Form');
  89. // Let's get to making our form, shall we?
  90. $form->add('add_plugins_form', array(
  91. 'action' => baseurl. '/index.php?action=admin&amp;sa=plugins_add',
  92. 'callback' => 'admin_plugins_add_handle',
  93. 'method' => 'post',
  94. 'submit' => l('Add plugin'),
  95. ));
  96. $form->current('add_plugins_form');
  97. // Do you want to upload the plugin?
  98. $form->add_input(array(
  99. 'name' => 'plugin_file',
  100. 'type' => 'file',
  101. 'label' => l('From a file'),
  102. 'subtext' => l('Select the plugin file you want to install.'),
  103. 'required' => false,
  104. ));
  105. // A URL? Sure!
  106. $form->add_input(array(
  107. 'name' => 'plugin_url',
  108. 'type' => 'string',
  109. 'label' => l('From a URL'),
  110. 'subtext' => l('Enter the URL of the plugin you want to download and install.'),
  111. 'default_value' => 'http://',
  112. ));
  113. }
  114. }
  115. if(!function_exists('admin_plugins_add_handle'))
  116. {
  117. /*
  118. Function: admin_plugins_add_handle
  119. Handles the form data submitted through the add plugins form.
  120. Parameters:
  121. array $data
  122. array &$errors
  123. Returns:
  124. bool - Returns false on failure, the user gets redirected to
  125. {baseurl}/index.php?action=admin&sa=plugins_add&install={filename}
  126. where the status of the plugin is checked and then installed.
  127. Note:
  128. This function is overloadable.
  129. */
  130. function admin_plugins_add_handle($data, &$errors = array())
  131. {
  132. // Where should this plugin go..?
  133. $filename = plugindir. '/'. uniqid('plugin_');
  134. while(file_exists($filename))
  135. {
  136. $filename = plugindir. '/'. uniqid('plugin_');
  137. }
  138. // We wanted to make sure the directory didn't exist yet.
  139. $filename .= '.tmp';
  140. // Uploading a file, are we?
  141. if(!empty($data['plugin_file']['tmp_name']))
  142. {
  143. // Simply try to move the file now.
  144. if(!move_uploaded_file($data['plugin_file']['tmp_name'], $filename))
  145. {
  146. // Woops, didn't work!
  147. $errors[] = l('Plugin upload failed.');
  148. return false;
  149. }
  150. }
  151. // You want us to download it? I can do that.
  152. elseif(!empty($data['plugin_url']) && strtolower($data['plugin_url']) != 'http://')
  153. {
  154. // The HTTP class can do all this, awesomely, of course!
  155. $http = api()->load_class('HTTP');
  156. if(!$http->request($data['plugin_url'], array(), 0, $filename))
  157. {
  158. // Sorry, but looks like it didn't work!!!
  159. $errors[] = l('Failed to download the plugin from &quot;%s&quot;', htmlchars($data['plugin_url']));
  160. return false;
  161. }
  162. }
  163. else
  164. {
  165. $errors[] = l('No file or URL specified.');
  166. return false;
  167. }
  168. // If it worked, we get redirected!
  169. redirect(baseurl. '/index.php?action=admin&sa=plugins_add&install='. urlencode(basename($filename)). '&sid='. member()->session_id());
  170. }
  171. }
  172. if(!function_exists('admin_plugins_install'))
  173. {
  174. /*
  175. Function: admin_plugins_install
  176. Handles the actual installing of the plugin, after things
  177. such as the plugins status is checked on SnowCMS.com
  178. Parameters:
  179. none
  180. Returns:
  181. void - Nothing is returned by this function.
  182. Note:
  183. This function is overloadable.
  184. */
  185. function admin_plugins_install()
  186. {
  187. api()->run_hooks('admin_plugins_install');
  188. // Can you add plugins?
  189. if(!member()->can('add_plugins'))
  190. {
  191. // That's what I thought!
  192. admin_access_denied();
  193. }
  194. admin_current_area('plugins_add');
  195. // Check the session id.
  196. verify_request('get');
  197. // Which file are you installing as a plugin?
  198. $filename = realpath(plugindir. '/'. basename($_GET['install']));
  199. $extension = explode('.', $filename);
  200. // Make sure the file exists, that it is a file, that it is within the
  201. // plugin directory, and that the extension is valid.
  202. if(empty($filename) || !is_file($filename) || substr($filename, 0, strlen(realpath(plugindir))) != realpath(plugindir) || count($extension) < 2 || $extension[count($extension) - 1] != 'tmp')
  203. {
  204. // Must not be valid, from what we can tell.
  205. theme()->set_title(l('An Error Occurred'));
  206. api()->context['error_title'] = '<img src="'. theme()->url(). '/style/images/plugins_add-small.png" alt="" /> '. l('Plugin Installation Error');
  207. api()->context['error_message'] = l('Sorry, but the supplied plugin file either does not exist or is not a valid file.');
  208. theme()->render('error');
  209. }
  210. else
  211. {
  212. // Time to get to installation!
  213. theme()->set_title(l('Installing Plugin'));
  214. // The Component class makes this a snap.
  215. $component = api()->load_class('Component');
  216. $result = $component->install($filename, 'plugin', array(
  217. 'ignore_status' => isset($_GET['status']) && $_GET['status'] == 'ignore',
  218. 'ignore_compatibility' => isset($_GET['compat']) && $_GET['compat'] == 'ignore',
  219. ));
  220. // Make our life even easier, please.
  221. foreach($result as $index => $value)
  222. {
  223. api()->context[$index] = $value;
  224. }
  225. // Should we delete that uploaded file?
  226. if(!empty($result['completed']))
  227. {
  228. unlink($filename);
  229. }
  230. // Set a couple of things.
  231. api()->context['install'] = htmlchars($_GET['install']);
  232. theme()->render('admin_plugins_install');
  233. }
  234. }
  235. }
  236. ?>