File indexing completed on 2024-12-22 05:33:23

0001 <?php
0002 
0003 /*
0004  *   TRT GFX 4.0.0
0005  * 
0006  *   support: happy.snizzo@gmail.com
0007  *   website: https://projects.kde.org/projects/playground/www/ocs-server
0008  *   credits: Claudio Desideri
0009  *   
0010  *   This software is released under the GPLv3 License.
0011  *   http://opensource.org/licenses/GPL-3.0
0012  */ 
0013 
0014 
0015 /*
0016  * Static class used as a helper for managing xml output in
0017  * order to be OCS compliant.
0018  */
0019 class OCSXML{
0020   
0021   /*
0022    * Function originally ported from reference implementation.
0023    * Useful to print server generated OCS server compliant xml.
0024    * 
0025    * Example:
0026    * 
0027      $xml['version']=EConfig::$data["ocsserver"]["version"];;
0028        $xml['website']=EConfig::$data["ocsserver"]["website"];
0029      $xml['host']=EConfig::$data["ocsserver"]["host"];;
0030      $xml['contact']=EConfig::$data["ocsserver"]["contact"];;
0031      $xml['ssl']=EConfig::$data["ocsserver"]["ssl"];;
0032      echo(OCSXML::generatexml($format,'ok',100,'',$xml,'config','',1));
0033    * 
0034    */
0035   static public function generatexml($format,$status,$statuscode,$message,$data=array(),$tag='',$tagattribute='',$dimension=-1,$itemscount='',$itemsperpage='') {
0036     if($format=='json') {
0037 
0038       $json=array();
0039       $json['status']=$status;
0040       $json['statuscode']=$statuscode;
0041       $json['message']=$message;
0042       $json['totalitems']=$itemscount;
0043       $json['itemsperpage']=$itemsperpage;
0044       $json['data']=$data;
0045       return(json_encode($json));
0046 
0047 
0048     }else{
0049       $txt='';
0050       $writer = xmlwriter_open_memory();
0051       xmlwriter_set_indent( $writer, 2 );
0052       xmlwriter_start_document($writer );
0053       xmlwriter_start_element($writer,'ocs');
0054       xmlwriter_start_element($writer,'meta');
0055       xmlwriter_write_element($writer,'status',$status);
0056       xmlwriter_write_element($writer,'statuscode',$statuscode);
0057       xmlwriter_write_element($writer,'message',$message);
0058       if($itemscount<>'') xmlwriter_write_element($writer,'totalitems',$itemscount);
0059       if(!empty($itemsperpage)) xmlwriter_write_element($writer,'itemsperpage',$itemsperpage);
0060       xmlwriter_end_element($writer);
0061       //echo($dimension);
0062       if($dimension=='0') {
0063         // 0 dimensions
0064         xmlwriter_write_element($writer,'data',$data);
0065 
0066       }elseif($dimension=='1') {
0067         xmlwriter_start_element($writer,'data');
0068         foreach($data as $key=>$entry) {
0069           xmlwriter_write_element($writer,$key,$entry);
0070         }
0071         xmlwriter_end_element($writer);
0072 
0073       }elseif($dimension=='2') {
0074         xmlwriter_start_element($writer,'data');
0075         if(!empty($data)){
0076           foreach($data as $entry) {
0077             xmlwriter_start_element($writer,$tag);
0078             if(!empty($tagattribute)) {
0079               xmlwriter_write_attribute($writer,'details',$tagattribute);
0080             }
0081             foreach($entry as $key=>$value) {
0082               if(is_array($value)){
0083                 foreach($value as $k=>$v) {
0084                   xmlwriter_write_element($writer,$k,$v);
0085                 }
0086               } else {
0087                 xmlwriter_write_element($writer,$key,$value);
0088               }
0089             }
0090             xmlwriter_end_element($writer);
0091           }
0092         }
0093         xmlwriter_end_element($writer);
0094 
0095       }elseif($dimension=='3') {
0096         xmlwriter_start_element($writer,'data');
0097         foreach($data as $entrykey=>$entry) {
0098           xmlwriter_start_element($writer,$tag);
0099           if(!empty($tagattribute)) {
0100             xmlwriter_write_attribute($writer,'details',$tagattribute);
0101           }
0102           foreach($entry as $key=>$value) {
0103             if(is_array($value)){
0104               xmlwriter_start_element($writer,$entrykey);
0105               foreach($value as $k=>$v) {
0106                 xmlwriter_write_element($writer,$k,$v);
0107               }
0108               xmlwriter_end_element($writer);
0109             } else {
0110               xmlwriter_write_element($writer,$key,$value);
0111             }
0112           }
0113           xmlwriter_end_element($writer);
0114         }
0115         xmlwriter_end_element($writer);
0116       }elseif($dimension=='dynamic') {
0117         xmlwriter_start_element($writer,'data');
0118         //$this->toxml($writer,$data,'comment');
0119         if(is_array($data)) OCSXML::toxml($writer,$data,$tag);
0120         xmlwriter_end_element($writer);
0121       }
0122 
0123       xmlwriter_end_element($writer);
0124 
0125       xmlwriter_end_document( $writer );
0126       $txt.=xmlwriter_output_memory( $writer );
0127       unset($writer);
0128       return($txt);
0129     }
0130   }
0131   
0132   /**
0133    * Take an array of any size, and make it into xml
0134    * @param xmlwriter   An xmlwriter instance
0135    * @param array     The array which is to be transformed
0136    * @param mixed     Either a string, or an array of elements defining element names for each level in the XML hierarchy
0137    *            In the case of multiple lists of differently titled items at the same level, adding an array inside the array will allow for this to be constructed.
0138    * @param int     Internal use (the index of the child item in question - corresponds to the index in the second level array above)
0139    */
0140   static public function toxml($writer,$data,$node,$childindex=0) {
0141     $nodename=$node;
0142     if(is_array($node)){
0143       $nodename=array_shift($node);
0144     }
0145 
0146     $childcount=-1;
0147     foreach($data as $key => $value) {
0148       $childcount++;
0149       if (is_numeric($key)) {
0150         if(is_array($nodename)) {
0151           $key = $nodename[$childindex];
0152         } else {
0153           $key = $nodename;
0154         }
0155       }
0156       if (is_array($value)){
0157         xmlwriter_start_element($writer,$key);
0158         OCSXML::toxml($writer,$value,$node,$childcount);
0159         xmlwriter_end_element($writer);
0160       }else{
0161         xmlwriter_write_element($writer,$key,$value);
0162       }
0163     }
0164     if(is_array($node)) {
0165       array_unshift($node,$nodename);
0166     }
0167   }
0168   /*
0169    * Generate a providers.xml file for clients.
0170    * If arguments are empty data will be auto-generated.
0171    */
0172   public static function generate_providers($serverid='',$name='',$location='',$termsofuse='',$register='', $ssl=false)
0173   {
0174     $version = EConfig::$data['ocsserver']['version'];
0175     
0176     $modules = array();
0177     
0178     //preconfigured for future modules
0179     if(class_exists("OCSUser")){ $modules[] = "person"; } //TODO: OCSUser should become OCSPerson
0180     if(class_exists("OCSFriend")){ $modules[] = "friend"; }
0181     if(class_exists("OCSMessage")){ $modules[] = "message"; }
0182     if(class_exists("OCSActivity")){ $modules[] = "activity"; }
0183     if(class_exists("OCSContent")){ $modules[] = "content"; }
0184     if(class_exists("OCSFan")){ $modules[] = "fan"; }
0185     if(class_exists("OCSKnowledgebase")){ $modules[] = "knowledgebase"; }
0186     if(class_exists("OCSEvent")){ $modules[] = "event"; }
0187     
0188     $xml = '';
0189     $xml .= "<providers>
0190       <provider>
0191         <id>$serverid</id>
0192         <location>$location</location>
0193         <name>$name</name>
0194         <icon/>
0195         <termsofuse>$termsofuse</termsofuse>
0196         <register>$register</register>
0197         <services>\n";
0198           foreach($modules as $module){
0199             $xml.= "\t\t\t\t<$module ocsversion=\"$version\"/>\n";
0200           }
0201         $xml .= "\t\t\t\t</services>
0202       </provider>
0203     </providers>";
0204     
0205     return $xml;
0206   }
0207 }
0208 ?>