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

Diff of /contributions/modules/image_pub/image_pub.module

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

revision 1.16, Tue Dec 2 22:46:58 2008 UTC revision 1.16.2.1, Sat Jan 31 02:00:25 2009 UTC
# Line 1  Line 1 
1  <?php  <?php
2  // $Id: image_pub.module,v 1.15 2008/11/19 14:39:58 egfrith Exp $  // $Id: image_pub.module,v 1.16 2008/12/02 22:46:58 egfrith Exp $
3    
4  /*  /*
5   * Image publishing support module   * Image publishing support module
# Line 27  function image_pub_menu() { Line 27  function image_pub_menu() {
27    $items['gallery_remote2.php'] = array(    $items['gallery_remote2.php'] = array(
28      'page callback' => '_image_pub_gr_request',      'page callback' => '_image_pub_gr_request',
29      'access callback' => TRUE,      'access callback' => TRUE,
30        'type' => MENU_CALLBACK);      'file' => 'image_pub.gr.inc',
31        'type' => MENU_CALLBACK);
32    $items['publish_xp'] = array(    $items['publish_xp'] = array(
33      'page callback' => '_image_pub_xp_request',      'page callback' => '_image_pub_xp_request',
34      'access callback' => TRUE,      'access callback' => TRUE,
35        'type' => MENU_CALLBACK);      'file' => 'image_pub.xp.inc',
36        'type' => MENU_CALLBACK);
37    $items['admin/settings/image_pub_xp_reghack'] = array(    $items['admin/settings/image_pub_xp_reghack'] = array(
38      'page callback' => '_image_pub_xp_reghack',      'page callback' => '_image_pub_xp_reghack',
39      'access callback' => TRUE,      'access callback' => TRUE,
40        'type' => MENU_CALLBACK);      'file' => 'image_pub.xp.inc',
41        'type' => MENU_CALLBACK);
42    $items['admin/settings/image_pub'] = array(    $items['admin/settings/image_pub'] = array(
43      'title' => 'Image publishing',      'title' => 'Image publishing',
44      'description' => 'Provides information regarding remote image publishing.',      'description' => 'Provides information regarding remote image publishing.',
# Line 56  function _image_pub_admin() { Line 59  function _image_pub_admin() {
59    return $info;    return $info;
60  }  }
61    
 /*  
  * Generalized helper functions for tasks that require tight integration  
  * with other parts of Drupal, such as image.module and taxonomy.module.  
  *      _image_pub_get_vid()  
  *      _image_pub_base_url()  
  *      _image_pub_image_baseurl()  
  *      _image_pub_get_imagefilename()  
  *      _image_pub_get_imageinfo()  
  *      _image_pub_album_get()  
  *      _image_pub_album_enum()  
  *      _image_pub_album_selector()  
  *      _image_pub_album_access()  
  *      _image_pub_album_images()  
  *  
  *      _image_pub_authenticate()  
  *      _image_pub_album_add()  
  *      _image_pub_image_add()  
  */  
 function _image_pub_get_vid() {  
   return _image_gallery_get_vid();  
 }  
 function _image_pub_base_url() {  
   global $base_url;  
   return $base_url;  
 }  
 function _image_pub_image_baseurl() {  
   return _image_pub_base_url() .'/'. variable_get('file_directory_path', 'files') .'/'. variable_get('image_default_path', 'images');  
 }  
 function _image_pub_get_imagefilename($node, $size = '_original') {  
   $fname = $node->images[$size];  
   if (isset($fname)) {  
     $imgbase = variable_get('image_default_path', 'images') .'/';  
     if (!strncmp($fname, $imgbase, strlen($imgbase))) {  
       $fname = substr($fname, strlen($imgbase));  
     }  
   }  
   return $fname;  
 }  
 function _image_pub_get_imageinfo($node, $size = '_original') {  
   $file = file_create_path($node->images[$size]);  
   $info = image_get_info($file);  
   $info['filesize'] = filesize($file);  
   return $info;  
 }  
 function _image_pub_album_get($albumid) {  
   
   $term = taxonomy_get_term($albumid);  
   if (!$term) {  
         if ($albumid == 0) {  
       $term = taxonomy_get_term('<root>');  
     }  
     else {  
       $term = NULL;  
     }  
   }  
   return $term;  
 }  
 function _image_pub_album_enum($albumid = 0, $subalbums = TRUE) {  
   return taxonomy_get_tree(_image_pub_get_vid(), $albumid, -1,  
                            ($subalbums ? NULL : 1));  
 }  
 function _image_pub_album_selector($fname, $showroot = FALSE, $selalbum = NULL) {  
   /* This is mostly redundant with taxonomy_form(). */  
   $body = array();  
   $tree = _image_pub_album_enum();  
   $options = array();  
   $albumid = 0;  
   if (isset($selalbum)) {  
     $albumid = $selalbum->tid;  
   }  
   if ($showroot) {  
     $options[0] = '&lt;Root Album&gt;';  
   }  
   if ($tree) {  
     foreach ($tree as $term) {  
       if (!$showroot && ($albumid == 0)) {  
         $albumid = $term->tid;  
       }  
       $dashes = '';  
       for ($i = 0; $i < $term->depth; $i++) {  
         $dashes .= '-';  
       }  
       $options[$term->tid] = $dashes . $term->name;  
     }  
   }  
   $body[] = '<select name="' . $fname . '">';  
   foreach ($options as $aid => $term) {  
     $sel = '';  
     if ($aid == $albumid) {  
       $sel = ' selected="selected"';  
     }  
     $body[] = '<option value="' .$aid . '"' . $sel . '>' . $term . '</option>';  
   }  
   $body[] = '</select>';  
   return implode("\r\n", $body);  
 }  
   
 function _image_pub_album_access($type, $album) {  
   switch ($type) {  
     case 'view':  
       return user_access('access content');  
     case 'update':  
       return user_access('administer images');  
     case 'create':  
       return user_access('create images');  
   }  
   return FALSE;  
 }  
 function _image_pub_album_images($tid) {  
   $nodes = array();  
   $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';";  
   $res = db_query($sql);  
   while ($term = db_fetch_object($res)) {  
     $nodes[] = node_load(array('nid' => $term->nid));  
   }  
   return $nodes;  
 }  
 function _image_pub_authenticate($uname, $pass) {  
   global $user;  
   if ($user->uid) {  
     watchdog('image_pub', 'Session closed for %user', array('%user' => theme('placeholder', $user->name)));  
     unset($user);  
   }  
   $user = user_authenticate(array('name' => $uname, 'pass' => $pass));  
   if (!$user->uid) {  
     return FALSE;  
   }  
   watchdog('image_pub', 'Session opened for %user', array('%user' => theme('placeholder', $user->name)));  
   return TRUE;  
 }  
 function _image_pub_album_add($title, $descr, $palbum) {  
   watchdog('image_pub', 'Album added with title %title', array('%title' => $title));  
   
   if (empty($title)) {  
     $title = 'Untitled album';  
   }  
   $edit = array(  
     'name' => $title,  
     'description' => $descr,  
     'vid' => _image_pub_get_vid(),  
     'parent' => array(isset($palbum) ? $palbum->tid : 0),  
     'weight' => 0,  
   );  
   $term = taxonomy_save_term($edit);  
   if (isset($term)) {  
     // taxonomy_save_term amends $edit directly, and simply returns a status  
     // return (object)$edit;  
     return (object)$edit;  
   }  
   return NULL;  
 }  
 function _image_pub_image_add($album, $caption, $description, $srcfield) {  
   global $user;  
   if (!isset($_FILES[$srcfield])) {  
     return array('success' => FALSE,  
                  'reason' => 'Forgetting a file?');  
   }  
   
   if (empty($caption)) {  
     $caption = $_POST['force_filename'];  
     if (empty($caption)) {  
       $caption = basename($_FILES[$srcfield]['name']);  
       if (empty($caption)) {  
         $caption = 'Untitled image';  
       }  
     }  
   }  
   
   // Create a drupal node  
   // In order for the image upload to work, the name, tmp_name and error  
   // of the $_FILES[$srcfield] have to be copied to locations  
   // that file_check_upload() recognises.  
   // The size needs to go into $_FILES['files']['size']['image'] for image  
   // module's size checking to occur.  
   $_FILES['files']['name']['image'] = $_FILES[$srcfield]['name'];  
   $_FILES['files']['tmp_name']['image'] = $_FILES[$srcfield]['tmp_name'];  
   $_FILES['files']['error']['image'] = $_FILES[$srcfield]['error'];  
   $_FILES['files']['size']['image'] = filesize($_FILES[$srcfield]['tmp_name']);  
   
   $form_state = array();  
   module_load_include('inc', 'node', 'node.pages');  
   $node = array('type' => 'image');  
   $form_state['values']['title'] = $caption;  
   $form_state['values']['uid'] = $user->uid;  
   $form_state['values']['name'] = $user->name;  
   $form_state['values']['body_field'] = $description;  
   $form_state['values']['taxonomy'][_image_pub_get_vid()] = $album->tid;  
   $form_state['values']['op'] = t('Save');  
   $redirect_url = drupal_execute('image_node_form', $form_state, (object)$node);  
   
   // Check that the uploaded image was accepted.  
   $errors = form_get_errors();  
   if (!empty($errors)) {  
     return array('success' => FALSE,  
                  'reason' => strip_tags(implode($errors, ' ')));  
   }  
   
   watchdog('image_pub', '%image was added.', array('%image' => $_FILES[$srcfield]['name']));  
   return array('success' => TRUE, 'redirect_url' => $redirect_url);  
 }  
   
   
 /*  
  * Definitions for handling the gallery remote protocol  
  * See http://gallery.menalto.com/ for more info.  
  */  
   
 define('GR_STAT_SUCCESS', 0);  
 define('GR_STAT_PROTO_MAJ_VER_INVAL', 101);  
 define('GR_STAT_PROTO_MIN_VER_INVAL', 102);  
 define('GR_STAT_PROTO_VER_FMT_INVAL', 103);  
 define('GR_STAT_PROTO_VER_MISSING', 104);  
 define('GR_STAT_PASSWD_WRONG', 201);  
 define('GR_STAT_LOGIN_MISSING', 202);  
 define('GR_STAT_UNKNOWN_CMD', 301);  
 define('GR_STAT_NO_ADD_PERMISSION', 401);  
 define('GR_STAT_NO_FILENAME', 402);  
 define('GR_STAT_UPLOAD_PHOTO_FAIL', 403);  
 define('GR_STAT_NO_WRITE_PERMISSION', 404);  
 define('GR_STAT_NO_CREATE_ALBUM_PERMISSION', 501);  
 define('GR_STAT_CREATE_ALBUM_FAILED', 502);  
   
 define('GR_SERVER_VERSION', '2.15');  
   
 function _image_pub_gr_get_albumname($term) {  
   return 'Album' . $term->tid;  
 }  
 function _image_pub_gr_get_albumid($albname) {  
   if (!strncmp($albname, 'Album', 5)) {  
     return substr($albname, 5);  
   }  
   return 0;  
 }  
   
   
 /*  
  * Protocol request handler entry points below  
  *    _image_pub_gr_request()               (main entry point)  
  *    _image_pub_gr_finish()                (completion helper)  
  *    _image_pub_gr_login()  
  *    _image_pub_gr_fetch_albums()  
  *    _image_pub_gr_fetch_album_images()  
  *    _image_pub_gr_add_album()  
  *    _image_pub_gr_move_album()  
  *    _image_pub_gr_add_image()  
  */  
   
 function _image_pub_gr_request() {  
   if (isset($_POST['cmd'])) {  
     $cmd = $_POST['cmd'];  
   }  
   else {  
     $cmd = $_GET['cmd'];  
   }  
   
   $numref = FALSE;  
   
   // watchdog('image_pub', 'Processing command %cmd', array('%cmd' => $cmd));  
   
   switch ($cmd) {  
     case 'login':  
       _image_pub_gr_login($_POST['uname'], $_POST['password']);  
       break;  
   
     case 'fetch-albums':  
       $numref = TRUE;  
     case 'fetch-albums-prune':  
       $check_writeable = ($_POST['check-writeable'] == 'yes') ? TRUE : FALSE;  
       _image_pub_gr_fetch_albums($numref, $check_writeable);  
       break;  
   
     case 'fetch-album-images':  
       $albums_too = ($_POST['albums_too'] == 'yes') ? TRUE : FALSE;  
       _image_pub_gr_fetch_album_images($_POST['set_albumName'],  
                                        $albums_too);  
       break;  
   
     case 'new-album':  
       _image_pub_gr_add_album($_POST['set_albumName'],  
                               $_POST['newAlbumTitle'],  
                               $_POST['newAlbumDesc']);  
       break;  
   
     case 'move-album':  
       _image_pub_gr_move_album($_POST['set_albumName'],  
                                $_POST['set_destalbumName']);  
       break;  
   
     case 'add-item':  
       _image_pub_gr_add_image($_POST['set_albumName'],  
               $_POST['caption'],  
               $_POST['extrafield_Description']);  
       break;  
   
     case '':  
       echo 'For more information about Gallery Remote, see Gallery\'s website located at <a href="http://gallery.sourceforge.net">http://gallery.sourceforge.net</a>';  
       exit;  
   
     default:  
       _image_pub_gr_finish(GR_STAT_UNKNOWN_CMD, '', t('Unknown command "%cmd"', array('%cmd' => theme('placeholder', $cmd))));  
       break;  
   }  
 }  
   
   
 function _image_pub_gr_finish($code, $body = NULL, $message = NULL) {  
   static $gr_messages;  
   if (!isset($gr_messages)) {  
     $gr_messages = array(  
       GR_STAT_SUCCESS => t('Successful'),  
       GR_STAT_PROTO_MAJ_VER_INVAL => t('The protocol major version the client is using is not supported.'),  
       GR_STAT_PROTO_MIN_VER_INVAL => t('The protocol minor version the client is using is not supported.'),  
       GR_STAT_PROTO_VER_FMT_INVAL => t('The format of the protocol version string the client sent in the request is invalid.'),  
       GR_STAT_PROTO_VER_MISSING => t('The request did not contain the required protocol_version key.'),  
       GR_STAT_PASSWD_WRONG => t('The password and/or username the client send in the request is invalid.'),  
       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.'),  
       GR_STAT_UNKNOWN_CMD => t('The value of the cmd key is not valid.'),  
       GR_STAT_NO_ADD_PERMISSION => t('The user does not have permission to add an item to the gallery.'),  
       GR_STAT_NO_FILENAME => t('No filename was specified.'),  
       GR_STAT_UPLOAD_PHOTO_FAIL => t('The file was received, but could not be processed or added to the album.'),  
       GR_STAT_NO_WRITE_PERMISSION => t('No write permission to destination album.'),  
       GR_STAT_NO_CREATE_ALBUM_PERMISSION => t('A new album could not be created because the user does not have permission to do so.'),  
       GR_STAT_CREATE_ALBUM_FAILED => t('A new album could not be created, for a different reason (name conflict).'),  
     );  
   }  
   if (!isset($message)) {  
     $message = $gr_messages[$code];  
     if (!isset($message)) {  
       $message = 'Undefined error code';  
     }  
   }  
   if ($code != GR_STAT_SUCCESS) {  
     watchdog('image_pub', 'Request failure: %code, %msg', array('%code' => $code, '%msg' => $message));  
   }  
   else {  
     //watchdog('image_pub', 'Command succeeded: %msg', array('%msg' => theme('placeholder', $message)));  
   }  
   drupal_set_header("Content-Type: text/plain");  
   echo "#__GR2PROTO__\n" .  
     $body .  
     "status=$code\n" .  
     "status_text=$message\n";  
   exit;  
 }  
   
   
 function _image_pub_gr_login($uname, $pass) {  
   global $user;  
   $body = 'server_version=' . GR_SERVER_VERSION . "\n";  
   if (!$uname || !$pass) {  
     if (!$uname && !$pass) {  
       // Not trying to log in, just asking for a version number.  
       _image_pub_gr_finish(GR_STAT_SUCCESS, $body);  
     }  
     _image_pub_gr_finish(GR_STAT_LOGIN_MISSING);  
   }  
   if (!_image_pub_authenticate($uname, $pass)) {  
     _image_pub_gr_finish(GR_STAT_PASSWD_WRONG);  
   }  
   else {  
     _image_pub_gr_finish(GR_STAT_SUCCESS, $body);  
   }  
 }  
   
   
 function _image_pub_gr_fetch_albums($refnum, $check_writeable) {  
   $body = '';  
   $thumb_dims = image_get_sizes(IMAGE_THUMBNAIL);  
   $thumb_max = min($thumb_dims['width'], $thumb_dims['height']);  
   $preview_dims = image_get_sizes(IMAGE_PREVIEW);  
   $preview_max = min($preview_dims['width'], $preview_dims['height']);  
   $nalbums = 0;  
   $cperm = 'false';  
   $aperm = 'false';  
   $crperm = 'no';  
   if (user_access('create images')) {  
     $cperm = 'true';  
   }  
   if (user_access('administer images')) {  
     $aperm = 'true';  
     $crperm = 'yes';  
   }  
   $tree = _image_pub_album_enum();  
   $aid = 0;  
   $aidref = array();  
   foreach ($tree as $term) {  
     if (_image_pub_album_access('view', $term)) {  
       $aid++;  
       $aname = _image_pub_gr_get_albumname($term);  
       $aidref[$aname] = $aid;  
   
       $pname = 0;  
       if (!empty($term->parents[0])) {  
         $pterm = _image_pub_album_get($term->parents[0]);  
         if ($refnum) {  
           $pname = $aidref[_image_pub_gr_get_albumname($pterm)];   // Guaranteed to exist?  
         }  
         else {  
           $pname = _image_pub_gr_get_albumname($pterm);  
         }  
       }  
   
       $body .= 'album.name.' .             $aid . '=' . $aname . "\n";  
       $body .= 'album.title.' .            $aid . '=' . $term->name . "\n";  
       $body .= 'album.summary.' .          $aid . '=' . $term->description . "\n";  
       $body .= 'album.parent.' .           $aid . '=' . $pname . "\n";  
       $body .= 'album.resize_size.' .      $aid . '=' . $preview_max . "\n";  
       $body .= 'album.max_size.' .         $aid . '=' . '0' . "\n";  
       $body .= 'album.thumb_size.' .       $aid . '=' . $thumb_max . "\n";  
       $body .= 'album.perms.add.' .        $aid . '=' . $cperm . "\n";  
       $body .= 'album.perms.write.' .      $aid . '=' . $aperm . "\n";  
       $body .= 'album.perms.del_item.' .   $aid . '=' . $aperm . "\n";  
       $body .= 'album.perms.del_alb.' .    $aid . '=' . $aperm . "\n";  
       $body .= 'album.perms.create_sub.' . $aid . '=' . $aperm . "\n";  
       $body .= 'album.extrafields.' .      $aid . "=Description\n";  
       $nalbums++;  
     }  
   }  
   $body .= 'album_count=' .     $nalbums . "\n";  
   $body .= 'can_create_root=' . $crperm . "\n";  
   _image_pub_gr_finish(GR_STAT_SUCCESS, $body);  
 }  
   
   
 function _image_pub_gr_fetch_album_images($albname, $albumstoo) {  
   $body = '';  
   $album = _image_pub_album_get(_image_pub_gr_get_albumid($albname));  
   if (!isset($album) || !_image_pub_album_access('view', $album)) {  
     _image_pub_gr_finish(GR_STAT_NO_FILENAME, $body, 'No such album');  
   }  
   
   if ($albumstoo) {  
     $tree = _image_pub_album_enum($album->tid, FALSE);  
     foreach ($tree as $term) {  
       if (_image_pub_album_access('view', $term)) {  
         $body .= 'album.name.' . $term->tid . '=' . _image_pub_gr_get_albumname($term) . "\n";  
       }  
     }  
     $numimages = taxonomy_term_count_nodes($album->tid, 'image');  
   
   }  
   else {  
     $numimages = 0;  
     $nodes = _image_pub_album_images($album->tid);  
     $iid = 0;  
     foreach ($nodes as $node) {  
       if (node_access('view', $node)) {  
         $iid++;  
         $body .= 'image.name.' .                   $iid . '=' . _image_pub_get_imagefilename($node) . "\n";  
         $info = _image_pub_get_imageinfo($node);  
         $body .= 'image.raw_width.' .              $iid . '=' . $info['width'] . "\n";  
         $body .= 'image.raw_height.'.              $iid . '=' . $info['height'] . "\n";  
         $body .= 'image.raw_filesize.' .           $iid . '=' . $info['filesize'] . "\n";  
   
         $body .= 'image.resizedName.' .            $iid . '=' . _image_pub_get_imagefilename($node, IMAGE_PREVIEW) . "\n";  
         $info = _image_pub_get_imageinfo($node, IMAGE_PREVIEW);  
         $body .= 'image.resized_width.' .          $iid . '=' . $info['width'] . "\n";  
         $body .= 'image.resized_height.' .         $iid . '=' . $info['height'] . "\n";  
         //$body .= 'image.resized_filesize.'.$iid.'='.$info['filesize'] . "\n";  
   
         $body .= 'image.thumbName.' .              $iid . '=' . _image_pub_get_imagefilename($node, IMAGE_THUMBNAIL) . "\n";  
         $info = _image_pub_get_imageinfo($node, IMAGE_THUMBNAIL);  
         $body .= 'image.thumb_width.' .            $iid . '=' . $info['width'] . "\n";  
         $body .= 'image.thumb_height.' .           $iid . '=' . $info['height'] . "\n";  
         //$body .= 'image.thumb_filesize.' . $iid . '=' . $info['filesize'] . "\n";  
   
         $body .= 'image.caption.' .                $iid . '=' . $node->title . "\n";  
         $body .= 'image.clicks.' .                 $iid . '=' . '0' . "\n";  
         $body .= 'image.extrafield.Description.' . $iid . '=' .$node->teaser . "\n";  
   
         $ctime = $node->created;  
         $body .= 'image.capturedate.year.' .       $iid . '=' . date('Y', $ctime) . "\n";  
         $body .= 'image.capturedate.mon.' .        $iid . '=' . date('n', $ctime) . "\n";  
         $body .= 'image.capturedate.mday.' .       $iid . '=' . date('j', $ctime) . "\n";  
         $body .= 'image.capturedate.hours.' .      $iid . '=' . date('H', $ctime) . "\n";  
         $body .= 'image.capturedate.minutes.' .    $iid . '=' . date('i', $ctime) . "\n";  
         $body .= 'image.capturedate.seconds.' .    $iid . '=' . date('s', $ctime) . "\n";  
         $body .= 'image.hidden.' .                 $iid . '=' . ($node->status != 1 ? 'yes' : 'no') . "\n";  
         $numimages++;  
       }  
     }  
   }  
   
   $body .= 'image_count=' . $numimages . "\n";  
   $body .= 'baseurl=' . _image_pub_image_baseurl() . "/\n";  
   
   _image_pub_gr_finish(GR_STAT_SUCCESS, $body);  
 }  
   
   
 function _image_pub_gr_add_album($parentaname, $title, $descr) {  
   $body = '';  
   
   $palbum = _image_pub_album_get(_image_pub_gr_get_albumid($parentaname));  
   if (!isset($palbum)) {  
     _image_pub_gr_finish(GR_STAT_CREATE_ALBUM_FAILED, $body, t('No such parent album: %parent', array('%parent' => $parentaname)));  
   }  
   if (!_image_pub_album_access('update', $palbum)) {  
     _image_pub_gr_finish(GR_STAT_NO_CREATE_ALBUM_PERMISSION, $body);  
   }  
   
   $term =_image_pub_album_add($title, $descr, $palbum);  
   if (!isset($term)) {  
     _image_pub_gr_finish(GR_STAT_CREATE_ALBUM_FAILED);  
   }  
   else {  
     $body .= 'album_name=' . _image_pub_gr_get_albumname($term) . "\n";  
     _image_pub_gr_finish(GR_STAT_SUCCESS, $body, 'Album created');  
   }  
 }  
   
   
 function _image_pub_gr_move_album($albname, $destaname) {  
   $body = '';  
   
   $album = _image_pub_album_get(_image_pub_gr_get_albumid($albname));  
   if (!isset($album)) {  
     _image_pub_gr_finish(GR_STAT_CREATE_ALBUM_FAILED, $body, 'No such album');  
   }  
   
   if (!_image_pub_album_access('update', $album)) {  
     _image_pub_gr_finish(GR_STAT_NO_WRITE_PERMISSION, $body);  
   }  
   
   if (!isset($destaname) || ($destaname == '0') || ($destaname == 'rootalbum')) {  
     $dtid = 0;  
   }  
   else {  
     $dalbum = _image_pub_album_get(_image_pub_gr_get_albumid($destaname));  
     if (!isset($dalbum) || !_image_pub_album_access('update', $dalbum)) {  
       _image_pub_gr_finish(GR_STAT_CREATE_ALBUM_FAILED, $body, 'Invalid destination album '.$destaname);  
     }  
     $dtid = $dalbum->tid;  
   }  
   
   $album->parent = array($dtid);  
   $term = (array)$album;  
   $status = taxonomy_save_term($term);  
   
   _image_pub_gr_finish(GR_STAT_SUCCESS, $body, 'Album reparented');  
 }  
   
   
 function _image_pub_gr_add_image($albname, $caption, $description) {  
   $body = '';  
   $album = _image_pub_album_get(_image_pub_gr_get_albumid($albname));  
   if (!isset($album)) {  
     _image_pub_gr_finish(GR_STAT_UPLOAD_PHOTO_FAIL, 'No such album');  
   }  
   if (!_image_pub_album_access('create', $album)) {  
     _image_pub_gr_finish(GR_STAT_NO_ADD_PERMISSION);  
   }  
   
   $result = _image_pub_image_add($album, $caption, $description, 'userfile');  
   
   if (!$result['success']) {  
     _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']);  
   }  
   else {  
     _image_pub_gr_finish(GR_STAT_SUCCESS, $body, 'Added node '.$result['node']->nid);  
   }  
 }  
   
   
 /*  
  * Windows Web publishing wizard support  
  *  
  * Below is support for the Windows XP Web publishing wizard.  It's just a  
  * glorified file picker and MSIE instance in a wizard form factor, but  
  * it gets the job done, and did we mention it comes with every copy of  
  * Windows?  
  *  
  * Microsoft even appears to have some sort of documentation for the  
  * publishing wizard on the MSDN site, although debugging is a major  
  * pain:  
  *  
  * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/shell/programmersguide/shell_basics/shell_basics_extending/publishing_wizard/pubwiz.asp  
  *  
  * Our part of the wizard has three steps:  
  * 1. Authenticate.  
  * 2. Select or create album.  
  * 3. Publish.  We send a blank page with a magic script to configure  
  *    the wizard.  
  *  
  * We send bare HTML and don't use themes.  Why not themes?  
  * -> The XP publishing wizard has a microscopic embedded MSIE instance  
  *    that we don't want to overflow with content.  That is hard to  
  *    guarantee using an arbitrary theme.  
  * -> Themes will display distracting links in headers and sidebars.  We  
  *    don't want the user navigating to one of the distractions.  
  */  
   
   
 /*  
  * Concoct a magic registry file to add this site to the list of  
  * usable destinations for the publishing wizard.  
  */  
 function _image_pub_xp_reghack() {  
   $pub_url = url('publish_xp', array('absolute' => TRUE));  
   $site_name = variable_get('site_name', 'drupal');  
   $site_descr = sprintf(t("Publish Your Photos to %s."), $site_name);  
   if (!strncmp($pub_url, 'https:', 6)) {  
     $site_descr .= ' (Secure)';  
   }  
   else {  
     $site_descr .= ' (Insecure)';  
   }  
   drupal_set_header("Cache-control: private");  
   drupal_set_header("Content-Type: application/octet-stream");  
   drupal_set_header("Content-Disposition: filename=install_registry.reg");  
   
   $lines[] = 'Windows Registry Editor Version 5.00';  
   $lines[] = '';  
   $lines[] = '[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\PublishingWizard\PublishingWizard\Providers\\' . $site_name . ']';  
   $lines[] = '"displayname"="' . $site_name . '"';  
   $lines[] = '"description"="' . $site_descr . '"';  
   $lines[] = '"href"="' . $pub_url . '"';  
   $lines[] = '"icon"="' . _image_pub_base_url() . '/favicon.ico"';  
   $lines[] = '';  
   echo join("\r\n", $lines);  
   exit;  
 }  
   
   
 /*  
  * Implementation of wizard forms and such:  
  *      _image_pub_xp_request()         (main entry point)  
  *      _image_pub_xp_login()  
  *      _image_pub_xp_album()  
  *      _image_pub_xp_addimage()  
  *      _image_pub_xp_page()            (HTML form template)  
  *      _image_pub_xp_login_form()  
  *      _image_pub_xp_album_form()  
  *      _image_pub_xp_addimage_script()  
  */  
   
 function _image_pub_xp_request() {  
   
   /*  
    * We require the user to be logged in to publish.  
    * We honor cookies saved from other MSIE sessions, and don't  
    * require the user to log in if they already have a session.  
    */  
   
   global $user;  
   $cmd = $_REQUEST['cmd'];  
   if (!$user->uid) {  
     if (!isset($cmd)) {  
       $cmd = 'login';  
     }  
   }  
   
   switch ($cmd) {  
     case 'login':  
       _image_pub_xp_login($_REQUEST['uname'], $_REQUEST['password']);  
       break;  
     case 'addimage':  
       _image_pub_xp_addimage($_REQUEST['albumid']);  
     default:  
       _image_pub_xp_album($_REQUEST['edit']);  
       break;  
   }  
 }  
   
   
 function _image_pub_xp_login($uname, $pass) {  
   if (empty($uname) && empty($pass)) {  
     _image_pub_xp_login_form();  
   }  
   elseif (!_image_pub_authenticate($uname, $pass)) {  
     drupal_set_message('Login failed. Please try again.', 'error');  
     _image_pub_xp_login_form("Invalid Login", $uname);  
   }  
   else {  
     _image_pub_xp_album_form();  
   }  
 }  
   
   
 function _image_pub_xp_album($edit) {  
   if (!isset($edit)) {  
     _image_pub_xp_album_form();  
     return;  
   }  
   
   $create_new = $edit['create_new'];  
   
   if ($create_new) {  
     $albumid = $edit['create_parentid'];  
     $create_name = $edit['create_name'];  
     $create_descr = $edit['create_descr'];  
   
     $album = _image_pub_album_get($albumid);  
     if (!_image_pub_album_access('update', $album)) {  
       drupal_set_message('Created album permission denied.', 'error');  
       _image_pub_xp_album_form('Create Album Permission Denied', $album);  
     }  
   
     $newalbum = _image_pub_album_add($create_name, $create_descr, $album);  
   
     if (isset($newalbum)) {  
       drupal_set_message('New album '.'<em>'.$newalbum->name.'</em>'.' created.');  
       _image_pub_xp_album_form('Created Album "'.$newalbum->name.'"', $newalbum);  
     }  
     else {  
       drupal_set_message('Error creating album '.'<em>'.$newalbum->name.'</em>.', 'error');  
       _image_pub_xp_album_form('Error Creating Album', $album);  
     }  
     return;  
   }  
   
   // if we got here we are choosing an existing album  
   $albumid = $edit['albumid'];  
   $album = _image_pub_album_get($albumid);  
   
   if (!isset($album)) {  
     drupal_set_mssage('Invalid album specified.', 'error');  
     _image_pub_xp_album_form('Invalid album specified');  
     return;  
   }  
   if (!_image_pub_album_access('create', $album)) {  
     drupal_set_message('Create image permission denied.', 'error');  
     _image_pub_xp_album_form('Create Image Permission Denied', $album);  
     return;  
   }  
   _image_pub_xp_addimage_script($album);  
 }  
   
   
 function _image_pub_xp_addimage($albumid) {  
   $album = _image_pub_album_get($albumid);  
   if (!isset($album)) {  
     _image_pub_xp_album_form('Invalid album specified');  
     return;  
   }  
   if (!_image_pub_album_access('update', $album)) {  
     _image_pub_xp_album_form('Create Image Permission Denied', $album);  
     return;  
   }  
   
   $result = _image_pub_image_add($album, NULL, NULL, 'userfile');  
 }  
   
   
 function _image_pub_xp_page($body, $header, $next, $back = NULL) {  
   global $language;  
   $site_name = variable_get('site_name', 'drupal');  
 ?>  
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">  
 <html xmlns="http://www.w3.org/1999/xhtml" lang="<?php print $language->language; ?>" xml:lang="<?php print $language->language; ?>">  
 <head>  
 </head>  
 <body>  
 <?php echo $body; ?>  
 <script>  
 function OnBack() {  
 <?php if (isset($back)) { ?>  
         window.location.href = "<?php echo $back; ?>";  
 <?php }  
 else { ?>  
         window.external.FinalBack();  
 <?php } ?>  
         window.external.SetWizardButtons(true,true,false);  
 }  
 function OnNext() {  
         <?php echo $next; ?>.submit();  
 }  
 function window.onload() {  
         window.external.SetHeaderText("<?php echo $site_name; ?>", "<?php echo $header; ?>");  
         window.external.SetWizardButtons(true,true,false);  
 }  
 </script>  
 </body>  
 </html>  
 <?php  
   exit;  
 }  
   
   
 function _image_pub_xp_login_form($message = NULL, $uname = NULL) {  
   
   if (!isset($uname)) {  
     $uname = '';  
   }  
   
   // Build a form array  
   $form = array();  
   
   $form['login'] = array(  
     '#type' => 'fieldset',  
     '#tree' => FALSE,  
   );  
   
   $form['login']['uname'] = array(  
     '#type' => 'textfield',  
     '#title' => t('User name'),  
     '#default_value' => check_plain($uname),  
     '#description' => t('Enter your user name.'),  
   );  
   
   $form['login']['password'] = array(  
     '#type' => 'password',  
     '#title' => t('Password'),  
     '#description' => t('Enter your password.'),  
   );  
   
   $form['cmd'] = array(  
     '#type' => 'hidden',  
     '#value' => 'login',  
   );  
   
   $form['lcid'] = array(  
     '#type' => 'hidden',  
     '#value' => check_plain($_REQUEST['lcid']),  
   );  
   
   $form['langid'] = array(  
     '#type' => 'hidden',  
     '#value' => check_plain($_REQUEST['langid']),  
   );  
   
   $form[session_name()] = array(  
     '#type' => 'hidden',  
     '#value' => check_plain(session_id()),  
   );  
   
   // Build the HTML output  
   $body = array();  
   
   // Title  
   $body[] = '<h1>Log in</h1>';  
   
   // Display error message if one is active  
   $body[] = theme('status_messages');  
   
   // Render the form elements to themed HTML  
   $body[] = '<form method="post" id="login">';  
   $body[] = drupal_render(form_builder('login', $form));  
   $body[] = '</form>';  
   $body[] = '';  
   
   // Output the wizard page  
   _image_pub_xp_page(implode("\r\n", $body), 'Upload photos...', 'login');  
   
 }  
   
   
 function _image_pub_xp_album_form($message = NULL, $select_album = NULL) {  
   
   /**  
    * This form is currently not very elegant in how it is build  
    * as it has to over-ride #parents to be compatible with the rest  
    * of the existing code. Field sets are used to present the data  
    * in logical blocks. It may be better in the longer term to make an  
    * additional form page - decide it you want an existing or a new album  
    * and then take the user there, rather than doing it all in one.  
    * Leave as is for now just to check the concept is working  
    */  
   
   // Build a form array  
   $form = array();  
   $form_state = array();  
   
   // create a fieldset to wrap the form in  
   // note #parents must be forced for this set to work with original code  
   $form['existing'] = array(  
     '#type' => 'fieldset',  
     '#tree' => FALSE,  
   );  
   
   // radio button for existing album  
   $form['existing']['create_new'] = array(  
     '#type' => 'radio',  
     '#return_value' => 0,  
     '#title' => t('Use an existing album'),  
     '#attributes' => array('checked' => 'checked'),  
     '#parents' => array('edit', 'create_new'),  
   );  
   
   // for some reason if a new album it doesn't always end up in the select list  
   // I tried using cache_clear_all but that doesn't help  
   // Leave as a bug for now  
   
   // generate the selector by calling _taxonomy_term_select  
   $form['existing']['albumid'] = _taxonomy_term_select('', '', $select_album->tid, _image_pub_get_vid(), '', FALSE, FALSE);  
   $form['existing']['albumid']['#parents'] = array('edit', 'albumid');  
   
   // reset weighting, and delete title from selector  
   unset($form['existing']['albumid']['#weight']);  
   unset($form['existing']['albumid']['#title']);  
   unset($form['existing']['albumid']['#description']);  
   
   // create a container for new album fields  
   // call it edit to avoid having to force #parents  
   $form['edit'] = array(  
     '#type' => 'fieldset',  
     '#tree' => TRUE,  
   );  
   
   // radio button for new album  
   $form['edit']['create_new'] = array(  
     '#type' => 'radio',  
     '#return_value' => 1,  
     '#title' => t('Create a new album'),  
   );  
   
   // get the parent selector by calling _image_gallery_parent_select  
   $form['edit']['create_parentid'] = _image_gallery_parent_select(NULL, 'Within');  
   
   // reset #required option in this instance  
   unset($form['edit']['create_parentid']['#required']);  
   
   $form['edit']['create_name'] = array(  
     '#type' => 'textfield',  
     '#title' => t('Album name'),  
   );  
   
   $form['edit']['create_descr'] = array(  
     '#type' => 'textfield',  
     '#title' => t('Album description'),  
   );  
   
   // Common section - render to all forms  
   $form['cmd'] = array(  
     '#type' => 'hidden',  
     '#value' => 'album',  
   );  
   
   $form['lcid'] = array(  
     '#type' => 'hidden',  
     '#value' => check_plain($_REQUEST['lcid']),  
   );  
   
   $form['langid'] = array(  
     '#type' => 'hidden',  
     '#value' => check_plain($_REQUEST['langid']),  
   );  
   
   $form[session_name()] = array(  
     '#type' => 'hidden',  
     '#value' => check_plain(session_id()),  
   );  
   
   // Build the HTML output  
   $body = array();  
   
   // Title  
   $body[] = '<h2>Select an album</h2>';  
   
   // Display error message if one is active  
   $body[] = theme('status_messages');  
   
   // Render the form elements to themed HTML  
   $body[] = '<form method="post" id="album">';  
   $body[] = drupal_render(form_builder('album', $form, $form_state));  
   $body[] = '</form>';  
   $body[] = '';  
   
    _image_pub_xp_page(implode("\r\n", $body), 'Upload photos...', 'album');  
 }  
   
   
 /*  
  * Concoct a magic ecmascript to configure the XP publishing wizard  
  * to perform the required image submission tasks.  
  */  
 function _image_pub_xp_addimage_script($album) {  
   $albumid = $album->tid;  
 ?>  
 <html>  
 <head>  
 <script>  
 function OnBack() {  
 }  
 function OnNext() {  
 }  
 function window.onload() {  
         window.external.SetWizardButtons(true,true,true);  
 }  
   
 var xml = window.external.Property("TransferManifest");  
 var files = xml.selectNodes("transfermanifest/filelist/file");  
   
 for (i = 0; i < files.length; i++) {  
         var postTag = xml.createNode(1, "post", "");  
         postTag.setAttribute("href", "<?php echo url('publish_xp',  array('absolute' => TRUE)); ?>");  
         postTag.setAttribute("name", "userfile");  
   
         var dataTag = xml.createNode(1, "formdata", "");  
         dataTag.setAttribute("name", "albumid");  
         dataTag.text = "<?php echo $albumid; ?>";  
         postTag.appendChild(dataTag);  
   
         dataTag = xml.createNode(1, "formdata", "");  
         dataTag.setAttribute("name", "cmd");  
         dataTag.text = "addimage";  
         postTag.appendChild(dataTag);  
   
         dataTag = xml.createNode(1, "formdata", "");  
         dataTag.setAttribute("name", "<?php echo session_name(); ?>");  
         dataTag.text = "<?php echo session_id(); ?>";  
         postTag.appendChild(dataTag);  
   
         dataTag = xml.createNode(1, "formdata", "");  
         dataTag.setAttribute("name", "userfile_name");  
         dataTag.text = files[i].getAttribute("destination");  
         postTag.appendChild(dataTag);  
   
         dataTag.setAttribute("name", "action");  
         dataTag.text = "SAVE";  
         postTag.appendChild(dataTag);  
   
         files.item(i).appendChild(postTag);  
 }  
   
 var uploadTag = xml.createNode(1, "uploadinfo", "");  
 var htmluiTag = xml.createNode(1, "htmlui", "");  
 htmluiTag.text = "<?php echo url('image/tid/'.$albumid, array('absolute' => TRUE)); ?>";  
 uploadTag.appendChild(htmluiTag);  
   
 xml.documentElement.appendChild(uploadTag);  
   
 window.external.Property("TransferManifest")=xml;  
 window.external.SetWizardButtons(true,true,true);  
 window.external.FinalNext();  
   
 </script>  
 </head>  
 </html>  
 <?php  
   exit;  
 }  

Legend:
Removed from v.1.16  
changed lines
  Added in v.1.16.2.1

  ViewVC Help
Powered by ViewVC 1.1.2