Application.php
1    <?php 
2    defined( 'BASEPATH' ) OR exit( 'No direct script access allowed' ); 
3     
4     
5 class
Application extends CI_Controller { 6 7 public $settings = NULL; 8 public $number_of_opens_per_session = NULL; 9 public $starting_hour = NULL; 10 public $ending_hour = NULL; 11 public $min_seconds_from_last_click = NULL; 12 public $ip_address = NULL; 13 public $chance_of_click = NULL; 14 15 /** 16 * Application constructor. 17 * Do inicialization of necessary variables. 18 */ 19 public function __construct() { 20 parent::__construct(); 21 $this->load->model( 'Clicks_model' ); 22 $this->load->model( 'Scheduller_model' ); 23 $this->load->model( 'Settings_model' ); 24 $this->load->model( 'Webs_model' ); 25 $this->load->model( 'Logs_model' ); 26 $this->settings = $this->Settings_model->get_settings(); 27 $this->number_of_opens_per_session = $this->settings['number_of_opens_per_session']; 28 $this->starting_hour = $this->settings['starting_hour']; 29 $this->ending_hour = $this->settings['ending_hour']; 30 $this->chance_of_click = $this->settings['chance_of_click']; 31 $this->min_seconds_from_last_click = (int) $this->settings['min_seconds_from_last_click']; 32 } 33 34 public function index() { 35 if ( ! $this->is_settings_exists() ) { 36 exec( 'sudo python ./ipchange.py' ); 37 sleep( 110 ); 38 $this->get_settings_from_remote(); 39 sleep( 20 ); 40 exit; 41 } 42 echo "Last settings update was: " . $this->Settings_model->get_settings_last_update() . "<br>"; 43 if ( ! $this->is_sheduller_exists() ) { 44 // Scheduller does not exists, let's create one for today. 45 $scheduller = $this->create_scheduller(); 46 $this->insert_scheduller_to_database( $scheduller ); 47 $this->Logs_model->log( 0 ); 48 echo "Scheduller was created."; 49 } else { 50 // Scheduller exists, let's check if is click time. 51 if ( $click = $this->is_click_time() ) { 52 exec( 'sudo python ./ipchange.py' ); 53 sleep( 110 ); 54 if ( $this->is_ipadress_unique_today() ) { 55 // Save click to database and redirect. 56 if ( $this->save_click_to_database( $click['id'] ) ) { 57 $p = mt_rand( 0, 99 ); 58 if ( $p < $this->chance_of_click * 100 ) { 59 exec( 'sudo python ./clicker.py' ); 60 $this->Logs_model->log( 4 ); 61 } else { 62 $this->Logs_model->log( 7 ); 63 } 64 exec( 'sudo python ./leonardo.py' ); 65 if ( ( strpos( $click['url'], 'cervenecocky' ) !== FALSE ) || ( strpos( $click['url'], 'sosovky' ) !== FALSE ) ) { 66 exec( 'sudo python ./sosovky.py' ); 67 } 68 redirect( 'http://' . $click['url'] ); 69 } else { 70 $this->Logs_model->log( 1 ); 71 echo "Click to database was not inserted correctly."; 72 } 73 } else { 74 $this->Logs_model->log( 2 ); 75 echo "Ip adress is not unique."; 76 } 77 } else { 78 $this->Logs_model->log( 3 ); 79 exec( 'sudo python ./gpioclean.py' ); 80 echo "Is not time to click."; 81 } 82 } 83 } 84 85 /** 86 * Create scheduller for today. 87 * Returns array of planned clicks. 88 * 89 * @param (string) $web 90 * 91 * @return array 92 */ 93 public function create_scheduller() { 94 $web_ids = $this->Webs_model->get_web_ids(); 95 $schedullers = []; 96 for ( $i = 0; $i < $this->number_of_opens_per_session; $i ++ ) { 97 $click = [ 98 'web' => $web_ids[ mt_rand( 0, count( $web_ids ) - 1 ) ], 99 'time_to_click' => 100 $this->convert_to_timestamp( 101 mt_rand( $this->starting_hour, $this->ending_hour ), 102 mt_rand( 0, 60 ), 0 ) 103 104 ]; 105 // First scheduller don't need verifying. 106 if ( count( $schedullers ) == 0 ) { 107 $schedullers[] = $click; 108 continue; 109 } 110 // Check if minimal allowed difference in time is not overflowed. 111 for ( $j = 0; $j < count( $schedullers ); $j ++ ) { 112 $diff = abs( $schedullers[ $j ]['time_to_click'] - $click['time_to_click'] ); 113 if ( $diff >= $this->min_seconds_from_last_click ) { 114 // Time is in allowed interval. 115 $schedullers[] = $click; 116 break; 117 } else { 118 // Time is not in allowed interval. Let's find new one. 119 do { 120 $click = [ 121 'web' => $web_ids[ mt_rand( 0, count( $web_ids ) - 1 ) ], 122 'time_to_click' => 123 $this->convert_to_timestamp( 124 mt_rand( $this->starting_hour, $this->ending_hour ), 125 mt_rand( 0, 60 ), 0 ) 126 ]; 127 $diff = abs( $schedullers[ $j ]['time_to_click'] - $click['time_to_click'] ); 128 } while ( $diff >= $this->min_seconds_from_last_click ); 129 $schedullers[] = $click; 130 break; 131 } 132 } 133 } 134 135 return $schedullers; 136 } 137 138 /** 139 * Insert scheduller to database. 140 * 141 * @param $scheduller 142 * 143 * @return bool 144 */ 145 public function insert_scheduller_to_database( $scheduller ) { 146 // Convert timestamp to readable format for MySQL. 147 for ( $i = 0; $i < count( $scheduller ); $i ++ ) { 148 $scheduller[ $i ]['time_to_click'] = date( "Y-m-d H:i:s", $scheduller[ $i ]['time_to_click'] ); 149 } 150 // Insert data to database. 151 $this->db->insert_batch( 'scheduller', $scheduller ); 152 if ( $this->db->affected_rows() ) { 153 return TRUE; 154 } else { 155 return FALSE; 156 } 157 } 158 159 /** 160 * Log click to database. 161 * 162 * @param $web_id 163 * 164 * @return bool 165 */ 166 public function save_click_to_database( $web_id ) { 167 $now = Carbon::now(); 168 $data = [ 169 'click_timestamp' => $now->format( 'Y-m-d H:i:s' ), 170 'web_id' => $web_id, 171 'ip_adress' => $this->get_ip_adress_from_remote() 172 ]; 173 if ( $this->db->insert( 'clicks', $data ) ) { 174 return TRUE; 175 } else { 176 return FALSE; 177 } 178 } 179 180 /** 181 * Check if scheduller for current day exists. 182 * If yes return TRUE. 183 * If not return FALSE. 184 * 185 * @return bool 186 */ 187 public function is_sheduller_exists() { 188 $date = new DateTime( "now" ); 189 $curr_date = $date->format( 'Y-m-d ' ); 190 $this->db->select( '*' ); 191 $this->db->from( 'scheduller' ); 192 $this->db->like( 'time_to_click', $curr_date ); 193 $this->db->limit( 1 ); 194 if ( count( $this->db->get()->result() ) ) { 195 return TRUE; 196 } else { 197 return FALSE; 198 } 199 } 200 201 /** 202 * Determine if is click time. 203 * If yes, returns datetime of click and web url. 204 * If no, returns FALSE. 205 * 206 * @return array|bool 207 */ 208 public function is_click_time() { 209 // Get schedule for today. 210 $date = new DateTime( "now" ); 211 $curr_date = $date->format( 'Y-m-d' ); 212 $query = $this->db->query( "SELECT w.id,w.url,sub.time_to_click,sub.id as scheduller_id FROM 213 (SELECT * 214 FROM `scheduller` 215 WHERE `time_to_click` LIKE '%$curr_date%') as sub 216 JOIN webs as w ON sub.web=w.id;" ); 217 $scheduller = $query->result_array(); 218 // Check if it's time to click. 219 $now = Carbon::now(); 220 //$now = Carbon::create( '2016', '8', '27', '12', '15', '0', NULL ); // Custom date just for testin. 221 foreach ( $scheduller as $click ) { 222 $click_timestamp = strtotime( $click['time_to_click'] ); 223 $click_time = Carbon::createFromTimestamp( $click_timestamp ); 224 if ( 225 $now->year == $click_time->year && 226 $now->day == $click_time->day && 227 $now->hour == $click_time->hour 228 ) { 229 // If is in interval of 10 minutes 230 $decreased_time = $click_time->modify( '-10 minutes' )->minute; 231 $increased_time = $click_time->modify( '+11 minutes' )->minute; 232 if ( ( $decreased_time <= $now->minute ) && ( $now->minute <= $increased_time ) ) { 233 // is Time to click! 234 $this->Scheduller_model->delete_scheduller_row( $click['scheduller_id'] ); 235 236 return $click; 237 } else { 238 239 } 240 } 241 } 242 243 return FALSE; 244 } 245 246 /** 247 * Get settings from remote server. 248 * Only once per day. 249 */ 250 public function get_settings_from_remote() { 251 if ( ! $this->Settings_model->get_settings_last_update() ) { 252 $datetime = FALSE; 253 } else { 254 $datetime = Carbon::parse( $this->Settings_model->get_settings_last_update() ); 255 } 256 $now = Carbon::now(); 257 $this->Logs_model->log( 8 ); 258 echo "Updatujem nastavenia"; 259 //var_dump($datetime->day); 260 if ( isset( $datetime->day ) ) { 261 if ( $datetime->day === $now->day ) { 262 // We have parser setting already for today 263 $this->Logs_model->log( 6 ); 264 265 return; 266 } 267 } 268 if ( $this->get_http_response_code( 'http://get_heurekaautomat.stranovsky.sk/settings.js' ) != "200" ) { 269 echo "error"; 270 $this->Logs_model->log( 5 ); 271 272 return; 273 } else { 274 $settings = file_get_contents( 'http://get_heurekaautomat.stranovsky.sk/settings.js' ); 275 } 276 $settings = json_decode( $settings ); 277 $data = array( 278 'number_of_opens_per_session' => $settings->clicksPerDay, 279 'starting_hour' => $settings->startHour, 280 'ending_hour' => $settings->endHour, 281 'min_seconds_from_last_click' => $settings->min_seconds_from_last_click, 282 'clicking' => $settings->clicking, 283 'chance_of_click' => $settings->chanceOfClick, 284 'updated_at' => $now->format( 'Y-m-d H:i:s' ) 285 ); 286 if ( $datetime ) { 287 $settings_id = $this->db->query( 'SELECT * FROM settings ORDER BY id DESC LIMIT 1' )->result_array(); 288 $this->db->where( 'id', $settings_id[0]['id'] ); 289 $this->db->update( 'settings', $data ); 290 } else { 291 $this->db->insert( 'settings', $data ); 292 } 293 // Now update table webs 294 $this->db->empty_table( 'webs' ); 295 $data = []; 296 foreach ( $settings->webpages as $webpage ) { 297 $data[] = [ 298 'url' => $webpage, 299 'active' => 1 300 ]; 301 } 302 $this->db->insert_batch( 'webs', $data ); 303 $this->db->empty_table( 'scheduller' ); 304 } 305 306 /** 307 * Connect to remote server to get client IP adress. 308 * 309 * @return bool|string 310 */ 311 public function get_ip_adress_from_remote() { 312 if ( $this->ip_address ) { 313 return $this->ip_address; 314 } else { 315 try { 316 $this->ip_address = file_get_contents( 'http://www.getipadress.stranovsky.sk/' ); 317 } catch( Exception $ex ) { 318 // No internet or other error 319 $this->Logs_model->log( 5 ); 320 exit; 321 } 322 if ( is_string( $this->ip_address ) ) { 323 return $this->ip_address; 324 } else { 325 return FALSE; 326 } 327 } 328 } 329 330 /** 331 * Check wether the system is connected to the internet. 332 * 333 * @param $url 334 * 335 * @return bool|string 336 */ 337 public function get_http_response_code( $url ) { 338 $headers = get_headers( $url ); 339 340 return substr( $headers[0], 9, 3 ); 341 } 342 343 /** 344 * Check if client IP adress is unique for today. 345 * 346 * @return bool 347 */ 348 public function is_ipadress_unique_today() { 349 $date = new DateTime( "now" ); 350 $curr_date = $date->format( 'Y-m-d' ); 351 $ip_adress = $this->get_ip_adress_from_remote(); 352 $query = $this->db->query( "SELECT sub.click_timestamp, sub.ip_adress, w.url FROM 353 (SELECT * FROM clicks WHERE `click_timestamp` LIKE '%$curr_date%' and `ip_adress` = '$ip_adress') as sub 354 JOIN webs as w ON sub.web_id = w.id; " ); 355 if ( ! $query->num_rows() ) { 356 return TRUE; 357 } else { 358 return FALSE; 359 } 360 } 361 } 362