]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/Event.php
Update more PHPDoc, including in include/
[friendica.git] / src / Model / Event.php
index e6ed20f72cb4e77cff497050078ef16ad242207b..8b7c7e706c15deebd3522d9df8a43ef4478d72f2 100644 (file)
@@ -7,19 +7,17 @@ namespace Friendica\Model;
 
 use Friendica\BaseObject;
 use Friendica\Content\Text\BBCode;
-use Friendica\Core\Addon;
+use Friendica\Core\Hook;
 use Friendica\Core\L10n;
 use Friendica\Core\Logger;
 use Friendica\Core\PConfig;
+use Friendica\Core\Renderer;
 use Friendica\Core\System;
 use Friendica\Database\DBA;
-use Friendica\Model\Contact;
 use Friendica\Util\DateTimeFormat;
 use Friendica\Util\Map;
-
-require_once 'boot.php';
-require_once 'include/dba.php';
-require_once 'include/items.php';
+use Friendica\Util\Strings;
+use Friendica\Util\XML;
 
 /**
  * @brief functions for interacting with the event database table
@@ -35,13 +33,13 @@ class Event extends BaseObject
 
                $bd_format = L10n::t('l F d, Y \@ g:i A'); // Friday January 18, 2011 @ 8 AM.
 
-               $event_start = day_translate(
+               $event_start = L10n::getDay(
                        !empty($event['adjust']) ?
                        DateTimeFormat::local($event['start'], $bd_format) : DateTimeFormat::utc($event['start'], $bd_format)
                );
 
                if (!empty($event['finish'])) {
-                       $event_end = day_translate(
+                       $event_end = L10n::getDay(
                                !empty($event['adjust']) ?
                                DateTimeFormat::local($event['finish'], $bd_format) : DateTimeFormat::utc($event['finish'], $bd_format)
                        );
@@ -51,11 +49,11 @@ class Event extends BaseObject
 
                if ($simple) {
                        if (!empty($event['summary'])) {
-                               $o = "<h3>" . BBCode::convert($event['summary'], false, $simple) . "</h3>";
+                               $o = "<h3>" . BBCode::convert(Strings::escapeHtml($event['summary']), false, $simple) . "</h3>";
                        }
 
                        if (!empty($event['desc'])) {
-                               $o .= "<div>" . BBCode::convert($event['desc'], false, $simple) . "</div>";
+                               $o .= "<div>" . BBCode::convert(Strings::escapeHtml($event['desc']), false, $simple) . "</div>";
                        }
 
                        $o .= "<h4>" . L10n::t('Starts:') . "</h4><p>" . $event_start . "</p>";
@@ -65,7 +63,7 @@ class Event extends BaseObject
                        }
 
                        if (!empty($event['location'])) {
-                               $o .= "<h4>" . L10n::t('Location:') . "</h4><p>" . BBCode::convert($event['location'], false, $simple) . "</p>";
+                               $o .= "<h4>" . L10n::t('Location:') . "</h4><p>" . BBCode::convert(Strings::escapeHtml($event['location']), false, $simple) . "</p>";
                        }
 
                        return $o;
@@ -73,7 +71,7 @@ class Event extends BaseObject
 
                $o = '<div class="vevent">' . "\r\n";
 
-               $o .= '<div class="summary event-summary">' . BBCode::convert($event['summary'], false, $simple) . '</div>' . "\r\n";
+               $o .= '<div class="summary event-summary">' . BBCode::convert(Strings::escapeHtml($event['summary']), false, $simple) . '</div>' . "\r\n";
 
                $o .= '<div class="event-start"><span class="event-label">' . L10n::t('Starts:') . '</span>&nbsp;<span class="dtstart" title="'
                        . DateTimeFormat::utc($event['start'], (!empty($event['adjust']) ? DateTimeFormat::ATOM : 'Y-m-d\TH:i:s'))
@@ -88,12 +86,12 @@ class Event extends BaseObject
                }
 
                if (!empty($event['desc'])) {
-                       $o .= '<div class="description event-description">' . BBCode::convert($event['desc'], false, $simple) . '</div>' . "\r\n";
+                       $o .= '<div class="description event-description">' . BBCode::convert(Strings::escapeHtml($event['desc']), false, $simple) . '</div>' . "\r\n";
                }
 
                if (!empty($event['location'])) {
                        $o .= '<div class="event-location"><span class="event-label">' . L10n::t('Location:') . '</span>&nbsp;<span class="location">'
-                               . BBCode::convert($event['location'], false, $simple)
+                               . BBCode::convert(Strings::escapeHtml($event['location']), false, $simple)
                                . '</span></div>' . "\r\n";
 
                        // Include a map of the location if the [map] BBCode is used.
@@ -150,6 +148,7 @@ class Event extends BaseObject
         * @brief Extract bbcode formatted event data from a string.
         *
         * @params: string $s The string which should be parsed for event data.
+        * @param $text
         * @return array The array with the event information.
         */
        public static function fromBBCode($text)
