[903] | 1 | <?php |
---|
| 2 | define('PHPGW_INCLUDE_ROOT','../'); |
---|
| 3 | define('PHPGW_API_INC','../phpgwapi/inc'); |
---|
| 4 | include_once(PHPGW_API_INC.'/class.db.inc.php'); |
---|
| 5 | include_once('class.dynamic_contacts.inc.php'); |
---|
| 6 | |
---|
| 7 | class db_functions |
---|
| 8 | { |
---|
| 9 | |
---|
| 10 | var $db; |
---|
| 11 | var $user_id; |
---|
| 12 | |
---|
| 13 | function db_functions(){ |
---|
| 14 | $this->db = new db(); |
---|
| 15 | $this->db->Halt_On_Error = 'no'; |
---|
| 16 | $this->db->connect( |
---|
| 17 | $_SESSION['phpgw_info']['expressomail']['server']['db_name'], |
---|
| 18 | $_SESSION['phpgw_info']['expressomail']['server']['db_host'], |
---|
| 19 | $_SESSION['phpgw_info']['expressomail']['server']['db_port'], |
---|
| 20 | $_SESSION['phpgw_info']['expressomail']['server']['db_user'], |
---|
| 21 | $_SESSION['phpgw_info']['expressomail']['server']['db_pass'], |
---|
| 22 | $_SESSION['phpgw_info']['expressomail']['server']['db_type'] |
---|
| 23 | ); |
---|
| 24 | $this -> user_id = $_SESSION['phpgw_info']['expressomail']['user']['account_id']; |
---|
| 25 | } |
---|
| 26 | |
---|
| 27 | // BEGIN of functions. |
---|
| 28 | function get_cc_contacts() |
---|
| 29 | { |
---|
| 30 | $result = array(); |
---|
| 31 | $stringDropDownContacts = ''; |
---|
| 32 | // Pesquisa no CC os nomes e email dos contatos. |
---|
| 33 | $query = 'select A.names_ordered, C.connection_value from phpgw_cc_contact A,'. |
---|
| 34 | 'phpgw_cc_contact_conns B, phpgw_cc_connections C where '. |
---|
| 35 | 'A.id_contact = B.id_contact and B.id_connection = C.id_connection '. |
---|
| 36 | 'and B.id_typeof_contact_connection = 1 and '. |
---|
| 37 | 'A.id_owner ='.$this -> user_id.' order by A.names_ordered'; |
---|
| 38 | |
---|
| 39 | if (!$this->db->query($query)) |
---|
| 40 | return null; |
---|
| 41 | |
---|
| 42 | |
---|
| 43 | while($this->db->next_record()) |
---|
| 44 | $result[] = $this->db->row(); |
---|
| 45 | |
---|
| 46 | if (count($result) != 0) |
---|
| 47 | { |
---|
| 48 | // Monta string |
---|
| 49 | foreach($result as $contact) |
---|
| 50 | $stringDropDownContacts = $stringDropDownContacts . $contact['names_ordered']. ';' . $contact['connection_value'] . ','; |
---|
| 51 | //Retira ultima virgula. |
---|
| 52 | $stringDropDownContacts = substr($stringDropDownContacts,0,(strlen($stringDropDownContacts) - 1)); |
---|
| 53 | } |
---|
| 54 | else |
---|
| 55 | return null; |
---|
| 56 | |
---|
| 57 | return $stringDropDownContacts; |
---|
| 58 | } |
---|
| 59 | |
---|
| 60 | function get_cc_groups() |
---|
| 61 | { |
---|
| 62 | // Pesquisa no CC os Grupos Pessoais. |
---|
| 63 | $stringDropDownContacts = ''; |
---|
| 64 | $result = array(); |
---|
| 65 | $query = 'select title, short_name from phpgw_cc_groups where owner ='.$this -> user_id.' order by title'; |
---|
| 66 | // Executa a query |
---|
| 67 | if (!$this->db->query($query)) |
---|
| 68 | return null; |
---|
| 69 | // Retorna cada resultado |
---|
| 70 | while($this->db->next_record()) |
---|
| 71 | $result[] = $this->db->row(); |
---|
| 72 | // Se houver grupos .... |
---|
| 73 | if (count($result) != 0) |
---|
| 74 | { |
---|
| 75 | foreach($result as $group) |
---|
| 76 | $stringDropDownContacts .= $group['title']. ';' . $group['short_name'] . ','; |
---|
| 77 | //Retira ultima virgula. |
---|
| 78 | $stringDropDownContacts = substr($stringDropDownContacts,0,(strlen($stringDropDownContacts) - 1)); |
---|
| 79 | } |
---|
| 80 | else |
---|
| 81 | return null; |
---|
| 82 | |
---|
| 83 | return $stringDropDownContacts; |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | function getContactsByGroupAlias($alias) |
---|
| 87 | { |
---|
| 88 | |
---|
| 89 | $query = "select C.id_connection, A.names_ordered, C.connection_value from phpgw_cc_contact A, ". |
---|
| 90 | "phpgw_cc_contact_conns B, phpgw_cc_connections C,phpgw_cc_contact_grps D,phpgw_cc_groups E where ". |
---|
| 91 | "A.id_contact = B.id_contact and B.id_connection = C.id_connection ". |
---|
| 92 | "and B.id_typeof_contact_connection = 1 and ". |
---|
| 93 | "A.id_owner =".$this -> user_id." and ". |
---|
| 94 | "D.id_group = E.id_group and ". |
---|
| 95 | "D.id_connection = C.id_connection and E.short_name = '".$alias. |
---|
| 96 | "' order by A.names_ordered"; |
---|
| 97 | |
---|
| 98 | if (!$this->db->query($query)) |
---|
| 99 | { |
---|
| 100 | exit ('Query failed! File: '.__FILE__.' on line'.__LINE__); |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | $return = false; |
---|
| 104 | |
---|
| 105 | while($this->db->next_record()) |
---|
| 106 | { |
---|
| 107 | $return[] = $this->db->row(); |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | return $return; |
---|
| 111 | } |
---|
| 112 | |
---|
| 113 | function getAddrs($array_addrs) { |
---|
| 114 | $array_addrs_final = array(); |
---|
| 115 | |
---|
| 116 | for($i = 0; $i < count($array_addrs); $i++){ |
---|
| 117 | $j = count($array_addrs_final); |
---|
| 118 | |
---|
| 119 | if(!strchr($array_addrs[$i],'@') |
---|
| 120 | && strchr($array_addrs[$i],'<') |
---|
| 121 | && strchr($array_addrs[$i],'>')) { |
---|
| 122 | |
---|
| 123 | $alias = substr($array_addrs[$i], strpos($array_addrs[$i],'<'), strpos($array_addrs[$i],'>')); |
---|
| 124 | $alias = str_replace('<','', str_replace('>','',$alias)); |
---|
| 125 | $arrayContacts = $this -> getContactsByGroupAlias($alias); |
---|
| 126 | |
---|
| 127 | if($arrayContacts) { |
---|
| 128 | foreach($arrayContacts as $index => $contact){ |
---|
| 129 | if($contact['names_ordered']) { |
---|
| 130 | $array_addrs_final[$j] = '"'.$contact['names_ordered'].'" <'.$contact['connection_value'].'>'; |
---|
| 131 | } |
---|
| 132 | else |
---|
| 133 | $array_addrs_final[$j] = $contact['connection_value']; |
---|
| 134 | |
---|
| 135 | $j++; |
---|
| 136 | } |
---|
| 137 | } |
---|
| 138 | } |
---|
| 139 | else |
---|
| 140 | $array_addrs_final[$j++] = $array_addrs[$i]; |
---|
| 141 | } |
---|
| 142 | return $array_addrs_final; |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | function get_dropdown_contacts() |
---|
| 146 | { |
---|
| 147 | $dynamic_contact = new dynamic_contacts(); |
---|
| 148 | |
---|
| 149 | $contacts = $this -> get_cc_contacts(); |
---|
| 150 | $groups = $this -> get_cc_groups(); |
---|
| 151 | $dynamic = $dynamic_contact->dynamic_contact_toString(); |
---|
| 152 | |
---|
| 153 | if(($contacts) && ($groups)) |
---|
| 154 | $stringDropDownContacts = $contacts . ',' . $groups; |
---|
| 155 | elseif ((!$contacts) && (!$groups)) |
---|
| 156 | $stringDropDownContacts = ''; |
---|
| 157 | elseif (($contacts) && (!$groups)) |
---|
| 158 | $stringDropDownContacts = $contacts; |
---|
| 159 | elseif ((!$contacts) && ($groups)) |
---|
| 160 | $stringDropDownContacts = $groups; |
---|
| 161 | if (($dynamic) && (($groups) || ($contacts))) |
---|
| 162 | $stringDropDownContacts .= ',' . $dynamic; |
---|
| 163 | else |
---|
| 164 | if ($dynamic) |
---|
| 165 | $stringDropDownContacts = $dynamic; |
---|
| 166 | |
---|
| 167 | return $stringDropDownContacts; |
---|
| 168 | } |
---|
| 169 | /* |
---|
| 170 | function update_preferences($params){ |
---|
| 171 | |
---|
| 172 | $aux = explode("##",$params['prefe_string']); |
---|
| 173 | $new_prefe = array(); |
---|
| 174 | |
---|
| 175 | foreach($aux as $key=>$tmp){ |
---|
| 176 | if($key == 0){ |
---|
| 177 | $new_prefe['max_email_per_page'] = $tmp; |
---|
| 178 | } |
---|
| 179 | if($key == 1){ |
---|
| 180 | $new_prefe['save_deleted_msg'] = $tmp; |
---|
| 181 | } |
---|
| 182 | if($key == 2){ |
---|
| 183 | $new_prefe['delete_trash_messages_after_n_days'] = $tmp; |
---|
| 184 | } |
---|
| 185 | if($key == 3){ |
---|
| 186 | $new_prefe['delete_and_show_previous_message'] = $tmp; |
---|
| 187 | } |
---|
| 188 | if($key == 4){ |
---|
| 189 | $new_prefe['alert_new_msg'] = $tmp; |
---|
| 190 | } |
---|
| 191 | if($key == 5){ |
---|
| 192 | $new_prefe['mainscreen_showmail'] = $tmp; |
---|
| 193 | } |
---|
| 194 | if($key == 10){ |
---|
| 195 | $new_prefe['signature'] = $tmp; |
---|
| 196 | } |
---|
| 197 | if($key == 7){ |
---|
| 198 | $new_prefe['hide_folders'] = $tmp; |
---|
| 199 | } |
---|
| 200 | if($key == 6){ |
---|
| 201 | $new_prefe['save_in_folder'] = $tmp; |
---|
| 202 | } |
---|
| 203 | if($key == 8){ |
---|
| 204 | $new_prefe['line_height'] = $tmp; |
---|
| 205 | } |
---|
| 206 | if($key == 9){ |
---|
| 207 | $new_prefe['font_size'] = $tmp; |
---|
| 208 | } |
---|
| 209 | if($key == 11){ |
---|
| 210 | $new_prefe['use_shortcuts'] = $tmp; |
---|
| 211 | } |
---|
| 212 | } |
---|
| 213 | |
---|
| 214 | $string_serial = serialize($new_prefe); |
---|
| 215 | $string_serial = get_magic_quotes_gpc() ? $string_serial : addslashes($string_serial); |
---|
| 216 | $query = "update phpgw_preferences set preference_value = '".$string_serial."' where preference_app = 'expressoMail'". |
---|
| 217 | " and preference_owner = '".$this->user_id."'"; |
---|
| 218 | |
---|
| 219 | if (!$this->db->query($query)) |
---|
| 220 | return "Failed!"; |
---|
| 221 | else |
---|
| 222 | return "OK!"; |
---|
| 223 | } |
---|
| 224 | */ |
---|
| 225 | function update_preferences($params){ |
---|
| 226 | $string_serial = urldecode($params['prefe_string']); |
---|
| 227 | $string_serial = get_magic_quotes_gpc() ? $string_serial : addslashes($string_serial); |
---|
| 228 | $query = "update phpgw_preferences set preference_value = '".$string_serial."' where preference_app = 'expressoMail'". |
---|
| 229 | " and preference_owner = '".$this->user_id."'"; |
---|
| 230 | |
---|
| 231 | if (!$this->db->query($query)) |
---|
| 232 | return $this->db->error; |
---|
| 233 | else |
---|
| 234 | return array("success" => true); |
---|
| 235 | } |
---|
| 236 | function getUserByEmail($params){ |
---|
| 237 | // Follow the referral |
---|
| 238 | $email = $params['email']; |
---|
| 239 | $query = 'select A.names_ordered, C.connection_name, C.connection_value, A.photo'. |
---|
| 240 | ' from phpgw_cc_contact A, phpgw_cc_contact_conns B, '. |
---|
| 241 | 'phpgw_cc_connections C where A.id_contact = B.id_contact'. |
---|
| 242 | ' and B.id_connection = C.id_connection and A.id_contact ='. |
---|
| 243 | '(select A.id_contact from phpgw_cc_contact A, phpgw_cc_contact_conns B,'. |
---|
| 244 | 'phpgw_cc_connections C where A.id_contact = B.id_contact'. |
---|
| 245 | ' and B.id_connection = C.id_connection and A.id_owner = '.$this -> user_id. |
---|
| 246 | ' and C.connection_value = \''.$email.'\') and '. |
---|
| 247 | 'C.connection_is_default = true and B.id_typeof_contact_connection = 2'; |
---|
| 248 | |
---|
| 249 | if (!$this->db->query($query)) |
---|
| 250 | return null; |
---|
| 251 | |
---|
| 252 | |
---|
| 253 | if($this->db->next_record()) { |
---|
| 254 | $result = $this->db->row(); |
---|
| 255 | |
---|
| 256 | $obj = array("cn" => $result['names_ordered'], |
---|
| 257 | "email" => $email, |
---|
| 258 | "type" => "personal", |
---|
| 259 | "telefone" => $result['connection_value']); |
---|
| 260 | |
---|
| 261 | if($result['photo']) |
---|
| 262 | $_SESSION['phpgw_info']['expressomail']['contact_photo'] = array($result['photo']); |
---|
| 263 | |
---|
| 264 | return $obj; |
---|
| 265 | } |
---|
| 266 | return $result; |
---|
| 267 | } |
---|
| 268 | |
---|
| 269 | function get_dynamic_contacts() |
---|
| 270 | { |
---|
| 271 | // Pesquisa os emails e ultima inserção nos contatos dinamicos. |
---|
| 272 | if(!$this->db->select('phpgw_expressomail_contacts','data', |
---|
| 273 | 'id_owner ='.$this -> user_id, |
---|
| 274 | __LINE__,__FILE__)) |
---|
| 275 | { |
---|
| 276 | return $this->db->Error; |
---|
| 277 | } |
---|
| 278 | while($this->db->next_record()) |
---|
| 279 | { |
---|
| 280 | $result[] = $this->db->row(); |
---|
| 281 | } |
---|
| 282 | foreach($result as $item) |
---|
| 283 | { |
---|
| 284 | $contacts = unserialize($item['data']); |
---|
| 285 | } |
---|
| 286 | if (count($contacts) == 0) |
---|
| 287 | { |
---|
| 288 | return null; |
---|
| 289 | } |
---|
| 290 | return $contacts; |
---|
| 291 | } |
---|
| 292 | |
---|
| 293 | function update_contacts($contacts=array()) |
---|
| 294 | { |
---|
| 295 | // Atualiza um email nos contatos dinamicos. |
---|
| 296 | if(!$this->db->update('phpgw_expressomail_contacts ','data=\''.serialize($contacts).'\'', |
---|
| 297 | 'id_owner ='.$this -> user_id, |
---|
| 298 | __LINE__,__FILE__)) |
---|
| 299 | { |
---|
| 300 | return $this->db->Error; |
---|
| 301 | } |
---|
| 302 | return $contacts; |
---|
| 303 | } |
---|
| 304 | |
---|
| 305 | function insert_contact($contact) |
---|
| 306 | { |
---|
| 307 | $contacts[] = array( 'timestamp' => time(), |
---|
| 308 | 'email' => $contact ); |
---|
| 309 | |
---|
| 310 | // Insere um email nos contatos dinamicos. |
---|
| 311 | $query = 'INSERT INTO phpgw_expressomail_contacts (data, id_owner) ' . |
---|
| 312 | 'values ( \''.serialize($contacts).'\', '.$this->user_id.')'; |
---|
| 313 | |
---|
| 314 | if(!$this->db->query($query,__LINE__,__FILE__)) |
---|
| 315 | return $this->db->Error; |
---|
| 316 | return $contacts; |
---|
| 317 | } |
---|
| 318 | |
---|
| 319 | function remove_dynamic_contact($user_id,$line,$file) |
---|
| 320 | { |
---|
| 321 | $where = $user_id.' = id_owner'; |
---|
| 322 | $this->db->delete('phpgw_expressomail_contacts',$where,$line,$file); |
---|
| 323 | } |
---|
| 324 | |
---|
| 325 | function insert_certificate($email,$certificate,$serialnumber,$authoritykeyidentifier=null) |
---|
| 326 | { |
---|
| 327 | if(!$email || !$certificate || !$serialnumber || !$authoritykeyidentifier) |
---|
| 328 | return false; |
---|
| 329 | // Insere uma chave publica na tabela phpgw_certificados. |
---|
| 330 | $data = array ('email' => $email, |
---|
| 331 | 'chave_publica' => $certificate, |
---|
| 332 | 'serialnumber' => $serialnumber, |
---|
| 333 | 'authoritykeyidentifier' => $authoritykeyidentifier); |
---|
| 334 | |
---|
| 335 | if(!$this->db->insert('phpgw_certificados',$data,__LINE__,__FILE__)) |
---|
| 336 | return $this->db->Error; |
---|
| 337 | return true; |
---|
| 338 | } |
---|
| 339 | |
---|
| 340 | function get_certificate($email=null) |
---|
| 341 | { |
---|
| 342 | if(!$email) return false; |
---|
| 343 | $result = array(); |
---|
| 344 | |
---|
| 345 | $where = array ('email' => $email, |
---|
| 346 | 'revogado' => 0, |
---|
| 347 | 'expirado' => 0); |
---|
| 348 | |
---|
| 349 | if(!$this->db->select('phpgw_certificados','chave_publica', $where, __LINE__,__FILE__)) |
---|
| 350 | { |
---|
| 351 | $result['dberr1'] = $this->db->Error; |
---|
| 352 | return $result; |
---|
| 353 | } |
---|
| 354 | $regs = array(); |
---|
| 355 | while($this->db->next_record()) |
---|
| 356 | { |
---|
| 357 | $regs[] = $this->db->row(); |
---|
| 358 | } |
---|
| 359 | if (count($regs) == 0) |
---|
| 360 | { |
---|
| 361 | $result['dberr2'] = ' Certificado nao localizado.'; |
---|
| 362 | return $result; |
---|
| 363 | } |
---|
| 364 | $result['certs'] = $regs; |
---|
| 365 | return $result; |
---|
| 366 | } |
---|
| 367 | |
---|
| 368 | function update_certificate($serialnumber=null,$email=null,$authoritykeyidentifier,$expirado,$revogado) |
---|
| 369 | { |
---|
| 370 | if(!$email || !$serialnumber) return false; |
---|
| 371 | if(!$expirado) |
---|
| 372 | $expirado = 0; |
---|
| 373 | if(!$revogado) |
---|
| 374 | $revogado = 0; |
---|
| 375 | |
---|
| 376 | $data = array ('expirado' => $expirado, |
---|
| 377 | 'revogado' => $revogado); |
---|
| 378 | |
---|
| 379 | $where = array ('email' => $email, |
---|
| 380 | 'serialnumber' => $serialnumber, |
---|
| 381 | 'authoritykeyidentifier' => $authoritykeyidentifier); |
---|
| 382 | |
---|
| 383 | if(!$this->db->update('phpgw_certificados',$data,$where,__LINE__,__FILE__)) |
---|
| 384 | { |
---|
| 385 | return $this->db->Error; |
---|
| 386 | } |
---|
| 387 | return true; |
---|
| 388 | } |
---|
| 389 | } |
---|
| 390 | ?> |
---|