was.rb 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205
  1. #!/usr/bin/ruby
  2. #
  3. # This file is part of centurio.work/ing/commands.
  4. #
  5. # centurio.work/ing/commands is free software: you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or (at your
  8. # option) any later version.
  9. #
  10. # centurio.work/ing/commands is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
  13. # Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License along with
  16. # centurio.work/ing/commands (file COPYING in the main directory). If not, see
  17. # <http://www.gnu.org/licenses/>.
  18. require 'rubygems'
  19. require 'json'
  20. require 'riddl/server'
  21. require 'riddl/client'
  22. require 'fileutils'
  23. require 'nokogiri'
  24. require 'sqlite3'
  25. require 'net/http'
  26. $db = SQLite3::Database.open 'database/stations.db'
  27. class GetJsonFiles < Riddl::Implementation
  28. def response
  29. Dir.chdir( __dir__ + '/json')
  30. ret = [];
  31. Dir.glob('*').select{ |e|
  32. ret.append(e)
  33. }
  34. Riddl::Parameter::Complex.new('list','application/json',JSON::pretty_generate(ret))
  35. end
  36. end
  37. class GetJson < Riddl::Implementation
  38. def response
  39. Dir.chdir( __dir__ + '/json')
  40. Riddl::Parameter::Complex.new('list','application/json',File.open(@r[1]).read) rescue Riddl::Parameter::Complex.new('list','application/json', '{"error":"No such file"}')
  41. end
  42. end
  43. class SaveJson < Riddl::Implementation
  44. def response
  45. Dir.chdir( __dir__ + '/json')
  46. File.write(@r.last,(@p[0].value.read))
  47. end
  48. end
  49. class GetStations < Riddl::Implementation
  50. def response
  51. result = $db.execute "SELECT * FROM stations"
  52. builder = Nokogiri::XML::Builder.new do |xml|
  53. xml.stations {
  54. result.each do |row|
  55. xml.station(:id => row[0]){
  56. resultstation = $db.execute "SELECT * FROM stationpattern WHERE station = #{row[0]}"
  57. resultstation.each do |row|
  58. xml.pattern(:id => row[1], :description => row[2], :changed => row[3]){
  59. xml.parts{
  60. resultpattern = $db.execute "SELECT * FROM pattern WHERE internalID = #{row[1]}"
  61. resultpattern.each do |row2|
  62. xml.send(row2[1], row2[2])
  63. end
  64. #$db.execute "CREATE TABLE IF NOT EXISTS pattern(internalID INT, type CHARACTER(20), value CHARACTER(20), FOREIGN KEY(internalID) REFERENCES stationpattern(internalID) ON DELETE CASCADE)"
  65. }
  66. }
  67. end
  68. }
  69. end
  70. }
  71. end
  72. #puts builder.to_xml
  73. Riddl::Parameter::Complex.new('stations','application/xml',builder.to_xml)
  74. end
  75. end
  76. class CreateStation < Riddl::Implementation
  77. def self::createDB(n)
  78. #result = $db.execute "SELECT MAX(station) FROM stations"
  79. #if(result[0][0] == nil)
  80. # $db.execute "INSERT INTO stations (station) VALUES (?)", 0
  81. #else
  82. # $db.execute "INSERT INTO stations (station) VALUES (?)", result[0][0]+1
  83. #end
  84. #$db.execute ("INSERT INTO stations (station, name) VALUES (?,?)", [n,n])
  85. $db.execute("INSERT OR IGNORE INTO stations (station, name) VALUES (?,?)", [n,n])
  86. end
  87. def response
  88. GetStation::createDB
  89. end
  90. end
  91. class GetStation < Riddl::Implementation
  92. def self::prepare(id)
  93. resultstation = $db.execute "SELECT * FROM stationpattern WHERE station = #{id}"
  94. Nokogiri::XML::Builder.new do |xml|
  95. xml.station(:id => id){
  96. resultstation.each do |row|
  97. xml.pattern(:id => row[1], :description => row[2], :changed => row[3]){
  98. xml.parts{
  99. resultpattern = $db.execute "SELECT * FROM pattern WHERE internalID = #{row[1]}"
  100. resultpattern.each do |row2|
  101. xml.send(row2[1], row2[2])
  102. end
  103. }
  104. }
  105. end
  106. }
  107. end
  108. end
  109. def response
  110. builder = GetStation::prepare(@r.last)
  111. Riddl::Parameter::Complex.new('station','application/xml',builder.to_xml)
  112. end
  113. end
  114. class CreatePattern < Riddl::Implementation
  115. def response
  116. doc = Nokogiri::XML(@p[0].value)
  117. result = $db.execute "SELECT MAX(internalID) FROM stationpattern"
  118. if(result[0][0] == nil)
  119. id = 0
  120. else
  121. id = result[0][0] +1
  122. end
  123. $db.execute("INSERT INTO stationpattern (station, internalID, description, date) VALUES (?,?,?,?)", [@r.last, id, doc.xpath("/*/@description")[0].value, doc.xpath("/*/@changed")[0].value])
  124. doc.xpath(".//parts/*").each do |node|
  125. if(node.text != "")
  126. $db.execute("INSERT INTO pattern (internalID, type, value) VALUES (?,?,?)", [id, node.name, node.text])
  127. end
  128. end
  129. ret = {:id => id}
  130. Riddl::Parameter::Complex.new('list','application/json',JSON::pretty_generate(ret))
  131. end
  132. end
  133. class GetPattern < Riddl::Implementation
  134. def response
  135. resultstation = $db.execute "SELECT * FROM stationpattern WHERE station = #{@r[0]} AND internalID = #{@r[1]}"
  136. builder = Nokogiri::XML::Builder.new do |xml|
  137. resultstation.each do |row|
  138. xml.pattern(:id => row[1], :description => row[2], :changed => row[3]){
  139. xml.parts{
  140. resultpattern = $db.execute "SELECT * FROM pattern WHERE internalID = #{row[1]}"
  141. resultpattern.each do |row2|
  142. xml.send(row2[1], row2[2])
  143. end
  144. }
  145. }
  146. end
  147. end
  148. Riddl::Parameter::Complex.new('pattern','application/xml',builder.to_xml)
  149. end
  150. end
  151. class UpdatePattern < Riddl::Implementation
  152. def response
  153. doc = Nokogiri::XML(@p[0].value)
  154. $db.execute("Update stationpattern SET description = ?, date = ? WHERE internalID = ?", [doc.xpath("/*/@description")[0].value, doc.xpath("/*/@changed")[0].value, @r[1]])
  155. $db.execute("DELETE FROM pattern WHERE internalID = ?", [@r[1]])
  156. doc.xpath(".//parts/*").each do |node|
  157. if(node.text != "")
  158. $db.execute("INSERT INTO pattern (internalID, type, value) VALUES (?,?,?)", [@r[1], node.name, node.text])
  159. end
  160. end
  161. end
  162. end
  163. class DeletePattern < Riddl::Implementation
  164. def response
  165. $db.execute("DELETE FROM stationpattern WHERE internalID = ?", [@r[1]])
  166. end
  167. end
  168. class DuplicatePattern < Riddl::Implementation
  169. def response
  170. #duplicate db etwas mühsam da der eintrag kopiert werden muss und dabei die unique ID verändert werden muss daher geht insert into select nicht so ganz.
  171. result = $db.execute "SELECT MAX(internalID) FROM stationpattern"
  172. if(result[0][0] == nil)
  173. id = 0
  174. else
  175. id = result[0][0] +1
  176. end
  177. result = $db.execute "SELECT * FROM stationpattern WHERE internalID = #{@r[1]}"
  178. result[0][1] = id;
  179. $db.execute( "INSERT INTO stationpattern values (?,?,?,?)", result[0])
  180. result = $db.execute "SELECT * FROM pattern WHERE internalID = #{@r[1]}"
  181. result.each do |row|
  182. row[0] = id;
  183. $db.execute( "INSERT INTO pattern values (?,?,?)", row)
  184. end
  185. #duplicate error
  186. #$db.execute "CREATE TABLE IF NOT EXISTS error(internalID INT, error TEXT, FOREIGN KEY(internalID) REFERENCES stationpattern(internalID) ON DELETE CASCADE)"
  187. result = $db.execute "SELECT * FROM error WHERE internalID = #{@r[1]}"
  188. if(result[0] != nil)
  189. result.each do |row|
  190. row[0] = id;
  191. questionmarks = ""
  192. row.length().times{questionmarks = questionmarks + ",?"}
  193. questionmarks[0] = ""
  194. $db.execute( "INSERT INTO error values (#{questionmarks})", row)
  195. end
  196. end
  197. #duplicate image db
  198. #$db.execute "CREATE TABLE IF NOT EXISTS image(internalID INT, imageID INT, language CHARACTER(20), label TEXT, FOREIGN KEY(internalID) REFERENCES stationpattern(internalID) ON DELETE CASCADE)"
  199. result = $db.execute "SELECT * FROM image WHERE internalID = #{@r[1]}"
  200. if(result[0] != nil)
  201. result.each do |row|
  202. row[0] = id;
  203. questionmarks = ""
  204. row.length().times{questionmarks = questionmarks + ",?"}
  205. questionmarks[0] = ""
  206. $db.execute( "INSERT INTO image values (#{questionmarks})", row)
  207. end
  208. end
  209. #duplicate images
  210. pathorigin = File.join(File.dirname(__dir__),'images/uploads', @r[0], @r[1])
  211. pathdestination = File.join(File.dirname(__dir__),'images/uploads', @r[0], id.to_s)
  212. if (Dir.exist?(pathorigin))
  213. FileUtils.mkdir_p(pathdestination)
  214. FileUtils.copy_entry pathorigin, pathdestination
  215. end
  216. #duplicate Replacement db
  217. #$db.execute "CREATE TABLE IF NOT EXISTS replacements(internalID INT, abbreviation TEXT, url TEXT, ordering INT, FOREIGN KEY(internalID) REFERENCES stationpattern(internalID) ON DELETE CASCADE)"
  218. result = $db.execute "SELECT * FROM replacements WHERE internalID = #{@r[1]}"
  219. if(result[0] != nil)
  220. result.each do |row|
  221. row[0] = id;
  222. questionmarks = ""
  223. row.length().times{questionmarks = questionmarks + ",?"}
  224. questionmarks[0] = ""
  225. $db.execute( "INSERT INTO replacements values (#{questionmarks})", row)
  226. end
  227. end
  228. ret = {:id => id}
  229. Riddl::Parameter::Complex.new('list','application/json',JSON::pretty_generate(ret))
  230. end
  231. end
  232. class SaveError < Riddl::Implementation
  233. def response
  234. $db.execute "CREATE TABLE IF NOT EXISTS error(internalID INT, error TEXT, FOREIGN KEY(internalID) REFERENCES stationpattern(internalID) ON DELETE CASCADE)"
  235. #Delete all, afterwards insert
  236. $db.execute("DELETE FROM error WHERE internalID = ?", [@r[1]])
  237. doc = Nokogiri::XML(@p[0].value)
  238. doc.xpath(".//reason").each do |node|
  239. $db.execute("INSERT INTO error (internalID, error) VALUES (?,?)", [@r[1], node.text])
  240. end
  241. end
  242. end
  243. class GetError < Riddl::Implementation
  244. def response
  245. result = $db.execute "SELECT * FROM error WHERE internalID = #{@r[1]}"
  246. builder = Nokogiri::XML::Builder.new do |xml|
  247. xml.error(){
  248. result.each do |row|
  249. xml.reason row[1]
  250. end
  251. }
  252. end
  253. Riddl::Parameter::Complex.new('error','application/xml',builder.to_xml)
  254. end
  255. end
  256. class SaveReplacement < Riddl::Implementation
  257. def response
  258. # $db.execute "CREATE TABLE IF NOT EXISTS replacements(internalID INT, abbreviation TEXT, url TEXT, ordering INT, FOREIGN KEY(internalID) REFERENCES stationpattern(internalID) ON DELETE CASCADE)"
  259. #Delete all, afterwards insert
  260. $db.execute("DELETE FROM replacements WHERE internalID = ?", [@r[1]])
  261. order = 0;
  262. doc = Nokogiri::XML(@p[0].value)
  263. doc.xpath(".//item").each do |node|
  264. $db.execute("INSERT INTO replacements (internalID, abbreviation, url, ordering) VALUES (?,?,?,?)", [@r[1], node.xpath(".//abbreviation").text, node.xpath(".//url").text, order])
  265. order += 1
  266. end
  267. end
  268. end
  269. class GetReplacement < Riddl::Implementation
  270. def response
  271. result = $db.execute "SELECT * FROM replacements WHERE internalID = #{@r[1]} ORDER BY ordering ASC"
  272. builder = Nokogiri::XML::Builder.new do |xml|
  273. xml.replacement(){
  274. result.each do |row|
  275. xml.item(){
  276. xml.abbreviation row[1]
  277. xml.url row[2]
  278. }
  279. end
  280. }
  281. end
  282. Riddl::Parameter::Complex.new('replacement','application/xml',builder.to_xml)
  283. end
  284. end
  285. class GetImages < Riddl::Implementation
  286. def response
  287. formatted_no_decl = Nokogiri::XML::Node::SaveOptions::FORMAT +
  288. Nokogiri::XML::Node::SaveOptions::NO_DECLARATION
  289. #$db.execute "CREATE TABLE IF NOT EXISTS image(internalID INT, imageID INT, language CHARACTER(20), label TEXT, FOREIGN KEY(internalID) REFERENCES stationpattern(internalID) ON DELETE CASCADE)"
  290. result = $db.execute "SELECT DISTINCT imageID FROM image WHERE internalID = #{@r[1]}"
  291. builder = Nokogiri::XML::Builder.new do |xml|
  292. xml.images(){
  293. result.each do |row|
  294. xml<< GetImage::prepare(@r[0], @r[1], row[0]).to_xml( save_with:formatted_no_decl )
  295. end
  296. }
  297. end
  298. Riddl::Parameter::Complex.new('images','application/xml',builder.to_xml)
  299. end
  300. end
  301. class UploadImage < Riddl::Implementation
  302. def response
  303. lang = @p[@p.length-1].value
  304. result = $db.execute "SELECT MAX(imageID) FROM image WHERE station = #{@r[0]} and patternID = #{@r[1]} and language = '#{lang}'"
  305. if(result[0][0] == nil)
  306. id = 0
  307. else
  308. id = result[0][0] +1
  309. end
  310. #puts JSON.pretty_generate(@p)
  311. i = 0
  312. while i < @p.length-1 do
  313. item = @p[i]
  314. if(item != nil && item.name == "files[]")
  315. path = File.join(File.dirname(__dir__),'images/uploads', @r[0], @r[1], id.to_s)
  316. FileUtils.mkdir_p(path)
  317. #juergen nach alternative fragen
  318. readFile = File.read(item.value.inspect.to_s[/Tempfile:(.*?)>/m, 1])
  319. File.open(File.join(path, lang + ".svg"), 'wb') do |file|
  320. file.write(readFile.to_s)
  321. end
  322. $db.execute("INSERT INTO image (station, patternID, imageID, language, label) VALUES (?,?,?,?,?)", [@r[0], @r[1], id, lang, "Label"])
  323. id += 1
  324. end
  325. i +=1
  326. end
  327. end
  328. end
  329. class AddExternalImage < Riddl::Implementation
  330. def response
  331. doc = Nokogiri::XML(@p[0].value)
  332. url = doc.xpath("/externalImage/url").text
  333. lang = doc.xpath("/externalImage/lang").text
  334. result = $db.execute "SELECT MAX(imageID) FROM image WHERE station = #{@r[0]} and patternID = #{@r[1]} and language = '#{lang}'"
  335. if(result[0][0] == nil)
  336. id = 0
  337. else
  338. id = result[0][0] +1
  339. end
  340. #Create link file (we create a file so moving/deleting works always the same and the database does not have to be changed for external files) only thing that had to be changed was imgReplacement.php
  341. image = '<svg width="100%" height="100%" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" overflow="hidden"> <image href="' + url +'" height="100%" width="100%"/>
  342. </svg>'
  343. path = File.join(File.dirname(__dir__),'images/uploads', @r[0], @r[1], id.to_s)
  344. FileUtils.mkdir_p(path)
  345. File.open(File.join(path, lang + ".svg"), 'wb') do |file|
  346. file.write(image)
  347. end
  348. #Create DB entry as usual
  349. $db.execute("INSERT INTO image (station, patternID, imageID, language, label) VALUES (?,?,?,?,?)", [@r[0], @r[1], id, lang, url])
  350. end
  351. end
  352. class ReorderImages < Riddl::Implementation
  353. def response
  354. #remove brackets
  355. iter = @p[0].value.read.chop
  356. iter[0] =""
  357. iter = iter.split(",").map(&:to_i)
  358. i = 0
  359. while i < iter.length do
  360. if(i != iter[i])
  361. #swap
  362. path = File.join(File.dirname(__dir__),'images/uploads', @r[0], @r[1], i.to_s)
  363. path2 = File.join(File.dirname(__dir__),'images/uploads', @r[0], @r[1], iter[i].to_s)
  364. tmp = File.join(File.dirname(__dir__),'images/uploads', @r[0], @r[1])
  365. FileUtils.mv(path + "/" + @r[4] + ".svg", tmp)
  366. FileUtils.mv(path2 + "/" + @r[4] + ".svg", path)
  367. FileUtils.mv(tmp + "/" + @r[4] + ".svg", path2)
  368. #DB Swap
  369. result = $db.execute "SELECT MAX(imageID) FROM image"
  370. if(result[0][0] == nil)
  371. maxImgId = 0
  372. else
  373. maxImgId = result[0][0] +1
  374. end
  375. #Wegen 999999 fragen
  376. #nutze max ID+1 zum tauschen
  377. $db.execute("UPDATE image SET imageID = ? WHERE station = ? AND patternID = ? AND imageID = ? AND language = ?", [maxImgId, @r[0], @r[1], i, @r[4]])
  378. $db.execute("UPDATE image SET imageID = ? WHERE station = ? AND patternID = ? AND imageID = ? AND language = ?", [i, @r[0], @r[1], iter[i], @r[4]])
  379. $db.execute("UPDATE image SET imageID = ? WHERE station = ? AND patternID = ? AND imageID = ? AND language = ?", [iter[i], @r[0], @r[1], maxImgId, @r[4]])
  380. iter.map! do |item|
  381. if(item == i)
  382. iter[i]
  383. else
  384. item
  385. end
  386. end
  387. end
  388. i +=1
  389. end
  390. end
  391. end
  392. class GetImage < Riddl::Implementation
  393. def self::prepare(station, pattern, imageID)
  394. result = $db.execute "SELECT * FROM image WHERE station = #{station} and patternID = #{pattern} and imageID = #{imageID}"
  395. Nokogiri::XML::Builder.new do |xml|
  396. xml.image(:id => imageID){
  397. result.each do |row|
  398. xml.variant(:lang => row[3], :label => row[4]){
  399. xml.text("https://centurio.work/customers/evva/was/images/uploads/#{station}/#{pattern}/#{imageID}/#{row[3]}.svg")
  400. }
  401. end
  402. }
  403. end
  404. end
  405. def response
  406. builder = GetImage::prepare(@r[0], @r[1], @r[3])
  407. Riddl::Parameter::Complex.new('image','application/xml',builder.to_xml)
  408. end
  409. end
  410. class UpdateImageLabel < Riddl::Implementation
  411. def response
  412. $db.execute("UPDATE image SET label = ? WHERE station = ? AND patternID = ? AND imageID = ? AND language = ?", [@p[0].value, @r[0], @r[1], @r[3], @r[4]])
  413. end
  414. end
  415. class GetRealImage < Riddl::Implementation
  416. def response
  417. #split on "." and tacke [0] to allow for e.g. de-at.svg
  418. #would lead to error on de.at.svg //should this be fixed? would also lead to error if everything after last "." would be removed in case of only having de.at
  419. #https://centurio.work/customers/evva/was/server/0/0/images/3/de-at?video=xyz
  420. img = File.read(File.join(File.dirname(__dir__),'images/uploads', @r[0], @r[1], @r[3], @r[4].split(".")[0] + ".svg"))
  421. if false #Currently replacements are done on the client side
  422. if(@p[0].nil?)
  423. puts "Undefined p"
  424. else
  425. if(@p[0].name == "video" && @p[0].value)
  426. xml = Nokogiri.parse img
  427. puts "Width " + xml.xpath("string(//xmlns:image/@width)")
  428. puts "Height " + xml.xpath("string(//xmlns:image/@height)")
  429. #puts "Posi " + xml.xpath("string(//xmlns:clipPath/path/@height)")
  430. puts "Posi " + xml.xpath("string(//xmlns:image/following-sibling::clipPath/@id)")
  431. puts xml.xpath("string(//xmlns:text[starts-with(text(), 'url')])").sub("url=", "")
  432. # text
  433. img = img.sub! "</svg>", '
  434. <g><g transform="translate(562,288),scale(1.35185 1.35185)">
  435. <foreignObject width="480" height="270">
  436. <video width="480" height="270" controls="" autoplay="autoplay" muted="muted" xmlns="http://www.w3.org/1999/xhtml" >
  437. <source src="' + xml.xpath("string(//xmlns:text[starts-with(text(), 'url')])").sub("url=", "") +'" type="video/mp4"/>
  438. </video>
  439. </foreignObject>
  440. </g></g></svg>'
  441. else
  442. @p.each do |item|
  443. unless item.name.nil? || item.value.nil?
  444. img.sub! item.name, item.value
  445. end
  446. end
  447. end
  448. end
  449. end
  450. Riddl::Parameter::Complex.new('theRealImage','image/svg+xml',img)
  451. end
  452. end
  453. class DeleteImage < Riddl::Implementation
  454. def response
  455. #puts "Delete Image"
  456. #puts File.join(File.dirname(__dir__),'images/uploads', @r[0], @r[1], @r[3] , @r[4] + ".svg")
  457. File.delete(File.join(File.dirname(__dir__),'images/uploads', @r[0], @r[1], @r[3] , @r[4] + ".svg")) if File.exist?(File.join(File.dirname(__dir__),'images/uploads', @r[0], @r[1], @r[3] , @r[4] + ".svg"))
  458. $db.execute("DELETE FROM image WHERE station = ? AND patternID = ? AND imageID = ? AND language = ?", [@r[0], @r[1], @r[3], @r[4]])
  459. result = $db.execute "SELECT MAX(imageID) FROM image WHERE station = #{@r[0]} and patternID = #{@r[1]} and language = '#{@r[4]}'"
  460. if(result[0][0] == nil)
  461. id = 0
  462. else
  463. id = result[0][0] +1
  464. end
  465. #reorder if !end gets deleted
  466. if(id == @r[3])
  467. return
  468. else
  469. cur = @r[3].to_i + 1
  470. prev = @r[3].to_i
  471. while cur < id.to_i do
  472. $db.execute("UPDATE image SET imageID = ? WHERE station = ? AND patternID = ? AND imageID = ? AND language = ?", [prev, @r[0], @r[1], cur, @r[4]])
  473. src = File.join(File.dirname(__dir__),'images/uploads', @r[0], @r[1], cur.to_s, @r[4] + ".svg")
  474. target = File.join(File.dirname(__dir__),'images/uploads', @r[0], @r[1], prev.to_s, @r[4] + ".svg")
  475. FileUtils.mv(src, target)
  476. cur+=1
  477. prev += 1
  478. end
  479. end
  480. end
  481. end
  482. class ListSearch < Riddl::Implementation
  483. def response
  484. ret = {
  485. :patternID => "/patternID",
  486. :multi => "/multi",
  487. :juergen => "/juergen"
  488. }
  489. Riddl::Parameter::Complex.new('list','application/json',JSON::pretty_generate(ret))
  490. end
  491. end
  492. class SearchPattern < Riddl::Implementation
  493. def response
  494. iter = @p[0].value.split(".")
  495. searchstring = ""
  496. count = 0
  497. iter.each do |item|
  498. if !item.nil?
  499. if item.to_s != "*"
  500. searchstring = searchstring + " and (P" + count.to_s + "='*' or P" + count.to_s + " = '" + item.to_s + "')"
  501. end
  502. count += 1
  503. end
  504. end
  505. result = $db.execute "SELECT patternID FROM station WHERE station = " + @r[0] + searchstring
  506. if result.length > 0
  507. ret = {
  508. :amount => result.length.to_s,
  509. :ids => result
  510. }
  511. end
  512. Riddl::Parameter::Complex.new('list','application/json',JSON::pretty_generate(ret))
  513. #puts the first found string
  514. #result = $db.execute "SELECT P0,P1,P2,P3,P4,P5,P6,P7,P8,P9,P10 FROM station WHERE station = 0" + searchstring
  515. #puts "found" + result.length.to_s
  516. #result[0].each do |item|
  517. # if !item.nil?
  518. # puts item + "."
  519. # end
  520. #end
  521. end
  522. end
  523. class SearchImages < Riddl::Implementation
  524. def response
  525. iter = @p[0].value.split(".")
  526. searchstring = ""
  527. count = 0
  528. #search = K.*.9.*
  529. #match: = K.5.9.7
  530. #match: = K.5.9.*
  531. #match: = K.*.*.*
  532. #no match: = K.*.8.*
  533. iter.each do |item|
  534. if !item.nil?
  535. if item.to_s != "*"
  536. searchstring = searchstring + " and (P" + count.to_s + "='*' or P" + count.to_s + " = '" + item.to_s + "')"
  537. else
  538. searchstring = searchstring + " and (P" + count.to_s + "!=''" + ")"
  539. end
  540. count += 1
  541. end
  542. end
  543. result = $db.execute "SELECT patternID FROM station WHERE station = " + @r[0] + searchstring
  544. ret = []
  545. if result.length > 0
  546. result.each do |item|
  547. result2 = $db.execute "SELECT DISTINCT station, patternID, imageID FROM image WHERE station = #{@r[0]} and patternID = #{item[0]}"
  548. result2.each do |item2|
  549. ret << item2[0].to_s + "/" + item2[1].to_s + "/" + item2[2].to_s
  550. end
  551. end
  552. end
  553. Riddl::Parameter::Complex.new('list','application/json',JSON::pretty_generate(ret))
  554. end
  555. end
  556. class SearchImagesSingle < Riddl::Implementation
  557. def response
  558. iter = @p[0].value.split(".")
  559. searchstring = ""
  560. count = 0
  561. iter.each do |item|
  562. if !item.nil?
  563. if item.to_s != "*"
  564. searchstring = searchstring + " and (P" + count.to_s + "='*' or P" + count.to_s + " = '" + item.to_s + "')"
  565. else
  566. searchstring = searchstring + " and (P" + count.to_s + "!=''" + ")"
  567. end
  568. count += 1
  569. end
  570. end
  571. result = $db.execute "SELECT patternID FROM station WHERE station = " + @r[0] + searchstring
  572. count = 0
  573. pattern = 0
  574. image = 0
  575. if result.length > 0
  576. result.each do |item|
  577. result2 = $db.execute "SELECT DISTINCT station, patternID, imageID FROM image WHERE station = #{@r[0]} and patternID = #{item[0]}"
  578. result2.each do |item2|
  579. if(count == @r[3].to_i)
  580. puts "Found "
  581. pattern = item2[1]
  582. image = item2[2]
  583. end
  584. count += 1
  585. end
  586. end
  587. end
  588. img = File.read(File.join(File.dirname(__dir__),'images/uploads', @r[0].to_s, pattern.to_s, image.to_s, "de-at.svg"))
  589. Riddl::Parameter::Complex.new('theRealImage','image/svg+xml',img)
  590. end
  591. end
  592. class SearchImages2 < Riddl::Implementation
  593. def response
  594. iter = @p[0].value.split(".")
  595. searchstring = ""
  596. count = 0
  597. #search = K.*.9.*
  598. #match: = K.5.9.7
  599. #match: = K.5.9.*
  600. #match: = K.*.*.*
  601. #no match: = K.*.8.*
  602. iter.each do |item|
  603. if !item.nil?
  604. if item.to_s != "*"
  605. item.to_s.gsub! '_empty_', '/'
  606. searchstring = searchstring + " and (P" + count.to_s + "='*' or P" + count.to_s + " = '" + item.to_s + "')"
  607. else
  608. searchstring = searchstring + " and (P" + count.to_s + "!=''" + ")"
  609. end
  610. count += 1
  611. end
  612. end
  613. result = $db.execute "SELECT patternID FROM station WHERE station = " + @r[0] + searchstring
  614. ret = []
  615. if result.length > 0
  616. result.each do |item|
  617. result2 = $db.execute "SELECT DISTINCT station, patternID, imageID FROM image WHERE station = #{@r[0]} and patternID = #{item[0]}"
  618. result2.each do |item2|
  619. ret << item2[0].to_s + "/" + item2[1].to_s + "/" + item2[2].to_s
  620. end
  621. end
  622. end
  623. Riddl::Parameter::Complex.new('list','application/json',JSON::pretty_generate(ret))
  624. end
  625. end
  626. class SearchImages2Single < Riddl::Implementation
  627. def response
  628. iter = @p[0].value.split(".")
  629. searchstring = ""
  630. count = 0
  631. iter.each do |item|
  632. if !item.nil?
  633. if item.to_s != "*"
  634. searchstring = searchstring + " and (P" + count.to_s + "='*' or P" + count.to_s + " = '" + item.to_s + "')"
  635. else
  636. searchstring = searchstring + " and (P" + count.to_s + "!=''" + ")"
  637. end
  638. count += 1
  639. end
  640. end
  641. result = $db.execute "SELECT patternID FROM station WHERE station = " + @r[0] + searchstring
  642. count = 0
  643. pattern = 0
  644. image = 0
  645. if result.length > 0
  646. result.each do |item|
  647. result2 = $db.execute "SELECT DISTINCT station, patternID, imageID FROM image WHERE station = #{@r[0]} and patternID = #{item[0]}"
  648. result2.each do |item2|
  649. if(count == @r[3].to_i)
  650. puts "Found "
  651. pattern = item2[1]
  652. image = item2[2]
  653. end
  654. count += 1
  655. end
  656. end
  657. end
  658. #https://centurio.work/customers/evva/was/ui/imageReplacement.php?___image___=8/23/0/de-at.svg
  659. img = File.read(File.join(File.dirname(__dir__),'images/uploads', @r[0].to_s, pattern.to_s, image.to_s, "de-at.svg"))
  660. uri = "https://centurio.work/customers/evva/was/ui/imageReplacement.php?___image___=" + @r[0].to_s + "/" + pattern.to_s + "/" + image.to_s + "/de-at.svg"
  661. string = '<html><body style="margin: 0"><iframe style="position: absolute; height: 100%; width: 100%; border: none" src="' + uri +'" title="description"></body></html>'
  662. Riddl::Parameter::Complex.new('html','text/html',string)
  663. end
  664. end
  665. class SearchImages3Single < Riddl::Implementation
  666. def response
  667. iter = @p[0].value.split(".")
  668. searchstring = ""
  669. count = 0
  670. iter.each do |item|
  671. if !item.nil?
  672. if item.to_s != "*"
  673. searchstring = searchstring + " and (P" + count.to_s + "='*' or P" + count.to_s + " = '" + item.to_s + "')"
  674. else
  675. searchstring = searchstring + " and (P" + count.to_s + "!=''" + ")"
  676. end
  677. count += 1
  678. end
  679. end
  680. result = $db.execute "SELECT patternID FROM station WHERE station = " + @r[0] + searchstring
  681. count = 0
  682. pattern = 0
  683. image = 0
  684. builder = Nokogiri::XML::Builder.new do |xml|
  685. xml.image(:id => @r[3].to_s){
  686. if result.length > 0
  687. result.each do |item|
  688. result2 = $db.execute "SELECT DISTINCT station, patternID, imageID FROM image WHERE station = #{@r[0]} and patternID = #{item[0]}"
  689. result2.each do |item2|
  690. if(count == @r[3].to_i)
  691. result3 = $db.execute "SELECT DISTINCT station, patternID, imageID, language, label FROM image WHERE station = #{@r[0]} and patternID = #{item[0]} and imageID =#{item2[2]}"
  692. result3.each do |item3|
  693. uri = "https://centurio.work/customers/evva/was/ui/imageReplacement.php?___image___=" + @r[0].to_s + "/" + item3[1].to_s + "/" + item3[2].to_s + "/" + item3[3].to_s + ".svg"
  694. @p.each_with_index do |item, index|
  695. if index != 0
  696. uri += "&" + item.name.to_s + "=" + item.value.to_s
  697. end
  698. end
  699. xml.variant(:lang => item3[3].to_s, :label => item3[4].to_s){
  700. xml.text(uri)
  701. }
  702. end
  703. end
  704. count += 1
  705. end
  706. end
  707. end
  708. }
  709. end
  710. #https://centurio.work/customers/evva/was/ui/imageReplacement.php?___image___=8/23/0/de-at.svg
  711. #puts builder.to_xml
  712. Riddl::Parameter::Complex.new('image','application/xml',builder.to_xml)
  713. end
  714. end
  715. class SearchErrors < Riddl::Implementation
  716. def response
  717. iter = @p[0].value.split(".")
  718. searchstring = ""
  719. count = 0
  720. #search = K.*.9.*
  721. #match: = K.5.9.7
  722. #match: = K.5.9.*
  723. #match: = K.*.*.*
  724. #no match: = K.*.8.*
  725. iter.each do |item|
  726. if !item.nil?
  727. if item.to_s != "*"
  728. searchstring = searchstring + " and (P" + count.to_s + "='*' or P" + count.to_s + " = '" + item.to_s + "')"
  729. else
  730. searchstring = searchstring + " and (P" + count.to_s + "!=''" + ")"
  731. end
  732. count += 1
  733. end
  734. end
  735. result = $db.execute "SELECT patternID FROM station WHERE station = " + @r[0] + searchstring
  736. ret = []
  737. if result.length > 0
  738. result.each do |item|
  739. result2 = $db.execute "SELECT DISTINCT error FROM error WHERE station = #{@r[0]} and patternID = #{item[0]}"
  740. result2.each do |item2|
  741. ret << item2[0].to_s
  742. end
  743. end
  744. end
  745. ret = ret.uniq
  746. builder = Nokogiri::XML::Builder.new do |xml|
  747. xml.reason{
  748. if result.length > 0
  749. ret.each do |item|
  750. xml.reason(:lang => "de-at"){
  751. xml.text(item)
  752. }
  753. end
  754. end
  755. }
  756. end
  757. Riddl::Parameter::Complex.new('errors','application/xml',builder.to_xml)
  758. end
  759. end
  760. class SearchImagesReverse < Riddl::Implementation
  761. def response
  762. iter = @p[0].value.split(".")
  763. searchstring = ""
  764. count = 0
  765. #search = K.*.9.*
  766. #match: = K.5.9.7
  767. #match: = K.5.9.*
  768. #match: = K.*.*.*
  769. #no match: = K.*.8.*
  770. iter.each do |item|
  771. if !item.nil?
  772. if item.to_s != "*"
  773. searchstring = searchstring + " and (P" + count.to_s + "='*' or P" + count.to_s + " = '" + item.to_s + "')"
  774. else
  775. searchstring = searchstring + " and (P" + count.to_s + "!=''" + ")"
  776. end
  777. count += 1
  778. end
  779. end
  780. result = $db.execute "SELECT patternID FROM station WHERE station = " + @r[0] + searchstring
  781. ret = []
  782. if result.length > 0
  783. result.each do |item|
  784. result2 = $db.execute "SELECT DISTINCT station, patternID, imageID FROM image WHERE station = #{@r[0]} and patternID = #{item[0]}"
  785. result2.each do |item2|
  786. ret << item2[0].to_s + "/" + item2[1].to_s + "/" + item2[2].to_s
  787. end
  788. end
  789. end
  790. Riddl::Parameter::Complex.new('list','application/json',JSON::pretty_generate(ret))
  791. end
  792. end
  793. def createDB(opts)
  794. $db.execute("PRAGMA foreign_keys=ON");
  795. $db.execute "CREATE TABLE IF NOT EXISTS stations(station INT, name VARCHAR(20), PRIMARY KEY(station))"
  796. opts[:appconf]["stations"].each { |n|
  797. CreateStation::createDB(n)
  798. }
  799. #vor DB struktur umstellung
  800. #$db.execute "CREATE TABLE IF NOT EXISTS station(station INT, patternID INT, pattern CHARACTER(256), description TEXT, date TEXT, P0 VARCHAR(10), P1 VARCHAR(10), P2 VARCHAR(10), P3 VARCHAR(10), P4 VARCHAR(10), P5 VARCHAR(10), P6 VARCHAR(10), P7 VARCHAR(10), P8 VARCHAR(10), P9 VARCHAR(10), P10 VARCHAR(10), PRIMARY KEY(station,patternID), UNIQUE(station, patternID))"
  801. #$db.execute "CREATE TABLE IF NOT EXISTS error(station INT, patternID INT, error TEXT, FOREIGN KEY(station, patternID) REFERENCES station(station, patternID) ON DELETE CASCADE)"
  802. #$db.execute "CREATE TABLE IF NOT EXISTS replacements(station INT, patternID INT, abbreviation TEXT, url TEXT, ordering INT, FOREIGN KEY(station, patternID) REFERENCES station(station, patternID) ON DELETE CASCADE)"
  803. #$db.execute "CREATE TABLE IF NOT EXISTS image(station INT, patternID INT, imageID INT, language CHARACTER(20), label TEXT, FOREIGN KEY(station, patternID) REFERENCES station(station, patternID) ON DELETE CASCADE,PRIMARY KEY(station,patternID,imageID,language))"
  804. $db.execute "CREATE TABLE IF NOT EXISTS stationpattern(station INT, internalID INT, description TEXT, date TEXT, PRIMARY KEY(internalID), UNIQUE(internalID))"
  805. $db.execute "CREATE TABLE IF NOT EXISTS pattern(internalID INT, type CHARACTER(20), value CHARACTER(20), FOREIGN KEY(internalID) REFERENCES stationpattern(internalID) ON DELETE CASCADE)"
  806. $db.execute "CREATE TABLE IF NOT EXISTS error(internalID INT, error TEXT, FOREIGN KEY(internalID) REFERENCES stationpattern(internalID) ON DELETE CASCADE)"
  807. $db.execute "CREATE TABLE IF NOT EXISTS replacements(internalID INT, abbreviation TEXT, url TEXT, ordering INT, FOREIGN KEY(internalID) REFERENCES stationpattern(internalID) ON DELETE CASCADE)"
  808. $db.execute "CREATE TABLE IF NOT EXISTS image(internalID INT, imageID INT, language CHARACTER(20), label TEXT, FOREIGN KEY(internalID) REFERENCES stationpattern(internalID) ON DELETE CASCADE)"
  809. end
  810. server = Riddl::Server.new(File.join(__dir__,'/was.xml'), :host => 'localhost') do |opts|
  811. accessible_description true
  812. cross_site_xhr true
  813. createDB(opts)
  814. # opts[:appconf]
  815. on resource do
  816. run GetStations if get
  817. run CreateStation if post
  818. on resource 'cutimage' do
  819. run CutImage if get
  820. #should add deleteReplacement
  821. end
  822. on resource 'json' do
  823. run GetJsonFiles if get
  824. on resource '.*' do
  825. run SaveJson if put 'list'
  826. run GetJson if get
  827. end
  828. #should add deleteReplacement
  829. end
  830. on resource '\d+' do
  831. run GetStation if get
  832. run CreatePattern if post 'pattern'
  833. on resource '\d+' do
  834. run GetPattern if get
  835. run UpdatePattern if put 'pattern'
  836. run DeletePattern if delete
  837. run DuplicatePattern if put 'patternID'
  838. on resource 'error' do
  839. run SaveError if put 'error'
  840. run GetError if get
  841. #should add deleteerror
  842. end
  843. on resource 'replacement' do
  844. run SaveReplacement if put 'replacement'
  845. run GetReplacement if get
  846. #should add deleteReplacement
  847. end
  848. on resource 'images' do
  849. run GetImages if get
  850. #run CreateImage if post #'image'
  851. run AddExternalImage if post 'externalImage'
  852. run UploadImage if post
  853. on resource 'reorder' do
  854. on resource do
  855. run ReorderImages if put 'orderlist'
  856. end
  857. end
  858. on resource '\d+' do
  859. run GetImage if get
  860. on resource do
  861. run UpdateImageLabel if put 'label'
  862. run GetRealImage if get
  863. run DeleteImage if delete
  864. end
  865. end
  866. end
  867. end
  868. on resource 'search' do
  869. run ListSearch if get
  870. on resource 'patternID' do
  871. run SearchPattern if get
  872. end
  873. on resource 'images' do
  874. run SearchImages if get
  875. on resource '\d+' do
  876. run SearchImagesSingle if get
  877. end
  878. end
  879. on resource 'imagesServerSide' do
  880. run SearchImages2 if get
  881. on resource '\d+' do
  882. run SearchImages2Single if get
  883. end
  884. end
  885. on resource 'imagesWIZ' do
  886. run SearchImages2 if get
  887. on resource '\d+' do
  888. run SearchImages3Single if get
  889. end
  890. end
  891. on resource 'errorsWIZ' do
  892. run SearchErrors if get
  893. end
  894. on resource 'imagesReverse' do
  895. run SearchImagesReverse if get
  896. end
  897. end
  898. end
  899. end
  900. end.loop!