xFacility/Codes2010. 5. 17. 23:27
1.0 버전에서 코드 효율성을 위하여 새로 작성한 2.0 버전입니다.
Database 입출력처리는 기존 버전의 코드를 계승했습니다.

//Part1. Common API
//1. [xF]Interaction
//1.1. Text
//1.1.1. Trim
function xfi_trm_str($string, $character = ",;") {
/*
DEVELOPMENT:
Michael Son(michaelson@nate.com)
28.Apr.2010.
DESCRIPTION:
Trim a special character(comma and semicolon) at end of string
CALL:
xfi_trm_str("lang=en,ko,jp,ch;hobby=reading books,programming,bowling;");
RETURN:
$string = "lang=en,ko,jp,ch;hobby=reading books,programming,bowling";
*/
//Define times for a Loop
$length = strlen($character);
//Check each character by a loop
for ($i=0; $i<$length; $i++) {
//If the end of string is a appointed character,
if(substr($string,-1) == substr($character, $i, 1)) {
//Delete the last character of string
$string = substr($string, 0, -1);
}
}
//Return a trimmed string
return $string;
}

//1.1.2. Range
//1.1.2.1. Parse a range
function xfi_prs_ran ($range) {
/*
DEVELOPMENT:
Michael Son(michaelson@nate.com)
28.Apr.2010.
DESCRIPTION:
Parse Range to Array
CALL:
xfi_prs_ran("1-3,7,10-11");
RETURN:
$array[0] = "1";
$array[1] = "2";
$array[2] = "3";
$array[3] = "7";
$array[4] = "10";
$array[5] = "11";
*/
//Trim a string
$range = xfi_trm_str($range);
$range = trim($range);
//Parse a string by comma
$temp = split(",", $range);
//Move values to array
$now = 0;
for ($i=0; $i<=substr_count($range,","); $i++) {
//Check a hyphen
if (substr_count($temp[$i],"-") == 1) {
list($start, $end) = split("-", $temp[$i]);
$difference = $end - $start;
for ($j=0; $j<=$difference; $j++) {
$array[$now] = $start + $j;
$now += 1;
}
} else {
$array[$now] = $temp[$i];
$now += 1;
}
}
//Delete overlapping values 
$array = array_unique($array);
//Sort the array
sort($array);
reset($array);
//Return Array
return $array;
}

//1.1.2.2. Convert a range to a condition
function xfi_cvt_ran ($range) {
/*
DEVELOPMENT:
Michael Son(michaelson@nate.com)
28.Apr.2010.
DESCRIPTION:
Convert a range to a condition of a SQL query
CALL:
xfi_cvt_ran("1-3,7,10-11");
RETURN:
$string = "`no` = '1' OR `no` = '2' OR `no` = '3', `no` = 7, `no` = '10', `no` = '11'";
*/
$array = xfi_prs_ran($range);
for($i=0; 1; $i++) {
//If the value is NULL,
if ($array[$i] == NULL) {
break;
} else {
//If this is the first time of loop,
if ($i == 0) {
$condition = "`no`='$array[$i]'";
} else {
$condition .= " OR `no`='$array[$i]'";
}
}
}
//Return condition
return $condition;
}

//1.1.3. Container
//1.1.3.1. Parse a container
function xfi_prs_con($container) {
/*
DEVELOPMENT:
Michael Son(michaelson@nate.com)
28.Apr.2010.
DESCRIPTION:
Parse a string including values to Array
CALL:
xfi_prs_con("lang=en,ko,jp,ch;hobby=reading books,programming,bowling;");
RETURN:
$array['lang'][0] = "en";
$array['lang'][1] = "ko";
$array['lang'][2] = "jp";
$array['lang'][3] = "ch";
$array['hobby'][0] = "reading books";
$array['hobby'][1] = "programming";
$array['hobby'][2] = "bowling";
*/

//Trim a string of values
$container = xfi_trm_str($container);
//Parse a string by semicolon
$temp = split(";", $container);
//Define times for a loop
$counter = substr_count($container, ";");
for ($i = 0; $i<=$counter; $i++) {
//Parse a string into name and values
list($name, $values) = split("=", $temp[$i]);
//Trim a left of name
$name = ltrim($name);
//Parse values into each value
$array[$name] = split(",", $values);
//Define times for a internal loop
$counter2 = substr_count($values, ",");
for ($j = 0; $j<=$counter2; $j++) {
//Trim a left of value
$array[$name][$j] = ltrim($array[$name][$j]);
}
}
//Return Array
return $array;
}

