fisheye
[ class tree: fisheye ] [ index: fisheye ] [ all elements ]

Source for file FisheyeRemote.php

Documentation is available at FisheyeRemote.php

  1. <?php
  2. /**
  3.  * Gallery2 Remote support for fisheye
  4.  *
  5.  * @package  fisheye
  6.  * @version  $Header$
  7.  * @author   spider <spider@steelsun.com>
  8.  * @author   tylerbello <tylerbello@gmail.com>
  9.  */
  10.  
  11. // +----------------------------------------------------------------------+
  12. // | Copyright (c) 2004, bitweaver.org
  13. // +----------------------------------------------------------------------+
  14. // | All Rights Reserved. See below for details and a complete list of authors.
  15. // | Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See http://www.gnu.org/copyleft/lesser.html for details
  16. // |
  17. // | For comments, please use phpdocu.sourceforge.net documentation standards!!!
  18. // | -> see http://phpdocu.sourceforge.net/
  19. // +----------------------------------------------------------------------+
  20. // | Authors: spider <spider@steelsun.com>
  21. // +----------------------------------------------------------------------+
  22.  
  23. define( 'FEG2REMOTE_SUCCESS', 0 );
  24.  
  25. define( 'FEG2REMOTE_PROTOCOL_MAJOR_VERSION_INVALID', 101 );
  26. define( 'FEG2REMOTE_PROTOCOL_MINOR_VERSION_INVALID', 102 );
  27. define( 'FEG2REMOTE_PROTOCOL_VERSION_FORMAT_INVALID', 103 );
  28. define( 'FEG2REMOTE_PROTOCOL_VERSION_MISSING', 104 );
  29.  
  30. define( 'FEG2REMOTE_PASSWORD_WRONG', 201 );
  31. define( 'FEG2REMOTE_LOGIN_MISSING', 202 );
  32.  
  33. define( 'FEG2REMOTE_UNKNOWN_COMMAND', 301 );
  34. define( 'FEG2REMOTE_MISSING_ARGUMENTS', 302 );
  35.  
  36. define( 'FEG2REMOTE_NO_ADD_PERMISSION', 401 );
  37. define( 'FEG2REMOTE_NO_FILENAME', 402 );
  38. define( 'FEG2REMOTE_UPLOAD_PHOTO_FAIL', 403 );
  39. define( 'FEG2REMOTE_NO_WRITE_PERMISSION', 404 );
  40. define( 'FEG2REMOTE_NO_VIEW PERMISSION', 405 );
  41.  
  42. define( 'FEG2REMOTE_NO_CREATE_ALBUM_PERMISSION', 501 );
  43. define( 'FEG2REMOTE_CREATE_ALBUM_FAILED', 502 );
  44. define( 'FEG2REMOTE_MOVE_ALBUM_FAILED', 503 );
  45. define( 'FEG2REMOTE_ROTATE_IMAGE_FAILED', 504 );
  46.  
  47. /**
  48.  * @package  fisheye
  49.  */
  50. class FisheyeRemote {
  51.  
  52.     var $mResponse = array();
  53.  
  54.     var $mSubGalIdx = 1;
  55.  
  56.     function getApiVersion() {
  57.         return '2.14';
  58.     }
  59.  
  60.  
  61.     // separate out pPostData and pParamhash data since some plugins can populate _POST['g2_form'] and _GET['g2_form'] differently.
  62.     // weird but true. ubermind is an example
  63.     function processRequest( $pGetData, $pPostData ) {
  64.         $pData = array_merge($pGetData, $pPostData); //Some programs (galleryexport) pass both post and get...and the cmd can be in either get or post
  65.  
  66.         if(!empty($pData)){
  67.             switch ($pData['cmd']) {
  68.                 case 'login':
  69.                     $response = $this->cmdLogin( $pPostData );
  70.                     break;
  71.  
  72.                 case 'fetch-albums':
  73.                     $response = $this->cmdFetchAlbums( $pPostData );
  74.                     break;
  75.  
  76.                 case 'fetch-albums-prune':
  77.                     $response = $this->cmdFetchAlbums( $pPostData );
  78.                     break;
  79.  
  80.                 case 'add-item':
  81.                     $response = $this->cmdAddItem( $pPostData );
  82.                     break;
  83.  
  84.                 case 'album-properties':
  85.                     // not implemented yet
  86.                     break;
  87.  
  88.                 case 'new-album':
  89.                     $response = $this->cmdNewAlbum( $pPostData );
  90.                     break;
  91.  
  92.                 case 'fetch-album-images':
  93.                     // not implemented yet
  94.                     break;
  95.  
  96.                 case 'move-album':
  97.                     // not implemented yet
  98.                     break;
  99.  
  100.                 case 'increment-view-count':
  101.                     // not implemented yet
  102.                     break;
  103.  
  104.                 case 'image-properties':
  105.                     // not implemented yet
  106.                     break;
  107.  
  108.                 case 'no-op':
  109.                     $response = $this->cmdNoOp( $pPostData );
  110.                     // not implemented yet
  111.                     break;
  112.  
  113.                 default:
  114.                     $response = $this->createResponse( FEG2REMOTE_UNKNOWN_COMMAND, "Command unknown: ".$pGetData['cmd'] );
  115.                     break;
  116.             }
  117.         } else {
  118.             $response = $this->createResponse( FEG2REMOTE_UNKNOWN_COMMAND, "No command received." );
  119.         }
  120.  
  121.         if( !empty( $response ) ) {
  122.             print $this->sendResponse( $response );
  123.         }
  124.     }
  125.  
  126.  
  127.     function cmdNoOp( $pParamHash ) {
  128.         global $gBitUser;
  129.     
  130.         $response = $this->createResponse( FEG2REMOTE_SUCCESS, 'No-op successful' );
  131.         return $response;
  132.     }
  133.  
  134.     function cmdLogin( $pParamHash ) {
  135.         global $gBitUser, $gBitSystem;
  136.         $url = $gBitUser->login( $pParamHash['uname'], $pParamHash['password'] );
  137.         if( $gBitUser->isRegistered() ) {
  138.             /*
  139.             $cookieTime =  ( int )( time() + $gBitSystem->getConfig( 'users_remember_time', 86400 ));
  140.             $cookiePath = $gBitSystem->getConfig( 'cookie_path', BIT_ROOT_URL );
  141.             $cookieDomain = $gBitSystem->getConfig( 'cookie_domain', "" );
  142.             setcookie( 'GALLERYSID', session_id(), $cookieTime, $cookiePath, $cookieDomain );
  143.             */
  144.             $response = $this->createResponse( FEG2REMOTE_SUCCESS, 'Login successful.', array( 'server_version' => $this->getApiVersion() ) );
  145.         } else {
  146.             $response = $this->createResponse( FEG2REMOTE_PASSWORD_WRONG, 'Invalid username or password' );
  147.         }
  148.         return $response;
  149.     }
  150.  
  151.     // Recursively traverses a multi-dimensional array of galleries
  152.     function traverseGalleries( &$pGalHash, &$pResponse ) {
  153.         global $gBitUser;
  154.  
  155.         // Albums don't like being 0 indexed 
  156.         $this->mSubGalIdx = 0;
  157.  
  158.         // the lightroom client is dumb, and can only handle one 0 level parent
  159.         if( stripos( $_SERVER['HTTP_USER_AGENT'], 'lightroom' ) !== FALSE ) {
  160.             $this->mSubGalIdx++;
  161.             $pResponse['album.parent.' . $this->mSubGalIdx] = 0;
  162.             $pResponse['album.name.' . $this->mSubGalIdx] = 1;
  163.             $pResponse['album.title.' . $this->mSubGalIdx] = tra( 'Select a Gallery' );
  164.             $pResponse['album.perms.add.' . $this->mSubGalIdx] = 'false';
  165.             $pResponse['album.perms.write.' . $this->mSubGalIdx] = 'false';
  166.             $pResponse['album.perms.del_alb.' . $this->mSubGalIdx] = 'false';
  167.             $pResponse['album.perms.create_sub.' . $this->mSubGalIdx] = 'true';
  168.         }
  169.         
  170.         return $this->traverseSubGalleries( $pGalHash, $pResponse, 1 );
  171.     }
  172.  
  173.     /**
  174.     * Function that returns link to display a piece of content
  175.     * @param $pGalHash branch of gallery information from FisheyeGallery::getTree
  176.     * @param $pResponse aggregate string containing response array
  177.     * @param $pParentRandom depth of pGalHash - this is used to non-definitively uniquify album.parent and album.name entries
  178.     * @return the url to display the gallery.
  179.     */
  180.     function traverseSubGalleries( &$pGalHash, &$pResponse, $pParentRandom ) {
  181.         global $gBitUser;
  182.         foreach( $pGalHash as $key=>$gallery) { 
  183.             $this->mSubGalIdx++;
  184.  
  185.             // Any number greater than 2 digits crashes iPhoto2Gallery
  186.             $randomizer = str_pad( rand( 1, 99 ), 2, '0' );
  187.             if($gallery['content']['level'] != 0){
  188.                 // Fisheye allows directories to below to multiple parents - not in gallery. This confuses some clients
  189.                 // We pad with a random number for uniqueness
  190.                 $pResponse['album.parent.' . $this->mSubGalIdx] = $gallery['content']['cb_gallery_content_id'].$pParentRandom;
  191.             } else {
  192.                 // the lightroom client is dumb, and can only handle one 0 level parent
  193.                 if( stripos( $_SERVER['HTTP_USER_AGENT'], 'lightroom' ) !== FALSE ) {
  194.                     $pResponse['album.parent.' . $this->mSubGalIdx] = 1;
  195.                 } else {
  196.                     $pResponse['album.parent.' . $this->mSubGalIdx] = 0;
  197.                 }
  198.             }
  199.             // append pParentRandom to make .name probably unique since Fisheye can handle one gallery linked to multiple parents
  200.             $pResponse['album.name.' . $this->mSubGalIdx] = $gallery['content']['content_id'].$randomizer;
  201.             $pResponse['album.title.' . $this->mSubGalIdx] = $this->cleanResponseValue( $gallery['content']['title'] );
  202.         
  203.             if( !empty( $gallery['content']['data'] ) ) {    
  204.                 $pResponse['album.summary.' . $this->mSubGalIdx] = $gallery['content']['data'];
  205.                 $pResponse['album.info.extrafields.' . $this->mSubGalIdx] = "Summary";
  206.             }
  207.  
  208.             $pResponse['album.perms.add.' . $this->mSubGalIdx] = 'true';
  209.             $pResponse['album.perms.write.' . $this->mSubGalIdx] = 'true';
  210.             $pResponse['album.perms.del_alb.' . $this->mSubGalIdx] = 'true';
  211.             $pResponse['album.perms.create_sub.' . $this->mSubGalIdx] = 'true';
  212.                 
  213.             if( !empty( $gallery['children'] ) ) { 
  214.                 $this->traverseSubGalleries($gallery['children'],$pResponse, $randomizer );
  215.             }
  216.         }
  217.         $ret = $this->mSubGalIdx;
  218.         return $ret;
  219.     }    
  220.  
  221.     function cmdFetchAlbums( $pParamHash ) {
  222.         require_once( FISHEYE_PKG_PATH.'FisheyeGallery.php' );
  223.         global $gBitUser;
  224.         if( $gBitUser->isRegistered() ) {
  225.             $treeGallery = new FisheyeGallery();
  226.             $listHash['user_id'] = $gBitUser->mUserId;
  227.             if( $galleryList = $treeGallery->getTree( $listHash,  array( 'name' => "gallery_id", 'id' => "gallerylist", 'item_attributes' => array( 'class'=>'listingtitle' ) ) ) ) {
  228.                 $galResponse = array();
  229.                 $galleryCount = $this->traverseGalleries( $galleryList, $galResponse );
  230.                 $galResponse['album_count'] = $galleryCount;
  231.                 $galResponse['can_create_root'] = 'true';
  232.                 $response = $this->createResponse( FEG2REMOTE_SUCCESS, 'Gallery list successful', $galResponse );
  233.             } else {
  234.                 // perhaps we should make at least on gallery at this point?
  235.                 $response = $this->createResponse( FEG2REMOTE_SUCCESS, 'No galleries', array( 'album_count' => 0 ) );
  236.             }
  237.         } else {
  238.             $response = $this->createResponse( FEG2REMOTE_PASSWORD_WRONG, 'Application not logged in' );
  239.         }
  240.         return $response;
  241.     }
  242.  
  243.  
  244.     function cmdAddItem( $pParamHash ) {
  245.         $response = array();
  246.  
  247.         $uploadFile =  (!empty( $_FILES['g2_userfile'] ) ? $_FILES['g2_userfile'] : NULL);
  248.  
  249.         if( empty( $pParamHash['set_albumName'] ) ) {
  250.             $response = $this->createResponse( CREATE_ALBUM_FAILED , 'No gallery specified' );
  251.         } elseif( empty( $uploadFile ) || empty( $uploadFile['size'] ) ) {
  252.             $response = $this->createResponse( FEG2REMOTE_NO_FILENAME, 'No image uploaded' );
  253.         } else {
  254.             $storeHash['title'] = !empty( $pParamHash['force_filename'] ) ? $pParamHash['force_filename'] : NULL;
  255.             $storeHash['summary'] = !empty( $pParamHash['extrafield.Summary'] ) ? $pParamHash['extrafield.Summary'] : NULL;
  256.             $storeHash['edit'] = !empty( $pParamHash['extrafield.Description'] ) ? $pParamHash['extrafield.Description'] : NULL;
  257.  
  258.             require_once (FISHEYE_PKG_PATH.'upload_inc.php');    
  259.         
  260.             $parentGallery = new FisheyeGallery();
  261.             if( $parentGallery = $parentGallery->lookup(array('content_id' => $pParamHash['set_albumName'] ) ) ) {
  262.                 $parentGallery->load();
  263.                 $_REQUEST['gallery_additions'] = array($parentGallery->mGalleryId);
  264.             }
  265.             if( $errors = fisheye_store_upload( $uploadFile , $storeHash ) ){
  266.                  $response = $this->createResponse( FEG2REMOTE_UPLOAD_PHOTO_FAIL, 'Export Failed' );
  267.             } else {
  268.                 $response = $this->createResponse( FEG2REMOTE_SUCCESS, 'Image added', array( 'item_name'=>$uploadFile['name'] ) );
  269.             }
  270.         }
  271.         
  272.         return $response;
  273.     }
  274.  
  275.     function cmdNewAlbum( $pParamHash ) {
  276.         global $gBitUser;
  277.         $response = array();
  278.  
  279.         if( empty( $pParamHash['newAlbumTitle'] ) ) {
  280.             $pParamHash['newAlbumTitle'] = $gBitUser->getTitle()."'s Gallery";
  281.         }
  282.  
  283.         $storeHash['title'] = !empty($pParamHash['newAlbumTitle']) ? $pParamHash['newAlbumTitle'] : '';
  284.         $storeHash['edit']  = !empty($pParamHash['newAlbumDesc'])  ? $pParamHash['newAlbumDesc']  : '';
  285.         $gallery = new FisheyeGallery();    
  286.         $gallery->store( $storeHash );
  287.  
  288.         if($pParamHash['set_albumName']){
  289.             $parentGallery = new FisheyeGallery();
  290.             if( $parentGallery = $parentGallery->lookup(array('content_id' => $pParamHash['set_albumName'] ) ) ) {
  291.                 $parentGallery->load();
  292.                 $gallery->addToGalleries(array($parentGallery->mGalleryId));
  293.             }
  294.         }
  295.  
  296.         $response = $this->createResponse( FEG2REMOTE_SUCCESS, 'Gallery created', array( 'album_name' => $storeHash['title'] ) );
  297.  
  298.         return $response;
  299.     }
  300.  
  301.     function sendResponse( $pResponse ) {
  302.         global $gBitUser;
  303.         print "#__GR2PROTO__\n";
  304. //error_log( "#__GR2PROTO__".' : '.$gBitUser->mUserId );
  305.         foreach ($pResponse as $k => $value) {
  306.             print  "$k=$value\n";
  307. //error_log( "$k=$value" );
  308.         }
  309.         // must be last
  310.         print "auth_token=".$gBitUser->mTicket;
  311. //error_log( "auth_token=".$gBitUser->mTicket );
  312. //error_log( "#__end__" );
  313.     }
  314.  
  315.     
  316.     function createResponse( $pStatus, $pStatusText, $pExtra = NULL ) {
  317.         $ret = array();
  318.         
  319.         // Each response must contain at least the keys: status and status_text. 
  320.         $ret['status'] = $this->cleanResponseValue( $pStatus );
  321.         // translate the text response for i18n
  322.         $ret['status_text'] = $this->cleanResponseValue( tra( $pStatusText ) );
  323.         // tack on any additional responses
  324.         if( !empty( $pExtra ) && is_array( $pExtra ) ) {
  325.             foreach( $pExtra as $k => $value ) {
  326.                 $ret[$this->cleanResponseKey( $k )] = $this->cleanResponseValue( $value );
  327.             }
  328.         }
  329.         return $ret;
  330.     }
  331.  
  332.     /**
  333.      * This will clean up the response value to make sure it is in an acceptable format for the remote client.
  334.      * Gallery apparently is very particular about the manner in which this data is cleaned up, and must be done
  335.      * in this specific order.
  336.      */
  337.     function cleanResponseValue( $pValue ) {
  338.         $pValue = str_replace('\\', '\\\\', $pValue);
  339.         $pValue = str_replace("\r\n", '\n', $pValue);
  340.         $pValue = str_replace(array("\r", "\n", "\t"), array('\n', '\n', '\t'), $pValue);
  341.         $pValue = str_replace(array('#', '!', '='), array('\\#', '\\!', '\\='), $pValue);
  342.         return $pValue;
  343.     }
  344.  
  345.  
  346.     function cleanResponseKey( $pKey ) {
  347.         return str_replace(array('#', '!', '=', ':'), array('\\#', '\\!', '\\=', '\\:'), $pKey);
  348.     }
  349.     
  350.  
  351. }

Documentation generated on Wed, 29 Jul 2015 13:56:35 +0000 by phpDocumentor 1.5.0-lsces