diff --git a/ifttt/ifttt.php b/ifttt/ifttt.php index 6d4f402c..faaea2ca 100644 --- a/ifttt/ifttt.php +++ b/ifttt/ifttt.php @@ -180,5 +180,5 @@ function ifttt_message($uid, $item) $link = hash('ripemd128', $item['msg']); } - Post\Delayed::add($link, $post, Worker::PRIORITY_MEDIUM, Post\Delayed::PREPARED); + Post\Delayed::add($link, $post, Worker::PRIORITY_MEDIUM, Post\Delayed::UNPREPARED); } diff --git a/mailstream/mailstream.php b/mailstream/mailstream.php index 71b123ad..ad28c18e 100644 --- a/mailstream/mailstream.php +++ b/mailstream/mailstream.php @@ -416,7 +416,7 @@ function mailstream_send(string $message_id, array $item, array $user): bool '$upstream' => DI::l10n()->t('Upstream'), '$local' => DI::l10n()->t('Local'), '$item' => $item]); - mailstream_html_wrap($mail->Body); + $mail->Body = mailstream_html_wrap($mail->Body); if (!$mail->Send()) { throw new Exception($mail->ErrorInfo); } @@ -437,7 +437,8 @@ function mailstream_send(string $message_id, array $item, array $user): bool * bbcode's output suitable for transmission, we try to break things * up so that lines are about 200 characters. * - * @param string $text text to word wrap - modified in-place + * @param string $text text to word wrap + * @return string wrapped text */ function mailstream_html_wrap(string &$text) { @@ -446,6 +447,7 @@ function mailstream_html_wrap(string &$text) $lines[$i] = preg_replace('/ /', "\n", $lines[$i], 1); } $text = implode($lines); + return $text; } /** diff --git a/retriever/retriever.php b/retriever/retriever.php index 977ed49a..9370271c 100644 --- a/retriever/retriever.php +++ b/retriever/retriever.php @@ -6,10 +6,12 @@ * Author: Matthew Exon */ +use Friendica\App; use Friendica\Core\Addon; use Friendica\Core\Hook; use Friendica\Core\Logger; use Friendica\Core\Renderer; +use Friendica\Core\Session; use Friendica\Core\System; use Friendica\Content\Text\HTML; use Friendica\Content\Text\BBCode; @@ -27,6 +29,8 @@ use Friendica\DI; * @brief Installation hook for retriever plugin */ function retriever_install() { + Logger::debug('Install retriever'); + Hook::register('addon_settings', 'addon/retriever/retriever.php', 'retriever_addon_settings'); Hook::register('addon_settings_post', 'addon/retriever/retriever.php', 'retriever_addon_settings_post'); Hook::register('post_remote', 'addon/retriever/retriever.php', 'retriever_post_remote_hook'); @@ -35,11 +39,11 @@ function retriever_install() { if (DI::config()->get('retriever', 'dbversion') == '0.14') { if (!DBA::e("ALTER TABLE `retriever_rule` CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci") || - !DBA::e("ALTER TABLE `retriever_item` CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci") || !DBA::e("ALTER TABLE `retriever_item` MODIFY `item-uri` varbinary(255) NOT NULL") || - !DBA::e("ALTER TABLE `retriever_resource` CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci") || + !DBA::e("ALTER TABLE `retriever_item` CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci") || !DBA::e("ALTER TABLE `retriever_resource` MODIFY `url` varbinary(700) NOT NULL") || !DBA::e("ALTER TABLE `retriever_resource` MODIFY `redirect-url` varbinary(700) NOT NULL")) { + !DBA::e("ALTER TABLE `retriever_resource` CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci") || Logger::warning('Unable to update database tables: ' . DBA::errorMessage()); return; } @@ -63,6 +67,8 @@ function retriever_install() { * @brief Uninstallation hook for retriever plugin */ function retriever_uninstall() { + Logger::debug('Uninstall retriever'); + Hook::unregister('addon_settings', 'addon/retriever/retriever.php', 'retriever_addon_settings'); Hook::unregister('addon_settings_post', 'addon/retriever/retriever.php', 'retriever_addon_settings_post'); Hook::unregister('post_remote', 'addon/retriever/retriever.php', 'retriever_post_remote_hook'); @@ -73,19 +79,19 @@ function retriever_uninstall() { } /** - * @brief Module hook for retriever plugin - * - * TODO: figure out what this should be used for + * This is a statement rather than an actual function definition. The simple + * existence of this method is checked to figure out if the addon offers a + * module. */ function retriever_module() {} /** * @brief Admin page hook for retriever plugin * - * @param App $a App object (by ref) + * @param App $a App object (unused) * @param string $o HTML to append content to (by ref) */ -function retriever_addon_admin(&$a, &$o) { +function retriever_addon_admin(App $a, string &$o) { $template = Renderer::getMarkupTemplate('admin.tpl', 'addon/retriever/'); $downloads_per_cron = DI::config()->get('retriever', 'downloads_per_cron'); @@ -137,7 +143,7 @@ $retriever_item_count = 0; * * @param int $max_items Maximum number of items to retrieve in this call */ -function retriever_retrieve_items($max_items) { +function retriever_retrieve_items(int $max_items) { global $retriever_item_count; $retriever_schedule = array(array(1,'minute'), @@ -183,7 +189,7 @@ function retriever_retrieve_items($max_items) { * * @param int $max_items Maximum number of items to retrieve in this call */ -function retriever_clean_up_completed_resources($max_items) { +function retriever_clean_up_completed_resources(int $max_items) { // TODO: figure out how to do this with DBA module $r = DBA::p("SELECT retriever_resource.`id` as resource, retriever_item.`id` as item FROM retriever_resource, retriever_item, retriever_rule WHERE retriever_item.`finished` = 0 AND retriever_item.`resource` = retriever_resource.`id` AND retriever_resource.`completed` IS NOT NULL AND retriever_item.`contact-id` = retriever_rule.`contact-id` AND retriever_item.`item-uid` = retriever_rule.`uid` LIMIT $max_items"); if (!DBA::isResult($r)) { @@ -236,7 +242,7 @@ function retriever_tidy() { * * @param array $resource The row from the retriever_resource table */ -function retrieve_dataurl_resource($resource) { +function retrieve_dataurl_resource(array $resource) { if (!preg_match("/date:(.*);base64,(.*)/", $resource['url'], $matches)) { Logger::warning('retrieve_dataurl_resource: resource ' . $resource['id'] . ' does not match pattern'); } else { @@ -254,7 +260,7 @@ function retrieve_dataurl_resource($resource) { * * @param array $resource The row from the retriever_resource table */ -function retrieve_resource($resource) { +function retrieve_resource(array $resource) { $components = parse_url($resource['url']); if (!$components) { Logger::warning('retrieve_resource: URL ' . $resource['url'] . ' could not be parsed'); @@ -321,7 +327,7 @@ function retrieve_resource($resource) { * @param boolean $create Whether to create a new configuration if none exists already * @return array The row from the retriever_rule database for this configuration */ -function get_retriever_rule($contact_id, $uid, $create) { +function get_retriever_rule(string $contact_id, string $uid, bool $create) { $retriever_rule = DBA::selectFirst('retriever_rule', [], ['contact-id' => intval($contact_id), 'uid' => intval($uid)]); if ($retriever_rule) { $retriever_rule['data'] = json_decode($retriever_rule['data'], true); @@ -340,7 +346,7 @@ function get_retriever_rule($contact_id, $uid, $create) { * @param array $retriever_item Row from the retriever_item table * @return array Item that was found, or undef if no item could be found */ -function retriever_get_item($retriever_item) { +function retriever_get_item(array $retriever_item) { $item = Post::selectFirst([], ['uri' => $retriever_item['item-uri'], 'uid' => intval($retriever_item['item-uid']), 'contact-id' => intval($retriever_item['contact-id'])]); if (!DBA::isResult($item)) { Logger::warning('retriever_get_item: no item found for uri ' . $retriever_item['item-uri']); @@ -355,7 +361,7 @@ function retriever_get_item($retriever_item) { * @param int $retriever_item_id ID of the retriever item corresponding to this resource * @param array $resource The full details of the completed resource */ -function retriever_item_completed($retriever_item_id, $resource) { +function retriever_item_completed(string $retriever_item_id, array $resource) { Logger::debug('retriever_item_completed: id ' . $retriever_item_id . ' url ' . $resource['url']); $retriever_item = DBA::selectFirst('retriever_item', [], ['id' => intval($retriever_item_id)]); @@ -382,7 +388,7 @@ function retriever_item_completed($retriever_item_id, $resource) { * * @param array $resource The full details of the completed resource */ -function retriever_resource_completed($resource) { +function retriever_resource_completed(array $resource) { Logger::debug('retriever_resource_completed: id ' . $resource['id'] . ' url ' . $resource['url']); foreach (DBA::selectToArray('retriever_item', ['id'], ['resource' => intval($resource['id'])]) as $retriever_item) { retriever_item_completed($retriever_item['id'], $resource); @@ -395,7 +401,7 @@ function retriever_resource_completed($resource) { * @param array $retriever The row from the retriever_rule table for the contact * @param int $num The number of existing items to queue for retrieval */ -function apply_retrospective($retriever, $num) { +function apply_retrospective(array $retriever, int $num) { foreach (Post::selectToArray([], ['contact-id' => intval($retriever['contact-id'])], ['order' => ['received' => true], 'limit' => $num]) as $item) { Item::update(['visible' => 0], ['id' => intval($item['id'])]); foreach (DBA::selectToArray('retriever_item', [], ['item-uri' => $item['uri'], 'item-uid' => $item['uid'], 'contact-id' => $item['contact-id']]) as $retriever_item) { @@ -414,7 +420,7 @@ function apply_retrospective($retriever, $num) { * * TODO: This queries then inserts. It should use some kind of lock to avoid requesting the same resource twice. */ -function retriever_on_item_insert($retriever, &$item) { +function retriever_on_item_insert(array $retriever, array &$item) { if (!$retriever || !$retriever['id']) { Logger::info('retriever_on_item_insert: No retriever supplied'); return; @@ -453,7 +459,7 @@ function retriever_on_item_insert($retriever, &$item) { * @param boolean $binary Specifies if this download should be done in binary mode * @return array The created resource */ -function add_retriever_resource($url, $uid, $cid, $binary = false) { +function add_retriever_resource(string $url, string $uid, string $cid, bool $binary = false) { Logger::debug('add_retriever_resource: url ' . $url . ' uid ' . $uid . ' contact-id ' . $cid); $scheme = parse_url($url, PHP_URL_SCHEME); @@ -501,7 +507,7 @@ function add_retriever_resource($url, $uid, $cid, $binary = false) { * @param array $resource Resource that the item needs to wait for. This must have already been stored in the database. * @return int ID of the retriever item that was created, or the existing one if present */ -function add_retriever_item($item, $resource) { +function add_retriever_item(array $item, array $resource) { Logger::debug('add_retriever_item: ' . $resource['url'] . ' for ' . $item['uri'] . ' ' . $item['uid'] . ' ' . $item['contact-id']); if (!array_key_exists('id', $resource) || !$resource['id']) { @@ -528,7 +534,7 @@ function add_retriever_item($item, $resource) { * @param array $resource The completed resource * @return string Character encoding, e.g. "utf-8" or "iso-8859-1" */ -function retriever_get_encoding($resource) { +function retriever_get_encoding(array $resource) { $matches = array(); if (preg_match('/charset=(.*)/', $resource['type'], $matches)) { return trim(array_pop($matches)); @@ -543,7 +549,7 @@ function retriever_get_encoding($resource) { * @param DOMDocument $doc Input to the XSLT template * @return DOMDocument Result of applying the template */ -function retriever_apply_xslt_text($xslt_text, $doc) { +function retriever_apply_xslt_text(string $xslt_text, DOMDocument $doc) { if (!$xslt_text) { Logger::info('retriever_apply_xslt_text: empty XSLT text'); return $doc; @@ -566,7 +572,7 @@ function retriever_apply_xslt_text($xslt_text, $doc) { * @param array &$item Item to be in which to store the new body (by ref). This may or may not be already stored in the database. * @param array $resource Newly completed resource, which should be text (HTML or XML) */ -function retriever_apply_dom_filter($retriever, &$item, $resource) { +function retriever_apply_dom_filter(array $retriever, array &$item, array $resource) { Logger::debug('retriever_apply_dom_filter: applying XSLT to uri ' . $item['uri'] . ' uid ' . $item['uid'] . ' contact ' . $item['contact-id']); if (!array_key_exists('include', $retriever['data']) && !array_key_exists('customxslt', $retriever['data'])) { @@ -610,7 +616,7 @@ function retriever_apply_dom_filter($retriever, &$item, $resource) { * * @param array $resource The resource containing the text content */ -function retriever_load_into_dom($resource) { +function retriever_load_into_dom(array $resource) { $encoding = retriever_get_encoding($resource); $content = mb_convert_encoding($resource['data'], 'HTML-ENTITIES', $encoding); $doc = new DOMDocument('1.0', 'UTF-8'); @@ -630,7 +636,7 @@ function retriever_load_into_dom($resource) { * @param array $retriever The retriever configuration for this contact * @return DOMDocument New DOM document containing only the desired content */ -function retriever_extract($doc, $retriever) { +function retriever_extract(DOMDocument $doc, array $retriever) { $params = array('$spec' => $retriever['data']); $extract_template = Renderer::getMarkupTemplate('extract.tpl', 'addon/retriever/'); $extract_xslt = Renderer::replaceMacros($extract_template, $params); @@ -652,7 +658,7 @@ function retriever_extract($doc, $retriever) { * @param array $resource Completed resource which contains the text in the DOM document * @return DOMDocument New DOM document with global URLs */ -function retriever_globalise_urls($doc, $resource) { +function retriever_globalise_urls(DOMDocument $doc, array $resource) { $components = parse_url($resource['redirect-url']); if (!array_key_exists('scheme', $components) || !array_key_exists('host', $components) || !array_key_exists('path', $components)) { return $doc; @@ -671,7 +677,7 @@ function retriever_globalise_urls($doc, $resource) { * * @param array $item Row from the item table */ -function retriever_get_body($item) { +function retriever_get_body(array $item) { if (!array_key_exists('uri-id', $item) || !$item['uri-id']) { // item has not yet been stored in database return $item['body']; @@ -699,7 +705,7 @@ function retriever_get_body($item) { * @param array &$item Item in which to set the body (by ref). This may or may not be already stored in the database. * @param string $body New body content */ -function retriever_set_body(&$item, $body) { +function retriever_set_body(array &$item, string $body) { $item['body'] = $body; if (!array_key_exists('id', $item) || !$item['id']) { // item has not yet been stored in database @@ -713,7 +719,7 @@ function retriever_set_body(&$item, $body) { * * @param array &$item Item to be searched for images and updated (by ref). This may or may not be already stored in the database. */ -function retrieve_images(&$item) { +function retrieve_images(array &$item) { if (!DI::config()->get('retriever', 'allow_images')) { return; } @@ -751,7 +757,7 @@ function retrieve_images(&$item) { * * @param array &$item Row from the item table (by ref) */ -function retriever_check_item_completed(&$item) +function retriever_check_item_completed(array &$item) { $waiting = DBA::selectFirst('retriever_item', [], ['item-uri' => $item['uri'], 'item-uid' => intval($item['uid']), 'contact-id' => intval($item['contact-id']), 'finished' => 0]); Logger::debug('retriever_check_item_completed: item ' . $item['uri'] . ' ' . $item['uid'] . ' '. $item['contact-id'] . ' waiting for resources'); @@ -770,7 +776,7 @@ function retriever_check_item_completed(&$item) * @param array &$item Row from the item table (by ref) * @param array $resource The resource that has just been completed */ -function retriever_apply_completed_resource_to_item($retriever, &$item, $resource) { +function retriever_apply_completed_resource_to_item(array $retriever, array &$item, array $resource) { Logger::debug('retriever_apply_completed_resource_to_item: retriever ' . ($retriever ? $retriever['id'] : 'none') . ' resource ' . $resource['url'] . ' plink ' . $item['plink']); if (strpos($resource['type'], 'image') !== false) { retriever_transform_images($item, $resource); @@ -796,7 +802,7 @@ function retriever_apply_completed_resource_to_item($retriever, &$item, $resourc * * TODO: split this into two functions, one to store the image, the other to change the item body */ -function retriever_transform_images(&$item, $resource) { +function retriever_transform_images(array &$item, array $resource) { if (!$resource['data']) { Logger::info('retriever_transform_images: no data available for ' . $resource['id'] . ' ' . $resource['url']); return; @@ -838,13 +844,13 @@ function retriever_transform_images(&$item, $resource) { * * @param App $a The App object */ -function retriever_content($a) { - if (!local_user()) { +function retriever_content(App $a) { + if (!Session::getLocalUser()) { $a->page['content'] .= "

