Notice: Undefined index: HTTP_ACCEPT in /var/local/cache/midgard/midgard/31-100-217-0.php(66) : eval()'d code on line 11

Blog: category "php"

Mobilephone browser detecting from PHP

Posted on 2009-04-17 10:07:34 EEST to .

While ago friend of me asked for a script to detect if site is browsed with mobile device.
There are lot of these around on the net and here is my contribution to that list.

Hopefully someone else find this usefull also:

 

public function is_mobile($include_iphone_and_ipod=true)
{
$mobile_browser = 0;
$user_agent = $this->get_value('_SERVER', 'HTTP_USER_AGENT');

if (preg_match('/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone)/i', strtolower($user_agent))) {
$mobile_browser++;
}

if (! $include_iphone_and_ipod) {
if (preg_match('/(iphone|ipod)/i', strtolower($user_agent))) {
$mobile_browser--;
}
}

if ( (strpos(strtolower($this->get_value('_SERVER', 'HTTP_ACCEPT')),'application/vnd.wap.xhtml+xml')>0)
|| (( $this->has_value('_SERVER', 'HTTP_X_WAP_PROFILE')
|| $this->has_value('_SERVER', 'HTTP_PROFILE'))))
{
$mobile_browser++;
}

$mobile_ua = strtolower(substr($user_agent, 0, 4));
$mobile_agents = array(
'w3c ','acs-','alav','alca','amoi','audi','avan','benq','bird','blac',
'blaz','brew','cell','cldc','cmd-','dang','doco','eric','hipt','inno',
'ipaq','java','jigs','kddi','keji','leno','lg-c','lg-d','lg-g','lge-',
'maui','maxo','midp','mits','mmef','mobi','mot-','moto','mwbp','nec-',
'newt','noki','oper','palm','pana','pant','phil','play','port','prox',
'qwap','sage','sams','sany','sch-','sec-','send','seri','sgh-','shar',
'sie-','siem','smal','smar','sony','sph-','symb','t-mo','teli','tim-',
'tosh','tsm-','upg1','upsi','vk-v','voda','wap-','wapa','wapi','wapp',
'wapr','webc','winw','winw','xda','xda-'
);

if (in_array($mobile_ua, $mobile_agents)) {
$mobile_browser++;
}
if ( $this->has_value('_SERVER', 'ALL_HTTP')
&& strpos(strtolower($this->get_value('_SERVER', 'ALL_HTTP')), 'OperaMini') > 0)
{
$mobile_browser++;
}

if (strpos(strtolower($user_agent), 'windows') > 0) {
$mobile_browser = 0;
}

if ($mobile_browser > 0) {
return true;
}

return false;
}

Crash and burn

Posted on 2008-09-02 15:36:28 EEST to .

Last friday I collided to quite frustrating moment while doing user import to clients Midgard site.

I created a simple import script to parse the old data from CSV and insert them as Midgard persons. After the script runned for a while I started seeing errors that the MySQL wasn't responding. Checked the server and realized that all disks were mounted as read-only and I couldn't mount them back. So after weekend of strugling with the server we decided with the hosting company to move all the data to new server.

After this I made some minor modifications to the import script so it sleeps 2 minutes after every 250 imported users. And monitored the server all the time. After 5hours of importing everything was ready and the server is still doing ok.

So just a small advice to anyone who has to do big imports (over 18k objects), add some sleep time to the import script so the server can take a deep breath once in a while.

on the next Midgard dev meeting I'll try to do this again on a development server and trace for memory leaks.

Midgard

PHP5 xmlObject

Posted on 2008-08-05 18:37:23 EEST to .

UPDATE [08/06/2008]: Version update. Did some fixing and made it work better with larger structures.

Here's another simple little helper I developed for Duct framework.
I use this one to convert my arrays to XML. As Duct automatically scaffolds objects from XML/YAML to database
and uses the same methods to render the database objects to html forms for editing I needed some quick way to
transform this data to and from XML. Class end examples are here

