Advertisement
Guest User

Others.php

a guest
Sep 30th, 2012
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 15.70 KB | None | 0 0
  1. <?php
  2.  
  3. //$urlpath = explode('/',$_SERVER['SCRIPT_NAME']);
  4. //count($urlpath) > 2 ? $urlpath= $urlpath[1] : $urlpath="";
  5. //
  6. //$includes_path=$_SERVER['DOCUMENT_ROOT'].'/'.$urlpath.'/includes/';
  7. //$classes_path=$_SERVER['DOCUMENT_ROOT'].'/'.$urlpath.'/classes/';
  8.  
  9. //require_once ('includes/constants.php');
  10. require_once(APP_PATH . 'includes/constants.php');
  11. //require_once ('includes/constants.php');
  12. //require_once (APP_PATH.'includes/constants.php');
  13. //require_once('includes/FirePHP.class.php'); //must be delete when all prog is finished
  14.  
  15. //$firephp = FirePHP::getInstance(true);  //must be delete when all prog is finished
  16. //$firephp->log($x,'X');  //must be delete when all prog is finished
  17. //$firephp->log($val);  //must be delete when all prog is finished
  18.  
  19.  
  20. class Others
  21. {
  22.     private $conn;
  23.    
  24.    
  25.     function __construct()
  26.     {
  27.         $this->conn = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_NAME) or die('There was a problem connecting to the database.');
  28.     }
  29.    
  30.    
  31.     //Get the greater number from list at table  
  32.     function get_greater_number($db_table_name, $list_field, $field_name = 0, $item_name = 0)
  33.     {
  34.         if (isset($field_name) && $field_name != "0") {
  35.             $query = "SELECT $list_field
  36.                      FROM $db_table_name
  37.                      WHERE $field_name = '$item_name'";
  38.         } else {
  39.             $query = "SELECT $list_field
  40.                      FROM $db_table_name";
  41.         }
  42.         if ($stmt = $this->conn->query($query)) {
  43.             if ($stmt->num_rows >= 1) {
  44.                 while ($row = $stmt->fetch_row()) {
  45.                     $newarray[] = $row;
  46.                 }
  47.                
  48.                
  49.                 $val = max($newarray);
  50.                 $x   = $val[0];
  51.                 //$x = settype($val,'int');
  52.                 //$x = explode("",$val);
  53.                
  54.                
  55.                 /* free result set */
  56.                 $stmt->close();
  57.                
  58.                 return $x;
  59.             } else {
  60.                 return 0;
  61.             }
  62.            
  63.         }
  64.        
  65.     } // End -> Get the greater number from list at table
  66.    
  67.     //Listing Server
  68.     function print_server_list()
  69.     {
  70.     } //End -> Listing Server
  71.    
  72.    
  73.     //Generate Password Randomlly
  74.     function generatePassword($length = 6, $strength = 0)
  75.     {
  76.         $vowels     = 'aeuy';
  77.         $consonants = 'bdghjmnpqrstvz';
  78.         if ($strength & 1) {
  79.             $consonants .= 'BDGHJLMNPQRSTVWXZ';
  80.         }
  81.         if ($strength & 2) {
  82.             $vowels .= "AEUY";
  83.         }
  84.         if ($strength & 4) {
  85.             $consonants .= '23456789';
  86.         }
  87.         if ($strength & 8) {
  88.             $consonants .= '@#$%';
  89.         }
  90.        
  91.         $password = '';
  92.         $alt      = time() % 2;
  93.         for ($i = 0; $i < $length; $i++) {
  94.             if ($alt == 1) {
  95.                 $password .= $consonants[(rand() % strlen($consonants))];
  96.                 $alt = 0;
  97.             } else {
  98.                 $password .= $vowels[(rand() % strlen($vowels))];
  99.                 $alt = 1;
  100.             }
  101.         }
  102.         return $password;
  103.     } //End -> Generate Password Randomlly
  104.    
  105.    
  106.     //Generate Token Randomlly
  107.     function generateToken($length = 12)
  108.     {
  109.         $prifix     = "ABCDEF";
  110.         $vowels     = 'aeuy';
  111.         $consonants = '123456789';
  112.        
  113.         $password = '';
  114.         $password .= $prifix[(rand() % strlen($prifix))];
  115.         $password .= $prifix[(rand() % strlen($prifix))];
  116.         $alt = time() % 2;
  117.         for ($i = 0; $i < $length; $i++) {
  118.             if ($alt == 1) {
  119.                 $password .= $consonants[(rand() % strlen($consonants))];
  120.                 $alt = 0;
  121.             } else {
  122.                 $password .= $consonants[(rand() % strlen($vowels))];
  123.                 $alt = 1;
  124.             }
  125.         }
  126.         return $password;
  127.     } //End -> Generate Token Randomlly
  128.    
  129.    
  130.     //Dump Data
  131.     function dump($data)
  132.     {
  133.         echo '<pre style="background:#fff; text-align:left">';
  134.         var_dump($data);
  135.         echo '</pre>';
  136.     }
  137.    
  138.    
  139.    
  140.    
  141.    
  142.    
  143.     function check_username($username)
  144.     {
  145.         $username = trim($username); // strip any white space
  146.         $response = array(); // our response
  147.        
  148.         // if the username is blank
  149.         if (!$username) {
  150.             $response = array(
  151.                 'ok' => false,
  152.                 'msg' => "Please specify a username"
  153.             );
  154.            
  155.             // if the username does not match a-z or '.', '-', '_' then it's not valid
  156.         } else if (!preg_match('/^[a-z0-9.-_]+$/', $username)) {
  157.             $response = array(
  158.                 'ok' => false,
  159.                 'msg' => "Your username can only contain alphanumerics and period, dash and underscore (.-_)"
  160.             );
  161.            
  162.             // this would live in an external library just to check if the username is taken
  163.         } else if (username_taken($username)) {
  164.             $response = array(
  165.                 'ok' => false,
  166.                 'msg' => "The selected username is not available"
  167.             );
  168.            
  169.             // it's all good
  170.         } else {
  171.             $response = array(
  172.                 'ok' => true,
  173.                 'msg' => "This username is free"
  174.             );
  175.         }
  176.        
  177.         return $response;
  178.     }
  179.    
  180.    
  181.    
  182.    
  183.     //Get time lift for user  = expire - now ;
  184.     function timeleft($expire)
  185.     {
  186.         list($dayx, $monthx, $yearx) = explode("/", $expire);
  187.         $now      = mktime(0, 0, 0, date("m"), date("d"), date("Y"));
  188.         $expire   = mktime(0, 0, 0, $monthx, $dayx, $yearx);
  189.         $timeleft = $expire - $now;
  190.         if ($timeleft <= 0) {
  191.             $daysleft = 0;
  192.         } else {
  193.             $daysleft = round((($timeleft / 24) / 60) / 60); //probably...
  194.         }
  195.        
  196.        
  197.         return array(
  198.             timeleft => $timeleft,
  199.             daysleft => $daysleft
  200.         );
  201.        
  202.        
  203.     }
  204.    
  205.    
  206.    
  207.     public static function addZeroToDate($date)
  208.     {
  209.         $dateArray = explode("/", $date);
  210.        
  211.         foreach ($dateArray as $value) {
  212.             if (preg_match("/^([0-9]{1})$/", $value)) {
  213.                 $newDate[] = "0" . $value;
  214.             } else {
  215.                 $newDate[] = $value;
  216.             }
  217.         }
  218.        
  219.         $date = implode("/", $newDate);
  220.        
  221.         return $date;
  222.     }
  223.    
  224.    
  225.     //Get time lift for user  = expire - now ;
  226.     public static function Statictimeleft($expire)
  227.     {
  228.         list($dayx, $monthx, $yearx) = explode("/", $expire);
  229.         $now      = mktime(0, 0, 0, date("m"), date("d"), date("Y"));
  230.         $expire   = mktime(0, 0, 0, $monthx, $dayx, $yearx);
  231.         $timeleft = $expire - $now;
  232.         if ($timeleft <= 0) {
  233.             $daysleft = 0;
  234.         } else {
  235.             $daysleft = round((($timeleft / 24) / 60) / 60); //probably...
  236.         }
  237.        
  238.        
  239.         return array(
  240.             "timeleft" => $timeleft,
  241.             "daysleft" => $daysleft
  242.         );
  243.        
  244.        
  245.     }
  246.    
  247.     //========================================  Create Zip file === [ start ] =================================
  248.    
  249.     /* creates a compressed zip file */
  250.     function create_zip($files = array(), $destination = '', $overwrite = false)
  251.     {
  252.         //if the zip file already exists and overwrite is false, return false
  253.         if (file_exists($destination) && !$overwrite) {
  254.             return false;
  255.         }
  256.         //vars
  257.         $valid_files = array();
  258.         //if files were passed in...
  259.         if (is_array($files)) {
  260.             //cycle through each file
  261.             foreach ($files as $file) {
  262.                 //make sure the file exists
  263.                 if (file_exists($file)) {
  264.                     $valid_files[] = $file;
  265.                 }
  266.             }
  267.         }
  268.         //if we have good files...
  269.         if (count($valid_files)) {
  270.             //create the archive
  271.             $zip = new ZipArchive();
  272.             if ($zip->open($destination, $overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) {
  273.                 return false;
  274.             }
  275.             //add the files
  276.             foreach ($valid_files as $file) {
  277.                 $file_parts           = explode("/", $file);
  278.                 $expolde_parts_number = count($file_parts);
  279.                 $last_count_value1    = $expolde_parts_number - 1;
  280.                 $last_count_value2    = $expolde_parts_number - 2;
  281.                 $file_name            = '/var/' . $file_parts[$last_count_value2] . '/' . $file_parts[$last_count_value1];
  282.                 //$file_name = $file_parts[$last_count_value];
  283.                 $zip->addFile($file, $file_name);
  284.             }
  285.             //debug
  286.             //echo 'The zip archive contains ',$zip->numFiles,' files with a status of ',$zip->status;
  287.            
  288.             //close the zip -- done!
  289.             $zip->close();
  290.            
  291.             //check to make sure the file exists
  292.             return file_exists($destination);
  293.         } else {
  294.             return false;
  295.         }
  296.     }
  297.    
  298.     //This example to use this function
  299.     //$files_to_zip = array(
  300.     //'preload-images/1.jpg',
  301.     //'preload-images/2.jpg',
  302.     //'preload-images/5.jpg',
  303.     //'kwicks/ringo.gif',
  304.     //'rod.jpg',
  305.     //'reddit.gif'
  306.     //);
  307.     ////if true, good; if false, zip creation failed
  308.     //$result = create_zip($files_to_zip,'my-archive.zip');
  309.    
  310.    
  311.     //========================================  Create Zip file === [  End  ] =================================
  312.    
  313.     //========================================  Import User File === [ start ] =================================
  314.    
  315.     /* Import User file */
  316.    
  317.     public static function cleanSpaces($string)
  318.     {
  319.         $cleanData1 = trim($string);
  320.         $cleanData2 = str_replace('\t', '', $cleanData1);
  321.         return $cleanData1;
  322.        
  323.     }
  324.    
  325.     public static function importUserFile($file)
  326.     {
  327.         $response = "";
  328.         $error    = "";
  329.        
  330.         // Checking File type
  331.         if ($file["error"] > 0) {
  332.             $error = "Error: " . $file["error"] . "<br />";
  333.         } elseif ($file["type"] <> "text/plain") {
  334.             $error = "Error: File type is not compatable." . "<br />";
  335.         } else {
  336.             $response = "Upload: " . $file["name"] . "<br />";
  337.             $response .= "Type: " . $file["type"] . "<br />";
  338.             $response .= "Size: " . ($file["size"] / 1024) . " Kb<br />";
  339.             $response .= "Stored in: " . $file["tmp_name"] . "<br />";
  340.            
  341.             if (is_dir('upload') && empty($error)) {
  342.                 move_uploaded_file($file["tmp_name"], "upload/" . "tempfile.txt");
  343.                 $response .= "Stored in: " . "upload/tempfile.txt";
  344.                 $userList = Others::getUsersFromFile();
  345.                
  346.                 $response = !$error ? $response : $response . $error;
  347.                
  348.                 return array(
  349.                     "list" => $userList,
  350.                     "response" => $response
  351.                 );
  352.             } else {
  353.                 $response = "Error: Upload folder is not exist." . "<br />";
  354.                 return array(
  355.                     "list" => null,
  356.                     "response" => $response
  357.                 );
  358.             }
  359.         }
  360.         $response = !$error ? $response : $response . $error;
  361.         return array(
  362.             "list" => null,
  363.             "response" => $response
  364.         );
  365.     }
  366.    
  367.     public static function getUsersFromFile()
  368.     {
  369.         $data = file_get_contents("upload/tempfile.txt");
  370.        
  371.         $line = explode("\n", $data);
  372.         for ($i = 0; $i < count($line); $i++) {
  373.             if (empty($line[$i])) {
  374.                 continue;
  375.             }
  376.             $pair        = explode(",", $line[$i]);
  377.             $pairsNumber = count($pair);
  378.             $username    = Others::cleanSpaces($pair[0]);
  379.             $password    = Others::cleanSpaces($pair[1]);
  380.             $expireDate  = ($pairsNumber == 3) ? Others::cleanSpaces($pair[2]) : null;
  381.            
  382.             $userList[] = array(
  383.                 'username' => $username,
  384.                 'password' => $password,
  385.                 'expireDate' => $expireDate
  386.             );
  387.            
  388.         }
  389.         return $userList;
  390.        
  391.        
  392.     }
  393.    
  394.    
  395.     public static function displayListTxt($usersList, $posts)
  396.     {
  397.         $txtOut     = "";
  398.         $txtOutRows = "";
  399.        
  400.         foreach ($usersList as $value) {
  401.             $username   = $value['username'];
  402.             $password   = $value['password'];
  403.             $expireDate = $value['expireDate'];
  404.             $txtOutRows .= $username . " - " . $password . " - " . $expireDate . "\n";
  405.         }
  406.        
  407.         $txtOut = " [ Users will Import to Database ]" . "\n";
  408.         $txtOut .= "===============================" . "\n";
  409.         $txtOut .= "Expire Date : " . $posts['expDateInput'] . "\n";
  410.         $txtOut .= "Server : " . $posts['serverInput'] . "\n";
  411.         $txtOut .= "Group : " . $posts['groupInput'] . "\n";
  412.         $txtOut .= "Status : " . $posts['statusInput'] . "\n";
  413.         $txtOut .= "==========================================" . "\n";
  414.         $txtOut .= $txtOutRows;
  415.        
  416.         return array(
  417.             "txtOut" => $txtOut,
  418.             "posts" => $posts
  419.         );
  420.     }
  421.    
  422.    
  423.     public function writeUsersToDb($info)
  424.     {
  425.         $expDateInput  = $info['expDateInput'];
  426.         $serverInput   = $info['serverInput'];
  427.         $groupInput    = $info['groupInput'];
  428.         $resellerInput = $info['resellerInput'];
  429.         $statusInput   = $info['statusInput'];
  430.        
  431.         $missedUserArray = array();
  432.         $missedUser      = "";
  433.        
  434.         $userList = Others::getUsersFromFile();
  435.        
  436.         $query = "INSERT INTO users (login, password, enabled, server, serverinfo, reseller, modified, expire) VALUES ";
  437.        
  438.         $current_key = 0;
  439.        
  440.         foreach ($userList as $value) {
  441.             $current_key++;
  442.             $username   = $value['username'];
  443.             $password   = $value['password'];
  444.             $expireDate = $value['expireDate'];
  445.             $expireDate = Others::addZeroToDate($expireDate);
  446.            
  447.             if (!preg_match("/^([0-9]{2})\/([0-9]{2})\/([0-9]{4})$/", $expireDate) && $expireDate <> null) {
  448.                 $expireDate        = null;
  449.                 $missedUserArray[] = $username;
  450.             }
  451.            
  452.             // $expireDate= ( $expireDate == null ) ? "" : "'" . $value['expireDate'] . "'";
  453.            
  454.             $query .= "('$username', '$password', $statusInput, $groupInput, $serverInput, $resellerInput, NOW(), ";
  455.             $query .= ($expireDate == null) ? "$expDateInput)" : "'" . $value['expireDate'] . "')";
  456.             $query .= ($current_key == count($userList)) ? "; \n" : ", \n";
  457.         }
  458.         if (count($missedUserArray) >= 1) {
  459.             $missedUser = "the following users have invalid dates and go for default values:";
  460.             foreach ($missedUserArray as $value) {
  461.                 $missedUser .= $value;
  462.                 $missedUser .= (count($missedUserArray) - 1 <> 1) ? ", " : ".";
  463.             }
  464.         }
  465.        
  466.         if ($stmt = $this->conn->query($query)) {
  467.             $responce = "Users successfully Imported.\n" . "<br/>" . $missedUser;
  468.             return $responce;
  469.         } else {
  470.             return $responce = "Error: " . $this->conn->error;
  471.         }
  472.        
  473.     }
  474.    
  475.     //========================================= End Class =====================================================
  476. }
  477.  
  478. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement