Exemplo n.º 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;
 }
Exemplo n.º 2
0
 public static function map_of_files_recursive($directory, $allowed_ext = '')
 {
     if (!is_dir($directory)) {
         return false;
     }
     $dir = array();
     foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory)) as $filename) {
         if (\site\utils::file_has_extension($filename, $allowed_ext)) {
             $dir[] = str_replace($directory, '', $filename);
         }
     }
     return $dir;
 }
Exemplo n.º 3
0
 public static function import_products($opt = array())
 {
     global $db;
     if (!ab_to(array('products' => 'import'))) {
         return false;
     }
     $opt = \site\utils::array_map_recursive('trim', $opt);
     if (empty($opt['file']) || !\site\utils::file_has_extension($opt['file']['name'], '.csv')) {
         return false;
     }
     $stmt = $db->stmt_init();
     $stmt->prepare("INSERT INTO " . DB_TABLE_PREFIX . "products (user, store, category, title, link, description, tags, image, price, old_price, currency, start, expiration, lastupdate_by, lastupdate, date) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NOW(), NOW())");
     $cat = !empty($opt['category']) ? $opt['category'] : 0;
     $success = $error = $line = 0;
     if (($handle = fopen($opt['file']['tmp_name'], 'r')) !== false) {
         while (($data = fgetcsv($handle, 3000, ',')) !== false) {
             if ($line === 0 && $opt['omit_first_line']) {
                 $line++;
                 continue;
             }
             /*
             If store URL isn't valid, omit that row.
             */
             if (empty($data[0]) || count($data) < 11) {
                 $error++;
                 continue;
             }
             $stmt2 = $db->stmt_init();
             $stmt2->prepare("SELECT COUNT(*), id, category FROM " . DB_TABLE_PREFIX . "stores WHERE link = ?");
             $stmt2->bind_param("s", $data[10]);
             $stmt2->execute();
             $stmt2->bind_result($count, $store, $store_cat);
             $stmt2->fetch();
             $stmt2->close();
             if ($count === 0) {
                 $error++;
                 continue;
             }
             if ($cat === 0) {
                 $cat = $store_cat;
             }
             $stmt->bind_param("iiisssssddsssi", $GLOBALS['me']->ID, $store, $cat, $data[0], $data[1], $data[2], $data[3], $data[4], $data[5], $data[6], $data[7], $data[8], $data[9], $GLOBALS['me']->ID);
             $execute = $stmt->execute();
             if (!$execute) {
                 $error++;
             } else {
                 $success++;
             }
         }
         fclose($handle);
     }
     @$stmt->close();
     return array($success, $error);
 }