Source for file filemanager.class.php

Documentation is available at filemanager.class.php

  1. <?php
  2. /**
  3.  * PHPDevShell is a RAD Framework aimed at developing administrative applications.
  4.  *
  5.  * @package PHPDevShell
  6.  * @link http://www.phpdevshell.org
  7.  * @copyright Copyright (C) 2007 Jason Schoeman, All rights reserved.
  8.  * @license GNU/LGPL, see readme/licensed_under_lgpl or http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
  9.  * @author Jason Schoeman, Contact: titan [at] phpdevshell [dot] org.
  10.  *
  11.  *  Copyright notice: See readme/notice
  12.  *  By using PHPDevShell you agree to notice and license, if you dont agree to this notice/license you are not allowed to use PHPDevShell.
  13.  */
  14. /**
  15.  * This class handles file uploads in its simplest form.
  16.  *
  17.  */
  18. {
  19.     /**
  20.      * Should file uploads be logged to the database.
  21.      * Default will load settings from database.
  22.      *
  23.      * @var boolean 
  24.      */
  25.     public $log_uploads = 'default';
  26.     /**
  27.      * The default upload relative path for files.
  28.      * Default will load settings from database.
  29.      * Example : myupload
  30.      *
  31.      * @var string 
  32.      */
  33.     public $default_upload_directory = 'default';
  34.     /**
  35.      * Set permission for newly uploaded files.
  36.      * Default will load settings from database.
  37.      * Example : 0777
  38.      *
  39.      * @var int 
  40.      */
  41.     public $cmod = 'default';
  42.     /**
  43.      * The maximum allowed file upload size.
  44.      * Default will load settings from database.
  45.      * Example : 1000 (1kb)
  46.      *
  47.      * @var int 
  48.      */
  49.     public $max_filesize = 'default';
  50.     /**
  51.      * The maximum allowed image upload size.
  52.      * Default will load settings from database.
  53.      * Example : 1000 (1kb)
  54.      *
  55.      * @var int 
  56.      */
  57.     public $max_imagesize = 'default';
  58.     /**
  59.      * The allowed extentions for uploads.
  60.      * Default will load settings from database.
  61.      * Example : jpg,png,gif
  62.      *
  63.      * @var string 
  64.      */
  65.     public $allowed_ext = 'default';
  66.     /**
  67.      * Should thumbnails be created on image uploads.
  68.      * Default will load settings from database.
  69.      *
  70.      * @var boolean 
  71.      */
  72.     public $do_create_thumb = 'default';
  73.     /**
  74.      * Image quality of converted images.
  75.      * Default will load settings from database.
  76.      * Example : 80
  77.      *
  78.      * @var int 
  79.      */
  80.     public $image_quality = 'default';
  81.     /**
  82.      * Typical resize type for thumbnails.
  83.      * Default will load settings from database.
  84.      * Options : resize | resizepercent | cropfromcenter | crop
  85.      *
  86.      * @var string 
  87.      */
  88.     public $thumbnail_type = 'default';
  89.     /**
  90.      * Resize by pixels.
  91.      * Default will load settings from database.
  92.      * [Max Width, Max Height] example (resize image to no wider than 250 pixels wide and 250 pixels high) : 250,250
  93.      *
  94.      * @var string 
  95.      */
  96.     public $resize_thumb_dimension = 'default';
  97.     /**
  98.      * Resize by percentage.
  99.      * Default will load settings from database.
  100.      * [Percentage] example (reduce the image by 50%) : 50
  101.      *
  102.      * @var string 
  103.      */
  104.     public $resize_thumb_percent = 'default';
  105.     /**
  106.      * Crop from center.
  107.      * Default will load settings from database.
  108.      * [Crop Size] example (create a 100x100 pixel crop from the center of an image) : 100
  109.      *
  110.      * @var string 
  111.      */
  112.     public $crop_thumb_fromcenter = 'default';
  113.     /**
  114.      * Crop by measure.
  115.      * Default will load settings from database.
  116.      * [startX, startY, width, height] example (create a 100x50 pixel crop from the top left corner of an image) : 0,0,100,50
  117.      *
  118.      * @var string 
  119.      */
  120.     public $crop_thumb_dimension = 'default';
  121.     /**
  122.      * Add thumb reflections.
  123.      * Default will load settings from database.
  124.      *
  125.      * @var boolean 
  126.      */
  127.     public $do_thumb_reflect = 'default';
  128.     /**
  129.      * Reflection options.
  130.      * Default will load settings from database.
  131.      * Data fields expected are [[Percentage of image], [Reflection percentage], [Transparency of reflection], [Set Border], [Border Color]. Example : 40,40,80,true,#a4a4a4
  132.      *
  133.      * @var string 
  134.      */
  135.     public $thumb_reflect_settings = 'default';
  136.     /**
  137.      * This option will shrink a large image to a smaller then original viewable image. This should be larger then a thumbnail in most cases as this is the image the user can see when clicking on a thumbnail.
  138.      * Default will load settings from database.
  139.      *
  140.      * @var boolean 
  141.      */
  142.     public $do_create_resize_image = 'default';
  143.     /**
  144.      * [Max Width, Max Height] example (resize image to no wider than 500 pixels wide and 500 pixels high) : 500,500
  145.      * Default will load settings from database.
  146.      *
  147.      * @var string 
  148.      */
  149.     public $resize_image_dimension = 'default';
  150.     /**
  151.      * An alias to group the images with in a certain application.
  152.      * Will use active plugin name if left empty.
  153.      *
  154.      * @var string 
  155.      */
  156.     public $alias;
  157.     /**
  158.      * Groups file upload by menu id.
  159.      * Will use active menu id if left empty.
  160.      *
  161.      * @var int 
  162.      */
  163.     public $menu_id;
  164.     /**
  165.      * Groups file uploads to a specific document.
  166.      * Will use active menu id if left empty.
  167.      *
  168.      * @var int 
  169.      */
  170.     public $sub_id;
  171.     /**
  172.      * The group a file batch belongs to.
  173.      * Will use uploaders primary group id if left empty.
  174.      *
  175.      * @var int 
  176.      */
  177.     public $group_id;
  178.     /**
  179.      * Convert pdf to image using convert in Linux.
  180.      * Please note imagemagick needs to be installed on Linux server.
  181.      *
  182.      * @var boolean 
  183.      */
  184.     public $convert_pdf = false;
  185.     /**
  186.      * Choose the density image will be converted to from pdf.
  187.      *
  188.      * @var integer 
  189.      */
  190.     public $convert_density = '300';
  191.     /**
  192.      * Graphics Engine.
  193.      * Supports gd, imagick
  194.      *
  195.      * @var string 
  196.      */
  197.     public $graphics_engine = 'gd';
  198.     /**
  199.      * When converting to pdf with multiple pages, image copies will be stored here.
  200.      *
  201.      * @var array 
  202.      */
  203.     public $image_copies = array();
  204.     /**
  205.      * Holds a record of recently uploaded files.
  206.      *
  207.      * @var array 
  208.      */
  209.     public $upload_history = array();
  210.     /**
  211.      * Text Domain
  212.      *
  213.      * @var string 
  214.      */
  215.     public $d = 'core.lang';
  216.     /**
  217.      * This method simply renames the file to unix standards.
  218.      *
  219.      * @param string $filename 
  220.      * @param string $replace Replace odd characters with what?
  221.      */
  222.     public function safe_file_name ($filename$replace '_')
  223.     {
  224.         global $core;
  225.         return $core->safe_name($filename$replace);
  226.     }
  227.  
  228.     /**
  229.      * Get a files extension.
  230.      *
  231.      * @param string $filename 
  232.      * @return string 
  233.      */
  234.     public function get_file_extension ($filename)
  235.     {
  236.         return end(explode('.'$filename));
  237.     }
  238.  
  239.     /**
  240.      * Get filename without extension.
  241.      *
  242.      * @param string $filename 
  243.      * @return string 
  244.      */
  245.     public function get_file_base ($filename)
  246.     {
  247.         $filename_ explode('.'$filename);
  248.         return $filename_[0];
  249.     }
  250.  
  251.     /**
  252.      * Takes a filename and checks what icon needs to go with it.
  253.      *
  254.      * @param string $extension_lookup 
  255.      */
  256.     public function icon_type ($extension_lookup)
  257.     {
  258.         global $configuration;
  259.         if (empty($extension_lookup)) return false;
  260.         switch ($extension_lookup{
  261.             case 'zip':
  262.                 $icon "<img src=\"templates/{$configuration['template_folder']}/images/icons/archive.gif\" border=\"0\" alt=\"$extension_lookup\" title=\"$extension_lookup\">";
  263.                 break;
  264.             case 'rar':
  265.                 $icon "<img src=\"templates/{$configuration['template_folder']}/images/icons/archive.gif\" border=\"0\" alt=\"$extension_lookup\" title=\"$extension_lookup\">";
  266.                 break;
  267.             case 'jar':
  268.                 $icon "<img src=\"templates/{$configuration['template_folder']}/images/icons/archive.gif\" border=\"0\" alt=\"$extension_lookup\" title=\"$extension_lookup\">";
  269.                 break;
  270.             case 'ace':
  271.                 $icon "<img src=\"templates/{$configuration['template_folder']}/images/icons/archive.gif\" border=\"0\" alt=\"$extension_lookup\" title=\"$extension_lookup\">";
  272.                 break;
  273.             case 'cab':
  274.                 $icon "<img src=\"templates/{$configuration['template_folder']}/images/icons/archive.gif\" border=\"0\" alt=\"$extension_lookup\" title=\"$extension_lookup\">";
  275.                 break;
  276.             case 'bz2':
  277.                 $icon "<img src=\"templates/{$configuration['template_folder']}/images/icons/archive.gif\" border=\"0\" alt=\"$extension_lookup\" title=\"$extension_lookup\">";
  278.                 break;
  279.             case 'tar':
  280.                 $icon "<img src=\"templates/{$configuration['template_folder']}/images/icons/archive.gif\" border=\"0\" alt=\"$extension_lookup\" title=\"$extension_lookup\">";
  281.                 break;
  282.             case 'gzip':
  283.                 $icon "<img src=\"templates/{$configuration['template_folder']}/images/icons/archive.gif\" border=\"0\" alt=\"$extension_lookup\" title=\"$extension_lookup\">";
  284.                 break;
  285.             case 'doc':
  286.                 $icon "<img src=\"templates/{$configuration['template_folder']}/images/icons/doc.gif\" border=\"0\" alt=\"$extension_lookup\" title=\"$extension_lookup\">";
  287.                 break;
  288.             case 'xls':
  289.                 $icon "<img src=\"templates/{$configuration['template_folder']}/images/icons/xls.gif\" border=\"0\" alt=\"$extension_lookup\" title=\"$extension_lookup\">";
  290.                 break;
  291.             case 'eps':
  292.                 $icon "<img src=\"templates/{$configuration['template_folder']}/images/icons/eps.gif\" border=\"0\" alt=\"$extension_lookup\" title=\"$extension_lookup\">";
  293.                 break;
  294.             case 'exe':
  295.                 $icon "<img src=\"templates/{$configuration['template_folder']}/images/icons/exe.gif\" border=\"0\" alt=\"$extension_lookup\" title=\"$extension_lookup\">";
  296.                 break;
  297.             case 'fla':
  298.                 $icon "<img src=\"templates/{$configuration['template_folder']}/images/icons/fla.gif\" border=\"0\" alt=\"$extension_lookup\" title=\"$extension_lookup\">";
  299.                 break;
  300.             case 'gif':
  301.                 $icon "<img src=\"templates/{$configuration['template_folder']}/images/icons/gif.gif\" border=\"0\" alt=\"$extension_lookup\" title=\"$extension_lookup\">";
  302.                 break;
  303.             case 'html':
  304.                 $icon "<img src=\"templates/{$configuration['template_folder']}/images/icons/html.gif\" border=\"0\" alt=\"$extension_lookup\" title=\"$extension_lookup\">";
  305.                 break;
  306.             case 'php':
  307.                 $icon "<img src=\"templates/{$configuration['template_folder']}/images/icons/html.gif\" border=\"0\" alt=\"$extension_lookup\" title=\"$extension_lookup\">";
  308.                 break;
  309.             case 'jpg':
  310.                 $icon "<img src=\"templates/{$configuration['template_folder']}/images/icons/jpg.gif\" border=\"0\" alt=\"$extension_lookup\" title=\"$extension_lookup\">";
  311.                 break;
  312.             case 'png':
  313.                 $icon "<img src=\"templates/{$configuration['template_folder']}/images/icons/jpg.gif\" border=\"0\" alt=\"$extension_lookup\" title=\"$extension_lookup\">";
  314.                 break;
  315.             case 'pdf':
  316.                 $icon "<img src=\"templates/{$configuration['template_folder']}/images/icons/pdf.gif\" border=\"0\" alt=\"$extension_lookup\" title=\"$extension_lookup\">";
  317.                 break;
  318.             case 'psd':
  319.                 $icon "<img src=\"templates/{$configuration['template_folder']}/images/icons/psd.gif\" border=\"0\" alt=\"$extension_lookup\" title=\"$extension_lookup\">";
  320.                 break;
  321.             case 'sig':
  322.                 $icon "<img src=\"templates/{$configuration['template_folder']}/images/icons/sig.gif\" border=\"0\" alt=\"$extension_lookup\" title=\"$extension_lookup\">";
  323.                 break;
  324.             case 'swf':
  325.                 $icon "<img src=\"templates/{$configuration['template_folder']}/images/icons/swf.gif\" border=\"0\" alt=\"$extension_lookup\" title=\"$extension_lookup\">";
  326.                 break;
  327.             case 'mpg':
  328.                 $icon "<img src=\"templates/{$configuration['template_folder']}/images/icons/video2.gif\" border=\"0\" alt=\"$extension_lookup\" title=\"$extension_lookup\">";
  329.                 break;
  330.             case 'avi':
  331.                 $icon "<img src=\"templates/{$configuration['template_folder']}/images/icons/video2.gif\" border=\"0\" alt=\"$extension_lookup\" title=\"$extension_lookup\">";
  332.                 break;
  333.             case 'wmp':
  334.                 $icon "<img src=\"templates/{$configuration['template_folder']}/images/icons/video.gif\" border=\"0\" alt=\"$extension_lookup\" title=\"$extension_lookup\">";
  335.                 break;
  336.             case 'bin':
  337.                 $icon "<img src=\"templates/{$configuration['template_folder']}/images/icons/bin.gif\" border=\"0\" alt=\"$extension_lookup\" title=\"$extension_lookup\">";
  338.                 break;
  339.             case 'txt':
  340.                 $icon "<img src=\"templates/{$configuration['template_folder']}/images/icons/txt.gif\" border=\"0\" alt=\"$extension_lookup\" title=\"$extension_lookup\">";
  341.                 break;
  342.             default:
  343.                 $icon "<img src=\"templates/{$configuration['template_folder']}/images/icons/unknown.gif\" border=\"0\" alt=\"$extension_lookup\" title=\"$extension_lookup\">";
  344.                 break;
  345.         }
  346.         return $icon;
  347.     }
  348.  
  349.     /**
  350.      * Deletes a filename.
  351.      *
  352.      * @param string $filename 
  353.      */
  354.     public function delete_filename ($filename false)
  355.     {
  356.         if (empty($filename)) {
  357.             if (file_exists($filename)) {
  358.                 if (unlink($filename)) {
  359.                     return true;
  360.                 else {
  361.                     return false;
  362.                 }
  363.             else {
  364.                 return false;
  365.             }
  366.         }
  367.     }
  368.  
  369.     /**
  370.      * Creates directory according to month and year.
  371.      *
  372.      * @param string $directory Writable directory where new directories will be created and files stored.
  373.      * @param integer $cmod 
  374.      */
  375.     public function create_date_directory ($directory false$cmod false)
  376.     {
  377.         $year date('Y');
  378.         $month date('m');
  379.         // Create directory strings.
  380.         $year_directory $directory '/' $year '/';
  381.         $full_directory $directory '/' $year '/' $month '/';
  382.         $thumb_directory $directory '/' $year '/' $month '/thumbs/';
  383.         $resize_directory $directory '/' $year '/' $month '/resize/';
  384.         // Check if year exists.
  385.         if (file_exists($year_directory)) {
  386.             mkdir($year_directory);
  387.             chmod($year_directory$cmod);
  388.             touch($year_directory 'index.html');
  389.         }
  390.         // Check if full folder exists.
  391.         if (file_exists($full_directory)) {
  392.             mkdir($full_directory);
  393.             chmod($full_directory$cmod);
  394.             touch($full_directory 'index.html');
  395.         }
  396.         // Check if thumbs folder exists.
  397.         if (file_exists($thumb_directory)) {
  398.             mkdir($thumb_directory);
  399.             chmod($thumb_directory$cmod);
  400.             touch($thumb_directory 'index.html');
  401.         }
  402.         // Check if thumbs folder exists.
  403.         if (file_exists($resize_directory)) {
  404.             mkdir($resize_directory);
  405.             chmod($resize_directory$cmod);
  406.             touch($resize_directory 'index.html');
  407.         }
  408.         // Return directory.
  409.         return $full_directory;
  410.     }
  411.  
  412.     /**
  413.      * Move file from temp directory to specified directory.
  414.      *
  415.      * @param string $uploaded_filename 
  416.      * @param string $directory 
  417.      * @param string $new_filename 
  418.      */
  419.     public function upload_file ($uploaded_filename$directory$new_filename)
  420.     {
  421.         if (move_uploaded_file($uploaded_filename$directory basename($new_filename))) {
  422.             return true;
  423.         else {
  424.             return false;
  425.         }
  426.     }
  427.  
  428.     /**
  429.      * Creates unique and safe filename.
  430.      *
  431.      */
  432.     public function rename_unique_filename ($filename$extension false)
  433.     {
  434.         if (empty($filename)) {
  435.             return false;
  436.         else {
  437.             if ($extension == false{
  438.                 $extension_ false;
  439.             else {
  440.                 $extension_ '.' $extension;
  441.             }
  442.             $random rand(0999999999);
  443.             $uniq $this->safe_file_name($this->get_file_base($filename)) '_' uniqid($random$extension_;
  444.             return $uniq;
  445.         }
  446.     }
  447.  
  448.     /**
  449.      * Simplifies the whole process of uploading files with type=file.
  450.      *
  451.      * @param string $input_name The name of the <form type="file" name="???" />
  452.      * @param string $file_overwrite_id The id of the log that needs to be overwritten.
  453.      * @param string $file_description a Simple file description for uploaded item.
  454.      *
  455.      * @return string Will return the complete uploaded directory with its filename.
  456.      */
  457.     public function auto_upload ($input_name$file_overwrite_id false$file_description false)
  458.     {
  459.         // Set global variables.
  460.         global $template$lang$db$configuration$core;
  461.         // Start Exception handling.
  462.         try {
  463.             // Get database settings.
  464.             if (empty($this->setting)) {
  465.                 $this->setting $db->get_settings(array('log_uploads' 'default_upload_directory' 'cmod' 'max_filesize' 'max_imagesize' 'allowed_ext' 'do_create_thumb' 'do_create_resize_image' 'image_quality' 'thumbnail_type' 'resize_thumb_dimension' 'resize_image_dimension' 'resize_thumb_percent' 'crop_thumb_fromcenter' 'crop_thumb_dimension' 'do_thumb_reflect' 'thumb_reflect_settings''graphics_engine')'PHPDevShell');
  466.             }
  467.             // Do we have files uploaded?
  468.             if (empty($_FILES[$input_name]['tmp_name']&& empty($_FILES[$input_name]['name'])) {
  469.                 // Gain filenames.
  470.                 $tmp_name $_FILES[$input_name]['tmp_name'];
  471.                 $original_name $_FILES[$input_name]['name'];
  472.                 $uploaded_size $_FILES[$input_name]['size'];
  473.                 $mime_type $_FILES[$input_name]['type'];
  474.                 $extention_type $this->get_file_extension($original_name);
  475.                 // Determine standard file association variables.
  476.                 (empty($this->sub_id)) $sub_id $configuration['m'$sub_id $this->sub_id;
  477.                 (empty($this->menu_id)) $menu_id $configuration['m'$menu_id $this->menu_id;
  478.                 (empty($this->alias)) $alias $core->active_plugin($alias $this->alias;
  479.                 (empty($file_description)) $file_description $original_name false;
  480.                 (empty($this->group_id)) $group_id $configuration['user_group'$group_id $this->group_id;
  481.                 // Determine correct file upload options.
  482.                 // graphics_engine
  483.                 $graphics_engine $this->setting($this->graphics_engine$this->setting['graphics_engine']'gd');
  484.                 define('DEFAULT_THUMBLIB_IMPLEMENTATION'$graphics_engine);
  485.                 // log_uploads
  486.                 $log_uploads $this->setting($this->log_uploads$this->setting['log_uploads']truetrue);
  487.                 // default_upload_directory
  488.                 $default_upload_directory $this->setting($this->default_upload_directory$this->setting['default_upload_directory']'upload');
  489.                 // cmod
  490.                 $cmod $this->setting($this->cmodoctdec($this->setting['cmod'])0644);
  491.                 // max_filesize
  492.                 $max_filesize $this->setting($this->max_filesize$this->setting['max_filesize']false);
  493.                 // max_imagesize
  494.                 $max_imagesize $this->setting($this->max_imagesize$this->setting['max_imagesize']false);
  495.                 // allowed_ext
  496.                 $allowed_ext $this->setting($this->allowed_ext$this->setting['allowed_ext']false);
  497.                 // do_create_thumb
  498.                 $do_create_thumb $this->setting($this->do_create_thumb$this->setting['do_create_thumb']truetrue);
  499.                 // do_create_resize_image
  500.                 $do_create_resize_image $this->setting($this->do_create_resize_image$this->setting['do_create_resize_image']truetrue);
  501.                 // resize_image_dimension
  502.                 $resize_image_dimension $this->setting($this->resize_image_dimension$this->setting['resize_image_dimension']'640,480');
  503.                 $resize_image_dimension_ explode(','$resize_image_dimension);
  504.                 // image_quality
  505.                 $image_quality $this->setting($this->image_quality$this->setting['image_quality']80);
  506.                 // Set mime type.
  507.                 if (stripos($mime_type'image'!== false{
  508.                     $mime_upload 'image';
  509.                     $check_size $max_imagesize;
  510.                 else {
  511.                     $mime_upload 'file';
  512.                     $check_size $max_filesize;
  513.                 }
  514.                 // Setup thumbnail settings.
  515.                 $set_thumb array('jpegQuality' => $image_quality);
  516.                 //////////////////////////////////////////////////////////////
  517.                 // Error checking.
  518.                 if (empty($check_size&& ($uploaded_size $check_size)) {
  519.                     throw new error(sprintf($lang['file_to_large']$original_name$this->display_filesize($uploaded_size)$this->display_filesize($check_size)));
  520.                     $error[0true;
  521.                 }
  522.                 // Check extentions allowed.
  523.                 if (empty($allowed_ext)) {
  524.                     // Check if we have the required extention.
  525.                     $extention_find stripos($allowed_ext$extention_type);
  526.                     // Check if extention is allowed.
  527.                     if ($extention_find === false{
  528.                         throw new error(sprintf($lang['extention_not_allowed']$extention_type$allowed_ext));
  529.                         $error[1true;
  530.                     }
  531.                 }
  532.                 //////////////////////////////////////////////////////////////
  533.                 // Check file size.
  534.                 if (empty($error)) {
  535.                     // Create date directory.
  536.                     $final_upload_path $this->create_date_directory($default_upload_directory$cmod);
  537.                     // Create unique filename.
  538.                     $unique_filename $this->rename_unique_filename($original_name$extention_type);
  539.                     // Move files to correct folder.
  540.                     if ($this->upload_file($tmp_name$final_upload_path$unique_filename)) {
  541.                         // Check if we have a pdf and if we need to convert it.
  542.                         if ($this->convert_pdf == true && $extention_type == 'pdf'{
  543.                             $mime_upload 'image';
  544.                             $pdf_path $final_upload_path $unique_filename;
  545.                             $jpg_file str_replace(".pdf"".jpg""$unique_filename");
  546.                             $jpg_path $final_upload_path $jpg_file;
  547.                             exec("convert -density {$this->convert_density} -resize $resize_image_dimension_[0] -quality $image_quality $pdf_path $jpg_path");
  548.                             $mime_type = 'image/jpg';
  549.                             $extention_type = 'jpg';
  550.                             // Check if system renamed it.
  551.                             if (file_exists($jpg_path)) {
  552.                                 $unique_filename = $jpg_file;
  553.                             } else {
  554.                                 $unique_filename = str_replace(".jpg", "-0.jpg", "$jpg_file");
  555.                             }
  556.                             // Finally delete old pdf.
  557.                             $this->delete_filename($pdf_path);
  558.                         }
  559.                         // Initiate thumbnail.
  560.                         if ($mime_upload == 'image') {
  561.                             // Create thumbnail.
  562.                             $thumbnail_path = $final_upload_path . 'thumbs/' . $unique_filename;
  563.                             // Initiate thumb class.
  564.                             $thumbnail = thumbnail::create($final_upload_path . $unique_filename, $set_thumb);
  565.                             if (! empty($do_create_thumb)) {
  566.                                 // Create thumbnail.
  567.                                 $thumbnail_path = $final_upload_path . 'thumbs/' . $unique_filename;
  568.                                 // Initiate thumb class.
  569.                                 $resizeimg = thumbnail::create($final_upload_path . $unique_filename, $set_thumb);
  570.                                 // thumbnail_type
  571.                                 $thumbnail_type = $this->setting($this->thumbnail_type$this->setting['thumbnail_type']'resize');
  572.                                 // Set preferred settings.
  573.                                 switch ($thumbnail_type{
  574.                                     case 'resize':
  575.                                         // resize_thumb_dimension
  576.                                         $resize_thumb_dimension = $this->setting($this->resize_thumb_dimension$this->setting['resize_thumb_dimension']'150,150');
  577.                                         $resize_thumb_dimension_ explode(','$resize_thumb_dimension);
  578.                                         $thumbnail->resize($resize_thumb_dimension_[0]$resize_thumb_dimension_[1]);
  579.                                         break;
  580.                                     case 'resizepercent':
  581.                                         // resize_thumb_percent
  582.                                         $resize_thumb_percent $this->setting($this->resize_thumb_percent$this->setting['resize_thumb_percent']'40');
  583.                                         $thumbnail->resizePercent($resize_thumb_percent);
  584.                                         break;
  585.                                     case 'cropfromcenter':
  586.                                         // crop_thumb_fromcenter
  587.                                         $crop_thumb_fromcenter $this->setting($this->crop_thumb_fromcenter$this->setting['crop_thumb_fromcenter']'150');
  588.                                         $thumbnail->cropFromCenter($crop_thumb_fromcenter);
  589.                                         break;
  590.                                     case 'crop':
  591.                                         // crop_thumb_dimension
  592.                                         $crop_thumb_dimension $this->setting($this->crop_thumb_dimension$this->setting['crop_thumb_dimension']'0,0,100,50');
  593.                                         $crop_thumb_dimension_ explode(','$crop_thumb_dimension);
  594.                                         $thumbnail->crop($crop_thumb_dimension_[0]$crop_thumb_dimension_[1]$crop_thumb_dimension_[2]$crop_thumb_dimension_[3]);
  595.                                         break;
  596.                                     default:
  597.                                         // resize_thumb_dimension
  598.                                         $resize_thumb_dimension $this->setting($this->resize_thumb_dimension$this->setting['resize_thumb_dimension']'150,150');
  599.                                         $resize_thumb_dimension_ explode(','$resize_thumb_dimension);
  600.                                         $thumbnail->resize($resize_thumb_dimension_[0]$resize_thumb_dimension_[1]);
  601.                                         break;
  602.                                 }
  603.                                 // do_thumb_reflect
  604.                                 $do_thumb_reflect = $this->setting($this->do_thumb_reflect$this->setting['do_thumb_reflect']falsetrue);
  605.                                 // thumb_reflect_settings
  606.                                 if (empty($do_thumb_reflect)) {
  607.                                     // thumb_reflect_settings
  608.                                     $thumb_reflect_settings = $this->setting($this->thumb_reflect_settings$this->setting['thumb_reflect_settings']'40,40,80,false,#fff');
  609.                                     $thumb_reflect_settings_ explode(','$thumb_reflect_settings);
  610.                                     $thumbnail->createReflection($thumb_reflect_settings_[0]$thumb_reflect_settings_[1]$thumb_reflect_settings_[2]$thumb_reflect_settings_[3]$thumb_reflect_settings_[4]);
  611.                                 }
  612.                                 //////////////////////////////////////////////
  613.                                 // Continue saving thumb image well. /////////
  614.                                 //////////////////////////////////////////////
  615.                                 // Finaly save thumbnail!
  616.                                 $thumbnail->save($thumbnail_path);
  617.                                 // Set needed permissions.
  618.                                 chmod($thumbnail_path$cmod);
  619.                             }
  620.                             //////////////////////////////////////////////
  621.                             // Continue saving a resized image as well. //
  622.                             //////////////////////////////////////////////
  623.                             if (! empty($do_create_resize_image)) {
  624.                                 // Create thumbnail.
  625.                                 $resized_view_path = $final_upload_path . 'resize/' . $unique_filename;
  626.                                 // Initiate thumb class.
  627.                                 $resizeimg = thumbnail::create($final_upload_path . $unique_filename, $set_thumb);
  628.                                 // Resize view image if we need it.
  629.                                 $dimension = $resizeimg->getCurrentDimensions();
  630.                                 if ($dimension['width'$resize_image_dimension_[0|| $dimension['height'$resize_image_dimension_[1]$resizeimg->resize($resize_image_dimension_[0]$resize_image_dimension_[1]);
  631.                                 // Save resized image!
  632.                                 $resizeimg->save($resized_view_path);
  633.                                 // Set needed permissions.
  634.                                 chmod($resized_view_path$cmod);
  635.                             }
  636.                         } else {
  637.                             // We dont have an image so we wont have thumbs and resized images.
  638.                             $thumbnail_path = false;
  639.                             $resized_view_path = false;
  640.                         }
  641.                         // Should it be logged?
  642.                         if (! empty($log_uploads)) {
  643.                             // Now that all is good, lets log this entry to the database.
  644.                             $db->new_query("
  645.                                 REPLACE INTO
  646.                                     _db_core_upload_logs (file_id, sub_id, menu_id, alias, original_filename, new_filename, relative_path, thumbnail, resized, extention, mime_type, file_desc, group_id, user_id, date_stored, file_size)
  647.                                 VALUES
  648.                                     ('$file_overwrite_id', '$sub_id', '$menu_id', '$alias', '$original_name', '$unique_filename', '$final_upload_path', '$thumbnail_path', '$resized_view_path', '$extention_type', '$mime_type', '$file_description', '$group_id', '{$configuration['user_id']}', '{$configuration['time']}', '$uploaded_size')
  649.                             ");
  650.                         }
  651.                         // Set needed permissions.
  652.                         chmod($final_upload_path . $unique_filename, $cmod);
  653.                         // Return filename and relative path.
  654.                         return $final_upload_path . $unique_filename;
  655.                     } else {
  656.                         return false;
  657.                     }
  658.                 } else {
  659.                     return false;
  660.                 }
  661.             } else {
  662.                 return false;
  663.             }
  664.         } catch (Exception $e) {
  665.             throw new error($e->getMessage());
  666.         }
  667.     }
  668.  
  669.     /**
  670.      * Convert numeric into file size.
  671.      *
  672.      * @param numeric $filesize
  673.      * @return string
  674.      * @author info@levaravel.com
  675.      */
  676.     public function display_filesize ($filesize)
  677.     {
  678.         // Set global.
  679.         global $lang;
  680.         if (is_numeric($filesize)) {
  681.             $decr = 1024;
  682.             $step = 0;
  683.             $prefix = array($lang['bytes'] , $lang['kb'] , $lang['mb'] , $lang['gb'] , $lang['tb'] , $lang['pb']);
  684.             while (($filesize / $decr) > 0.9) {
  685.                 $filesize = $filesize / $decr;
  686.                 $step ++;
  687.             }
  688.             return round($filesize, 2) . ' ' . $prefix[$step];
  689.         } else {
  690.             return false;
  691.         }
  692.     }
  693.  
  694.     /**
  695.      * Internal check to see what thumbnail what settings to return.
  696.      *
  697.      * @param mixed $preferred_setting First passed through settings.
  698.      * @param mixed $database_setting Database called settings.
  699.      * @param mixed $fail_setting If all else fail use this setting.
  700.      * @param boolean $boolean Is setting a boolean value?
  701.      * @return mixed
  702.      */
  703.     private function setting ($preferred_setting, $database_setting, $fail_setting, $boolean = false)
  704.     {
  705.         if (empty($boolean)) {
  706.             if ($preferred_setting == 'default' && ! empty($database_setting)) {
  707.                 return $database_setting;
  708.             } else if (! empty($preferred_setting)) {
  709.                 return $preferred_setting;
  710.             } else {
  711.                 return $fail_setting;
  712.             }
  713.         } else {
  714.             if ($preferred_setting == 'default' && isset($database_setting)) {
  715.                 return $database_setting;
  716.             } else if (isset($preferred_setting)) {
  717.                 return $preferred_setting;
  718.             } else {
  719.                 return $fail_setting;
  720.             }
  721.         }
  722.     }
  723.  
  724.     /**
  725.      * Build query for extracting stored files.
  726.      *
  727.      * @param integer $file_id Get only a single file with a file_id.
  728.      * @param string $alias Load only files by this alias.
  729.      * @param integer $sub_id Load only files by this sub_id.
  730.      * @param integer $menu_id Load only files by this menu_id.
  731.      * @return string
  732.      */
  733.     private function build_file_query ($file_id = false, $alias = false, $sub_id = false, $menu_id = false)
  734.     {
  735.         // Define.
  736.         $query_grouped = false;
  737.         // Check if we need a single file.
  738.         if (! empty($file_id)) {
  739.             $query_grouped = " WHERE file_id = '$file_id";
  740.         } else {
  741.             // Group WHERE queries first level.
  742.             if (! empty($alias)) {
  743.                 $query_grouped .= " WHERE alias = '$alias";
  744.             } else if (! empty($sub_id)) {
  745.                 $query_grouped .= " WHERE sub_id = '$sub_id";
  746.                 $sub_id_set = true;
  747.             } else if (! empty($menu_id)) {
  748.                 $query_grouped .= " WHERE menu_id = '$menu_id";
  749.                 $menu_id_set = true;
  750.             }
  751.             // Group AND queries second level.
  752.             if (! empty($query_grouped)) {
  753.                 if (! empty($sub_id) && empty($sub_id_set)) {
  754.                     $query_grouped .= " AND sub_id = '$sub_id";
  755.                 }
  756.                 if (! empty($menu_id) && empty($menu_id_set)) {
  757.                     $query_grouped .= " AND menu_id = '$menu_id";
  758.                 }
  759.             } else {
  760.                 // We need no queries here.
  761.                 $query_grouped = false;
  762.             }
  763.         }
  764.         // Return results.
  765.         return $query_grouped;
  766.     }
  767.  
  768.     /**
  769.      * Delete file logs from database.
  770.      *
  771.      * @param integer $file_id Get only a single file with a file_id.
  772.      * @param string $alias Load only files by this alias.
  773.      * @param integer $sub_id Load only files by this sub_id.
  774.      * @param integer $menu_id Load only files by this menu_id.
  775.      * @return boolean Will return true if deleted.
  776.      */
  777.     public function delete_files ($file_id = false, $alias = false, $sub_id = false, $menu_id = false)
  778.     {
  779.         global $db;
  780.         // Load deleted item.
  781.         $files = $this->load_files($file_id, $alias, $sub_id, $menu_id);
  782.         // Define.
  783.         $query_grouped = $this->build_file_query($file_id, $alias, $sub_id, $menu_id);
  784.         // Delete query.
  785.         if ($db->new_query("
  786.             DELETE FROM
  787.                 _db_core_upload_logs
  788.                 $query_grouped
  789.         ")) {
  790.                 // Delete files.
  791.                 foreach ($files as $fa) {
  792.                     // Check if multiple images exists of a pdf conversion left over.
  793.                     if (strstr($fa['download_file'], '-0.jpg')) {
  794.                         $this->return_image_copies($fa['download_file']);
  795.                         // Loop multiple images and list them.
  796.                         if (!empty($this->image_copies)) {
  797.                             foreach ($this->image_copies as $multiple_images_) {
  798.                                 $this->delete_filename($multiple_images_);
  799.                             }
  800.                         }
  801.                     }
  802.                     // Delete Original.
  803.                     if (! empty($fa['download_file'])) $this->delete_filename($fa['download_file']);
  804.                     // Delete Resized.
  805.                     if (! empty($fa['resized'])) $this->delete_filename($fa['resized']);
  806.                     // Delete Thumb.
  807.                     if (! empty($fa['thumbnail'])) $this->delete_filename($fa['thumbnail']);
  808.                 }
  809.                 // Return array.
  810.                 return $files;
  811.         } else {
  812.             return false;
  813.         }
  814.     }
  815.  
  816.     /**
  817.      * Load uploaded files from the log database.
  818.      *
  819.      * @param integer $file_id Get only a single file with a file_id.
  820.      * @param string $alias Load only files by this alias.
  821.      * @param integer $sub_id Load only files by this sub_id.
  822.      * @param integer $menu_id Load only files by this menu_id.
  823.      * @param string $order What the query should be ordered by. allows you to order by any of these column values : file_id, sub_id, menu_id, alias, original_filename, new_filename, relative_path, thumbnail, resized, extention, mime_type, file_desc, group_id, user_id, date_stored
  824.      * @param string $limit how many files should be returned in the array.
  825.      * @return array
  826.      */
  827.     public function load_files ($file_id = false, $alias = false, $sub_id = false, $menu_id = false, $order = 'file_id DESC', $limit = '0,5')
  828.     {
  829.         global $db;
  830.         // Define.
  831.         $query_grouped = $this->build_file_query($file_id, $alias, $sub_id, $menu_id);
  832.         // Check order.
  833.         if (empty($file_id)) {
  834.             // Create order and limit query.
  835.             $order_by = " ORDER BY $order LIMIT $limit";
  836.         } else {
  837.             $order_by = false;
  838.         }
  839.         // Load files from database.
  840.         $load_files_db = $db->new_query("
  841.             SELECT
  842.                 file_id,
  843.                 sub_id,
  844.                 menu_id,
  845.                 alias,
  846.                 original_filename,
  847.                 new_filename,
  848.                 relative_path,
  849.                 thumbnail,
  850.                 resized,
  851.                 extention,
  852.                 mime_type,
  853.                 file_desc,
  854.                 group_id,
  855.                 user_id,
  856.                 date_stored,
  857.                 file_size
  858.             FROM
  859.                 _db_core_upload_logs
  860.                 $query_grouped
  861.                 $order_by
  862.         ");
  863.                 // Loop and gather results.
  864.                 while ($files_array = mysql_fetch_array($load_files_db)) {
  865.                     // Create download filename.
  866.                     $files_array['download_file'] = $files_array['relative_path'] . $files_array['new_filename'];
  867.                     // Create human readable file size.
  868.                     $files_array['format_file_size'] = $this->display_filesize($files_array['file_size']);
  869.                     // Create extention image.
  870.                     $files_array['extention_img'] = $this->icon_type($files_array['extention']);
  871.                     // File array.
  872.                     $final_files_return[$files_array['file_id']] = $files_array;
  873.                     // Clear.
  874.                     unset($files_array);
  875.                 }
  876.                 if (! empty($final_files_return)) {
  877.                     // Return results.
  878.                     return $final_files_return;
  879.                 } else {
  880.                     // No results to return.
  881.                     return array();
  882.                 }
  883.     }
  884.     /**
  885.      * Check if multiple images exists converted from a pdf with multiple pages.
  886.      *
  887.      * @param string $file
  888.      * @param int $nr The current file number in the loop.
  889.      */
  890.     public function return_image_copies ($file, $nr=0)
  891.     {
  892.         // Multiple files should exist, recall and check.
  893.         if (file_exists($file)) {
  894.             $this->image_copies[$nr] = $file;
  895.             $nr_next = $nr + 1;
  896.             $file_next = str_replace("-$nr.jpg", "-$nr_next.jpg", "$file");
  897.             $this->return_image_copies($file_next, $nr_next);
  898.         }
  899.     }
  900.  
  901.     /**
  902.      * Enables the caller to download a file to a local writable directory.
  903.      *
  904.      * @param $url The url to download from
  905.      * @param $copy_to The location to copy to add the name to the file too, example: /var/www/test.zip
  906.      * @return mixed
  907.      */
  908.     public function download_file($url, $copy_to)
  909.     {
  910.         global $template;
  911.         // Check if function exists.
  912.         if (!function_exists('curl_init')) {
  913.             throw new <a href="../PHPDevShell/error.html">error</a>(__('Oops cURL is not installed on your server, I cannot continue.', $this->d));
  914.             return false;
  915.         }
  916.         $err_msg = '';
  917.         $out = fopen($copy_to, 'wb');
  918.         if ($out == false) {
  919.             throw new <a href="../PHPDevShell/error.html">error</a>(sprintf(__('File %s could not be written to %s, please check permission, directories and if file exists.', $this->d), $url, $copy_to));
  920.             return false;
  921.         }
  922.         $ch = curl_init();
  923.         curl_setopt($ch, CURLOPT_FILE, $out);
  924.         curl_setopt($ch, CURLOPT_HEADER, 0);
  925.         curl_setopt($ch, CURLOPT_URL, $url);
  926.         curl_exec($ch);
  927.         if (curl_error($ch)) {
  928.             throw new <a href="../PHPDevShell/error.html">error</a>(sprintf(__('There was a cURL error : %s when I tried to copy to : %s',$this->d), curl_error($ch), $copy_to));
  929.             return false;
  930.         }
  931.         curl_close($ch);
  932.         return $copy_to;
  933.     }
  934.  
  935.     /**
  936.      * Extracts a ZIP archive to the specified extract path
  937.      *
  938.      * @param string $file The ZIP archive to extract (including the path)
  939.      * @param string $extractPath The path to extract the ZIP archive to
  940.      *
  941.      * @return boolean TURE if the ZIP archive is successfully extracted, FALSE if there was an errror
  942.      *
  943.      */
  944.     public function zip_extract($file, $extractPath)
  945.     {
  946.         $zip = new ZipArchive();
  947.         $res = $zip->open($file);
  948.         if ($res === TRUE) {
  949.             $zip->extractTo($extractPath);
  950.             $zip->close();
  951.             return TRUE;
  952.         } else {
  953.             return FALSE;
  954.         }
  955.  
  956.     }
  957.  
  958.     /**
  959.      * Copy file or folder from source to destination, it can do
  960.      * recursive copy as well and is very smart
  961.      * It recursively creates the dest file or directory path if there weren't exists
  962.      * Situtaions :
  963.      * - Src:/home/test/file.txt ,Dst:/home/test/b ,Result:/home/test/b -> If source was file copy file.txt name with b as name to destination
  964.      * - Src:/home/test/file.txt ,Dst:/home/test/b/ ,Result:/home/test/b/file.txt -> If source was file Creates b directory if does not exsits and copy file.txt into it
  965.      * - Src:/home/test ,Dst:/home/ ,Result:/home/test/** -> If source was directory copy test directory and all of its content into dest
  966.      * - Src:/home/test/ ,Dst:/home/ ,Result:/home/**-> if source was direcotry copy its content to dest
  967.      * - Src:/home/test ,Dst:/home/test2 ,Result:/home/test2/** -> if source was directoy copy it and its content to dest with test2 as name
  968.      * - Src:/home/test/ ,Dst:/home/test2 ,Result:->/home/test2/** if source was directoy copy it and its content to dest with test2 as name
  969.      * @todo
  970.      * - Should have rollback technique so it can undo the copy when it wasn't successful
  971.      * - Auto destination technique should be possible to turn off
  972.      * - Supporting callback function
  973.      * - May prevent some issues on shared enviroments : http://us3.php.net/umask
  974.      * @param $source //file or folder
  975.      * @param $dest ///file or folder
  976.      * @param $options //folderPermission,filePermission
  977.      * @return boolean
  978.      */
  979.     public function smartCopy($source, $dest, $options=array('folderPermission'=>0755,'filePermission'=>0644))
  980.     {
  981.         $result=false;
  982.  
  983.         if (is_file($source)) {
  984.             if ($dest[strlen($dest)-1]=='/') {
  985.                 if (!file_exists($dest)) {
  986.                     cmfcDirectory::makeAll($dest,$options['folderPermission'],true);
  987.                 }
  988.                 $__dest=$dest."/".basename($source);
  989.             } else {
  990.                 $__dest=$dest;
  991.             }
  992.             $result=copy($source, $__dest);
  993.             chmod($__dest,$options['filePermission']);
  994.  
  995.         } elseif(is_dir($source)) {
  996.             if ($dest[strlen($dest)-1]=='/') {
  997.                 if ($source[strlen($source)-1]=='/') {
  998.                     //Copy only contents
  999.                 } else {
  1000.                     //Change parent itself and its contents
  1001.                     $dest=$dest.basename($source);
  1002.                     @mkdir($dest);
  1003.                     chmod($dest,$options['filePermission']);
  1004.                 }
  1005.             } else {
  1006.                 if ($source[strlen($source)-1]=='/') {
  1007.                     //Copy parent directory with new name and all its content
  1008.                     @mkdir($dest,$options['folderPermission']);
  1009.                     chmod($dest,$options['filePermission']);
  1010.                 } else {
  1011.                     //Copy parent directory with new name and all its content
  1012.                     @mkdir($dest,$options['folderPermission']);
  1013.                     chmod($dest,$options['filePermission']);
  1014.                 }
  1015.             }
  1016.  
  1017.             $dirHandle=opendir($source);
  1018.             while($file=readdir($dirHandle))
  1019.             {
  1020.                 if($file!="." && $file!="..")
  1021.                 {
  1022.                     if(!is_dir($source."/".$file)) {
  1023.                         $__dest=$dest."/".$file;
  1024.                     } else {
  1025.                         $__dest=$dest."/".$file;
  1026.                     }
  1027.                     //echo "$source/$file ||| $__dest<br />";
  1028.                     $result = $this->smartCopy($source."/".$file, $__dest, $options);
  1029.                 }
  1030.             }
  1031.             closedir($dirHandle);
  1032.  
  1033.         } else {
  1034.             $result=false;
  1035.         }
  1036.         return $result;
  1037.     }
  1038.  
  1039.     /**
  1040.      * Will delete a whole directory recursively (directory muste be writable).
  1041.      *
  1042.      * @param $dirname Directory to delete.
  1043.      * @return boolean.
  1044.      */
  1045.     public function delete_dir($dirname) {
  1046.         if (is_dir($dirname))
  1047.         $dir_handle = opendir($dirname);
  1048.         if (!$dir_handle)
  1049.         return false;
  1050.         while($file = readdir($dir_handle)) {
  1051.             if ($file != "." && $file != "..") {
  1052.                 if (!is_dir($dirname."/".$file))
  1053.                 unlink($dirname."/".$file);
  1054.                 else
  1055.                 $this->delete_dir($dirname.'/'.$file);
  1056.             }
  1057.         }
  1058.         closedir($dir_handle);
  1059.         rmdir($dirname);
  1060.         return true;
  1061.     }
  1062.  
  1063.  
  1064.     /**
  1065.      * Connects to a normal ftp or secure server.
  1066.      * If values left empty system will use PHPDevShell configuration values.
  1067.      *
  1068.      * @param $username
  1069.      * @param $password
  1070.      * @param $host
  1071.      * @param $port
  1072.      * @param $ssl
  1073.      * @param $timeout
  1074.      * @param $chdir
  1075.      * @return resource connection id.
  1076.      */
  1077.     public function establish_ftp ($username='config', $password='config', $host='config', $port='config', $ssl='config', $timeout='config', $chdir='config')
  1078.     {
  1079.         global $db, $template;
  1080.  
  1081.         // Load database settings.
  1082.         $conf = $db->get_settings(array('ftp_enable', 'ftp_username', 'ftp_password', 'ftp_host', 'ftp_port', 'ftp_ssl', 'ftp_timeout', 'ftp_root'));
  1083.  
  1084.         // Load defaults.
  1085.         if (!empty($conf['ftp_enable'])) {
  1086.             if ($username == 'config') $username = $conf['ftp_username'];
  1087.             if ($password == 'config') $password = $conf['ftp_password'];
  1088.             if ($host == 'config') $host = $conf['ftp_host'];
  1089.             if ($port == 'config') $port = $conf['ftp_port'];
  1090.             if ($ssl == 'config') $ssl = $conf['ftp_ssl'];
  1091.             if ($timeout == 'config') $timeout = $conf['ftp_timeout'];
  1092.             if ($chdir == 'config' && ! empty($conf['ftp_root'])) $chdir = $conf['ftp_root'];
  1093.         }
  1094.         // Establish connection.
  1095.         if ($ssl == false) {
  1096.             if (!$resource = ftp_connect($host, $port, $timeout)) {
  1097.                 throw new <a href="../PHPDevShell/error.html">error</a>(sprintf(__('I could not connect to local ftp server %s, user %s.', $this->d), $host, $username));
  1098.                 return false;
  1099.             }
  1100.         } else {
  1101.             if (!$resource = ftp_ssl_connect($host, $port, $timeout)) {
  1102.                 throw new <a href="../PHPDevShell/error.html">error</a>(sprintf(__('I could not connect using SSL to local ftp server %s, user %s.', $this->d), $host, $username));
  1103.                 return false;
  1104.             }
  1105.         }
  1106.         // Log in to server.
  1107.         if (ftp_login($resource, $username, $password)) {
  1108.             // Should we change the directory path.
  1109.             if (! empty($chdir) && $chdir != 'config') {
  1110.                 ftp_chdir($resource, $chdir);
  1111.             }
  1112.  
  1113.             return $resource;
  1114.         } else {
  1115.             throw new <a href="../PHPDevShell/error.html">error</a>(sprintf(__('Incorrect login for local ftp server %s, user %s.', $this->d), $host, $username));
  1116.             return false;
  1117.         }
  1118.     }
  1119.  
  1120.     /**
  1121.      * The following is a fully tested function (based on a previous note) that recursively puts files from a source directory to a destination directory. See http://rufy.com/tech/archives/000026.html for more information.
  1122.      *
  1123.      * NOTE: Use full path name for the destination directory and the destination directory must already exist
  1124.      *
  1125.      * @param resource $conn_id
  1126.      * @param string $src_dir
  1127.      * @param string $dst_dir
  1128.      * @return void
  1129.      */
  1130.     public function ftp_rcopy($conn_id, $src_dir, $dst_dir)
  1131.     {
  1132.         global $template;
  1133.         if ($dir = dir($src_dir)) {
  1134.             while($file = $dir->read()) { // do this for each file in the directory
  1135.                 if ($file != '.' && $file != '..') { // to prevent an infinite loop
  1136.                     if (is_dir($src_dir . '/' . $file)) { // do the following if it is a directory
  1137.                         if (!@ftp_nlist($conn_id, $dst_dir . '/' . $file)) {
  1138.                             ftp_mkdir($conn_id, $dst_dir . '/' . $file); // create directories that do not yet exist
  1139.                             ftp_chmod($conn_id, 0755, $dst_dir . '/' . $file);
  1140.                         }
  1141.                         $this->ftp_rcopy($conn_id, $src_dir . '/' . $file, $dst_dir . '/' . $file); // recursive part
  1142.                         $this->upload_history[] = array('from' => $src_dir . '/' . $file, 'to' => $dst_dir . '/' . $file);
  1143.                     } else {
  1144.                         if (!ftp_put($conn_id, $dst_dir . '/' . $file, $src_dir . '/' . $file, FTP_BINARY)) { // put the files
  1145.                             throw new <a href="../PHPDevShell/error.html">error</a>(sprintf(__('There was a problem copying file %s',$this->d), $dst_dir . '/' . $file));
  1146.                         } else {
  1147.                             $this->upload_history[] = array('from' => $src_dir . '/' . $file, 'to' => $dst_dir . '/' . $file);
  1148.                             ftp_chmod($conn_id, 0644, $dst_dir . '/' . $file);
  1149.                         }
  1150.                     }
  1151.                 }
  1152.             }
  1153.             $dir->close();
  1154.             if (!empty($this->upload_history)) {
  1155.                 return true;
  1156.             } else {
  1157.                 return false;
  1158.             }
  1159.         } else {
  1160.             return false;
  1161.         }
  1162.     }
  1163. }

Documentation generated on Sun, 09 Aug 2009 08:06:20 +0200 by phpDocumentor 1.4.1