Example #1
0
 /**
  * 按id获取专辑 
  * 
  * @param int $id, 专辑id 
  */
 public function album($id)
 {
     $url = $this->server . 'album/' . $id . '/?id=' . $id . '&csrf_token=';
     $http = new \Net_Http_Client();
     // Set referer header
     $http->setHeader('Referer', 'http://music.163.com');
     $body = $http->get($url)->getBody();
     $res = json_decode($body, true);
     if (!isset($res['code']) || $res['code'] != 200) {
         $msg = isset($res['message']) ? $res['message'] : 'Failed to connect the api server.';
         $res['status'] = 0;
         $res['message'] = $msg;
         //throw new \UnexpectedValueException($msg);
     }
     $artist = array();
     foreach ($res['album']['artists'] as $v) {
         $artist[] = array('id' => $v['id'], 'name' => $v['name'], 'pic' => $v['picUrl']);
     }
     $songs = array();
     foreach ($res['album']['songs'] as $v) {
         $songs[] = array('id' => $v['id'], 'name' => $v['name'], 'duration' => $v['duration'], 'mp3' => $v['mp3Url']);
     }
     // return data
     $return = array('id' => $res['album']['id'], 'name' => $res['album']['name'], 'cover' => $res['album']['picUrl'], 'releaseTime' => $res['album']['publishTime']);
     $return['artist'] = $artist;
     $return['songs'] = $songs;
     return $return;
 }
 public function testClientOptionsOverrideHttpOptions()
 {
     Net_Http::setTimeout(1000);
     $client = new Net_Http_Client();
     $client->setTimeout(2000);
     $opts = $client->getOptions();
     $this->assertArrayHasKey(CURLOPT_TIMEOUT, $opts);
     $this->assertEquals(2000, $opts[CURLOPT_TIMEOUT]);
 }
Example #3
0
 public function componentOnlineThemes()
 {
     try {
         $client = new \Net_Http_Client();
         $client->get('http://www.thebuggenie.com/themes.json');
         $json_themes = json_decode($client->getBody());
     } catch (\Exception $e) {
     }
     $themes = array();
     $existing_themes = framework\Context::getThemes();
     if (isset($json_themes) && isset($json_themes->featured)) {
         foreach ($json_themes->featured as $key => $theme) {
             if (!array_key_exists($theme->key, $existing_themes)) {
                 $themes[] = $theme;
             }
         }
     }
     $this->themes = $themes;
 }
 /** {@inheritdoc}
  */
 public function getStatus()
 {
     return $this->isMock() ? 200 : parent::getStatus();
 }
