Skip to content

Instantly share code, notes, and snippets.

@kechol
Created December 18, 2011 09:46
Show Gist options
  • Save kechol/1492880 to your computer and use it in GitHub Desktop.
Save kechol/1492880 to your computer and use it in GitHub Desktop.
2年前くらいに作った携帯用のサイトエンジン(for PHP4) HTML_Emojiを利用して3キャリアで絵文字が表示できたりした。
<?php
require_once 'Mobile.class.php';
class HogeMobile extends Mobile {
var $title = 'TItle';
var $key_color = '#000';
var $link_color = '#00f';
var $visited_color = '#00f';
var $active_color = '#f00';
function head($page) {
parent::start();
echo <<<END
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja">
<head>
<meta http-equiv="Content-Style-Type" content="text/css" />
<title>{$page}|{$this->title}</title>
<style type="text/css">
<!--
a{text-decoration:none}
a:link{color:{$this->link_color}}
a:visited{color:{$this->visited_color}}
a:hover{color:white;background-color:{$this->active_color}}
a:focus{color:white;background-color:{$this->active_color}}
a:active{color:white;background-color:{$this->active_color}}
-->
</style>
</head>
<body style="margin:0 auto">
<h1 style="margin:0" id="top" name="top">Title</h1>
<div style="line-height:1.4em;font-size:small">
END;
if($this->selectCarrier() == $this->pc) {
$this->hr();
echo <<<END
<div style="text-align:center">!! このサイトは携帯用です。 !!</div>
END;
$this->hr();
}
}
function foot() {
$this->hr();
echo <<<END
 <a href="#top" accesskey="9" style="font-size:x-small">▲ページトップ</a><br />
 <a href="index.php" title="{$this->title}" accesskey="0" style="font-size:x-small">ホーム</a>
END;
$this->hr();
echo <<<END
<div style="text-align:center;font-size:x-small">
(c) @Kechol
</div>
</div>
</body>
</html>
END;
parent::finish();
}
function headline($str) {
echo '<div style="margin:10px 0 5px;line-height:1.4em;color:#fff;background:'.$this->key_color.';font-size:x-small">'.$str.'</div>'."\n";
}
function hr() {
echo '<hr style="color:'.$this->key_color.';background:'.$this->key_color.';height:2px;border:0px solid '.$this->key_color.';margin:5px 0" size="3" />'."\n";
}
function twitter_timeline($url) {
$data = $this->twitter($url);
if(!$data) {
echo "現在表示できる情報はありません。";
} else {
foreach($data as $status) {
$text = $status['text'];
//$date = substr($status['created_at'], 0, 11);
$date = $status['created_at'];
echo '<div style="color:#ccc;font-size:xx-small">'.$date.'</div>';
echo '<div style="font-size:small">'.$text.'</div>';
$this->hr();
}
}
}
}
<?php
require_once 'Emoji/Emoji.php';
class Mobile {
var $docomo = 'i';
var $au = 'e';
var $softbank = 's';
var $pc = 'pc';
var $emoji;
var $carrier;
var $serial;
function __construct() {
mb_internal_encoding('utf-8');
}
function start() {
$this->emoji = HTML_Emoji::getInstance();
$this->emoji->setImageUrl('Emoji/images/');
$this->emoji->useHalfwidthKatakana();
header("Content-Type: application/xhtml+xml;");
if($this->selectCarrier() == $this->au) {
header('Cache-Control: no-cache');
}
ob_start();
switch($this->selectCarrier()) {
case $this->pc:
case $this->docomo:
echo '<!DOCTYPE html PUBLIC "-//i-mode group (ja)//DTD XHTML i-XHTML(Locale/Var.=ja/2.3) 1.0//EN" "i-xhtml_4ja_10.dtd">';
break;
case $this->au:
echo '<!DOCTYPE html PUBLIC "-//OPENWAVE//DTD XHTML 1.0//EN" "http://www.openwave.com/DTD/xhtml-basic.dtd">';
break;
case $this->softbank:
echo '<!DOCTYPE html PUBLIC "-//J-PHONE//DTD XHTML Basic 1.0 Plus//EN" "xhtml-basic10-plus.dtd">';
break;
}
}
function finish() {
$content = ob_get_contents();
ob_end_clean();
$content = $this->emoji->convertCarrier($content);
if ($this->emoji->isSjisCarrier()) {
$content = $this->emoji->convertEncoding($content, 'sjis', 'utf-8');
$charset = 'Shift_JIS';
} else {
$charset = 'utf-8';
}
header('Content-Type: text/html; charset=' . $charset);
echo $content;
}
function selectCarrier() {
if($this->carrier != null) {
return $this->carrier;
}
$uaList = array(
array(
'regexp' => '!^DoCoMo!',
'carrier' => $this->docomo
),
array(
'regexp' => '!^KDDI-!',
'carrier' => $this->au
),
array(
'regexp' => '!^UP\.Browser!',
'carrier' => $this->au
),
array(
'regexp' => '!^SoftBank!',
'carrier' => $this->softbank
),
array(
'regexp' => '!^Vodafone!',
'carrier' => $this->softbank
),
array(
'regexp' => '!^J-PHONE!',
'carrier' => $this->softbank
),
array(
'regexp' => '!^MOT-!',
'carrier' => $this->softbank
),
array(
'regexp' => '!^Semulator!',
'carrier' => $this->softbank
),
array(
'regexp' => '!^Vemulator!',
'carrier' => $this->softbank
),
array(
'regexp' => '!^J-EMULATOR!',
'carrier' => $this->softbank
),
array(
'regexp' => '!^MOTEMULATOR!',
'carrier' => $this->softbank
)
);
$ua = $_SERVER['HTTP_USER_AGENT'];
foreach($uaList as $item) {
if(preg_match($item['regexp'], $ua)) {
$this->carrier = $item['carrier'];
break;
}
}
if($this->carrier == null) {
$this->carrier = $this->pc;
}
return $this->carrier;
}
function getSerial() {
switch($this->selectCarrier()) {
case $this->docomo:
$this->serial = $_SERVER['HTTP_X_DCMGUID'];
break;
case $this->softbank:
$this->serial = $_SERVER['HTTP_X_JPHONE_UID'];
break;
case $this->au:
$this->serial = $_SERVER['HTTP_X_UP_SUBNO'];
break;
}
return $this->serial;
}
function generateGPSLink($url, $anchor) {
switch($this->selectCarrier()) {
case $this->docomo:
echo '<a href="'.$url.'" lcs>'.$anchor.'</a><br />'."\n";
echo "<span style=\"font-size:x-small\">DoCoMo movaなど一部の機種には対応しておりません。</span><br />\n";
break;
case $this->au:
echo '<a href="device:gpsone?url='.$url.'&ver=1&datum=1&unit=0&acry=0&number=0">'.$anchor.'</a><br />'."\n";
//echo '<a href="device:location?url='.$url.'">'.$anchor.'</a><br />'."\n";
echo "<span style=\"font-size:x-small\">一部の機種には対応しておりません。</span><br />\n";
break;
case $this->softbank:
echo '<a href="location:auto?url='.$url.'">'.$anchor.'</a><br />'."\n";
echo "<span style=\"font-size:x-small\">一部の機種には対応しておりません。</span><br />\n";
break;
default:
echo "<span style=\"font-size:x-small\">お手持ちの機種では当サイトでGPS機能をお使いいただくことができません。</span>\n";
}
}
function getLocation($dd_lat = null, $dd_lon = null, $goto = null) {
$dms_lat;
$dms_lon;
$pos;
if(empty($dd_lat)&&empty($dd_lon)) {
switch($this->selectCarrier()) {
case $this->docomo:
case $this->au:
$pos_lat = htmlspecialchars($_GET['lat']);
$pos_lon = htmlspecialchars($_GET['lon']);
$dms_lat = explode(".", substr($pos_lat, 1));
$dms_lon = explode(".", substr($pos_lon, 1));
$dd_lat = $dms_lat[0] + $dms_lat[1] / 60 + $dms_lat[2] / 3600 + $dms_lat[3] / 360000;
$dd_lon = $dms_lon[0] + $dms_lon[1] / 60 + $dms_lon[2] / 3600 + $dms_lon[3] / 360000;
break;
case $this->softbank:
$pos = htmlspecialchars($_GET['pos'], ENT_QUOTES);
$pos = strtr($pos, array("N"=>"+", "S"=>"-", "W"=>"+", "E"=>"-"));
$pos_arr = preg_split("/[\+\-]/", $pos);
$dms_lat = explode(".", $pos_arr[1]);
$dms_lon = explode(".", $pos_arr[2]);
$dd_lat = $dms_lat[0] + $dms_lat[1] / 60 + $dms_lat[2] / 3600 + $dms_lat[3] / 360000;
$dd_lon = $dms_lon[0] + $dms_lon[1] / 60 + $dms_lon[2] / 3600 + $dms_lon[3] / 360000;
break;
default:
echo "申し訳ありませんが、お使いの携帯電話では当サイトのGPS機能をお使いになれません。<br />\n";
}
} elseif(!empty($goto)) {
switch($goto) {
case 'n':
$dd_lat += 0.001;
break;
case 's':
$dd_lat -= 0.001;
break;
case 'w':
$dd_lon -= 0.0005;
break;
case 'e':
$dd_lon += 0.0005;
break;
}
}
return array('lat' => $dd_lat, 'lon' => $dd_lon);
}
function twitter($url) {
ini_set('user_agent', 'User-Agent: '.$_SERVER['HTTP_USER_AGENT']);
try {
$json_data = @file_get_contents($url);
} catch(Exception $e) {
$data = null;
}
if(isset($json_data)) {
$data = json_decode($json_data, true);
}
return $data;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment