Ejemplo n.º 1
0
 /**
  * Handle the event.
  *
  * @param  DhcpSubnetWasAdded  $event
  * @return void
  */
 public function handle(DhcpSubnetWasAdded $event)
 {
     $subnet = $event->dhcp_subnet;
     $start_ip = IP\Address::factory($event->start_ip);
     $end_ip = IP\Address::factory($event->end_ip);
     $network = IPv4\NetworkAddress::factory($subnet->network_address, $subnet->cidr);
     foreach ($network as $ip) {
         $address = $ip->__toString();
         if (($ip->compare_to($start_ip) == 0 || $ip->compare_to($start_ip) == 1) && ($ip->compare_to($end_ip) == 0 || $ip->compare_to($end_ip) == -1)) {
             $subnet->ip_addresses()->create(['address' => $address]);
         }
     }
 }
function podlove_handle_media_file_tracking(\Podlove\Model\MediaFile $media_file)
{
    if (\Podlove\get_setting('tracking', 'mode') !== "ptm_analytics") {
        return;
    }
    if (strtoupper($_SERVER['REQUEST_METHOD']) === 'HEAD') {
        return;
    }
    $intent = new Model\DownloadIntent();
    $intent->media_file_id = $media_file->id;
    $intent->accessed_at = date('Y-m-d H:i:s');
    $ptm_source = trim(podlove_get_query_var('ptm_source'));
    $ptm_context = trim(podlove_get_query_var('ptm_context'));
    if ($ptm_source) {
        $intent->source = $ptm_source;
    }
    if ($ptm_context) {
        $intent->context = $ptm_context;
    }
    // set user agent
    $ua_string = trim($_SERVER['HTTP_USER_AGENT']);
    if ($agent = Model\UserAgent::find_or_create_by_uastring($ua_string)) {
        $intent->user_agent_id = $agent->id;
    }
    // save HTTP range header
    // @see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.35 for spec
    if (isset($_SERVER['HTTP_RANGE'])) {
        $intent->httprange = $_SERVER['HTTP_RANGE'];
    }
    // get ip, but don't store it
    $ip_string = $_SERVER['REMOTE_ADDR'];
    try {
        $ip = IP\Address::factory($_SERVER['REMOTE_ADDR']);
        if (method_exists($ip, 'as_IPv6_address')) {
            $ip = $ip->as_IPv6_address();
        }
        $ip_string = $ip->format(IP\Address::FORMAT_COMPACT);
    } catch (\InvalidArgumentException $e) {
        \Podlove\Log::get()->addWarning('Could not use IP "' . $_SERVER['REMOTE_ADDR'] . '"' . $e->getMessage());
    }
    // Generate a hash from IP address and UserAgent so we can identify
    // identical requests without storing an IP address.
    if (function_exists('openssl_digest')) {
        $intent->request_id = openssl_digest($ip_string . $ua_string, 'sha256');
    } else {
        $intent->request_id = sha1($ip_string . $ua_string);
    }
    $intent = $intent->add_geo_data($ip_string);
    $intent->save();
}
Ejemplo n.º 3
0
 /**
  * @param $pathToImportFile
  * @return array
  */
 public function import($pathToImportFile)
 {
     if (($handle = fopen($pathToImportFile, "r")) !== FALSE) {
         $this->validateImportFile($pathToImportFile);
         $this->getExistingSubnets();
         $failureLogName = tempnam(getcwd() . "/log_output", "ip_pool_import_failures");
         $failureLog = fopen($failureLogName, "w");
         $successLogName = tempnam(getcwd() . "/log_output", "ip_pool_import_successes");
         $successLog = fopen($successLogName, "w");
         $returnData = ['successes' => 0, 'failures' => 0, 'failure_log_name' => $failureLogName, 'success_log_name' => $successLogName];
         $validData = [];
         while (($data = fgetcsv($handle, 8096, ",")) !== FALSE) {
             $lethStart = Address::factory(trim($data[1]));
             $lethEnd = Address::factory(trim($data[2]));
             $foundAFit = false;
             foreach ($this->subnets as $subnet) {
                 try {
                     if ($subnet['object']->encloses_address($lethStart) && $subnet['object']->encloses_address($lethEnd)) {
                         array_push($data, $subnet['id']);
                         array_push($data, $subnet['supernet_id']);
                         array_push($validData, $data);
                         $foundAFit = true;
                         break;
                     }
                 } catch (Exception $e) {
                     //
                 }
             }
             if ($foundAFit === false) {
                 array_push($data, "IP pool did not fit into any defined subnets.");
                 fputcsv($failureLog, $data);
                 $returnData['failures'] += 1;
             }
         }
         $requests = function () use($validData) {
             foreach ($validData as $validDatum) {
                 $supernetID = array_pop($validDatum);
                 $subnetID = array_pop($validDatum);
                 (yield new Request("POST", $this->uri . "/api/v1/network/ipam/supernets/{$supernetID}/subnets/{$subnetID}/ip_pools", ['Content-Type' => 'application/json; charset=UTF8', 'timeout' => 30, 'Authorization' => 'Basic ' . base64_encode($this->username . ':' . $this->password)], json_encode($this->buildPayload($validDatum))));
             }
         };
         $pool = new Pool($this->client, $requests(), ['concurrency' => 10, 'fulfilled' => function ($response, $index) use(&$returnData, $successLog, $failureLog, $validData) {
             $statusCode = $response->getStatusCode();
             if ($statusCode > 201) {
                 $body = json_decode($response->getBody()->getContents());
                 $line = $validData[$index];
                 array_push($line, $body);
                 fputcsv($failureLog, $line);
                 $returnData['failures'] += 1;
             } else {
                 $returnData['successes'] += 1;
                 fwrite($successLog, "Import succeeded for account ID {$validData[$index][0]}" . "\n");
             }
         }, 'rejected' => function ($reason, $index) use(&$returnData, $failureLog, $validData) {
             $response = $reason->getResponse();
             $body = json_decode($response->getBody()->getContents());
             $returnMessage = implode(", ", (array) $body->error->message);
             $line = $validData[$index];
             array_push($line, $returnMessage);
             fputcsv($failureLog, $line);
             $returnData['failures'] += 1;
         }]);
         $promise = $pool->promise();
         $promise->wait();
     } else {
         throw new InvalidArgumentException("File could not be opened.");
     }
     fclose($failureLog);
     fclose($successLog);
     return $returnData;
 }