Esempio n. 1
0
 /**
  * Create object and set filter.
  *
  * @param array $config
  * @return void
  */
 function __construct($config = array())
 {
     $this->filter[JOOMDOC_FILTER_STATE] = JOOMDOC_STRING;
     $this->filter[JOOMDOC_FILTER_ORDERING] = JOOMDOC_STRING;
     $this->filter[JOOMDOC_FILTER_DIRECTION] = JOOMDOC_STRING;
     $this->filter[JOOMDOC_FILTER_START] = JOOMDOC_INT;
     $this->filter[JOOMDOC_FILTER_LINKS] = JOOMDOC_INT;
     $this->filter[JOOMDOC_FILTER_LIMIT] = JOOMDOC_INT;
     $this->filter[JOOMDOC_FILTER_KEYWORDS] = JOOMDOC_STRING;
     if (empty($config['filter_fields'])) {
         $config['filter_fields'] = array(JOOMDOC_FILTER_ID, JOOMDOC_FILTER_TITLE, JOOMDOC_FILTER_STATE, JOOMDOC_FILTER_CREATED, JOOMDOC_FILTER_ORDERING, JOOMDOC_ORDER_ORDERING);
     }
     parent::__construct($config);
 }
Esempio n. 2
0
 /**
  * Revert selected Version as last Version.
  *
  * @param int $id restored Version row ID
  * @param string $path restored Version Path
  * @return JObject
  *     revertVersion   reverted Version Number
  *     oldLastVersion  archived last Version Number
  *     newLastVersion  new last Version from reverted Version number
  */
 function revert($id, $path)
 {
     $safePath = $this->_db->quote($path);
     $this->_db->setQuery('SELECT MAX(`id`) FROM `#__joomdoc_file` WHERE `path` = ' . $safePath . ' GROUP BY `path`');
     $cid = $this->_db->loadColumn();
     // the latest file version list
     // get Row with given ID and given Path which isn't last Version - candidate to revert as last Version
     $query = 'SELECT `id`, `version` FROM `#__joomdoc_file` WHERE `id` = ' . (int) $id . ' AND `path` = ' . $safePath;
     if ($cid) {
         $query .= ' AND `id` NOT IN (' . implode(', ', $cid) . ')';
     }
     $this->_db->setQuery($query);
     $candidate = $this->_db->loadObject();
     if (is_null($candidate)) {
         return false;
     }
     // get last Version number
     $query = 'SELECT MAX(`version`) FROM `#__joomdoc_file` WHERE `path` = ' . $safePath;
     $this->_db->setQuery($query);
     $lastVersion = $this->_db->loadResult();
     if (is_null($lastVersion)) {
         return false;
     }
     // full Path of last Version
     $lastVersionPath = JoomDOCFileSystem::getFullPath($path);
     // full Path of revert Version
     $revertVersionPath = JoomDOCFileSystem::getVersionPath($lastVersionPath, $candidate->version, true);
     // full Path to archive last Version
     $archiveVersionPath = JoomDOCFileSystem::getVersionPath($lastVersionPath, $lastVersion, true);
     // move last Version as archive Version
     if (!JFile::move($lastVersionPath, $archiveVersionPath)) {
         return false;
     }
     // copy revert Version as last Version
     if (!JFile::copy($revertVersionPath, $lastVersionPath)) {
         return false;
     }
     // copy revert Version DB Row
     $newLastVersionID = JoomDOCModelList::copyRow('#__joomdoc_file', 'id', $candidate->id);
     // increment new last Version number
     $newLastVersion = $lastVersion + 1;
     $query = 'UPDATE `#__joomdoc_file` SET `version` = ' . $newLastVersion . ', `state` = ' . JOOMDOC_STATE_PUBLISHED . ' WHERE `id` = ' . $newLastVersionID;
     $this->_db->setQuery($query);
     $this->_db->query();
     $output = new JObject();
     $output->revertVersion = $candidate->version;
     $output->oldLastVersion = $lastVersion;
     $output->newLastVersion = $newLastVersion;
     JModelLegacy::getInstance(JOOMDOC_DOCUMENTS, JOOMDOC_MODEL_PREFIX)->flat($path);
     return $output;
 }
