<?php
include "../includes/config.php";
include "../includes/functions.php";
include "../classes/cls_products.php";  
include_once '../includes/class.upload.php';
include_once('../includes/S3.php');
ini_set('max_execution_time',0);
if( ini_get('allow_url_fopen') ) { echo "allow_url_fopen is enabled";}else {echo "allow_url_fopen - is Not Enabled";exit;}

$i = 0;
$dir_dest = "../products";

// save product images 

//Read product images
$queryProductImages = " SELECT product_option_id,product_id,image_small,image_big FROM ".$tableprefix."product_options 
WHERE image_small like '%http%' OR image_big like '%http%' LIMIT 0,100 ";



$productImagesResult = mysql_query($queryProductImages);
while($productImagesRow = mysql_fetch_assoc($productImagesResult)){

    $productOptionId = $productImagesRow['product_option_id'];
    $imageUrl = $productImagesRow['image_big'];
    $productId = $productImagesRow['product_id']; 
    
        if ($productId != '') {

        if (strstr($imageUrl, "http://") || strstr($imageUrl, "https://")) {
            
         
            $filename = basename($imageUrl);
            $file_extn = end(explode(".", $filename));
            $bigImage_noextn = "productbig_" . $productId . "_" . time();
            $bigImageName = $bigImage_noextn . "." . $file_extn;
            /*if (file_exists('../products/' . $bigImageName)) {
                continue;
            }*/
            $image = getimg($imageUrl);

            file_put_contents('../products/' . $bigImageName, $image);

            $handle = new Upload('../products/' . $bigImageName);

            $handle->image_resize = true;
            $handle->image_x = 489;
            $handle->image_y = 328;
            $handle->image_ratio = true;
            $handle->file_new_name_body = $bigImage_noextn;
            $handle->Process($dir_dest);
            $big_image_after_resize = $handle->file_dst_name;
            
            // Save Big Image Thubnail to folder
            $big_image_after_resize_ext  =  end(explode(".", $big_image_after_resize));   
            $big_image_after_no_ext      =  basename($big_image_after_resize,".".$big_image_after_resize_ext);
            // Save Big Image Thubnail to folder
            //$handle = new Upload('../products/' . $bigImageName);
            $big_thumb_file_name = "thumb_" . $big_image_after_no_ext;
            $handle->file_new_name_body = $big_thumb_file_name;
            $handle->image_resize = true;
            $handle->image_x = 82;
            $handle->image_y = 79;
            $handle->image_ratio_y = true;
            $handle->process($dir_dest);
            $bigImageNameThumb = $handle->file_dst_name;
        } else {
            //if image URL not present, update with existing value
            $big_image_after_resize = $imageUrl;
        }
            $imagepath   = "../products/".$big_image_after_resize;
            moveImageToCDN($imagepath,'products/'.$big_image_after_resize);
        
            if ($productImagesRow['image_small'] != '') {

                //if small image URL is present  
                if (strstr($productImagesRow['image_small'], "http://") || strstr($productImagesRow['image_small'], "https://")) {
                $imageUrl = $productImagesRow['image_small'];
                $filename = basename($imageUrl);
                $file_extn = end(explode(".", $filename));
                $smallImage_noextn = "productsmall_" . $productId . "_" . time();
                $smallImageName = $smallImage_noextn . "." . $file_extn;
                   /* if (file_exists('../products/' . $smallImageName)) {
                        continue;
                    }*/
                $image = getimg($imageUrl);
                file_put_contents('../products/' . $smallImageName, $image);
                $handle = new Upload('../products/' . $smallImageName);
                $small_image_file_name = "productsmall_" . $smallImage_noextn;
                $handle->file_new_name_body = $small_image_file_name;
                $handle->image_resize = true;
                $handle->image_x = 387;
                $handle->image_y = 260;
                $handle->image_ratio = true;
                $handle->process($dir_dest);
                $small_image_name_after_upload = $handle->file_dst_name;

                $imagepath             = "../products/".$small_image_name_after_upload;
                moveImageToCDN($imagepath,'products/'.$small_image_name_after_upload);

                } else { 
                     $small_image_name_after_upload = $productImagesRow['image_small'];//if image is already saved
                     $imagepath             = "../products/".$small_image_name_after_upload;
                     moveImageToCDN($imagepath,'products/'.$small_image_name_after_upload);
                  }
            } else {

                  if($bigImage_noextn!=''){
                    //if small image is not present resize big image to small image size
                        $big_img_resize_file_name = "productsmall_" . $bigImage_noextn;
                        $handle->file_new_name_body = $big_img_resize_file_name;
                        $imagepath             = "../products/".$big_img_resize_file_name;
                        moveImageToCDN($imagepath,'products/'.$big_img_resize_file_name);
                        $handle->image_resize = true;
                        $handle->image_x = 387;
                        $handle->image_y = 260;
                        $handle->image_ratio = true;
                        $handle->process($dir_dest);
                        $small_image_name_after_upload = $handle->file_dst_name;
                        $imagepath             = "../products/".$small_image_name_after_upload;
                        moveImageToCDN($imagepath,'products/'.$small_image_name_after_upload);
                }
            }


        //update big image name to replace image URL    
            $updateMoreImagesQuery = "UPDATE ".$tableprefix."product_options SET
            image_small = '".addslashes($small_image_name_after_upload)."',
            image_big = '".addslashes($big_image_after_resize)."'
            WHERE product_option_id = ".$productOptionId;
            
            
            mysql_query($updateMoreImagesQuery); 
            
            //update default option image to products table
            $objOptionVo                                    =    Products::getProduct_Default_Option_Values($productId);
            $objDefaultOptionVo->default_image_small        =   $objOptionVo->image_small;            
            $objDefaultOptionVo->product_id                 =   $productId;
            // Save default option values to product table
            $productinfoUpdate                              =   Products::updateProduct_Default_Option_Image($objDefaultOptionVo);
            $i++;
            
        }else{
             $fileErrorUrls = 'Product ID : '.$productId.'<br>Image Url : '.$imageUrl.'<br><br>';
             $currentProductImageStatus = 'failure';
        }        
    
}