//1.1.3.2. Convert a container to a condition
function xfi_cvt_con($container) {
/*
DEVELOPMENT:
Michael Son(michaelson@nate.com)
28.Apr.2010.
DESCRIPTION:
Convert a string including values to a condition of a SQL query
CALL:
xfi_cvt_con("lang=en,ko,jp,ch;hobby=reading books,programming,bowling;");
RETURN:
$condition = "`lang` = 'en' OR `lang` = 'en' OR `lang` = 'en' OR `lang` = 'OR' `hobby` = 'reading books' OR `hobby` = 'programming' OR `hobby` = 'bowling'"; 
*/
//Trim a string of values
$container = xfi_trm_str($container);
//Parse a string by semicolon
$temp = split(";", $container);
//Define times for a loop
$counter = substr_count($container, ";");
for ($i = 0; $i<=$counter; $i++) {
//Parse a string into name and values
list($name, $values) = split("=", $temp[$i]);
//Trim a left of name
$name = ltrim($name);
//Parse values into each value
$value = split(",", $values);
//Define times for a internal loop
$counter2 = substr_count($values, ",");
for ($j = 0; $j<=$counter2; $j++) {
//If this is the first time of loop,
if ($i ==0 && $j == 0) {
$condition = "`$name`= '$value[$j]'";
} else {
$condition .= " OR `$name`= '$value[$j]'";
}
}
}
return $condition;
}

//1.2. Database
//1.2.1. Excute a query
function xfi_exe_que($query) {
/*
DEVELOPMENT:
Michael Son(michaelson@nate.com)
28.Apr.2010.
DESCRIPTION:
Execute an query
CALL:
xfi_exe_que("DROP TABLE `xf_authority`;");
RETURN:
$result = <<RESULT OF QUERY>>;
$result = NULL; // Error
*/
//Load information about DB 
include $_SERVER['DOCUMENT_ROOT']."/shelf/database.php";
//Connect to DB, Select DB and run a query
//If the database program is MySQL,
if($xf_db['kind'] == "mysql") {
//Connect to database Program
$link = @mysql_connect($xf_db['server'], $xf_db['username'], $xf_db['password']);
//Select database
mysql_select_db($xf_db['database'], $link);
$result = mysql_query($query, $link) or die("Query Error!\n");
//Close the connection
mysql_close();
} else {
$result = NULL;
}
//Return result of database
return $result;
}

//1.2.2. Count a number of rows of a result
function xfi_cnt_res($result) {
/*
DEVELOPMENT:
Michael Son(michaelson@nate.com)
28.Apr.2010.
DESCRIPTION:
Count the rows of a result 
CALL:
xfi_cnt_res(<<RESULT OF QUERY>>);
RETURN:
$number = 3;
$number = NULL; // Error
*/
//Load information about DB 
include $_SERVER['DOCUMENT_ROOT']."/shelf/database.php";
//If the database program is MySQL,
if($xf_db['kind'] == "mysql") {
$number = mysql_num_rows($result);
} else {
$number = NULL;
}
//Return a number
return $number;
}

