Wilcard (*) Match for Zend_Controller_Router_Route_Hostname - php

Is it possible to create a wildcard match using the Zend Framework Zend_Controller_Router_Route_Hostname for the actual domain? I tried the simple example below but the system would not recognize the route. When I hardwired the route (login.domain.com), it would work properly.
resources.router.routes.login.type = "Zend_Controller_Router_Route_Hostname"
resources.router.routes.login.route = "login.*"
resources.router.routes.login.chains.index.type = "Zend_Controller_Router_Route"
resources.router.routes.login.chains.index.route = ":action/*"
resources.router.routes.login.chains.index.defaults.controller = "login"
resources.router.routes.login.chains.index.defaults.action = "index"

$route = new Zend_Controller_Router_Route_Hostname ('login.:domain.:net');
$_SERVER ['HTTP_HOST'] = 'login.example.com';
$request = new Zend_Controller_Request_Http ();
$match = $route->match ($request);
var_dump($match);

Is this possible without the :domain.:net both being explicit, i.e. without stipulating just one period?
i.e. I currently have
new Zend_Controller_Router_Route_Hostname('sub.example.com',array('controller' => 'x'));
..but what I'd really like to do is:
new Zend_Controller_Router_Route_Hostname('sub.:remainder',array('controller' => 'x'));
..whereby this route will match any hostname that begins with 'sub.' including sub.example.com, sub.another.example.com, sub.somethingelse.com, sub.com etc.
Doesn't seem to work though!
Anyone got this working?

Related

Stuck with REQUST_URI Parsing ( PHP )

I have a page working as I need it to, with the last /arist-name/ parsing into the correct variable, but the client is adding /artist-name/?google-tracking=1234fad to their links, which is breaking it.
http://www.odonwagnergallery.com/artist/pierre-coupey/ WORKS
http://www.odonwagnergallery.com/artist/pierre-coupey/?mc_cid=b7e918fce5&mc_eid=[UNIQID] DOES NOT WORK
$expl = explode("/",$_SERVER["REQUEST_URI"]);
$ArtistURL = $expl[count($expl)-1];
$ArtistURL = preg_replace('/[^a-z,-.]/', '', $ArtistURL);
Please help, I have been searching for a solution. Thanks so much!
PHP has a function called parse_url which should clean up the request uri for you before you try to use it.
parse_url
Parse a URL and return its components
http://php.net/parse_url
Example:
// This
$url_array = parse_url('/artist/pierre-coupey/?mc_cid=b7e918fce5&mc_eid=[UNIQID]');
print_r($url_array);
// Outputs this
Array
(
[path] => /artist/pierre-coupey/
[query] => mc_cid=b7e918fce5&mc_eid=[UNIQID]
)
Here is a demo: https://eval.in/873699
Then you can use the path piece to perform your existing logic.
If all your URLs are http://DOMAIN/artist/SOMEARTIST/
you could do:
$ArtistURL = preg_replace('/.*\/artist\/(.*)\/.*/','$1',"http://www.odonwagnergallery.com/artist/pierre-coupey/oij");
It would work in this context. Specify other possible scenarios if there are others. But #neuromatter answer is more generic, +1.
if you simply want to remove any and all query parameters, this single line would suffice:
$url=explode("?",$url)[0];
this would turn
http://www.odonwagnergallery.com/artist/pierre-coupey/?mc_cid=b7e918fce5&mc_eid=[UNIQID]&anything_else=whatever
into
http://www.odonwagnergallery.com/artist/pierre-coupey/
but if you want to specifically remove any mc_cid and mc_eid parameters, but otherwise keep the url intact:
$url=explode("?",$url);
if(count($url)===2){
parse_str($url[1],$tmp);
unset($tmp['mc_cid']);
unset($tmp['mc_eid']);
$url=$url[0].(empty($tmp)? '':('?'.http_build_query($tmp)));
}else if(count($url)===1){
$url=$url[0];
}else{
throw new \LogicException('malformed url!');
}
this would turn
http://www.odonwagnergallery.com/artist/pierre-coupey/?mc_cid=b7e918fce5&mc_eid=[UNIQID]&anything_else=whatever
into
http://www.odonwagnergallery.com/artist/pierre-coupey/?anything_else=whatever

How to retrieve script name from full absolute URL using PHP?

I'm able to retrieve the full URL like: http://www-click08-co-uk/wonga.php
and I need to retrieve the script name "wonga" from it.
The url will be changing depending on what page the user is on and I will always need the word or phrase after the / and not including the .php, in the example above I would like to create a variable with the value of this being wonga
This is the code I currently have, where "argos" is, is where the database is searched and responds with the information I need, this is where the vaiable would be used
<?php
//-----------------------------------------------------
// Include files and set Classes
//-----------------------------------------------------
require_once $_SERVER["DOCUMENT_ROOT"] . "/includes/common.php";
$db = new dbConnection();
$directorydata = new directorydata();
$phoneDirectory = new phoneDirectory();
$conn = $db->pdoConnect();
// Load the directorydata row via the row ID - 543 is "best buy"
//$directorydata->get($db, 543);
// Load the directorydata row via the url alias field
$directorydata->get($db, "Argos");
// Phone number isn't formatted coming out the DB
$formattedPhoneNumber = $phoneDirectory->formatPhoneNumber($directorydata->Number1);
?>
Just because you didn't provide any code, I provide you a way to solve this on your own:
$url = "http://www-click08-co-uk/wonga.php";
// SEARCH and replace
// FIRST_FUNCTION => google => php trailing name component of path
// SECOND_FUNCTION => google => php explode a string by string
// THIRD_FUNCTION => google => php pop first element of array
$urlParts = SECOND_FUNCTION( ".", FIRST_FUNCTION( $url ) );
echo THIRD_FUNCTION( $urlParts );
OUTPUT:
wonga
An example use of parse_url could be:
$url = 'http://www-click08-co-uk/wonga.php?page=74';
$route = parse_url($url, PHP_URL_PATH);
$routeTokens = explode('/', $route);
$scriptName = array_pop($routeTokens);
echo $scriptName;
which in this case outputs wonga.php.
Just note that this is a very rare task that you would have to take care of yourself. So instead of parse_url you might look at the bigger picture here and start looking for some good MVC framework.

