public function processRequest()
 {
     $request = $this->getRequest();
     $user = $request->getUser();
     $cache = new PhabricatorKeyValueDatabaseCache();
     $cache = new PhutilKeyValueCacheProfiler($cache);
     $cache->setProfiler(PhutilServiceProfiler::getInstance());
     $result = $cache->getKey('darkconsole:' . $this->key);
     if (!$result) {
         return new Aphront400Response();
     }
     try {
         $result = phutil_json_decode($result);
     } catch (PhutilJSONParserException $ex) {
         return new Aphront400Response();
     }
     if ($result['vers'] != DarkConsoleCore::STORAGE_VERSION) {
         return new Aphront400Response();
     }
     if ($result['user'] != $user->getPHID()) {
         return new Aphront400Response();
     }
     $output = array();
     $output['tabs'] = $result['tabs'];
     $output['panel'] = array();
     foreach ($result['data'] as $class => $data) {
         try {
             $obj = newv($class, array());
             $obj->setData($data);
             $obj->setRequest($request);
             $panel = $obj->renderPanel();
             // Because cookie names can now be prefixed, wipe out any cookie value
             // with the session cookie name anywhere in its name.
             $pattern = '(' . preg_quote(PhabricatorCookies::COOKIE_SESSION) . ')';
             foreach ($_COOKIE as $cookie_name => $cookie_value) {
                 if (preg_match($pattern, $cookie_name)) {
                     $panel = PhutilSafeHTML::applyFunction('str_replace', $cookie_value, '(session-key)', $panel);
                 }
             }
             $output['panel'][$class] = $panel;
         } catch (Exception $ex) {
             $output['panel'][$class] = 'error';
         }
     }
     return id(new AphrontAjaxResponse())->setContent($output);
 }
Пример #2
0
 public function getKey(AphrontRequest $request)
 {
     $plugins = $this->getPlugins();
     foreach ($plugins as $plugin) {
         $plugin->setRequest($request);
         $plugin->willShutdown();
     }
     foreach ($plugins as $plugin) {
         $plugin->didShutdown();
     }
     foreach ($plugins as $plugin) {
         $plugin->setData($plugin->generateData());
     }
     $plugins = msort($plugins, 'getOrderKey');
     $key = Filesystem::readRandomCharacters(24);
     $tabs = array();
     $data = array();
     foreach ($plugins as $plugin) {
         $class = get_class($plugin);
         $tabs[] = array('class' => $class, 'name' => $plugin->getName(), 'color' => $plugin->getColor());
         $data[$class] = $this->sanitizeForJSON($plugin->getData());
     }
     $storage = array('vers' => self::STORAGE_VERSION, 'tabs' => $tabs, 'data' => $data, 'user' => $request->getUser() ? $request->getUser()->getPHID() : null);
     $cache = new PhabricatorKeyValueDatabaseCache();
     $cache = new PhutilKeyValueCacheProfiler($cache);
     $cache->setProfiler(PhutilServiceProfiler::getInstance());
     // This encoding may fail if there are, e.g., database queries which
     // include binary data. It would be a little cleaner to try to strip these,
     // but just do something non-broken here if we end up with unrepresentable
     // data.
     $json = @json_encode($storage);
     if (!$json) {
         $json = '{}';
     }
     $cache->setKeys(array('darkconsole:' . $key => $json), $ttl = 60 * 60 * 6);
     return $key;
 }
Пример #3
0
 private static function addProfilerToCaches(array $caches)
 {
     foreach ($caches as $key => $cache) {
         $pcache = new PhutilKeyValueCacheProfiler($cache);
         $pcache->setProfiler(PhutilServiceProfiler::getInstance());
         $caches[$key] = $pcache;
     }
     return $caches;
 }
