]> git.mxchange.org Git - friendica-addons.git/blobdiff - twitter/twitter.php
Merge pull request #486 from MrPetovan/task/fix-scrutinizer-issues
[friendica-addons.git] / twitter / twitter.php
index f88d4542b41a0c1a9f695216b3f9badbb7615b33..ccfdeb1fbf9cf04d458b167e9ae2e890c2f51a98 100644 (file)
  */
 
 use Friendica\App;
+use Friendica\Content\OEmbed;
 use Friendica\Core\Config;
 use Friendica\Core\PConfig;
 use Friendica\Core\Worker;
 use Friendica\Model\GContact;
+use Friendica\Model\Group;
 use Friendica\Model\Photo;
+use Friendica\Model\User;
 use Friendica\Object\Image;
 
 require_once 'include/enotify.php';
@@ -222,7 +225,7 @@ function twitter_settings_post(App $a, $post)
                        PConfig::set(local_user(), 'twitter', 'oauthsecret', $token['oauth_token_secret']);
                        PConfig::set(local_user(), 'twitter', 'post', 1);
                        //  reload the Addon Settings page, if we don't do it see Bug #42
-                       goaway($a->get_baseurl() . '/settings/connectors');
+                       goaway('settings/connectors');
                } else {
                        //  if no PIN is supplied in the POST variables, the user has changed the setting
                        //  to post a tweet for every new __public__ posting to the wall
@@ -256,16 +259,12 @@ function twitter_settings(App $a, &$s)
        $csecret = Config::get('twitter', 'consumersecret' );
        $otoken  = PConfig::get(local_user(), 'twitter', 'oauthtoken'  );
        $osecret = PConfig::get(local_user(), 'twitter', 'oauthsecret' );
-       $enabled = PConfig::get(local_user(), 'twitter', 'post');
-       $checked = (($enabled) ? ' checked="checked" ' : '');
-       $defenabled = PConfig::get(local_user(), 'twitter', 'post_by_default');
-       $defchecked = (($defenabled) ? ' checked="checked" ' : '');
-       $mirrorenabled = PConfig::get(local_user(), 'twitter', 'mirror_posts');
-       $mirrorchecked = (($mirrorenabled) ? ' checked="checked" ' : '');
-       $importenabled = PConfig::get(local_user(), 'twitter', 'import');
-       $importchecked = (($importenabled) ? ' checked="checked" ' : '');
-       $create_userenabled = PConfig::get(local_user(), 'twitter', 'create_user');
-       $create_userchecked = (($create_userenabled) ? ' checked="checked" ' : '');
+
+       $enabled            = intval(PConfig::get(local_user(), 'twitter', 'post'));
+       $defenabled         = intval(PConfig::get(local_user(), 'twitter', 'post_by_default'));
+       $mirrorenabled      = intval(PConfig::get(local_user(), 'twitter', 'mirror_posts'));
+       $importenabled      = intval(PConfig::get(local_user(), 'twitter', 'import'));
+       $create_userenabled = intval(PConfig::get(local_user(), 'twitter', 'create_user'));
 
        $css = (($enabled) ? '' : '-disabled');
 
@@ -318,36 +317,40 @@ function twitter_settings(App $a, &$s)
                        require_once 'library/twitteroauth.php';
                        $connection = new TwitterOAuth($ckey, $csecret, $otoken, $osecret);
                        $details = $connection->get('account/verify_credentials');
-                       $s .= '<div id="twitter-info" ><img id="twitter-avatar" src="' . $details->profile_image_url . '" /><p id="twitter-info-block">' . t('Currently connected to: ') . '<a href="https://twitter.com/' . $details->screen_name . '" target="_twitter">' . $details->screen_name . '</a><br /><em>' . $details->description . '</em></p></div>';
-                       $s .= '<p>' . t('If enabled all your <strong>public</strong> postings can be posted to the associated Twitter account. You can choose to do so by default (here) or for every posting separately in the posting options when writing the entry.') . '</p>';
-                       if ($a->user['hidewall']) {
-                               $s .= '<p>' . t('<strong>Note</strong>: Due your privacy settings (<em>Hide your profile details from unknown viewers?</em>) the link potentially included in public postings relayed to Twitter will lead the visitor to a blank page informing the visitor that the access to your profile has been restricted.') . '</p>';
-                       }
-                       $s .= '<div id="twitter-enable-wrapper">';
-                       $s .= '<label id="twitter-enable-label" for="twitter-checkbox">' . t('Allow posting to Twitter') . '</label>';
-                       $s .= '<input id="twitter-checkbox" type="checkbox" name="twitter-enable" value="1" ' . $checked . '/>';
-                       $s .= '<div class="clear"></div>';
-                       $s .= '<label id="twitter-default-label" for="twitter-default">' . t('Send public postings to Twitter by default') . '</label>';
-                       $s .= '<input id="twitter-default" type="checkbox" name="twitter-default" value="1" ' . $defchecked . '/>';
-                       $s .= '<div class="clear"></div>';
 
-                       $s .= '<label id="twitter-mirror-label" for="twitter-mirror">' . t('Mirror all posts from twitter that are no replies') . '</label>';
-                       $s .= '<input id="twitter-mirror" type="checkbox" name="twitter-mirror" value="1" ' . $mirrorchecked . '/>';
+                       $field_checkbox = get_markup_template('field_checkbox.tpl');
+
+                       $s .= '<div id="twitter-info" >
+                               <p>' . t('Currently connected to: ') . '<a href="https://twitter.com/' . $details->screen_name . '" target="_twitter">' . $details->screen_name . '</a>
+                                       <button type="submit" name="twitter-disconnect" value="1">' . t('Disconnect') . '</button>
+                               </p>
+                               <p id="twitter-info-block">
+                                       <a href="https://twitter.com/' . $details->screen_name . '" target="_twitter"><img id="twitter-avatar" src="' . $details->profile_image_url . '" /></a>
+                                       <em>' . $details->description . '</em>
+                               </p>
+                       </div>';
                        $s .= '<div class="clear"></div>';
-                       $s .= '</div>';
 
-                       $s .= '<label id="twitter-import-label" for="twitter-import">' . t('Import the remote timeline') . '</label>';
-                       $s .= '<input id="twitter-import" type="checkbox" name="twitter-import" value="1" ' . $importchecked . '/>';
-                       $s .= '<div class="clear"></div>';
+                       $s .= replace_macros($field_checkbox, array(
+                               '$field' => array('twitter-enable', t('Allow posting to Twitter'), $enabled, t('If enabled all your <strong>public</strong> postings can be posted to the associated Twitter account. You can choose to do so by default (here) or for every posting separately in the posting options when writing the entry.'))
+                       ));
+                       if ($a->user['hidewall']) {
+                               $s .= '<p>' . t('<strong>Note</strong>: Due to your privacy settings (<em>Hide your profile details from unknown viewers?</em>) the link potentially included in public postings relayed to Twitter will lead the visitor to a blank page informing the visitor that the access to your profile has been restricted.') . '</p>';
+                       }
+                       $s .= replace_macros($field_checkbox, array(
+                               '$field' => array('twitter-default', t('Send public postings to Twitter by default'), $defenabled, '')
+                       ));
+                       $s .= replace_macros($field_checkbox, array(
+                               '$field' => array('twitter-mirror', t('Mirror all posts from twitter that are no replies'), $mirrorenabled, '')
+                       ));
+                       $s .= replace_macros($field_checkbox, array(
+                               '$field' => array('twitter-import', t('Import the remote timeline'), $importenabled, '')
+                       ));
+                       $s .= replace_macros($field_checkbox, array(
+                               '$field' => array('twitter-create_user', t('Automatically create contacts'), $create_userenabled, '')
+                       ));
 
-                       $s .= '<label id="twitter-create_user-label" for="twitter-create_user">' . t('Automatically create contacts') . '</label>';
-                       $s .= '<input id="twitter-create_user" type="checkbox" name="twitter-create_user" value="1" ' . $create_userchecked . '/>';
                        $s .= '<div class="clear"></div>';
-
-                       $s .= '<div id="twitter-disconnect-wrapper">';
-                       $s .= '<label id="twitter-disconnect-label" for="twitter-disconnect">' . t('Clear OAuth configuration') . '</label>';
-                       $s .= '<input id="twitter-disconnect" type="checkbox" name="twitter-disconnect" value="1" />';
-                       $s .= '</div><div class="clear"></div>';
                        $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="twitter-submit" class="settings-submit" value="' . t('Save Settings') . '" /></div>';
                }
        }
