Esempio n. 1
0
 /** Save context & attributes to database */
 protected function save()
 {
     if (($id =& $this->id) !== null) {
         throw new SystemException('Cannot save existent context!');
     }
     $id = self::EMPTY_CONTEXT_ID;
     if ($attributes = $this->attributes) {
         // leave only one attribute in group
         static $groupedTypes;
         if (!$groupedTypes) {
             $groupedTypes = AttributeManager::getGroupedTypes();
             unset($groupedTypes[null]);
         }
         foreach ($groupedTypes as $types) {
             $intersection = array_intersect_key($types, $attributes);
             if (count($intersection) > 1) {
                 array_shift($intersection);
                 foreach ($intersection as $name => $type) {
                     unset($attributes[$name]);
                 }
             }
         }
         // save to database
         $snapshot = self::getSnapshot($attributes);
         $query = array('limit' => 1, 'select' => array('ID'), 'filter' => array('=SNAPSHOT' => $snapshot));
         if ($row = ContextTable::getList($query)->fetch()) {
             $id = (int) $row['ID'];
         } elseif (Option::get('conversion', 'CONTEXT_TABLE') != 'locked') {
             try {
                 $result = ContextTable::add(array('SNAPSHOT' => $snapshot));
                 if ($result->isSuccess()) {
                     $id = $result->getId();
                     foreach ($attributes as $name => $value) {
                         // TODO resetContext if not success and return null!!!
                         $result = ContextAttributeTable::add(array('CONTEXT_ID' => $id, 'NAME' => $name, 'VALUE' => (string) $value));
                     }
                 } else {
                     throw new DB\SqlQueryException();
                 }
             } catch (DB\SqlQueryException $e) {
                 if ($row = ContextTable::getList($query)->fetch()) {
                     $id = (int) $row['ID'];
                 }
             }
         }
     }
 }
Esempio n. 2
0
 /** Get day context singleton instance.
  * @return self
  */
 public static function getInstance()
 {
     if (!self::$instance) {
         $instance = new self();
         $varName = self::getVarName();
         $session =& $_SESSION[$varName];
         $expire = strtotime('today 23:59');
         if (!(is_array($session) && is_int($session['ID']) && $session['EXPIRE'] === $expire)) {
             $session = array('ID' => null, 'EXPIRE' => $expire, 'UNIQUE' => array());
             //global $APPLICATION; $cookie = $APPLICATION->get_cookie($varname);
             if ($cookie = $_COOKIE[$varName]) {
                 try {
                     $cookie = Json::decode($cookie);
                 } catch (ArgumentException $e) {
                 }
             }
             // check if cookie is valid
             if (is_array($cookie) && is_array($cookie['UNIQUE']) && $cookie['EXPIRE'] === $expire && ($id = $cookie['ID']) !== null && is_int($id) && ($id === self::EMPTY_CONTEXT_ID || Internals\ContextTable::getByPrimary($id)->fetch())) {
                 $session['ID'] = $id;
                 $session['UNIQUE'] = $cookie['UNIQUE'];
             }
         }
         $instance->id = $session['ID'];
         self::$session =& $session;
         self::$instance = $instance;
     }
     return self::$instance;
 }
Esempio n. 3
0
 /** Save context & attributes to database */
 protected function save()
 {
     if (($id =& $this->id) !== null) {
         throw new SystemException('Cannot save existent context!');
     }
     // save to database
     $id = self::EMPTY_CONTEXT_ID;
     if ($attributes = $this->attributes) {
         $snapshot = self::getSnapshot($attributes);
         $query = array('limit' => 1, 'select' => array('ID'), 'filter' => array('=SNAPSHOT' => $snapshot));
         if ($row = Internals\ContextTable::getList($query)->fetch()) {
             $id = (int) $row['ID'];
         } else {
             try {
                 $result = Internals\ContextTable::add(array('SNAPSHOT' => $snapshot));
                 if ($result->isSuccess()) {
                     $id = $result->getId();
                     foreach ($attributes as $attribute) {
                         // TODO resetContext if not success and return null!!!
                         $result = Internals\ContextAttributeTable::add(array('CONTEXT_ID' => $id, 'NAME' => $attribute['NAME'], 'VALUE' => $attribute['VALUE']));
                     }
                 } else {
                     throw new DB\SqlQueryException();
                 }
             } catch (DB\SqlQueryException $e) {
                 if ($row = Internals\ContextTable::getList($query)->fetch()) {
                     $id = (int) $row['ID'];
                 }
             }
         }
     }
 }
Esempio n. 4
0
    /** Get day context singleton instance.
     * @return self
     */
    public static function getInstance()
    {
        if (!self::$instance) {
            $instance = new self();
            $varName = self::getVarName();
            $session =& $_SESSION[$varName];
            $expire = strtotime('today 23:59');
            if (!(is_array($session) && is_int($session['ID']) && $session['EXPIRE'] === $expire)) {
                $session = array('ID' => null, 'EXPIRE' => $expire, 'UNIQUE' => array());
                //global $APPLICATION; $cookie = $APPLICATION->get_cookie($varname);
                if ($cookie = $_COOKIE[$varName]) {
                    try {
                        $cookie = Json::decode($cookie);
                    } catch (ArgumentException $e) {
                    }
                }
                // validate cookie
                if (is_array($cookie) && is_array($cookie['UNIQUE']) && $cookie['EXPIRE'] === $expire && ($id = $cookie['ID']) !== null && is_int($id) && ($id === self::EMPTY_CONTEXT_ID || Internals\ContextTable::getByPrimary($id)->fetch())) {
                    // 1. valid cookie
                    $session['ID'] = $id;
                    $session['UNIQUE'] = $cookie['UNIQUE'];
                } else {
                    // 2. invalid cookie
                    Asset::getInstance()->addString('
					<script type="text/javascript">
						BX.ajax.post(
							"/bitrix/tools/conversion/ajax_counter.php",
							{
								SITE_ID: BX.message("SITE_ID"),
								sessid: BX.message("bitrix_sessid"),
								HTTP_REFERER: "' . \CUtil::JSEscape($_SERVER['HTTP_REFERER']) . '"
							},
							function () {}
						);
					</script>
					');
                }
            }
            $instance->id = $session['ID'];
            self::$session =& $session;
            self::$instance = $instance;
        }
        return self::$instance;
    }