Location: PHPKode > projects > rapidshareBot > rapidsharebot/libraries/functions.php
<?php
//Fetch external urls GET request
function fetch_url($url, $ref) {
    $user_agent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.1) Gecko/2008070208 Firefox/3.0.1';
	//try to use culr first. to send reffrer and browser user agent properly.
	if(function_exists('curl_init')) {
			$ch = curl_init($url);
			curl_setopt($ch, CURLOPT_URL, $url);
			curl_setopt($ch, CURLOPT_REFERER, $ref);
	        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
	        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120);
	        curl_setopt($ch, CURLOPT_TIMEOUT, 120);
	        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);  //use curl_redir_exec when open_basedir is set
	        curl_setopt($ch, CURLOPT_MAXREDIRS,10);
	        curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
	        curl_setopt($ch, CURLOPT_HTTPGET, 0);
	        $file = curl_exec($ch);
			curl_close($ch);
	}
	else {
        if(ini_get('allow_url_fopen') == 1 || strtolower(ini_get('allow_url_fopen')) == 'on') {
		$file = @file_get_contents($url);
		if($file == false) {
			$handle = @fopen($url, 'r');
			$file = @fread($handle, 4096);
			@fclose($handle);
			}
	    }	
	}
	if($file != false && $file != '') return $file;
}

//make absolute url
function makeabsolute($url,$link) {
		$p = parse_url($url);
		if (strpos( $link,"http://")===0 ) return $link;
		if($p['scheme']."://".$p['host']==$url && $link[0]!="/" && $link!=$url) return $p['scheme']."://".$p['host']."/".$link;
		if (strpos( $link, "/")===0) return "http://".$p['host'].$link;
		return str_replace(substr(strrchr($url, "/"), 1),"",$url).$link;
	}


function make_clickable_url($text = '', $title = '') {
	$text = trim(delimit_urls($text, ' '));
	$hyperlinksArray = array();
	preg_match_all('(((f|ht){1}(tp://|tps://))' . '[-a-zA-Z0-9@:%_+.~#?&//=]+)', $text, $hyperlinksArray);
	for($i = 0; $i < count($hyperlinksArray[0]); $i++) {
		$link = $hyperlinksArray[0][$i];
		$text = str_replace($hyperlinksArray[0][$i],'<a href="' . $link . '" title="Free Download ' . $title . ' part ' . ($i + 1) . '" target="_blank" rel="nofollow" style="color:red;">Free Download ' . $title . ' part ' . ($i + 1) . '</a>', $text);
		} 
	return $text;
	}