//1.2.4. Parse a Result
function xfi_prs_res($result, $fields = NULL) {
/*
DEVELOPMENT:
Michael Son(michaelson@nate.com)
28.Apr.2010.
DESCRIPTION:
Parse a result of query
CALL:
xfi_prs_res(<<RESULT OF QUERY>>);
RETURN:
$array['no'][0] = 3;
$array['indicator'][0] = 1349851283;
$array['status'][0] = 1;
$array['id'][0] = "root";
$array['pw'][0] = "*68F9CD57023F17CBE06EE6365D9B4FEBF3EB3EE4";
$array['etc'][0] = "lang=en,ko,jp,ch";
$array['no'][1] = 4;
$array['indicator'][1] = 1352878344;
$array['status'][1] = 1;
$array['id'][1] = "administrator";
$array['pw'][1] = "*1F7E399139C29C99909A2C7E8C56247043C4FEE1";
$array['etc'][1] = "lang=ko,en";
$array = NULL //Error
*/
//Load information about DB
include $_SERVER['DOCUMENT_ROOT']."/shelf/database.php";
//If the list of fields are missed,
if($fields == NULL) {
$counter = 0;
} else {
//Parse fields by comma
$temp = split(",", $fields);
//Estimate times for a loop
$counter = substr_count($fields, ",");
}
for ($i=0; $i<=$counter; $i++) {
//If the list of fields are missed,
if($fields == NULL) {
//If the database program is MySQL,
if($xf_db['kind'] == "mysql") {
//Get field Name
$field = @mysql_field_name($result, $i);
} else {
//Stop the processing of this function
return $array = NULL;
}
//If there is no field name,
if ($field == NULL) {
//Stop this Loop
break;
} else {
//One more time
$counter++;
}
} else {
$field = $temp[$i];
}
//Estimate times for a subloop
$counter2 = xfi_cnt_res($result);
for($j=0; $j<$counter2; $j++) {
//If the database program is MySQL,
if($xf_db['kind'] == "mysql") {
$array[$field][$j] = mysql_result($result, $j, $field);
} else {
//Stop the processing of this function
return $array = NULL;
}
}
}
//Return Array
return $array;
}
function xfi_prs_fie($table) {
/*
DEVELOPMENT:
Michael Son(michaelson@nate.com)
28.Apr.2010.
DESCRIPTION:
Parse field names of a table
CALL:
xfi_prs_fie("xf_user");
RETURN:
$array[0] = "no";
$array[1] = "indicator";
$array[2] = "status";
$array[3] = "id";
$array[4] = "pw";
$array[5] = "etc";
 */
//Load information about DB
include $_SERVER['DOCUMENT_ROOT']."/shelf/database.php";
//Run a query
$query = "SELECT * FROM `$table`";
$result = xfi_exe_que($query);
//Start a loop for making array 
for($i=0; 1; $i++) {
//If a kind of database is mysql,
if($xf_db['kind'] == "mysql") {
$array[$i] = @mysql_field_name($result, $i);
} else {
return false;
}
//If a name of field is null,
if ($array[$i] == NULL) {
unset($array[$i]);
break;
}
}
//Return an array;
return $array;
}
//1.3. XML

