Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //$urlpath = explode('/',$_SERVER['SCRIPT_NAME']);
- //count($urlpath) > 2 ? $urlpath= $urlpath[1] : $urlpath="";
- //
- //$includes_path=$_SERVER['DOCUMENT_ROOT'].'/'.$urlpath.'/includes/';
- //$classes_path=$_SERVER['DOCUMENT_ROOT'].'/'.$urlpath.'/classes/';
- //require_once ('includes/constants.php');
- require_once(APP_PATH . 'includes/constants.php');
- //require_once ('includes/constants.php');
- //require_once (APP_PATH.'includes/constants.php');
- //require_once('includes/FirePHP.class.php'); //must be delete when all prog is finished
- //$firephp = FirePHP::getInstance(true); //must be delete when all prog is finished
- //$firephp->log($x,'X'); //must be delete when all prog is finished
- //$firephp->log($val); //must be delete when all prog is finished
- class Others
- {
- private $conn;
- function __construct()
- {
- $this->conn = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_NAME) or die('There was a problem connecting to the database.');
- }
- //Get the greater number from list at table
- function get_greater_number($db_table_name, $list_field, $field_name = 0, $item_name = 0)
- {
- if (isset($field_name) && $field_name != "0") {
- $query = "SELECT $list_field
- FROM $db_table_name
- WHERE $field_name = '$item_name'";
- } else {
- $query = "SELECT $list_field
- FROM $db_table_name";
- }
- if ($stmt = $this->conn->query($query)) {
- if ($stmt->num_rows >= 1) {
- while ($row = $stmt->fetch_row()) {
- $newarray[] = $row;
- }
- $val = max($newarray);
- $x = $val[0];
- //$x = settype($val,'int');
- //$x = explode("",$val);
- /* free result set */
- $stmt->close();
- return $x;
- } else {
- return 0;
- }
- }
- } // End -> Get the greater number from list at table
- //Listing Server
- function print_server_list()
- {
- } //End -> Listing Server
- //Generate Password Randomlly
- function generatePassword($length = 6, $strength = 0)
- {
- $vowels = 'aeuy';
- $consonants = 'bdghjmnpqrstvz';
- if ($strength & 1) {
- $consonants .= 'BDGHJLMNPQRSTVWXZ';
- }
- if ($strength & 2) {
- $vowels .= "AEUY";
- }
- if ($strength & 4) {
- $consonants .= '23456789';
- }
- if ($strength & 8) {
- $consonants .= '@#$%';
- }
- $password = '';
- $alt = time() % 2;
- for ($i = 0; $i < $length; $i++) {
- if ($alt == 1) {
- $password .= $consonants[(rand() % strlen($consonants))];
- $alt = 0;
- } else {
- $password .= $vowels[(rand() % strlen($vowels))];
- $alt = 1;
- }
- }
- return $password;
- } //End -> Generate Password Randomlly
- //Generate Token Randomlly
- function generateToken($length = 12)
- {
- $prifix = "ABCDEF";
- $vowels = 'aeuy';
- $consonants = '123456789';
- $password = '';
- $password .= $prifix[(rand() % strlen($prifix))];
- $password .= $prifix[(rand() % strlen($prifix))];
- $alt = time() % 2;
- for ($i = 0; $i < $length; $i++) {
- if ($alt == 1) {
- $password .= $consonants[(rand() % strlen($consonants))];
- $alt = 0;
- } else {
- $password .= $consonants[(rand() % strlen($vowels))];
- $alt = 1;
- }
- }
- return $password;
- } //End -> Generate Token Randomlly
- //Dump Data
- function dump($data)
- {
- echo '<pre style="background:#fff; text-align:left">';
- var_dump($data);
- echo '</pre>';
- }
- function check_username($username)
- {
- $username = trim($username); // strip any white space
- $response = array(); // our response
- // if the username is blank
- if (!$username) {
- $response = array(
- 'ok' => false,
- 'msg' => "Please specify a username"
- );
- // if the username does not match a-z or '.', '-', '_' then it's not valid
- } else if (!preg_match('/^[a-z0-9.-_]+$/', $username)) {
- $response = array(
- 'ok' => false,
- 'msg' => "Your username can only contain alphanumerics and period, dash and underscore (.-_)"
- );
- // this would live in an external library just to check if the username is taken
- } else if (username_taken($username)) {
- $response = array(
- 'ok' => false,
- 'msg' => "The selected username is not available"
- );
- // it's all good
- } else {
- $response = array(
- 'ok' => true,
- 'msg' => "This username is free"
- );
- }
- return $response;
- }
- //Get time lift for user = expire - now ;
- function timeleft($expire)
- {
- list($dayx, $monthx, $yearx) = explode("/", $expire);
- $now = mktime(0, 0, 0, date("m"), date("d"), date("Y"));
- $expire = mktime(0, 0, 0, $monthx, $dayx, $yearx);
- $timeleft = $expire - $now;
- if ($timeleft <= 0) {
- $daysleft = 0;
- } else {
- $daysleft = round((($timeleft / 24) / 60) / 60); //probably...
- }
- return array(
- timeleft => $timeleft,
- daysleft => $daysleft
- );
- }
- public static function addZeroToDate($date)
- {
- $dateArray = explode("/", $date);
- foreach ($dateArray as $value) {
- if (preg_match("/^([0-9]{1})$/", $value)) {
- $newDate[] = "0" . $value;
- } else {
- $newDate[] = $value;
- }
- }
- $date = implode("/", $newDate);
- return $date;
- }
- //Get time lift for user = expire - now ;
- public static function Statictimeleft($expire)
- {
- list($dayx, $monthx, $yearx) = explode("/", $expire);
- $now = mktime(0, 0, 0, date("m"), date("d"), date("Y"));
- $expire = mktime(0, 0, 0, $monthx, $dayx, $yearx);
- $timeleft = $expire - $now;
- if ($timeleft <= 0) {
- $daysleft = 0;
- } else {
- $daysleft = round((($timeleft / 24) / 60) / 60); //probably...
- }
- return array(
- "timeleft" => $timeleft,
- "daysleft" => $daysleft
- );
- }
- //======================================== Create Zip file === [ start ] =================================
- /* creates a compressed zip file */
- function create_zip($files = array(), $destination = '', $overwrite = false)
- {
- //if the zip file already exists and overwrite is false, return false
- if (file_exists($destination) && !$overwrite) {
- return false;
- }
- //vars
- $valid_files = array();
- //if files were passed in...
- if (is_array($files)) {
- //cycle through each file
- foreach ($files as $file) {
- //make sure the file exists
- if (file_exists($file)) {
- $valid_files[] = $file;
- }
- }
- }
- //if we have good files...
- if (count($valid_files)) {
- //create the archive
- $zip = new ZipArchive();
- if ($zip->open($destination, $overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) {
- return false;
- }
- //add the files
- foreach ($valid_files as $file) {
- $file_parts = explode("/", $file);
- $expolde_parts_number = count($file_parts);
- $last_count_value1 = $expolde_parts_number - 1;
- $last_count_value2 = $expolde_parts_number - 2;
- $file_name = '/var/' . $file_parts[$last_count_value2] . '/' . $file_parts[$last_count_value1];
- //$file_name = $file_parts[$last_count_value];
- $zip->addFile($file, $file_name);
- }
- //debug
- //echo 'The zip archive contains ',$zip->numFiles,' files with a status of ',$zip->status;
- //close the zip -- done!
- $zip->close();
- //check to make sure the file exists
- return file_exists($destination);
- } else {
- return false;
- }
- }
- //This example to use this function
- //$files_to_zip = array(
- //'preload-images/1.jpg',
- //'preload-images/2.jpg',
- //'preload-images/5.jpg',
- //'kwicks/ringo.gif',
- //'rod.jpg',
- //'reddit.gif'
- //);
- ////if true, good; if false, zip creation failed
- //$result = create_zip($files_to_zip,'my-archive.zip');
- //======================================== Create Zip file === [ End ] =================================
- //======================================== Import User File === [ start ] =================================
- /* Import User file */
- public static function cleanSpaces($string)
- {
- $cleanData1 = trim($string);
- $cleanData2 = str_replace('\t', '', $cleanData1);
- return $cleanData1;
- }
- public static function importUserFile($file)
- {
- $response = "";
- $error = "";
- // Checking File type
- if ($file["error"] > 0) {
- $error = "Error: " . $file["error"] . "<br />";
- } elseif ($file["type"] <> "text/plain") {
- $error = "Error: File type is not compatable." . "<br />";
- } else {
- $response = "Upload: " . $file["name"] . "<br />";
- $response .= "Type: " . $file["type"] . "<br />";
- $response .= "Size: " . ($file["size"] / 1024) . " Kb<br />";
- $response .= "Stored in: " . $file["tmp_name"] . "<br />";
- if (is_dir('upload') && empty($error)) {
- move_uploaded_file($file["tmp_name"], "upload/" . "tempfile.txt");
- $response .= "Stored in: " . "upload/tempfile.txt";
- $userList = Others::getUsersFromFile();
- $response = !$error ? $response : $response . $error;
- return array(
- "list" => $userList,
- "response" => $response
- );
- } else {
- $response = "Error: Upload folder is not exist." . "<br />";
- return array(
- "list" => null,
- "response" => $response
- );
- }
- }
- $response = !$error ? $response : $response . $error;
- return array(
- "list" => null,
- "response" => $response
- );
- }
- public static function getUsersFromFile()
- {
- $data = file_get_contents("upload/tempfile.txt");
- $line = explode("\n", $data);
- for ($i = 0; $i < count($line); $i++) {
- if (empty($line[$i])) {
- continue;
- }
- $pair = explode(",", $line[$i]);
- $pairsNumber = count($pair);
- $username = Others::cleanSpaces($pair[0]);
- $password = Others::cleanSpaces($pair[1]);
- $expireDate = ($pairsNumber == 3) ? Others::cleanSpaces($pair[2]) : null;
- $userList[] = array(
- 'username' => $username,
- 'password' => $password,
- 'expireDate' => $expireDate
- );
- }
- return $userList;
- }
- public static function displayListTxt($usersList, $posts)
- {
- $txtOut = "";
- $txtOutRows = "";
- foreach ($usersList as $value) {
- $username = $value['username'];
- $password = $value['password'];
- $expireDate = $value['expireDate'];
- $txtOutRows .= $username . " - " . $password . " - " . $expireDate . "\n";
- }
- $txtOut = " [ Users will Import to Database ]" . "\n";
- $txtOut .= "===============================" . "\n";
- $txtOut .= "Expire Date : " . $posts['expDateInput'] . "\n";
- $txtOut .= "Server : " . $posts['serverInput'] . "\n";
- $txtOut .= "Group : " . $posts['groupInput'] . "\n";
- $txtOut .= "Status : " . $posts['statusInput'] . "\n";
- $txtOut .= "==========================================" . "\n";
- $txtOut .= $txtOutRows;
- return array(
- "txtOut" => $txtOut,
- "posts" => $posts
- );
- }
- public function writeUsersToDb($info)
- {
- $expDateInput = $info['expDateInput'];
- $serverInput = $info['serverInput'];
- $groupInput = $info['groupInput'];
- $resellerInput = $info['resellerInput'];
- $statusInput = $info['statusInput'];
- $missedUserArray = array();
- $missedUser = "";
- $userList = Others::getUsersFromFile();
- $query = "INSERT INTO users (login, password, enabled, server, serverinfo, reseller, modified, expire) VALUES ";
- $current_key = 0;
- foreach ($userList as $value) {
- $current_key++;
- $username = $value['username'];
- $password = $value['password'];
- $expireDate = $value['expireDate'];
- $expireDate = Others::addZeroToDate($expireDate);
- if (!preg_match("/^([0-9]{2})\/([0-9]{2})\/([0-9]{4})$/", $expireDate) && $expireDate <> null) {
- $expireDate = null;
- $missedUserArray[] = $username;
- }
- // $expireDate= ( $expireDate == null ) ? "" : "'" . $value['expireDate'] . "'";
- $query .= "('$username', '$password', $statusInput, $groupInput, $serverInput, $resellerInput, NOW(), ";
- $query .= ($expireDate == null) ? "$expDateInput)" : "'" . $value['expireDate'] . "')";
- $query .= ($current_key == count($userList)) ? "; \n" : ", \n";
- }
- if (count($missedUserArray) >= 1) {
- $missedUser = "the following users have invalid dates and go for default values:";
- foreach ($missedUserArray as $value) {
- $missedUser .= $value;
- $missedUser .= (count($missedUserArray) - 1 <> 1) ? ", " : ".";
- }
- }
- if ($stmt = $this->conn->query($query)) {
- $responce = "Users successfully Imported.\n" . "<br/>" . $missedUser;
- return $responce;
- } else {
- return $responce = "Error: " . $this->conn->error;
- }
- }
- //========================================= End Class =====================================================
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement