ossn.lib.input.php
Go to the documentation of this file.
1 <?php
2 /**
3  * Open Source Social Network
4  *
5  * @packageOpen Source Social Network
6  * @author Open Social Website Core Team <[email protected]>
7  * @copyright 2014 iNFORMATIKON TECHNOLOGIES
8  * @license General Public Licence http://www.opensource-socialnetwork.org/licence
9  * @link http://www.opensource-socialnetwork.org/licence
10  */
11 /**
12  * Escape input string
13  *
14  * @param string $str A valid string in which you want to restore lines.
15  * @param boolean $newlines If you also want to escape new lines , default settings will replace it
16  *
17  * @return string
18  */
19 function ossn_input_escape($str, $newlines = true) {
20  $replacements = array(
21  "\x00" => '\x00',
22  "\n" => '\n',
23  "'" => "\'",
24  '"' => '\"',
25  "\x1a" => '\x1a'
26  );
27  if($newlines === true) {
28  $newline = array(
29  "\r" => '\r',
30  "\\" => '\\\\'
31  );
32  $replacements = array_merge($replacements, $newline);
33  }
34  if(!empty($str)) {
35  return strtr($str, $replacements);
36  }
37  return false;
38 }
39 /**
40  * Get input from user; using secure method;
41  *
42  * @param string $input Name of input;
43  * @params integer $noencode If you don't want to encode to html entities then add 1 as second arg in function.
44  *
45  * @last edit: $arsalanshah
46  * @reason: fix docs;
47  * @return false|string
48  */
49 function input($input, $noencode = '', $default = false) {
50  $str = false;
51  if(!isset($_REQUEST[$input]) && $default) {
52  return $default;
53  }
54  if(isset($_REQUEST[$input]) && empty($noencode)) {
55  $data = htmlentities($_REQUEST[$input], ENT_QUOTES, 'UTF-8');
56  $str = $data;
57  } elseif($noencode == 1) {
58  $str = ossn_input_escape($data);
59  }
60  if($str) {
61  return ossn_input_escape($str);
62  }
63  return false;
64 }
65 /**
66  * Ossn Restore New Lines
67  * Restore \n\r from the string to new line
68  *
69  * @param string $string A valid string in which you want to restore lines.
70  * @param boolean $br If you want newlines to replaced by html <br /> tag.
71  *
72  * @return string
73  */
74 function ossn_restore_new_lines($string, $br = false) {
75  if(empty($string)) {
76  return false;
77  }
78  $replacements = array(
79  "\n" => '\n',
80  "\r" => '\r'
81  );
82  $replacements = array_flip($replacements);
83  $result = strtr($string, $replacements);
84  if($br === true) {
85  $result = nl2br($result);
86  }
87  return $result;
88 }
89 /**
90  * Set a value for input
91  *
92  * @param string $name Name of input
93  * @param string $value Value of input
94  *
95  * @return void
96  */
97 function ossn_set_input($name, $value) {
98  if(isset($name) && isset($value)) {
99  $_REQUEST[$name] = $value;
100  }
101 }