/*
Reference: http://myguide.bagarinao.com/index.php?/archives/7-Extending-phpWebSite-for-mobile-access.html
Date: 15 May 2007
*/
/* this function should be set to return the base directory of phpWebsite
relative to where this file is stored. This is currently set to be at the
root directory of the phpWebSite installation.
*/
function getPWSPath() {
return(realpath("."));
}
/* extracted from core's Text.php
this function will be called by breaker()
*/
function my_sentence($text, $stripNewlines = FALSE){
if (!is_string($text)) exit ("my_sentence() was not sent a string");
if (strstr($text, "\r"))
$text_array = explode("\r\n",$text);
else
$text_array = explode("\n",$text);
return $text_array;
}// END FUNC my_sentence()
/* extracted also from core's Text.php
its main function is to replace newlines with
*/
function breaker($text){
if (!is_string($text)) exit ("breaker() was not sent a string");
$text_array = my_sentence($text);
$lines = count($text_array);
$endings = array ("
",
"
",
"",
"<\/?p.*>",
"<\/?area.*>",
"<\/?map.*>",
"<\/?li.*>",
"<\/?ol.*>",
"<\/?ul.*>",
"<\/?dl.*>",
"<\/?dt.*>",
"<\/?dd.*>",
"<\/?table.*>",
"<\/?th.*>",
"<\/?tr.*>",
"<\/?td.*>",
"<\/?h..*>");
$loop = 0;
$search_string = NULL;
foreach ($endings as $tag){
if ($loop) $search_string .= "|";
$search_string .= $tag."\$";
$loop = 1;
}
$count = 0;
$content = NULL;
$preFlag = false;
foreach ($text_array as $sentence){
$count++;
if ($count < $lines){
if(!$preFlag) {
if(preg_match("/
\$/iU", trim($sentence))) {
$preFlag = true;
$content .= $sentence."\n";
continue;
}
if (!preg_match("/".$search_string."/iU" , trim($sentence))) $content .= $sentence."
\n";
else $content .= $sentence."\n";
} else if(preg_match("/<\/pre>\$/iU", trim($sentence))) {
$preFlag = false;
$content .= $sentence."\n";
continue;
} else {
$content .= $sentence."\n";
}
} else
$content .= $sentence;
}
return $content;
}// END FUNC breaker()
/* retrieves the most recent entries */
function get_current_entries($db) {
global $table_prefix, $source_http;
$entries = '';
$sql = "SELECT id, title, updated_date FROM ". $table_prefix;
$sql .= "mod_article ORDER BY updated_date DESC LIMIT 10";
$res=$db->query($sql);
while($data = $res->fetchRow(DB_FETCHMODE_ASSOC)) {
$id=$data['id'];
$title=$data['title'];
$entries .= "
$title";
$entries .= "
updated on ".$data['updated_date'] ."
";
}
if (!empty($entries)) {
$entries = "Recent Articles
" . $entries . "
";
}
return $entries;
}
/* retrieves an entry from the database */
function view_entry($db,$id) {
global $table_prefix, $theTitle;
$entry = '';
// retrieve article info
$sql = "SELECT * FROM ". $table_prefix;
$sql .= "mod_article WHERE id = ". $id;
$res = $db->query($sql);
$data = $res->fetchRow(DB_FETCHMODE_ASSOC);
if ($data == NULL) {
$entry = "ERROR: Cannot retrieve information for entry id = $id";
return $entry;
}
$order = unserialize($data['section_order']);
$entry .= "" . $data['title'] . "
";
$theTitle = $data['title'] . ' - Mobile Edition';
if (count($order)>0) {
$sql = "SELECT * FROM " . $table_prefix;
$sql .= "mod_article_sections ";
$sql .= "WHERE id IN (" . implode(',',$order) . ")";
$res = $db->query($sql);
$body = '';
while ($secData = $res->fetchRow(DB_FETCHMODE_ASSOC)) {
$body .= "".$secData['title']."
";
$body .= breaker($secData['text']);
}
if (!empty($body))
$entry .= "" . $body . "";
}
// increment the number of hits
$sql = "UPDATE " . $table_prefix . "mod_article ";
$sql .= "SET hits=hits+1 WHERE id=".$id;
$res = $db->query($sql);
return $entry;
}
$theEntry = ''; // the html code that will be displayed
$theTitle = 'Mobile Edition'; // title of the page
// get PWS installation directory
$pws = getPWSPath();
// get full path of the config.php file
$config = $pws."/conf/config.php";
// check if config.php exists, exit if not
file_exists($config) or die("Error loading page!");
// load config file
require($config);
// set pear library's path to the included path
set_include_path(get_include_path() . ':' . $pws . '/lib/pear/');
// load pear's DB class in i/file:///C|/baggy/myweb/test/DB.php file
require_once('DB.php');
// form connection string. NOTE: $dbversion, $dbuser, etc.
// are specified in config.php
$conn = "$dbversion://$dbuser:$dbpass@$dbhost/$dbname";
// connect to the database
$db = DB::connect($conn);
if (isset($_REQUEST['v']) && is_numeric($_REQUEST['v'])) {
$theEntry = view_entry($db,$_REQUEST['v']);
} else {
$theEntry = get_current_entries($db);
}
$db->disconnect();
?>
echo $theTitle; ?>
Mobile Edition
echo $theEntry;
?>
_____
Sample script from myGUIDE