die("Fail to connect db"); } # 查询IP的上次提交时间,限制1分钟提交一次代码. $sql = "SELECT count(`ip`) FROM " . TB_LOGIN . " WHERE `ip`=? "; if (exec_sql($db, $sql, $ip) != 0) { $sql = "SELECT TIMESTAMPDIFF(SECOND,`last`,now()) AS ltime FROM " . TB_LOGIN . " WHERE `ip`=?"; if ($time_limit && ($dif = exec_sql($db, $sql, $ip)) <= 60) { die("Retry 1 minute later."); } } $sql = "INSERT INTO " . TB_LOGIN . " SET `ip`=? ON DUPLICATE KEY UPDATE `last`=now(),`n`=`n`+1 "; $res = $db->prepare($sql); $res->bind_param("s", $ip); $res->execute(); # 代码存入数据库 $uuid = uuid4(); $file = gen_path($uuid); #insert code $sql = "INSERT INTO " . TB_CODE . "(`num`,`op`,`src`,`img`,`lang`) VALUES(?,?,?,?,?)"; $res = $db->prepare($sql); $res->bind_param("sssss", $uuid, $ip, $code, $file, $lang); $res->execute(); # 跳转到代码显示页面,后台生成图片. $url = "Location:show_code.php?id={$uuid}"; header($url); gen_pic($code, $uuid, $file); exit(0); # 根据当前时间创建目录,生成 id.png 的路径 function gen_path($id) { $dir = 'pics/' . date('Y/m/d/H/i', time());
public function render($mode, Doku_Renderer $renderer, $data) { global $ID, $conf; if ($mode != 'xhtml') { return false; } // check .nlogo file read permission $src = $data['src']; /* testing: disable filetype checking. Does this allow remote download of file, eg. from github? [Rik, 2016-11-25] resolve_mediaid(getNS($ID),$src,$exists); if(auth_quickaclcheck(getNS($src).':X') < AUTH_READ){ // auth_quickaclcheck() mimicked from http://xref.dokuwiki.org/reference/dokuwiki/_functions/checkfilestatus.html $renderer->doc .= '<div class="error">NetLogo: File not allowed: ' . $src . '</div>'; return true; } $src = mediaFN($src); if (!$exists) { $renderer->doc .= '<div class="error">NetLogo: File not found: ' . $src . '</div>'; return true; } */ // parse file to get contents if (is_null($data['width']) || is_null($data['height']) || $data['do'] === 'code' || $data['do'] === 'info' || $data['do'] === 'mdinfo') { $nlogo = file_get_contents($src); $nlogoparts = explode('@#$#@#$#@', $nlogo); /* [0] => code [1] => interface [2] => info [3] => turtle shapes [4] => NetLogo version [5] => preview commands [6] => system dynamics modeler [7] => BehaviorSpace [8] => HubNet client [9] => link shapes [10] =>model settings [11] =>reserved by Michelle [12] => (empty) */ // show code if ($data['do'] === 'code') { $renderer->doc .= p_render('xhtml', p_get_instructions('<code netlogo>' . $nlogoparts[0] . '</code>'), $info); return true; } // show info if ($data['do'] === 'info') { $renderer->doc .= p_render('xhtml', p_get_instructions($nlogoparts[2]), $info); return true; } // show info wrapped in '<markdown>...</markdown>' tags if ($data['do'] === 'mdinfo') { $renderer->doc .= p_render('xhtml', p_get_instructions('<markdown>' . $nlogoparts[2] . '</markdown>'), $info); return true; } // width & height? if (is_null($data['width']) || is_null($data['height'])) { // store x,y coordinates of bottom right corner in $rightbottom[2] & $rightbottom[3], respectively preg_match_all('/(^|\\n)\\n[A-Z\\-]+\\n[0-9]+\\n[0-9]+\\n([0-9]+)\\n([0-9]+)\\n/', $nlogoparts[1], $rightbottom); if (is_null($data['width'])) { $data['width'] = max($rightbottom[2]) + 50; } if (is_null($data['height'])) { $data['height'] = max($rightbottom[3]) + 300; } } } // download libraries? Todo: move root url to config option $urlroot = 'http://ccl.northwestern.edu/netlogo/'; // $src is currently realpath. Turn into relative path from DokuWiki media folder // temporarily disabled while testing remote urls [Rik, 2016-11-26] //$src = relativePath(DOKU_INC.'data/media/',$src); // Will pass token to servefile.php to authorize. First generate secret uuid if not found. $uuidfile = 'data/tmp/plugin_netlogo_uuid'; if (!file_exists($uuidfile)) { if (!($handle = fopen($uuidfile, 'w'))) { $renderer->doc .= '<div class="error">NetLogo: Cannot create UUID ' . $uuidfile . '</div>'; return true; } // Write uuid to our opened file. if (fwrite($handle, uuid4()) === FALSE) { $renderer->doc .= '<div class="error">NetLogo: Cannot write UUID to ' . $uuidfile . '</div>'; return true; } fclose($handle); } // read uuid from file $uuid = file_get_contents($uuidfile); // when should the servefile.php link expire? $expires = time() + min(max($conf['cachetime'], 60), 3600); // expires in cachetime, but no less than 1 minute or more than 1 hour // disable caching of this page to ensure parameters passed to servefile.php are always fresh [Rik, 2012-10-06] $renderer->info['cache'] = false; // generate token for servefile.php to authorize, use $uuid as salt. servefile.php must be able to generate same token or it won't serve file. $token = hash('sha256', $uuid . $src . $expires); // replace crypt() for more than first 8 chars [Rik, 2012-10-06] // special handling for center $pcenter = false; if (!is_null($data['align']) && $data['align'] === 'center') { $pcenter = true; $data['align'] = null; } /* old servefile method: '"lib/plugins/netlogo/inc/servefile.php?src='.urlencode($src).'&expires='.$expires.'&token='.urlencode($token).'"' may still be needed because fetch.php throws Cross-Origin Resource Sharing error [Rik, 2016-11-27] */ if ($pcenter) { $renderer->doc .= '<p align="center">'; } $renderer->doc .= '<iframe title="" src="http://netlogoweb.org/web?' . $src . '" style="width:' . $data['width'] . 'px; height:' . $data['height'] . 'px"></iframe>'; if ($pcenter) { $renderer->doc .= '</p>'; } return true; }
/** * Generate a HashID. */ public static function bootHasHashid() { static::creating(function ($model) { $model->{static::$hashidColumn} = uuid4(); }); }