Beispiel #1
0
 function __construct()
 {
     JRequest::setVar('cbIsNew', 0);
     if (JRequest::getCmd('task', '') == 'delete' || JRequest::getCmd('task', '') == 'publish') {
         $items = JRequest::getVar('cid', array(), 'request', 'array');
         contentbuilder::setPermissions(JRequest::getInt('id', 0), $items, class_exists('cbFeMarker') ? '_fe' : '');
     } else {
         if (JRequest::getCmd('record_id', '')) {
             contentbuilder::setPermissions(JRequest::getInt('id', 0), JRequest::getCmd('record_id', ''), class_exists('cbFeMarker') ? '_fe' : '');
         } else {
             JRequest::setVar('cbIsNew', 1);
             contentbuilder::setPermissions(JRequest::getInt('id', 0), 0, class_exists('cbFeMarker') ? '_fe' : '');
         }
     }
     parent::__construct();
 }
 function onContentPrepare($context, &$article, &$params, $limitstart = 0)
 {
     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;
     }
     if (isset($article->id) && $article->id) {
         $frontend = true;
         if (JFactory::getApplication()->isAdmin()) {
             $frontend = false;
         }
         $db = JFactory::getDBO();
         $db->setQuery("Select 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 && !(JRequest::getVar('option', '') == 'com_contentbuilder' && JRequest::getVar('controller', '') == 'edit')) {
             JFactory::getLanguage()->load('com_contentbuilder');
             contentbuilder::setPermissions($data['form_id'], $data['record_id'], $frontend ? '_fe' : '');
             if (JRequest::getCmd('view') == 'article') {
                 contentbuilder::checkPermissions('view', JText::_('COM_CONTENTBUILDER_PERMISSIONS_VIEW_NOT_ALLOWED'), $frontend ? '_fe' : '');
             } else {
                 if ($frontend) {
                     if (!contentbuilder::authorizeFe('view')) {
                         $article->text = JText::_('COM_CONTENTBUILDER_PERMISSIONS_VIEW_NOT_ALLOWED');
                     }
                 } else {
                     if (!contentbuilder::authorize('view')) {
                         $article->text = JText::_('COM_CONTENTBUILDER_PERMISSIONS_VIEW_NOT_ALLOWED');
                     }
                 }
             }
         }
     }
     return true;
 }