Esempio n. 3
0
 public function onContentSearch($text, $phrase = '', $ordering = '', $areas = null)
 {
     if (!JString::trim($text) || is_array($areas) && !array_intersect($areas, array_keys(plgSearchJoomDOC::onContentSearchAreas()))) {
         return array();
     }
     // import component JoomDOC framework
     $jdcAdm = JPATH_ADMINISTRATOR . '/components/com_joomdoc/';
     $jdcSit = JPATH_SITE . '/components/com_joomdoc/';
     $jdcLib = $jdcAdm . 'libraries/joomdoc/';
     $includes['JoomDOCRoute'] = $jdcLib . 'application/route.php';
     $includes['JoomDOCMenu'] = $jdcLib . 'application/menu.php';
     $includes['JoomDOCConfig'] = $jdcLib . 'application/config.php';
     $includes['JoomDOCFileSystem'] = $jdcLib . 'filesystem/filesystem.php';
     $includes['JoomDOCString'] = $jdcLib . 'utilities/string.php';
     $includes['JoomDOCHelper'] = $jdcLib . 'utilities/helper.php';
     $includes['JoomDOCModelList'] = $jdcLib . 'application/component/modellist.php';
     $includes[] = $jdcAdm . 'defines.php';
     foreach ($includes as $classname => $include) {
         $include = JPath::clean($include);
         if (!JFile::exists($include)) {
             // JoomDOC is propably uninstalled or corupted
             return array();
         }
         is_string($classname) ? JLoader::register($classname, $include) : (include_once $include);
     }
     JModelLegacy::addIncludePath(JPath::clean($jdcAdm . 'models'));
     JTable::addIncludePath(JPath::clean($jdcAdm . 'tables'));
     $db = JFactory::getDbo();
     $nullDate = $db->getNullDate();
     // prepare SQL WHERE criteria
     $wheres = array();
     // according to type of searching
     switch ($phrase) {
         case 'exact':
             // exactly phrase
             $keywords = $db->q('%' . JString::strtolower(JString::trim($text)) . '%');
             $items[] = 'LOWER(`document`.`title`) LIKE ' . $keywords;
             $items[] = 'LOWER(`document`.`description`) LIKE ' . $keywords;
             $items[] = 'LOWER(`document`.`path`) LIKE ' . $keywords;
             $items[] = 'LOWER(`document`.`content`) LIKE ' . $keywords;
             $where[] = '(' . implode(') OR (', $items) . ') ';
             break;
         case 'all':
         case 'any':
         default:
             // all words or any word or default
             $keywords = explode(' ', $text);
             // split to words
             $wheres = array();
             // search for each word
             foreach ($keywords as $keyword) {
                 $keyword = JString::trim($keyword);
                 if ($keyword) {
                     $keyword = $db->q('%' . JString::strtolower($keyword) . '%');
                     $items[] = 'LOWER(`document`.`title`) LIKE ' . $keyword;
                     $items[] = 'LOWER(`document`.`description`) LIKE ' . $keyword;
                     $items[] = 'LOWER(`document`.`path`) LIKE ' . $keyword;
                     $items[] = 'LOWER(`document`.`content`) LIKE ' . $keyword;
                     $parts[] = implode(' OR ', $items);
                 }
             }
             if (isset($parts)) {
                 if ($phrase == 'all') {
                     $where[] = '(' . implode(') AND (', $parts) . ') ';
                 } else {
                     $where[] = '(' . implode(') OR (', $parts) . ') ';
                 }
             }
             break;
     }
     $where[] = '(' . JoomDOCModelList::getDocumentPublished() . ')';
     $where = ' WHERE ' . implode(' AND ', $where) . ' ';
     // prepare SQL ORDER BY criteria
     switch ($ordering) {
         case 'oldest':
             // oldest items first (oldest documents created or oldest file uploaded)
             $order = ' ORDER BY `document`.`created` ASC, `document`.`upload` ASC ';
             break;
         case 'popular':
             // most hits items first
             $order = ' ORDER BY `document`.`hits` DESC, `document`.`title` ASC, `document`.`path` ASC ';
             break;
         case 'alpha':
             // alphabetically (document title or file name)
             $order = ' ORDER BY `document`.`title` ASC, `document`.`path` ASC ';
             break;
         case 'category':
             // by parent folder (parent folder name or document title)
             $order = ' ORDER BY `parent`.`title` ASC, `parent`.`path` ASC ';
             break;
         case 'newest':
         default:
             // newest items first (newest documents created or file uploaded)
             $order = ' ORDER BY `document`.`created` DESC, `document`.`upload` DESC ';
             break;
     }
     $query = 'SELECT `document`.`title`,`document`.`path`, ';
     $query .= '`document`.`description`, `document`.`content`, ';
     $query .= '`document`.`created`,`document`.`upload`,  ';
     $query .= '`parent`.`title` AS `ptitle`,  ';
     // document folder title
     $query .= '`document`.`full_alias` ';
     $query .= 'FROM `#__joomdoc_flat` AS `document` ';
     $query .= 'LEFT JOIN `#__joomdoc_flat` AS `parent` ON `document`.`parent_path` = `parent`.`path`  ';
     // document folder
     $query .= $where . ' GROUP BY `document`.`path` ' . $order;
     $rows = $db->setQuery($query)->loadObjectList();
     foreach ($rows as $row) {
         $GLOBALS['joomDOCPath'] = $row->path;
         $row->title = JString::trim($row->title) ? $row->title : JFile::getName($row->path);
         $row->text = JString::trim($row->description) ? $row->description : $row->content;
         $row->created = $row->created == $nullDate ? $row->upload : $row->created;
         $row->section = JString::trim($row->ptitle) ? $row->ptitle : JoomDOCFileSystem::getParentPath($row->path);
         if ($this->params->get('document_link', 1) == 1) {
             $row->href = JRoute::_(JoomDOCRoute::viewDocuments($row->path, empty($row->full_alias) ? $row->path : $row->full_alias));
         } else {
             $row->href = JRoute::_(JoomDOCRoute::download($row->path, empty($row->full_alias) ? $row->path : $row->full_alias));
         }
         $row->browsernav = 2;
         // open document title in the same window
     }
     unset($GLOBALS['joomDOCPath']);
     return $rows;
 }
