Advertisement
Guest User

LinksController.php

a guest
Mar 24th, 2019
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 27.36 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Controller;
  4.  
  5. use App\Controller\AppController;
  6. use Cake\Event\Event;
  7. use Cake\I18n\Time;
  8. use Cake\Network\Exception\NotFoundException;
  9. use Cake\Network\Exception\BadRequestException;
  10. use Cake\ORM\TableRegistry;
  11.  
  12. class LinksController extends AppController
  13. {
  14.  
  15.     public function beforeFilter(Event $event)
  16.     {
  17.         parent::beforeFilter($event);
  18.         $this->viewBuilder()->layout('go');
  19.         $this->Auth->allow([ 'shorten', 'st', 'api', 'view', 'go', 'stats']);
  20.     }
  21.  
  22.     public function shorten()
  23.     {
  24.         $this->autoRender = false;
  25.  
  26.         $this->response->type('json');
  27.  
  28.         if (!$this->request->is('ajax')) {
  29.             $content = [
  30.                 'status' => 'error',
  31.                 'message' => __('Bad Request.'),
  32.                 'url' => ''
  33.             ];
  34.             $this->response->body(json_encode($content));
  35.             return $this->response;
  36.         }
  37.  
  38.  
  39.         $user_id = 1;
  40.         if (null !== $this->Auth->user('id')) {
  41.             $user_id = $this->Auth->user('id');
  42.         }
  43.  
  44.         $user = $this->Links->Users->find()->where(['id' => $user_id, 'status' => 'active'])->first();
  45.  
  46.         if (!$user) {
  47.             $content = [
  48.                 'status' => 'error',
  49.                 'message' => __('Invalid user'),
  50.                 'url' => ''
  51.             ];
  52.             $this->response->body(json_encode($content));
  53.             return $this->response;
  54.         }
  55.  
  56.         $this->request->data['url'] = trim($this->request->data['url']);
  57.         $this->request->data['url'] = str_replace(" ", "%20", $this->request->data['url']);
  58.         $this->request->data['url'] = parse_url($this->request->data['url'], PHP_URL_SCHEME) === null ? 'http://' . $this->request->data['url'] : $this->request->data['url'];
  59.  
  60.         $link = $this->Links->find()->where(['url' => $this->request->data['url'], 'user_id' => $user->id])->first();
  61.  
  62.         if ($link) {
  63.             $content = [
  64.                 'status' => 'success',
  65.                 'message' => '',
  66.                 'url' => get_short_url($link->alias)
  67.             ];
  68.             $this->response->body(json_encode($content));
  69.             return $this->response;
  70.         }
  71.  
  72.         $link = $this->Links->newEntity();
  73.         $data = [];
  74.  
  75.         $data['user_id'] = $user->id;
  76.         $data['url'] = $this->request->data['url'];
  77.         $data['alias'] = $this->Links->geturl();
  78.         $data['ad_type'] = ( null !== $this->request->data['ad_type'] ) ? $this->request->data['ad_type'] : 1;
  79.         $link->status = 'active';
  80.         $link->hits = 0;
  81.  
  82.         $linkMeta = $this->Links->getLinkMeta($this->request->data['url']);
  83.  
  84.         $link->title = $linkMeta['title'];
  85.         $link->description = $linkMeta['description'];
  86.         $link->image = $linkMeta['image'];
  87.  
  88.  
  89.         $link = $this->Links->patchEntity($link, $data);
  90.         if ($this->Links->save($link)) {
  91.             $content = [
  92.                 'status' => 'success',
  93.                 'message' => '',
  94.                 'url' => get_short_url($link->alias)
  95.             ];
  96.             $this->response->body(json_encode($content));
  97.             return $this->response;
  98.         }
  99.  
  100.         $content = [
  101.             'status' => 'error',
  102.             'message' => __('Invalid URL.'),
  103.             'url' => ''
  104.         ];
  105.         $this->response->body(json_encode($content));
  106.         return $this->response;
  107.     }
  108.  
  109.     public function st()
  110.     {
  111.         $this->autoRender = false;
  112.        
  113.         $message = '';
  114.  
  115.         if (!isset($this->request->query) || !isset($this->request->query['api']) || !isset($this->request->query['url'])) {
  116.             $message = __('Invalid Request.');
  117.             $this->set('message', $message);
  118.             return;
  119.         }
  120.  
  121.         $api = $this->request->query['api'];
  122.         $url = urldecode($this->request->query['url']);
  123.  
  124.         $ad_type = 1;
  125.         if (isset($this->request->query['type'])) {
  126.             if ($this->request->query['type'] == 2) {
  127.                 $ad_type = 2;
  128.             } elseif ($this->request->query['type'] == 0) {
  129.                 $ad_type = 0;
  130.             }
  131.         }
  132.  
  133.         $user = $this->Links->Users->find()->where(['api_token' => $api, 'status' => 'active'])->first();
  134.  
  135.         if (!$user) {
  136.             $message = __('Invalid API token.');
  137.             $this->set('message', $message);
  138.             return;
  139.         }
  140.  
  141.         $url = trim($url);
  142.         $url = str_replace(" ", "%20", $url);
  143.        
  144.         $url = parse_url($url, PHP_URL_SCHEME) === null ? 'http://' . $url : $url;
  145.  
  146.         $link = $this->Links->find()->where(['url' => $url, 'user_id' => $user->id])->first();
  147.  
  148.         if ($link) {
  149.             return $this->redirect('/' . $link->alias);
  150.         }
  151.  
  152.         $link = $this->Links->newEntity();
  153.         $data = [];
  154.  
  155.         $data['user_id'] = $user->id;
  156.         $data['url'] = $url;
  157.         $data['alias'] = $this->Links->geturl();
  158.         $link->status = 'active';
  159.         $link->hits = 0;
  160.  
  161.         $link->ad_type = $ad_type;
  162.  
  163.         $linkMeta = $this->Links->getLinkMeta($url);
  164.  
  165.         $link->title = $linkMeta['title'];
  166.         $link->description = $linkMeta['description'];
  167.         $link->image = $linkMeta['image'];
  168.  
  169.         $link = $this->Links->patchEntity($link, $data);
  170.         if ($this->Links->save($link)) {
  171.             return $this->redirect('/' . $link->alias);
  172.         }
  173.  
  174.         $message = __('Error.');
  175.         $this->set('message', $message);
  176.         return;
  177.     }
  178.  
  179.     public function api()
  180.     {
  181.         $this->autoRender = false;
  182.  
  183.         $this->response->type('json');
  184.  
  185.  
  186.         if (!isset($this->request->query) || !isset($this->request->query['api']) || !isset($this->request->query['url'])) {
  187.             $body = json_encode([
  188.                 'status' => 'error',
  189.                 'message' => 'Invalid API call',
  190.                 'shortenedUrl' => ''
  191.             ]);
  192.             $this->response->body($body);
  193.             return $this->response;
  194.         }
  195.  
  196.         $api = $this->request->query['api'];
  197.         $url = $this->request->query['url'];
  198.  
  199.         $user = $this->Links->Users->find()->where(['api_token' => $api, 'status' => 'active'])->first();
  200.  
  201.         if (!$user) {
  202.             $body = json_encode([
  203.                 'status' => 'error',
  204.                 'message' => 'Invalid API token',
  205.                 'shortenedUrl' => ''
  206.             ]);
  207.             $this->response->body($body);
  208.             return $this->response;
  209.         }
  210.  
  211.         $url = parse_url($url, PHP_URL_SCHEME) === null ? 'http://' . $url : $url;
  212.  
  213.         $link = $this->Links->find()->where(['url' => $url, 'user_id' => $user->id])->first();
  214.  
  215.         if ($link) {
  216.             $body = json_encode([
  217.                 'status' => 'success',
  218.                 'shortenedUrl' => get_short_url($link->alias)
  219.             ]);
  220.             $this->response->body($body);
  221.             return $this->response;
  222.         }
  223.  
  224.         $link = $this->Links->newEntity();
  225.         $data = [];
  226.  
  227.         $data['user_id'] = $user->id;
  228.         $data['url'] = $url;
  229.         $data['alias'] = $this->Links->geturl();
  230.         $link->status = 'active';
  231.         $link->hits = 0;
  232.  
  233.         $linkMeta = $this->Links->getLinkMeta($url);
  234.  
  235.         $link->title = $linkMeta['title'];
  236.         $link->description = $linkMeta['description'];
  237.         $link->image = $linkMeta['image'];
  238.  
  239.         $link = $this->Links->patchEntity($link, $data);
  240.  
  241.         if ($this->Links->save($link)) {
  242.             $body = json_encode([
  243.                 'status' => 'success',
  244.                 'shortenedUrl' => get_short_url($link->alias)
  245.             ]);
  246.             $this->response->body($body);
  247.             return $this->response;
  248.         }
  249.  
  250.         $body = json_encode([
  251.             'status' => 'error',
  252.             'message' => 'Invalid URL',
  253.             'shortenedUrl' => ''
  254.         ]);
  255.         $this->response->body($body);
  256.         return $this->response;
  257.     }
  258.  
  259.     public function view($alias = null)
  260.     {
  261.         if (!$alias) {
  262.             throw new NotFoundException(__('Invalid link'));
  263.         }
  264.  
  265.         $use_redirector = isset($this->request->session()->read('use_redirector'))
  266.             ? $this->request->session()->read('use_redirector')
  267.             : true;
  268.  
  269.         if ($use_redirector) {
  270.             $redirect_url = 'https://coindows.com/linkdows/';
  271.             $shortened_link = 'https://linkdows.pw/'.$alias;
  272.  
  273.             $this->request->session()->write('use_redirector', false);
  274.             $hash = base64_encode($shortened_link);
  275.  
  276.             return $this->redirect($redirect_url.'?hash='.$hash);
  277.         }
  278.  
  279.         //$link = $this->Links->find()->where( ['alias' => $alias, 'status' => 1] )->contain(['Users'])->first();
  280.         $link = $this->Links->find()->where(['alias' => $alias, 'status <>' => 'inactive'])->first();
  281.         if (!$link) {
  282.             throw new NotFoundException(__('404 Not Found'));
  283.         }
  284.         $this->set('link', $link);
  285.  
  286.         $user = $this->Links->Users->find()->where(['id' => $link->user_id, 'status' => 'active'])->first();
  287.         if (!$user) {
  288.             throw new NotFoundException(__('404 Not Found'));
  289.         }
  290.  
  291.         // No Ads
  292.         if ($link->ad_type == 0) {
  293.             return $this->redirect($link->url);
  294.         }
  295.  
  296.         $this->viewBuilder()->layout('captcha');
  297.         $this->render('captcha');
  298.  
  299.         if (!( (get_option('enable_captcha_shortlink') == 'yes') && isset_recaptcha() ) || $this->request->is('post')) {
  300.  
  301.             if ((get_option('enable_captcha_shortlink') == 'yes') && isset_recaptcha() && !$this->Recaptcha->verify($this->request->data['g-recaptcha-response'])) {
  302.                 throw new BadRequestException(__('The CAPTCHA was incorrect. Try again'));
  303.             }
  304.  
  305.             //env('HTTP_REFERER', $this->request->data['ref']);
  306.  
  307.             $_SERVER['HTTP_REFERER'] = (!( (get_option('enable_captcha_shortlink') == 'yes') && isset_recaptcha() )) ? env('HTTP_REFERER') : $this->request->data['ref'];
  308.  
  309.             $this->_setVisitorCookie();
  310.  
  311.             $country = $this->Links->Statistics->get_country(get_ip());
  312.  
  313.             $detector = new \Detection\MobileDetect();
  314.             if ($detector->isMobile()) {
  315.                 $traffic_source = 3;
  316.             } else {
  317.                 $traffic_source = 2;
  318.             }
  319.  
  320.             $CampaignItems = TableRegistry::get('CampaignItems');
  321.  
  322.             $campaign_items = $CampaignItems->find()
  323.                 ->contain(['Campaigns'])
  324.                 ->where([
  325.                     'Campaigns.ad_type' => $link->ad_type,
  326.                     'CampaignItems.weight <' => 100,
  327.                     'Campaigns.status' => 'Active',
  328.                     "Campaigns.traffic_source IN (1, :traffic_source)",
  329.                     "CampaignItems.country IN ( 'all', :country)",
  330.                     'Campaigns.default_campaign' => 0,
  331.                     //'Campaigns.user_id <>' => $link->user_id
  332.                 ])
  333.                 ->order(['CampaignItems.weight' => 'ASC'])
  334.                 ->bind(':traffic_source', $traffic_source, 'integer')
  335.                 ->bind(':country', $country, 'string')
  336.                 ->limit(10)
  337.                 ->toArray();
  338.  
  339.             if (count($campaign_items) == 0) {
  340.                 $campaign_items = $CampaignItems->find()
  341.                     ->contain(['Campaigns'])
  342.                     ->where([
  343.                         'Campaigns.ad_type' => $link->ad_type,
  344.                         'CampaignItems.weight <' => 100,
  345.                         'Campaigns.status' => 'Active',
  346.                         "Campaigns.traffic_source IN (1, :traffic_source)",
  347.                         "CampaignItems.country IN ( 'all', :country)",
  348.                         'Campaigns.default_campaign' => 1,
  349.                         //'Campaigns.user_id <>' => $link->user_id
  350.                     ])
  351.                     ->order(['CampaignItems.weight' => 'ASC'])
  352.                     ->bind(':traffic_source', $traffic_source, 'integer')
  353.                     ->bind(':country', $country, 'string')
  354.                     ->limit(10)
  355.                     ->toArray();
  356.             }
  357.  
  358.             shuffle($campaign_items);
  359.             $campaign_item = array_values($campaign_items)[0];
  360.  
  361.             $this->set('campaign_item', $campaign_item);
  362.  
  363.             // Interstitial Ads
  364.             if ($link->ad_type == 1) {
  365.                 $this->viewBuilder()->layout('go_interstitial');
  366.                 $this->render('view_interstitial');
  367.             }
  368.  
  369.             // Banner Ads
  370.             if ($link->ad_type == 2) {
  371.  
  372.                 $banner_728x90 = get_option('banner_728x90', '');
  373.                 if ('728x90' == $campaign_item->campaign->banner_size) {
  374.                     $banner_728x90 = $campaign_item->campaign->banner_code;
  375.                 }
  376.  
  377.                 $banner_468x60 = get_option('banner_468x60', '');
  378.                 if ('468x60' == $campaign_item->campaign->banner_size) {
  379.                     $banner_468x60 = $campaign_item->campaign->banner_code;
  380.                 }
  381.  
  382.                 $banner_336x280 = get_option('banner_336x280', '');
  383.                 if ('336x280' == $campaign_item->campaign->banner_size) {
  384.                     $banner_336x280 = $campaign_item->campaign->banner_code;
  385.                 }
  386.  
  387.                 $this->set('banner_728x90', $banner_728x90);
  388.                 $this->set('banner_468x60', $banner_468x60);
  389.                 $this->set('banner_336x280', $banner_336x280);
  390.  
  391.                 $this->viewBuilder()->layout('go_banner');
  392.                 $this->render('view_banner');
  393.             }
  394.         }
  395.     }
  396.  
  397.     public function go()
  398.     {
  399.         $this->autoRender = false;
  400.         $this->response->type('json');
  401.  
  402.         if (!$this->request->is('ajax')) {
  403.             $content = [
  404.                 'status' => 'error',
  405.                 'message' => 'Bad Request.',
  406.                 'url' => ''
  407.             ];
  408.             $this->response->body(json_encode($content));
  409.             return $this->response;
  410.         }
  411.  
  412.         $link = $this->Links->find()->contain(['Users'])->where([
  413.                 'Links.alias' => $this->request->data['alias'],
  414.                 'Links.status <>' => 'inactive'
  415.             ])->first();
  416.         if (!$link) {
  417.             $content = [
  418.                 'status' => 'error',
  419.                 'message' => '404 Not Found.',
  420.                 'url' => ''
  421.             ];
  422.             $this->response->body(json_encode($content));
  423.             return $this->response;
  424.         }
  425.  
  426.         /**
  427.          * Views reasons
  428.          * 1- Earn
  429.          * 2- Disabled cookie
  430.          * 3- Anonymous user
  431.          * 4- Adblock
  432.          * 5- Proxy
  433.          * 6- IP changed
  434.          * 7- Not unique
  435.          * 8- Full weight
  436.          * 9- Default campaign
  437.          */
  438.        
  439.         /**
  440.          * Check if cookie valid
  441.          */
  442.         $cookie = $this->Cookie->read('visitor');
  443.         if (!is_array($cookie)) {
  444.             // Update link hits
  445.             $this->_updateLinkHits($link);
  446.             $this->_addNormalStatisticEntry($link, $this->request->data, $cookie, 2);
  447.             $content = [
  448.                 'status' => 'success',
  449.                 'message' => 'Go without Earn because no cookie',
  450.                 'url' => $link->url
  451.             ];
  452.             $this->response->body(json_encode($content));
  453.             return $this->response;
  454.             //return debug( 'Go without Earn because no cookie' );
  455.             //return $this->redirect( $link->url );
  456.         }
  457.  
  458.         /**
  459.          * Check if anonymous user
  460.          */
  461.         if ('anonymous' == $link->user->username) {
  462.             // Update link hits
  463.             $this->_updateLinkHits($link);
  464.             $this->_addNormalStatisticEntry($link, $this->request->data, $cookie, 3);
  465.             $content = [
  466.                 'status' => 'success',
  467.                 'message' => 'Go without Earn because anonymous user',
  468.                 'url' => $link->url
  469.             ];
  470.             $this->response->body(json_encode($content));
  471.             return $this->response;
  472.             //return debug( 'Go without Earn because no cookie' );
  473.             //return $this->redirect( $link->url );
  474.         }
  475.  
  476.         /**
  477.          * Check for Adblock
  478.          */
  479.         if (!empty($this->request->cookie('adblockUser'))) {
  480.             // Update link hits
  481.             $this->_updateLinkHits($link);
  482.             $this->_addNormalStatisticEntry($link, $this->request->data, $cookie, 4);
  483.             $content = [
  484.                 'status' => 'success',
  485.                 'message' => 'Go without Earn because Adblock',
  486.                 'url' => $link->url
  487.             ];
  488.             $this->response->body(json_encode($content));
  489.             return $this->response;
  490.             //return debug( 'Go without Earn because no cookie' );
  491.             //return $this->redirect( $link->url );
  492.         }
  493.  
  494.         /**
  495.          * Check if proxy
  496.          */
  497.         /*
  498.           if (!isset($_SERVER["HTTP_CF_CONNECTING_IP"]) && $this->_isProxy()) {
  499.           // Update link hits
  500.           $this->_updateLinkHits($link);
  501.           $this->_addNormalStatisticEntry($link, $this->request->data, $cookie, 5);
  502.           $content = [
  503.           'status' => 'success',
  504.           'message' => 'Go without Earn because proxy',
  505.           'url' => $link->url
  506.           ];
  507.           $this->response->body(json_encode($content));
  508.           return $this->response;
  509.           //return debug( 'Go without Earn because proxy' );
  510.           //return $this->redirect( $link->url );
  511.           }
  512.          */
  513.  
  514.         /**
  515.          * Check if IP changed
  516.          */
  517.         if ($cookie['ip'] != get_ip()) {
  518.             // Update link hits
  519.             $this->_updateLinkHits($link);
  520.             $this->_addNormalStatisticEntry($link, $this->request->data, $cookie, 6);
  521.             $content = [
  522.                 'status' => 'success',
  523.                 'message' => 'Go without Earn because IP changed',
  524.                 'url' => $link->url
  525.             ];
  526.             $this->response->body(json_encode($content));
  527.             return $this->response;
  528.             //return debug( 'Go without Earn because IP changed' );
  529.             //return $this->redirect( $link->url );
  530.         }
  531.  
  532.         /**
  533.          * Check for unique vistits within last 24 hour
  534.          */
  535.         $startOfToday = Time::today()->format('Y-m-d H:i:s');
  536.         $endOfToday = Time::now()->endOfDay()->format('Y-m-d H:i:s');
  537.  
  538.         $statistics = $this->Links->Statistics->find()
  539.             ->where([
  540.                 'Statistics.ip' => $cookie['ip'],
  541.                 'Statistics.campaign_id' => $this->request->data['ci'],
  542.                 'Statistics.publisher_earn >' => 0,
  543.                 'Statistics.created BETWEEN :startOfToday AND :endOfToday'
  544.             ])
  545.             ->bind(':startOfToday', $startOfToday, 'datetime')
  546.             ->bind(':endOfToday', $endOfToday, 'datetime')
  547.             ->count();
  548.  
  549.         if ($statistics >= get_option('campaign_paid_views_day', 1)) {
  550.             // Update link hits
  551.             $this->_updateLinkHits($link);
  552.             $this->_addNormalStatisticEntry($link, $this->request->data, $cookie, 7);
  553.             $content = [
  554.                 'status' => 'success',
  555.                 'message' => 'Go without Earn because Not unique.',
  556.                 'url' => $link->url
  557.             ];
  558.             $this->response->body(json_encode($content));
  559.             return $this->response;
  560.             //return debug( 'Go without Earn because Not unique.' );
  561.             //return $this->redirect( $link->url );
  562.         }
  563.  
  564.  
  565.         /**
  566.          * Check Campaign Item weight
  567.          */
  568.         $CampaignItems = TableRegistry::get('CampaignItems');
  569.  
  570.         $campaign_item = $CampaignItems->find()
  571.             ->contain(['Campaigns'])
  572.             ->where(['CampaignItems.id' => $this->request->data['cii']])
  573.             ->where(['CampaignItems.weight <' => 100])
  574.             ->where(['Campaigns.status' => 'Active'])
  575.             ->first();
  576.  
  577.  
  578.         if (!$campaign_item) {
  579.             // Update link hits
  580.             $this->_updateLinkHits($link);
  581.             $this->_addNormalStatisticEntry($link, $this->request->data, $cookie, 8);
  582.             $content = [
  583.                 'status' => 'success',
  584.                 'message' => 'Go without Earn because Campaign Item weight is full.',
  585.                 'url' => $link->url
  586.             ];
  587.             $this->response->body(json_encode($content));
  588.             return $this->response;
  589.             //return debug( 'Go without Earn because Campaign Item weight is full.' );
  590.             //return $this->redirect( $link->url );
  591.         }
  592.  
  593.         /**
  594.          * Check if default campaign
  595.          */
  596.         if ($campaign_item->campaign->default_campaign) {
  597.             // Update link hits
  598.             $this->_updateLinkHits($link);
  599.             $this->_addNormalStatisticEntry($link, $this->request->data, $cookie, 9);
  600.             $content = [
  601.                 'status' => 'success',
  602.                 'message' => 'Go without Earn because Default Campaign.',
  603.                 'url' => $link->url
  604.             ];
  605.             $this->response->body(json_encode($content));
  606.             return $this->response;
  607.             //return debug( 'Go without Earn because Default Campaign.' );
  608.             //return $this->redirect( $link->url );
  609.         }
  610.  
  611.         /**
  612.          * Add statistic record
  613.          */
  614.         $country = $this->Links->Statistics->get_country($cookie['ip']);
  615.  
  616.         $statistic = $this->Links->Statistics->newEntity();
  617.  
  618.         $statistic->link_id = $link->id;
  619.         $statistic->user_id = $link->user_id;
  620.         $statistic->ad_type = $campaign_item['campaign']['ad_type'];
  621.         $statistic->campaign_id = $campaign_item['campaign']['id'];
  622.         $statistic->campaign_user_id = $campaign_item['campaign']['user_id'];
  623.         $statistic->campaign_item_id = $campaign_item['id'];
  624.         $statistic->ip = $cookie['ip'];
  625.         $statistic->country = $country;
  626.         $statistic->owner_earn = ($campaign_item['advertiser_price'] - $campaign_item['publisher_price']) / 1000;
  627.         $statistic->publisher_earn = $campaign_item['publisher_price'] / 1000;
  628.         $statistic->referer_domain = (parse_url($this->request->data['ref'], PHP_URL_HOST) ? parse_url($this->request->data['ref'], PHP_URL_HOST) : 'Direct');
  629.         $statistic->referer = $this->request->data['ref'];
  630.         $statistic->user_agent = env('HTTP_USER_AGENT');
  631.         $statistic->reason = 1;
  632.         $this->Links->Statistics->save($statistic);
  633.  
  634.         $user_update = $this->Links->Users->get($link->user_id);
  635.         $user_update->publisher_earnings += $campaign_item['publisher_price'] / 1000;
  636.  
  637.         $this->Links->Users->save($user_update);
  638.  
  639.         if (!empty($user_update->referred_by)) {
  640.             $user_referred_by = $this->Links->Users->get($user_update->referred_by);
  641.             $referral_percentage = get_option('referral_percentage', 20) / 100;
  642.             $user_referred_by->referral_earnings += ( $campaign_item['publisher_price'] / 1000 ) * $referral_percentage;
  643.  
  644.             $this->Links->Users->save($user_referred_by);
  645.         }
  646.  
  647.         /**
  648.          * Update campaign item views and weight
  649.          */
  650.         $campaign_item_update = $CampaignItems->newEntity();
  651.         $campaign_item_update->id = $campaign_item['id'];
  652.         $campaign_item_update->views = $campaign_item['views'] + 1;
  653.         $campaign_item_update->weight = (($campaign_item['views'] + 1) / ($campaign_item['purchase'] * 1000)) * 100;
  654.         $CampaignItems->save($campaign_item_update);
  655.  
  656.         // Update link hits
  657.         $this->_updateLinkHits($link);
  658.         $content = [
  659.             'status' => 'success',
  660.             'message' => 'Go With earning :)',
  661.             'url' => $link->url
  662.         ];
  663.         $this->request->session()->delete('use_redirector'));
  664.         $this->response->body(json_encode($content));
  665.         return $this->response;
  666.         //debug( "Go to URL $link->url With earning :)" );
  667.         //return $this->redirect( $link->url );
  668.     }
  669.  
  670.     protected function _addNormalStatisticEntry($link, $data, $cookie, $reason = 0)
  671.     {
  672.         $ip = get_ip();
  673.         if (is_array($cookie)) {
  674.             $ip = $cookie['ip'];
  675.         }
  676.         $country = $this->Links->Statistics->get_country($cookie['ip']);
  677.  
  678.         $statistic = $this->Links->Statistics->newEntity();
  679.  
  680.         $statistic->link_id = $link->id;
  681.         $statistic->user_id = $link->user_id;
  682.         $statistic->ad_id = 1;
  683.         $statistic->campaign_id = $data['ci'];
  684.         $statistic->campaign_user_id = $data['cui'];
  685.         $statistic->campaign_item_id = $data['cii'];
  686.         $statistic->ip = $ip;
  687.         $statistic->country = $country;
  688.         $statistic->owner_earn = 0;
  689.         $statistic->publisher_earn = 0;
  690.         $statistic->referer_domain = (parse_url($data['ref'], PHP_URL_HOST) ? parse_url($data['ref'], PHP_URL_HOST) : 'Direct');
  691.         $statistic->referer = $data['ref'];
  692.         $statistic->user_agent = env('HTTP_USER_AGENT');
  693.         $statistic->reason = $reason;
  694.         $this->Links->Statistics->save($statistic);
  695.     }
  696.  
  697.     protected function _setVisitorCookie()
  698.     {
  699.         $cookie = $this->Cookie->read('visitor');
  700.  
  701.         if (isset($cookie)) {
  702.             return true;
  703.         }
  704.  
  705.         $cookie_data = [
  706.             'ip' => get_ip(),
  707.             'date' => (new Time())->toDateTimeString()
  708.         ];
  709.         $this->Cookie->configKey('visitor', [
  710.             'expires' => '+1 day',
  711.             'httpOnly' => true
  712.         ]);
  713.         $this->Cookie->write('visitor', $cookie_data);
  714.  
  715.         return true;
  716.     }
  717.  
  718.     protected function _updateLinkHits($link = null)
  719.     {
  720.         if (!$link) {
  721.             return;
  722.         }
  723.         $link->hits += 1;
  724.         $link->modified = $link->modified;
  725.         $this->Links->save($link);
  726.         return;
  727.     }
  728.  
  729.     protected function _isProxy()
  730.     {
  731.         $proxy_headers = [
  732.             'HTTP_VIA',
  733.             'HTTP_X_FORWARDED_FOR',
  734.             'HTTP_FORWARDED_FOR',
  735.             'HTTP_X_FORWARDED',
  736.             'HTTP_FORWARDED',
  737.             'HTTP_CLIENT_IP',
  738.             'HTTP_FORWARDED_FOR_IP',
  739.             'VIA',
  740.             'X_FORWARDED_FOR',
  741.             'FORWARDED_FOR',
  742.             'X_FORWARDED',
  743.             'FORWARDED',
  744.             'CLIENT_IP',
  745.             'FORWARDED_FOR_IP',
  746.             'HTTP_PROXY_CONNECTION'
  747.         ];
  748.         foreach ($proxy_headers as $proxy_header) {
  749.             if (isset($_SERVER[$proxy_header])) {
  750.                 return true;
  751.             }
  752.         }
  753.         return false;
  754.     }
  755.  
  756.     protected function _getBaseURL()
  757.     {
  758.         if(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on') {
  759.             $link = "https";
  760.         } else {
  761.             $link = "http";
  762.         }
  763.  
  764.         $link .= "://";
  765.         $link .= $_SERVER['HTTP_HOST'];
  766.         $link .= $_SERVER['REQUEST_URI'];
  767.  
  768.         return $link;
  769.     }
  770.  
  771.     protected function _getRandomHash($length)
  772.     {
  773.         $hash = '';
  774.         if ($length > 2) {
  775.             $alphabet = str_split('qwertyuiopasdfghjklzxcvbnm');
  776.             for ($i = 0; $i < 2; $i++) {
  777.                 $hash .= $alphabet[array_rand($alphabet)];
  778.             }
  779.             $length -= 2;
  780.         }
  781.         $alphabet = str_split('qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890');
  782.         for ($i = 0; $i < $length; $i++) {
  783.             $hash .= $alphabet[array_rand($alphabet)];
  784.         }
  785.         return $hash;
  786.     }
  787. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement