<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Dna extends CI_Controller {
  public function __construct()
  {
      parent::__construct();    
      $this->load->helper('download');
      $this->load->helper('diamond');
      //$this->load->library('excel'); 
      header('X-Robots-Tag: noindex');
      header('X-Content-Type-Options: nosniff');
      header('X-DNS-Prefetch-Control: off');
      header('X-Download-Options: noopen');
      //header('X-Frame-Options: SAMEORIGIN');
      header('X-Permitted-Cross-Domain-Policies: none');
      header('X-XSS-Protection: 0');
  } 
  function diamond_details()
  {
      $packet_no = $this->uri->segment(2);
      //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++      
      //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++      
      $record = $this->get_record($packet_no); 
      //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
      $baseAsset = 'https://dnalink.in/Imaged/';
      $data_cert = '';
      $data_still = '';
      $data_still_2 = '';      
      $data_video360 = '';
      $data_video_2 = '';
      $isB2b = 0;
      $dataImg = array(
        array( 'name' => 'heart', 'id' => 'img_heart', 'path' => '', 'isAvail' => 0, 'image' => 'heart.png', 'tooltip' => 'Heart'),
        array( 'name' => 'arrow', 'id' => 'img_arrow', 'path' => '', 'isAvail' => 0, 'image' => 'arrow.png', 'tooltip' => 'Arrow'),
        array( 'name' => 'asset', 'id' => 'img_asset', 'path' => '', 'isAvail' => 0, 'image' => 'AssetScope.png', 'tooltip' => 'Asset Scope'),
        array( 'name' => 'white', 'id' => 'img_white', 'path' => '', 'isAvail' => 0, 'image' => 'Dbig.png', 'tooltip' => 'White'),
        array( 'name' => 'realimage', 'id' => 'img_realimage', 'path' => '', 'isAvail' => 0, 'image' => 'Dbig.png', 'tooltip' => 'Real'),
      );
      $dataVideo = array(
        array( 'name' => 'hand_images', 'id' => 'vid_hand_images', 'path' => '', 'isAvail' => 0, 'image' => 'hand_img.png', 'tooltip' => 'Hand'),
        array( 'name' => 'sparklevideo', 'id' => 'vid_sparklevideo', 'path' => '', 'isAvail' => 0, 'image' => 'v-sparkle.png', 'tooltip' => 'Sparkle'),
        array( 'name' => 'whitevideo', 'id' => 'vid_whitevideo', 'path' => '', 'isAvail' => 0, 'image' => 'v-1.png', 'tooltip' => 'White'),
        array( 'name' => 'fluorescencevideo', 'id' => 'vid_fluorescencevideo', 'path' => '', 'isAvail' => 0, 'image' => 'v-floursance.png', 'tooltip' => 'Fluorescence'),
        array( 'name' => 'blackvideo', 'id' => 'vid_blackvideo', 'path' => '', 'isAvail' => 0, 'image' => 'v-black.png', 'tooltip' => 'Black'),
      );      
      $dataB2B = array(
        array( 'name' => 'Heart_Black_BG.jpg', 'image' => 'Heart_Black_BG.jpg', 'id' => 'b2b_Heart_Black', 'path' => '', 'isAvail' => 0, 'title' => 'Heart', 'tooltip' => 'Heart Black'),
        array( 'name' => 'Arrow_Black_BG.jpg', 'image' => 'Arrow_Black_BG.jpg', 'id' => 'b2b_Arrow_Black', 'path' => '', 'isAvail' => 0, 'title' => 'Arrow', 'tooltip' => 'Arrow Black'),
        array( 'name' => 'ASET_Black_BG.jpg', 'image' => 'ASET_Black_BG.jpg', 'id' => 'b2b_ASET_Black', 'path' => '', 'isAvail' => 0, 'title' => 'Asset', 'tooltip' => 'Asset Black'),
        array( 'name' => 'IDEAL_White_BG.jpg', 'image' => 'IDEAL_White_BG.jpg', 'id' => 'b2b_Ideal_White', 'path' => '', 'isAvail' => 0, 'title' => 'Ideal', 'tooltip' => 'Ideal White'),
        array( 'name' => 'Dark_Field_White_BG.jpg', 'image' => 'Dark_Field_White_BG.jpg', 'id' => 'b2b_Dark_Field', 'path' => '', 'isAvail' => 0, 'title' => 'Dark Field', 'tooltip' => 'Dark Field White'),
        array( 'name' => 'Office_Light_Black_BG.jpg', 'image' => 'Office_Light_Black_BG.jpg', 'id' => 'b2b_Office_Light', 'path' => '', 'isAvail' => 0, 'title' => 'Office Light', 'tooltip' => 'Office Light Black'),
        array( 'name' => 'fluorescence.jpg', 'image' => 'fluorescence.jpg', 'id' => 'b2b_fluor', 'path' => '', 'isAvail' => 0, 'title' => 'Fluorescence', 'tooltip' => 'Fluorescence'),
      );
      if(!empty($record)) {
          $PACKET_NO = $record['0']->PACKET_NO;
          $allUrl = [];
          //$allUrl[] = $video360_new = $baseAsset.$PACKET_NO.'/'.$PACKET_NO.'.html';
          $video360_new = 'https://dnalink.in/Vision360.html?d='.$PACKET_NO.'&v=2&sv=0,1,2,3,4&z=1&btn=1,2,3,5?s=100&sr=0';
          $allUrl[] = $video360_check = 'https://dnalink.in/imaged/'.$PACKET_NO.'/0.json?version=1';
          $allUrl[] = $video_2 = $baseAsset.'B2B/'.$PACKET_NO.'/'.$PACKET_NO.'.html';
          $allUrl[] = $still = $baseAsset.$PACKET_NO.'/still.jpg';
          $allUrl[] = $still_2 = $baseAsset.'B2B/'.$PACKET_NO.'/still.jpg';
          $allUrl[] = $cert = $baseAsset.'certificate_Images/'.$record['0']->REPORT_NO.'.pdf';
          foreach ($dataImg as $key => $value) {
            $allUrl[] = $dataImg[$key]['path'] = $baseAsset.$value['name'].'/'.$PACKET_NO.'.jpg'; 
          }
          foreach ($dataVideo as $key => $value) {
            $allUrl[] = $dataVideo[$key]['path'] = $baseAsset.$value['name'].'/'.$PACKET_NO.'.html';
          }
          foreach ($dataB2B as $key => $value) {
            $allUrl[] = $dataB2B[$key]['path'] = $baseAsset.'B2B/'.$PACKET_NO.'/'.$value['name'];
          }
          $allUrlExist = $this->urlExist($allUrl);
          $data_video360 = (in_array($video360_check, $allUrlExist)) ? $video360_new : '';
          $data_video_2 = (in_array($video_2, $allUrlExist)) ? $video_2 : '';
          $data_still = (in_array($still, $allUrlExist)) ? $still : '';
          $data_still_2 = (in_array($still_2, $allUrlExist)) ? $still_2 : '';
          $data_cert = (in_array($cert, $allUrlExist)) ? $cert : '';
          foreach ($dataImg as $key => $value) {
            $dataImg[$key]['isAvail'] = (in_array($value['path'], $allUrlExist)) ? 1 : 0;
          }
          foreach ($dataVideo as $key => $value) {
            $dataVideo[$key]['isAvail'] = (in_array($value['path'], $allUrlExist)) ? 1 : 0;
          }
          foreach ($dataB2B as $key => $value) {
            if(in_array($value['path'], $allUrlExist)) {
              $dataB2B[$key]['isAvail'] = 1;
              $isB2b = 1;
            }
          }
          //print_ex($dataB2B);
      } 
      $data = array(
        'record' => $record,
        'data_video360' => $data_video360,
        'data_video_2' => $data_video_2,
        'data_cert' => $data_cert,
        'data_still' => $data_still,
        'data_still_2' => $data_still_2,        
        'dataImg' => $dataImg,
        'dataVideo' => $dataVideo,
        'isB2b' => $isB2b,
        'dataB2B' => $dataB2B,
      );
      $data['varDiamondDetail'] = diaVar('varDiamondDetail');
      $data['varDiamondMeas'] = diaVar('varDiamondMeas');
      $data['varDiamondInclusion'] = diaVar('varDiamondInclusion');
      //print_ex($data);
      $this->session->set_userdata('isView', 1);
      $this->load->view('diamond/dna',$data);      
  }
  function bb_video()
  {
      $data_cert = '';
      $data_video_2 = '';
      $packet_no = $this->uri->segment(2);     
      $baseAsset = 'https://dnalink.in/Imaged/';
      $record = $this->get_record($packet_no); 
      if(!empty($record)) {
          $PACKET_NO = $record['0']->PACKET_NO;
          $video_2 = $video360_b2b = $baseAsset.'B2B/'.$PACKET_NO.'/'.$PACKET_NO.'.html';

          //$video_2 = 'https://diamondmedia.s3.us-east-1.amazonaws.com/B2B/imaged/'.$PACKET_NO.'/'.$PACKET_NO.'.html';
          //$video360_b2b = 'https://diamondmedia.s3.us-east-1.amazonaws.com/B2B/Vision360.html?d='.$PACKET_NO.'&sr=10&s=20';

          $cert = $baseAsset.'certificate_Images/'.$record['0']->REPORT_NO.'.pdf';
          $data_video_2 = (checkLink($video_2) == '200') ? $video360_b2b : '';
          $data_cert = (checkLink($cert) == '200') ? $cert : '';
      } 
      $data = array(
        'record' => $record,
        'data_video_2' => $data_video_2,
        'data_cert' => $data_cert,
      );
      //print_ex($data);
      $this->session->set_userdata('isView', 1);
      $this->load->view('diamond/bb_video',$data); 
  }
  private function checkUser() {
    if(!$this->session->userdata('isView')) {
      exit('No direct script access allowed');
    }
  }
  function download(){
      $this->checkUser();
      $stock = $this->input->post('stock');
      $type = $this->input->post('type');
      if(trim($stock)=='' || trim($type)==''){
        redirect($_SERVER['HTTP_REFERER']);
      }
      $baseAsset = 'https://dnalink.in/Imaged/';
      $allUrl = [];
      if($type=='image') {
          $zip = new ZipArchive();
          $zipFile = $stock.'Image'.time().'.zip';
          if ($zip->open('uploads/'.$zipFile, ZipArchive::CREATE | ZipArchive::OVERWRITE) !== TRUE) {
               die ('Could not open archive');
          }
          $file_content = file_get_contents($baseAsset.$stock.'/still.jpg');
          $zip->addFromString('still.jpg', $file_content);
          $dataImg = array(
            array( 'name' => 'heart', 'path' => '' ),
            array( 'name' => 'arrow', 'path' => '' ),
            array( 'name' => 'asset', 'path' => '' ),
            array( 'name' => 'white', 'path' => '' )
          );
          $dataB2B = array(
            array( 'name' => 'Arrow_Black_BG.jpg', 'path' => '' ),
            array( 'name' => 'ASET_Black_BG.jpg', 'path' => '' ),
            array( 'name' => 'Dark_Field_White_BG.jpg', 'path' => '' ),
            array( 'name' => 'fluorescence.jpg', 'path' => '' ),
            array( 'name' => 'Heart_Black_BG.jpg', 'path' => '' ),
            array( 'name' => 'IDEAL_White_BG.jpg', 'path' => '' ),
            array( 'name' => 'Office_Light_Black_BG.jpg', 'path' => '' )
          );
          foreach ($dataImg as $key => $value) {
            $allUrl[] = $dataImg[$key]['path'] = $baseAsset.$value['name'].'/'.$stock.'.jpg';
          }
          foreach ($dataB2B as $key => $value) {
            $allUrl[] = $dataB2B[$key]['path'] = $baseAsset.'B2B/'.$stock.'/'.$value['name'];
          }
          $allUrlExist = $this->urlExist($allUrl);
          foreach ($dataImg as $key => $value) {
            if(in_array($value['path'], $allUrlExist)) {
              $file_content = file_get_contents($value['path']);
              $zip->addFromString($value['name'].'.jpg', $file_content);
            }
          }
          foreach ($dataB2B as $key => $value) {
            if(in_array($value['path'], $allUrlExist)) {
              $file_content = file_get_contents($value['path']);
              $zip->addFromString('B2B_'.$value['name'], $file_content);
            }
          }
          $zip->close();
          force_download('uploads/'.$zipFile, NULL);          
      }      
      else if($type=='cert') {
          $zip = new ZipArchive();
          $zipFile = $stock.'Cert'.time().'.zip';
          if ($zip->open('uploads/'.$zipFile, ZipArchive::CREATE | ZipArchive::OVERWRITE) !== TRUE) {
               die ('Could not open archive');
          }
          $file_content = file_get_contents($baseAsset.'certificate_Images/'.$stock.'.pdf');
          $zip->addFromString($stock.'.pdf', $file_content);
          $zip->close();
          force_download('uploads/'.$zipFile, NULL);           
      }      
      else if($type=='video') {
          $zip = new ZipArchive();
          $zipFile = $stock.'Video'.time().'.zip';
          if ($zip->open('uploads/'.$zipFile, ZipArchive::CREATE | ZipArchive::OVERWRITE) !== TRUE) {
               die ('Could not open archive');
          }
          $file_content = file_get_contents($baseAsset.$stock.'/video.mp4');
          $zip->addFromString($stock.'.mp4', $file_content);
          /*$b2bVideo = $baseAsset.'B2B/'.$stock.'/video.mp4';
          if(checkLink($b2bVideo)=='200') {
            $file_content = file_get_contents($b2bVideo);
            $zip->addFromString('B2B_'.$stock.'.mp4', $file_content);
          }*/
          $zip->close();
          force_download('uploads/'.$zipFile, NULL);         
      }
      else if($type=='excel') {
        $this->export_diamond();
      }
  }
  private function export_diamond()
  {        
    $where = ""; 
    $stock = $this->input->post('stock');
    $type = $this->input->post('type'); 
    //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    $record = $this->get_record($stock);    
    //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++       
    $this->excel->setActiveSheetIndex(0);
    $this->excel->getActiveSheet()->setTitle('Stock');
    $styleArray = array(
      'font'  => array(
          'color' => array('rgb' => '8c0808'),
          'size'  => 18,
          'name' => 'Verdana',
          'horizontal' => PHPExcel_Style_Alignment::HORIZONTAL_CENTER,              
    )); 
    $this->excel->getActiveSheet()->setCellValueByColumnAndRow('0','1' ,'Stock#');
    $this->excel->getActiveSheet()->setCellValueByColumnAndRow('1','1' ,'Shape');
    $this->excel->getActiveSheet()->setCellValueByColumnAndRow('2','1' ,'Status');
    $this->excel->getActiveSheet()->setCellValueByColumnAndRow('3','1' ,'Cts');
    $this->excel->getActiveSheet()->setCellValueByColumnAndRow('4','1' ,'Color');
    $this->excel->getActiveSheet()->setCellValueByColumnAndRow('5','1' ,'Clarity');
    $this->excel->getActiveSheet()->setCellValueByColumnAndRow('6','1' ,'Cut');
    $this->excel->getActiveSheet()->setCellValueByColumnAndRow('7','1' ,'Pol');
    $this->excel->getActiveSheet()->setCellValueByColumnAndRow('8','1' ,'Sym');
    $this->excel->getActiveSheet()->setCellValueByColumnAndRow('9','1' ,'Fluor');    
    $this->excel->getActiveSheet()->setCellValueByColumnAndRow('10','1' ,'Disc%');
    $this->excel->getActiveSheet()->setCellValueByColumnAndRow('11','1' ,'Depth%');
    $this->excel->getActiveSheet()->setCellValueByColumnAndRow('12','1' ,'Table%');
    $this->excel->getActiveSheet()->setCellValueByColumnAndRow('13','1' ,'Rap.($)');
    $this->excel->getActiveSheet()->setCellValueByColumnAndRow('14','1' ,'$/Carat');
    $this->excel->getActiveSheet()->setCellValueByColumnAndRow('15','1' ,'Total $');
    $this->excel->getActiveSheet()->setCellValueByColumnAndRow('16','1' ,'Descrip');
    $this->excel->getActiveSheet()->setCellValueByColumnAndRow('17','1' ,'Lab');
    $this->excel->getActiveSheet()->setCellValueByColumnAndRow('18','1' ,'Pic');
    $this->excel->getActiveSheet()->setCellValueByColumnAndRow('19','1' ,'Brand');
    $this->excel->getActiveSheet()->setCellValueByColumnAndRow('20','1' ,'Measurements');
    $this->excel->getActiveSheet()->setCellValueByColumnAndRow('21','1' ,'Report No');
    $this->excel->getActiveSheet()->getStyle('A1:V1')
      ->applyFromArray(
          array(
              'font' => array(                    
                  'color' => array('rgb' => 'ffffff'),
                  'size'  => 11,
                  'name' => 'Calibri'
              ),
              'fill' => array(
                  'type' => PHPExcel_Style_Fill::FILL_SOLID,
                  'color' => array('rgb' => '17375d')
              )
          )
      );
    $i=2;       
    foreach($record as $row) 
    {   
        $this->excel->getActiveSheet()->setCellValueByColumnAndRow('0',$i,$row->PACKET_NO);
        $this->excel->getActiveSheet()->setCellValueByColumnAndRow('1',$i,$row->SHAPE_SEQ);         
        $this->excel->getActiveSheet()->setCellValueByColumnAndRow('2',$i,$row->MemoStatus);         
        $this->excel->getActiveSheet()->setCellValueByColumnAndRow('3',$i,number_format($row->CTS,2));
        $this->excel->getActiveSheet()->setCellValueByColumnAndRow('4',$i,$row->COLOR_SEQ);
        $this->excel->getActiveSheet()->setCellValueByColumnAndRow('5',$i,$row->PURITY_SEQ);
        $this->excel->getActiveSheet()->setCellValueByColumnAndRow('6',$i,$row->CUT_SEQ);
        $this->excel->getActiveSheet()->setCellValueByColumnAndRow('7',$i,$row->POLISH_SEQ);
        $this->excel->getActiveSheet()->setCellValueByColumnAndRow('8',$i,$row->SYMM_SEQ);
        $this->excel->getActiveSheet()->setCellValueByColumnAndRow('9',$i,$row->FLS_SEQ);        
        $this->excel->getActiveSheet()->setCellValueByColumnAndRow('10',$i,number_format($row->DISC_PER,1));
        $this->excel->getActiveSheet()->setCellValueByColumnAndRow('11',$i,number_format($row->DEPTH_PER,1));
        $this->excel->getActiveSheet()->setCellValueByColumnAndRow('12',$i,(int)$row->TABLE_PER);
        $this->excel->getActiveSheet()->setCellValueByColumnAndRow('13',$i,round($row->RATE));        
        $this->excel->getActiveSheet()->setCellValueByColumnAndRow('14',$i,number_format($row->NET_RATE,2));        
        $this->excel->getActiveSheet()->setCellValueByColumnAndRow('15',$i,number_format($row->NET_VALUE,2));        
        $this->excel->getActiveSheet()->setCellValueByColumnAndRow('16',$i,$row->DESCRIPTION);       
        $this->excel->getActiveSheet()->setCellValueByColumnAndRow('17',$i,$row->LAB_SEQ);       
        $this->excel->getActiveSheet()->setCellValueByColumnAndRow('18',$i,'DNA');       
        $this->excel->getActiveSheet()->setCellValueByColumnAndRow('19',$i,$row->HA);       
        $this->excel->getActiveSheet()->setCellValueByColumnAndRow('20',$i,$row->LWD);       
        $this->excel->getActiveSheet()->setCellValueByColumnAndRow('21',$i,$row->REPORT_NO); 
        $link = "https://dnalink.in/Imaged/certificate_Images/".$row->REPORT_NO.".pdf";
        $this->excel->getActiveSheet()->getCellByColumnAndRow(17,$i)->getHyperlink()->setUrl($link);        
        $link = "https://dna.dnalink.in/d/".$row->PACKET_NO;
        $this->excel->getActiveSheet()->getCellByColumnAndRow(18,$i)->getHyperlink()->setUrl($link);
        $this->excel->getActiveSheet()->getStyle('R'.$i.':S'.$i.'')
          ->applyFromArray(
              array(
                  'font' => array(                    
                      'color' => array('rgb' => '0000FF'),
                      'size'  => 11,
                      'name' => 'Calibri',
                      'underline' => 'single'
                  )
              )
          );
        $NET_RATE += $row->NET_RATE;
        $NET_VALUE += $row->NET_VALUE;
        $i++;
    }
    $this->excel->getActiveSheet()->setCellValueByColumnAndRow('0',$i,count($records->record));
    $this->excel->getActiveSheet()->setCellValueByColumnAndRow('14',$i,$NET_RATE);
    $this->excel->getActiveSheet()->setCellValueByColumnAndRow('15',$i,$NET_VALUE);
    //$this->excel->getActiveSheet()->setAutoFilter('A1:V1');
    foreach (range('A', $this->excel->getActiveSheet()->getHighestDataColumn()) as $col) {
        $this->excel->getActiveSheet()->getColumnDimension($col)->setAutoSize(true);
    } 
    $this->excel->getActiveSheet()->getStyle('A'.$i.':V'.$i.'')
      ->applyFromArray(
          array(
              'font' => array(                    
                  'color' => array('rgb' => '000000'),
                  'size'  => 11,
                  'name' => 'Calibri'
              ),
              'fill' => array(
                  'type' => PHPExcel_Style_Fill::FILL_SOLID,
                  'color' => array('rgb' => 'd3d3d3')
              )
          )
      );
      $this->excel->getActiveSheet()
        ->getStyle( $this->excel->getActiveSheet()->calculateWorksheetDimension() )
        ->getAlignment()
        ->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
      $filename='Stock_'.date('m_d_Y_h_i_s_A').'.xls'; 
      header('Content-Type: application/vnd.ms-excel'); 
      header('Content-Disposition: attachment;filename="'.$filename.'"'); 
      header('Cache-Control: max-age=0');                   
      $objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'Excel5');  
      $objWriter->save('php://output');
  }
  private function get_token() 
  {   
    $token = $this->session->tempdata('dna_token');
    if(empty($token)) 
    {
      $curl = curl_init();
      curl_setopt_array($curl, array(
      CURLOPT_URL => APIURL."/get-token",
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_ENCODING => "",
      CURLOPT_MAXREDIRS => 10,
      CURLOPT_TIMEOUT => 30,
      CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
      CURLOPT_CUSTOMREQUEST => "POST",
      CURLOPT_POSTFIELDS => "username=".APIUSER."&password=".APIPASSWORD."",
      CURLOPT_HTTPHEADER => array(
          "Accept: */*",
          "Content-Type: application/x-www-form-urlencoded",
        ),
      ));
      $response = curl_exec($curl);
      $err = curl_error($curl);
      curl_close($curl);
      if (empty($err)){
        $response = json_decode($response, true);
        $token = $response['token'];
        $this->session->set_tempdata('dna_token',$token,3000);
      } 
    } 
    return $token;
  }
  private function get_record($stock) 
  {   
      $record = array();
      //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
      $token = $this->get_token();
      //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
      $curl = curl_init();
      curl_setopt_array($curl, array(
        CURLOPT_URL => APIURL."/diamond-dna/".$stock,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => "",
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 30,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => "GET",
        CURLOPT_HTTPHEADER => array(
          "Accept: */*",
          "Authorization: Bearer ".$token         
        ),
      ));
      $response = curl_exec($curl);
      $err = curl_error($curl);
      curl_close($curl);
      if (empty($err)){
        $response = json_decode($response);
        $record = $response->record;
      }
      return $record;
  }
  //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  function del_file() {
    $this->load->helper('file');
    delete_files('./uploads/', FALSE, TRUE);
  }
  //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
  private function urlExist($urls) {
    $requests = array();
    $returnArray = array();
    $mh = curl_multi_init();
    foreach ($urls as $k => $url) {
        $requests[$k] = array();
        $requests[$k]['url'] = $url;
        $requests[$k]['curl_handle'] = curl_init($url);
        curl_setopt($requests[$k]['curl_handle'], CURLOPT_RETURNTRANSFER, true);
        curl_setopt($requests[$k]['curl_handle'], CURLOPT_FOLLOWLOCATION, false);
        curl_setopt($requests[$k]['curl_handle'], CURLOPT_TIMEOUT, 10);
        curl_setopt($requests[$k]['curl_handle'], CURLOPT_NOBODY, true);
        curl_setopt($requests[$k]['curl_handle'], CURLOPT_HEADER, true);
        curl_multi_add_handle($mh, $requests[$k]['curl_handle']);
    }
    $stillRunning = false;
    do {
        curl_multi_exec($mh, $stillRunning);
    } while ($stillRunning);
    foreach ($requests as $k => $request) {
        curl_multi_remove_handle($mh, $request['curl_handle']);        
        $http_code = curl_getinfo($request['curl_handle'], CURLINFO_HTTP_CODE);
        $requests[$k]['http_code'] = curl_getinfo($request['curl_handle'], CURLINFO_HTTP_CODE);
        if ($requests[$k]['http_code'] == 200) {
          $returnArray[] = $request['url'];
        }
        curl_close($requests[$k]['curl_handle']);
    }
    curl_multi_close($mh);
    return $returnArray;
  }
}
