The following set of documents and sample code, PHP programs are an example.
UCenter the API interface file should be stored at the application root directory
"api/" directory, and file name can be customized, the default is uc.php,
the "api/uc.php".
File structure can be divided into four parts,
contains 12 interfaces the project.
Part 1 for the constant definitions,
Part 2 to inform the judge and the initialization method input parameters,
Part 3 class code for the interface,
Part 4 for the function definition.
1. Constant definition
define('UC_CLIENT_VERSION', '1.6.0'); //note UCenter Version Identification
define('UC_CLIENT_RELEASE', '20101001');
define('API_DELETEUSER', 1); //note user delete API
define('API_RENAMEUSER', 1); //note rename user API
define('API_GETTAG', 1); //note get tag API
define('API_SYNLOGIN', 1); //note synchronous login API
define('API_SYNLOGOUT', 1); //note synchronous logout API
define('API_UPDATEPW', 1); //note change user password API
define('API_UPDATEBADWORDS', 1); //note update bad words API
define('API_UPDATEHOSTS', 1); //note update dns hosts cache API
define('API_UPDATEAPPS', 1); //note update application list API
define('API_UPDATECLIENT', 1); //note update client cache API
define('API_UPDATECREDIT', 1); //note update user points API
define('API_GETCREDITSETTINGS', 1); //note get UCenter points settings API
define('API_GETCREDIT', 1); //note get users points API
define('API_UPDATECREDITSETTINGS', 1); //note update the application settings for user points
define('API_RETURN_SUCCEED', '1');
define('API_RETURN_FAILED', '-1');
define('API_RETURN_FORBIDDEN', '-2');
Constant definition section defines the interfaces used by the switch 14 interfaces the project settings,
"1" is open, "0" is off.
If enabled, then the code after the run return API_RETURN_SUCCEED, otherwise API_RETURN_FORBIDDEN.
On fails the interface returns API_RETURN_FAILED.
2. Notify determine and init input parameters
if(!defined('IN_UC')) {
error_reporting(0);
set_magic_quotes_runtime(0);
define('DISCUZ_ROOT', substr(dirname(__FILE__), 0, -3));
defined('MAGIC_QUOTES_GPC') || define('MAGIC_QUOTES_GPC', get_magic_quotes_gpc());
require_once DISCUZ_ROOT.'./config.inc.php';
$_DCACHE = $get = $post = array();
$code = @$_GET['code'];
parse_str(_authcode($code, 'DECODE', UC_KEY), $get);
if(MAGIC_QUOTES_GPC) {
$get = _stripslashes($get);
}
$timestamp = time();
if($timestamp - $get['time'] > 3600) {
exit('Authracation has expiried');
}
if(empty($get)) {
exit('Invalid Request');
}
$action = $get['action'];
require_once DISCUZ_ROOT.'./uc_client/lib/xml.class.php';
$post = xml_unserialize(file_get_contents('php://input'));
if(in_array($get['action'], array('test', 'deleteuser', 'renameuser', 'gettag', 'synlogin', 'synlogout', 'updatepw', 'updatebadwords', 'updatehosts', 'updateapps', 'updateclient', 'updatecredit', 'getcreditsettings', 'updatecreditsettings'))) {
require_once DISCUZ_ROOT.'./include/db_'.$database.'.class.php';
$GLOBALS['db'] = new dbstuff;
$GLOBALS['db']->connect($dbhost, $dbuser, $dbpw, $dbname, $pconnect, true, $dbcharset);
$GLOBALS['tablepre'] = $tablepre;
unset($dbhost, $dbuser, $dbpw, $dbname, $pconnect);
$uc_note = new uc_note();
exit($uc_note->$get['action']($get, $post));
} else {
exit(API_RETURN_FAILED);
}
} else {
define('DISCUZ_ROOT', $app['extra']['apppath']);
require_once DISCUZ_ROOT.'./config.inc.php';
require_once DISCUZ_ROOT.'./include/db_'.$database.'.class.php';
$GLOBALS['db'] = new dbstuff;
$GLOBALS['db']->connect($dbhost, $dbuser, $dbpw, $dbname, $pconnect, true, $dbcharset);
$GLOBALS['tablepre'] = $tablepre;
unset($dbhost, $dbuser, $dbpw, $dbname, $pconnect);
}
Through constant IN_UC to determine HTTP interface file access by remote or direct way calling included
3. Interface code
Interface class name uc_note, interface name such a way as, uc_note general code structure is as follows:
class uc_note {
var $dbconfig = '';
//var $db = '';
var $appdir = '';
function _serialize($arr, $htmlon = 0) {
if(!function_exists('xml_serialize')) {
include_once DISCUZ_ROOT.'./uc_client/lib/xml.class.php';
}
return xml_serialize($arr, $htmlon);
}
function uc_note() {
$this->appdir = substr(dirname(__FILE__), 0, -3);
$this->dbconfig = $this->appdir.'./config.inc.php';
}
function test($get, $post) {
return API_RETURN_SUCCEED;
}
function deleteuser($get, $post) {
/*Code omitted*/
return API_RETURN_SUCCEED;
}
/* More Interface Project */
}
Specific code can refer to Discuz7.0.0/api/uc.php
test
This interface for the only test the connection. When the test was launched UCenter interface request, if successful, get to the interface returned API_RETURN_SUCCEED value and application of communications, said UCenter normal.
deleteuser
When UCenter delete a user, the interface will be launched deleteuser request to all applications to remove the corresponding user.
Input parameters on the $get['ids'], the value is a comma-separated user ID.
If successful then delete the output API_RETURN_SUCCEED.
$query = $db->query("DELETE FROM {$tablepre}members WHERE uid IN ($get[ids])");
exit(API_RETURN_SUCCEED);
renameuser
When UCenter change a user's user name, the interface will be launched renameuser request, notify all applications renamed.
Input parameter $get['uid'] means that the user ID, $get['oldusername'] that the old user name, $get['newusername'] that the new user name. If the modification is successful, the output API_RETURN_SUCCEED.
$uid = $get['uid'];
$usernamenew = $get['newusername'];
$db->query("UPDATE {$tablepre}members SET username='$usernamenew' WHERE uid='$uid'");
exit(API_RETURN_SUCCEED);
updatepw
When the user changes the user password, this interface to receive a new password sent UCenter.
Input parameter $get['username'] that the user name, $get['password'] that the new password. If the modification is successful, the output API_RETURN_SUCCEED.
$username = $get['username'];
$password = md5($get['password']);
$db->query("UPDATE members SET password='$password' WHERE username='$username'");
exit(API_RETURN_SUCCEED);
gettag
If the application exists tag feature, you can put the application through this interface label data transfer to the UCenter.
Input parameters on the $get['id'], the value of the label name. The output of the array and subject to uc_serialize treatment.
| integer [0] | Tag name |
| array [1] |
Tag data
Multiple array of custom items, the index name any, with return to UCenter. In order to ensure the sharing of data between applications, you need to "Application Management" and "label single display template" and "label template tag that" set the template and instructions. Template "(xxx)" said the index of tag data, representing the corresponding data. Such as extended data in the template "(image)" will show "['image']" array to the contents.
|
$query = $db->query("SELECT * FROM threadtags WHERE tagname='$get[id]' ORDER BY dateline DESC LIMIT 10");
$threadlist = array();
while($data = $db->fetch_array($query)) {
$threadlist[] = array(
'name' => $data['subject'],
'uid' => $data['authorid'],
'username' => $data['author'],
'dateline' => $data['dateline'],
'url' => 'http://www.yourwebsite.com/thread.php?id='.$data['id'],
'image' => ''http://www.yourwebsite.com/threadimage.php?id='.$data['id'],
);
}
}
$return = array($name, $threadlist);
echo uc_serialize($return, 1);
synlogin
synlogout
If the application needs and other applications for synchronization log out, this part of the code responsible for revocation of the user's login state.
This interface for the notification interface, no input parameters and output. Sync out of need to use
P3P Standard.
header('P3P: CP="CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR"');
dsetcookie('Example_auth', '', -86400 * 365);
updatebadwords
When UCenter words filter settings changes, this interface is responsible for informing all the words after the application update the content filter settings.
Set the content using POST method to submit to the interface. Interface operation is completed the output API_RETURN_SUCCEED.
$post = uc_unserialize(file_get_contents('php://input'));
$cachefile = DISCUZ_ROOT.'./uc_client/data/cache/badwords.php';
$fp = fopen($cachefile, 'w');
$s = "<?php\r\n";
$s .= '$_CACHE[\'badwords\'] = '.var_export($post, TRUE).";\r\n";
fwrite($fp, $s);
fclose($fp);
exit(API_RETURN_SUCCEED);
updatehosts
When UCenter the domain name resolution settings changes, this interface to all applications for the domain name resolution settings updated content.
Set the content using POST method to submit to the interface. Interface operation is completed the output API_RETURN_SUCCEED.
$post = uc_unserialize(file_get_contents('php://input'));
$cachefile = DISCUZ_ROOT.'./uc_client/data/cache/hosts.php';
$fp = fopen($cachefile, 'w');
$s = "<?php\r\n";
$s .= '$_CACHE[\'hosts\'] = '.var_export($post, TRUE).";\r\n";
fwrite($fp, $s);
fclose($fp);
exit(API_RETURN_SUCCEED);
updateapps
When an application UCenter list changes, this interface is responsible for notifying all applications updated list of applications.
Set the content using POST method to submit to the interface. Interface operation is completed the output API_RETURN_SUCCEED.
$post = uc_unserialize(file_get_contents('php://input'));
$cachefile = DISCUZ_ROOT.'./uc_client/data/cache/apps.php';
$fp = fopen($cachefile, 'w');
$s = "<?php\r\n";
$s .= '$_CACHE[\'apps\'] = '.var_export($post, TRUE).";\r\n";
fwrite($fp, $s);
fclose($fp);
exit(API_RETURN_SUCCEED);
updateclient
When UCenter basic settings changes, this interface to all applications for the basic settings updated content.
Set the content using POST method to submit to the interface. Interface operation is completed the output API_RETURN_SUCCEED.
$post = uc_unserialize(file_get_contents('php://input'));
$cachefile = DISCUZ_ROOT.'./uc_client/data/cache/setting.php';
$fp = fopen($cachefile, 'w');
$s = "<?php\r\n";
$s .= '$_CACHE[\'setting\'] = '.var_export($post, TRUE).";\r\n";
fwrite($fp, $s);
fclose($fp);
exit(API_RETURN_SUCCEED);
updatecredit
When an application requests the implementation of the interface exchange integral function uc_credit_exchange_request () later, this interface has been converted for the purpose of notice required to modify the user application integral value.
Input parameter $get['credit'] that integral number, $get['amount'] that the increase or decrease the value of integration, $get['uid'] that the user ID.
$credit = intval($get['credit']);
$amount = intval($get['amount']);
$uid = intval($get['uid']);
$db->query("UPDATE members SET extcredits$credit=extcredits$credit+'$amount' WHERE uid='$uid'");
exit(API_RETURN_SUCCEED);
getcreditsettings
This interface is responsible for application integration settings passed to the UCenter, for UCenter redeem settings in use.
This interface without input parameters. The output of the array and subject to uc_serialize treatment.
Array of single output structure:
| 1-dimension KEY |
2-dimension KEY |
| array [n] |
Integral number |
string [0] | Points Name |
| string [1] | Integral unit |
Such as:
array(
'1' => array('Prestige', ''),
'2' => array('Money', 'gold'),
)
$credits = array();
foreach($_DCACHE['settings']['extcredits'] as $id => $extcredits) {
$credits[$id] = array($extcredits['title'], $extcredits['unit']);
}
echo uc_serialize($credits);
updatecreditsettings
This interface is responsible for receiving UCenter redeem the parameters set.
Input parameters on the $get['credit'], the value of the array to set the parameters. Interface operation is completed the output API_RETURN_SUCCEED.
Input array of single structure:
| 1-d KEY |
2-d KEY |
| array [appid] |
Application ID |
integer ['appiddesc'] | Redeem the target application ID
|
| integer ['creditdesc'] |
Integral number exchange aim points
|
| integer ['creditsrc'] | Exchange integral number of source points
|
| string ['title'] | Points Name
|
| string ['unit'] | Integral unit
|
| integer ['ratio'] | Points exchange rate
|
Such as:
array(
'1' => array(
'appiddesc' => 2,
'creditdesc' => 1,
'creditsrc' => 1,
'title' => 'Money',
'unit' => 'Points',
'ratio' => 2,
),
)
$outextcredits = array();
foreach($get['credit'] as $appid => $credititems) {
if($appid == UC_APPID) {
foreach($credititems as $value) {
$outextcredits[$value['appiddesc'].'|'.$value['creditdesc']] = array(
'creditsrc' => $value['creditsrc'],
'title' => $value['title'],
'unit' => $value['unit'],
'ratio' => $value['ratio']
);
}
}
}
$db->query("REPLACE INTO cdb_settings (variable, value) VALUES ('outextcredits', '".addslashes(serialize($outextcredits))."');", 'UNBUFFERED');
exit(API_RETURN_SUCCEED);
getcredit
This interface is used to specify the application passes the user's points UCenter.
Input parameter $get['uid'] for the user ID, $get['credit'] for the integral number. Interface operation is completed the output integral value.
$uid = intval($get['uid']);
$credit = intval($get['credit']);
echo $db->result_first("SELECT extcredits$credit FROM members WHERE uid='$uid'");
4. Function definition
Interface function must contain the following function, or not correctly passed over on the UCenter to decode the data.
function authcode($string, $operation = 'DECODE', $key = '', $expiry = 0) {
$ckey_length = 4;
$key = md5($key ? $key : UC_KEY);
$keya = md5(substr($key, 0, 16));
$keyb = md5(substr($key, 16, 16));
$keyc = $ckey_length ? ($operation == 'DECODE' ? substr($string, 0, $ckey_length): substr(md5(microtime()), -$ckey_length)) : '';
$cryptkey = $keya.md5($keya.$keyc);
$key_length = strlen($cryptkey);
$string = $operation == 'DECODE' ? base64_decode(substr($string, $ckey_length)) : sprintf('%010d', $expiry ? $expiry + time() : 0).substr(md5($string.$keyb), 0, 16).$string;
$string_length = strlen($string);
$result = '';
$box = range(0, 255);
$rndkey = array();
for($i = 0; $i <= 255; $i++) {
$rndkey[$i] = ord($cryptkey[$i % $key_length]);
}
for($j = $i = 0; $i < 256; $i++) {
$j = ($j + $box[$i] + $rndkey[$i]) % 256;
$tmp = $box[$i];
$box[$i] = $box[$j];
$box[$j] = $tmp;
}
for($a = $j = $i = 0; $i < $string_length; $i++) {
$a = ($a + 1) % 256;
$j = ($j + $box[$a]) % 256;
$tmp = $box[$a];
$box[$a] = $box[$j];
$box[$j] = $tmp;
$result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));
}
if($operation == 'DECODE') {
if((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26).$keyb), 0, 16)) {
return substr($result, 26);
} else {
return '';
}
} else {
return $keyc.str_replace('=', '', base64_encode($result));
}
}
function uc_serialize($arr, $htmlon = 0) {
include_once UC_CLIENT_ROOT.'./lib/xml.class.php';
return xml_serialize($arr, $htmlon);
}
function uc_unserialize($s) {
include_once UC_CLIENT_ROOT.'./lib/xml.class.php';
return xml_unserialize($s);
}