Please log in

"; return; } if (isset(DI::args()->getArgv()[1]) and DI::args()->getArgv()[1] === 'help') { - $feeds = DBA::selectToArray('contact', ['id', 'name', 'thumb'], ['uid' => local_user(), 'network' => 'feed']); + $feeds = DBA::selectToArray('contact', ['id', 'name', 'thumb'], ['uid' => Session::getLocalUser(), 'network' => 'feed']); for ($i = 0; $i < count($feeds); ++$i) { $feeds[$i]['url'] = DI::baseUrl()->get(true) . '/retriever/' . $feeds[$i]['id']; } @@ -857,13 +863,13 @@ function retriever_content($a) { } if (isset(DI::args()->getArgv()[1])) { $arg1 = DI::args()->getArgv()[1]; - $retriever_rule = get_retriever_rule($arg1, local_user(), false); + $retriever_rule = get_retriever_rule($arg1, Session::getLocalUser(), false); if (!$retriever_rule) { $retriever_rule = ['id' => 0, 'data' => ['enable' => 0, 'modurl' => '', 'pattern' => '', 'replace' => '', 'images' => 0, 'storecookies' => 0, 'cookiedata' => '', 'customxslt' => '', 'include' => '', 'exclude' => '']]; } if (!empty($_POST["id"])) { - $retriever_rule = get_retriever_rule($arg1, local_user(), true); + $retriever_rule = get_retriever_rule($arg1, Session::getLocalUser(), true); $retriever_rule['data'] = array(); foreach (array('modurl', 'pattern', 'replace', 'enable', 'images', 'customxslt', 'storecookies', 'cookiedata') as $setting) { if (empty($_POST['retriever_' . $setting])) { @@ -966,10 +972,10 @@ function retriever_content($a) { /** * @brief Hook that adds the retriever option to the contact menu * - * @param App $a The App object + * @param App $a The App object (by ref) * @param array $args Contact menu details to be filled in (by ref) */ -function retriever_contact_photo_menu($a, &$args) { +function retriever_contact_photo_menu(App &$a, array &$args) { if (!$args) { return; } @@ -984,7 +990,7 @@ function retriever_contact_photo_menu($a, &$args) { * @param App $a The App object (by ref) * @param array $item New item, which has not yet been inserted into database (by ref) */ -function retriever_post_remote_hook(&$a, &$item) { +function retriever_post_remote_hook(App &$a, array &$item) { Logger::info('retriever_post_remote_hook: ' . $item['uri'] . ' ' . $item['uid'] . ' ' . $item['contact-id']); $retriever_rule = get_retriever_rule($item['contact-id'], $item["uid"], false); @@ -1011,9 +1017,9 @@ function retriever_post_remote_hook(&$a, &$item) { * @param App $a The App object (by ref) * @param string $s HTML string to which to append settings content (by ref) */ -function retriever_addon_settings(&$a, &$s) { - $all_photos = DI::config()->get(local_user(), 'retriever', 'all_photos'); - $oembed = DI::config()->get(local_user(), 'retriever', 'oembed'); +function retriever_addon_settings(App &$a, string &$s) { + $all_photos = DI::config()->get(Session::getLocalUser(), 'retriever', 'all_photos'); + $oembed = DI::config()->get(Session::getLocalUser(), 'retriever', 'oembed'); $template = Renderer::getMarkupTemplate('/settings.tpl', 'addon/retriever/'); $config = array('$submit' => DI::l10n()->t('Save Settings'), '$title' => DI::l10n()->t('Retriever Settings'), @@ -1036,17 +1042,17 @@ function retriever_addon_settings(&$a, &$s) { * @param App $a The App object * @param array $post Posted content */ -function retriever_addon_settings_post($a, $post) { +function retriever_addon_settings_post(App $a, array $post) { if ($post['retriever_all_photos']) { - DI::config()->set(local_user(), 'retriever', 'all_photos', $post['retriever_all_photos']); + DI::config()->set(Session::getLocalUser(), 'retriever', 'all_photos', $post['retriever_all_photos']); } else { - DI::config()->delete(local_user(), 'retriever', 'all_photos'); + DI::config()->delete(Session::getLocalUser(), 'retriever', 'all_photos'); } if ($post['retriever_oembed']) { - DI::config()->set(local_user(), 'retriever', 'oembed', $post['retriever_oembed']); + DI::config()->set(Session::getLocalUser(), 'retriever', 'oembed', $post['retriever_oembed']); } else { - DI::config()->delete(local_user(), 'retriever', 'oembed'); + DI::config()->delete(Session::getLocalUser(), 'retriever', 'oembed'); } } diff --git a/twitter/twitter.php b/twitter/twitter.php index c55489a0..59861a35 100644 --- a/twitter/twitter.php +++ b/twitter/twitter.php @@ -1363,7 +1363,7 @@ function twitter_fetchtimeline(int $uid): void Logger::info('Posting mirror post', ['twitter-id' => $post->id_str, 'uid' => $uid]); - Post\Delayed::add($mirrorpost['extid'], $mirrorpost, Worker::PRIORITY_MEDIUM, Post\Delayed::PREPARED); + Post\Delayed::add($mirrorpost['extid'], $mirrorpost, Worker::PRIORITY_MEDIUM, Post\Delayed::UNPREPARED); } } DI::pConfig()->set($uid, 'twitter', 'lastid', $lastid);