@@ -217,6 +216,7 @@ class Event extends BaseObject
         *
         * @param int $event_id Event ID.
         * @return void
+        * @throws \Exception
         */
        public static function delete($event_id)
        {
@@ -235,6 +235,7 @@ class Event extends BaseObject
         *
         * @param array $arr Array with event data.
         * @return int The new event id.
+        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         */
        public static function store($arr)
        {
@@ -301,8 +302,8 @@ class Event extends BaseObject
 
                        $item = Item::selectFirst(['id'], ['event-id' => $event['id'], 'uid' => $event['uid']]);
                        if (DBA::isResult($item)) {
-                               $object = '<object><type>' . xmlify(ACTIVITY_OBJ_EVENT) . '</type><title></title><id>' . xmlify($event['uri']) . '</id>';
-                               $object .= '<content>' . xmlify(self::getBBCode($event)) . '</content>';
+                               $object = '<object><type>' . XML::escape(ACTIVITY_OBJ_EVENT) . '</type><title></title><id>' . XML::escape($event['uri']) . '</id>';
+                               $object .= '<content>' . XML::escape(self::getBBCode($event)) . '</content>';
                                $object .= '</object>' . "\n";
 
                                $fields = ['body' => self::getBBCode($event), 'object' => $object, 'edited' => $event['edited']];
@@ -313,52 +314,57 @@ class Event extends BaseObject
                                $item_id = 0;
                        }
 
-                       Addon::callHooks('event_updated', $event['id']);
+                       Hook::callAll('event_updated', $event['id']);
                } else {
                        $event['guid']  = defaults($arr, 'guid', System::createUUID());
 
                        // New event. Store it.
                        DBA::insert('event', $event);
 
-                       $event['id'] = DBA::lastInsertId();
-
-                       $item_arr = [];
-
-                       $item_arr['uid']           = $event['uid'];
-                       $item_arr['contact-id']    = $event['cid'];
-                       $item_arr['uri']           = $event['uri'];
-                       $item_arr['parent-uri']    = $event['uri'];
-                       $item_arr['guid']          = $event['guid'];
-                       $item_arr['plink']         = defaults($arr, 'plink', '');
-                       $item_arr['post-type']     = Item::PT_EVENT;
-                       $item_arr['wall']          = $event['cid'] ? 0 : 1;
-                       $item_arr['contact-id']    = $contact['id'];
-                       $item_arr['owner-name']    = $contact['name'];
-                       $item_arr['owner-link']    = $contact['url'];
-                       $item_arr['owner-avatar']  = $contact['thumb'];
-                       $item_arr['author-name']   = $contact['name'];
-                       $item_arr['author-link']   = $contact['url'];
-                       $item_arr['author-avatar'] = $contact['thumb'];
-                       $item_arr['title']         = '';
-                       $item_arr['allow_cid']     = $event['allow_cid'];
-                       $item_arr['allow_gid']     = $event['allow_gid'];
-                       $item_arr['deny_cid']      = $event['deny_cid'];
-                       $item_arr['deny_gid']      = $event['deny_gid'];
-                       $item_arr['private']       = $private;
-                       $item_arr['visible']       = 1;
-                       $item_arr['verb']          = ACTIVITY_POST;
-                       $item_arr['object-type']   = ACTIVITY_OBJ_EVENT;
-                       $item_arr['origin']        = $event['cid'] === 0 ? 1 : 0;
-                       $item_arr['body']          = self::getBBCode($event);
-                       $item_arr['event-id']      = $event['id'];
-
-                       $item_arr['object']  = '<object><type>' . xmlify(ACTIVITY_OBJ_EVENT) . '</type><title></title><id>' . xmlify($event['uri']) . '</id>';
-                       $item_arr['object'] .= '<content>' . xmlify(self::getBBCode($event)) . '</content>';
-                       $item_arr['object'] .= '</object>' . "\n";
-
-                       $item_id = Item::insert($item_arr);
-
-                       Addon::callHooks("event_created", $event['id']);
+                       $item_id = 0;
+
+                       // Don't create an item for birthday events
+                       if ($event['type'] == 'event') {
+                               $event['id'] = DBA::lastInsertId();
+
+                               $item_arr = [];
+
+                               $item_arr['uid']           = $event['uid'];
+                               $item_arr['contact-id']    = $event['cid'];
+                               $item_arr['uri']           = $event['uri'];
+                               $item_arr['parent-uri']    = $event['uri'];
+                               $item_arr['guid']          = $event['guid'];
+                               $item_arr['plink']         = defaults($arr, 'plink', '');
+                               $item_arr['post-type']     = Item::PT_EVENT;
+                               $item_arr['wall']          = $event['cid'] ? 0 : 1;
+                               $item_arr['contact-id']    = $contact['id'];
+                               $item_arr['owner-name']    = $contact['name'];
+                               $item_arr['owner-link']    = $contact['url'];
+                               $item_arr['owner-avatar']  = $contact['thumb'];
+                               $item_arr['author-name']   = $contact['name'];
+                               $item_arr['author-link']   = $contact['url'];
+                               $item_arr['author-avatar'] = $contact['thumb'];
+                               $item_arr['title']         = '';
+                               $item_arr['allow_cid']     = $event['allow_cid'];
+                               $item_arr['allow_gid']     = $event['allow_gid'];
+                               $item_arr['deny_cid']      = $event['deny_cid'];
+                               $item_arr['deny_gid']      = $event['deny_gid'];
+                               $item_arr['private']       = $private;
+                               $item_arr['visible']       = 1;
+                               $item_arr['verb']          = ACTIVITY_POST;
+                               $item_arr['object-type']   = ACTIVITY_OBJ_EVENT;
+                               $item_arr['origin']        = $event['cid'] === 0 ? 1 : 0;
+                               $item_arr['body']          = self::getBBCode($event);
+                               $item_arr['event-id']      = $event['id'];
+
+                               $item_arr['object']  = '<object><type>' . XML::escape(ACTIVITY_OBJ_EVENT) . '</type><title></title><id>' . XML::escape($event['uri']) . '</id>';
+                               $item_arr['object'] .= '<content>' . XML::escape(self::getBBCode($event)) . '</content>';
+                               $item_arr['object'] .= '</object>' . "\n";
+
+                               $item_id = Item::insert($item_arr);
+                       }
+
+                       Hook::callAll("event_created", $event['id']);
                }
 
                return $item_id;
@@ -368,6 +374,7 @@ class Event extends BaseObject
         * @brief Create an array with translation strings used for events.
         *
         * @return array Array with translations strings.
+        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         */
        public static function getStrings()
        {
@@ -464,6 +471,7 @@ class Event extends BaseObject
         * @param int    $event_id  The ID of the event in the event table
         * @param string $sql_extra
         * @return array Query result
+        * @throws \Exception
         */
        public static function getListById($owner_uid, $event_id, $sql_extra = '')
        {
@@ -492,17 +500,18 @@ class Event extends BaseObject
        /**
         * @brief Get all events in a specific time frame.
         *
-        * @param int $owner_uid The User ID of the owner of the events.
-        * @param array $event_params An associative array with
-        *      int 'ignore' =>
-        *      string 'start' => Start time of the timeframe.
-        *      string 'finish' => Finish time of the timeframe.
-        *      string 'adjust_start' =>
-        *      string 'adjust_finish' =>
+        * @param int    $owner_uid    The User ID of the owner of the events.
+        * @param array  $event_params An associative array with
+        *                             int 'ignore' =>
+        *                             string 'start' => Start time of the timeframe.
+        *                             string 'finish' => Finish time of the timeframe.
+        *                             string 'adjust_start' =>
+        *                             string 'adjust_finish' =>
         *
-        * @param string $sql_extra Additional sql conditions (e.g. permission request).
+        * @param string $sql_extra    Additional sql conditions (e.g. permission request).
         *
         * @return array Query results.
+        * @throws \Exception
         */
        public static function getListByDate($owner_uid, $event_params, $sql_extra = '')
        {
@@ -543,6 +552,8 @@ class Event extends BaseObject
         *
         * @param array $event_result Event query array.
         * @return array Event array for the template.
+        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+        * @throws \ImagickException
         */
        public static function prepareListForTemplate(array $event_result)
        {
@@ -562,7 +573,7 @@ class Event extends BaseObject
                        $start = $event['adjust'] ? DateTimeFormat::local($event['start'], 'c')  : DateTimeFormat::utc($event['start'], 'c');
                        $j     = $event['adjust'] ? DateTimeFormat::local($event['start'], 'j')  : DateTimeFormat::utc($event['start'], 'j');
                        $day   = $event['adjust'] ? DateTimeFormat::local($event['start'], $fmt) : DateTimeFormat::utc($event['start'], $fmt);
-                       $day   = day_translate($day);
+                       $day   = L10n::getDay($day);
 
                        if ($event['nofinish']) {
                                $end = null;
@@ -585,10 +596,9 @@ class Event extends BaseObject
                                $drop =                  [System::baseUrl() . '/events/drop/' . $event['id'] , L10n::t('Delete event')   , '', ''];
                        }
 
-                       $title = strip_tags(html_entity_decode(BBCode::convert($event['summary']), ENT_QUOTES, 'UTF-8'));
+                       $title = BBCode::convert(Strings::escapeHtml($event['summary']));
                        if (!$title) {
-                               list($title, $_trash) = explode("<br", BBCode::convert($event['desc']), 2);
-                               $title = strip_tags(html_entity_decode($title, ENT_QUOTES, 'UTF-8'));
+                               list($title, $_trash) = explode("<br", BBCode::convert(Strings::escapeHtml($event['desc'])), 2);
                        }
 
                        $author_link = $event['author-link'];
@@ -598,8 +608,9 @@ class Event extends BaseObject
                        $event['plink']       = Contact::magicLink($author_link, $plink);
 
                        $html = self::getHTML($event);
-                       $event['desc']     = BBCode::convert($event['desc']);
-                       $event['location'] = BBCode::convert($event['location']);
+                       $event['summary']  = BBCode::convert(Strings::escapeHtml($event['summary']));
+                       $event['desc']     = BBCode::convert(Strings::escapeHtml($event['desc']));
+                       $event['location'] = BBCode::convert(Strings::escapeHtml($event['location']));
                        $event_list[] = [
                                'id'       => $event['id'],
                                'start'    => $start,
@@ -624,13 +635,13 @@ class Event extends BaseObject
        /**
         * @brief Format event to export format (ical/csv).
         *
-        * @param array  $events   Query result for events.
-        * @param string $format   The output format (ical/csv).
-        * @param string $timezone The timezone of the user (not implemented yet).
+        * @param array  $events Query result for events.
+        * @param string $format The output format (ical/csv).
         *
+        * @param        $timezone
         * @return string Content according to selected export format.
         *
-        * @todo Implement timezone support
+        * @todo  Implement timezone support
         */
        private static function formatListForExport(array $events, $format, $timezone)
        {
@@ -738,6 +749,7 @@ class Event extends BaseObject
         * @param int $uid The user ID.
         *
         * @return array Query results.
+        * @throws \Exception
         */
        private static function getListByUserId($uid = 0)
        {
@@ -768,14 +780,15 @@ class Event extends BaseObject
 
        /**
         *
-        * @param int $uid The user ID.
+        * @param int    $uid    The user ID.
         * @param string $format Output format (ical/csv).
         * @return array With the results:
-        *      bool 'success' => True if the processing was successful,<br>
-        *      string 'format' => The output format,<br>
-        *      string 'extension' => The file extension of the output format,<br>
-        *      string 'content' => The formatted output content.<br>
+        *                       bool 'success' => True if the processing was successful,<br>
+        *                       string 'format' => The output format,<br>
+        *                       string 'extension' => The file extension of the output format,<br>
+        *                       string 'content' => The formatted output content.<br>
         *
+        * @throws \Exception
         * @todo Respect authenticated users with events_by_uid().
         */
        public static function exportListByUserId($uid, $format = 'ical')
@@ -828,6 +841,8 @@ class Event extends BaseObject
         *
         * @param array $item Array with item and event data.
         * @return string HTML output.
+        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+        * @throws \ImagickException
         */
        public static function getItemHTML(array $item) {
                $same_date = false;
@@ -839,14 +854,14 @@ class Event extends BaseObject
                $tformat       = L10n::t('g:i A'); // 8:01 AM.
 
                // Convert the time to different formats.
-               $dtstart_dt = day_translate(
+               $dtstart_dt = L10n::getDay(
                        $item['event-adjust'] ?
                                DateTimeFormat::local($item['event-start'], $dformat)
                                : DateTimeFormat::utc($item['event-start'], $dformat)
                );
                $dtstart_title = DateTimeFormat::utc($item['event-start'], $item['event-adjust'] ? DateTimeFormat::ATOM : 'Y-m-d\TH:i:s');
                // Format: Jan till Dec.
-               $month_short = day_short_translate(
+               $month_short = L10n::getDayShort(
                        $item['event-adjust'] ?
                                DateTimeFormat::local($item['event-start'], 'M')
                                : DateTimeFormat::utc($item['event-start'], 'M')
@@ -858,7 +873,7 @@ class Event extends BaseObject
                $start_time = $item['event-adjust'] ?
                        DateTimeFormat::local($item['event-start'], $tformat)
                        : DateTimeFormat::utc($item['event-start'], $tformat);
-               $start_short = day_short_translate(
+               $start_short = L10n::getDayShort(
                        $item['event-adjust'] ?
                                DateTimeFormat::local($item['event-start'], $dformat_short)
                                : DateTimeFormat::utc($item['event-start'], $dformat_short)
@@ -867,13 +882,13 @@ class Event extends BaseObject
                // If the option 'nofinisch' isn't set, we need to format the finish date/time.
                if (!$item['event-nofinish']) {
                        $finish = true;
-                       $dtend_dt  = day_translate(
+                       $dtend_dt  = L10n::getDay(
                                $item['event-adjust'] ?
                                        DateTimeFormat::local($item['event-finish'], $dformat)
                                        : DateTimeFormat::utc($item['event-finish'], $dformat)
                        );
                        $dtend_title = DateTimeFormat::utc($item['event-finish'], $item['event-adjust'] ? DateTimeFormat::ATOM : 'Y-m-d\TH:i:s');
-                       $end_short = day_short_translate(
+                       $end_short = L10n::getDayShort(
                                $item['event-adjust'] ?
                                        DateTimeFormat::local($item['event-finish'], $dformat_short)
                                        : DateTimeFormat::utc($item['event-finish'], $dformat_short)
@@ -898,8 +913,8 @@ class Event extends BaseObject
                // Construct the profile link (magic-auth).
                $profile_link = Contact::magicLinkById($item['author-id']);
 
-               $tpl = get_markup_template('event_stream_item.tpl');
-               $return = replace_macros($tpl, [
+               $tpl = Renderer::getMarkupTemplate('event_stream_item.tpl');
+               $return = Renderer::replaceMacros($tpl, [
                        '$id'             => $item['event-id'],
                        '$title'          => prepare_text($item['event-summary']),
                        '$dtstart_label'  => L10n::t('Starts:'),
@@ -942,6 +957,7 @@ class Event extends BaseObject
         *  'name' => The name of the location,<br>
         * 'address' => The address of the location,<br>
         * 'coordinates' => Latitude‎ and longitude‎ (e.g. '48.864716,2.349014').<br>
+        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
         */
        private static function locationToArray($s = '') {
                if ($s == '') {
@@ -979,4 +995,48 @@ class Event extends BaseObject
 
                return $location;
        }
+
+       /**
+        * @brief Add new birthday event for this person
+        *
+        * @param array  $contact  Contact array, expects: id, uid, url, name
+        * @param string $birthday Birthday of the contact
+        * @return bool
+        * @throws \Exception
+        */
+       public static function createBirthday($contact, $birthday)
+       {
+               // Check for duplicates
+               $condition = [
+                       'uid' => $contact['uid'],
+                       'cid' => $contact['id'],
+                       'start' => DateTimeFormat::utc($birthday),
+                       'type' => 'birthday'
+               ];
+               if (DBA::exists('event', $condition)) {
+                       return false;
+               }
+
+               /*
+                * Add new birthday event for this person
+                *
+                * summary is just a readable placeholder in case the event is shared
+                * with others. We will replace it during presentation to our $importer
+                * to contain a sparkle link and perhaps a photo.
+                */
+               $values = [
+                       'uid'     => $contact['uid'],
+                       'cid'     => $contact['id'],
+                       'start'   => DateTimeFormat::utc($birthday),
+                       'finish'  => DateTimeFormat::utc($birthday . ' + 1 day '),
+                       'summary' => L10n::t('%s\'s birthday', $contact['name']),
+                       'desc'    => L10n::t('Happy Birthday %s', ' [url=' . $contact['url'] . ']' . $contact['name'] . '[/url]'),
+                       'type'    => 'birthday',
+                       'adjust'  => 0
+               ];
+
+               self::store($values);
+
+               return true;
+       }
 }