Example #5
0
 public function runUserdata(framework\Request $request)
 {
     if ($this->getUser()->isGuest()) {
         return $this->renderJSON(array());
     } else {
         $data = array();
         if ($request->isPost()) {
             switch ($request['say']) {
                 case 'install-module':
                     try {
                         entities\Module::downloadModule($request['module_key']);
                         $module = entities\Module::installModule($request['module_key']);
                         $data['installed'] = true;
                         $data['module_key'] = $request['module_key'];
                         $data['module'] = $this->getComponentHTML('configuration/modulebox', array('module' => $module));
                     } catch (framework\exceptions\ModuleDownloadException $e) {
                         $this->getResponse()->setHttpStatus(400);
                         switch ($e->getCode()) {
                             case framework\exceptions\ModuleDownloadException::JSON_NOT_FOUND:
                                 return $this->renderJSON(array('message' => $this->getI18n()->__('An error occured when trying to retrieve the module data')));
                                 break;
                             case framework\exceptions\ModuleDownloadException::FILE_NOT_FOUND:
                                 return $this->renderJSON(array('message' => $this->getI18n()->__('The module could not be downloaded')));
                                 break;
                         }
                     } catch (\Exception $e) {
                         $this->getResponse()->setHttpStatus(400);
                         return $this->renderJSON(array('message' => $this->getI18n()->__('An error occured when trying to install the module')));
                     }
                     break;
                 case 'install-theme':
                     try {
                         entities\Module::downloadTheme($request['theme_key']);
                         $data['installed'] = true;
                         $data['theme_key'] = $request['theme_key'];
                         $themes = framework\Context::getThemes();
                         $data['theme'] = $this->getComponentHTML('configuration/theme', array('theme' => $themes[$request['theme_key']]));
                     } catch (framework\exceptions\ModuleDownloadException $e) {
                         $this->getResponse()->setHttpStatus(400);
                         switch ($e->getCode()) {
                             case framework\exceptions\ModuleDownloadException::JSON_NOT_FOUND:
                                 return $this->renderJSON(array('message' => $this->getI18n()->__('An error occured when trying to retrieve the module data')));
                                 break;
                             case framework\exceptions\ModuleDownloadException::FILE_NOT_FOUND:
                                 return $this->renderJSON(array('message' => $this->getI18n()->__('The module could not be downloaded')));
                                 break;
                         }
                     } catch (\Exception $e) {
                         $this->getResponse()->setHttpStatus(400);
                         return $this->renderJSON(array('message' => $this->getI18n()->__('An error occured when trying to install the module')));
                     }
                     break;
                 case 'notificationstatus':
                     $notification = tables\Notifications::getTable()->selectById($request['notification_id']);
                     $data['notification_id'] = $request['notification_id'];
                     $data['is_read'] = 1;
                     if ($notification instanceof entities\Notification) {
                         $notification->setIsRead(!$notification->isRead());
                         $notification->save();
                         $data['is_read'] = (int) $notification->isRead();
                         $this->getUser()->markNotificationGroupedNotificationsRead($notification);
                     }
                     break;
                 case 'notificationsread':
                     $this->getUser()->markAllNotificationsRead();
                     $data['all'] = 'read';
                     break;
             }
         } else {
             switch ($request['say']) {
                 case 'get_module_updates':
                     $addons_param = array();
                     foreach ($request['addons'] as $addon) {
                         $addons_param[] = 'addons[]=' . $addon;
                     }
                     try {
                         $client = new \Net_Http_Client();
                         $client->get('http://www.thebuggenie.com/addons.json?' . join('&', $addons_param));
                         $addons_json = json_decode($client->getBody(), true);
                     } catch (\Exception $e) {
                     }
                     return $this->renderJSON($addons_json);
                     break;
                 case 'getsearchcounts':
                     $counts_json = array();
                     foreach ($request['search_ids'] as $search_id) {
                         if (is_numeric($search_id)) {
                             $search = tables\SavedSearches::getTable()->selectById($search_id);
                         } else {
                             $predefined_id = str_replace('predefined_', '', $search_id);
                             $search = \thebuggenie\core\entities\SavedSearch::getPredefinedSearchObject($predefined_id);
                         }
                         if ($search instanceof entities\SavedSearch) {
                             $counts_json[$search_id] = $search->getTotalNumberOfIssues();
                         }
                     }
                     return $this->renderJSON($counts_json);
                     break;
                 case 'get_theme_updates':
                     $addons_param = array();
                     foreach ($request['addons'] as $addon) {
                         $addons_param[] = 'themes[]=' . $addon;
                     }
                     try {
                         $client = new \Net_Http_Client();
                         $client->get('http://www.thebuggenie.com/themes.json?' . join('&', $addons_param));
                         $addons_json = json_decode($client->getBody(), true);
                     } catch (\Exception $e) {
                     }
                     return $this->renderJSON($addons_json);
                     break;
                 case 'verify_module_update_file':
                     $filename = THEBUGGENIE_CACHE_PATH . $request['module_key'] . '.zip';
                     $exists = file_exists($filename) && dirname($filename) . DS == THEBUGGENIE_CACHE_PATH;
                     return $this->renderJSON(array('verified' => (int) $exists));
                     break;
                 case 'get_modules':
                     return $this->renderComponent('configuration/onlinemodules');
                     break;
                 case 'get_themes':
                     return $this->renderComponent('configuration/onlinethemes');
                     break;
                 case 'get_mentionables':
                     switch ($request['target_type']) {
                         case 'issue':
                             $target = entities\Issue::getB2DBTable()->selectById($request['target_id']);
                             break;
                         case 'article':
                             $target = \thebuggenie\modules\publish\entities\tables\Articles::getTable()->selectById($request['target_id']);
                             break;
                         case 'project':
                             $target = tables\Projects::getTable()->selectById($request['target_id']);
                             break;
                     }
                     $mentionables = array();
                     if (isset($target) && $target instanceof \thebuggenie\core\helpers\MentionableProvider) {
                         foreach ($target->getMentionableUsers() as $user) {
                             if ($user->isOpenIdLocked()) {
                                 continue;
                             }
                             $mentionables[$user->getID()] = array('username' => $user->getUsername(), 'name' => $user->getName(), 'image' => $user->getAvatarURL());
                         }
                     }
                     foreach ($this->getUser()->getFriends() as $user) {
                         if ($user->isOpenIdLocked()) {
                             continue;
                         }
                         $mentionables[$user->getID()] = array('username' => $user->getUsername(), 'name' => $user->getName(), 'image' => $user->getAvatarURL());
                     }
                     foreach ($this->getUser()->getTeams() as $team) {
                         foreach ($team->getMembers() as $user) {
                             if ($user->isOpenIdLocked()) {
                                 continue;
                             }
                             $mentionables[$user->getID()] = array('username' => $user->getUsername(), 'name' => $user->getName(), 'image' => $user->getAvatarURL());
                         }
                     }
                     foreach ($this->getUser()->getClients() as $client) {
                         foreach ($client->getMembers() as $user) {
                             if ($user->isOpenIdLocked()) {
                                 continue;
                             }
                             $mentionables[$user->getID()] = array('username' => $user->getUsername(), 'name' => $user->getName(), 'image' => $user->getAvatarURL());
                         }
                     }
                     $data['mentionables'] = array_values($mentionables);
                     break;
                 default:
                     $data['unread_notifications_count'] = $this->getUser()->getNumberOfUnreadNotifications();
                     $data['unread_notifications'] = array();
                     foreach ($this->getUser()->getUnreadNotifications() as $unread_notification) {
                         $data['unread_notifications'][] = $unread_notification->getID();
                     }
                     $data['poll_interval'] = framework\Settings::getNotificationPollInterval();
             }
         }
         return $this->renderJSON($data);
     }
 }
 public function testFailOnErrorThrowsExceptionFromServerError()
 {
     $client = new Net_Http_Client();
     $client->failOnError();
     try {
         $client->get(self::HOST . '/basic/errors/crash');
     } catch (Net_Http_ServerError $e) {
         $this->assertContains('Internal Server Error', $e->getMessage());
         $this->assertEquals(500, $e->getCode());
         $this->assertEquals(500, $e->getResponse()->getStatus());
         $this->assertContains('The Server Exploded', $e->getBody());
     }
 }