Yii using a variable with an IN condition

I am trying to pull information into a page using my model. The issue is that I need to use an IN condition on my mysql using a variable.
Here is the code I use currently
$list_id = '1,3';
$clients = ListSubscriber::model()->findAll(array('condition'=>'list_id IN (:list_id)','params'=>array(':list_id'=>$list_id)));
I won't necessarily know how many numbers will be stored within $list_id, hence the need for a variable to work with the IN.
The code does execute without errors, but only seems to return the values for the first number of $list_id, so in this case it only finds users where the list_id = 1.
Any help is appreciated. I have found this question Yii addInCondition
However they are using static values, which does not resolve my issue.
When I do use static values, the code executes with results as expected.
You can use addInCondition :
$list_id = '1,3';
$criteria = new CDbCriteria();
$arr_list_id = explode(",",$list_id);
$criteria->addInCondition("list_id ", $arr_list_id );
$clients = ListSubscriber::model()->findAll($criteria);
$list_ids = array(1,3);
$clients = ListSubscriber::model()->findAllByAttributes(array('list_id'=>$list_ids));

get subdomain in php

I have following urls.
reservation.abchotel.com
booking.abchotels.org
abc1.abc.dev
I want to get sub domain from above urls.
ex:-
.abchotel.com
.abchotels.org
.abc.dev
How I do it? I'm using zend feamwork. Please help me. What is the best solution?
It seems you don't want the subdomain but the domain. Because what you listed are not the subdomains.
The following pieces of knowledge will enable you to successfully deal with domain names.
Zend_Validate_Hostname - also good to look at the code
PHP Server variables
String manipulation in general. Hostnames are easy to in- and explode since they're always delimited by dots.
PHP parse_url
See also[this question on SO on how to get subdomain(s) from an url.
If they are always in that form you could do something like the following (assuming $url is set).
$split_url = explode(".", $url);
$subdomain = ".".$split_url[1].".".$split_url[2];
Or did you want to know how to get the URL in the first place too, or to allow for more than 3-level domains?
A very simple way to get domain and subdomain :
$parts = explode('.', $_SERVER['HTTP_HOST']);
$domain = '.' . implode( '.', array_reverse(
array(
array_pop($parts),
array_pop($parts)
)
);
$subdomain = implode('.',$parts);
$url = $_SERVER["SERVER_NAME"];
$replace_domains = array(
".abchotel.com" => "",
".abchotels.org" => "",
".abc.dev" => "");
$url = str_replace(array_keys($replace_domains), array_values($replace_domains), $url);
echo $url;

Symfony and Zend Lucene Error

I use symfony with Zend Lucene Search. I have
$query = Zend_Search_Lucene_Search_QueryParser::parse($query.'*');
$hits = self::getLuceneIndex()->find($query);
Sometimes I have error :
At least 3 non-wildcard characters are required at the beginning of pattern.
When I make like in documentations:
$pattern = new Zend_Search_Lucene_Index_Term($query.'*');
$query = new Zend_Search_Lucene_Search_Query_Wildcard($pattern);
$hits = self::getLuceneIndex()->find($query);
It finds nothing.
I do not is it right , but it is work for me :
So, query fail in my case, because it have < 3 characters or have some special characters, so in my search action :
public function executeAds(sfWebRequest $request)
{
if (!$query = $request->getParameter('query'))
{
return $this->forward('search', 'adssearch');
}
$query = str_replace(" ", "", $query);
$query = preg_replace("/[^A-Za-z0-9]/","",$query);
if (strlen(trim($query))<3)
{
$this->redirect('search/notice');
}
$this->ads = Doctrine_Core::getTable('Ads') ->getAdsLuceneQuery($query);
I do not use
$pattern = new Zend_Search_Lucene_Index_Term($query.'*');
$query = new Zend_Search_Lucene_Search_Query_Wildcard($pattern);
$hits = self::getLuceneIndex()->find($query);
Because it is not work for me.
Taken directly from the Zend Reference documentation, you can use:
Zend_Search_Lucene_Search_Query_Wildcard::getMinPrefixLength() to
query the minimum required prefix length and
use Zend_Search_Lucene_Search_Query_Wildcard::setMinPrefixLength() to
set it.
So my suggestion would be either of two things:
Set the prefixMinLength to 0 using Zend_Search_Lucene_Search_Query_Wildcard::setMinPrefixLength(0) - based on this, your original code snippet should work fine (it did for my Zend Lucene implementation)
As you yourself suggested, validate all search queries using javascript or otherwise to ensure there is a minimum of Zend_Search_Lucene_Search_Query_Wildcard::getMinPrefixLength() before any wildcards used (I recommend querying that instead of assuming the default of "3" so the validation is flexible)

Categories