示例#1
0
function url_get_contents($Url)
{
    if (!function_exists('curl_init')) {
        die('CURL is not installed!');
    }
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $Url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2);
    curl_setopt($ch, CURLOPT_TIMEOUT, 2);
    //timeout in seconds
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    // curl_setopt($ch, CURLOPT_SSLVERSION, 3);
    $output = curl_exec($ch);
    if ($output === false) {
        $err = curl_error($ch);
        DiscoUtils::error('Error downloading from [' . $Url . '] ' . $err);
    }
    curl_close($ch);
    return $output;
}
示例#2
0
 function update()
 {
     $items = array();
     $cachefile = '/tmp/feide-services.json';
     if (file_exists($cachefile)) {
         $services = json_decode(file_get_contents($cachefile), true);
     } else {
         $url = 'https://api.feide.no/spinfo/0/all';
         $data = file_get_contents($url);
         file_put_contents($cachefile, $data);
         $services = json_decode($data, true);
     }
     /**
      * Load special file based upon Kind ID to entityID from kundeportalen / KIND.
      */
     global $BASE;
     $mapFile = dirname($BASE) . '/etc/entityIDmapping.json';
     $entityIDmapping = json_decode(file_get_contents($mapFile), true);
     /**
      * Load metadata...
      */
     $metadataFile = dirname($BASE) . '/var/dynamic/metadata-prod/saml20-sp-remote.php';
     require_once $metadataFile;
     $metadataId2entityID = array();
     foreach ($metadata as $entityID => $meta) {
         if (isset($meta['feide:kindID'])) {
             if (!isset($metadataId2entityID[$meta['feide:kindID']])) {
                 $metadataId2entityID[$meta['feide:kindID']] = array();
             }
             $metadataId2entityID[$meta['feide:kindID']][] = $entityID;
             // echo "entity id " . $meta['feide:kindID'] . '    ' . $entityID . "\n";
         }
     }
     // print_r($metadataId2entityID);
     // exit;
     $logocache = new LogoCache('feide');
     $items = array();
     foreach ($services as $k => $d) {
         // If we found a mapping from Kind ID to entities based upon metadat, use that
         if (isset($metadataId2entityID[$d['id']])) {
             $d['entityIDs'] = $metadataId2entityID[$d['id']];
             // If not, then use the mapping found in the external file.
         } else {
             $entityIDs = $this->getEntityIDs($d['id'], $entityIDmapping);
             if (!empty($entityIDs)) {
                 $d['entityIDs'] = $entityIDs;
             } else {
                 continue;
             }
             foreach ($entityIDs as $entityID) {
                 if (isset($metadata[$entityID]) && isset($metadata[$entityID]['logo'])) {
                     // feide/splogos/eu-supply_logo.jpg
                     if (preg_match('|feide/splogos/(.*)|', $metadata[$entityID]['logo'], $matches)) {
                         $d['logo'] = $matches[1];
                     }
                 }
             }
         }
         // $entityIDhash = sha1(join(',', $entityIDs));
         if (isset($d['logo'])) {
             // echo " " . $d['id'] . " logo => " . $d['logo'] . "\n";
             $src = dirname($BASE) . '/var/dynamic/splogos/' . $d['logo'];
             if (!file_exists($src)) {
                 DiscoUtils::error('Could not find local logo file ' . $src);
             } else {
                 $id = 'feide:sp:' . $d['id'];
                 $meta = array('type' => 'feide-sp');
                 if (count($entityIDs) === 1) {
                     $meta['entityID'] = $entityIDs[0];
                 }
                 // $id, $src, $meta, $localFile = false)
                 $ok = $logocache->getLogoURL($id, $src, $meta, true);
                 // $ok = $logocache->getLogo($this->entityId, $this->feed, $cl);
                 if (!empty($ok)) {
                     $d['icon'] = $ok;
                 }
             }
         } else {
             DiscoUtils::debug('No logo available for ' . $d['id']);
             // echo " " . $d['id'] . " ----- ----- ----- \n";
         }
         // echo "Dump"; print_r($d);
         // if ($i++ > 3) exit;
         // print_r($services[$k]);
         $item = new FeideService($d);
         $items[] = $item;
     }
     function arrSum($a, $b)
     {
         $res = array();
         foreach ($a as $k => $v) {
             if (isset($b[$k]) && isset($a[$k])) {
                 $res[] = $a[$k] + $b[$k];
             } else {
                 if (isset($a[$k])) {
                     $res[] = $a[$k];
                 } else {
                     $res[] = $b[$k];
                 }
             }
         }
         return $res;
     }
     // exit;
     $stats = $this->getStatisticsTotal();
     $statsfull = $this->getStatsAllWeeks(array_keys($stats));
     // print_r($statsfull); exit;
     foreach ($items as $item) {
         $entityIDs = $item->get('entityIDs');
         $total = 0;
         foreach ($entityIDs as $entityID) {
             if (isset($stats[$entityID])) {
                 $total += $stats[$entityID];
             }
         }
         $item->set('statistics', $total);
         $fullStat = array();
         foreach ($entityIDs as $entityID) {
             if (isset($statsfull[$entityID])) {
                 $fullStat = arrSum($statsfull[$entityID], $total);
                 // $total += $stats[$entityID];
             }
         }
         $item->set('statistics-full', $fullStat);
         // $item->save();
         // print_r($item); exit;
     }
     // exit;
     foreach ($items as $item) {
         $item->save();
         // print_r($item); exit;
     }
 }
