/**
  * Check permissions on CSS file
  * and display the content via HTML_Joom_AdminCssEdit()
  *
  */
 function displayCssEdit()
 {
     // error warning msg for CSS editor
     $msg = '';
     jimport('joomla.filesystem.file');
     $cssfile = $this->cssPath . 'joom_local.css.README';
     $editExistingFile = file_exists($this->localCssFile);
     if ($editExistingFile) {
         $cssfile = $this->localCssFile;
         // test by trying to set permissions:
         Joom_Chmod($cssfile, 0766);
         if (!is_writable($cssfile)) {
             $msg = JText::_('JGA_CSS_WARNING_PERMS');
         }
     } else {
         if (!is_writable($this->cssPath)) {
             $msg = JText::_('JGA_CSS_WARNING_PERMS');
         }
     }
     if (!($content = JFile::read($cssfile))) {
         // output error, overwrite last error (this one is more important)
         $msg = JText::_('JGA_CSS_ERROR_READING') . $cssfile;
     } else {
         $content = htmlspecialchars($content, ENT_QUOTES, 'UTF-8');
     }
     require_once JPATH_COMPONENT . DS . 'includes' . DS . 'html' . DS . 'admin.cssedit.html.php';
     $htmladmincss = new HTML_Joom_AdminCssEdit($content, $this->localCssFile, $editExistingFile, $msg);
 }
 function Joom_Favourites_CreateZip()
 {
     $mainframe =& JFactory::getApplication('site');
     $database =& JFactory::getDBO();
     $user =& JFactory::getUser();
     $config = Joom_getConfig();
     // Kontrollabfrage, ob der Zip-Download erlaubt ist
     if ($config->jg_zipdownload != 1 && ($user->get('id') || $config->jg_usefavouritesforpubliczip != 1)) {
         $mainframe->redirect(JRoute::_($this->showfavourites_url . _JOOM_ITEMID, false), JText::_('JGS_FAV_NOT_ALLOWED'));
     }
     // Einbinden der PclZip-Library
     if (file_exists(JPATH_ADMINISTRATOR . DS . 'includes' . DS . 'pcl' . DS . 'pclzip.lib.php')) {
         require_once JPATH_ADMINISTRATOR . DS . 'includes' . DS . 'pcl' . DS . 'pclzip.lib.php';
     } else {
         $mainframe->redirect(JRoute::_($this->showfavourites_url . _JOOM_ITEMID, false), JText::_('JGS_FAV_ZIPLIBRARY_NOT_FOUND'));
     }
     // Name des Zip-Archivs
     $zipname = 'components/com_joomgallery/joomgallery_' . date('d_m_Y') . '__';
     if ($user->get('id')) {
         $zipname .= $user->get('id') . '_';
     }
     $zipname .= mt_rand(10000, 99999) . '.zip';
     // Erstellen des Zip-Archivs
     $zipfile = new PclZip($zipname);
     if (!is_null($this->piclist)) {
         $picids = explode(',', $this->piclist);
         $files = array();
         foreach ($picids as $picid) {
             $database->setQuery(" SELECT \n                                catid,imgfilename \n                              FROM \n                                #__joomgallery\n                              WHERE \n                                id = '" . $picid . "'\n                            ");
             $row = $database->loadObject();
             $catpath = Joom_getCatPath($row->catid);
             if (file_exists(JPath::clean(JPATH_ROOT . DS . $config->jg_pathoriginalimages . $catpath . $row->imgfilename))) {
                 array_push($files, $config->jg_pathoriginalimages . $catpath . $row->imgfilename);
             } elseif (file_exists(JPath::clean(JPATH_ROOT . DS . $config->jg_pathimages . $catpath . $row->imgfilename))) {
                 array_push($files, $config->jg_pathimages . $catpath . $row->imgfilename);
             }
         }
         $createzip = $zipfile->create($files, PCLZIP_OPT_REMOVE_ALL_PATH);
         if ($createzip == 0) {
             // workaround for servers with wwwwrun problem
             Joom_Chmod(JPATH_ROOT . DS . 'components' . DS . 'com_joomgallery', 0777);
             $createzip = $zipfile->create($files, PCLZIP_OPT_REMOVE_ALL_PATH);
             Joom_Chmod(JPATH_ROOT . DS . 'components' . DS . 'com_joomgallery', 0755);
         }
         if ($user->get('id')) {
             if ($this->user_exists) {
                 $database->setQuery(" SELECT \n                                  zipname \n                                FROM \n                                  #__joomgallery_users\n                                WHERE \n                                  uuserid = '" . $user->get('id') . "'\n                              ");
                 if ($old_zip = $database->loadResult()) {
                     if (file_exists($old_zip)) {
                         jimport('joomla.filesystem.file');
                         JFile::delete($old_zip);
                     }
                 }
                 $database->setQuery(" UPDATE \n                                  #__joomgallery_users\n                                SET \n                                  time = NOW(),zipname = '" . $zipname . "'\n                                WHERE \n                                  uuserid = '" . $user->get('id') . "'\n                              ");
             } else {
                 $database->setQuery(" INSERT INTO \n                                  #__joomgallery_users \n                                    (uuserid,time,zipname)\n                                VALUES\n                                  ('" . $user->get('id') . "', NOW(), '" . $zipname . "')\n                              ");
             }
         } else {
             $database->setQuery(" INSERT INTO \n                                #__joomgallery_users\n                                  (time,zipname)\n                              VALUES\n                                (NOW(),'" . $zipname . "')\n                            ");
         }
         $database->query();
         include_once JPATH_COMPONENT . DS . 'includes' . DS . 'html' . DS . 'joom.favourites.html.php';
         if ($createzip != 0) {
             $zipsize = filesize($zipname);
             if ($zipsize < 1000000) {
                 $zipsize = round($zipsize, -3) / 1000;
                 $zipsize_string = $zipsize . ' KB';
             } else {
                 $zipsize = round($zipsize, -6) / 1000000;
                 $zipsize_string = $zipsize . ' MB';
             }
             HTML_Joom_Favourites::Joom_Favourites_CreateZip_HTML($zipname, $zipsize_string);
         } else {
             HTML_Joom_Favourites::Joom_Favourites_CreateZip_Error_HTML($zipfile);
         }
     } else {
         $mainframe->redirect(JRoute::_($this->showfavourites_url . _JOOM_ITEMID, false), $this->Output('NO_PICTURES'));
     }
 }
 /**
  * JAVA Applet upload
  * @param Kategorie id of destination category
  */
 function Upload_AppletReceive_Backend($catid)
 {
     // If the applet checks for the serverProtocol, it issues a HEAD request
     // -> Simply return an empty doc.
     if ($_SERVER['REQUEST_METHOD'] == 'HEAD') {
         jexit();
     }
     $config = Joom_getConfig();
     $database =& JFactory::getDBO();
     $user =& JFactory::getUser();
     $debugoutput = '';
     //The Applet recognize an error with the text 'JOOMGALLERYUPLOADERROR'
     //and shows them within an JS alert box
     //check common requirements
     //no catid
     if ($catid == 0) {
         jexit('JOOMGALLERYUPLOADERROR ' . JText::_('JGA_JUPLOAD_YOU_MUST_SELECT_CATEGORY'));
     }
     //non common title
     if (!$config->jg_useorigfilename && empty($this->gentitle)) {
         jexit('JOOMGALLERYUPLOADERROR ' . JText::_('JGA_JUPLOAD_PICTURE_MUST_HAVE_TITLE'));
     }
     //Category path
     $catpath = Joom_GetCatPath($catid);
     foreach ($_FILES as $file => $fileArray) {
         //If 'delete originals' chosen in backend and the picture
         //shall be uploaded resized this will be done locally in the applet
         //then only the detail picture will be uploaded
         //therefore adjust path of destination category
         if ($config->jg_delete_original && $config->jg_resizetomaxwidth) {
             $no_original = true;
             $picpath = $config->jg_pathimages;
         } else {
             $no_original = false;
             $picpath = $config->jg_pathoriginalimages;
         }
         $screenshot = $fileArray["tmp_name"];
         $screenshot_name = $fileArray["name"];
         $screenshot_name = Joom_FixFilename($screenshot_name);
         $tag = strtolower(JFile::getExt($screenshot_name));
         //check the possible available memory for picture resizing
         //if not available echo error message and continue with next picture
         if ($this->Upload_CheckMemory($debugoutput, $screenshot, $tag) == false) {
             $this->debug = 1;
             continue;
         }
         //Create new filename
         //if generic filename setted in backend use them
         if ($config->jg_useorigfilename) {
             $screenshot_name = Joom_FixFilename($screenshot_name);
             $newfilename = $this->Upload_GenFilename($screenshot_name, $tag);
         } else {
             $screenshot_name = Joom_FixFilename($this->gentitle);
             $newfilename = $this->Upload_GenFilename($screenshot_name, $tag);
         }
         //Move uploaded picture in destination folder (original or details)
         if (strlen($screenshot) > 0 && $screenshot != 'none') {
             $returnval = JFile::upload($screenshot, JPATH_ROOT . DS . $picpath . $catpath . $newfilename);
             if (!$returnval) {
                 $debugoutput .= JText::_('JGA_WRONG_FILENAME') . ': ' . JPATH_ROOT . DS . $picpath . $catpath . $newfilename . '<br />';
                 $this->debug = 1;
                 continue;
             }
             Joom_Chmod(JPath::clean(JPATH_ROOT . DS . $picpath . $catpath . $newfilename));
             if (!$returnval) {
                 $debugoutput .= JPath::clean(JPATH_ROOT . DS . $picpath . $catpath . $newfilename) . ': ' . JText::_('JGA_CHECK_PERMISSIONS');
                 $this->Upload_Rollback($debugoutput, JPATH_ROOT . DS . $picpath . $catpath . $newfilename, null, null);
                 $this->debug = 1;
                 continue;
             }
             //Create thumbnail
             $returnval = Joom_ResizeImage($debugoutput, JPATH_ROOT . DS . $picpath . $catpath . $newfilename, JPATH_ROOT . DS . $config->jg_paththumbs . $catpath . $newfilename, $config->jg_useforresizedirection, $config->jg_thumbwidth, $config->jg_thumbheight, $config->jg_thumbcreation, $config->jg_thumbquality);
             if (!$returnval) {
                 $debugoutput .= JText::_('JGA_WRONG_FILENAME') . ': ' . JPATH_ROOT . DS . $config->jg_paththumbs . $catpath . $newfilename;
                 $this->Upload_Rollback($debugoutput, JPATH_ROOT . DS . $picpath . $catpath . $newfilename, null, JPATH_ROOT . DS . $config->jg_paththumbs . $catpath . $newfilename);
                 $this->debug = 1;
                 continue;
             }
             $debugoutput .= JText::_('JGA_THUMBNAIL_CREATED') . "\n";
             //evtl. create detail picture
             //not if 'delete originals' and resize setted in backend
             //In this case the applet made the resize and upload the detail picture
             if (!$no_original) {
                 if ($config->jg_resizetomaxwidth && ($this->create_special_gif != 1 || $tag != 'gif' && $tag != 'png')) {
                     $returnval = Joom_ResizeImage($debugoutput, JPATH_ROOT . DS . $picpath . $catpath . $newfilename, JPATH_ROOT . DS . $config->jg_pathimages . $catpath . $newfilename, false, $config->jg_maxwidth, false, $config->jg_thumbcreation, $config->jg_picturequality, true);
                     if (!$returnval) {
                         $debugoutput .= JText::_('JGA_WRONG_FILENAME') . ': ' . JPATH_ROOT . DS . $config->jg_paththumbs . $catpath . $newfilename;
                         continue;
                     }
                     $debugoutput .= JText::_('JGA_RESIZED_TO_MAXWIDTH') . "\n";
                 } else {
                     $returnval = JFile::copy($picpath . $catpath . $newfilename, $config->jg_pathimages . $catpath . $newfilename, JPATH_ROOT);
                     if (!$returnval) {
                         $debugoutput .= JText::_('JGA_PROBLEM_COPYING ') . $config->jg_pathimages . $catpath . $newfilename;
                         $this->Upload_Rollback($debugoutput, JPATH_ROOT . DS . $picpath . $catpath . $newfilename, null, JPATH_ROOT . DS . $config->jg_paththumbs . $catpath . $newfilename);
                         $this->debug = 1;
                         continue;
                     }
                 }
                 $returnval = Joom_Chmod(JPath::clean(JPATH_ROOT . DS . $config->jg_pathimages . $catpath . $newfilename));
                 if (!$returnval) {
                     $debugoutput .= JPATH_ROOT . DS . $config->jg_pathimages . $catpath . $newfilename . ' ' . JText::_('JGA_CHECK_PERMISSIONS');
                     $this->Upload_Rollback($debugoutput, JPATH_ROOT . DS . $picpath . $catpath . $newfilename, JPATH_ROOT . DS . $config->jg_pathimages . $catpath . $newfilename, JPATH_ROOT . DS . $config->jg_paththumbs . $catpath . $newfilename);
                     $this->debug = 1;
                     continue;
                 }
             }
             //Delete original picture only if setted in upload window
             //not if setted in backend
             if ($config->jg_delete_original == 2 && $this->original_delete == 1) {
                 if (JFile::delete(JPATH_ROOT . DS . $config->jg_pathoriginalimages . $catpath . $newfilename)) {
                     $debugoutput .= JText::_('JGA_ORIGINAL_DELETED');
                 } else {
                     $debugoutput .= JText::_('JGA_PROBLEM_DELETING_ORIGINAL') . ': ' . JPath::clean(JPATH_ROOT . DS . $config->jg_pathoriginalimages) . ' ' . JText::_('JGA_CHECK_PERMISSIONS');
                     $this->Upload_Rollback($debugoutput, JPATH_ROOT . DS . $picpath . $catpath . $newfilename, JPATH_ROOT . DS . $config->jg_pathimages . $catpath . $newfilename, JPATH_ROOT . DS . $config->jg_paththumbs . $catpath . $newfilename);
                     $this->debug = 1;
                 }
             }
             //new entry for ordering
             $ordering = $this->Upload_GetOrdering($config->jg_uploadorder, $catid);
             $batchtime = mktime();
             if ($config->jg_useorigfilename) {
                 $fileextensionlength = strlen($tag);
                 $filenamelength = strlen($screenshot_name);
                 $imgname = substr($screenshot_name, -$filenamelength, -$fileextensionlength - 1);
             } else {
                 $imgname = $this->gentitle;
             }
             $query = "INSERT INTO #__joomgallery(id, catid, imgtitle, imgauthor,\n            imgtext, imgdate, imgcounter, imgvotes,\n            imgvotesum, published, imgfilename, imgthumbname,\n            checked_out,owner,approved, ordering)\n            VALUES\n            (NULL, '{$catid}', '{$imgname}', '{$this->photocred}',\n            '{$this->gendesc}', '{$batchtime}', '0', '0',\n            '0', '1', '{$newfilename}', '{$newfilename}',\n            '0', '" . $user->get('id') . "', 1, '{$ordering}')";
             $database->setQuery($query);
             if (!$database->query()) {
                 $debugoutput .= $database->getErrorMsg();
                 $this->Upload_Rollback($debugoutput, JPATH_ROOT . DS . $picpath . $catpath . $newfilename, JPATH_ROOT . DS . $config->jg_pathimages . $catpath . $newfilename, JPATH_ROOT . DS . $config->jg_paththumbs . $catpath . $newfilename);
                 $this->debug = 1;
             }
         } else {
             $debugoutput .= JText::_('JGA_WRONG_FILENAME');
             $this->debug = 1;
         }
     }
     if ($this->debug) {
         echo "\nJOOMGALLERYUPLOADERROR\n";
     } else {
         echo "\nJOOMGALLERYUPLOADSUCCESS\n";
     }
     echo $debugoutput;
     jexit();
 }