Beispiel #3
0
 function __construct()
 {
     if (class_exists('cbFeMarker') && JRequest::getInt('Itemid', 0)) {
         $option = 'com_contentbuilder';
         // try menu item
         jimport('joomla.version');
         $version = new JVersion();
         if (version_compare($version->getShortVersion(), '1.6', '>=')) {
             $menu = JSite::getMenu();
             $item = $menu->getActive();
             if (is_object($item)) {
                 if ($item->params->get('record_id', null) !== null) {
                     JRequest::setVar('record_id', $item->params->get('record_id', null));
                     $this->_show_back_button = $item->params->get('show_back_button', null);
                 }
                 //if($item->params->get('show_page_heading', null) !== null){
                 //    $this->_show_page_heading = $item->params->get('show_page_heading', null);
                 //}
             }
         } else {
             $params = JComponentHelper::getParams($option);
             if ($params->get('record_id', null)) {
                 JRequest::setVar('record_id', $params->get('record_id', null));
                 $this->_show_back_button = $params->get('show_back_button', null);
             }
             //if($params->get('show_page_heading', null) !== null){
             //    $this->_show_page_heading = $params->get('show_page_heading', null);
             //}
         }
     }
     if (JRequest::getWord('view', '') == 'latest') {
         $db = JFactory::getDBO();
         $db->setQuery('Select `type`, `reference_id` From #__contentbuilder_forms Where id = ' . intval(JRequest::getInt('id', 0)) . ' And published = 1');
         $form = $db->loadAssoc();
         $form = contentbuilder::getForm($form['type'], $form['reference_id']);
         $labels = $form->getElementLabels();
         $ids = array();
         foreach ($labels as $reference_id => $label) {
             $ids[] = $db->Quote($reference_id);
         }
         if (count($ids)) {
             $db->setQuery("Select Distinct `label`, reference_id From #__contentbuilder_elements Where form_id = " . intval(JRequest::getInt('id', 0)) . " And reference_id In (" . implode(',', $ids) . ") And published = 1 Order By ordering");
             $rows = $db->loadAssocList();
             $ids = array();
             foreach ($rows as $row) {
                 $ids[] = $row['reference_id'];
             }
         }
         $rec = $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 = $form->getRecord($rec->colRecord, false, -1, true);
             $record_id = $rec->colRecord;
             JRequest::setVar('record_id', $record_id);
         }
         if (!JRequest::getCmd('record_id', '')) {
             JRequest::setVar('cbIsNew', 1);
             contentbuilder::setPermissions(JRequest::getInt('id', 0), 0, class_exists('cbFeMarker') ? '_fe' : '');
             $auth = class_exists('cbFeMarker') ? 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=' . JRequest::getInt('id', 0) . (JRequest::getVar('tmpl', '') != '' ? '&tmpl=' . JRequest::getVar('tmpl', '') : '') . (JRequest::getVar('layout', '') != '' ? '&layout=' . JRequest::getVar('layout', '') : '') . '&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);
             }
         }
     }
     contentbuilder::setPermissions(JRequest::getInt('id', 0), JRequest::getCmd('record_id', 0), class_exists('cbFeMarker') ? '_fe' : '');
     parent::__construct();
 }
 function cbCheckPermissions()
 {
     // CONTENTBUILDER BEGIN
     jimport('joomla.filesystem.file');
     $cbData = null;
     $cbForm = null;
     $cbRecord = null;
     $cbFrontend = true;
     $cbFull = false;
     if (JFile::exists(JPATH_ADMINISTRATOR . DS . 'components' . DS . 'com_contentbuilder' . DS . 'contentbuilder.xml')) {
         if (JFactory::getApplication()->isAdmin()) {
             $cbFrontend = false;
         }
         if ($cbFrontend) {
             JFactory::getLanguage()->load('com_contentbuilder');
         } else {
             JFactory::getLanguage()->load('com_contentbuilder', JPATH_SITE . DS . 'administrator');
         }
         $db = JFactory::getDBO();
         require_once JPATH_ADMINISTRATOR . DS . 'components' . DS . 'com_contentbuilder' . DS . 'classes' . DS . 'contentbuilder.php';
         $db->setQuery("Select `id` From #__contentbuilder_forms Where `type` = 'com_breezingforms' And `reference_id` = " . intval($this->form) . " And published = 1");
         jimport('joomla.version');
         $version = new JVersion();
         if (version_compare($version->getShortVersion(), '3.0', '<')) {
             $cbForms = $db->loadResultArray();
         } else {
             $cbForms = $db->loadColumn();
         }
         // if no BF form is associated with contentbuilder, we don't need no further checks
         if (!count($cbForms)) {
             return array('form' => $cbForm, 'record' => $cbRecord, 'frontend' => $cbFrontend, 'data' => $cbData, 'full' => $cbFull);
         }
         // test if there is any published contentbuilder view that allows to create new submissions
         if (!JRequest::getInt('cb_record_id', 0) || !JRequest::getInt('cb_form_id', 0)) {
             $cbAuth = false;
             foreach ($cbForms as $cbFormId) {
                 contentbuilder::setPermissions($cbFormId, 0, $cbFrontend ? '_fe' : '');
                 if ($cbFrontend) {
                     $cbAuth = contentbuilder::authorizeFe('new');
                 } else {
                     $cbAuth = contentbuilder::authorize('new');
                 }
                 if ($cbAuth) {
                     break;
                 }
             }
             if (count($cbForms) && !$cbAuth) {
                 JError::raiseError(403, JText::_('COM_CONTENTBUILDER_PERMISSIONS_NEW_NOT_ALLOWED'));
             }
         }
         if (JRequest::getInt('cb_form_id', 0)) {
             // test the permissions of given record
             if (JRequest::getInt('cb_record_id', 0)) {
                 contentbuilder::setPermissions(JRequest::getInt('cb_form_id', 0), JRequest::getInt('cb_record_id', 0), $cbFrontend ? '_fe' : '');
                 contentbuilder::checkPermissions('edit', JText::_('COM_CONTENTBUILDER_PERMISSIONS_EDIT_NOT_ALLOWED'), $cbFrontend ? '_fe' : '');
             } else {
                 contentbuilder::setPermissions(JRequest::getInt('cb_form_id', 0), 0, $cbFrontend ? '_fe' : '');
                 contentbuilder::checkPermissions('new', JText::_('COM_CONTENTBUILDER_PERMISSIONS_NEW_NOT_ALLOWED'), $cbFrontend ? '_fe' : '');
             }
             $db->setQuery("Select * From #__contentbuilder_forms Where id = " . JRequest::getInt('cb_form_id', 0) . " And published = 1");
             $cbData = $db->loadAssoc();
             if (is_array($cbData)) {
                 $cbFull = $cbFrontend ? contentbuilder::authorizeFe('fullarticle') : contentbuilder::authorize('fullarticle');
                 $cbForm = contentbuilder::getForm('com_breezingforms', $cbData['reference_id']);
                 $cbRecord = $cbForm->getRecord(JRequest::getInt('cb_record_id', 0), $cbData['published_only'], $cbFrontend ? $cbData['own_only_fe'] ? JFactory::getUser()->get('id', 0) : -1 : ($cbData['own_only'] ? JFactory::getUser()->get('id', 0) : -1), $cbFrontend ? $cbData['show_all_languages_fe'] : true);
                 if (!count($cbRecord) && !JRequest::getBool('cbIsNew')) {
                     JError::raiseError(404, JText::_('COM_CONTENTBUILDER_RECORD_NOT_FOUND'));
                 }
             }
         }
     }
     return array('form' => $cbForm, 'record' => $cbRecord, 'frontend' => $cbFrontend, 'data' => $cbData, 'full' => $cbFull);
     // CONTENTBUILDER END
 }
