Exemple #1
0
 /**
  * Gets the currencies
  * @return array List of currencies
  */
 function getData()
 {
     // Lets load the data if it doesn't already exist
     if (empty($this->_data)) {
         $query = $this->_buildQuery();
         $this->_data = $this->_getList($query, 0, 1);
         if (!count($this->_data)) {
             JError::raiseError(404, JText::_('COM_CONTENTBUILDER_FORM_NOT_FOUND'));
         }
         foreach ($this->_data as $data) {
             if (!$this->frontend && $data->display_in == 0) {
                 JError::raiseError(404, JText::_('COM_CONTENTBUILDER_RECORD_NOT_FOUND'));
             } else {
                 if ($this->frontend && $data->display_in == 1) {
                     JError::raiseError(404, JText::_('COM_CONTENTBUILDER_RECORD_NOT_FOUND'));
                 }
             }
             $data->form_id = $this->_id;
             $data->record_id = $this->_record_id;
             if ($data->type && $data->reference_id) {
                 $data->form = contentbuilder::getForm($data->type, $data->reference_id);
                 $data->labels = $data->form->getElementLabels();
                 $ids = array();
                 foreach ($data->labels as $reference_id => $label) {
                     $ids[] = $this->_db->Quote($reference_id);
                 }
                 if (count($ids)) {
                     $this->_db->setQuery("Select Distinct `label`, reference_id From #__contentbuilder_elements Where form_id = " . intval($this->_id) . " And reference_id In (" . implode(',', $ids) . ") And published = 1 Order By ordering");
                     $rows = $this->_db->loadAssocList();
                     $ids = array();
                     foreach ($rows as $row) {
                         $ids[] = $row['reference_id'];
                     }
                 }
                 if ($this->_latest) {
                     $rec = $data->form->getListRecords($ids, '', array(), 0, 1, '', array(), 'desc', 0, false, JFactory::getUser()->get('id', 0), 0, -1, -1, -1, -1, array(), true, null);
                     if (count($rec) > 0) {
                         $rec = $rec[0];
                         $rec2 = $data->form->getRecord($rec->colRecord, false, -1, true);
                         $data->record_id = $rec->colRecord;
                         JRequest::setVar('record_id', $data->record_id);
                         $this->_record_id = $data->record_id;
                     } else {
                         JRequest::setVar('cbIsNew', 1);
                         contentbuilder::setPermissions(JRequest::getInt('id', 0), 0, $this->frontend ? '_fe' : '');
                         $auth = $this->frontend ? contentbuilder::authorizeFe('new') : contentbuilder::authorize('new');
                         if ($auth) {
                             JFactory::getApplication()->redirect(JRoute::_('index.php?option=com_contentbuilder&controller=edit&latest=1&backtolist=' . JRequest::getInt('backtolist', 0) . '&id=' . $this->_id . '&record_id=&limitstart=' . JRequest::getInt('limitstart', 0) . '&filter_order=' . JRequest::getVar('filter_order', ''), false));
                         } else {
                             JFactory::getApplication()->enqueueMessage(JText::_('COM_CONTENTBUILDER_ADD_ENTRY_FIRST'));
                             JFactory::getApplication()->redirect('index.php', false);
                         }
                     }
                 }
                 $data->show_page_heading = $this->_show_page_heading;
                 if (!$data->form->exists) {
                     JError::raiseError(404, JText::_('COM_CONTENTBUILDER_FORM_NOT_FOUND'));
                 }
                 $data->page_title = '';
                 if (JRequest::getInt('cb_prefix_in_title', 1)) {
                     if (!$this->_menu_item) {
                         $data->page_title = $data->use_view_name_as_title ? $data->name : $data->form->getPageTitle();
                     } else {
                         $data->page_title = $data->use_view_name_as_title ? $data->name : JFactory::getDocument()->getTitle();
                     }
                 }
                 if ($this->frontend) {
                     $document = JFactory::getDocument();
                     $document->setTitle($data->page_title);
                 }
                 $data->show_back_button = $this->_show_back_button;
                 if (isset($rec2) && count($rec2)) {
                     $data->items = $rec2;
                 } else {
                     $data->items = $data->form->getRecord($this->_record_id, $data->published_only, $this->frontend ? $data->own_only_fe ? JFactory::getUser()->get('id', 0) : -1 : ($data->own_only ? JFactory::getUser()->get('id', 0) : -1), $this->frontend ? $data->show_all_languages_fe : true);
                 }
                 if (count($data->items)) {
                     $user = null;
                     if ($data->act_as_registration) {
                         $meta = $data->form->getRecordMetadata($this->_record_id);
                         $this->_db->setQuery("Select * From #__users Where id = " . $meta->created_id);
                         $user = $this->_db->loadObject();
                     }
                     $label = '';
                     foreach ($data->items as $rec) {
                         if ($rec->recElementId == $data->title_field) {
                             if ($data->act_as_registration && $user !== null) {
                                 if ($data->registration_name_field == $rec->recElementId) {
                                     $rec->recValue = $user->name;
                                 } else {
                                     if ($data->registration_username_field == $rec->recElementId) {
                                         $item->recValue = $user->username;
                                     } else {
                                         if ($data->registration_email_field == $item->recElementId) {
                                             $rec->recValue = $user->email;
                                         } else {
                                             if ($data->registration_email_repeat_field == $rec->recElementId) {
                                                 $rec->recValue = $user->email;
                                             }
                                         }
                                     }
                                 }
                             }
                             $label = cbinternal($rec->recValue);
                             break;
                         }
                     }
                     $ordered_extra_title = '';
                     foreach ($this->_menu_filter_order as $order_key => $order) {
                         if (isset($this->_menu_filter[$order_key])) {
                             // range test
                             $is_range = strstr(strtolower(implode(',', $this->_menu_filter[$order_key])), '@range') !== false;
                             $is_match = strstr(strtolower(implode(',', $this->_menu_filter[$order_key])), '@match') !== false;
                             if ($is_range) {
                                 $ex = explode('/', implode(', ', $this->_menu_filter[$order_key]));
                                 if (count($ex) == 3) {
                                     $ex2 = explode('to', trim($ex[2]));
                                     $out = '';
                                     $val = $ex2[0];
                                     $val2 = '';
                                     if (isset($ex2[1])) {
                                         $val2 = $ex2[1];
                                     }
                                     if (strtolower(trim($ex[1])) == 'date') {
                                         $val = JHTML::_('date', $ex2[0], JText::_('DATE_FORMAT_LC3'));
                                         if (isset($ex2[1])) {
                                             $val2 = JHTML::_('date', $ex2[1], JText::_('DATE_FORMAT_LC3'));
                                         }
                                     }
                                     if (count($ex2) == 2) {
                                         $out = (trim($ex2[0]) ? JText::_('COM_CONTENTBUILDER_FROM') . ' ' . trim($val) : '') . ' ' . JText::_('COM_CONTENTBUILDER_TO') . ' ' . trim($val2);
                                     } else {
                                         if (count($ex2) > 0) {
                                             $out = JText::_('COM_CONTENTBUILDER_FROM') . ' ' . trim($val);
                                         }
                                     }
                                     if ($out) {
                                         $this->_menu_filter[$order_key] = $ex;
                                         $ordered_extra_title .= ' » ' . htmlentities($data->labels[$order_key], ENT_QUOTES, 'UTF-8') . ': ' . htmlentities($out, ENT_QUOTES, 'UTF-8');
                                     }
                                 }
                             } else {
                                 if ($is_match) {
                                     $ex = explode('/', implode(', ', $this->_menu_filter[$order_key]));
                                     if (count($ex) == 2) {
                                         $ex2 = explode(';', trim($ex[1]));
                                         $out = '';
                                         $size = count($ex2);
                                         $i = 0;
                                         foreach ($ex2 as $val) {
                                             if ($i + 1 < $size) {
                                                 $out .= trim($val) . ' ' . JText::_('COM_CONTENTBUILDER_AND') . ' ';
                                             } else {
                                                 $out .= trim($val);
                                             }
                                             $i++;
                                         }
                                         if ($out) {
                                             $this->_menu_filter[$order_key] = $ex;
                                             $ordered_extra_title .= ' &raquo; ' . htmlentities($data->labels[$order_key], ENT_QUOTES, 'UTF-8') . ': ' . htmlentities($out, ENT_QUOTES, 'UTF-8');
                                         }
                                     }
                                 } else {
                                     $ordered_extra_title .= ' &raquo; ' . htmlentities($data->labels[$order_key], ENT_QUOTES, 'UTF-8') . ': ' . htmlentities(implode(', ', $this->_menu_filter[$order_key]), ENT_QUOTES, 'UTF-8');
                                 }
                             }
                         }
                     }
                     $data->page_title .= $ordered_extra_title;
                     // trying first element if no title field given
                     if (!$label) {
                         $label = cbinternal($data->items[0]->recValue);
                     }
                     // "buddy quaid hack", should be an option in future versions
                     jimport('joomla.version');
                     $version = new JVersion();
                     if (version_compare($version->getShortVersion(), '1.6', '>=')) {
                         if ($this->_show_page_heading && $this->_page_title != '' && $this->_page_heading != '' && $this->_page_title == $this->_page_heading) {
                             $data->page_title = $this->_page_title;
                         } else {
                             $data->page_title .= $label ? (!$data->page_title ? '' : (!$ordered_extra_title ? ': ' : ' &raquo; ')) . $label : '';
                         }
                         if ($this->frontend) {
                             $document = JFactory::getDocument();
                             $document->setTitle(html_entity_decode($data->page_title, ENT_QUOTES, 'UTF-8'));
                         }
                     } else {
                         if ($this->_show_page_heading && $this->_page_title != '' && !JRequest::getInt('cb_prefix_in_title', 1)) {
                             $data->page_title = $this->_page_title;
                         } else {
                             $data->page_title .= $label ? (!$data->page_title ? '' : (!$ordered_extra_title ? ': ' : ' &raquo; ')) . $label : '';
                         }
                         if ($this->frontend) {
                             $document = JFactory::getDocument();
                             $document->setTitle(html_entity_decode($data->page_title, ENT_QUOTES, 'UTF-8'));
                         }
                     }
                     $data->template = contentbuilder::getTemplate($this->_id, $this->_record_id, $data->items, $ids);
                     $metadata = $data->form->getRecordMetadata($this->_record_id);
                     if ($metadata instanceof stdClass && $data->metadata) {
                         $data->created = $metadata->created ? $metadata->created : '';
                         $data->created_by = $metadata->created_by ? $metadata->created_by : '';
                         $data->modified = $metadata->modified ? $metadata->modified : '';
                         $data->modified_by = $metadata->modified_by ? $metadata->modified_by : '';
                         $data->metadesc = $metadata->metadesc;
                         $data->metakey = $metadata->metakey;
                         $data->author = $metadata->author;
                         $data->rights = $metadata->rights;
                         $data->robots = $metadata->robots;
                         $data->xreference = $metadata->xreference;
                     } else {
                         $data->created = '';
                         $data->created_by = '';
                         $data->modified = '';
                         $data->modified_by = '';
                         $data->metadesc = '';
                         $data->metakey = '';
                         $data->author = '';
                         $data->rights = '';
                         $data->robots = '';
                         $data->xreference = '';
                     }
                 } else {
                     JError::raiseError(404, JText::_('COM_CONTENTBUILDER_RECORD_NOT_FOUND'));
                 }
             }
             return $data;
         }
     }
     return null;
 }
 public static function createArticle($contentbuilder_form_id, $record_id, array $record, array $elements_allowed, $title_field = '', $metadata = null, $config = array(), $full = false, $limited_options = true, $menu_cat_id = null)
 {
     $tz = new DateTimeZone(JFactory::getApplication()->getCfg('offset'));
     if (isset($config['publish_up']) && $config['publish_up'] && $config['publish_up'] != '0000-00-00 00:00:00') {
         $config['publish_up'] = JFactory::getDate($config['publish_up'], $tz);
         $config['publish_up'] = $config['publish_up']->format('Y-m-d H:i:s');
     } else {
         $config['publish_up'] = '0000-00-00 00:00:00';
     }
     if (isset($config['created']) && $config['created'] && $config['created'] != '0000-00-00 00:00:00') {
         $config['created'] = JFactory::getDate($config['created'], $tz);
         $config['created'] = $config['created']->format('Y-m-d H:i:s');
     } else {
         $config['created'] = '0000-00-00 00:00:00';
     }
     if (isset($config['publish_down']) && $config['publish_down'] && $config['publish_down'] != '0000-00-00 00:00:00') {
         $config['publish_down'] = JFactory::getDate($config['publish_down'], $tz);
         $config['publish_down'] = $config['publish_down']->format('Y-m-d H:i:s');
     } else {
         $config['publish_down'] = '0000-00-00 00:00:00';
     }
     $is15 = true;
     $version = new JVersion();
     if (version_compare($version->getShortVersion(), '1.6', '>=')) {
         $is15 = false;
     }
     $tpl = self::getTemplate($contentbuilder_form_id, $record_id, $record, $elements_allowed, true);
     if (!$tpl) {
         return 0;
     }
     $db = JFactory::getDBO();
     $db->setQuery("Select * From #__contentbuilder_forms Where id = " . intval($contentbuilder_form_id) . " And published = 1");
     $form = $db->loadAssoc();
     if (!$form) {
         return 0;
     }
     if ($is15 && $menu_cat_id !== null) {
         if (intval($menu_cat_id) > -2) {
             $menu_cat_id = explode(':', $menu_cat_id);
             if (count($menu_cat_id) == 2) {
                 $form['default_category'] = $menu_cat_id[1];
                 $form['default_section'] = $menu_cat_id[0];
             }
         }
     } else {
         if ($menu_cat_id !== null && intval($menu_cat_id) > -2) {
             $form['default_category'] = $menu_cat_id;
         }
     }
     $user = null;
     if ($form['act_as_registration']) {
         if ($record_id) {
             $form_ = contentbuilder::getForm($form['type'], $form['reference_id']);
             $meta = $form_->getRecordMetadata($record_id);
             $db->setQuery("Select * From #__users Where id = " . $meta->created_id);
             $user = $db->loadObject();
         } else {
             if (JFactory::getUser()->get('id', 0)) {
                 $db->setQuery("Select * From #__users Where id = " . JFactory::getUser()->get('id', 0));
                 $user = $db->loadObject();
             }
         }
     }
     $label = '';
     foreach ($record as $rec) {
         if ($rec->recElementId == $title_field) {
             if ($form['act_as_registration'] && $user !== null) {
                 if ($form['registration_name_field'] == $rec->recElementId) {
                     $rec->recValue = $user->name;
                 } else {
                     if ($form['registration_username_field'] == $rec->recElementId) {
                         $rec->recValue = $user->username;
                     } else {
                         if ($form['registration_email_field'] == $rec->recElementId) {
                             $rec->recValue = $user->email;
                         } else {
                             if ($form['registration_email_repeat_field'] == $rec->recElementId) {
                                 $rec->recValue = $user->email;
                             }
                         }
                     }
                 }
             }
             $label = cbinternal($rec->recValue);
             break;
         }
     }
     // trying first element if no title field given
     if (!$label && !count($record)) {
         $label = 'Unnamed';
     } else {
         if (!$label && count($record)) {
             $label = cbinternal($record[0]->recValue);
         }
     }
     // Clean text for xhtml transitional compliance
     $introtext = '';
     $fulltext = '';
     $tpl = str_replace('<br>', '<br />', $tpl);
     // Search for the {readmore} tag and split the text up accordingly.
     $pattern = '#<hr\\s+id=("|\')system-readmore("|\')\\s*\\/*>#i';
     $tagPos = preg_match($pattern, $tpl);
     if ($tagPos == 0) {
         $introtext = $tpl;
     } else {
         list($introtext, $fulltext) = preg_split($pattern, $tpl, 2);
     }
     // retrieve the publish state from the list view
     $state = 1;
     $db->setQuery("Select published, is_future, publish_up, publish_down From #__contentbuilder_records Where `type` = " . $db->Quote($form['type']) . " And reference_id = " . $db->Quote($form['reference_id']) . " And record_id = " . $db->Quote($record_id));
     $state = $db->loadAssoc();
     $publish_up_record = $state['publish_up'];
     $publish_down_record = $state['publish_down'];
     $state = $state['is_future'] ? 1 : $state['published'];
     // save/update articles
     $alias = '';
     $db->setQuery("Select articles.`article_id`, content.`alias` From #__contentbuilder_articles As articles, #__content As content Where content.id = articles.article_id And (content.state = 1 Or content.state = 0) And articles.form_id = " . intval($contentbuilder_form_id) . " And articles.record_id = " . $db->Quote($record_id));
     $article = $db->loadAssoc();
     if (is_array($article)) {
         $alias = $article['alias'];
         $article = $article['article_id'];
     }
     // params
     $attribs = '';
     $meta = '';
     $rules = '';
     $metakey = '';
     $metadesc = '';
     $created_by = 0;
     $created_by_alias = '';
     $created_article = null;
     $_now = JFactory::getDate();
     $created_up = $publish_up_record;
     $created_down = $publish_down_record;
     if (is_array($article) && isset($article['article_id']) && intval($form['default_publish_up_days']) != 0) {
         // this will cause errors on 64bit systems, as strtotime's behavior is different for null dates
         // $date = JFactory::getDate(strtotime('now +'.intval($form['default_publish_up_days']).' days'));
         // fix as of forum post http://crosstec.de/forums/37-contentbuilder-general-forum-english/62084-64-bit-strtotime-bug.html#62084
         // thanks to user Fremmedkar
         $date = JFactory::getDate(strtotime(($created_up !== null && $created_up != '0000-00-00 00:00:00' ? $created_up : $_now) . ' +' . intval($form['default_publish_down_days']) . ' days'));
         if (version_compare($version->getShortVersion(), '3.0', '>=')) {
             $created_up = $date->toSql();
         } else {
             $created_up = $date->toMySQL();
         }
     }
     $publish_up = $created_up;
     if (is_array($article) && isset($article['article_id']) && intval($form['default_publish_down_days']) != 0) {
         //$date = JFactory::getDate(strtotime( ($created_up !== null ? $created_up : $_now).' +'.intval($form['default_publish_down_days']).' days'));
         $date = JFactory::getDate(strtotime(($created_up !== null && $created_up != '0000-00-00 00:00:00' ? $created_up : $_now) . ' +' . intval($form['default_publish_down_days']) . ' days'));
         if (version_compare($version->getShortVersion(), '3.0', '>=')) {
             $created_down = $date->toSql();
         } else {
             $created_down = $date->toMySQL();
         }
     }
     $publish_down = $created_down;
     $featured = $form['default_featured'];
     $ignore_lang_code = '*';
     if ($form['default_lang_code_ignore']) {
         if (!$is15) {
             $db->setQuery("Select lang_code From #__languages Where published = 1 And sef = " . $db->Quote(JRequest::getCmd('lang', '')));
             $ignore_lang_code = $db->loadResult();
             if (!$ignore_lang_code) {
                 $ignore_lang_code = '*';
             }
         } else {
             $ignore_lang_code = '';
             $codes = self::getLanguageCodes();
             foreach ($codes as $code) {
                 if (strstr(strtolower($code), strtolower(trim(JRequest::getCmd('lang', 'xxxxx')))) !== false) {
                     $ignore_lang_code = $code;
                     break;
                 }
             }
         }
     }
     $language = $form['default_lang_code_ignore'] ? $ignore_lang_code : $form['default_lang_code'];
     if ($is15) {
         // an exception regarding limited options for language
         if (!isset($config['params'])) {
             $registry = new JRegistry();
             $attr = array('language' => $language);
             $registry->loadArray($attr);
             $attribs = (string) $registry->toString();
         }
     }
     $access = $form['default_access'];
     $ordering = 0;
     if (!$is15 && $full) {
         //$state = isset($config['state']) ? $config['state'] : $state;
         // change the state in the CB records as well if coming from article settings
         //if(isset($config['state'])){
         //    $db->setQuery("Update #__contentbuilder_records Set `published` = ".( intval($config['state'])  == 1 ? 1 : 0 )." Where reference_id = ".$db->Quote($form['reference_id'])." And record_id = " . $db->Quote($record_id));
         //    $db->query();
         //}
         // limited
         $alias = isset($config['alias']) ? $config['alias'] : $alias;
         // limited
         $form['default_category'] = isset($config['catid']) ? $config['catid'] : $form['default_category'];
         // limited
         $access = isset($config['access']) ? $config['access'] : $access;
         // limited
         $featured = isset($config['featured']) ? $config['featured'] : 0;
         // limited
         $language = isset($config['language']) ? $config['language'] : $language;
         if ($form['article_record_impact_language'] && isset($config['language'])) {
             $db->setQuery("Select sef From #__languages Where published = 1 And lang_code = " . $db->Quote($config['language']));
             $sef = $db->loadResult();
             if ($sef === null) {
                 $sef = '';
             }
             $db->setQuery("Update #__contentbuilder_records Set sef = " . $db->Quote($sef) . ", lang_code = " . $db->Quote($config['language']) . " Where `type` = " . $db->Quote($form['type']) . " And reference_id = " . $db->Quote($form['reference_id']) . " And record_id = " . $db->Quote($record_id));
             $db->query();
         }
         // limited
         $created_by_alias = isset($config['created_by_alias']) ? $config['created_by_alias'] : '';
         if ($form['article_record_impact_publish'] && isset($config['publish_up']) && $config['publish_up'] != $publish_up) {
             // check in strtotime due to php's different behavior on 64bit machines
             if (version_compare($version->getShortVersion(), '3.0', '>=')) {
                 $___now = $_now->toSql();
             } else {
                 $___now = $_now->toMySQL();
             }
             $db->setQuery("Update #__contentbuilder_records Set " . (strtotime($config['publish_up'] == '0000-00-00 00:00:00' ? '1976-03-07 22:10:00' : $config['publish_up']) >= strtotime($___now) ? 'published = 0, is_future = 1, ' : '') . " publish_up = " . $db->Quote($config['publish_up']) . " Where `type` = " . $db->Quote($form['type']) . " And reference_id = " . $db->Quote($form['reference_id']) . " And record_id = " . $db->Quote($record_id));
             $db->query();
         }
         // limited
         $publish_up = isset($config['publish_up']) ? $config['publish_up'] : $publish_up;
         if ($form['article_record_impact_publish'] && isset($config['publish_down']) && $config['publish_down'] != $publish_down) {
             if (version_compare($version->getShortVersion(), '3.0', '>=')) {
                 $___now = $_now->toSql();
             } else {
                 $___now = $_now->toMySQL();
             }
             $db->setQuery("Update #__contentbuilder_records Set " . (strtotime($config['publish_down'] == '0000-00-00 00:00:00' ? '1976-03-07 22:10:00' : $config['publish_down']) <= strtotime($___now) ? 'published = 0,' : '') . "  publish_down = " . $db->Quote($config['publish_down']) . " Where `type` = " . $db->Quote($form['type']) . " And reference_id = " . $db->Quote($form['reference_id']) . " And record_id = " . $db->Quote($record_id));
             $db->query();
         }
         // limited
         $publish_down = isset($config['publish_down']) ? $config['publish_down'] : $publish_down;
         // limited
         $metakey = isset($config['metakey']) ? $config['metakey'] : '';
         // limited
         $metadesc = isset($config['metadesc']) ? $config['metadesc'] : '';
         $robots = '';
         $author = '';
         $rights = '';
         $xreference = '';
         if (!$limited_options) {
             // FULL
             $created_article = isset($config['created']) ? $config['created'] : null;
             // FULL
             if (JFactory::getApplication()->isAdmin()) {
                 $created_by = isset($config['created_by']) ? $config['created_by'] : 0;
             }
             // FULL
             if (isset($config['attribs']) && is_array($config['attribs'])) {
                 $registry = new JRegistry();
                 $registry->loadArray($config['attribs']);
                 $attribs = (string) $registry;
             }
             // FULL
             if (isset($config['metadata']) && is_array($config['metadata'])) {
                 if (isset($config['metadata']['robots'])) {
                     $robots = $config['metadata']['robots'];
                 }
                 if (isset($config['metadata']['author'])) {
                     $author = $config['metadata']['author'];
                 }
                 if (isset($config['metadata']['rights'])) {
                     $rights = $config['metadata']['rights'];
                 }
                 if (isset($config['metadata']['xreference'])) {
                     $xreference = $config['metadata']['xreference'];
                 }
                 $registry = new JRegistry();
                 $registry->loadArray($config['metadata']);
                 $meta = (string) $registry;
             }
         }
         $db->setQuery("Update #__contentbuilder_records Set robots = " . $db->Quote($robots) . ", author = " . $db->Quote($author) . ", rights = " . $db->Quote($rights) . ", xreference = " . $db->Quote($xreference) . ", metakey = " . $db->Quote($metakey) . ", metadesc = " . $db->Quote($metadesc) . " Where `type` = " . $db->Quote($form['type']) . " And reference_id = " . $db->Quote($form['reference_id']) . " And record_id = " . $db->Quote($record_id));
         $db->query();
         // Trigger the onContentBeforeSave event.
         $isNew = true;
         $dispatcher = JDispatcher::getInstance();
         $table = JTable::getInstance('content');
         if ($article > 0) {
             $table->load($article);
             $isNew = false;
         }
         $dispatcher->trigger('onContentBeforeSave', array('com_content.article', &$table, $isNew));
     } else {
         if ($full) {
             //$state = isset($config['state']) ? $config['state'] : $state;
             // change the state in the CB records as well if coming from article settings
             //if(isset($config['state'])){
             //    $db->setQuery("Update #__contentbuilder_records Set `published` = ".( intval($config['state'])  == 1 ? 1 : 0 )." Where reference_id = ".$db->Quote($form['reference_id'])." And record_id = " . $db->Quote($record_id));
             //    $db->query();
             //}
             // limited
             $alias = isset($config['alias']) ? $config['alias'] : $alias;
             // limited
             $form['default_category'] = isset($config['catid']) ? $config['catid'] : $form['default_category'];
             // limited
             $form['default_section'] = isset($config['sectionid']) ? $config['sectionid'] : $form['default_section'];
             // limited
             $access = isset($config['details']) && isset($config['details']['access']) ? $config['details']['access'] : $access;
             // limited
             $created_by_alias = isset($config['details']) && isset($config['details']['created_by_alias']) ? $config['details']['created_by_alias'] : '';
             // limited
             $ordering = isset($config['ordering']) && isset($config['ordering']) ? intval($config['ordering']) : 0;
             if ($form['article_record_impact_publish'] && isset($config['details']) && isset($config['details']['publish_up']) && $config['details']['publish_up'] != $publish_up) {
                 if (version_compare($version->getShortVersion(), '3.0', '>=')) {
                     $___now = $_now->toSql();
                 } else {
                     $___now = $_now->toMySQL();
                 }
                 $db->setQuery("Update #__contentbuilder_records Set " . (strtotime($config['details']['publish_up'] == '0000-00-00 00:00:00' ? '1976-03-07 22:10:00' : $config['details']['publish_up']) >= strtotime($___now) ? 'published = 0,' : '') . " publish_up = " . $db->Quote($config['details']['publish_up']) . ", is_future = 1 Where `type` = " . $db->Quote($form['type']) . " And reference_id = " . $db->Quote($form['reference_id']) . " And record_id = " . $db->Quote($record_id));
                 $db->query();
             }
             // limited
             $publish_up = isset($config['details']) && isset($config['details']['publish_up']) ? $config['details']['publish_up'] == JText::_('Never') ? '0000-00-00 00:00:00' : $config['details']['publish_up'] : $publish_up;
             if ($form['article_record_impact_publish'] && isset($config['details']) && isset($config['details']['publish_down']) && $config['details']['publish_down'] != $publish_down) {
                 if (version_compare($version->getShortVersion(), '3.0', '>=')) {
                     $___now = $_now->toSql();
                 } else {
                     $___now = $_now->toMySQL();
                 }
                 $db->setQuery("Update #__contentbuilder_records Set " . (strtotime($config['details']['publish_down'] == '0000-00-00 00:00:00' ? '1976-03-07 22:10:00' : $config['details']['publish_down']) <= strtotime($___now) ? 'published = 0,' : '') . " publish_down = " . $db->Quote($config['details']['publish_down']) . " Where `type` = " . $db->Quote($form['type']) . " And reference_id = " . $db->Quote($form['reference_id']) . " And record_id = " . $db->Quote($record_id));
                 $db->query();
             }
             // limited
             $publish_down = isset($config['details']) && isset($config['details']['publish_down']) ? $config['details']['publish_down'] == JText::_('Never') ? '0000-00-00 00:00:00' : $config['details']['publish_down'] : $publish_down;
             // limited
             $metakey = isset($config['meta']) && isset($config['meta']['keywords']) ? $config['meta']['keywords'] : '';
             // limited
             $metadesc = isset($config['meta']) && isset($config['meta']['description']) ? $config['meta']['description'] : '';
             $robots = '';
             $author = '';
             $rights = '';
             // an exception regarding limited options for language
             if (!isset($config['params'])) {
                 $ex = explode('-', $language);
                 $sef = '';
                 if (count($ex)) {
                     $sef = strtolower($ex[0]);
                 }
                 if ($form['article_record_impact_language']) {
                     $db->setQuery("Update #__contentbuilder_records Set sef = " . $db->Quote($sef) . ", lang_code = " . $db->Quote($language) . " Where `type` = " . $db->Quote($form['type']) . " And reference_id = " . $db->Quote($form['reference_id']) . " And record_id = " . $db->Quote($record_id));
                     $db->query();
                 }
                 $registry = new JRegistry();
                 $attr = array('language' => $language);
                 $registry->loadArray($attr);
                 $attribs = (string) $registry->toString();
             }
             if (!$limited_options) {
                 // FULL
                 $created_by = isset($config['details']) && isset($config['details']['created_by']) ? $config['details']['created_by'] : 0;
                 // FULL
                 $created_article = isset($config['details']) && isset($config['details']['created']) ? $config['details']['created'] == JText::_('Never') ? '0000-00-00 00:00:00' : $config['details']['created'] : null;
                 // FULL
                 if (isset($config['params']) && is_array($config['params'])) {
                     if (isset($config['params']['language'])) {
                         if ($form['article_record_impact_language'] && $config['params']['language'] != $language) {
                             $ex = explode('-', $config['params']['language']);
                             $sef = '';
                             if (count($ex)) {
                                 $sef = strtolower($ex[0]);
                             }
                             $db->setQuery("Update #__contentbuilder_records Set sef = " . $db->Quote($sef) . ", lang_code = " . $db->Quote($config['params']['language']) . " Where `type` = " . $db->Quote($form['type']) . " And reference_id = " . $db->Quote($form['reference_id']) . " And record_id = " . $db->Quote($record_id));
                             $db->query();
                         } else {
                             $config['params']['language'] = $language;
                         }
                     }
                     $registry = new JRegistry();
                     $registry->loadArray($config['params']);
                     $attribs = (string) $registry->toString();
                 }
                 // FULL
                 if (isset($config['meta']) && is_array($config['meta'])) {
                     if (isset($config['meta']['robots'])) {
                         $robots = $config['meta']['robots'];
                     }
                     if (isset($config['meta']['author'])) {
                         $author = $config['meta']['author'];
                     }
                     if (isset($config['meta']['rights'])) {
                         $rights = $config['meta']['rights'];
                     }
                     $registry = new JRegistry();
                     $registry->loadArray($config['meta']);
                     $meta = (string) $registry->toString();
                 }
             }
             $db->setQuery("Update #__contentbuilder_records Set robots = " . $db->Quote($robots) . ", author = " . $db->Quote($author) . ", rights = " . $db->Quote($rights) . ", xreference = '', metakey = " . $db->Quote($metakey) . ", metadesc = " . $db->Quote($metadesc) . " Where `type` = " . $db->Quote($form['type']) . " And reference_id = " . $db->Quote($form['reference_id']) . " And record_id = " . $db->Quote($record_id));
             $db->query();
             $dispatcher = JDispatcher::getInstance();
             $isNew = true;
             $dispatcher = JDispatcher::getInstance();
             $table = JTable::getInstance('content');
             $isNew = true;
             if ($article > 0) {
                 $table->load($article);
                 $isNew = false;
             }
             $dispatcher->trigger('onBeforeContentSave', array(&$table, $isNew));
         }
     }
     $created_by = $created_by ? $created_by : $metadata->created_id;
     $date = JFactory::getDate();
     if (version_compare($version->getShortVersion(), '3.0', '>=')) {
         $created = $date->toSql();
     } else {
         $created = $date->toMySQL();
     }
     $created = $created_article ? $created_article : ($metadata->created ? $metadata->created : $created);
     if ($created && strlen(trim($created)) <= 10) {
         $created .= ' 00:00:00';
     }
     if (!$publish_up || $publish_up == '0000-00-00 00:00:00') {
         $publish_up = $created;
     }
     if (!$publish_down && !$article) {
         $publish_down = '0000-00-00 00:00:00';
     }
     $alias = $alias ? self::stringURLUnicodeSlug($alias) : self::stringURLUnicodeSlug($label);
     if (trim(str_replace('-', '', $alias)) == '') {
         $datenow = JFactory::getDate();
         if (version_compare($version->getShortVersion(), '3.0', '>=')) {
             $alias = $datenow->format("%Y-%m-%d-%H-%M-%S");
         } else {
             $alias = $datenow->toFormat("%Y-%m-%d-%H-%M-%S");
         }
     }
     // not existing, create
     if (!$article) {
         $db->setQuery("Insert Into \n                    #__content \n                        (\n                         `title`,\n                         `alias`,\n                         `introtext`,\n                         `fulltext`,\n                         `state`,\n                         " . ($is15 ? "`sectionid`," : '') . "\n                         `catid`,\n                         `created`,\n                         `created_by`,\n                         `modified`,\n                         `modified_by`,\n                         `checked_out`,\n                         `checked_out_time`,\n                         `publish_up`,\n                         `publish_down`,\n                         `attribs`,\n                         `version`,\n                         `metakey`,\n                         `metadesc`,\n                         `metadata`,\n                         `access`,\n                         `created_by_alias`,\n                         `ordering`\n                         " . (!$is15 ? ',featured' : '') . "\n                         " . (!$is15 ? ',language' : '') . "\n                         " . (!$is15 ? ',xreference' : '') . "\n                        ) \n                    Values \n                        (\n                          " . $db->Quote($label) . ",\n                          " . $db->Quote($alias) . ",\n                          " . $db->Quote($introtext) . ",\n                          " . $db->Quote($fulltext) . ",\n                          " . $db->Quote($state) . ",\n                          " . ($is15 ? intval($form['default_section']) . "," : '') . "\n                          " . intval($form['default_category']) . ",\n                          " . $db->Quote($created) . ",\n                          " . $db->Quote($created_by ? $created_by : JFactory::getUser()->get('id', 0)) . ",\n                          '0000-00-00 00:00:00',\n                          '0',\n                          '0',\n                          '0000-00-00 00:00:00',\n                          " . $db->Quote($publish_up) . ",\n                          " . $db->Quote($publish_down) . ",\n                          " . $db->Quote($attribs) . ",\n                          '1',\n                          " . $db->Quote($metakey) . ",\n                          " . $db->Quote($metadesc) . ",\n                          " . $db->Quote($meta) . ",\n                          " . $db->Quote($access) . ",\n                          " . $db->Quote($created_by_alias) . ",\n                          " . $db->Quote($ordering) . "\n                          " . (!$is15 ? ',' . $db->Quote($featured) : '') . "\n                          " . (!$is15 ? ',' . $db->Quote($language) : '') . "\n                          " . (!$is15 ? ',' . $db->Quote(is_array($config) && isset($config['metadata']) && is_array($config['metadata']) && isset($config['metadata']['xreference']) ? $config['metadata']['xreference'] : '') : '') . "\n                        )\n            ");
         $db->query();
         $article = $db->insertid();
         $datenow = JFactory::getDate();
         if (version_compare($version->getShortVersion(), '3.0', '>=')) {
             $___datenow = $datenow->toSql();
         } else {
             $___datenow = $datenow->toMySQL();
         }
         $db->setQuery("Insert Into #__contentbuilder_articles (`type`,`reference_id`,`last_update`,`article_id`,`record_id`,`form_id`) Values (" . $db->Quote($form['type']) . "," . $db->Quote($form['reference_id']) . "," . $db->Quote($___datenow) . ",{$article}," . $db->Quote($record_id) . "," . intval($contentbuilder_form_id) . ")");
         $db->query();
         $db->setQuery("Update #__content Set introtext = concat('<div style=\\'display:none;\\'><!--(cbArticleId:{$article})--></div>', introtext) Where id = {$article}");
         $db->query();
         // existing, update
     } else {
         $datenow = JFactory::getDate();
         if (version_compare($version->getShortVersion(), '3.0', '>=')) {
             $___datenow = $datenow->toSql();
         } else {
             $___datenow = $datenow->toMySQL();
         }
         $modified = $metadata->modified ? $metadata->modified : $___datenow;
         $modified_by = $metadata->modified_id ? $metadata->modified_id : JFactory::getUser()->get('id', 0);
         if ($full) {
             $db->setQuery("Update \n                        #__content \n                            Set\n                             `title` = " . $db->Quote($label) . ",\n                             `alias` = " . $db->Quote($alias) . ",\n                             `introtext` = " . $db->Quote('<div style=\'display:none;\'><!--(cbArticleId:' . $article . ')--></div>' . $introtext) . ",\n                             `fulltext` = " . $db->Quote($fulltext . '<div style=\'display:none;\'><!--(cbArticleId:' . $article . ')--></div>') . ",\n                             `state` = " . $db->Quote($state) . ",\n                             " . ($is15 ? "`sectionid` = " . intval($form['default_section']) . "," : '') . "\n                             `catid` = " . intval($form['default_category']) . ",\n                             `modified` = " . $db->Quote($modified) . ",\n                             `modified_by` = " . $db->Quote($modified_by ? $modified_by : JFactory::getUser()->get('id', 0)) . ",\n                             `attribs` = " . $db->Quote($attribs) . ",\n                             `metakey` = " . $db->Quote($metakey) . ",\n                             `metadesc` = " . $db->Quote($metadesc) . ",\n                             `metadata` = " . $db->Quote($meta) . ",\n                             `version` = `version`+1,\n                             `created` = " . $db->Quote($created) . ",\n                             `created_by` = " . $db->Quote($created_by) . ",\n                             `created_by_alias` = " . $db->Quote($created_by_alias) . ",\n                             `publish_up` = " . $db->Quote($publish_up) . ",\n                             `publish_down` = " . $db->Quote($publish_down) . ",\n                             `access` = " . $db->Quote($access) . ",\n                             `ordering` = " . $db->Quote($ordering) . "\n                             " . (!$is15 ? ',featured=' . $db->Quote($featured) : '') . "\n                             " . (!$is15 ? ',language=' . $db->Quote($language) : '') . "\n                             " . (!$is15 ? ',xreference=' . $db->Quote(is_array($config) && isset($config['metadata']) && is_array($config['metadata']) && isset($config['metadata']['xreference']) ? $config['metadata']['xreference'] : '') : '') . "\n                        Where id = {$article}\n                ");
         } else {
             $db->setQuery("Update \n                        #__content \n                            Set\n                             `title` = " . $db->Quote($label) . ",\n                             `alias` = " . $db->Quote($alias) . ",\n                             `introtext` = " . $db->Quote('<div style=\'display:none;\'><!--(cbArticleId:' . $article . ')--></div>' . $introtext) . ",\n                             `fulltext` = " . $db->Quote($fulltext . '<div style=\'display:none;\'><!--(cbArticleId:' . $article . ')--></div>') . ",\n                             `state` = " . $db->Quote($state) . ",\n                             `modified` = " . $db->Quote($modified) . ",\n                             `modified_by` = " . $db->Quote($modified_by ? $modified_by : JFactory::getUser()->get('id', 0)) . ",\n                             `version` = `version`+1\n                             " . (!$is15 ? ',language=' . $db->Quote($language) : '') . "\n                        Where id = {$article}\n                ");
         }
         $db->query();
         // here we do not limit to the form_id, as this is a record change and all articles should be notified
         if (version_compare($version->getShortVersion(), '3.0', '>=')) {
             $___datenow = $datenow->toSql();
         } else {
             $___datenow = $datenow->toMySQL();
         }
         $db->setQuery("Update #__contentbuilder_articles Set `last_update` = " . $db->Quote($___datenow) . " Where `type` = " . $db->Quote($form['type']) . " And form_id = " . intval($contentbuilder_form_id) . " And reference_id = " . $db->Quote($form['reference_id']) . " And record_id = " . $db->Quote($record_id));
         $db->query();
     }
     // Bind the rules.
     /*
             if ($full && !$is15 && $article && isset($config['rules']) && is_array($config['rules'])) {
                 
                 // Cleaning the rules
                 $new = array();
                 foreach($config['rules'] As $key => $the_rules){
                     foreach($the_rules As $key2 => $the_rule){
                         if(!isset($new[$key])){
                             $new[$key] = array();
                         }
                         if(trim($the_rule) != ''){
                             $new[$key][$key2] = intval($the_rule);
                         }
                     }
                 }
                 $config['rules'] = $new;
                 
                 $registry = new JRegistry();
                 $registry->loadArray($config['rules']);
                 $json_rules = (string) $registry;
                 
                 $cat_asset = JTable::getInstance('Asset');
                 $cat_asset->loadByName('com_content.category.' . $form['default_category']);
     
                 $article_asset = JTable::getInstance('Asset');
                 $article_asset->loadByName('com_content.article.' . $article);
                 
                 if($cat_asset->id != $article_asset->parent_id){
                     $article_asset->setLocation($cat_asset->id, 'last-child');
                 }
                 
                 $article_asset->name = 'com_content.article.'.$article;
                 $article_asset->rules = $json_rules;
                 $article_asset->title = $label;
                 
                 $article_asset->store();
                 
                 $db->setQuery("Update #__content Set asset_id = " . $article_asset->id. " Where id = " . $article);
                 $db->query();
             }*/
     if ($article && $is15) {
         $row = JTable::getInstance('content');
         if ($row->load($article)) {
             $row->reorder('sectionid = ' . (int) $form['default_section'] . ' And catid = ' . (int) $form['default_category'] . ' AND state >= 0');
         }
     } else {
         if ($article) {
             $row = JTable::getInstance('content');
             if ($row->load($article)) {
                 $row->reorder('catid = ' . (int) $form['default_category'] . ' AND state >= 0');
             }
         }
     }
     if (!$is15) {
         // cleaning cache
         // Trigger the onContentCleanCache event.
         $conf = JFactory::getConfig();
         $options = array('defaultgroup' => 'com_content', 'cachebase' => $conf->get('cache_path', JPATH_SITE . DS . 'cache'));
         $cache = JFactory::getCache('com_content');
         $cache->clean();
         $cache = JFactory::getCache('com_contentbuilder');
         $cache->clean();
         $dispatcher = JDispatcher::getInstance();
         $dispatcher->trigger('onContentCleanCache', $options);
         //// trigger onContentAfterSave event
         $isNew = true;
         $dispatcher = JDispatcher::getInstance();
         $table = JTable::getInstance('content');
         if ($article > 0) {
             $table->load($article);
             $isNew = false;
         }
         $dispatcher->trigger('onContentAfterSave', array('com_content.article', &$table, $isNew));
     } else {
         //if($article && $full){
         $frontpage = isset($config['frontpage']) ? $config['frontpage'] : $featured;
         if ($frontpage !== null) {
             if (!$frontpage) {
                 $query = 'Delete From #__content_frontpage Where content_id = ' . (int) $article;
                 $db->setQuery($query);
                 $db->query();
             } else {
                 $query = 'Select content_id From #__content_frontpage Where content_id = ' . (int) $article;
                 $db->setQuery($query);
                 if (!$db->loadResult()) {
                     $query = 'Insert Into #__content_frontpage (content_id) Values (' . (int) $article . ')';
                     $db->setQuery($query);
                     $db->query();
                 }
             }
             JTable::addIncludePath(JPATH_SITE . DS . 'administrator' . DS . 'components' . DS . 'com_frontpage' . DS . 'tables');
             $fp = JTable::getInstance('frontpage', 'Table');
             $fp->reorder();
         }
         //}
         $cache = JFactory::getCache('com_content');
         $cache->clean();
         $cache = JFactory::getCache('com_contentbuilder');
         $cache->clean();
         $isNew = true;
         $table = JTable::getInstance('content');
         if ($article > 0) {
             $table->load($article);
             $isNew = false;
         }
         $dispatcher = JDispatcher::getInstance();
         $dispatcher->trigger('onAfterContentSave', array(&$table, $isNew));
     }
     JPluginHelper::importPlugin('contentbuilder_listaction');
     $dispatcher = JDispatcher::getInstance();
     $result = $dispatcher->trigger('onAfterArticleCreation', array($contentbuilder_form_id, $record_id, $article));
     $msg = implode('', $result);
     if ($msg) {
         JFactory::getApplication()->enqueueMessage($msg);
     }
     return $article;
 }