Example #7
0
 public static function downloadPlugin($plugin_type, $plugin_key)
 {
     try {
         $client = new \Net_Http_Client();
         $client->get('http://www.thebuggenie.com/' . $plugin_type . 's/' . $plugin_key . '.json');
         $plugin_json = json_decode($client->getBody());
     } catch (\Exception $e) {
     }
     if (isset($plugin_json) && $plugin_json !== false) {
         $filename = THEBUGGENIE_CACHE_PATH . $plugin_type . '_' . $plugin_json->key . '.zip';
         $client->get($plugin_json->download);
         if ($client->getResponse()->getStatus() != 200) {
             throw new framework\exceptions\ModuleDownloadException("", framework\exceptions\ModuleDownloadException::JSON_NOT_FOUND);
         }
         file_put_contents($filename, $client->getBody());
         $module_zip = new \ZipArchive();
         $module_zip->open($filename);
         switch ($plugin_type) {
             case 'addon':
                 $target_folder = THEBUGGENIE_MODULES_PATH;
                 break;
             case 'theme':
                 $target_folder = THEBUGGENIE_PATH . 'themes';
                 break;
         }
         $module_zip->extractTo(realpath($target_folder));
         $module_zip->close();
         unlink($filename);
     } else {
         throw new framework\exceptions\ModuleDownloadException("", framework\exceptions\ModuleDownloadException::FILE_NOT_FOUND);
     }
 }