Beispiel #5
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 .= ' &raquo; ' . 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;
 }
 function getPermissions()
 {
     $perms = array();
     if ($this->show_permissions) {
         foreach ($this->items as $item) {
             contentbuilder::setPermissions($item->id, '', '_fe');
             $view = contentbuilder::authorizeFe('view');
             $new = contentbuilder::authorizeFe('new');
             $edit = contentbuilder::authorizeFe('edit');
             $perms[$item->id] = array('view' => $view, 'new' => $new, 'edit' => $edit);
         }
     }
     return $perms;
 }
Beispiel #7
0
 function __construct()
 {
     parent::__construct();
     contentbuilder::setPermissions(JRequest::getInt('id', 0), 0, class_exists('cbFeMarker') ? '_fe' : '');
 }
 function onContentPrepare($context, &$article, &$params, $limitstart = 0, $is_list = false, $form = null, $item = null)
 {
     $protect = false;
     $plugin = JPluginHelper::getPlugin('content', 'contentbuilder_rating');
     jimport('joomla.html.parameter');
     $pluginParams = CBCompat::getParams($plugin->params);
     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;
     }
     require_once JPATH_SITE . DS . 'administrator' . DS . 'components' . DS . 'com_contentbuilder' . DS . 'classes' . DS . 'contentbuilder.php';
     $lang = JFactory::getLanguage();
     $lang->load('plg_content_contentbuilder_rating', JPATH_ADMINISTRATOR);
     /*
      * 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 (isset($article->id) || isset($article->cbrecord)) {
         $db = JFactory::getDBO();
         $matches = array();
         preg_match_all("/\\{CBRating([^}]*)\\}/i", $article->text, $matches);
         if (isset($matches[0]) && is_array($matches[0]) && isset($matches[1]) && is_array($matches[1])) {
             $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.rating_slots,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) {
                     $form_id = $data['form_id'];
                     $record_id = $data['record_id'];
                     $rating_slots = $data['rating_slots'];
                 }
             } else {
                 if (isset($article->cbrecord) && isset($article->cbrecord->id) && $article->cbrecord->id) {
                     $form = $article->cbrecord->form;
                     $form_id = $article->cbrecord->id;
                     $record_id = $article->cbrecord->record_id;
                     $rating_slots = $article->cbrecord->rating_slots;
                 }
             }
             $rating = 0;
             $rating_count = 0;
             $rating_sum = 0;
             if (!is_object($form)) {
                 return true;
             }
             $record = $form->getRecord($record_id, false, -1, true);
             if (count($record)) {
                 $rating = $record[0]->recRating;
                 $rating_count = $record[0]->recRatingCount;
                 $rating_sum = $record[0]->recRatingSum;
             }
             $rating_allowed = true;
             if (!$is_list) {
                 contentbuilder::setPermissions($form_id, $record_id, $frontend ? '_fe' : '');
                 if ($frontend) {
                     if (!contentbuilder::authorizeFe('rating')) {
                         $rating_allowed = false;
                     }
                 } else {
                     if (!contentbuilder::authorize('rating')) {
                         $rating_allowed = false;
                     }
                 }
             }
             $i = 0;
             foreach ($matches[1] as $match) {
                 $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]))) {
                             default:
                         }
                     }
                 }
                 $out = contentbuilder::getRating($form_id, $record_id, $rating, $rating_slots, JRequest::getCmd('lang', ''), $rating_allowed, $rating_count, $rating_sum);
                 $article->text = str_replace($matches[0][$i], $out, $article->text);
                 $i++;
             }
         }
     }
     return true;
 }
 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;
 }
 function onContentPrepare($context, &$article, &$params, $limitstart = 0, $is_list = false, $form = null, $item = null)
 {
     $protect = false;
     $plugin = JPluginHelper::getPlugin('content', 'contentbuilder_download');
     jimport('joomla.html.parameter');
     $pluginParams = CBCompat::getParams($plugin->params);
     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;
     }
     $lang = JFactory::getLanguage();
     $lang->load('plg_content_contentbuilder_download', JPATH_ADMINISTRATOR);
     /*
      * 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 . 'download')) {
         JFolder::create(JPATH_SITE . DS . 'media' . DS . 'contentbuilder' . DS . 'plugins' . DS . 'download');
     }
     if (!JFile::exists(JPATH_SITE . DS . 'media' . DS . 'contentbuilder' . DS . 'plugins' . DS . 'download' . DS . 'index.html')) {
         JFile::write(JPATH_SITE . DS . 'media' . DS . 'contentbuilder' . DS . 'plugins' . DS . 'image_scale' . DS . 'index.html', $def = '');
     }
     if (isset($article->id) || isset($article->cbrecord)) {
         $db = JFactory::getDBO();
         $matches = array();
         preg_match_all("/\\{CBDownload([^}]*)\\}/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;
             $type = '';
             $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'];
                     $type = $data['type'];
                 }
             } 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;
                     $type = $article->cbrecord->type;
                 }
             }
             if (!$is_list) {
                 contentbuilder::setPermissions($form_id, $record_id, $frontend ? '_fe' : '');
                 if ($frontend) {
                     if (!contentbuilder::authorizeFe('view')) {
                         if (JRequest::getVar('contentbuilder_download_file', '', 'GET', 'STRING', JREQUEST_ALLOWRAW)) {
                             ob_end_clean();
                             die('No Access');
                         } else {
                             return true;
                         }
                     }
                 } else {
                     if (!contentbuilder::authorize('view')) {
                         if (JRequest::getVar('contentbuilder_download_file', '', 'GET', 'STRING', JREQUEST_ALLOWRAW)) {
                             ob_end_clean();
                             die('No Access');
                         } else {
                             return true;
                         }
                     }
                 }
             }
             if (!trim($default_title)) {
                 $default_title = strtotime('now');
             }
             $i = 0;
             foreach ($matches[1] as $match) {
                 $out = '';
                 $field = $is_list ? $article->cbrecord->items[0]->recName : '';
                 $box_style = 'border-width:thin::border-color:#000000::border-style:dashed::padding:5px::';
                 $info_style = '';
                 $align = '';
                 $info = true;
                 $hide_filename = false;
                 $hide_mime = false;
                 $hide_size = false;
                 $hide_downloads = false;
                 $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 'field':
                                 $field = $value;
                                 break;
                             case 'info-style':
                                 $info_style = $value;
                                 break;
                             case 'box-style':
                                 $box_style = $value;
                                 break;
                             case 'align':
                                 $align = $value;
                                 break;
                             case 'info':
                                 $info = $value == 'true' ? true : false;
                                 break;
                             case 'hide-filename':
                                 $hide_filename = $value == 'true' ? true : false;
                                 break;
                             case 'hide-mime':
                                 $hide_mime = $value == 'true' ? true : false;
                                 break;
                             case 'hide-size':
                                 $hide_size = $value == 'true' ? true : false;
                                 break;
                             case 'hide-downloads':
                                 $hide_downloads = $value == 'true' ? true : false;
                                 break;
                         }
                     }
                 }
                 $is_series = false;
                 if ($field && isset($record) && $record !== null && is_array($record)) {
                     foreach ($record as $item) {
                         if ($default_title == $item->recElementId) {
                             $default_title = $item->recValue;
                             break;
                         }
                     }
                     foreach ($record as $item) {
                         if ($item->recName == $field) {
                             $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) {
                                     $exists = JFile::exists($the_value);
                                     if ($exists) {
                                         $phpversion = explode('-', phpversion());
                                         $phpversion = $phpversion[0];
                                         // because of mime_content_type deprecation
                                         if (version_compare($phpversion, '5.3', '<')) {
                                             if (function_exists('mime_content_type')) {
                                                 $mime = mime_content_type($the_value);
                                             } else {
                                                 // fallback if not even that one exists
                                                 $mime = $this->mime_content_type($the_value);
                                             }
                                         } else {
                                             if (function_exists('finfo_open')) {
                                                 $finfo = finfo_open(FILEINFO_MIME_TYPE);
                                                 $mime = finfo_file($finfo, $the_value);
                                                 finfo_close($finfo);
                                             } else {
                                                 $mime = $this->mime_content_type($the_value);
                                             }
                                         }
                                         if (JRequest::getVar('contentbuilder_download_file', '', 'GET', 'STRING', JREQUEST_ALLOWRAW) == sha1($field . $the_value)) {
                                             $download_name = basename(JFilterOutput::stringURLSafe($default_title) . '_' . $the_value);
                                             $file_id = md5($type . $item->recElementId . $the_value);
                                             if (!JFactory::getSession()->get('downloaded' . $type . $item->recElementId . $file_id, false, 'com_contentbuilder.plugin.download')) {
                                                 $db->setQuery("Select hits From #__contentbuilder_resource_access Where `type` = " . $db->Quote($type) . " And resource_id = '" . $file_id . "' And element_id = " . $db->Quote($item->recElementId));
                                                 if ($db->loadResult() === null) {
                                                     $db->setQuery("Insert Into #__contentbuilder_resource_access (`type`, form_id, element_id, resource_id, hits) values (" . $db->Quote($type) . "," . intval($form_id) . ", " . $db->Quote($item->recElementId) . ", '" . $file_id . "',1)");
                                                 } else {
                                                     $db->setQuery("Update #__contentbuilder_resource_access Set `type` = " . $db->Quote($type) . ", resource_id = '" . $file_id . "', form_id = " . intval($form_id) . ", element_id = " . $db->Quote($item->recElementId) . ", hits = hits + 1 Where `type` = " . $db->Quote($type) . " And resource_id = '" . $file_id . "' And element_id = " . $db->Quote($item->recElementId));
                                                 }
                                                 $db->query();
                                             }
                                             JFactory::getSession()->set('downloaded' . $type . $item->recElementId . $file_id, true, 'com_contentbuilder.plugin.download');
                                             // clean up before displaying
                                             @ob_end_clean();
                                             header('Content-Type: application/octet-stream; name="' . $download_name . '"');
                                             header('Content-Disposition: inline; filename="' . $download_name . '"');
                                             header('Content-Length: ' . @filesize($the_value));
                                             // NOTE: if running IIS and CGI, raise the CGI timeout to serve large files
                                             @$this->readfile_chunked($the_value);
                                             exit;
                                         }
                                         $info_style_ = $info_style;
                                         $box_style_ = $box_style;
                                         $info_ = $info;
                                         $align_ = $align;
                                         $download_name = basename(JFilterOutput::stringURLSafe($default_title) . '_' . $the_value);
                                         $file_id = md5($type . $item->recElementId . $the_value);
                                         $db->setQuery("Select hits From #__contentbuilder_resource_access Where resource_id = '" . $file_id . "' And `type` = " . intval($type) . " And element_id = " . $db->Quote($item->recElementId));
                                         $hits = $db->loadResult();
                                         if (!$hits) {
                                             $hits = 0;
                                         }
                                         $size = @number_format(filesize($the_value) / (1024 * 1024), 2) . ' MB';
                                         if (!floatval($size)) {
                                             $size = @number_format(filesize($the_value) / 1024, 2) . ' kb';
                                         }
                                         $hide_filename_ = $hide_filename;
                                         $hide_mime_ = $hide_mime;
                                         $hide_size_ = $hide_size;
                                         $hide_downloads_ = $hide_downloads;
                                         $url = JURI::getInstance()->toString();
                                         $open_ = JRoute::_($url . (strstr($url, '?') !== false ? '&' : '?') . 'contentbuilder_download_file=' . sha1($field . $the_value));
                                         $out .= '<div style="' . ($align_ ? 'float: ' . $align_ . ';' : '') . str_replace('::', ';', $box_style_) . '">
                                                     <a href="' . $open_ . '">' . JText::_('COM_CONTENTBUILDER_PLUGIN_DOWNLOAD_DOWNLOAD') . '</a>' . ($info_ ? '<div style="' . str_replace('::', ';', $info_style_) . '">
                                                                 ' . ($hide_filename_ ? '' : '<span class="cbPluginDownloadFilename">' . JText::_('COM_CONTENTBUILDER_PLUGIN_DOWNLOAD_FILENAME') . ':</span> ' . $download_name . '<br/>') . '
                                                                 ' . ($hide_mime_ ? '' : '<span class="cbPluginDownloadMime">' . JText::_('COM_CONTENTBUILDER_PLUGIN_DOWNLOAD_MIME') . ':</span> ' . $mime . '<br/>') . '
                                                                 ' . ($hide_size_ ? '' : '<span ' . ($hide_size_ ? ' style="display:none;" ' : '') . 'class="cbPluginDownloadSize">' . JText::_('COM_CONTENTBUILDER_PLUGIN_DOWNLOAD_SIZE') . ':</span> ' . $size . '<br/>') . '
                                                                 ' . ($hide_downloads_ ? '' : '<span ' . ($hide_downloads_ ? ' style="display:none;" ' : '') . 'class="cbPluginDownloadDownloads">' . JText::_('COM_CONTENTBUILDER_PLUGIN_DOWNLOAD_DOWNLOADS') . ':</span> ' . $hits . '<br/>') . '
                                                              </div>' : '') . '</div>';
                                         if ($is_series && $align_ && (strtolower($align_) == 'left' || strtolower($align_) == 'right')) {
                                             $out .= '<div style="float:' . strtolower($align_) . ';width: 5px;">&nbsp;</div>';
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
                 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;
 }