Assembla home | Assembla project page
 

Changeset 1010

Show
Ignore:
Timestamp:
07/02/08 23:05:06 (5 months ago)
Author:
Pernod
Message:

Updated the Load/Save Settings feature to include parent categories, rss subscriptions, tv expressions, and tvid settings. Also ensured that all data is properly UTF-8 encoded and escaped before being written to XML.

Fixes #147

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/swisscenter/base/swisscenter_configuration.php

    r853 r1010  
    6767        { 
    6868          if ( !in_array($row["NAME"],$exceptions) ) 
    69             $this->xml->appendChild($xpath,'<setting name="'.$row["NAME"].'">'.$row["VALUE"].'</setting>');  
     69            $this->xml->appendChild($xpath,'<setting name="'.$row["NAME"].'">'.utf8_encode(htmlspecialchars($row["VALUE"])).'</setting>');  
    7070        } 
    7171      } 
     
    8080    { 
    8181      $xpath = $this->xml->appendChild($this->settings_path,'<users />'); 
    82       $data = db_toarray("select u.user_id, u.name username, c.name certificate, c.scheme, u.pin from users u, certificates c where u.maxcert = c.cert_id"); 
    83       if ($data !== false && count($data)>0) 
    84       { 
    85         foreach ($data as $row) 
    86         { 
    87           $user_path = $this->xml->appendChild($xpath,'<user name="'.$row["USERNAME"].'"/>'); 
     82      $data = db_toarray("select u.user_id, u.name username, c.name certificate, c.scheme, u.pin, u.admin from users u, certificates c where u.maxcert = c.cert_id"); 
     83      if ($data !== false && count($data)>0) 
     84      { 
     85        foreach ($data as $row) 
     86        { 
     87          $user_path = $this->xml->appendChild($xpath,'<user name="'.utf8_encode(htmlspecialchars($row["USERNAME"]).'"/>')); 
    8888          $this->xml->appendChild($user_path, '<max_cert scheme="'.$row["SCHEME"].'">'.$row["CERTIFICATE"].'</max_cert>'); 
    8989          $this->xml->appendChild($user_path, '<pin>'.$row["PIN"].'</pin>'); 
     90          $this->xml->appendChild($user_path, '<admin>'.$row["ADMIN"].'</admin>'); 
    9091          $pref_path = $this->xml->appendChild($user_path, '<preferences />'); 
    9192          $prefs = db_toarray("select * from user_prefs where user_id=$row[USER_ID]"); 
     
    9394          { 
    9495            foreach ($prefs as $pref) 
    95               $this->xml->appendChild($pref_path,'<setting name="'.$pref["NAME"].'">'.$pref["VALUE"].'</setting>');          
     96              $this->xml->appendChild($pref_path,'<setting name="'.$pref["NAME"].'">'.utf8_encode(htmlspecialchars($pref["VALUE"])).'</setting>');          
    9697          } 
    9798        } 
     
    120121             $cert_xpath = $this->xml->appendChild( $scheme_xpath,'<certificate name="'.$row["NAME"].'" />'); 
    121122             $this->xml->appendChild( $cert_xpath,'<rank>'.$row["RANK"].'</rank>'); 
    122              $this->xml->appendChild( $cert_xpath,'<description>'.$row["DESCRIPTION"].'</description>'); 
     123             $this->xml->appendChild( $cert_xpath,'<description>'.utf8_encode(htmlspecialchars($row["DESCRIPTION"])).'</description>'); 
    123124           }      
    124125         } 
     
    135136    { 
    136137      $xpath = $this->xml->appendChild($this->settings_path,'<categories />'); 
    137       $data = db_toarray("select * from categories"); 
     138      $data = db_toarray("select c.cat_name, p.cat_name PARENT, c.download_info, c.parent_id from categories c, categories p where c.parent_id=p.cat_id"); 
    138139      if ($data !== false && count($data)>0) 
    139140      { 
     
    141142        { 
    142143          $cat_xpath = $this->xml->appendChild( $xpath,'<category />'); 
    143           $this->xml->appendChild($cat_xpath,'<name>'.$row["CAT_NAME"].'</name>');      
     144          $this->xml->appendChild($cat_xpath,'<name>'.utf8_encode(htmlspecialchars($row["CAT_NAME"])).'</name>'); 
     145          if ( $row["PARENT_ID"] > 0 ) 
     146            $this->xml->appendChild($cat_xpath,'<parent>'.utf8_encode(htmlspecialchars($row["PARENT"])).'</parent>'); 
     147          else 
     148            $this->xml->appendChild($cat_xpath,'<parent />'); 
    144149          $this->xml->appendChild($cat_xpath,'<download_info>'.$row["DOWNLOAD_INFO"].'</download_info>');      
    145150        } 
     
    184189          $loc_xpath = $this->xml->appendChild( $xpath,'<location />'); 
    185190          $this->xml->appendChild( $loc_xpath, '<type>'.$row["MEDIA_NAME"].'</type>'); 
    186           $this->xml->appendChild( $loc_xpath, '<path>'.$row["NAME"].'</path>'); 
     191          $this->xml->appendChild( $loc_xpath, '<path>'.utf8_encode(htmlspecialchars($row["NAME"])).'</path>'); 
    187192          $this->xml->appendChild( $loc_xpath, '<default_certificate scheme="'.$row["SCHEME"].'">'.$row["CERTIFICATE"].'</default_certificate>'); 
    188           $this->xml->appendChild( $loc_xpath, '<category>'.$row["CAT_NAME"].'</category>'); 
     193          $this->xml->appendChild( $loc_xpath, '<category>'.utf8_encode(htmlspecialchars($row["CAT_NAME"])).'</category>'); 
     194        } 
     195      } 
     196    } 
     197     
     198    /** 
     199     * Inserts the tv expressions into the XML document at the specified xpath location. 
     200     *  
     201     */ 
     202   
     203    function export_tv_expressions() 
     204    { 
     205      $xpath = $this->xml->appendChild($this->settings_path,'<tv_expressions />'); 
     206      $data = db_toarray("select  pos, expression from tv_expressions order by pos"); 
     207      if ($data !== false && count($data)>0) 
     208      { 
     209        foreach ($data as $row) 
     210        { 
     211          $loc_xpath = $this->xml->appendChild( $xpath,'<expression />'); 
     212          $this->xml->appendChild( $loc_xpath, '<pos>'.$row["POS"].'</pos>'); 
     213          $this->xml->appendChild( $loc_xpath, '<expression>'.utf8_encode(htmlspecialchars($row["EXPRESSION"])).'</expression>'); 
     214        } 
     215      } 
     216    } 
     217     
     218    /** 
     219     * Inserts the rss subscription details into the XML document at the specified xpath location. 
     220     *  
     221     */ 
     222   
     223    function export_rss_subscriptions() 
     224    { 
     225      $xpath = $this->xml->appendChild($this->settings_path,'<rss_subscriptions />'); 
     226      $data = db_toarray("select rs.*, mt.media_name 
     227                            from rss_subscriptions rs, media_types mt 
     228                           where rs.type = mt.media_id"); 
     229      if ($data !== false && count($data)>0) 
     230      { 
     231        foreach ($data as $row) 
     232        { 
     233          $loc_xpath = $this->xml->appendChild( $xpath,'<subscription />'); 
     234          $this->xml->appendChild( $loc_xpath, '<type>'.$row["MEDIA_NAME"].'</type>'); 
     235          $this->xml->appendChild( $loc_xpath, '<url>'.utf8_encode(htmlspecialchars($row["URL"])).'</url>'); 
     236          $this->xml->appendChild( $loc_xpath, '<title>'.utf8_encode(htmlspecialchars($row["TITLE"])).'</title>'); 
     237          $this->xml->appendChild( $loc_xpath, '<update>'.$row["UPDATE_FREQUENCY"].'</update>'); 
     238          $this->xml->appendChild( $loc_xpath, '<cache>'.$row["CACHE"].'</cache>'); 
     239        } 
     240      } 
     241    } 
     242     
     243    /** 
     244     * Inserts the tvid details into the XML document at the specified xpath location. 
     245     *  
     246     */ 
     247   
     248    function export_tvid_prefs() 
     249    { 
     250      $xpath = $this->xml->appendChild($this->settings_path,'<tvid_prefs />'); 
     251      $data = db_toarray("select * from tvid_prefs where tvid_custom is not null"); 
     252      if ($data !== false && count($data)>0) 
     253      { 
     254        foreach ($data as $row) 
     255        { 
     256          $loc_xpath = $this->xml->appendChild( $xpath,'<tvid />'); 
     257          $this->xml->appendChild( $loc_xpath, '<player>'.$row["PLAYER_TYPE"].'</player>'); 
     258          $this->xml->appendChild( $loc_xpath, '<tvid>'.$row["TVID_SC"].'</tvid>'); 
     259          $this->xml->appendChild( $loc_xpath, '<tvid_custom>'.$row["TVID_CUSTOM"].'</tvid_custom>'); 
    189260        } 
    190261      } 
     
    204275      $this->export_artfiles(); 
    205276      $this->export_media_locations(); 
     277      $this->export_tv_expressions(); 
     278      $this->export_rss_subscriptions(); 
     279      $this->export_tvid_prefs(); 
    206280    } 
    207281 
     
    213287    function import_categories() 
    214288    { 
     289      $cat_parent = array(); 
    215290      foreach ($this->xml->match('/swisscenter[1]/config[1]/categories[1]/category') as $abspath) 
    216291      { 
    217         $name = $this->xml->getData($abspath.'/name[1]'); 
     292        $name = html_entity_decode(utf8_decode($this->xml->getData($abspath.'/name[1]'))); 
     293        $parent = html_entity_decode(utf8_decode($this->xml->getData($abspath.'/parent[1]'))); 
     294        if ( !empty($parent) ) $cat_parent[$name] = $parent; 
    218295        $download = $this->xml->getData($abspath.'/download_info[1]'); 
    219         if (db_value("select count(*) from categories where cat_name = '$name'") == 0) 
    220           db_insert_row('categories',array("cat_name"=>$name, "download_info"=>$download)); 
     296        if (db_value("select count(*) from categories where cat_name = '".db_escape_str(un_magic_quote($name))."'") == 0) 
     297          db_insert_row('categories',array("cat_name"=>$name, "parent_id"=>0, "download_info"=>$download)); 
     298      } 
     299      foreach ($cat_parent as $name=>$parent) 
     300      { 
     301        $cat_id = db_value("select cat_id from categories where cat_name = '".db_escape_str(un_magic_quote($name))."'"); 
     302        $parent_id = db_value("select cat_id from categories where cat_name = '".db_escape_str(un_magic_quote($parent))."'"); 
     303        db_sqlcommand("update categories set parent_id=$parent_id where cat_id=$cat_id"); 
    221304      } 
    222305    } 
     
    235318        $value = $this->xml->getData($abspath); 
    236319        if (db_value("select count(*) from system_prefs where name = '".$attrib["NAME"]."'") == 0) 
    237           db_insert_row('system_prefs',array("name"=>$attrib["NAME"], "value"=>$value)); 
     320          db_insert_row('system_prefs',array("name"=>$attrib["NAME"], "value"=>addslashes($value))); 
    238321        else  
    239           db_sqlcommand("update system_prefs set value ='$value' where name ='".$attrib["NAME"]."'"); 
     322          db_sqlcommand("update system_prefs set value ='".addslashes($value)."' where name ='".$attrib["NAME"]."'"); 
    240323      } 
    241324    } 
     
    254337        // Import user 
    255338        $attrib = $this->xml->getAttributes($userpath); 
    256         $name = $attrib["NAME"]
    257         if (db_value("select count(*) from users where name = '$name'") == 0) 
     339        $name = html_entity_decode(utf8_decode($attrib["NAME"]))
     340        if (db_value("select count(*) from users where name = '".db_escape_str(un_magic_quote($name))."'") == 0) 
    258341        { 
    259342          $attrib      = $this->xml->getAttributes($userpath.'/max_cert[1]'); 
     
    261344          $cert_name   = $this->xml->getData($userpath.'/max_cert[1]'); 
    262345          $pin         = $this->xml->getData($userpath.'/pin[1]'); 
     346          $admin       = $this->xml->getData($userpath.'/admin[1]'); 
    263347           
    264348          if (($cert_id = db_value("select cert_id from certificates where name='$cert_name' and scheme='$cert_scheme'")) === false) 
     
    269353          else 
    270354          { 
    271             db_insert_row('users',array("name"=>$name, "maxcert"=>$cert_id, "pin"=>$pin) );    
     355            db_insert_row('users',array("name"=>$name, "maxcert"=>$cert_id, "pin"=>$pin, "admin"=>$admin) );    
    272356          }                     
    273357        } 
    274358         
    275359        // Determine the user_id for importing settings. 
    276         $user_id = db_value("select user_id from users where name = '$name'"); 
     360        $user_id = db_value("select user_id from users where name = '".db_escape_str(un_magic_quote($name))."'"); 
    277361         
    278362        // Import user preferences 
     
    282366          $value   = $this->xml->getData($prefpath); 
    283367          if (db_value("select count(*) from user_prefs where user_id=$user_id and name = '".$attrib["NAME"]."'") == 0) 
    284             db_insert_row('user_prefs',array("user_id"=>$user_id, "name"=>$attrib["NAME"], "value"=>$value)); 
     368            db_insert_row('user_prefs',array("user_id"=>$user_id, "name"=>$attrib["NAME"], "value"=>addslashes($value))); 
    285369          else 
    286             db_sqlcommand("update user_prefs set value ='$value' where user_id=$user_id and name ='".$attrib["NAME"]."'"); 
     370            db_sqlcommand("update user_prefs set value ='".addslashes($value)."' where user_id=$user_id and name ='".$attrib["NAME"]."'"); 
    287371        }         
    288372      } 
     
    306390          $cert_name = $attrib["NAME"]; 
    307391          $rank      = $this->xml->getData($certpath.'/rank[1]'); 
    308           $desc      = $this->xml->getData($certpath.'/description[1]'); 
     392          $desc      = html_entity_decode(utf8_decode($this->xml->getData($certpath.'/description[1]'))); 
    309393           
    310394          if (db_value("select count(*) from certificates where name='$cert_name' and scheme='$scheme_name'") == 0) 
     
    326410      { 
    327411        $type        = $this->xml->getData($locpath.'/type[1]'); 
    328         $path        = $this->xml->getData($locpath.'/path[1]'); 
    329         $cat_name    = $this->xml->getData($locpath.'/category[1]'); 
     412        $path        = html_entity_decode(utf8_decode($this->xml->getData($locpath.'/path[1]'))); 
     413        $cat_name    = html_entity_decode(utf8_decode($this->xml->getData($locpath.'/category[1]'))); 
    330414        $cert_name   = $this->xml->getData($locpath.'/default_certificate[1]'); 
    331415        $attrib      = $this->xml->getAttributes($locpath.'/default_certificate[1]'); 
     
    338422        elseif (($cat_id = db_value("select cat_id from categories where cat_name='$cat_name'")) === false)                
    339423          $errors[] = str('IMP_LOC_CAT_MISSING',$path,$cat_name); 
    340         elseif (db_value("select count(*) from media_locations where name = '$path' and media_type=$type_id") == 0) 
     424        elseif (db_value("select count(*) from media_locations where name = '".db_escape_str(un_magic_quote($path))."' and media_type=$type_id") == 0) 
    341425        { 
    342426          if ( db_insert_row("media_locations", array("name"=>$path, "media_type"=>$type_id, "cat_id"=>$cat_id, "unrated"=>$cert_id)) !== false) 
    343427          { 
    344             $id = db_value("select location_id from media_locations where name='$path' and media_type=".$type_id);             
     428            $id = db_value("select location_id from media_locations where name='".db_escape_str(un_magic_quote($path))."' and media_type=".$type_id);             
    345429             
    346430            if (! is_windows() ) 
     
    356440   
    357441    /** 
    358      * Imports all media_locations into the database from the XML document 
     442     * Imports all artfile details into the database from the XML document 
    359443     * 
    360444     * @return array - Array of errors 
     
    369453        if (!in_array($name,$files)) 
    370454          db_insert_row("art_files",array("filename"=>$name)); 
     455      } 
     456    } 
     457     
     458    /** 
     459     * Imports all tv expressions into the database from the XML document 
     460     * 
     461     */ 
     462     
     463    function import_tv_expressions() 
     464    { 
     465      foreach ($this->xml->match('/swisscenter[1]/config[1]/tv_expressions[1]/expression') as $expressionpath) 
     466      { 
     467        $pos        = $this->xml->getData($expressionpath.'/pos[1]'); 
     468        $expression = $this->xml->getData($expressionpath.'/expression[1]'); 
     469         
     470        if (db_value("select count(*) from tv_expressions where pos = $pos") == 0) 
     471          db_insert_row("tv_expressions", array("pos"=>$pos, "expression"=>addslashes($expression))); 
     472        else  
     473          db_sqlcommand("update tv_expressions set expression='".addslashes($expression)."' where pos=$pos"); 
     474      } 
     475    } 
     476     
     477    /** 
     478     * Imports all rss_subscriptions into the database from the XML document 
     479     *  
     480     */ 
     481   
     482    function import_rss_subscriptions() 
     483    { 
     484      foreach ($this->xml->match('/swisscenter[1]/config[1]/rss_subscriptions[1]/subscription') as $rsspath) 
     485      { 
     486        $type    = $this->xml->getData($rsspath.'/type[1]'); 
     487        $url     = html_entity_decode(utf8_decode($this->xml->getData($rsspath.'/url[1]'))); 
     488        $title   = html_entity_decode(utf8_decode($this->xml->getData($rsspath.'/title[1]'))); 
     489        $update  = $this->xml->getData($rsspath.'/update[1]'); 
     490        $cache   = $this->xml->getData($rsspath.'/cache[1]'); 
     491        $type_id = db_value("select media_id from media_types where media_name='$type'"); 
     492         
     493        if (db_value("select count(*) from rss_subscriptions where type=$type_id and url='".db_escape_str(un_magic_quote($url))."'") == 0) 
     494          db_insert_row("rss_subscriptions", array("type"=>$type_id, "url"=>$url, "title"=>$title, "update_frequency"=>$update, "cache"=>$cache)); 
     495      } 
     496    } 
     497     
     498    /** 
     499     * Imports all tvid preferences into the database from the XML document 
     500     * 
     501     */ 
     502     
     503    function import_tvid_prefs() 
     504    { 
     505      foreach ($this->xml->match('/swisscenter[1]/config[1]/tvid_prefs[1]/tvid') as $tvidpath) 
     506      { 
     507        $player      = $this->xml->getData($tvidpath.'/player[1]'); 
     508        $tvid        = $this->xml->getData($tvidpath.'/tvid[1]'); 
     509        $tvid_custom = $this->xml->getData($tvidpath.'/tvid_custom[1]'); 
     510         
     511        if ($tvid_id = db_value("select tvid_id from tvid_prefs where player_type='$player' and tvid_sc='$tvid'")); 
     512        { 
     513          $data["tvid_custom"] = $tvid_custom; 
     514          db_sqlcommand("update tvid_prefs set tvid_custom='$tvid_custom' where tvid_id=$tvid_id"); 
     515        } 
    371516      } 
    372517    } 
     
    383528      $this->import_sys_prefs();     
    384529      $this->import_certificates(); 
     530      $this->import_tv_expressions(); 
     531      $this->import_rss_subscriptions(); 
     532      $this->import_tvid_prefs(); 
    385533       
    386534      $errors = array();