//--------------------------------------------------------------------------------//
// Save more images
$queryMoreImages = " SELECT nproduct_image_id,vimage_name,nimage_productid FROM ".$tableprefix."product_view_images 
WHERE vimage_name like '%http%' LIMIT 0,100";



$moreImagesResult = mysql_query($queryMoreImages);

while($moreImagesRow = mysql_fetch_assoc($moreImagesResult)){

    $productImageId = $moreImagesRow['nproduct_image_id'];
    $imageUrl = $moreImagesRow['vimage_name'];
    $productId = $moreImagesRow['nimage_productid']; 
     
        if($imageUrl!='' && (strstr($imageUrl, "http://") || strstr($imageUrl, "https://"))){
           
            $filename= basename($imageUrl);
            $file_extn              = end(explode(".",$filename));       
            $moreImage_noextn   = "productmore_".$productId."_".time();
            $moreImageName      = $moreImage_noextn.".".$file_extn;
           
            if(file_exists('../products/'.$moreImageName)){continue;} 
            $image = getimg($imageUrl); 
            
            file_put_contents('../products/'.$moreImageName,$image);    
            
            

            $handle = new Upload('../products/'.$moreImageName);        
            
            $handle->image_resize = true;
            $handle->image_x = 489;
            $handle->image_y = 328;
            $handle->image_ratio= true;
            $handle->file_new_name_body = $moreImage_noextn;            
            $handle->Process($dir_dest);
            $moreimage_after_resize = $handle->file_dst_name; 
            $imagepath   = "../products/".$moreimage_after_resize;
            moveImageToCDN($imagepath,'products/'.$moreimage_after_resize);
            
            // Save More Image Thubnail to thumb folder
                $moreimage_after_resize_ext         =  end(explode(".", $moreimage_after_resize));   
                $moreimage_after_resize_no_ext      =  basename($moreimage_after_resize,".".$moreimage_after_resize_ext);
                $more_thumb_file_name           =    "thumb_".$moreimage_after_resize_no_ext;
               
                $handle->file_new_name_body     =    $more_thumb_file_name;
                $handle->image_resize           = true;
                $handle->image_x                = 82;
                $handle->image_y                = 79;
                $handle->image_ratio_y            = true;
                $handle->process($dir_dest);
                $moreImageNameThumb = $handle->file_dst_name;    
                $imagepath   = "../products/".$moreImageNameThumb;
                moveImageToCDN($imagepath,'products/'.$moreImageNameThumb);
              
            //update big image name to replace image URL    
            $updateMoreImagesQuery = "UPDATE ".$tableprefix."product_view_images SET
            vimage_name = '".addslashes($moreimage_after_resize)."'
            WHERE nproduct_image_id = ".$productImageId;

            mysql_query($updateMoreImagesQuery);           

            $i++;
        }else{
             
             $fileErrorUrls = 'Product ID : '.$productId.'<br>Image Url : '.$imageUrl.'<br><br>';
             $currentProductImageStatus = 'failure';
        }        
    
}
echo "<br>".$i." images saved";



?>