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

Contents of /contributions/modules/image_pub/image_pub.module

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


Revision 1.16 - (show annotations) (download) (as text)
Tue Dec 2 22:46:58 2008 UTC (11 months, 3 weeks ago) by egfrith
Branch: MAIN
CVS Tags: DRUPAL-6--1-0-BETA1, HEAD
Branch point for: DRUPAL-6--1
Changes since 1.15: +46 -45 lines
File MIME type: text/x-php
#270085 by wmmv and singularo: Port to Drupal 6
1 <?php
2 // $Id: image_pub.module,v 1.15 2008/11/19 14:39:58 egfrith Exp $
3
4 /*
5 * Image publishing support module
6 * Glue code for various 3rd party image publishing frontends,
7 * including:
8 * - Gallery Remote (http://gallery.menalto.com)
9 * - Windows Web Publishing Wizard
10 */
11
12 /**
13 * Implementation of hook_help().
14 */
15 function image_pub_help($path = 'admin/help#image_pub', $arg=null) {
16 switch ($path) {
17 case 'admin/help#image_pub':
18 return '<p>'. t('The Image Publishing module supports third party image upload and management applications.') .'</p>';
19 }
20 }
21
22 /**
23 * Implementation of hook_menu().
24 */
25 function image_pub_menu() {
26 $items = array();
27 $items['gallery_remote2.php'] = array(
28 'page callback' => '_image_pub_gr_request',
29 'access callback' => TRUE,
30 'type' => MENU_CALLBACK);
31 $items['publish_xp'] = array(
32 'page callback' => '_image_pub_xp_request',
33 'access callback' => TRUE,
34 'type' => MENU_CALLBACK);
35 $items['admin/settings/image_pub_xp_reghack'] = array(
36 'page callback' => '_image_pub_xp_reghack',
37 'access callback' => TRUE,
38 'type' => MENU_CALLBACK);
39 $items['admin/settings/image_pub'] = array(
40 'title' => 'Image publishing',
41 'description' => 'Provides information regarding remote image publishing.',
42 'page callback' => '_image_pub_admin',
43 'access arguments' => array('create images'));
44 return $items;
45 }
46
47 function _image_pub_admin() {
48 $info = '<p>'. t('The Image Publishing module supports 3rd party image upload and management applications.') .'</p>';
49 $info .= '<h3>'. t('Gallery Remote') .'</h3>';
50 $info .= '<p>'. t('To use a Gallery Remote client, configure the client with the URL of your Drupal root directory. It will attempt to access the gallery_remote2.php entry point, for which this module registers a virtual handler.') .'</p>';
51 $info .= '<p>'. t('This module has been tested successfully with the <a href="@gallery-remote" target="_blank">Java Gallery Remote</a> clients, and <a href="@digikam" target="_blank">digiKam</a>.', array('@gallery-remote' => 'http://gallery.menalto.com/wiki/Gallery_Remote', '@digikam' => 'http://www.digikam.org')) .'</p>';
52 $info .= '<h3>'. t('Windows Web Publishing Wizard') .'</h3>';
53 $info .= '<p>'. t('Windows XP and on have an interesting built-in shell feature called the Publishing Wizard. The sidebar task "Publish this file/folder to the Web" invokes the Web Publishing Wizard. When this task is invoked, the Web Publishing Wizard offers a list of sites to which your images can be published. The list comes from the registry, and with this module, you can add Drupal sites to that list.') .'</p>';
54 $info .= '<p>'. t('The below link will send a registry editor file to your computer. Merging this file will add a new option to the list of sites to which Web content can be published, using the <em>Publish this file/folder to the Web</em> option in Explorer. If you ever wish to remove this site from your system\'s list of publishing sites, you must manually edit your system registry.') .'</p>';
55 $info .= '<p>'. t('<a href="@image_pub_xp_reghack">Install Windows Web Publishing Wizard Support</a>', array('@image_pub_xp_reghack' => url('admin/settings/image_pub_xp_reghack'))) .'</p>';
56 return $info;
57 }
58
59 /*
60 * Generalized helper functions for tasks that require tight integration
61 * with other parts of Drupal, such as image.module and taxonomy.module.
62 * _image_pub_get_vid()
63 * _image_pub_base_url()
64 * _image_pub_image_baseurl()
65 * _image_pub_get_imagefilename()
66 * _image_pub_get_imageinfo()
67 * _image_pub_album_get()
68 * _image_pub_album_enum()
69 * _image_pub_album_selector()
70 * _image_pub_album_access()
71 * _image_pub_album_images()
72 *
73 * _image_pub_authenticate()
74 * _image_pub_album_add()
75 * _image_pub_image_add()
76 */
77 function _image_pub_get_vid() {
78 return _image_gallery_get_vid();
79 }
80 function _image_pub_base_url() {
81 global $base_url;
82 return $base_url;
83 }
84 function _image_pub_image_baseurl() {
85 return _image_pub_base_url() .'/'. variable_get('file_directory_path', 'files') .'/'. variable_get('image_default_path', 'images');
86 }
87 function _image_pub_get_imagefilename($node, $size = '_original') {
88 $fname = $node->images[$size];
89 if (isset($fname)) {
90 $imgbase = variable_get('image_default_path', 'images') .'/';
91 if (!strncmp($fname, $imgbase, strlen($imgbase))) {
92 $fname = substr($fname, strlen($imgbase));
93 }
94 }
95 return $fname;
96 }
97 function _image_pub_get_imageinfo($node, $size = '_original') {
98 $file = file_create_path($node->images[$size]);
99 $info = image_get_info($file);
100 $info['filesize'] = filesize($file);
101 return $info;
102 }
103 function _image_pub_album_get($albumid) {
104
105 $term = taxonomy_get_term($albumid);
106 if (!$term) {
107 if ($albumid == 0) {
108 $term = taxonomy_get_term('<root>');
109 }
110 else {
111 $term = NULL;
112 }
113 }
114 return $term;
115 }
116 function _image_pub_album_enum($albumid = 0, $subalbums = TRUE) {
117 return taxonomy_get_tree(_image_pub_get_vid(), $albumid, -1,
118 ($subalbums ? NULL : 1));
119 }
120 function _image_pub_album_selector($fname, $showroot = FALSE, $selalbum = NULL) {
121 /* This is mostly redundant with taxonomy_form(). */
122 $body = array();
123 $tree = _image_pub_album_enum();
124 $options = array();
125 $albumid = 0;
126 if (isset($selalbum)) {
127 $albumid = $selalbum->tid;
128 }
129 if ($showroot) {
130 $options[0] = '&lt;Root Album&gt;';
131 }
132 if ($tree) {
133 foreach ($tree as $term) {
134 if (!$showroot && ($albumid == 0)) {
135 $albumid = $term->tid;
136 }
137 $dashes = '';
138 for ($i = 0; $i < $term->depth; $i++) {
139 $dashes .= '-';
140 }
141 $options[$term->tid] = $dashes . $term->name;
142 }
143 }
144 $body[] = '<select name="' . $fname . '">';
145 foreach ($options as $aid => $term) {
146 $sel = '';
147 if ($aid == $albumid) {
148 $sel = ' selected="selected"';
149 }
150 $body[] = '<option value="' .$aid . '"' . $sel . '>' . $term . '</option>';
151 }
152 $body[] = '</select>';
153 return implode("\r\n", $body);
154 }
155
156 function _image_pub_album_access($type, $album) {
157 switch ($type) {
158 case 'view':
159 return user_access('access content');
160 case 'update':
161 return user_access('administer images');
162 case 'create':
163 return user_access('create images');
164 }
165 return FALSE;
166 }
167 function _image_pub_album_images($tid) {
168 $nodes = array();
169 $sql = "SELECT n.nid FROM {node} n INNER JOIN {term_node} t ON t.nid = n.nid WHERE t.tid = $tid AND n.type = 'image';";
170 $res = db_query($sql);
171 while ($term = db_fetch_object($res)) {
172 $nodes[] = node_load(array('nid' => $term->nid));
173 }
174 return $nodes;
175 }
176 function _image_pub_authenticate($uname, $pass) {
177 global $user;
178 if ($user->uid) {
179 watchdog('image_pub', 'Session closed for %user', array('%user' => theme('placeholder', $user->name)));
180 unset($user);
181 }
182 $user = user_authenticate(array('name' => $uname, 'pass' => $pass));
183 if (!$user->uid) {
184 return FALSE;
185 }
186 watchdog('image_pub', 'Session opened for %user', array('%user' => theme('placeholder', $user->name)));
187 return TRUE;
188 }
189 function _image_pub_album_add($title, $descr, $palbum) {
190 watchdog('image_pub', 'Album added with title %title', array('%title' => $title));
191
192 if (empty($title)) {
193 $title = 'Untitled album';
194 }
195 $edit = array(
196 'name' => $title,
197 'description' => $descr,
198 'vid' => _image_pub_get_vid(),
199 'parent' => array(isset($palbum) ? $palbum->tid : 0),
200 'weight' => 0,
201 );
202 $term = taxonomy_save_term($edit);
203 if (isset($term)) {
204 // taxonomy_save_term amends $edit directly, and simply returns a status
205 // return (object)$edit;
206 return (object)$edit;
207 }
208 return NULL;
209 }
210 function _image_pub_image_add($album, $caption, $description, $srcfield) {
211 global $user;
212 if (!isset($_FILES[$srcfield])) {
213 return array('success' => FALSE,
214 'reason' => 'Forgetting a file?');
215 }
216
217 if (empty($caption)) {
218 $caption = $_POST['force_filename'];
219 if (empty($caption)) {
220 $caption = basename($_FILES[$srcfield]['name']);
221 if (empty($caption)) {
222 $caption = 'Untitled image';
223 }
224 }
225 }
226
227 // Create a drupal node
228 // In order for the image upload to work, the name, tmp_name and error
229 // of the $_FILES[$srcfield] have to be copied to locations
230 // that file_check_upload() recognises.
231 // The size needs to go into $_FILES['files']['size']['image'] for image
232 // module's size checking to occur.
233 $_FILES['files']['name']['image'] = $_FILES[$srcfield]['name'];
234 $_FILES['files']['tmp_name']['image'] = $_FILES[$srcfield]['tmp_name'];
235 $_FILES['files']['error']['image'] = $_FILES[$srcfield]['error'];
236 $_FILES['files']['size']['image'] = filesize($_FILES[$srcfield]['tmp_name']);
237
238 $form_state = array();
239 module_load_include('inc', 'node', 'node.pages');
240 $node = array('type' => 'image');
241 $form_state['values']['title'] = $caption;
242 $form_state['values']['uid'] = $user->uid;
243 $form_state['values']['name'] = $user->name;
244 $form_state['values']['body_field'] = $description;
245 $form_state['values']['taxonomy'][_image_pub_get_vid()] = $album->tid;
246 $form_state['values']['op'] = t('Save');
247 $redirect_url = drupal_execute('image_node_form', $form_state, (object)$node);
248
249 // Check that the uploaded image was accepted.
250 $errors = form_get_errors();
251 if (!empty($errors)) {
252 return array('success' => FALSE,
253 'reason' => strip_tags(implode($errors, ' ')));
254 }
255
256 watchdog('image_pub', '%image was added.', array('%image' => $_FILES[$srcfield]['name']));
257 return array('success' => TRUE, 'redirect_url' => $redirect_url);
258 }
259
260
261 /*
262 * Definitions for handling the gallery remote protocol
263 * See http://gallery.menalto.com/ for more info.
264 */
265
266 define('GR_STAT_SUCCESS', 0);
267 define('GR_STAT_PROTO_MAJ_VER_INVAL', 101);
268 define('GR_STAT_PROTO_MIN_VER_INVAL', 102);
269 define('GR_STAT_PROTO_VER_FMT_INVAL', 103);
270 define('GR_STAT_PROTO_VER_MISSING', 104);
271 define('GR_STAT_PASSWD_WRONG', 201);
272 define('GR_STAT_LOGIN_MISSING', 202);
273 define('GR_STAT_UNKNOWN_CMD', 301);
274 define('GR_STAT_NO_ADD_PERMISSION', 401);
275 define('GR_STAT_NO_FILENAME', 402);
276 define('GR_STAT_UPLOAD_PHOTO_FAIL', 403);
277 define('GR_STAT_NO_WRITE_PERMISSION', 404);
278 define('GR_STAT_NO_CREATE_ALBUM_PERMISSION', 501);
279 define('GR_STAT_CREATE_ALBUM_FAILED', 502);
280
281 define('GR_SERVER_VERSION', '2.15');
282
283 function _image_pub_gr_get_albumname($term) {
284 return 'Album' . $term->tid;
285 }
286 function _image_pub_gr_get_albumid($albname) {
287 if (!strncmp($albname, 'Album', 5)) {
288 return substr($albname, 5);
289 }
290 return 0;
291 }
292
293
294 /*
295 * Protocol request handler entry points below
296 * _image_pub_gr_request() (main entry point)
297 * _image_pub_gr_finish() (completion helper)
298 * _image_pub_gr_login()
299 * _image_pub_gr_fetch_albums()
300 * _image_pub_gr_fetch_album_images()
301 * _image_pub_gr_add_album()
302 * _image_pub_gr_move_album()
303 * _image_pub_gr_add_image()
304 */
305
306 function _image_pub_gr_request() {
307 if (isset($_POST['cmd'])) {
308 $cmd = $_POST['cmd'];
309 }
310 else {
311 $cmd = $_GET['cmd'];
312 }
313
314 $numref = FALSE;
315
316 // watchdog('image_pub', 'Processing command %cmd', array('%cmd' => $cmd));
317
318 switch ($cmd) {
319 case 'login':
320 _image_pub_gr_login($_POST['uname'], $_POST['password']);
321 break;
322
323 case 'fetch-albums':
324 $numref = TRUE;
325 case 'fetch-albums-prune':
326 $check_writeable = ($_POST['check-writeable'] == 'yes') ? TRUE : FALSE;
327 _image_pub_gr_fetch_albums($numref, $check_writeable);
328 break;
329
330 case 'fetch-album-images':
331 $albums_too = ($_POST['albums_too'] == 'yes') ? TRUE : FALSE;
332 _image_pub_gr_fetch_album_images($_POST['set_albumName'],
333 $albums_too);
334 break;
335
336 case 'new-album':
337 _image_pub_gr_add_album($_POST['set_albumName'],
338 $_POST['newAlbumTitle'],
339 $_POST['newAlbumDesc']);
340 break;
341
342 case 'move-album':
343 _image_pub_gr_move_album($_POST['set_albumName'],
344 $_POST['set_destalbumName']);
345 break;
346
347 case 'add-item':
348 _image_pub_gr_add_image($_POST['set_albumName'],
349 $_POST['caption'],
350 $_POST['extrafield_Description']);
351 break;
352
353 case '':
354 echo 'For more information about Gallery Remote, see Gallery\'s website located at <a href="http://gallery.sourceforge.net">http://gallery.sourceforge.net</a>';
355 exit;
356
357 default:
358 _image_pub_gr_finish(GR_STAT_UNKNOWN_CMD, '', t('Unknown command "%cmd"', array('%cmd' => theme('placeholder', $cmd))));
359 break;
360 }
361 }
362
363
364 function _image_pub_gr_finish($code, $body = NULL, $message = NULL) {
365 static $gr_messages;
366 if (!isset($gr_messages)) {
367 $gr_messages = array(
368 GR_STAT_SUCCESS => t('Successful'),
369 GR_STAT_PROTO_MAJ_VER_INVAL => t('The protocol major version the client is using is not supported.'),
370 GR_STAT_PROTO_MIN_VER_INVAL => t('The protocol minor version the client is using is not supported.'),
371 GR_STAT_PROTO_VER_FMT_INVAL => t('The format of the protocol version string the client sent in the request is invalid.'),
372 GR_STAT_PROTO_VER_MISSING => t('The request did not contain the required protocol_version key.'),
373 GR_STAT_PASSWD_WRONG => t('The password and/or username the client send in the request is invalid.'),
374 GR_STAT_LOGIN_MISSING => t('The client used the login command in the request but failed to include either the username or password (or both) in the request.'),
375 GR_STAT_UNKNOWN_CMD => t('The value of the cmd key is not valid.'),
376 GR_STAT_NO_ADD_PERMISSION => t('The user does not have permission to add an item to the gallery.'),
377 GR_STAT_NO_FILENAME => t('No filename was specified.'),
378 GR_STAT_UPLOAD_PHOTO_FAIL => t('The file was received, but could not be processed or added to the album.'),
379 GR_STAT_NO_WRITE_PERMISSION => t('No write permission to destination album.'),
380 GR_STAT_NO_CREATE_ALBUM_PERMISSION => t('A new album could not be created because the user does not have permission to do so.'),
381 GR_STAT_CREATE_ALBUM_FAILED => t('A new album could not be created, for a different reason (name conflict).'),
382 );
383 }
384 if (!isset($message)) {
385 $message = $gr_messages[$code];
386 if (!isset($message)) {
387 $message = 'Undefined error code';
388 }
389 }
390 if ($code != GR_STAT_SUCCESS) {
391 watchdog('image_pub', 'Request failure: %code, %msg', array('%code' => $code, '%msg' => $message));
392 }
393 else {
394 //watchdog('image_pub', 'Command succeeded: %msg', array('%msg' => theme('placeholder', $message)));
395 }
396 drupal_set_header("Content-Type: text/plain");
397 echo "#__GR2PROTO__\n" .
398 $body .
399 "status=$code\n" .
400 "status_text=$message\n";
401 exit;
402 }
403
404
405 function _image_pub_gr_login($uname, $pass) {
406 global $user;
407 $body = 'server_version=' . GR_SERVER_VERSION . "\n";
408 if (!$uname || !$pass) {
409 if (!$uname && !$pass) {
410 // Not trying to log in, just asking for a version number.
411 _image_pub_gr_finish(GR_STAT_SUCCESS, $body);
412 }
413 _image_pub_gr_finish(GR_STAT_LOGIN_MISSING);
414 }
415 if (!_image_pub_authenticate($uname, $pass)) {
416 _image_pub_gr_finish(GR_STAT_PASSWD_WRONG);
417 }
418 else {
419 _image_pub_gr_finish(GR_STAT_SUCCESS, $body);
420 }
421 }
422
423
424 function _image_pub_gr_fetch_albums($refnum, $check_writeable) {
425 $body = '';
426 $thumb_dims = image_get_sizes(IMAGE_THUMBNAIL);
427 $thumb_max = min($thumb_dims['width'], $thumb_dims['height']);
428 $preview_dims = image_get_sizes(IMAGE_PREVIEW);
429 $preview_max = min($preview_dims['width'], $preview_dims['height']);
430 $nalbums = 0;
431 $cperm = 'false';
432 $aperm = 'false';
433 $crperm = 'no';
434 if (user_access('create images')) {
435 $cperm = 'true';
436 }
437 if (user_access('administer images')) {
438 $aperm = 'true';
439 $crperm = 'yes';
440 }
441 $tree = _image_pub_album_enum();
442 $aid = 0;
443 $aidref = array();
444 foreach ($tree as $term) {
445 if (_image_pub_album_access('view', $term)) {
446 $aid++;
447 $aname = _image_pub_gr_get_albumname($term);
448 $aidref[$aname] = $aid;
449
450 $pname = 0;
451 if (!empty($term->parents[0])) {
452 $pterm = _image_pub_album_get($term->parents[0]);
453 if ($refnum) {
454 $pname = $aidref[_image_pub_gr_get_albumname($pterm)]; // Guaranteed to exist?
455 }
456 else {
457 $pname = _image_pub_gr_get_albumname($pterm);
458 }
459 }
460
461 $body .= 'album.name.' . $aid . '=' . $aname . "\n";
462 $body .= 'album.title.' . $aid . '=' . $term->name . "\n";
463 $body .= 'album.summary.' . $aid . '=' . $term->description . "\n";
464 $body .= 'album.parent.' . $aid . '=' . $pname . "\n";
465 $body .= 'album.resize_size.' . $aid . '=' . $preview_max . "\n";
466 $body .= 'album.max_size.' . $aid . '=' . '0' . "\n";
467 $body .= 'album.thumb_size.' . $aid . '=' . $thumb_max . "\n";
468 $body .= 'album.perms.add.' . $aid . '=' . $cperm . "\n";
469 $body .= 'album.perms.write.' . $aid . '=' . $aperm . "\n";
470 $body .= 'album.perms.del_item.' . $aid . '=' . $aperm . "\n";
471 $body .= 'album.perms.del_alb.' . $aid . '=' . $aperm . "\n";
472 $body .= 'album.perms.create_sub.' . $aid . '=' . $aperm . "\n";
473 $body .= 'album.extrafields.' . $aid . "=Description\n";
474 $nalbums++;
475 }
476 }
477 $body .= 'album_count=' . $nalbums . "\n";
478 $body .= 'can_create_root=' . $crperm . "\n";
479 _image_pub_gr_finish(GR_STAT_SUCCESS, $body);
480 }
481
482
483 function _image_pub_gr_fetch_album_images($albname, $albumstoo) {
484 $body = '';
485 $album = _image_pub_album_get(_image_pub_gr_get_albumid($albname));
486 if (!isset($album) || !_image_pub_album_access('view', $album)) {
487 _image_pub_gr_finish(GR_STAT_NO_FILENAME, $body, 'No such album');
488 }
489
490 if ($albumstoo) {
491 $tree = _image_pub_album_enum($album->tid, FALSE);
492 foreach ($tree as $term) {
493 if (_image_pub_album_access('view', $term)) {
494 $body .= 'album.name.' . $term->tid . '=' . _image_pub_gr_get_albumname($term) . "\n";
495 }
496 }
497 $numimages = taxonomy_term_count_nodes($album->tid, 'image');
498
499 }
500 else {
501 $numimages = 0;
502 $nodes = _image_pub_album_images($album->tid);
503 $iid = 0;
504 foreach ($nodes as $node) {
505 if (node_access('view', $node)) {
506 $iid++;
507 $body .= 'image.name.' . $iid . '=' . _image_pub_get_imagefilename($node) . "\n";
508 $info = _image_pub_get_imageinfo($node);
509 $body .= 'image.raw_width.' . $iid . '=' . $info['width'] . "\n";
510 $body .= 'image.raw_height.'. $iid . '=' . $info['height'] . "\n";
511 $body .= 'image.raw_filesize.' . $iid . '=' . $info['filesize'] . "\n";
512
513 $body .= 'image.resizedName.' . $iid . '=' . _image_pub_get_imagefilename($node, IMAGE_PREVIEW) . "\n";
514 $info = _image_pub_get_imageinfo($node, IMAGE_PREVIEW);
515 $body .= 'image.resized_width.' . $iid . '=' . $info['width'] . "\n";
516 $body .= 'image.resized_height.' . $iid . '=' . $info['height'] . "\n";
517 //$body .= 'image.resized_filesize.'.$iid.'='.$info['filesize'] . "\n";
518
519 $body .= 'image.thumbName.' . $iid . '=' . _image_pub_get_imagefilename($node, IMAGE_THUMBNAIL) . "\n";
520 $info = _image_pub_get_imageinfo($node, IMAGE_THUMBNAIL);
521 $body .= 'image.thumb_width.' . $iid . '=' . $info['width'] . "\n";
522 $body .= 'image.thumb_height.' . $iid . '=' . $info['height'] . "\n";
523 //$body .= 'image.thumb_filesize.' . $iid . '=' . $info['filesize'] . "\n";
524
525 $body .= 'image.caption.' . $iid . '=' . $node->title . "\n";
526 $body .= 'image.clicks.' . $iid . '=' . '0' . "\n";
527 $body .= 'image.extrafield.Description.' . $iid . '=' .$node->teaser . "\n";
528
529 $ctime = $node->created;
530 $body .= 'image.capturedate.year.' . $iid . '=' . date('Y', $ctime) . "\n";
531 $body .= 'image.capturedate.mon.' . $iid . '=' . date('n', $ctime) . "\n";
532 $body .= 'image.capturedate.mday.' . $iid . '=' . date('j', $ctime) . "\n";
533 $body .= 'image.capturedate.hours.' . $iid . '=' . date('H', $ctime) . "\n";
534 $body .= 'image.capturedate.minutes.' . $iid . '=' . date('i', $ctime) . "\n";
535 $body .= 'image.capturedate.seconds.' . $iid . '=' . date('s', $ctime) . "\n";
536 $body .= 'image.hidden.' . $iid . '=' . ($node->status != 1 ? 'yes' : 'no') . "\n";
537 $numimages++;
538 }
539 }
540 }
541
542 $body .= 'image_count=' . $numimages . "\n";
543 $body .= 'baseurl=' . _image_pub_image_baseurl() . "/\n";
544
545 _image_pub_gr_finish(GR_STAT_SUCCESS, $body);
546 }
547
548
549 function _image_pub_gr_add_album($parentaname, $title, $descr) {
550 $body = '';
551
552 $palbum = _image_pub_album_get(_image_pub_gr_get_albumid($parentaname));
553 if (!isset($palbum)) {
554 _image_pub_gr_finish(GR_STAT_CREATE_ALBUM_FAILED, $body, t('No such parent album: %parent', array('%parent' => $parentaname)));
555 }
556 if (!_image_pub_album_access('update', $palbum)) {
557 _image_pub_gr_finish(GR_STAT_NO_CREATE_ALBUM_PERMISSION, $body);
558 }
559
560 $term =_image_pub_album_add($title, $descr, $palbum);
561 if (!isset($term)) {
562 _image_pub_gr_finish(GR_STAT_CREATE_ALBUM_FAILED);
563 }
564 else {
565 $body .= 'album_name=' . _image_pub_gr_get_albumname($term) . "\n";
566 _image_pub_gr_finish(GR_STAT_SUCCESS, $body, 'Album created');
567 }
568 }
569
570
571 function _image_pub_gr_move_album($albname, $destaname) {
572 $body = '';
573
574 $album = _image_pub_album_get(_image_pub_gr_get_albumid($albname));
575 if (!isset($album)) {
576 _image_pub_gr_finish(GR_STAT_CREATE_ALBUM_FAILED, $body, 'No such album');
577 }
578
579 if (!_image_pub_album_access('update', $album)) {
580 _image_pub_gr_finish(GR_STAT_NO_WRITE_PERMISSION, $body);
581 }
582
583 if (!isset($destaname) || ($destaname == '0') || ($destaname == 'rootalbum')) {
584 $dtid = 0;
585 }
586 else {
587 $dalbum = _image_pub_album_get(_image_pub_gr_get_albumid($destaname));
588 if (!isset($dalbum) || !_image_pub_album_access('update', $dalbum)) {
589 _image_pub_gr_finish(GR_STAT_CREATE_ALBUM_FAILED, $body, 'Invalid destination album '.$destaname);
590 }
591 $dtid = $dalbum->tid;
592 }
593
594 $album->parent = array($dtid);
595 $term = (array)$album;
596 $status = taxonomy_save_term($term);
597
598 _image_pub_gr_finish(GR_STAT_SUCCESS, $body, 'Album reparented');
599 }
600
601
602 function _image_pub_gr_add_image($albname, $caption, $description) {
603 $body = '';
604 $album = _image_pub_album_get(_image_pub_gr_get_albumid($albname));
605 if (!isset($album)) {
606 _image_pub_gr_finish(GR_STAT_UPLOAD_PHOTO_FAIL, 'No such album');
607 }
608 if (!_image_pub_album_access('create', $album)) {
609 _image_pub_gr_finish(GR_STAT_NO_ADD_PERMISSION);
610 }
611
612 $result = _image_pub_image_add($album, $caption, $description, 'userfile');
613
614 if (!$result['success']) {
615 _image_pub_gr_finish(GR_STAT_UPLOAD_PHOTO_FAIL, $body, t('The file was received, but could not be processed or added to the album.') . ' ' . $result['reason']);
616 }
617 else {
618 _image_pub_gr_finish(GR_STAT_SUCCESS, $body, 'Added node '.$result['node']->nid);
619 }
620 }
621
622
623 /*
624 * Windows Web publishing wizard support
625 *
626 * Below is support for the Windows XP Web publishing wizard. It's just a
627 * glorified file picker and MSIE instance in a wizard form factor, but
628 * it gets the job done, and did we mention it comes with every copy of
629 * Windows?
630 *
631 * Microsoft even appears to have some sort of documentation for the
632 * publishing wizard on the MSDN site, although debugging is a major
633 * pain:
634 *
635 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/shell/programmersguide/shell_basics/shell_basics_extending/publishing_wizard/pubwiz.asp
636 *
637 * Our part of the wizard has three steps:
638 * 1. Authenticate.
639 * 2. Select or create album.
640 * 3. Publish. We send a blank page with a magic script to configure
641 * the wizard.
642 *
643 * We send bare HTML and don't use themes. Why not themes?
644 * -> The XP publishing wizard has a microscopic embedded MSIE instance
645 * that we don't want to overflow with content. That is hard to
646 * guarantee using an arbitrary theme.
647 * -> Themes will display distracting links in headers and sidebars. We
648 * don't want the user navigating to one of the distractions.
649 */
650
651
652 /*
653 * Concoct a magic registry file to add this site to the list of
654 * usable destinations for the publishing wizard.
655 */
656 function _image_pub_xp_reghack() {
657 $pub_url = url('publish_xp', array('absolute' => TRUE));
658 $site_name = variable_get('site_name', 'drupal');
659 $site_descr = sprintf(t("Publish Your Photos to %s."), $site_name);
660 if (!strncmp($pub_url, 'https:', 6)) {
661 $site_descr .= ' (Secure)';
662 }
663 else {
664 $site_descr .= ' (Insecure)';
665 }
666 drupal_set_header("Cache-control: private");
667 drupal_set_header("Content-Type: application/octet-stream");
668 drupal_set_header("Content-Disposition: filename=install_registry.reg");
669
670 $lines[] = 'Windows Registry Editor Version 5.00';
671 $lines[] = '';
672 $lines[] = '[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\PublishingWizard\PublishingWizard\Providers\\' . $site_name . ']';
673 $lines[] = '"displayname"="' . $site_name . '"';
674 $lines[] = '"description"="' . $site_descr . '"';
675 $lines[] = '"href"="' . $pub_url . '"';
676 $lines[] = '"icon"="' . _image_pub_base_url() . '/favicon.ico"';
677 $lines[] = '';
678 echo join("\r\n", $lines);
679 exit;
680 }
681
682
683 /*
684 * Implementation of wizard forms and such:
685 * _image_pub_xp_request() (main entry point)
686 * _image_pub_xp_login()
687 * _image_pub_xp_album()
688 * _image_pub_xp_addimage()
689 * _image_pub_xp_page() (HTML form template)
690 * _image_pub_xp_login_form()
691 * _image_pub_xp_album_form()
692 * _image_pub_xp_addimage_script()
693 */
694
695 function _image_pub_xp_request() {
696
697 /*
698 * We require the user to be logged in to publish.
699 * We honor cookies saved from other MSIE sessions, and don't
700 * require the user to log in if they already have a session.
701 */
702
703 global $user;
704 $cmd = $_REQUEST['cmd'];
705 if (!$user->uid) {
706 if (!isset($cmd)) {
707 $cmd = 'login';
708 }
709 }
710
711 switch ($cmd) {
712 case 'login':
713 _image_pub_xp_login($_REQUEST['uname'], $_REQUEST['password']);
714 break;
715 case 'addimage':
716 _image_pub_xp_addimage($_REQUEST['albumid']);
717 default:
718 _image_pub_xp_album($_REQUEST['edit']);
719 break;
720 }
721 }
722
723
724 function _image_pub_xp_login($uname, $pass) {
725 if (empty($uname) && empty($pass)) {
726 _image_pub_xp_login_form();
727 }
728 elseif (!_image_pub_authenticate($uname, $pass)) {
729 drupal_set_message('Login failed. Please try again.', 'error');
730 _image_pub_xp_login_form("Invalid Login", $uname);
731 }
732 else {
733 _image_pub_xp_album_form();
734 }
735 }
736
737
738 function _image_pub_xp_album($edit) {
739 if (!isset($edit)) {
740 _image_pub_xp_album_form();
741 return;
742 }
743
744 $create_new = $edit['create_new'];
745
746 if ($create_new) {
747 $albumid = $edit['create_parentid'];
748 $create_name = $edit['create_name'];
749 $create_descr = $edit['create_descr'];
750
751 $album = _image_pub_album_get($albumid);
752 if (!_image_pub_album_access('update', $album)) {
753 drupal_set_message('Created album permission denied.', 'error');
754 _image_pub_xp_album_form('Create Album Permission Denied', $album);
755 }
756
757 $newalbum = _image_pub_album_add($create_name, $create_descr, $album);
758
759 if (isset($newalbum)) {
760 drupal_set_message('New album '.'<em>'.$newalbum->name.'</em>'.' created.');
761 _image_pub_xp_album_form('Created Album "'.$newalbum->name.'"', $newalbum);
762 }
763 else {
764 drupal_set_message('Error creating album '.'<em>'.$newalbum->name.'</em>.', 'error');
765 _image_pub_xp_album_form('Error Creating Album', $album);
766 }
767 return;
768 }
769
770 // if we got here we are choosing an existing album
771 $albumid = $edit['albumid'];
772 $album = _image_pub_album_get($albumid);
773
774 if (!isset($album)) {
775 drupal_set_mssage('Invalid album specified.', 'error');
776 _image_pub_xp_album_form('Invalid album specified');
777 return;
778 }
779 if (!_image_pub_album_access('create', $album)) {
780 drupal_set_message('Create image permission denied.', 'error');
781 _image_pub_xp_album_form('Create Image Permission Denied', $album);
782 return;
783 }
784 _image_pub_xp_addimage_script($album);
785 }
786
787
788 function _image_pub_xp_addimage($albumid) {
789 $album = _image_pub_album_get($albumid);
790 if (!isset($album)) {
791 _image_pub_xp_album_form('Invalid album specified');
792 return;
793 }
794 if (!_image_pub_album_access('update', $album)) {
795 _image_pub_xp_album_form('Create Image Permission Denied', $album);
796 return;
797 }
798
799 $result = _image_pub_image_add($album, NULL, NULL, 'userfile');
800 }
801
802
803 function _image_pub_xp_page($body, $header, $next, $back = NULL) {
804 global $language;
805 $site_name = variable_get('site_name', 'drupal');
806 ?>
807 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
808 <html xmlns="http://www.w3.org/1999/xhtml" lang="<?php print $language->language; ?>" xml:lang="<?php print $language->language; ?>">
809 <head>
810 </head>
811 <body>
812 <?php echo $body; ?>
813 <script>
814 function OnBack() {
815 <?php if (isset($back)) { ?>
816 window.location.href = "<?php echo $back; ?>";
817 <?php }
818 else { ?>
819 window.external.FinalBack();
820 <?php } ?>
821 window.external.SetWizardButtons(true,true,false);
822 }
823 function OnNext() {
824 <?php echo $next; ?>.submit();
825 }
826 function window.onload() {
827 window.external.SetHeaderText("<?php echo $site_name; ?>", "<?php echo $header; ?>");
828 window.external.SetWizardButtons(true,true,false);
829 }
830 </script>
831 </body>
832 </html>
833 <?php
834 exit;
835 }
836
837
838 function _image_pub_xp_login_form($message = NULL, $uname = NULL) {
839
840 if (!isset($uname)) {
841 $uname = '';
842 }
843
844 // Build a form array
845 $form = array();
846
847 $form['login'] = array(
848 '#type' => 'fieldset',
849 '#tree' => FALSE,
850 );
851
852 $form['login']['uname'] = array(
853 '#type' => 'textfield',
854 '#title' => t('User name'),
855 '#default_value' => check_plain($uname),
856 '#description' => t('Enter your user name.'),
857 );
858
859 $form['login']['password'] = array(
860 '#type' => 'password',
861 '#title' => t('Password'),
862 '#description' => t('Enter your password.'),
863 );
864
865 $form['cmd'] = array(
866 '#type' => 'hidden',
867 '#value' => 'login',
868 );
869
870 $form['lcid'] = array(
871 '#type' => 'hidden',
872 '#value' => check_plain($_REQUEST['lcid']),
873 );
874
875 $form['langid'] = array(
876 '#type' => 'hidden',
877 '#value' => check_plain($_REQUEST['langid']),
878 );
879
880 $form[session_name()] = array(
881 '#type' => 'hidden',
882 '#value' => check_plain(session_id()),
883 );
884
885 // Build the HTML output
886 $body = array();
887
888 // Title
889 $body[] = '<h1>Log in</h1>';
890
891 // Display error message if one is active
892 $body[] = theme('status_messages');
893
894 // Render the form elements to themed HTML
895 $body[] = '<form method="post" id="login">';
896 $body[] = drupal_render(form_builder('login', $form));
897 $body[] = '</form>';
898 $body[] = '';
899
900 // Output the wizard page
901 _image_pub_xp_page(implode("\r\n", $body), 'Upload photos...', 'login');
902
903 }
904
905
906 function _image_pub_xp_album_form($message = NULL, $select_album = NULL) {
907
908 /**
909 * This form is currently not very elegant in how it is build
910 * as it has to over-ride #parents to be compatible with the rest
911 * of the existing code. Field sets are used to present the data
912 * in logical blocks. It may be better in the longer term to make an
913 * additional form page - decide it you want an existing or a new album
914 * and then take the user there, rather than doing it all in one.
915 * Leave as is for now just to check the concept is working
916 */
917
918 // Build a form array
919 $form = array();
920 $form_state = array();
921
922 // create a fieldset to wrap the form in
923 // note #parents must be forced for this set to work with original code
924 $form['existing'] = array(
925 '#type' => 'fieldset',
926 '#tree' => FALSE,
927 );
928
929 // radio button for existing album
930 $form['existing']['create_new'] = array(
931 '#type' => 'radio',
932 '#return_value' => 0,
933 '#title' => t('Use an existing album'),
934 '#attributes' => array('checked' => 'checked'),
935 '#parents' => array('edit', 'create_new'),
936 );
937
938 // for some reason if a new album it doesn't always end up in the select list
939 // I tried using cache_clear_all but that doesn't help
940 // Leave as a bug for now
941
942 // generate the selector by calling _taxonomy_term_select
943 $form['existing']['albumid'] = _taxonomy_term_select('', '', $select_album->tid, _image_pub_get_vid(), '', FALSE, FALSE);
944 $form['existing']['albumid']['#parents'] = array('edit', 'albumid');
945
946 // reset weighting, and delete title from selector
947 unset($form['existing']['albumid']['#weight']);
948 unset($form['existing']['albumid']['#title']);
949 unset($form['existing']['albumid']['#description']);
950
951 // create a container for new album fields
952 // call it edit to avoid having to force #parents
953 $form['edit'] = array(
954 '#type' => 'fieldset',
955 '#tree' => TRUE,
956 );
957
958 // radio button for new album
959 $form['edit']['create_new'] = array(
960 '#type' => 'radio',
961 '#return_value' => 1,
962 '#title' => t('Create a new album'),
963 );
964
965 // get the parent selector by calling _image_gallery_parent_select
966 $form['edit']['create_parentid'] = _image_gallery_parent_select(NULL, 'Within');
967
968 // reset #required option in this instance
969 unset($form['edit']['create_parentid']['#required']);
970
971 $form['edit']['create_name'] = array(
972 '#type' => 'textfield',
973 '#title' => t('Album name'),
974 );
975
976 $form['edit']['create_descr'] = array(
977 '#type' => 'textfield',
978 '#title' => t('Album description'),
979 );
980
981 // Common section - render to all forms
982 $form['cmd'] = array(
983 '#type' => 'hidden',
984 '#value' => 'album',
985 );
986
987 $form['lcid'] = array(
988 '#type' => 'hidden',
989 '#value' => check_plain($_REQUEST['lcid']),
990 );
991
992 $form['langid'] = array(
993 '#type' => 'hidden',
994 '#value' => check_plain($_REQUEST['langid']),
995 );
996
997 $form[session_name()] = array(
998 '#type' => 'hidden',
999 '#value' => check_plain(session_id()),
1000 );
1001
1002 // Build the HTML output
1003 $body = array();
1004
1005 // Title
1006 $body[] = '<h2>Select an album</h2>';
1007
1008 // Display error message if one is active
1009 $body[] = theme('status_messages');
1010
1011 // Render the form elements to themed HTML
1012 $body[] = '<form method="post" id="album">';
1013 $body[] = drupal_render(form_builder('album', $form, $form_state));
1014 $body[] = '</form>';
1015 $body[] = '';
1016
1017 _image_pub_xp_page(implode("\r\n", $body), 'Upload photos...', 'album');
1018 }
1019
1020
1021 /*
1022 * Concoct a magic ecmascript to configure the XP publishing wizard
1023 * to perform the required image submission tasks.
1024 */
1025 function _image_pub_xp_addimage_script($album) {
1026 $albumid = $album->tid;
1027 ?>
1028 <html>
1029 <head>
1030 <script>
1031 function OnBack() {
1032 }
1033 function OnNext() {
1034 }
1035 function window.onload() {
1036 window.external.SetWizardButtons(true,true,true);
1037 }
1038
1039 var xml = window.external.Property("TransferManifest");
1040 var files = xml.selectNodes("transfermanifest/filelist/file");
1041
1042 for (i = 0; i < files.length; i++) {
1043 var postTag = xml.createNode(1, "post", "");
1044 postTag.setAttribute("href", "<?php echo url('publish_xp', array('absolute' => TRUE)); ?>");
1045 postTag.setAttribute("name", "userfile");
1046
1047 var dataTag = xml.createNode(1, "formdata", "");
1048 dataTag.setAttribute("name", "albumid");
1049 dataTag.text = "<?php echo $albumid; ?>";
1050 postTag.appendChild(dataTag);
1051
1052 dataTag = xml.createNode(1, "formdata", "");
1053 dataTag.setAttribute("name", "cmd");
1054 dataTag.text = "addimage";
1055 postTag.appendChild(dataTag);
1056
1057 dataTag = xml.createNode(1, "formdata", "");
1058 dataTag.setAttribute("name", "<?php echo session_name(); ?>");
1059 dataTag.text = "<?php echo session_id(); ?>";
1060 postTag.appendChild(dataTag);
1061
1062 dataTag = xml.createNode(1, "formdata", "");
1063 dataTag.setAttribute("name", "userfile_name");
1064 dataTag.text = files[i].getAttribute("destination");
1065 postTag.appendChild(dataTag);
1066
1067 dataTag.setAttribute("name", "action");
1068 dataTag.text = "SAVE";
1069 postTag.appendChild(dataTag);
1070
1071 files.item(i).appendChild(postTag);
1072 }
1073
1074 var uploadTag = xml.createNode(1, "uploadinfo", "");
1075 var htmluiTag = xml.createNode(1, "htmlui", "");
1076 htmluiTag.text = "<?php echo url('image/tid/'.$albumid, array('absolute' => TRUE)); ?>";
1077 uploadTag.appendChild(htmluiTag);
1078
1079 xml.documentElement.appendChild(uploadTag);
1080
1081 window.external.Property("TransferManifest")=xml;
1082 window.external.SetWizardButtons(true,true,true);
1083 window.external.FinalNext();
1084
1085 </script>
1086 </head>
1087 </html>
1088 <?php
1089 exit;
1090 }

  ViewVC Help
Powered by ViewVC 1.1.2