Esempio n. 4
0
 /**
  * Get document titles for selected paths.
  *
  * @param array $paths relative paths
  * @return array stdClass with params title and path (title can be empty if file hasn't document)
  */
 public function getPathsDocsTitles($paths)
 {
     $paths = $this->getQuotedArray($paths);
     if (!count($paths)) {
         return array();
     }
     $query = 'SELECT `title`, `path`, `full_alias` ';
     $query .= 'FROM `#__joomdoc_flat` AS `document` ';
     $query .= 'WHERE `path` IN (' . implode(', ', $paths) . ') AND ((' . JoomDOCModelList::getDocumentPublished() . ') OR `document`.`id` IN (NULL, 0))';
     $this->_db->setQuery($query);
     return $this->_db->loadObjectList();
 }
Esempio n. 5
0
 /**
  * Load document by path value.
  *
  * @param string $path file path
  * @return stdClass
  */
 public function getItemByPath($path)
 {
     $path = $this->_db->quote($path);
     $query = 'SELECT `id`, (' . JoomDOCModelList::getDocumentPublished() . ') AS `published`, `title`, `description`, `params`, `full_alias`, `created_by`, `state`, `created`, `modified`, `favorite`, `parent_path`, `file_state`, `access`, `download`, `file_id` ';
     foreach ($this->getPublishedFields() as $field) {
         $query .= ', `field' . $field->id . '` ';
     }
     $query .= 'FROM `#__joomdoc_flat` AS `document` 
     		   WHERE `path` = ' . $path;
     $item = $this->_db->setQuery($query)->loadObject();
     if (!$item) {
         $item = new stdClass();
     }
     $this->_db->setQuery('SELECT SUM(`hits`) FROM `#__joomdoc_file` WHERE `path` = ' . $path . ' GROUP BY `path`');
     $item->hits = $this->_db->loadResult();
     return $item;
 }