/**
 * Resize image with functions from gd/gd2/imagemagick
 *
 * @param string $src_file path to source file
 * @param string $dest_file path to destination file
 * @param string $useforresizedirection resize to width or height ratio
 * @param int $new_width   width to resize
 * @param int $thumbheight height to resize
 * @param int $method      1=gd1 2=gd2 3=im
 * @param int $dest_qual   $config->jg_thumbquality
 * @param bool $max_width    true=resize to maxwidth
 * @return bool true=resize succesful
 */
function Joom_ResizeImage(&$debugoutput, $src_file, $dest_file, $useforresizedirection, $new_width, $thumbheight, $method, $dest_qual, $max_width = false)
{
    $config = Joom_getConfig();
    //Ensure that the pathes are valid and clean
    $src_file = JPath::clean($src_file);
    $dest_file = JPath::clean($dest_file);
    //Doing resize instead of thumbnail, copy original and remove it.
    //@TODO check this extensions if needful
    $imagetype = array(1 => 'GIF', 2 => 'JPG', 3 => 'PNG', 4 => 'SWF', 5 => 'PSD', 6 => 'BMP', 7 => 'TIFF', 8 => 'TIFF', 9 => 'JPC', 10 => 'JP2', 11 => 'JPX', 12 => 'JB2', 13 => 'SWC', 14 => 'IFF');
    $imginfo = getimagesize($src_file);
    if ($imginfo == null) {
        die(JText::_('JG_FILE_NOT_FOUND'));
    }
    $imginfo[2] = $imagetype[$imginfo[2]];
    // GD can only handle JPG & PNG images
    if ($imginfo[2] != 'JPG' && $imginfo[2] != 'PNG' && $imginfo[2] != 'GIF' && ($method == 'gd1' || $method == 'gd2')) {
        die(JText::_('JG_GD_ONLY_JPG_PNG'));
    }
    // height/width
    $srcWidth = $imginfo[0];
    $srcHeight = $imginfo[1];
    if ($max_width) {
        $debugoutput .= JText::_('JG_RESIZE_TO_MAX') . "<br />";
        $ratio = max($srcHeight, $srcWidth) / $new_width;
        //$ratio = $srcWidth / $new_width;
    } else {
        $debugoutput .= JText::_('JG_CREATE_THUMBNAIL_FROM') . " {$imginfo['2']}, {$imginfo['0']} x {$imginfo['1']}...<br />";
        //convert to width ratio
        if ($useforresizedirection) {
            $ratio = $srcWidth / $new_width;
            $testheight = $srcHeight / $ratio;
            //if new height exceeds the setted max. height
            if ($testheight > $thumbheight) {
                $ratio = $srcHeight / $thumbheight;
            }
            //convert to height ratio
        } else {
            $ratio = $srcHeight / $thumbheight;
            $testwidth = $srcWidth / $ratio;
            //if new width exceeds setted max. width
            if ($testwidth > $new_width) {
                $ratio = $srcWidth / $new_width;
            }
        }
    }
    $ratio = max($ratio, 1.0);
    $destWidth = (int) ($srcWidth / $ratio);
    $destHeight = (int) ($srcHeight / $ratio);
    // Method for creation of the resized image
    switch ($method) {
        case 'gd1':
            if (!function_exists('imagecreatefromjpeg')) {
                $debugoutput .= JText::_('JG_GD_LIBARY_NOT_INSTALLED');
                return false;
            }
            if ($imginfo[2] == 'JPG') {
                $src_img = imagecreatefromjpeg($src_file);
            } else {
                if ($imginfo[2] == 'PNG') {
                    $src_img = imagecreatefrompng($src_file);
                } else {
                    $src_img = imagecreatefromgif($src_file);
                }
            }
            if (!$src_img) {
                $ERROR = $lang_errors['invalid_image'];
                return false;
            }
            $dst_img = imagecreate($destWidth, $destHeight);
            imagecopyresized($dst_img, $src_img, 0, 0, 0, 0, $destWidth, (int) $destHeight, $srcWidth, $srcHeight);
            if (!@imagejpeg($dst_img, $dest_file, $dest_qual)) {
                // workaround for servers with wwwrun problem
                $dir = dirname($dest_file);
                Joom_Chmod($dir, 0777);
                imagejpeg($dst_img, $dest_file, $dest_qual);
                Joom_Chmod($dir, 0755);
            }
            imagedestroy($src_img);
            imagedestroy($dst_img);
            break;
        case 'gd2':
            if (!function_exists('imagecreatefromjpeg')) {
                $debugoutput .= JText::_('JG_GD_LIBARY_NOT_INSTALLED');
                return false;
            }
            if (!function_exists('imagecreatetruecolor')) {
                $debugoutput .= JText::_('JG_GD_NO_TRUECOLOR');
                return false;
            }
            if ($imginfo[2] == 'JPG') {
                $src_img = imagecreatefromjpeg($src_file);
            } else {
                if ($imginfo[2] == 'PNG') {
                    $src_img = imagecreatefrompng($src_file);
                } else {
                    $src_img = imagecreatefromgif($src_file);
                }
            }
            if (!$src_img) {
                $ERROR = $lang_errors['invalid_image'];
                return false;
            }
            $dst_img = imagecreatetruecolor($destWidth, $destHeight);
            if ($config->jg_fastgd2thumbcreation == 0) {
                imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $destWidth, (int) $destHeight, $srcWidth, $srcHeight);
            } else {
                Joom_FastImageCopyResampled($dst_img, $src_img, 0, 0, 0, 0, $destWidth, (int) $destHeight, $srcWidth, $srcHeight);
            }
            if (!@imagejpeg($dst_img, $dest_file, $dest_qual)) {
                // workaround for servers with wwwrun problem
                $dir = dirname($dest_file);
                Joom_Chmod($dir, 0777);
                imagejpeg($dst_img, $dest_file, $dest_qual);
                Joom_Chmod($dir, 0755);
            }
            imagedestroy($src_img);
            imagedestroy($dst_img);
            break;
        case 'im':
            $disabled_functions = explode(',', ini_get('disabled_functions'));
            foreach ($disabled_functions as $disabled_function) {
                if (trim($disabled_function) == 'exec') {
                    return false;
                }
            }
            if (!empty($config->jg_impath)) {
                $convert_path = $config->jg_impath . 'convert';
            } else {
                $convert_path = 'convert';
            }
            $commands = ' -resize "' . $destWidth . 'x' . $destHeight . '" -quality "' . $dest_qual . '"  -unsharp "3.5x1.2+1.0+0.10"';
            $convert = $convert_path . ' ' . $commands . ' "' . $src_file . '" "' . $dest_file . '"';
            //echo $convert.'<br />';
            $return_var = null;
            $dummy = null;
            @exec($convert, $dummy, $return_var);
            if ($return_var != 0) {
                // workaround for servers with wwwrun problem
                // TODO: necessary here? probably test required
                $dir = dirname($dest_file);
                Joom_Chmod($dir, 0777);
                @exec($convert, $dummy, $return_var);
                Joom_Chmod($dir, 0755);
                if ($return_var != 0) {
                    return false;
                }
            }
            break;
    }
    // We check that the image is valid
    $imginfo = getimagesize($dest_file);
    if ($imginfo == null) {
        return false;
    } else {
        return true;
    }
}