/[drupal]/contributions/modules/image_pub/image_pub.gr.inc
ViewVC logotype

Diff of /contributions/modules/image_pub/image_pub.gr.inc

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

revision 1.1, Sat Jan 31 02:00:25 2009 UTC revision 1.2, Sat Jan 31 17:51:40 2009 UTC
# Line 0  Line 1 
1    <?php
2    // $Id: image_pub.gr.inc,v 1.1.2.1 2009/01/31 02:00:25 egfrith Exp $
3    
4    require_once 'image_pub.common.inc';
5    
6    /*
7     * Definitions for handling the gallery remote protocol
8     * See http://gallery.menalto.com/ for more info.
9     */
10    
11    define('GR_STAT_SUCCESS', 0);
12    define('GR_STAT_PROTO_MAJ_VER_INVAL', 101);
13    define('GR_STAT_PROTO_MIN_VER_INVAL', 102);
14    define('GR_STAT_PROTO_VER_FMT_INVAL', 103);
15    define('GR_STAT_PROTO_VER_MISSING', 104);
16    define('GR_STAT_PASSWD_WRONG', 201);
17    define('GR_STAT_LOGIN_MISSING', 202);
18    define('GR_STAT_UNKNOWN_CMD', 301);
19    define('GR_STAT_NO_ADD_PERMISSION', 401);
20    define('GR_STAT_NO_FILENAME', 402);
21    define('GR_STAT_UPLOAD_PHOTO_FAIL', 403);
22    define('GR_STAT_NO_WRITE_PERMISSION', 404);
23    define('GR_STAT_NO_CREATE_ALBUM_PERMISSION', 501);
24    define('GR_STAT_CREATE_ALBUM_FAILED', 502);
25    
26    define('GR_SERVER_VERSION', '2.15');
27    
28    function _image_pub_gr_get_albumname($term) {
29      return 'Album' . $term->tid;
30    }
31    function _image_pub_gr_get_albumid($albname) {
32      if (!strncmp($albname, 'Album', 5)) {
33        return substr($albname, 5);
34      }
35      return 0;
36    }
37    
38    
39    /*
40     * Protocol request handler entry points below
41     *    _image_pub_gr_request()               (main entry point)
42     *    _image_pub_gr_finish()                (completion helper)
43     *    _image_pub_gr_login()
44     *    _image_pub_gr_fetch_albums()
45     *    _image_pub_gr_fetch_album_images()
46     *    _image_pub_gr_add_album()
47     *    _image_pub_gr_move_album()
48     *    _image_pub_gr_add_image()
49     */
50    
51    function _image_pub_gr_request() {
52      if (isset($_POST['cmd'])) {
53        $cmd = $_POST['cmd'];
54      }
55      else {
56        $cmd = $_GET['cmd'];
57      }
58    
59      $numref = FALSE;
60    
61      // watchdog('image_pub', 'Processing command %cmd', array('%cmd' => $cmd));
62    
63      switch ($cmd) {
64        case 'login':
65          _image_pub_gr_login($_POST['uname'], $_POST['password']);
66          break;
67    
68        case 'fetch-albums':
69          $numref = TRUE;
70        case 'fetch-albums-prune':
71          $check_writeable = ($_POST['check-writeable'] == 'yes') ? TRUE : FALSE;
72          _image_pub_gr_fetch_albums($numref, $check_writeable);
73          break;
74    
75        case 'fetch-album-images':
76          $albums_too = ($_POST['albums_too'] == 'yes') ? TRUE : FALSE;
77          _image_pub_gr_fetch_album_images($_POST['set_albumName'],
78                                           $albums_too);
79          break;
80    
81        case 'new-album':
82          _image_pub_gr_add_album($_POST['set_albumName'],
83                                  $_POST['newAlbumTitle'],
84                                  $_POST['newAlbumDesc']);
85          break;
86    
87        case 'move-album':
88          _image_pub_gr_move_album($_POST['set_albumName'],
89                                   $_POST['set_destalbumName']);
90          break;
91    
92        case 'add-item':
93          _image_pub_gr_add_image($_POST['set_albumName'],
94                  $_POST['caption'],
95                  $_POST['extrafield_Description']);
96          break;
97    
98        case '':
99          echo 'For more information about Gallery Remote, see Gallery\'s website located at <a href="http://gallery.sourceforge.net">http://gallery.sourceforge.net</a>';
100          exit;
101    
102        default:
103          _image_pub_gr_finish(GR_STAT_UNKNOWN_CMD, '', t('Unknown command "%cmd"', array('%cmd' => theme('placeholder', $cmd))));
104          break;
105      }
106    }
107    
108    
109    function _image_pub_gr_finish($code, $body = NULL, $message = NULL) {
110      static $gr_messages;
111      if (!isset($gr_messages)) {
112        $gr_messages = array(
113          GR_STAT_SUCCESS => t('Successful'),
114          GR_STAT_PROTO_MAJ_VER_INVAL => t('The protocol major version the client is using is not supported.'),
115          GR_STAT_PROTO_MIN_VER_INVAL => t('The protocol minor version the client is using is not supported.'),
116          GR_STAT_PROTO_VER_FMT_INVAL => t('The format of the protocol version string the client sent in the request is invalid.'),
117          GR_STAT_PROTO_VER_MISSING => t('The request did not contain the required protocol_version key.'),
118          GR_STAT_PASSWD_WRONG => t('The password and/or username the client send in the request is invalid.'),
119          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.'),
120          GR_STAT_UNKNOWN_CMD => t('The value of the cmd key is not valid.'),
121          GR_STAT_NO_ADD_PERMISSION => t('The user does not have permission to add an item to the gallery.'),
122          GR_STAT_NO_FILENAME => t('No filename was specified.'),
123          GR_STAT_UPLOAD_PHOTO_FAIL => t('The file was received, but could not be processed or added to the album.'),
124          GR_STAT_NO_WRITE_PERMISSION => t('No write permission to destination album.'),
125          GR_STAT_NO_CREATE_ALBUM_PERMISSION => t('A new album could not be created because the user does not have permission to do so.'),
126          GR_STAT_CREATE_ALBUM_FAILED => t('A new album could not be created, for a different reason (name conflict).'),
127        );
128      }
129      if (!isset($message)) {
130        $message = $gr_messages[$code];
131        if (!isset($message)) {
132          $message = 'Undefined error code';
133        }
134      }
135      if ($code != GR_STAT_SUCCESS) {
136        watchdog('image_pub', 'Request failure: %code, %msg', array('%code' => $code, '%msg' => $message));
137      }
138      else {
139        //watchdog('image_pub', 'Command succeeded: %msg', array('%msg' => theme('placeholder', $message)));
140      }
141      drupal_set_header("Content-Type: text/plain");
142      echo "#__GR2PROTO__\n" .
143        $body .
144        "status=$code\n" .
145        "status_text=$message\n";
146      exit;
147    }
148    
149    
150    function _image_pub_gr_login($uname, $pass) {
151      global $user;
152      $body = 'server_version=' . GR_SERVER_VERSION . "\n";
153      if (!$uname || !$pass) {
154        if (!$uname && !$pass) {
155          // Not trying to log in, just asking for a version number.
156          _image_pub_gr_finish(GR_STAT_SUCCESS, $body);
157        }
158        _image_pub_gr_finish(GR_STAT_LOGIN_MISSING);
159      }
160      if (!_image_pub_authenticate($uname, $pass)) {
161        _image_pub_gr_finish(GR_STAT_PASSWD_WRONG);
162      }
163      else {
164        _image_pub_gr_finish(GR_STAT_SUCCESS, $body);
165      }
166    }
167    
168    
169    function _image_pub_gr_fetch_albums($refnum, $check_writeable) {
170      $body = '';
171      $thumb_dims = image_get_sizes(IMAGE_THUMBNAIL);
172      $thumb_max = min($thumb_dims['width'], $thumb_dims['height']);
173      $preview_dims = image_get_sizes(IMAGE_PREVIEW);
174      $preview_max = min($preview_dims['width'], $preview_dims['height']);
175      $nalbums = 0;
176      $cperm = 'false';
177      $aperm = 'false';
178      $crperm = 'no';
179      if (user_access('create images')) {
180        $cperm = 'true';
181      }
182      if (user_access('administer images')) {
183        $aperm = 'true';
184        $crperm = 'yes';
185      }
186      $tree = _image_pub_album_enum();
187      $aid = 0;
188      $aidref = array();
189      foreach ($tree as $term) {
190        if (_image_pub_album_access('view', $term)) {
191          $aid++;
192          $aname = _image_pub_gr_get_albumname($term);
193          $aidref[$aname] = $aid;
194    
195          $pname = 0;
196          if (!empty($term->parents[0])) {
197            $pterm = _image_pub_album_get($term->parents[0]);
198            if ($refnum) {
199              $pname = $aidref[_image_pub_gr_get_albumname($pterm)];   // Guaranteed to exist?
200            }
201            else {
202              $pname = _image_pub_gr_get_albumname($pterm);
203            }
204          }
205    
206          $body .= 'album.name.' .             $aid . '=' . $aname . "\n";
207          $body .= 'album.title.' .            $aid . '=' . $term->name . "\n";
208          $body .= 'album.summary.' .          $aid . '=' . $term->description . "\n";
209          $body .= 'album.parent.' .           $aid . '=' . $pname . "\n";
210          $body .= 'album.resize_size.' .      $aid . '=' . $preview_max . "\n";
211          $body .= 'album.max_size.' .         $aid . '=' . '0' . "\n";
212          $body .= 'album.thumb_size.' .       $aid . '=' . $thumb_max . "\n";
213          $body .= 'album.perms.add.' .        $aid . '=' . $cperm . "\n";
214          $body .= 'album.perms.write.' .      $aid . '=' . $aperm . "\n";
215          $body .= 'album.perms.del_item.' .   $aid . '=' . $aperm . "\n";
216          $body .= 'album.perms.del_alb.' .    $aid . '=' . $aperm . "\n";
217          $body .= 'album.perms.create_sub.' . $aid . '=' . $aperm . "\n";
218          $body .= 'album.extrafields.' .      $aid . "=Description\n";
219          $nalbums++;
220        }
221      }
222      $body .= 'album_count=' .     $nalbums . "\n";
223      $body .= 'can_create_root=' . $crperm . "\n";
224      _image_pub_gr_finish(GR_STAT_SUCCESS, $body);
225    }
226    
227    
228    function _image_pub_gr_fetch_album_images($albname, $albumstoo) {
229      $body = '';
230      $album = _image_pub_album_get(_image_pub_gr_get_albumid($albname));
231      if (!isset($album) || !_image_pub_album_access('view', $album)) {
232        _image_pub_gr_finish(GR_STAT_NO_FILENAME, $body, 'No such album');
233      }
234    
235      if ($albumstoo) {
236        $tree = _image_pub_album_enum($album->tid, FALSE);
237        foreach ($tree as $term) {
238          if (_image_pub_album_access('view', $term)) {
239            $body .= 'album.name.' . $term->tid . '=' . _image_pub_gr_get_albumname($term) . "\n";
240          }
241        }
242        $numimages = taxonomy_term_count_nodes($album->tid, 'image');
243    
244      }
245      else {
246        $numimages = 0;
247        $nodes = _image_pub_album_images($album->tid);
248        $iid = 0;
249        foreach ($nodes as $node) {
250          if (node_access('view', $node)) {
251            $iid++;
252            $body .= 'image.name.' .                   $iid . '=' . _image_pub_get_imagefilename($node) . "\n";
253            $info = _image_pub_get_imageinfo($node);
254            $body .= 'image.raw_width.' .              $iid . '=' . $info['width'] . "\n";
255            $body .= 'image.raw_height.'.              $iid . '=' . $info['height'] . "\n";
256            $body .= 'image.raw_filesize.' .           $iid . '=' . $info['filesize'] . "\n";
257    
258            $body .= 'image.resizedName.' .            $iid . '=' . _image_pub_get_imagefilename($node, IMAGE_PREVIEW) . "\n";
259            $info = _image_pub_get_imageinfo($node, IMAGE_PREVIEW);
260            $body .= 'image.resized_width.' .          $iid . '=' . $info['width'] . "\n";
261            $body .= 'image.resized_height.' .         $iid . '=' . $info['height'] . "\n";
262            //$body .= 'image.resized_filesize.'.$iid.'='.$info['filesize'] . "\n";
263    
264            $body .= 'image.thumbName.' .              $iid . '=' . _image_pub_get_imagefilename($node, IMAGE_THUMBNAIL) . "\n";
265            $info = _image_pub_get_imageinfo($node, IMAGE_THUMBNAIL);
266            $body .= 'image.thumb_width.' .            $iid . '=' . $info['width'] . "\n";
267            $body .= 'image.thumb_height.' .           $iid . '=' . $info['height'] . "\n";
268            //$body .= 'image.thumb_filesize.' . $iid . '=' . $info['filesize'] . "\n";
269    
270            $body .= 'image.caption.' .                $iid . '=' . $node->title . "\n";
271            $body .= 'image.clicks.' .                 $iid . '=' . '0' . "\n";
272            $body .= 'image.extrafield.Description.' . $iid . '=' .$node->teaser . "\n";
273    
274            $ctime = $node->created;
275            $body .= 'image.capturedate.year.' .       $iid . '=' . date('Y', $ctime) . "\n";
276            $body .= 'image.capturedate.mon.' .        $iid . '=' . date('n', $ctime) . "\n";
277            $body .= 'image.capturedate.mday.' .       $iid . '=' . date('j', $ctime) . "\n";
278            $body .= 'image.capturedate.hours.' .      $iid . '=' . date('H', $ctime) . "\n";
279            $body .= 'image.capturedate.minutes.' .    $iid . '=' . date('i', $ctime) . "\n";
280            $body .= 'image.capturedate.seconds.' .    $iid . '=' . date('s', $ctime) . "\n";
281            $body .= 'image.hidden.' .                 $iid . '=' . ($node->status != 1 ? 'yes' : 'no') . "\n";
282            $numimages++;
283          }
284        }
285      }
286    
287      $body .= 'image_count=' . $numimages . "\n";
288      $body .= 'baseurl=' . _image_pub_image_baseurl() . "/\n";
289    
290      _image_pub_gr_finish(GR_STAT_SUCCESS, $body);
291    }
292    
293    
294    function _image_pub_gr_add_album($parentaname, $title, $descr) {
295      $body = '';
296    
297      $palbum = _image_pub_album_get(_image_pub_gr_get_albumid($parentaname));
298      if (!isset($palbum)) {
299        _image_pub_gr_finish(GR_STAT_CREATE_ALBUM_FAILED, $body, t('No such parent album: %parent', array('%parent' => $parentaname)));
300      }
301      if (!_image_pub_album_access('update', $palbum)) {
302        _image_pub_gr_finish(GR_STAT_NO_CREATE_ALBUM_PERMISSION, $body);
303      }
304    
305      $term =_image_pub_album_add($title, $descr, $palbum);
306      if (!isset($term)) {
307        _image_pub_gr_finish(GR_STAT_CREATE_ALBUM_FAILED);
308      }
309      else {
310        $body .= 'album_name=' . _image_pub_gr_get_albumname($term) . "\n";
311        _image_pub_gr_finish(GR_STAT_SUCCESS, $body, 'Album created');
312      }
313    }
314    
315    
316    function _image_pub_gr_move_album($albname, $destaname) {
317      $body = '';
318    
319      $album = _image_pub_album_get(_image_pub_gr_get_albumid($albname));
320      if (!isset($album)) {
321        _image_pub_gr_finish(GR_STAT_CREATE_ALBUM_FAILED, $body, 'No such album');
322      }
323    
324      if (!_image_pub_album_access('update', $album)) {
325        _image_pub_gr_finish(GR_STAT_NO_WRITE_PERMISSION, $body);
326      }
327    
328      if (!isset($destaname) || ($destaname == '0') || ($destaname == 'rootalbum')) {
329        $dtid = 0;
330      }
331      else {
332        $dalbum = _image_pub_album_get(_image_pub_gr_get_albumid($destaname));
333        if (!isset($dalbum) || !_image_pub_album_access('update', $dalbum)) {
334          _image_pub_gr_finish(GR_STAT_CREATE_ALBUM_FAILED, $body, 'Invalid destination album '.$destaname);
335        }
336        $dtid = $dalbum->tid;
337      }
338    
339      $album->parent = array($dtid);
340      $term = (array)$album;
341      $status = taxonomy_save_term($term);
342    
343      _image_pub_gr_finish(GR_STAT_SUCCESS, $body, 'Album reparented');
344    }
345    
346    
347    function _image_pub_gr_add_image($albname, $caption, $description) {
348      $body = '';
349      $album = _image_pub_album_get(_image_pub_gr_get_albumid($albname));
350      if (!isset($album)) {
351        _image_pub_gr_finish(GR_STAT_UPLOAD_PHOTO_FAIL, 'No such album');
352      }
353      if (!_image_pub_album_access('create', $album)) {
354        _image_pub_gr_finish(GR_STAT_NO_ADD_PERMISSION);
355      }
356    
357      $result = _image_pub_image_add($album, $caption, $description, 'userfile');
358    
359      if (!$result['success']) {
360        _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']);
361      }
362      else {
363        _image_pub_gr_finish(GR_STAT_SUCCESS, $body, 'Added node '.$result['node']->nid);
364      }
365    }

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.2

  ViewVC Help
Powered by ViewVC 1.1.2