From 07f5140771388c9e0c8a99b0dd2e5d950bdb173b Mon Sep 17 00:00:00 2001 From: Yuchen Pei Date: Thu, 14 Oct 2021 15:16:42 +1100 Subject: moving h-source subdir out. --- Application/Include/myFunctions.php | 542 ++++++++++++++++++++++++++++++++++++ 1 file changed, 542 insertions(+) create mode 100644 Application/Include/myFunctions.php (limited to 'Application/Include/myFunctions.php') diff --git a/Application/Include/myFunctions.php b/Application/Include/myFunctions.php new file mode 100644 index 0000000..b121424 --- /dev/null +++ b/Application/Include/myFunctions.php @@ -0,0 +1,542 @@ +. + +if (!defined('EG')) die('Direct access not allowed!'); + +function encodeUrl($url) +{ + $url = str_replace(' ','-',$url); + $url = str_replace('.','-',$url); + $url = str_replace(',','-',$url); + $url = str_replace('[','-',$url); + $url = str_replace(']','-',$url); + $url = str_replace('(','-',$url); + $url = str_replace(')','-',$url); + $url = str_replace('/','-',$url); + $url = str_replace(''','-',$url); + $url = str_replace('@','-at-',$url); + $url = str_replace('?','-',$url); + +// $temp = null; +// for ($i=0;$i $ovalue) + { + $nkeys = array_keys($new, $ovalue); +// echo memory_get_peak_usage(true)."
"; + foreach($nkeys as $nindex) + { + $matrix[$oindex][$nindex] = isset($matrix[$oindex - 1][$nindex - 1]) ? + $matrix[$oindex - 1][$nindex - 1] + 1 : 1; + if($matrix[$oindex][$nindex] > $maxlen) + { + $maxlen = $matrix[$oindex][$nindex]; + $omax = $oindex + 1 - $maxlen; + $nmax = $nindex + 1 - $maxlen; + } + } + } + if($maxlen == 0) return array(array('d'=>$old, 'i'=>$new)); + return array_merge( + diff(array_slice($old, 0, $omax), array_slice($new, 0, $nmax)), + array_slice($new, $nmax, $maxlen), + diff(array_slice($old, $omax + $maxlen), array_slice($new, $nmax + $maxlen))); +} + +function htmlDiff($old, $new) +{ + $old = str_replace("\r\n"," \r\n ",$old); + $new = str_replace("\r\n"," \r\n ",$new); + + $ret = null; + $diff = diff(removeEmptyStrings(explode(' ', $old)),removeEmptyStrings(explode(' ', $new))); + foreach($diff as $k) + { + if(is_array($k)) + $ret .= (!empty($k['d'])?"".implode(' ',$k['d'])." ":''). + (!empty($k['i'])?"".implode(' ',$k['i'])." ":''); + else $ret .= $k . ' '; + } + return $ret; +} + + +function applyBreaks($values,$fields) +{ + $fieldsArray = explode(',',$fields); + + foreach ($fieldsArray as $field) + { + if (array_key_exists($field,$values)) + { + $values[$field] = nl2br($values[$field]); + } + } + return $values; +} + + +function getLinkToUser($user) +{ + if (strstr($user,'__')) + { + return str_replace('__',null,$user); + } + else + { + return "$user"; + } +} + +function getUserName($id_user = 0) +{ + $clean['id_user'] = (int)$id_user; + $u = new UsersModel(); + return $u->getUser($clean['id_user']); +} + +function getMotivation($row,$controller) +{ + if (strcmp($row['deletion']['object'],'duplication') === 0) + { + $clean['id_hard'] = (int)$row['deletion']['id_duplicate']; + $hardware = new HardwareModel(); + $name = encodeUrl($hardware->getTheModelName($clean['id_hard'])); + return "duplication of the model having id ".$clean['id_hard'].""; + } + else + { + return "".$row['deletion']['object'].""; + } +} + +//get the text in the right language +function gtext($string) +{ + if (isset(Lang::$i18n[Lang::$current][$string])) + { + return Lang::$i18n[Lang::$current][$string]; + } + return $string; +} + +function singular($string) +{ + if (isset(Lang::$singular[Lang::$current][$string])) + { + return Lang::$singular[Lang::$current][$string]; + } + return $string; +} + +function plural($string) +{ + if (isset(Lang::$plural[Lang::$current][$string])) + { + return Lang::$plural[Lang::$current][$string]; + } + return $string; +} + +//get the hardware info from the talk id +function getHardwareInfoFromTalkId($id = 0) +{ + $clean['id'] = (int)$id; + $talk = new TalkModel(); + $res = $talk->select('hardware.type,hardware.id_hard')->from('hardware inner join talk')->using('id_hard')->where(array('id_talk'=>$clean['id']))->send(); + return count($res) > 0 ? $res[0]['hardware'] : null; +} + +//get hardware info from id +function getHardwareInfoFromId($id = 0) +{ + $clean['id'] = (int)$id; + $hw = new HardwareModel(); + $res = $hw->select()->where(array('id_hard'=>$clean['id']))->send(); + $controller = 'home/index/en'; + $model = ''; + if (count($res) > 0) + { + $controller = Hardware::getControllerFromType($res[0]['hardware']['type']); + $controller = strcmp($controller,'') !== 0 ? $controller : 'home/index/en'; + $model = $res[0]['hardware']['model']; + } + + return array('controller'=>$controller,'model'=>$model); +} + +//get the wiki page info from the talk id +function getWikiPageInfoFromTalkId($id = 0) +{ + $clean['id'] = (int)$id; + $talk = new WikitalkModel(); + $res = $talk->select('wiki.id_wiki')->from('wiki inner join wiki_talk')->using('id_wiki')->where(array('id_talk'=>$clean['id']))->send(); + return count($res) > 0 ? $res[0]['wiki']['id_wiki'] : ''; +} + +//get the issue info from the message id +function getIssueNumberFromMessageId($id = 0) +{ + $clean['id'] = (int)$id; + $mess = new MessagesModel(); + $res = $mess->select('issues.id_issue')->from('issues inner join messages')->using('id_issue')->where(array('id_mes'=>$clean['id']))->toList('issues.id_issue')->send(); + return count($res) > 0 ? $res[0] : ''; +} + +//get thw wiki name from the id +function getWikiNameFromId($id = 0) +{ + $clean['id'] = (int)$id; + $wiki = new WikiModel(); + $name = $wiki->getTheModelName($clean['id']); + return $name; +} + +//return the URL to the moderated object page +function goToModeratedItem( $row = array() ) +{ + $url = null; + + switch ($row['type']) + { + case 'message': + $url = 'issues/view/'.Lang::$current.'/'.getIssueNumberFromMessageId($row['id']).'#message-'.$row['id']; + break; + case 'talk': + $hardInfo = getHardwareInfoFromTalkId($row['id']); + if (isset($hardInfo)) + { + $controller = Hardware::$typeToController[$hardInfo['type']]; + $url = $controller.'/talk/'.Lang::$current.'/'.$hardInfo['id_hard'].'#talk-'.$row['id']; + } + else + { + $url = 'last/modactions/'.Lang::$current; + } + break; + case 'user': + $url = 'meet/user/'.Lang::$current.'/'.getUserName($row['id']); + break; + case 'issue': + $url = 'issues/view/'.Lang::$current.'/'.$row['id']; + break; + case 'issue_del': + $url = 'issues/view/'.Lang::$current.'/'.$row['id']; + break; + case 'wiki_talk': + $url = 'wiki/talk/'.Lang::$current.'/'.getWikiPageInfoFromTalkId($row['id']).'#wiki-talk-'.$row['id']; + break; + case 'page': + $url = 'wiki/page/'.Lang::$current.'/'.encodeUrl(getWikiNameFromId($row['id'])); + break; + case 'page_del': + $url = 'wiki/page/'.Lang::$current.'/'.encodeUrl(getWikiNameFromId($row['id'])); + break; + case 'device': + $hardInfo = getHardwareInfoFromId($row['id']); + $url = $hardInfo['controller'].'/view/'.Lang::$current.'/'.$row['id'].'/'.encodeUrl($hardInfo['model']); + break; + case 'device_app': + $hardInfo = getHardwareInfoFromId($row['id']); + $url = $hardInfo['controller'].'/view/'.Lang::$current.'/'.$row['id'].'/'.encodeUrl($hardInfo['model']); + break; + case 'device_cl': + $hardInfo = getHardwareInfoFromId($row['id']); + $url = $hardInfo['controller'].'/view/'.Lang::$current.'/'.$row['id'].'/'.encodeUrl($hardInfo['model']); + break; + case 'message_ins': + $url = 'issues/view/'.Lang::$current.'/'.getIssueNumberFromMessageId($row['id']).'#message-'.$row['id']; + break; + case 'wiki_talk_ins': + $url = 'wiki/talk/'.Lang::$current.'/'.getWikiPageInfoFromTalkId($row['id']).'#wiki-talk-'.$row['id']; + break; + case 'issue_ins': + $url = 'issues/view/'.Lang::$current.'/'.$row['id']; + break; + case 'talk_ins': + $hardInfo = getHardwareInfoFromTalkId($row['id']); + if (isset($hardInfo)) + { + $controller = Hardware::$typeToController[$hardInfo['type']]; + $url = $controller.'/talk/'.Lang::$current.'/'.$hardInfo['id_hard'].'#talk-'.$row['id']; + } + else + { + $url = 'last/modactions/'.Lang::$current; + } + break; + case 'hardware': + $hardInfo = getHardwareInfoFromId($row['id']); + $url = $hardInfo['controller'].'/view/'.Lang::$current.'/'.$row['id'].'/'.encodeUrl($hardInfo['model']); + break; + case 'wiki': + $url = 'wiki/page/'.Lang::$current.'/'.encodeUrl(getWikiNameFromId($row['id'])); + break; + } + return Url::getRoot($url); +} + +function getUrlsFromIdHard($id_hard) +{ + $clean['id_hard'] = (int)$id_hard; + + $urlView = $urlTalk = Url::getRoot(); + + $deviceName = null; + + $hard = new HardwareModel(); + $res = $hard->select('type,model')->where(array('id_hard'=>$clean['id_hard']))->send(); + if (count($res) > 0) + { + $urlView = Url::getRoot().Hardware::$typeToController[$res[0]['hardware']['type']]."/view/".Lang::$current."/".$clean['id_hard']."/".encodeUrl($res[0]['hardware']['model']); + + $urlTalk = Url::getRoot().Hardware::$typeToController[$res[0]['hardware']['type']]."/talk/".Lang::$current."/".$clean['id_hard']; + + $deviceName = $res[0]['hardware']['model']; + } + + return array('urlView'=>$urlView,'urlTalk'=>$urlTalk,'modelName'=>$deviceName); + +} + +function getDiffArray($associativeArray, $oldArray, $newArray) +{ + $diffArray = array(); + foreach ($associativeArray as $field => $label) + { + if (array_key_exists($field,$oldArray) and array_key_exists($field,$newArray)) + { + $diffArray[$label] = htmlDiff($oldArray[$field], $newArray[$field]); + } + } + return $diffArray; +} + +//get the translation function +function getTranslationFunction($fieldName) +{ + $fieldsFunc = array( + "vendor" => "betterVendor", + "interface" => "translate_and_gtext", + "subtype" => "translate_and_gtext", + ); + + if (array_key_exists($fieldName,$fieldsFunc)) + { + return $fieldsFunc[$fieldName]; + } + + return "gtext"; +} + +//Map db type names to display names +function displayName($name) { + switch ($name) { + case '3G-card': + $name = '3G cards'; + break; + case 'bluetooth': + $name = 'Bluetooth'; + break; + case 'ethernet-card': + $name = 'Ethernet cards'; + break; + case 'fingerprint-reader': + $name = 'Fingerprint readers'; + break; + case 'host-controller': + $name = 'Host Controllers'; + break; + case 'modem': + $name = 'Modems'; + break; + case 'notebook': + $name = 'Notebooks'; + break; + case 'printer': + $name = 'Printers'; + break; + case 'RAID-adapter'; + $name = 'RAID adapters'; + break; + case 'scanner': + $name = 'Scanners'; + break; + case 'sd-card-reader': + $name = 'SD card readers'; + break; + case 'soundcard': + $name = 'Sound cards'; + break; + case 'videocard': + $name = 'Video cards'; + break; + case 'webcam': + $name = 'Webcams'; + break; + case 'wifi': + $name = 'Wifi cards'; + break; + case 'acquisition-card': + $name = 'Acquisition cards'; + break; + } + return $name; +} -- cgit v1.2.3