Fix checkRpItemsPosition
[ryzomcore.git] / web / public_php / admin / functions_common.php
bloba06183109164daa22056b14b9026d2eca73f7d2d
1 <?php
3 /*
4 * THIS FILE SHOULD ONLY INCLUDE SMALL USEFUL FUNCTIONS
5 */
7 if (!function_exists('ereg')) {
8 /** removed from php 7.0.0 */
9 function ereg($pattern, $line, &$match = array()) {
10 return preg_match('/'.$pattern.'/', $line, $match);
15 * pushes some data in the debug variable
17 function nt_common_add_debug($data)
19 global $nel_debug;
21 if (is_array($data)) $nel_debug[] = print_r($data,true);
22 else $nel_debug[] = $data;
26 * redirects to a defined url
28 function nt_common_redirect($url)
30 $url = trim($url);
31 if (substr($url,0,1) == '/') $url = substr($url,1);
33 $redirect = NELTOOL_SITEBASE . $url;
35 header("Location: ". $redirect);
36 exit();
40 * adds a user action log
42 function nt_common_add_log($userinfo, $log_action, $log_desc)
44 global $db, $NELTOOL;
46 $log_action = trim($log_action);
47 $log_desc = trim($log_desc);
49 if (!is_array($userinfo) && (!isset($userinfo['user_id']))) return false;
50 if ($log_action == '') return false;
51 if ($log_desc == '') return false;
53 $sql = "INSERT INTO ". NELDB_LOG_TABLE ." ('log_user_id','log_action','log_description','log_date','log_ip') VALUES ('". $userinfo['user_id'] ."','". addslashes($log_action) ."','". addslashes($log_desc) ."','". time() ."','". $NELTOOL['SERVER_VARS']['REMOTE_ADDR'] ."')";
54 $db->sql_query($sql);
56 return true;
60 if (!function_exists('array_combine'))
62 function array_combine( $keys, $vals )
64 $keys = array_values( (array) $keys );
65 $vals = array_values( (array) $vals );
66 $n = max( count( $keys ), count( $vals ) );
67 $r = array();
68 for( $i=0; $i<$n; $i++ )
70 $r[ $keys[ $i ] ] = $vals[ $i ];
72 return $r;
76 if (!function_exists('array_chunk')) {
78 function array_chunk($input,$size,$preserve_keys=false)
80 @reset($input);
82 $i = $j = 0;
84 while (@list($key,$value) = @each($input))
86 if( !( isset( $chunks[$i] ) ) )
88 $chunks[$i] = array();
91 if( count( $chunks[$i] ) < $size )
93 if( $preserve_keys )
95 $chunks[$i][$key] = $value;
96 $j++;
98 else
100 $chunks[$i][] = $value;
103 else
105 $i++;
107 if( $preserve_keys )
109 $chunks[$i][$key] = $value;
110 $j++;
112 else
114 $j = 0;
115 $chunks[$i][$j] = $value;
120 return $chunks;
124 function array_natsort_list($array)
126 // for all arguments without the first starting at end of list
127 for ($i=func_num_args();$i>1;$i--)
129 // get column to sort by
130 $sort_by = func_get_arg($i-1);
132 // clear arrays
133 $new_array = array();
134 $temporary_array = array();
136 // walk through original array
137 foreach($array as $original_key => $original_value)
139 // and save only values
140 $temporary_array[] = $original_value[$sort_by];
143 // sort array on values
144 natsort($temporary_array);
146 // delete double values
147 $temporary_array = array_unique($temporary_array);
149 // walk through temporary array
150 foreach($temporary_array as $temporary_value)
152 // walk through original array
153 foreach($array as $original_key => $original_value)
155 // and search for entries having the right value
156 if($temporary_value == $original_value[$sort_by])
158 // save in new array
159 $new_array[$original_key] = $original_value;
164 // update original array
165 $array = $new_array;
168 return $array;
171 function nt_common_assert( $script, $line, $message )
173 nt_common_add_debug('ASSERT ('. $script .':'. $line .') : '. ereg_replace( '^.*//\*', '', $message ));
174 //exit;
177 function nt_log($data)
179 global $db;
180 global $nel_user;
182 $log_user_name = $nel_user['user_name'];
183 $log_date = time();
184 $log_data = addslashes(trim($data));
186 $sql = "INSERT INTO ". NELDB_LOG_TABLE ." (`logs_user_name`,`logs_date`,`logs_data`) VALUES ('". $log_user_name ."','". $log_date ."','". $log_data ."')";
187 $db->sql_query($sql);
190 function nt_sleep($delay)
192 if ($delay > 0)
194 sleep($delay);
198 function nt_email($subject,$message,$emails=null)
200 if ($message !== '' && $subject !== '')
202 if ($emails === null)
204 $emails = 'vl@ryzom.com';
206 elseif (is_array($emails))
208 $emails = implode(', ', $emails);
211 $headers = "From: vl@ryzom.com\r\nReply-To: vl@ryzom.com\r\nX-Mailer: Shard Admin Tool\r\n";
212 mail($emails, $subject, $message, $headers);