SHINDIG-1056 by lipeng, BasicRemoteContentTest doesn't depend on static private key...
[shindig.git] / php / src / social / sample / DefaultInvalidateService.php
blob468219546210b454db1a9915d4be0175cfca57fa
1 <?php
2 /**
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
21 class DefaultInvalidateService implements InvalidateService {
23 /**
24 * @var Cache
26 private $invalidationEntry;
28 /**
29 * @var Cache
31 private $cache;
33 private static $marker = null;
35 /**
36 * @var Cache
38 private static $makerCache = null;
40 private static $TOKEN_PREFIX = 'INV_TOK_';
42 public function __construct(Cache $cache) {
43 $this->cache = $cache;
44 $this->invalidationEntry = Cache::createCache(Config::get('data_cache'), 'InvalidationEntry');
45 if (self::$makerCache == null) {
46 self::$makerCache = Cache::createCache(Config::get('data_cache'), 'MarkerCache');
47 $value = self::$makerCache->expiredGet('marker');
48 if ($value['found']) {
49 self::$marker = $value['data'];
50 } else {
51 self::$marker = 0;
52 self::$makerCache->set('marker', self::$marker);
56 /**
57 * Invalidate a set of cached resources that are part of the application specification itself.
58 * This includes gadget specs, manifests and message bundles
59 * @param uris of content to invalidate
60 * @param token identifying the calling application
62 function invalidateApplicationResources(Array $uris, SecurityToken $token) {
63 foreach($uris as $uri) {
64 $request = new RemoteContentRequest($uri);
65 $this->cache->invalidate($request->toHash());
69 /**
70 * Invalidate all cached resources where the specified user ids were used as either the
71 * owner or viewer id when a signed or OAuth request was made for the content by the application
72 * identified in the security token.
73 * @param opensocialIds Set of user ids to invalidate authenticated/signed content for
74 * @param token identifying the calling application
76 function invalidateUserResources(Array $opensocialIds, SecurityToken $token) {
77 foreach($opensocialIds as $opensocialId) {
78 ++self::$marker;
79 self::$makerCache->set('marker', self::$marker);
80 $this->invalidationEntry->set($this->getKey($opensocialId, $token), self::$marker);
84 /**
85 * Is the specified request still valid. If the request is signed or authenticated
86 * has its content been invalidated by a call to invalidateUserResource subsequent to the
87 * response being cached.
89 function isValid(RemoteContentRequest $request) {
90 if ($request->getAuthType() == RemoteContentRequest::$AUTH_NONE) {
91 return true;
93 return $request->getInvalidation() == $this->getInvalidationMark($request);
96 /**
97 * Mark the request prior to caching it so that subsequent calls to isValid can detect
98 * if it has been invalidated.
100 function markResponse(RemoteContentRequest $request) {
101 $mark = $this->getInvalidationMark($request);
102 if ($mark) {
103 $request->setInvalidation($mark);
108 * @return string
110 private function getKey($userId, SecurityToken $token) {
111 $pos = strrpos($userId, ':');
112 if ($pos !== false) {
113 $userId = substr($userId, $pos + 1);
116 if ($token->getAppId()) {
117 return DefaultInvalidateService::$TOKEN_PREFIX . $token->getAppId() . '_' . $userId;
121 private function getInvalidationMark(RemoteContentRequest $request) {
122 $token = $request->getToken();
123 if (!$token) {
124 return null;
126 $currentInvalidation = '';
127 if ($token->getOwnerId()) {
128 $ownerKey = $this->getKey($token->getOwnerId(), $token);
129 $cached = $this->invalidationEntry->expiredGet($ownerKey);
130 $ownerStamp = $cached['found'] ? $cached['data'] : false;
132 if ($token->getViewerId()) {
133 $viewerKey = $this->getKey($token->getViewerId(), $token);
134 $cached = $this->invalidationEntry->expiredGet($viewerKey);
135 $viewerStamp = $cached['found'] ? $cached['data'] : false;
137 if ($ownerStamp) {
138 $currentInvalidation = $currentInvalidation . 'o=' . $ownerStamp . ';';
140 if ($viewerStamp) {
141 $currentInvalidation = $currentInvalidation . 'v=' . $viewerStamp . ';';
143 return $currentInvalidation;