A tumblelog CMS built on AJAX, PHP and MySQL.

functions.php 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. <?php
  2. if(!defined('entry') || !entry) die('Not a valid page');
  3. /* ===========================
  4. gelato CMS - A PHP based tumblelog CMS
  5. development version
  6. http://www.gelatocms.com/
  7. gelato CMS is a free software licensed under the GPL 2.0
  8. Copyright (C) 2007 by Pedro Santana <pecesama at gmail dot com>
  9. =========================== */
  10. ?>
  11. <?php
  12. function version() {
  13. return "0.90";
  14. }
  15. function beginsWith($str, $sub) {
  16. return (strpos($str, $sub) === 0);
  17. }
  18. function endsWith($str, $sub) {
  19. return (substr($str, strlen($str) - strlen($sub)) == $sub);
  20. }
  21. function getFileName($fileUrl) {
  22. $path = explode('/', $fileUrl);
  23. return $path[count($path)-1];
  24. }
  25. function isMP3($fileUrl) {
  26. if (endsWith($fileUrl, ".mp3")) {
  27. return true;
  28. } else {
  29. return false;
  30. }
  31. }
  32. function getMP3File($remoteFileName) {
  33. if (isMP3($remoteFileName)) {
  34. if (getFile($remoteFileName)) {
  35. return true;
  36. } else {
  37. return false;
  38. }
  39. } elseif (isGoEar($remoteFileName)) {
  40. return true;
  41. } else {
  42. return false;
  43. }
  44. }
  45. function getGoEarCode($songUrl) {
  46. $pos = strpos($songUrl, "?v=");
  47. $lon = strlen($songUrl);
  48. $str = substr($songUrl, $pos + 3, $lon);
  49. return $str;
  50. }
  51. function isGoEar($songUrl) {
  52. if (beginsWith($songUrl, "http://www.goear.com/listen.php?v="))
  53. return true;
  54. else
  55. return false;
  56. }
  57. function isImageFile($photoUrl) {
  58. if (endsWith($photoUrl, ".jpg")) { return true; }
  59. elseif (endsWith($photoUrl, ".gif")) { return true; }
  60. elseif (endsWith($photoUrl, ".png")) { return true; }
  61. else { return false; }
  62. }
  63. function getPhotoFile($remoteFileName) {
  64. if (isImageFile($remoteFileName)) {
  65. if (getFile($remoteFileName)) {
  66. return true;
  67. } else {
  68. return false;
  69. }
  70. } else {
  71. return false;
  72. }
  73. }
  74. function getFile($remoteFileName) {
  75. $fileName = "../uploads/".sanitizeName($remoteFileName);
  76. $str = _file_get_contents($remoteFileName);
  77. if (!$handle = fopen($fileName, 'w')) {
  78. return false;
  79. }
  80. if (fwrite($handle, $str) === FALSE) {
  81. return false;
  82. }
  83. fclose($handle);
  84. return true;
  85. }
  86. function isVimeoVideo($videoUrl) {
  87. if (beginsWith($videoUrl, "http://vimeo.com/clip:") || beginsWith($videoUrl, "http://www.vimeo.com/clip:"))
  88. return true;
  89. else
  90. return false;
  91. }
  92. function getVimeoVideoUrl($videoUrl) {
  93. return array_pop(explode("clip:",$videoUrl));
  94. }
  95. function isYoutubeVideo($videoUrl) {
  96. if (beginsWith($videoUrl, "http://youtube.com/watch?v=") || beginsWith($videoUrl, "http://www.youtube.com/watch?v="))
  97. return true;
  98. else
  99. return false;
  100. }
  101. function getYoutubeVideoUrl($videoUrl) {
  102. $params = explode("?v=", $videoUrl);
  103. $params2 = explode("&",$params[1]);
  104. return $params2[0];
  105. }
  106. function isVideo($url) {
  107. if (isYoutubeVideo($url)) { return true; }
  108. elseif (isVimeoVideo($url)) { return true; }
  109. else { return false; }
  110. }
  111. function sendMail($to, $title, $body, $from) {
  112. $rp = trim($from);
  113. $org = "gelato CMS";
  114. $mailer = "gelato CMS Mailer";
  115. $head = '';
  116. $head .= "Content-Type: text/html \r\n";
  117. $head .= "Date: ". date('r'). " \r\n";
  118. $head .= "Return-Path: $rp \r\n";
  119. $head .= "From: $from \r\n";
  120. $head .= "Sender: $from \r\n";
  121. $head .= "Reply-To: $from \r\n";
  122. $head .= "Organization: $org \r\n";
  123. $head .= "X-Sender: $from \r\n";
  124. $head .= "X-Priority: 3 \r\n";
  125. $head .= "X-Mailer: $mailer \r\n";
  126. $body = str_replace("\r\n", "\n", $body);
  127. $body = str_replace("\n", "\r\n", $body);
  128. return @mail($to, $title, $body, $head);
  129. }
  130. function getThemes() {
  131. $themes_dir = "themes";
  132. $dirs = array();
  133. $path = getcwd();
  134. $dir = (substr(PHP_OS, 0, 3) == 'WIN') ? $path."\\".$themes_dir : $path."/".$themes_dir;
  135. $dir = str_replace("admin\\", "", $dir);
  136. $dir = str_replace("admin/", "", $dir);
  137. $handle = opendir($dir);
  138. $i=0;
  139. while($filename = readdir($handle)) {
  140. if($filename != "." && $filename != "..") {
  141. $dirs[$i]=trim($filename);
  142. $i++;
  143. }
  144. }
  145. closedir($handle);
  146. return $dirs;
  147. }
  148. function sanitizeName($name) {
  149. $name = preg_replace('/[\'"]/', '', $name);
  150. $name = preg_replace('/[^a-zA-Z0-9]+/', '-', $name);
  151. $name = trim($name, '-');
  152. $name = strtolower($name);
  153. //HACK: We need to rework the regular expression to allow the dot
  154. $ext = substr($name, strlen($name)-3, strlen($name));
  155. $body = substr($name, 0, strlen($name)-4);
  156. $name = $body.".".$ext;
  157. return $name;
  158. }
  159. function _file_get_contents($path) {
  160. // Modified function from:
  161. // http://work.dokoku.net/Anieto2k/_file_get_contents.phps
  162. // http://www.anieto2k.com/2007/02/09/file_get_contents-y-dreamhost/
  163. if (!preg_match('/^http/i', $path)) {
  164. if ($fp = fopen($path, 'r')) {
  165. return fread($fp, 1024);
  166. } else {
  167. return false;
  168. }
  169. } else {
  170. if (extension_loaded('curl') && version_compare(get_curl_version(), '7.10.5', '>=')) {
  171. $ch = curl_init();
  172. $timeout = 5;
  173. curl_setopt ($ch, CURLOPT_URL, $path);
  174. curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
  175. curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  176. $file_contents = curl_exec($ch);
  177. curl_close($ch);
  178. if (is_string($file_contents)) {
  179. return $file_contents;
  180. } else {
  181. return false;
  182. }
  183. } else {
  184. $data = parse_url($path);
  185. if (!$data['host'] || $data['scheme'] != "http") {
  186. return false;
  187. }
  188. $f = @fsockopen($data['host'], ($data['port']) ? $data['port'] : 80, $e1, $e2, 3);
  189. if (!$f) {
  190. return false;
  191. }
  192. $q = "GET " . $data['path'] . (isset($data['query'])?'?'.$data['query']:'') . " HTTP/1.1\r\n";
  193. $q .= "Host: " . $data['host'] . "\r\n";
  194. $q .= "Connection: close\r\n";
  195. $q .= "Referer: http://www.gelatocms.com/\r\n\r\n";
  196. $recv = "";
  197. fwrite($f, $q);
  198. while (!feof($f)) {
  199. $recv .= fread($f, 1024);
  200. }
  201. $request = $q;
  202. $response = substr($recv, 0, strpos($recv, "\r\n\r\n"));
  203. $body = substr($recv, strpos($recv, "\r\n\r\n") + 4);
  204. if (preg_match('/http\/1\\.[0|1] ([0-9]{3})/i', $response, $res)) {
  205. if ($res[1][0] != "2") {
  206. return false;
  207. }
  208. } else {
  209. return false;
  210. }
  211. if (preg_match('/transfer-encoding:\s*chunked/i', $response)) {
  212. $tmp_body = $body;
  213. $new = "";
  214. $exit = false;
  215. while (!$exit) {
  216. if (preg_match('/^([0-9a-f]+).*?\r\n/i', $tmp_body, $res)) {
  217. $len = hexdec($res[1]);
  218. if ($len == "0") {
  219. $exit = true;
  220. break;
  221. }
  222. $new .= substr($tmp_body, strlen($res[0]), $len);
  223. $tmp_body = substr($tmp_body, strlen($res[0]) + $len + strlen("\r\n"));
  224. } else {
  225. $exit = true;
  226. }
  227. }
  228. $body = $new;
  229. }
  230. return $body;
  231. }
  232. }
  233. }
  234. function get_curl_version() {
  235. $curl = 0;
  236. if (is_array(curl_version())) {
  237. $curl = curl_version();
  238. $curl = $curl['version'];
  239. } else {
  240. $curl = curl_version();
  241. $curl = explode(' ', $curl);
  242. $curl = explode('/', $curl[0]);
  243. $curl = $curl[1];
  244. }
  245. return $curl;
  246. }
  247. function transform_offset($offset){
  248. $sp = strpos($offset , ".")? explode("." , $offset) : false;
  249. if(is_array($sp)){
  250. $minutes = strval($sp[1]);
  251. $off_h = $sp[0]*3600;
  252. $off_m = (($minutes*60)/100)*60;
  253. $off = $off_h+$off_m;
  254. } else {
  255. $off = ($offset*3600);
  256. }
  257. return $off;
  258. }
  259. function getLangs() {
  260. $langs_dir = "languages";
  261. $dirs = array();
  262. $path = getcwd();
  263. $dir = (substr(PHP_OS, 0, 3) == 'WIN') ? $path."\\".$langs_dir : $path."/".$langs_dir;
  264. $dir = str_replace("admin\\", "", $dir);
  265. $dir = str_replace("admin/", "", $dir);
  266. $i=0;
  267. $cls_lang_dir = @ dir($dir);
  268. while (($directory = $cls_lang_dir->read()) !== false) {
  269. if($directory != "." && $directory != "..") {
  270. $dir2 = (substr(PHP_OS, 0, 3) == 'WIN') ? $path."\\".$langs_dir."\\".$directory : $path."/".$langs_dir."/".$directory;
  271. $dir2 = str_replace("admin\\", "", $dir2);
  272. $dir2 = str_replace("admin/", "", $dir2);
  273. if(is_dir($dir2)){
  274. $cls_lang_dir2 = @ dir($dir2);
  275. while (($directory2 = $cls_lang_dir2->read()) !== false) {
  276. if($directory2 != "." && $directory2 != "..") {
  277. if (preg_match('|^\.+$|', $directory2)){
  278. continue;
  279. }
  280. if (preg_match('|\.mo$|', $directory2)){
  281. if(!in_array($directory2,$dirs)){
  282. $dirs[$i]=trim($directory);
  283. $i++;
  284. }
  285. }
  286. }
  287. }
  288. }
  289. }
  290. }
  291. $dirs = array_unique($dirs);
  292. return $dirs;
  293. }
  294. function sql_escape($value) {
  295. if(get_magic_quotes_gpc()) {
  296. $value = stripslashes($value);
  297. }
  298. if( function_exists("mysql_real_escape_string")) {
  299. $value = mysql_real_escape_string($value);
  300. } else {
  301. $value = addslashes($value);
  302. }
  303. return $value;
  304. }
  305. ?>