<?php defined('BASEPATH') OR exit('No direct script access allowed');

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

class Dna extends CI_Controller {

  public function __construct()
  {
      parent::__construct();    
      $this->load->helper('download');
      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);

      $data_cert = $data_video360 = $data_still = $data_mp4 = '';      
      $data_video_2 = $data_still_2 = $data_mp4_2 = '';
      $isB2b = $isB2bVideo = 0;

      $dataImg = [
        [ 'name' => 'H', 'id' => 'img_heart', 'path' => '', 'isAvail' => 0, 'image' => 'heart.png', 'tooltip' => 'Heart Image'],
        [ 'name' => 'A', 'id' => 'img_arrow', 'path' => '', 'isAvail' => 0, 'image' => 'arrow.png', 'tooltip' => 'Arrow Image'],
        [ 'name' => 'asset', 'id' => 'img_asset', 'path' => '', 'isAvail' => 0, 'image' => 'AssetScope.png', 'tooltip' => 'Asset Scope Image'],
        [ 'name' => 'white', 'id' => 'img_white', 'path' => '', 'isAvail' => 0, 'image' => 'Dbig.png', 'tooltip' => 'White Image'],
        [ 'name' => 'realimage', 'id' => 'img_realimage', 'path' => '', 'isAvail' => 0, 'image' => 'Dbig.png', 'tooltip' => 'Real Image'],
      ];

      $dataVideo = [
        [ 'name' => 'hand_images', 'id' => 'vid_hand_images', 'path' => '', 'isAvail' => 0, 'image' => 'hand_img.png', 'tooltip' => 'Hand Image'],
        [ 'name' => 'sparklevideo', 'id' => 'vid_sparklevideo', 'path' => '', 'isAvail' => 0, 'image' => 'v-sparkle.png', 'tooltip' => 'Sparkle Video'],
        [ 'name' => 'whitevideo', 'id' => 'vid_whitevideo', 'path' => '', 'isAvail' => 0, 'image' => 'v-1.png', 'tooltip' => 'White Video'],
        [ 'name' => 'fluorescencevideo', 'id' => 'vid_fluorescencevideo', 'path' => '', 'isAvail' => 0, 'image' => 'v-floursance.png', 'tooltip' => 'Fluorescence Video'],
        [ 'name' => 'blackvideo', 'id' => 'vid_blackvideo', 'path' => '', 'isAvail' => 0, 'image' => 'v-black.png', 'tooltip' => 'Black Video'],
      ];

      $dataB2cVideo = [
        [ 'name' => 'Bvideo.mp4', 'id' => 'vid_Bvideo', 'path' => '', 'isAvail' => 0, 'tooltip' => 'Bvideo'],
        [ 'name' => 'Fvideo.mp4', 'id' => 'vid_Fvideo', 'path' => '', 'isAvail' => 0, 'tooltip' => 'Fvideo'],
        [ 'name' => 'Tvideo.mp4', 'id' => 'vid_Tvideo', 'path' => '', 'isAvail' => 0, 'tooltip' => 'Tvideo'],
        [ 'name' => 'Hvideo.mp4', 'id' => 'vid_Hvideo', 'path' => '', 'isAvail' => 0, 'tooltip' => 'Hvideo'],
      ];

      $dataB2bVideo = [
        [ 'name' => 'Bvideo.mp4', 'id' => 'Bvideo', 'path' => '', 'isAvail' => 0, 'tooltip' => 'Bvideo'],
        [ 'name' => 'Fvideo.mp4', 'id' => 'Fvideo', 'path' => '', 'isAvail' => 0, 'tooltip' => 'Fvideo'],
        [ 'name' => 'Tvideo.mp4', 'id' => 'Tvideo', 'path' => '', 'isAvail' => 0, 'tooltip' => 'Tvideo'],
        [ 'name' => 'Hvideo.mp4', 'id' => 'Hvideo', 'path' => '', 'isAvail' => 0, 'tooltip' => 'Hvideo'],
      ];

