TrashController.php 8.87 KB
<?php
namespace controllers;

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

class TrashController extends \Phalcon\Mvc\Controller
{

    function indexAction()
    {
        $page  = '';
        $items = array();
        $final_list = array();
        $result = array();

        $item_reg = '/class="tabletitle"/';
        $link_reg = '/<td><a href="([^t]*)">Поиск<\/a><\/td>/';
        $items_reg = '/<td class="tabletitle" colspan="12">(.*?)<\/td><\/tr><tr>(.*?)<\/tr>/';
        $item_name = '/<a .*>(.*)<\/a>/';
        $item_desc = '/<div class="descblock">(.*)<\/div>/';
        $item_price = '/<td class="price" align="right">(.*)<\/td>/';




        if ($this->request->hasFiles() == true) {
            //Print the real file names and their sizes
            foreach ($this->request->getUploadedFiles() as $file) {
                //Move the file into the application
                $route = STORAGE_PATH . 'temp/' . $file->getName();
                $file->moveTo($route);
            }

            $this->exelphp->addFile($route);
            $result = $this->exelphp->getRows();


        }
        foreach($result as $row){
                $proxy = \proxy::getInstance();
                $page = $this->parseExistLink('price.aspx?sr=-4&pcode='.$row[1],$proxy->getProxy());

                $check_items = preg_match($item_reg, $page, $check);

                if($check_items){
                    preg_match_all($items_reg, $page, $one_item);

                    $items[] = $one_item;
                } else {
                    preg_match_all($link_reg, $page, $links);

                    $items = array();
                    foreach($links[1] as $link){

                        $page = $this->parseExistLink($link,$proxy->getProxy());

                        preg_match_all($items_reg, $page, $one_item);
                        $items[] = $one_item;

                    }
                }

                die(print_r($items));
                if(!empty($items)){
                    $num = count($items);
                    for($i=0; $i<$num; $i++){

                        /*$sub_num = count($items[$i][1]);

                        for($x=0; $x<$sub_num; $x++){
                            $final_list[$i][$x] = preg_split('/\|+/', preg_replace('/<(.*?)>/', '|', $items[$i][0][$x]));
                        }*/

                        preg_match_all(  $item_name, $items[$i][0][0], $name);

                        //$final_list[] = $name;


                    }


                } else {
                    die('по запросу ничего не найдено');
                }

//            Array
//            (
//                [0] => Array
//                (
//                    [0] => Array
//                        (
//                            [0] => <td class="tabletitle" colspan="12">• Запрошенный артикул</td></tr><tr id="_0_" attr="firm_1260" bl="-10"><td><div class="artblock"><div class="firmname"><a href="/hint/?s=a00006dd-4ec8-4000-bce3-d00000c00000" onclick='javascript:ShowTipLayer(this,event,this.href, 30,130); return false;'>Bearmach</a></div><div class="art">CA......CL</div></div><div class="descblock">Брызговики передние</div></td><td class="informicon" align="center"></td><td align="center"></td><td class="statis mobile_h">---</td><td class="price" align="right">---</td><td></td></tr>
//                            [1] => <td class="tabletitle" colspan="12">• Предложения по оригинальным производителям</td></tr><tr id="_1_" attr="firm_59" bl="-1"><td><div class="artblock"><div class="firmname"><a href="/hint/?s=78411e5b-0037-403b-bae3-0426e0004dc5" onclick='javascript:ShowTipLayer(this,event,this.href, 30,130); return false;'>Land Rover</a></div><div class="art">CA......CL</div></div><div class="descblock">Брызговики передние RR Sport</div></td><td class="informicon" align="center"><a target='_blank' rel='popup_window_51eefed0-2178-4e7b-b388-1d8f90300058' href='/Parts.axd?pid=20504A03&flag=419449380'><span class="img"></span><span class="appl"></span></a></td><td align="center">1</td><td class="statis mobile_h">3 дн.<span class="none" title="Нет данных"></span></td><td class="price" align="right">2 262,00грн.</td><td class="basket"><a title="В вашей корзине: 0 шт." onclick="wait(this);" href="/profile/orders/basket.aspx?pid=BE109952_010000004d005b57018a6e1e000042bb&sr=-4"></a></td></tr>
//                        )
//
//                    [1] => Array
//                        (
//                            [0] => • Запрошенный артикул
//                            [1] => • Предложения по оригинальным производителям
//                        )
//
//                    [2] => Array
//                    (
//                        [0] =>  id="_0_" attr="firm_1260" bl="-10"><td><div class="artblock"><div class="firmname"><a href="/hint/?s=a00006dd-4ec8-4000-bce3-d00000c00000" onclick='javascript:ShowTipLayer(this,event,this.href, 30,130); return false;'>Bearmach</a></div><div class="art">CA......CL</div></div><div class="descblock">Брызговики передние</div></td><td class="informicon" align="center"></td><td align="center"></td><td class="statis mobile_h">---</td><td class="price" align="right">---</td><td></td>
//                        [1] =>  id="_1_" attr="firm_59" bl="-1"><td><div class="artblock"><div class="firmname"><a href="/hint/?s=78411e5b-0037-403b-bae3-0426e0004dc5" onclick='javascript:ShowTipLayer(this,event,this.href, 30,130); return false;'>Land Rover</a></div><div class="art">CA......CL</div></div><div class="descblock">Брызговики передние RR Sport</div></td><td class="informicon" align="center"><a target='_blank' rel='popup_window_51eefed0-2178-4e7b-b388-1d8f90300058' href='/Parts.axd?pid=20504A03&flag=419449380'><span class="img"></span><span class="appl"></span></a></td><td align="center">1</td><td class="statis mobile_h">3 дн.<span class="none" title="Нет данных"></span></td><td class="price" align="right">2 262,00грн.</td><td class="basket"><a title="В вашей корзине: 0 шт." onclick="wait(this);" href="/profile/orders/basket.aspx?pid=BE109952_010000004d005b57018a6e1e000042bb&sr=-4"></a></td>
//                    )
//
//                )
//
//
//            )

        }





        /* if((isset($price) && !empty($price)) && (isset($h1) && !empty($h1))) {
            $price = preg_replace("/[^0-9]/", '', $price[1]);

        }*/
        $this->view->setVars([
            'data' =>$final_list
        ]);
    }