Пример #4
0
 private function loadContent(array $pastes)
 {
     $cache = new PhabricatorKeyValueDatabaseCache();
     $cache = new PhutilKeyValueCacheProfiler($cache);
     $cache->setProfiler(PhutilServiceProfiler::getInstance());
     $keys = array();
     foreach ($pastes as $paste) {
         $keys[] = $this->getContentCacheKey($paste);
     }
     $caches = $cache->getKeys($keys);
     $results = array();
     $need_raw = array();
     foreach ($pastes as $key => $paste) {
         $key = $this->getContentCacheKey($paste);
         if (isset($caches[$key])) {
             $paste->attachContent(phutil_safe_html($caches[$key]));
             $results[$paste->getID()] = $paste;
         } else {
             $need_raw[$key] = $paste;
         }
     }
     if (!$need_raw) {
         return $results;
     }
     $write_data = array();
     $need_raw = $this->loadRawContent($need_raw);
     foreach ($need_raw as $key => $paste) {
         $content = $this->buildContent($paste);
         $paste->attachContent($content);
         $write_data[$this->getContentCacheKey($paste)] = (string) $content;
         $results[$paste->getID()] = $paste;
     }
     $cache->setKeys($write_data);
     return $results;
 }
 private function loadContent(array $pastes)
 {
     $cache = new PhabricatorKeyValueDatabaseCache();
     $cache = new PhutilKeyValueCacheProfiler($cache);
     $cache->setProfiler(PhutilServiceProfiler::getInstance());
     $keys = array();
     foreach ($pastes as $paste) {
         $keys[] = $this->getContentCacheKey($paste);
     }
     $caches = $cache->getKeys($keys);
     $need_raw = array();
     $have_cache = array();
     foreach ($pastes as $paste) {
         $key = $this->getContentCacheKey($paste);
         if (isset($caches[$key])) {
             $paste->attachContent(phutil_safe_html($caches[$key]));
             $have_cache[$paste->getPHID()] = true;
         } else {
             $need_raw[$key] = $paste;
         }
     }
     if (!$need_raw) {
         return $pastes;
     }
     $write_data = array();
     $have_raw = $this->loadRawContent($need_raw);
     $have_raw = mpull($have_raw, null, 'getPHID');
     foreach ($pastes as $key => $paste) {
         $paste_phid = $paste->getPHID();
         if (isset($have_cache[$paste_phid])) {
             continue;
         }
         if (empty($have_raw[$paste_phid])) {
             unset($pastes[$key]);
             continue;
         }
         $content = $this->buildContent($paste);
         $paste->attachContent($content);
         $write_data[$this->getContentCacheKey($paste)] = (string) $content;
     }
     if ($write_data) {
         $cache->setKeys($write_data);
     }
     return $pastes;
 }
Пример #6
0
 private function loadSnippets(array $pastes)
 {
     $cache = new PhabricatorKeyValueDatabaseCache();
     $cache = new PhutilKeyValueCacheProfiler($cache);
     $cache->setProfiler(PhutilServiceProfiler::getInstance());
     $keys = array();
     foreach ($pastes as $paste) {
         $keys[] = $this->getSnippetCacheKey($paste);
     }
     $caches = $cache->getKeys($keys);
     $need_raw = array();
     $have_cache = array();
     foreach ($pastes as $paste) {
         $key = $this->getSnippetCacheKey($paste);
         if (isset($caches[$key])) {
             $snippet_data = phutil_json_decode($caches[$key], true);
             $snippet = new PhabricatorPasteSnippet(phutil_safe_html($snippet_data['content']), $snippet_data['type']);
             $paste->attachSnippet($snippet);
             $have_cache[$paste->getPHID()] = true;
         } else {
             $need_raw[$key] = $paste;
         }
     }
     if (!$need_raw) {
         return $pastes;
     }
     $write_data = array();
     $have_raw = $this->loadRawContent($need_raw);
     $have_raw = mpull($have_raw, null, 'getPHID');
     foreach ($pastes as $key => $paste) {
         $paste_phid = $paste->getPHID();
         if (isset($have_cache[$paste_phid])) {
             continue;
         }
         if (empty($have_raw[$paste_phid])) {
             unset($pastes[$key]);
             continue;
         }
         $snippet = $this->buildSnippet($paste);
         $paste->attachSnippet($snippet);
         $snippet_data = array('content' => (string) $snippet->getContent(), 'type' => (string) $snippet->getType());
         $write_data[$this->getSnippetCacheKey($paste)] = phutil_json_encode($snippet_data);
     }
     if ($write_data) {
         $cache->setKeys($write_data);
     }
     return $pastes;
 }
 public static function loadMaps(array $maps)
 {
     $keys = array();
     foreach ($maps as $map) {
         list($u, $v) = $map;
         $keys[self::getCacheKey($u, $v)] = $map;
     }
     $cache = new PhabricatorKeyValueDatabaseCache();
     $cache = new PhutilKeyValueCacheProfiler($cache);
     $cache->setProfiler(PhutilServiceProfiler::getInstance());
     $results = array();
     if ($keys) {
         $caches = $cache->getKeys(array_keys($keys));
         foreach ($caches as $key => $value) {
             list($u, $v) = $keys[$key];
             try {
                 $results[$u][$v] = self::newFromMap(phutil_json_decode($value));
             } catch (Exception $ex) {
                 // Ignore, rebuild below.
             }
             unset($keys[$key]);
         }
     }
     if ($keys) {
         $built = self::buildMaps($maps);
         $write = array();
         foreach ($built as $u => $list) {
             foreach ($list as $v => $map) {
                 $write[self::getCacheKey($u, $v)] = json_encode($map->getMap());
                 $results[$u][$v] = $map;
             }
         }
         $cache->setKeys($write);
     }
     return $results;
 }