123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296 |
- <?php
- defined('GNUSOCIAL') || die();
- define('ANONYMOUS_FAVE_PLUGIN_VERSION', '0.1.0');
- class AnonymousFavePlugin extends Plugin
- {
-
-
- public $restricted = array();
- public function onArgsInitialize()
- {
-
- common_ensure_session();
- }
-
- public function onCheckSchema()
- {
- $schema = Schema::get();
-
- $schema->ensureTable('fave_tally', Fave_tally::schemaDef());
- return true;
- }
- public function onEndShowHTML($action)
- {
- if (!common_logged_in()) {
-
- common_set_returnto($action->selfUrl());
- }
- }
- public function onEndShowScripts($action)
- {
-
-
- $action->inlineScript('SN.U.NoticeFavor();');
- }
- public function onStartInitializeRouter($m)
- {
- $m->connect('main/anonfavor', array('action' => 'AnonFavor'));
- $m->connect('main/anondisfavor', array('action' => 'AnonDisFavor'));
- return true;
- }
- public function onStartShowNoticeOptionItems(NoticeListItem $item): bool
- {
- if (!common_logged_in()) {
- $item->out->elementStart('div', 'notice-options');
- if (Event::handle('StartShowFaveForm', [$item])) {
- Event::handle('EndShowFaveForm', [$item]);
- }
- $item->out->elementEnd('div');
- }
- return true;
- }
- public function onStartShowFaveForm($item)
- {
- if (!common_logged_in() && $this->hasAnonFaving($item)) {
- $profile = AnonymousFavePlugin::getAnonProfile();
- if ($profile instanceof Profile) {
- if (Fave::existsForProfile($item->notice, $profile)) {
- $disfavor = new AnonDisFavorForm($item->out, $item->notice);
- $disfavor->show();
- } else {
- $favor = new AnonFavorForm($item->out, $item->notice);
- $favor->show();
- }
- }
- }
- return true;
- }
- public function onEndFavorNoticeForm($form, $notice)
- {
- $this->showTally($form->out, $notice);
- }
- public function onEndDisFavorNoticeForm($form, $notice)
- {
- $this->showTally($form->out, $notice);
- }
- private function showTally($out, Notice $notice): void
- {
- $tally = Fave_tally::ensureTally($notice->id);
- if (!empty($tally)) {
- $out->elementStart(
- 'div',
- array(
- 'id' => 'notice-' . $notice->id . '-tally',
- 'class' => 'notice-tally'
- )
- );
- $out->elementStart('span', array('class' => 'fave-tally-title'));
-
- $out->raw(sprintf(_m("Favored")));
- $out->elementEnd('span');
- $out->elementStart('span', array('class' => 'fave-tally'));
- $out->raw($tally->count);
- $out->elementEnd('span');
- $out->elementEnd('div');
- }
- }
- public function onEndFavorNotice($profile, $notice)
- {
- $tally = Fave_tally::increment($notice->id);
- }
- public function onEndDisfavorNotice($profile, $notice)
- {
- $tally = Fave_tally::decrement($notice->id);
- }
-
- public function onNoticeDeleteRelated(Notice $notice): bool
- {
- $ft = Fave_tally::getKV('notice_id', $notice->id);
- if (!empty($ft)) {
- $ft->delete();
- }
- return true;
- }
- private static function createAnonProfile(): Profile
- {
-
- list($proxy, $ip) = common_client_ip();
-
- $baseNickname = $ip . '-' . time() . '-' . common_random_hexstr(5);
- $profile = new Profile();
- $profile->nickname = $baseNickname;
- $id = $profile->insert();
- if (!$id) {
-
- throw new ServerException(_m("Could not create anonymous user session."));
- }
-
- $orig = clone($profile);
- $profile->nickname = 'anon-' . $id . '-' . $baseNickname;
- $result = $profile->update($orig);
- if (!$result) {
-
- throw new ServerException(_m("Could not create anonymous user session."));
- }
- common_log(
- LOG_INFO,
- "AnonymousFavePlugin - created profile for anonymous user from IP: "
- . $ip
- . ', nickname = '
- . $profile->nickname
- );
- return $profile;
- }
- public static function getAnonProfile()
- {
- $token = $_SESSION['anon_token'];
- $anon = base64_decode($token);
- $profile = null;
- if (!empty($anon) && substr($anon, 0, 5) == 'anon-') {
- $parts = explode('-', $anon);
- $id = $parts[1];
-
- $profile = Profile::getKV('id', $id);
- } else {
- $profile = AnonymousFavePlugin::createAnonProfile();
-
- $_SESSION['anon_token'] = base64_encode($profile->nickname);
- }
- return $profile;
- }
-
- private function hasAnonFaving($item): bool
- {
- $profile = Profile::getKV('id', $item->notice->profile_id);
- if (in_array($profile->nickname, $this->restricted)) {
- return false;
- }
- return true;
- }
-
- public function onPluginVersion(array &$versions): bool
- {
- $url = GNUSOCIAL_ENGINE_REPO_URL . 'tree/master/plugins/AnonymousFave';
- $versions[] = array('name' => 'AnonymousFave',
- 'version' => ANONYMOUS_FAVE_PLUGIN_VERSION,
- 'author' => 'Zach Copley',
- 'homepage' => $url,
- 'rawdescription' =>
-
- _m('Allow anonymous users to favorite notices.'));
- return true;
- }
- }
|