      /*$dataB2B = array(
        array( 'name' => 'Heart_Black_BG.jpg', 'id' => 'b2b_Heart_Black', 'path' => '', 'isAvail' => 0, 'title' => 'Heart', 'tooltip' => 'Heart Black'),
        array( 'name' => 'Arrow_Black_BG.jpg', 'id' => 'b2b_Arrow_Black', 'path' => '', 'isAvail' => 0, 'title' => 'Arrow', 'tooltip' => 'Arrow Black'),
        array( 'name' => 'ASET_Black_BG.jpg', 'id' => 'b2b_ASET_Black', 'path' => '', 'isAvail' => 0, 'title' => 'Asset', 'tooltip' => 'Asset Black'),
        array( 'name' => 'IDEAL_White_BG.jpg', 'id' => 'b2b_Ideal_White', 'path' => '', 'isAvail' => 0, 'title' => 'Ideal', 'tooltip' => 'IDEAL White'),
        array( 'name' => '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', 'id' => 'b2b_Office_Light', 'path' => '', 'isAvail' => 0, 'title' => 'Office Light', 'tooltip' => 'Office Light Black'),
        array( 'name' => 'fluorescence.jpg', 'id' => 'b2b_fluor', 'path' => '', 'isAvail' => 0, 'title' => 'Fluorescence', 'tooltip' => 'Fluorescence'),
      );*/

