The below are the changes for the Create API : --------------------------------------------------------------------------------------------------------- api/ost-api.php (create API ) - *****Please take a back up of your script before making any changes --------------------------------------------------------------------------------------------------------- function get_param($arr, $key, $default=""){ return isset($arr[$key])?$arr[$key]:$default; } $ip=array(); $ip['helptopic']= get_param($_POST, 'helptopic', ''); $ip['email']= get_param($_POST, 'email',''); $ip['name']= get_param($_POST, 'name',''); $ip['phone']= get_param($_POST, 'phone',''); $ip['extension']= get_param($_POST, 'extension',''); $ip['issue']= get_param($_POST, 'issue',''); $ip['priority']= get_param($_POST, 'priority',''); $ip['issuedetails']= get_param($_POST, 'issuedetails',''); $ip['department']= get_param($_POST, 'department',''); $ip['ipaddress']= get_param($_POST, 'ipaddress',''); //this is the ip address for remote address from where it is accessing from $target_dir=""; $filename=""; $attachArray=array(); if(!empty($_FILES)){ $target_dir=$_SERVER['DOCUMENT_ROOT']."/osticket/upload/"; $target_dir = $target_dir . basename($_FILES["attachments"]["name"]); $filename=$_FILES["attachments"]["name"]; if (move_uploaded_file($_FILES["attachments"]["tmp_name"], $target_dir)) { chmod($target_dir, 0777); } else { echo "Sorry, there was an error uploading your file."; } $path= $target_dir; $type = pathinfo($path, PATHINFO_EXTENSION); $mime='data:text/plain;'; $imgext=array("jpg","jpeg","gif","png","bmp"); $excelext=array("xlsx","xls"); $pdfext=array("pdf"); $docext=array("doc","docx"); if(in_array($type, $imgext)){ $mime="data:image/".$type.";"; } if(in_array($type, $pdfext)){ $mime="data:application/".$type.";"; } if(in_array($type, $excelext)){ if($type=="xlsx") $mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;"; else $mime="application/msword;"; } if(in_array($type, $docext)){ if($type=="docx") $mime="application/vnd.openxmlformats-officedocument.wordprocessingml.document;"; else $mime="application/msword;"; } $attachArray=array($filename => $mime.'base64,'.base64_encode(file_get_contents($target_dir))); } $config = array( 'url'=>'/api/http.php/tickets.json', //Need to crate a tickets.json file under api folder and chmod to 777 to the file 'key'=>'' ); $assign=1; # Fill in the data for the new ticket, this will likely come from $_POST. $data = array( 'alert' => true, 'autorespond' => true, 'name' => $ip['name'], 'email' => $ip['email'], 'subject' => $ip['issue'], "priority" => $ip['priority'], 'message' => $ip['issuedetails'], 'ip' => $ip['ipaddress'], 'field_32' => $ip['department'], 'assignId' => $assign, 'attachments' => $attachArray, ); #pre-checks function_exists('curl_version') or die('CURL support required'); function_exists('json_encode') or die('JSON support required'); #set timeout set_time_limit(10); #curl post $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $config['url']); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); curl_setopt($ch, CURLOPT_USERAGENT, 'osTicket API Client v1.9.2'); curl_setopt($ch, CURLOPT_HEADER, FALSE); curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Expect:', 'X-API-Key: '.$config['key'],'X-API-ADDR: '.$ip['ipaddress'])); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); $result=curl_exec($ch); $code = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); if ($code != 201) die('Unable to create ticket: '.$result); $ticket_id = (int) $result; # Continue onward here if necessary. $ticket_id has the ID number of the # newly-created ticket //Use postfix exit codes...expected by MTA. include_once 'automail.php'; echo $ticket_id; ----------------------------------------------------------------------------------------------------------------------------------------------- api/automail.php (for sending an email) ----------------------------------------------------------------------------------------------------------------------------------------------- '/api/tickets.email', //Need to crate a tickets.email file under api folder and chmod to 777 to the file 'key'=>'' ); #pre-checks function_exists('file_get_contents') or die('upgrade php >=4.3'); function_exists('curl_version') or die('CURL support required'); set_time_limit(10); #curl post $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $config['url']); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); curl_setopt($ch, CURLOPT_USERAGENT, 'osTicket API Client v1.9.2'); curl_setopt($ch, CURLOPT_HEADER, TRUE); curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Expect:', 'X-API-Key: '.$config['key'])); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); $result=curl_exec($ch); curl_close($ch); $code = 75; if(preg_match('/HTTP\/.* ([0-9]+) .*/', $result, $status)) { switch($status[1]) { case 201: //Success $code = 0; break; case 400: $code = 66; break; case 401: /* permission denied */ case 403: $code = 77; break; case 415: case 416: case 417: case 501: $code = 65; break; case 503: $code = 69; break; case 500: //Server error. default: //Temp (unknown) failure - retry $code = 75; } } if(preg_match('/HTTP\/.* ([0-9]+) .*/', $result, $status)) $code=$status[1]; echo $code; ?> ------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Changes need to be done in the other files as follows : ---------------------------------------------------------------------- includes/class.ticket.php --------------------------- (Line 2312 - 2320) if ($vars['topicId'] && ($topic=Topic::lookup($vars['topicId']))) { if ($topic_form = $topic->getForm()) { $TF = $topic_form->getForm($vars); $topic_form = $topic_form->instanciate(); $topic_form->setSource($vars); if (!$TF->isValid($field_filter('topic'))) $errors = array_merge($errors, $TF->errors()); } } (Line 2359 - 2360) if ($vars['topicId'] && !$topic) $errors['topicId'] = 'Invalid help topic selected'; ------------------------------------------------------------------------------------------------------------------------------------------------------------------------- includes/class.forms.php ----------------------------------------- (Line 258 - 262) function parse($value) { $val = preg_replace('/[^\dX]/', '', $value); return $val ?: $value; } -------------------------------------------------------------------------------------------------------------------------------------------------------------------------- includes/class.dynamic__forms.php ---------------------------------------- under //class DynamicFormEntry extends VerySimpleModel put the below code ----------------------------------- (Line 525) var $_source = null; (Line 577 - 586) function getFields() { if (!isset($this->_fields)) { $this->_fields = array(); foreach ($this->getAnswers() as $a) { $T = $this->_fields[] = $a->getField(); $T->setForm($this); } } return $this->_fields; } (Line 600 - 602) function getSource() { return $this->_source ?: (isset($this->id) ? false : $_POST); } (Line 612 - 614) function setSource($source) { $this->_source = $source; } (Line 836 - 874) function save() { if (count($this->dirty)) $this->set('updated', new SqlFunction('NOW')); parent::save(); foreach ($this->getAnswers() as $a) { $field = $a->getField(); foreach ($this->getFields() as $field) { $a = $field->getAnswer(); if ($this->object_type == 'U' && in_array($field->get('name'), array('name','email'))) continue; if ($this->object_type == 'O' && in_array($field->get('name'), array('name'))) continue; $val = $field->to_database($field->getClean()); if (is_array($val)) { $a->set('value', $val[0]); $a->set('value_id', $val[1]); } else $a->set('value', $val); $a->set('entry_id', $this->get('id')); // Don't save answers for presentation-only fields if ($field->hasData() && !$field->isPresentationOnly()) $a->save(); } } $this->_values = null; } (Line 1239 - 1247) function parse($value) { $config = $this->getConfiguration(); if (is_int($value)) $val = $this->to_php($this->getWidget()->getEnteredValue(), (int) $value); elseif (!$config['typeahead']) $val = $this->to_php(null, (int) $value); if (!$val) $val = $this->to_php($value); return $val; } ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- includes/class.user.php --------------------------- (Line 473 - 520) function updateInfo($vars, &$errors) { $valid = true; $forms = $this->getDynamicData(); foreach ($forms as $cd) { $cd->setSource($vars); if (!$cd->isValidForClient()) $valid = false; elseif ($cd->get('type') == 'U' && ($form= $cd->getForm()) && ($f=$form->getField('email')) && $f->getClean() && ($u=User::lookup(array('emails__address'=>$f->getClean()))) && $u->id != $this->getId()) { $valid = false; $f->addError('Email is assigned to another user'); } } if (!$valid) return false; foreach ($this->getDynamicData() as $cd) { if (($f=$cd->getForm()) && $f->get('type') == 'U') { if (($name = $f->getField('name'))) { $this->name = $name->getClean(); $this->save(); } if (($email = $f->getField('email'))) { $this->default_email->address = $email->getClean(); $this->default_email->save(); } } $cd->save(); } return true; } --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- includes/class.api.php ------------------------------ Under //Class ApiController (Line 170 - 188) function requireApiKey() { if(!($key=$this->getApiKey())) return $this->exerr(401, 'Valid API key required'); elseif (!$key->isActive() || $key->getIPAddr()!=$_SERVER['HTTP_X_API_ADDR'])//$_SERVER['REMOTE_ADDR'] return $this->exerr(401, 'API key not found/active or source IP not authorized'); return $key; } function getApiKey() { if (!$this->apikey && isset($_SERVER['HTTP_X_API_KEY']) && isset($_SERVER['HTTP_X_API_ADDR'])) $this->apikey = API::lookupByKey($_SERVER['HTTP_X_API_KEY'], $_SERVER['HTTP_X_API_ADDR']); return $this->apikey; } //The below function for image upload and attachment (Line 381 - 417) function fixup($current) { if (!is_array($current)) return $current; foreach ($current as $key=>&$value) { if ($key == "phone") { list($value,$current["phone_ext"]) = explode("X", strtoupper($value), 2); } else if ($key == "alert") { $value = (bool)$value; } else if ($key == "autorespond") { $value = (bool)$value; } else if ($key == "attachments") { $keyparameter= key($value); foreach ($value as &$info) { $data = reset($info); # PHP5: fopen("data://$data[5:]"); if (substr($info, 0, 5) != "data:") { $data = substr($data,5); $info = array( "data" => $data, "type" => ($type) ? $type : "text/plain", "name" => $keyparameter); } else { $data = substr($info,5); list($meta, $contents) = explode(",", $data); list($type, $extra) = explode(";", $meta); $info = array( "data" => $contents, "type" => ($type) ? $type : "text/plain", "name" => $keyparameter); if (substr($extra, -6) == "base64") $info["encoding"] = "base64"; # Handle 'charset' hint in $extra, such as # data:text/plain;charset=iso-8859-1,Blah # Convert to utf-8 since it's the encoding scheme # for the database. Otherwise, assume utf-8 list($param,$charset) = explode('=', $extra); if ($param == 'charset' && function_exists('iconv')) $contents = iconv($charset, "UTF-8", $contents); } } unset($value); } if (is_array($value)) { $value = $this->fixup($value); } } return $current; } ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------