public function fetchRemoteSearch($text, $tag = false) { $endpoint_url = $this['url'] . "search_items"; if ($tag) { $endpoint_url .= "?tag=" . urlencode(studip_utf8encode($text)); } else { $endpoint_url .= "?text=" . urlencode(studip_utf8encode($text)); } $output = @file_get_contents($endpoint_url); if ($output) { $output = studip_utf8decode(json_decode($output, true)); foreach ((array) $output['results'] as $material_data) { $host = LernmarktplatzHost::findOneBySQL("public_key = ?", array($material_data['host']['public_key'])); if (!$host) { $host = new LernmarktplatzHost(); $host['url'] = $material_data['host']['url']; $host->fetchPublicKey(); $host->store(); } if (!$host->isMe()) { //set user: $user = LernmarktplatzUser::findOneBySQL("foreign_user_id", array($material_data['user']['user_id'], $host->getId())); if (!$user) { $user = new LernmarktplatzUser(); $user['foreign_user_id'] = $material_data['user']['user_id']; $user['host_id'] = $host->getId(); } $user['name'] = $material_data['user']['name']; $user['avatar'] = $material_data['user']['avatar'] ?: null; $user->store(); //set material: $material_data['data']['foreign_material_id'] = $material_data['data']['id']; $material = LernmarktplatzMaterial::findOneBySQL("foreign_material_id = ? AND host_id = ?", array($material_data['data']['foreign_material_id'], $host->getId())); if (!$material) { $material = new LernmarktplatzMaterial(); } unset($material_data['data']['id']); $material->setData($material_data['data']); $material['host_id'] = $host->getId(); $material['user_id'] = $user->getId(); $material->store(); //set topics: $material->setTopics($material_data['topics']); } } } }
/** * Update data of an item via POST-request. */ public function push_data_action() { if (Request::isPost()) { $public_key_hash = $_SERVER['HTTP_' . str_replace("-", "_", strtoupper($GLOBALS['LERNMARKTPLATZ_HEADER_PUBLIC_KEY_HASH']))]; $signature = base64_decode($_SERVER['HTTP_' . str_replace("-", "_", strtoupper($GLOBALS['LERNMARKTPLATZ_HEADER_SIGNATURE']))]); $host = LernmarktplatzHost::findOneBySQL("MD5(public_key) = ?", array($public_key_hash)); if ($host && !$host->isMe()) { $body = file_get_contents('php://input'); if ($host->verifySignature($body, $signature)) { $data = studip_utf8decode(json_decode($body, true)); $material = LernmarktplatzMaterial::findOneBySQL("host_id = ? AND foreign_material_id = ?", array($host->getId(), $data['data']['foreign_material_id'])); if (!$material) { $material = new LernmarktplatzMaterial(); } if ($data['delete_material']) { $material->delete(); echo "deleted "; } else { $material->setData($data['data']); $material['host_id'] = $host->getId(); //update user $user = LernmarktplatzUser::findOneBySQL("host_id = ? AND foreign_user_id = ?", array($host->getId(), $data['user']['user_id'])); if (!$user) { $user = new LernmarktplatzUser(); $user['host_id'] = $host->getId(); $user['foreign_user_id'] = $data['user']['user_id']; } $user['name'] = $data['user']['name']; $user['avatar'] = $data['user']['avatar']; $user['description'] = $data['user']['description'] ?: null; $user->store(); $material['user_id'] = $user->getId(); $material->store(); $material->setTopics($data['topics']); echo "stored "; } } else { throw new Exception("Wrong signature, sorry."); } } $this->render_text(""); } else { throw new Exception("USE POST TO PUSH."); } }