@@ -465,7 +468,7 @@ function twitter_post_hook(App $a, &$b)
 
                // Dont't post if the post doesn't belong to us.
                // This is a check for forum postings
-               $self = dba::select('contact', array('id'), array('uid' => $b['uid'], 'self' => true), array('limit' => 1));
+               $self = dba::selectFirst('contact', ['id'], ['uid' => $b['uid'], 'self' => true]);
                if ($b['contact-id'] != $self['id']) {
                        return;
                }
@@ -520,7 +523,7 @@ function twitter_post_hook(App $a, &$b)
 
                $max_char = 280;
                require_once "include/plaintext.php";
-               $msgarr = plaintext($a, $b, $max_char, true, 8);
+               $msgarr = plaintext($b, $max_char, true, 8);
                $msg = $msgarr["text"];
 
                if (($msg == "") && isset($msgarr["title"])) {
@@ -589,7 +592,7 @@ function twitter_post_hook(App $a, &$b)
 // -----------------
                        $max_char = 280;
                        require_once "include/plaintext.php";
-                       $msgarr = plaintext($a, $b, $max_char, true, 8);
+                       $msgarr = plaintext($b, $max_char, true, 8);
                        $msg = $msgarr["text"];
 
                        if (($msg == "") && isset($msgarr["title"])) {
@@ -786,7 +789,7 @@ function twitter_prepare_body(App $a, &$b)
                        }
                }
 
