ACC SHELL

Path : /srv/www/vhosts/petrikov/test/epf/
File Upload :
Current File : /srv/www/vhosts/petrikov/test/epf/helpers.php

<?php
  /**
   *  @file helpers.php
   *  Miscellaneous helper functions.
   */

  /*
  Easy PHP Framework

  Copyright (c) 2005 Michal Molhanec

  This software is provided 'as-is', without any express or implied
  warranty. In no event will the authors be held liable for any damages
  arising from the use of this software.

  Permission is granted to anyone to use this software for any purpose,
  including commercial applications, and to alter it and redistribute
  it freely, subject to the following restrictions:

      1. The origin of this software must not be misrepresented;
         you must not claim that you wrote the original software.
         If you use this software in a product, an acknowledgment
         in the product documentation would be appreciated but
         is not required.

      2. Altered source versions must be plainly marked as such,
         and must not be misrepresented as being the original software.

      3. This notice may not be removed or altered from any
         source distribution.
  */
  
  /**
   *  Logs message into the log.txt file. Make sure that script has
   *  write access to the file.
   */
  function lg($msg) {
    $f = fopen('log.txt', 'a');
    fwrite($f, $msg . "\n");
    fclose($f);
  }

  /**
   *  Shortcut for
   *  <a href='http://www.php.net/manual/en/function.htmlspecialchars.php'>htmlspecialchars($str, ENT_QUOTES);</a>
   */
  function html($str) {
    return htmlspecialchars($str, ENT_QUOTES);
  }

  /**
   *  Shortcut for <a href='http://www.php.net/manual/en/function.urlencode.php'>urlencode($str);</a>
   */
  function url($str) {
    return urlencode($str);
  }

  /**
   * Helper class used by email() function.
   */
  class AtElementCreator extends ElementCreator {
    function __construct() {
      parent::__construct('img');
    }
    function create_element($doc) {
      $this->attrs['src'] = 'at.gif';
      $this->attrs['alt'] = 'at';
      return parent::create_element($doc);
    }
  }

  /**
   * Helper class used by email() function.
   */
  class DotElementCreator extends ElementCreator {
    function __construct() {
      parent::__construct('img');
    }
    function create_element($doc) {
      $this->attrs['src'] = 'dot.gif';
      $this->attrs['alt'] = 'dot';
      return parent::create_element($doc);
    }
  }

  /**
   *  Makes the email robots unreadable. Expects that there exists
   *  at.gif and dot.gif files.
   *  @param[in] $doc <a href='http://www.php.net/manual/en/ref.dom.php'>DOMDocument</a> instance.
   *  @param[in] $email Email address.
   *  @return Array of DOMNodes, representing mangled email.
   */
  function email($doc, $email) {
    // make some space around images
    $email = str_replace('@', ' @ ', str_replace('.', ' . ', $email));
    return xml_convert_str2elem($doc,
      xml_convert_str2elem($doc, $email, '@', new AtElementCreator),
      '.', new DotElementCreator);
  }

  /**
   *  This function is identical to
   *  <a href='http://www.php.net/manual/en/function.nl2br.php'>nl2br</a>
   *  except that it returns an array of DOMNode instances instead of plain HTML
   *  code (it also removes \\n unlike original nl2br).
   *  You can use it like this -- PHP side:
   *  @code
   *    $doc = new DOMDocument();
   *    ...
   *    $some_elem = $doc->createElement('someElem');
   *    ...
   *    $coverted = nl2brXML($doc, $some_string);
   *    foreach($converted as $node):
   *      $some_elem->appendChild($node);
   *    endforeach;
   *  @endcode
   *  XSLT side:
   *  @code
   *    <xsl:copy-of select='someElem'/>
   *  @endcode
   *  @param[in] $doc <a href='http://www.php.net/manual/en/ref.dom.php'>DOMDocument</a> instance.
   *  @param[in] $text String which is going to be converted.
   *  @return Array of DOMNodes, there is always at least one DOMText instance.
   */
  function nl2brXML($doc, $text) {
    return xml_convert_str2elem($doc, $text, "\n", 'br');
  }

  /**
   * Helper class used by link2aXML() function.
   * You can use it whenever you need ElementCreator
   * which creates links that mirrors content in the href attribute.
   */
  class LinkElementCreator extends ElementCreator {
    function __construct() {
      parent::__construct('a');
    }
    function create_element($doc, $content) {
      $this->attrs['href'] = $content;
      return parent::create_element($doc, $content);
    }
  }

  /**
   *  It recognises URLs which begins with http://, https:// or ftp://
   *  and converts them into <a href=> elements.
   *  @param[in] $doc <a href='http://www.php.net/manual/en/ref.dom.php'>DOMDocument</a> instance.
   *  @param[in] $text String which is going to be converted.
   *  @return Array of DOMNodes, there is always at least one DOMText instance.
   */
  function link2aXML($doc, $text) {
    return xml_convert_regex2elem($doc, $text, '#((?:http|https|ftp)://\S+)#i', new LinkElementCreator);
  }

  /**
   *  Combinates nl2brXML() and link2aXML().
   */
  function nl2br_and_link2a_XML($doc, $text) {
    return link2aXML($doc, nl2brXML($doc, $text));
  }

  /**
   *  Tries to make absolute URL from relative one.
   *  @param[in] $relative_url If empty, 'index.php' is used instead.
   *  @return Absolute URL.
   */
  function make_absolute_url($relative_url) {
    $dir = dirname($_SERVER['PHP_SELF']);
    if ($dir == '\\'):
      $dir = '';
    endif;
    if ($relative_url == '') $relative_url = 'index.php';
    if (strlen($dir) == 0 or $dir[strlen($dir) - 1] != '/'):
      $dir .= '/';
    endif;
    return $_SERVER['HTTP_HOST'] . $dir . $relative_url;
  }

  /**
   *  Redirects browser using 302 HTTP response code and
   *  Location header to some new page. Stops execution of
   *  the PHP script.
   */
  function go_to($relative_url) {
    header("Location: http://" . make_absolute_url($relative_url));
    exit;
  }

  /**
   *  Java <a href='http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html#startsWith(java.lang.String)'
   *  >startsWith</a> equivalent.
   *  @param[in] $what Searched prefix.
   *  @param[in] $where String which possibly starts with the prefix.
   *  @return TRUE if $where starts with $what, FALSE otherwise.
   */
  function starts_with($what, $where) {
    if (strpos($where, $what) === 0):
      return TRUE;
    endif;
    return FALSE;
  }

  /**
   *  Generates simple random password. It will consists only of letters
   *  a-z (to not be error-prone). Use this if user forgets its password.
   *  @param[out] $passw Generated password.
   *  @param[out] $md5passw MD5 hash of the generated password.
   *  @param[in]  $len Password length. Default is 5.
   */
  function generate_password(&$passw, &$md5passw, $len = 5) {
    for ($i = 0; $i < $len; $i++):
      $passw .= chr(rand(97, 122));
    endfor;
    $md5passw = md5($passw);
  }
  
  /**
   *  Tests if given URL exists.
   *  @return TRUE if exists, FALSE otherwise.
   */
  function urlfile_exists($url) {
    $handle = @fopen($url, 'r');
    if (!$handle):
      return FALSE;
    endif;
    fclose($handle);
    return TRUE;
  }

ACC SHELL 2018