Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /* DataSync.php - Version 2.0
- * Created By: Colin Knapp - SA Technology and Web Coordinator
- * Purpose: A direct interface to the CampusLabs WebServices API (CLWSAPI)
- * : This will not only request and retrieve data with parameters given
- * : But will also parse the results and populate a database online
- */
- require_once ("/www_vol/data/sa-data/ccc-api/scripts/connect/mysqli.connect.php");
- ini_set('output_buffering', 0);
- ini_set('implicit_flush', 1);
- header("Content-Type: text/html; charset=utf-8");
- if (isset($_GET)) {
- $instruction = "";
- $parameters = "";
- if (isset($_GET['instruction'])) {
- $instruction = $_GET['instruction'];
- }
- if (isset($_GET['parameters'])) {
- $parameters = $_GET['parameters'];
- }
- $dataClass = new dataSync($instruction, $parameters);
- if (isset($dataClass -> result)) {
- //throw new Exception(var_dump($dataClass -> result));
- echo $dataClass -> result;
- }
- } else {
- echo "No GET parameters sent. (DataSync.php)";
- }
- class dataSync {
- // Constants
- private $apiKeyConst = "YOUR API KEY - The one from CL like ABC12SAGHEHWAA"
- private $apiUserConst = "YOUR API USER - Like User-01"
- private $ipAddrConst = "YOUR IP GIVEN TO CL"
- private $lastMessage = "";
- private $messageArray = array();
- private $messageCnt = 0;
- private $totalOrgs = 0;
- private $totalUsers = 0;
- public $result = null;
- // Initializing Method
- public function __construct($i, $p){
- if($i == ""){
- return FALSE;
- } else {
- if ($p != "") {
- //throw new Exception(var_dump(json_decode($p)));
- $params = json_decode(stripslashes(urldecode($p)), true);
- //throw new Exception(var_dump($params));
- }
- $this -> callFunction($i, $params);
- }
- }
- public function callFunction($inst, $params = NULL){
- if($params == NULL){
- call_user_func(array('self',$inst));
- } else {
- call_user_func(array('self',$inst), $params);
- }
- }
- public function fullescape($str) {
- # convert characters > 255 into HTML entities
- $convmap = array(0xFF, 0x2FFFF, 0, 0xFFFF);
- $str = mb_encode_numericentity($str, $convmap, "UTF-8");
- # escape HTML entities, so they are not urlencoded
- $str = preg_replace('/&#([0-9a-fA-F]{2,5});/i', 'mark\\1mark', $str);
- $str = urlencode($str);
- # now convert escaped entities into unicode url syntax
- $str = preg_replace('/mark([0-9a-fA-F]{2,5})mark/i', '%u\\1', $str);
- $str = str_replace("+", "%20", $str);
- return $str;
- }
- private function getGUID() {
- // Get a MS style GUID since PHP uses UUIDs
- // GUID Format = EC3597BD-A1DE-B544-9F97-8611944356CC
- if (function_exists('com_create_guid')) {
- return com_create_guid();
- } else {
- mt_srand((double)microtime() * 10000);
- $charid = strtoupper(md5(uniqid(rand(), TRUE)));
- $hyphen = chr(45);
- $uuid = substr($charid, 0, 8) . $hyphen . substr($charid, 8, 4) . $hyphen . substr($charid, 12, 4) . $hyphen . substr($charid, 16, 4) . $hyphen . substr($charid, 20, 12);
- return $uuid;
- }
- }
- private function buildHash() {
- $guid = $this -> getGUID();
- $utc = (string)round(microtime(true) * 1000);
- $prehash = $this -> apiUserConst . $this -> ipAddrConst . $utc . $guid . $this -> apiKeyConst;
- $hash = (string)md5($prehash);
- $parameters = array();
- // Required Parameters
- $parameters['apikey'] = $this -> apiUserConst;
- $parameters['time'] = $utc;
- $parameters['random'] = $guid;
- $parameters['hash'] = $hash;
- // Optional Parameters
- //$parameters['pagesize'] = 500;
- // Max results
- return $parameters;
- }
- private function fetchData($url, $parameters){
- $requesturl = $url;
- $cnt = 0;
- foreach($parameters as $key => $value){
- $op = ($cnt == 0 ? "?" : "&");
- $url .= $op . $key . "=" . urlencode($value);
- $cnt++;
- }
- //echo $url . "\r\n";
- $session = curl_init($url);
- curl_setopt($session, CURLOPT_HEADER, false);
- curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
- $response = curl_exec($session);
- return $response;
- }
- private function getRosters(){
- $time = time();
- ob_end_flush();
- ob_start();
- $page = 1;
- $parameters = $this -> buildHash();
- $parameters['id'] = 64382;
- $parameters['pagesize'] = 500;
- $url = "https://your.url/ws/organization/roster";
- $xmlData = simplexml_load_string($this -> fetchData($url, $parameters));
- $decoded = $this -> xml2array($xmlData);
- $roster = $this -> processArray($decoded);
- print "<pre>";
- print var_dump($roster);
- print "</pre>";
- }
- private function xml2array(SimpleXMLElement $parent) {
- $array = array();
- foreach ($parent as $name => $element) {
- ($node = & $array[$name]) && (1 === count($node) ? $node = array($node) : 1) && $node = & $node[];
- $node = $element->count() ? $this -> xml2array($element) : trim($element);
- }
- return $array;
- }
- private function processArray($in){
- $pageData = $in['results']['page'];
- $itemData = $in['results']['page']['items'];
- $returnData = NULL;
- foreach($itemData as $key => $val){
- $holder = $itemData[$key];
- switch($key){
- case "event":
- break;
- case "member":
- $curmem = array();
- $moc = 0;
- $start = -1;
- $reccnt = intval($pageData['pageSize']) - 1;
- for($i=$start;$i<$reccnt;$i++){
- if($i == -1){
- $memrecord = $holder;
- } else {
- $memrecord = $holder[$i];
- }
- $pcnt = 0;
- $positions = $memrecord['positions']['position'];
- $curmem[$moc] = array();
- $curmem[$moc]['position'] = array();
- $curmem[$moc]['position'][$pcnt] = array();
- foreach($positions as $pk => $pv){
- if(!is_numeric($pk)){
- $curmem[$moc]['position'][$pcnt]['pos_' . $pk] = $pv;
- } else {
- $pos = $positions[$pk];
- $pcnt++;
- foreach($pos as $ppk => $ppv){
- $curmem[$moc]['position'][$pcnt]['pos_' . $ppk] = $ppv;
- }
- }
- }
- foreach($memrecord as $hk => $hv){
- if(!is_numeric($hk) && $hk != "positions"){
- $curmem[$moc][$hk] = $hv;
- }
- }
- $moc++;
- }
- return $curmem;
- break;
- case "membership":
- break;
- case "organization":
- $curorg = array();
- $coc = 0;
- $start = -1;
- $reccnt = intval($pageData['totalItems']) - 1;
- for($i=$start;$i<$reccnt;$i++){
- if($i == -1){
- $orgrecord = $holder;
- } else {
- $orgrecord = $holder[$i];
- }
- $address = $orgrecord['addresses']['address'];
- $category = $orgrecord['categories']['category'];
- $customfld = $orgrecord['customfields']['customfield'];
- array_splice($orgrecord, 0, 3);
- $getcf = false;
- $year = "";
- foreach($customfld as $kcf => $vcf){
- if($kcf == "name"){
- if($vcf == "Year Founded:"){
- $getcf = true;
- }
- }
- if($getcf && $kcf == "values"){
- $year = $customfld[$kcf]["string"];
- break;
- }
- }
- $curorg[$coc] = array();
- foreach($address as $ak => $av){
- $curorg[$coc]['adr_' . $ak] = $av;
- }
- foreach($category as $ck => $cv){
- $curorg[$coc]['cat_' . $ck] = $cv;
- }
- foreach($orgrecord as $hk => $hv){
- if(!is_numeric($hk)){
- if($hk == "description"){
- $hv = htmlspecialchars($hv);
- }
- $curorg[$coc][$hk] = $hv;
- }
- }
- if($year != ""){
- $curorg[$coc]['year'] = $year;
- }
- $coc++;
- }
- return $curorg;
- break;
- }
- }
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement