コード例 #1
0
ファイル: Geolocation.php プロジェクト: menatwork/geolocation
 /**
  * Geolocation core functions
  * 
  * @global Object $objPage 
  */
 protected function checkGeolocation()
 {
     global $objPage;
     $arrMethods = array();
     // Check options for this page
     if ($objPage->geo_single_page == true) {
         $arrMethods = deserialize($objPage->geo_single_choose);
     } else {
         if ($objPage->geo_child_page == true) {
             $arrMethods = deserialize($objPage->geo_child_choose);
         } else {
             $intID = $objPage->pid;
             while ($intID != 0) {
                 $arrResult = $this->Database->prepare("SELECT * FROM tl_page WHERE id =?")->execute($intID);
                 if ($arrResult->numRows == 0) {
                     break;
                 }
                 $intID = $arrResult->pid;
                 if ($arrResult->geo_child_choose == true) {
                     $arrMethods = deserialize($arrResult->geo_child_choose);
                     break;
                 }
             }
         }
     }
     // Get current running method as string
     $stringCurrentRunning = "";
     switch ($this->objUserGeolocation->getTrackRunning()) {
         case GeolocationContainer::LOCATION_W3C:
             $stringCurrentRunning = "w3c";
             break;
         case GeolocationContainer::LOCATION_IP:
             $stringCurrentRunning = "ip";
             break;
         case GeolocationContainer::LOCATION_FALLBACK:
             $stringCurrentRunning = "fallback";
             break;
         case GeolocationContainer::LOCATION_NONE:
         default:
             $stringCurrentRunning = "none";
     }
     // Rest current running loacatin operation, if not in current methods
     if (!in_array($stringCurrentRunning, $arrMethods)) {
         $this->objUserGeolocation->setTrackRunning(GeolocationContainer::LOCATION_NONE);
         $this->objUserGeolocation->setFailed(false);
         $this->objUserGeolocation->setError("");
         $this->objUserGeolocation->setErrorID("");
     }
     // Check if optios were found
     if (count($arrMethods) != 0) {
         // Check each option
         foreach ($arrMethods as $value) {
             // Flag for skipping this foreach
             $booBreakOutter = false;
             // Some informations
             $intCurrenRunning = $this->objUserGeolocation->getTrackRunning();
             $intMethodsStillLeft = count($arrMethods) - $this->objUserGeolocation->countTrackFinished();
             switch ($value) {
                 case "w3c":
                     $booNoneRunning = $intCurrenRunning == GeolocationContainer::LOCATION_NONE;
                     $booAllreadyRun = $this->objUserGeolocation->isTrackFinished(GeolocationContainer::LOCATION_W3C);
                     // Check if no other method is running and if this is the first time
                     if ($booNoneRunning == true && $booAllreadyRun == false) {
                         // Include js and hock fpr w3c
                         $GLOBALS['TL_JAVASCRIPT']['geoCore'] = "system/modules/geolocation/html/js/geoCore.js";
                         $GLOBALS['TL_HOOKS']['parseFrontendTemplate'][] = array('Geolocation', 'insertJSVars');
                         // Set information
                         $this->objUserGeolocation->setTrackRunning(GeolocationContainer::LOCATION_W3C);
                         $this->objUserGeolocation->setFailed(false);
                         //Break foreach
                         $booBreakOutter = true;
                     } else {
                         if ($intCurrenRunning == GeolocationContainer::LOCATION_W3C && $this->objUserGeolocation->isFailed() == true) {
                             // Set information
                             $this->objUserGeolocation->addTrackFinished(GeolocationContainer::LOCATION_W3C);
                             $this->objUserGeolocation->setTrackRunning(GeolocationContainer::LOCATION_NONE);
                             // Set tracked true, if this the only method
                             if ($intMethodsStillLeft == 1) {
                                 $this->objUserGeolocation->setTracked(true);
                                 $this->objUserGeolocation->setFailed(true);
                             }
                         } else {
                             if ($intCurrenRunning == GeolocationContainer::LOCATION_W3C) {
                                 // Check if this is the only method
                                 if ($intMethodsStillLeft == 1) {
                                     $GLOBALS['TL_JAVASCRIPT']['geoCore'] = "system/modules/geolocation/html/js/geoCore.js";
                                     $GLOBALS['TL_HOOKS']['parseFrontendTemplate'][] = array('Geolocation', 'insertJSVars');
                                     $booBreakOutter = true;
                                 } else {
                                     // Skip W3C and use next
                                     $this->objUserGeolocation->addTrackFinished(GeolocationContainer::LOCATION_W3C);
                                     $this->objUserGeolocation->setTrackRunning(GeolocationContainer::LOCATION_NONE);
                                 }
                             }
                         }
                     }
                     break;
                 case "ip":
                     $booNoneRunning = $intCurrenRunning == GeolocationContainer::LOCATION_NONE;
                     $booAllreadyRun = $this->objUserGeolocation->isTrackFinished(GeolocationContainer::LOCATION_IP);
                     // Check if no other method is running and if this is the first time
                     if ($booNoneRunning == true && $booAllreadyRun == false) {
                         $this->objUserGeolocation->setFailed(false);
                         // Try to load the location from IP
                         $this->objUserGeolocation->setIP($_SERVER['REMOTE_ADDR']);
                         $objResult = $this->doIPLookUp($this->objUserGeolocation);
                         // Check if we got a result
                         if ($objResult == FALSE) {
                             $this->objUserGeolocation->addTrackFinished(GeolocationContainer::LOCATION_IP);
                             $this->objUserGeolocation->setTrackRunning(GeolocationContainer::LOCATION_NONE);
                             $this->objUserGeolocation->setError("No ip result.");
                             $this->objUserGeolocation->setErrorID(GeolocationContainer::ERROR_NO_IP_RESULT);
                             // Set information for geolocation
                             $this->objUserGeolocation->setIP(preg_replace("/\\.\\d?\\d?\\d?\$/", ".0", $this->objUserGeolocation->getIP()));
                             if ($intMethodsStillLeft == 1) {
                                 $this->objUserGeolocation->setTracked(true);
                                 $this->objUserGeolocation->setFailed(true);
                             }
                         } else {
                             // Got a result
                             $this->objUserGeolocation = $objResult;
                             $booBreakOutter = true;
                         }
                     }
                     break;
                 case "fallback":
                     $booNoneRunning = $intCurrenRunning == GeolocationContainer::LOCATION_NONE;
                     $booAllreadyRun = $this->objUserGeolocation->isTrackFinished(GeolocationContainer::LOCATION_FALLBACK);
                     // Check if a fallback is define
                     if ($booNoneRunning == true && $booAllreadyRun == false) {
                         if (strlen($GLOBALS['TL_CONFIG']['geo_countryFallback']) != 0) {
                             // Set information for geolocation
                             $this->objUserGeolocation->setCountry($this->getCountryByShortTag($GLOBALS['TL_CONFIG']['geo_countryFallback']));
                             $this->objUserGeolocation->setCountryShort($GLOBALS['TL_CONFIG']['geo_countryFallback']);
                             $this->objUserGeolocation->setTracked(true);
                             $this->objUserGeolocation->setTrackType(GeolocationContainer::LOCATION_FALLBACK);
                             $this->objUserGeolocation->addTrackFinished(GeolocationContainer::LOCATION_FALLBACK);
                             $this->objUserGeolocation->setFailed(false);
                             $this->objUserGeolocation->setError("");
                             $this->objUserGeolocation->setErrorID(GeolocationContainer::ERROR_NONE);
                         } else {
                             $this->objUserGeolocation->addTrackFinished(GeolocationContainer::LOCATION_FALLBACK);
                             $this->objUserGeolocation->setTrackRunning(GeolocationContainer::LOCATION_NONE);
                             $this->objUserGeolocation->setFailed(true);
                             $this->objUserGeolocation->setError("No fallback defined");
                             $this->objUserGeolocation->setErrorID(GeolocationContainer::ERROR_POSITION_UNAVAILABLE);
                             if ($intMethodsStillLeft == 1) {
                                 $this->objUserGeolocation->setTracked(true);
                                 $this->objUserGeolocation->setFailed(true);
                             }
                         }
                     }
                     break;
                 default:
                     break;
             }
             if ($booBreakOutter == true) {
                 break;
             }
         }
         // Save in Session and cookie
         $this->saveSession();
         $this->saveCookie();
     }
     $this->booRunFinished = true;
 }