Ignore:
Timestamp:
Apr 6, 2020, 11:17:40 PM (4 years ago)
Author:
chronos
Message:
  • Modified: Improved code format.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Modules/NetworkConfigRouterOS/Routerboard.php

    r860 r873  
    2222  {
    2323    $Output = array();
    24     if(is_array($Commands))
     24    if (is_array($Commands))
    2525    {
    2626      $I = 0;
    2727      $Batch = array();
    28       while($I < count($Commands))
    29       {
    30         if(($I % $this->MaxBurstLineCount) == 0)
    31         {
    32           if(count($Batch) > 0)
     28      while ($I < count($Commands))
     29      {
     30        if (($I % $this->MaxBurstLineCount) == 0)
     31        {
     32          if (count($Batch) > 0)
    3333            $Output = array_merge($Output, $this->ExecuteBatch(implode(';', $Batch)));
    3434          $Batch = array();
     
    3737        $I++;
    3838      }
    39       if(count($Batch) > 0)
     39      if (count($Batch) > 0)
    4040       $Output = array_merge($Output, $this->ExecuteBatch(implode(';', $Batch)));
    4141    } else
    4242      $Output = array_merge($Output, $this->ExecuteBatch($Commands));
    43     return($Output);
     43    return ($Output);
    4444  }
    4545
     
    4747  {
    4848    $Commands = trim($Commands);
    49     if($Commands != '')
     49    if ($Commands != '')
    5050    {
    5151      $Commands = addslashes($Commands);
     
    5757      $Command = $this->SSHPath.' -oBatchMode=no -o ConnectTimeout='.$this->Timeout.' -l '.$this->UserName.
    5858        $PrivKey.' '.$this->HostName.' "'.$Commands.'"';
    59       if($this->Debug) echo($Command);
     59      if ($this->Debug) echo($Command);
    6060      $Output = array();
    6161      exec($Command, $Output);
    6262    } else $Output = '';
    63     if($this->Debug) print_r($Output);
    64     return($Output);
     63    if ($this->Debug) print_r($Output);
     64    return ($Output);
    6565  }
    6666
     
    7070    array_pop($Result);
    7171    $List = array();
    72     foreach($Result as $ResultLine)
     72    foreach ($Result as $ResultLine)
    7373    {
    7474      $ResultLineParts = explode(' ', trim($ResultLine));
    75       if(count($ResultLineParts) > 1)
    76       {
    77         if($ResultLineParts[1]{0} == '"') $ResultLineParts[1] = substr($ResultLineParts[1], 1, -1); // Remove quotes
     75      if (count($ResultLineParts) > 1)
     76      {
     77        if ($ResultLineParts[1]{0} == '"') $ResultLineParts[1] = substr($ResultLineParts[1], 1, -1); // Remove quotes
    7878        $List[substr($ResultLineParts[0], 0, -1)] = $ResultLineParts[1];
    7979      } else $List[substr($ResultLineParts[0], 0, -1)] = '';
    8080    }
    81     return($List);
     81    return ($List);
    8282  }
    8383
     
    8585  {
    8686    $PropertyList = '"';
    87     foreach($Properties as $Index => $Property)
     87    foreach ($Properties as $Index => $Property)
    8888    {
    8989      $PropertyList .= $Index.'=".[get $i '.$Property.']." ';
     
    9292
    9393    $ConditionList = '';
    94     foreach($Conditions as $Index => $Item)
    95     {
    96       if($Item == 'no') $ConditionList .= $Index.'='.$Item.' ';
     94    foreach ($Conditions as $Index => $Item)
     95    {
     96      if ($Item == 'no') $ConditionList .= $Index.'='.$Item.' ';
    9797      else $ConditionList .= $Index.'="'.$Item.'" ';
    9898    }
     
    101101    $Result = $this->Execute(implode(' ', $Path).' {:foreach i in=[find '.$ConditionList.'] do={:put ('.$PropertyList.')}}');
    102102    $List = array();
    103     foreach($Result as $ResultLine)
     103    foreach ($Result as $ResultLine)
    104104    {
    105105      $ResultLineParts = explode(' ', $ResultLine);
    106106      $ListItem = array();
    107       foreach($ResultLineParts as $ResultLinePart)
     107      foreach ($ResultLineParts as $ResultLinePart)
    108108      {
    109109        $Value = explode('=', $ResultLinePart);
    110         if(count($Value) > 1) $ListItem[$Properties[$Value[0]]] = $Value[1];
     110        if (count($Value) > 1) $ListItem[$Properties[$Value[0]]] = $Value[1];
    111111          else $ListItem[$Properties[$Value[0]]] = '';
    112112      }
    113113      $List[] = $ListItem;
    114114    }
    115     return($List);
     115    return ($List);
    116116  }
    117117
     
    119119  {
    120120    $ConditionList = '';
    121     foreach($Conditions as $Index => $Item)
     121    foreach ($Conditions as $Index => $Item)
    122122    {
    123123      $ConditionList .= $Index.'="'.$Item.'" ';
    124124    }
    125125    $ConditionList = substr($ConditionList, 0, -1);
    126     if(trim($ConditionList) != '')
     126    if (trim($ConditionList) != '')
    127127      $ConditionList = ' where '.$ConditionList;
    128128
    129129    $Result = $this->Execute(implode(' ', $Path).' print terse'.$ConditionList);
    130130    $List = array();
    131     foreach($Result as $ResultLine)
     131    foreach ($Result as $ResultLine)
    132132    {
    133133      $ResultLineParts = explode(' ', $ResultLine);
    134134      $ListItem = array();
    135       foreach($ResultLineParts as $ResultLinePart)
     135      foreach ($ResultLineParts as $ResultLinePart)
    136136      {
    137137        $Value = explode('=', $ResultLinePart);
    138         if(in_array($Value[0], $Properties))
    139         {
    140           if(count($Value) > 1)
     138        if (in_array($Value[0], $Properties))
     139        {
     140          if (count($Value) > 1)
    141141          {
    142             if($Value[1]{0} == '"') $Value[1] = substr($Value[1], 1, -1);
    143             //if(strlen($Value[1]) > 0)
     142            if ($Value[1]{0} == '"') $Value[1] = substr($Value[1], 1, -1);
     143            //if (strlen($Value[1]) > 0)
    144144            $ListItem[$Value[0]] = $Value[1];
    145145          } else $ListItem[$Value[0]] = '';
    146146        }
    147147      }
    148       if(count($ListItem) > 0) $List[] = $ListItem;
    149     }
    150     return($List);
     148      if (count($ListItem) > 0) $List[] = $ListItem;
     149    }
     150    return ($List);
    151151  }
    152152
     
    159159  {
    160160    // Get current list from routerboard
    161     if($UsePrint == 0)
     161    if ($UsePrint == 0)
    162162    {
    163163      $List = $this->ListGet($Path, $Properties, $Condition);
    164164      // Change boolean values yes/no to true/false
    165       foreach($List as $Index => $ListItem)
    166       {
    167         foreach($ListItem as $Index2 => $Item2)
    168         {
    169           if($Item2 == 'true') $List[$Index][$Index2] = 'yes';
    170           if($Item2 == 'false') $List[$Index][$Index2] = 'no';
     165      foreach ($List as $Index => $ListItem)
     166      {
     167        foreach ($ListItem as $Index2 => $Item2)
     168        {
     169          if ($Item2 == 'true') $List[$Index][$Index2] = 'yes';
     170          if ($Item2 == 'false') $List[$Index][$Index2] = 'no';
    171171        }
    172172      }
     
    178178
    179179    // Add empty properties to values
    180     foreach($Values as $Index => $Item)
    181     {
    182       foreach($Properties as $Property)
    183       {
    184         if(!array_key_exists($Property, $Item))
     180    foreach ($Values as $Index => $Item)
     181    {
     182      foreach ($Properties as $Property)
     183      {
     184        if (!array_key_exists($Property, $Item))
    185185           $Item[$Property] = '';
    186186      }
    187187      $Values[$Index] = $Item;
    188188    }
    189     foreach($List as $Index => $Item)
    190     {
    191       foreach($Properties as $Property)
    192       {
    193         if(!array_key_exists($Property, $Item))
     189    foreach ($List as $Index => $Item)
     190    {
     191      foreach ($Properties as $Property)
     192      {
     193        if (!array_key_exists($Property, $Item))
    194194           $Item[$Property] = '';
    195195      }
     
    198198
    199199    // Sort properties
    200     foreach($Values as $Index => $Item)
     200    foreach ($Values as $Index => $Item)
    201201    {
    202202      ksort($Values[$Index]);
    203203    }
    204     foreach($List as $Index => $Item)
     204    foreach ($List as $Index => $Item)
    205205    {
    206206      ksort($List[$Index]);
    207207    }
    208     if($this->Debug) print_r($List);
    209     if($this->Debug) print_r($Values);
     208    if ($this->Debug) print_r($List);
     209    if ($this->Debug) print_r($Values);
    210210
    211211    // Erase all items not existed in $Values
    212     foreach($List as $Index => $ListItem)
    213     {
    214       if(!in_array($ListItem, $Values))
     212    foreach ($List as $Index => $ListItem)
     213    {
     214      if (!in_array($ListItem, $Values))
    215215      {
    216216        $Prop = '';
    217         foreach($ListItem as $Index => $Property)
    218         {
    219           if($Property != '')
     217        foreach ($ListItem as $Index => $Property)
     218        {
     219          if ($Property != '')
    220220          {
    221             if(($Property == 'yes') or ($Property == 'no')) $Prop .= $Index.'='.$Property.' ';
     221            if (($Property == 'yes') or ($Property == 'no')) $Prop .= $Index.'='.$Property.' ';
    222222              else $Prop .= $Index.'="'.$Property.'" ';
    223223          }
    224224        }
    225225        $Prop = substr($Prop, 0, -1);
    226         if(trim($Prop) != '')
     226        if (trim($Prop) != '')
    227227          $Commands[] = implode(' ', $Path).' remove [find '.$Prop.']';
    228228      }
     
    230230
    231231    // Add new items
    232     foreach($Values as $ListItem)
    233     {
    234       if(!in_array($ListItem, $List))
     232    foreach ($Values as $ListItem)
     233    {
     234      if (!in_array($ListItem, $List))
    235235      {
    236236        $Prop = '';
    237         foreach($ListItem as $Index => $Property)
    238         {
    239           if($Property != '') $Prop .= $Index.'="'.$Property.'" ';
     237        foreach ($ListItem as $Index => $Property)
     238        {
     239          if ($Property != '') $Prop .= $Index.'="'.$Property.'" ';
    240240        }
    241241        $Prop = substr($Prop, 0, -1);
     
    243243      }
    244244    }
    245     if($this->Debug) print_r($Commands);
    246     return($this->Execute($Commands));
     245    if ($this->Debug) print_r($Commands);
     246    return ($this->Execute($Commands));
    247247  }
    248248}
Note: See TracChangeset for help on using the changeset viewer.