public function fetchData()
 {
     if ($this['host_id']) {
         $host = new LernmarktplatzHost($this['host_id']);
         if ($host) {
             $data = $host->fetchItemData($this['foreign_material_id']);
             if (!$data) {
                 return false;
             }
             if ($data['deleted']) {
                 return "deleted";
             }
             //user:
             $user = LernmarktplatzUser::findOneBySQL("foreign_user_id", array($data['user']['user_id'], $host->getId()));
             if (!$user) {
                 $user = new LernmarktplatzUser();
                 $user['foreign_user_id'] = $data['user']['user_id'];
                 $user['host_id'] = $host->getId();
             }
             $user['name'] = $data['user']['name'];
             $user['avatar'] = $data['user']['avatar'] ?: null;
             $user['description'] = $data['user']['description'] ?: null;
             $user->store();
             //material:
             $material_data = $data['data'];
             unset($material_data['material_id']);
             unset($material_data['user_id']);
             unset($material_data['mkdate']);
             $this->setData($material_data);
             $this->store();
             //topics:
             $this->setTopics($data['topics']);
             foreach ((array) $data['reviews'] as $review_data) {
                 $currenthost = LernmarktplatzHost::findOneByUrl(trim($review_data['host']['url']));
                 if (!$currenthost) {
                     $currenthost = new LernmarktplatzHost();
                     $currenthost['url'] = trim($review_data['host']['url']);
                     $currenthost['last_updated'] = time();
                     $currenthost->fetchPublicKey();
                     if ($currenthost['public_key']) {
                         $currenthost->store();
                     }
                 }
                 if ($currenthost && $currenthost['public_key'] && !$currenthost->isMe()) {
                     $review = LernmarktplatzReview::findOneBySQL("foreign_review_id = ? AND host_id = ?", array($review_data['foreign_review_id'], $currenthost->getId()));
                     if (!$review) {
                         $review = new LernmarktplatzReview();
                         $review['foreign_review_id'] = $review_data['foreign_review_id'];
                         $review['material_id'] = $this->getId();
                         $review['host_id'] = $currenthost->getId();
                     }
                     $review['review'] = $review_data['review'];
                     $review['rating'] = $review_data['rating'];
                     if ($review_data['chdate']) {
                         $review['chdate'] = $review_data['chdate'];
                     }
                     if ($review_data['mkdate']) {
                         $review['mkdate'] = $review_data['mkdate'];
                     }
                     $user = LernmarktplatzUser::findOneBySQL("foreign_user_id", array($review_data['user']['user_id'], $currenthost->getId()));
                     if (!$user) {
                         $user = new LernmarktplatzUser();
                         $user['foreign_user_id'] = $review_data['user']['user_id'];
                         $user['host_id'] = $currenthost->getId();
                     }
                     $user['name'] = $review_data['user']['name'];
                     $user['avatar'] = $review_data['user']['avatar'] ?: null;
                     $user['description'] = $review_data['user']['description'] ?: null;
                     $user->store();
                     $review['user_id'] = $user->getId();
                     $review->store();
                 }
             }
         }
     }
     return true;
 }
Ejemplo n.º 2
0
 protected function refreshHost($url)
 {
     $host_data = file_get_contents($url . "fetch_public_host_key");
     if ($host_data) {
         $host_data = studip_utf8decode(json_decode($host_data, true));
         if ($host_data) {
             $host = LernmarktplatzHost::findOneByUrl($url);
             if (!$host) {
                 $host = LernmarktplatzHost::findOneByPublic_key($host_data['public_key']);
             }
             if (!$host) {
                 $host = new LernmarktplatzHost();
             }
             $host['name'] = $host_data['name'];
             $host['url'] = Request::get("from");
             $host['public_key'] = $host_data['public_key'];
             $host['last_updated'] = time();
             if ($host->isNew()) {
                 $host['active'] = get_config("LERNMARKTPLATZ_ACTIVATE_NEW_HOSTS") ? 1 : 0;
             }
             $host->store();
         }
     }
 }
Ejemplo n.º 3
0
 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']);
             }
         }
     }
 }
Ejemplo n.º 4
0
 public function toggle_index_server_action()
 {
     if (Request::isPost()) {
         $host = new LernmarktplatzHost(Request::option("host_id"));
         if ($host->isMe()) {
             $host['index_server'] = Request::int("active", 0);
             $host->store();
             //distribute this info to adjacent server
             $data = array('data' => array('public_key' => $host['public_key'], 'url' => $host['url'], 'name' => $host['name'], 'index_server' => $host['index_server']));
             foreach (LernmarktplatzHost::findAll() as $remote) {
                 if (!$remote->isMe()) {
                     $remote->pushDataToEndpoint("update_server_info", $data);
                 }
             }
             /*$curl_multi_handle = curl_multi_init();
               $requests = array();
               foreach (LernmarktplatzHost::findAll() as $remote) {
                   if (!$remote->isMe()) {
                       $request = $remote->pushDataToEndpoint("update_server_info", $data, true);
                       curl_multi_add_handle($curl_multi_handle, $request);
                       $requests[] = $request;
                   }
               }
               $active = null;
               do {
                   $mrc = curl_multi_exec($curl_multi_handle, $active);
               } while ($mrc == CURLM_CALL_MULTI_PERFORM);
               while ($active && $mrc == CURLM_OK) {
                   if (curl_multi_select($curl_multi_handle) != -1) {
                       do {
                           $mrc = curl_multi_exec($curl_multi_handle, $active);
                       } while ($mrc == CURLM_CALL_MULTI_PERFORM);
                   }
               }
               foreach ($requests as $request) {
                   curl_multi_remove_handle($curl_multi_handle, $request);
               }
               curl_multi_close($curl_multi_handle);
               */
         } else {
             $host['allowed_as_index_server'] = Request::int("active", 0);
             $host->store();
         }
     }
     $this->render_text(Icon::create("checkbox-" . (Request::int("active") ? "" : "un") . "checked"));
 }