Advertisement
Shaun_B

view.html.php file for Joomla component: My Pictures v1.0.1

Feb 5th, 2013
402
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.88 KB | None | 0 0
  1. <?php
  2. /**
  3.  * @version             $Id: view.html.php 74 2013-02-05 15:00:00Z sbebbers $
  4.  * @package             My Pictures v1.0.1
  5.  * @subpackage          Components
  6.  * @copyright           Copyright (C) 2013 Metapps Ltd
  7.  * @author              Shaun Bebbington
  8.  * @link                http://www.metaps.co.uk
  9.  * @license             License GNU General Public License version 2 or later
  10.  *                      Use at your own risk - no warranty implied or given
  11.  * @note                I wanted a quick and dirty solution to test the logic,
  12.  *          so I put the file-handling stuff here. Might not be the
  13.  *          best idea ever, but it works for now. Will separate the
  14.  *          logic further when I get some time.
  15.  */
  16. defined('_JEXEC') or die('You do not have the correct permissions to view this page.');
  17. jimport('joomla.application.component.view');
  18. class AnotherOneViewAnotherOne extends JView {
  19.     // This will take the image data from the session data:
  20.     public $imgData = array('user' => '',
  21.                 'exts' => '',
  22.                 'fileName' => '',
  23.                 );
  24.     // Used for making directories (default is set in default.php):
  25.     public $dir = '';
  26.     public function display( $tpl = null ) {
  27.         // Gets user data:
  28.         $tempUser = array();
  29.         // Get user details into array buffer
  30.         $tempUser = JFactory::getUser();
  31.         // We only need the username:
  32.         $this->imgData['user'] = $tempUser->username;
  33.         // Now get rid of the user details
  34.         $tempUser = null;
  35.         // Example type and name set (for testing purposes):
  36.         $this->imgData['exts']  = 'jpg';
  37.         $this->imgData['fileName'] = 'test';
  38.         JToolBarHelper::title( 'Another One Administration Page' );
  39.         JToolBarHelper::cancel( 'Anotherone.cancel' );
  40.         parent::display( $tpl );
  41.     }
  42.     public function makeDir( $directory = '' ) {
  43.         // Does some checking to see if the session data is stored correctly:
  44.         if( is_null( $_FILES['file']['name'] ) ) {
  45.             echo '<br />No file chosen<br />';
  46.         }
  47.         if( is_null( $directory ) ) {
  48.             echo '<br />No directory name set - please contact the administrator<br />';
  49.         } else {
  50.             // Sets directory name:
  51.             $this->dir = $directory;
  52.         }
  53.         // Checks if the directory already exsists or not:
  54.         if( file_exists( $this->dir ) ) {
  55.             // Sends file data to the upload function:
  56.             $this->upload( $_FILES );
  57.         } else {
  58.             // So, if not (ie, first-time use), let's make it:
  59.             mkdir( $this->dir, 0777 );
  60.             $this->upload( $_FILES );
  61.         }
  62.     }
  63.     public function upload( $file = array() ) {
  64.         // Checks for the correct file types:
  65.         if(! ( ( $file['file']['type']=='image/png' )   ||
  66.          ( $file['file']['type'] == 'image/jpeg' )  ||
  67.          ( $file['file']['type'] == 'image/pjpeg' ) ||
  68.          ( $file['file']['type'] == 'image/gif' ) ) ) {
  69.             echo '<br />Invalid file type';
  70.         }
  71.         if( ( int ) $file['file']['size'] > 8388608 ) {
  72.             // This is just a test: maximum file size is 8 Megabytes
  73.             echo '<br />File too big</br>';
  74.         } else {
  75.             // Error checknig...
  76.             if( ( int ) $file['file']['error'] > 0) {
  77.                 echo '<br />Error code: ' . $file['file']['error'] . '<br />';
  78.             } else {
  79.                 // Okay, everything seems good, so we may proceed:
  80.                 if( isset( $file['file']['name'] ) ) {
  81.                 $this->imgData['fileName'] = explode( '.', $file['file']['name'] );
  82.                 } else {
  83.                     // Maybe everything isn't so good; it shouldn't get to this stage though as the above
  84.                     // error checking etc. should be pretty bullet-proof:
  85.                     echo '<br />Hmm... this is embarrassing - my code has not picked up the file name.<br />Please try again.';
  86.                 }
  87.                 // Temp variable:
  88.                 $num = count( $this->imgData->exts )-1;
  89.                 $this->imgData->exts = $this->exts[$num];
  90.                 // Generates a filename with the current user (admin) and a 10-character randomly generated
  91.                 // key:
  92.                 $this->imgData['fileName'] =    $this->imgData['user']
  93.                                 . $this->generateKey( 6 )
  94.                                 . '.'
  95.                                 . $this->imgData['exts'];
  96.                 // There isn't much chance of this happening, but if there was the same name already in use then we need to
  97.                 // stop as it'll over-write the previous file:
  98.                 if( file_exists( $this->dir . '/' . $this->imgData['fileName'] ) ) {
  99.                     echo '<br />There seems to be a duplicate file name - which is a rare event<br />'
  100.                         . 'due to the randomly generated key. You should buy a lottery ticket.';
  101.                 } else {
  102.                     $destination = $this->dir . '/' .$this->imgData['fileName'];
  103.                     // Okay, so the file doesn't exsist, let's save it:
  104.                     move_uploaded_file( $file['file']['tmp_name'], $destination );
  105.                     echo '<br />File created: <b>' . $destination . '</b><br />';
  106.                 }
  107.             }
  108.         }
  109.     }
  110.     // Generates random key for the file names of the pictures, like Facebook does:
  111.     public function generateKey( $noOfChars = 10 ) {
  112.         $random = 'qwertyuiopasdfghjklzxcvbnm0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  113.         return substr( str_shuffle( $random ),0 ,$noOfChars );
  114.     }
  115.     public function __destruct() {
  116.         // Releases resources:
  117.         $this->imgData = null;
  118.         $this->dir = null;
  119.     }
  120. }
  121. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement