Guest User

Untitled

a guest
Oct 23rd, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.43 KB | None | 0 0
  1. class controller_base extends base{
  2.     function __construct(){
  3.         parent::__construct();
  4.     }
  5.     protected function generate_password($number){
  6.         $arr = array('a','b','c','d','e','f',
  7.                      'g','h','i','j','k','l',
  8.                      'm','n','o','q','p','r','s',
  9.                      't','u','v','w','x','y','z',
  10.                      'A','B','C','D','E','F',
  11.                      'G','H','I','J','K','L',
  12.                      'M','N','O','Q','P','R','S',
  13.                      'T','U','V','W','X','Y','Z',
  14.                      '1','2','3','4','5','6',
  15.                      '7','8','9','0');
  16.         // Генерируем пароль
  17.         $pass = "";
  18.         for($i = 0; $i < $number; $i++){
  19.             // Вычисляем случайный индекс массива
  20.             $index = rand(0, 61);
  21.             $pass .= $arr[$index];
  22.         }
  23.         return $pass;
  24.     }
  25.    
  26.     private function add_child($dom,$root,$child_name,$child_value='',$array_attr=''){
  27.         if (empty($root)) throw new XML_Exception("Не установлен root параметр",0);
  28.         if (empty($child_name)) throw new XML_Exception("Не установлен child_name параметр",1);
  29.         $child=$dom->createElement($child_name);
  30.         if (isset($child_value)) {
  31.             if (is_array($child_value)) {
  32.                 $array_attr=$child_value;
  33.             } else {
  34.                 $child->appendChild($dom->createTextNode($child_value));
  35.             }
  36.         }
  37.         if (is_array($array_attr)){
  38.             foreach ($array_attr as $attr_name => $attr_value){
  39.                 $child->setAttribute($attr_name,$attr_value);  
  40.             }
  41.         }
  42.         return $root->appendChild($child);
  43.     }
  44.    
  45.     private function addch_fr_arr($dom,$root,$parametrs){
  46.         foreach ($parametrs as $key=>$value){
  47.             $child_name=$key;
  48.             if (empty($value)) {
  49.                 $child=$this->add_child($dom,$root,$child_name,$child_value,$array_attr);
  50.                 //throw new Exception("Пустой xml",2);
  51.             } else {
  52.                 foreach ($value as $key=>$value){
  53.                     if ($key===0){
  54.                         $child_value=$value;
  55.                     } elseif ($key=="attr"){
  56.                         $array_attr = $value;
  57.                     } elseif ($key=="child"){
  58.                         $child_array=$value;
  59.                     } else {
  60.                         $child_value=$value;
  61.                     }
  62.                 }
  63.                 $child=$this->add_child($dom,$root,$child_name,$child_value,$array_attr);
  64.                 unset($child_value);
  65.                 if (is_array($child_array)) $this->addch_fr_arr($dom,$child,$child_array);
  66.             }
  67.         }
  68.     }
  69.    
  70.     public function arr2xml($charset,$parametrs){
  71.         $dom = new DOMDocument('1.0', $charset);
  72.         $this->addch_fr_arr($dom,$dom,$parametrs);
  73.         return $dom->saveXML();
  74.     }
  75. }
Add Comment
Please, Sign In to add comment