      if(!empty($record)) {
          $PACKET_NO = $record['0']->PACKET_NO;
          $BR = $record['0']->BR;
          $allUrl = [];

          $allUrl[] = $video360 = DNA_URL.$PACKET_NO.'/'.$PACKET_NO.'.html';
          $allUrl[] = $still = DNA_URL.$PACKET_NO.'/still.jpg';
          $allUrl[] = $mp4 = DNA_URL.$PACKET_NO.'/video.mp4';
          $allUrl[] = $video_2 = DNA_URL.'B2B/'.$PACKET_NO.'/'.$PACKET_NO.'.html';
          $allUrl[] = $still_2 = DNA_URL.'B2B/'.$PACKET_NO.'/still.jpg';
          $allUrl[] = $mp4_2 = DNA_URL.'B2B/'.$PACKET_NO.'/video.mp4';
          $allUrl[] = $cert = DNA_URL.'certificate_Images/'.$record['0']->REPORT_NO.'.pdf';

          foreach ($dataImg as $key => $value) {
            //$allUrl[] = $dataImg[$key]['path'] = DNA_URL.$value['name'].'/'.$PACKET_NO.'.jpg'; 
            $allUrl[] = $dataImg[$key]['path'] = DNA_URL.$PACKET_NO.'/'.$value['name'].'.jpg';
          }
          foreach ($dataVideo as $key => $value) {
            $allUrl[] = $dataVideo[$key]['path'] = DNA_URL.$value['name'].'/'.$PACKET_NO.'.html';
          }
          /*foreach ($dataB2B as $key => $value) {
            $allUrl[] = $dataB2B[$key]['path'] = DNA_URL.'B2B/'.$PACKET_NO.'/'.$value['name'];
          }*/
          foreach ($dataB2cVideo as $key => $value) {
            $allUrl[] = $dataB2cVideo[$key]['path'] = DNA_URL.$PACKET_NO.'/'.$value['name'];
          }

          foreach ($dataB2bVideo as $key => $value) {
            $allUrl[] = $dataB2bVideo[$key]['path'] = DNA_URL.'B2B/'.$PACKET_NO.'/'.$value['name'];
          }

          $allUrlExist = $this->urlExist($allUrl);

          $data_video360 = (in_array($video360, $allUrlExist)) ? $video360 : '';
          $data_still = (in_array($still, $allUrlExist)) ? $still : '';
          $data_mp4 = (in_array($mp4, $allUrlExist)) ? $mp4 : '';
          $data_video_2 = (in_array($video_2, $allUrlExist)) ? $video_2 : '';
          $data_still_2 = (in_array($still_2, $allUrlExist)) ? $still_2 : '';
          $data_mp4_2 = (in_array($mp4_2, $allUrlExist)) ? $mp4_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;
            }
          }*/
          foreach ($dataB2cVideo as $key => $value) {
            $dataB2cVideo[$key]['isAvail'] = (in_array($value['path'], $allUrlExist)) ? 1 : 0;
          } 
          foreach ($dataB2bVideo as $key => $value) {
            if(in_array($value['path'], $allUrlExist)) {
              $dataB2bVideo[$key]['isAvail'] = 1;
              $isB2bVideo = 1;
            }
          } 

          if($BR =='B') {
            $a1 = $data_video360;
            $a2 = $data_still;
            $a3 = $data_mp4;
            $b1 = $data_video_2;
            $b2 = $data_still_2;
            $b3 = $data_mp4_2;

            $data_video360 = $b1;
            $data_still = $b2;
            $data_mp4 = $b3;
            $data_video_2 = $a1;
            $data_still_2 = $a2;
            $data_mp4_2 = $a3;
          }     
          
      } 
      $data = [
        'record' => $record,
        'data_cert' => $data_cert,
        'data_video360' => $data_video360,
        'data_still' => $data_still,
        'data_mp4' => $data_mp4,
        'dataImg' => $dataImg,
        'dataVideo' => $dataVideo,
        'data_video_2' => $data_video_2,
        'data_still_2' => $data_still_2,
        'data_mp4_2' => $data_mp4_2,
        'dataB2cVideo' => $dataB2cVideo,
        //'isB2b' => $isB2b,
        //'dataB2B' => $dataB2B,
        'isB2bVideo' => $isB2bVideo,
        'dataB2bVideo' => $dataB2bVideo,
      ];
      //print_ex($data);
      $this->session->set_userdata('isView', 1);
      $this->load->view('diamond/dna',$data);
  }
  function bb_video()
  {
      $data_cert = $data_video = '';
      $PACKET_NO = $this->uri->segment(2);     
      $segment_3 = $this->uri->segment(3);     

      $record = $this->get_record($PACKET_NO); 
      
      if(!empty($record)) {
          $PACKET_NO = $record['0']->PACKET_NO;

          $cert = DNA_URL.'certificate_Images/'.$record['0']->REPORT_NO.'.pdf';
          $data_cert = (checkLink($cert) == '200') ? $cert : '';
      } 
                        
      $video360 = DNA_URL.$PACKET_NO.'/'.$PACKET_NO.'.html';
      $data_video = (checkLink($video360)) ? $video360 : ''; 

      //$still = DNA_URL.$PACKET_NO.'/still.jpg';   
      //$data_still = (checkLink($still)) ? $still : ''; 

      $mp4 = DNA_URL.$PACKET_NO.'/video.mp4'; 
      $data_mp4 = (checkLink($mp4)) ? $mp4 : '';    
      
      $data = array(
        'record' => $record,
        'data_video' => $data_video,
        'data_still' => '',
        'data_mp4' => $data_mp4,
        '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']);
      }

      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(DNA_URL.$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' => '' )
          );

          foreach ($dataImg as $key => $value) {
            $allUrl[] = $dataImg[$key]['path'] = DNA_URL.$value['name'].'/'.$stock.'.jpg';
          }

          $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);
            }
          }

          $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(DNA_URL.'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(DNA_URL.$stock.'/video.mp4');
          $zip->addFromString($stock.'.mp4', $file_content);
          $zip->close();

          force_download('uploads/'.$zipFile, NULL);           
      }
      else if($type=='excel') {
        $this->export_diamond();
      }

  }

  private function export_diamond()
  {        
      $NET_RATE = $NET_VALUE = 0;
      $where = ""; 
      $stock = $this->input->post('stock');
      $type = $this->input->post('type'); 
      //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
      $records = $this->get_record($stock);    
      //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++      
    
      $spreadsheet = new Spreadsheet();
      $sheet = $spreadsheet->getActiveSheet();

      $sheet->setCellValue('A1' ,'Stock#');
      $sheet->setCellValue('B1' ,'Shape');
      $sheet->setCellValue('C1' ,'Status');
      $sheet->setCellValue('D1' ,'Cts');
      $sheet->setCellValue('E1' ,'Color');
      $sheet->setCellValue('F1' ,'Clarity');
      $sheet->setCellValue('G1' ,'Cut');
      $sheet->setCellValue('H1' ,'Pol');
      $sheet->setCellValue('I1' ,'Sym');
      $sheet->setCellValue('J1' ,'Fluor');    
      $sheet->setCellValue('K1' ,'Disc%');
      $sheet->setCellValue('L1' ,'Depth%');
      $sheet->setCellValue('M1' ,'Table%');
      $sheet->setCellValue('N1' ,'Descrip');
      $sheet->setCellValue('O1' ,'Lab');
      $sheet->setCellValue('P1' ,'Pic');
      $sheet->setCellValue('Q1' ,'Brand');
      $sheet->setCellValue('R1' ,'Measurements');
      $sheet->setCellValue('S1' ,'Report No');
    
      $sheet->getStyle('A1:S1')
        ->applyFromArray([
            'font' => [                    
              'color' => ['argb' => 'ffffff'],
              'size'  => 11,
              'name' => 'Calibri'
            ],
          'fill' =>[ 
              'fillType' => \PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID,
              'color' => ['argb' => '17375d']
            ]
        ]);
      $i=2;       
      foreach($records as $row) 
      {   
          $sheet->setCellValue('A' . $i, $row->PACKET_NO);
          $sheet->setCellValue('B' . $i, $row->SHAPE_SEQ);         
          $sheet->setCellValue('C' . $i, $row->MemoStatus);         
          $sheet->setCellValue('D' . $i, number_format($row->CTS,2));
          $sheet->setCellValue('E' . $i, $row->COLOR_SEQ);
          $sheet->setCellValue('F' . $i, $row->PURITY_SEQ);
          $sheet->setCellValue('G' . $i, $row->CUT_SEQ);
          $sheet->setCellValue('H' . $i, $row->POLISH_SEQ);
          $sheet->setCellValue('I' . $i, $row->SYMM_SEQ);
          $sheet->setCellValue('J' . $i, $row->FLS_SEQ);        
          $sheet->setCellValue('K' . $i, number_format($row->DISC_PER));
          $sheet->setCellValue('L' . $i, number_format($row->DEPTH_PER,1));
          $sheet->setCellValue('M' . $i, (int)$row->TABLE_PER);       
          $sheet->setCellValue('N' . $i, $row->DESCRIPTION);       
          $sheet->setCellValue('O' . $i, $row->LAB_SEQ);       
          $sheet->setCellValue('P' . $i, 'DNA');       
          $sheet->setCellValue('Q' . $i, $row->HA);       
          $sheet->setCellValue('R' . $i, $row->LWD);       
          $sheet->setCellValue('S' . $i, $row->REPORT_NO); 

          $link = "https://dnalinks.in/certificate_Images/".$row->REPORT_NO.".pdf"; 
          $sheet->getCell('O' . $i)->getHyperlink()->setUrl($link);

          $link = "https://dna.dnalinks.in/d/".$row->PACKET_NO;
          $sheet->getCell('P' . $i)->getHyperlink()->setUrl($link);

          $sheet->getStyle('O'.$i.':P'.$i)
            ->applyFromArray(
                [
                    'font' => [                    
                        'color' => ['argb' => '0000FF'],
                        'size'  => 11,
                        'name' => 'Calibri',
                        'underline' => 'single'
                    ]
                ]
            );

          $i++;
      }

      $sheet->setCellValue('A' . $i, count($records));

      $sheet->getStyle('A'.$i.':S'.$i)
        ->applyFromArray(
          [
              'font' => [                    
                  'color' => ['argb' => '000000'],
                  'size'  => 11,
                  'name' => 'Calibri'
              ],
              'fill' => [
                  'fillType' => \PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID,
                  'color' => ['argb' => 'd3d3d3']
              ]
          ]
      );
       
      $filename='Stock_'.date('m_d_Y_h_i_s_A').'.xlsx'; 
      $writer = new Xlsx($spreadsheet);               
      $writer->save('uploads/'.$filename);
    
      header('Content-Type: application/vnd.ms-excel'); 
      header('Content-Disposition: attachment;filename="'.$filename.'"'); 
      header('Cache-Control: max-age=0');
      readfile("uploads/".$filename);

      unlink("uploads/".$filename);
  }

  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();
      $record = $this->check_record($stock);
      if(!empty($record)) {
          return $record;
      }
      //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
      $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)){
        $responseArr = json_decode($response);
        $record = $responseArr->record;

        if(!empty($record)) {
          $this->save_record($record[0]->PACKET_NO, $record[0]->REPORT_NO, $response); 
        }
      }
      
      return $record;
  }

  private function save_record($packet_no, $report_no, $response) {
    $data = array(
            'packet_no' => $packet_no,
            'report_no'  => $report_no,
            'data'  => $response,
            'added_at'  => date('Y-m-d H:i')
    );

    $this->db->replace('tbl_diamond', $data);
  }
  private function check_record($packet_no) {    
    $record = [];

    $result = $this->db->query("SELECT * FROM tbl_diamond WHERE packet_no = '$packet_no'")->result();

    if(!empty($result)) {
        $responseArr = json_decode($result[0]->data);       
        $record = $responseArr->record;
        // if(date('Y-m-d') == date('Y-m-d', strtotime($result[0]->added_at))) { }
    }
    return $record;
  }
  //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  function del_file() {
    $this->load->database();
    $this->db->truncate('ci_sessions');
    $this->db->delete('tbl_diamond', [ 'added_at <' => date("Y-m-d", strtotime("- 10 day")) ]);

    $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;
  }
  
}