    public function parseExistLink($url,$proxy){

        $url = 'http://www.exist.ua/'.$url;

        $proxy = explode(':', $proxy);
        //$proxyauth = 'RUS143854:Z8lhjhYPUP';
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
        //curl_setopt($ch, CURLOPT_PROXYTYPE, 7);
        curl_setopt($ch, CURLOPT_PROXY, $proxy[0]);
        curl_setopt($ch, CURLOPT_PROXYPORT, $proxy[1]);
       // curl_setopt($ch, CURLOPT_PROXYUSERPWD, $proxyauth);
        curl_setopt($ch, CURLOPT_HEADER, 1);
        curl_setopt($ch,  CURLOPT_USERAGENT , "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)");
        $headers = array
        (
            'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*;q=0.8',
            'Accept-Language: ru,en-us;q=0.7,en;q=0.3',
            'Accept-Encoding: deflate',
            'Accept-Charset: windows-1251,utf-8;q=0.7,*;q=0.7'
        );

        curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
        sleep(5);
        $exec = curl_exec($ch);

        $info = curl_getinfo($ch);

        curl_close($ch);

        return preg_replace('/>\s*</', '><', $exec);

    }



    function addAction()
    {
        $model = new \rdsServices;
        $model->save();

        $model = \rdsServices::find(array("order" => 'id'));
        $model = $model->getLast();
        $this->view->disableLevel(\Phalcon\Mvc\View::LEVEL_MAIN_LAYOUT);
        $this->view->setVars([
            'item' => $model,
        ]);

    }

    function deleteAction()
    {

        $id = $this->request->get('id');
        $model = \rdsServices::findFirst("id = '$id'");
        if($model instanceof \rdsServices) {
            $model->delete();
        }

    }

    function updateAction()
    {
        $data  =  $this->request->getPost('data');
        $id  =  $this->request->getPost('id');
        $data = json_decode($data);
        $model = \rdsServices::findFirst(array("id = '$id'"));
        foreach(get_object_vars($data) as $key=>$value) {
            $array[$key] = $value;
        }
        $model->save($array);
        die();
    }


}