function delimit_urls($text, $delimiter = ' ') {
    $url_pattern = '/# Match http & ftp URL that is not already linkified.
      # Alternative 1: URL delimited by (parentheses).
      (\()                     # $1  "(" start delimiter.
      ((?:ht|f)tps?:\/\/[a-z0-9\-._~!$&\'()*+,;=:\/?#[\]@%]+)  # $2: URL.
      (\))                     # $3: ")" end delimiter.
    | # Alternative 2: URL delimited by [square brackets].
      (\[)                     # $4: "[" start delimiter.
      ((?:ht|f)tps?:\/\/[a-z0-9\-._~!$&\'()*+,;=:\/?#[\]@%]+)  # $5: URL.
      (\])                     # $6: "]" end delimiter.
    | # Alternative 3: URL delimited by {curly braces}.
      (\{)                     # $7: "{" start delimiter.
      ((?:ht|f)tps?:\/\/[a-z0-9\-._~!$&\'()*+,;=:\/?#[\]@%]+)  # $8: URL.
      (\})                     # $9: "}" end delimiter.
    | # Alternative 4: URL delimited by <angle brackets>.
      (<|&(?:lt|\#60|\#x3c);)  # $10: "<" start delimiter (or HTML entity).
      ((?:ht|f)tps?:\/\/[a-z0-9\-._~!$&\'()*+,;=:\/?#[\]@%]+)  # $11: URL.
      (>|&(?:gt|\#62|\#x3e);)  # $12: ">" end delimiter (or HTML entity).
    | # Alternative 5: URL not delimited by (), [], {} or <>.
      (                        # $13: Prefix proving URL not already linked.
        (?: ^                  # Can be a beginning of line or string, or
        | [^=\s\'"\]]          # a non-"=", non-quote, non-"]", followed by
        ) \s*[\'"]?            # optional whitespace and optional quote;
      | [^=\s]\s+              # or a non-equals sign followed by whitespace.
      )                        # End $13. Non-prelinkified-proof prefix.
      ( \b                     # $14: Other non-delimited URL.
        (?:ht|f)tps?:\/\/      # Required http, https, ftp or ftps prefix.
        [a-z0-9\-._~!$\'()*+,;=:\/?#[\]@%]+ # All URI chars except "&".
        (?:                    # Either on a "&" or at the end of URI.
          (?!                  # Allow a "&" char only if not start of an...
            &(?:gt|\#0*62|\#x0*3e);                  # HTML ">" entity, or
          | &(?:amp|apos|quot|\#0*3[49]|\#x0*2[27]); # a [&\'"] entity if
            [.!&\',:?;]?        # followed by optional punctuation then
            (?:[^a-z0-9\-._~!$&\'()*+,;=:\/?#[\]@%]|$) # a non-URI char or EOS.
          ) &                  # If neg-assertion true, match "&" (special).
          [a-z0-9\-._~!$\'()*+,;=:\/?#[\]@%]* # More non-& URI chars.
        )*                     # Unroll-the-loop (special normal*)*.
        [a-z0-9\-_~$()*+=\/#[\]@%]  # Last char can\'t be [.!&\',;:?]
      )                        # End $14. Other non-delimited URL.
    /imx';
    $url_replace =	'$1$4$7$10$13' . $delimiter . '$2$5$8$11$14' .
			$delimiter . '$3$6$9$12';
    return	trim(preg_replace($url_pattern, $url_replace, $text));
}

// PHP4 workaround
if (!function_exists('file_put_contents')) {
  function file_put_contents($filename, $data, $respect_lock = true) {
    // Open the file for writing
    $fh = @fopen($filename, 'w');
    if ($fh === false) {
      return false;
    }

    // Check to see if we want to make sure the file is locked before we write to it
    if ($respect_lock === true && !flock($fh, LOCK_EX)) {
      fclose($fh);
      return false;
    }

    // Convert the data to an acceptable string format
    if (is_array($data)) {
      $data = implode('', $data);
    } else {
       $data = (string) $data;
    }

    // Write the data to the file and close it
    $bytes = fwrite($fh, $data);

    // This will implicitly unlock the file if it's locked
    fclose($fh);

    return $bytes;
  }
}

////////////////////

function logout($id_type) {
  unset($_SESSION[$id_type]);
  $url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME'];
  header("Location: $url");
}

//////////////////////

function authorize($id_type) {
  if (!isset($_SESSION[$id_type])) {
    if (!isset($_POST['login'])) {
      include('login.php');
      exit();
    } else {
      include('config/conf.inc.php');
      if (($conf['adm_password'] !== $_POST['password']) or ($conf['adm_user'] !== $_POST['login'])) {
        $loginerror = "Wrong login or password!";
        include('login.php');
        exit();
      } else {
        $_SESSION['loginidadmin2121'] = $conf['adm_user'];
      }
    }
  }
}

//////////////////////

function update_config($post, $config) {
  $config_str = "<?php\r\n\$".$config['type']." = array();\r\n";

  foreach($config as $key=>$val) {
    $new_val = false;

    (isset($post[$key]))
      ? $new_val = $post[$key]
      : $new_val = $config[$key];

    $config_str .= "\$".$config['type']."['{$key}'] = '{$new_val}'; \r\n";
  }

  $config_str .= "?>";
  $fp = @fopen('config/' . $config['type'].".inc.php", 'w');
  if ($fp) {
    fwrite($fp, $config_str, strlen($config_str));
    return true;
  } else {
    return false;
  }
}

//////////////////////

function getFeed($feed_url) {
  $content = file_get_contents($feed_url);
  if ($content) {
    $x = new SimpleXmlElement($content);
    echo "<ul id=\"feedlist\">";
    echo "<li id='head'><a href='http://blog.kreci.net' target='_blank'>RSS FEED: hide@address.com</a></li>";
    foreach($x->channel->item as $entry) {
      echo "<li><a href='$entry->link' title='$entry->title' target='_blank'>$entry->title</a></li>";
    }
    echo "</ul>";
  }
}

////////////////////

function Random_Password($length) {
  $possible_characters = "abcdefghijklmnopqrstuvwxyz1234567890";
  $string = "";
  while(strlen($string)<$length) {
    $string .= substr($possible_characters, rand()%(strlen($possible_characters)),1);
  }
  return($string);
}

/////////////////////

function ok($file) {
  echo "<span style=\"font-weight: bold;\">$file:</span> <span class=\"ok\">OK!</span><br />";
}

/////////////////////

function warn($file) {
  echo "<span style=\"font-weight: bold;\">$file:</span> <span class=\"warn\">ERROR! Please CHMOD!</span><br />";
}

/////////////////////

function fileperm($array) {
  foreach ($array as $file) {
    if (is_writable($file)) {
      ok($file);
    } else {
      warn($file);
      $error = 1;
    }
  }
  if(isset($error)){return false;}else{return true;}
}

?>

Return current item: rapidshareBot