//1.4. xFacility
//1.4.1. Basic I/O
//1.4.1.1. Write
function xfi_wrt_ite($table, $container) {
/*
DEVELOPMENT:
Michael Son(michaelson@nate.com)
28.Apr.2010.
DESCRIPTION:
Write an item in xFacility
CALL:
xfi_wrt_ite("xf_user","status=1;id=root;pw=password;lang=en,jp;");
RETURN:
$item['no'][0] = 1;
$item['indicator'][0] = 1238944426;
$item['status'][0] = 1;
$item['id'][0] = "root";
$item['pw'][0] = "*1F7E399139C29C99909A2C7E8C56247043C4FEE1";
$item['etc'][0] = "lang=en,ko";
*/
//Trim a string of values
$container = xfi_trm_str($container);
//Parse a string by semicolon
$temp = split(";", $container);
//Define times for a loop
$counter = substr_count($container, ";");
for ($i = 0; $i<=$counter; $i++) {
//Parse a string into name and values
list($name[$i], $contents[$i]) = split("=", $temp[$i]);
//Trim the left of name and values
$name[$i] = ltrim($name[$i]);
$contents[$i] = ltrim($contents[$i]);
}
//Get names of fields
$field = xfi_prs_fie($table);
//Compare a fieldname with a name of variables
for($i=0; 1; $i++) {
if($name[$i] == NULL) {
break;
}
for($j=0; 1; $j++) {
if($field[$j] == "etc") {
if($value[$j] == NULL) {
$value[$j] = $name[$i]."=".$contents[$i];
} else {
$value[$j] .= ";".$name[$i]."=".$contents[$i];
}
break;
} else if($field[$j] == $name[$i]) {
$value[$j] = $contents[$i];
break;
}
}
}
//Fields & Values
for($i=0; 1; $i++) {
if($field[$i] == NULL) {
break;
} else if($field[$i] == "no") {
$field[$i] = NULL;
$value[$i] = NULL;
} else if($field[$i] == "indicator") {
$indicator = time();
$value[$i] = "'$indicator'";
} else if($field[$i]=="pw" || $field[$i]=="password") {
$value[$i] = "password('$value[$i]')";
} else {
$value[$i] = "'$value[$i]'";
}
if($fields == NULL) {
$fields = $field[$i];
$values = $value[$i];
} else {
$fields .= ", $field[$i]";
$values .= ", $value[$i]";
}
}
//Make a query
$query = "INSERT INTO `$table`($fields) VALUES($values);";
$result = xfi_exe_que($query);
//Find information
$result = xfi_fnd_ite($table, "`indicator`='$indicator'");
$item = xfi_prs_res($result);
//Return information
return $item;
}
//1.4.1.2. Find
function xfi_fnd_ite($table, $condition) {
/*
DEVELOPMENT:
Michael Son(michaelson@nate.com)
28.Apr.2010.
DESCRIPTION:
Find items in xFacility
CALL:
xfi_fnd_ite("xf_user", "no='1'");
RETURN:
<<RESULT OF QUERY>>
*/
$query = "SELECT * FROM `$table` WHERE $condition;";
$result = xfi_exe_que($query);
//Return result of query
return $result;
}
//1.4.1.3. Read
function xfi_red_ite($table, $range) {
/*
DEVELOPMENT:
Michael Son(michaelson@nate.com)
DESCRIPTION:
Read items in xFacility
CALL:
xfi_red_ite("xf_user", "1-3,7");
RETURN:
$item['no'][0] = 1;
$item['indicator'][0] = 1238944426;
$item['status'][0] = 1;
$item['id'][0] = "root";
$item['pw'][0] = "*1F7E399139C29C99909A2C7E8C56247043C4FEE1";
$item['etc'][0] = "lang=en,ko";
$item['no'][0] = 2;
$item['indicator'][0] = 1283849349;
$item['status'][0] = 1;
$item['id'][0] = "administrator";
$item['pw'][0] = "*68F9CD57023F17CBE06EE6365D9B4FEBF3EB3EE4";
$item['etc'][0] = "lang=en,jp";
$item['no'][0] = 3;
$item['indicator'][0] = 1349851283;
$item['status'][0] = 1;
$item['id'][0] = "admin";
$item['pw'][0] = "*B94651992BD5A5D8316CEE083B6D7E9E3AEAC137";
$item['etc'][0] = "lang=en,ch";
$item['no'][0] = 7;
$item['indicator'][0] = 1739234725;
$item['status'][0] = 1;
$item['id'][0] = "michaelson";
$item['pw'][0] = "*68F9CD57023F17CBE06EE6365D9B4FEBF3EB3EE4";
$item['etc'][0] = "lang=ko,en";
*/
//Convert a range to a condition
$condition = xfi_cvt_ran($range);
//Run a query
$result = xfi_fnd_ite($table, $condition);
//Parse a result
$item = xfi_prs_res($result);
//Return items
return $item;
}
//1.4.1.4. Edit
function xfi_edi_ite($table, $container) {
/*
DEVELOPMENT:
Michael Son(michaelson@nate.com)
28.Apr.2010.
DESCRIPTION:
Edit an item in xFacility
CALL:
xfi_edi_ite("xf_user","status=1;id=root;pw=password;lang=en,jp;");
RETURN:
$item['no'][0] = 1;
$item['indicator'][0] = 1238944426;
$item['status'][0] = 1;
$item['id'][0] = "root";
$item['pw'][0] = "*1F7E399139C29C99909A2C7E8C56247043C4FEE1";
$item['etc'][0] = "lang=en,ko";
*/
//Trim a string of values
$container = xfi_trm_str($container);
//Parse a string by semicolon
$temp = split(";", $container);
//Define times for a loop
$counter = substr_count($container, ";");
for ($i = 0; $i<=$counter; $i++) {
//Parse a string into name and values
list($name[$i], $contents[$i]) = split("=", $temp[$i]);
//Trim the left of name and values
$name[$i] = ltrim($name[$i]);
$contents[$i] = ltrim($contents[$i]);
}
//Get names of fields
$field = xfi_prs_fie($table);
//Compare a fieldname with a name of variables
for($i=0; 1; $i++) {
if($name[$i] == NULL) {
break;
}
for($j=0; 1; $j++) {
if($field[$j] == "etc") {
if($value[$j] == NULL) {
$value[$j] = $name[$i]."=".$contents[$i];
} else {
$value[$j] .= ";".$name[$i]."=".$contents[$i];
}
break;
} else if($field[$j] == $name[$i]) {
$value[$j] = $contents[$i];
break;
}
}
}
//Fields & Values
for($i=0; 1; $i++) {
if($field[$i] == NULL) {
break;
} else if($field[$i] == "no") {
$field[$i] = NULL;
$no = $value[$i];
$value[$i] = NULL;
} else if($field[$i] == "indicator") {
$field[$i] = NULL;
$value[$i] = NULL;
} else if($field[$i]=="pw" || $field[$i]=="password") {
$value[$i] = "password('$value[$i]')";
} else {
$value[$i] = "'$value[$i]'";
}
if($field[$i] != NULL) {
if($setting == NULL) {
$setting = "`$field[$i]`=$value[$i]";
} else {
$setting .= ", `$field[$i]`=$value[$i]";
}
}
}
//Make a query
$query = "UPDATE `$table` SET $setting WHERE `no`='$no';";
$result = xfi_exe_que($query);
//Find information
$result = xfi_fnd_ite($table, "`no`='$no'");
$item = xfi_prs_res($result);
//Return information
return $item;
}
//1.4.1.5. Delete
function xfi_del_ite($table, $range) {
/*
DEVELOPER:
Michael Son(michaelson@nate.com)
28.Apr.2010.
DESCRIPTION:
Delete items in xFacility
CALL:
xfi_del_ite("xf_user", "1-3,7");
RESULT:
<<RESULT OF QUERY>>
*/
//Convert a range to a condition
$condition = xfi_cvt_ran($range);
//Write a query
$query = "DELETE FROM `$table` WHERE $condition";
//Run a query
$result = xfi_exe_res($query);
//Return result of query
return $result;
}
//Relationship
//1.4.2. Relationship
function xfi_mke_rel($table, $no, $event) {
/*
DEVELOPMENT:
Michael Son(michaelson@nate.com)
28.Apr.2010.
DESCRIPTION:
Make relationships with other library
CALL:
xfi_mke_rel("xf_party", 1, "write");
RETURN:
TRUE;
FALSE;
*/
//If the prefix is xf_party
if ($table == "xf_party") {

//If the prefix is xf_shelf
} else if ($table == "xf_shelf") {

//If the prefix is xf_compass,
} else if ($table == "xf_compass") {

//If the prefix is xf_timeline,
} else if ($table == "xf_timeline") {
//If the prefix is xf_mission,
} else if ($table == "xf_mission") {
}
}
//1.4.3. Checking
//1.4.3.1. Data
//1.4.3.1. Data
function xfi_chk_dat() {
/*
DEVELOPER:
Michael Son(michaelson@nate.com)
28.Apr.2010.
DESCRIPTION:
Check a form of data
CALL:
xfi_chk_dat();
RESULT:
TRUE;
FALSE;
*/
}
//1.4.3.2. Authority
//1.4.3.2. Authority
function xfi_chk_ath() {
/*
DEVELOPER:
Michael Son(michaelson@nate.com)
28.Apr.2010.
DESCRIPTION:
Check a authority of access
CALL:
xfi_chk_ath();
RESULT:
TRUE;
FALSE;
*/
}

//Part2. System API
//2. [xF]User
//3. [xF]Owner
//4. [xF]Authority
//5. [xF]Referer
//Part3. Data API
//6. [xF]Party
//7. [xF]Shelf
//8. [xF]Compass
//9. [xF]Timeline

'xFacility > Codes' 카테고리의 다른 글

container()  (0) 2010.06.08
xFacility^PHP > Internal API 3.0  (0) 2010.06.08
xFacility^PHP > Internal API 1.0  (0) 2010.05.17
Posted by 마이클