Exemple #3
0
 function store()
 {
     JRequest::checkToken('default') or jexit(JText::_('JInvalid_Token'));
     JPluginHelper::importPlugin('contentbuilder_submit');
     $submit_dispatcher = JDispatcher::getInstance();
     JFactory::getSession()->clear('cb_failed_values', 'com_contentbuilder.' . $this->_id);
     JRequest::setVar('cb_submission_failed', 0);
     $query = $this->_buildQuery();
     $this->_data = $this->_getList($query, 0, 1);
     if (!count($this->_data)) {
         JError::raiseError(404, JText::_('COM_CONTENTBUILDER_FORM_NOT_FOUND'));
     }
     foreach ($this->_data as $data) {
         if (!$this->frontend && $data->display_in == 0) {
             JError::raiseError(404, JText::_('COM_CONTENTBUILDER_RECORD_NOT_FOUND'));
         } else {
             if ($this->frontend && $data->display_in == 1) {
                 JError::raiseError(404, JText::_('COM_CONTENTBUILDER_RECORD_NOT_FOUND'));
             }
         }
         $data->form_id = $this->_id;
         if ($data->type && $data->reference_id) {
             $values = array();
             $data->form = contentbuilder::getForm($data->type, $data->reference_id);
             $meta = $data->form->getRecordMetadata($this->_record_id);
             if (!$data->edit_by_type) {
                 $noneditable_fields = contentbuilder::getListNonEditableElements($this->_id);
                 $names = $data->form->getElementNames();
                 $this->_db->setQuery("Select * From #__contentbuilder_elements Where form_id = " . $this->_id . " And published = 1 And editable = 1");
                 $fields = $this->_db->loadAssocList();
                 $the_fields = array();
                 $the_name_field = null;
                 $the_username_field = null;
                 $the_password_field = null;
                 $the_password_repeat_field = null;
                 $the_email_field = null;
                 $the_email_repeat_field = null;
                 $the_html_fields = array();
                 $the_upload_fields = array();
                 $the_captcha_field = null;
                 $the_failed_registration_fields = array();
                 jimport('joomla.filesystem.file');
                 jimport('joomla.filesystem.folder');
                 foreach ($fields as $special_field) {
                     switch ($special_field['type']) {
                         case 'text':
                         case 'upload':
                         case 'captcha':
                         case 'textarea':
                             if ($special_field['type'] == 'upload') {
                                 $options = unserialize(base64_decode($special_field['options']));
                                 $special_field['options'] = $options;
                                 $the_upload_fields[$special_field['reference_id']] = $special_field;
                             } else {
                                 if ($special_field['type'] == 'captcha') {
                                     $options = unserialize(base64_decode($special_field['options']));
                                     $special_field['options'] = $options;
                                     $the_captcha_field = $special_field;
                                 } else {
                                     if ($special_field['type'] == 'textarea') {
                                         $options = unserialize(base64_decode($special_field['options']));
                                         $special_field['options'] = $options;
                                         if (isset($special_field['options']->allow_html) && $special_field['options']->allow_html) {
                                             $the_html_fields[$special_field['reference_id']] = $special_field;
                                         } else {
                                             $the_fields[$special_field['reference_id']] = $special_field;
                                         }
                                     } else {
                                         if ($special_field['type'] == 'text') {
                                             $options = unserialize(base64_decode($special_field['options']));
                                             $special_field['options'] = $options;
                                             if ($data->act_as_registration && $data->registration_username_field == $special_field['reference_id']) {
                                                 $the_username_field = $special_field;
                                             } else {
                                                 if ($data->act_as_registration && $data->registration_name_field == $special_field['reference_id']) {
                                                     $the_name_field = $special_field;
                                                 } else {
                                                     if ($data->act_as_registration && $data->registration_password_field == $special_field['reference_id']) {
                                                         $the_password_field = $special_field;
                                                     } else {
                                                         if ($data->act_as_registration && $data->registration_password_repeat_field == $special_field['reference_id']) {
                                                             $the_password_repeat_field = $special_field;
                                                         } else {
                                                             if ($data->act_as_registration && $data->registration_email_field == $special_field['reference_id']) {
                                                                 $the_email_field = $special_field;
                                                             } else {
                                                                 if ($data->act_as_registration && $data->registration_email_repeat_field == $special_field['reference_id']) {
                                                                     $the_email_repeat_field = $special_field;
                                                                 } else {
                                                                     $the_fields[$special_field['reference_id']] = $special_field;
                                                                 }
                                                             }
                                                         }
                                                     }
                                                 }
                                             }
                                         }
                                     }
                                 }
                             }
                             break;
                         default:
                             $options = unserialize(base64_decode($special_field['options']));
                             $special_field['options'] = $options;
                             $the_fields[$special_field['reference_id']] = $special_field;
                     }
                 }
                 // we have defined a captcha, so let's test it
                 if ($the_captcha_field !== null && !in_array($the_captcha_field['reference_id'], $noneditable_fields)) {
                     if (!class_exists('Securimage')) {
                         require_once JPATH_SITE . DS . 'components' . DS . 'com_contentbuilder' . DS . 'images' . DS . 'securimage' . DS . 'securimage.php';
                     }
                     $securimage = new Securimage();
                     $cap_value = JRequest::getVar('cb_' . $the_captcha_field['reference_id'], null, 'POST');
                     if ($securimage->check($cap_value) == false) {
                         JRequest::setVar('cb_submission_failed', 1);
                         JFactory::getApplication()->enqueueMessage(JText::_('COM_CONTENTBUILDER_CAPTCHA_FAILED'), 'error');
                     }
                     $values[$the_captcha_field['reference_id']] = $cap_value;
                     $noneditable_fields[] = $the_captcha_field['reference_id'];
                 }
                 // now let us see if we have a registration
                 // make sure to wait for previous errors
                 if ($data->act_as_registration && $the_name_field !== null && $the_email_field !== null && $the_email_repeat_field !== null && $the_password_field !== null && $the_password_repeat_field !== null && $the_username_field !== null) {
                     $pw1 = JRequest::getVar('cb_' . $the_password_field['reference_id'], '', 'POST', 'STRING', JREQUEST_NOTRIM | JREQUEST_ALLOWRAW);
                     $pw2 = JRequest::getVar('cb_' . $the_password_repeat_field['reference_id'], '', 'POST', 'STRING', JREQUEST_NOTRIM | JREQUEST_ALLOWRAW);
                     $email = JRequest::getVar('cb_' . $the_email_field['reference_id'], '', 'POST', 'STRING', JREQUEST_ALLOWRAW);
                     $email2 = JRequest::getVar('cb_' . $the_email_repeat_field['reference_id'], '', 'POST', 'STRING', JREQUEST_ALLOWRAW);
                     $name = JRequest::getVar('cb_' . $the_name_field['reference_id'], '', 'POST', 'STRING', JREQUEST_ALLOWRAW);
                     $username = JRequest::getVar('cb_' . $the_username_field['reference_id'], '', 'POST', 'STRING', JREQUEST_NOTRIM | JREQUEST_ALLOWRAW);
                     if (!JRequest::getVar('cb_submission_failed', 0)) {
                         if (!trim($name)) {
                             JRequest::setVar('cb_submission_failed', 1);
                             JFactory::getApplication()->enqueueMessage(JText::_('COM_CONTENTBUILDER_NAME_EMPTY'), 'error');
                         }
                         if (!trim($username)) {
                             JRequest::setVar('cb_submission_failed', 1);
                             JFactory::getApplication()->enqueueMessage(JText::_('COM_CONTENTBUILDER_USERNAME_EMPTY'), 'error');
                         } else {
                             if (preg_match("#[<>\"'%;()&]#i", $username) || strlen(utf8_decode($username)) < 2) {
                                 JRequest::setVar('cb_submission_failed', 1);
                                 JFactory::getApplication()->enqueueMessage(JText::_('COM_CONTENTBUILDER_USERNAME_INVALID'), 'error');
                             }
                         }
                         if (!trim($email)) {
                             JRequest::setVar('cb_submission_failed', 1);
                             JFactory::getApplication()->enqueueMessage(JText::_('COM_CONTENTBUILDER_EMAIL_EMPTY'), 'error');
                         } else {
                             if (!contentbuilder_is_email($email)) {
                                 JRequest::setVar('cb_submission_failed', 1);
                                 JFactory::getApplication()->enqueueMessage(JText::_('COM_CONTENTBUILDER_EMAIL_INVALID'), 'error');
                             } else {
                                 if ($email != $email2) {
                                     JRequest::setVar('cb_submission_failed', 1);
                                     JFactory::getApplication()->enqueueMessage(JText::_('COM_CONTENTBUILDER_EMAIL_MISMATCH'), 'error');
                                 }
                             }
                         }
                         if (!$meta->created_id && !JFactory::getUser()->get('id', 0)) {
                             $this->_db->setQuery("Select count(id) From #__users Where `username` = " . $this->_db->Quote($username));
                             if ($this->_db->loadResult()) {
                                 JRequest::setVar('cb_submission_failed', 1);
                                 JFactory::getApplication()->enqueueMessage(JText::_('COM_CONTENTBUILDER_USERNAME_NOT_AVAILABLE'), 'error');
                             }
                             $this->_db->setQuery("Select count(id) From #__users Where `email` = " . $this->_db->Quote($email));
                             if ($this->_db->loadResult()) {
                                 JRequest::setVar('cb_submission_failed', 1);
                                 JFactory::getApplication()->enqueueMessage(JText::_('COM_CONTENTBUILDER_EMAIL_NOT_AVAILABLE'), 'error');
                             }
                             if ($pw1 != $pw2) {
                                 JRequest::setVar('cb_submission_failed', 1);
                                 JFactory::getApplication()->enqueueMessage(JText::_('COM_CONTENTBUILDER_PASSWORD_MISMATCH'), 'error');
                                 JRequest::setVar('cb_' . $the_password_field['reference_id'], '');
                                 JRequest::setVar('cb_' . $the_password_repeat_field['reference_id'], '');
                             } else {
                                 if (!trim($pw1)) {
                                     JRequest::setVar('cb_submission_failed', 1);
                                     JFactory::getApplication()->enqueueMessage(JText::_('COM_CONTENTBUILDER_PASSWORD_EMPTY'), 'error');
                                     JRequest::setVar('cb_' . $the_password_field['reference_id'], '');
                                     JRequest::setVar('cb_' . $the_password_repeat_field['reference_id'], '');
                                 }
                             }
                         } else {
                             if ($meta->created_id && $meta->created_id != JFactory::getUser()->get('id', 0)) {
                                 $this->_db->setQuery("Select count(id) From #__users Where id <> " . $this->_db->Quote($meta->created_id) . " And `username` = " . $this->_db->Quote($username));
                                 if ($this->_db->loadResult()) {
                                     JRequest::setVar('cb_submission_failed', 1);
                                     JFactory::getApplication()->enqueueMessage(JText::_('COM_CONTENTBUILDER_USERNAME_NOT_AVAILABLE'), 'error');
                                 }
                                 $this->_db->setQuery("Select count(id) From #__users Where id <> " . $this->_db->Quote($meta->created_id) . " And `email` = " . $this->_db->Quote($email));
                                 if ($this->_db->loadResult()) {
                                     JRequest::setVar('cb_submission_failed', 1);
                                     JFactory::getApplication()->enqueueMessage(JText::_('COM_CONTENTBUILDER_EMAIL_NOT_AVAILABLE'), 'error');
                                 }
                             } else {
                                 $this->_db->setQuery("Select count(id) From #__users Where id <> " . $this->_db->Quote(JFactory::getUser()->get('id', 0)) . " And `username` = " . $this->_db->Quote($username));
                                 if ($this->_db->loadResult()) {
                                     JRequest::setVar('cb_submission_failed', 1);
                                     JFactory::getApplication()->enqueueMessage(JText::_('COM_CONTENTBUILDER_USERNAME_NOT_AVAILABLE'), 'error');
                                 }
                                 $this->_db->setQuery("Select count(id) From #__users Where id <> " . $this->_db->Quote(JFactory::getUser()->get('id', 0)) . " And `email` = " . $this->_db->Quote($email));
                                 if ($this->_db->loadResult()) {
                                     JRequest::setVar('cb_submission_failed', 1);
                                     JFactory::getApplication()->enqueueMessage(JText::_('COM_CONTENTBUILDER_EMAIL_NOT_AVAILABLE'), 'error');
                                 }
                             }
                             if (trim($pw1) != '' || trim($pw2) != '') {
                                 if ($pw1 != $pw2) {
                                     JRequest::setVar('cb_submission_failed', 1);
                                     JFactory::getApplication()->enqueueMessage(JText::_('COM_CONTENTBUILDER_PASSWORD_MISMATCH'), 'error');
                                     JRequest::setVar('cb_' . $the_password_field['reference_id'], '');
                                     JRequest::setVar('cb_' . $the_password_repeat_field['reference_id'], '');
                                 } else {
                                     if (!trim($pw1)) {
                                         JRequest::setVar('cb_submission_failed', 1);
                                         JFactory::getApplication()->enqueueMessage(JText::_('COM_CONTENTBUILDER_PASSWORD_EMPTY'), 'error');
                                         JRequest::setVar('cb_' . $the_password_field['reference_id'], '');
                                         JRequest::setVar('cb_' . $the_password_repeat_field['reference_id'], '');
                                     }
                                 }
                             }
                         }
                         if (!JRequest::getVar('cb_submission_failed', 0)) {
                             //$noneditable_fields[] = $the_name_field['reference_id'];
                             $noneditable_fields[] = $the_password_field['reference_id'];
                             $noneditable_fields[] = $the_password_repeat_field['reference_id'];
                             //$noneditable_fields[] = $the_email_field['reference_id'];
                             $noneditable_fields[] = $the_email_repeat_field['reference_id'];
                             //$noneditable_fields[] = $the_username_field['reference_id'];
                         } else {
                             $the_failed_registration_fields[$the_name_field['reference_id']] = $the_name_field;
                             //$the_failed_registration_fields[$the_password_field['reference_id']] = $the_password_field;
                             //$the_failed_registration_fields[$the_password_repeat_field['reference_id']] = $the_password_repeat_field;
                             $the_failed_registration_fields[$the_email_field['reference_id']] = $the_email_field;
                             $the_failed_registration_fields[$the_email_repeat_field['reference_id']] = $the_email_repeat_field;
                             $the_failed_registration_fields[$the_username_field['reference_id']] = $the_username_field;
                         }
                     } else {
                         $the_failed_registration_fields[$the_name_field['reference_id']] = $the_name_field;
                         //$the_failed_registration_fields[$the_password_field['reference_id']] = $the_password_field;
                         //$the_failed_registration_fields[$the_password_repeat_field['reference_id']] = $the_password_repeat_field;
                         $the_failed_registration_fields[$the_email_field['reference_id']] = $the_email_field;
                         $the_failed_registration_fields[$the_email_repeat_field['reference_id']] = $the_email_repeat_field;
                         $the_failed_registration_fields[$the_username_field['reference_id']] = $the_username_field;
                     }
                 }
                 $form_elements_objects = array();
                 $_items = $data->form->getRecord($this->_record_id, $data->published_only, $this->frontend ? $data->own_only_fe ? JFactory::getUser()->get('id', 0) : -1 : ($data->own_only ? JFactory::getUser()->get('id', 0) : -1), $this->frontend ? $data->show_all_languages_fe : true);
                 // asigning the proper names first
                 foreach ($names as $id => $name) {
                     if (!in_array($id, $noneditable_fields)) {
                         $value = '';
                         $is_array = 'STRING';
                         if (is_array(JRequest::getVar('cb_' . $id, ''))) {
                             $is_array = 'ARRAY';
                         }
                         if (isset($the_fields[$id]['options']->allow_raw) && $the_fields[$id]['options']->allow_raw) {
                             $value = JRequest::getVar('cb_' . $id, '', 'POST', $is_array, JREQUEST_ALLOWRAW);
                         } else {
                             if (isset($the_fields[$id]['options']->allow_html) && $the_fields[$id]['options']->allow_html) {
                                 $value = JRequest::getVar('cb_' . $id, '', 'POST', $is_array, JREQUEST_ALLOWHTML);
                             } else {
                                 $value = JRequest::getVar('cb_' . $id, '', 'POST', $is_array);
                             }
                         }
                         if (isset($the_fields[$id]['options']->transfer_format)) {
                             $value = contentbuilder_convert_date($value, $the_fields[$id]['options']->format, $the_fields[$id]['options']->transfer_format);
                         }
                         if (isset($the_html_fields[$id])) {
                             $the_html_fields[$id]['name'] = $name;
                             $the_html_fields[$id]['value'] = $value;
                         } else {
                             if (isset($the_failed_registration_fields[$id])) {
                                 $the_failed_registration_fields[$id]['name'] = $name;
                                 $the_failed_registration_fields[$id]['value'] = $value;
                             } else {
                                 if (isset($the_upload_fields[$id])) {
                                     $the_upload_fields[$id]['name'] = $name;
                                     $the_upload_fields[$id]['value'] = '';
                                     $the_upload_fields[$id]['orig_value'] = '';
                                     if ($id == $the_upload_fields[$id]['reference_id']) {
                                         // delete if triggered
                                         if (JRequest::getInt('cb_delete_' . $id, 0) == 1 && isset($the_upload_fields[$id]['validations']) && $the_upload_fields[$id]['validations'] == '') {
                                             if (count($_items)) {
                                                 foreach ($_items as $_item) {
                                                     if ($_item->recElementId == $the_upload_fields[$id]['reference_id']) {
                                                         $_value = $_item->recValue;
                                                         $_files = explode("\n", str_replace("\r", '', $_value));
                                                         foreach ($_files as $_file) {
                                                             if (strpos(strtolower($_file), '{cbsite}') === 0) {
                                                                 $_file = str_replace(array('{cbsite}', '{CBSite}'), array(JPATH_SITE, JPATH_SITE), $_file);
                                                             }
                                                             if (JFile::exists($_file)) {
                                                                 JFile::delete($_file);
                                                             }
                                                             $values[$id] = '';
                                                         }
                                                     }
                                                 }
                                             }
                                         }
                                         $file = JRequest::getVar('cb_' . $id, null, 'files', 'array');
                                         if (trim(JFile::makeSafe($file['name'])) != '' && $file['size'] > 0) {
                                             $filename = trim(JFile::makeSafe($file['name']));
                                             $infile = $filename;
                                             $src = $file['tmp_name'];
                                             $dest = '';
                                             $tmp_dest = '';
                                             $tmp_upload_field_dir = '';
                                             $tmp_upload_dir = '';
                                             if (isset($the_upload_fields[$id]['options']) && isset($the_upload_fields[$id]['options']->upload_directory) && $the_upload_fields[$id]['options']->upload_directory != '') {
                                                 $tmp_upload_field_dir = $the_upload_fields[$id]['options']->upload_directory;
                                                 $tmp_dest = $tmp_upload_field_dir;
                                             } else {
                                                 if ($data->upload_directory != '') {
                                                     $tmp_upload_dir = $data->upload_directory;
                                                     $tmp_dest = $tmp_upload_dir;
                                                 }
                                             }
                                             if (isset($the_upload_fields[$id]['options']) && isset($the_upload_fields[$id]['options']->upload_directory) && $the_upload_fields[$id]['options']->upload_directory != '') {
                                                 $dest = str_replace(array('{CBSite}', '{cbsite}'), JPATH_SITE, $the_upload_fields[$id]['options']->upload_directory);
                                             } else {
                                                 if ($data->upload_directory != '') {
                                                     $dest = str_replace(array('{CBSite}', '{cbsite}'), JPATH_SITE, $data->upload_directory);
                                                 }
                                             }
                                             // create dest path by tokens
                                             $dest = $this->createPathByTokens($dest, $names);
                                             $msg = '';
                                             $uploaded = false;
                                             // FILE SIZE TEST
                                             if ($dest != '' && isset($the_upload_fields[$id]['options']) && isset($the_upload_fields[$id]['options']->max_filesize) && $the_upload_fields[$id]['options']->max_filesize > 0) {
                                                 $val = $the_upload_fields[$id]['options']->max_filesize;
                                                 $val = trim($val);
                                                 $last = strtolower($val[strlen($val) - 1]);
                                                 switch ($last) {
                                                     case 'g':
                                                         $val *= 1024;
                                                     case 'm':
                                                         $val *= 1024;
                                                     case 'k':
                                                         $val *= 1024;
                                                 }
                                                 if ($file['size'] > $val) {
                                                     $msg = JText::_('COM_CONTENTBUILDER_FILESIZE_EXCEEDED') . ' ' . $the_upload_fields[$id]['options']->max_filesize . 'b';
                                                 }
                                             }
                                             // FILE EXT TEST
                                             if ($dest != '' && isset($the_upload_fields[$id]['options']) && isset($the_upload_fields[$id]['options']->allowed_file_extensions) && $the_upload_fields[$id]['options']->allowed_file_extensions != '') {
                                                 $allowed = explode(',', str_replace(' ', '', strtolower($the_upload_fields[$id]['options']->allowed_file_extensions)));
                                                 $ext = strtolower(JFile::getExt($filename));
                                                 if (!in_array($ext, $allowed)) {
                                                     $msg = JText::_('COM_CONTENTBUILDER_FILE_EXTENSION_NOT_ALLOWED');
                                                 }
                                             }
                                             // UPLOAD
                                             if ($dest != '' && $msg == '') {
                                                 // limit file's name size
                                                 $ext = strtolower(JFile::getExt($filename));
                                                 $stripped = JFile::stripExt($filename);
                                                 // in some apache configurations unknown file extensions could lead to security risks
                                                 // because it will try to find an executable extensions within the chain of dots. So we simply remove them.
                                                 $filename = str_replace(array(' ', '.'), '_', $stripped) . '.' . $ext;
                                                 $maxnamesize = 100;
                                                 if (function_exists('mb_strlen')) {
                                                     if (mb_strlen($filename) > $maxnamesize) {
                                                         $filename = mb_substr($filename, mb_strlen($filename) - $maxnamesize);
                                                     }
                                                 } else {
                                                     if (strlen($filename) > $maxnamesize) {
                                                         $filename = substr($filename, strlen($filename) - $maxnamesize);
                                                     }
                                                 }
                                                 // take care of existing filenames
                                                 if (JFile::exists($dest . DS . $filename)) {
                                                     $filename = md5(mt_rand(0, mt_getrandmax()) . time()) . '_' . $filename;
                                                 }
                                                 // create pseudo security index.html
                                                 if (!JFile::exists($dest . DS . 'index.html')) {
                                                     JFile::write($dest . DS . 'index.html', $buffer = '');
                                                 }
                                                 if (count($_items)) {
                                                     $files_to_delete = array();
                                                     foreach ($_items as $_item) {
                                                         if ($_item->recElementId == $the_upload_fields[$id]['reference_id']) {
                                                             $_value = $_item->recValue;
                                                             $_files = explode("\n", str_replace("\r", '', $_value));
                                                             foreach ($_files as $_file) {
                                                                 if (strpos(strtolower($_file), '{cbsite}') === 0) {
                                                                     $_file = str_replace(array('{cbsite}', '{CBSite}'), array(JPATH_SITE, JPATH_SITE), $_file);
                                                                 }
                                                                 $files_to_delete[] = $_file;
                                                             }
                                                             break;
                                                         }
                                                     }
                                                     foreach ($files_to_delete as $file_to_delete) {
                                                         if (JFile::exists($file_to_delete)) {
                                                             JFile::delete($file_to_delete);
                                                         }
                                                     }
                                                 }
                                                 // final upload file moving
                                                 $uploaded = JFile::upload($src, $dest . DS . $filename);
                                                 if (!$uploaded) {
                                                     $msg = JText::_('COM_CONTENTBUILDER_UPLOAD_FAILED');
                                                 }
                                             }
                                             if ($dest == '' || $uploaded !== true) {
                                                 JRequest::setVar('cb_submission_failed', 1);
                                                 JFactory::getApplication()->enqueueMessage($msg . ' (' . $infile . ')', 'error');
                                                 $the_upload_fields[$id]['value'] = '';
                                             } else {
                                                 if (strpos(strtolower($tmp_dest), '{cbsite}') === 0) {
                                                     $dest = str_replace(array(JPATH_SITE, JPATH_SITE), array('{cbsite}', '{CBSite}'), $dest);
                                                 }
                                                 $values[$id] = $dest . DS . $filename;
                                                 $the_upload_fields[$id]['value'] = $values[$id];
                                             }
                                             $the_upload_fields[$id]['orig_value'] = JFile::makeSafe($file['name']);
                                         }
                                         if (trim($the_upload_fields[$id]['custom_validation_script'])) {
                                             $msg = self::customValidate(trim($the_upload_fields[$id]['custom_validation_script']), $the_upload_fields[$id], $merged = array_merge($the_upload_fields, $the_fields, $the_html_fields), JRequest::getCmd('record_id', ''), $data->form, isset($values[$id]) ? $values[$id] : '');
                                             $msg = trim($msg);
                                             if (!empty($msg)) {
                                                 JRequest::setVar('cb_submission_failed', 1);
                                                 JFactory::getApplication()->enqueueMessage(trim($msg), 'error');
                                             }
                                         }
                                         $removables = array();
                                         $validations = explode(',', $the_upload_fields[$id]['validations']);
                                         foreach ($validations as $validation) {
                                             $plgs = CBPluginHelper::importPlugin('contentbuilder_validation', $validation);
                                             $removables = array_merge($removables, $plgs);
                                         }
                                         $dispatcher = JDispatcher::getInstance();
                                         $results = $dispatcher->trigger('onValidate', array($the_upload_fields[$id], $merged = array_merge($the_upload_fields, $the_fields, $the_html_fields), JRequest::getCmd('record_id', ''), $data->form, isset($values[$id]) ? $values[$id] : ''));
                                         foreach ($removables as $removable) {
                                             $dispatcher->detach($removable);
                                         }
                                         $all_errors = implode('', $results);
                                         if (!empty($all_errors)) {
                                             if (isset($values[$id]) && JFile::exists($values[$id])) {
                                                 JFile::delete($values[$id]);
                                             }
                                             JRequest::setVar('cb_submission_failed', 1);
                                             foreach ($results as $result) {
                                                 $result = trim($result);
                                                 if (!empty($result)) {
                                                     JFactory::getApplication()->enqueueMessage(trim($result), 'error');
                                                 }
                                             }
                                         }
                                     }
                                 } else {
                                     if (isset($the_fields[$id])) {
                                         $the_fields[$id]['name'] = $name;
                                         $the_fields[$id]['value'] = $value;
                                     }
                                 }
                             }
                         }
                     }
                 }
                 foreach ($names as $id => $name) {
                     if (!in_array($id, $noneditable_fields)) {
                         if (isset($the_upload_fields[$id]) && $id == $the_upload_fields[$id]['reference_id']) {
                             // nothing, done above already
                         } else {
                             $f = null;
                             if (isset($the_html_fields[$id])) {
                                 $value = JRequest::getVar('cb_' . $id, '', 'POST', 'STRING', JREQUEST_ALLOWHTML);
                                 $f = $the_html_fields[$id];
                                 $the_html_fields[$id]['value'] = $value;
                             }
                             if (isset($the_failed_registration_fields[$id])) {
                                 $value = JRequest::getVar('cb_' . $id, '', 'POST', 'STRING', JREQUEST_NOTRIM | JREQUEST_ALLOWRAW);
                                 $f = $the_failed_registration_fields[$id];
                                 $the_failed_registration_fields[$id]['value'] = $value;
                             }
                             if (isset($the_fields[$id])) {
                                 $is_array = 'STRING';
                                 if (is_array(JRequest::getVar('cb_' . $id, ''))) {
                                     $is_array = 'ARRAY';
                                 }
                                 if (isset($the_fields[$id]['options']->allow_raw) && $the_fields[$id]['options']->allow_raw) {
                                     $value = JRequest::getVar('cb_' . $id, '', 'POST', $is_array, JREQUEST_ALLOWRAW);
                                 } else {
                                     if (isset($the_fields[$id]['options']->allow_html) && $the_fields[$id]['options']->allow_html) {
                                         $value = JRequest::getVar('cb_' . $id, '', 'POST', $is_array, JREQUEST_ALLOWHTML);
                                     } else {
                                         $value = JRequest::getVar('cb_' . $id, '', 'POST', $is_array);
                                     }
                                 }
                                 if (isset($the_fields[$id]['options']->transfer_format)) {
                                     $value = contentbuilder_convert_date($value, $the_fields[$id]['options']->format, $the_fields[$id]['options']->transfer_format);
                                 }
                                 $f = $the_fields[$id];
                                 $the_fields[$id]['value'] = $value;
                             }
                             if ($f !== null) {
                                 if (trim($f['custom_validation_script'])) {
                                     $msg = self::customValidate(trim($f['custom_validation_script']), $f, $merged = array_merge($the_upload_fields, $the_fields, $the_html_fields), JRequest::getCmd('record_id', ''), $data->form, $value);
                                     $msg = trim($msg);
                                     if (!empty($msg)) {
                                         JRequest::setVar('cb_submission_failed', 1);
                                         JFactory::getApplication()->enqueueMessage(trim($msg), 'error');
                                     }
                                 }
                                 $removables = array();
                                 $validations = explode(',', $f['validations']);
                                 foreach ($validations as $validation) {
                                     $plgs = CBPluginHelper::importPlugin('contentbuilder_validation', $validation);
                                     $removables = array_merge($removables, $plgs);
                                 }
                                 $dispatcher = JDispatcher::getInstance();
                                 $results = $dispatcher->trigger('onValidate', array($f, $merged = array_merge($the_upload_fields, $the_fields, $the_html_fields), JRequest::getCmd('record_id', ''), $data->form, $value));
                                 foreach ($removables as $removable) {
                                     $dispatcher->detach($removable);
                                 }
                                 $all_errors = implode('', $results);
                                 $values[$id] = $value;
                                 if (!empty($all_errors)) {
                                     JRequest::setVar('cb_submission_failed', 1);
                                     foreach ($results as $result) {
                                         $result = trim($result);
                                         if (!empty($result)) {
                                             JFactory::getApplication()->enqueueMessage(trim($result), 'error');
                                         }
                                     }
                                 } else {
                                     $removables = array();
                                     $plgs = CBPluginHelper::importPlugin('contentbuilder_form_elements', $f['type']);
                                     $removables = array_merge($removables, $plgs);
                                     $dispatcher = JDispatcher::getInstance();
                                     $plugin_validations = $dispatcher->trigger('onAfterValidationSuccess', array($f, $m = array_merge($the_upload_fields, $the_fields, $the_html_fields), JRequest::getCmd('record_id', ''), $data->form, $value));
                                     if (count($plugin_validations)) {
                                         $form_elements_objects[] = $plugin_validations[0];
                                     }
                                     foreach ($removables as $removable) {
                                         $dispatcher->detach($removable);
                                     }
                                 }
                             }
                         }
                     }
                 }
                 $submit_before_result = $submit_dispatcher->trigger('onBeforeSubmit', array(JRequest::getCmd('record_id', ''), $data->form, $values));
                 if (JRequest::getVar('cb_submission_failed', 0)) {
                     JFactory::getSession()->set('cb_failed_values', $values, 'com_contentbuilder.' . $this->_id);
                     return JRequest::getCmd('record_id', '');
                 }
                 $record_return = $data->form->saveRecord(JRequest::getCmd('record_id', ''), $values);
                 foreach ($form_elements_objects as $form_elements_object) {
                     if ($form_elements_object instanceof CBFormElementAfterValidation) {
                         $form_elements_object->onSaveRecord($record_return);
                     }
                 }
                 if ($data->act_as_registration && $record_return) {
                     $meta = $data->form->getRecordMetadata($record_return);
                     if (!$data->registration_bypass_plugin || $meta->created_id) {
                         $user_id = $this->register('', '', '', $meta->created_id, JRequest::getVar('cb_' . $the_name_field['reference_id'], '', 'POST', 'STRING', JREQUEST_ALLOWRAW), JRequest::getVar('cb_' . $the_username_field['reference_id'], '', 'POST', 'STRING', JREQUEST_NOTRIM | JREQUEST_ALLOWRAW), JRequest::getVar('cb_' . $the_email_field['reference_id'], '', 'POST', 'STRING', JREQUEST_ALLOWRAW), JRequest::getVar('cb_' . $the_password_field['reference_id'], '', 'POST', 'STRING', JREQUEST_NOTRIM | JREQUEST_ALLOWRAW));
                         if (intval($user_id) > 0) {
                             JFactory::getSession()->set('cb_last_record_user_id', $user_id, 'com_contentbuilder');
                             $data->form->saveRecordUserData($record_return, $user_id, JRequest::getVar('cb_' . $the_name_field['reference_id'], '', 'POST', 'STRING', JREQUEST_ALLOWRAW), JRequest::getVar('cb_' . $the_username_field['reference_id'], '', 'POST', 'STRING', JREQUEST_NOTRIM | JREQUEST_ALLOWRAW));
                         }
                     } else {
                         if (!$meta->created_id) {
                             $bypass = new stdClass();
                             $verification_name = str_replace(array(';', '___', '|'), '-', trim($data->registration_bypass_verification_name) ? trim($data->registration_bypass_verification_name) : $data->title);
                             $verify_view = trim($data->registration_bypass_verify_view) ? trim($data->registration_bypass_verify_view) : $data->id;
                             $bypass->text = $orig_text = '{CBVerify plugin: ' . $data->registration_bypass_plugin . '; verification-name: ' . $verification_name . '; verify-view: ' . $verify_view . '; ' . str_replace(array("\r", "\n"), '', $data->registration_bypass_plugin_params) . '}';
                             $params = new stdClass();
                             JPluginHelper::importPlugin('content', 'contentbuilder_verify');
                             $bypass_dispatcher = JDispatcher::getInstance();
                             $bypass_result = $bypass_dispatcher->trigger('onPrepareContent', array(&$bypass, &$params));
                             $verification_id = '';
                             if ($bypass->text != $orig_text) {
                                 $verification_id = md5(uniqid(null, true) . mt_rand(0, mt_getrandmax()) . JFactory::getUser()->get('id', 0));
                             }
                             $user_id = $this->register($data->registration_bypass_plugin, $verification_name, $verification_id, $meta->created_id, JRequest::getVar('cb_' . $the_name_field['reference_id'], '', 'POST', 'STRING', JREQUEST_ALLOWRAW), JRequest::getVar('cb_' . $the_username_field['reference_id'], '', 'POST', 'STRING', JREQUEST_NOTRIM | JREQUEST_ALLOWRAW), JRequest::getVar('cb_' . $the_email_field['reference_id'], '', 'POST', 'STRING', JREQUEST_ALLOWRAW), JRequest::getVar('cb_' . $the_password_field['reference_id'], '', 'POST', 'STRING', JREQUEST_NOTRIM | JREQUEST_ALLOWRAW));
                             if (intval($user_id) > 0) {
                                 JFactory::getSession()->set('cb_last_record_user_id', $user_id, 'com_contentbuilder');
                                 $data->form->saveRecordUserData($record_return, $user_id, JRequest::getVar('cb_' . $the_name_field['reference_id'], '', 'POST', 'STRING', JREQUEST_ALLOWRAW), JRequest::getVar('cb_' . $the_username_field['reference_id'], '', 'POST', 'STRING', JREQUEST_NOTRIM | JREQUEST_ALLOWRAW));
                             }
                             if ($bypass->text != $orig_text && intval($user_id) > 0) {
                                 $_now = JFactory::getDate();
                                 $setup = JFactory::getSession()->get($data->registration_bypass_plugin . $verification_name, '', 'com_contentbuilder.verify.' . $data->registration_bypass_plugin . $verification_name);
                                 JFactory::getSession()->clear($data->registration_bypass_plugin . $verification_name, 'com_contentbuilder.verify.' . $data->registration_bypass_plugin . $verification_name);
                                 jimport('joomla.version');
                                 $version = new JVersion();
                                 if (version_compare($version->getShortVersion(), '3.0', '>=')) {
                                     $___now = $_now->toSql();
                                 } else {
                                     $___now = $_now->toMySQL();
                                 }
                                 $this->_db->setQuery("\n                                            Insert Into #__contentbuilder_verifications\n                                            (\n                                            `verification_hash`,\n                                            `start_date`,\n                                            `verification_data`,\n                                            `user_id`,\n                                            `plugin`,\n                                            `ip`,\n                                            `setup`,\n                                            `client`\n                                            )\n                                            Values\n                                            (\n                                            " . $this->_db->Quote($verification_id) . ",\n                                            " . $this->_db->Quote($___now) . ",\n                                            " . $this->_db->Quote('type=registration&') . ",\n                                            " . $user_id . ",\n                                            " . $this->_db->Quote($data->registration_bypass_plugin) . ",\n                                            " . $this->_db->Quote($_SERVER['REMOTE_ADDR']) . ",\n                                            " . $this->_db->Quote($setup) . ",\n                                            " . intval(JFactory::getApplication()->isAdmin() ? 1 : 0) . "\n                                            )\n                                    ");
                                 $this->_db->query();
                             }
                         }
                     }
                 }
                 if ($this->frontend && !JRequest::getCmd('record_id', '') && $record_return && !JRequest::getVar('return', '')) {
                     if ($data->force_login) {
                         if (!JFactory::getUser()->get('id', 0)) {
                             if (!$this->is15) {
                                 JRequest::setVar('return', base64_encode(JRoute::_('index.php?option=com_users&view=login&Itemid=' . JRequest::getInt('Itemid', 0), false)));
                             } else {
                                 JRequest::setVar('return', base64_encode(JRoute::_('index.php?option=com_user&view=login&Itemid=' . JRequest::getInt('Itemid', 0), false)));
                             }
                         } else {
                             if (!$this->is15) {
                                 JRequest::setVar('return', base64_encode(JRoute::_('index.php?option=com_users&view=profile&Itemid=' . JRequest::getInt('Itemid', 0), false)));
                             } else {
                                 JRequest::setVar('return', base64_encode(JRoute::_('index.php?option=com_user&view=user&Itemid=' . JRequest::getInt('Itemid', 0), false)));
                             }
                         }
                     } else {
                         if (trim($data->force_url)) {
                             JRequest::setVar('cbInternalCheck', 0);
                             JRequest::setVar('return', base64_encode(trim($data->force_url)));
                         }
                     }
                 }
                 if ($record_return) {
                     $sef = '';
                     $ignore_lang_code = '*';
                     if ($data->default_lang_code_ignore) {
                         jimport('joomla.version');
                         $version = new JVersion();
                         if (version_compare($version->getShortVersion(), '1.6', '>=')) {
                             $this->_db->setQuery("Select lang_code From #__languages Where published = 1 And sef = " . $this->_db->Quote(trim(JRequest::getCmd('lang', ''))));
                             $ignore_lang_code = $this->_db->loadResult();
                             if (!$ignore_lang_code) {
                                 $ignore_lang_code = '*';
                             }
                         } else {
                             $codes = contentbuilder::getLanguageCodes();
                             foreach ($codes as $code) {
                                 if (strstr(strtolower($code), strtolower(trim(JRequest::getCmd('lang', '')))) !== false) {
                                     $ignore_lang_code = strtolower($code);
                                     break;
                                 }
                             }
                         }
                         $sef = trim(JRequest::getCmd('lang', ''));
                         if ($ignore_lang_code == '*') {
                             $sef = '';
                         }
                     } else {
                         jimport('joomla.version');
                         $version = new JVersion();
                         if (version_compare($version->getShortVersion(), '1.6', '>=')) {
                             $this->_db->setQuery("Select sef From #__languages Where published = 1 And lang_code = " . $this->_db->Quote($data->default_lang_code));
                             $sef = $this->_db->loadResult();
                         } else {
                             $codes = contentbuilder::getLanguageCodes();
                             foreach ($codes as $code) {
                                 if ($code == $data->default_lang_code) {
                                     $sef = explode('-', $code);
                                     if (count($sef)) {
                                         $sef = strtolower($sef[0]);
                                     }
                                     break;
                                 }
                             }
                         }
                     }
                     $language = $data->default_lang_code_ignore ? $ignore_lang_code : $data->default_lang_code;
                     $this->_db->setQuery("Select id, edited From #__contentbuilder_records Where `type` = " . $this->_db->Quote($data->type) . " And `reference_id` = " . $this->_db->Quote($data->form->getReferenceId()) . " And record_id = " . $this->_db->Quote($record_return));
                     $res = $this->_db->loadAssoc();
                     $last_update = JFactory::getDate();
                     jimport('joomla.version');
                     $version = new JVersion();
                     if (version_compare($version->getShortVersion(), '3.0', '>=')) {
                         $last_update = $last_update->toSql();
                     } else {
                         $last_update = $last_update->toMySQL();
                     }
                     if (!is_array($res)) {
                         $is_future = 0;
                         $created_up = JFactory::getDate();
                         if (version_compare($version->getShortVersion(), '3.0', '>=')) {
                             $created_up = $created_up->toSql();
                         } else {
                             $created_up = $created_up->toMySQL();
                         }
                         if (intval($data->default_publish_up_days) != 0) {
                             $is_future = 1;
                             $date = JFactory::getDate(strtotime('now +' . intval($data->default_publish_up_days) . ' days'));
                             if (version_compare($version->getShortVersion(), '3.0', '>=')) {
                                 $created_up = $date->toSql();
                             } else {
                                 $created_up = $date->toMySQL();
                             }
                         }
                         $created_down = '0000-00-00 00:00:00';
                         if (intval($data->default_publish_down_days) != 0) {
                             $date = JFactory::getDate(strtotime($created_up . ' +' . intval($data->default_publish_down_days) . ' days'));
                             if (version_compare($version->getShortVersion(), '3.0', '>=')) {
                                 $created_down = $date->toSql();
                             } else {
                                 $created_down = $date->toMySQL();
                             }
                         }
                         $this->_db->setQuery("Insert Into #__contentbuilder_records (session_id,`type`,last_update,is_future,lang_code, sef, published, record_id, reference_id, publish_up, publish_down) Values ('" . JFactory::getSession()->getId() . "'," . $this->_db->Quote($data->type) . "," . $this->_db->Quote($last_update) . ",{$is_future}," . $this->_db->Quote($language) . "," . $this->_db->Quote(trim($sef)) . "," . $this->_db->Quote($data->auto_publish && !$is_future ? 1 : 0) . ", " . $this->_db->Quote($record_return) . ", " . $this->_db->Quote($data->form->getReferenceId()) . ", " . $this->_db->Quote($created_up) . ", " . $this->_db->Quote($created_down) . ")");
                         $this->_db->query();
                     } else {
                         $this->_db->setQuery("Update #__contentbuilder_records Set last_update = " . $this->_db->Quote($last_update) . ",lang_code = " . $this->_db->Quote($language) . ", sef = " . $this->_db->Quote(trim($sef)) . ", edited = edited + 1 Where `type` = " . $this->_db->Quote($data->type) . " And  `reference_id` = " . $this->_db->Quote($data->form->getReferenceId()) . " And record_id = " . $this->_db->Quote($record_return));
                         $this->_db->query();
                     }
                 }
             } else {
                 $record_return = JRequest::getCmd('record_id', '');
             }
             $data->items = $data->form->getRecord($record_return, $data->published_only, $this->frontend ? $data->own_only_fe ? JFactory::getUser()->get('id', 0) : -1 : ($data->own_only ? JFactory::getUser()->get('id', 0) : -1), true);
             $data_email_items = $data->form->getRecord($record_return, false, -1, false);
             $data->labels = $data->form->getElementLabels();
             $ids = array();
             foreach ($data->labels as $reference_id => $label) {
                 $ids[] = $this->_db->Quote($reference_id);
             }
             $data->labels = array();
             if (count($ids)) {
                 $this->_db->setQuery("Select Distinct `label`, reference_id From #__contentbuilder_elements Where form_id = " . intval($this->_id) . " And reference_id In (" . implode(',', $ids) . ") And published = 1 Order By ordering");
                 $rows = $this->_db->loadAssocList();
                 $ids = array();
                 foreach ($rows as $row) {
                     $ids[] = $row['reference_id'];
                 }
             }
             $article_id = 0;
             // creating the article
             if ($data->create_articles && count($data->items)) {
                 $data->page_title = $data->use_view_name_as_title ? $data->name : $data->form->getPageTitle();
                 //if(!count($data->items)){
                 //     JError::raiseError(404, JText::_('COM_CONTENTBUILDER_RECORD_NOT_FOUND'));
                 //}
                 $this->_db->setQuery("Select articles.`id` From #__contentbuilder_articles As articles, #__content As content Where content.id = articles.article_id And (content.state = 1 Or content.state = 0) And articles.form_id = " . intval($this->_id) . " And articles.record_id = " . $this->_db->Quote($record_return));
                 $article = $this->_db->loadResult();
                 $config = array();
                 if ($article) {
                     if (!$this->is15) {
                         $config = JRequest::getVar('jform', array());
                     } else {
                         $config = array('ordering' => JRequest::getInt('ordering', 0), 'sectionid' => JRequest::getInt('sectionid', 0), 'catid' => JRequest::getInt('catid', 0), 'alias' => JRequest::getVar('alias', ''), 'frontpage' => JRequest::getInt('frontpage', 0), 'state' => JRequest::getInt('state', 0), 'details' => JRequest::getVar('details', array()), 'params' => JRequest::getVar('params', array()), 'meta' => JRequest::getVar('meta', array()));
                     }
                 }
                 $full = $this->frontend ? contentbuilder::authorizeFe('fullarticle') : contentbuilder::authorize('fullarticle');
                 $article_id = contentbuilder::createArticle($this->_id, $record_return, $data->items, $ids, $data->title_field, $data->form->getRecordMetadata($record_return), $config, $full, $this->frontend ? $data->limited_article_options_fe : $data->limited_article_options, JRequest::getVar('cb_category_id', null));
                 if (isset($form_elements_objects)) {
                     foreach ($form_elements_objects as $form_elements_object) {
                         if ($form_elements_object instanceof CBFormElementAfterValidation) {
                             $form_elements_object->onSaveArticle($article_id);
                         }
                     }
                 }
             }
             // required to determine blocked users in system plugin
             if ($data->act_as_registration && isset($user_id) && intval($user_id) > 0) {
                 $this->_db->setQuery("Insert Into #__contentbuilder_registered_users (user_id, form_id, record_id) Values (" . intval($user_id) . ", " . $this->_id . ", " . $this->_db->Quote($record_return) . ")");
                 $this->_db->query();
             }
             if (!$data->edit_by_type) {
                 $cleanedValues = array();
                 foreach ($values as $rawvalue) {
                     if (is_array($rawvalue)) {
                         if (isset($rawvalue[0]) && $rawvalue[0] == 'cbGroupMark') {
                             unset($rawvalue[0]);
                             $cleanedValues[] = array_values($rawvalue);
                         } else {
                             $cleanedValues[] = $rawvalue;
                         }
                     } else {
                         $cleanedValues[] = $rawvalue;
                     }
                 }
                 $submit_after_result = $submit_dispatcher->trigger('onAfterSubmit', array($record_return, $article_id, $data->form, $cleanedValues));
                 foreach ($fields as $actionField) {
                     if (trim($actionField['custom_action_script'])) {
                         self::customAction(trim($actionField['custom_action_script']), $record_return, $article_id, $data->form, $actionField, $fields, $cleanedValues);
                     }
                 }
                 if (!JRequest::getCmd('record_id', '') && $data->email_notifications || JRequest::getCmd('record_id', '') && $data->email_update_notifications) {
                     $from = $MailFrom = CBCompat::getJoomlaConfig('config.mailfrom');
                     $fromname = CBCompat::getJoomlaConfig('config.fromname');
                     $mailer = JFactory::getMailer();
                     $email_admin_template = '';
                     $email_template = '';
                     // admin email
                     if (trim($data->email_admin_recipients)) {
                         // sender
                         if (trim($data->email_admin_alternative_from)) {
                             foreach ($data->items as $item) {
                                 $data->email_admin_alternative_from = str_replace('{' . $item->recName . '}', cbinternal($item->recValue), $data->email_admin_alternative_from);
                             }
                             $from = $data->email_admin_alternative_from;
                         }
                         if (trim($data->email_admin_alternative_fromname)) {
                             foreach ($data->items as $item) {
                                 $data->email_admin_alternative_fromname = str_replace('{' . $item->recName . '}', cbinternal($item->recValue), $data->email_admin_alternative_fromname);
                             }
                             $fromname = $data->email_admin_alternative_fromname;
                         }
                         $mailer->setSender(array(trim($MailFrom), trim($fromname)));
                         $mailer->addReplyTo(array($from, $fromname));
                         // recipients
                         foreach ($data->items as $item) {
                             $data->email_admin_recipients = str_replace('{' . $item->recName . '}', cbinternal($item->recValue), $data->email_admin_recipients);
                         }
                         $recipients_checked_admin = array();
                         $recipients_admin = explode(';', $data->email_admin_recipients);
                         foreach ($recipients_admin as $recipient_admin) {
                             if (contentbuilder_is_email(trim($recipient_admin))) {
                                 $recipients_checked_admin[] = trim($recipient_admin);
                             }
                         }
                         $mailer->addBCC($recipients_checked_admin);
                         $email_admin_template = contentbuilder::getEmailTemplate($this->_id, $record_return, $data_email_items, $ids, true);
                         // subject
                         $subject_admin = JText::_('COM_CONTENTBUILDER_EMAIL_RECORD_RECEIVED');
                         if (trim($data->email_admin_subject)) {
                             foreach ($data->items as $item) {
                                 $data->email_admin_subject = str_replace('{' . $item->recName . '}', cbinternal($item->recValue), $data->email_admin_subject);
                             }
                             $subject_admin = $data->email_admin_subject;
                             $subject_admin = str_replace(array('{RECORD_ID}', '{record_id}'), $record_return, $subject_admin);
                             $subject_admin = str_replace(array('{USER_ID}', '{user_id}'), JFactory::getUser()->get('id'), $subject_admin);
                             $subject_admin = str_replace(array('{USERNAME}', '{username}'), JFactory::getUser()->get('username'), $subject_admin);
                             $subject_admin = str_replace(array('{USER_FULL_NAME}', '{user_full_name}'), JFactory::getUser()->get('name'), $subject_admin);
                             $subject_admin = str_replace(array('{EMAIL}', '{email}'), JFactory::getUser()->get('email'), $subject_admin);
                             $subject_admin = str_replace(array('{VIEW_NAME}', '{view_name}'), $data->name, $subject_admin);
                             $subject_admin = str_replace(array('{VIEW_ID}', '{view_id}'), $this->_id, $subject_admin);
                             $subject_admin = str_replace(array('{IP}', '{ip}'), $_SERVER['REMOTE_ADDR'], $subject_admin);
                         }
                         $mailer->setSubject($subject_admin);
                         // attachments
                         foreach ($data->items as $item) {
                             $data->email_admin_recipients_attach_uploads = str_replace('{' . $item->recName . '}', $item->recValue, $data->email_admin_recipients_attach_uploads);
                         }
                         $attachments_admin = explode(';', $data->email_admin_recipients_attach_uploads);
                         $attached_admin = array();
                         foreach ($attachments_admin as $attachment_admin) {
                             $attachment_admin = explode("\n", str_replace("\r", "", trim($attachment_admin)));
                             foreach ($attachment_admin as $att_admin) {
                                 if (strpos(strtolower($att_admin), '{cbsite}') === 0) {
                                     $att_admin = str_replace(array('{cbsite}', '{CBSite}'), array(JPATH_SITE, JPATH_SITE), $att_admin);
                                 }
                                 if (JFile::exists(trim($att_admin))) {
                                     $attached_admin[] = trim($att_admin);
                                 }
                             }
                         }
                         $mailer->addAttachment($attached_admin);
                         $mailer->isHTML($data->email_admin_html);
                         $mailer->setBody($email_admin_template);
                         if (count($recipients_checked_admin)) {
                             $send = $mailer->Send();
                             if ($send !== true) {
                                 JFactory::getApplication()->enqueueMessage('Error sending email: ' . $mailer->ErrorInfo, 'error');
                             }
                         }
                         $mailer->ClearAddresses();
                         $mailer->ClearAllRecipients();
                         $mailer->ClearAttachments();
                     }
                     // public email
                     if (trim($data->email_recipients)) {
                         // sender
                         if (trim($data->email_alternative_from)) {
                             foreach ($data->items as $item) {
                                 $data->email_alternative_from = str_replace('{' . $item->recName . '}', cbinternal($item->recValue), $data->email_alternative_from);
                             }
                             $from = $data->email_alternative_from;
                         }
                         if (trim($data->email_alternative_fromname)) {
                             foreach ($data->items as $item) {
                                 $data->email_alternative_fromname = str_replace('{' . $item->recName . '}', cbinternal($item->recValue), $data->email_alternative_fromname);
                             }
                             $fromname = $data->email_alternative_fromname;
                         }
                         $mailer->setSender(array(trim($MailFrom), trim($fromname)));
                         $mailer->addReplyTo(array($from, $fromname));
                         // recipients
                         foreach ($data->items as $item) {
                             $data->email_recipients = str_replace('{' . $item->recName . '}', cbinternal($item->recValue), $data->email_recipients);
                         }
                         $recipients_checked = array();
                         $recipients = explode(';', $data->email_recipients);
                         foreach ($recipients as $recipient) {
                             if (contentbuilder_is_email($recipient)) {
                                 $recipients_checked[] = $recipient;
                             }
                         }
                         $mailer->addBCC($recipients_checked);
                         $email_template = contentbuilder::getEmailTemplate($this->_id, $record_return, $data_email_items, $ids, false);
                         // subject
                         $subject = JText::_('COM_CONTENTBUILDER_EMAIL_RECORD_RECEIVED');
                         if (trim($data->email_subject)) {
                             foreach ($data->items as $item) {
                                 $data->email_subject = str_replace('{' . $item->recName . '}', cbinternal($item->recValue), $data->email_subject);
                             }
                             $subject = $data->email_subject;
                             $subject = str_replace(array('{RECORD_ID}', '{record_id}'), $record_return, $subject);
                             $subject = str_replace(array('{USER_ID}', '{user_id}'), JFactory::getUser()->get('id'), $subject);
                             $subject = str_replace(array('{USERNAME}', '{username}'), JFactory::getUser()->get('username'), $subject);
                             $subject = str_replace(array('{EMAIL}', '{email}'), JFactory::getUser()->get('email'), $subject);
                             $subject = str_replace(array('{USER_FULL_NAME}', '{user_full_name}'), JFactory::getUser()->get('name'), $subject);
                             $subject = str_replace(array('{VIEW_NAME}', '{view_name}'), $data->name, $subject);
                             $subject = str_replace(array('{VIEW_ID}', '{view_id}'), $this->_id, $subject);
                             $subject = str_replace(array('{IP}', '{ip}'), $_SERVER['REMOTE_ADDR'], $subject);
                         }
                         $mailer->setSubject($subject);
                         // attachments
                         foreach ($data->items as $item) {
                             $data->email_recipients_attach_uploads = str_replace('{' . $item->recName . '}', $item->recValue, $data->email_recipients_attach_uploads);
                         }
                         $attachments = explode(';', $data->email_recipients_attach_uploads);
                         $attached = array();
                         foreach ($attachments as $attachment) {
                             $attachment = explode("\n", str_replace("\r", "", trim($attachment)));
                             foreach ($attachment as $att) {
                                 if (strpos(strtolower($att), '{cbsite}') === 0) {
                                     $att = str_replace(array('{cbsite}', '{CBSite}'), array(JPATH_SITE, JPATH_SITE), $att);
                                 }
                                 if (JFile::exists(trim($att))) {
                                     $attached[] = trim($att);
                                 }
                             }
                         }
                         $mailer->addAttachment($attached);
                         $mailer->isHTML($data->email_html);
                         $mailer->setBody($email_template);
                         if (count($recipients_checked)) {
                             $send = $mailer->Send();
                             if ($send !== true) {
                                 JFactory::getApplication()->enqueueMessage('Error sending email: ' . $mailer->ErrorInfo, 'error');
                             }
                         }
                         $mailer->ClearAddresses();
                         $mailer->ClearAllRecipients();
                         $mailer->ClearAttachments();
                     }
                 }
             }
             return $record_return;
         }
     }
     if (!$this->is15) {
         $cache = JFactory::getCache('com_content');
         $cache->clean();
         $cache = JFactory::getCache('com_contentbuilder');
         $cache->clean();
     } else {
         $cache = JFactory::getCache('com_content');
         $cache->clean();
         $cache = JFactory::getCache('com_contentbuilder');
         $cache->clean();
     }
     return false;
 }
 function onContentPrepare($context, &$article, &$params, $limitstart = 0, $is_list = false, $form = null, $item = null)
 {
     static $use_title;
     static $use_form;
     if (!extension_loaded('gd') || !function_exists('gd_info')) {
         return true;
     }
     $protect = false;
     $time_passed = 0;
     $start_time = $this->measureTime();
     $max_exec_time = 15;
     if (function_exists('ini_get')) {
         $max_exec_time = @ini_get('max_execution_time');
     }
     $max_time = !empty($max_exec_time) ? intval($max_exec_time) / 2 : 15;
     $plugin = JPluginHelper::getPlugin('content', 'contentbuilder_image_scale');
     jimport('joomla.html.parameter');
     $pluginParams = CBCompat::getParams($plugin->params);
     $max_filesize = 8 * 8 * 8 * 1024 * 2 * intval($pluginParams->def('max_filesize', 4));
     // 4M default
     jimport('joomla.filesystem.file');
     jimport('joomla.filesystem.folder');
     if (!JFile::exists(JPATH_SITE . DS . 'administrator' . DS . 'components' . DS . 'com_contentbuilder' . DS . 'classes' . DS . 'contentbuilder.php')) {
         return true;
     }
     /*
      * As of Joomla! 1.6 there is just the text passed if the article data is not passed in article context.
      * (for instance with categories).
      * But we need the article id, so we use the article id flag from content generation.
      */
     if (is_object($article) && !isset($article->id) && !isset($article->cbrecord) && isset($article->text) && $article->text) {
         preg_match_all("/<!--\\(cbArticleId:(\\d{1,})\\)-->/si", $article->text, $matched_id);
         if (isset($matched_id[1]) && isset($matched_id[1][0])) {
             $article->id = intval($matched_id[1][0]);
         }
     }
     // if this content plugin has been called from within list context
     if ($is_list) {
         if (!trim($article->text)) {
             return true;
         }
         $article->cbrecord = $form;
         $article->cbrecord->items = array();
         $article->cbrecord->items[0] = $item;
         $article->cbrecord->record_id = $item->colRecord;
     }
     if (!JFolder::exists(JPATH_SITE . DS . 'media' . DS . 'contentbuilder')) {
         JFolder::create(JPATH_SITE . DS . 'media' . DS . 'contentbuilder');
     }
     if (!JFile::exists(JPATH_SITE . DS . 'media' . DS . 'contentbuilder' . DS . 'index.html')) {
         JFile::write(JPATH_SITE . DS . 'media' . DS . 'contentbuilder' . DS . 'index.html', $def = '');
     }
     if (!JFolder::exists(JPATH_SITE . DS . 'media' . DS . 'contentbuilder' . DS . 'plugins')) {
         JFolder::create(JPATH_SITE . DS . 'media' . DS . 'contentbuilder' . DS . 'plugins');
     }
     if (!JFile::exists(JPATH_SITE . DS . 'media' . DS . 'contentbuilder' . DS . 'plugins' . DS . 'index.html')) {
         JFile::write(JPATH_SITE . DS . 'media' . DS . 'contentbuilder' . DS . 'plugins' . DS . 'index.html', $def = '');
     }
     if (!JFolder::exists(JPATH_SITE . DS . 'media' . DS . 'contentbuilder' . DS . 'plugins' . DS . 'image_scale')) {
         JFolder::create(JPATH_SITE . DS . 'media' . DS . 'contentbuilder' . DS . 'plugins' . DS . 'image_scale');
     }
     if (isset($article->id) || isset($article->cbrecord)) {
         $db = JFactory::getDBO();
         $matches = array();
         preg_match_all("/\\{CBImageScale([^}]*)\\}/i", $article->text, $matches);
         if (isset($matches[0]) && is_array($matches[0]) && isset($matches[1]) && is_array($matches[1])) {
             $record = null;
             $default_title = '';
             $protect = 0;
             $form_id = 0;
             $record_id = 0;
             $frontend = true;
             if (JFactory::getApplication()->isAdmin()) {
                 $frontend = false;
             }
             if (isset($article->id) && $article->id && !isset($article->cbrecord)) {
                 // try to obtain the record id if if this is just an article
                 $db->setQuery("Select form.`title_field`,form.`protect_upload_directory`,form.`reference_id`,article.`record_id`,article.`form_id`,form.`type`,form.`published_only`,form.`own_only`,form.`own_only_fe` From #__contentbuilder_articles As article, #__contentbuilder_forms As form Where form.`published` = 1 And form.id = article.`form_id` And article.`article_id` = " . $article->id);
                 $data = $db->loadAssoc();
                 require_once JPATH_SITE . DS . 'administrator' . DS . 'components' . DS . 'com_contentbuilder' . DS . 'classes' . DS . 'contentbuilder.php';
                 $form = contentbuilder::getForm($data['type'], $data['reference_id']);
                 if (!$form || !$form->exists) {
                     return true;
                 }
                 if ($form) {
                     $protect = $data['protect_upload_directory'];
                     $record = $form->getRecord($data['record_id'], $data['published_only'], $frontend ? $data['own_only_fe'] ? JFactory::getUser()->get('id', 0) : -1 : ($data['own_only'] ? JFactory::getUser()->get('id', 0) : -1), true);
                     $default_title = $data['title_field'];
                     $form_id = $data['form_id'];
                     $record_id = $data['record_id'];
                     $ref_id = $record_id = $data['reference_id'];
                     $ref_type = $data['type'];
                     $ref_published_only = $data['published_only'];
                     $ref_own_only_fe = $data['own_only_fe'];
                     $ref_own_only = $data['own_only'];
                 }
             } else {
                 if (isset($article->cbrecord) && isset($article->cbrecord->id) && $article->cbrecord->id) {
                     $protect = $article->cbrecord->protect_upload_directory;
                     $record = $article->cbrecord->items;
                     $default_title = $article->cbrecord->title_field;
                     $form_id = $article->cbrecord->id;
                     $record_id = $article->cbrecord->record_id;
                     $ref_id = $article->cbrecord->reference_id;
                     $ref_type = $article->cbrecord->type;
                     $ref_published_only = $article->cbrecord->published_only;
                     $ref_own_only_fe = $article->cbrecord->own_only_fe;
                     $ref_own_only = $article->cbrecord->own_only;
                 }
             }
             if (!JFile::exists(JPATH_SITE . DS . 'media' . DS . 'contentbuilder' . DS . 'plugins' . DS . 'image_scale' . DS . 'index.html')) {
                 JFile::write(JPATH_SITE . DS . 'media' . DS . 'contentbuilder' . DS . 'plugins' . DS . 'image_scale' . DS . 'index.html', $def = '');
             }
             if (!JFolder::exists(JPATH_SITE . DS . 'media' . DS . 'contentbuilder' . DS . 'plugins' . DS . 'image_scale' . DS . 'cache')) {
                 JFolder::create(JPATH_SITE . DS . 'media' . DS . 'contentbuilder' . DS . 'plugins' . DS . 'image_scale' . DS . 'cache');
             }
             if (!JFile::exists(JPATH_SITE . DS . 'media' . DS . 'contentbuilder' . DS . 'plugins' . DS . 'image_scale' . DS . 'cache' . DS . 'index.html')) {
                 JFile::write(JPATH_SITE . DS . 'media' . DS . 'contentbuilder' . DS . 'plugins' . DS . 'image_scale' . DS . 'cache' . DS . 'index.html', $def = '');
             }
             if (!JFolder::exists(JPATH_SITE . DS . 'media' . DS . 'contentbuilder' . DS . 'plugins' . DS . 'image_scale' . DS . 'cache' . DS . $form_id)) {
                 JFolder::create(JPATH_SITE . DS . 'media' . DS . 'contentbuilder' . DS . 'plugins' . DS . 'image_scale' . DS . 'cache' . DS . $form_id);
             }
             if (!JFile::exists(JPATH_SITE . DS . 'media' . DS . 'contentbuilder' . DS . 'plugins' . DS . 'image_scale' . DS . 'cache' . DS . $form_id . DS . 'index.html')) {
                 JFile::write(JPATH_SITE . DS . 'media' . DS . 'contentbuilder' . DS . 'plugins' . DS . 'image_scale' . DS . 'cache' . DS . $form_id . DS . 'index.html', $def = '');
             }
             if ($protect) {
                 if (!JFile::exists(JPATH_SITE . DS . 'media' . DS . 'contentbuilder' . DS . 'plugins' . DS . 'image_scale' . DS . 'cache' . DS . $form_id . DS . '.htaccess')) {
                     JFile::write(JPATH_SITE . DS . 'media' . DS . 'contentbuilder' . DS . 'plugins' . DS . 'image_scale' . DS . 'cache' . DS . $form_id . DS . '.htaccess', $def = 'deny from all');
                 }
             } else {
                 if (JFile::exists(JPATH_SITE . DS . 'media' . DS . 'contentbuilder' . DS . 'plugins' . DS . 'image_scale' . DS . 'cache' . DS . $form_id . DS . '.htaccess')) {
                     JFile::delete(JPATH_SITE . DS . 'media' . DS . 'contentbuilder' . DS . 'plugins' . DS . 'image_scale' . DS . 'cache' . DS . $form_id . DS . '.htaccess');
                 }
             }
             $default_folder = JPATH_SITE . DS . 'media' . DS . 'contentbuilder' . DS . 'plugins' . DS . 'image_scale' . DS . 'cache' . DS . $form_id;
             // if it is a list, permissions will be handled by the list
             if (!$is_list) {
                 contentbuilder::setPermissions($form_id, $record_id, $frontend ? '_fe' : '');
                 if ($frontend) {
                     if (!contentbuilder::authorizeFe('view')) {
                         if (JRequest::getInt('contentbuilder_display', 0) || $protect && JRequest::getInt('contentbuilder_display_detail', 0)) {
                             ob_end_clean();
                             die('No Access');
                         } else {
                             return true;
                         }
                     }
                 } else {
                     if (!contentbuilder::authorize('view')) {
                         if (JRequest::getInt('contentbuilder_display', 0) || $protect && JRequest::getInt('contentbuilder_display_detail', 0)) {
                             ob_end_clean();
                             die('No Access');
                         } else {
                             return true;
                         }
                     }
                 }
             }
             if (!trim($default_title)) {
                 $default_title = strtotime('now');
             }
             $i = 0;
             foreach ($matches[1] as $match) {
                 $alt = '';
                 $out = '';
                 $width = 0;
                 $height = 0;
                 $original_width = 0;
                 $original_height = 0;
                 $field = $is_list ? $article->cbrecord->items[0]->recName : '';
                 $folder = $default_folder;
                 $bgcolor = null;
                 $title = '';
                 $type = '';
                 $cache = 86400;
                 $global_cache = 86400;
                 $align = '';
                 $open = '';
                 $default_image = '';
                 $default_image_width = 50;
                 $default_image_height = 50;
                 $options = explode(';', trim($match));
                 foreach ($options as $option) {
                     $keyval = explode(':', trim($option), 2);
                     if (count($keyval) == 2) {
                         $value = trim($keyval[1]);
                         switch (strtolower(trim($keyval[0]))) {
                             case 'width':
                                 $width = $value;
                                 break;
                             case 'height':
                                 $height = $value;
                                 break;
                             case 'original-width':
                                 $original_width = $value;
                                 break;
                             case 'original-height':
                                 $original_height = $value;
                                 break;
                             case 'field':
                                 $field = $is_list ? $article->items[0]->recName : $value;
                                 break;
                             case 'background-color':
                                 $bgcolor = $value;
                                 break;
                             case 'folder':
                                 $folder = $value;
                                 break;
                             case 'alt':
                                 $alt = $value;
                                 break;
                             case 'title':
                                 $title = $value;
                                 break;
                             case 'type':
                                 $type = $value;
                                 break;
                             case 'cache':
                                 $cache = $value;
                                 break;
                             case 'global_cache':
                                 $global_cache = $value;
                                 break;
                             case 'align':
                                 $align = $value;
                                 break;
                             case 'open':
                                 $open = $value;
                                 break;
                             case 'default-image':
                                 $default_image = $value;
                                 break;
                             case 'default-image-width':
                                 $default_image_width = $value;
                                 break;
                             case 'default-image-height':
                                 $default_image_height = $value;
                                 break;
                         }
                     }
                 }
                 if ($is_list && $alt == 'USE-TITLE') {
                     if (!$use_form) {
                         require_once JPATH_SITE . DS . 'administrator' . DS . 'components' . DS . 'com_contentbuilder' . DS . 'classes' . DS . 'contentbuilder.php';
                         $use_form = contentbuilder::getForm($ref_type, $ref_id);
                     }
                     if ($use_form && $use_form->exists) {
                         if (!is_array($use_title) || !isset($use_title[intval($default_title)])) {
                             $use_record = $use_form->getRecord($record_id, $ref_published_only, $frontend ? $ref_own_only_fe ? JFactory::getUser()->get('id', 0) : -1 : ($ref_own_only ? JFactory::getUser()->get('id', 0) : -1), true);
                             foreach ($use_record as $use_item) {
                                 if ($default_title == $use_item->recElementId) {
                                     $default_title = cbinternal($item->recValue);
                                     if (!$is_list && $alt == 'USE-TITLE') {
                                         $alt = $default_title;
                                         $title = $default_title;
                                     }
                                     break;
                                 }
                             }
                             $use_title[intval($default_title)] = $db->loadResult();
                         }
                         $alt = $use_title[intval($default_title)];
                         $title = $use_title[intval($default_title)];
                     }
                 } else {
                     if ($is_list && trim($alt) == '') {
                         $alt = cbinternal($article->cbrecord->items[0]->recValue);
                         $title = cbinternal($article->cbrecord->items[0]->recValue);
                     }
                 }
                 $is_series = false;
                 if ($field && ($width || $height)) {
                     if ($record !== null) {
                         if (isset($record) && is_array($record)) {
                             foreach ($record as $item) {
                                 if ($default_title == $item->recElementId) {
                                     $default_title = cbinternal($item->recValue);
                                     if (!$is_list && $alt == 'USE-TITLE') {
                                         $alt = $default_title;
                                         $title = $default_title;
                                     }
                                     break;
                                 }
                             }
                             foreach ($record as $item) {
                                 if ($item->recName == $field) {
                                     if (trim($alt) == '') {
                                         $alt = cbinternal($item->recValue);
                                         $title = cbinternal($item->recValue);
                                     }
                                     $the_files = explode("\n", str_replace("\r", '', $item->recValue));
                                     $the_files_size = count($the_files);
                                     if ($the_files_size > 0) {
                                         $is_series = true;
                                     }
                                     for ($fcnt = 0; $fcnt < $the_files_size; $fcnt++) {
                                         $the_value = str_replace(array('{CBSite}', '{cbsite}'), JPATH_SITE, trim($the_files[$fcnt]));
                                         if ($the_value && ($width || $height)) {
                                             $image = @getimagesize($the_value);
                                             if ($image !== false) {
                                                 if ($type != 'simple') {
                                                     if (!$width || $width < 0) {
                                                         $width = $height;
                                                     }
                                                     if (!$height || $height < 0) {
                                                         $height = $width;
                                                     }
                                                 }
                                                 if ($width > 16384) {
                                                     $width = 16384;
                                                 }
                                                 if ($height > 16384) {
                                                     $height = 16384;
                                                 }
                                                 $exif_type = exif_imagetype($the_value);
                                                 // displaying the original file on request
                                                 if (JRequest::getInt('contentbuilder_display_detail', 0)) {
                                                     if (JRequest::getVar('contentbuilder_detail_file', '', 'REQUEST', 'STRING', JREQUEST_ALLOWRAW) == sha1($field . $the_value)) {
                                                         // clean up before displaying
                                                         ob_end_clean();
                                                         switch ($exif_type) {
                                                             case IMAGETYPE_JPEG2000:
                                                                 header('Content-Type: ' . @image_type_to_mime_type(IMAGETYPE_JPEG2000));
                                                                 break;
                                                             case IMAGETYPE_JPEG:
                                                                 header('Content-Type: ' . @image_type_to_mime_type(IMAGETYPE_JPEG));
                                                                 break;
                                                             case IMAGETYPE_GIF:
                                                                 header('Content-Type: ' . @image_type_to_mime_type(IMAGETYPE_GIF));
                                                                 break;
                                                             case IMAGETYPE_PNG:
                                                                 header('Content-Type: ' . @image_type_to_mime_type(IMAGETYPE_PNG));
                                                                 break;
                                                         }
                                                         header('Content-Disposition: inline; filename="' . basename(JFilterOutput::stringURLSafe($title) . '_' . $the_value) . '"');
                                                         header('Content-Length: ' . @filesize($the_value));
                                                         @$this->readfile_chunked($the_value);
                                                         exit;
                                                     }
                                                 }
                                                 $filename = '';
                                                 $pathinfo = pathinfo($the_value);
                                                 $basename = basename($the_value, '.' . $pathinfo['extension']) . '_' . $width . 'x' . $height . '_cbresized';
                                                 if ($folder && JFolder::exists($folder)) {
                                                     $filename = $folder . DS . $basename . image_type_to_extension($exif_type);
                                                 } else {
                                                     $filename = $pathinfo['dirname'] . DS . $basename . image_type_to_extension($exif_type);
                                                     $folder = $pathinfo['dirname'];
                                                 }
                                                 if (is_numeric($global_cache)) {
                                                     $limit = intval($global_cache);
                                                     $sourcePath = $folder . DS;
                                                     if (@file_exists($sourcePath) && @is_readable($sourcePath) && @is_dir($sourcePath) && ($handle = @opendir($sourcePath))) {
                                                         while (false !== ($file = @readdir($handle))) {
                                                             if ($file != "." && $file != "..") {
                                                                 $parts = explode('_', $file);
                                                                 $exparts = explode('.', isset($parts[count($parts) - 1]) ? $parts[count($parts) - 1] : array());
                                                                 if (isset($exparts[0]) && $exparts[0] == 'cbresized') {
                                                                     if (@JFile::exists($sourcePath . $file) && @is_readable($sourcePath . $file)) {
                                                                         $fileCreationTime = @filectime($sourcePath . $file);
                                                                         $fileAge = time() - $fileCreationTime;
                                                                         if ($fileAge >= $limit) {
                                                                             JFile::delete($sourcePath . $file);
                                                                         }
                                                                     }
                                                                 }
                                                             }
                                                         }
                                                         @closedir($handle);
                                                     }
                                                 }
                                                 $image_changed = false;
                                                 $image_filesize = @filesize($filename);
                                                 if ($image_filesize !== null) {
                                                     $existing_image = @getimagesize($filename);
                                                     if ($existing_image[0] != $width || $existing_image[1] != $height) {
                                                         $image_changed = true;
                                                     }
                                                 }
                                                 $create = false;
                                                 switch ($cache) {
                                                     case 'none':
                                                         $create = true;
                                                         break;
                                                     default:
                                                         if (is_numeric($cache) && JFile::exists($filename)) {
                                                             $limit = intval($cache);
                                                             $fileCreationTime = @filectime($filename);
                                                             $fileAge = time() - $fileCreationTime;
                                                             if ($fileAge >= $limit) {
                                                                 JFile::delete($filename);
                                                                 $create = true;
                                                             }
                                                         }
                                                 }
                                                 $max_ok = true;
                                                 if (@filesize($the_value) > $max_filesize) {
                                                     $max_ok = false;
                                                 }
                                                 if ($max_ok && ($create || $image_filesize === false || $image_changed)) {
                                                     $col_ = $bgcolor;
                                                     if ($bgcolor !== null) {
                                                         $col = array();
                                                         $col[0] = intval(@hexdec(@substr($bgcolor, 1, 2)));
                                                         $col[1] = intval(@hexdec(@substr($bgcolor, 3, 2)));
                                                         $col[2] = intval(@hexdec(@substr($bgcolor, 5, 2)));
                                                         $col_ = $col;
                                                     }
                                                     $resized = false;
                                                     $resource = false;
                                                     // try to prevent memory issues
                                                     $memory = true;
                                                     $imageInfo = $image;
                                                     $MB = 1048576;
                                                     $K64 = 65536;
                                                     $TWEAKFACTOR = 1.5;
                                                     $memoryNeeded = round(($imageInfo[0] * $imageInfo[1] * @$imageInfo['bits'] * (@$imageInfo['channels'] / 8) + $K64) * $TWEAKFACTOR);
                                                     $ini = 8 * $MB;
                                                     if (ini_get('memory_limit') !== false) {
                                                         $ini = $this->returnBytes(ini_get('memory_limit'));
                                                     }
                                                     $memoryLimit = $ini;
                                                     if (function_exists('memory_get_usage') && memory_get_usage() + $memoryNeeded > $memoryLimit) {
                                                         $memory = false;
                                                     }
                                                     if ($memory) {
                                                         switch ($exif_type) {
                                                             case IMAGETYPE_JPEG2000:
                                                             case IMAGETYPE_JPEG:
                                                                 $resource = @imagecreatefromjpeg($the_value);
                                                                 if ($resource) {
                                                                     $resized = @$this->resize_image($resource, $width, $height, $type == 'crop' ? 1 : ($type == 'simple' ? 3 : 2), $col_);
                                                                     if ($resized) {
                                                                         @imagejpeg($resized, $filename);
                                                                     }
                                                                     if ($resized) {
                                                                         @imagedestroy($resized);
                                                                     }
                                                                     if ($image[0] != $original_width && $image[1] != $original_height && ($original_width > 0 || $original_height > 0)) {
                                                                         if ($original_width != 0 && $original_height == 0) {
                                                                             $original_height = $original_width;
                                                                         }
                                                                         if ($original_width == 0 && $original_height != 0) {
                                                                             $original_width = $original_height;
                                                                         }
                                                                         $resized2 = @$this->resize_image($resource, $original_width, $original_height, $type == 'crop' ? 1 : ($type == 'simple' ? 3 : 2), $col_);
                                                                         if ($resized2) {
                                                                             @imagejpeg($resized2, $the_value);
                                                                             @imagedestroy($resized2);
                                                                             $image = @getimagesize($the_value);
                                                                         }
                                                                     }
                                                                     @imagedestroy($resource);
                                                                 }
                                                                 break;
                                                             case IMAGETYPE_GIF:
                                                                 $resource = @imagecreatefromgif($the_value);
                                                                 if ($resource) {
                                                                     $resized = @$this->resize_image($resource, $width, $height, $type == 'crop' ? 1 : ($type == 'simple' ? 3 : 2), $col_);
                                                                     if ($resized) {
                                                                         @imagegif($resized, $filename);
                                                                     }
                                                                     if ($resized) {
                                                                         @imagedestroy($resized);
                                                                     }
                                                                     if ($image[0] != $original_width && $image[1] != $original_height && ($original_width > 0 || $original_height > 0)) {
                                                                         if ($original_width != 0 && $original_height == 0) {
                                                                             $original_height = $original_width;
                                                                         }
                                                                         if ($original_width == 0 && $original_height != 0) {
                                                                             $original_width = $original_height;
                                                                         }
                                                                         $resized2 = @$this->resize_image($resource, $original_width, $original_height, $type == 'crop' ? 1 : ($type == 'simple' ? 3 : 2), $col_);
                                                                         if ($resized2) {
                                                                             @imagegif($resized2, $the_value);
                                                                             @imagedestroy($resized2);
                                                                             $image = @getimagesize($the_value);
                                                                         }
                                                                     }
                                                                     @imagedestroy($resource);
                                                                 }
                                                                 break;
                                                             case IMAGETYPE_PNG:
                                                                 $resource = @imagecreatefrompng($the_value);
                                                                 if ($resource) {
                                                                     $resized = @$this->resize_image($resource, $width, $height, $type == 'crop' ? 1 : ($type == 'simple' ? 3 : 2), $col_);
                                                                     if ($resized) {
                                                                         @imagepng($resized, $filename);
                                                                     }
                                                                     if ($resized) {
                                                                         @imagedestroy($resized);
                                                                     }
                                                                     if ($image[0] != $original_width && $image[1] != $original_height && ($original_width > 0 || $original_height > 0)) {
                                                                         if ($original_width != 0 && $original_height == 0) {
                                                                             $original_height = $original_width;
                                                                         }
                                                                         if ($original_width == 0 && $original_height != 0) {
                                                                             $original_width = $original_height;
                                                                         }
                                                                         $resized2 = @$this->resize_image($resource, $original_width, $original_height, $type == 'crop' ? 1 : ($type == 'simple' ? 3 : 2), $col_);
                                                                         if ($resized2) {
                                                                             @imagepng($resized2, $the_value);
                                                                             @imagedestroy($resized2);
                                                                             $image = @getimagesize($the_value);
                                                                         }
                                                                     }
                                                                     @imagedestroy($resource);
                                                                 }
                                                                 break;
                                                         }
                                                     }
                                                 }
                                                 if ($filename) {
                                                     $the_image = @getimagesize($filename);
                                                     if ($the_image !== false) {
                                                         if (JRequest::getInt('contentbuilder_display', 0)) {
                                                             if (JRequest::getVar('contentbuilder_field', '', 'REQUEST', 'STRING', JREQUEST_ALLOWRAW) == sha1($field . $filename)) {
                                                                 // clean up before displaying
                                                                 ob_end_clean();
                                                                 switch ($exif_type) {
                                                                     case IMAGETYPE_JPEG2000:
                                                                         header('Content-Type: ' . @image_type_to_mime_type(IMAGETYPE_JPEG2000));
                                                                         break;
                                                                     case IMAGETYPE_JPEG:
                                                                         header('Content-Type: ' . @image_type_to_mime_type(IMAGETYPE_JPEG));
                                                                         break;
                                                                     case IMAGETYPE_GIF:
                                                                         header('Content-Type: ' . @image_type_to_mime_type(IMAGETYPE_GIF));
                                                                         break;
                                                                     case IMAGETYPE_PNG:
                                                                         header('Content-Type: ' . @image_type_to_mime_type(IMAGETYPE_PNG));
                                                                         break;
                                                                 }
                                                                 header('Content-Disposition: inline; filename="' . JFilterOutput::stringURLSafe($title) . '_' . basename($filename) . '"');
                                                                 header('Content-Length: ' . @filesize($filename));
                                                                 @$this->readfile_chunked($filename);
                                                                 exit;
                                                             }
                                                         } else {
                                                             $align_ = $align;
                                                             $open_ = '';
                                                             $close_ = '';
                                                             $url = JURI::getInstance()->toString();
                                                             if (trim($open) == 'true') {
                                                                 if ($protect) {
                                                                     $open_ = JRoute::_($url . (strstr($url, '?') !== false ? '&' : '?') . 'contentbuilder_display_detail=1&contentbuilder_detail_file=' . sha1($field . $the_value));
                                                                 } else {
                                                                     $ex = explode(JPATH_SITE . DS, JPath::clean($the_value), 2);
                                                                     $open_ = JURI::root(true) . '/' . str_replace("\\", "/", $ex[count($ex) - 1]);
                                                                 }
                                                             }
                                                             if ($open_) {
                                                                 $inPopup = false;
                                                                 JHTML::_('behavior.modal');
                                                                 if ($image[0] > 960) {
                                                                     $inPopup = true;
                                                                 }
                                                                 if ($image[1] > 720) {
                                                                     $inPopup = true;
                                                                 }
                                                                 $hrefalign = $align_ ? 'style="float: ' . $align_ . ';" ' : '';
                                                                 if ($inPopup) {
                                                                     $open_ = '<a href="javascript:var win = window.open(\'' . $open_ . '\',\'ImageSizer\',\'height=' . $image[1] . ',width=' . $image[0] . ',scrollbars=1\');win.focus();void(0);" ' . $hrefalign . '>';
                                                                 } else {
                                                                     $open_ = '<a href="' . $open_ . '" ' . $hrefalign . 'class="modal" rel="{handler:\'iframe\',size:{x:' . ($image[0] + 20) . ',y:' . ($image[1] + 20) . '}}">';
                                                                 }
                                                                 $close_ = '</a>';
                                                             }
                                                             if ($protect) {
                                                                 $src = JRoute::_($url . (strstr($url, '?') !== false ? '&' : '?') . 'contentbuilder_display=1&contentbuilder_field=' . sha1($field . $filename));
                                                             } else {
                                                                 $ex = explode(JPATH_SITE . DS, $filename, 2);
                                                                 $src = JURI::root(true) . '/' . str_replace("\\", "/", $ex[count($ex) - 1]);
                                                             }
                                                             $out .= $open_ . '<img border="0" ' . $the_image[3] . ' ' . ($align_ ? 'style="float: ' . $align_ . ';" ' : '') . 'alt="' . $alt . '" title="' . $title . '" src="' . $src . '"/>' . $close_;
                                                             if ($is_series && $align_ && (strtolower($align_) == 'left' || strtolower($align_) == 'right')) {
                                                                 $out .= '<div style="float:' . strtolower($align_) . ';width: 5px;">&nbsp;</div>';
                                                             }
                                                             $align_ = '';
                                                         }
                                                     }
                                                 }
                                             }
                                         }
                                         $time_passed = $this->measureTime();
                                         if ($time_passed - $start_time > $max_time) {
                                             break;
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
                 if (trim($out) == '' && JFile::exists(JPATH_SITE . DS . 'media' . DS . 'contentbuilder' . DS . 'plugins' . DS . 'image_scale' . DS . basename($default_image))) {
                     $out = '<img width="' . $default_image_width . '" height="' . $default_image_height . '" alt="" src="' . JURI::root(true) . '/media/contentbuilder/plugins/image_scale/' . basename($default_image) . '"/>';
                 }
                 if ($is_series && $align && (strtolower($align) == 'left' || strtolower($align) == 'right')) {
                     $out .= '<div style="clear:' . strtolower($align) . ';"></div>';
                 }
                 $article->text = str_replace($matches[0][$i], $out, $article->text);
                 $i++;
             }
         }
     }
     return true;
 }
Exemple #5
0
for ($i = 0; $i < $n; $i++) {
    $row = $this->items[$i];
    $label = '';
    $first = '';
    foreach ($row as $key => $value) {
        if (!$first && $key != 'colRecord') {
            $first = html_entity_decode(cbinternal(strip_tags($value)), ENT_QUOTES, 'UTF-8');
        }
        // filtering out disallowed columns
        if (str_replace('col', '', $key) == $this->title_field) {
            $label = html_entity_decode(cbinternal(strip_tags($value)), ENT_QUOTES, 'UTF-8');
            break;
        }
    }
    if (!$label && $first) {
        $label = cbinternal($first);
    }
    $label = $label ? ': ' . $label : '';
    $this->slug = html_entity_decode($this->slug, ENT_QUOTES, 'UTF-8');
    $this->slug2 = html_entity_decode($this->slug2, ENT_QUOTES, 'UTF-8');
    $link = JRoute::_('index.php?option=com_contentbuilder&title=' . contentbuilder::stringURLUnicodeSlug($this->slug . '-' . $this->slug2 . $label) . '&controller=details&id=' . $this->form_id . '&record_id=' . $row->colRecord . '&Itemid=' . JRequest::getInt('Itemid', 0) . (JRequest::getVar('tmpl', '') != '' ? '&tmpl=' . JRequest::getVar('tmpl', '') : '') . (JRequest::getVar('layout', '') != '' ? '&layout=' . JRequest::getVar('layout', '') : '') . '&limitstart=' . JRequest::getInt('limitstart', 0) . '&filter_order=' . JRequest::getCmd('filter_order'));
    $edit_link = JRoute::_('index.php?option=com_contentbuilder&controller=edit&backtolist=1&id=' . $this->form_id . '&record_id=' . $row->colRecord . '&Itemid=' . JRequest::getInt('Itemid', 0) . (JRequest::getVar('tmpl', '') != '' ? '&tmpl=' . JRequest::getVar('tmpl', '') : '') . (JRequest::getVar('layout', '') != '' ? '&layout=' . JRequest::getVar('layout', '') : '') . '&limitstart=' . JRequest::getInt('limitstart', 0) . '&filter_order=' . JRequest::getCmd('filter_order'));
    $publish_link = JRoute::_('index.php?option=com_contentbuilder&controller=edit&task=publish&backtolist=1&id=' . $this->form_id . '&list_publish=1&cid[]=' . $row->colRecord . '&Itemid=' . JRequest::getInt('Itemid', 0) . (JRequest::getVar('tmpl', '') != '' ? '&tmpl=' . JRequest::getVar('tmpl', '') : '') . (JRequest::getVar('layout', '') != '' ? '&layout=' . JRequest::getVar('layout', '') : '') . '&limitstart=' . JRequest::getInt('limitstart', 0) . '&filter_order=' . JRequest::getCmd('filter_order'));
    $unpublish_link = JRoute::_('index.php?option=com_contentbuilder&controller=edit&task=publish&backtolist=1&id=' . $this->form_id . '&list_publish=0&cid[]=' . $row->colRecord . '&Itemid=' . JRequest::getInt('Itemid', 0) . (JRequest::getVar('tmpl', '') != '' ? '&tmpl=' . JRequest::getVar('tmpl', '') : '') . (JRequest::getVar('layout', '') != '' ? '&layout=' . JRequest::getVar('layout', '') : '') . '&limitstart=' . JRequest::getInt('limitstart', 0) . '&filter_order=' . JRequest::getCmd('filter_order'));
    $select = '<input type="checkbox" name="cid[]" value="' . $row->colRecord . '"/>';
    ?>
        <tr class="<?php 
    echo "row{$k}";
    ?>
">
            <?php