示例#3
0
 /**
  * Process metadata, and set a country tag to indicate which country this entry comes from.
  * @param  [type] $data [description]
  * @param  [type] $m    [description]
  * @return [type]       [description]
  */
 protected function getCountry(&$data, $m)
 {
     $countrytags = array('croatia' => 'HR', 'czech' => 'CZ', 'denmark' => 'DK', 'finland' => 'FI', 'france' => 'FR', 'germany' => 'DE', 'greece' => 'GR', 'ireland' => 'IE', 'italy' => 'IT', 'luxembourg' => 'LU', 'hungary' => 'HU', 'netherlands' => 'NL', 'norway' => 'NO', 'portugal' => 'PT', 'poland' => 'PL', 'slovenia' => 'SI', 'spain' => 'ES', 'sweden' => 'SE', 'switzerland' => 'CH', 'turkey' => 'TR', 'us' => 'US', 'uk' => 'GB', 'japan' => 'JP');
     if (!empty($m['tags'])) {
         foreach ($m['tags'] as $tag) {
             if (array_key_exists($tag, $countrytags)) {
                 $data['country'] = $countrytags[$tag];
                 DiscoUtils::debug('Setting country from a metadata tag ' . $data['country']);
                 return;
             }
         }
     }
     $c = $this->geoservice->countryFromURL($m['entityid']);
     if (!empty($c)) {
         $data['country'] = $c;
         DiscoUtils::debug('Setting country from entityid ' . $data['country']);
         return;
     }
     // TODO default country
     if (!empty($this->feedconfig['country'])) {
         $data['country'] = $this->feedconfig['country'];
         DiscoUtils::debug('Setting country from default value ' . $data['country']);
         return;
     }
     if (!empty($m['SingleSignOnService'])) {
         $m['metadata-set'] = 'saml20-idp-remote';
         $endpoint = DiscoUtils::getDefaultEndpoint($m['SingleSignOnService']);
         DiscoUtils::debug('Setting country from SingleSignOnService endpoint ' . $endpoint['Location']);
         $c = $this->geoservice->countryFromURL($endpoint['Location']);
         if (!empty($c)) {
             $data['country'] = $c;
             DiscoUtils::debug('Setting country from sso endpoint value ' . $data['country']);
             // return;
         }
         try {
             $host = parse_url($endpoint['Location'], PHP_URL_HOST);
             $ip = gethostbyname($host);
             DiscoUtils::debug('Setting country from sso endpoint IP address ' . $ip);
             $c = $this->geoservice->countryFromIP($ip);
             if (!empty($c)) {
                 $data['country'] = $c;
                 DiscoUtils::debug('Setting country from sso endpoint value ' . $data['country']);
                 return;
             }
             // DiscoUtils::debug('Setting country from sso endpoint IP address ' . $c);
             // $capi = new sspmod_discojuice_Country($ip);
             // $region = $capi->getRegion();
             // if (preg_match('|^([A-Z][A-Z])/|', $region, $match)) {
             // 	$data['country'] = $match[1];
             // }
         } catch (Exception $e) {
         }
     }
     DiscoUtils::error('Not able to set country for this item');
     return null;
 }