/[drupal]/contributions/modules/upload_package/upload_package.module
ViewVC logotype

Contents of /contributions/modules/upload_package/upload_package.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.2 - (show annotations) (download) (as text)
Wed Dec 5 21:19:31 2007 UTC (23 months, 3 weeks ago) by shawnconn
Branch: MAIN
CVS Tags: DRUPAL-5--1-0, HEAD
Branch point for: DRUPAL-5
Changes since 1.1: +0 -0 lines
File MIME type: text/x-php
*** empty log message ***
1 <?php
2 // $Id$
3
4 /**
5 * @file
6 * Upload modules & themes through the drupal administration interface
7 *
8 * upload_package allows module and theme tarballs (.tar.gz) from drupal.org to
9 * be uploaded and unpacked to a site's module or theme directory within the
10 * drupal administration interface.
11 *
12 * Dependant on PEAR's Archive Tar Package
13 * http://pear.php.net/package/Archive_Tar
14 */
15
16 include_once('PEAR.php');
17
18 /*******************************************************************************
19 * Hook Functions (Drupal)
20 *******************************************************************************/
21
22 /**
23 * Implementation of hook_menu().
24 */
25 function upload_package_menu($may_cache) {
26 $items = array();
27
28 if ($may_cache) {
29 $items[] = array(
30 'path' => 'admin/settings/upload_package',
31 'title' => t('Upload package'),
32 'callback' => 'drupal_get_form',
33 'callback arguments' => array('upload_package_settings_form'),
34 'access' => user_access('administer site configuration'),
35 'type' => MENU_NORMAL_ITEM,
36 );
37 }
38 else {
39 $items[] = array(
40 'path' => 'admin/build/modules/upload_package',
41 'title' => t('Upload'),
42 'access' => user_access('administer site configuration'),
43 'callback' => 'drupal_get_form',
44 'callback arguments' => array('upload_package_upload_form','module'),
45 'description' => t('Upload a module package'),
46 'type' => MENU_LOCAL_TASK,
47 );
48 $items[] = array(
49 'path' => 'admin/build/themes/upload_package',
50 'title' => t('Upload'),
51 'access' => user_access('administer site configuration'),
52 'callback' => 'drupal_get_form',
53 'callback arguments' => array('upload_package_upload_form','theme'),
54 'description' => t('Upload a theme package'),
55 'type' => MENU_LOCAL_TASK,
56 );
57 }
58
59 return $items;
60 }
61
62 /*******************************************************************************
63 * Callback Functions, Forms, and Tables
64 *******************************************************************************/
65
66 function upload_package_upload_form($type = 'module') {
67 $path = array('%directory_path' => (($type == 'module') ? variable_get('upload_package_module_path','sites/all/modules') : variable_get('upload_package_theme_path','sites/all/themes')));
68 $form['type'] = array(
69 '#type' => 'value',
70 '#value' => $type,
71 );
72 $form['upload'] = array(
73 '#type' => 'file',
74 '#title' => t('!package_type Package',array('!package_type' => ($type == 'module') ? t('Module') : t('Theme'))),
75 '#size' => 40,
76 '#description' => t('The package must be a file in the .tar.gz format (this package will be unpacked to %directory_path).',$path),
77 );
78 $form['url'] = array(
79 '#type' => 'textfield',
80 '#title' => t('Package URL'),
81 '#description' => t('Optional, the URL containing the module package (this package will be unpacked to %directory_path).',$path),
82 );
83 $form['submit'] = array(
84 '#type' => 'submit',
85 '#value' => t('Upload'),
86 );
87 $form['#attributes'] = array('enctype' => "multipart/form-data");
88
89 return $form;
90 }
91 function upload_package_upload_form_validate($form_id, $form_values) {
92 if (!file_check_upload() && empty($form_values['url'])) {
93 form_set_error('upload',t('Select a file to upload or enter a URL to download.'));
94 }
95 }
96 function upload_package_upload_form_submit($form_id, $form_values) {
97 include_once ('Archive/Tar.php');
98 $filename = tempnam(variable_get('file_directory_path','files'), 'upload_package');
99
100 if (!($uploaded_file = file_check_upload())) {
101 $file_request = drupal_http_request($form_values['url']);
102 if ($file_request->code != 200) {
103 form_set_error('url',t('An error occurred retrieving the package from the specified URL.'));
104 return;
105 }
106 file_put_contents($filename,$file_request->data);
107 }
108 else {
109 file_put_contents($filename,file_get_contents($uploaded_file->filepath));
110 }
111
112 $package = new Archive_Tar($filename);
113 if (!($package_contents = $package->listContent())) {
114 drupal_set_message(t("An error occurred while reading the contents of the package."),'error');
115 return;
116 }
117
118 if ($form_values['type'] == 'module') {
119 _upload_package_save_module($package);
120 }
121 else {
122 _upload_package_save_theme($package);
123 }
124 unlink($filename);
125 }
126
127 function upload_package_settings_form() {
128 $form['upload_package_module_path'] = array(
129 '#type' => 'textfield',
130 '#title' => t('Module upload path'),
131 '#default_value' => variable_get('upload_package_module_path','sites/all/modules'),
132 '#description' => t('The relative path from site root where uploaded modules are unpacked to.'),
133 );
134 $form['upload_package_theme_path'] = array(
135 '#type' => 'textfield',
136 '#title' => t('Theme upload path'),
137 '#default_value' => variable_get('upload_package_theme_path','sites/all/themes'),
138 '#description' => t('The relative path from site root where uploaded themes are unpacked to.'),
139 );
140
141 return system_settings_form($form);
142 }
143 function upload_package_settings_form_validate($form_id, $form_values) {
144 file_check_directory($form_values['upload_package_module_path'], FILE_CREATE_DIRECTORY, 'upload_package_module_path');
145 file_check_directory($form_values['upload_package_theme_path'], FILE_CREATE_DIRECTORY, 'upload_package_theme_path');
146 }
147
148 /*******************************************************************************
149 * Module and Helper Functions
150 *******************************************************************************/
151
152 /**
153 * Function saves the contents of a tarball to the module directory
154 * @param $package
155 * The Archive_Tar object that is to be unpacked to the module directory
156 */
157 function _upload_package_save_module(&$package) {
158 $module_dir = variable_get('upload_package_module_path','sites/all/modules');
159 $package_contents = $package->listContent();
160 $is_module = FALSE;
161
162 foreach ($package_contents as $file) {
163 if (strpos($file['filename'],'.module')) {
164 $is_module = TRUE;
165 $module = db_fetch_object(db_query("SELECT filename FROM {system} WHERE type = 'module' AND filename LIKE '%%%s'",$file['filename']));
166 if ($module->filename != $module_dir.'/'.$file['filename'] && !empty($module)) {
167 drupal_set_message(t("Can't install module to %new_dir, this module already exists in %existing_dir",array('%new_dir' => $module_dir.'/'.$file['filename'], '%existing_dir' => $module->filename)),'error');
168 return;
169 }
170 }
171 }
172
173 if (!$is_module) {
174 drupal_set_message(t("The contents of the package do not contain any modules."),'error');
175 return;
176 }
177
178 foreach ($package_contents as $file) {
179 $local_file = $module_dir.'/'.$file['filename'];
180 if (substr($file['filename'],-1) == '/') {
181 if (!is_dir($local_file)) {
182 drupal_set_message(t("The directory %directory_name has been created",array('%directory_name' => $local_file)));
183 mkdir($local_file, 0700);
184 }
185 }
186 else {
187 drupal_set_message(t("The file %file_name has been !action.",array('%file_name' => $local_file, '!action' => ((is_file($local_file)) ? t("overwritten") : t("created")))));
188 file_put_contents($local_file,$package->extractInString($file['filename']));
189 }
190 }
191
192 drupal_goto('admin/build/modules');
193 }
194
195 /**
196 * Function saves the contents of a tarball to the theme directory
197 * @param $package
198 * The Archive_Tar object that is to be unpacked to the theme directory
199 */
200 function _upload_package_save_theme(&$package) {
201 $theme_dir = variable_get('upload_package_theme_path','sites/all/themes');
202 $package_contents = $package->listContent();
203 $is_theme = FALSE;
204
205 foreach ($package_contents as $file) {
206 if (preg_match('/page.tpl.php$|style.css$|([^\/]+)\/\\1\.theme$/', $file['filename'], $matches)) {
207 $is_theme = TRUE;
208 $theme = db_fetch_object(db_query("SELECT filename FROM {system} WHERE type = 'theme' AND filename LIKE '%%%s'",$file['filename']));
209 if ($theme->filename != $theme_dir.'/'.$file['filename'] && !empty($theme)) {
210 drupal_set_message(t("Can't install theme to %new_dir, this module already exists in %existing_dir",array('%new_dir' => $theme_dir.'/'.$file['filename'], '%existing_dir' => $theme->filename)),'error');
211 return;
212 }
213 }
214 }
215
216 if (!$is_theme) {
217 drupal_set_message(t("The contents of the package do not contain any themes."),'error');
218 return;
219 }
220
221 foreach ($package_contents as $file) {
222 $local_file = $theme_dir.'/'.$file['filename'];
223 if (substr($file['filename'],-1) == '/') {
224 if (!is_dir($local_file)) {
225 drupal_set_message(t("The directory %directory_name has been created",array('%directory_name' => $local_file)));
226 mkdir($local_file, 0700);
227 }
228 }
229 else {
230 drupal_set_message(t("The file %file_name has been !action.",array('%file_name' => $local_file, '!action' => ((is_file($local_file)) ? t("overwritten") : t("created")))));
231 file_put_contents($local_file,$package->extractInString($file['filename']));
232 }
233 }
234
235 drupal_goto('admin/build/themes');
236 }

  ViewVC Help
Powered by ViewVC 1.1.2