cmsdirectory."/web-analytics-pages"; $fullpathusers = $config->cmsdirectory."/web-analytics-users"; $user = $config->username; $pass = $config->password; //supplies WSDL $client = new SoapClient($config->CMSURL . "/ws/services/AssetOperationService?wsdl", array('trace' => 1)); //read the block's current content $blockContentsPages = readXMLBlock($fullpathpages); $blockContentsUsers = readXMLBlock($fullpathusers); //Begin building page-based XML $break = FALSE; $found = FALSE; $blockContentsPages->totalsitevisits = $blockContentsPages->totalsitevisits + 1; //If block is empty if (!isset($blockContentsPages->page)) { $page = $blockContentsPages->addChild('page'); $page->addAttribute('name',$_SERVER['SERVER_NAME'].$_SERVER['PHP_SELF']); $page->addChild('numberofvisits',1); $visit = $page->addChild('visit'); $visit->addChild('IP',$IP); $visit->addChild('timestamp',$timestamp); $visit->addChild('username',$_SESSION['username']); } else { foreach($blockContentsPages->page as $p) { foreach($p->attributes() as $a => $x) { //If we find the right page if($a == 'name' && $x == $_SERVER['SERVER_NAME'].$_SERVER['PHP_SELF']) { $found = TRUE; $visit = $p->addChild('visit'); $visit->addChild('IP',$IP); $visit->addChild('timestamp',$timestamp); $visit->addChild('username',$_SESSION['username']); $p->numberofvisits = $p->numberofvisits + 1; $break = TRUE; break; } } if ($break) { break; } } //If block not empty but page doesn't exist if(!$found) { $page = $blockContentsPages->addChild('page'); $page->addAttribute('name',$_SERVER['SERVER_NAME'].$_SERVER['PHP_SELF']); $page->addChild('numberofvisits',1); $visit = $page->addChild('visit'); $visit->addChild('IP',$IP); $visit->addChild('timestamp',$timestamp); $visit->addChild('username',$_SESSION['username']); } } //Begin building user-based XML $break = FALSE; $found = FALSE; $foundPage = 0; $tempuser; $blockContentsUsers->totalsitevisits = $blockContentsUsers->totalsitevisits + 1; //If block is empty if (!isset($blockContentsUsers->user)) { $userel = $blockContentsUsers->addChild('user'); $userel->addAttribute('name',$_SESSION['username']); $userel->addChild('numberofvisits',1); $userel->addChild('totalsitetime',0); $userp = $userel->addChild('page'); $userp->addAttribute('name',$_SERVER['SERVER_NAME'].$_SERVER['PHP_SELF']); $userp->addChild('totaltime',0); $visit = $userp->addChild('visit'); $visit->addChild('IP',$IP); $visit->addChild('timestamp',$timestamp); } else { foreach($blockContentsUsers->user as $u) { foreach($u->attributes() as $a => $x) { //If we found the user if($a == 'name' && $x == $_SESSION['username']) { $found = TRUE; $tempuser = $u; $u->totalsitetime = $u->totalsitetime + $timediff; foreach($u->page as $p) { foreach($p->attributes() as $a => $x) { if($a == 'name' && $x == $_SERVER['SERVER_NAME'].$_SERVER['PHP_SELF'] && $foundPage < 2) { $foundPage++; $visit = $p->addChild('visit'); $visit->addChild('IP',$IP); $visit->addChild('timestamp',$timestamp); $u->numberofvisits = $u->numberofvisits + 1; } //Add $timediff to previous page else if ($a == 'name' && $x == $prevpage && $foundPage < 2) { $foundPage++; $p->totaltime = $p->totaltime + $timediff; } if ($foundPage == 2) { break; } } if ($foundPage == 2) { break; } } $break = TRUE; break; } } if ($break) { break; } } //If the user doesn't exist if(!$found) { $userel = $blockContentsUsers->addChild('user'); $userel->addAttribute('name',$_SESSION['username']); $userel->addChild('numberofvisits',1); $userel->addChild('totalsitetime',0); $userp = $userel->addChild('page'); $userp->addAttribute('name',$_SERVER['SERVER_NAME'].$_SERVER['PHP_SELF']); $userp->addChild('totaltime',0); $visit = $userp->addChild('visit'); $visit->addChild('IP',$IP); $visit->addChild('timestamp',$timestamp); } //If user exists but has not visited this page yet else if($foundPage < 2) { $tempuser->numberofvisits = $tempuser->numberofvisits + 1; $userp = $tempuser->addChild('page'); $userp->addAttribute('name',$_SERVER['SERVER_NAME'].$_SERVER['PHP_SELF']); $userp->addChild('totaltime',0); $visit = $userp->addChild('visit'); $visit->addChild('IP',$IP); $visit->addChild('timestamp',$timestamp); } } outputToCMS($blockContentsPages,$fullpathpages); outputToCMS($blockContentsUsers,$fullpathusers); //Reads the data from the block and returns as SimpleXML function readXMLBlock($fullpath) { global $config; global $client; global $user; global $pass; $out; //build params $read_params = array ( 'authentication' => array( 'password' => $pass . "", 'username' => $user . "" ), 'identifier' => array( 'path' => $fullpath, 'type' => "block" )); try { //attempt to read the block $xmlstr = $client->read($read_params); //if read fails then it does not exist, so initialize $out to a new object //otherwise initialize $out to the data obtained from the block if ( $xmlstr->readReturn->success != 'true' ) { $out = new SimpleXMLElement(" 0"); } else { $xmlstr = $xmlstr->readReturn->asset->xmlBlock->xml; $out = new SimpleXMLElement($xmlstr); } } catch (Exception $e) { print("Block read request failed to conform to the WSDL.\n"); print($client->__getLastResponse()); } return $out; } //Sets up parameters and then writes to CMS function outputToCMS($out,$fullpath) { global $config; global $client; global $user; global $pass; $data = $out->asXML(); //build params $edit_params = array ( 'authentication' => array( 'password' => $pass . "", 'username' => $user . "" ), 'asset' => array( 'xmlBlock' => array( 'name' => basename($fullpath), 'parentFolderPath' => dirname($fullpath), 'path' => $fullpath, 'metadataSetPath' => "/Default", 'xml' => $data ))); try { //first, attempt to edit the block $reply = $client->edit($edit_params); //if edit fails, then block did not exist, so create from scratch if ( $reply->editReturn->success != 'true' ) { $edit_params['asset']['xmlBlock']['path'] = ""; $client->create($edit_params); } } catch (Exception $e) { print("Block creation/edit request failed to conform to the WSDL.\n"); print($client->__getLastResponse()); } } ?>