function MtreeBuildRoute(&$query) { global $mtconf, $listing_tasks; $segments = array(); $db =& JFactory::getDBO(); if (!class_exists('mtLinks')) { require_once $mtconf->getjconf('absolute_path') . '/administrator/components/com_mtree/admin.mtree.class.php'; } if (!isset($query['task'])) { return $segments; } switch ($query['task']) { case 'listcats': if (isset($query['cat_id'])) { $segments = appendCat($query['cat_id']); unset($query['cat_id']); if (isset($query['start'])) { $page = getPage($query['start'], $mtconf->get('fe_num_of_links')); $segments[] = $mtconf->get('sef_category_page') . $page; } } break; case 'viewlink': $mtLink = new mtLinks($db); $mtLink->load($query['link_id']); $segments = array_merge($segments, appendCat($mtLink->cat_id)); if (isset($query['start'])) { // http://example.com/c/mtree/Computer/Games/Donkey_Kong/reviews23 $page = getPage($query['start'], $mtconf->get('fe_num_of_reviews')); $segments = array_merge($segments, appendListing($mtLink->link_name, $mtLink->link_id, $mtLink->alias, false)); $segments[] = $mtconf->get('sef_reviews_page') . $page; } else { $segments = array_merge($segments, appendListing($mtLink->link_name, $mtLink->link_id, $mtLink->alias, false)); } unset($query['link_id']); break; case 'mypage': $segments[] = $mtconf->get('sef_mypage'); if (isset($query['start'])) { $page = getPage($query['start'], $mtconf->get('fe_num_of_links')); $segments[] = $mtconf->get('sef_category_page') . $page; } break; case 'listfeatured': case 'listnew': case 'listupdated': case 'listfavourite': case 'listpopular': case 'listmostrated': case 'listtoprated': case 'listmostreview': $type = strtoupper(substr($query['task'], 4)); $cat_id = getId('cat', $query); $segments = appendCat($cat_id); $segments[] = $mtconf->get('sef_' . strtolower($type)); if (isset($query['start'])) { $page = getPage($query['start'], $mtconf->get('fe_num_of_' . strtolower($type))); $segments[] = $mtconf->get('sef_category_page') . $page; } break; case 'advsearch': $segments[] = $mtconf->get('sef_advsearch'); break; case 'advsearch2': $segments[] = $mtconf->get('sef_advsearch2'); $search_id = getId('search', $query); $page = 1; if (isset($query['start'])) { $page = getPage($query['start'], $mtconf->get('fe_num_of_searchresults')); $segments[] = $search_id; $segments[] = $page; } else { $segments[] = $search_id; } break; case 'listalpha': $cat_id = getId('cat', $query); $segments = appendCat($cat_id); $segments[] = $mtconf->get('sef_listalpha'); $segments[] = urlencode($query['alpha']); unset($query['alpha']); if (isset($query['start'])) { $page = getPage($query['start'], $mtconf->get('fe_num_of_links')); $segments[] = $page; } break; case 'viewowner': case 'viewusersreview': case 'viewusersfav': $user_id = getId('user', $query); $db->setQuery("SELECT username FROM #__users WHERE id= " . $db->quote($user_id) . " AND block='0'"); $username = $db->loadResult(); if (!empty($username)) { switch ($query['task']) { default: $segments[] = $mtconf->get('sef_owner'); break; case 'viewusersreview': $segments[] = $mtconf->get('sef_reviews'); break; case 'viewusersfav': $segments[] = $mtconf->get('sef_favourites'); break; } $segments[] = murlencode($username); } if (isset($query['start'])) { $page = getPage($query['start'], $mtconf->get('fe_num_of_links')); $segments[] = $page; } break; case 'viewimage': $segments[] = $mtconf->get('sef_image'); $segments[] = getId('img', $query); break; case 'replyreview': $segments[] = $mtconf->get('sef_replyreview'); $segments[] = getId('rev', $query); break; case 'reportreview': $segments[] = $mtconf->get('sef_reportreview'); $segments[] = getId('rev', $query); break; // Listing's tasks // Listing's tasks case array_key_exists($query['task'], $listing_tasks) !== false: $mtLink = new mtLinks($db); $mtLink->load($query['link_id']); $segments = appendCatListing($mtLink, false); $segments[] = $listing_tasks[$query['task']]; unset($query['link_id']); break; case 'addlisting': case 'addcategory': if (isset($query['link_id'])) { $mtLink = new mtLinks($db); $mtLink->load(getId('link', $query)); $segments = appendCat($mtLink->cat_id); } elseif (isset($query['cat_id'])) { $segments = appendCat(getId('cat', $query)); } if ($query['task'] == 'addlisting') { $segments[] = $mtconf->get('sef_addlisting'); } else { $segments[] = $mtconf->get('sef_addcategory'); } break; case 'searchby': $cf_id = getId('cf', $query); $cat_id = getId('cat', $query); $segments = appendCat($cat_id); $segments[] = $mtconf->get('sef_searchby'); $segments[] = appendTag($cf_id); $segments[] = $query['value']; if (isset($query['start'])) { $page = getPage($query['start'], $mtconf->get('fe_num_of_searchresults')); $segments[] = $page; } unset($query['value']); break; case 'search': $cat_id = getId('cat', $query); $segments = appendCat($cat_id); $segments[] = $mtconf->get('sef_search'); $badchars = array('#', '>', '<', '\\'); $searchword = urldecode(trim(str_replace($badchars, '', $query['searchword']))); // limit searchword to 20 characters if (JString::strlen($searchword) > 20) { $searchword = JString::substr($searchword, 0, 19); } if (strpos($searchword, '?') !== false or strpos($searchword, '%') !== false or strpos($searchword, '/') !== false) { $searchword = urlencode($searchword); } $searchword = urlencode($searchword); if (isset($searchword) && !empty($searchword)) { $segments[] = $searchword; } // Retrieve configuration options - needed to know which SEF URLs are used $app =& JFactory::getApplication(); // Allows for searching on strings that include ".xxx" that appear to Apache as an extension if ($app->getCfg('sef') && $app->getCfg('sef_rewrite') && !$app->getCfg('sef_suffix') && strpos($searchword, '.') !== false) { $segments[] .= '/'; } unset($query['searchword']); break; case 'rss': $cat_id = getId('cat', $query); $segments = appendCat($cat_id); $segments[] = $mtconf->get('sef_rss'); if (isset($query['type']) && $query['type'] == 'new') { $segments[] = $mtconf->get('sef_rss_new'); } else { $segments[] = $mtconf->get('sef_rss_updated'); } unset($query['type']); break; } if ($query['task'] != 'search') { unset($query['start']); } unset($query['limit']); unset($query['task']); return $segments; }
public function validate($data) { $this->input = $data; $elements =& $this->useElements(); $validators = $output = $storage = array(); $this->_fire('pre_validate'); function murlencode($name, $k, $arr) { $out = array(); foreach ($arr as $k2 => $v) { $out[] = $name . '[' . $k . '][' . $k2 . ']=' . urlencode((string) $v); } return implode('&', $out); } foreach ($elements as $name => &$element) { $element['_name'] = $name; $this->elementTitle($element); // named form element if (is_string($name)) { if (!empty($element['required'])) { if (!$this->validateRequired($this, $name)) { $this->errors[$name][] = $this->errorMessage('required', $element); } } if (!empty($element['regex']) && empty($this->errors[$name])) { $match = preg_match('/^' . $element['regex'] . '$/', (string) $this->input($name, '')); if (!$match) { $this->errors[$name][] = $this->errorMessage('regex', $element); } } if (!empty($element['validation']) && empty($this->errors[$name]) && '' !== $this->input($name, '')) { $fn = $element['validation']; if (is_string($fn)) { $validationFunction = 'validate' . ucfirst($fn); $fn = array($this, $validationFunction); } if (is_callable($fn)) { $r = call_user_func($fn, $this, $name); if (false === $r || is_string($r)) { $this->errors[$name][] = false === $r ? isset($element['error']) ? $element['error'] : $this->errorMessage('custom', $element) : $r; } } } if (isset($this->input[$name])) { $elName = $this->elementName($element); if (array_key_exists($name, $this->output)) { $input = $this->output[$name]; } else { $input = $this->input[$name]; } $elStorage = isset($element['storage']) ? (string) $element['storage'] : 'default'; isset($storage[$elStorage]) or $storage[$elStorage] = array(); $storage[$elStorage][] = $elName; foreach ((array) $input as $k => $v) { $output[] = is_array($v) ? murlencode($elName, $k, $v) : $elName . '=' . urlencode((string) $v); } } } else { if (isset($element['validation'])) { // must have fields connection if (isset($element['fields'])) { $validators[] = $element; } } } unset($element); } // output -> array -> into $this $output = implode('&', $output); $this->output = array(); parse_str($output, $this->output); // split output array $this->output = $this->splitOutput($storage); $noErrors = empty($this->errors); // check extra (field agnostic) validators foreach ($validators as $validator) { // require previous validation for some fields? $require = isset($validator['require']) ? (array) $validator['require'] : null; // do this validator always, independant of previous/rest validation $always = !empty($validator['always']); // Always or No field errors so far if ($always || $noErrors) { // requirements met? if (empty($require) || !array_intersect($require, array_keys($this->errors))) { // validator must be class method or Closure $v = $validator['validation']; $v = is_string($v) ? array($this, $v) : $v; // execute validator function $r = $v($this); // false for failed with standard message, String for failed with response as message if (false === $r || is_string($r)) { // error message $error = false === $r ? $this->errorMessage('custom', $validator) : $r; // show message for these fields is_array($validator['fields']) or $validator['fields'] = explode(',', $validator['fields']); foreach ($validator['fields'] as $name) { $this->errors[trim($name)][] = $error; } } // validation response } // validator's requirements } // Always || NoErrors } // extra validators if (0 == count($this->errors)) { $this->_fire('post_validation'); return true; } $this->_fire('post_validation_failed'); return false; }