/** * @param $observer Class Class with method notify($event, $type, $ids, $extraData) * @param $types [array] Types to receive notications for * @param $label [string] Name of the observer */ public static function registerObserver($observer, $types = [], $name = '') { if (is_scalar($types)) { $types = array($types); } foreach ($types as $type) { if (!in_array($type, self::$types)) { throw new Exception("Invalid type '{$type}'"); } } if (empty($name) || isset(self::$observers[$name])) { $len = 2; $tries = 10; do { // Increase the hash length if we can't find a unique key if (!$tries) { $len++; $tries = 5; } $key = strlen($name) ? "{$name}-" : ''; $key .= Zotero_Utilities::randomString($len, 'mixed'); $tries--; } while (isset(self::$observers[$key])); } else { $key = $name; } Z_Core::debug('Registering observer for ' . ($types ? '[' . implode(",", $types) . ']' : 'all types') . ' in notifier ' . $key . "'", 4); self::$observers[$key] = array("observer" => $observer, "types" => $types); return $key; }
public static function generate() { $tries = 5; while ($tries > 0) { $str = Zotero_Utilities::randomString(24, 'mixed'); $sql = "SELECT COUNT(*) FROM `keys` WHERE `key`=?"; if (Zotero_DB::valueQuery($sql, $str)) { $tries--; continue; } return $str; } throw new Exception("Unique key could not be generated"); }
public static function getKey() { return Zotero_Utilities::randomString(8, 'key', true); }
public function testFullTextNoAccess() { API::groupClear(self::$config['ownedPrivateGroupID2']); // Add item to group as user 2 $user2SessionID = Sync::login(['username' => self::$config['username2'], 'password' => self::$config['password2']]); $xml = Sync::updated($user2SessionID); $updateKey = (string) $xml['updateKey']; $key = Zotero_Utilities::randomString(8, 'key', true); $dateAdded = date('Y-m-d H:i:s', time() - 1); $dateModified = date('Y-m-d H:i:s'); $xmlstr = '<data version="9">' . '<items>' . '<item libraryID="' . self::$config['ownedPrivateGroupLibraryID2'] . '" ' . 'itemType="attachment" ' . 'dateAdded="' . $dateAdded . '" ' . 'dateModified="' . $dateModified . '" ' . 'key="' . $key . '"/>' . '</items>' . '</data>'; $response = Sync::upload($user2SessionID, $updateKey, $xmlstr); Sync::waitForUpload($user2SessionID, $response, $this); // Make sure item exists $xml = Sync::updated($user2SessionID, 1); $this->assertEquals(1, $xml->updated[0]->items->count()); $this->assertEquals(1, $xml->updated[0]->items[0]->item->count()); // Try to add full-text content as user 1 $xml = Sync::updated(self::$sessionID); $updateKey = (string) $xml['updateKey']; $content = "This is some full-text content."; $totalChars = 2500; $xmlstr = '<data version="9">' . '<fulltexts>' . '<fulltext libraryID="' . self::$config['ownedPrivateGroupLibraryID2'] . '" ' . 'key="' . $key . '" ' . 'indexedChars="' . strlen($content) . '" ' . 'totalChars="' . $totalChars . '" ' . 'indexedPages="0" ' . 'totalPages="0">' . htmlspecialchars($content) . '</fulltext>' . '</fulltexts>' . '</data>'; $response = Sync::upload(self::$sessionID, $updateKey, $xmlstr); Sync::waitForUpload(self::$sessionID, $response, $this); // Retrieve it as user 2 $xml = Sync::updated($user2SessionID, 1, false, false, ["ft" => 1]); $this->assertEquals(0, $xml->updated[0]->fulltexts->count()); API::groupClear(self::$config['ownedPrivateGroupID2']); }
public static function createSearch($sessionID, $libraryID, $name, $conditions, $context) { if ($conditions == 'default') { $conditions = array(array('condition' => 'title', 'operator' => 'contains', 'value' => 'test')); } $xml = Sync::updated($sessionID); $updateKey = (string) $xml['updateKey']; $key = Zotero_Utilities::randomString(8, 'key', true); $dateAdded = date('Y-m-d H:i:s', time() - 1); $dateModified = date('Y-m-d H:i:s', time()); $xmlstr = '<data version="9">' . '<searches>' . '<search libraryID="' . $libraryID . '" ' . 'name="' . $name . '" ' . 'dateAdded="' . $dateAdded . '" ' . 'dateModified="' . $dateModified . '" ' . 'key="' . $key . '">'; $i = 1; foreach ($conditions as $condition) { $xmlstr .= '<condition id="' . $i . '" ' . 'condition="' . $condition['condition'] . '" ' . 'operator="' . $condition['operator'] . '" ' . 'value="' . $condition['value'] . '"/>'; $i++; } $xmlstr .= '</search>' . '</searches>' . '</data>'; $response = Sync::upload($sessionID, $updateKey, $xmlstr); Sync::waitForUpload($sessionID, $response, $context); return $key; }