Example #1
0
 /**
  * Returns the object identified by $objectId
  *
  * @param string $objectId
  *
  * @return mixed value
  */
 public function load($objectId)
 {
     $success = null;
     $value = $this->adapter->getItem($objectId, $success);
     if ($success) {
         return $value;
     }
     return null;
 }
Example #2
0
 /**
  * Get an item.
  *
  * @param string $cacheId
  * @param bool   $withVersion
  * @param bool   $success
  *
  * @return mixed Data on success, null on failure
  */
 public function getItem($cacheId, $withVersion = true, &$success = null)
 {
     if ($withVersion) {
         $cacheId .= '.' . $this->getVersion();
     }
     if (!$this->cache->hasItem($cacheId)) {
         $success = false;
         return null;
     }
     $success = null;
     $data = $this->cache->getItem($cacheId, $success);
     if (!$success) {
         $success = false;
         return null;
     }
     if (!isset($data['content'])) {
         $success = false;
         return null;
     }
     $success = true;
     return unserialize($data['content']);
 }
Example #3
0
 /**
  * Tests to see if:
  *     - see if this is a debug request with appropriately formed pid, else
  *     - see if the cookie for the per user test has been set so we can record the results and add to the session
  *     - see if a session has already been opened for the request browser, if so send the info back, else
  *     - see if the cookie for the full test has been set so we can build the profile, if so build the profile &
  *     send the info back, else
  *     - see if this browser reports being a spider, doesn't support JS or doesn't support cookies
  *     - see if detector can find an already created profile for the browser, if so send the info back, else
  *     - start the process for building a profile for this unknown browser
  *
  * @param string|array|\Wurfl\Request\GenericRequest $request
  *
  * @return null|\stdClass an object that contains all the properties for this particular user agent
  */
 public function build($request = null)
 {
     $request = $this->initRequest($request);
     $sessionID = md5($request->getBrowserUserAgent() . '-session-' . $this->coreVersion . '-' . $this->extendedVersion);
     $pid = isset($_REQUEST['pid']) && preg_match('/[a-z0-9]{32}/', $_REQUEST['pid']) ? $_REQUEST['pid'] : false;
     // offer the ability to review profiles saved in the system
     if ($pid) {
         $this->foundIn = 'archive';
         // decode the core data
         $info = json_decode(@file_get_contents(__DIR__ . '/' . $this->uaDir . 'ua.' . $pid . '.json'));
         if ($info instanceof \stdClass) {
             // some general properties
             $info->nojs = false;
             $info->nocookies = false;
             // put the merged JSON info into session
             if (isset($_SESSION)) {
                 $_SESSION[$sessionID] = $info;
             }
             // return to the script
             //  return $info;
         }
     }
     $cacheId = hash('sha512', $request->getDeviceUserAgent() . '||||' . $request->getBrowserUserAgent());
     $result = null;
     $success = false;
     $info = $this->cache->getItem($cacheId, $success);
     // populate some variables specific to build()
     $uaHash = md5($request->getBrowserUserAgent());
     $uaFile = __DIR__ . '/' . $this->uaDir . $this->uaDir() . 'ua.' . $uaHash . '.json';
     if ($success && $info instanceof \stdClass) {
         $this->foundIn = 'cache';
         $this->save($request, $info, $uaFile, $cacheId);
         // send the data back to the script to be used
         //return $info;
     }
     session_id($sessionID);
     if (@session_start() && isset($_SESSION) && isset($_SESSION[$sessionID])) {
         $this->foundIn = 'session';
         $info = $_SESSION[$sessionID];
         $this->save($request, $info, $uaFile, $cacheId);
         // send the data back to the script to be used
         //return $info;
     }
     $cookieID = $this->getCookieId($request);
     $modernizrData = Modernizr::getData($cookieID);
     if ($this->checkSpider($request) || isset($_REQUEST['nojs']) && $_REQUEST['nojs'] === 'true' || isset($_REQUEST['nocookies']) && $_REQUEST['nocookies'] === 'true') {
         $this->foundIn = 'nojs';
         // use ua-parser to set-up the basic properties for this UA, populate other core properties
         // include the basic properties of the UA
         $info = $this->createUAProperties($request);
         $info->uaHash = $uaHash;
         $info->coreVersion = $this->coreVersion;
         if (null !== $modernizrData) {
             foreach ($modernizrData as $property => $value) {
                 $info->{$property} = $value;
             }
         }
         // some general properties
         $info->nojs = false;
         $info->nocookies = false;
         // add an attribute to the object in case no js or no cookies was sent
         if (isset($_REQUEST['nojs']) && $_REQUEST['nojs'] == 'true') {
             $info->nojs = true;
         } else {
             if (isset($_REQUEST['nocookies']) && $_REQUEST['nocookies'] == 'true') {
                 $info->nocookies = true;
             }
         }
         // try setting the session unless cookies are actively not supported
         if (!(isset($_REQUEST['nocookies']) && $_REQUEST['nocookies'] == 'true') && isset($_SESSION)) {
             $_SESSION[$sessionID] = $info;
         }
         $this->save($request, $info, $uaFile, $cacheId);
         // return the collected data to the script for use in this go around
         return $info;
     }
     if (null !== $modernizrData) {
         // to be clear, this section means that a UA was unknown, was profiled with modernizr
         // & now we're saving that data to build a new profile
         $this->foundIn = 'cookie';
         // use ua-parser to set-up the basic properties for this UA, populate other core properties
         $info = $this->createUAProperties($request);
         $info->uaHash = $uaHash;
         $info->coreVersion = $this->coreVersion;
         foreach ($modernizrData as $property => $value) {
             $info->{$property} = $value;
         }
         // some general properties
         $info->nojs = false;
         $info->nocookies = false;
         // unset the cookie that held the vast amount of test data
         setcookie($cookieID, '');
         // add our collected data to the session for use in future requests, also add the per request data
         if (isset($_SESSION)) {
             $_SESSION[$sessionID] = $info;
         }
         $this->save($request, $info, $uaFile, $cacheId);
         // return the collected data to the script for use in this go around
         return $info;
     }
     return null;
 }