Exemple #1
0
 public static function upload($file, $prefix, $etc = array('name' => '', 'location' => '', 'current' => '', 'path' => '', 'max_size' => '', 'max_height' => '', 'max_width' => ''), $delete_old_file = true)
 {
     if (!isset($etc['path'])) {
         $etc['path'] = '';
     }
     if (!isset($file['tmp_name'])) {
         // check if file is empty, local or external url
         if (empty($file)) {
             return isset($etc['current']) ? $etc['current'] : false;
         } else {
             if (filter_var($file, FILTER_VALIDATE_URL)) {
                 $ufile['tmp_name'] = $etc['path'] . TEMP_LOCATION . '/' . basename($file);
                 $ufile['size'] = @file_put_contents($ufile['tmp_name'], file_get_contents($file));
             } else {
                 $ufile['tmp_name'] = $etc['path'] . TEMP_LOCATION . '/' . basename($file);
                 $ufile['size'] = @file_put_contents($ufile['tmp_name'], file_get_contents($etc['path'] . $file));
             }
         }
         $ufile['name'] = basename($ufile['tmp_name']);
         $file = $ufile;
     }
     if (!empty($etc['location'])) {
         $location = $etc['location'];
     } else {
         $location = UPLOAD_IMAGES_LOC;
     }
     if (isset($file['size']) && (int) $file['size'] === 0) {
         @unlink($file['tmp_name']);
         return isset($etc['current']) ? $etc['current'] : false;
     }
     list($width, $height) = getimagesize($file['tmp_name']);
     if (isset($etc['max_size']) && $etc['max_size'] * 1024 < $file['size'] || isset($etc['max_height']) && $etc['max_height'] < $height || isset($etc['max_width']) && $etc['max_width'] < $width) {
         if (!empty($file['tmp_name'])) {
             // delete the temporary file
             @unlink($file['tmp_name']);
         }
         return !empty($etc['current']) ? $etc['current'] : false;
         // It's not a image in standars, size it's too big or filename it's empty. In this case return the current image, if is not set, then return false.
     }
     if (!\site\utils::file_has_extension($file['name'], '.jpg,.jpeg,.png,.gif')) {
         if (!empty($file['tmp_name'])) {
             // delete the temporary file
             @unlink($file['tmp_name']);
         }
         return !empty($etc['current']) ? $etc['current'] : false;
         // This file has not an allowed extension.
     }
     $new_name = !empty($etc['name']) && strtolower($etc['name']) !== 'auto' ? $etc['name'] : uniqid($prefix) . \site\utils::get_extension($file['name']);
     if (file_exists($etc['path'] . $location . '/' . $new_name) || !copy($file['tmp_name'], $etc['path'] . $location . '/' . $new_name)) {
         // delete the temporary file
         @unlink($file['tmp_name']);
         return !empty($etc['current']) ? $etc['current'] : false;
     }
     if (!empty($etc['current']) && $delete_old_file === true) {
         // delete the temporary file
         @unlink($etc['path'] . $etc['current']);
     }
     // delete the temporary file
     @unlink($file['tmp_name']);
     return $location . '/' . $new_name;
 }
Exemple #2
0
 public static function extract_plugin($plugin = '', $location = '')
 {
     global $LANG;
     if (!$GLOBALS['me']->is_admin) {
         return false;
     }
     if (\site\utils::get_extension(basename($plugin)) !== '.zip') {
         throw new Exception($LANG['plugins_only_zip']);
     }
     if (empty($location)) {
         if (!($file = @file_put_contents($temploc = DIR . '/' . TEMP_LOCATION . '/plugin-' . time() . '.zip', file_get_contents($plugin)))) {
             throw new Exception($LANG['plugins_wrongurl']);
         }
         $location = $uplocation = $temploc;
     }
     $zip = new ZipArchive();
     if ($zip->open($location)) {
         $files_map['pfiles'] = $files_map['main_dirs'] = array();
         for ($i = 0; $i < $zip->numFiles; $i++) {
             if (preg_match('/^([^\\/]*)\\/$/', $zip->getNameIndex($i))) {
                 $files_map['main_dirs'][] = $zip->getNameIndex($i);
             } else {
                 $files_map['pfiles'][] = $zip->getNameIndex($i);
             }
         }
         if (count($files_map['main_dirs']) === 0) {
             // delete the temporary file
             if (isset($uplocation)) {
                 @unlink($uplocation);
             }
             throw new Exception($LANG['plugins_err_dirmiss']);
         }
         if (count($files_map['main_dirs']) > 1) {
             // delete the temporary file
             var_dump($files_map);
             if (isset($uplocation)) {
                 @unlink($uplocation);
             }
             throw new Exception($LANG['plugins_err_manydirs']);
         }
         if (is_dir(DIR . '/' . UPDIR . '/' . $files_map['main_dirs'][0])) {
             // delete the temporary file
             if (isset($uplocation)) {
                 @unlink($uplocation);
             }
             throw new Exception(sprintf($LANG['plugins_plugin_exists'], rtrim($files_map['main_dirs'][0], '/')));
         }
         // all files inside plugin
         $pfiles = array();
         foreach ($files_map['pfiles'] as $file) {
             if (preg_match('/^([^\\/]*)\\//', $file)) {
                 $pfiles[] = $file;
             }
         }
         $extract = $zip->extractTo(DIR . '/' . UPDIR, array_merge($files_map['main_dirs'], $pfiles));
         $zip->close();
         if (!$extract) {
             // delete the temporary file
             if (isset($uplocation)) {
                 @unlink($uplocation);
             }
             throw new Exception($LANG['themes_extracting_error']);
         } else {
             /*
             Without errors until installation,
                   Then try to install it.
             */
             require_once 'includes/plugin_installer.php';
             try {
                 $install = (new plugin_installer($files_map['main_dirs'][0]))->install();
                 if (isset($uplocation)) {
                     @unlink($uplocation);
                 }
             } catch (Exception $e) {
                 // delete the temporary files
                 if (isset($uplocation)) {
                     @unlink($uplocation);
                 }
                 \site\files::delete_directory(DIR . '/' . UPDIR . '/' . $files_map['main_dirs'][0]);
                 throw new Exception($e->getMessage());
             }
         }
     } else {
         // delete the temporary file
         if (isset($uplocation)) {
             @unlink($uplocation);
         }
         throw new Exception($LANG['themes_cantunzip']);
     }
     if (isset($uplocation)) {
         @unlink($uplocation);
     }
     return true;
 }