Source codes, small scripts...

Collapse
This is a sticky topic.
X
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    include very easy

    Special to reuse code in different folders
    Code function:
    PHP Code:
    function inc ($inc) {
    $result count(explode ("/",$_SERVER['PHP_SELF']))-2;
    switch(
    $result){
    case 
    '0':$inc='';break;
    case 
    '1':$inc='../';break;
    case 
    '2':$inc='../../';break;
    case 
    '3':$inc='../../../';break;
    case 
    '4':$inc='../../../../';break;
    case 
    '5':$inc='../../../../../';break;
                       }
    return 
    "$inc";

    Example:
    PHP Code:
    $inc inc(inc);
    include_once(
    $inc."/config.php"); 

    Comment


      mcrypt based encrytpion/decrypt if it is instal on ur sver
      PHP Code:
      #########################################################
      function encrypt($input)
      {
          
      $iv_size mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256MCRYPT_MODE_ECB);
          
      $iv mcrypt_create_iv($iv_sizeMCRYPT_RAND);
          
      $textkey 'ZTlhZGY';
          return 
      base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256hash('sha256',$textkey,TRUE), $inputMCRYPT_MODE_ECB$iv));
      }
      #########################################################
      function decrypt($input)
      {
          
      $iv_size mcrypt_get_iv_size(MCRYPT_RIJNDAEL_256MCRYPT_MODE_ECB);
          
      $iv mcrypt_create_iv($iv_sizeMCRYPT_RAND);
          
      $textkey 'ZTlhZGY';
          return 
      trim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256hash('sha256',$textkey,TRUE), base64_decode($input), MCRYPT_MODE_ECB$iv));
      }
      ######################################################### 

      Comment


        I can't remember the topic where I put last time and date snippet but it is not quite exact in months and years, so this one is ok.
        Replacement language is Serbian.

        PHP Code:
        function TimeDate() {
        $dan date('l'time() + (60 60));
        $dan str_replace('Monday''Ponedeljak'$dan);
        $dan str_replace('Tuesday''Utorak'$dan);
        $dan str_replace('Wednesday''Sreda'$dan);
        $dan str_replace('Thursday''Četvrtak'$dan);
        $dan str_replace('Friday''Petak'$dan);
        $dan str_replace('Saturday''Subota'$dan);
        $dan str_replace('Sunday''Nedelja'$dan);

        $datum date('d.F Y.'time() + (60 60));
        $datum str_replace('January''Januar'$datum);
        $datum str_replace('February''Februar'$datum);
        $datum str_replace('March''Mart'$datum);
        $datum str_replace('April''April'$datum);
        $datum str_replace('May''Maj'$datum);
        $datum str_replace('June''Jun'$datum);
        $datum str_replace('July''Jul'$datum);
        $datum str_replace('August''Avgust'$datum);
        $datum str_replace('September''Septembar'$datum);
        $datum str_replace('October''Oktobar'$datum);
        $datum str_replace('November''Novembar'$datum);
        $datum str_replace('December''Decembar'$datum);

        $sat date('H:i'time() + (60 60));

        echo 
        '
        <p>'
        .$dan.', '.$datum.' '.$sat.'</p>';
         } 
        To adjust change value 2 to your needs in (2 * 60 * 60) , rest don't change.

        Echo it:

        PHP Code:
        echo TimeDate(); 
        Looks like:

        Petak, 28.Januar 2011. 20:03
        Last edited by arnage; 28.01.11, 20:08.
        <!DOCTYPE html PUBLIC "-//WAPFORUM.RS

        Comment


          Input link validation regardless is written http:// or not:

          PHP Code:
          if (!preg_match('/^(http:\/\/)?[a-z0-9-\.]+\.[a-z]{2,4}$/iD'$var)) {
          echo 
          'Invalid URL format!';

          Last edited by arnage; 18.03.12, 07:52.
          <!DOCTYPE html PUBLIC "-//WAPFORUM.RS

          Comment


            A useful little function to convert the symbols in the different inputs.

            PHP Code:
            function ConvertSimbols($var$ConvertQuotes 0) {
            if (
            $ConvertQuotes 0) {
            $var htmlentities($varENT_NOQUOTES'UTF-8');
            $var str_replace(array('\"'"\'"), ''$var);
            } else {
            $var htmlentities($varENT_QUOTES'UTF-8');
            }
            return 
            $var;

            Usage with quotes for example message:
            PHP Code:
            $message ConvertSimbols($message); 
            Usage without quotes for example link:
            PHP Code:
            $link ConvertSimbols($link1); 
            Get user agent.

            PHP Code:
            function user_agent() {
            $Browser = isset($_SERVER['HTTP__AGENT']) ? $_SERVER['HTTP__AGENT'] : '';
            $Browser = isset($_SERVER['HTTP_X_OPERAMINI_PHONE_UA']) ? $_SERVER['HTTP_X_OPERAMINI_PHONE_UA'] : $Browser;
            $Browser = isset($_SERVER['HTTP_X_ORIGINAL__AGENT']) ? $_SERVER['HTTP_X_ORIGINAL__AGENT'] : $Browser;
            $Browser = isset($_SERVER['HTTP_X_DEVICE__AGENT']) ? $_SERVER['HTTP_X_DEVICE__AGENT'] : $Browser;
            $Browser = isset($_SERVER['HTTP_X_MOBILE_UA']) ? $_SERVER['HTTP_X_MOBILE_UA'] : $Browser;
            $Browser = isset($_SERVER['HTTP_X_BOLT_PHONE_UA']) ? $_SERVER['HTTP_X_BOLT_PHONE_UA'] : $Browser;
            $Agent htmlentities($BrowserENT_QUOTES'UTF-8');
            if (empty(
            $Agent)) {
            $Agent 'ProxyUA';
            }
            return 
            $Agent;

            Last edited by arnage; 17.04.11, 17:18.
            <!DOCTYPE html PUBLIC "-//WAPFORUM.RS

            Comment


              From php.net comments:

              PHP Code:
              function openFile($file$mode$input) {
                  if (
              $mode == "READ") {
                      if (
              file_exists($file)) {
                          
              $handle fopen($file"r"); 
                          
              $output fread($handlefilesize($file));
                          return 
              $output// output file text
                      
              } else {
                          return 
              false// failed.
                      
              }
                  } elseif (
              $mode == "WRITE") {
                      
              $handle fopen($file"w");
                      if (!
              fwrite($handle$input)) {
                          return 
              false// failed.
                      
              } else {
                          return 
              true//success.
                      
              }
                  } elseif (
              $mode == "READ/WRITE") {        
                      if (
              file_exists($file) && isset($input)) {
                          
              $handle fopen($file"r+");
                          
              $read fread($handlefilesize($file));
                          
              $data $read.$input;
                          if (!
              fwrite($handle$data)) {
                              return 
              false// failed.
                          
              } else {
                              return 
              true// success.
                          
              }
                      } else {
                          return 
              false// failed.
                      
              }
                  } else {
                      return 
              false// failed.
                  
              }
                  
              fclose($handle); 

              Example calls are:

              PHP Code:
              openFile("files/text.txt""WRITE""Hello World!");
              echo 
              openFile("files/text.txt""READ"); // OUTPUT > Hello World!
              openFile("files/text.txt""READ/WRITE""!!");
              echo 
              openFile("files/text.txt""READ"); // OUTPUT > Hello World!!! 
              And i edited it to be more usable for me with:

              PHP Code:
              elseif ($mode == 'UPDATE') {
              $marker "\n";
              if (
              is_writeable($file) && isset($input)) {
              $handle = @fopen($file'a');
              $data $input.$marker;
              if (!
              fwrite($handle$data)) {
              return 
              false;
              } else {
              return 
              true;
              }
              } else {
              return 
              false;
              }

              Last edited by arnage; 19.04.11, 15:57.
              <!DOCTYPE html PUBLIC "-//WAPFORUM.RS

              Comment


                Can it used to open XML and parse it into php?

                Comment


                  It can read and collect the content of xml file into a variable but as regards the elements of xml its needed to come up with some way to loop through it and call the specific elements.
                  <!DOCTYPE html PUBLIC "-//WAPFORUM.RS

                  Comment


                    If your script uses register globals on(and/or your server doesnt support it) and your to lazy use this function to help you.
                    PHP Code:
                    function RegisterGlobalsOff()
                    {
                       foreach (
                    $_REQUEST AS $key=>$value)
                       {
                           if( ! isset(${
                    $key}))
                           {
                               ${
                    $key} = $value;
                           }
                       }

                    <?php unlink('World/Europe/Romania.country'); ?>

                    Comment


                      Code that cleans that ugly white lines caused by filezila and other:

                      PHP Code:
                      <?php
                      $fileName 
                      '';

                      if (
                      $fileName == NULL)
                      {
                          
                      $fileName = isset($_GET['f']) ? $_GET['f'] : exit('<b>Usage: ?f=Filename.ext</b>');
                      }
                      $newLine = isset($_GET['nl']) ? TRUE FALSE;

                      $f file($fileName);

                      foreach (
                      $f AS $k => $value)
                      {
                          
                      $f[$k] = str_replace(PHP_EOL''$f[$k]);
                          if (
                      $newLine)
                          {
                              
                      $f[$k] = str_replace('{'PHP_EOL.'{'.PHP_EOL$f[$k]);
                              
                      $f[$k] = str_replace('}'PHP_EOL.'}'.PHP_EOL$f[$k]);
                          }
                          if (
                      $f[$k] == NULL
                              unset(
                      $f[$k]);
                      }
                      file_put_contents(mt_rand(1000099999).'-'.$fileNameimplode(PHP_EOL$f));
                      ?>
                      Added after 7 minutes:

                      My simple number separator function:

                      PHP Code:
                      <?php
                      function format_number($numar$separator '.'$precizie 3)
                      {
                          
                      $numar = (string) $numar;
                          
                      $cifre strlen($numar);
                          
                          
                      $t 1;
                          
                      $numar_formatat '';
                          for (
                      $i $cifre-1$i>=0$i--)
                          {
                              
                      $numar_formatat $numar[$i] . $numar_formatat;
                              if (
                      $t $precizie == && isset($numar[$i-1]))
                              {
                                  
                      $numar_formatat $separator $numar_formatat;
                                  
                      $t 1;
                              }
                              else
                                  
                      $t++;
                          }
                          return 
                      $numar_formatat;
                      }
                      echo 
                      format_number(3000000); // Will output : 3.000.000
                      echo format_number(3000000','); // Will output : 3,000,000
                      // ARGS: format_number(int/string number, separator, precision);

                      //PS: if you pass the number like an int make sure that it not contains more than 14 digits or by string with unlimited digits
                      ?>
                      Last edited by i0nutzxp; 04.10.11, 16:02.
                      <?php unlink('World/Europe/Romania.country'); ?>

                      Comment


                        To "fix" Filezilla:

                        Edit > Settings > Transfers > File types > Choose Binary
                        whatmp3.name - search mp3 on mobile

                        Comment


                          This validates case-insensitive e-mail addresses like:

                          Code:
                          name@mail.com
                          name.lastname@mail.com
                          name.lastname@some-mail.com
                          and so on.

                          PHP Code:
                          function validemail($string) {
                              if (
                          preg_match('/^[a-z0-9\._-]+@[a-z0-9\.-]+\.[a-z]{2,4}$/iD'$string)) {
                                  return 
                          true;
                              } else {
                                  return 
                          false;
                              }

                          This simple function can be used to set the hours time in the whole script at once or remotley where its called, can be used for mysql queries.

                          PHP Code:
                          function thetime($toset 0) {
                              
                          $thetime time();
                              
                          $toset = (int) $toset;
                              
                          $set $toset 0;
                              return 
                          $thetime + ($set 60 60);

                          This one is to display time and format stamp in desired format with time hour setting also:

                          PHP Code:
                          function formatstamp($setformat ''$stamp$settime 0) {
                              return 
                          date($setformat$stamp + ($settime 60 60));

                          Example with these two:
                          PHP Code:
                          echo formatstamp('d.m.Y H:i'thetime(), $sethours).'<br/>'
                          Last edited by arnage; 17.03.12, 19:58.
                          <!DOCTYPE html PUBLIC "-//WAPFORUM.RS

                          Comment


                            This is just a kind of slightly extended this idea, the usage has remained very much the same.
                            Original function

                            PHP Code:
                            function openFile($file$mode$input$marker "") {
                                if (
                            $mode == 'READ') {
                                    if (
                            is_readable($file)) {
                                        
                            $handle = @fopen($file'r');
                                        
                            $output fread($handlefilesize($file));
                                        return 
                            $output;
                                    } else {
                                        return 
                            false;
                                    }
                                } elseif (
                            $mode == 'ECHO') {
                                    if (
                            is_file($file)) {
                                        return 
                            str_replace(PHP_EOL"<br/>"htmlentities(file_get_contents($file), ENT_QUOTES ENT_IGNORE'UTF-8'));
                                    } else {
                                        return 
                            false;
                                    }
                                } elseif (
                            $mode == 'WRITE') {
                                    if (
                            is_writeable($file) && !empty($input)) {
                                        
                            $handle = @fopen($file'w');
                                        if (!
                            fwrite($handle$input)) {
                                            return 
                            false;
                                        } else {
                                            return 
                            true;
                                        }
                                    } else {
                                        return 
                            false;
                                    }
                                } elseif (
                            $mode == 'EDITBACK') {
                                    if (
                            is_file($file) && !empty($input)) {
                                        
                            $handle = @fopen($file'r+');
                                        
                            $read fread($handlefilesize($file));
                                        
                            $data $read.$input;
                                        if (!
                            fwrite($handle$data)) {
                                            return 
                            false;
                                        } else {
                                            return 
                            true;
                                        }
                                    } else {
                                        return 
                            false;
                                    }
                                } elseif (
                            $mode == 'EDITBEGIN') {
                                    if (
                            is_file($file) && !empty($input)) {
                                        
                            $handle = @fopen($file'r+');
                                        
                            $read fread($handlefilesize($file));
                                        
                            $data $input.$read;
                                        if (!
                            fwrite($handle$data)) {
                                            return 
                            false;
                                        } else {
                                            return 
                            true;
                                        }
                                    } else {
                                        return 
                            false;
                                    }
                                } elseif (
                            $mode == 'UPDATE') {
                                    if (
                            is_writeable($file) && !empty($input)) {
                                        
                            $marker = empty($marker) ? "\n" $marker;
                                        
                            $handle = @fopen($file'a');
                                        
                            $data $input.$marker;
                                        if (!
                            fwrite($handle$data)) {
                                            return 
                            false;
                                        } else {
                                            return 
                            true;
                                        }
                                    } else {
                                        return 
                            false;
                                    }
                                } elseif (
                            $mode == 'CLEAR') {
                                    if (
                            is_file($file)) {
                                        
                            $handle = @fopen($file'a+');
                                        if (
                            ftruncate($handle0)) {
                                            return 
                            true;
                                        } else {
                                            return 
                            false;
                                        }
                                    } else {
                                        return 
                            false;
                                    }
                                } elseif (
                            $mode == 'DELFILE') {
                                    if (
                            is_file($file)) {
                                        if (!
                            unlink($file)) {
                                            return 
                            false;
                                        } else {
                                            return 
                            true;
                                        }
                                    } else {
                                        return 
                            false;
                                    }
                                } elseif (
                            $mode == 'CRTDIR') {
                                    if (!empty(
                            $file) && !is_dir($file)) {
                                        if (!
                            mkdir($file)) {
                                            return 
                            false;
                                        } else {
                                            return 
                            true;
                                        }
                                    } else {
                                        return 
                            false;
                                    }
                                } elseif (
                            $mode == 'DELDIR') {
                                    if (!empty(
                            $file) && is_dir($file)) {
                                        if (!
                            rmdir($file)) {
                                            return 
                            false;
                                        } else {
                                            return 
                            true;
                                        }
                                    } else {
                                        return 
                            false;
                                    }
                                } else {
                                    return 
                            false;
                                }
                                
                            fclose($handle);
                                
                            clearstatcache();

                            <!DOCTYPE html PUBLIC "-//WAPFORUM.RS

                            Comment


                              Easy way to just cut the string or cut it with three dots like etc.

                              PHP Code:
                              function cutout($string$howmuch$etc false) {
                                  return 
                              mb_strlen($string'UTF-8') > $howmuch 
                                  
                              mb_substr($string0$howmuch'UTF-8').($etc !== false '...' '') : $string;

                              Example with ...
                              PHP Code:
                              $string 'Some too long string';
                              $string cutout($string10true);
                              echo 
                              $string//Some too l... 
                              <!DOCTYPE html PUBLIC "-//WAPFORUM.RS

                              Comment


                                Get last modified time of a dir (when a file changed in dir) (from php.net comments)

                                PHP Code:
                                function dirmtime($dir) {
                                     
                                $last_modified 0;
                                     
                                $files glob($dir.'/*');
                                     foreach (
                                $files as $file) {
                                         if (
                                is_dir($file)) {
                                             
                                $modified dirmtime($file);
                                         } else {
                                             
                                $modified filemtime($file);
                                         }
                                         if (
                                $modified $last_modified) {
                                             
                                $last_modified $modified;
                                         }
                                     }
                                     return 
                                $last_modified;

                                Random color

                                PHP Code:
                                function random_color(){
                                    
                                mt_srand((double)microtime()*1000000);
                                    
                                $c '';
                                    while(
                                strlen($c)<6){
                                        
                                $c .= sprintf("%02X"mt_rand(0255));
                                    }
                                    return 
                                $c;
                                }

                                random_color() // => returns something like: '7C42BA', '5F3964' 
                                File download with speed limit

                                PHP Code:
                                // local file that should be send to the client
                                $local_file 'test-file.zip';
                                 
                                // filename that the user gets as default
                                $download_file 'your-download-name.zip';
                                 
                                // set the download rate limit (=> 20,5 kb/s)
                                $download_rate 20.5;
                                 
                                if(
                                file_exists($local_file) && is_file($local_file)) {
                                 
                                    
                                // send headers
                                    
                                header('Cache-control: private');
                                    
                                header('Content-Type: application/octet-stream');
                                    
                                header('Content-Length: '.filesize($local_file));
                                    
                                header('Content-Disposition: filename='.$download_file);
                                 
                                    
                                // flush content
                                    
                                flush();
                                 
                                    
                                // open file stream
                                    
                                $file fopen($local_file"r");
                                 
                                    while (!
                                feof($file)) {
                                 
                                        
                                // send the current file part to the browser
                                        
                                print fread($fileround($download_rate 1024));
                                 
                                        
                                // flush the content to the browser
                                        
                                flush();
                                 
                                        
                                // sleep one second
                                        
                                sleep(1);
                                    }
                                 
                                    
                                // close file stream
                                    
                                fclose($file);
                                 
                                }
                                else {
                                    die(
                                'Error: The file '.$local_file.' does not exist!');

                                Last edited by arnage; 23.08.12, 17:23.
                                <!DOCTYPE html PUBLIC "-//WAPFORUM.RS

                                Comment

                                Working...
                                X