xFacility/Codes2010. 6. 8. 11:11
API 2.0에서 함수명만을 변경해서 가져온 것도 있고, 처리 알고리즘이 변화한 함수들도 있습니다.

<?php
//Common
//String
//Get extension of url or path
function get_extension($path) {
$return = substr($path,strrpos($path,".")+1);
return $return;
}
//Trim the strings at end
function trim_string($string, $character = ",;") {
/*
DEVELOPMENT:
Michael Son(michaelson@nate.com)
22.Apr.2010.
DESCRIPTION:
Trim a special character(comma and semicolon) at end of string
CALL:
trim_string("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;
}
//Drop string at first needle
function drop_atfirsttext($haystack, $needle, $from = false) {
/*
DEVELOPMENT:
Michael Son(michaelson@nate.com)
07.Jun.2010.
DESCRIPTION:
Drop a text from(or to) a first needle.
CALL:
drop_atfirsttext("http://www.nate.com:8080/", ":");
RETURN:
if($from = true) {
$return = "//www.nate.com:8080/";
} else if($from = false) {
$return = "http";
}
*/
if($from == true || $from == 1) {
$start = strpos($haystack, $needle) + strlen($needle);
$string = substr($haystack, $start);
$string = ltrim($string);
} else {
$length = strpos($haystack, $needle);
$string = substr($haystack, 0, $length);
$string = rtrim($string);
}
return $string;
}
//Drop string at first needle
function drop_atlasttext($haystack, $needle, $from = false) {
/*
DEVELOPMENT:
Michael Son(michaelson@nate.com)
07.Jun.2010.
DESCRIPTION:
Drop a text from(or to) a last needle.
CALL:
drop_atlasttext("http://www.nate.com:8080/", ":");
RETURN:
if($from = true) {
$return = "http://www.nate.com";
} else if($to = false) {
$return = "8080/";
}
*/
if($from == true || $from == 1) {
$start = strrpos($haystack, $needle);
$length = strlen($haystack)-$start;
$string = substr($haystack, $start+strlen($needle), $length);
$string = rtrim($string);
} else {
$length = strrpos($haystack, $needle);
$string = substr($haystack, 0, $length);
$string = rtrim($string);
}
return $string;
}
//Drop a text to a needle
function drop_fronttext($haystack, $needle, $position) {
for($i=0; $i<$position; $i++) {
$start = strpos($haystack, $needle) + strlen($needle);
$haystack = substr($haystack, $start);
}
$string = ltrim($haystack);
return $string;
}
//Position of a needle
function whereis_string($haystack, $needle) {
if(substr_count($haystack, $needle) == 0) {
$return[0] = "xfacility";
$return[1] = "return";
$return[2] = 1;
$return[5] = "Nothing to Find.";
} else {
for($i=0; substr_count($haystack, $needle)!=0; $i++) {
if($i == 0){
$array[$i] = strpos($haystack, $needle);
} else {
$array[$i] = $array[$i-1] + strlen($needle) + strpos($haystack, $needle);
}
$haystack = substr($haystack, strlen($needle) + strpos($haystack, $needle));
}
$return = $array;
}
return $return;
}
//File
//Scandir for PHP4
function scandir_php4($path) {
$handle = dir($path);
while (false !== ($entry = $handle->read())) {
$result[$i] = $entry; 
$i++;
}
$handle->close();
return $result;
}
//Count numbers of directories and files
function count_dir($path) {
$handle = dir($path);
while (false !== ($entry = $handle->read())) {
$result++;
}
$handle->close();
return $result;
}
//Upload a file
function upload_file($file=NULL, $counter=1) {
/*
DEVELOPMENT:
Michael Son(michaelson@nate.com)
07.Jun.2010.
DESCRIPTION:
Upload each files to shelf.
CALL:
upload_file($_FILES);
RETURN:
$return['path'] = "/home/hosting_users/username/www/shelf/123901233.bmp";
$return['url'] = "/shelf/123901233.bmp";
$return['size'] = "1255320";
$return['name'] = "123901233.bmp":
$return['mime'] = "image/bmp";
$return['ext'] = "bmp";
*/
$return[0] = "xFacility";
$return[1] = "return";
$return[2] = 0;
$return[3] = "API";
$return[4] = have_thetime();
if($file == NULL) {
if($_FILES == NULL) {
$return[2] = 1;
$return[5] = "There is nothing to process.";
return $return;
} else {
$file = $_FILES;
}
}
for($i=0; $i<$counter; $i++) {
if ($file[$i]['size'] > 0) {
$timestamp = time();
$extension = get_extension($file[$i]['name']);
$filename = $timestamp.".".$extension;
$directory = $_SERVER['DOCUMENT_ROOT']."/shelf/";
$path = $directory.$filename;
$url = "/shelf/".$filename;
if(file_exists($url)) {
$return[2] = 1;
$return[5] = "There is a file having the name.<br />Please retry.<br />Filename: $filename";
return $return;
} else {
move_uploaded_file($file[$i]['tmp_name'], $path);
$result['path'][$i] = $path;
$result['url'][$i] = $url;
$result['size'][$i] = $file[$i]['size'];
$result['name'][$i] = $file[$i]['name'];
$result['mime'][$i] = $file[$i]['type'];
$result['ext'][$i] = $extension;
}
} else {
$return[2] = 1;
$return[5] = "There is nothing to upload.";
return $return;
}
}
$return = $result;
return $return;
}
//find icon
function find_icon($type="file", $mime="text/plain", $extension="txt", $size="32") {
switch($type) {
case "file":
switch($mime) {
case "application/x-msdownload":
$return = "exe";
break;
case "application/unknown":
$return = "hwp";
break;
case "image/jpeg":
case "image/bmp" :
case "text/plain":
default:
$return = "txt";
break;
}
break;
case "text":
$return ="txt";
break;
case "link":
default:
$return = "htm";
}
$return .= $size.".png";
return $return;
}
//HTML
function redirect($url) {
echo "<meta http-equiv='Refresh' content='0; url=$url' />"; 
return 0;
}
//JavaScript
function alert($message) {
echo "<script type='text/javascript'>\n";
echo "<!--\n";
   echo "alert('$message');\n";
   echo "// -->\n";
echo "</script>\n";
return 0;
}
//XML
//Bring a value of an element
function get_valueofelement($codes, $element) {
//Check Codes
//Numbers of open tags match numbers of close tags
if($return == NULL) {
$open = substr_count($codes, "<$element>");
$close = substr_count($codes, "</$element>");
if($open != $close) {
$return = "1";
}
}
//Overlapping of open or close code
if($return == NULL) {
$open = whereis_string($codes, "<$element>");
if($open[0]=="xfacility"&&$open[1]=="return"&&$open[2]==1) {
$open = whereis_string($codes, "<$element ");
if($open[0]=="xfacility"&&$open[1]=="return"&&$open[2]==1) {
$return = $open;
}
}
$close = whereis_string($codes, "</$element>");
for($i=0; 1; $i++){
if($open[$i]==NULL) {
break;
} else if($open[$i]>=$close[$i]) {
$return = "1";
}
}
}
//Parse
if($return == NULL) {
for($i=0; 1; $i++) {
if($close[$i]==NULL) {
break;
}
$return[$i] = substr($codes, $open[$i]);
$return[$i] = drop_atfirsttext($return[$i], ">");
$return[$i] = drop_atfirsttext($return[$i], "</$element>", 0);
$return[$i] = trim($return[$i]);
$return[$i] = str_replace("\t", "", $return[$i]);
}
}
return $return;
}
//Convert what to condition
function cvt_what2condition() {
/*
DEVELOPMENT:
Michael Son(michaelson@nate.com)
28.Apr.2010.
DESCRIPTION:
Parse Range to Array
CALL:
parse_range("<item>
<no>1</no>
<subject>hello world!</subject>
</item>
<item>
<no>2</no>
<subject>play</subject>
</item>
<item>
<no>3</no>
</item>");
RETURN:
$condition = (`no`='1' AND `subject`='hello world!') OR (`no`='2' AND `subject1='play') OR (`no`='3');
*/
}
//Convert where to Table
function cvt_where2table() {
/*
DEVELOPMENT:
Michael Son(michaelson@nate.com)
28.Apr.2010.
DESCRIPTION:
Parse Range to Array
CALL:
parse_range("1-3,7,10-11");
RETURN:
$array[0] = "1";
$array[1] = "2";
$array[2] = "3";
$array[3] = "7";
$array[4] = "10";
$array[5] = "11";
*/
}
//Range
//Is range
function is_range($range) {
/*
DEVELOPMENT:
Michael Son(michaelson@nate.com)
17.May.2010.
DESCRIPTION:
Check this out as a range.
CALL:
is_range("1-3,7,10-11");
RETURN:
$is = true;
*/
return $is;
}
//Parse Range
function parse_range($range) {
/*
DEVELOPMENT:
Michael Son(michaelson@nate.com)
28.Apr.2010.
DESCRIPTION:
Parse Range to Array
CALL:
parse_range("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 = trim_string($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;
}
//Convert range to condition
function cvt_range2condition($range) {
if($range == 0) {
$return = 1;
} else {
$array = parse_range($range);
$i = 0;
foreach ($array as $value) {
if($i != 0) {
$return .= " OR ";
}
$return .= "`no`=".$value;
$i++; 
}
$return = trim_string($return);
}
return $return;
}
//Container
function parse_container($container) {
/*
DEVELOPMENT:
Michael Son(michaelson@nate.com)
22.Apr.2010.
DESCRIPTION:
Parse a string including values to Array
CALL:
parse_container("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 = trim_string($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;
}
//Time
function have_thetime($timestamp = NULL) {
/*
DEVELOPMENT:
Michael Son(michaelson@nate.com)
16.May.2010.
DESCRIPTION:
Have the time
CALL:
have_thetime("0");
RETURN:
$result = "1970Jan01090000";
*/
if($timestamp == NULL) {
$timestamp = time();
}
return date('YMdHis', $timestamp);
}
//Checker
function check_sentence($sentence) {
//Parse
$who = get_valueofelement($sentence, "who");
$how = get_valueofelement($sentence, "how");
$what = get_valueofelement($sentence, "what");
$where = get_valueofelement($sentence, "where");
$when = get_valueofelement($sentence, "when");
$why = get_valueofelement($sentence, "why");
//Authority
if($return==NULL) {
//If the sentence of xFXML was written by xfacility, 
if($who[0]=="xfacility") {
$return[2] = 1;
$return[5] = "We cannot support to send a message from xfacility to xfacility.";
//Is there a user having the number?
} else {
$query = "SELECT * FROM `xf_user` WHERE `no`='$who[0]'";
$result = execute_query($query);
$number = count_result($result);
if($number==0) {
$return[2] = 1;
$return[5] = "There is no user who have the number.";
}
}
//Find settings in xF_Authority
}
//Form
if($return==NULL) {
}
//Rule
if($return==NULL) {
}
//Return
return $return;
}
//Database
//Execute
function execute_query($query) {
/*
DEVELOPMENT:
Michael Son(michaelson@nate.com)
28.Apr.2010.
DESCRIPTION:
Execute an query
CALL:
execute_query("DROP TABLE `xf_authority`;");
RETURN:
$result = <<RESULT OF QUERY>>;
$result = 1; // 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);
//Close the connection
mysql_close();
} else {
$result = false;
}
//Return result of database
if($result == false) {
$return[0] = "xFacility";
$return[1] = "return";
$return[2] = "1";
$return[3] = "API";
$return[4] = have_thetime();
$return[5] = "There is an error while executing a query.<br />Query:<br />".$query;
} else {
$return = $result;
}
return $return;
}
//Count a number of results
function count_result($result) {
/*
DEVELOPMENT:
Michael Son(michaelson@nate.com)
28.Apr.2010.
DESCRIPTION:
Count the rows of a result 
CALL:
count_result(<<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;
}
function parse_result($result, $fields = NULL) {
/*
DEVELOPMENT:
Michael Son(michaelson@nate.com)
30.May.2010.
DESCRIPTION:
Parse a result of query
CALL:
xfx_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 = count_result($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;
}
//Database I/O
//Write
function write_db($what, $where) {
//Infinity loop
for($i=0; 1; $i++) {
//If there is nothing to input in DB,
if($item[$i][0] == NULL) {
break;
}
//If there is not designated number,
if($no[$i] == NULL) {
//Insert
$query = "INSERT INTO `$table[$i]`($fields[$i]) VALUES($values[$i])";
} else {
//Update
$query = "UPDATE `$table[$i]` SET $settings[$i] WHERE $conditions[$i]";
}
$result = execute_query($query);
if($result == 1) {
$return[0] = "xFacility";
$return[1] = "return";
$return[2] = "1";
$return[3] = $where[3];
$return[5] = "There is an error during writing into db.";
return $return;
}
}
//Return
if($return == NULL) {
$return = 0;
}
return $return;
}
//Read
function read_db($what, $where) {
if (is_array($what[$i])==true) {
} else if (is_range($what[$i])==true) {
} else {
$return[2] = "1";
$return[5] = "There is nothing to read.";
}
return $return;
}

//Delete
function delete_db($what, $where) {
$condition = cvt_what2condition($what);
$table = cvt_where2table($where);
$query = "DELETE FROM `$table` WHERE $condition";
$return = execute_query($query);
return $return;
}
//XML I/O
//Read
function read_xfxml($xfxml) {
//Parse
//Trim a xFXML
$sentence = get_valueofelement($xfxml, "sentence");
if($sentence == 1) {
$return = 1;
}
//Check
if($return == NULL) {
foreach($sentence as $key => $value) {
$result = check_sentence($value);
if($result[2]==1) {
$return = $result;
break;
}
}
}
//Execute
if($return == NULL) {
foreach($sentence as $key => $value) {
$how = get_valueofelement($value, "how");
$what = get_valueofelement($value, "what");
$where = get_valueofelement($value, "where");
switch($how[0]) {
case "write":
case "edit":
echo "write_db($what[0], $where[0])";
break;
case "read":
echo "read_db($what[0], $where[0])";
break;
case "delete":
echo "delete_db($what[0], $where[0])";
break;
case "return":
$return[2] = 1;
$return[5] = "There is nothing to run.";
}
}
}
//Return
echo write_xfxml($return);
}
//Write
function write_xfxml($return) {
if($return[0] == NULL) {
$return[0] = "xfacility";
}
if ($return[1] == NULL) {
$return[1] = "return";
}
if ($return[2] == NULL) {
$return[2] = "0";
}
if ($return[4] == NULL) {
$return[4] = have_thetime();
}
//Declare
$xfxml = "<?xml version='1.0' encoding='utf-8' ?>\n\n";
//Open sentences
$xfxml .= "<sentences>\n";
//Open sentence
$xfxml .= "\t<sentence>\n";
//Who
$xfxml .= "\t\t<who>$return[0]</who>\n";
//How
$xfxml .= "\t\t<how>$return[1]</how>\n";
//What
$xfxml .= "\t\t<what>$return[2]</what>\n";
//Where
$xfxml .= "\t\t<where>$return[3]</where>\n";
//When
$xfxml .= "\t\t<when>$return[4]</when>\n";
//Why
$xfxml .= "\t\t<why>$return[5]</why>\n";
//Close sentence
$xfxml .= "\t</sentence>\n";
//Close sentences
$xfxml .= "</sentences>\n";
return $xfxml;
}
//[xF]User
function xfu_signin($id, $pw, $url=NULL) {
if($id == NULL) {
alert("아이디가 입력되지 않았습니다.");
} else {
if ($pw == NULL) {
alert("비밀번호가 입력되지 않았습니다.");
} else {
$query = "SELECT * FROM `xf_user` WHERE `id`='$id'";
$result = execute_query($query);
if($result == 1) {
alert("쿼리 실행중 오류가 발생하였습니다.");
} else {
$counter = count_result($result);
if($counter > 1) {
alert("DB에 문제가 있습니다. 동일 아이디가 $counter개 있습니다.");
} else if($counter < 1) {
alert("There is no ID in system.");
} else {
$query = "SELECT * FROM `xf_user` WHERE `id`='$id' AND `pw`=password('$pw')";
$result = execute_query($query);
if($result == 1) {
alert("Error on executing a query.");
} else {
$counter = count_result($result);
if($counter == 0) {
alert("The password is wrong.");
} else {
alert("You have signed in.");
$array = parse_result($result);
$return['xf_id'] = $array['no'][0];
$return['xf_etc'] = parse_container($array['etc'][0]);
}
}
}
}
if($url != NULL) {
redirect($url);
}
}
}
return $return;
}
//[xF]Appbox
function get_nameofapps() {
$path = $_SERVER['DOCUMENT_ROOT']."/appbox/";
$array = scandir_php4($path); 
$counter = count_dir($path);
for($i=0; $i<$counter; $i++) {
if($array[$i]=="." || $array[$i]=="..") {
unset($array[$i]);
} else if(!is_dir($path.$array[$i])) {
unset($array[$i]);
}
}
sort($array);
reset($array);
$return = $array;
return $return;
}
?>

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

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