/[drupal]/contributions/modules/bitcache/bitcache.admin.inc
ViewVC logotype

Contents of /contributions/modules/bitcache/bitcache.admin.inc

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


Revision 1.28 - (show annotations) (download) (as text)
Wed Jul 1 04:09:48 2009 UTC (4 months, 3 weeks ago) by arto
Branch: MAIN
CVS Tags: HEAD
Changes since 1.27: +2 -2 lines
File MIME type: text/x-php
Changelog:
- #433708: Implemented support for tokenized file system repository paths.
- Added an optional repository parameter to the Services API method 'bitcache.put'.
1 <?php
2 // $Id$
3
4 //////////////////////////////////////////////////////////////////////////////
5 // Bitcache settings
6
7 /**
8 * Form builder: displays the Bitcache configuration screen.
9 *
10 * @ingroup forms
11 * @see system_settings_form()
12 */
13 function bitcache_admin_settings() {
14 $form = array();
15
16 // Server settings
17 $form['server'] = array('#type' => 'fieldset', '#title' => t('Server settings'), '#collapsible' => TRUE, '#collapsed' => TRUE);
18 $form['server']['bitcache_server'] = array(
19 '#type' => 'radios',
20 '#title' => t('Bitcache REST API'),
21 '#default_value' => (int)BITCACHE_SERVER,
22 '#options' => array(t('Disabled'), t('Enabled')),
23 '#description' => t('When enabled, the built-in Bitcache server is made available at !url. Access to the <a href="http://en.wikipedia.org/wiki/Representational_State_Transfer" target="_blank" title="Representational State Transfer">REST</a> <acronym title="Application Programming Interface">API</acronym> is subject to Drupal <a href="@manage-users">authentication</a> and <a href="@manage-perms">permissions</a>.', array('!url' => l(BITCACHE_URL, BITCACHE_URL), '@manage-users' => url('admin/user'), '@manage-perms' => url('admin/user/permissions', array('fragment' => 'module-bitcache')))),
24 );
25 $form['server']['bitcache_features'] = array(
26 '#type' => 'checkboxes',
27 '#title' => t('Enabled features'),
28 '#default_value' => bitcache_get_features(),
29 '#options' => array('index' => t('INDEX'), 'get' => t('GET/HEAD'), 'post' => t('POST'), 'put' => t('PUT'), 'delete' => t('DELETE')),
30 '#description' => t('Determines which HTTP methods will be supported in the <a href="http://bitcache.org/api/rest" target="_blank">REST API</a>. For finer-grained access controls based on user credentials, refer to the Drupal <a href="@manage-perms">permissions</a> for Bitcache.', array('@manage-perms' => url('admin/user/permissions', array('fragment' => 'module-bitcache')))),
31 );
32 $form['server']['bitcache_transfer_method'] = array(
33 '#type' => 'select',
34 '#title' => t('Download method'),
35 '#default_value' => BITCACHE_TRANSFER_METHOD,
36 '#options' => array('redirect' => t('Redirect (external)'), 'xsendfile' => t('Redirect (internal X-Sendfile)'), 'readfile' => t('Passthrough (unbuffered)'), 'read4k' => t('Passthrough (4K chunks)')),
37 '#description' => t('Determines how the bitstream contents will be transferred to the client when a download is requested. Note that this is a default setting only, and the actual transfer method may in practice be determined by the repository storage location.'),
38 );
39
40 // Bitcache modules
41 $form['modules'] = array('#type' => 'fieldset', '#title' => t('Bitcache modules'), '#collapsible' => TRUE, '#collapsed' => TRUE);
42 if (count(bitcache_get_modules()) > 0) {
43 $head = array(array('data' => t('Name'), 'width' => '30%'), t('Description'));
44 $rows = array();
45 foreach (bitcache_get_modules('info') as $name => $info) {
46 $rows[] = array($info->title, array('data' => $info->description, 'class' => 'description'));
47 }
48 $modules = theme('table', $head, $rows);
49 }
50 else {
51 $modules = '<div>' . t('No Bitcache modules currently installed.') . '</div>';
52 }
53 $form['modules']['bitcache_modules'] = array('#type' => 'markup', '#value' => $modules);
54
55 // Bitcache adapters
56 $form['adapters'] = array('#type' => 'fieldset', '#title' => t('Bitcache adapters'), '#collapsible' => TRUE, '#collapsed' => TRUE);
57 if (count(bitcache_get_adapters()) > 0) {
58 $head = array(array('data' => t('Name'), 'width' => '30%'), t('Description'));
59 $rows = array();
60 foreach (bitcache_get_adapters('info') as $name => $info) {
61 $row = array($info->title, array('data' => $info->description, 'class' => 'description'));
62 // Show greyed-out text for adapters that are not available (e.g. due to missing PHP extensions)
63 $rows[] = !empty($info->enabled) ? $row : array('data' => $row, 'class' => 'menu-disabled');
64 }
65 $adapters = theme('table', $head, $rows);
66 }
67 else {
68 $adapters = '<div>' . t('No Bitcache adapters currently available.') . '</div>';
69 }
70 $form['adapters']['bitcache_adapters'] = array('#type' => 'markup', '#value' => $adapters);
71
72 // Bitcache tools
73 $form['tools'] = array('#type' => 'fieldset', '#title' => t('Bitcache tools'), '#collapsible' => TRUE, '#collapsed' => TRUE);
74 $form['tools']['bitcache_exec'] = array(
75 '#type' => 'textfield',
76 '#title' => t('Path to Bitcache executable'),
77 '#default_value' => BITCACHE_EXEC,
78 '#size' => 60,
79 '#maxlength' => 255,
80 '#required' => FALSE,
81 '#description' => t('A file system path to the <code>bit</code> binary. On Unix systems, this would typically be located at <code>/usr/bin/bit</code> or <code>/usr/local/bin/bit</code>. On Mac OS X with MacPorts, the path would typically be <code>/opt/local/bin/bit</code>. In case you have installed the command-line tools locally under your Drupal installation according to <code>INSTALL.txt</code>, the path would be <code>!path</code>.', array('!path' => drupal_get_path('module', 'bitcache') . '/tools/bin/bit'))
82 );
83
84 // Cron settings
85 $form['cron'] = array('#type' => 'fieldset', '#title' => t('Cron script'), '#collapsible' => TRUE, '#collapsed' => TRUE);
86 $form['cron']['bitcache_cron'] = array('#type' => 'textfield', '#title' => t('Shell command to execute on cron runs'), '#default_value' => BITCACHE_CRON, '#maxlength' => 255, '#description' => t('To synchronize the local Bitcache repository with a remote location, enter an <em>rsync</em> or <em>bit-sync</em> command that should be triggered during cron maintenance runs. Leave empty to disable this functionality.'), '#required' => FALSE);
87 // TODO: execute only if creates/deletes vs. execute regardless
88
89 // Stream wrappers
90 $wrappers = implode(', ', stream_get_wrappers()); // requires PHP 5
91 $form['wrappers'] = array('#type' => 'fieldset', '#title' => t('Stream wrappers'), '#collapsible' => TRUE, '#collapsed' => TRUE);
92 $form['wrappers']['list'] = array(
93 '#type' => 'markup',
94 '#value' => '<div class="form-item"><label>' . t('Available stream wrappers') . ':</label>' . $wrappers . '<div class="description">' . t('This is the list of currently registered <a href="http://php.net/stream">PHP stream wrappers</a> that can be used when specifying storage locations for file system <a href="@manage-repos">repositories</a>.', array('@manage-repos' => url('admin/settings/bitcache/repositories'))) . '</div></div>',
95 );
96
97 return system_settings_form($form);
98 }
99
100 //////////////////////////////////////////////////////////////////////////////
101 // Bitcache bitstream management
102
103 function bitcache_admin_bitstreams(&$form_state, $name = NULL, $max_count = 100) {
104 drupal_add_css(drupal_get_path('module', 'bitcache') . '/bitcache.css');
105
106 $head = array(t('ID'), t('Kind'), array('data' => t('Size'), 'class' => 'size'), array('data' => t('Operations'), 'colspan' => 2));
107 $rows = array();
108
109 $repo = bitcache_get_repository($name);
110 foreach ($repo as $id => $stream) {
111 $rows[$id] = array(
112 l($id, 'admin/content/bitcache/view/' . $id),
113 $stream->type(),
114 array('data' => format_size($stream->size()), 'class' => 'size'),
115 bitcache_l('download', $id),
116 l('delete', 'admin/content/bitcache/delete/' . $id),
117 );
118 if (@++$count == $max_count) {
119 break;
120 }
121 }
122 ksort($rows);
123 $rows = array_values($rows);
124
125 if (empty($rows)) {
126 $rows[] = array(array('data' => t('No bitstreams available.'), 'colspan' => '5'));
127 }
128
129 $form['table'] = array('#type' => 'markup', '#value' => theme('table', $head, $rows, array('class' => 'bitcache')));
130 return $form;
131 }
132
133 function theme_bitcache_admin_bitstreams($form) {
134 return drupal_render($form); // TODO
135 }
136
137 function bitcache_admin_bitstream_upload(&$form_state, $edit = array('attach' => '', 'repository' => 'bitcache')) {
138 $edit = (object)$edit;
139 $form = array();
140
141 $form['upload'] = array('#type' => 'fieldset', '#title' => t('Upload file'));
142 $form['upload']['attach'] = array(
143 '#type' => 'file',
144 '#title' => '',
145 '#default_value' => $edit->attach,
146 '#description' => t(''), // TODO
147 );
148
149 $form['upload']['repository'] = array(
150 '#type' => 'select',
151 '#title' => t('Repository'),
152 '#default_value' => $edit->repository,
153 '#options' => bitcache_get_repositories('titles'),
154 '#description' => t('Select the Bitcache repository that will be used to store the uploaded bitstream.'),
155 );
156
157 $form['submit'] = array('#type' => 'submit', '#value' => t('Upload bitstream'));
158 $form['#attributes']['enctype'] = 'multipart/form-data';
159 return $form;
160 }
161
162 function bitcache_admin_bitstream_upload_validate($form, &$form_state) {
163 extract($form_state['values'], EXTR_SKIP | EXTR_REFS);
164 $source = 'attach';
165
166 if (!isset($_FILES['files']) || !$_FILES['files']['name'][$source] || !is_uploaded_file($_FILES['files']['tmp_name'][$source])) {
167 form_set_error($source, t('File upload required.'));
168 }
169 }
170
171 function bitcache_admin_bitstream_upload_submit($form, &$form_state) {
172 extract($form_state['values'], EXTR_SKIP | EXTR_REFS);
173 $source = 'attach';
174
175 if (isset($_FILES['files']) && $_FILES['files']['name'][$source] && is_uploaded_file($_FILES['files']['tmp_name'][$source])) {
176 bitcache_use_repository($repository);
177 if (!($id = bitcache_put_file(NULL, $_FILES['files']['tmp_name'][$source], TRUE))) {
178 watchdog('bitcache', 'Upload error. Could not move uploaded file %file to repository.', array('%file' => $_FILES['files']['tmp_name'][$source]), WATCHDOG_ERROR);
179 return;
180 }
181 watchdog('bitcache', 'Bitstream uploaded: %id.', array('%id' => $id), WATCHDOG_NOTICE, l(t('view'), 'admin/content/bitcache'));
182 drupal_set_message(t('The bitstream %id was successfully uploaded.', array('%id' => $id)));
183 }
184
185 $form_state['redirect'] = 'admin/content/bitcache';
186 }
187
188 function bitcache_admin_bitstream_fetch(&$form_state, $edit = array('url' => 'http://', 'repository' => 'bitcache')) {
189 $edit = (object)$edit;
190 $form = array();
191
192 set_time_limit(0); // let's take all the time we need
193
194 $form['fetch'] = array('#type' => 'fieldset', '#title' => t('Fetch URL'));
195 $form['fetch']['url'] = array(
196 '#type' => 'textfield',
197 '#title' => '',
198 '#default_value' => $edit->url,
199 '#maxlength' => 255,
200 '#description' => t(''),
201 '#required' => FALSE,
202 );
203
204 $form['fetch']['repository'] = array(
205 '#type' => 'select',
206 '#title' => t('Repository'),
207 '#default_value' => $edit->repository,
208 '#options' => bitcache_get_repositories('titles'),
209 '#description' => t('Select the Bitcache repository that will be used to store the fetched bitstream.'),
210 );
211
212 $form['submit'] = array('#type' => 'submit', '#value' => t('Fetch bitstream'));
213 return $form;
214 }
215
216 function bitcache_admin_bitstream_fetch_validate($form, &$form_state) {
217 extract($form_state['values'], EXTR_SKIP | EXTR_REFS);
218
219 $url = trim($url);
220 if (!valid_url($url) || !@parse_url($url)) {
221 form_set_error('url', t('Malformed or invalid URL.'));
222 }
223 }
224
225 function bitcache_admin_bitstream_fetch_submit($form, &$form_state) {
226 extract($form_state['values'], EXTR_SKIP | EXTR_REFS);
227
228 if (preg_match('!^(http|https)://!', $url)) { // HTTP or HTTPS
229 if ($response = @drupal_http_request($url)) {
230 if (isset($response->error)) {
231 form_set_error('url', t('Error fetching %url: @error', array('%url' => $url, '@error' => $response->error)));
232 return;
233 }
234 $data = $response->data;
235 }
236 }
237 else {
238 if (($data = file_get_contents($url)) === FALSE) {
239 form_set_error('url', t('Error fetching %url.', array('%url' => $url)));
240 return;
241 }
242 }
243
244 if (isset($data)) {
245 bitcache_use_repository($repository);
246 if (!($id = bitcache_put(NULL, $data))) {
247 watchdog('bitcache', 'Upload error. Could not store contents of %url to repository.', array('%url' => $url), WATCHDOG_ERROR);
248 return;
249 }
250 watchdog('bitcache', 'Bitstream uploaded: %id.', array('%id' => $id), WATCHDOG_NOTICE, l(t('view'), 'admin/content/bitcache'));
251 drupal_set_message(t('The bitstream %id was successfully fetched.', array('%id' => $id)));
252 }
253
254 $form_state['redirect'] = 'admin/content/bitcache';
255 }
256
257 function bitcache_admin_bitstream_view($id) {
258 if (!bitcache_exists($id)) {
259 return drupal_not_found();
260 }
261
262 $blob = bitcache_get($id);
263
264 $rows = array(
265 array(
266 array('data' => t('Repository'), 'header' => TRUE),
267 t('Default'), // FIXME
268 ),
269 array(
270 array('data' => t('ID'), 'header' => TRUE),
271 t('@id (%algo)', array('@id' => $id, '%algo' => 'SHA-1')), // FIXME
272 ),
273 array(
274 array('data' => t('Size'), 'header' => TRUE),
275 format_size($blob->size()),
276 ),
277 array(
278 array('data' => t('MIME type'), 'header' => TRUE),
279 check_plain($blob->type()),
280 ),
281 array(
282 array('data' => t('Created'), 'header' => TRUE),
283 format_date(@filectime($blob->path), 'large'), // FIXME
284 ),
285 array(
286 array('data' => t('Compressed'), 'header' => TRUE),
287 !empty($blob->compressed) ? t('Yes') : t('No'),
288 ),
289 array(
290 array('data' => t('Encrypted'), 'header' => TRUE),
291 !empty($blob->encrypted) ? t('Yes') : t('No'),
292 ),
293 );
294
295 return theme('table', array(), $rows, array('class' => 'bitcache bitstream'));
296 }
297
298 function bitcache_admin_bitstream_delete($form_state, $id) {
299 $form['id'] = array('#type' => 'value', '#value' => $id);
300
301 return confirm_form($form,
302 t('Are you sure you want to delete the bitstream %id?', array('%id' => $id)),
303 isset($_GET['destination']) ? $_GET['destination'] : 'admin/content/bitcache',
304 t('This action cannot be undone.'));
305 }
306
307 function bitcache_admin_bitstream_delete_submit($form, &$form_state) {
308 extract($form_state['values'], EXTR_SKIP | EXTR_REFS);
309
310 if ($form_state['values']['confirm']) {
311 bitcache_delete($id);
312
313 watchdog('bitcache', 'Bitstream deleted: %id.', array('%id' => $id));
314 drupal_set_message(t('The bitstream %id has been deleted.', array('%id' => $id)));
315 }
316
317 $form_state['redirect'] = 'admin/content/bitcache';
318 }
319
320 //////////////////////////////////////////////////////////////////////////////
321 // Bitcache repository management
322
323 /**
324 * Menu callback: displays the Bitcache repository management screen.
325 */
326 function bitcache_admin_repos(&$form_state) {
327 $form = array('#tree' => TRUE);
328
329 $adapters = bitcache_get_adapters('titles');
330 foreach (bitcache_get_repositories('info') as $name => $info) {
331 $available = bitcache_has_adapter($info['adapter']);
332 $configurable = ($name != 'bitcache') && empty($info['module']);
333
334 $form[$name]['title'] = array('#value' => !$available ? $info['title'] : l($info['title'], 'admin/content/bitcache/' . $name, array('attributes' => array('title' => @$info['description']))));
335 $form[$name]['adapter'] = array('#value' => isset($adapters[$info['adapter']]) ? $adapters[$info['adapter']] : $info['adapter']);
336 $form[$name]['count'] = array('#value' => !$available ? '-' : number_format(bitcache_get_repository_count($name)));
337 $form[$name]['size'] = array('#value' => !$available ? '-' : format_size(bitcache_get_repository_size($name)));
338 $form[$name]['weight'] = array('#type' => 'weight', '#delta' => 50, '#default_value' => $info['weight']);
339 $form[$name]['edit'] = array('#value' => !$available || !$configurable ? '' : l(t('configure'), 'admin/settings/bitcache/repository/edit/' . $name));
340 $form[$name]['delete'] = array('#value' => !$configurable ? '' : l(t('delete'), 'admin/settings/bitcache/repository/delete/' . $name));
341 $form[$name]['#class'] = !$available ? 'menu-disabled' : '';
342 }
343
344 $form['buttons']['submit'] = array('#type' => 'submit', '#value' => t('Save configuration'));
345 $form['buttons']['reset'] = array('#type' => 'submit', '#value' => t('Reset to defaults'));
346 return $form;
347 }
348
349 function theme_bitcache_admin_repos($form) {
350 $head = array(t('Name'), t('Kind'), t('Total Items'), t('Total Size'), t('Weight'), array('data' => t('Operations'), 'colspan' => '2'));
351 $rows = array();
352
353 foreach (element_children($form) as $key) {
354 if (($row = &$form[$key]) && !isset($row['title'])) {
355 continue;
356 }
357
358 $row['weight']['#attributes']['class'] = 'repository-weight';
359 $rows[] = array(
360 'data' => array(
361 drupal_render($row['title']),
362 drupal_render($row['adapter']),
363 drupal_render($row['count']),
364 drupal_render($row['size']),
365 drupal_render($row['weight']),
366 drupal_render($row['edit']),
367 drupal_render($row['delete']),
368 ),
369 'class' => trim('draggable' . ' ' . $row['#class']),
370 );
371 }
372
373 $form['#value'] = theme('table', $head, $rows, array('id' => 'bitcache-repositories', 'class' => 'bitcache repositories'));
374 drupal_add_tabledrag('bitcache-repositories', 'order', 'sibling', 'repository-weight');
375
376 return drupal_render($form);
377 }
378
379 function bitcache_admin_repos_submit($form, &$form_state) {
380 extract($form_state['values'], EXTR_SKIP | EXTR_REFS);
381
382 if ($op == $buttons['reset']) {
383 db_query("UPDATE {bitcache_repositories} SET weight = 0"); // reset weights
384 db_query("UPDATE {bitcache_repositories} SET weight = -10 WHERE name = '%s'", 'bitcache');
385 db_query("UPDATE {bitcache_repositories} SET weight = -1 WHERE name = '%s'", 'default');
386 drupal_set_message(t('The configuration options have been reset to their default values.'));
387 return;
388 }
389
390 foreach ($form_state['values'] as $name => $row) {
391 if (isset($row['weight'])) {
392 db_query("UPDATE {bitcache_repositories} SET weight = %d WHERE name = '%s'", (int)$row['weight'], $name);
393 }
394 unset($form_state['values'][$name]);
395 }
396 drupal_set_message(t('The configuration options have been saved.'));
397 }
398
399 function bitcache_admin_repo_create($adapter = 'sql') {
400 return drupal_get_form('bitcache_admin_repo_form', array('name' => '', 'title' => '', 'description' => '', 'adapter' => $adapter, 'location' => BITCACHE_ROOT . '/'));
401 }
402
403 function bitcache_admin_repo_edit($name) {
404 return drupal_get_form('bitcache_admin_repo_form', bitcache_get_repository($name)->options);
405 }
406
407 function bitcache_admin_repo_form(&$form_state, $edit = array('name' => '', 'title' => '', 'description' => '', 'adapter' => 'sql', 'location' => '')) {
408 $edit = (object)$edit;
409 $form = array();
410
411 // Repository identification
412 $form['identity'] = array('#type' => 'fieldset', '#title' => t('Repository identification'));
413 $form['identity']['title'] = array(
414 '#type' => 'textfield',
415 '#title' => t('Title'),
416 '#default_value' => $edit->title,
417 '#maxlength' => 64,
418 '#description' => t('The human-readable name of this repository. This text will be displayed as part of the list on the repository management page. This name must begin with a capital letter and contain only letters, numbers, and spaces. This name must be unique.'),
419 '#required' => TRUE,
420 '#disabled' => ($edit->name == 'bitcache'),
421 );
422 $form['identity']['name'] = array(
423 '#type' => 'textfield',
424 '#title' => t('Name'),
425 '#default_value' => $edit->name,
426 '#maxlength' => 64,
427 '#description' => t('The machine-readable name of this repository. This name must contain only lowercase letters, numbers, and underscores. This name must be unique.'),
428 '#required' => TRUE,
429 '#disabled' => ($edit->name == 'bitcache'),
430 );
431 $form['identity']['description'] = array(
432 '#title' => t('Description'),
433 '#type' => 'textarea',
434 '#default_value' => $edit->description,
435 '#rows' => 2,
436 '#description' => t('A brief description of this repository. This is only used for administrative purposes, so you can leave this empty if you wish.'),
437 '#disabled' => ($edit->name == 'bitcache'),
438 );
439 drupal_add_js(array('bitcache' => array('locationBase' => !empty($edit->location) ? $edit->location : '', 'locationChanged' => FALSE)), 'setting');
440 if (empty($edit->name)) { // only for new repositories
441 drupal_add_js('if (Drupal.jsEnabled) { $(document).ready(function() { $("#edit-name").keyup(function() { if (!Drupal.settings.bitcache.locationChanged) { var path = Drupal.settings.bitcache.locationBase + this.value; $("#edit-file-location").val(path); $("#edit-gdbm-location").val(path + ".db"); } }); }); }', 'inline');
442 }
443
444 // Repository storage
445 $form['storage'] = array('#type' => 'fieldset', '#title' => t('Repository storage'));
446 $form['storage']['adapter'] = array(
447 '#type' => 'select',
448 '#title' => t('Storage adapter'),
449 '#default_value' => !empty($edit->adapter) ? $edit->adapter : 'sql',
450 '#options' => bitcache_get_adapters('titles', TRUE),
451 '#description' => t('Select a repository <a href="http://bitcache.org/faq/storage-adapters" target="_blank">storage adapter</a>. This determines how and where the repository content is stored.'),
452 '#disabled' => !empty($edit->name), // only enabled when creating repository
453 );
454
455 // Repository storage (PDO)
456 if (bitcache_has_adapter('pdo')) {
457 $form['storage']['pdo'] = array(
458 '#prefix' => '<div id="edit-pdo-wrapper" style="display: ' . ((!empty($edit->adapter) && $edit->adapter != 'pdo') ? 'none' : 'block') . ';">',
459 '#suffix' => '</div>',
460 '#tree' => TRUE,
461 );
462 $form['storage']['pdo']['dsn'] = array(
463 '#type' => 'textfield',
464 '#title' => t('DSN'),
465 '#default_value' => !empty($edit->dsn) ? $edit->dsn : '',
466 '#maxlength' => 255,
467 '#description' => t('The Data Source Name (DSN) that contains the information required to connect to the database. You can use any of the available <a href="http://php.net/manual/en/pdo.drivers.php" target="_blank">PDO drivers</a>. For <a href="http://php.net/manual/en/ref.pdo-sqlite.php" target="_blank">SQLite</a>, you can use <code>sqlite::memory:</code> for a non-persistent in-memory database, or <code>sqlite:/path/to/database.sqlite</code> for a persistent database file. For <a href="http://php.net/manual/en/ref.pdo-mysql.php" target="_blank">MySQL</a>, specify the DSN in a form that includes the user name and password, as in <code>mysql:host=localhost;dbname=mydb;user=myuser;password=mypasswd</code>. Note that by default (regardless of the PDO driver used), data will be stored in a database table named %table, which will be created if it doesn\'t exist; you can override this by specifying <code>;table=mytable</code> as part of the DSN.', array('%table' => BITCACHE_TABLE_DEFAULT)),
468 '#required' => FALSE,
469 );
470 drupal_add_js('if (Drupal.jsEnabled) { $(document).ready(function() { $("#edit-adapter").change(function() { $("#edit-pdo-wrapper").css("display", (this.value == "pdo" ? "block" : "none")); }); }); }', 'inline');
471 }
472
473 // Repository storage (GDBM)
474 if (bitcache_has_adapter('gdbm')) {
475 $form['storage']['gdbm'] = array(
476 '#prefix' => '<div id="edit-gdbm-wrapper" style="display: ' . ((!empty($edit->adapter) && $edit->adapter != 'gdbm') ? 'none' : 'block') . ';">',
477 '#suffix' => '</div>',
478 '#tree' => TRUE,
479 );
480 $form['storage']['gdbm']['location'] = array(
481 '#type' => 'textfield',
482 '#title' => t('Location'),
483 '#default_value' => !empty($edit->location) ? $edit->location : '',
484 '#maxlength' => 255,
485 '#description' => t('The file system path to the GDBM file. If the file doesn\'t exist, it will be created provided the directory is writable.'),
486 '#required' => FALSE,
487 );
488 drupal_add_js('if (Drupal.jsEnabled) { $(document).ready(function() { $("#edit-adapter").change(function() { $("#edit-gdbm-wrapper").css("display", (this.value == "gdbm" ? "block" : "none")); }); }); }', 'inline');
489 drupal_add_js('if (Drupal.jsEnabled) { $(document).ready(function() { $("#edit-gdbm-location").keypress(function() { Drupal.settings.bitcache.locationChanged = true; }); }); }', 'inline');
490 }
491
492 // Repository storage (File system)
493 if (bitcache_has_adapter('file')) {
494 $form['storage']['file'] = array(
495 '#prefix' => '<div id="edit-file-wrapper" style="display: ' . (empty($edit->location) || (!empty($edit->adapter) && $edit->adapter != 'file') ? 'none' : 'block') . ';">',
496 '#suffix' => '</div>',
497 '#tree' => TRUE,
498 );
499 $form['storage']['file']['location'] = array(
500 '#type' => 'textfield',
501 '#title' => t('Location'),
502 '#default_value' => !empty($edit->location) ? $edit->location : '',
503 '#maxlength' => 255,
504 '#description' => t('The storage location for this repository, given either as a local file system path or a full URL making use of any of the available <a href="http://php.net/manual/en/book.stream.php" target="_blank">PHP stream wrappers</a>. Typically you would place this directory under the defalt Bitcache directory %path.', array('%path' => BITCACHE_ROOT)),
505 '#required' => FALSE,
506 );
507 drupal_add_js('if (Drupal.jsEnabled) { $(document).ready(function() { $("#edit-adapter").change(function() { $("#edit-file-wrapper").css("display", (this.value == "file" ? "block" : "none")); }); }); }', 'inline');
508 drupal_add_js('if (Drupal.jsEnabled) { $(document).ready(function() { $("#edit-file-location").keypress(function() { Drupal.settings.bitcache.locationChanged = true; }); }); }', 'inline');
509 }
510
511 // Repository storage (Amazon S3)
512 if (bitcache_has_adapter('aws_s3')) {
513 $form['storage']['aws_s3'] = array(
514 '#prefix' => '<div id="edit-aws_s3-wrapper" style="display: ' . ((!empty($edit->adapter) && $edit->adapter != 'aws_s3') ? 'none' : 'block') . ';">',
515 '#suffix' => '</div>',
516 '#tree' => TRUE,
517 );
518 $form['storage']['aws_s3']['bucket'] = array(
519 '#type' => 'textfield',
520 '#title' => t('Bucket name'),
521 '#default_value' => !empty($edit->bucket) ? $edit->bucket : '',
522 '#maxlength' => 255,
523 '#description' => t('<a href="https://s3.amazonaws.com/" target="_blank">Amazon S3</a> stores bitstreams in uniquely-named "buckets". Bucket names are used to form the URL that bitstreams can be accessed at, such as <code>https://BUCKET.s3.amazonaws.com/</code>. Buckets can be located either in the United States or in Europe, and access to buckets can be either public or private. If the specified bucket doesn\'t exist, it will be created when it is first accessed.</code>'),
524 '#required' => FALSE,
525 );
526 $form['storage']['aws_s3']['acl'] = array(
527 '#type' => 'select',
528 '#title' => t('Access control policy'),
529 '#default_value' => !empty($edit->bucket) ? $edit->bucket : 'public-read',
530 '#options' => array('public-read' => t('Owner read/write, public read'), 'public-read-write' => t('Owner read/write, public read/write'), 'private' => t('Owner read/write, public none')),
531 '#description' => t('This access control policy is applied to the bucket itself (if it does not exist yet), as well as to any new bitstreams stored into the bucket. To modify access policies for previously-existing buckets or bitstreams, use client software such as <a href="http://www.s3fox.net/" target="_blank">S3Fox</a>.'),
532 );
533 $form['storage']['aws_s3']['access_key'] = array(
534 '#type' => 'textfield',
535 '#title' => t('Access key'),
536 '#default_value' => !empty($edit->access_key) ? $edit->access_key : '',
537 '#maxlength' => 255,
538 '#description' => t(''), // TODO
539 '#required' => FALSE,
540 );
541 $form['storage']['aws_s3']['secret_key'] = array(
542 '#type' => 'textfield',
543 '#title' => t('Secret key'),
544 '#default_value' => !empty($edit->secret_key) ? $edit->secret_key : '',
545 '#maxlength' => 255,
546 '#description' => t(''), // TODO
547 '#required' => FALSE,
548 );
549 drupal_add_js('if (Drupal.jsEnabled) { $(document).ready(function() { $("#edit-adapter").change(function() { $("#edit-aws_s3-wrapper").css("display", (this.value == "aws_s3" ? "block" : "none")); }); }); }', 'inline');
550 }
551
552 // Repository storage > Content identification
553 $form['storage']['id'] = array('#type' => 'fieldset', '#title' => t('Content identification'), '#collapsible' => TRUE, '#collapsed' => !empty($edit->name) || TRUE); // FIXME
554 $form['storage']['id']['#description'] = t('<a href="http://en.wikipedia.org/wiki/Bitstream" target="_blank">Bitstreams</a> are uniquely identified and addressed by a <a href="http://bitcache.org/faq/digital-fingerprint" target="_blank">digital fingerprint</a>.');
555 $form['storage']['id']['fingerprint'] = array(
556 '#type' => 'select',
557 '#title' => t('Fingerprint algorithm'),
558 '#default_value' => !empty($edit->fingerprint) ? $edit->fingerprint : 'sha1',
559 '#options' => bitcache_get_algorithms('fingerprint'),
560 '#description' => t('Select the <a href="http://en.wikipedia.org/wiki/Cryptographic_hash_function" target="_blank">cryptographic hash algorithm</a> that will be used to compute digital fingerprints for bitstream contents.'),
561 );
562 $form['storage']['id']['encode'] = array(
563 '#type' => 'select',
564 '#title' => t('Fingerprint encoding'),
565 '#default_value' => !empty($edit->encode) ? $edit->encode : 'base16',
566 '#options' => bitcache_get_algorithms('encode'),
567 '#description' => t('Select the encoding method that bitstream fingerprints will be displayed in.'),
568 );
569
570 // Repository storage > Content storage
571 $form['storage']['content'] = array('#type' => 'fieldset', '#title' => t('Content storage'), '#collapsible' => TRUE, '#collapsed' => !empty($edit->name) || TRUE); // FIXME
572 $form['storage']['content']['#description'] = t('Bitstream contents can optionally be <a href="http://en.wikipedia.org/wiki/Data_compression" target="_blank">compressed</a> (optimizing the use of storage space at the cost of some performance) and/or <a href="http://en.wikipedia.org/wiki/Encryption" target="_blank">encrypted</a> (securing repository contents against compromised storage, again at a performance cost).');
573 $form['storage']['content']['compress'] = array(
574 '#type' => 'select',
575 '#title' => t('Compression algorithm'),
576 '#default_value' => !empty($edit->compress) ? $edit->compress : '',
577 '#options' => array_merge(array('' => t('<no compression>')), bitcache_get_algorithms('compress')),
578 '#description' => t('Select a compression algorithm that will be applied to bitstream contents before they are written to the repository.'),
579 );
580 $form['storage']['content']['encrypt'] = array(
581 '#type' => 'select',
582 '#default_value' => !empty($edit->encrypt) ? $edit->encrypt : '',
583 '#options' => array_merge(array('' => t('<no encryption>')), bitcache_get_algorithms('encrypt')),
584 '#title' => t('Encryption algorithm'),
585 '#description' => t('Select an encryption algorithm that will be applied to bitstream contents before they are written to the repository. Note that if you have also selected a compression algorithm, bitstream contents are always compressed first and only then encrypted.'),
586 );
587
588 if (!empty($edit->name) || TRUE) { // FIXME
589 foreach (element_children($form['storage']['id']) as $field) {
590 $form['storage']['id'][$field]['#attributes']['disabled'] = 'disabled';
591 }
592 foreach (element_children($form['storage']['content']) as $field) {
593 $form['storage']['content'][$field]['#attributes']['disabled'] = 'disabled';
594 }
595 }
596
597 // TODO: Policy settings
598 // Minimum bitstream size
599 // Maximum bitstream size
600
601 if ($edit->name != 'bitcache') {
602 $form['key'] = array('#type' => 'hidden', '#value' => $edit->name);
603 $form['submit'] = array('#type' => 'submit', '#value' => empty($edit->name) ? t('Create new repository') : t('Update repository settings'));
604 }
605 return $form;
606 }
607
608 function bitcache_admin_repo_form_validate($form, &$form_state) {
609 extract($form_state['values'], EXTR_SKIP | EXTR_REFS);
610
611 if (!preg_match('/^[a-z]+[a-z\d_]*$/', $name)) {
612 form_set_error('name', t('The machine-readable name can only consist of lowercase letters, underscores, and numbers.', array('%name' => $name)));
613 }
614
615 if (empty($key)) {
616 // Prevent duplicate names for repositories:
617 if (array_search($name, bitcache_get_repositories('names')) !== FALSE) {
618 form_set_error('name', t('The machine-readable name %name is already used by another repository.', array('%name' => $name)));
619 }
620
621 if ($adapter == 'file') {
622 // Prevent duplicate paths for repositories:
623
624 $file['location'] = rtrim(trim($file['location']), '/\\');
625
626 if (!trim($file['location'])) {
627 form_set_error('file][location', t('Location field is required.'));
628 }
629
630 foreach (bitcache_get_repositories('info') as $info) {
631 if ($info['adapter'] == $adapter && $info['location'] == $file['location']) {
632 form_set_error('file][location', t('The location %path is already used by another repository.', array('%path' => $file['location'])));
633 break;
634 }
635 }
636
637 // Ensure that the directory can be created:
638 if (!is_dir($location = bitcache_token_replace($file['location'])) && !@mkdir($location)) {
639 form_set_error('file][location', t('The directory %path could not be created.', array('%path' => $location)));
640 }
641 }
642 }
643 }
644
645 function bitcache_admin_repo_form_submit($form, &$form_state) {
646 extract($form_state['values'], EXTR_SKIP | EXTR_REFS);
647
648 $options = call_user_func_array('compact', array_merge(array('title', 'description', 'adapter', 'fingerprint', 'encode', 'compress', 'encrypt')));
649 $options = array_merge($options, isset($$adapter) ? $$adapter : array());
650
651 if (empty($key)) {
652 bitcache_create_repository($name, $options);
653 drupal_set_message(t('The repository has been created.'));
654 }
655 else {
656 if ($key != $name) {
657 bitcache_rename_repository($key, $name);
658 }
659 bitcache_update_repository($name, $options);
660 drupal_set_message(t('The repository settings have been updated.'));
661 }
662
663 $form_state['redirect'] = 'admin/settings/bitcache/repositories';
664 }
665
666 function bitcache_admin_repo_delete($form_state, $name) {
667 $form['name'] = array('#type' => 'value', '#value' => $name);
668 $output = confirm_form($form,
669 t('Are you sure you want to delete the repository %title?', array('%title' => $name)),
670 isset($_GET['destination']) ? $_GET['destination'] : 'admin/settings/bitcache/repositories',
671 t('This action will destroy all data contained in the repository and cannot be undone.'));
672 return $output;
673 }
674
675 function bitcache_admin_repo_delete_submit($form, &$form_state) {
676 if ($form_state['values']['confirm']) {
677 bitcache_delete_repository($form_state['values']['name']);
678 drupal_set_message(t('The repository has been deleted.'));
679
680 $form_state['redirect'] = 'admin/settings/bitcache/repositories';
681 }
682 }

  ViewVC Help
Powered by ViewVC 1.1.2