X-Git-Url: https://git.mxchange.org/?p=friendica-addons.git;a=blobdiff_plain;f=statusnet%2Fstatusnet.php;h=16f68a8aa1f5b36f5cf190ebdcddb4b8278b9896;hp=9c44ef0236b68182a2e2e2bf48e31491d6dc6743;hb=39dd3dffe07efd69fa1ac6d0bd243c7fc0e3a66f;hpb=092221d4717bb40354d12ba201f9466a843514cc diff --git a/statusnet/statusnet.php b/statusnet/statusnet.php index 9c44ef02..16f68a8a 100644 --- a/statusnet/statusnet.php +++ b/statusnet/statusnet.php @@ -32,115 +32,57 @@ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * */ - - -/*** - * We have to alter the TwitterOAuth class a little bit to work with any GNU Social - * installation abroad. Basically it's only make the API path variable and be happy. - * - * Thank you guys for the Twitter compatible API! - */ - define('STATUSNET_DEFAULT_POLL_INTERVAL', 5); // given in minutes -require_once('library/twitteroauth.php'); -require_once('include/enotify.php'); -require_once("include/socgraph.php"); - -class StatusNetOAuth extends TwitterOAuth { - function get_maxlength() { - $config = $this->get($this->host . 'statusnet/config.json'); - return $config->site->textlimit; - } - function accessTokenURL() { return $this->host.'oauth/access_token'; } - function authenticateURL() { return $this->host.'oauth/authenticate'; } - function authorizeURL() { return $this->host.'oauth/authorize'; } - function requestTokenURL() { return $this->host.'oauth/request_token'; } - function __construct($apipath, $consumer_key, $consumer_secret, $oauth_token = NULL, $oauth_token_secret = NULL) { - parent::__construct($consumer_key, $consumer_secret, $oauth_token, $oauth_token_secret); - $this->host = $apipath; - } - /** - * Make an HTTP request - * - * @return API results - * - * Copied here from the twitteroauth library and complemented by applying the proxy settings of friendica - */ - function http($url, $method, $postfields = NULL) { - $this->http_info = array(); - $ci = curl_init(); - /* Curl settings */ - $prx = get_config('system','proxy'); - if(strlen($prx)) { - curl_setopt($ci, CURLOPT_HTTPPROXYTUNNEL, 1); - curl_setopt($ci, CURLOPT_PROXY, $prx); - $prxusr = get_config('system','proxyuser'); - if(strlen($prxusr)) - curl_setopt($ci, CURLOPT_PROXYUSERPWD, $prxusr); - } - curl_setopt($ci, CURLOPT_USERAGENT, $this->useragent); - curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, $this->connecttimeout); - curl_setopt($ci, CURLOPT_TIMEOUT, $this->timeout); - curl_setopt($ci, CURLOPT_RETURNTRANSFER, TRUE); - curl_setopt($ci, CURLOPT_HTTPHEADER, array('Expect:')); - curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, $this->ssl_verifypeer); - curl_setopt($ci, CURLOPT_HEADERFUNCTION, array($this, 'getHeader')); - curl_setopt($ci, CURLOPT_HEADER, FALSE); - - switch ($method) { - case 'POST': - curl_setopt($ci, CURLOPT_POST, TRUE); - if (!empty($postfields)) { - curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields); - } - break; - case 'DELETE': - curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'DELETE'); - if (!empty($postfields)) { - $url = "{$url}?{$postfields}"; - } - } - - curl_setopt($ci, CURLOPT_URL, $url); - $response = curl_exec($ci); - $this->http_code = curl_getinfo($ci, CURLINFO_HTTP_CODE); - $this->http_info = array_merge($this->http_info, curl_getinfo($ci)); - $this->url = $url; - curl_close ($ci); - return $response; - } -} +require_once __DIR__ . DIRECTORY_SEPARATOR . 'library' . DIRECTORY_SEPARATOR . 'statusnetoauth.php'; +require_once 'include/enotify.php'; + +use Codebird\Codebird; +use CodebirdSN\CodebirdSN; +use Friendica\App; +use Friendica\Content\OEmbed; +use Friendica\Content\Text\BBCode; +use Friendica\Content\Text\Plaintext; +use Friendica\Core\Addon; +use Friendica\Core\Config; +use Friendica\Core\L10n; +use Friendica\Core\PConfig; +use Friendica\Model\GContact; +use Friendica\Model\Group; +use Friendica\Model\Item; +use Friendica\Model\Photo; +use Friendica\Model\User; +use Friendica\Util\DateTimeFormat; +use Friendica\Util\Network; function statusnet_install() { // we need some hooks, for the configuration and for sending tweets - register_hook('connector_settings', 'addon/statusnet/statusnet.php', 'statusnet_settings'); - register_hook('connector_settings_post', 'addon/statusnet/statusnet.php', 'statusnet_settings_post'); - register_hook('notifier_normal', 'addon/statusnet/statusnet.php', 'statusnet_post_hook'); - register_hook('post_local', 'addon/statusnet/statusnet.php', 'statusnet_post_local'); - register_hook('jot_networks', 'addon/statusnet/statusnet.php', 'statusnet_jot_nets'); - register_hook('cron', 'addon/statusnet/statusnet.php', 'statusnet_cron'); - register_hook('prepare_body', 'addon/statusnet/statusnet.php', 'statusnet_prepare_body'); - register_hook('check_item_notification','addon/statusnet/statusnet.php', 'statusnet_check_item_notification'); + Addon::registerHook('connector_settings', 'addon/statusnet/statusnet.php', 'statusnet_settings'); + Addon::registerHook('connector_settings_post', 'addon/statusnet/statusnet.php', 'statusnet_settings_post'); + Addon::registerHook('notifier_normal', 'addon/statusnet/statusnet.php', 'statusnet_post_hook'); + Addon::registerHook('post_local', 'addon/statusnet/statusnet.php', 'statusnet_post_local'); + Addon::registerHook('jot_networks', 'addon/statusnet/statusnet.php', 'statusnet_jot_nets'); + Addon::registerHook('cron', 'addon/statusnet/statusnet.php', 'statusnet_cron'); + Addon::registerHook('prepare_body', 'addon/statusnet/statusnet.php', 'statusnet_prepare_body'); + Addon::registerHook('check_item_notification', 'addon/statusnet/statusnet.php', 'statusnet_check_item_notification'); logger("installed GNU Social"); } - -function statusnet_uninstall() { - unregister_hook('connector_settings', 'addon/statusnet/statusnet.php', 'statusnet_settings'); - unregister_hook('connector_settings_post', 'addon/statusnet/statusnet.php', 'statusnet_settings_post'); - unregister_hook('notifier_normal', 'addon/statusnet/statusnet.php', 'statusnet_post_hook'); - unregister_hook('post_local', 'addon/statusnet/statusnet.php', 'statusnet_post_local'); - unregister_hook('jot_networks', 'addon/statusnet/statusnet.php', 'statusnet_jot_nets'); - unregister_hook('cron', 'addon/statusnet/statusnet.php', 'statusnet_cron'); - unregister_hook('prepare_body', 'addon/statusnet/statusnet.php', 'statusnet_prepare_body'); - unregister_hook('check_item_notification','addon/statusnet/statusnet.php', 'statusnet_check_item_notification'); +function statusnet_uninstall() +{ + Addon::unregisterHook('connector_settings', 'addon/statusnet/statusnet.php', 'statusnet_settings'); + Addon::unregisterHook('connector_settings_post', 'addon/statusnet/statusnet.php', 'statusnet_settings_post'); + Addon::unregisterHook('notifier_normal', 'addon/statusnet/statusnet.php', 'statusnet_post_hook'); + Addon::unregisterHook('post_local', 'addon/statusnet/statusnet.php', 'statusnet_post_local'); + Addon::unregisterHook('jot_networks', 'addon/statusnet/statusnet.php', 'statusnet_jot_nets'); + Addon::unregisterHook('cron', 'addon/statusnet/statusnet.php', 'statusnet_cron'); + Addon::unregisterHook('prepare_body', 'addon/statusnet/statusnet.php', 'statusnet_prepare_body'); + Addon::unregisterHook('check_item_notification', 'addon/statusnet/statusnet.php', 'statusnet_check_item_notification'); // old setting - remove only - unregister_hook('post_local_end', 'addon/statusnet/statusnet.php', 'statusnet_post_hook'); - unregister_hook('plugin_settings', 'addon/statusnet/statusnet.php', 'statusnet_settings'); - unregister_hook('plugin_settings_post', 'addon/statusnet/statusnet.php', 'statusnet_settings_post'); - + Addon::unregisterHook('post_local_end', 'addon/statusnet/statusnet.php', 'statusnet_post_hook'); + Addon::unregisterHook('addon_settings', 'addon/statusnet/statusnet.php', 'statusnet_settings'); + Addon::unregisterHook('addon_settings_post', 'addon/statusnet/statusnet.php', 'statusnet_settings_post'); } function statusnet_check_item_notification($a, &$notification_data) { @@ -155,8 +97,8 @@ function statusnet_jot_nets(&$a,&$b) { if(intval($statusnet_post) == 1) { $statusnet_defpost = get_pconfig(local_user(),'statusnet','post_by_default'); $selected = ((intval($statusnet_defpost) == 1) ? ' checked="checked" ' : ''); - $b .= '
' - . t('Post to GNU Social') . '
'; + $b .= '
' + . L10n::t('Post to GNU Social') . '
'; } } @@ -184,24 +126,53 @@ function statusnet_settings_post ($a,$post) { del_pconfig(local_user(), 'statusnet', 'create_user'); del_pconfig(local_user(), 'statusnet', 'own_id'); } else { - if (isset($_POST['statusnet-preconf-apiurl'])) { - /*** - * If the user used one of the preconfigured GNU Social server credentials - * use them. All the data are available in the global config. - * Check the API Url never the less and blame the admin if it's not working ^^ - */ - $globalsn = get_config('statusnet', 'sites'); - foreach ( $globalsn as $asn) { - if ($asn['apiurl'] == $_POST['statusnet-preconf-apiurl'] ) { - $apibase = $asn['apiurl']; - $c = fetch_url( $apibase . 'statusnet/version.xml' ); + if (isset($_POST['statusnet-preconf-apiurl'])) { + /* * * + * If the user used one of the preconfigured GNU Social server credentials + * use them. All the data are available in the global config. + * Check the API Url never the less and blame the admin if it's not working ^^ + */ + $globalsn = Config::get('statusnet', 'sites'); + foreach ($globalsn as $asn) { + if ($asn['apiurl'] == $_POST['statusnet-preconf-apiurl']) { + $apibase = $asn['apiurl']; + $c = Network::fetchUrl($apibase . 'statusnet/version.xml'); + if (strlen($c) > 0) { + PConfig::set(local_user(), 'statusnet', 'consumerkey', $asn['consumerkey']); + PConfig::set(local_user(), 'statusnet', 'consumersecret', $asn['consumersecret']); + PConfig::set(local_user(), 'statusnet', 'baseapi', $asn['apiurl']); + //PConfig::set(local_user(), 'statusnet', 'application_name', $asn['applicationname'] ); + } else { + notice(L10n::t('Please contact your site administrator.
The provided API URL is not valid.') . EOL . $asn['apiurl'] . EOL); + } + } + } + goaway('settings/connectors'); + } else { + if (isset($_POST['statusnet-consumersecret'])) { + // check if we can reach the API of the GNU Social server + // we'll check the API Version for that, if we don't get one we'll try to fix the path but will + // resign quickly after this one try to fix the path ;-) + $apibase = $_POST['statusnet-baseapi']; + $c = Network::fetchUrl($apibase . 'statusnet/version.xml'); if (strlen($c) > 0) { set_pconfig(local_user(), 'statusnet', 'consumerkey', $asn['consumerkey'] ); set_pconfig(local_user(), 'statusnet', 'consumersecret', $asn['consumersecret'] ); set_pconfig(local_user(), 'statusnet', 'baseapi', $asn['apiurl'] ); //set_pconfig(local_user(), 'statusnet', 'application_name', $asn['applicationname'] ); } else { - notice( t('Please contact your site administrator.
The provided API URL is not valid.').EOL.$asn['apiurl'].EOL ); + // the API path is not correct, maybe missing trailing / ? + $apibase = $apibase . '/'; + $c = Network::fetchUrl($apibase . 'statusnet/version.xml'); + if (strlen($c) > 0) { + // ok the API path is now correct, let's save the settings + PConfig::set(local_user(), 'statusnet', 'consumerkey', $_POST['statusnet-consumerkey']); + PConfig::set(local_user(), 'statusnet', 'consumersecret', $_POST['statusnet-consumersecret']); + PConfig::set(local_user(), 'statusnet', 'baseapi', $apibase); + } else { + // still not the correct API base, let's do noting + notice(L10n::t('We could not contact the GNU Social API with the Path you entered.') . EOL); + } } } } @@ -229,8 +200,37 @@ function statusnet_settings_post ($a,$post) { set_pconfig(local_user(), 'statusnet', 'consumersecret', $_POST['statusnet-consumersecret']); set_pconfig(local_user(), 'statusnet', 'baseapi', $apibase ); } else { - // still not the correct API base, let's do noting - notice( t('We could not contact the GNU Social API with the Path you entered.').EOL ); + if (isset($_POST['statusnet-pin'])) { + // if the user supplied us with a PIN from GNU Social, let the magic of OAuth happen + $api = PConfig::get(local_user(), 'statusnet', 'baseapi'); + $ckey = PConfig::get(local_user(), 'statusnet', 'consumerkey'); + $csecret = PConfig::get(local_user(), 'statusnet', 'consumersecret'); + // the token and secret for which the PIN was generated were hidden in the settings + // form as token and token2, we need a new connection to GNU Social using these token + // and secret to request a Access Token with the PIN + $connection = new StatusNetOAuth($api, $ckey, $csecret, $_POST['statusnet-token'], $_POST['statusnet-token2']); + $token = $connection->getAccessToken($_POST['statusnet-pin']); + // ok, now that we have the Access Token, save them in the user config + PConfig::set(local_user(), 'statusnet', 'oauthtoken', $token['oauth_token']); + PConfig::set(local_user(), 'statusnet', 'oauthsecret', $token['oauth_token_secret']); + PConfig::set(local_user(), 'statusnet', 'post', 1); + PConfig::set(local_user(), 'statusnet', 'post_taglinks', 1); + // reload the Addon Settings page, if we don't do it see Bug #42 + goaway('settings/connectors'); + } else { + // if no PIN is supplied in the POST variables, the user has changed the setting + // to post a dent for every new __public__ posting to the wall + PConfig::set(local_user(), 'statusnet', 'post', intval($_POST['statusnet-enable'])); + PConfig::set(local_user(), 'statusnet', 'post_by_default', intval($_POST['statusnet-default'])); + PConfig::set(local_user(), 'statusnet', 'mirror_posts', intval($_POST['statusnet-mirror'])); + PConfig::set(local_user(), 'statusnet', 'import', intval($_POST['statusnet-import'])); + PConfig::set(local_user(), 'statusnet', 'create_user', intval($_POST['statusnet-create_user'])); + + if (!intval($_POST['statusnet-mirror'])) + PConfig::delete(local_user(), 'statusnet', 'lastid'); + + info(L10n::t('GNU Social settings updated.') . EOL); + } } } goaway($a->get_baseurl().'/settings/connectors'); @@ -299,11 +299,11 @@ function statusnet_settings(&$a,&$s) { $css = (($enabled) ? '' : '-disabled'); $s .= ''; - $s .= '

'. t('GNU Social Import/Export/Mirror').'

'; + $s .= '

' . L10n::t('GNU Social Import/Export/Mirror') . '

'; $s .= '
'; $s .= '
'; } -function statusnet_post_local(&$a,&$b) { - if($b['edit']) +function statusnet_post_local(&$a, &$b) { + if ($b['edit']) { return; + } - if((local_user()) && (local_user() == $b['uid']) && (! $b['private'])) { + if (!local_user() || (local_user() != $b['uid'])) { + return; + } - $statusnet_post = get_pconfig(local_user(),'statusnet','post'); - $statusnet_enable = (($statusnet_post && x($_REQUEST,'statusnet_enable')) ? intval($_REQUEST['statusnet_enable']) : 0); + $statusnet_post = get_pconfig(local_user(),'statusnet','post'); + $statusnet_enable = (($statusnet_post && x($_REQUEST,'statusnet_enable')) ? intval($_REQUEST['statusnet_enable']) : 0); - // if API is used, default to the chosen settings - if($_REQUEST['api_source'] && intval(get_pconfig(local_user(),'statusnet','post_by_default'))) - $statusnet_enable = 1; + // if API is used, default to the chosen settings + if ($b['api_source'] && intval(get_pconfig(local_user(),'statusnet','post_by_default'))) { + $statusnet_enable = 1; + } - if(! $statusnet_enable) - return; + if (!$statusnet_enable) { + return; + } - if(strlen($b['postopts'])) - $b['postopts'] .= ','; - $b['postopts'] .= 'statusnet'; + if (strlen($b['postopts'])) { + $b['postopts'] .= ','; } + + $b['postopts'] .= 'statusnet'; } function statusnet_action($a, $uid, $pid, $action) { @@ -494,8 +500,8 @@ function statusnet_post_hook(&$a,&$b) { // Looking if its a reply to a GNU Social post $hostlength = strlen($hostname) + 2; - if ((substr($b["parent-uri"], 0, $hostlength) != $hostname."::") AND (substr($b["extid"], 0, $hostlength) != $hostname."::") - AND (substr($b["thr-parent"], 0, $hostlength) != $hostname."::")) { + if ((substr($b["parent-uri"], 0, $hostlength) != $hostname."::") && (substr($b["extid"], 0, $hostlength) != $hostname."::") + && (substr($b["thr-parent"], 0, $hostlength) != $hostname."::")) { logger("statusnet_post_hook: no GNU Social post ".$b["parent"]); return; } @@ -523,18 +529,18 @@ function statusnet_post_hook(&$a,&$b) { $nicknameplain = "@".$nick; logger("statusnet_post_hook: comparing ".$nickname." and ".$nicknameplain." with ".$b["body"], LOGGER_DEBUG); - if ((strpos($b["body"], $nickname) === false) AND (strpos($b["body"], $nicknameplain) === false)) + if ((strpos($b["body"], $nickname) === false) && (strpos($b["body"], $nicknameplain) === false)) $b["body"] = $nickname." ".$b["body"]; logger("statusnet_post_hook: parent found ".print_r($orig_post, true), LOGGER_DEBUG); } else { $iscomment = false; - if($b['private'] OR !strstr($b['postopts'],'statusnet')) + if($b['private'] || !strstr($b['postopts'],'statusnet')) return; } - if (($b['verb'] == ACTIVITY_POST) AND $b['deleted']) + if (($b['verb'] == ACTIVITY_POST) && $b['deleted']) statusnet_action($a, $b["uid"], substr($orig_post["uri"], $hostlength), "delete"); if($b['verb'] == ACTIVITY_LIKE) { @@ -572,50 +578,42 @@ function statusnet_post_hook(&$a,&$b) { if (statusnet_is_retweet($a, $b['uid'], $b['body'])) return; - require_once('include/bbcode.php'); - $dent = new StatusNetOAuth($api,$ckey,$csecret,$otoken,$osecret); + $dent = new StatusNetOAuth($api, $ckey, $csecret, $otoken, $osecret); $max_char = $dent->get_maxlength(); // max. length for a dent set_pconfig($b['uid'], 'statusnet', 'max_char', $max_char); $tempfile = ""; - require_once("include/plaintext.php"); - require_once("include/network.php"); - $msgarr = plaintext($a, $b, $max_char, true, 7); + $msgarr = BBCode::toPlaintext($b, $max_char, true, 7); $msg = $msgarr["text"]; - if (($msg == "") AND isset($msgarr["title"])) - $msg = shortenmsg($msgarr["title"], $max_char - 50); + if (($msg == "") && isset($msgarr["title"])) + $msg = Plaintext::shorten($msgarr["title"], $max_char - 50); $image = ""; - if (isset($msgarr["url"]) AND ($msgarr["type"] != "photo")) { - if ((strlen($msgarr["url"]) > 20) AND - ((strlen($msg." \n".$msgarr["url"]) > $max_char))) - $msg .= " \n".short_link($msgarr["url"]); - else - $msg .= " \n".$msgarr["url"]; - } elseif (isset($msgarr["image"]) AND ($msgarr["type"] != "video")) + if (isset($msgarr["url"]) && ($msgarr["type"] != "photo")) { + $msg .= " \n" . $msgarr["url"]; + } elseif (isset($msgarr["image"]) && ($msgarr["type"] != "video")) { $image = $msgarr["image"]; if ($image != "") { - $img_str = fetch_url($image); + $img_str = Network::fetchUrl($image); $tempfile = tempnam(get_temppath(), "cache"); file_put_contents($tempfile, $img_str); $postdata = array("status" => $msg, "media[]" => $tempfile); } else $postdata = array("status"=>$msg); - // and now dent it :-) - if(strlen($msg)) { - + // and now send it :-) + if (strlen($msg)) { if ($iscomment) { $postdata["in_reply_to_status_id"] = substr($orig_post["uri"], $hostlength); logger('statusnet_post send reply '.print_r($postdata, true), LOGGER_DEBUG); } // New code that is able to post pictures - require_once("addon/statusnet/codebird.php"); + require_once __DIR__ . DIRECTORY_SEPARATOR . 'library' . DIRECTORY_SEPARATOR . 'codebirdsn.php'; $cb = \CodebirdSN\CodebirdSN::getInstance(); $cb->setAPIEndpoint($api); $cb->setConsumerKey($ckey, $csecret); @@ -631,12 +629,8 @@ function statusnet_post_hook(&$a,&$b) { if ($result->error) { logger('Send to GNU Social failed: "'.$result->error.'"'); } elseif ($iscomment) { - logger('statusnet_post: Update extid '.$result->id." for post id ".$b['id']); - q("UPDATE `item` SET `extid` = '%s', `body` = '%s' WHERE `id` = %d", - dbesc($hostname."::".$result->id), - dbesc($result->text), - intval($b['id']) - ); + logger('statusnet_post: Update extid ' . $result->id . " for post id " . $b['id']); + Item::update(['extid' => $hostname . "::" . $result->id, 'body' => $result->text], ['id' => $b['id']]); } } if ($tempfile != "") @@ -644,7 +638,9 @@ function statusnet_post_hook(&$a,&$b) { } } -function statusnet_plugin_admin_post(&$a){ +function statusnet_addon_admin_post(App $a) +{ + $sites = []; $sites = array(); @@ -676,17 +672,17 @@ function statusnet_plugin_admin_post(&$a){ } -function statusnet_plugin_admin(&$a, &$o){ - - $sites = get_config('statusnet','sites'); - $sitesform=array(); - if (is_array($sites)){ - foreach($sites as $id=>$s){ - $sitesform[] = Array( - 'sitename' => Array("sitename[$id]", "Site name", $s['sitename'], ""), - 'apiurl' => Array("apiurl[$id]", "Api url", $s['apiurl'], t("Base API Path \x28remember the trailing /\x29") ), - 'secret' => Array("secret[$id]", "Secret", $s['consumersecret'], ""), - 'key' => Array("key[$id]", "Key", $s['consumerkey'], ""), +function statusnet_addon_admin(App $a, &$o) +{ + $sites = Config::get('statusnet', 'sites'); + $sitesform = []; + if (is_array($sites)) { + foreach ($sites as $id => $s) { + $sitesform[] = [ + 'sitename' => ["sitename[$id]", "Site name", $s['sitename'], ""], + 'apiurl' => ["apiurl[$id]", "Api url", $s['apiurl'], L10n::t("Base API Path \x28remember the trailing /\x29")], + 'secret' => ["secret[$id]", "Secret", $s['consumersecret'], ""], + 'key' => ["key[$id]", "Key", $s['consumerkey'], ""], //'applicationname' => Array("applicationname[$id]", "Application name", $s['applicationname'], ""), 'delete' => Array("delete[$id]", "Delete", False , "Check to delete this preset"), ); @@ -694,17 +690,17 @@ function statusnet_plugin_admin(&$a, &$o){ } /* empty form to add new site */ $id++; - $sitesform[] = Array( - 'sitename' => Array("sitename[$id]", t("Site name"), "", ""), - 'apiurl' => Array("apiurl[$id]", "Api url", "", t("Base API Path \x28remember the trailing /\x29") ), - 'secret' => Array("secret[$id]", t("Consumer Secret"), "", ""), - 'key' => Array("key[$id]", t("Consumer Key"), "", ""), - //'applicationname' => Array("applicationname[$id]", t("Application name"), "", ""), - ); - - $t = get_markup_template( "admin.tpl", "addon/statusnet/" ); - $o = replace_macros($t, array( - '$submit' => t('Save Settings'), + $sitesform[] = [ + 'sitename' => ["sitename[$id]", L10n::t("Site name"), "", ""], + 'apiurl' => ["apiurl[$id]", "Api url", "", L10n::t("Base API Path \x28remember the trailing /\x29")], + 'secret' => ["secret[$id]", L10n::t("Consumer Secret"), "", ""], + 'key' => ["key[$id]", L10n::t("Consumer Key"), "", ""], + //'applicationname' => Array("applicationname[$id]", L10n::t("Application name"), "", ""), + ]; + + $t = get_markup_template("admin.tpl", "addon/statusnet/"); + $o = replace_macros($t, [ + '$submit' => L10n::t('Save Settings'), '$sites' => $sitesform, )); } @@ -718,9 +714,8 @@ function statusnet_prepare_body(&$a,&$b) { if (intval($max_char) == 0) $max_char = 140; - require_once("include/plaintext.php"); - $item = $b["item"]; - $item["plink"] = $a->get_baseurl()."/display/".$a->user["nickname"]."/".$item["parent"]; + $item = $b["item"]; + $item["plink"] = $a->get_baseurl() . "/display/" . $a->user["nickname"] . "/" . $item["parent"]; $r = q("SELECT `item`.`author-link`, `item`.`uri`, `contact`.`nick` AS contact_nick FROM `item` INNER JOIN `contact` ON `contact`.`id` = `item`.`contact-id` @@ -738,15 +733,17 @@ function statusnet_prepare_body(&$a,&$b) { $nickname = "@[url=".$orig_post["author-link"]."]".$nick."[/url]"; $nicknameplain = "@".$nick; - if ((strpos($item["body"], $nickname) === false) AND (strpos($item["body"], $nicknameplain) === false)) + if ((strpos($item["body"], $nickname) === false) && (strpos($item["body"], $nicknameplain) === false)) $item["body"] = $nickname." ".$item["body"]; } + $msgarr = BBCode::toPlaintext($item, $max_char, true, 7); + $msg = $msgarr["text"]; $msgarr = plaintext($a, $item, $max_char, true, 7); $msg = $msgarr["text"]; - if (isset($msgarr["url"]) AND ($msgarr["type"] != "photo")) + if (isset($msgarr["url"]) && ($msgarr["type"] != "photo")) $msg .= " ".$msgarr["url"]; if (isset($msgarr["image"])) @@ -784,7 +781,7 @@ function statusnet_cron($a,$b) { if ($abandon_days < 1) $abandon_days = 0; - $abandon_limit = date("Y-m-d H:i:s", time() - $abandon_days * 86400); + $abandon_limit = date(DateTimeFormat::MYSQL, time() - $abandon_days * 86400); $r = q("SELECT * FROM `pconfig` WHERE `cat` = 'statusnet' AND `k` = 'import' AND `v` ORDER BY RAND()"); if(count($r)) { @@ -929,10 +926,10 @@ function statusnet_fetch_contact($uid, $contact, $create_user) { $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `alias` = '%s' AND `network` = '%s'LIMIT 1", intval($uid), dbesc(normalise_link($contact->statusnet_profile_url)), dbesc(NETWORK_STATUSNET)); - if(!count($r) AND !$create_user) + if(!count($r) && !$create_user) return(0); - if (count($r) AND ($r[0]["readonly"] OR $r[0]["blocked"])) { + if (count($r) && ($r[0]["readonly"] || $r[0]["blocked"])) { logger("statusnet_fetch_contact: Contact '".$r[0]["nick"]."' is blocked or readonly.", LOGGER_DEBUG); return(-1); } @@ -944,7 +941,7 @@ function statusnet_fetch_contact($uid, $contact, $create_user) { `location`, `about`, `writable`, `blocked`, `readonly`, `pending` ) VALUES ( %d, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, %d, '%s', '%s', %d, 0, 0, 0 ) ", intval($uid), - dbesc(datetime_convert()), + dbesc(DateTimeFormat::utcNow()), dbesc($contact->statusnet_profile_url), dbesc(normalise_link($contact->statusnet_profile_url)), dbesc(statusnet_address($contact)), @@ -993,14 +990,13 @@ function statusnet_fetch_contact($uid, $contact, $create_user) { dbesc($photos[0]), dbesc($photos[1]), dbesc($photos[2]), - dbesc(datetime_convert()), + dbesc(DateTimeFormat::utcNow()), intval($contact_id) ); } else { // update profile photos once every two weeks as we have no notification of when they change. - - //$update_photo = (($r[0]['avatar-date'] < datetime_convert('','','now -2 days')) ? true : false); - $update_photo = ($r[0]['avatar-date'] < datetime_convert('','','now -12 hours')); + //$update_photo = (($r[0]['avatar-date'] < DateTimeFormat::convert('now -2 days', '', '', )) ? true : false); + $update_photo = ($r[0]['avatar-date'] < DateTimeFormat::utc('now -12 hours')); // check that we have all the photos, this has been known to fail on occasion @@ -1029,9 +1025,9 @@ function statusnet_fetch_contact($uid, $contact, $create_user) { dbesc($photos[0]), dbesc($photos[1]), dbesc($photos[2]), - dbesc(datetime_convert()), - dbesc(datetime_convert()), - dbesc(datetime_convert()), + dbesc(DateTimeFormat::utcNow()), + dbesc(DateTimeFormat::utcNow()), + dbesc(DateTimeFormat::utcNow()), dbesc($contact->statusnet_profile_url), dbesc(normalise_link($contact->statusnet_profile_url)), dbesc(statusnet_address($contact)), @@ -1054,9 +1050,9 @@ function statusnet_fetchuser($a, $uid, $screen_name = "", $user_id = "") { $otoken = get_pconfig($uid, 'statusnet', 'oauthtoken'); $osecret = get_pconfig($uid, 'statusnet', 'oauthsecret'); - require_once("addon/statusnet/codebird.php"); + require_once __DIR__ . DIRECTORY_SEPARATOR . 'library' . DIRECTORY_SEPARATOR . 'codebirdsn.php'; - $cb = \Codebird\Codebird::getInstance(); + $cb = \CodebirdSN\CodebirdSN::getInstance(); $cb->setConsumerKey($ckey, $csecret); $cb->setToken($otoken, $osecret); @@ -1179,7 +1175,7 @@ function statusnet_createpost($a, $uid, $post, $self, $create_user, $only_existi $postarray['owner-link'] = $post->user->statusnet_profile_url; $postarray['owner-avatar'] = $post->user->profile_image_url; } - if(($contactid == 0) AND !$only_existing_contact) + if(($contactid == 0) && !$only_existing_contact) $contactid = $self['id']; elseif ($contactid <= 0) return(array()); @@ -1209,8 +1205,8 @@ function statusnet_createpost($a, $uid, $post, $self, $create_user, $only_existi $postarray['body'] = $converted["body"]; $postarray['tag'] = $converted["tags"]; - $postarray['created'] = datetime_convert('UTC','UTC',$content->created_at); - $postarray['edited'] = datetime_convert('UTC','UTC',$content->created_at); + $postarray['created'] = DateTimeFormat::utc($content->created_at); + $postarray['edited'] = DateTimeFormat::utc($content->created_at); if (is_string($content->place->name)) $postarray["location"] = $content->place->name; @@ -1279,7 +1275,7 @@ function statusnet_checknotification($a, $uid, $own_url, $top_item, $postarray) foreach($myconv as $conv) { // now if we find a match, it means we're in this conversation - if(!link_compare($conv['author-link'],$user[0]["url"]) AND !link_compare($conv['author-link'],$own_user[0]["url"])) + if(!link_compare($conv['author-link'],$user[0]["url"]) && !link_compare($conv['author-link'],$own_user[0]["url"])) continue; require_once('include/enotify.php'); @@ -1289,14 +1285,14 @@ function statusnet_checknotification($a, $uid, $own_url, $top_item, $postarray) notification(array( 'type' => NOTIFY_COMMENT, 'notify_flags' => $user[0]['notify-flags'], - 'language' => $user[0]['language'], - 'to_name' => $user[0]['username'], - 'to_email' => $user[0]['email'], - 'uid' => $user[0]['uid'], - 'item' => $postarray, - 'link' => $a->get_baseurl().'/display/'.urlencode(get_item_guid($top_item)), - 'source_name' => $postarray['author-name'], - 'source_link' => $postarray['author-link'], + 'language' => $user[0]['language'], + 'to_name' => $user[0]['username'], + 'to_email' => $user[0]['email'], + 'uid' => $user[0]['uid'], + 'item' => $postarray, + 'link' => $a->get_baseurl() . '/display/' . urlencode(Item::getGuidById($top_item)), + 'source_name' => $postarray['author-name'], + 'source_link' => $postarray['author-link'], 'source_photo' => $postarray['author-avatar'], 'verb' => ACTIVITY_POST, 'otype' => 'item', @@ -1324,8 +1320,7 @@ function statusnet_fetchhometimeline($a, $uid, $mode = 1) { logger("statusnet_fetchhometimeline: Fetching for user ".$uid, LOGGER_DEBUG); - require_once('library/twitteroauth.php'); - require_once('include/items.php'); + require_once 'include/items.php'; $connection = new StatusNetOAuth($api, $ckey,$csecret,$otoken,$osecret); @@ -1375,11 +1370,11 @@ function statusnet_fetchhometimeline($a, $uid, $mode = 1) { $items = $connection->get('statuses/home_timeline', $parameters); if (!is_array($items)) { - if (is_object($items) AND isset($items->error)) + if (is_object($items) && isset($items->error)) $errormsg = $items->error; elseif (is_object($items)) $errormsg = print_r($items, true); - elseif (is_string($items) OR is_float($items) OR is_int($items)) + elseif (is_string($items) || is_float($items) || is_int($items)) $errormsg = $items; else $errormsg = "Unknown error"; @@ -1412,12 +1407,12 @@ function statusnet_fetchhometimeline($a, $uid, $mode = 1) { if (trim($postarray['body']) == "") continue; - $item = item_store($postarray); + $item = Item::insert($postarray); $postarray["id"] = $item; logger('statusnet_fetchhometimeline: User '.$self["nick"].' posted home timeline item '.$item); - if ($item AND !function_exists("check_item_notification")) + if ($item && !function_exists("check_item_notification")) statusnet_checknotification($a, $uid, $nick, $item, $postarray); } @@ -1463,12 +1458,12 @@ function statusnet_fetchhometimeline($a, $uid, $mode = 1) { if (trim($postarray['body']) != "") { continue; - $item = item_store($postarray); - $postarray["id"] = $item; + $item = Item::insert($postarray); + $postarray["id"] = $item; logger('statusnet_fetchhometimeline: User '.$self["nick"].' posted mention timeline item '.$item); - if ($item AND function_exists("check_item_notification")) + if ($item && function_exists("check_item_notification")) check_item_notification($item, $uid, NOTIFY_TAGSELF); } } @@ -1482,7 +1477,7 @@ function statusnet_fetchhometimeline($a, $uid, $mode = 1) { $parent_id = $r[0]['parent']; } - if (($item != 0) AND !function_exists("check_item_notification")) { + if (($item != 0) && !function_exists("check_item_notification")) { require_once('include/enotify.php'); notification(array( 'type' => NOTIFY_TAGSELF, @@ -1492,7 +1487,7 @@ function statusnet_fetchhometimeline($a, $uid, $mode = 1) { 'to_email' => $u[0]['email'], 'uid' => $u[0]['uid'], 'item' => $postarray, - 'link' => $a->get_baseurl().'/display/'.urlencode(get_item_guid($item)), + 'link' => $a->get_baseurl() . '/display/' . urlencode(Item::getGuidById($item)), 'source_name' => $postarray['author-name'], 'source_link' => $postarray['author-link'], 'source_photo' => $postarray['author-avatar'], @@ -1515,9 +1510,7 @@ function statusnet_complete_conversation($a, $uid, $self, $create_user, $nick, $ $osecret = get_pconfig($uid, 'statusnet', 'oauthsecret'); $own_url = get_pconfig($uid, 'statusnet', 'own_url'); - require_once('library/twitteroauth.php'); - - $connection = new StatusNetOAuth($api, $ckey,$csecret,$otoken,$osecret); + $connection = new StatusNetOAuth($api, $ckey, $csecret, $otoken, $osecret); $parameters["count"] = 200; @@ -1525,29 +1518,26 @@ function statusnet_complete_conversation($a, $uid, $self, $create_user, $nick, $ if (is_array($items)) { $posts = array_reverse($items); - foreach($posts AS $post) { + foreach ($posts as $post) { $postarray = statusnet_createpost($a, $uid, $post, $self, false, false); if (trim($postarray['body']) == "") continue; - //print_r($postarray); - $item = item_store($postarray); + $item = Item::insert($postarray); $postarray["id"] = $item; logger('statusnet_complete_conversation: User '.$self["nick"].' posted home timeline item '.$item); - if ($item AND !function_exists("check_item_notification")) + if ($item && !function_exists("check_item_notification")) statusnet_checknotification($a, $uid, $nick, $item, $postarray); } } } -function statusnet_convertmsg($a, $body, $no_tags = false) { - - require_once("include/oembed.php"); - require_once("include/items.php"); - require_once("include/network.php"); +function statusnet_convertmsg(App $a, $body, $no_tags = false) +{ + require_once "include/items.php"; $body = preg_replace("=\[url\=https?://([0-9]*).([0-9]*).([0-9]*).([0-9]*)/([0-9]*)\](.*?)\[\/url\]=ism","$1.$2.$3.$4/$5",$body); @@ -1565,7 +1555,7 @@ function statusnet_convertmsg($a, $body, $no_tags = false) { logger("statusnet_convertmsg: expanding url ".$match[1], LOGGER_DEBUG); - $expanded_url = original_url($match[1]); + $expanded_url = Network::finalUrl($match[1]); logger("statusnet_convertmsg: fetching data for ".$expanded_url, LOGGER_DEBUG); @@ -1582,12 +1572,12 @@ function statusnet_convertmsg($a, $body, $no_tags = false) { $footerlink = "[url=".$expanded_url."]".$expanded_url."[/url]"; $body = str_replace($search, $footerlink, $body); - } elseif (($oembed_data->type == "photo") AND isset($oembed_data->url) AND !$dontincludemedia) - $body = str_replace($search, "[url=".$expanded_url."][img]".$oembed_data->url."[/img][/url]", $body); - elseif ($oembed_data->type != "link") - $body = str_replace($search, "[url=".$expanded_url."]".$expanded_url."[/url]", $body); - else { - $img_str = fetch_url($expanded_url, true, $redirects, 4); + } elseif (($oembed_data->type == "photo") && isset($oembed_data->url) && !$dontincludemedia) { + $body = str_replace($search, "[url=" . $expanded_url . "][img]" . $oembed_data->url . "[/img][/url]", $body); + } elseif ($oembed_data->type != "link") { + $body = str_replace($search, "[url=" . $expanded_url . "]" . $expanded_url . "[/url]", $body); + } else { + $img_str = Network::fetchUrl($expanded_url, true, $redirects, 4); $tempfile = tempnam(get_temppath(), "cache"); file_put_contents($tempfile, $img_str); @@ -1610,10 +1600,10 @@ function statusnet_convertmsg($a, $body, $no_tags = false) { if ($footerurl != "") $footer = add_page_info($footerurl); - if (($footerlink != "") AND (trim($footer) != "")) { + if (($footerlink != "") && (trim($footer) != "")) { $removedlink = trim(str_replace($footerlink, "", $body)); - if (($removedlink == "") OR strstr($body, $removedlink)) + if (($removedlink == "") || strstr($body, $removedlink)) $body = $removedlink; $body .= $footer; @@ -1661,9 +1651,7 @@ function statusnet_fetch_own_contact($a, $uid) { $contact_id = 0; if ($own_url == "") { - require_once('library/twitteroauth.php'); - - $connection = new StatusNetOAuth($api, $ckey,$csecret,$otoken,$osecret); + $connection = new StatusNetOAuth($api, $ckey, $csecret, $otoken, $osecret); // Fetching user data $user = $connection->get('account/verify_credentials');