/**
  * Extract an archive file to a directory.
  *
  * @param   string  $archivename  The name of the archive file
  * @param   string  $extractdir   Directory to unpack into
  *
  * @return  boolean  True for success
  *
  * @since   11.1
  * @throws  InvalidArgumentException
  */
 public static function extract($archivename, $extractdir)
 {
     $untar = false;
     $result = false;
     $ext = File::getExt(strtolower($archivename));
     // Check if a tar is embedded...gzip/bzip2 can just be plain files!
     if (File::getExt(File::stripExt(strtolower($archivename))) == 'tar') {
         $untar = true;
     }
     switch ($ext) {
         case 'zip':
             $adapter = self::getAdapter('zip');
             if ($adapter) {
                 $result = $adapter->extract($archivename, $extractdir);
             }
             break;
         case 'tar':
             $adapter = self::getAdapter('tar');
             if ($adapter) {
                 $result = $adapter->extract($archivename, $extractdir);
             }
             break;
         case 'tgz':
             // This format is a tarball gzip'd
             $untar = true;
         case 'gz':
         case 'gzip':
             // This may just be an individual file (e.g. sql script)
             $adapter = self::getAdapter('gzip');
             if ($adapter) {
                 $config = Factory::getConfig();
                 $tmpfname = $config->get('tmp_path') . '/' . uniqid('gzip');
                 $gzresult = $adapter->extract($archivename, $tmpfname);
                 if ($gzresult instanceof Exception) {
                     @unlink($tmpfname);
                     return false;
                 }
                 if ($untar) {
                     // Try to untar the file
                     $tadapter = self::getAdapter('tar');
                     if ($tadapter) {
                         $result = $tadapter->extract($tmpfname, $extractdir);
                     }
                 } else {
                     $path = Path::clean($extractdir);
                     Folder::create($path);
                     $result = File::copy($tmpfname, $path . '/' . File::stripExt(basename(strtolower($archivename))), null, 1);
                 }
                 @unlink($tmpfname);
             }
             break;
         case 'tbz2':
             // This format is a tarball bzip2'd
             $untar = true;
         case 'bz2':
         case 'bzip2':
             // This may just be an individual file (e.g. sql script)
             $adapter = self::getAdapter('bzip2');
             if ($adapter) {
                 $config = Factory::getConfig();
                 $tmpfname = $config->get('tmp_path') . '/' . uniqid('bzip2');
                 $bzresult = $adapter->extract($archivename, $tmpfname);
                 if ($bzresult instanceof Exception) {
                     @unlink($tmpfname);
                     return false;
                 }
                 if ($untar) {
                     // Try to untar the file
                     $tadapter = self::getAdapter('tar');
                     if ($tadapter) {
                         $result = $tadapter->extract($tmpfname, $extractdir);
                     }
                 } else {
                     $path = Path::clean($extractdir);
                     Folder::create($path);
                     $result = File::copy($tmpfname, $path . '/' . File::stripExt(basename(strtolower($archivename))), null, 1);
                 }
                 @unlink($tmpfname);
             }
             break;
         default:
             throw new InvalidArgumentException('Unknown Archive Type');
     }
     if (!$result || $result instanceof Exception) {
         return false;
     }
     return true;
 }
 /**
  * Add photo
  *
  * @param   string  $file  Filename
  *
  * @return  mixed  Data from Google
  *
  * @since   12.3
  * @throws UnexpectedValueException
  */
 protected function getMIME($file)
 {
     switch (strtolower(File::getExt($file))) {
         case 'bmp':
         case 'bm':
             return 'image/bmp';
         case 'gif':
             return 'image/gif';
         case 'jpg':
         case 'jpeg':
         case 'jpe':
         case 'jif':
         case 'jfif':
         case 'jfi':
             return 'image/jpeg';
         case 'png':
             return 'image/png';
         case '3gp':
             return 'video/3gpp';
         case 'avi':
             return 'video/avi';
         case 'mov':
         case 'moov':
         case 'qt':
             return 'video/quicktime';
         case 'mp4':
         case 'm4a':
         case 'm4p':
         case 'm4b':
         case 'm4r':
         case 'm4v':
             return 'video/mp4';
         case 'mpg':
         case 'mpeg':
         case 'mp1':
         case 'mp2':
         case 'mp3':
         case 'm1v':
         case 'm1a':
         case 'm2a':
         case 'mpa':
         case 'mpv':
             return 'video/mpeg';
         case 'asf':
             return 'video/x-ms-asf';
         case 'wmv':
             return 'video/x-ms-wmv';
         default:
             return false;
     }
 }
 /**
  * Compute the files to be included
  *
  * @param   string   $folder          folder name to search into (images, css, js, ...)
  * @param   string   $file            path to file
  * @param   boolean  $relative        path to file is relative to /media folder
  * @param   boolean  $detect_browser  detect browser to include specific browser files
  * @param   boolean  $detect_debug    detect debug to include compressed files if debug is on
  *
  * @return  array    files to be included
  *
  * @see     JBrowser
  * @since   11.1
  */
 protected static function includeRelativeFiles($folder, $file, $relative, $detect_browser, $detect_debug)
 {
     // If http is present in filename
     if (strpos($file, 'http') === 0) {
         $includes = array($file);
     } else {
         // Extract extension and strip the file
         $strip = File::stripExt($file);
         $ext = File::getExt($file);
         // Prepare array of files
         $includes = array();
         // Detect browser and compute potential files
         if ($detect_browser) {
             $navigator = Browser::getInstance();
             $browser = $navigator->getBrowser();
             $major = $navigator->getMajor();
             $minor = $navigator->getMinor();
             // Try to include files named filename.ext, filename_browser.ext, filename_browser_major.ext, filename_browser_major_minor.ext
             // where major and minor are the browser version names
             $potential = array($strip, $strip . '_' . $browser, $strip . '_' . $browser . '_' . $major, $strip . '_' . $browser . '_' . $major . '_' . $minor);
         } else {
             $potential = array($strip);
         }
         // If relative search in template directory or media directory
         if ($relative) {
             // Get the template
             $app = Factory::getApplication();
             $template = $app->getTemplate();
             // For each potential files
             foreach ($potential as $strip) {
                 $files = array();
                 // Detect debug mode
                 if ($detect_debug && Factory::getConfig()->get('debug')) {
                     /*
                      * Detect if we received a file in the format name.min.ext
                      * If so, strip the .min part out, otherwise append -uncompressed
                      */
                     if (strrpos($strip, '.min', '-4')) {
                         $position = strrpos($strip, '.min', '-4');
                         $filename = str_replace('.min', '.', $strip, $position);
                         $files[] = $filename . $ext;
                     } else {
                         $files[] = $strip . '-uncompressed.' . $ext;
                     }
                 }
                 $files[] = $strip . '.' . $ext;
                 /*
                  * Loop on 1 or 2 files and break on first found.
                  * Add the content of the MD5SUM file located in the same folder to url to ensure cache browser refresh
                  * This MD5SUM file must represent the signature of the folder content
                  */
                 foreach ($files as $file) {
                     // If the file is in the template folder
                     $path = JPATH_THEMES . "/{$template}/{$folder}/{$file}";
                     if (file_exists($path)) {
                         $md5 = dirname($path) . '/MD5SUM';
                         $includes[] = Uri::base(true) . "/templates/{$template}/{$folder}/{$file}" . (file_exists($md5) ? '?' . file_get_contents($md5) : '');
                         break;
                     } else {
                         // If the file contains any /: it can be in an media extension subfolder
                         if (strpos($file, '/')) {
                             // Divide the file extracting the extension as the first part before /
                             list($extension, $file) = explode('/', $file, 2);
                             // If the file yet contains any /: it can be a plugin
                             if (strpos($file, '/')) {
                                 // Divide the file extracting the element as the first part before /
                                 list($element, $file) = explode('/', $file, 2);
                                 // Try to deal with plugins group in the media folder
                                 $path = JPATH_ROOT . "/media/{$extension}/{$element}/{$folder}/{$file}";
                                 if (file_exists($path)) {
                                     $md5 = dirname($path) . '/MD5SUM';
                                     $includes[] = Uri::root(true) . "/media/{$extension}/{$element}/{$folder}/{$file}" . (file_exists($md5) ? '?' . file_get_contents($md5) : '');
                                     break;
                                 }
                                 // Try to deal with classical file in a a media subfolder called element
                                 $path = JPATH_ROOT . "/media/{$extension}/{$folder}/{$element}/{$file}";
                                 if (file_exists($path)) {
                                     $md5 = dirname($path) . '/MD5SUM';
                                     $includes[] = Uri::root(true) . "/media/{$extension}/{$folder}/{$element}/{$file}" . (file_exists($md5) ? '?' . file_get_contents($md5) : '');
                                     break;
                                 }
                                 // Try to deal with system files in the template folder
                                 $path = JPATH_THEMES . "/{$template}/{$folder}/system/{$element}/{$file}";
                                 if (file_exists($path)) {
                                     $md5 = dirname($path) . '/MD5SUM';
                                     $includes[] = Uri::root(true) . "/templates/{$template}/{$folder}/system/{$element}/{$file}" . (file_exists($md5) ? '?' . file_get_contents($md5) : '');
                                     break;
                                 }
                                 // Try to deal with system files in the media folder
                                 $path = JPATH_ROOT . "/media/system/{$folder}/{$element}/{$file}";
                                 if (file_exists($path)) {
                                     $md5 = dirname($path) . '/MD5SUM';
                                     $includes[] = Uri::root(true) . "/media/system/{$folder}/{$element}/{$file}" . (file_exists($md5) ? '?' . file_get_contents($md5) : '');
                                     break;
                                 }
                             } else {
                                 // Try to deals in the extension media folder
                                 $path = JPATH_ROOT . "/media/{$extension}/{$folder}/{$file}";
                                 if (file_exists($path)) {
                                     $md5 = dirname($path) . '/MD5SUM';
                                     $includes[] = Uri::root(true) . "/media/{$extension}/{$folder}/{$file}" . (file_exists($md5) ? '?' . file_get_contents($md5) : '');
                                     break;
                                 }
                                 // Try to deal with system files in the template folder
                                 $path = JPATH_THEMES . "/{$template}/{$folder}/system/{$file}";
                                 if (file_exists($path)) {
                                     $md5 = dirname($path) . '/MD5SUM';
                                     $includes[] = Uri::root(true) . "/templates/{$template}/{$folder}/system/{$file}" . (file_exists($md5) ? '?' . file_get_contents($md5) : '');
                                     break;
                                 }
                                 // Try to deal with system files in the media folder
                                 $path = JPATH_ROOT . "/media/system/{$folder}/{$file}";
                                 if (file_exists($path)) {
                                     $md5 = dirname($path) . '/MD5SUM';
                                     $includes[] = Uri::root(true) . "/media/system/{$folder}/{$file}" . (file_exists($md5) ? '?' . file_get_contents($md5) : '');
                                     break;
                                 }
                             }
                         } else {
                             $path = JPATH_ROOT . "/media/system/{$folder}/{$file}";
                             if (file_exists($path)) {
                                 $md5 = dirname($path) . '/MD5SUM';
                                 $includes[] = Uri::root(true) . "/media/system/{$folder}/{$file}" . (file_exists($md5) ? '?' . file_get_contents($md5) : '');
                                 break;
                             }
                         }
                     }
                 }
             }
         } else {
             foreach ($potential as $strip) {
                 $files = array();
                 // Detect debug mode
                 if ($detect_debug && Factory::getConfig()->get('debug')) {
                     $files[] = $strip . '-uncompressed.' . $ext;
                 }
                 $files[] = $strip . '.' . $ext;
                 /*
                  * Loop on 1 or 2 files and break on first found.
                  * Add the content of the MD5SUM file located in the same folder to url to ensure cache browser refresh
                  * This MD5SUM file must represent the signature of the folder content
                  */
                 foreach ($files as $file) {
                     $path = JPATH_ROOT . "/{$file}";
                     if (file_exists($path)) {
                         $md5 = dirname($path) . '/MD5SUM';
                         $includes[] = Uri::root(true) . "/{$file}" . (file_exists($md5) ? '?' . file_get_contents($md5) : '');
                         break;
                     }
                 }
             }
         }
     }
     return $includes;
 }