Usage example:

<?php
$resource = new xmlObjectItem();
$resource->items = array(
'id' => 100,
'name' => "Jerry Jalava",
'person_id' => 1
);
$resource->attributes = array(
'person_id' => array(
'is_assoc' => true,
'foreign_model' => 'person',
'foreign_key' => 'id'
)
);

$resource_set = new xmlObjectSet();
$resource_set->wrapper_id = 'resource';
$resource_set->items[] = $resource;

$projects = array();
for ($i=1; $i <= 1; $i++)
{
$object = new xmlObjectItem();
$object->items = array(
'id' => $i,
'name' => "Project {$i}",
'manager_id' => $i+1,
'resources' => $resource_set
);

$projects[] = $object;
}

$project_set = new xmlObjectSet();
$project_set->wrapper_id = 'project';
$project_set->items = $projects;

$clients = array();
for ($i=1; $i <= 1; $i++)
{
$client = new xmlObjectItem();
$client->items = array(
'id' => $i,
'name' => "Client {$i}",
'contact_id' => $i+1,
'team-leader' => null,
'projects' => $project_set
);
$client->attributes = array(
'contact_id' => array(
'is_assoc' => true,
'foreign_model' => 'person',
'foreign_key' => 'id'
)
);

$clients[] = $client;
}
$client_set = new xmlObjectSet();
$client_set->wrapper_id = 'client';
$client_set->items = $clients;

echo "complex set:\n";
var_dump($client_set);

echo "\n--------toXml---------\n";

$xmlSet_clients = xmlObjects::toXml($client_set, 'clients');
echo "complex:\n";
echo $xmlSet_clients;

echo "\n--------fromXml---------\n";

$client_set = xmlObjects::fromXml($xmlSet_clients);
echo "client_set root: {$client_set->root}\n";
var_dump($client_set);

echo "\n--------back toXml---------\n";

$xmlSet_clients2 = xmlObjects::toXml($client_set);
echo "complex:\n";
echo $xmlSet_clients2;

if ($xmlSet_clients == $xmlSet_clients2) {
echo "\n\nVALID SET CONVERSION\n\n";
}

?>

PHP5 events class

Posted on 2008-08-04 12:42:01 EEST to .

While I was building Duct Frameworks server side with PHP5 I realized I needed some kind of event support for it.
So I created similar approach that ActionScript uses. You can download the class with example from here.

Hopefully this comes in handy for someone else also. 

Here is simple example also:

 

<?php

class mother extends events_observable
{
public function __construct()
{
parent::__construct();

$this->addEventListener(events_event::ONCONSTRUCT, events::delegate($this, 'on_object_constructed'));
}

public function on_object_constructed($target, $args)
{
echo "{$target->name} has born.";
}
}

class child extends events_observable
{
public $name;

public function __construct($name)
{
parent::__construct();

$this->name = $name;

$this->dispatchEvent(new events_event(events_event::ONCONSTRUCT, $this));
}
}

$mother = new mother();
$child = new child('Jack');

?>

Duct Framework

Posted on 2008-07-17 18:15:30 EEST to .

Duct is something I have planned doing for a while and started coding last week.

What is Duct? (or what it will be)

Duct is a Flash framework with different generators and tools.
with single click it can generate dynamic Flash Sites, Banners, Polls, Surveys, etc. (through engines)
It has it's own server implementation also which allows dynamic content, mail and other features
for Flash. Server side is written with PHP but I have planned a Python version of it also.

Currently Duct UI is built to Flash IDE but I have plans to add Commandline, Online and Standalone support also.

I'm going to publish this as a Open Source project as soon as it gets a bit more mature.
Also more about the whole project will be published later.

Here are some screenshots of the Banner Engine. (And scaffolded banner project.)

scaffoldedBanner.png
(All these files have been generated after pressing the "Create" -button)