| 1 | 
           efrain | 
           1 | 
           <?php
  | 
        
        
            | 
            | 
           2 | 
           // This file is part of Moodle - http://moodle.org/
  | 
        
        
            | 
            | 
           3 | 
           //
  | 
        
        
            | 
            | 
           4 | 
           // Moodle is free software: you can redistribute it and/or modify
  | 
        
        
            | 
            | 
           5 | 
           // it under the terms of the GNU General Public License as published by
  | 
        
        
            | 
            | 
           6 | 
           // the Free Software Foundation, either version 3 of the License, or
  | 
        
        
            | 
            | 
           7 | 
           // (at your option) any later version.
  | 
        
        
            | 
            | 
           8 | 
           //
  | 
        
        
            | 
            | 
           9 | 
           // Moodle is distributed in the hope that it will be useful,
  | 
        
        
            | 
            | 
           10 | 
           // but WITHOUT ANY WARRANTY; without even the implied warranty of
  | 
        
        
            | 
            | 
           11 | 
           // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  | 
        
        
            | 
            | 
           12 | 
           // GNU General Public License for more details.
  | 
        
        
            | 
            | 
           13 | 
           //
  | 
        
        
            | 
            | 
           14 | 
           // You should have received a copy of the GNU General Public License
  | 
        
        
            | 
            | 
           15 | 
           // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  | 
        
        
            | 
            | 
           16 | 
              | 
        
        
            | 
            | 
           17 | 
           /**
  | 
        
        
            | 
            | 
           18 | 
            * Form for editing RSS client block instances.
  | 
        
        
            | 
            | 
           19 | 
            *
  | 
        
        
            | 
            | 
           20 | 
            * @package   block_rss_client
  | 
        
        
            | 
            | 
           21 | 
            * @copyright 2009 Tim Hunt
  | 
        
        
            | 
            | 
           22 | 
            * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  | 
        
        
            | 
            | 
           23 | 
            */
  | 
        
        
            | 
            | 
           24 | 
              | 
        
        
           | 1441 | 
           ariadna | 
           25 | 
           defined('MOODLE_INTERNAL') || die();
  | 
        
        
            | 
            | 
           26 | 
              | 
        
        
            | 
            | 
           27 | 
           require_once($CFG->libdir .'/simplepie/moodle_simplepie.php');
  | 
        
        
            | 
            | 
           28 | 
              | 
        
        
           | 1 | 
           efrain | 
           29 | 
           /**
  | 
        
        
            | 
            | 
           30 | 
            * Form for editing RSS client block instances.
  | 
        
        
            | 
            | 
           31 | 
            *
  | 
        
        
            | 
            | 
           32 | 
            * @copyright 2009 Tim Hunt
  | 
        
        
            | 
            | 
           33 | 
            * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  | 
        
        
            | 
            | 
           34 | 
            */
  | 
        
        
            | 
            | 
           35 | 
           class block_rss_client_edit_form extends block_edit_form {
  | 
        
        
           | 1441 | 
           ariadna | 
           36 | 
              | 
        
        
            | 
            | 
           37 | 
               /** @var stdClass|null The new RSS feed URL object. */
  | 
        
        
            | 
            | 
           38 | 
               private ?stdClass $newrss = null;
  | 
        
        
            | 
            | 
           39 | 
              | 
        
        
           | 1 | 
           efrain | 
           40 | 
               protected function specific_definition($mform) {
  | 
        
        
            | 
            | 
           41 | 
                   global $CFG, $DB, $USER;
  | 
        
        
            | 
            | 
           42 | 
              | 
        
        
            | 
            | 
           43 | 
                   // Fields for editing block contents.
  | 
        
        
            | 
            | 
           44 | 
                   $mform->addElement('header', 'configheader', get_string('blocksettings', 'block'));
  | 
        
        
            | 
            | 
           45 | 
              | 
        
        
           | 1441 | 
           ariadna | 
           46 | 
                   $radiogroup = [
  | 
        
        
            | 
            | 
           47 | 
                       $mform->createElement('radio', 'config_method',
  | 
        
        
            | 
            | 
           48 | 
                           get_string('configmethodexisting', 'block_rss_client'), null, 'existing'),
  | 
        
        
            | 
            | 
           49 | 
                       $mform->createElement('radio', 'config_method',
  | 
        
        
            | 
            | 
           50 | 
                           get_string('configmethodnew', 'block_rss_client'), null, 'new'),
  | 
        
        
            | 
            | 
           51 | 
                   ];
  | 
        
        
            | 
            | 
           52 | 
                   $mform->addGroup(
  | 
        
        
            | 
            | 
           53 | 
                       elements: $radiogroup,
  | 
        
        
            | 
            | 
           54 | 
                       name: 'config_method_group',
  | 
        
        
            | 
            | 
           55 | 
                       separator: ['   '],
  | 
        
        
            | 
            | 
           56 | 
                       appendName: false,
  | 
        
        
            | 
            | 
           57 | 
                   );
  | 
        
        
            | 
            | 
           58 | 
                   $mform->setDefault('config_method', 'existing');
  | 
        
        
           | 1 | 
           efrain | 
           59 | 
              | 
        
        
           | 1441 | 
           ariadna | 
           60 | 
                   // Add new RSS feed.
  | 
        
        
            | 
            | 
           61 | 
                   $mform->addElement('text', 'config_feedurl', get_string('feedurl', 'block_rss_client'));
  | 
        
        
            | 
            | 
           62 | 
                   $mform->setType('config_feedurl', PARAM_URL);
  | 
        
        
            | 
            | 
           63 | 
                   $mform->hideIf('config_feedurl', 'config_method', 'ne', 'new');
  | 
        
        
           | 1 | 
           efrain | 
           64 | 
              | 
        
        
           | 1441 | 
           ariadna | 
           65 | 
                   // Select existing RSS feed.
  | 
        
        
           | 1 | 
           efrain | 
           66 | 
                   $insql = '';
  | 
        
        
            | 
            | 
           67 | 
                   $params = array('userid' => $USER->id);
  | 
        
        
            | 
            | 
           68 | 
                   if (!empty($this->block->config) && !empty($this->block->config->rssid)) {
  | 
        
        
            | 
            | 
           69 | 
                       list($insql, $inparams) = $DB->get_in_or_equal($this->block->config->rssid, SQL_PARAMS_NAMED);
  | 
        
        
            | 
            | 
           70 | 
                       $insql = "OR id $insql ";
  | 
        
        
            | 
            | 
           71 | 
                       $params += $inparams;
  | 
        
        
            | 
            | 
           72 | 
                   }
  | 
        
        
            | 
            | 
           73 | 
              | 
        
        
            | 
            | 
           74 | 
                   $titlesql = "CASE WHEN {$DB->sql_isempty('block_rss_client','preferredtitle', false, false)}
  | 
        
        
            | 
            | 
           75 | 
                                 THEN {$DB->sql_compare_text('title', 64)} ELSE preferredtitle END";
  | 
        
        
            | 
            | 
           76 | 
              | 
        
        
            | 
            | 
           77 | 
                   $rssfeeds = $DB->get_records_sql_menu("
  | 
        
        
            | 
            | 
           78 | 
                           SELECT id, $titlesql
  | 
        
        
            | 
            | 
           79 | 
                             FROM {block_rss_client}
  | 
        
        
            | 
            | 
           80 | 
                            WHERE userid = :userid OR shared = 1 $insql
  | 
        
        
            | 
            | 
           81 | 
                            ORDER BY $titlesql",
  | 
        
        
            | 
            | 
           82 | 
                           $params);
  | 
        
        
            | 
            | 
           83 | 
              | 
        
        
            | 
            | 
           84 | 
                   if ($rssfeeds) {
  | 
        
        
            | 
            | 
           85 | 
                       $select = $mform->addElement('select', 'config_rssid', get_string('choosefeedlabel', 'block_rss_client'), $rssfeeds);
  | 
        
        
            | 
            | 
           86 | 
                       $select->setMultiple(true);
  | 
        
        
           | 1441 | 
           ariadna | 
           87 | 
                       $mform->hideIf('config_rssid', 'config_method', 'ne', 'existing');
  | 
        
        
           | 1 | 
           efrain | 
           88 | 
                   } else {
  | 
        
        
            | 
            | 
           89 | 
                       $mform->addElement('static', 'config_rssid_no_feeds', get_string('choosefeedlabel', 'block_rss_client'),
  | 
        
        
            | 
            | 
           90 | 
                               get_string('nofeeds', 'block_rss_client'));
  | 
        
        
           | 1441 | 
           ariadna | 
           91 | 
                       $mform->hideIf('config_rssid_no_feeds', 'config_method', 'ne', 'existing');
  | 
        
        
           | 1 | 
           efrain | 
           92 | 
                   }
  | 
        
        
            | 
            | 
           93 | 
              | 
        
        
           | 1441 | 
           ariadna | 
           94 | 
                   // Subheading: Display settings for RSS feed.
  | 
        
        
            | 
            | 
           95 | 
                   $startsubheading = '<div class="row"><h4 class="col-md-12 col-form-label d-flex">';
  | 
        
        
            | 
            | 
           96 | 
                   $endsubheading = '</h4></div>';
  | 
        
        
            | 
            | 
           97 | 
                   $mform->addElement('html', $startsubheading . get_string('displaysettings', 'block_rss_client') . $endsubheading);
  | 
        
        
           | 1 | 
           efrain | 
           98 | 
              | 
        
        
            | 
            | 
           99 | 
                   $mform->addElement('text', 'config_title', get_string('uploadlabel'));
  | 
        
        
            | 
            | 
           100 | 
                   $mform->setType('config_title', PARAM_NOTAGS);
  | 
        
        
            | 
            | 
           101 | 
              | 
        
        
           | 1441 | 
           ariadna | 
           102 | 
                   $mform->addElement('selectyesno', 'config_display_description', get_string('displaydescriptionlabel', 'block_rss_client'));
  | 
        
        
            | 
            | 
           103 | 
                   $mform->setDefault('config_display_description', 0);
  | 
        
        
            | 
            | 
           104 | 
              | 
        
        
            | 
            | 
           105 | 
                   $mform->addElement('text', 'config_shownumentries', get_string('shownumentrieslabel', 'block_rss_client'), ['size' => 5]);
  | 
        
        
            | 
            | 
           106 | 
                   $mform->setType('config_shownumentries', PARAM_INT);
  | 
        
        
            | 
            | 
           107 | 
                   $mform->addRule('config_shownumentries', null, 'numeric', null, 'client');
  | 
        
        
            | 
            | 
           108 | 
                   if (!empty($CFG->block_rss_client_num_entries)) {
  | 
        
        
            | 
            | 
           109 | 
                       $mform->setDefault('config_shownumentries', $CFG->block_rss_client_num_entries);
  | 
        
        
            | 
            | 
           110 | 
                   } else {
  | 
        
        
            | 
            | 
           111 | 
                       $mform->setDefault('config_shownumentries', 5);
  | 
        
        
            | 
            | 
           112 | 
                   }
  | 
        
        
            | 
            | 
           113 | 
              | 
        
        
           | 1 | 
           efrain | 
           114 | 
                   $mform->addElement('selectyesno', 'config_block_rss_client_show_channel_link', get_string('clientshowchannellinklabel', 'block_rss_client'));
  | 
        
        
            | 
            | 
           115 | 
                   $mform->setDefault('config_block_rss_client_show_channel_link', 0);
  | 
        
        
            | 
            | 
           116 | 
              | 
        
        
            | 
            | 
           117 | 
                   $mform->addElement('selectyesno', 'config_block_rss_client_show_channel_image', get_string('clientshowimagelabel', 'block_rss_client'));
  | 
        
        
            | 
            | 
           118 | 
                   $mform->setDefault('config_block_rss_client_show_channel_image', 0);
  | 
        
        
            | 
            | 
           119 | 
               }
  | 
        
        
            | 
            | 
           120 | 
              | 
        
        
            | 
            | 
           121 | 
               /**
  | 
        
        
           | 1441 | 
           ariadna | 
           122 | 
                * Overriding the get_data function to insert a new RSS ID.
  | 
        
        
            | 
            | 
           123 | 
                */
  | 
        
        
            | 
            | 
           124 | 
               public function get_data(): ?stdClass {
  | 
        
        
            | 
            | 
           125 | 
                   $data = parent::get_data();
  | 
        
        
            | 
            | 
           126 | 
                   // Force the 'existing` method as a default.
  | 
        
        
            | 
            | 
           127 | 
                   $data->config_method = 'existing';
  | 
        
        
            | 
            | 
           128 | 
                   // Sanitize the title to prevent XSS (Cross-Site Scripting) attacks by encoding special characters into HTML entities.
  | 
        
        
            | 
            | 
           129 | 
                   $data->config_title = htmlspecialchars($data->config_title, ENT_QUOTES, 'utf-8');
  | 
        
        
            | 
            | 
           130 | 
                   // If the new RSS is not empty then add the ID to the config_rssid.
  | 
        
        
            | 
            | 
           131 | 
                   if ($data && $this->newrss) {
  | 
        
        
            | 
            | 
           132 | 
                       $data->config_rssid[] = $this->newrss->id;
  | 
        
        
            | 
            | 
           133 | 
                   }
  | 
        
        
            | 
            | 
           134 | 
                   return $data;
  | 
        
        
            | 
            | 
           135 | 
               }
  | 
        
        
            | 
            | 
           136 | 
              | 
        
        
            | 
            | 
           137 | 
               /**
  | 
        
        
            | 
            | 
           138 | 
                * Overriding the definition_after_data to empty the input.
  | 
        
        
            | 
            | 
           139 | 
                */
  | 
        
        
            | 
            | 
           140 | 
               public function definition_after_data(): void {
  | 
        
        
            | 
            | 
           141 | 
                   parent::definition_after_data();
  | 
        
        
            | 
            | 
           142 | 
                   $mform =& $this->_form;
  | 
        
        
            | 
            | 
           143 | 
                   // If form is not submitted then empty the feed URL.
  | 
        
        
            | 
            | 
           144 | 
                   if (!$this->is_submitted()) {
  | 
        
        
            | 
            | 
           145 | 
                       $mform->getElement('config_feedurl')->setValue('');
  | 
        
        
            | 
            | 
           146 | 
                   }
  | 
        
        
            | 
            | 
           147 | 
               }
  | 
        
        
            | 
            | 
           148 | 
              | 
        
        
            | 
            | 
           149 | 
               /**
  | 
        
        
            | 
            | 
           150 | 
                * Overriding the validation to validate the RSS URL and store it to the database.
  | 
        
        
            | 
            | 
           151 | 
                *
  | 
        
        
            | 
            | 
           152 | 
                * If there are no errors, insert the new feed to the database and store the object in
  | 
        
        
            | 
            | 
           153 | 
                * the private property so it can be saved to the RSS block config.
  | 
        
        
            | 
            | 
           154 | 
                *
  | 
        
        
            | 
            | 
           155 | 
                * @param array $data Data from the form.
  | 
        
        
            | 
            | 
           156 | 
                * @param array $files Files form the form.
  | 
        
        
            | 
            | 
           157 | 
                * @return array of errors from validation.
  | 
        
        
            | 
            | 
           158 | 
                */
  | 
        
        
            | 
            | 
           159 | 
               public function validation($data, $files): array {
  | 
        
        
            | 
            | 
           160 | 
                   global $USER, $DB;
  | 
        
        
            | 
            | 
           161 | 
                   $errors = parent::validation($data, $files);
  | 
        
        
            | 
            | 
           162 | 
              | 
        
        
            | 
            | 
           163 | 
                   if ($data['config_method'] === "new") {
  | 
        
        
            | 
            | 
           164 | 
                       // If the "New" method is selected and the feed URL is not empty, then proceed.
  | 
        
        
            | 
            | 
           165 | 
                       if ($data['config_feedurl']) {
  | 
        
        
            | 
            | 
           166 | 
                           if (!filter_var($data['config_feedurl'], FILTER_VALIDATE_URL)) {
  | 
        
        
            | 
            | 
           167 | 
                               $errors['config_feedurl'] = get_string('couldnotfindloadrssfeed', 'block_rss_client');
  | 
        
        
            | 
            | 
           168 | 
                               return $errors;
  | 
        
        
            | 
            | 
           169 | 
                           }
  | 
        
        
            | 
            | 
           170 | 
                           try {
  | 
        
        
            | 
            | 
           171 | 
                               $rss = new moodle_simplepie();
  | 
        
        
            | 
            | 
           172 | 
                               // Set timeout for longer than normal to try and grab the feed.
  | 
        
        
            | 
            | 
           173 | 
                               $rss->set_timeout(10);
  | 
        
        
            | 
            | 
           174 | 
                               $rss->set_feed_url($data['config_feedurl']);
  | 
        
        
            | 
            | 
           175 | 
                               $rss->set_autodiscovery_cache_duration(0);
  | 
        
        
            | 
            | 
           176 | 
                               $rss->set_autodiscovery_level(moodle_simplepie::LOCATOR_ALL);
  | 
        
        
            | 
            | 
           177 | 
                               $rss->init();
  | 
        
        
            | 
            | 
           178 | 
                               if ($rss->error()) {
  | 
        
        
            | 
            | 
           179 | 
                                   $errors['config_feedurl'] = get_string('couldnotfindloadrssfeed', 'block_rss_client');
  | 
        
        
            | 
            | 
           180 | 
                               } else {
  | 
        
        
            | 
            | 
           181 | 
                                   // Return URL without quoting.
  | 
        
        
            | 
            | 
           182 | 
                                   $discoveredurl = new moodle_url($rss->subscribe_url());
  | 
        
        
            | 
            | 
           183 | 
                                   $theurl = $discoveredurl->out(false);
  | 
        
        
            | 
            | 
           184 | 
                                   // Save the RSS to the database.
  | 
        
        
            | 
            | 
           185 | 
                                   $this->newrss = new stdClass;
  | 
        
        
            | 
            | 
           186 | 
                                   $this->newrss->userid = $USER->id;
  | 
        
        
            | 
            | 
           187 | 
                                   $this->newrss->title = $rss->get_title();
  | 
        
        
            | 
            | 
           188 | 
                                   $this->newrss->description = $rss->get_description();
  | 
        
        
            | 
            | 
           189 | 
                                   $this->newrss->url = $theurl;
  | 
        
        
            | 
            | 
           190 | 
                                   $newrssid = $DB->insert_record('block_rss_client', $this->newrss);
  | 
        
        
            | 
            | 
           191 | 
                                   $this->newrss->id = $newrssid;
  | 
        
        
            | 
            | 
           192 | 
                               }
  | 
        
        
            | 
            | 
           193 | 
                           } catch (Exception $e) {
  | 
        
        
            | 
            | 
           194 | 
                               $errors['config_feedurl'] = get_string('couldnotfindloadrssfeed', 'block_rss_client');
  | 
        
        
            | 
            | 
           195 | 
                           }
  | 
        
        
            | 
            | 
           196 | 
                       } else {
  | 
        
        
            | 
            | 
           197 | 
                           // If the "New" method is selected, but the feed URL is empty, then raise error.
  | 
        
        
            | 
            | 
           198 | 
                           $errors['config_feedurl'] = get_string('err_required', 'form');
  | 
        
        
            | 
            | 
           199 | 
                       }
  | 
        
        
            | 
            | 
           200 | 
              | 
        
        
            | 
            | 
           201 | 
                   }
  | 
        
        
            | 
            | 
           202 | 
              | 
        
        
            | 
            | 
           203 | 
                   return $errors;
  | 
        
        
            | 
            | 
           204 | 
               }
  | 
        
        
            | 
            | 
           205 | 
              | 
        
        
            | 
            | 
           206 | 
               /**
  | 
        
        
           | 1 | 
           efrain | 
           207 | 
                * Display the configuration form when block is being added to the page
  | 
        
        
            | 
            | 
           208 | 
                *
  | 
        
        
            | 
            | 
           209 | 
                * @return bool
  | 
        
        
            | 
            | 
           210 | 
                */
  | 
        
        
            | 
            | 
           211 | 
               public static function display_form_when_adding(): bool {
  | 
        
        
            | 
            | 
           212 | 
                   return true;
  | 
        
        
            | 
            | 
           213 | 
               }
  | 
        
        
            | 
            | 
           214 | 
           }
  |