-               $msgarr = plaintext($a, $item, $max_char, true, 8);
+               $msgarr = plaintext($item, $max_char, true, 8);
                $msg = $msgarr["text"];
 
                if (isset($msgarr["url"]) && ($msgarr["type"] != "photo")) {
@@ -1055,14 +1058,7 @@ function twitter_fetch_contact($uid, $contact, $create_user)
 
                $contact_id = $r[0]['id'];
 
-               $g = q("SELECT def_gid FROM user WHERE uid = %d LIMIT 1",
-                       intval($uid)
-               );
-
-               if ($g && intval($g[0]['def_gid'])) {
-                       require_once 'include/group.php';
-                       group_add_member($uid, '', $contact_id, $g[0]['def_gid']);
-               }
+               Group::addMember(User::getDefaultGroup($uid), $contact_id);
 
                $photos = Photo::importProfilePhoto($avatar, $uid, $contact_id, true);
 
@@ -1177,7 +1173,6 @@ function twitter_fetchuser(App $a, $uid, $screen_name = "", $user_id = "")
 
 function twitter_expand_entities(App $a, $body, $item, $no_tags = false, $picture)
 {
-       require_once "include/oembed.php";
        require_once "include/network.php";
 
        $tags = "";
@@ -1196,7 +1191,7 @@ function twitter_expand_entities(App $a, $body, $item, $no_tags = false, $pictur
                        if ($url->url && $url->expanded_url && $url->display_url) {
                                $expanded_url = original_url($url->expanded_url);
 
-                               $oembed_data = oembed_fetch_url($expanded_url);
+                               $oembed_data = OEmbed::fetchURL($expanded_url);
 
                                // Quickfix: Workaround for URL with "[" and "]" in it
                                if (strpos($expanded_url, "[") || strpos($expanded_url, "]")) {