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

Source for file upload_inc.php

Documentation is available at upload_inc.php

  1. <?php
  2. /**
  3.  * @package fisheye
  4.  * @subpackage functions
  5.  */
  6.  
  7.  
  8.  
  9. /**
  10.  * fisheye_handle_upload
  11.  */
  12. function fisheye_handle_upload&$pFiles {
  13.     global $gBitUser$gContent$gBitSystem$fisheyeErrors$fisheyeWarnings$fisheyeSuccess$gFisheyeUploads;
  14.  
  15.     // first of all set the execution time for this process to unlimited
  16.     set_time_limit(0);
  17.  
  18.     $upImages array();
  19.     $upArchives array();
  20.     $upErrors array();
  21.     $upData array();
  22.  
  23.     $i 0;
  24.     usort$pFiles'fisheye_sort_uploads' );
  25.  
  26.     foreacharray_keys$pFiles as $key {
  27.         $pFiles[$key]['type'$gBitSystem->verifyMimeType$pFiles[$key]['tmp_name');
  28.         ifpreg_match'/(^image|pdf|vnd)/i'$pFiles[$key]['type') ) {
  29.             $upImages[$key$pFiles[$key];
  30.             // clone the request data so edit service values are passed into store process
  31.             $upData[$key$_REQUEST;
  32.             // add the form data for each upload
  33.             if!empty$_REQUEST['imagedata'][$i) ) {
  34.                 array_merge$upData[$key]$_REQUEST['imagedata'][$i);
  35.             }
  36.         elseif!empty$pFiles[$key]['tmp_name'&& !empty$pFiles[$key]['name') ) {
  37.             $upArchives[$key$pFiles[$key];
  38.         }
  39.         $i++;
  40.     }
  41.  
  42.     $gallery_additions array();
  43.  
  44.     // No gallery was specified, let's try to find one or create one.
  45.     ifempty$_REQUEST['gallery_additions') ) {
  46.         if$gBitUser->hasPermission'p_fisheye_create' )) {
  47.             $_REQUEST['gallery_additions'arrayfisheye_get_default_gallery_id$gBitUser->mUserId$gBitUser->getDisplayName()."'s Gallery" ) );
  48.         else {
  49.             $gBitSystem->fatalErrortra"You don't have permissions to create a new gallery. Please select an existing one to insert your images to." ));
  50.         }
  51.     }
  52.  
  53.     foreacharray_keys$upArchives as $key {
  54.         $upErrors fisheye_process_archive$upArchives[$key]$gContentTRUE );
  55.     }
  56.  
  57.     foreacharray_keys$upImages as $key {
  58.         // resize original if we the user requests it
  59.         if!empty$_REQUEST['resize') ) {
  60.             $upImages[$key]['resize'$_REQUEST['resize'];
  61.         }
  62.         $upErrors array_merge$upErrorsfisheye_store_upload$upImages[$key]$upData[$key]!empty$_REQUEST['rotate_image')));
  63.     }
  64.  
  65.     if!is_object$gContent || !$gContent->isValid() ) {
  66.         $gContent new FisheyeGallery$_REQUEST['gallery_additions'][0);
  67.         $gContent->load();
  68.     }
  69.  
  70.     if!empty$gFisheyeUploads ) ){
  71.         $_REQUEST['uploaded_objects'&$gFisheyeUploads;
  72.         $gContent->invokeServices"content_post_upload_function"$_REQUEST );
  73.     }
  74.  
  75.     return $upErrors;
  76. }
  77.  
  78.  
  79. /**
  80.  * fisheye_sort_upload
  81.  */
  82. function fisheye_sort_uploads$a$b {
  83.     return strnatcmp$a['name']$b['name');
  84. }
  85.  
  86. /**
  87.  * fisheye_get_default_gallery_id
  88.  */
  89. function fisheye_get_default_gallery_id$pUserId$pNewName {
  90.     global $gBitUser;
  91.     $gal new FisheyeGallery();
  92.     $getHash array'user_id' => $pUserId'max_records' => 1'sort_mode' => 'created_desc''show_empty' => TRUE );
  93.     $upList $gal->getList$getHash );
  94.     if!empty$upList ) ) {
  95.         $ret key$upList );
  96.     else // if( $gBitUser->hasPermission( 'p_fisheye_create' ) ) {
  97.         $galleryHash array'title' => $pNewName );
  98.         if$gal->store$galleryHash ) ) {
  99.             $ret $gal->mGalleryId;
  100.         }
  101. /*
  102.     } else {
  103.         $getHash = array( 'max_records' => 1, 'sort_mode' => 'created_desc' );
  104.         $upList = $gal->getList( $getHash );
  105.         if( !empty( $upList ) ) {
  106.             $ret = key( $upList );
  107.         }
  108. */
  109.     }
  110.  
  111.     global $gContent;
  112.     if$ret && (!is_object$gContent || !$gContent->isValid()) ) {
  113.         $gContent new FisheyeGallery$ret );
  114.         $gContent->load();
  115.     }
  116.     return $ret;
  117. }
  118.  
  119. /**
  120.  * fisheye_store_upload
  121.  */
  122. function fisheye_store_upload&$pFileHash$pImageData array()$pAutoRotate=TRUE {
  123.     global $gBitSystem$gFisheyeUploads;
  124.     $ret array();
  125.  
  126.     // verifyMimeType to make sure we are working with the proper file type assumptions
  127.     $pFileHash['type'$gBitSystem->verifyMimeType($pFileHash['tmp_name']);    
  128.     if!empty$pFileHash && $pFileHash['size'&& is_file$pFileHash['tmp_name'&& fisheye_verify_upload_item(  $pFileHash ) ) {
  129.         // make a copy for each image we need to store
  130.         $image new FisheyeImage();
  131.         // Store/Update the image
  132.         $pImageData['_files_override'array$pFileHash );
  133.         $pImageData['process_storage'STORAGE_IMAGE;
  134.         $pImageData['purge_from_galleries'TRUE;
  135.         // store the image
  136.         if!$image->store$pImageData ) ) {
  137.             $ret $image->mErrors;
  138.         else {
  139.             $image->load();
  140.             // play with image some more if user has requested it
  141.             if$pAutoRotate {
  142.                 $image->rotateImage'auto' );
  143.             }
  144.             $image->addToGalleries$_REQUEST['gallery_additions');
  145.             $gFisheyeUploads[$image;
  146.         }
  147.  
  148.         // when we're using xupload, we need to remove temp files manually
  149.         @unlink$pFileHash['tmp_name');
  150.     }
  151.     return $ret;
  152. }
  153.  
  154. /**
  155.  * Recursively builds a tree where each directory represents a gallery, and files are assumed to be images.
  156.  */
  157. function fisheye_process_archive&$pFileHash&$pParentGallery$pRoot=FALSE {
  158.     global $gBitSystem$gBitUser;
  159.     $errors array();
  160.  
  161.     if( ( $destDir liberty_process_archive$pFileHash ) ) && !empty$_REQUEST['process_archive'|| !$gBitUser->hasPermission'p_fisheye_upload_nonimages' ) ) ) {
  162.         ifempty$pParentGallery && !is_file$pFileHash['tmp_name') ) {
  163.             $pParentGallery new FisheyeGallery();
  164.             $galleryHash array'title' => basename$destDir ) );
  165.             if!$pParentGallery->store$galleryHash ) ) {
  166.                 $errors array_merge$errorsarray_values$pParentGallery->mErrors ) );
  167.             }
  168.             global $gContent;
  169.             $gContent &$pParentGallery;
  170.         }
  171.  
  172.         fisheye_process_directory$destDir$pParentGallery$pRoot );
  173.     else {
  174.         global $gBitUser;
  175.         if$gBitUser->hasPermission'p_fisheye_upload_nonimages' ) ) {
  176.             $errors array_merge$errorsfisheye_store_upload$pFileHash ));
  177.         else {
  178.             $errors['upload'tra'Your upload could not be processed because it was determined to be a non-image and you only have permission to upload images.' );
  179.         }
  180.     }
  181.     return $errors;
  182. }
  183.  
  184. if!function_exists'fisheye_verify_upload_item' ) ) {
  185. // Possible override
  186. function fisheye_verify_upload_item$pScanFile {
  187.     global $gBitUser;
  188.     return $gBitUser->hasPermission'p_fisheye_upload_nonimages' || preg_match'/^video\/*/'$pScanFile['type'|| preg_match'/^image\/*/'$pScanFile['type'|| preg_match'/pdf/i'$pScanFile['type');
  189. }
  190. }
  191.  
  192. /**
  193.  * Recursively builds a tree where each directory represents a gallery, and files are assumed to be images.
  194.  */
  195. function fisheye_process_directory$pDestinationDir&$pParentGallery$pRoot=FALSE {
  196.     global $gBitSystem$gBitUser;
  197.     $errors array();
  198.     if$archiveDir opendir$pDestinationDir ) ) {
  199.         $order 100;
  200.         while$fileName readdir($archiveDir) ) {
  201.             $sortedNames[$fileName;
  202.         }
  203.         sort$sortedNames );
  204.         foreach$sortedNames as $fileName {
  205.             if$fileName == 'Thumbs.db' {
  206.                 unlink"$pDestinationDir/$fileName);
  207.             }
  208.             if!preg_match'/^\./'$fileName && $fileName != 'Thumbs.db' ) ) {
  209.                 $mimeResults $gBitSystem->verifyFileExtension$pDestinationDir.'/'.$fileName );
  210.                 $scanFile array(
  211.                     'type' => $mimeResults[1],
  212.                     'name' => $fileName,
  213.                     'size' => filesize"$pDestinationDir/$fileName),
  214.                     'tmp_name' => "$pDestinationDir/$fileName",
  215.                 );
  216.  
  217.                 if!empty$_REQUEST['resize'&& is_numeric$_REQUEST['resize') ) {
  218.                     $scanFile['max_height'$scanFile['max_width'$_REQUEST['resize'];
  219.                 }
  220.  
  221.                 ifis_dir$pDestinationDir.'/'.$fileName ) ) {
  222.                     if$fileName == '__MACOSX' {
  223.                         // Mac OS resources file
  224.                         unlink_r$pDestinationDir.'/'.$fileName );
  225.                     else {
  226.                         // We found a new Gallery!
  227.                         $newGallery new FisheyeGallery();
  228.                         $galleryHash array'title' => str_replace'_'' '$fileName ) );
  229.                         if$newGallery->store$galleryHash ) ) {
  230.                             if$pRoot {
  231.                                 $newGallery->addToGalleries$_REQUEST['gallery_additions');
  232.                             }
  233.                             ifis_object$pParentGallery ) ) {
  234.                                 $pParentGallery->addItem$newGallery->mContentId$order );
  235.                             }
  236.                             //recurse down in!
  237.                             $errors array_merge$errorsfisheye_process_archive$scanFile$newGallery ) );
  238.                         else {
  239.                             $errors array_merge$errorsarray_values$newGallery->mErrors ) );
  240.                         }
  241.                     }
  242.                 elseifpreg_match'/.+\/*zip*/'$scanFile['type') ) {
  243.                     //recurse down in!
  244.                     $errors array_merge$errorsfisheye_process_archive$scanFile$pParentGallery ) );
  245.                 elseiffisheye_verify_upload_item$scanFile ) ) {
  246.                     $newImage new FisheyeImage();
  247.                     $imageHash array'_files_override' => array$scanFile ) );
  248.                     if$newImage->store$imageHash ) ) {
  249.                         if$pRoot {
  250.                             $newImage->addToGalleries$_REQUEST['gallery_additions');
  251.                         }
  252.                         if!is_object$pParentGallery ) ) {
  253.                             global $gBitUser;
  254.                             $pParentGallery new FisheyeGalleryfisheye_get_default_gallery_id$gBitUser->mUserId$gBitUser->getDisplayName()."'s Gallery" ) );
  255.                             $pParentGallery->load();
  256.                         }
  257.                         $pParentGallery->addItem$newImage->mContentId );
  258.                     else {
  259.                         $errors array_merge$errorsarray_values$newImage->mErrors ) );
  260.                     }
  261.                 elseifis_file$scanFile['tmp_name') ) {
  262.                     // unknown file type, let's be tidy and clean it up
  263.                     unlink$scanFile['tmp_name');
  264.                 }
  265.                 $order += 10;
  266.             }
  267.         }
  268.         if !is_windows() ) {
  269.             unlink_r$pDestinationDir );
  270.         }
  271.     }
  272.     return $errors;
  273. }
  274.  
  275.  
  276. // this function will process a directory and all it's sub directories without
  277. // making any assumptions. hierarchy of sub directories is maintained and
  278. // archives can be processed or simply added to the galleries.
  279. function fisheye_process_ftp_directory$pProcessDir {
  280.     global $gBitSystem$gBitUser;
  281.     ifempty$_REQUEST['gallery_additions') ) {
  282.         $_REQUEST['gallery_additions'array();
  283.     }
  284.  
  285.     $errors array();
  286.     if$archiveDir opendir$pProcessDir ) ) {
  287.         $order 100;
  288.         while$fileName readdir($archiveDir) ) {
  289.             $sortedNames[$fileName;
  290.         }
  291.  
  292.         sort$sortedNames );
  293.         $order 100;
  294.  
  295.         foreach$sortedNames as $fileName {
  296.             if!preg_match'/^\./'$fileName && $fileName != 'Thumbs.db' ) ) {
  297.                 $scanFile array(
  298.                     'type'     => $gBitSystem->lookupMimeTypesubstr$fileNamestrrpos$fileName'.' )  ) ),
  299.                     'name'     => $fileName,
  300.                     'size'     => filesize"$pProcessDir/$fileName),
  301.                     'tmp_name' => "$pProcessDir/$fileName",
  302.                 );
  303.  
  304.                 ifis_dir$pProcessDir.'/'.$fileName ) ) {
  305.                     // create a new gallery from directory
  306.                     $dirGallery new FisheyeGallery();
  307.                     $galleryHash array'title' => str_replace'_'' '$fileName ) );
  308.                     if$dirGallery->store$galleryHash ) ) {
  309.                         $dirGallery->addToGalleries$_REQUEST['gallery_additions');
  310.                         $errors array_merge$errorsfisheye_process_directory$pProcessDir.'/'.$fileName$dirGallery ) );
  311.                     else {
  312.                         $errors array_merge$errorsarray_values$dirGallery->mErrors ) );
  313.                     }
  314.                     unset$dirGallery );
  315.                 else {
  316.                     ifpreg_match'/(^image|pdf)/i'$scanFile['type') ) {
  317.                         // process image
  318.                         $newImage new FisheyeImage();
  319.                         $imageHash array'upload' => $scanFile );
  320.                         if$newImage->store$imageHash ) ) {
  321.                             $newImage->addToGalleries$_REQUEST['gallery_additions');
  322.  
  323.                             // if we have a gallery to add these images to, load one of them
  324.                             if!empty$_REQUEST['gallery_additions'][0&& @!is_object$imageGallery ) ) {
  325.                                 $imageGallery new FisheyeGallery();
  326.                                 $imageGallery->mGalleryId $_REQUEST['gallery_additions'][0];
  327.                                 $imageGallery->load();
  328.                             }
  329.  
  330.                             if@!is_object$imageGallery ) ) {
  331.                                 global $gBitUser;
  332.                                 $galleryHash array'title' => $gBitUser->getDisplayName()."'s Gallery" );
  333.                                 $imageGallery new FisheyeGallery();
  334.                                 if$imageGallery->store$galleryHash ) ) {
  335.                                     $imageGallery->load();
  336.                                 else {
  337.                                     $errors array_merge$errorsarray_values$imageGallery->mErrors ) );
  338.                                 }
  339.                             }
  340.  
  341.                             $imageGallery->addItem$newImage->mContentId );
  342.                         else {
  343.                             $errors array_merge$errorsarray_values$newImage->mErrors ) );
  344.                         }
  345.                     else {
  346.                         // create a new gallery from archive
  347.                         $archiveGallery new FisheyeGallery();
  348.                         $galleryHash array'title' => substr$fileName0str_replace'_'' 'strrpos$fileName'.' ) ) ) ) );
  349.                         if!$archiveGallery->store$galleryHash ) ) {
  350.                             $errors array_merge$errorsarray_values$archiveGallery->mErrors ) );
  351.                         }
  352.  
  353.                         $errors fisheye_process_archive$scanFile$archiveGalleryTRUE );
  354.                         unset$archiveGallery );
  355.                     }
  356.                 }
  357.                 $order += 10;
  358.             }
  359.         }
  360.     }
  361.     return $errors;
  362. }
  363. ?>

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