Esempio n. 6
0
 /**
  * Get SQL query for documents list.
  *
  * @return string
  */
 protected function getListQuery($searchActive = false)
 {
     $config = JoomDOCConfig::getInstance();
     $mainframe = JFactory::getApplication();
     /* @var $mainframe JApplication */
     // have concrete paths of search files
     $paths = $this->getState(JOOMDOC_FILTER_PATHS);
     // cleanup paths and quote
     $paths = $this->getQuotedArray($paths);
     if (count($paths)) {
         if (JRequest::getVar('com_joomdoc_scope') != 'mod_joomdoc_explorer') {
             $this->_db->setQuery('SELECT `path`, SUM(`hits`) AS `hits` FROM `#__joomdoc_file` ' . ($searchActive ? '' : 'WHERE `path` IN (' . implode(', ', $paths) . ')') . ' GROUP BY `path`');
             $this->hits = $this->_db->loadObjectList('path');
         }
     }
     if (!isset($this->docids) && !isset($this->fileids)) {
         $this->docids = $this->fileids = array();
     }
     $published = JoomDOCModelList::getDocumentPublished();
     $fields = $this->getListFields();
     $query = 'SELECT `id`, `title`, `description`, `full_alias`, `modified`, `created`, `created_by`, `state`, `params`, `favorite`, `ordering`, `publish_up`, (' . $published . ') AS `published`, `upload`, `parent_path`, `path`, `file_state`,  `license_id`, `license_title`, `license_alias`, `license_state`, `access`, `download`, `file_id` ';
     foreach ($this->getListFields() as $field) {
         $query .= ', `field' . $field->id . '` ';
     }
     // load from flat index
     $query .= 'FROM `#__joomdoc_flat` AS `document`';
     // filter for files
     if (!$searchActive) {
         $where[] = !empty($paths) ? '`path` IN (' . implode(', ', $paths) . ')' : 0;
     }
     // published files only
     $where[] = '`file_state` = ' . JOOMDOC_STATE_PUBLISHED;
     $filter = $this->getState(JOOMDOC_FILTER_SEARCH);
     // filter for keywords
     if ($searchActive) {
         $extra = array();
         if ($filter->parent) {
             $where[] = 'LOWER(`document`.`path`) LIKE ' . $this->_db->q(JString::strtolower($filter->parent) . '%');
         }
         if (!empty($filter->fields)) {
             foreach ($filter->fields as $id => $data) {
                 if ($data['type'] == JOOMDOC_FIELD_DATE || $data['type'] == JOOMDOC_FIELD_RADIO || $data['type'] == JOOMDOC_FIELD_SELECT) {
                     if ($data['value'] !== '') {
                         $where[] = '`field' . $id . '` = ' . $this->_db->quote($data['value']);
                     }
                 } elseif ($data['type'] == JOOMDOC_FIELD_CHECKBOX || $data['type'] == JOOMDOC_FIELD_MULTI_SELECT || $data['type'] == JOOMDOC_FIELD_SUGGEST) {
                     foreach ($data['value'] as $var => $val) {
                         if ($val !== '') {
                             // J!2.5 uses native JSON, J!3 converts JSON UCS-2BE into UTF-8 by MBString
                             $where[] = '(`field' . $id . '` LIKE ' . $this->_db->quote('%"' . $val . '"%') . ' OR `field' . $id . '` LIKE ' . $this->_db->quote('%' . $this->_db->escape(json_encode($val)) . '%') . ')';
                         }
                     }
                 } else {
                     if ($data['value'] || $config->searchKeyword == 2) {
                         $extra[] = $id;
                         // text field (text, textarea, editor) use in keyword searching
                     }
                 }
             }
         }
         if ($config->searchKeyword == 1) {
             // single keyword for all areas
             $keywords['global'] = JString::strtolower($filter->keywords);
         } elseif ($config->searchKeyword == 2) {
             // separate keyword for every area
             if ($config->searchShowTitle) {
                 // document title
                 $keywords['title'] = JString::strtolower($filter->keywords_title);
             }
             if ($config->searchShowText) {
                 // document description
                 $keywords['text'] = JString::strtolower($filter->keywords_text);
             }
             if ($config->searchShowMetadata) {
                 // document metadata
                 $keywords['meta'] = JString::strtolower($filter->keywords_meta);
             }
             if ($config->searchShowFulltext) {
                 // file content
                 $keywords['full'] = JString::strtolower($filter->keywords_full);
             }
             foreach ($extra as $id) {
                 // custom text fields keyword
                 $keywords['field' . $id] = JString::strtolower($filter->get('keywords_field' . $id));
             }
         }
         if ($filter->type == JOOMDOC_SEARCH_ANYKEY || $filter->type == JOOMDOC_SEARCH_ALLKEY) {
             // search for any/all word from keywords
             // split to unique words
             foreach ($keywords as $type => $keys) {
                 $keywords[$type] = explode(' ', $keys);
             }
         } else {
             // search for full phrase
             foreach ($keywords as $type => $keys) {
                 $keywords[$type] = array($keys);
             }
         }
         // cleanup and quote words
         foreach ($keywords as $type => $keys) {
             $keywords[$type] = $this->getQuotedArray($keys, $filter->type != JOOMDOC_SEARCH_REGEXP);
         }
         foreach ($keywords as $type => $keys) {
             foreach ($keys as $keyword) {
                 $criteria = array();
                 foreach ($extra as $id) {
                     if ($type == 'global' || $type == 'field' . $id) {
                         if ($filter->type == JOOMDOC_SEARCH_REGEXP) {
                             $criteria[] = '`field' . $id . '` REGEXP ' . $keyword;
                         } else {
                             $criteria[] = 'LOWER(`field' . $id . '`) LIKE ' . $keyword;
                         }
                     }
                 }
                 if ($filter->areaTitle && $type == 'global' || $type == 'title') {
                     // document title or file path
                     if ($filter->type == JOOMDOC_SEARCH_REGEXP) {
                         $criteria[] = '`title` REGEXP ' . $keyword;
                         $criteria[] = '`path` REGEXP ' . $keyword;
                     } else {
                         $criteria[] = 'LOWER(`title`) LIKE ' . $keyword;
                         $criteria[] = 'LOWER(`path`) LIKE ' . $keyword;
                     }
                 }
                 if ($filter->areaText && $type == 'global' || $type == 'text') {
                     // document description
                     if ($filter->type == JOOMDOC_SEARCH_REGEXP) {
                         $criteria[] = '`description` REGEXP ' . $keyword;
                     } else {
                         $criteria[] = 'LOWER(`description`) LIKE ' . $keyword;
                     }
                 }
                 if ($filter->areaFull && $type == 'global' || $type == 'full') {
                     // file content
                     if ($filter->type == JOOMDOC_SEARCH_REGEXP) {
                         $criteria[] = '`content` REGEXP ' . $keyword;
                     } else {
                         $criteria[] = 'LOWER(`content`) LIKE ' . $keyword;
                     }
                 }
                 if ($filter->areaMeta && $type == 'globa' || $type == 'meta') {
                     // need non escaped form
                     $keyword = JString::substr($keyword, 1, JString::strlen($keyword) - 2);
                     if ($filter->type == JOOMDOC_SEARCH_REGEXP) {
                         $metakeywords = $this->_db->quote('.*"metakeywords":"' . $keyword . '".*');
                         $metadescription = $this->_db->quote('.*"metadescription":"' . $keyword . '".*');
                     } else {
                         $metakeywords = $this->_db->quote('%"metakeywords":"' . $keyword . '"%');
                         $metadescription = $this->_db->quote('%"metadescription":"' . $keyword . '"%');
                     }
                     if ($filter->type == JOOMDOC_SEARCH_REGEXP) {
                         $criteria[] = '`params` REGEXP ' . $metakeywords;
                         $criteria[] = '`params` REGEXP ' . $metadescription;
                     } else {
                         $criteria[] = 'LOWER(`params`) LIKE ' . $metakeywords;
                         $criteria[] = 'LOWER(`params`) LIKE ' . $metadescription;
                     }
                 }
                 // find word in one of items
                 if (count($criteria)) {
                     $oneWordFilter[] = '(' . implode(' OR ', $criteria) . ')';
                 }
             }
         }
         if (isset($oneWordFilter)) {
             if ($filter->type == JOOMDOC_SEARCH_ALLKEY) {
                 // all words
                 $where[] = '(' . implode(' AND ', $oneWordFilter) . ')';
             } else {
                 // any word
                 $where[] = '(' . implode(' OR ', $oneWordFilter) . ')';
             }
         }
     }
     if (isset($where)) {
         $query .= ' WHERE ' . implode(' AND ', $where);
     }
     return $query;
 }